08cfbcc1e973b751e803b9f551b1575d10dc7523
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9.burg.in
1 %{               // -*- C++ -*-
2 Xinclude <cstdio>
3 Xinclude <llvm/CodeGen/InstrForest.h>
4
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)
10 Xdefine PANIC           printf
11
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
18
19 #include "llvm/Instruction.def"
20
21 %}
22
23 %start stmt
24
25 %term Ret=RetOPCODE             /* return void from a function */
26 %term RetValue=101              /* return a value from a function */
27 %term BrUncond=BrOPCODE
28 %term BrCond=102
29 %term Switch=SwitchOPCODE
30                 /* 4 is unused */
31 %term Add=AddOPCODE
32 %term Sub=SubOPCODE
33 %term Mul=MulOPCODE
34 %term Div=DivOPCODE
35 %term Rem=RemOPCODE
36 %term And=AndOPCODE
37 %term Or=OrOPCODE
38 %term Xor=XorOPCODE
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.
44                  */
45 %term BAnd=111
46 %term BOr=112
47 %term BXor=113
48 %term BNot=213
49                 /* The next one is the boolean Not(val) == bool XOR val, true
50                  * Note that it is also a binary operator, not unary.
51                  */
52 %term  Not=313
53
54 %term SetCC=114 /* use this to match all SetCC instructions */
55         /* %term SetEQ=13 */
56         /* %term SetNE=14 */
57         /* %term SetLE=15 */
58         /* %term SetGE=16 */
59         /* %term SetLT=17 */
60         /* %term SetGT=18 */
61 %term Malloc=MallocOPCODE
62 %term Free=FreeOPCODE
63 %term Alloca=AllocaOPCODE
64 %term AllocaN=122               /* alloca with arg N */
65 %term Load=LoadOPCODE
66 %term Store=StoreOPCODE
67 %term GetElemPtr=GetElementPtrOPCODE
68 %term GetElemPtrIdx=125         /* getElemPtr with index vector */
69
70 %term Phi=PHIOPCODE
71
72 %term Cast=CastOPCODE /* cast that will be ignored. others are made explicit */
73 %term ToBoolTy=127
74 %term ToUByteTy=128
75 %term ToSByteTy=129
76 %term ToUShortTy=130
77 %term ToShortTy=131
78 %term ToUIntTy=132
79 %term ToIntTy=133
80 %term ToULongTy=134
81 %term ToLongTy=135
82 %term ToFloatTy=136
83 %term ToDoubleTy=137
84 %term ToArrayTy=138
85 %term ToPointerTy=139
86
87 %term Call=CallOPCODE
88 %term Shl=ShlOPCODE
89 %term Shr=ShrOPCODE
90 %term VANext=VANextOPCODE
91 %term VAArg=VAArgOPCODE
92                 /* 33...46 are unused */
93     /*
94      * The foll. values should match the constants in InstrForest.h
95      */
96 %term VRegList=97
97 %term VReg=98
98 %term Constant=99
99 %term Label=100
100                 /* 50+i is a variant of i, as defined above */
101
102
103 %%
104 /*-----------------------------------------------------------------------*
105  * The productions of the grammar.
106  * Note that all chain rules are numbered 101 and above.
107  * Also, a special case of production X is numbered 100+X, 200+X, etc.
108  * The cost of a 1-cycle operation is represented as 10, to allow
109  * finer comparisons of costs (effectively, fractions of 1/10).
110  *-----------------------------------------------------------------------*/
111
112         /*
113          * The top-level statements
114          */
115 stmt:   Ret                     =   1 (30);
116 stmt:   RetValue(reg)           =   2 (30);
117 stmt:   Store(reg,reg)          =   3 (10);
118 stmt:   Store(reg,ptrreg)       =   4 (10);
119 stmt:   BrUncond                =   5 (20);
120 stmt:   BrCond(setCC)           =   6 (20);     /* branch on cond. code */
121 stmt:   BrCond(setCCconst)      = 206 (10);     /* may save one instruction */
122 stmt:   BrCond(reg)             =   8 (20);     /* may avoid an extra instr */
123 stmt:   BrCond(Constant)        = 208 (20);     /* may avoid an extra instr */
124 stmt:   Switch(reg)             =   9 (30);     /* cost = load + branch */
125
126 stmt:   reg                     =  111 (0);
127
128         /*
129          * List node used for nodes with more than 2 children
130          */
131 reg:    VRegList(reg,reg)       =  10 (0);
132
133         /*
134          * Special case non-terminals to help combine unary instructions.
135          *      Eg1:  zdouble <- todouble(xfloat) * todouble(yfloat)
136          *      Eg2:  c       <- a AND (NOT b).
137          * Note that the costs are counted for the special non-terminals here,
138          * and should not be counted again for the reg productions later.
139          */
140 not:      Not(reg,reg)          =   21 (10);
141 tobool:   ToBoolTy(reg)         =   22 (10);
142 not:      Not(tobool, reg)      =  322 (10); // fold cast-to-bool into not
143 toubyte:  ToUByteTy(reg)        =   23 (10);
144 tosbyte:  ToSByteTy(reg)        =   24 (10);
145 toushort: ToUShortTy(reg)       =   25 (10);
146 toshort:  ToShortTy(reg)        =   26 (10);
147 touint:   ToUIntTy(reg)         =   27 (10);
148 toint:    ToIntTy(reg)          =   28 (10);
149 toulong:  ToULongTy(reg)        =   29 (10);
150 tolong:   ToLongTy(reg)         =   30 (10);
151 tofloat:  ToFloatTy(reg)        =   31 (10);
152 todouble: ToDoubleTy(reg)       =   32 (10);
153 todoubleConst: ToDoubleTy(Constant) = 232 (10);
154
155         /*
156          * All the ways to produce a boolean value (Not and ToBoolTy are above):
157          * -- boolean operators: Not, And, Or, ..., ToBoolTy, SetCC
158          * -- an existing boolean register not in the same tree
159          * -- a boolean constant
160          * 
161          * For And, Or, Xor, we add special cases for when:
162          * (a) one operand is a constant.
163          * (b) one operand is a NOT, to use the ANDN, ORN, and XORN instrns.
164          * We do not need the cases when both operands are constant
165          * because constant folding should take care of that beforehand.
166          */
167 reg:    And(reg,reg)            =   38 (10);
168 reg:    And(reg,not)            =  138 (0);     /* cost is counted for not */
169 reg:    And(reg,Constant)       =  238 (10);
170 reg:    Or (reg,reg)            =   39 (10);
171 reg:    Or (reg,not)            =  139 (0);     /* cost is counted for not */
172 reg:    Or (reg,Constant)       =  239 (10);
173 reg:    Xor(reg,reg)            =   40 (10);
174 reg:    Xor(reg,not)            =  140 (0);     /* cost is counted for not */
175 reg:    Xor(reg,Constant)       =  240 (10);
176
177         /* Special case non-terms for BrCond(setCC) and BrCond(setCCconst) */
178 setCCconst: SetCC(reg,Constant) =   41 (5);
179 setCC:      SetCC(reg,reg)      =   42 (10);
180
181 reg:    not                     =  221 (0);
182 reg:    tobool                  =  222 (0);
183 reg:    setCCconst              =  241 (0);
184 reg:    setCC                   =  242 (0);
185
186         /*
187          * Special case non-terminals for the unary cast operators.
188          * Some of these can be folded into other operations (e.g., todouble).
189          * The rest are just for uniformity.
190          */
191 reg:    toubyte                 =  123 (0);
192 reg:    tosbyte                 =  124 (0);
193 reg:    toushort                =  125 (0);
194 reg:    toshort                 =  126 (0);
195 reg:    touint                  =  127 (0);
196 reg:    toint                   =  128 (0);
197 reg:    toulong                 =  129 (0);
198 reg:    tolong                  =  130 (0);
199 reg:    tofloat                 =  131 (0);
200 reg:    todouble                =  132 (0);
201 reg:    todoubleConst           =  133 (0);
202
203 reg:    ToArrayTy(reg)          =  19 (10);
204 reg:    ToPointerTy(reg)        =  20 (10);
205
206         /*
207          * The binary arithmetic operators.
208          */
209 reg:    Add(reg,reg)            =   33 (10);
210 reg:    Sub(reg,reg)            =   34 (10);
211 reg:    Mul(reg,reg)            =   35 (30);
212 reg:    Mul(todouble,todouble)  =  135 (20);    /* avoids 1-2 type converts */
213 reg:    Div(reg,reg)            =   36 (60);
214 reg:    Rem(reg,reg)            =   37 (60);
215
216         /*
217          * The binary bitwise logical operators.
218          */
219 reg:    BAnd(reg,reg)           =  338 (10);
220 reg:    BAnd(reg,bnot)          =  438 ( 0);    /* cost is counted for not */
221 reg:    BOr( reg,reg)           =  339 (10);
222 reg:    BOr( reg,bnot)          =  439 ( 0);    /* cost is counted for not */
223 reg:    BXor(reg,reg)           =  340 (10);
224 reg:    BXor(reg,bnot)          =  440 ( 0);    /* cost is counted for not */
225
226 reg:    bnot                    =  321 ( 0);
227 bnot:   BNot(reg,reg)           =  421 (10);
228
229         /*
230          * Special cases for the binary operators with one constant argument.
231          * Not and BNot are effectively just one argument, so not needed here.
232          */
233 reg:    Add(reg,Constant)       =  233 (10);
234 reg:    Sub(reg,Constant)       =  234 (10);
235 reg:    Mul(reg,Constant)       =  235 (30);
236 reg:    Mul(todouble,todoubleConst) = 335 (20); /* avoids 1-2 type converts */
237 reg:    Div(reg,Constant)       =  236 (60);
238 reg:    Rem(reg,Constant)       =  237 (60);
239
240 reg:    BAnd(reg,Constant)      =  538 (0);
241 reg:    BOr( reg,Constant)      =  539 (0);
242 reg:    BXor(reg,Constant)      =  540 (0);
243
244         /*
245          * Memory access instructions
246          */
247 reg:    Load(reg)               =   51 (30);
248 reg:    Load(ptrreg)            =   52 (20);    /* 1 counted for ptrreg */
249 reg:    ptrreg                  =  155 (0);
250 ptrreg: GetElemPtr(reg)         =   55 (10);
251 ptrreg: GetElemPtrIdx(reg,reg)  =   56 (10);
252 reg:    Alloca                  =   57 (10);
253 reg:    AllocaN(reg)            =   58 (10);
254
255         /*
256          * Other operators producing register values
257          */
258 reg:    Call                    =   61 (20);    /* just ignore the operands! */
259 reg:    Shl(reg,reg)            =   62 (20);    /* 1 for issue restrictions */
260 reg:    Shr(reg,reg)            =   63 (20);    /* 1 for issue restrictions */
261 reg:    Phi(reg,reg)            =   64 (0);
262 reg:    VANext(reg)             =   65 (40);    /* incr stack slot pointer */
263 reg:    VAArg(reg)              =   66 (40);    /* get a vararg */
264
265         /*
266          * Finally, leaf nodes of expression trees.
267          */
268 reg:    VReg                    =   71 (0);
269 reg:    Constant                =   72 (3);     /* prefer direct use */
270
271
272
273 %%
274 /*-----------------------------------------------------------------------*
275  * The rest of this file provides code to print the cover produced
276  * by BURG and information about computed tree cost and matches.
277  * This code was taken from sample.gr provided with BURG.
278  *-----------------------------------------------------------------------*/
279
280 void printcover(NODEPTR_TYPE p, int goalnt, int indent) {
281         int eruleno = burm_rule(STATE_LABEL(p), goalnt);
282         short *nts = burm_nts[eruleno];
283         NODEPTR_TYPE kids[10];
284         int i;
285         
286         if (eruleno == 0) {
287                 printf("no cover\n");
288                 return;
289         }
290         for (i = 0; i < indent; i++)
291                 printf(".");
292         printf("%s\n", burm_string[eruleno]);
293         burm_kids(p, eruleno, kids);
294         for (i = 0; nts[i]; i++)
295                 printcover(kids[i], nts[i], indent+1);
296 }
297
298 void printtree(NODEPTR_TYPE p) {
299         int op = burm_op_label(p);
300
301         printf("%s", burm_opname[op]);
302         switch (burm_arity[op]) {
303         case 0:
304                 break;
305         case 1:
306                 printf("(");
307                 printtree(burm_child(p, 0));
308                 printf(")");
309                 break;
310         case 2:
311                 printf("(");
312                 printtree(burm_child(p, 0));
313                 printf(", ");
314                 printtree(burm_child(p, 1));
315                 printf(")");
316                 break;
317         }
318 }
319
320 int treecost(NODEPTR_TYPE p, int goalnt, int costindex) {
321         int eruleno = burm_rule(STATE_LABEL(p), goalnt);
322         int cost = burm_cost[eruleno][costindex], i;
323         short *nts = burm_nts[eruleno];
324         NODEPTR_TYPE kids[10];
325
326         burm_kids(p, eruleno, kids);
327         for (i = 0; nts[i]; i++)
328                 cost += treecost(kids[i], nts[i], costindex);
329         return cost;
330 }
331
332 void printMatches(NODEPTR_TYPE p) {
333         int nt;
334         int eruleno;
335
336         printf("Node 0x%lx= ", (unsigned long)p);
337         printtree(p);
338         printf(" matched rules:\n");
339         for (nt = 1; burm_ntname[nt] != (char*)NULL; nt++)
340                 if ((eruleno = burm_rule(STATE_LABEL(p), nt)) != 0)
341                         printf("\t%s\n", burm_string[eruleno]);
342 }