blob: 2321effd081be3484ffc2ff132c06cd79a97532c [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]>;
76def SDTFPBinOp : SDTypeProfile<1, 2, [ // fadd, fmul, etc.
77 SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisFP<0>
78]>;
79def SDTIntUnaryOp : SDTypeProfile<1, 1, [ // ctlz
80 SDTCisSameAs<0, 1>, SDTCisInt<0>
81]>;
Chris Lattner444215d2005-10-14 06:40:20 +000082def SDTIntExtendOp : SDTypeProfile<1, 1, [ // sext, zext, anyext
83 SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<1, 0>
84]>;
85def SDTIntTruncOp : SDTypeProfile<1, 1, [ // trunc
86 SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<0, 1>
87]>;
Chris Lattner17f2cf02005-10-10 06:00:30 +000088def SDTFPUnaryOp : SDTypeProfile<1, 1, [ // fneg, fsqrt, etc
89 SDTCisSameAs<0, 1>, SDTCisFP<0>
90]>;
Chris Lattner13664a62005-10-14 04:55:10 +000091def SDTFPRoundOp : SDTypeProfile<1, 1, [ // fround
92 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<0, 1>
93]>;
94def SDTFPExtendOp : SDTypeProfile<1, 1, [ // fextend
95 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<1, 0>
96]>;
Duraid Madinae2fd9e22005-11-01 03:07:25 +000097def SDTIntToFPOp : SDTypeProfile<1, 1, [ // [su]int_to_fp
98 SDTCisFP<0>, SDTCisInt<1>
99]>;
100def SDTFPToIntOp : SDTypeProfile<1, 1, [ // fp_to_[su]int
101 SDTCisInt<0>, SDTCisFP<1>
102]>;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000103def SDTExtInreg : SDTypeProfile<1, 2, [ // sext_inreg
104 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisVT<2, OtherVT>,
105 SDTCisVTSmallerThanOp<2, 1>
106]>;
107
Chris Lattner1f426de2005-10-26 17:00:25 +0000108def SDTSetCC : SDTypeProfile<1, 3, [ // setcc
109 SDTCisInt<0>, SDTCisSameAs<1, 2>, SDTCisVT<3, OtherVT>
110]>;
111
Duraid Madina59669552005-11-02 02:37:18 +0000112def SDTSelect : SDTypeProfile<1, 3, [ // select
113 SDTCisInt<1>, SDTCisSameAs<0, 2>, SDTCisSameAs<2, 3>
114]>;
115
Evan Chengf8ac8142005-12-04 08:13:17 +0000116def SDTBr : SDTypeProfile<0, 1, [ // br
117 SDTCisVT<0, OtherVT>
118]>;
119
120def SDTBrCond : SDTypeProfile<0, 2, [ // brcond
121 SDTCisInt<0>, SDTCisVT<1, OtherVT>
122]>;
123
124def SDTRet : SDTypeProfile<0, 0, [ // ret
125]>;
126
127def SDTWritePort : SDTypeProfile<0, 2, [ // writeport
128 SDTCisInt<0>, SDTCisInt<1>
129]>;
130
Chris Lattner17f2cf02005-10-10 06:00:30 +0000131//===----------------------------------------------------------------------===//
132// Selection DAG Node Properties.
133//
134// Note: These are hard coded into tblgen.
135//
136class SDNodeProperty;
137def SDNPCommutative : SDNodeProperty; // X op Y == Y op X
138def SDNPAssociative : SDNodeProperty; // (X op Y) op Z == X op (Y op Z)
Evan Chengf8ac8142005-12-04 08:13:17 +0000139def SDNPHasChain : SDNodeProperty; // R/W chain operand and result
Chris Lattner17f2cf02005-10-10 06:00:30 +0000140
141//===----------------------------------------------------------------------===//
142// Selection DAG Node definitions.
143//
144class SDNode<string opcode, SDTypeProfile typeprof,
145 list<SDNodeProperty> props = [], string sdclass = "SDNode"> {
146 string Opcode = opcode;
147 string SDClass = sdclass;
148 list<SDNodeProperty> Properties = props;
149 SDTypeProfile TypeProfile = typeprof;
150}
151
152def set;
153def node;
154
155def imm : SDNode<"ISD::Constant" , SDTImm , [], "ConstantSDNode">;
Evan Chengf8ac8142005-12-04 08:13:17 +0000156def vt : SDNode<"ISD::VALUETYPE" , SDTOther , [], "VTSDNode">;
157def bb : SDNode<"ISD::BasicBlock", SDTOther , [], "BasicBlockSDNode">;
158def cond : SDNode<"ISD::CONDCODE" , SDTOther , [], "CondCodeSDNode">;
Chris Lattner97898262005-10-25 21:03:14 +0000159def undef : SDNode<"ISD::UNDEF" , SDTUNDEF , []>;
Chris Lattnerdb40dc22005-11-17 07:20:15 +0000160def globaladdr : SDNode<"ISD::GlobalAddress", SDTImm, [],
161 "GlobalAddressSDNode">;
162def tglobaladdr : SDNode<"ISD::TargetGlobalAddress", SDTImm, [],
163 "GlobalAddressSDNode">;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000164def add : SDNode<"ISD::ADD" , SDTIntBinOp ,
165 [SDNPCommutative, SDNPAssociative]>;
166def sub : SDNode<"ISD::SUB" , SDTIntBinOp>;
167def mul : SDNode<"ISD::MUL" , SDTIntBinOp,
168 [SDNPCommutative, SDNPAssociative]>;
169def mulhs : SDNode<"ISD::MULHS" , SDTIntBinOp, [SDNPCommutative]>;
170def mulhu : SDNode<"ISD::MULHU" , SDTIntBinOp, [SDNPCommutative]>;
171def sdiv : SDNode<"ISD::SDIV" , SDTIntBinOp>;
172def udiv : SDNode<"ISD::UDIV" , SDTIntBinOp>;
173def srem : SDNode<"ISD::SREM" , SDTIntBinOp>;
174def urem : SDNode<"ISD::UREM" , SDTIntBinOp>;
175def srl : SDNode<"ISD::SRL" , SDTIntBinOp>;
176def sra : SDNode<"ISD::SRA" , SDTIntBinOp>;
177def shl : SDNode<"ISD::SHL" , SDTIntBinOp>;
178def and : SDNode<"ISD::AND" , SDTIntBinOp,
179 [SDNPCommutative, SDNPAssociative]>;
180def or : SDNode<"ISD::OR" , SDTIntBinOp,
181 [SDNPCommutative, SDNPAssociative]>;
182def xor : SDNode<"ISD::XOR" , SDTIntBinOp,
183 [SDNPCommutative, SDNPAssociative]>;
Chris Lattner444215d2005-10-14 06:40:20 +0000184
185def sext_inreg : SDNode<"ISD::SIGN_EXTEND_INREG", SDTExtInreg>;
186def ctlz : SDNode<"ISD::CTLZ" , SDTIntUnaryOp>;
Andrew Lenharthd684e1a2005-10-20 19:38:11 +0000187def cttz : SDNode<"ISD::CTTZ" , SDTIntUnaryOp>;
188def ctpop : SDNode<"ISD::CTPOP" , SDTIntUnaryOp>;
Chris Lattner444215d2005-10-14 06:40:20 +0000189def sext : SDNode<"ISD::SIGN_EXTEND", SDTIntExtendOp>;
190def zext : SDNode<"ISD::ZERO_EXTEND", SDTIntExtendOp>;
191def anyext : SDNode<"ISD::ANY_EXTEND" , SDTIntExtendOp>;
192def trunc : SDNode<"ISD::TRUNCATE" , SDTIntTruncOp>;
193
Chris Lattner17f2cf02005-10-10 06:00:30 +0000194def fadd : SDNode<"ISD::FADD" , SDTFPBinOp, [SDNPCommutative]>;
195def fsub : SDNode<"ISD::FSUB" , SDTFPBinOp>;
196def fmul : SDNode<"ISD::FMUL" , SDTFPBinOp, [SDNPCommutative]>;
197def fdiv : SDNode<"ISD::FDIV" , SDTFPBinOp>;
198def frem : SDNode<"ISD::FREM" , SDTFPBinOp>;
199def fabs : SDNode<"ISD::FABS" , SDTFPUnaryOp>;
200def fneg : SDNode<"ISD::FNEG" , SDTFPUnaryOp>;
201def fsqrt : SDNode<"ISD::FSQRT" , SDTFPUnaryOp>;
202
Chris Lattner13664a62005-10-14 04:55:10 +0000203def fround : SDNode<"ISD::FP_ROUND" , SDTFPRoundOp>;
204def fextend : SDNode<"ISD::FP_EXTEND" , SDTFPExtendOp>;
205
Duraid Madinae2fd9e22005-11-01 03:07:25 +0000206def sint_to_fp : SDNode<"ISD::SINT_TO_FP" , SDTIntToFPOp>;
207def uint_to_fp : SDNode<"ISD::UINT_TO_FP" , SDTIntToFPOp>;
208def fp_to_sint : SDNode<"ISD::FP_TO_SINT" , SDTFPToIntOp>;
209def fp_to_uint : SDNode<"ISD::FP_TO_UINT" , SDTFPToIntOp>;
210
Chris Lattner1f426de2005-10-26 17:00:25 +0000211def setcc : SDNode<"ISD::SETCC" , SDTSetCC>;
Duraid Madina59669552005-11-02 02:37:18 +0000212def select : SDNode<"ISD::SELECT" , SDTSelect>;
Chris Lattner1f426de2005-10-26 17:00:25 +0000213
Evan Chengf8ac8142005-12-04 08:13:17 +0000214def br : SDNode<"ISD::BR" , SDTBr, [SDNPHasChain]>;
215def brcond : SDNode<"ISD::BRCOND" , SDTBrCond, [SDNPHasChain]>;
216def ret : SDNode<"ISD::RET" , SDTRet, [SDNPHasChain]>;
217
218def writeport : SDNode<"ISD::WRITEPORT" , SDTWritePort, [SDNPHasChain]>;
219
Chris Lattner1f426de2005-10-26 17:00:25 +0000220//===----------------------------------------------------------------------===//
221// Selection DAG Condition Codes
222
223class CondCode; // ISD::CondCode enums
224def SETOEQ : CondCode; def SETOGT : CondCode;
225def SETOGE : CondCode; def SETOLT : CondCode; def SETOLE : CondCode;
226def SETONE : CondCode; def SETO : CondCode; def SETUO : CondCode;
227def SETUEQ : CondCode; def SETUGT : CondCode; def SETUGE : CondCode;
228def SETULT : CondCode; def SETULE : CondCode; def SETUNE : CondCode;
229
230def SETEQ : CondCode; def SETGT : CondCode; def SETGE : CondCode;
231def SETLT : CondCode; def SETLE : CondCode; def SETNE : CondCode;
232
233
Chris Lattner17f2cf02005-10-10 06:00:30 +0000234//===----------------------------------------------------------------------===//
235// Selection DAG Node Transformation Functions.
236//
237// This mechanism allows targets to manipulate nodes in the output DAG once a
238// match has been formed. This is typically used to manipulate immediate
239// values.
240//
241class SDNodeXForm<SDNode opc, code xformFunction> {
242 SDNode Opcode = opc;
243 code XFormFunction = xformFunction;
244}
245
246def NOOP_SDNodeXForm : SDNodeXForm<imm, [{}]>;
247
248
249//===----------------------------------------------------------------------===//
250// Selection DAG Pattern Fragments.
251//
252// Pattern fragments are reusable chunks of dags that match specific things.
253// They can take arguments and have C++ predicates that control whether they
254// match. They are intended to make the patterns for common instructions more
255// compact and readable.
256//
257
258/// PatFrag - Represents a pattern fragment. This can match something on the
259/// DAG, frame a single node to multiply nested other fragments.
260///
261class PatFrag<dag ops, dag frag, code pred = [{}],
262 SDNodeXForm xform = NOOP_SDNodeXForm> {
263 dag Operands = ops;
264 dag Fragment = frag;
265 code Predicate = pred;
266 SDNodeXForm OperandTransform = xform;
267}
268
269// PatLeaf's are pattern fragments that have no operands. This is just a helper
270// to define immediates and other common things concisely.
271class PatLeaf<dag frag, code pred = [{}], SDNodeXForm xform = NOOP_SDNodeXForm>
272 : PatFrag<(ops), frag, pred, xform>;
273
274// Leaf fragments.
275
276def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000277
278def vtInt : PatLeaf<(vt), [{ return MVT::isInteger(N->getVT()); }]>;
279def vtFP : PatLeaf<(vt), [{ return MVT::isFloatingPoint(N->getVT()); }]>;
280
281// Other helper fragments.
282
283def not : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>;
Chris Lattnereae6d642005-10-20 23:30:37 +0000284def ineg : PatFrag<(ops node:$in), (sub 0, node:$in)>;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000285
Chris Lattner1f426de2005-10-26 17:00:25 +0000286
287// setcc convenience fragments.
288def setoeq : PatFrag<(ops node:$lhs, node:$rhs),
289 (setcc node:$lhs, node:$rhs, SETOEQ)>;
290def setogt : PatFrag<(ops node:$lhs, node:$rhs),
291 (setcc node:$lhs, node:$rhs, SETOGT)>;
292def setoge : PatFrag<(ops node:$lhs, node:$rhs),
293 (setcc node:$lhs, node:$rhs, SETOGE)>;
294def setolt : PatFrag<(ops node:$lhs, node:$rhs),
295 (setcc node:$lhs, node:$rhs, SETOLT)>;
296def setole : PatFrag<(ops node:$lhs, node:$rhs),
297 (setcc node:$lhs, node:$rhs, SETOLE)>;
298def setone : PatFrag<(ops node:$lhs, node:$rhs),
299 (setcc node:$lhs, node:$rhs, SETONE)>;
300def seto : PatFrag<(ops node:$lhs, node:$rhs),
301 (setcc node:$lhs, node:$rhs, SETO)>;
302def setuo : PatFrag<(ops node:$lhs, node:$rhs),
303 (setcc node:$lhs, node:$rhs, SETUO)>;
304def setueq : PatFrag<(ops node:$lhs, node:$rhs),
305 (setcc node:$lhs, node:$rhs, SETUEQ)>;
306def setugt : PatFrag<(ops node:$lhs, node:$rhs),
307 (setcc node:$lhs, node:$rhs, SETUGT)>;
308def setuge : PatFrag<(ops node:$lhs, node:$rhs),
309 (setcc node:$lhs, node:$rhs, SETUGE)>;
310def setult : PatFrag<(ops node:$lhs, node:$rhs),
311 (setcc node:$lhs, node:$rhs, SETULT)>;
312def setule : PatFrag<(ops node:$lhs, node:$rhs),
313 (setcc node:$lhs, node:$rhs, SETULE)>;
314def setune : PatFrag<(ops node:$lhs, node:$rhs),
315 (setcc node:$lhs, node:$rhs, SETUNE)>;
316def seteq : PatFrag<(ops node:$lhs, node:$rhs),
317 (setcc node:$lhs, node:$rhs, SETEQ)>;
318def setgt : PatFrag<(ops node:$lhs, node:$rhs),
319 (setcc node:$lhs, node:$rhs, SETGT)>;
320def setge : PatFrag<(ops node:$lhs, node:$rhs),
321 (setcc node:$lhs, node:$rhs, SETGE)>;
322def setlt : PatFrag<(ops node:$lhs, node:$rhs),
323 (setcc node:$lhs, node:$rhs, SETLT)>;
324def setle : PatFrag<(ops node:$lhs, node:$rhs),
325 (setcc node:$lhs, node:$rhs, SETLE)>;
326def setne : PatFrag<(ops node:$lhs, node:$rhs),
327 (setcc node:$lhs, node:$rhs, SETNE)>;
328
Chris Lattner17f2cf02005-10-10 06:00:30 +0000329//===----------------------------------------------------------------------===//
330// Selection DAG Pattern Support.
331//
332// Patterns are what are actually matched against the target-flavored
333// instruction selection DAG. Instructions defined by the target implicitly
334// define patterns in most cases, but patterns can also be explicitly added when
335// an operation is defined by a sequence of instructions (e.g. loading a large
336// immediate value on RISC targets that do not support immediates as large as
337// their GPRs).
338//
339
340class Pattern<dag patternToMatch, list<dag> resultInstrs> {
341 dag PatternToMatch = patternToMatch;
342 list<dag> ResultInstrs = resultInstrs;
343}
344
345// Pat - A simple (but common) form of a pattern, which produces a simple result
346// not needing a full list.
347class Pat<dag pattern, dag result> : Pattern<pattern, [result]>;
348