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