1 /**
2  * Compiler implementation of the
3  * $(LINK2 http://www.dlang.org, D programming language).
4  *
5  * Copyright:   Copyright (C) 1986-1998 by Symantec
6  *              Copyright (C) 2000-2020 by The D Language Foundation, All Rights Reserved
7  * Authors:     $(LINK2 http://www.digitalmars.com, Walter Bright)
8  * License:     Distributed under the Boost Software License, Version 1.0.
9  *              http://www.boost.org/LICENSE_1_0.txt
10  * Source:      https://github.com/dlang/dmd/blob/master/src/dmd/backend/goh.d
11  */
12 
13 module dmd.backend.goh;
14 
15 import core.stdc.stdio;
16 import core.stdc.stdlib;
17 import core.stdc.string;
18 import core.stdc.time;
19 
20 import dmd.backend.cc;
21 import dmd.backend.cdef;
22 import dmd.backend.oper;
23 import dmd.backend.global;
24 import dmd.backend.el;
25 import dmd.backend.symtab;
26 import dmd.backend.ty;
27 import dmd.backend.type;
28 
29 import dmd.backend.barray;
30 import dmd.backend.dlist;
31 import dmd.backend.dvec;
32 
33 extern (C++):
34 nothrow:
35 
36 /***************************************
37  * Bit masks for various optimizations.
38  */
39 
40 alias mftype = uint;        /* a type big enough for all the flags  */
41 enum
42 {
43     MFdc    = 1,               // dead code
44     MFda    = 2,               // dead assignments
45     MFdv    = 4,               // dead variables
46     MFreg   = 8,               // register variables
47     MFcse   = 0x10,            // global common subexpressions
48     MFvbe   = 0x20,            // very busy expressions
49     MFtime  = 0x40,            // favor time (speed) over space
50     MFli    = 0x80,            // loop invariants
51     MFliv   = 0x100,           // loop induction variables
52     MFcp    = 0x200,           // copy propagation
53     MFcnp   = 0x400,           // constant propagation
54     MFloop  = 0x800,           // loop till no more changes
55     MFtree  = 0x1000,          // optelem (tree optimization)
56     MFlocal = 0x2000,          // localize expressions
57     MFall   = 0xFFFF,          // do everything
58 }
59 
60 /**********************************
61  * Definition elem vector, used for reaching definitions.
62  */
63 
64 struct DefNode
65 {
66     elem    *DNelem;        // pointer to definition elem
67     block   *DNblock;       // pointer to block that the elem is in
68     vec_t    DNunambig;     // vector of unambiguous definitions
69 }
70 
71 /* Global Variables */
72 //extern __gshared uint[] optab;
73 
74 /* Global Optimizer variables
75  */
76 struct GlobalOptimizer
77 {
78     mftype mfoptim;
79     uint changes;       // # of optimizations performed
80 
81     Barray!DefNode defnod;    // array of definition elems
82     uint unambigtop;    // number of unambiguous defininitions ( <= deftop )
83 
84     Barray!(vec_base_t) dnunambig;  // pool to allocate DNunambig vectors from
85 
86     Barray!(elem*) expnod;      // array of expression elems
87     uint exptop;        // top of expnod[]
88     Barray!(block*) expblk;     // parallel array of block pointers
89 
90     vec_t defkill;      // vector of AEs killed by an ambiguous definition
91     vec_t starkill;     // vector of AEs killed by a definition of something that somebody could be
92                         // pointing to
93     vec_t vptrkill;     // vector of AEs killed by an access
94 }
95 
96 extern __gshared GlobalOptimizer go;
97 
98 /* gdag.c */
99 void builddags();
100 void boolopt();
101 void opt_arraybounds();
102 
103 /* gflow.c */
104 void flowrd();
105 void flowlv();
106 void flowvbe();
107 void flowcp();
108 void flowae();
109 void genkillae();
110 void flowarraybounds();
111 int ae_field_affect(elem *lvalue,elem *e);
112 
113 /* glocal.c */
114 void localize();
115 
116 /* gloop.c */
117 int blockinit();
118 void compdom();
119 void loopopt();
120 extern (C) void fillInDNunambig(vec_t v, elem *e);
121 extern (C) void updaterd(elem *n,vec_t GEN,vec_t KILL);
122 
123 /* gother.c */
124 void rd_arraybounds();
125 void rd_free();
126 void constprop();
127 void copyprop();
128 void rmdeadass();
129 void elimass(elem *);
130 void deadvar();
131 void verybusyexp();
132 void listrds(vec_t, elem *, vec_t, Barray!(elem*)*);
133 
134 /* gslice.c */
135 void sliceStructs(ref symtab_t symtab, block* startblock);
136