Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 1 | //===-- LegalizeVectorOps.cpp - Implement SelectionDAG::LegalizeVectors ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SelectionDAG::LegalizeVectors method. |
| 11 | // |
| 12 | // The vector legalizer looks for vector operations which might need to be |
Eli Friedman | 3b25170 | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 13 | // scalarized and legalizes them. This is a separate step from Legalize because |
| 14 | // scalarizing can introduce illegal types. For example, suppose we have an |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 15 | // ISD::SDIV of type v2i64 on x86-32. The type is legal (for example, addition |
| 16 | // on a v2i64 is legal), but ISD::SDIV isn't legal, so we have to unroll the |
| 17 | // operation, which introduces nodes with the illegal type i64 which must be |
| 18 | // expanded. Similarly, suppose we have an ISD::SRA of type v16i8 on PowerPC; |
| 19 | // the operation must be unrolled, which introduces nodes with the illegal |
| 20 | // type i8 which must be promoted. |
| 21 | // |
| 22 | // This does not legalize vector manipulations like ISD::BUILD_VECTOR, |
Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 23 | // or operations that happen to take a vector which are custom-lowered; |
| 24 | // the legalization for such operations never produces nodes |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 25 | // with illegal types, so it's okay to put off legalizing them until |
| 26 | // SelectionDAG::Legalize runs. |
| 27 | // |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | #include "llvm/CodeGen/SelectionDAG.h" |
| 31 | #include "llvm/Target/TargetLowering.h" |
| 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
| 35 | class VectorLegalizer { |
| 36 | SelectionDAG& DAG; |
Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 37 | const TargetLowering &TLI; |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 38 | bool Changed; // Keep track of whether anything changed |
| 39 | |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 40 | /// For nodes that are of legal width, and that have more than one use, this |
| 41 | /// map indicates what regularized operand to use. This allows us to avoid |
| 42 | /// legalizing the same thing more than once. |
Preston Gurd | 0959bb7 | 2013-01-25 15:18:54 +0000 | [diff] [blame] | 43 | SmallDenseMap<SDValue, SDValue, 64> LegalizedNodes; |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 44 | |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 45 | /// \brief Adds a node to the translation cache. |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 46 | void AddLegalizedOperand(SDValue From, SDValue To) { |
| 47 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 48 | // If someone requests legalization of the new node, return itself. |
| 49 | if (From != To) |
| 50 | LegalizedNodes.insert(std::make_pair(To, To)); |
| 51 | } |
| 52 | |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 53 | /// \brief Legalizes the given node. |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 54 | SDValue LegalizeOp(SDValue Op); |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 55 | |
| 56 | /// \brief Assuming the node is legal, "legalize" the results. |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 57 | SDValue TranslateLegalizeResults(SDValue Op, SDValue Result); |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 58 | |
| 59 | /// \brief Implements unrolling a VSETCC. |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 60 | SDValue UnrollVSETCC(SDValue Op); |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 61 | |
Chandler Carruth | c1bedac | 2014-07-02 06:23:34 +0000 | [diff] [blame] | 62 | /// \brief Implement expand-based legalization of vector operations. |
| 63 | /// |
| 64 | /// This is just a high-level routine to dispatch to specific code paths for |
| 65 | /// operations to legalize them. |
| 66 | SDValue Expand(SDValue Op); |
| 67 | |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 68 | /// \brief Implements expansion for FNEG; falls back to UnrollVectorOp if |
| 69 | /// FSUB isn't legal. |
| 70 | /// |
| 71 | /// Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if |
| 72 | /// SINT_TO_FLOAT and SHR on vectors isn't legal. |
Nadav Rotem | e7a101c | 2011-03-19 13:09:10 +0000 | [diff] [blame] | 73 | SDValue ExpandUINT_TO_FLOAT(SDValue Op); |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 74 | |
| 75 | /// \brief Implement expansion for SIGN_EXTEND_INREG using SRL and SRA. |
Nadav Rotem | dbe5c72 | 2013-01-11 22:57:48 +0000 | [diff] [blame] | 76 | SDValue ExpandSEXTINREG(SDValue Op); |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 77 | |
| 78 | /// \brief Expand bswap of vectors into a shuffle if legal. |
Benjamin Kramer | f3ad235 | 2014-05-19 13:12:38 +0000 | [diff] [blame] | 79 | SDValue ExpandBSWAP(SDValue Op); |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 80 | |
| 81 | /// \brief Implement vselect in terms of XOR, AND, OR when blend is not |
| 82 | /// supported by the target. |
Nadav Rotem | 52202fb | 2011-09-13 19:17:42 +0000 | [diff] [blame] | 83 | SDValue ExpandVSELECT(SDValue Op); |
Nadav Rotem | ea973bd | 2012-08-30 19:17:29 +0000 | [diff] [blame] | 84 | SDValue ExpandSELECT(SDValue Op); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 85 | SDValue ExpandLoad(SDValue Op); |
| 86 | SDValue ExpandStore(SDValue Op); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 87 | SDValue ExpandFNEG(SDValue Op); |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 88 | |
| 89 | /// \brief Implements vector promotion. |
| 90 | /// |
| 91 | /// This is essentially just bitcasting the operands to a different type and |
| 92 | /// bitcasting the result back to the original type. |
Chandler Carruth | 1cfa895 | 2014-07-02 03:07:11 +0000 | [diff] [blame] | 93 | SDValue Promote(SDValue Op); |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 94 | |
| 95 | /// \brief Implements [SU]INT_TO_FP vector promotion. |
| 96 | /// |
| 97 | /// This is a [zs]ext of the input operand to the next size up. |
Chandler Carruth | 1cfa895 | 2014-07-02 03:07:11 +0000 | [diff] [blame] | 98 | SDValue PromoteINT_TO_FP(SDValue Op); |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 99 | |
| 100 | /// \brief Implements FP_TO_[SU]INT vector promotion of the result type. |
| 101 | /// |
| 102 | /// It is promoted to the next size up integer type. The result is then |
| 103 | /// truncated back to the original type. |
Chandler Carruth | 1cfa895 | 2014-07-02 03:07:11 +0000 | [diff] [blame] | 104 | SDValue PromoteFP_TO_INT(SDValue Op, bool isSigned); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 105 | |
Chandler Carruth | 68adf15 | 2014-07-02 02:16:57 +0000 | [diff] [blame] | 106 | public: |
| 107 | /// \brief Begin legalizer the vector operations in the DAG. |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 108 | bool Run(); |
| 109 | VectorLegalizer(SelectionDAG& dag) : |
| 110 | DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {} |
| 111 | }; |
| 112 | |
| 113 | bool VectorLegalizer::Run() { |
Nadav Rotem | b7f90bd | 2013-02-22 23:33:30 +0000 | [diff] [blame] | 114 | // Before we start legalizing vector nodes, check if there are any vectors. |
| 115 | bool HasVectors = false; |
| 116 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 117 | E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) { |
Nadav Rotem | b7f90bd | 2013-02-22 23:33:30 +0000 | [diff] [blame] | 118 | // Check if the values of the nodes contain vectors. We don't need to check |
| 119 | // the operands because we are going to check their values at some point. |
| 120 | for (SDNode::value_iterator J = I->value_begin(), E = I->value_end(); |
| 121 | J != E; ++J) |
| 122 | HasVectors |= J->isVector(); |
| 123 | |
| 124 | // If we found a vector node we can start the legalization. |
| 125 | if (HasVectors) |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | // If this basic block has no vectors then no need to legalize vectors. |
| 130 | if (!HasVectors) |
| 131 | return false; |
| 132 | |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 133 | // The legalize process is inherently a bottom-up recursive process (users |
| 134 | // legalize their uses before themselves). Given infinite stack space, we |
| 135 | // could just start legalizing on the root and traverse the whole graph. In |
| 136 | // practice however, this causes us to run out of stack space on large basic |
| 137 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 138 | // node is only legalized after all of its operands are legalized. |
| 139 | DAG.AssignTopologicalOrder(); |
| 140 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 141 | E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 142 | LegalizeOp(SDValue(I, 0)); |
| 143 | |
| 144 | // Finally, it's possible the root changed. Get the new root. |
| 145 | SDValue OldRoot = DAG.getRoot(); |
| 146 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 147 | DAG.setRoot(LegalizedNodes[OldRoot]); |
| 148 | |
| 149 | LegalizedNodes.clear(); |
| 150 | |
| 151 | // Remove dead nodes now. |
| 152 | DAG.RemoveDeadNodes(); |
| 153 | |
| 154 | return Changed; |
| 155 | } |
| 156 | |
| 157 | SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) { |
| 158 | // Generic legalization: just pass the operand through. |
| 159 | for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i) |
| 160 | AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); |
| 161 | return Result.getValue(Op.getResNo()); |
| 162 | } |
| 163 | |
| 164 | SDValue VectorLegalizer::LegalizeOp(SDValue Op) { |
| 165 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 166 | // means that we always must cache transformed nodes. |
| 167 | DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op); |
| 168 | if (I != LegalizedNodes.end()) return I->second; |
| 169 | |
| 170 | SDNode* Node = Op.getNode(); |
| 171 | |
| 172 | // Legalize the operands |
| 173 | SmallVector<SDValue, 8> Ops; |
| 174 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 175 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 176 | |
Craig Topper | 8c0b4d0 | 2014-04-28 05:57:50 +0000 | [diff] [blame] | 177 | SDValue Result = SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops), 0); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 178 | |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 179 | if (Op.getOpcode() == ISD::LOAD) { |
| 180 | LoadSDNode *LD = cast<LoadSDNode>(Op.getNode()); |
| 181 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 182 | if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD) { |
| 183 | if (TLI.isLoadExtLegal(LD->getExtensionType(), LD->getMemoryVT())) |
| 184 | return TranslateLegalizeResults(Op, Result); |
| 185 | Changed = true; |
| 186 | return LegalizeOp(ExpandLoad(Op)); |
| 187 | } |
| 188 | } else if (Op.getOpcode() == ISD::STORE) { |
| 189 | StoreSDNode *ST = cast<StoreSDNode>(Op.getNode()); |
| 190 | EVT StVT = ST->getMemoryVT(); |
Patrik Hagglund | d7cdcf8 | 2012-12-19 08:28:51 +0000 | [diff] [blame] | 191 | MVT ValVT = ST->getValue().getSimpleValueType(); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 192 | if (StVT.isVector() && ST->isTruncatingStore()) |
Patrik Hagglund | d7cdcf8 | 2012-12-19 08:28:51 +0000 | [diff] [blame] | 193 | switch (TLI.getTruncStoreAction(ValVT, StVT.getSimpleVT())) { |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 194 | default: llvm_unreachable("This action is not supported yet!"); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 195 | case TargetLowering::Legal: |
| 196 | return TranslateLegalizeResults(Op, Result); |
| 197 | case TargetLowering::Custom: |
| 198 | Changed = true; |
Tom Stellard | 1b2c2d8 | 2013-08-21 22:42:58 +0000 | [diff] [blame] | 199 | return TranslateLegalizeResults(Op, TLI.LowerOperation(Result, DAG)); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 200 | case TargetLowering::Expand: |
| 201 | Changed = true; |
| 202 | return LegalizeOp(ExpandStore(Op)); |
| 203 | } |
| 204 | } |
| 205 | |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 206 | bool HasVectorValue = false; |
| 207 | for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end(); |
| 208 | J != E; |
| 209 | ++J) |
| 210 | HasVectorValue |= J->isVector(); |
| 211 | if (!HasVectorValue) |
| 212 | return TranslateLegalizeResults(Op, Result); |
| 213 | |
Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 214 | EVT QueryType; |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 215 | switch (Op.getOpcode()) { |
| 216 | default: |
| 217 | return TranslateLegalizeResults(Op, Result); |
| 218 | case ISD::ADD: |
| 219 | case ISD::SUB: |
| 220 | case ISD::MUL: |
| 221 | case ISD::SDIV: |
| 222 | case ISD::UDIV: |
| 223 | case ISD::SREM: |
| 224 | case ISD::UREM: |
| 225 | case ISD::FADD: |
| 226 | case ISD::FSUB: |
| 227 | case ISD::FMUL: |
| 228 | case ISD::FDIV: |
| 229 | case ISD::FREM: |
| 230 | case ISD::AND: |
| 231 | case ISD::OR: |
| 232 | case ISD::XOR: |
| 233 | case ISD::SHL: |
| 234 | case ISD::SRA: |
| 235 | case ISD::SRL: |
| 236 | case ISD::ROTL: |
| 237 | case ISD::ROTR: |
Hal Finkel | 5c968d9 | 2014-02-03 17:27:25 +0000 | [diff] [blame] | 238 | case ISD::BSWAP: |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 239 | case ISD::CTLZ: |
Chandler Carruth | 637cc6a | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 240 | case ISD::CTTZ: |
| 241 | case ISD::CTLZ_ZERO_UNDEF: |
| 242 | case ISD::CTTZ_ZERO_UNDEF: |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 243 | case ISD::CTPOP: |
| 244 | case ISD::SELECT: |
Nadav Rotem | 52202fb | 2011-09-13 19:17:42 +0000 | [diff] [blame] | 245 | case ISD::VSELECT: |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 246 | case ISD::SELECT_CC: |
Duncan Sands | f2641e1 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 247 | case ISD::SETCC: |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 248 | case ISD::ZERO_EXTEND: |
| 249 | case ISD::ANY_EXTEND: |
| 250 | case ISD::TRUNCATE: |
| 251 | case ISD::SIGN_EXTEND: |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 252 | case ISD::FP_TO_SINT: |
| 253 | case ISD::FP_TO_UINT: |
| 254 | case ISD::FNEG: |
| 255 | case ISD::FABS: |
Hal Finkel | 0c5c01aa | 2013-08-19 23:35:46 +0000 | [diff] [blame] | 256 | case ISD::FCOPYSIGN: |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 257 | case ISD::FSQRT: |
| 258 | case ISD::FSIN: |
| 259 | case ISD::FCOS: |
| 260 | case ISD::FPOWI: |
| 261 | case ISD::FPOW: |
| 262 | case ISD::FLOG: |
| 263 | case ISD::FLOG2: |
| 264 | case ISD::FLOG10: |
| 265 | case ISD::FEXP: |
| 266 | case ISD::FEXP2: |
| 267 | case ISD::FCEIL: |
| 268 | case ISD::FTRUNC: |
| 269 | case ISD::FRINT: |
| 270 | case ISD::FNEARBYINT: |
Hal Finkel | 171817e | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 271 | case ISD::FROUND: |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 272 | case ISD::FFLOOR: |
Eli Friedman | e6385e6 | 2012-11-15 22:44:27 +0000 | [diff] [blame] | 273 | case ISD::FP_ROUND: |
Eli Friedman | 3083494 | 2012-11-17 01:52:46 +0000 | [diff] [blame] | 274 | case ISD::FP_EXTEND: |
Craig Topper | 2da13f9 | 2012-08-30 07:34:22 +0000 | [diff] [blame] | 275 | case ISD::FMA: |
Nadav Rotem | 771f296 | 2011-07-14 11:11:14 +0000 | [diff] [blame] | 276 | case ISD::SIGN_EXTEND_INREG: |
Eli Friedman | aea9b65 | 2009-06-06 03:27:50 +0000 | [diff] [blame] | 277 | QueryType = Node->getValueType(0); |
| 278 | break; |
Dan Gohman | 6bd3ef8 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 279 | case ISD::FP_ROUND_INREG: |
| 280 | QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
| 281 | break; |
Eli Friedman | aea9b65 | 2009-06-06 03:27:50 +0000 | [diff] [blame] | 282 | case ISD::SINT_TO_FP: |
| 283 | case ISD::UINT_TO_FP: |
| 284 | QueryType = Node->getOperand(0).getValueType(); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | |
Eli Friedman | aea9b65 | 2009-06-06 03:27:50 +0000 | [diff] [blame] | 288 | switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) { |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 289 | case TargetLowering::Promote: |
Chandler Carruth | 2746c28 | 2014-07-02 03:07:15 +0000 | [diff] [blame] | 290 | Result = Promote(Op); |
| 291 | Changed = true; |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 292 | break; |
Chandler Carruth | 2746c28 | 2014-07-02 03:07:15 +0000 | [diff] [blame] | 293 | case TargetLowering::Legal: |
| 294 | break; |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 295 | case TargetLowering::Custom: { |
| 296 | SDValue Tmp1 = TLI.LowerOperation(Op, DAG); |
| 297 | if (Tmp1.getNode()) { |
| 298 | Result = Tmp1; |
| 299 | break; |
| 300 | } |
| 301 | // FALL THROUGH |
| 302 | } |
| 303 | case TargetLowering::Expand: |
Chandler Carruth | c1bedac | 2014-07-02 06:23:34 +0000 | [diff] [blame] | 304 | Result = Expand(Op); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // Make sure that the generated code is itself legal. |
| 308 | if (Result != Op) { |
| 309 | Result = LegalizeOp(Result); |
| 310 | Changed = true; |
| 311 | } |
| 312 | |
| 313 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 314 | // means that we always must cache transformed nodes. |
| 315 | AddLegalizedOperand(Op, Result); |
| 316 | return Result; |
| 317 | } |
| 318 | |
Chandler Carruth | 1cfa895 | 2014-07-02 03:07:11 +0000 | [diff] [blame] | 319 | SDValue VectorLegalizer::Promote(SDValue Op) { |
Chandler Carruth | 2746c28 | 2014-07-02 03:07:15 +0000 | [diff] [blame] | 320 | // For a few operations there is a specific concept for promotion based on |
| 321 | // the operand's type. |
| 322 | switch (Op.getOpcode()) { |
| 323 | case ISD::SINT_TO_FP: |
| 324 | case ISD::UINT_TO_FP: |
| 325 | // "Promote" the operation by extending the operand. |
| 326 | return PromoteINT_TO_FP(Op); |
Chandler Carruth | 2746c28 | 2014-07-02 03:07:15 +0000 | [diff] [blame] | 327 | case ISD::FP_TO_UINT: |
| 328 | case ISD::FP_TO_SINT: |
| 329 | // Promote the operation by extending the operand. |
| 330 | return PromoteFP_TO_INT(Op, Op->getOpcode() == ISD::FP_TO_SINT); |
Chandler Carruth | 2746c28 | 2014-07-02 03:07:15 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // The rest of the time, vector "promotion" is basically just bitcasting and |
| 334 | // doing the operation in a different type. For example, x86 promotes |
| 335 | // ISD::AND on v2i32 to v1i64. |
Patrik Hagglund | fd41b5b | 2012-12-19 11:21:04 +0000 | [diff] [blame] | 336 | MVT VT = Op.getSimpleValueType(); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 337 | assert(Op.getNode()->getNumValues() == 1 && |
| 338 | "Can't promote a vector with multiple results!"); |
Patrik Hagglund | fd41b5b | 2012-12-19 11:21:04 +0000 | [diff] [blame] | 339 | MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT); |
Benjamin Kramer | 351d53c | 2013-05-28 16:31:26 +0000 | [diff] [blame] | 340 | SDLoc dl(Op); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 341 | SmallVector<SDValue, 4> Operands(Op.getNumOperands()); |
| 342 | |
| 343 | for (unsigned j = 0; j != Op.getNumOperands(); ++j) { |
| 344 | if (Op.getOperand(j).getValueType().isVector()) |
Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 345 | Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j)); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 346 | else |
| 347 | Operands[j] = Op.getOperand(j); |
| 348 | } |
| 349 | |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 350 | Op = DAG.getNode(Op.getOpcode(), dl, NVT, Operands); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 351 | |
Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 352 | return DAG.getNode(ISD::BITCAST, dl, VT, Op); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Chandler Carruth | 1cfa895 | 2014-07-02 03:07:11 +0000 | [diff] [blame] | 355 | SDValue VectorLegalizer::PromoteINT_TO_FP(SDValue Op) { |
Jim Grosbach | e0c10d8 | 2012-06-28 21:03:44 +0000 | [diff] [blame] | 356 | // INT_TO_FP operations may require the input operand be promoted even |
| 357 | // when the type is otherwise legal. |
| 358 | EVT VT = Op.getOperand(0).getValueType(); |
| 359 | assert(Op.getNode()->getNumValues() == 1 && |
| 360 | "Can't promote a vector with multiple results!"); |
| 361 | |
| 362 | // Normal getTypeToPromoteTo() doesn't work here, as that will promote |
| 363 | // by widening the vector w/ the same element width and twice the number |
| 364 | // of elements. We want the other way around, the same number of elements, |
| 365 | // each twice the width. |
| 366 | // |
| 367 | // Increase the bitwidth of the element to the next pow-of-two |
| 368 | // (which is greater than 8 bits). |
Jim Grosbach | e0c10d8 | 2012-06-28 21:03:44 +0000 | [diff] [blame] | 369 | |
Adam Nemet | 24381f1 | 2014-03-17 17:06:14 +0000 | [diff] [blame] | 370 | EVT NVT = VT.widenIntegerVectorElementType(*DAG.getContext()); |
| 371 | assert(NVT.isSimple() && "Promoting to a non-simple vector type!"); |
Benjamin Kramer | 351d53c | 2013-05-28 16:31:26 +0000 | [diff] [blame] | 372 | SDLoc dl(Op); |
Jim Grosbach | e0c10d8 | 2012-06-28 21:03:44 +0000 | [diff] [blame] | 373 | SmallVector<SDValue, 4> Operands(Op.getNumOperands()); |
| 374 | |
| 375 | unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND : |
| 376 | ISD::SIGN_EXTEND; |
| 377 | for (unsigned j = 0; j != Op.getNumOperands(); ++j) { |
| 378 | if (Op.getOperand(j).getValueType().isVector()) |
| 379 | Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j)); |
| 380 | else |
| 381 | Operands[j] = Op.getOperand(j); |
| 382 | } |
| 383 | |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 384 | return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), Operands); |
Jim Grosbach | e0c10d8 | 2012-06-28 21:03:44 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Adam Nemet | 24381f1 | 2014-03-17 17:06:14 +0000 | [diff] [blame] | 387 | // For FP_TO_INT we promote the result type to a vector type with wider |
| 388 | // elements and then truncate the result. This is different from the default |
| 389 | // PromoteVector which uses bitcast to promote thus assumning that the |
| 390 | // promoted vector type has the same overall size. |
Chandler Carruth | 1cfa895 | 2014-07-02 03:07:11 +0000 | [diff] [blame] | 391 | SDValue VectorLegalizer::PromoteFP_TO_INT(SDValue Op, bool isSigned) { |
Adam Nemet | 24381f1 | 2014-03-17 17:06:14 +0000 | [diff] [blame] | 392 | assert(Op.getNode()->getNumValues() == 1 && |
| 393 | "Can't promote a vector with multiple results!"); |
| 394 | EVT VT = Op.getValueType(); |
| 395 | |
| 396 | EVT NewVT; |
| 397 | unsigned NewOpc; |
| 398 | while (1) { |
| 399 | NewVT = VT.widenIntegerVectorElementType(*DAG.getContext()); |
| 400 | assert(NewVT.isSimple() && "Promoting to a non-simple vector type!"); |
| 401 | if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewVT)) { |
| 402 | NewOpc = ISD::FP_TO_SINT; |
| 403 | break; |
| 404 | } |
| 405 | if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewVT)) { |
| 406 | NewOpc = ISD::FP_TO_UINT; |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | SDLoc loc(Op); |
| 412 | SDValue promoted = DAG.getNode(NewOpc, SDLoc(Op), NewVT, Op.getOperand(0)); |
| 413 | return DAG.getNode(ISD::TRUNCATE, SDLoc(Op), VT, promoted); |
| 414 | } |
| 415 | |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 416 | |
| 417 | SDValue VectorLegalizer::ExpandLoad(SDValue Op) { |
Benjamin Kramer | 351d53c | 2013-05-28 16:31:26 +0000 | [diff] [blame] | 418 | SDLoc dl(Op); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 419 | LoadSDNode *LD = cast<LoadSDNode>(Op.getNode()); |
| 420 | SDValue Chain = LD->getChain(); |
| 421 | SDValue BasePTR = LD->getBasePtr(); |
| 422 | EVT SrcVT = LD->getMemoryVT(); |
Nadav Rotem | 75c2229 | 2011-10-18 22:32:43 +0000 | [diff] [blame] | 423 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 424 | |
Michael Liao | 7fb3966 | 2013-02-20 18:04:21 +0000 | [diff] [blame] | 425 | SmallVector<SDValue, 8> Vals; |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 426 | SmallVector<SDValue, 8> LoadChains; |
| 427 | unsigned NumElem = SrcVT.getVectorNumElements(); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 428 | |
Michael Liao | 7fb3966 | 2013-02-20 18:04:21 +0000 | [diff] [blame] | 429 | EVT SrcEltVT = SrcVT.getScalarType(); |
| 430 | EVT DstEltVT = Op.getNode()->getValueType(0).getScalarType(); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 431 | |
Michael Liao | 7fb3966 | 2013-02-20 18:04:21 +0000 | [diff] [blame] | 432 | if (SrcVT.getVectorNumElements() > 1 && !SrcEltVT.isByteSized()) { |
| 433 | // When elements in a vector is not byte-addressable, we cannot directly |
| 434 | // load each element by advancing pointer, which could only address bytes. |
| 435 | // Instead, we load all significant words, mask bits off, and concatenate |
| 436 | // them to form each element. Finally, they are extended to destination |
| 437 | // scalar type to build the destination vector. |
| 438 | EVT WideVT = TLI.getPointerTy(); |
Nadav Rotem | 75c2229 | 2011-10-18 22:32:43 +0000 | [diff] [blame] | 439 | |
Michael Liao | 7fb3966 | 2013-02-20 18:04:21 +0000 | [diff] [blame] | 440 | assert(WideVT.isRound() && |
| 441 | "Could not handle the sophisticated case when the widest integer is" |
| 442 | " not power of 2."); |
| 443 | assert(WideVT.bitsGE(SrcEltVT) && |
| 444 | "Type is not legalized?"); |
| 445 | |
| 446 | unsigned WideBytes = WideVT.getStoreSize(); |
| 447 | unsigned Offset = 0; |
| 448 | unsigned RemainingBytes = SrcVT.getStoreSize(); |
| 449 | SmallVector<SDValue, 8> LoadVals; |
| 450 | |
| 451 | while (RemainingBytes > 0) { |
| 452 | SDValue ScalarLoad; |
| 453 | unsigned LoadBytes = WideBytes; |
| 454 | |
| 455 | if (RemainingBytes >= LoadBytes) { |
| 456 | ScalarLoad = DAG.getLoad(WideVT, dl, Chain, BasePTR, |
| 457 | LD->getPointerInfo().getWithOffset(Offset), |
| 458 | LD->isVolatile(), LD->isNonTemporal(), |
Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 459 | LD->isInvariant(), LD->getAlignment(), |
| 460 | LD->getTBAAInfo()); |
Michael Liao | 7fb3966 | 2013-02-20 18:04:21 +0000 | [diff] [blame] | 461 | } else { |
| 462 | EVT LoadVT = WideVT; |
| 463 | while (RemainingBytes < LoadBytes) { |
| 464 | LoadBytes >>= 1; // Reduce the load size by half. |
| 465 | LoadVT = EVT::getIntegerVT(*DAG.getContext(), LoadBytes << 3); |
| 466 | } |
| 467 | ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, WideVT, Chain, BasePTR, |
| 468 | LD->getPointerInfo().getWithOffset(Offset), |
| 469 | LoadVT, LD->isVolatile(), |
Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 470 | LD->isNonTemporal(), LD->getAlignment(), |
| 471 | LD->getTBAAInfo()); |
Michael Liao | 7fb3966 | 2013-02-20 18:04:21 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | RemainingBytes -= LoadBytes; |
| 475 | Offset += LoadBytes; |
| 476 | BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR, |
Tom Stellard | 838e234 | 2013-08-26 15:06:10 +0000 | [diff] [blame] | 477 | DAG.getConstant(LoadBytes, BasePTR.getValueType())); |
Michael Liao | 7fb3966 | 2013-02-20 18:04:21 +0000 | [diff] [blame] | 478 | |
| 479 | LoadVals.push_back(ScalarLoad.getValue(0)); |
| 480 | LoadChains.push_back(ScalarLoad.getValue(1)); |
| 481 | } |
| 482 | |
| 483 | // Extract bits, pack and extend/trunc them into destination type. |
| 484 | unsigned SrcEltBits = SrcEltVT.getSizeInBits(); |
| 485 | SDValue SrcEltBitMask = DAG.getConstant((1U << SrcEltBits) - 1, WideVT); |
| 486 | |
| 487 | unsigned BitOffset = 0; |
| 488 | unsigned WideIdx = 0; |
| 489 | unsigned WideBits = WideVT.getSizeInBits(); |
| 490 | |
| 491 | for (unsigned Idx = 0; Idx != NumElem; ++Idx) { |
| 492 | SDValue Lo, Hi, ShAmt; |
| 493 | |
| 494 | if (BitOffset < WideBits) { |
| 495 | ShAmt = DAG.getConstant(BitOffset, TLI.getShiftAmountTy(WideVT)); |
| 496 | Lo = DAG.getNode(ISD::SRL, dl, WideVT, LoadVals[WideIdx], ShAmt); |
| 497 | Lo = DAG.getNode(ISD::AND, dl, WideVT, Lo, SrcEltBitMask); |
| 498 | } |
| 499 | |
| 500 | BitOffset += SrcEltBits; |
| 501 | if (BitOffset >= WideBits) { |
| 502 | WideIdx++; |
| 503 | Offset -= WideBits; |
| 504 | if (Offset > 0) { |
| 505 | ShAmt = DAG.getConstant(SrcEltBits - Offset, |
| 506 | TLI.getShiftAmountTy(WideVT)); |
| 507 | Hi = DAG.getNode(ISD::SHL, dl, WideVT, LoadVals[WideIdx], ShAmt); |
| 508 | Hi = DAG.getNode(ISD::AND, dl, WideVT, Hi, SrcEltBitMask); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | if (Hi.getNode()) |
| 513 | Lo = DAG.getNode(ISD::OR, dl, WideVT, Lo, Hi); |
| 514 | |
| 515 | switch (ExtType) { |
| 516 | default: llvm_unreachable("Unknown extended-load op!"); |
| 517 | case ISD::EXTLOAD: |
| 518 | Lo = DAG.getAnyExtOrTrunc(Lo, dl, DstEltVT); |
| 519 | break; |
| 520 | case ISD::ZEXTLOAD: |
| 521 | Lo = DAG.getZExtOrTrunc(Lo, dl, DstEltVT); |
| 522 | break; |
| 523 | case ISD::SEXTLOAD: |
| 524 | ShAmt = DAG.getConstant(WideBits - SrcEltBits, |
| 525 | TLI.getShiftAmountTy(WideVT)); |
| 526 | Lo = DAG.getNode(ISD::SHL, dl, WideVT, Lo, ShAmt); |
| 527 | Lo = DAG.getNode(ISD::SRA, dl, WideVT, Lo, ShAmt); |
| 528 | Lo = DAG.getSExtOrTrunc(Lo, dl, DstEltVT); |
| 529 | break; |
| 530 | } |
| 531 | Vals.push_back(Lo); |
| 532 | } |
| 533 | } else { |
| 534 | unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8; |
| 535 | |
| 536 | for (unsigned Idx=0; Idx<NumElem; Idx++) { |
| 537 | SDValue ScalarLoad = DAG.getExtLoad(ExtType, dl, |
| 538 | Op.getNode()->getValueType(0).getScalarType(), |
| 539 | Chain, BasePTR, LD->getPointerInfo().getWithOffset(Idx * Stride), |
| 540 | SrcVT.getScalarType(), |
| 541 | LD->isVolatile(), LD->isNonTemporal(), |
Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 542 | LD->getAlignment(), LD->getTBAAInfo()); |
Michael Liao | 7fb3966 | 2013-02-20 18:04:21 +0000 | [diff] [blame] | 543 | |
| 544 | BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR, |
Tom Stellard | 838e234 | 2013-08-26 15:06:10 +0000 | [diff] [blame] | 545 | DAG.getConstant(Stride, BasePTR.getValueType())); |
Michael Liao | 7fb3966 | 2013-02-20 18:04:21 +0000 | [diff] [blame] | 546 | |
| 547 | Vals.push_back(ScalarLoad.getValue(0)); |
| 548 | LoadChains.push_back(ScalarLoad.getValue(1)); |
| 549 | } |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 550 | } |
Nadav Rotem | 75c2229 | 2011-10-18 22:32:43 +0000 | [diff] [blame] | 551 | |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 552 | SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 553 | SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl, |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 554 | Op.getNode()->getValueType(0), Vals); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 555 | |
| 556 | AddLegalizedOperand(Op.getValue(0), Value); |
| 557 | AddLegalizedOperand(Op.getValue(1), NewChain); |
| 558 | |
| 559 | return (Op.getResNo() ? NewChain : Value); |
| 560 | } |
| 561 | |
| 562 | SDValue VectorLegalizer::ExpandStore(SDValue Op) { |
Benjamin Kramer | 351d53c | 2013-05-28 16:31:26 +0000 | [diff] [blame] | 563 | SDLoc dl(Op); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 564 | StoreSDNode *ST = cast<StoreSDNode>(Op.getNode()); |
| 565 | SDValue Chain = ST->getChain(); |
| 566 | SDValue BasePTR = ST->getBasePtr(); |
| 567 | SDValue Value = ST->getValue(); |
| 568 | EVT StVT = ST->getMemoryVT(); |
| 569 | |
| 570 | unsigned Alignment = ST->getAlignment(); |
| 571 | bool isVolatile = ST->isVolatile(); |
| 572 | bool isNonTemporal = ST->isNonTemporal(); |
Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 573 | const MDNode *TBAAInfo = ST->getTBAAInfo(); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 574 | |
| 575 | unsigned NumElem = StVT.getVectorNumElements(); |
| 576 | // The type of the data we want to save |
| 577 | EVT RegVT = Value.getValueType(); |
| 578 | EVT RegSclVT = RegVT.getScalarType(); |
| 579 | // The type of data as saved in memory. |
| 580 | EVT MemSclVT = StVT.getScalarType(); |
| 581 | |
| 582 | // Cast floats into integers |
| 583 | unsigned ScalarSize = MemSclVT.getSizeInBits(); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 584 | |
| 585 | // Round odd types to the next pow of two. |
| 586 | if (!isPowerOf2_32(ScalarSize)) |
| 587 | ScalarSize = NextPowerOf2(ScalarSize); |
| 588 | |
| 589 | // Store Stride in bytes |
| 590 | unsigned Stride = ScalarSize/8; |
| 591 | // Extract each of the elements from the original vector |
| 592 | // and save them into memory individually. |
| 593 | SmallVector<SDValue, 8> Stores; |
| 594 | for (unsigned Idx = 0; Idx < NumElem; Idx++) { |
| 595 | SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Tom Stellard | d42c594 | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 596 | RegSclVT, Value, DAG.getConstant(Idx, TLI.getVectorIdxTy())); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 597 | |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 598 | // This scalar TruncStore may be illegal, but we legalize it later. |
| 599 | SDValue Store = DAG.getTruncStore(Chain, dl, Ex, BasePTR, |
| 600 | ST->getPointerInfo().getWithOffset(Idx*Stride), MemSclVT, |
Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 601 | isVolatile, isNonTemporal, Alignment, TBAAInfo); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 602 | |
Nadav Rotem | 75c2229 | 2011-10-18 22:32:43 +0000 | [diff] [blame] | 603 | BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR, |
Tom Stellard | 838e234 | 2013-08-26 15:06:10 +0000 | [diff] [blame] | 604 | DAG.getConstant(Stride, BasePTR.getValueType())); |
Nadav Rotem | 75c2229 | 2011-10-18 22:32:43 +0000 | [diff] [blame] | 605 | |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 606 | Stores.push_back(Store); |
| 607 | } |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 608 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); |
Nadav Rotem | ebe13bc | 2011-10-15 07:41:10 +0000 | [diff] [blame] | 609 | AddLegalizedOperand(Op, TF); |
| 610 | return TF; |
| 611 | } |
| 612 | |
Chandler Carruth | c1bedac | 2014-07-02 06:23:34 +0000 | [diff] [blame] | 613 | SDValue VectorLegalizer::Expand(SDValue Op) { |
| 614 | switch (Op->getOpcode()) { |
| 615 | case ISD::SIGN_EXTEND_INREG: |
| 616 | return ExpandSEXTINREG(Op); |
| 617 | case ISD::BSWAP: |
| 618 | return ExpandBSWAP(Op); |
| 619 | case ISD::VSELECT: |
| 620 | return ExpandVSELECT(Op); |
| 621 | case ISD::SELECT: |
| 622 | return ExpandSELECT(Op); |
| 623 | case ISD::UINT_TO_FP: |
| 624 | return ExpandUINT_TO_FLOAT(Op); |
| 625 | case ISD::FNEG: |
| 626 | return ExpandFNEG(Op); |
| 627 | case ISD::SETCC: |
| 628 | return UnrollVSETCC(Op); |
| 629 | default: |
| 630 | return DAG.UnrollVectorOp(Op.getNode()); |
| 631 | } |
| 632 | } |
| 633 | |
Nadav Rotem | ea973bd | 2012-08-30 19:17:29 +0000 | [diff] [blame] | 634 | SDValue VectorLegalizer::ExpandSELECT(SDValue Op) { |
| 635 | // Lower a select instruction where the condition is a scalar and the |
| 636 | // operands are vectors. Lower this select to VSELECT and implement it |
Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 637 | // using XOR AND OR. The selector bit is broadcasted. |
Nadav Rotem | ea973bd | 2012-08-30 19:17:29 +0000 | [diff] [blame] | 638 | EVT VT = Op.getValueType(); |
Benjamin Kramer | 351d53c | 2013-05-28 16:31:26 +0000 | [diff] [blame] | 639 | SDLoc DL(Op); |
Nadav Rotem | ea973bd | 2012-08-30 19:17:29 +0000 | [diff] [blame] | 640 | |
| 641 | SDValue Mask = Op.getOperand(0); |
| 642 | SDValue Op1 = Op.getOperand(1); |
| 643 | SDValue Op2 = Op.getOperand(2); |
| 644 | |
| 645 | assert(VT.isVector() && !Mask.getValueType().isVector() |
| 646 | && Op1.getValueType() == Op2.getValueType() && "Invalid type"); |
| 647 | |
| 648 | unsigned NumElem = VT.getVectorNumElements(); |
| 649 | |
| 650 | // If we can't even use the basic vector operations of |
| 651 | // AND,OR,XOR, we will have to scalarize the op. |
| 652 | // Notice that the operation may be 'promoted' which means that it is |
| 653 | // 'bitcasted' to another type which is handled. |
| 654 | // Also, we need to be able to construct a splat vector using BUILD_VECTOR. |
| 655 | if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand || |
| 656 | TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand || |
| 657 | TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand || |
| 658 | TLI.getOperationAction(ISD::BUILD_VECTOR, VT) == TargetLowering::Expand) |
| 659 | return DAG.UnrollVectorOp(Op.getNode()); |
| 660 | |
| 661 | // Generate a mask operand. |
Matt Arsenault | d232222 | 2013-09-10 00:41:56 +0000 | [diff] [blame] | 662 | EVT MaskTy = VT.changeVectorElementTypeToInteger(); |
Nadav Rotem | ea973bd | 2012-08-30 19:17:29 +0000 | [diff] [blame] | 663 | |
| 664 | // What is the size of each element in the vector mask. |
| 665 | EVT BitTy = MaskTy.getScalarType(); |
| 666 | |
Matt Arsenault | d2f0332 | 2013-06-14 22:04:37 +0000 | [diff] [blame] | 667 | Mask = DAG.getSelect(DL, BitTy, Mask, |
Nadav Rotem | 500d691 | 2012-09-02 08:20:07 +0000 | [diff] [blame] | 668 | DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), BitTy), |
Nadav Rotem | 10f6b88 | 2012-09-02 12:21:50 +0000 | [diff] [blame] | 669 | DAG.getConstant(0, BitTy)); |
Nadav Rotem | ea973bd | 2012-08-30 19:17:29 +0000 | [diff] [blame] | 670 | |
| 671 | // Broadcast the mask so that the entire vector is all-one or all zero. |
| 672 | SmallVector<SDValue, 8> Ops(NumElem, Mask); |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 673 | Mask = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskTy, Ops); |
Nadav Rotem | ea973bd | 2012-08-30 19:17:29 +0000 | [diff] [blame] | 674 | |
| 675 | // Bitcast the operands to be the same type as the mask. |
| 676 | // This is needed when we select between FP types because |
| 677 | // the mask is a vector of integers. |
| 678 | Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1); |
| 679 | Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2); |
| 680 | |
| 681 | SDValue AllOnes = DAG.getConstant( |
| 682 | APInt::getAllOnesValue(BitTy.getSizeInBits()), MaskTy); |
| 683 | SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes); |
| 684 | |
| 685 | Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask); |
| 686 | Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask); |
| 687 | SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2); |
| 688 | return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val); |
| 689 | } |
| 690 | |
Nadav Rotem | dbe5c72 | 2013-01-11 22:57:48 +0000 | [diff] [blame] | 691 | SDValue VectorLegalizer::ExpandSEXTINREG(SDValue Op) { |
| 692 | EVT VT = Op.getValueType(); |
| 693 | |
Benjamin Kramer | 5ea0349 | 2013-01-12 19:06:44 +0000 | [diff] [blame] | 694 | // Make sure that the SRA and SHL instructions are available. |
Nadav Rotem | dbe5c72 | 2013-01-11 22:57:48 +0000 | [diff] [blame] | 695 | if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand || |
Benjamin Kramer | 5ea0349 | 2013-01-12 19:06:44 +0000 | [diff] [blame] | 696 | TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand) |
Nadav Rotem | dbe5c72 | 2013-01-11 22:57:48 +0000 | [diff] [blame] | 697 | return DAG.UnrollVectorOp(Op.getNode()); |
| 698 | |
Benjamin Kramer | 351d53c | 2013-05-28 16:31:26 +0000 | [diff] [blame] | 699 | SDLoc DL(Op); |
Nadav Rotem | dbe5c72 | 2013-01-11 22:57:48 +0000 | [diff] [blame] | 700 | EVT OrigTy = cast<VTSDNode>(Op->getOperand(1))->getVT(); |
| 701 | |
| 702 | unsigned BW = VT.getScalarType().getSizeInBits(); |
| 703 | unsigned OrigBW = OrigTy.getScalarType().getSizeInBits(); |
| 704 | SDValue ShiftSz = DAG.getConstant(BW - OrigBW, VT); |
| 705 | |
| 706 | Op = Op.getOperand(0); |
Benjamin Kramer | 5ea0349 | 2013-01-12 19:06:44 +0000 | [diff] [blame] | 707 | Op = DAG.getNode(ISD::SHL, DL, VT, Op, ShiftSz); |
Nadav Rotem | dbe5c72 | 2013-01-11 22:57:48 +0000 | [diff] [blame] | 708 | return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz); |
| 709 | } |
| 710 | |
Benjamin Kramer | f3ad235 | 2014-05-19 13:12:38 +0000 | [diff] [blame] | 711 | SDValue VectorLegalizer::ExpandBSWAP(SDValue Op) { |
| 712 | EVT VT = Op.getValueType(); |
| 713 | |
| 714 | // Generate a byte wise shuffle mask for the BSWAP. |
| 715 | SmallVector<int, 16> ShuffleMask; |
| 716 | int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8; |
| 717 | for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) |
| 718 | for (int J = ScalarSizeInBytes - 1; J >= 0; --J) |
| 719 | ShuffleMask.push_back((I * ScalarSizeInBytes) + J); |
| 720 | |
| 721 | EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size()); |
| 722 | |
| 723 | // Only emit a shuffle if the mask is legal. |
| 724 | if (!TLI.isShuffleMaskLegal(ShuffleMask, ByteVT)) |
| 725 | return DAG.UnrollVectorOp(Op.getNode()); |
| 726 | |
| 727 | SDLoc DL(Op); |
| 728 | Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Op.getOperand(0)); |
| 729 | Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), |
| 730 | ShuffleMask.data()); |
| 731 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 732 | } |
| 733 | |
Nadav Rotem | 52202fb | 2011-09-13 19:17:42 +0000 | [diff] [blame] | 734 | SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) { |
| 735 | // Implement VSELECT in terms of XOR, AND, OR |
| 736 | // on platforms which do not support blend natively. |
Benjamin Kramer | 351d53c | 2013-05-28 16:31:26 +0000 | [diff] [blame] | 737 | SDLoc DL(Op); |
Nadav Rotem | 52202fb | 2011-09-13 19:17:42 +0000 | [diff] [blame] | 738 | |
| 739 | SDValue Mask = Op.getOperand(0); |
| 740 | SDValue Op1 = Op.getOperand(1); |
| 741 | SDValue Op2 = Op.getOperand(2); |
| 742 | |
Matt Arsenault | a5733dc | 2013-05-07 20:24:18 +0000 | [diff] [blame] | 743 | EVT VT = Mask.getValueType(); |
| 744 | |
Nadav Rotem | 52202fb | 2011-09-13 19:17:42 +0000 | [diff] [blame] | 745 | // If we can't even use the basic vector operations of |
| 746 | // AND,OR,XOR, we will have to scalarize the op. |
Nadav Rotem | 8824472 | 2011-10-19 20:43:16 +0000 | [diff] [blame] | 747 | // Notice that the operation may be 'promoted' which means that it is |
| 748 | // 'bitcasted' to another type which is handled. |
Pete Cooper | 2455e9c | 2012-09-01 22:27:48 +0000 | [diff] [blame] | 749 | // This operation also isn't safe with AND, OR, XOR when the boolean |
| 750 | // type is 0/1 as we need an all ones vector constant to mask with. |
| 751 | // FIXME: Sign extend 1 to all ones if thats legal on the target. |
Nadav Rotem | 8824472 | 2011-10-19 20:43:16 +0000 | [diff] [blame] | 752 | if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand || |
| 753 | TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand || |
Pete Cooper | 2455e9c | 2012-09-01 22:27:48 +0000 | [diff] [blame] | 754 | TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand || |
| 755 | TLI.getBooleanContents(true) != |
| 756 | TargetLowering::ZeroOrNegativeOneBooleanContent) |
Nadav Rotem | 8824472 | 2011-10-19 20:43:16 +0000 | [diff] [blame] | 757 | return DAG.UnrollVectorOp(Op.getNode()); |
Nadav Rotem | 52202fb | 2011-09-13 19:17:42 +0000 | [diff] [blame] | 758 | |
Matt Arsenault | a5733dc | 2013-05-07 20:24:18 +0000 | [diff] [blame] | 759 | // If the mask and the type are different sizes, unroll the vector op. This |
| 760 | // can occur when getSetCCResultType returns something that is different in |
| 761 | // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8. |
| 762 | if (VT.getSizeInBits() != Op1.getValueType().getSizeInBits()) |
| 763 | return DAG.UnrollVectorOp(Op.getNode()); |
| 764 | |
Nadav Rotem | 52202fb | 2011-09-13 19:17:42 +0000 | [diff] [blame] | 765 | // Bitcast the operands to be the same type as the mask. |
| 766 | // This is needed when we select between FP types because |
| 767 | // the mask is a vector of integers. |
| 768 | Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1); |
| 769 | Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2); |
| 770 | |
| 771 | SDValue AllOnes = DAG.getConstant( |
| 772 | APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), VT); |
| 773 | SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes); |
| 774 | |
| 775 | Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask); |
| 776 | Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask); |
Nadav Rotem | 02ef0c3 | 2012-04-15 15:08:09 +0000 | [diff] [blame] | 777 | SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2); |
| 778 | return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val); |
Nadav Rotem | 52202fb | 2011-09-13 19:17:42 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Nadav Rotem | e7a101c | 2011-03-19 13:09:10 +0000 | [diff] [blame] | 781 | SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) { |
Nadav Rotem | e7a101c | 2011-03-19 13:09:10 +0000 | [diff] [blame] | 782 | EVT VT = Op.getOperand(0).getValueType(); |
Benjamin Kramer | 351d53c | 2013-05-28 16:31:26 +0000 | [diff] [blame] | 783 | SDLoc DL(Op); |
Nadav Rotem | e7a101c | 2011-03-19 13:09:10 +0000 | [diff] [blame] | 784 | |
| 785 | // Make sure that the SINT_TO_FP and SRL instructions are available. |
Nadav Rotem | 8824472 | 2011-10-19 20:43:16 +0000 | [diff] [blame] | 786 | if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand || |
| 787 | TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand) |
| 788 | return DAG.UnrollVectorOp(Op.getNode()); |
Nadav Rotem | e7a101c | 2011-03-19 13:09:10 +0000 | [diff] [blame] | 789 | |
| 790 | EVT SVT = VT.getScalarType(); |
| 791 | assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) && |
| 792 | "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide"); |
| 793 | |
| 794 | unsigned BW = SVT.getSizeInBits(); |
| 795 | SDValue HalfWord = DAG.getConstant(BW/2, VT); |
| 796 | |
| 797 | // Constants to clear the upper part of the word. |
| 798 | // Notice that we can also use SHL+SHR, but using a constant is slightly |
| 799 | // faster on x86. |
| 800 | uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF; |
| 801 | SDValue HalfWordMask = DAG.getConstant(HWMask, VT); |
| 802 | |
| 803 | // Two to the power of half-word-size. |
| 804 | SDValue TWOHW = DAG.getConstantFP((1<<(BW/2)), Op.getValueType()); |
| 805 | |
| 806 | // Clear upper part of LO, lower HI |
| 807 | SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord); |
| 808 | SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask); |
| 809 | |
| 810 | // Convert hi and lo to floats |
| 811 | // Convert the hi part back to the upper values |
| 812 | SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI); |
| 813 | fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW); |
| 814 | SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO); |
| 815 | |
| 816 | // Add the two halves |
| 817 | return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO); |
| 818 | } |
| 819 | |
| 820 | |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 821 | SDValue VectorLegalizer::ExpandFNEG(SDValue Op) { |
| 822 | if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) { |
| 823 | SDValue Zero = DAG.getConstantFP(-0.0, Op.getValueType()); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 824 | return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(), |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 825 | Zero, Op.getOperand(0)); |
| 826 | } |
Mon P Wang | 32f8bb9 | 2009-11-30 02:42:02 +0000 | [diff] [blame] | 827 | return DAG.UnrollVectorOp(Op.getNode()); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) { |
Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 831 | EVT VT = Op.getValueType(); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 832 | unsigned NumElems = VT.getVectorNumElements(); |
Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 833 | EVT EltVT = VT.getVectorElementType(); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 834 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2); |
Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 835 | EVT TmpEltVT = LHS.getValueType().getVectorElementType(); |
Benjamin Kramer | 351d53c | 2013-05-28 16:31:26 +0000 | [diff] [blame] | 836 | SDLoc dl(Op); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 837 | SmallVector<SDValue, 8> Ops(NumElems); |
| 838 | for (unsigned i = 0; i < NumElems; ++i) { |
| 839 | SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS, |
Tom Stellard | d42c594 | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 840 | DAG.getConstant(i, TLI.getVectorIdxTy())); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 841 | SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS, |
Tom Stellard | d42c594 | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 842 | DAG.getConstant(i, TLI.getVectorIdxTy())); |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 843 | Ops[i] = DAG.getNode(ISD::SETCC, dl, |
| 844 | TLI.getSetCCResultType(*DAG.getContext(), TmpEltVT), |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 845 | LHSElem, RHSElem, CC); |
Matt Arsenault | d2f0332 | 2013-06-14 22:04:37 +0000 | [diff] [blame] | 846 | Ops[i] = DAG.getSelect(dl, EltVT, Ops[i], |
| 847 | DAG.getConstant(APInt::getAllOnesValue |
| 848 | (EltVT.getSizeInBits()), EltVT), |
| 849 | DAG.getConstant(0, EltVT)); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 850 | } |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 851 | return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops); |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Eli Friedman | da90dd6 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | bool SelectionDAG::LegalizeVectors() { |
| 857 | return VectorLegalizer(*this).Run(); |
| 858 | } |