blob: 702659a452f3baad233abeffc080b619e48bcf93 [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'.
71def SDTVT : SDTypeProfile<1, 0, [SDTCisVT<0, OtherVT>]>; // for 'vt'
72def SDTIntBinOp : SDTypeProfile<1, 2, [ // add, and, or, xor, udiv, etc.
73 SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisInt<0>
74]>;
75def SDTFPBinOp : SDTypeProfile<1, 2, [ // fadd, fmul, etc.
76 SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisFP<0>
77]>;
78def SDTIntUnaryOp : SDTypeProfile<1, 1, [ // ctlz
79 SDTCisSameAs<0, 1>, SDTCisInt<0>
80]>;
81def SDTFPUnaryOp : SDTypeProfile<1, 1, [ // fneg, fsqrt, etc
82 SDTCisSameAs<0, 1>, SDTCisFP<0>
83]>;
Chris Lattner13664a62005-10-14 04:55:10 +000084def SDTFPRoundOp : SDTypeProfile<1, 1, [ // fround
85 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<0, 1>
86]>;
87def SDTFPExtendOp : SDTypeProfile<1, 1, [ // fextend
88 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<1, 0>
89]>;
Chris Lattner17f2cf02005-10-10 06:00:30 +000090def SDTExtInreg : SDTypeProfile<1, 2, [ // sext_inreg
91 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisVT<2, OtherVT>,
92 SDTCisVTSmallerThanOp<2, 1>
93]>;
94
95//===----------------------------------------------------------------------===//
96// Selection DAG Node Properties.
97//
98// Note: These are hard coded into tblgen.
99//
100class SDNodeProperty;
101def SDNPCommutative : SDNodeProperty; // X op Y == Y op X
102def SDNPAssociative : SDNodeProperty; // (X op Y) op Z == X op (Y op Z)
103
104//===----------------------------------------------------------------------===//
105// Selection DAG Node definitions.
106//
107class SDNode<string opcode, SDTypeProfile typeprof,
108 list<SDNodeProperty> props = [], string sdclass = "SDNode"> {
109 string Opcode = opcode;
110 string SDClass = sdclass;
111 list<SDNodeProperty> Properties = props;
112 SDTypeProfile TypeProfile = typeprof;
113}
114
115def set;
116def node;
117
118def imm : SDNode<"ISD::Constant" , SDTImm , [], "ConstantSDNode">;
119def vt : SDNode<"ISD::VALUETYPE" , SDTVT , [], "VTSDNode">;
120def add : SDNode<"ISD::ADD" , SDTIntBinOp ,
121 [SDNPCommutative, SDNPAssociative]>;
122def sub : SDNode<"ISD::SUB" , SDTIntBinOp>;
123def mul : SDNode<"ISD::MUL" , SDTIntBinOp,
124 [SDNPCommutative, SDNPAssociative]>;
125def mulhs : SDNode<"ISD::MULHS" , SDTIntBinOp, [SDNPCommutative]>;
126def mulhu : SDNode<"ISD::MULHU" , SDTIntBinOp, [SDNPCommutative]>;
127def sdiv : SDNode<"ISD::SDIV" , SDTIntBinOp>;
128def udiv : SDNode<"ISD::UDIV" , SDTIntBinOp>;
129def srem : SDNode<"ISD::SREM" , SDTIntBinOp>;
130def urem : SDNode<"ISD::UREM" , SDTIntBinOp>;
131def srl : SDNode<"ISD::SRL" , SDTIntBinOp>;
132def sra : SDNode<"ISD::SRA" , SDTIntBinOp>;
133def shl : SDNode<"ISD::SHL" , SDTIntBinOp>;
134def and : SDNode<"ISD::AND" , SDTIntBinOp,
135 [SDNPCommutative, SDNPAssociative]>;
136def or : SDNode<"ISD::OR" , SDTIntBinOp,
137 [SDNPCommutative, SDNPAssociative]>;
138def xor : SDNode<"ISD::XOR" , SDTIntBinOp,
139 [SDNPCommutative, SDNPAssociative]>;
140def fadd : SDNode<"ISD::FADD" , SDTFPBinOp, [SDNPCommutative]>;
141def fsub : SDNode<"ISD::FSUB" , SDTFPBinOp>;
142def fmul : SDNode<"ISD::FMUL" , SDTFPBinOp, [SDNPCommutative]>;
143def fdiv : SDNode<"ISD::FDIV" , SDTFPBinOp>;
144def frem : SDNode<"ISD::FREM" , SDTFPBinOp>;
145def fabs : SDNode<"ISD::FABS" , SDTFPUnaryOp>;
146def fneg : SDNode<"ISD::FNEG" , SDTFPUnaryOp>;
147def fsqrt : SDNode<"ISD::FSQRT" , SDTFPUnaryOp>;
148
Chris Lattner13664a62005-10-14 04:55:10 +0000149def fround : SDNode<"ISD::FP_ROUND" , SDTFPRoundOp>;
150def fextend : SDNode<"ISD::FP_EXTEND" , SDTFPExtendOp>;
151
Chris Lattner17f2cf02005-10-10 06:00:30 +0000152def sext_inreg : SDNode<"ISD::SIGN_EXTEND_INREG", SDTExtInreg>;
153def ctlz : SDNode<"ISD::CTLZ" , SDTIntUnaryOp>;
154
155//===----------------------------------------------------------------------===//
156// Selection DAG Node Transformation Functions.
157//
158// This mechanism allows targets to manipulate nodes in the output DAG once a
159// match has been formed. This is typically used to manipulate immediate
160// values.
161//
162class SDNodeXForm<SDNode opc, code xformFunction> {
163 SDNode Opcode = opc;
164 code XFormFunction = xformFunction;
165}
166
167def NOOP_SDNodeXForm : SDNodeXForm<imm, [{}]>;
168
169
170//===----------------------------------------------------------------------===//
171// Selection DAG Pattern Fragments.
172//
173// Pattern fragments are reusable chunks of dags that match specific things.
174// They can take arguments and have C++ predicates that control whether they
175// match. They are intended to make the patterns for common instructions more
176// compact and readable.
177//
178
179/// PatFrag - Represents a pattern fragment. This can match something on the
180/// DAG, frame a single node to multiply nested other fragments.
181///
182class PatFrag<dag ops, dag frag, code pred = [{}],
183 SDNodeXForm xform = NOOP_SDNodeXForm> {
184 dag Operands = ops;
185 dag Fragment = frag;
186 code Predicate = pred;
187 SDNodeXForm OperandTransform = xform;
188}
189
190// PatLeaf's are pattern fragments that have no operands. This is just a helper
191// to define immediates and other common things concisely.
192class PatLeaf<dag frag, code pred = [{}], SDNodeXForm xform = NOOP_SDNodeXForm>
193 : PatFrag<(ops), frag, pred, xform>;
194
195// Leaf fragments.
196
197def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>;
198def immZero : PatLeaf<(imm), [{ return N->isNullValue(); }]>;
199
200def vtInt : PatLeaf<(vt), [{ return MVT::isInteger(N->getVT()); }]>;
201def vtFP : PatLeaf<(vt), [{ return MVT::isFloatingPoint(N->getVT()); }]>;
202
203// Other helper fragments.
204
205def not : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>;
206def ineg : PatFrag<(ops node:$in), (sub immZero, node:$in)>;
207
208//===----------------------------------------------------------------------===//
209// Selection DAG Pattern Support.
210//
211// Patterns are what are actually matched against the target-flavored
212// instruction selection DAG. Instructions defined by the target implicitly
213// define patterns in most cases, but patterns can also be explicitly added when
214// an operation is defined by a sequence of instructions (e.g. loading a large
215// immediate value on RISC targets that do not support immediates as large as
216// their GPRs).
217//
218
219class Pattern<dag patternToMatch, list<dag> resultInstrs> {
220 dag PatternToMatch = patternToMatch;
221 list<dag> ResultInstrs = resultInstrs;
222}
223
224// Pat - A simple (but common) form of a pattern, which produces a simple result
225// not needing a full list.
226class Pat<dag pattern, dag result> : Pattern<pattern, [result]>;
227