aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Target/SparcV9/SparcV9.burg.in
blob: 6c3b659199c850e6aa2a26cdf757501a4c6baecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
%{               // -*- C++ -*-
Xinclude <stdio.h>
Xinclude <llvm/CodeGen/InstrForest.h>

typedef InstrTreeNode* NODEPTR_TYPE;
Xdefine OP_LABEL(p)	((p)->opLabel)
Xdefine LEFT_CHILD(p)	((p)->LeftChild)
Xdefine RIGHT_CHILD(p)	((p)->RightChild)
Xdefine STATE_LABEL(p)	((p)->state)
Xdefine PANIC		printf

// Get definitions for various instruction values that we will need...
#define HANDLE_TERM_INST(N, OPC, CLASS)   Ydefine OPC##OPCODE N
#define HANDLE_UNARY_INST(N, OPC, CLASS)  Ydefine OPC##OPCODE N
#define HANDLE_BINARY_INST(N, OPC, CLASS) Ydefine OPC##OPCODE N
#define HANDLE_MEMORY_INST(N, OPC, CLASS) Ydefine OPC##OPCODE N
#define HANDLE_OTHER_INST(N, OPC, CLASS)  Ydefine OPC##OPCODE N

#include "llvm/Instruction.def"

%}

%start stmt

%term Ret=RetOPCODE		/* return void from a function */
%term RetValue=101              /* return a value from a function */
%term BrUncond=BrOPCODE
%term BrCond=102
%term Switch=SwitchOPCODE
		/* 4 is unused */
%term Add=AddOPCODE
%term Sub=SubOPCODE
%term Mul=MulOPCODE
%term Div=DivOPCODE
%term Rem=RemOPCODE
%term And=AndOPCODE
%term Or=OrOPCODE
%term Xor=XorOPCODE
                /* Use the next 4 to distinguish bitwise operators from
                 * logical operators.  This is no longer used for Sparc,
                 * but may be useful for other target machines.
                 * The last one is the bitwise Not(val) == XOR val, 11..1.
                 * Note that it is also a binary operator, not unary.
                 */
%term BAnd=111
%term BOr=112
%term BXor=113
%term BNot=213
                /* The next one is the boolean Not(val) == bool XOR val, true
                 * Note that it is also a binary operator, not unary.
                 */
%term  Not=313

%term SetCC=114	/* use this to match all SetCC instructions */
	/* %term SetEQ=13 */
	/* %term SetNE=14 */
	/* %term SetLE=15 */
	/* %term SetGE=16 */
	/* %term SetLT=17 */
	/* %term SetGT=18 */
%term Malloc=MallocOPCODE
%term Free=FreeOPCODE
%term Alloca=AllocaOPCODE
%term AllocaN=122               /* alloca with arg N */
%term Load=LoadOPCODE
%term Store=StoreOPCODE
%term GetElemPtr=GetElementPtrOPCODE
%term GetElemPtrIdx=125         /* getElemPtr with index vector */

%term Phi=PHINodeOPCODE

%term Cast=CastOPCODE /* cast that will be ignored. others are made explicit */
%term ToBoolTy=127
%term ToUByteTy=128
%term ToSByteTy=129
%term ToUShortTy=130
%term ToShortTy=131
%term ToUIntTy=132
%term ToIntTy=133
%term ToULongTy=134
%term ToLongTy=135
%term ToFloatTy=136
%term ToDoubleTy=137
%term ToArrayTy=138
%term ToPointerTy=139

%term Call=CallOPCODE
%term Shl=ShlOPCODE
%term Shr=ShrOPCODE
%term VaArg=VarArgOPCODE
		/* 32...46 are unused */
    /*
     * The foll. values should match the constants in InstrForest.h
     */
%term VRegList=97
%term VReg=98
%term Constant=99
%term Label=100
		/* 50+i is a variant of i, as defined above */


%%
/*-----------------------------------------------------------------------*
 * The productions of the grammar.
 * Note that all chain rules are numbered 101 and above.
 * Also, a special case of production X is numbered 100+X, 200+X, etc.
 * The cost of a 1-cycle operation is represented as 10, to allow
 * finer comparisons of costs (effectively, fractions of 1/10).
 *-----------------------------------------------------------------------*/

	/*
	 * The top-level statements
	 */
