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 class SDTCisPtrTy<int OpNum> : SDTypeConstraint<OpNum>;
33 // SDTCisInt - The specified operand is has integer type.
34 class SDTCisInt<int OpNum> : SDTypeConstraint<OpNum>;
36 // SDTCisFP - The specified operand is has floating point type.
37 class SDTCisFP<int OpNum> : SDTypeConstraint<OpNum>;
39 // SDTCisSameAs - The two specified operands have identical types.
40 class SDTCisSameAs<int OpNum, int OtherOp> : SDTypeConstraint<OpNum> {
41 int OtherOperandNum = OtherOp;
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;
50 class SDTCisOpSmallerThanOp<int SmallOp, int BigOp> : SDTypeConstraint<SmallOp>{
51 int BigOperandNum = BigOp;
54 //===----------------------------------------------------------------------===//
55 // Selection DAG Type Profile definitions.
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.
62 // SDTypeProfile - This profile describes the type requirements of a Selection
64 class SDTypeProfile<int numresults, int numoperands,
65 list<SDTypeConstraint> constraints> {
66 int NumResults = numresults;
67 int NumOperands = numoperands;
68 list<SDTypeConstraint> Constraints = constraints;
72 def SDTImm : SDTypeProfile<1, 0, [SDTCisInt<0>]>; // for 'imm'.
73 def SDTOther : SDTypeProfile<1, 0, [SDTCisVT<0, OtherVT>]>; // for 'vt'.
74 def SDTUNDEF : SDTypeProfile<1, 0, []>; // for 'undef'.
75 def SDTIntBinOp : SDTypeProfile<1, 2, [ // add, and, or, xor, udiv, etc.
76 SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisInt<0>
78 def SDTIntShiftOp : SDTypeProfile<1, 2, [ // shl, sra, srl
79 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisInt<2>
81 def SDTFPBinOp : SDTypeProfile<1, 2, [ // fadd, fmul, etc.
82 SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisFP<0>
84 def SDTIntUnaryOp : SDTypeProfile<1, 1, [ // ctlz
85 SDTCisSameAs<0, 1>, SDTCisInt<0>
87 def SDTIntExtendOp : SDTypeProfile<1, 1, [ // sext, zext, anyext
88 SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<1, 0>
90 def SDTIntTruncOp : SDTypeProfile<1, 1, [ // trunc
91 SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<0, 1>
93 def SDTFPUnaryOp : SDTypeProfile<1, 1, [ // fneg, fsqrt, etc
94 SDTCisSameAs<0, 1>, SDTCisFP<0>
96 def SDTFPRoundOp : SDTypeProfile<1, 1, [ // fround
97 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<0, 1>
99 def SDTFPExtendOp : SDTypeProfile<1, 1, [ // fextend
100 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<1, 0>
102 def SDTIntToFPOp : SDTypeProfile<1, 1, [ // [su]int_to_fp
103 SDTCisFP<0>, SDTCisInt<1>
105 def SDTFPToIntOp : SDTypeProfile<1, 1, [ // fp_to_[su]int
106 SDTCisInt<0>, SDTCisFP<1>
108 def SDTExtInreg : SDTypeProfile<1, 2, [ // sext_inreg
109 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisVT<2, OtherVT>,
110 SDTCisVTSmallerThanOp<2, 1>
113 def SDTSetCC : SDTypeProfile<1, 3, [ // setcc
114 SDTCisInt<0>, SDTCisSameAs<1, 2>, SDTCisVT<3, OtherVT>
117 def SDTSelect : SDTypeProfile<1, 3, [ // select
118 SDTCisInt<1>, SDTCisSameAs<0, 2>, SDTCisSameAs<2, 3>
121 def SDTBr : SDTypeProfile<0, 1, [ // br
125 def SDTBrCond : SDTypeProfile<0, 2, [ // brcond
126 SDTCisInt<0>, SDTCisVT<1, OtherVT>
129 def SDTRet : SDTypeProfile<0, 0, [ // ret
132 def SDTWritePort : SDTypeProfile<0, 2, [ // writeport
133 SDTCisInt<0>, SDTCisInt<1>
136 def SDTLoad : SDTypeProfile<1, 1, [ // load
140 def SDTStore : SDTypeProfile<0, 2, [ // store
144 //===----------------------------------------------------------------------===//
145 // Selection DAG Node Properties.
147 // Note: These are hard coded into tblgen.
149 class SDNodeProperty;
150 def SDNPCommutative : SDNodeProperty; // X op Y == Y op X
151 def SDNPAssociative : SDNodeProperty; // (X op Y) op Z == X op (Y op Z)
152 def SDNPHasChain : SDNodeProperty; // R/W chain operand and result
154 //===----------------------------------------------------------------------===//
155 // Selection DAG Node definitions.
157 class SDNode<string opcode, SDTypeProfile typeprof,
158 list<SDNodeProperty> props = [], string sdclass = "SDNode"> {
159 string Opcode = opcode;
160 string SDClass = sdclass;
161 list<SDNodeProperty> Properties = props;
162 SDTypeProfile TypeProfile = typeprof;
168 def imm : SDNode<"ISD::Constant" , SDTImm , [], "ConstantSDNode">;
169 def vt : SDNode<"ISD::VALUETYPE" , SDTOther , [], "VTSDNode">;
170 def bb : SDNode<"ISD::BasicBlock", SDTOther , [], "BasicBlockSDNode">;
171 def cond : SDNode<"ISD::CONDCODE" , SDTOther , [], "CondCodeSDNode">;
172 def undef : SDNode<"ISD::UNDEF" , SDTUNDEF , []>;
173 def globaladdr : SDNode<"ISD::GlobalAddress", SDTImm, [],
174 "GlobalAddressSDNode">;
175 def tglobaladdr : SDNode<"ISD::TargetGlobalAddress", SDTImm, [],
176 "GlobalAddressSDNode">;
177 def tconstpool : SDNode<"ISD::TargetConstantPool", SDTImm, [],
178 "ConstantPoolSDNode">;
179 def add : SDNode<"ISD::ADD" , SDTIntBinOp ,
180 [SDNPCommutative, SDNPAssociative]>;
181 def sub : SDNode<"ISD::SUB" , SDTIntBinOp>;
182 def mul : SDNode<"ISD::MUL" , SDTIntBinOp,
183 [SDNPCommutative, SDNPAssociative]>;
184 def mulhs : SDNode<"ISD::MULHS" , SDTIntBinOp, [SDNPCommutative]>;
185 def mulhu : SDNode<"ISD::MULHU" , SDTIntBinOp, [SDNPCommutative]>;
186 def sdiv : SDNode<"ISD::SDIV" , SDTIntBinOp>;
187 def udiv : SDNode<"ISD::UDIV" , SDTIntBinOp>;
188 def srem : SDNode<"ISD::SREM" , SDTIntBinOp>;
189 def urem : SDNode<"ISD::UREM" , SDTIntBinOp>;
190 def srl : SDNode<"ISD::SRL" , SDTIntShiftOp>;
191 def sra : SDNode<"ISD::SRA" , SDTIntShiftOp>;
192 def shl : SDNode<"ISD::SHL" , SDTIntShiftOp>;
193 def and : SDNode<"ISD::AND" , SDTIntBinOp,
194 [SDNPCommutative, SDNPAssociative]>;
195 def or : SDNode<"ISD::OR" , SDTIntBinOp,
196 [SDNPCommutative, SDNPAssociative]>;
197 def xor : SDNode<"ISD::XOR" , SDTIntBinOp,
198 [SDNPCommutative, SDNPAssociative]>;
200 def sext_inreg : SDNode<"ISD::SIGN_EXTEND_INREG", SDTExtInreg>;
201 def ctlz : SDNode<"ISD::CTLZ" , SDTIntUnaryOp>;
202 def cttz : SDNode<"ISD::CTTZ" , SDTIntUnaryOp>;
203 def ctpop : SDNode<"ISD::CTPOP" , SDTIntUnaryOp>;
204 def sext : SDNode<"ISD::SIGN_EXTEND", SDTIntExtendOp>;
205 def zext : SDNode<"ISD::ZERO_EXTEND", SDTIntExtendOp>;
206 def anyext : SDNode<"ISD::ANY_EXTEND" , SDTIntExtendOp>;
207 def trunc : SDNode<"ISD::TRUNCATE" , SDTIntTruncOp>;
209 def fadd : SDNode<"ISD::FADD" , SDTFPBinOp, [SDNPCommutative]>;
210 def fsub : SDNode<"ISD::FSUB" , SDTFPBinOp>;
211 def fmul : SDNode<"ISD::FMUL" , SDTFPBinOp, [SDNPCommutative]>;
212 def fdiv : SDNode<"ISD::FDIV" , SDTFPBinOp>;
213 def frem : SDNode<"ISD::FREM" , SDTFPBinOp>;
214 def fabs : SDNode<"ISD::FABS" , SDTFPUnaryOp>;
215 def fneg : SDNode<"ISD::FNEG" , SDTFPUnaryOp>;
216 def fsqrt : SDNode<"ISD::FSQRT" , SDTFPUnaryOp>;
218 def fround : SDNode<"ISD::FP_ROUND" , SDTFPRoundOp>;
219 def fextend : SDNode<"ISD::FP_EXTEND" , SDTFPExtendOp>;
221 def sint_to_fp : SDNode<"ISD::SINT_TO_FP" , SDTIntToFPOp>;
222 def uint_to_fp : SDNode<"ISD::UINT_TO_FP" , SDTIntToFPOp>;
223 def fp_to_sint : SDNode<"ISD::FP_TO_SINT" , SDTFPToIntOp>;
224 def fp_to_uint : SDNode<"ISD::FP_TO_UINT" , SDTFPToIntOp>;
226 def setcc : SDNode<"ISD::SETCC" , SDTSetCC>;
227 def select : SDNode<"ISD::SELECT" , SDTSelect>;
229 def br : SDNode<"ISD::BR" , SDTBr, [SDNPHasChain]>;
230 def brcond : SDNode<"ISD::BRCOND" , SDTBrCond, [SDNPHasChain]>;
231 def ret : SDNode<"ISD::RET" , SDTRet, [SDNPHasChain]>;
233 def writeport : SDNode<"ISD::WRITEPORT" , SDTWritePort, [SDNPHasChain]>;
235 def load : SDNode<"ISD::LOAD" , SDTLoad, [SDNPHasChain]>;
236 def store : SDNode<"ISD::STORE" , SDTStore, [SDNPHasChain]>;
238 //===----------------------------------------------------------------------===//
239 // Selection DAG Condition Codes
241 class CondCode; // ISD::CondCode enums
242 def SETOEQ : CondCode; def SETOGT : CondCode;
243 def SETOGE : CondCode; def SETOLT : CondCode; def SETOLE : CondCode;
244 def SETONE : CondCode; def SETO : CondCode; def SETUO : CondCode;
245 def SETUEQ : CondCode; def SETUGT : CondCode; def SETUGE : CondCode;
246 def SETULT : CondCode; def SETULE : CondCode; def SETUNE : CondCode;
248 def SETEQ : CondCode; def SETGT : CondCode; def SETGE : CondCode;
249 def SETLT : CondCode; def SETLE : CondCode; def SETNE : CondCode;
252 //===----------------------------------------------------------------------===//
253 // Selection DAG Node Transformation Functions.
255 // This mechanism allows targets to manipulate nodes in the output DAG once a
256 // match has been formed. This is typically used to manipulate immediate
259 class SDNodeXForm<SDNode opc, code xformFunction> {
261 code XFormFunction = xformFunction;
264 def NOOP_SDNodeXForm : SDNodeXForm<imm, [{}]>;
267 //===----------------------------------------------------------------------===//
268 // Selection DAG Pattern Fragments.
270 // Pattern fragments are reusable chunks of dags that match specific things.
271 // They can take arguments and have C++ predicates that control whether they
272 // match. They are intended to make the patterns for common instructions more
273 // compact and readable.
276 /// PatFrag - Represents a pattern fragment. This can match something on the
277 /// DAG, frame a single node to multiply nested other fragments.
279 class PatFrag<dag ops, dag frag, code pred = [{}],
280 SDNodeXForm xform = NOOP_SDNodeXForm> {
283 code Predicate = pred;
284 SDNodeXForm OperandTransform = xform;
287 // PatLeaf's are pattern fragments that have no operands. This is just a helper
288 // to define immediates and other common things concisely.
289 class PatLeaf<dag frag, code pred = [{}], SDNodeXForm xform = NOOP_SDNodeXForm>
290 : PatFrag<(ops), frag, pred, xform>;
294 def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>;
296 def vtInt : PatLeaf<(vt), [{ return MVT::isInteger(N->getVT()); }]>;
297 def vtFP : PatLeaf<(vt), [{ return MVT::isFloatingPoint(N->getVT()); }]>;
299 // Other helper fragments.
301 def not : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>;
302 def ineg : PatFrag<(ops node:$in), (sub 0, node:$in)>;
305 // setcc convenience fragments.
306 def setoeq : PatFrag<(ops node:$lhs, node:$rhs),
307 (setcc node:$lhs, node:$rhs, SETOEQ)>;
308 def setogt : PatFrag<(ops node:$lhs, node:$rhs),
309 (setcc node:$lhs, node:$rhs, SETOGT)>;
310 def setoge : PatFrag<(ops node:$lhs, node:$rhs),
311 (setcc node:$lhs, node:$rhs, SETOGE)>;
312 def setolt : PatFrag<(ops node:$lhs, node:$rhs),
313 (setcc node:$lhs, node:$rhs, SETOLT)>;
314 def setole : PatFrag<(ops node:$lhs, node:$rhs),
315 (setcc node:$lhs, node:$rhs, SETOLE)>;
316 def setone : PatFrag<(ops node:$lhs, node:$rhs),
317 (setcc node:$lhs, node:$rhs, SETONE)>;
318 def seto : PatFrag<(ops node:$lhs, node:$rhs),
319 (setcc node:$lhs, node:$rhs, SETO)>;
320 def setuo : PatFrag<(ops node:$lhs, node:$rhs),
321 (setcc node:$lhs, node:$rhs, SETUO)>;
322 def setueq : PatFrag<(ops node:$lhs, node:$rhs),
323 (setcc node:$lhs, node:$rhs, SETUEQ)>;
324 def setugt : PatFrag<(ops node:$lhs, node:$rhs),
325 (setcc node:$lhs, node:$rhs, SETUGT)>;
326 def setuge : PatFrag<(ops node:$lhs, node:$rhs),
327 (setcc node:$lhs, node:$rhs, SETUGE)>;
328 def setult : PatFrag<(ops node:$lhs, node:$rhs),
329 (setcc node:$lhs, node:$rhs, SETULT)>;
330 def setule : PatFrag<(ops node:$lhs, node:$rhs),
331 (setcc node:$lhs, node:$rhs, SETULE)>;
332 def setune : PatFrag<(ops node:$lhs, node:$rhs),
333 (setcc node:$lhs, node:$rhs, SETUNE)>;
334 def seteq : PatFrag<(ops node:$lhs, node:$rhs),
335 (setcc node:$lhs, node:$rhs, SETEQ)>;
336 def setgt : PatFrag<(ops node:$lhs, node:$rhs),
337 (setcc node:$lhs, node:$rhs, SETGT)>;
338 def setge : PatFrag<(ops node:$lhs, node:$rhs),
339 (setcc node:$lhs, node:$rhs, SETGE)>;
340 def setlt : PatFrag<(ops node:$lhs, node:$rhs),
341 (setcc node:$lhs, node:$rhs, SETLT)>;
342 def setle : PatFrag<(ops node:$lhs, node:$rhs),
343 (setcc node:$lhs, node:$rhs, SETLE)>;
344 def setne : PatFrag<(ops node:$lhs, node:$rhs),
345 (setcc node:$lhs, node:$rhs, SETNE)>;
347 //===----------------------------------------------------------------------===//
348 // Selection DAG Pattern Support.
350 // Patterns are what are actually matched against the target-flavored
351 // instruction selection DAG. Instructions defined by the target implicitly
352 // define patterns in most cases, but patterns can also be explicitly added when
353 // an operation is defined by a sequence of instructions (e.g. loading a large
354 // immediate value on RISC targets that do not support immediates as large as
358 class Pattern<dag patternToMatch, list<dag> resultInstrs> {
359 dag PatternToMatch = patternToMatch;
360 list<dag> ResultInstrs = resultInstrs;
363 // Pat - A simple (but common) form of a pattern, which produces a simple result
364 // not needing a full list.
365 class Pat<dag pattern, dag result> : Pattern<pattern, [result]>;
367 //===----------------------------------------------------------------------===//
368 // Complex pattern definitions.
370 // Complex patterns, e.g. X86 addressing mode, requires pattern matching code
371 // in C++. NumOperands is the number of operands returned by the select function;
372 // SelectFunc is the name of the function used to pattern match the max. pattern;
373 // RootNodes are the list of possible root nodes of the sub-dags to match.
374 // e.g. X86 addressing mode - def addr : ComplexPattern<4, "SelectAddr", [add]>;
376 class ComplexPattern<ValueType ty, int numops, string fn, list<SDNode> roots = []> {
378 int NumOperands = numops;
379 string SelectFunc = fn;
380 list<SDNode> RootNodes = roots;