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