stmt:	Ret			=   1 (30);
stmt:	RetValue(reg)		=   2 (30);
stmt:	Store(reg,reg)		=   3 (10);
stmt:	Store(reg,ptrreg)	=   4 (10);
stmt:	BrUncond		=   5 (20);
stmt:	BrCond(setCC)		=   6 (20);     /* branch on cond. code */
stmt:	BrCond(setCCconst)	= 206 (10);	/* may save one instruction */
stmt:	BrCond(reg)		=   8 (20);	/* may avoid an extra instr */
stmt:	BrCond(Constant)	= 208 (20);	/* may avoid an extra instr */
stmt:	Switch(reg)		=   9 (30);	/* cost = load + branch */

stmt:	reg			=  111 (0);

	/*
	 * List node used for nodes with more than 2 children
	 */
reg:	VRegList(reg,reg)	=  10 (0);

	/*
	 * Special case non-terminals to help combine unary instructions.
	 *	Eg1:  zdouble <- todouble(xfloat) * todouble(yfloat)
	 *	Eg2:  c       <- a AND (NOT b).
	 * Note that the costs are counted for the special non-terminals here,
	 * and should not be counted again for the reg productions later.
	 */
not:	  Not(reg,reg)		=   21 (10);
tobool:	  ToBoolTy(reg)		=   22 (10);
not:      Not(tobool, reg)      =  322 (10); // fold cast-to-bool into not
toubyte:  ToUByteTy(reg)	=   23 (10);
tosbyte:  ToSByteTy(reg)	=   24 (10);
toushort: ToUShortTy(reg)	=   25 (10);
toshort:  ToShortTy(reg)	=   26 (10);
touint:	  ToUIntTy(reg)		=   27 (10);
toint:	  ToIntTy(reg)		=   28 (10);
toulong:  ToULongTy(reg)	=   29 (10);
tolong:	  ToLongTy(reg)		=   30 (10);
tofloat:  ToFloatTy(reg)	=   31 (10);
todouble: ToDoubleTy(reg)	=   32 (10);
todoubleConst: ToDoubleTy(Constant) = 232 (10);

	/*
	 * All the ways to produce a boolean value (Not and ToBoolTy are above):
	 * -- boolean operators: Not, And, Or, ..., ToBoolTy, SetCC
	 * -- an existing boolean register not in the same tree
	 * -- a boolean constant
	 * 
	 * For And, Or, Xor, we add special cases for when:
         * (a) one operand is a constant.
         * (b) one operand is a NOT, to use the ANDN, ORN, and XORN instrns.
	 * We do not need the cases when both operands are constant
	 * because constant folding should take care of that beforehand.
	 */
reg:	And(reg,reg)		=   38 (10);
reg:	And(reg,not)		=  138 (0);	/* cost is counted for not */
reg:	And(reg,Constant)	=  238 (10);
reg:	Or (reg,reg)		=   39 (10);
reg:	Or (reg,not)		=  139 (0);	/* cost is counted for not */
reg:	Or (reg,Constant)	=  239 (10);
reg:	Xor(reg,reg)		=   40 (10);
reg:	Xor(reg,not)		=  140 (0);	/* cost is counted for not */
reg:	Xor(reg,Constant)	=  240 (10);

        /* Special case non-terms for BrCond(setCC) and BrCond(setCCconst) */
setCCconst: SetCC(reg,Constant)	=   41 (5);
setCC:	    SetCC(reg,reg)	=   42 (10);

reg:	not			=  221 (0);
reg:	tobool                  =  222 (0);
reg:	setCCconst		=  241 (0);
reg:	setCC                   =  242 (0);

	/*
	 * Special case non-terminals for the unary cast operators.
         * Some of these can be folded into other operations (e.g., todouble).
         * The rest are just for uniformity.
	 */
reg:	toubyte			=  123 (0);
reg:	tosbyte			=  124 (0);
reg:	toushort		=  125 (0);
reg:	toshort			=  126 (0);
reg:	touint			=  127 (0);
reg:	toint			=  128 (0);
reg:	toulong			=  129 (0);
reg:	tolong			=  130 (0);
reg:	tofloat			=  131 (0);
reg:	todouble		=  132 (0);
reg:	todoubleConst		=  133 (0);

reg:	ToArrayTy(reg)          =  19 (10);
reg:	ToPointerTy(reg)	=  20 (10);

	/*
	 * The binary arithmetic operators.
	 */
reg:	Add(reg,reg)		=   33 (10);
reg:	Sub(reg,reg)		=   34 (10);
reg:	Mul(reg,reg)		=   35 (30);
reg:	Mul(todouble,todouble)	=  135 (20);	/* avoids 1-2 type converts */
reg:	Div(reg,reg)		=   36 (60);
reg:	Rem(reg,reg)		=   37 (60);

	/*
	 * The binary bitwise logical operators.
	 */
