3 Xinclude <llvm/CodeGen/InstrForest.h>
5 typedef InstrTreeNode* NODEPTR_TYPE;
6 Xdefine OP_LABEL(p) ((p)->opLabel)
7 Xdefine LEFT_CHILD(p) ((p)->LeftChild)
8 Xdefine RIGHT_CHILD(p) ((p)->RightChild)
9 Xdefine STATE_LABEL(p) ((p)->state)
12 // Get definitions for various instruction values that we will need...
13 #define HANDLE_TERM_INST(N, OPC, CLASS) Ydefine OPC##OPCODE N
14 #define HANDLE_UNARY_INST(N, OPC, CLASS) Ydefine OPC##OPCODE N
15 #define HANDLE_BINARY_INST(N, OPC, CLASS) Ydefine OPC##OPCODE N
16 #define HANDLE_MEMORY_INST(N, OPC, CLASS) Ydefine OPC##OPCODE N
17 #define HANDLE_OTHER_INST(N, OPC, CLASS) Ydefine OPC##OPCODE N
19 #include "llvm/Instruction.def"
25 %term Ret=RetOPCODE /* return void from a function */
26 %term RetValue=101 /* return a value from a function */
27 %term BrUncond=BrOPCODE
29 %term Switch=SwitchOPCODE
39 /* Use the next 4 to distinguish bitwise operators from
40 * logical operators. This is no longer used for Sparc,
41 * but may be useful for other target machines.
42 * The last one is the bitwise Not(val) == XOR val, 11..1.
43 * Note that it is also a binary operator, not unary.
49 /* The next one is the boolean Not(val) == bool XOR val, true
50 * Note that it is also a binary operator, not unary.
54 %term SetCC=114 /* use this to match all SetCC instructions */
61 %term Malloc=MallocOPCODE
63 %term Alloca=AllocaOPCODE
64 %term AllocaN=122 /* alloca with arg N */
66 %term Store=StoreOPCODE
67 %term GetElemPtr=GetElementPtrOPCODE
68 %term GetElemPtrIdx=125 /* getElemPtr with index vector */
70 %term Phi=PHINodeOPCODE
72 %term Cast=CastOPCODE /* cast that will be ignored. others are made explicit */
90 /* 30...46 are unused */
92 * The foll. values should match the constants in InstrForest.h
98 /* 50+i is a variant of i, as defined above */
102 /*-----------------------------------------------------------------------*
103 * The productions of the grammar.
104 * Note that all chain rules are numbered 101 and above.
105 * Also, a special case of production X is numbered 100+X, 200+X, etc.
106 * The cost of a 1-cycle operation is represented as 10, to allow
107 * finer comparisons of costs (effectively, fractions of 1/10).
108 *-----------------------------------------------------------------------*/
111 * The top-level statements
114 stmt: RetValue(reg) = 2 (30);
115 stmt: Store(reg,reg) = 3 (10);
116 stmt: Store(reg,ptrreg) = 4 (10);
117 stmt: BrUncond = 5 (20);
118 stmt: BrCond(setCC) = 6 (20); /* branch on cond. code */
119 stmt: BrCond(setCCconst) = 206 (10); /* may save one instruction */
120 stmt: BrCond(reg) = 8 (20); /* may avoid an extra instr */
121 stmt: BrCond(Constant) = 208 (20); /* may avoid an extra instr */
122 stmt: Switch(reg) = 9 (30); /* cost = load + branch */
127 * List node used for nodes with more than 2 children
129 reg: VRegList(reg,reg) = 10 (0);
132 * Special case non-terminals to help combine unary instructions.
133 * Eg1: zdouble <- todouble(xfloat) * todouble(yfloat)
134 * Eg2: c <- a AND (NOT b).
135 * Note that the costs are counted for the special non-terminals here,
136 * and should not be counted again for the reg productions later.
138 not: Not(reg,reg) = 21 (10);
139 tobool: ToBoolTy(reg) = 22 (10);
140 toubyte: ToUByteTy(reg) = 23 (10);
141 tosbyte: ToSByteTy(reg) = 24 (10);
142 toushort: ToUShortTy(reg) = 25 (10);
143 toshort: ToShortTy(reg) = 26 (10);
144 touint: ToUIntTy(reg) = 27 (10);
145 toint: ToIntTy(reg) = 28 (10);
146 toulong: ToULongTy(reg) = 29 (10);
147 tolong: ToLongTy(reg) = 30 (10);
148 tofloat: ToFloatTy(reg) = 31 (10);
149 todouble: ToDoubleTy(reg) = 32 (10);
150 todoubleConst: ToDoubleTy(Constant) = 232 (10);
153 * All the ways to produce a boolean value (Not and ToBoolTy are above):
154 * -- boolean operators: Not, And, Or, ..., ToBoolTy, SetCC
155 * -- an existing boolean register not in the same tree
156 * -- a boolean constant
158 * For And, Or, Xor, we add special cases for when:
159 * (a) one operand is a constant.
160 * (b) one operand is a NOT, to use the ANDN, ORN, and XORN instrns.
161 * We do not need the cases when both operands are constant
162 * because constant folding should take care of that beforehand.
164 reg: And(reg,reg) = 38 (10);
165 reg: And(reg,not) = 138 (0); /* cost is counted for not */
166 reg: And(reg,Constant) = 238 (10);
167 reg: Or (reg,reg) = 39 (10);
168 reg: Or (reg,not) = 139 (0); /* cost is counted for not */
169 reg: Or (reg,Constant) = 239 (10);
170 reg: Xor(reg,reg) = 40 (10);
171 reg: Xor(reg,not) = 140 (0); /* cost is counted for not */
172 reg: Xor(reg,Constant) = 240 (10);
174 /* Special case non-terms for BrCond(setCC) and BrCond(setCCconst) */
175 setCCconst: SetCC(reg,Constant) = 41 (5);
176 setCC: SetCC(reg,reg) = 42 (10);
179 reg: tobool = 222 (0);
180 reg: setCCconst = 241 (0);
181 reg: setCC = 242 (0);
184 * Special case non-terminals for the unary cast operators.
185 * Some of these can be folded into other operations (e.g., todouble).
186 * The rest are just for uniformity.
188 reg: toubyte = 123 (0);
189 reg: tosbyte = 124 (0);
190 reg: toushort = 125 (0);
191 reg: toshort = 126 (0);
192 reg: touint = 127 (0);
193 reg: toint = 128 (0);
194 reg: toulong = 129 (0);
195 reg: tolong = 130 (0);
196 reg: tofloat = 131 (0);
197 reg: todouble = 132 (0);
198 reg: todoubleConst = 133 (0);
200 reg: ToArrayTy(reg) = 19 (10);
201 reg: ToPointerTy(reg) = 20 (10);
204 * The binary arithmetic operators.
206 reg: Add(reg,reg) = 33 (10);
207 reg: Sub(reg,reg) = 34 (10);
208 reg: Mul(reg,reg) = 35 (30);
209 reg: Mul(todouble,todouble) = 135 (20); /* avoids 1-2 type converts */
210 reg: Div(reg,reg) = 36 (60);
211 reg: Rem(reg,reg) = 37 (60);
214 * The binary bitwise logical operators.
216 reg: BAnd(reg,reg) = 338 (10);
217 reg: BAnd(reg,bnot) = 438 ( 0); /* cost is counted for not */
218 reg: BOr( reg,reg) = 339 (10);
219 reg: BOr( reg,bnot) = 439 ( 0); /* cost is counted for not */
220 reg: BXor(reg,reg) = 340 (10);
221 reg: BXor(reg,bnot) = 440 ( 0); /* cost is counted for not */
223 reg: bnot = 321 ( 0);
224 bnot: BNot(reg,reg) = 421 (10);
227 * Special cases for the binary operators with one constant argument.
228 * Not and BNot are effectively just one argument, so not needed here.
230 reg: Add(reg,Constant) = 233 (10);
231 reg: Sub(reg,Constant) = 234 (10);
232 reg: Mul(reg,Constant) = 235 (30);
233 reg: Mul(todouble,todoubleConst) = 335 (20); /* avoids 1-2 type converts */
234 reg: Div(reg,Constant) = 236 (60);
235 reg: Rem(reg,Constant) = 237 (60);
237 reg: BAnd(reg,Constant) = 538 (0);
238 reg: BOr( reg,Constant) = 539 (0);
239 reg: BXor(reg,Constant) = 540 (0);
242 * Memory access instructions
244 reg: Load(reg) = 51 (30);
245 reg: Load(ptrreg) = 52 (20); /* 1 counted for ptrreg */
246 reg: ptrreg = 155 (0);
247 ptrreg: GetElemPtr(reg) = 55 (10);
248 ptrreg: GetElemPtrIdx(reg,reg) = 56 (10);
249 reg: Alloca = 57 (10);
250 reg: AllocaN(reg) = 58 (10);
253 * Other operators producing register values
255 reg: Call = 61 (20); /* just ignore the operands! */
256 reg: Shl(reg,reg) = 62 (20); /* 1 for issue restrictions */
257 reg: Shr(reg,reg) = 63 (20); /* 1 for issue restrictions */
258 reg: Phi(reg,reg) = 64 (0);
261 * Finally, leaf nodes of expression trees.
264 reg: Constant = 72 (3); /* prefer direct use */
269 /*-----------------------------------------------------------------------*
270 * The rest of this file provides code to print the cover produced
271 * by BURG and information about computed tree cost and matches.
272 * This code was taken from sample.gr provided with BURG.
273 *-----------------------------------------------------------------------*/
275 void printcover(NODEPTR_TYPE p, int goalnt, int indent) {
276 int eruleno = burm_rule(STATE_LABEL(p), goalnt);
277 short *nts = burm_nts[eruleno];
278 NODEPTR_TYPE kids[10];
282 printf("no cover\n");
285 for (i = 0; i < indent; i++)
287 printf("%s\n", burm_string[eruleno]);
288 burm_kids(p, eruleno, kids);
289 for (i = 0; nts[i]; i++)
290 printcover(kids[i], nts[i], indent+1);
293 void printtree(NODEPTR_TYPE p) {
294 int op = burm_op_label(p);
296 printf("%s", burm_opname[op]);
297 switch (burm_arity[op]) {
302 printtree(burm_child(p, 0));
307 printtree(burm_child(p, 0));
309 printtree(burm_child(p, 1));
315 int treecost(NODEPTR_TYPE p, int goalnt, int costindex) {
316 int eruleno = burm_rule(STATE_LABEL(p), goalnt);
317 int cost = burm_cost[eruleno][costindex], i;
318 short *nts = burm_nts[eruleno];
319 NODEPTR_TYPE kids[10];
321 burm_kids(p, eruleno, kids);
322 for (i = 0; nts[i]; i++)
323 cost += treecost(kids[i], nts[i], costindex);
327 void printMatches(NODEPTR_TYPE p) {
331 printf("Node 0x%lx= ", (unsigned long)p);
333 printf(" matched rules:\n");
334 for (nt = 1; burm_ntname[nt] != (char*)NULL; nt++)
335 if ((eruleno = burm_rule(STATE_LABEL(p), nt)) != 0)
336 printf("\t%s\n", burm_string[eruleno]);