blob: 5be65e2933b2974c10c5a4731eaf927e76ce9227 [file] [log] [blame]
Chris Lattner17f2cf02005-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 Lattner13664a62005-10-14 04:55:10 +000048class SDTCisOpSmallerThanOp<int SmallOp, int BigOp> : SDTypeConstraint<SmallOp>{
49 int BigOperandNum = BigOp;
50}
51
Chris Lattner17f2cf02005-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 Chengf8ac8142005-12-04 08:13:17 +000071def SDTOther : SDTypeProfile<1, 0, [SDTCisVT<0, OtherVT>]>; // for 'vt'.
Chris Lattner97898262005-10-25 21:03:14 +000072def SDTUNDEF : SDTypeProfile<1, 0, []>; // for 'undef'.
Chris Lattner17f2cf02005-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 Lattner68bfd9c2005-12-05 02:37:26 +000076def SDTIntShiftOp : SDTypeProfile<1, 2, [ // shl, sra, srl
77 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisInt<2>
78]>;
Chris Lattner17f2cf02005-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 Lattner444215d2005-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 Lattner17f2cf02005-10-10 06:00:30 +000091def SDTFPUnaryOp : SDTypeProfile<1, 1, [ // fneg, fsqrt, etc
92 SDTCisSameAs<0, 1>, SDTCisFP<0>
93]>;
Chris Lattner13664a62005-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 Madinae2fd9e22005-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 Lattner17f2cf02005-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 Lattner1f426de2005-10-26 17:00:25 +0000111def SDTSetCC : SDTypeProfile<1, 3, [ // setcc
112 SDTCisInt<0>, SDTCisSameAs<1, 2>, SDTCisVT<3, OtherVT>
113]>;
114
Duraid Madina59669552005-11-02 02:37:18 +0000115def SDTSelect : SDTypeProfile<1, 3, [ // select
116 SDTCisInt<1>, SDTCisSameAs<0, 2>, SDTCisSameAs<2, 3>
117]>;
118
Evan Chengf8ac8142005-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
Chris Lattner17f2cf02005-10-10 06:00:30 +0000134//===----------------------------------------------------------------------===//
135// Selection DAG Node Properties.
136//
137// Note: These are hard coded into tblgen.
138//
139class SDNodeProperty;
140def SDNPCommutative : SDNodeProperty; // X op Y == Y op X
141def SDNPAssociative : SDNodeProperty; // (X op Y) op Z == X op (Y op Z)
Evan Chengf8ac8142005-12-04 08:13:17 +0000142def SDNPHasChain : SDNodeProperty; // R/W chain operand and result
Chris Lattner17f2cf02005-10-10 06:00:30 +0000143
144//===----------------------------------------------------------------------===//
145// Selection DAG Node definitions.
146//
147class 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
155def set;
156def node;
157
158def imm : SDNode<"ISD::Constant" , SDTImm , [], "ConstantSDNode">;
Evan Chengf8ac8142005-12-04 08:13:17 +0000159def vt : SDNode<"ISD::VALUETYPE" , SDTOther , [], "VTSDNode">;
160def bb : SDNode<"ISD::BasicBlock", SDTOther , [], "BasicBlockSDNode">;
161def cond : SDNode<"ISD::CONDCODE" , SDTOther , [], "CondCodeSDNode">;
Chris Lattner97898262005-10-25 21:03:14 +0000162def undef : SDNode<"ISD::UNDEF" , SDTUNDEF , []>;
Chris Lattnerdb40dc22005-11-17 07:20:15 +0000163def globaladdr : SDNode<"ISD::GlobalAddress", SDTImm, [],
164 "GlobalAddressSDNode">;
165def tglobaladdr : SDNode<"ISD::TargetGlobalAddress", SDTImm, [],
166 "GlobalAddressSDNode">;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000167def add : SDNode<"ISD::ADD" , SDTIntBinOp ,
168 [SDNPCommutative, SDNPAssociative]>;
169def sub : SDNode<"ISD::SUB" , SDTIntBinOp>;
170def mul : SDNode<"ISD::MUL" , SDTIntBinOp,
171 [SDNPCommutative, SDNPAssociative]>;
172def mulhs : SDNode<"ISD::MULHS" , SDTIntBinOp, [SDNPCommutative]>;
173def mulhu : SDNode<"ISD::MULHU" , SDTIntBinOp, [SDNPCommutative]>;
174def sdiv : SDNode<"ISD::SDIV" , SDTIntBinOp>;
175def udiv : SDNode<"ISD::UDIV" , SDTIntBinOp>;
176def srem : SDNode<"ISD::SREM" , SDTIntBinOp>;
177def urem : SDNode<"ISD::UREM" , SDTIntBinOp>;
Chris Lattner68bfd9c2005-12-05 02:37:26 +0000178def srl : SDNode<"ISD::SRL" , SDTIntShiftOp>;
179def sra : SDNode<"ISD::SRA" , SDTIntShiftOp>;
180def shl : SDNode<"ISD::SHL" , SDTIntShiftOp>;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000181def and : SDNode<"ISD::AND" , SDTIntBinOp,
182 [SDNPCommutative, SDNPAssociative]>;
183def or : SDNode<"ISD::OR" , SDTIntBinOp,
184 [SDNPCommutative, SDNPAssociative]>;
185def xor : SDNode<"ISD::XOR" , SDTIntBinOp,
186 [SDNPCommutative, SDNPAssociative]>;
Chris Lattner444215d2005-10-14 06:40:20 +0000187
188def sext_inreg : SDNode<"ISD::SIGN_EXTEND_INREG", SDTExtInreg>;
189def ctlz : SDNode<"ISD::CTLZ" , SDTIntUnaryOp>;
Andrew Lenharthd684e1a2005-10-20 19:38:11 +0000190def cttz : SDNode<"ISD::CTTZ" , SDTIntUnaryOp>;
191def ctpop : SDNode<"ISD::CTPOP" , SDTIntUnaryOp>;
Chris Lattner444215d2005-10-14 06:40:20 +0000192def sext : SDNode<"ISD::SIGN_EXTEND", SDTIntExtendOp>;
193def zext : SDNode<"ISD::ZERO_EXTEND", SDTIntExtendOp>;
194def anyext : SDNode<"ISD::ANY_EXTEND" , SDTIntExtendOp>;
195def trunc : SDNode<"ISD::TRUNCATE" , SDTIntTruncOp>;
196
Chris Lattner17f2cf02005-10-10 06:00:30 +0000197def fadd : SDNode<"ISD::FADD" , SDTFPBinOp, [SDNPCommutative]>;
198def fsub : SDNode<"ISD::FSUB" , SDTFPBinOp>;
199def fmul : SDNode<"ISD::FMUL" , SDTFPBinOp, [SDNPCommutative]>;
200def fdiv : SDNode<"ISD::FDIV" , SDTFPBinOp>;
201def frem : SDNode<"ISD::FREM" , SDTFPBinOp>;
202def fabs : SDNode<"ISD::FABS" , SDTFPUnaryOp>;
203def fneg : SDNode<"ISD::FNEG" , SDTFPUnaryOp>;
204def fsqrt : SDNode<"ISD::FSQRT" , SDTFPUnaryOp>;
205
Chris Lattner13664a62005-10-14 04:55:10 +0000206def fround : SDNode<"ISD::FP_ROUND" , SDTFPRoundOp>;
207def fextend : SDNode<"ISD::FP_EXTEND" , SDTFPExtendOp>;
208
Duraid Madinae2fd9e22005-11-01 03:07:25 +0000209def sint_to_fp : SDNode<"ISD::SINT_TO_FP" , SDTIntToFPOp>;
210def uint_to_fp : SDNode<"ISD::UINT_TO_FP" , SDTIntToFPOp>;
211def fp_to_sint : SDNode<"ISD::FP_TO_SINT" , SDTFPToIntOp>;
212def fp_to_uint : SDNode<"ISD::FP_TO_UINT" , SDTFPToIntOp>;
213
Chris Lattner1f426de2005-10-26 17:00:25 +0000214def setcc : SDNode<"ISD::SETCC" , SDTSetCC>;
Duraid Madina59669552005-11-02 02:37:18 +0000215def select : SDNode<"ISD::SELECT" , SDTSelect>;
Chris Lattner1f426de2005-10-26 17:00:25 +0000216
Evan Chengf8ac8142005-12-04 08:13:17 +0000217def br : SDNode<"ISD::BR" , SDTBr, [SDNPHasChain]>;
218def brcond : SDNode<"ISD::BRCOND" , SDTBrCond, [SDNPHasChain]>;
219def ret : SDNode<"ISD::RET" , SDTRet, [SDNPHasChain]>;
220
221def writeport : SDNode<"ISD::WRITEPORT" , SDTWritePort, [SDNPHasChain]>;
222
Chris Lattner1f426de2005-10-26 17:00:25 +0000223//===----------------------------------------------------------------------===//
224// Selection DAG Condition Codes
225
226class CondCode; // ISD::CondCode enums
227def SETOEQ : CondCode; def SETOGT : CondCode;
228def SETOGE : CondCode; def SETOLT : CondCode; def SETOLE : CondCode;
229def SETONE : CondCode; def SETO : CondCode; def SETUO : CondCode;
230def SETUEQ : CondCode; def SETUGT : CondCode; def SETUGE : CondCode;
231def SETULT : CondCode; def SETULE : CondCode; def SETUNE : CondCode;
232
233def SETEQ : CondCode; def SETGT : CondCode; def SETGE : CondCode;
234def SETLT : CondCode; def SETLE : CondCode; def SETNE : CondCode;
235
236
Chris Lattner17f2cf02005-10-10 06:00:30 +0000237//===----------------------------------------------------------------------===//
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//
244class SDNodeXForm<SDNode opc, code xformFunction> {
245 SDNode Opcode = opc;
246 code XFormFunction = xformFunction;
247}
248
249def 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///
264class 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.
274class PatLeaf<dag frag, code pred = [{}], SDNodeXForm xform = NOOP_SDNodeXForm>
275 : PatFrag<(ops), frag, pred, xform>;
276
277// Leaf fragments.
278
279def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000280
281def vtInt : PatLeaf<(vt), [{ return MVT::isInteger(N->getVT()); }]>;
282def vtFP : PatLeaf<(vt), [{ return MVT::isFloatingPoint(N->getVT()); }]>;
283
284// Other helper fragments.
285
286def not : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>;
Chris Lattnereae6d642005-10-20 23:30:37 +0000287def ineg : PatFrag<(ops node:$in), (sub 0, node:$in)>;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000288
Chris Lattner1f426de2005-10-26 17:00:25 +0000289
290// setcc convenience fragments.
291def setoeq : PatFrag<(ops node:$lhs, node:$rhs),
292 (setcc node:$lhs, node:$rhs, SETOEQ)>;
293def setogt : PatFrag<(ops node:$lhs, node:$rhs),
294 (setcc node:$lhs, node:$rhs, SETOGT)>;
295def setoge : PatFrag<(ops node:$lhs, node:$rhs),
296 (setcc node:$lhs, node:$rhs, SETOGE)>;
297def setolt : PatFrag<(ops node:$lhs, node:$rhs),
298 (setcc node:$lhs, node:$rhs, SETOLT)>;
299def setole : PatFrag<(ops node:$lhs, node:$rhs),
300 (setcc node:$lhs, node:$rhs, SETOLE)>;
301def setone : PatFrag<(ops node:$lhs, node:$rhs),
302 (setcc node:$lhs, node:$rhs, SETONE)>;
303def seto : PatFrag<(ops node:$lhs, node:$rhs),
304 (setcc node:$lhs, node:$rhs, SETO)>;
305def setuo : PatFrag<(ops node:$lhs, node:$rhs),
306 (setcc node:$lhs, node:$rhs, SETUO)>;
307def setueq : PatFrag<(ops node:$lhs, node:$rhs),
308 (setcc node:$lhs, node:$rhs, SETUEQ)>;
309def setugt : PatFrag<(ops node:$lhs, node:$rhs),
310 (setcc node:$lhs, node:$rhs, SETUGT)>;
311def setuge : PatFrag<(ops node:$lhs, node:$rhs),
312 (setcc node:$lhs, node:$rhs, SETUGE)>;
313def setult : PatFrag<(ops node:$lhs, node:$rhs),
314 (setcc node:$lhs, node:$rhs, SETULT)>;
315def setule : PatFrag<(ops node:$lhs, node:$rhs),
316 (setcc node:$lhs, node:$rhs, SETULE)>;
317def setune : PatFrag<(ops node:$lhs, node:$rhs),
318 (setcc node:$lhs, node:$rhs, SETUNE)>;
319def seteq : PatFrag<(ops node:$lhs, node:$rhs),
320 (setcc node:$lhs, node:$rhs, SETEQ)>;
321def setgt : PatFrag<(ops node:$lhs, node:$rhs),
322 (setcc node:$lhs, node:$rhs, SETGT)>;
323def setge : PatFrag<(ops node:$lhs, node:$rhs),
324 (setcc node:$lhs, node:$rhs, SETGE)>;
325def setlt : PatFrag<(ops node:$lhs, node:$rhs),
326 (setcc node:$lhs, node:$rhs, SETLT)>;
327def setle : PatFrag<(ops node:$lhs, node:$rhs),
328 (setcc node:$lhs, node:$rhs, SETLE)>;
329def setne : PatFrag<(ops node:$lhs, node:$rhs),
330 (setcc node:$lhs, node:$rhs, SETNE)>;
331
Chris Lattner17f2cf02005-10-10 06:00:30 +0000332//===----------------------------------------------------------------------===//
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
343class 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.
350class Pat<dag pattern, dag result> : Pattern<pattern, [result]>;
351