9d56ec6e064d916c27f9449432ad6e4440a4ae33
[oota-llvm.git] / lib / Target / TargetSelectionDAG.td
1 //===- TargetSelectionDAG.td - Common code for DAG isels ---*- tablegen -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the target-independent interfaces used by SelectionDAG
11 // instruction selection generators.
12 //
13 //===----------------------------------------------------------------------===//
14
15 //===----------------------------------------------------------------------===//
16 // Selection DAG Type Constraint definitions.
17 //
18 // Note that the semantics of these constraints are hard coded into tblgen.  To
19 // modify or add constraints, you have to hack tblgen.
20 //
21
22 class SDTypeConstraint<int opnum> {
23   int OperandNum = opnum;
24 }
25
26 // SDTCisVT - The specified operand has exactly this VT.
27 class SDTCisVT<int OpNum, ValueType vt> : SDTypeConstraint<OpNum> {
28   ValueType VT = vt;
29 }
30
31 class SDTCisPtrTy<int OpNum> : SDTypeConstraint<OpNum>;
32
33 // SDTCisInt - The specified operand is has integer type.
34 class SDTCisInt<int OpNum> : SDTypeConstraint<OpNum>;
35
36 // SDTCisFP - The specified operand is has floating point type.
37 class SDTCisFP<int OpNum> : SDTypeConstraint<OpNum>;
38
39 // SDTCisSameAs - The two specified operands have identical types.
40 class SDTCisSameAs<int OpNum, int OtherOp> : SDTypeConstraint<OpNum> {
41   int OtherOperandNum = OtherOp;
42 }
43
44 // SDTCisVTSmallerThanOp - The specified operand is a VT SDNode, and its type is
45 // smaller than the 'Other' operand.
46 class SDTCisVTSmallerThanOp<int OpNum, int OtherOp> : SDTypeConstraint<OpNum> {
47   int OtherOperandNum = OtherOp;
48 }
49
50 class SDTCisOpSmallerThanOp<int SmallOp, int BigOp> : SDTypeConstraint<SmallOp>{
51   int BigOperandNum = BigOp;
52 }
53
54 //===----------------------------------------------------------------------===//
55 // Selection DAG Type Profile definitions.
56 //
57 // These use the constraints defined above to describe the type requirements of
58 // the various nodes.  These are not hard coded into tblgen, allowing targets to
59 // add their own if needed.
60 //
61
62 // SDTypeProfile - This profile describes the type requirements of a Selection
63 // DAG node.
64 class SDTypeProfile<int numresults, int numoperands,
65                     list<SDTypeConstraint> constraints> {
66   int NumResults = numresults;
67   int NumOperands = numoperands;
68   list<SDTypeConstraint> Constraints = constraints;
69 }
70
71 // Builtin profiles.
72 def SDTIntLeaf: SDTypeProfile<1, 0, [SDTCisInt<0>]>;      // for 'imm'.
73 def SDTPtrLeaf: SDTypeProfile<1, 0, [SDTCisPtrTy<0>]>;      // for '&g'.
74 def SDTOther  : SDTypeProfile<1, 0, [SDTCisVT<0, OtherVT>]>; // for 'vt'.
75 def SDTUNDEF  : SDTypeProfile<1, 0, []>; // for 'undef'.
76 def SDTIntBinOp : SDTypeProfile<1, 2, [   // add, and, or, xor, udiv, etc.
77   SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisInt<0>
78 ]>;
79 def SDTIntShiftOp : SDTypeProfile<1, 2, [   // shl, sra, srl
80   SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisInt<2>
81 ]>;
82 def SDTFPBinOp : SDTypeProfile<1, 2, [      // fadd, fmul, etc.
83   SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisFP<0>
84 ]>;
85 def SDTIntUnaryOp : SDTypeProfile<1, 1, [   // ctlz
86   SDTCisSameAs<0, 1>, SDTCisInt<0>
87 ]>;
88 def SDTIntExtendOp : SDTypeProfile<1, 1, [  // sext, zext, anyext
89   SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<1, 0>
90 ]>;
91 def SDTIntTruncOp  : SDTypeProfile<1, 1, [  // trunc
92   SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<0, 1>
93 ]>;
94 def SDTFPUnaryOp  : SDTypeProfile<1, 1, [   // fneg, fsqrt, etc
95   SDTCisSameAs<0, 1>, SDTCisFP<0>
96 ]>;
97 def SDTFPRoundOp  : SDTypeProfile<1, 1, [   // fround
98   SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<0, 1>
99 ]>;
100 def SDTFPExtendOp  : SDTypeProfile<1, 1, [   // fextend
101   SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<1, 0>
102 ]>;
103 def SDTIntToFPOp : SDTypeProfile<1, 1, [   // [su]int_to_fp 
104   SDTCisFP<0>, SDTCisInt<1>
105 ]>;
106 def SDTFPToIntOp : SDTypeProfile<1, 1, [   // fp_to_[su]int 
107   SDTCisInt<0>, SDTCisFP<1>
108 ]>;
109 def SDTExtInreg : SDTypeProfile<1, 2, [   // sext_inreg
110   SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisVT<2, OtherVT>,
111   SDTCisVTSmallerThanOp<2, 1>
112 ]>;
113
114 def SDTSetCC : SDTypeProfile<1, 3, [ // setcc
115   SDTCisInt<0>, SDTCisSameAs<1, 2>, SDTCisVT<3, OtherVT>
116 ]>;
117
118 def SDTSelect : SDTypeProfile<1, 3, [ // select 
119   SDTCisInt<1>, SDTCisSameAs<0, 2>, SDTCisSameAs<2, 3>
120 ]>;
121
122 def SDTBr : SDTypeProfile<0, 1, [ // br
123   SDTCisVT<0, OtherVT>
124 ]>;
125
126 def SDTBrCond : SDTypeProfile<0, 2, [ // brcond
127   SDTCisInt<0>, SDTCisVT<1, OtherVT>
128 ]>;
129
130 def SDTRet : SDTypeProfile<0, 0, [ // ret
131 ]>;
132
133 def SDTWritePort : SDTypeProfile<0, 2, [ // writeport
134   SDTCisInt<0>, SDTCisInt<1>
135 ]>;
136
137 def SDTLoad : SDTypeProfile<1, 1, [ // load
138   SDTCisPtrTy<1>  
139 ]>;
140
141 def SDTStore : SDTypeProfile<0, 2, [ // store
142   SDTCisPtrTy<1>  
143 ]>;
144
145 //===----------------------------------------------------------------------===//
146 // Selection DAG Node Properties.
147 //
148 // Note: These are hard coded into tblgen.
149 //
150 class SDNodeProperty;
151 def SDNPCommutative : SDNodeProperty;   // X op Y == Y op X
152 def SDNPAssociative : SDNodeProperty;   // (X op Y) op Z == X op (Y op Z)
153 def SDNPHasChain    : SDNodeProperty;   // R/W chain operand and result
154
155 //===----------------------------------------------------------------------===//
156 // Selection DAG Node definitions.
157 //
158 class SDNode<string opcode, SDTypeProfile typeprof,
159              list<SDNodeProperty> props = [], string sdclass = "SDNode"> {
160   string Opcode  = opcode;
161   string SDClass = sdclass;
162   list<SDNodeProperty> Properties = props;
163   SDTypeProfile TypeProfile = typeprof;
164 }
165
166 def set;
167 def node;
168
169 def imm        : SDNode<"ISD::Constant"  , SDTIntLeaf , [], "ConstantSDNode">;
170 def vt         : SDNode<"ISD::VALUETYPE" , SDTOther   , [], "VTSDNode">;
171 def bb         : SDNode<"ISD::BasicBlock", SDTOther   , [], "BasicBlockSDNode">;
172 def cond       : SDNode<"ISD::CONDCODE"  , SDTOther   , [], "CondCodeSDNode">;
173 def undef      : SDNode<"ISD::UNDEF"     , SDTUNDEF   , []>;
174 def globaladdr : SDNode<"ISD::GlobalAddress", SDTPtrLeaf, [],
175                         "GlobalAddressSDNode">;
176 def tglobaladdr : SDNode<"ISD::TargetGlobalAddress", SDTPtrLeaf, [],
177                         "GlobalAddressSDNode">;
178 def tconstpool  : SDNode<"ISD::TargetConstantPool", SDTPtrLeaf, [],
179                         "ConstantPoolSDNode">;
180 def add        : SDNode<"ISD::ADD"       , SDTIntBinOp   ,
181                         [SDNPCommutative, SDNPAssociative]>;
182 def sub        : SDNode<"ISD::SUB"       , SDTIntBinOp>;
183 def mul        : SDNode<"ISD::MUL"       , SDTIntBinOp,
184                         [SDNPCommutative, SDNPAssociative]>;
185 def mulhs      : SDNode<"ISD::MULHS"     , SDTIntBinOp, [SDNPCommutative]>;
186 def mulhu      : SDNode<"ISD::MULHU"     , SDTIntBinOp, [SDNPCommutative]>;
187 def sdiv       : SDNode<"ISD::SDIV"      , SDTIntBinOp>;
188 def udiv       : SDNode<"ISD::UDIV"      , SDTIntBinOp>;
189 def srem       : SDNode<"ISD::SREM"      , SDTIntBinOp>;
190 def urem       : SDNode<"ISD::UREM"      , SDTIntBinOp>;
191 def srl        : SDNode<"ISD::SRL"       , SDTIntShiftOp>;
192 def sra        : SDNode<"ISD::SRA"       , SDTIntShiftOp>;
193 def shl        : SDNode<"ISD::SHL"       , SDTIntShiftOp>;
194 def and        : SDNode<"ISD::AND"       , SDTIntBinOp,
195                         [SDNPCommutative, SDNPAssociative]>;
196 def or         : SDNode<"ISD::OR"        , SDTIntBinOp,
197                         [SDNPCommutative, SDNPAssociative]>;
198 def xor        : SDNode<"ISD::XOR"       , SDTIntBinOp,
199                         [SDNPCommutative, SDNPAssociative]>;
200                         
201 def sext_inreg : SDNode<"ISD::SIGN_EXTEND_INREG", SDTExtInreg>;
202 def ctlz       : SDNode<"ISD::CTLZ"       , SDTIntUnaryOp>;
203 def cttz       : SDNode<"ISD::CTTZ"       , SDTIntUnaryOp>;
204 def ctpop      : SDNode<"ISD::CTPOP"      , SDTIntUnaryOp>;
205 def sext       : SDNode<"ISD::SIGN_EXTEND", SDTIntExtendOp>;
206 def zext       : SDNode<"ISD::ZERO_EXTEND", SDTIntExtendOp>;
207 def anyext     : SDNode<"ISD::ANY_EXTEND" , SDTIntExtendOp>;
208 def trunc      : SDNode<"ISD::TRUNCATE"   , SDTIntTruncOp>;
209                         
210 def fadd       : SDNode<"ISD::FADD"       , SDTFPBinOp, [SDNPCommutative]>;
211 def fsub       : SDNode<"ISD::FSUB"       , SDTFPBinOp>;
212 def fmul       : SDNode<"ISD::FMUL"       , SDTFPBinOp, [SDNPCommutative]>;
213 def fdiv       : SDNode<"ISD::FDIV"       , SDTFPBinOp>;
214 def frem       : SDNode<"ISD::FREM"       , SDTFPBinOp>;
215 def fabs       : SDNode<"ISD::FABS"       , SDTFPUnaryOp>;
216 def fneg       : SDNode<"ISD::FNEG"       , SDTFPUnaryOp>;
217 def fsqrt      : SDNode<"ISD::FSQRT"      , SDTFPUnaryOp>;
218
219 def fround     : SDNode<"ISD::FP_ROUND"   , SDTFPRoundOp>;
220 def fextend    : SDNode<"ISD::FP_EXTEND"  , SDTFPExtendOp>;
221
222 def sint_to_fp : SDNode<"ISD::SINT_TO_FP" , SDTIntToFPOp>;
223 def uint_to_fp : SDNode<"ISD::UINT_TO_FP" , SDTIntToFPOp>;
224 def fp_to_sint : SDNode<"ISD::FP_TO_SINT" , SDTFPToIntOp>;
225 def fp_to_uint : SDNode<"ISD::FP_TO_UINT" , SDTFPToIntOp>;
226
227 def setcc      : SDNode<"ISD::SETCC"      , SDTSetCC>;
228 def select     : SDNode<"ISD::SELECT"     , SDTSelect>;
229
230 def br         : SDNode<"ISD::BR"         , SDTBr,     [SDNPHasChain]>;
231 def brcond     : SDNode<"ISD::BRCOND"     , SDTBrCond, [SDNPHasChain]>;
232 def ret        : SDNode<"ISD::RET"        , SDTRet,    [SDNPHasChain]>;
233
234 def writeport  : SDNode<"ISD::WRITEPORT"  , SDTWritePort, [SDNPHasChain]>;
235
236 def load       : SDNode<"ISD::LOAD"       , SDTLoad,  [SDNPHasChain]>;
237 def store      : SDNode<"ISD::STORE"      , SDTStore, [SDNPHasChain]>;
238
239 //===----------------------------------------------------------------------===//
240 // Selection DAG Condition Codes
241
242 class CondCode; // ISD::CondCode enums
243 def SETOEQ : CondCode; def SETOGT : CondCode;
244 def SETOGE : CondCode; def SETOLT : CondCode; def SETOLE : CondCode;
245 def SETONE : CondCode; def SETO   : CondCode; def SETUO  : CondCode;
246 def SETUEQ : CondCode; def SETUGT : CondCode; def SETUGE : CondCode;
247 def SETULT : CondCode; def SETULE : CondCode; def SETUNE : CondCode;
248
249 def SETEQ : CondCode; def SETGT : CondCode; def SETGE : CondCode;
250 def SETLT : CondCode; def SETLE : CondCode; def SETNE : CondCode;
251
252
253 //===----------------------------------------------------------------------===//
254 // Selection DAG Node Transformation Functions.
255 //
256 // This mechanism allows targets to manipulate nodes in the output DAG once a
257 // match has been formed.  This is typically used to manipulate immediate
258 // values.
259 //
260 class SDNodeXForm<SDNode opc, code xformFunction> {
261   SDNode Opcode = opc;
262   code XFormFunction = xformFunction;
263 }
264
265 def NOOP_SDNodeXForm : SDNodeXForm<imm, [{}]>;
266
267
268 //===----------------------------------------------------------------------===//
269 // Selection DAG Pattern Fragments.
270 //
271 // Pattern fragments are reusable chunks of dags that match specific things.
272 // They can take arguments and have C++ predicates that control whether they
273 // match.  They are intended to make the patterns for common instructions more
274 // compact and readable.
275 //
276
277 /// PatFrag - Represents a pattern fragment.  This can match something on the
278 /// DAG, frame a single node to multiply nested other fragments.
279 ///
280 class PatFrag<dag ops, dag frag, code pred = [{}],
281               SDNodeXForm xform = NOOP_SDNodeXForm> {
282   dag Operands = ops;
283   dag Fragment = frag;
284   code Predicate = pred;
285   SDNodeXForm OperandTransform = xform;
286 }
287
288 // PatLeaf's are pattern fragments that have no operands.  This is just a helper
289 // to define immediates and other common things concisely.
290 class PatLeaf<dag frag, code pred = [{}], SDNodeXForm xform = NOOP_SDNodeXForm>
291  : PatFrag<(ops), frag, pred, xform>;
292
293 // Leaf fragments.
294
295 def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>;
296
297 def vtInt      : PatLeaf<(vt),  [{ return MVT::isInteger(N->getVT()); }]>;
298 def vtFP       : PatLeaf<(vt),  [{ return MVT::isFloatingPoint(N->getVT()); }]>;
299
300 // Other helper fragments.
301
302 def not  : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>;
303 def ineg : PatFrag<(ops node:$in), (sub 0, node:$in)>;
304
305
306 // setcc convenience fragments.
307 def setoeq : PatFrag<(ops node:$lhs, node:$rhs),
308                      (setcc node:$lhs, node:$rhs, SETOEQ)>;
309 def setogt : PatFrag<(ops node:$lhs, node:$rhs),
310                      (setcc node:$lhs, node:$rhs, SETOGT)>;
311 def setoge : PatFrag<(ops node:$lhs, node:$rhs),
312                      (setcc node:$lhs, node:$rhs, SETOGE)>;
313 def setolt : PatFrag<(ops node:$lhs, node:$rhs),
314                      (setcc node:$lhs, node:$rhs, SETOLT)>;
315 def setole : PatFrag<(ops node:$lhs, node:$rhs),
316                      (setcc node:$lhs, node:$rhs, SETOLE)>;
317 def setone : PatFrag<(ops node:$lhs, node:$rhs),
318                      (setcc node:$lhs, node:$rhs, SETONE)>;
319 def seto   : PatFrag<(ops node:$lhs, node:$rhs),
320                      (setcc node:$lhs, node:$rhs, SETO)>;
321 def setuo  : PatFrag<(ops node:$lhs, node:$rhs),
322                      (setcc node:$lhs, node:$rhs, SETUO)>;
323 def setueq : PatFrag<(ops node:$lhs, node:$rhs),
324                      (setcc node:$lhs, node:$rhs, SETUEQ)>;
325 def setugt : PatFrag<(ops node:$lhs, node:$rhs),
326                      (setcc node:$lhs, node:$rhs, SETUGT)>;
327 def setuge : PatFrag<(ops node:$lhs, node:$rhs),
328                      (setcc node:$lhs, node:$rhs, SETUGE)>;
329 def setult : PatFrag<(ops node:$lhs, node:$rhs),
330                      (setcc node:$lhs, node:$rhs, SETULT)>;
331 def setule : PatFrag<(ops node:$lhs, node:$rhs),
332                      (setcc node:$lhs, node:$rhs, SETULE)>;
333 def setune : PatFrag<(ops node:$lhs, node:$rhs),
334                      (setcc node:$lhs, node:$rhs, SETUNE)>;
335 def seteq  : PatFrag<(ops node:$lhs, node:$rhs),
336                      (setcc node:$lhs, node:$rhs, SETEQ)>;
337 def setgt  : PatFrag<(ops node:$lhs, node:$rhs),
338                      (setcc node:$lhs, node:$rhs, SETGT)>;
339 def setge  : PatFrag<(ops node:$lhs, node:$rhs),
340                      (setcc node:$lhs, node:$rhs, SETGE)>;
341 def setlt  : PatFrag<(ops node:$lhs, node:$rhs),
342                      (setcc node:$lhs, node:$rhs, SETLT)>;
343 def setle  : PatFrag<(ops node:$lhs, node:$rhs),
344                      (setcc node:$lhs, node:$rhs, SETLE)>;
345 def setne  : PatFrag<(ops node:$lhs, node:$rhs),
346                      (setcc node:$lhs, node:$rhs, SETNE)>;
347
348 //===----------------------------------------------------------------------===//
349 // Selection DAG Pattern Support.
350 //
351 // Patterns are what are actually matched against the target-flavored
352 // instruction selection DAG.  Instructions defined by the target implicitly
353 // define patterns in most cases, but patterns can also be explicitly added when
354 // an operation is defined by a sequence of instructions (e.g. loading a large
355 // immediate value on RISC targets that do not support immediates as large as
356 // their GPRs).
357 //
358
359 class Pattern<dag patternToMatch, list<dag> resultInstrs> {
360   dag       PatternToMatch = patternToMatch;
361   list<dag> ResultInstrs   = resultInstrs;
362 }
363
364 // Pat - A simple (but common) form of a pattern, which produces a simple result
365 // not needing a full list.
366 class Pat<dag pattern, dag result> : Pattern<pattern, [result]>;
367
368 //===----------------------------------------------------------------------===//
369 // Complex pattern definitions.
370 //
371 // Complex patterns, e.g. X86 addressing mode, requires pattern matching code
372 // in C++. NumOperands is the number of operands returned by the select function;
373 // SelectFunc is the name of the function used to pattern match the max. pattern;
374 // RootNodes are the list of possible root nodes of the sub-dags to match.
375 // e.g. X86 addressing mode - def addr : ComplexPattern<4, "SelectAddr", [add]>;
376 //
377 class ComplexPattern<ValueType ty, int numops, string fn, list<SDNode> roots = []> {
378   ValueType Ty = ty;
379   int NumOperands = numops;
380   string SelectFunc = fn;
381   list<SDNode> RootNodes = roots;
382 }