blob: 996777f1ed8b1ed3d0f87198d2dd7a30ac82e838 [file] [log] [blame]
Chris Lattnerd83571b2005-10-10 06:00:30 +00001//===- 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
22class SDTypeConstraint<int opnum> {
23 int OperandNum = opnum;
24}
25
26// SDTCisVT - The specified operand has exactly this VT.
27class SDTCisVT <int OpNum, ValueType vt> : SDTypeConstraint<OpNum> {
28 ValueType VT = vt;
29}
30
31// SDTCisInt - The specified operand is has integer type.
32class SDTCisInt<int OpNum> : SDTypeConstraint<OpNum>;
33
34// SDTCisFP - The specified operand is has floating point type.
35class SDTCisFP <int OpNum> : SDTypeConstraint<OpNum>;
36
37// SDTCisSameAs - The two specified operands have identical types.
38class 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.
44class SDTCisVTSmallerThanOp<int OpNum, int OtherOp> : SDTypeConstraint<OpNum> {
45 int OtherOperandNum = OtherOp;
46}
47
Chris Lattner6e83cbf2005-10-14 04:55:10 +000048class SDTCisOpSmallerThanOp<int SmallOp, int BigOp> : SDTypeConstraint<SmallOp>{
49 int BigOperandNum = BigOp;
50}
51
Chris Lattnerd83571b2005-10-10 06:00:30 +000052//===----------------------------------------------------------------------===//
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.
62class 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.
70def SDTImm : SDTypeProfile<1, 0, [SDTCisInt<0>]>; // for 'imm'.
Evan Chenge8531382005-12-04 08:13:17 +000071def SDTOther : SDTypeProfile<1, 0, [SDTCisVT<0, OtherVT>]>; // for 'vt'.
Chris Lattner3a4b1412005-10-25 21:03:14 +000072def SDTUNDEF : SDTypeProfile<1, 0, []>; // for 'undef'.
Chris Lattnerd83571b2005-10-10 06:00:30 +000073def SDTIntBinOp : SDTypeProfile<1, 2, [ // add, and, or, xor, udiv, etc.
74 SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisInt<0>
75]>;
Chris Lattner282c2af2005-12-05 02:37:26 +000076def SDTIntShiftOp : SDTypeProfile<1, 2, [ // shl, sra, srl
77 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisInt<2>
78]>;
Chris Lattnerd83571b2005-10-10 06:00:30 +000079def SDTFPBinOp : SDTypeProfile<1, 2, [ // fadd, fmul, etc.
80 SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisFP<0>
81]>;
82def SDTIntUnaryOp : SDTypeProfile<1, 1, [ // ctlz
83 SDTCisSameAs<0, 1>, SDTCisInt<0>
84]>;
Chris Lattner56f31f52005-10-14 06:40:20 +000085def SDTIntExtendOp : SDTypeProfile<1, 1, [ // sext, zext, anyext
86 SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<1, 0>
87]>;
88def SDTIntTruncOp : SDTypeProfile<1, 1, [ // trunc
89 SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<0, 1>
90]>;
Chris Lattnerd83571b2005-10-10 06:00:30 +000091def SDTFPUnaryOp : SDTypeProfile<1, 1, [ // fneg, fsqrt, etc
92 SDTCisSameAs<0, 1>, SDTCisFP<0>
93]>;
Chris Lattner6e83cbf2005-10-14 04:55:10 +000094def SDTFPRoundOp : SDTypeProfile<1, 1, [ // fround
95 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<0, 1>
96]>;
97def SDTFPExtendOp : SDTypeProfile<1, 1, [ // fextend
98 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<1, 0>
99]>;
Duraid Madina6c912bf2005-11-01 03:07:25 +0000100def SDTIntToFPOp : SDTypeProfile<1, 1, [ // [su]int_to_fp
101 SDTCisFP<0>, SDTCisInt<1>
102]>;
103def SDTFPToIntOp : SDTypeProfile<1, 1, [ // fp_to_[su]int
104 SDTCisInt<0>, SDTCisFP<1>
105]>;
Chris Lattnerd83571b2005-10-10 06:00:30 +0000106def SDTExtInreg : SDTypeProfile<1, 2, [ // sext_inreg
107 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisVT<2, OtherVT>,
108 SDTCisVTSmallerThanOp<2, 1>
109]>;
110
Chris Lattnerce9580f2005-10-26 17:00:25 +0000111def SDTSetCC : SDTypeProfile<1, 3, [ // setcc
112 SDTCisInt<0>, SDTCisSameAs<1, 2>, SDTCisVT<3, OtherVT>
113]>;
114
Duraid Madina17decbb2005-11-02 02:37:18 +0000115def SDTSelect : SDTypeProfile<1, 3, [ // select
116 SDTCisInt<1>, SDTCisSameAs<0, 2>, SDTCisSameAs<2, 3>
117]>;
118
Evan Chenge8531382005-12-04 08:13:17 +0000119def SDTBr : SDTypeProfile<0, 1, [ // br
120 SDTCisVT<0, OtherVT>
121]>;
122
123def SDTBrCond : SDTypeProfile<0, 2, [ // brcond
124 SDTCisInt<0>, SDTCisVT<1, OtherVT>
125]>;
126
127def SDTRet : SDTypeProfile<0, 0, [ // ret
128]>;
129
130def SDTWritePort : SDTypeProfile<0, 2, [ // writeport
131 SDTCisInt<0>, SDTCisInt<1>
132]>;
133
Evan Cheng790af6d2005-12-08 04:28:48 +0000134def SDTLoad : SDTypeProfile<1, 1, [ // load
135 SDTCisInt<1>
136]>;
137
Chris Lattnerd83571b2005-10-10 06:00:30 +0000138//===----------------------------------------------------------------------===//
139// Selection DAG Node Properties.
140//
141// Note: These are hard coded into tblgen.
142//
143class SDNodeProperty;
144def SDNPCommutative : SDNodeProperty; // X op Y == Y op X
145def SDNPAssociative : SDNodeProperty; // (X op Y) op Z == X op (Y op Z)
Evan Chenge8531382005-12-04 08:13:17 +0000146def SDNPHasChain : SDNodeProperty; // R/W chain operand and result
Chris Lattnerd83571b2005-10-10 06:00:30 +0000147
148//===----------------------------------------------------------------------===//
149// Selection DAG Node definitions.
150//
151class SDNode<string opcode, SDTypeProfile typeprof,
152 list<SDNodeProperty> props = [], string sdclass = "SDNode"> {
153 string Opcode = opcode;
154 string SDClass = sdclass;
155 list<SDNodeProperty> Properties = props;
156 SDTypeProfile TypeProfile = typeprof;
157}
158
159def set;
160def node;
161
162def imm : SDNode<"ISD::Constant" , SDTImm , [], "ConstantSDNode">;
Evan Chenge8531382005-12-04 08:13:17 +0000163def vt : SDNode<"ISD::VALUETYPE" , SDTOther , [], "VTSDNode">;
164def bb : SDNode<"ISD::BasicBlock", SDTOther , [], "BasicBlockSDNode">;
165def cond : SDNode<"ISD::CONDCODE" , SDTOther , [], "CondCodeSDNode">;
Chris Lattner3a4b1412005-10-25 21:03:14 +0000166def undef : SDNode<"ISD::UNDEF" , SDTUNDEF , []>;
Chris Lattner84045822005-11-17 07:20:15 +0000167def globaladdr : SDNode<"ISD::GlobalAddress", SDTImm, [],
168 "GlobalAddressSDNode">;
169def tglobaladdr : SDNode<"ISD::TargetGlobalAddress", SDTImm, [],
170 "GlobalAddressSDNode">;
Chris Lattnerd83571b2005-10-10 06:00:30 +0000171def add : SDNode<"ISD::ADD" , SDTIntBinOp ,
172 [SDNPCommutative, SDNPAssociative]>;
173def sub : SDNode<"ISD::SUB" , SDTIntBinOp>;
174def mul : SDNode<"ISD::MUL" , SDTIntBinOp,
175 [SDNPCommutative, SDNPAssociative]>;
176def mulhs : SDNode<"ISD::MULHS" , SDTIntBinOp, [SDNPCommutative]>;
177def mulhu : SDNode<"ISD::MULHU" , SDTIntBinOp, [SDNPCommutative]>;
178def sdiv : SDNode<"ISD::SDIV" , SDTIntBinOp>;
179def udiv : SDNode<"ISD::UDIV" , SDTIntBinOp>;
180def srem : SDNode<"ISD::SREM" , SDTIntBinOp>;
181def urem : SDNode<"ISD::UREM" , SDTIntBinOp>;
Chris Lattner282c2af2005-12-05 02:37:26 +0000182def srl : SDNode<"ISD::SRL" , SDTIntShiftOp>;
183def sra : SDNode<"ISD::SRA" , SDTIntShiftOp>;
184def shl : SDNode<"ISD::SHL" , SDTIntShiftOp>;
Chris Lattnerd83571b2005-10-10 06:00:30 +0000185def and : SDNode<"ISD::AND" , SDTIntBinOp,
186 [SDNPCommutative, SDNPAssociative]>;
187def or : SDNode<"ISD::OR" , SDTIntBinOp,
188 [SDNPCommutative, SDNPAssociative]>;
189def xor : SDNode<"ISD::XOR" , SDTIntBinOp,
190 [SDNPCommutative, SDNPAssociative]>;
Chris Lattner56f31f52005-10-14 06:40:20 +0000191
192def sext_inreg : SDNode<"ISD::SIGN_EXTEND_INREG", SDTExtInreg>;
193def ctlz : SDNode<"ISD::CTLZ" , SDTIntUnaryOp>;
Andrew Lenharth7e0e8232005-10-20 19:38:11 +0000194def cttz : SDNode<"ISD::CTTZ" , SDTIntUnaryOp>;
195def ctpop : SDNode<"ISD::CTPOP" , SDTIntUnaryOp>;
Chris Lattner56f31f52005-10-14 06:40:20 +0000196def sext : SDNode<"ISD::SIGN_EXTEND", SDTIntExtendOp>;
197def zext : SDNode<"ISD::ZERO_EXTEND", SDTIntExtendOp>;
198def anyext : SDNode<"ISD::ANY_EXTEND" , SDTIntExtendOp>;
199def trunc : SDNode<"ISD::TRUNCATE" , SDTIntTruncOp>;
200
Chris Lattnerd83571b2005-10-10 06:00:30 +0000201def fadd : SDNode<"ISD::FADD" , SDTFPBinOp, [SDNPCommutative]>;
202def fsub : SDNode<"ISD::FSUB" , SDTFPBinOp>;
203def fmul : SDNode<"ISD::FMUL" , SDTFPBinOp, [SDNPCommutative]>;
204def fdiv : SDNode<"ISD::FDIV" , SDTFPBinOp>;
205def frem : SDNode<"ISD::FREM" , SDTFPBinOp>;
206def fabs : SDNode<"ISD::FABS" , SDTFPUnaryOp>;
207def fneg : SDNode<"ISD::FNEG" , SDTFPUnaryOp>;
208def fsqrt : SDNode<"ISD::FSQRT" , SDTFPUnaryOp>;
209
Chris Lattner6e83cbf2005-10-14 04:55:10 +0000210def fround : SDNode<"ISD::FP_ROUND" , SDTFPRoundOp>;
211def fextend : SDNode<"ISD::FP_EXTEND" , SDTFPExtendOp>;
212
Duraid Madina6c912bf2005-11-01 03:07:25 +0000213def sint_to_fp : SDNode<"ISD::SINT_TO_FP" , SDTIntToFPOp>;
214def uint_to_fp : SDNode<"ISD::UINT_TO_FP" , SDTIntToFPOp>;
215def fp_to_sint : SDNode<"ISD::FP_TO_SINT" , SDTFPToIntOp>;
216def fp_to_uint : SDNode<"ISD::FP_TO_UINT" , SDTFPToIntOp>;
217
Chris Lattnerce9580f2005-10-26 17:00:25 +0000218def setcc : SDNode<"ISD::SETCC" , SDTSetCC>;
Duraid Madina17decbb2005-11-02 02:37:18 +0000219def select : SDNode<"ISD::SELECT" , SDTSelect>;
Chris Lattnerce9580f2005-10-26 17:00:25 +0000220
Evan Chenge8531382005-12-04 08:13:17 +0000221def br : SDNode<"ISD::BR" , SDTBr, [SDNPHasChain]>;
222def brcond : SDNode<"ISD::BRCOND" , SDTBrCond, [SDNPHasChain]>;
223def ret : SDNode<"ISD::RET" , SDTRet, [SDNPHasChain]>;
224
225def writeport : SDNode<"ISD::WRITEPORT" , SDTWritePort, [SDNPHasChain]>;
226
Evan Cheng790af6d2005-12-08 04:28:48 +0000227def load : SDNode<"ISD::LOAD" , SDTLoad, [SDNPHasChain]>;
228
Chris Lattnerce9580f2005-10-26 17:00:25 +0000229//===----------------------------------------------------------------------===//
230// Selection DAG Condition Codes
231
232class CondCode; // ISD::CondCode enums
233def SETOEQ : CondCode; def SETOGT : CondCode;
234def SETOGE : CondCode; def SETOLT : CondCode; def SETOLE : CondCode;
235def SETONE : CondCode; def SETO : CondCode; def SETUO : CondCode;
236def SETUEQ : CondCode; def SETUGT : CondCode; def SETUGE : CondCode;
237def SETULT : CondCode; def SETULE : CondCode; def SETUNE : CondCode;
238
239def SETEQ : CondCode; def SETGT : CondCode; def SETGE : CondCode;
240def SETLT : CondCode; def SETLE : CondCode; def SETNE : CondCode;
241
242
Chris Lattnerd83571b2005-10-10 06:00:30 +0000243//===----------------------------------------------------------------------===//
244// Selection DAG Node Transformation Functions.
245//
246// This mechanism allows targets to manipulate nodes in the output DAG once a
247// match has been formed. This is typically used to manipulate immediate
248// values.
249//
250class SDNodeXForm<SDNode opc, code xformFunction> {
251 SDNode Opcode = opc;
252 code XFormFunction = xformFunction;
253}
254
255def NOOP_SDNodeXForm : SDNodeXForm<imm, [{}]>;
256
257
258//===----------------------------------------------------------------------===//
259// Selection DAG Pattern Fragments.
260//
261// Pattern fragments are reusable chunks of dags that match specific things.
262// They can take arguments and have C++ predicates that control whether they
263// match. They are intended to make the patterns for common instructions more
264// compact and readable.
265//
266
267/// PatFrag - Represents a pattern fragment. This can match something on the
268/// DAG, frame a single node to multiply nested other fragments.
269///
270class PatFrag<dag ops, dag frag, code pred = [{}],
271 SDNodeXForm xform = NOOP_SDNodeXForm> {
272 dag Operands = ops;
273 dag Fragment = frag;
274 code Predicate = pred;
275 SDNodeXForm OperandTransform = xform;
276}
277
278// PatLeaf's are pattern fragments that have no operands. This is just a helper
279// to define immediates and other common things concisely.
280class PatLeaf<dag frag, code pred = [{}], SDNodeXForm xform = NOOP_SDNodeXForm>
281 : PatFrag<(ops), frag, pred, xform>;
282
283// Leaf fragments.
284
285def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>;
Chris Lattnerd83571b2005-10-10 06:00:30 +0000286
287def vtInt : PatLeaf<(vt), [{ return MVT::isInteger(N->getVT()); }]>;
288def vtFP : PatLeaf<(vt), [{ return MVT::isFloatingPoint(N->getVT()); }]>;
289
290// Other helper fragments.
291
292def not : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>;
Chris Lattnera5537802005-10-20 23:30:37 +0000293def ineg : PatFrag<(ops node:$in), (sub 0, node:$in)>;
Chris Lattnerd83571b2005-10-10 06:00:30 +0000294
Chris Lattnerce9580f2005-10-26 17:00:25 +0000295
296// setcc convenience fragments.
297def setoeq : PatFrag<(ops node:$lhs, node:$rhs),
298 (setcc node:$lhs, node:$rhs, SETOEQ)>;
299def setogt : PatFrag<(ops node:$lhs, node:$rhs),
300 (setcc node:$lhs, node:$rhs, SETOGT)>;
301def setoge : PatFrag<(ops node:$lhs, node:$rhs),
302 (setcc node:$lhs, node:$rhs, SETOGE)>;
303def setolt : PatFrag<(ops node:$lhs, node:$rhs),
304 (setcc node:$lhs, node:$rhs, SETOLT)>;
305def setole : PatFrag<(ops node:$lhs, node:$rhs),
306 (setcc node:$lhs, node:$rhs, SETOLE)>;
307def setone : PatFrag<(ops node:$lhs, node:$rhs),
308 (setcc node:$lhs, node:$rhs, SETONE)>;
309def seto : PatFrag<(ops node:$lhs, node:$rhs),
310 (setcc node:$lhs, node:$rhs, SETO)>;
311def setuo : PatFrag<(ops node:$lhs, node:$rhs),
312 (setcc node:$lhs, node:$rhs, SETUO)>;
313def setueq : PatFrag<(ops node:$lhs, node:$rhs),
314 (setcc node:$lhs, node:$rhs, SETUEQ)>;
315def setugt : PatFrag<(ops node:$lhs, node:$rhs),
316 (setcc node:$lhs, node:$rhs, SETUGT)>;
317def setuge : PatFrag<(ops node:$lhs, node:$rhs),
318 (setcc node:$lhs, node:$rhs, SETUGE)>;
319def setult : PatFrag<(ops node:$lhs, node:$rhs),
320 (setcc node:$lhs, node:$rhs, SETULT)>;
321def setule : PatFrag<(ops node:$lhs, node:$rhs),
322 (setcc node:$lhs, node:$rhs, SETULE)>;
323def setune : PatFrag<(ops node:$lhs, node:$rhs),
324 (setcc node:$lhs, node:$rhs, SETUNE)>;
325def seteq : PatFrag<(ops node:$lhs, node:$rhs),
326 (setcc node:$lhs, node:$rhs, SETEQ)>;
327def setgt : PatFrag<(ops node:$lhs, node:$rhs),
328 (setcc node:$lhs, node:$rhs, SETGT)>;
329def setge : PatFrag<(ops node:$lhs, node:$rhs),
330 (setcc node:$lhs, node:$rhs, SETGE)>;
331def setlt : PatFrag<(ops node:$lhs, node:$rhs),
332 (setcc node:$lhs, node:$rhs, SETLT)>;
333def setle : PatFrag<(ops node:$lhs, node:$rhs),
334 (setcc node:$lhs, node:$rhs, SETLE)>;
335def setne : PatFrag<(ops node:$lhs, node:$rhs),
336 (setcc node:$lhs, node:$rhs, SETNE)>;
337
Chris Lattnerd83571b2005-10-10 06:00:30 +0000338//===----------------------------------------------------------------------===//
339// Selection DAG Pattern Support.
340//
341// Patterns are what are actually matched against the target-flavored
342// instruction selection DAG. Instructions defined by the target implicitly
343// define patterns in most cases, but patterns can also be explicitly added when
344// an operation is defined by a sequence of instructions (e.g. loading a large
345// immediate value on RISC targets that do not support immediates as large as
346// their GPRs).
347//
348
349class Pattern<dag patternToMatch, list<dag> resultInstrs> {
350 dag PatternToMatch = patternToMatch;
351 list<dag> ResultInstrs = resultInstrs;
352}
353
354// Pat - A simple (but common) form of a pattern, which produces a simple result
355// not needing a full list.
356class Pat<dag pattern, dag result> : Pattern<pattern, [result]>;
357
Evan Cheng790af6d2005-12-08 04:28:48 +0000358//===----------------------------------------------------------------------===//
359// Complex pattern definitions.
360//
361// Complex patterns, e.g. X86 addressing mode, requires pattern matching code
362// in C++. NumOperands is the number of operands returned by the select function;
363// SelectFunc is the name of the function used to pattern match the max. pattern;
364// RootNodes are the list of possible root nodes of the sub-dags to match.
365// e.g. X86 addressing mode - def addr : ComplexPattern<4, "SelectAddr", [add]>;
366//
367class ComplexPattern<ValueType ty, int numops, string fn, list<SDNode> roots = []> {
368 ValueType Ty = ty;
369 int NumOperands = numops;
370 string SelectFunc = fn;
371 list<SDNode> RootNodes = roots;
372}