1 //===- TargetSelectionDAG.td - Common code for DAG isels ---*- tablegen -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the target-independent interfaces used by SelectionDAG
11 // instruction selection generators.
13 //===----------------------------------------------------------------------===//
15 //===----------------------------------------------------------------------===//
16 // Selection DAG Type Constraint definitions.
18 // Note that the semantics of these constraints are hard coded into tblgen. To
19 // modify or add constraints, you have to hack tblgen.
22 class SDTypeConstraint<int opnum> {
23 int OperandNum = opnum;
26 // SDTCisVT - The specified operand has exactly this VT.
27 class SDTCisVT <int OpNum, ValueType vt> : SDTypeConstraint<OpNum> {
31 // SDTCisInt - The specified operand is has integer type.
32 class SDTCisInt<int OpNum> : SDTypeConstraint<OpNum>;
34 // SDTCisFP - The specified operand is has floating point type.
35 class SDTCisFP <int OpNum> : SDTypeConstraint<OpNum>;
37 // SDTCisSameAs - The two specified operands have identical types.
38 class SDTCisSameAs<int OpNum, int OtherOp> : SDTypeConstraint<OpNum> {
39 int OtherOperandNum = OtherOp;
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;
48 class SDTCisOpSmallerThanOp<int SmallOp, int BigOp> : SDTypeConstraint<SmallOp>{
49 int BigOperandNum = BigOp;
52 //===----------------------------------------------------------------------===//
53 // Selection DAG Type Profile definitions.
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.
60 // SDTypeProfile - This profile describes the type requirements of a Selection
62 class SDTypeProfile<int numresults, int numoperands,
63 list<SDTypeConstraint> constraints> {
64 int NumResults = numresults;
65 int NumOperands = numoperands;
66 list<SDTypeConstraint> Constraints = constraints;
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>
76 def SDTIntShiftOp : SDTypeProfile<1, 2, [ // shl, sra, srl
77 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisInt<2>
79 def SDTFPBinOp : SDTypeProfile<1, 2, [ // fadd, fmul, etc.
80 SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisFP<0>
82 def SDTIntUnaryOp : SDTypeProfile<1, 1, [ // ctlz
83 SDTCisSameAs<0, 1>, SDTCisInt<0>
85 def SDTIntExtendOp : SDTypeProfile<1, 1, [ // sext, zext, anyext
86 SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<1, 0>
88 def SDTIntTruncOp : SDTypeProfile<1, 1, [ // trunc
89 SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<0, 1>
91 def SDTFPUnaryOp : SDTypeProfile<1, 1, [ // fneg, fsqrt, etc
92 SDTCisSameAs<0, 1>, SDTCisFP<0>
94 def SDTFPRoundOp : SDTypeProfile<1, 1, [ // fround
95 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<0, 1>
97 def SDTFPExtendOp : SDTypeProfile<1, 1, [ // fextend
98 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<1, 0>
100 def SDTIntToFPOp : SDTypeProfile<1, 1, [ // [su]int_to_fp
101 SDTCisFP<0>, SDTCisInt<1>
103 def SDTFPToIntOp : SDTypeProfile<1, 1, [ // fp_to_[su]int
104 SDTCisInt<0>, SDTCisFP<1>
106 def SDTExtInreg : SDTypeProfile<1, 2, [ // sext_inreg
107 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisVT<2, OtherVT>,
108 SDTCisVTSmallerThanOp<2, 1>
111 def SDTSetCC : SDTypeProfile<1, 3, [ // setcc
112 SDTCisInt<0>, SDTCisSameAs<1, 2>, SDTCisVT<3, OtherVT>
115 def SDTSelect : SDTypeProfile<1, 3, [ // select
116 SDTCisInt<1>, SDTCisSameAs<0, 2>, SDTCisSameAs<2, 3>
119 def SDTBr : SDTypeProfile<0, 1, [ // br
123 def SDTBrCond : SDTypeProfile<0, 2, [ // brcond
124 SDTCisInt<0>, SDTCisVT<1, OtherVT>
127 def SDTRet : SDTypeProfile<0, 0, [ // ret
130 def SDTWritePort : SDTypeProfile<0, 2, [ // writeport
131 SDTCisInt<0>, SDTCisInt<1>
134 //===----------------------------------------------------------------------===//
135 // Selection DAG Node Properties.
137 // Note: These are hard coded into tblgen.
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
144 //===----------------------------------------------------------------------===//
145 // Selection DAG Node definitions.
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;
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]>;
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>;
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>;
206 def fround : SDNode<"ISD::FP_ROUND" , SDTFPRoundOp>;
207 def fextend : SDNode<"ISD::FP_EXTEND" , SDTFPExtendOp>;
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>;
214 def setcc : SDNode<"ISD::SETCC" , SDTSetCC>;
215 def select : SDNode<"ISD::SELECT" , SDTSelect>;
217 def br : SDNode<"ISD::BR" , SDTBr, [SDNPHasChain]>;
218 def brcond : SDNode<"ISD::BRCOND" , SDTBrCond, [SDNPHasChain]>;
219 def ret : SDNode<"ISD::RET" , SDTRet, [SDNPHasChain]>;
221 def writeport : SDNode<"ISD::WRITEPORT" , SDTWritePort, [SDNPHasChain]>;
223 //===----------------------------------------------------------------------===//
224 // Selection DAG Condition Codes
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;
233 def SETEQ : CondCode; def SETGT : CondCode; def SETGE : CondCode;
234 def SETLT : CondCode; def SETLE : CondCode; def SETNE : CondCode;
237 //===----------------------------------------------------------------------===//
238 // Selection DAG Node Transformation Functions.
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
244 class SDNodeXForm<SDNode opc, code xformFunction> {
246 code XFormFunction = xformFunction;
249 def NOOP_SDNodeXForm : SDNodeXForm<imm, [{}]>;
252 //===----------------------------------------------------------------------===//
253 // Selection DAG Pattern Fragments.
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.
261 /// PatFrag - Represents a pattern fragment. This can match something on the
262 /// DAG, frame a single node to multiply nested other fragments.
264 class PatFrag<dag ops, dag frag, code pred = [{}],
265 SDNodeXForm xform = NOOP_SDNodeXForm> {
268 code Predicate = pred;
269 SDNodeXForm OperandTransform = xform;
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>;
279 def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>;
281 def vtInt : PatLeaf<(vt), [{ return MVT::isInteger(N->getVT()); }]>;
282 def vtFP : PatLeaf<(vt), [{ return MVT::isFloatingPoint(N->getVT()); }]>;
284 // Other helper fragments.
286 def not : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>;
287 def ineg : PatFrag<(ops node:$in), (sub 0, node:$in)>;
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)>;
332 //===----------------------------------------------------------------------===//
333 // Selection DAG Pattern Support.
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
343 class Pattern<dag patternToMatch, list<dag> resultInstrs> {
344 dag PatternToMatch = patternToMatch;
345 list<dag> ResultInstrs = resultInstrs;
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]>;