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