blob: f58d4ab7145c83d0488c13871ce5f02c2ec525af [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'.
Chris Lattner97898262005-10-25 21:03:14 +000071def SDTVT : SDTypeProfile<1, 0, [SDTCisVT<0, OtherVT>]>; // for 'vt'.
72def 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]>;
Chris Lattner17f2cf02005-10-10 06:00:30 +000097def SDTExtInreg : SDTypeProfile<1, 2, [ // sext_inreg
98 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisVT<2, OtherVT>,
99 SDTCisVTSmallerThanOp<2, 1>
100]>;
101
Chris Lattner1f426de2005-10-26 17:00:25 +0000102def SDTSetCC : SDTypeProfile<1, 3, [ // setcc
103 SDTCisInt<0>, SDTCisSameAs<1, 2>, SDTCisVT<3, OtherVT>
104]>;
105
Chris Lattner17f2cf02005-10-10 06:00:30 +0000106//===----------------------------------------------------------------------===//
107// Selection DAG Node Properties.
108//
109// Note: These are hard coded into tblgen.
110//
111class SDNodeProperty;
112def SDNPCommutative : SDNodeProperty; // X op Y == Y op X
113def SDNPAssociative : SDNodeProperty; // (X op Y) op Z == X op (Y op Z)
114
115//===----------------------------------------------------------------------===//
116// Selection DAG Node definitions.
117//
118class SDNode<string opcode, SDTypeProfile typeprof,
119 list<SDNodeProperty> props = [], string sdclass = "SDNode"> {
120 string Opcode = opcode;
121 string SDClass = sdclass;
122 list<SDNodeProperty> Properties = props;
123 SDTypeProfile TypeProfile = typeprof;
124}
125
126def set;
127def node;
128
129def imm : SDNode<"ISD::Constant" , SDTImm , [], "ConstantSDNode">;
130def vt : SDNode<"ISD::VALUETYPE" , SDTVT , [], "VTSDNode">;
Chris Lattner1f426de2005-10-26 17:00:25 +0000131def cond : SDNode<"ISD::CONDCODE" , SDTVT , [], "CondCodeSDNode">;
Chris Lattner97898262005-10-25 21:03:14 +0000132def undef : SDNode<"ISD::UNDEF" , SDTUNDEF , []>;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000133def add : SDNode<"ISD::ADD" , SDTIntBinOp ,
134 [SDNPCommutative, SDNPAssociative]>;
135def sub : SDNode<"ISD::SUB" , SDTIntBinOp>;
136def mul : SDNode<"ISD::MUL" , SDTIntBinOp,
137 [SDNPCommutative, SDNPAssociative]>;
138def mulhs : SDNode<"ISD::MULHS" , SDTIntBinOp, [SDNPCommutative]>;
139def mulhu : SDNode<"ISD::MULHU" , SDTIntBinOp, [SDNPCommutative]>;
140def sdiv : SDNode<"ISD::SDIV" , SDTIntBinOp>;
141def udiv : SDNode<"ISD::UDIV" , SDTIntBinOp>;
142def srem : SDNode<"ISD::SREM" , SDTIntBinOp>;
143def urem : SDNode<"ISD::UREM" , SDTIntBinOp>;
144def srl : SDNode<"ISD::SRL" , SDTIntBinOp>;
145def sra : SDNode<"ISD::SRA" , SDTIntBinOp>;
146def shl : SDNode<"ISD::SHL" , SDTIntBinOp>;
147def and : SDNode<"ISD::AND" , SDTIntBinOp,
148 [SDNPCommutative, SDNPAssociative]>;
149def or : SDNode<"ISD::OR" , SDTIntBinOp,
150 [SDNPCommutative, SDNPAssociative]>;
151def xor : SDNode<"ISD::XOR" , SDTIntBinOp,
152 [SDNPCommutative, SDNPAssociative]>;
Chris Lattner444215d2005-10-14 06:40:20 +0000153
154def sext_inreg : SDNode<"ISD::SIGN_EXTEND_INREG", SDTExtInreg>;
155def ctlz : SDNode<"ISD::CTLZ" , SDTIntUnaryOp>;
Andrew Lenharthd684e1a2005-10-20 19:38:11 +0000156def cttz : SDNode<"ISD::CTTZ" , SDTIntUnaryOp>;
157def ctpop : SDNode<"ISD::CTPOP" , SDTIntUnaryOp>;
Chris Lattner444215d2005-10-14 06:40:20 +0000158def sext : SDNode<"ISD::SIGN_EXTEND", SDTIntExtendOp>;
159def zext : SDNode<"ISD::ZERO_EXTEND", SDTIntExtendOp>;
160def anyext : SDNode<"ISD::ANY_EXTEND" , SDTIntExtendOp>;
161def trunc : SDNode<"ISD::TRUNCATE" , SDTIntTruncOp>;
162
Chris Lattner17f2cf02005-10-10 06:00:30 +0000163def fadd : SDNode<"ISD::FADD" , SDTFPBinOp, [SDNPCommutative]>;
164def fsub : SDNode<"ISD::FSUB" , SDTFPBinOp>;
165def fmul : SDNode<"ISD::FMUL" , SDTFPBinOp, [SDNPCommutative]>;
166def fdiv : SDNode<"ISD::FDIV" , SDTFPBinOp>;
167def frem : SDNode<"ISD::FREM" , SDTFPBinOp>;
168def fabs : SDNode<"ISD::FABS" , SDTFPUnaryOp>;
169def fneg : SDNode<"ISD::FNEG" , SDTFPUnaryOp>;
170def fsqrt : SDNode<"ISD::FSQRT" , SDTFPUnaryOp>;
171
Chris Lattner13664a62005-10-14 04:55:10 +0000172def fround : SDNode<"ISD::FP_ROUND" , SDTFPRoundOp>;
173def fextend : SDNode<"ISD::FP_EXTEND" , SDTFPExtendOp>;
174
Chris Lattner1f426de2005-10-26 17:00:25 +0000175def setcc : SDNode<"ISD::SETCC" , SDTSetCC>;
176
177//===----------------------------------------------------------------------===//
178// Selection DAG Condition Codes
179
180class CondCode; // ISD::CondCode enums
181def SETOEQ : CondCode; def SETOGT : CondCode;
182def SETOGE : CondCode; def SETOLT : CondCode; def SETOLE : CondCode;
183def SETONE : CondCode; def SETO : CondCode; def SETUO : CondCode;
184def SETUEQ : CondCode; def SETUGT : CondCode; def SETUGE : CondCode;
185def SETULT : CondCode; def SETULE : CondCode; def SETUNE : CondCode;
186
187def SETEQ : CondCode; def SETGT : CondCode; def SETGE : CondCode;
188def SETLT : CondCode; def SETLE : CondCode; def SETNE : CondCode;
189
190
Chris Lattner17f2cf02005-10-10 06:00:30 +0000191//===----------------------------------------------------------------------===//
192// Selection DAG Node Transformation Functions.
193//
194// This mechanism allows targets to manipulate nodes in the output DAG once a
195// match has been formed. This is typically used to manipulate immediate
196// values.
197//
198class SDNodeXForm<SDNode opc, code xformFunction> {
199 SDNode Opcode = opc;
200 code XFormFunction = xformFunction;
201}
202
203def NOOP_SDNodeXForm : SDNodeXForm<imm, [{}]>;
204
205
206//===----------------------------------------------------------------------===//
207// Selection DAG Pattern Fragments.
208//
209// Pattern fragments are reusable chunks of dags that match specific things.
210// They can take arguments and have C++ predicates that control whether they
211// match. They are intended to make the patterns for common instructions more
212// compact and readable.
213//
214
215/// PatFrag - Represents a pattern fragment. This can match something on the
216/// DAG, frame a single node to multiply nested other fragments.
217///
218class PatFrag<dag ops, dag frag, code pred = [{}],
219 SDNodeXForm xform = NOOP_SDNodeXForm> {
220 dag Operands = ops;
221 dag Fragment = frag;
222 code Predicate = pred;
223 SDNodeXForm OperandTransform = xform;
224}
225
226// PatLeaf's are pattern fragments that have no operands. This is just a helper
227// to define immediates and other common things concisely.
228class PatLeaf<dag frag, code pred = [{}], SDNodeXForm xform = NOOP_SDNodeXForm>
229 : PatFrag<(ops), frag, pred, xform>;
230
231// Leaf fragments.
232
233def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000234
235def vtInt : PatLeaf<(vt), [{ return MVT::isInteger(N->getVT()); }]>;
236def vtFP : PatLeaf<(vt), [{ return MVT::isFloatingPoint(N->getVT()); }]>;
237
238// Other helper fragments.
239
240def not : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>;
Chris Lattnereae6d642005-10-20 23:30:37 +0000241def ineg : PatFrag<(ops node:$in), (sub 0, node:$in)>;
Chris Lattner17f2cf02005-10-10 06:00:30 +0000242
Chris Lattner1f426de2005-10-26 17:00:25 +0000243
244// setcc convenience fragments.
245def setoeq : PatFrag<(ops node:$lhs, node:$rhs),
246 (setcc node:$lhs, node:$rhs, SETOEQ)>;
247def setogt : PatFrag<(ops node:$lhs, node:$rhs),
248 (setcc node:$lhs, node:$rhs, SETOGT)>;
249def setoge : PatFrag<(ops node:$lhs, node:$rhs),
250 (setcc node:$lhs, node:$rhs, SETOGE)>;
251def setolt : PatFrag<(ops node:$lhs, node:$rhs),
252 (setcc node:$lhs, node:$rhs, SETOLT)>;
253def setole : PatFrag<(ops node:$lhs, node:$rhs),
254 (setcc node:$lhs, node:$rhs, SETOLE)>;
255def setone : PatFrag<(ops node:$lhs, node:$rhs),
256 (setcc node:$lhs, node:$rhs, SETONE)>;
257def seto : PatFrag<(ops node:$lhs, node:$rhs),
258 (setcc node:$lhs, node:$rhs, SETO)>;
259def setuo : PatFrag<(ops node:$lhs, node:$rhs),
260 (setcc node:$lhs, node:$rhs, SETUO)>;
261def setueq : PatFrag<(ops node:$lhs, node:$rhs),
262 (setcc node:$lhs, node:$rhs, SETUEQ)>;
263def setugt : PatFrag<(ops node:$lhs, node:$rhs),
264 (setcc node:$lhs, node:$rhs, SETUGT)>;
265def setuge : PatFrag<(ops node:$lhs, node:$rhs),
266 (setcc node:$lhs, node:$rhs, SETUGE)>;
267def setult : PatFrag<(ops node:$lhs, node:$rhs),
268 (setcc node:$lhs, node:$rhs, SETULT)>;
269def setule : PatFrag<(ops node:$lhs, node:$rhs),
270 (setcc node:$lhs, node:$rhs, SETULE)>;
271def setune : PatFrag<(ops node:$lhs, node:$rhs),
272 (setcc node:$lhs, node:$rhs, SETUNE)>;
273def seteq : PatFrag<(ops node:$lhs, node:$rhs),
274 (setcc node:$lhs, node:$rhs, SETEQ)>;
275def setgt : PatFrag<(ops node:$lhs, node:$rhs),
276 (setcc node:$lhs, node:$rhs, SETGT)>;
277def setge : PatFrag<(ops node:$lhs, node:$rhs),
278 (setcc node:$lhs, node:$rhs, SETGE)>;
279def setlt : PatFrag<(ops node:$lhs, node:$rhs),
280 (setcc node:$lhs, node:$rhs, SETLT)>;
281def setle : PatFrag<(ops node:$lhs, node:$rhs),
282 (setcc node:$lhs, node:$rhs, SETLE)>;
283def setne : PatFrag<(ops node:$lhs, node:$rhs),
284 (setcc node:$lhs, node:$rhs, SETNE)>;
285
Chris Lattner17f2cf02005-10-10 06:00:30 +0000286//===----------------------------------------------------------------------===//
287// Selection DAG Pattern Support.
288//
289// Patterns are what are actually matched against the target-flavored
290// instruction selection DAG. Instructions defined by the target implicitly
291// define patterns in most cases, but patterns can also be explicitly added when
292// an operation is defined by a sequence of instructions (e.g. loading a large
293// immediate value on RISC targets that do not support immediates as large as
294// their GPRs).
295//
296
297class Pattern<dag patternToMatch, list<dag> resultInstrs> {
298 dag PatternToMatch = patternToMatch;
299 list<dag> ResultInstrs = resultInstrs;
300}
301
302// Pat - A simple (but common) form of a pattern, which produces a simple result
303// not needing a full list.
304class Pat<dag pattern, dag result> : Pattern<pattern, [result]>;
305