reg:    BAnd(reg,reg)           =  338 (10);
reg:    BAnd(reg,bnot)          =  438 ( 0);	/* cost is counted for not */
reg:    BOr( reg,reg)           =  339 (10);
reg:    BOr( reg,bnot)          =  439 ( 0);	/* cost is counted for not */
reg:    BXor(reg,reg)           =  340 (10);
reg:    BXor(reg,bnot)          =  440 ( 0);	/* cost is counted for not */

reg:    bnot                    =  321 ( 0);
bnot:   BNot(reg,reg)           =  421 (10);

	/*
	 * Special cases for the binary operators with one constant argument.
         * Not and BNot are effectively just one argument, so not needed here.
	 */
reg:	Add(reg,Constant)	=  233 (10);
reg:	Sub(reg,Constant)	=  234 (10);
reg:	Mul(reg,Constant)	=  235 (30);
reg:	Mul(todouble,todoubleConst) = 335 (20);	/* avoids 1-2 type converts */
reg:	Div(reg,Constant)	=  236 (60);
reg:	Rem(reg,Constant)	=  237 (60);

reg:    BAnd(reg,Constant)      =  538 (0);
reg:    BOr( reg,Constant)      =  539 (0);
reg:    BXor(reg,Constant)      =  540 (0);

	/*
	 * Memory access instructions
	 */
reg:	Load(reg)		=   51 (30);
reg:	Load(ptrreg)		=   52 (20);	/* 1 counted for ptrreg */
reg:	ptrreg			=  155 (0);
ptrreg:	GetElemPtr(reg)		=   55 (10);
ptrreg:	GetElemPtrIdx(reg,reg)	=   56 (10);
reg:	Alloca			=   57 (10);
reg:	AllocaN(reg)		=   58 (10);

	/*
	 * Other operators producing register values
	 */
reg:	Call			=   61 (20);	/* just ignore the operands! */
reg:	Shl(reg,reg)		=   62 (20);	/* 1 for issue restrictions */
reg:	Shr(reg,reg)		=   63 (20);	/* 1 for issue restrictions */
reg:	Phi(reg,reg)		=   64 (0);
reg:	VaArg(reg)		=   65 (40);	/* load from stack then incr */

	/*
	 * Finally, leaf nodes of expression trees.
	 */
reg:	VReg			=   71 (0);
reg:	Constant		=   72 (3);	/* prefer direct use */



%%
/*-----------------------------------------------------------------------*
 * The rest of this file provides code to print the cover produced
 * by BURG and information about computed tree cost and matches.
 * This code was taken from sample.gr provided with BURG.
 *-----------------------------------------------------------------------*/

void printcover(NODEPTR_TYPE p, int goalnt, int indent) {
	int eruleno = burm_rule(STATE_LABEL(p), goalnt);
	short *nts = burm_nts[eruleno];
	NODEPTR_TYPE kids[10];
	int i;
	
	if (eruleno == 0) {
		printf("no cover\n");
		return;
	}
	for (i = 0; i < indent; i++)
		printf(".");
	printf("%s\n", burm_string[eruleno]);
	burm_kids(p, eruleno, kids);
	for (i = 0; nts[i]; i++)
		printcover(kids[i], nts[i], indent+1);
}

void printtree(NODEPTR_TYPE p) {
	int op = burm_op_label(p);

	printf("%s", burm_opname[op]);
	switch (burm_arity[op]) {
	case 0:
		break;
	case 1:
		printf("(");
		printtree(burm_child(p, 0));
		printf(")");
		break;
	case 2:
		printf("(");
		printtree(burm_child(p, 0));
		printf(", ");
		printtree(burm_child(p, 1));
		printf(")");
		break;
	}
}

int treecost(NODEPTR_TYPE p, int goalnt, int costindex) {
	int eruleno = burm_rule(STATE_LABEL(p), goalnt);
	int cost = burm_cost[eruleno][costindex], i;
	short *nts = burm_nts[eruleno];
	NODEPTR_TYPE kids[10];

	burm_kids(p, eruleno, kids);
	for (i = 0; nts[i]; i++)
		cost += treecost(kids[i], nts[i], costindex);
	return cost;
}

void printMatches(NODEPTR_TYPE p) {
	int nt;
	int eruleno;

	printf("Node 0x%lx= ", (unsigned long)p);
	printtree(p);
	printf(" matched rules:\n");
	for (nt = 1; burm_ntname[nt] != (char*)NULL; nt++)
		if ((eruleno = burm_rule(STATE_LABEL(p), nt)) != 0)
			printf("\t%s\n", burm_string[eruleno]);
}