Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1 | //===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===// |
| 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 defines an instruction selector for the SystemZ target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SystemZTargetMachine.h" |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame^] | 15 | #include "llvm/Analysis/AliasAnalysis.h" |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 17 | #include "llvm/Support/Debug.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | // Used to build addressing modes. |
| 24 | struct SystemZAddressingMode { |
| 25 | // The shape of the address. |
| 26 | enum AddrForm { |
| 27 | // base+displacement |
| 28 | FormBD, |
| 29 | |
| 30 | // base+displacement+index for load and store operands |
| 31 | FormBDXNormal, |
| 32 | |
| 33 | // base+displacement+index for load address operands |
| 34 | FormBDXLA, |
| 35 | |
| 36 | // base+displacement+index+ADJDYNALLOC |
| 37 | FormBDXDynAlloc |
| 38 | }; |
| 39 | AddrForm Form; |
| 40 | |
| 41 | // The type of displacement. The enum names here correspond directly |
| 42 | // to the definitions in SystemZOperand.td. We could split them into |
| 43 | // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it. |
| 44 | enum DispRange { |
| 45 | Disp12Only, |
| 46 | Disp12Pair, |
| 47 | Disp20Only, |
| 48 | Disp20Only128, |
| 49 | Disp20Pair |
| 50 | }; |
| 51 | DispRange DR; |
| 52 | |
| 53 | // The parts of the address. The address is equivalent to: |
| 54 | // |
| 55 | // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0) |
| 56 | SDValue Base; |
| 57 | int64_t Disp; |
| 58 | SDValue Index; |
| 59 | bool IncludesDynAlloc; |
| 60 | |
| 61 | SystemZAddressingMode(AddrForm form, DispRange dr) |
| 62 | : Form(form), DR(dr), Base(), Disp(0), Index(), |
| 63 | IncludesDynAlloc(false) {} |
| 64 | |
| 65 | // True if the address can have an index register. |
| 66 | bool hasIndexField() { return Form != FormBD; } |
| 67 | |
| 68 | // True if the address can (and must) include ADJDYNALLOC. |
| 69 | bool isDynAlloc() { return Form == FormBDXDynAlloc; } |
| 70 | |
| 71 | void dump() { |
| 72 | errs() << "SystemZAddressingMode " << this << '\n'; |
| 73 | |
| 74 | errs() << " Base "; |
| 75 | if (Base.getNode() != 0) |
| 76 | Base.getNode()->dump(); |
| 77 | else |
| 78 | errs() << "null\n"; |
| 79 | |
| 80 | if (hasIndexField()) { |
| 81 | errs() << " Index "; |
| 82 | if (Index.getNode() != 0) |
| 83 | Index.getNode()->dump(); |
| 84 | else |
| 85 | errs() << "null\n"; |
| 86 | } |
| 87 | |
| 88 | errs() << " Disp " << Disp; |
| 89 | if (IncludesDynAlloc) |
| 90 | errs() << " + ADJDYNALLOC"; |
| 91 | errs() << '\n'; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | class SystemZDAGToDAGISel : public SelectionDAGISel { |
| 96 | const SystemZTargetLowering &Lowering; |
| 97 | const SystemZSubtarget &Subtarget; |
| 98 | |
| 99 | // Used by SystemZOperands.td to create integer constants. |
| 100 | inline SDValue getImm(const SDNode *Node, uint64_t Imm) { |
| 101 | return CurDAG->getTargetConstant(Imm, Node->getValueType(0)); |
| 102 | } |
| 103 | |
| 104 | // Try to fold more of the base or index of AM into AM, where IsBase |
| 105 | // selects between the base and index. |
| 106 | bool expandAddress(SystemZAddressingMode &AM, bool IsBase); |
| 107 | |
| 108 | // Try to describe N in AM, returning true on success. |
| 109 | bool selectAddress(SDValue N, SystemZAddressingMode &AM); |
| 110 | |
| 111 | // Extract individual target operands from matched address AM. |
| 112 | void getAddressOperands(const SystemZAddressingMode &AM, EVT VT, |
| 113 | SDValue &Base, SDValue &Disp); |
| 114 | void getAddressOperands(const SystemZAddressingMode &AM, EVT VT, |
| 115 | SDValue &Base, SDValue &Disp, SDValue &Index); |
| 116 | |
| 117 | // Try to match Addr as a FormBD address with displacement type DR. |
| 118 | // Return true on success, storing the base and displacement in |
| 119 | // Base and Disp respectively. |
| 120 | bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr, |
| 121 | SDValue &Base, SDValue &Disp); |
| 122 | |
| 123 | // Try to match Addr as a FormBDX* address of form Form with |
| 124 | // displacement type DR. Return true on success, storing the base, |
| 125 | // displacement and index in Base, Disp and Index respectively. |
| 126 | bool selectBDXAddr(SystemZAddressingMode::AddrForm Form, |
| 127 | SystemZAddressingMode::DispRange DR, SDValue Addr, |
| 128 | SDValue &Base, SDValue &Disp, SDValue &Index); |
| 129 | |
| 130 | // PC-relative address matching routines used by SystemZOperands.td. |
| 131 | bool selectPCRelAddress(SDValue Addr, SDValue &Target) { |
| 132 | if (Addr.getOpcode() == SystemZISD::PCREL_WRAPPER) { |
| 133 | Target = Addr.getOperand(0); |
| 134 | return true; |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | // BD matching routines used by SystemZOperands.td. |
| 140 | bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) { |
| 141 | return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp); |
| 142 | } |
| 143 | bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) { |
| 144 | return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp); |
| 145 | } |
| 146 | bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) { |
| 147 | return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp); |
| 148 | } |
| 149 | bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) { |
| 150 | return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp); |
| 151 | } |
| 152 | |
| 153 | // BDX matching routines used by SystemZOperands.td. |
| 154 | bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp, |
| 155 | SDValue &Index) { |
| 156 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 157 | SystemZAddressingMode::Disp12Only, |
| 158 | Addr, Base, Disp, Index); |
| 159 | } |
| 160 | bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
| 161 | SDValue &Index) { |
| 162 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 163 | SystemZAddressingMode::Disp12Pair, |
| 164 | Addr, Base, Disp, Index); |
| 165 | } |
| 166 | bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp, |
| 167 | SDValue &Index) { |
| 168 | return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc, |
| 169 | SystemZAddressingMode::Disp12Only, |
| 170 | Addr, Base, Disp, Index); |
| 171 | } |
| 172 | bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp, |
| 173 | SDValue &Index) { |
| 174 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 175 | SystemZAddressingMode::Disp20Only, |
| 176 | Addr, Base, Disp, Index); |
| 177 | } |
| 178 | bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp, |
| 179 | SDValue &Index) { |
| 180 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 181 | SystemZAddressingMode::Disp20Only128, |
| 182 | Addr, Base, Disp, Index); |
| 183 | } |
| 184 | bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
| 185 | SDValue &Index) { |
| 186 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 187 | SystemZAddressingMode::Disp20Pair, |
| 188 | Addr, Base, Disp, Index); |
| 189 | } |
| 190 | bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
| 191 | SDValue &Index) { |
| 192 | return selectBDXAddr(SystemZAddressingMode::FormBDXLA, |
| 193 | SystemZAddressingMode::Disp12Pair, |
| 194 | Addr, Base, Disp, Index); |
| 195 | } |
| 196 | bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
| 197 | SDValue &Index) { |
| 198 | return selectBDXAddr(SystemZAddressingMode::FormBDXLA, |
| 199 | SystemZAddressingMode::Disp20Pair, |
| 200 | Addr, Base, Disp, Index); |
| 201 | } |
| 202 | |
| 203 | // If Op0 is null, then Node is a constant that can be loaded using: |
| 204 | // |
| 205 | // (Opcode UpperVal LowerVal) |
| 206 | // |
| 207 | // If Op0 is nonnull, then Node can be implemented using: |
| 208 | // |
| 209 | // (Opcode (Opcode Op0 UpperVal) LowerVal) |
| 210 | SDNode *splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0, |
| 211 | uint64_t UpperVal, uint64_t LowerVal); |
| 212 | |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame^] | 213 | bool storeLoadCanUseMVC(SDNode *N) const; |
| 214 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 215 | public: |
| 216 | SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel) |
| 217 | : SelectionDAGISel(TM, OptLevel), |
| 218 | Lowering(*TM.getTargetLowering()), |
| 219 | Subtarget(*TM.getSubtargetImpl()) { } |
| 220 | |
| 221 | // Override MachineFunctionPass. |
| 222 | virtual const char *getPassName() const LLVM_OVERRIDE { |
| 223 | return "SystemZ DAG->DAG Pattern Instruction Selection"; |
| 224 | } |
| 225 | |
| 226 | // Override SelectionDAGISel. |
| 227 | virtual SDNode *Select(SDNode *Node) LLVM_OVERRIDE; |
| 228 | virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
| 229 | char ConstraintCode, |
| 230 | std::vector<SDValue> &OutOps) |
| 231 | LLVM_OVERRIDE; |
| 232 | |
| 233 | // Include the pieces autogenerated from the target description. |
| 234 | #include "SystemZGenDAGISel.inc" |
| 235 | }; |
| 236 | } // end anonymous namespace |
| 237 | |
| 238 | FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM, |
| 239 | CodeGenOpt::Level OptLevel) { |
| 240 | return new SystemZDAGToDAGISel(TM, OptLevel); |
| 241 | } |
| 242 | |
| 243 | // Return true if Val should be selected as a displacement for an address |
| 244 | // with range DR. Here we're interested in the range of both the instruction |
| 245 | // described by DR and of any pairing instruction. |
| 246 | static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { |
| 247 | switch (DR) { |
| 248 | case SystemZAddressingMode::Disp12Only: |
| 249 | return isUInt<12>(Val); |
| 250 | |
| 251 | case SystemZAddressingMode::Disp12Pair: |
| 252 | case SystemZAddressingMode::Disp20Only: |
| 253 | case SystemZAddressingMode::Disp20Pair: |
| 254 | return isInt<20>(Val); |
| 255 | |
| 256 | case SystemZAddressingMode::Disp20Only128: |
| 257 | return isInt<20>(Val) && isInt<20>(Val + 8); |
| 258 | } |
| 259 | llvm_unreachable("Unhandled displacement range"); |
| 260 | } |
| 261 | |
| 262 | // Change the base or index in AM to Value, where IsBase selects |
| 263 | // between the base and index. |
| 264 | static void changeComponent(SystemZAddressingMode &AM, bool IsBase, |
| 265 | SDValue Value) { |
| 266 | if (IsBase) |
| 267 | AM.Base = Value; |
| 268 | else |
| 269 | AM.Index = Value; |
| 270 | } |
| 271 | |
| 272 | // The base or index of AM is equivalent to Value + ADJDYNALLOC, |
| 273 | // where IsBase selects between the base and index. Try to fold the |
| 274 | // ADJDYNALLOC into AM. |
| 275 | static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase, |
| 276 | SDValue Value) { |
| 277 | if (AM.isDynAlloc() && !AM.IncludesDynAlloc) { |
| 278 | changeComponent(AM, IsBase, Value); |
| 279 | AM.IncludesDynAlloc = true; |
| 280 | return true; |
| 281 | } |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | // The base of AM is equivalent to Base + Index. Try to use Index as |
| 286 | // the index register. |
| 287 | static bool expandIndex(SystemZAddressingMode &AM, SDValue Base, |
| 288 | SDValue Index) { |
| 289 | if (AM.hasIndexField() && !AM.Index.getNode()) { |
| 290 | AM.Base = Base; |
| 291 | AM.Index = Index; |
| 292 | return true; |
| 293 | } |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects |
| 298 | // between the base and index. Try to fold Op1 into AM's displacement. |
| 299 | static bool expandDisp(SystemZAddressingMode &AM, bool IsBase, |
| 300 | SDValue Op0, ConstantSDNode *Op1) { |
| 301 | // First try adjusting the displacement. |
| 302 | int64_t TestDisp = AM.Disp + Op1->getSExtValue(); |
| 303 | if (selectDisp(AM.DR, TestDisp)) { |
| 304 | changeComponent(AM, IsBase, Op0); |
| 305 | AM.Disp = TestDisp; |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | // We could consider forcing the displacement into a register and |
| 310 | // using it as an index, but it would need to be carefully tuned. |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM, |
| 315 | bool IsBase) { |
| 316 | SDValue N = IsBase ? AM.Base : AM.Index; |
| 317 | unsigned Opcode = N.getOpcode(); |
| 318 | if (Opcode == ISD::TRUNCATE) { |
| 319 | N = N.getOperand(0); |
| 320 | Opcode = N.getOpcode(); |
| 321 | } |
| 322 | if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) { |
| 323 | SDValue Op0 = N.getOperand(0); |
| 324 | SDValue Op1 = N.getOperand(1); |
| 325 | |
| 326 | unsigned Op0Code = Op0->getOpcode(); |
| 327 | unsigned Op1Code = Op1->getOpcode(); |
| 328 | |
| 329 | if (Op0Code == SystemZISD::ADJDYNALLOC) |
| 330 | return expandAdjDynAlloc(AM, IsBase, Op1); |
| 331 | if (Op1Code == SystemZISD::ADJDYNALLOC) |
| 332 | return expandAdjDynAlloc(AM, IsBase, Op0); |
| 333 | |
| 334 | if (Op0Code == ISD::Constant) |
| 335 | return expandDisp(AM, IsBase, Op1, cast<ConstantSDNode>(Op0)); |
| 336 | if (Op1Code == ISD::Constant) |
| 337 | return expandDisp(AM, IsBase, Op0, cast<ConstantSDNode>(Op1)); |
| 338 | |
| 339 | if (IsBase && expandIndex(AM, Op0, Op1)) |
| 340 | return true; |
| 341 | } |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | // Return true if an instruction with displacement range DR should be |
| 346 | // used for displacement value Val. selectDisp(DR, Val) must already hold. |
| 347 | static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { |
| 348 | assert(selectDisp(DR, Val) && "Invalid displacement"); |
| 349 | switch (DR) { |
| 350 | case SystemZAddressingMode::Disp12Only: |
| 351 | case SystemZAddressingMode::Disp20Only: |
| 352 | case SystemZAddressingMode::Disp20Only128: |
| 353 | return true; |
| 354 | |
| 355 | case SystemZAddressingMode::Disp12Pair: |
| 356 | // Use the other instruction if the displacement is too large. |
| 357 | return isUInt<12>(Val); |
| 358 | |
| 359 | case SystemZAddressingMode::Disp20Pair: |
| 360 | // Use the other instruction if the displacement is small enough. |
| 361 | return !isUInt<12>(Val); |
| 362 | } |
| 363 | llvm_unreachable("Unhandled displacement range"); |
| 364 | } |
| 365 | |
| 366 | // Return true if Base + Disp + Index should be performed by LA(Y). |
| 367 | static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) { |
| 368 | // Don't use LA(Y) for constants. |
| 369 | if (!Base) |
| 370 | return false; |
| 371 | |
| 372 | // Always use LA(Y) for frame addresses, since we know that the destination |
| 373 | // register is almost always (perhaps always) going to be different from |
| 374 | // the frame register. |
| 375 | if (Base->getOpcode() == ISD::FrameIndex) |
| 376 | return true; |
| 377 | |
| 378 | if (Disp) { |
| 379 | // Always use LA(Y) if there is a base, displacement and index. |
| 380 | if (Index) |
| 381 | return true; |
| 382 | |
| 383 | // Always use LA if the displacement is small enough. It should always |
| 384 | // be no worse than AGHI (and better if it avoids a move). |
| 385 | if (isUInt<12>(Disp)) |
| 386 | return true; |
| 387 | |
| 388 | // For similar reasons, always use LAY if the constant is too big for AGHI. |
| 389 | // LAY should be no worse than AGFI. |
| 390 | if (!isInt<16>(Disp)) |
| 391 | return true; |
| 392 | } else { |
| 393 | // Don't use LA for plain registers. |
| 394 | if (!Index) |
| 395 | return false; |
| 396 | |
| 397 | // Don't use LA for plain addition if the index operand is only used |
| 398 | // once. It should be a natural two-operand addition in that case. |
| 399 | if (Index->hasOneUse()) |
| 400 | return false; |
| 401 | |
| 402 | // Prefer addition if the second operation is sign-extended, in the |
| 403 | // hope of using AGF. |
| 404 | unsigned IndexOpcode = Index->getOpcode(); |
| 405 | if (IndexOpcode == ISD::SIGN_EXTEND || |
| 406 | IndexOpcode == ISD::SIGN_EXTEND_INREG) |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | // Don't use LA for two-operand addition if either operand is only |
| 411 | // used once. The addition instructions are better in that case. |
| 412 | if (Base->hasOneUse()) |
| 413 | return false; |
| 414 | |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | // Return true if Addr is suitable for AM, updating AM if so. |
| 419 | bool SystemZDAGToDAGISel::selectAddress(SDValue Addr, |
| 420 | SystemZAddressingMode &AM) { |
| 421 | // Start out assuming that the address will need to be loaded separately, |
| 422 | // then try to extend it as much as we can. |
| 423 | AM.Base = Addr; |
| 424 | |
| 425 | // First try treating the address as a constant. |
| 426 | if (Addr.getOpcode() == ISD::Constant && |
| 427 | expandDisp(AM, true, SDValue(), cast<ConstantSDNode>(Addr))) |
| 428 | ; |
| 429 | else |
| 430 | // Otherwise try expanding each component. |
| 431 | while (expandAddress(AM, true) || |
| 432 | (AM.Index.getNode() && expandAddress(AM, false))) |
| 433 | continue; |
| 434 | |
| 435 | // Reject cases where it isn't profitable to use LA(Y). |
| 436 | if (AM.Form == SystemZAddressingMode::FormBDXLA && |
| 437 | !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode())) |
| 438 | return false; |
| 439 | |
| 440 | // Reject cases where the other instruction in a pair should be used. |
| 441 | if (!isValidDisp(AM.DR, AM.Disp)) |
| 442 | return false; |
| 443 | |
| 444 | // Make sure that ADJDYNALLOC is included where necessary. |
| 445 | if (AM.isDynAlloc() && !AM.IncludesDynAlloc) |
| 446 | return false; |
| 447 | |
| 448 | DEBUG(AM.dump()); |
| 449 | return true; |
| 450 | } |
| 451 | |
| 452 | // Insert a node into the DAG at least before Pos. This will reposition |
| 453 | // the node as needed, and will assign it a node ID that is <= Pos's ID. |
| 454 | // Note that this does *not* preserve the uniqueness of node IDs! |
| 455 | // The selection DAG must no longer depend on their uniqueness when this |
| 456 | // function is used. |
| 457 | static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) { |
| 458 | if (N.getNode()->getNodeId() == -1 || |
| 459 | N.getNode()->getNodeId() > Pos->getNodeId()) { |
| 460 | DAG->RepositionNode(Pos, N.getNode()); |
| 461 | N.getNode()->setNodeId(Pos->getNodeId()); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, |
| 466 | EVT VT, SDValue &Base, |
| 467 | SDValue &Disp) { |
| 468 | Base = AM.Base; |
| 469 | if (!Base.getNode()) |
| 470 | // Register 0 means "no base". This is mostly useful for shifts. |
| 471 | Base = CurDAG->getRegister(0, VT); |
| 472 | else if (Base.getOpcode() == ISD::FrameIndex) { |
| 473 | // Lower a FrameIndex to a TargetFrameIndex. |
| 474 | int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex(); |
| 475 | Base = CurDAG->getTargetFrameIndex(FrameIndex, VT); |
| 476 | } else if (Base.getValueType() != VT) { |
| 477 | // Truncate values from i64 to i32, for shifts. |
| 478 | assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 && |
| 479 | "Unexpected truncation"); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 480 | SDLoc DL(Base); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 481 | SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base); |
| 482 | insertDAGNode(CurDAG, Base.getNode(), Trunc); |
| 483 | Base = Trunc; |
| 484 | } |
| 485 | |
| 486 | // Lower the displacement to a TargetConstant. |
| 487 | Disp = CurDAG->getTargetConstant(AM.Disp, VT); |
| 488 | } |
| 489 | |
| 490 | void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, |
| 491 | EVT VT, SDValue &Base, |
| 492 | SDValue &Disp, SDValue &Index) { |
| 493 | getAddressOperands(AM, VT, Base, Disp); |
| 494 | |
| 495 | Index = AM.Index; |
| 496 | if (!Index.getNode()) |
| 497 | // Register 0 means "no index". |
| 498 | Index = CurDAG->getRegister(0, VT); |
| 499 | } |
| 500 | |
| 501 | bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR, |
| 502 | SDValue Addr, SDValue &Base, |
| 503 | SDValue &Disp) { |
| 504 | SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR); |
| 505 | if (!selectAddress(Addr, AM)) |
| 506 | return false; |
| 507 | |
| 508 | getAddressOperands(AM, Addr.getValueType(), Base, Disp); |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form, |
| 513 | SystemZAddressingMode::DispRange DR, |
| 514 | SDValue Addr, SDValue &Base, |
| 515 | SDValue &Disp, SDValue &Index) { |
| 516 | SystemZAddressingMode AM(Form, DR); |
| 517 | if (!selectAddress(Addr, AM)) |
| 518 | return false; |
| 519 | |
| 520 | getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index); |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | SDNode *SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node, |
| 525 | SDValue Op0, uint64_t UpperVal, |
| 526 | uint64_t LowerVal) { |
| 527 | EVT VT = Node->getValueType(0); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 528 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 529 | SDValue Upper = CurDAG->getConstant(UpperVal, VT); |
| 530 | if (Op0.getNode()) |
| 531 | Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper); |
| 532 | Upper = SDValue(Select(Upper.getNode()), 0); |
| 533 | |
| 534 | SDValue Lower = CurDAG->getConstant(LowerVal, VT); |
| 535 | SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower); |
| 536 | return Or.getNode(); |
| 537 | } |
| 538 | |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame^] | 539 | // N is a (store (load ...), ...) pattern. Return true if it can use MVC. |
| 540 | bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const { |
| 541 | StoreSDNode *Store = cast<StoreSDNode>(N); |
| 542 | LoadSDNode *Load = cast<LoadSDNode>(Store->getValue().getNode()); |
| 543 | |
| 544 | // MVC is logically a bytewise copy, so can't be used for volatile accesses. |
| 545 | if (Load->isVolatile() || Store->isVolatile()) |
| 546 | return false; |
| 547 | |
| 548 | // Prefer not to use MVC if either address can use ... RELATIVE LONG |
| 549 | // instructions. |
| 550 | assert(Load->getMemoryVT() == Store->getMemoryVT() && |
| 551 | "Should already have checked that the types match"); |
| 552 | uint64_t Size = Load->getMemoryVT().getStoreSize(); |
| 553 | if (Size > 1 && Size <= 8) { |
| 554 | // Prefer LHRL, LRL and LGRL. |
| 555 | if (Load->getBasePtr().getOpcode() == SystemZISD::PCREL_WRAPPER) |
| 556 | return false; |
| 557 | // Prefer STHRL, STRL and STGRL. |
| 558 | if (Store->getBasePtr().getOpcode() == SystemZISD::PCREL_WRAPPER) |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | // There's no chance of overlap if the load is invariant. |
| 563 | if (Load->isInvariant()) |
| 564 | return true; |
| 565 | |
| 566 | // If both operands are aligned, they must be equal or not overlap. |
| 567 | if (Load->getAlignment() >= Size && Store->getAlignment() >= Size) |
| 568 | return true; |
| 569 | |
| 570 | // Otherwise we need to check whether there's an alias. |
| 571 | const Value *V1 = Load->getSrcValue(); |
| 572 | const Value *V2 = Store->getSrcValue(); |
| 573 | if (!V1 || !V2) |
| 574 | return false; |
| 575 | |
| 576 | int64_t End1 = Load->getSrcValueOffset() + Size; |
| 577 | int64_t End2 = Store->getSrcValueOffset() + Size; |
| 578 | return !AA->alias(AliasAnalysis::Location(V1, End1, Load->getTBAAInfo()), |
| 579 | AliasAnalysis::Location(V2, End2, Store->getTBAAInfo())); |
| 580 | } |
| 581 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 582 | SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) { |
| 583 | // Dump information about the Node being selected |
| 584 | DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n"); |
| 585 | |
| 586 | // If we have a custom node, we already have selected! |
| 587 | if (Node->isMachineOpcode()) { |
| 588 | DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | unsigned Opcode = Node->getOpcode(); |
| 593 | switch (Opcode) { |
| 594 | case ISD::OR: |
| 595 | case ISD::XOR: |
| 596 | // If this is a 64-bit operation in which both 32-bit halves are nonzero, |
| 597 | // split the operation into two. |
| 598 | if (Node->getValueType(0) == MVT::i64) |
| 599 | if (ConstantSDNode *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) { |
| 600 | uint64_t Val = Op1->getZExtValue(); |
| 601 | if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) |
| 602 | Node = splitLargeImmediate(Opcode, Node, Node->getOperand(0), |
| 603 | Val - uint32_t(Val), uint32_t(Val)); |
| 604 | } |
| 605 | break; |
| 606 | |
| 607 | case ISD::Constant: |
| 608 | // If this is a 64-bit constant that is out of the range of LLILF, |
| 609 | // LLIHF and LGFI, split it into two 32-bit pieces. |
| 610 | if (Node->getValueType(0) == MVT::i64) { |
| 611 | uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue(); |
| 612 | if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) |
| 613 | Node = splitLargeImmediate(ISD::OR, Node, SDValue(), |
| 614 | Val - uint32_t(Val), uint32_t(Val)); |
| 615 | } |
| 616 | break; |
| 617 | |
| 618 | case ISD::ATOMIC_LOAD_SUB: |
| 619 | // Try to convert subtractions of constants to additions. |
| 620 | if (ConstantSDNode *Op2 = dyn_cast<ConstantSDNode>(Node->getOperand(2))) { |
| 621 | uint64_t Value = -Op2->getZExtValue(); |
| 622 | EVT VT = Node->getValueType(0); |
| 623 | if (VT == MVT::i32 || isInt<32>(Value)) { |
| 624 | SDValue Ops[] = { Node->getOperand(0), Node->getOperand(1), |
| 625 | CurDAG->getConstant(int32_t(Value), VT) }; |
| 626 | Node = CurDAG->MorphNodeTo(Node, ISD::ATOMIC_LOAD_ADD, |
| 627 | Node->getVTList(), Ops, array_lengthof(Ops)); |
| 628 | } |
| 629 | } |
| 630 | break; |
| 631 | } |
| 632 | |
| 633 | // Select the default instruction |
| 634 | SDNode *ResNode = SelectCode(Node); |
| 635 | |
| 636 | DEBUG(errs() << "=> "; |
| 637 | if (ResNode == NULL || ResNode == Node) |
| 638 | Node->dump(CurDAG); |
| 639 | else |
| 640 | ResNode->dump(CurDAG); |
| 641 | errs() << "\n"; |
| 642 | ); |
| 643 | return ResNode; |
| 644 | } |
| 645 | |
| 646 | bool SystemZDAGToDAGISel:: |
| 647 | SelectInlineAsmMemoryOperand(const SDValue &Op, |
| 648 | char ConstraintCode, |
| 649 | std::vector<SDValue> &OutOps) { |
| 650 | assert(ConstraintCode == 'm' && "Unexpected constraint code"); |
| 651 | // Accept addresses with short displacements, which are compatible |
| 652 | // with Q, R, S and T. But keep the index operand for future expansion. |
| 653 | SDValue Base, Disp, Index; |
| 654 | if (!selectBDXAddr(SystemZAddressingMode::FormBD, |
| 655 | SystemZAddressingMode::Disp12Only, |
| 656 | Op, Base, Disp, Index)) |
| 657 | return true; |
| 658 | OutOps.push_back(Base); |
| 659 | OutOps.push_back(Disp); |
| 660 | OutOps.push_back(Index); |
| 661 | return false; |
| 662 | } |