Chris Lattner | 17f2cf0 | 2005-10-10 06:00:30 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 22 | class SDTypeConstraint<int opnum> { |
| 23 | int OperandNum = opnum; |
| 24 | } |
| 25 | |
| 26 | // SDTCisVT - The specified operand has exactly this VT. |
| 27 | class SDTCisVT <int OpNum, ValueType vt> : SDTypeConstraint<OpNum> { |
| 28 | ValueType VT = vt; |
| 29 | } |
| 30 | |
| 31 | // SDTCisInt - The specified operand is has integer type. |
| 32 | class SDTCisInt<int OpNum> : SDTypeConstraint<OpNum>; |
| 33 | |
| 34 | // SDTCisFP - The specified operand is has floating point type. |
| 35 | class SDTCisFP <int OpNum> : SDTypeConstraint<OpNum>; |
| 36 | |
| 37 | // SDTCisSameAs - The two specified operands have identical types. |
| 38 | class 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. |
| 44 | class SDTCisVTSmallerThanOp<int OpNum, int OtherOp> : SDTypeConstraint<OpNum> { |
| 45 | int OtherOperandNum = OtherOp; |
| 46 | } |
| 47 | |
Chris Lattner | 13664a6 | 2005-10-14 04:55:10 +0000 | [diff] [blame] | 48 | class SDTCisOpSmallerThanOp<int SmallOp, int BigOp> : SDTypeConstraint<SmallOp>{ |
| 49 | int BigOperandNum = BigOp; |
| 50 | } |
| 51 | |
Chris Lattner | 17f2cf0 | 2005-10-10 06:00:30 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
| 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. |
| 62 | class 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. |
| 70 | def SDTImm : SDTypeProfile<1, 0, [SDTCisInt<0>]>; // for 'imm'. |
Chris Lattner | 9789826 | 2005-10-25 21:03:14 +0000 | [diff] [blame^] | 71 | def SDTVT : SDTypeProfile<1, 0, [SDTCisVT<0, OtherVT>]>; // for 'vt'. |
| 72 | def SDTUNDEF : SDTypeProfile<1, 0, []>; // for 'undef'. |
Chris Lattner | 17f2cf0 | 2005-10-10 06:00:30 +0000 | [diff] [blame] | 73 | def SDTIntBinOp : SDTypeProfile<1, 2, [ // add, and, or, xor, udiv, etc. |
| 74 | SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisInt<0> |
| 75 | ]>; |
| 76 | def SDTFPBinOp : SDTypeProfile<1, 2, [ // fadd, fmul, etc. |
| 77 | SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisFP<0> |
| 78 | ]>; |
| 79 | def SDTIntUnaryOp : SDTypeProfile<1, 1, [ // ctlz |
| 80 | SDTCisSameAs<0, 1>, SDTCisInt<0> |
| 81 | ]>; |
Chris Lattner | 444215d | 2005-10-14 06:40:20 +0000 | [diff] [blame] | 82 | def SDTIntExtendOp : SDTypeProfile<1, 1, [ // sext, zext, anyext |
| 83 | SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<1, 0> |
| 84 | ]>; |
| 85 | def SDTIntTruncOp : SDTypeProfile<1, 1, [ // trunc |
| 86 | SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<0, 1> |
| 87 | ]>; |
Chris Lattner | 17f2cf0 | 2005-10-10 06:00:30 +0000 | [diff] [blame] | 88 | def SDTFPUnaryOp : SDTypeProfile<1, 1, [ // fneg, fsqrt, etc |
| 89 | SDTCisSameAs<0, 1>, SDTCisFP<0> |
| 90 | ]>; |
Chris Lattner | 13664a6 | 2005-10-14 04:55:10 +0000 | [diff] [blame] | 91 | def SDTFPRoundOp : SDTypeProfile<1, 1, [ // fround |
| 92 | SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<0, 1> |
| 93 | ]>; |
| 94 | def SDTFPExtendOp : SDTypeProfile<1, 1, [ // fextend |
| 95 | SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<1, 0> |
| 96 | ]>; |
Chris Lattner | 17f2cf0 | 2005-10-10 06:00:30 +0000 | [diff] [blame] | 97 | def SDTExtInreg : SDTypeProfile<1, 2, [ // sext_inreg |
| 98 | SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisVT<2, OtherVT>, |
| 99 | SDTCisVTSmallerThanOp<2, 1> |
| 100 | ]>; |
| 101 | |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | // Selection DAG Node Properties. |
| 104 | // |
| 105 | // Note: These are hard coded into tblgen. |
| 106 | // |
| 107 | class SDNodeProperty; |
| 108 | def SDNPCommutative : SDNodeProperty; // X op Y == Y op X |
| 109 | def SDNPAssociative : SDNodeProperty; // (X op Y) op Z == X op (Y op Z) |
| 110 | |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | // Selection DAG Node definitions. |
| 113 | // |
| 114 | class SDNode<string opcode, SDTypeProfile typeprof, |
| 115 | list<SDNodeProperty> props = [], string sdclass = "SDNode"> { |
| 116 | string Opcode = opcode; |
| 117 | string SDClass = sdclass; |
| 118 | list<SDNodeProperty> Properties = props; |
| 119 | SDTypeProfile TypeProfile = typeprof; |
| 120 | } |
| 121 | |
| 122 | def set; |
| 123 | def node; |
| 124 | |
| 125 | def imm : SDNode<"ISD::Constant" , SDTImm , [], "ConstantSDNode">; |
| 126 | def vt : SDNode<"ISD::VALUETYPE" , SDTVT , [], "VTSDNode">; |
Chris Lattner | 9789826 | 2005-10-25 21:03:14 +0000 | [diff] [blame^] | 127 | def undef : SDNode<"ISD::UNDEF" , SDTUNDEF , []>; |
Chris Lattner | 17f2cf0 | 2005-10-10 06:00:30 +0000 | [diff] [blame] | 128 | def add : SDNode<"ISD::ADD" , SDTIntBinOp , |
| 129 | [SDNPCommutative, SDNPAssociative]>; |
| 130 | def sub : SDNode<"ISD::SUB" , SDTIntBinOp>; |
| 131 | def mul : SDNode<"ISD::MUL" , SDTIntBinOp, |
| 132 | [SDNPCommutative, SDNPAssociative]>; |
| 133 | def mulhs : SDNode<"ISD::MULHS" , SDTIntBinOp, [SDNPCommutative]>; |
| 134 | def mulhu : SDNode<"ISD::MULHU" , SDTIntBinOp, [SDNPCommutative]>; |
| 135 | def sdiv : SDNode<"ISD::SDIV" , SDTIntBinOp>; |
| 136 | def udiv : SDNode<"ISD::UDIV" , SDTIntBinOp>; |
| 137 | def srem : SDNode<"ISD::SREM" , SDTIntBinOp>; |
| 138 | def urem : SDNode<"ISD::UREM" , SDTIntBinOp>; |
| 139 | def srl : SDNode<"ISD::SRL" , SDTIntBinOp>; |
| 140 | def sra : SDNode<"ISD::SRA" , SDTIntBinOp>; |
| 141 | def shl : SDNode<"ISD::SHL" , SDTIntBinOp>; |
| 142 | def and : SDNode<"ISD::AND" , SDTIntBinOp, |
| 143 | [SDNPCommutative, SDNPAssociative]>; |
| 144 | def or : SDNode<"ISD::OR" , SDTIntBinOp, |
| 145 | [SDNPCommutative, SDNPAssociative]>; |
| 146 | def xor : SDNode<"ISD::XOR" , SDTIntBinOp, |
| 147 | [SDNPCommutative, SDNPAssociative]>; |
Chris Lattner | 444215d | 2005-10-14 06:40:20 +0000 | [diff] [blame] | 148 | |
| 149 | def sext_inreg : SDNode<"ISD::SIGN_EXTEND_INREG", SDTExtInreg>; |
| 150 | def ctlz : SDNode<"ISD::CTLZ" , SDTIntUnaryOp>; |
Andrew Lenharth | d684e1a | 2005-10-20 19:38:11 +0000 | [diff] [blame] | 151 | def cttz : SDNode<"ISD::CTTZ" , SDTIntUnaryOp>; |
| 152 | def ctpop : SDNode<"ISD::CTPOP" , SDTIntUnaryOp>; |
Chris Lattner | 444215d | 2005-10-14 06:40:20 +0000 | [diff] [blame] | 153 | def sext : SDNode<"ISD::SIGN_EXTEND", SDTIntExtendOp>; |
| 154 | def zext : SDNode<"ISD::ZERO_EXTEND", SDTIntExtendOp>; |
| 155 | def anyext : SDNode<"ISD::ANY_EXTEND" , SDTIntExtendOp>; |
| 156 | def trunc : SDNode<"ISD::TRUNCATE" , SDTIntTruncOp>; |
| 157 | |
Chris Lattner | 17f2cf0 | 2005-10-10 06:00:30 +0000 | [diff] [blame] | 158 | def fadd : SDNode<"ISD::FADD" , SDTFPBinOp, [SDNPCommutative]>; |
| 159 | def fsub : SDNode<"ISD::FSUB" , SDTFPBinOp>; |
| 160 | def fmul : SDNode<"ISD::FMUL" , SDTFPBinOp, [SDNPCommutative]>; |
| 161 | def fdiv : SDNode<"ISD::FDIV" , SDTFPBinOp>; |
| 162 | def frem : SDNode<"ISD::FREM" , SDTFPBinOp>; |
| 163 | def fabs : SDNode<"ISD::FABS" , SDTFPUnaryOp>; |
| 164 | def fneg : SDNode<"ISD::FNEG" , SDTFPUnaryOp>; |
| 165 | def fsqrt : SDNode<"ISD::FSQRT" , SDTFPUnaryOp>; |
| 166 | |
Chris Lattner | 13664a6 | 2005-10-14 04:55:10 +0000 | [diff] [blame] | 167 | def fround : SDNode<"ISD::FP_ROUND" , SDTFPRoundOp>; |
| 168 | def fextend : SDNode<"ISD::FP_EXTEND" , SDTFPExtendOp>; |
| 169 | |
Chris Lattner | 17f2cf0 | 2005-10-10 06:00:30 +0000 | [diff] [blame] | 170 | //===----------------------------------------------------------------------===// |
| 171 | // Selection DAG Node Transformation Functions. |
| 172 | // |
| 173 | // This mechanism allows targets to manipulate nodes in the output DAG once a |
| 174 | // match has been formed. This is typically used to manipulate immediate |
| 175 | // values. |
| 176 | // |
| 177 | class SDNodeXForm<SDNode opc, code xformFunction> { |
| 178 | SDNode Opcode = opc; |
| 179 | code XFormFunction = xformFunction; |
| 180 | } |
| 181 | |
| 182 | def NOOP_SDNodeXForm : SDNodeXForm<imm, [{}]>; |
| 183 | |
| 184 | |
| 185 | //===----------------------------------------------------------------------===// |
| 186 | // Selection DAG Pattern Fragments. |
| 187 | // |
| 188 | // Pattern fragments are reusable chunks of dags that match specific things. |
| 189 | // They can take arguments and have C++ predicates that control whether they |
| 190 | // match. They are intended to make the patterns for common instructions more |
| 191 | // compact and readable. |
| 192 | // |
| 193 | |
| 194 | /// PatFrag - Represents a pattern fragment. This can match something on the |
| 195 | /// DAG, frame a single node to multiply nested other fragments. |
| 196 | /// |
| 197 | class PatFrag<dag ops, dag frag, code pred = [{}], |
| 198 | SDNodeXForm xform = NOOP_SDNodeXForm> { |
| 199 | dag Operands = ops; |
| 200 | dag Fragment = frag; |
| 201 | code Predicate = pred; |
| 202 | SDNodeXForm OperandTransform = xform; |
| 203 | } |
| 204 | |
| 205 | // PatLeaf's are pattern fragments that have no operands. This is just a helper |
| 206 | // to define immediates and other common things concisely. |
| 207 | class PatLeaf<dag frag, code pred = [{}], SDNodeXForm xform = NOOP_SDNodeXForm> |
| 208 | : PatFrag<(ops), frag, pred, xform>; |
| 209 | |
| 210 | // Leaf fragments. |
| 211 | |
| 212 | def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>; |
Chris Lattner | 17f2cf0 | 2005-10-10 06:00:30 +0000 | [diff] [blame] | 213 | |
| 214 | def vtInt : PatLeaf<(vt), [{ return MVT::isInteger(N->getVT()); }]>; |
| 215 | def vtFP : PatLeaf<(vt), [{ return MVT::isFloatingPoint(N->getVT()); }]>; |
| 216 | |
| 217 | // Other helper fragments. |
| 218 | |
| 219 | def not : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>; |
Chris Lattner | eae6d64 | 2005-10-20 23:30:37 +0000 | [diff] [blame] | 220 | def ineg : PatFrag<(ops node:$in), (sub 0, node:$in)>; |
Chris Lattner | 17f2cf0 | 2005-10-10 06:00:30 +0000 | [diff] [blame] | 221 | |
| 222 | //===----------------------------------------------------------------------===// |
| 223 | // Selection DAG Pattern Support. |
| 224 | // |
| 225 | // Patterns are what are actually matched against the target-flavored |
| 226 | // instruction selection DAG. Instructions defined by the target implicitly |
| 227 | // define patterns in most cases, but patterns can also be explicitly added when |
| 228 | // an operation is defined by a sequence of instructions (e.g. loading a large |
| 229 | // immediate value on RISC targets that do not support immediates as large as |
| 230 | // their GPRs). |
| 231 | // |
| 232 | |
| 233 | class Pattern<dag patternToMatch, list<dag> resultInstrs> { |
| 234 | dag PatternToMatch = patternToMatch; |
| 235 | list<dag> ResultInstrs = resultInstrs; |
| 236 | } |
| 237 | |
| 238 | // Pat - A simple (but common) form of a pattern, which produces a simple result |
| 239 | // not needing a full list. |
| 240 | class Pat<dag pattern, dag result> : Pattern<pattern, [result]>; |
| 241 | |