1 //===- TargetSelectionDAG.td - Common code for DAG isels ---*- tablegen -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the target-independent interfaces used by SelectionDAG
11 // instruction selection generators.
13 //===----------------------------------------------------------------------===//
15 //===----------------------------------------------------------------------===//
16 // Selection DAG Type Constraint definitions.
18 // Note that the semantics of these constraints are hard coded into tblgen. To
19 // modify or add constraints, you have to hack tblgen.
22 class SDTypeConstraint<int opnum> {
23 int OperandNum = opnum;
26 // SDTCisVT - The specified operand has exactly this VT.
27 class SDTCisVT <int OpNum, ValueType vt> : SDTypeConstraint<OpNum> {
31 // SDTCisInt - The specified operand is has integer type.
32 class SDTCisInt<int OpNum> : SDTypeConstraint<OpNum>;
34 // SDTCisFP - The specified operand is has floating point type.
35 class SDTCisFP <int OpNum> : SDTypeConstraint<OpNum>;
37 // SDTCisSameAs - The two specified operands have identical types.
38 class SDTCisSameAs<int OpNum, int OtherOp> : SDTypeConstraint<OpNum> {
39 int OtherOperandNum = OtherOp;
42 // SDTCisVTSmallerThanOp - The specified operand is a VT SDNode, and its type is
43 // smaller than the 'Other' operand.
44 class SDTCisVTSmallerThanOp<int OpNum, int OtherOp> : SDTypeConstraint<OpNum> {
45 int OtherOperandNum = OtherOp;
48 class SDTCisOpSmallerThanOp<int SmallOp, int BigOp> : SDTypeConstraint<SmallOp>{
49 int BigOperandNum = BigOp;
52 //===----------------------------------------------------------------------===//
53 // Selection DAG Type Profile definitions.
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.
60 // SDTypeProfile - This profile describes the type requirements of a Selection
62 class SDTypeProfile<int numresults, int numoperands,
63 list<SDTypeConstraint> constraints> {
64 int NumResults = numresults;
65 int NumOperands = numoperands;
66 list<SDTypeConstraint> Constraints = constraints;
70 def SDTImm : SDTypeProfile<1, 0, [SDTCisInt<0>]>; // for 'imm'.
71 def SDTVT : SDTypeProfile<1, 0, [SDTCisVT<0, OtherVT>]>; // for 'vt'
72 def SDTIntBinOp : SDTypeProfile<1, 2, [ // add, and, or, xor, udiv, etc.
73 SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisInt<0>
75 def SDTFPBinOp : SDTypeProfile<1, 2, [ // fadd, fmul, etc.
76 SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisFP<0>
78 def SDTIntUnaryOp : SDTypeProfile<1, 1, [ // ctlz
79 SDTCisSameAs<0, 1>, SDTCisInt<0>
81 def SDTIntExtendOp : SDTypeProfile<1, 1, [ // sext, zext, anyext
82 SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<1, 0>
84 def SDTIntTruncOp : SDTypeProfile<1, 1, [ // trunc
85 SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<0, 1>
87 def SDTFPUnaryOp : SDTypeProfile<1, 1, [ // fneg, fsqrt, etc
88 SDTCisSameAs<0, 1>, SDTCisFP<0>
90 def SDTFPRoundOp : SDTypeProfile<1, 1, [ // fround
91 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<0, 1>
93 def SDTFPExtendOp : SDTypeProfile<1, 1, [ // fextend
94 SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<1, 0>
96 def SDTExtInreg : SDTypeProfile<1, 2, [ // sext_inreg
97 SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisVT<2, OtherVT>,
98 SDTCisVTSmallerThanOp<2, 1>
101 //===----------------------------------------------------------------------===//
102 // Selection DAG Node Properties.
104 // Note: These are hard coded into tblgen.
106 class SDNodeProperty;
107 def SDNPCommutative : SDNodeProperty; // X op Y == Y op X
108 def SDNPAssociative : SDNodeProperty; // (X op Y) op Z == X op (Y op Z)
110 //===----------------------------------------------------------------------===//
111 // Selection DAG Node definitions.
113 class SDNode<string opcode, SDTypeProfile typeprof,
114 list<SDNodeProperty> props = [], string sdclass = "SDNode"> {
115 string Opcode = opcode;
116 string SDClass = sdclass;
117 list<SDNodeProperty> Properties = props;
118 SDTypeProfile TypeProfile = typeprof;
124 def imm : SDNode<"ISD::Constant" , SDTImm , [], "ConstantSDNode">;
125 def vt : SDNode<"ISD::VALUETYPE" , SDTVT , [], "VTSDNode">;
126 def add : SDNode<"ISD::ADD" , SDTIntBinOp ,
127 [SDNPCommutative, SDNPAssociative]>;
128 def sub : SDNode<"ISD::SUB" , SDTIntBinOp>;
129 def mul : SDNode<"ISD::MUL" , SDTIntBinOp,
130 [SDNPCommutative, SDNPAssociative]>;
131 def mulhs : SDNode<"ISD::MULHS" , SDTIntBinOp, [SDNPCommutative]>;
132 def mulhu : SDNode<"ISD::MULHU" , SDTIntBinOp, [SDNPCommutative]>;
133 def sdiv : SDNode<"ISD::SDIV" , SDTIntBinOp>;
134 def udiv : SDNode<"ISD::UDIV" , SDTIntBinOp>;
135 def srem : SDNode<"ISD::SREM" , SDTIntBinOp>;
136 def urem : SDNode<"ISD::UREM" , SDTIntBinOp>;
137 def srl : SDNode<"ISD::SRL" , SDTIntBinOp>;
138 def sra : SDNode<"ISD::SRA" , SDTIntBinOp>;
139 def shl : SDNode<"ISD::SHL" , SDTIntBinOp>;
140 def and : SDNode<"ISD::AND" , SDTIntBinOp,
141 [SDNPCommutative, SDNPAssociative]>;
142 def or : SDNode<"ISD::OR" , SDTIntBinOp,
143 [SDNPCommutative, SDNPAssociative]>;
144 def xor : SDNode<"ISD::XOR" , SDTIntBinOp,
145 [SDNPCommutative, SDNPAssociative]>;
147 def sext_inreg : SDNode<"ISD::SIGN_EXTEND_INREG", SDTExtInreg>;
148 def ctlz : SDNode<"ISD::CTLZ" , SDTIntUnaryOp>;
149 def cttz : SDNode<"ISD::CTTZ" , SDTIntUnaryOp>;
150 def ctpop : SDNode<"ISD::CTPOP" , SDTIntUnaryOp>;
151 def sext : SDNode<"ISD::SIGN_EXTEND", SDTIntExtendOp>;
152 def zext : SDNode<"ISD::ZERO_EXTEND", SDTIntExtendOp>;
153 def anyext : SDNode<"ISD::ANY_EXTEND" , SDTIntExtendOp>;
154 def trunc : SDNode<"ISD::TRUNCATE" , SDTIntTruncOp>;
156 def fadd : SDNode<"ISD::FADD" , SDTFPBinOp, [SDNPCommutative]>;
157 def fsub : SDNode<"ISD::FSUB" , SDTFPBinOp>;
158 def fmul : SDNode<"ISD::FMUL" , SDTFPBinOp, [SDNPCommutative]>;
159 def fdiv : SDNode<"ISD::FDIV" , SDTFPBinOp>;
160 def frem : SDNode<"ISD::FREM" , SDTFPBinOp>;
161 def fabs : SDNode<"ISD::FABS" , SDTFPUnaryOp>;
162 def fneg : SDNode<"ISD::FNEG" , SDTFPUnaryOp>;
163 def fsqrt : SDNode<"ISD::FSQRT" , SDTFPUnaryOp>;
165 def fround : SDNode<"ISD::FP_ROUND" , SDTFPRoundOp>;
166 def fextend : SDNode<"ISD::FP_EXTEND" , SDTFPExtendOp>;
168 //===----------------------------------------------------------------------===//
169 // Selection DAG Node Transformation Functions.
171 // This mechanism allows targets to manipulate nodes in the output DAG once a
172 // match has been formed. This is typically used to manipulate immediate
175 class SDNodeXForm<SDNode opc, code xformFunction> {
177 code XFormFunction = xformFunction;
180 def NOOP_SDNodeXForm : SDNodeXForm<imm, [{}]>;
183 //===----------------------------------------------------------------------===//
184 // Selection DAG Pattern Fragments.
186 // Pattern fragments are reusable chunks of dags that match specific things.
187 // They can take arguments and have C++ predicates that control whether they
188 // match. They are intended to make the patterns for common instructions more
189 // compact and readable.
192 /// PatFrag - Represents a pattern fragment. This can match something on the
193 /// DAG, frame a single node to multiply nested other fragments.
195 class PatFrag<dag ops, dag frag, code pred = [{}],
196 SDNodeXForm xform = NOOP_SDNodeXForm> {
199 code Predicate = pred;
200 SDNodeXForm OperandTransform = xform;
203 // PatLeaf's are pattern fragments that have no operands. This is just a helper
204 // to define immediates and other common things concisely.
205 class PatLeaf<dag frag, code pred = [{}], SDNodeXForm xform = NOOP_SDNodeXForm>
206 : PatFrag<(ops), frag, pred, xform>;
210 def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>;
211 def immZero : PatLeaf<(imm), [{ return N->isNullValue(); }]>;
213 def vtInt : PatLeaf<(vt), [{ return MVT::isInteger(N->getVT()); }]>;
214 def vtFP : PatLeaf<(vt), [{ return MVT::isFloatingPoint(N->getVT()); }]>;
216 // Other helper fragments.
218 def not : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>;
219 def ineg : PatFrag<(ops node:$in), (sub immZero, node:$in)>;
221 //===----------------------------------------------------------------------===//
222 // Selection DAG Pattern Support.
224 // Patterns are what are actually matched against the target-flavored
225 // instruction selection DAG. Instructions defined by the target implicitly
226 // define patterns in most cases, but patterns can also be explicitly added when
227 // an operation is defined by a sequence of instructions (e.g. loading a large
228 // immediate value on RISC targets that do not support immediates as large as
232 class Pattern<dag patternToMatch, list<dag> resultInstrs> {
233 dag PatternToMatch = patternToMatch;
234 list<dag> ResultInstrs = resultInstrs;
237 // Pat - A simple (but common) form of a pattern, which produces a simple result
238 // not needing a full list.
239 class Pat<dag pattern, dag result> : Pattern<pattern, [result]>;