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