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 | |
| 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. |
| 58 | class 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. |
| 66 | def SDTImm : SDTypeProfile<1, 0, [SDTCisInt<0>]>; // for 'imm'. |
| 67 | def SDTVT : SDTypeProfile<1, 0, [SDTCisVT<0, OtherVT>]>; // for 'vt' |
| 68 | def SDTIntBinOp : SDTypeProfile<1, 2, [ // add, and, or, xor, udiv, etc. |
| 69 | SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisInt<0> |
| 70 | ]>; |
| 71 | def SDTFPBinOp : SDTypeProfile<1, 2, [ // fadd, fmul, etc. |
| 72 | SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisFP<0> |
| 73 | ]>; |
| 74 | def SDTIntUnaryOp : SDTypeProfile<1, 1, [ // ctlz |
| 75 | SDTCisSameAs<0, 1>, SDTCisInt<0> |
| 76 | ]>; |
| 77 | def SDTFPUnaryOp : SDTypeProfile<1, 1, [ // fneg, fsqrt, etc |
| 78 | SDTCisSameAs<0, 1>, SDTCisFP<0> |
| 79 | ]>; |
| 80 | def 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 | // |
| 90 | class SDNodeProperty; |
| 91 | def SDNPCommutative : SDNodeProperty; // X op Y == Y op X |
| 92 | def SDNPAssociative : SDNodeProperty; // (X op Y) op Z == X op (Y op Z) |
| 93 | |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | // Selection DAG Node definitions. |
| 96 | // |
| 97 | class 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 | |
| 105 | def set; |
| 106 | def node; |
| 107 | |
| 108 | def imm : SDNode<"ISD::Constant" , SDTImm , [], "ConstantSDNode">; |
| 109 | def vt : SDNode<"ISD::VALUETYPE" , SDTVT , [], "VTSDNode">; |
| 110 | def add : SDNode<"ISD::ADD" , SDTIntBinOp , |
| 111 | [SDNPCommutative, SDNPAssociative]>; |
| 112 | def sub : SDNode<"ISD::SUB" , SDTIntBinOp>; |
| 113 | def mul : SDNode<"ISD::MUL" , SDTIntBinOp, |
| 114 | [SDNPCommutative, SDNPAssociative]>; |
| 115 | def mulhs : SDNode<"ISD::MULHS" , SDTIntBinOp, [SDNPCommutative]>; |
| 116 | def mulhu : SDNode<"ISD::MULHU" , SDTIntBinOp, [SDNPCommutative]>; |
| 117 | def sdiv : SDNode<"ISD::SDIV" , SDTIntBinOp>; |
| 118 | def udiv : SDNode<"ISD::UDIV" , SDTIntBinOp>; |
| 119 | def srem : SDNode<"ISD::SREM" , SDTIntBinOp>; |
| 120 | def urem : SDNode<"ISD::UREM" , SDTIntBinOp>; |
| 121 | def srl : SDNode<"ISD::SRL" , SDTIntBinOp>; |
| 122 | def sra : SDNode<"ISD::SRA" , SDTIntBinOp>; |
| 123 | def shl : SDNode<"ISD::SHL" , SDTIntBinOp>; |
| 124 | def and : SDNode<"ISD::AND" , SDTIntBinOp, |
| 125 | [SDNPCommutative, SDNPAssociative]>; |
| 126 | def or : SDNode<"ISD::OR" , SDTIntBinOp, |
| 127 | [SDNPCommutative, SDNPAssociative]>; |
| 128 | def xor : SDNode<"ISD::XOR" , SDTIntBinOp, |
| 129 | [SDNPCommutative, SDNPAssociative]>; |
| 130 | def fadd : SDNode<"ISD::FADD" , SDTFPBinOp, [SDNPCommutative]>; |
| 131 | def fsub : SDNode<"ISD::FSUB" , SDTFPBinOp>; |
| 132 | def fmul : SDNode<"ISD::FMUL" , SDTFPBinOp, [SDNPCommutative]>; |
| 133 | def fdiv : SDNode<"ISD::FDIV" , SDTFPBinOp>; |
| 134 | def frem : SDNode<"ISD::FREM" , SDTFPBinOp>; |
| 135 | def fabs : SDNode<"ISD::FABS" , SDTFPUnaryOp>; |
| 136 | def fneg : SDNode<"ISD::FNEG" , SDTFPUnaryOp>; |
| 137 | def fsqrt : SDNode<"ISD::FSQRT" , SDTFPUnaryOp>; |
| 138 | |
| 139 | def sext_inreg : SDNode<"ISD::SIGN_EXTEND_INREG", SDTExtInreg>; |
| 140 | def 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 | // |
| 149 | class SDNodeXForm<SDNode opc, code xformFunction> { |
| 150 | SDNode Opcode = opc; |
| 151 | code XFormFunction = xformFunction; |
| 152 | } |
| 153 | |
| 154 | def 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 | /// |
| 169 | class 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. |
| 179 | class PatLeaf<dag frag, code pred = [{}], SDNodeXForm xform = NOOP_SDNodeXForm> |
| 180 | : PatFrag<(ops), frag, pred, xform>; |
| 181 | |
| 182 | // Leaf fragments. |
| 183 | |
| 184 | def immAllOnes : PatLeaf<(imm), [{ return N->isAllOnesValue(); }]>; |
| 185 | def immZero : PatLeaf<(imm), [{ return N->isNullValue(); }]>; |
| 186 | |
| 187 | def vtInt : PatLeaf<(vt), [{ return MVT::isInteger(N->getVT()); }]>; |
| 188 | def vtFP : PatLeaf<(vt), [{ return MVT::isFloatingPoint(N->getVT()); }]>; |
| 189 | |
| 190 | // Other helper fragments. |
| 191 | |
| 192 | def not : PatFrag<(ops node:$in), (xor node:$in, immAllOnes)>; |
| 193 | def 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 | |
| 206 | class 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. |
| 213 | class Pat<dag pattern, dag result> : Pattern<pattern, [result]>; |
| 214 | |