1 //===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===// 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 ARM target. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "ARM.h" 15 #include "ARMBaseInstrInfo.h" 16 #include "ARMTargetMachine.h" 17 #include "MCTargetDesc/ARMAddressingModes.h" 18 #include "llvm/ADT/StringSwitch.h" 19 #include "llvm/CodeGen/MachineFrameInfo.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 #include "llvm/CodeGen/SelectionDAG.h" 24 #include "llvm/CodeGen/SelectionDAGISel.h" 25 #include "llvm/IR/CallingConv.h" 26 #include "llvm/IR/Constants.h" 27 #include "llvm/IR/DerivedTypes.h" 28 #include "llvm/IR/Function.h" 29 #include "llvm/IR/Intrinsics.h" 30 #include "llvm/IR/LLVMContext.h" 31 #include "llvm/Support/CommandLine.h" 32 #include "llvm/Support/Debug.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Target/TargetLowering.h" 35 #include "llvm/Target/TargetOptions.h" 36 37 using namespace llvm; 38 39 #define DEBUG_TYPE "arm-isel" 40 41 static cl::opt<bool> 42 DisableShifterOp("disable-shifter-op", cl::Hidden, 43 cl::desc("Disable isel of shifter-op"), 44 cl::init(false)); 45 46 //===--------------------------------------------------------------------===// 47 /// ARMDAGToDAGISel - ARM specific code to select ARM machine 48 /// instructions for SelectionDAG operations. 49 /// 50 namespace { 51 52 enum AddrMode2Type { 53 AM2_BASE, // Simple AM2 (+-imm12) 54 AM2_SHOP // Shifter-op AM2 55 }; 56 57 class ARMDAGToDAGISel : public SelectionDAGISel { 58 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can 59 /// make the right decision when generating code for different targets. 60 const ARMSubtarget *Subtarget; 61 62 public: 63 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm, CodeGenOpt::Level OptLevel) 64 : SelectionDAGISel(tm, OptLevel) {} 65 66 bool runOnMachineFunction(MachineFunction &MF) override { 67 // Reset the subtarget each time through. 68 Subtarget = &MF.getSubtarget<ARMSubtarget>(); 69 SelectionDAGISel::runOnMachineFunction(MF); 70 return true; 71 } 72 73 StringRef getPassName() const override { return "ARM Instruction Selection"; } 74 75 void PreprocessISelDAG() override; 76 77 /// getI32Imm - Return a target constant of type i32 with the specified 78 /// value. 79 inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) { 80 return CurDAG->getTargetConstant(Imm, dl, MVT::i32); 81 } 82 83 void Select(SDNode *N) override; 84 85 bool hasNoVMLxHazardUse(SDNode *N) const; 86 bool isShifterOpProfitable(const SDValue &Shift, 87 ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt); 88 bool SelectRegShifterOperand(SDValue N, SDValue &A, 89 SDValue &B, SDValue &C, 90 bool CheckProfitability = true); 91 bool SelectImmShifterOperand(SDValue N, SDValue &A, 92 SDValue &B, bool CheckProfitability = true); 93 bool SelectShiftRegShifterOperand(SDValue N, SDValue &A, 94 SDValue &B, SDValue &C) { 95 // Don't apply the profitability check 96 return SelectRegShifterOperand(N, A, B, C, false); 97 } 98 bool SelectShiftImmShifterOperand(SDValue N, SDValue &A, 99 SDValue &B) { 100 // Don't apply the profitability check 101 return SelectImmShifterOperand(N, A, B, false); 102 } 103 104 bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm); 105 bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc); 106 107 AddrMode2Type SelectAddrMode2Worker(SDValue N, SDValue &Base, 108 SDValue &Offset, SDValue &Opc); 109 bool SelectAddrMode2Base(SDValue N, SDValue &Base, SDValue &Offset, 110 SDValue &Opc) { 111 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_BASE; 112 } 113 114 bool SelectAddrMode2ShOp(SDValue N, SDValue &Base, SDValue &Offset, 115 SDValue &Opc) { 116 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_SHOP; 117 } 118 119 bool SelectAddrMode2(SDValue N, SDValue &Base, SDValue &Offset, 120 SDValue &Opc) { 121 SelectAddrMode2Worker(N, Base, Offset, Opc); 122 // return SelectAddrMode2ShOp(N, Base, Offset, Opc); 123 // This always matches one way or another. 124 return true; 125 } 126 127 bool SelectCMOVPred(SDValue N, SDValue &Pred, SDValue &Reg) { 128 const ConstantSDNode *CN = cast<ConstantSDNode>(N); 129 Pred = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(N), MVT::i32); 130 Reg = CurDAG->getRegister(ARM::CPSR, MVT::i32); 131 return true; 132 } 133 134 bool SelectAddrMode2OffsetReg(SDNode *Op, SDValue N, 135 SDValue &Offset, SDValue &Opc); 136 bool SelectAddrMode2OffsetImm(SDNode *Op, SDValue N, 137 SDValue &Offset, SDValue &Opc); 138 bool SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N, 139 SDValue &Offset, SDValue &Opc); 140 bool SelectAddrOffsetNone(SDValue N, SDValue &Base); 141 bool SelectAddrMode3(SDValue N, SDValue &Base, 142 SDValue &Offset, SDValue &Opc); 143 bool SelectAddrMode3Offset(SDNode *Op, SDValue N, 144 SDValue &Offset, SDValue &Opc); 145 bool SelectAddrMode5(SDValue N, SDValue &Base, 146 SDValue &Offset); 147 bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align); 148 bool SelectAddrMode6Offset(SDNode *Op, SDValue N, SDValue &Offset); 149 150 bool SelectAddrModePC(SDValue N, SDValue &Offset, SDValue &Label); 151 152 // Thumb Addressing Modes: 153 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset); 154 bool SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, SDValue &Base, 155 SDValue &OffImm); 156 bool SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base, 157 SDValue &OffImm); 158 bool SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base, 159 SDValue &OffImm); 160 bool SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base, 161 SDValue &OffImm); 162 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm); 163 164 // Thumb 2 Addressing Modes: 165 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm); 166 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base, 167 SDValue &OffImm); 168 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N, 169 SDValue &OffImm); 170 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base, 171 SDValue &OffReg, SDValue &ShImm); 172 bool SelectT2AddrModeExclusive(SDValue N, SDValue &Base, SDValue &OffImm); 173 174 inline bool is_so_imm(unsigned Imm) const { 175 return ARM_AM::getSOImmVal(Imm) != -1; 176 } 177 178 inline bool is_so_imm_not(unsigned Imm) const { 179 return ARM_AM::getSOImmVal(~Imm) != -1; 180 } 181 182 inline bool is_t2_so_imm(unsigned Imm) const { 183 return ARM_AM::getT2SOImmVal(Imm) != -1; 184 } 185 186 inline bool is_t2_so_imm_not(unsigned Imm) const { 187 return ARM_AM::getT2SOImmVal(~Imm) != -1; 188 } 189 190 // Include the pieces autogenerated from the target description. 191 #include "ARMGenDAGISel.inc" 192 193 private: 194 void transferMemOperands(SDNode *Src, SDNode *Dst); 195 196 /// Indexed (pre/post inc/dec) load matching code for ARM. 197 bool tryARMIndexedLoad(SDNode *N); 198 bool tryT1IndexedLoad(SDNode *N); 199 bool tryT2IndexedLoad(SDNode *N); 200 201 /// SelectVLD - Select NEON load intrinsics. NumVecs should be 202 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for 203 /// loads of D registers and even subregs and odd subregs of Q registers. 204 /// For NumVecs <= 2, QOpcodes1 is not used. 205 void SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs, 206 const uint16_t *DOpcodes, const uint16_t *QOpcodes0, 207 const uint16_t *QOpcodes1); 208 209 /// SelectVST - Select NEON store intrinsics. NumVecs should 210 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for 211 /// stores of D registers and even subregs and odd subregs of Q registers. 212 /// For NumVecs <= 2, QOpcodes1 is not used. 213 void SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs, 214 const uint16_t *DOpcodes, const uint16_t *QOpcodes0, 215 const uint16_t *QOpcodes1); 216 217 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should 218 /// be 2, 3 or 4. The opcode arrays specify the instructions used for 219 /// load/store of D registers and Q registers. 220 void SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating, 221 unsigned NumVecs, const uint16_t *DOpcodes, 222 const uint16_t *QOpcodes); 223 224 /// SelectVLDDup - Select NEON load-duplicate intrinsics. NumVecs 225 /// should be 2, 3 or 4. The opcode array specifies the instructions used 226 /// for loading D registers. (Q registers are not supported.) 227 void SelectVLDDup(SDNode *N, bool isUpdating, unsigned NumVecs, 228 const uint16_t *Opcodes); 229 230 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2, 231 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be 232 /// generated to force the table registers to be consecutive. 233 void SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc); 234 235 /// Try to select SBFX/UBFX instructions for ARM. 236 bool tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned); 237 238 // Select special operations if node forms integer ABS pattern 239 bool tryABSOp(SDNode *N); 240 241 bool tryReadRegister(SDNode *N); 242 bool tryWriteRegister(SDNode *N); 243 244 bool tryInlineAsm(SDNode *N); 245 246 void SelectConcatVector(SDNode *N); 247 248 bool trySMLAWSMULW(SDNode *N); 249 250 void SelectCMP_SWAP(SDNode *N); 251 252 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for 253 /// inline asm expressions. 254 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, 255 std::vector<SDValue> &OutOps) override; 256 257 // Form pairs of consecutive R, S, D, or Q registers. 258 SDNode *createGPRPairNode(EVT VT, SDValue V0, SDValue V1); 259 SDNode *createSRegPairNode(EVT VT, SDValue V0, SDValue V1); 260 SDNode *createDRegPairNode(EVT VT, SDValue V0, SDValue V1); 261 SDNode *createQRegPairNode(EVT VT, SDValue V0, SDValue V1); 262 263 // Form sequences of 4 consecutive S, D, or Q registers. 264 SDNode *createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3); 265 SDNode *createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3); 266 SDNode *createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3); 267 268 // Get the alignment operand for a NEON VLD or VST instruction. 269 SDValue GetVLDSTAlign(SDValue Align, const SDLoc &dl, unsigned NumVecs, 270 bool is64BitVector); 271 272 /// Returns the number of instructions required to materialize the given 273 /// constant in a register, or 3 if a literal pool load is needed. 274 unsigned ConstantMaterializationCost(unsigned Val) const; 275 276 /// Checks if N is a multiplication by a constant where we can extract out a 277 /// power of two from the constant so that it can be used in a shift, but only 278 /// if it simplifies the materialization of the constant. Returns true if it 279 /// is, and assigns to PowerOfTwo the power of two that should be extracted 280 /// out and to NewMulConst the new constant to be multiplied by. 281 bool canExtractShiftFromMul(const SDValue &N, unsigned MaxShift, 282 unsigned &PowerOfTwo, SDValue &NewMulConst) const; 283 284 /// Replace N with M in CurDAG, in a way that also ensures that M gets 285 /// selected when N would have been selected. 286 void replaceDAGValue(const SDValue &N, SDValue M); 287 }; 288 } 289 290 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant 291 /// operand. If so Imm will receive the 32-bit value. 292 static bool isInt32Immediate(SDNode *N, unsigned &Imm) { 293 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) { 294 Imm = cast<ConstantSDNode>(N)->getZExtValue(); 295 return true; 296 } 297 return false; 298 } 299 300 // isInt32Immediate - This method tests to see if a constant operand. 301 // If so Imm will receive the 32 bit value. 302 static bool isInt32Immediate(SDValue N, unsigned &Imm) { 303 return isInt32Immediate(N.getNode(), Imm); 304 } 305 306 // isOpcWithIntImmediate - This method tests to see if the node is a specific 307 // opcode and that it has a immediate integer right operand. 308 // If so Imm will receive the 32 bit value. 309 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { 310 return N->getOpcode() == Opc && 311 isInt32Immediate(N->getOperand(1).getNode(), Imm); 312 } 313 314 /// \brief Check whether a particular node is a constant value representable as 315 /// (N * Scale) where (N in [\p RangeMin, \p RangeMax). 316 /// 317 /// \param ScaledConstant [out] - On success, the pre-scaled constant value. 318 static bool isScaledConstantInRange(SDValue Node, int Scale, 319 int RangeMin, int RangeMax, 320 int &ScaledConstant) { 321 assert(Scale > 0 && "Invalid scale!"); 322 323 // Check that this is a constant. 324 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Node); 325 if (!C) 326 return false; 327 328 ScaledConstant = (int) C->getZExtValue(); 329 if ((ScaledConstant % Scale) != 0) 330 return false; 331 332 ScaledConstant /= Scale; 333 return ScaledConstant >= RangeMin && ScaledConstant < RangeMax; 334 } 335 336 void ARMDAGToDAGISel::PreprocessISelDAG() { 337 if (!Subtarget->hasV6T2Ops()) 338 return; 339 340 bool isThumb2 = Subtarget->isThumb(); 341 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), 342 E = CurDAG->allnodes_end(); I != E; ) { 343 SDNode *N = &*I++; // Preincrement iterator to avoid invalidation issues. 344 345 if (N->getOpcode() != ISD::ADD) 346 continue; 347 348 // Look for (add X1, (and (srl X2, c1), c2)) where c2 is constant with 349 // leading zeros, followed by consecutive set bits, followed by 1 or 2 350 // trailing zeros, e.g. 1020. 351 // Transform the expression to 352 // (add X1, (shl (and (srl X2, c1), (c2>>tz)), tz)) where tz is the number 353 // of trailing zeros of c2. The left shift would be folded as an shifter 354 // operand of 'add' and the 'and' and 'srl' would become a bits extraction 355 // node (UBFX). 356 357 SDValue N0 = N->getOperand(0); 358 SDValue N1 = N->getOperand(1); 359 unsigned And_imm = 0; 360 if (!isOpcWithIntImmediate(N1.getNode(), ISD::AND, And_imm)) { 361 if (isOpcWithIntImmediate(N0.getNode(), ISD::AND, And_imm)) 362 std::swap(N0, N1); 363 } 364 if (!And_imm) 365 continue; 366 367 // Check if the AND mask is an immediate of the form: 000.....1111111100 368 unsigned TZ = countTrailingZeros(And_imm); 369 if (TZ != 1 && TZ != 2) 370 // Be conservative here. Shifter operands aren't always free. e.g. On 371 // Swift, left shifter operand of 1 / 2 for free but others are not. 372 // e.g. 373 // ubfx r3, r1, #16, #8 374 // ldr.w r3, [r0, r3, lsl #2] 375 // vs. 376 // mov.w r9, #1020 377 // and.w r2, r9, r1, lsr #14 378 // ldr r2, [r0, r2] 379 continue; 380 And_imm >>= TZ; 381 if (And_imm & (And_imm + 1)) 382 continue; 383 384 // Look for (and (srl X, c1), c2). 385 SDValue Srl = N1.getOperand(0); 386 unsigned Srl_imm = 0; 387 if (!isOpcWithIntImmediate(Srl.getNode(), ISD::SRL, Srl_imm) || 388 (Srl_imm <= 2)) 389 continue; 390 391 // Make sure first operand is not a shifter operand which would prevent 392 // folding of the left shift. 393 SDValue CPTmp0; 394 SDValue CPTmp1; 395 SDValue CPTmp2; 396 if (isThumb2) { 397 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1)) 398 continue; 399 } else { 400 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1) || 401 SelectRegShifterOperand(N0, CPTmp0, CPTmp1, CPTmp2)) 402 continue; 403 } 404 405 // Now make the transformation. 406 Srl = CurDAG->getNode(ISD::SRL, SDLoc(Srl), MVT::i32, 407 Srl.getOperand(0), 408 CurDAG->getConstant(Srl_imm + TZ, SDLoc(Srl), 409 MVT::i32)); 410 N1 = CurDAG->getNode(ISD::AND, SDLoc(N1), MVT::i32, 411 Srl, 412 CurDAG->getConstant(And_imm, SDLoc(Srl), MVT::i32)); 413 N1 = CurDAG->getNode(ISD::SHL, SDLoc(N1), MVT::i32, 414 N1, CurDAG->getConstant(TZ, SDLoc(Srl), MVT::i32)); 415 CurDAG->UpdateNodeOperands(N, N0, N1); 416 } 417 } 418 419 /// hasNoVMLxHazardUse - Return true if it's desirable to select a FP MLA / MLS 420 /// node. VFP / NEON fp VMLA / VMLS instructions have special RAW hazards (at 421 /// least on current ARM implementations) which should be avoidded. 422 bool ARMDAGToDAGISel::hasNoVMLxHazardUse(SDNode *N) const { 423 if (OptLevel == CodeGenOpt::None) 424 return true; 425 426 if (!Subtarget->hasVMLxHazards()) 427 return true; 428 429 if (!N->hasOneUse()) 430 return false; 431 432 SDNode *Use = *N->use_begin(); 433 if (Use->getOpcode() == ISD::CopyToReg) 434 return true; 435 if (Use->isMachineOpcode()) { 436 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>( 437 CurDAG->getSubtarget().getInstrInfo()); 438 439 const MCInstrDesc &MCID = TII->get(Use->getMachineOpcode()); 440 if (MCID.mayStore()) 441 return true; 442 unsigned Opcode = MCID.getOpcode(); 443 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD) 444 return true; 445 // vmlx feeding into another vmlx. We actually want to unfold 446 // the use later in the MLxExpansion pass. e.g. 447 // vmla 448 // vmla (stall 8 cycles) 449 // 450 // vmul (5 cycles) 451 // vadd (5 cycles) 452 // vmla 453 // This adds up to about 18 - 19 cycles. 454 // 455 // vmla 456 // vmul (stall 4 cycles) 457 // vadd adds up to about 14 cycles. 458 return TII->isFpMLxInstruction(Opcode); 459 } 460 461 return false; 462 } 463 464 bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift, 465 ARM_AM::ShiftOpc ShOpcVal, 466 unsigned ShAmt) { 467 if (!Subtarget->isLikeA9() && !Subtarget->isSwift()) 468 return true; 469 if (Shift.hasOneUse()) 470 return true; 471 // R << 2 is free. 472 return ShOpcVal == ARM_AM::lsl && 473 (ShAmt == 2 || (Subtarget->isSwift() && ShAmt == 1)); 474 } 475 476 unsigned ARMDAGToDAGISel::ConstantMaterializationCost(unsigned Val) const { 477 if (Subtarget->isThumb()) { 478 if (Val <= 255) return 1; // MOV 479 if (Subtarget->hasV6T2Ops() && 480 (Val <= 0xffff || ARM_AM::getT2SOImmValSplatVal(Val) != -1)) 481 return 1; // MOVW 482 if (Val <= 510) return 2; // MOV + ADDi8 483 if (~Val <= 255) return 2; // MOV + MVN 484 if (ARM_AM::isThumbImmShiftedVal(Val)) return 2; // MOV + LSL 485 } else { 486 if (ARM_AM::getSOImmVal(Val) != -1) return 1; // MOV 487 if (ARM_AM::getSOImmVal(~Val) != -1) return 1; // MVN 488 if (Subtarget->hasV6T2Ops() && Val <= 0xffff) return 1; // MOVW 489 if (ARM_AM::isSOImmTwoPartVal(Val)) return 2; // two instrs 490 } 491 if (Subtarget->useMovt(*MF)) return 2; // MOVW + MOVT 492 return 3; // Literal pool load 493 } 494 495 bool ARMDAGToDAGISel::canExtractShiftFromMul(const SDValue &N, 496 unsigned MaxShift, 497 unsigned &PowerOfTwo, 498 SDValue &NewMulConst) const { 499 assert(N.getOpcode() == ISD::MUL); 500 assert(MaxShift > 0); 501 502 // If the multiply is used in more than one place then changing the constant 503 // will make other uses incorrect, so don't. 504 if (!N.hasOneUse()) return false; 505 // Check if the multiply is by a constant 506 ConstantSDNode *MulConst = dyn_cast<ConstantSDNode>(N.getOperand(1)); 507 if (!MulConst) return false; 508 // If the constant is used in more than one place then modifying it will mean 509 // we need to materialize two constants instead of one, which is a bad idea. 510 if (!MulConst->hasOneUse()) return false; 511 unsigned MulConstVal = MulConst->getZExtValue(); 512 if (MulConstVal == 0) return false; 513 514 // Find the largest power of 2 that MulConstVal is a multiple of 515 PowerOfTwo = MaxShift; 516 while ((MulConstVal % (1 << PowerOfTwo)) != 0) { 517 --PowerOfTwo; 518 if (PowerOfTwo == 0) return false; 519 } 520 521 // Only optimise if the new cost is better 522 unsigned NewMulConstVal = MulConstVal / (1 << PowerOfTwo); 523 NewMulConst = CurDAG->getConstant(NewMulConstVal, SDLoc(N), MVT::i32); 524 unsigned OldCost = ConstantMaterializationCost(MulConstVal); 525 unsigned NewCost = ConstantMaterializationCost(NewMulConstVal); 526 return NewCost < OldCost; 527 } 528 529 void ARMDAGToDAGISel::replaceDAGValue(const SDValue &N, SDValue M) { 530 CurDAG->RepositionNode(N.getNode()->getIterator(), M.getNode()); 531 CurDAG->ReplaceAllUsesWith(N, M); 532 } 533 534 bool ARMDAGToDAGISel::SelectImmShifterOperand(SDValue N, 535 SDValue &BaseReg, 536 SDValue &Opc, 537 bool CheckProfitability) { 538 if (DisableShifterOp) 539 return false; 540 541 // If N is a multiply-by-constant and it's profitable to extract a shift and 542 // use it in a shifted operand do so. 543 if (N.getOpcode() == ISD::MUL) { 544 unsigned PowerOfTwo = 0; 545 SDValue NewMulConst; 546 if (canExtractShiftFromMul(N, 31, PowerOfTwo, NewMulConst)) { 547 HandleSDNode Handle(N); 548 replaceDAGValue(N.getOperand(1), NewMulConst); 549 BaseReg = Handle.getValue(); 550 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ARM_AM::lsl, 551 PowerOfTwo), 552 SDLoc(N), MVT::i32); 553 return true; 554 } 555 } 556 557 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode()); 558 559 // Don't match base register only case. That is matched to a separate 560 // lower complexity pattern with explicit register operand. 561 if (ShOpcVal == ARM_AM::no_shift) return false; 562 563 BaseReg = N.getOperand(0); 564 unsigned ShImmVal = 0; 565 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1)); 566 if (!RHS) return false; 567 ShImmVal = RHS->getZExtValue() & 31; 568 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal), 569 SDLoc(N), MVT::i32); 570 return true; 571 } 572 573 bool ARMDAGToDAGISel::SelectRegShifterOperand(SDValue N, 574 SDValue &BaseReg, 575 SDValue &ShReg, 576 SDValue &Opc, 577 bool CheckProfitability) { 578 if (DisableShifterOp) 579 return false; 580 581 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode()); 582 583 // Don't match base register only case. That is matched to a separate 584 // lower complexity pattern with explicit register operand. 585 if (ShOpcVal == ARM_AM::no_shift) return false; 586 587 BaseReg = N.getOperand(0); 588 unsigned ShImmVal = 0; 589 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1)); 590 if (RHS) return false; 591 592 ShReg = N.getOperand(1); 593 if (CheckProfitability && !isShifterOpProfitable(N, ShOpcVal, ShImmVal)) 594 return false; 595 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal), 596 SDLoc(N), MVT::i32); 597 return true; 598 } 599 600 601 bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N, 602 SDValue &Base, 603 SDValue &OffImm) { 604 // Match simple R + imm12 operands. 605 606 // Base only. 607 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB && 608 !CurDAG->isBaseWithConstantOffset(N)) { 609 if (N.getOpcode() == ISD::FrameIndex) { 610 // Match frame index. 611 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 612 Base = CurDAG->getTargetFrameIndex( 613 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 614 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32); 615 return true; 616 } 617 618 if (N.getOpcode() == ARMISD::Wrapper && 619 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress && 620 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol && 621 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) { 622 Base = N.getOperand(0); 623 } else 624 Base = N; 625 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32); 626 return true; 627 } 628 629 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) { 630 int RHSC = (int)RHS->getSExtValue(); 631 if (N.getOpcode() == ISD::SUB) 632 RHSC = -RHSC; 633 634 if (RHSC > -0x1000 && RHSC < 0x1000) { // 12 bits 635 Base = N.getOperand(0); 636 if (Base.getOpcode() == ISD::FrameIndex) { 637 int FI = cast<FrameIndexSDNode>(Base)->getIndex(); 638 Base = CurDAG->getTargetFrameIndex( 639 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 640 } 641 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32); 642 return true; 643 } 644 } 645 646 // Base only. 647 Base = N; 648 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32); 649 return true; 650 } 651 652 653 654 bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, 655 SDValue &Opc) { 656 if (N.getOpcode() == ISD::MUL && 657 ((!Subtarget->isLikeA9() && !Subtarget->isSwift()) || N.hasOneUse())) { 658 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) { 659 // X * [3,5,9] -> X + X * [2,4,8] etc. 660 int RHSC = (int)RHS->getZExtValue(); 661 if (RHSC & 1) { 662 RHSC = RHSC & ~1; 663 ARM_AM::AddrOpc AddSub = ARM_AM::add; 664 if (RHSC < 0) { 665 AddSub = ARM_AM::sub; 666 RHSC = - RHSC; 667 } 668 if (isPowerOf2_32(RHSC)) { 669 unsigned ShAmt = Log2_32(RHSC); 670 Base = Offset = N.getOperand(0); 671 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, 672 ARM_AM::lsl), 673 SDLoc(N), MVT::i32); 674 return true; 675 } 676 } 677 } 678 } 679 680 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB && 681 // ISD::OR that is equivalent to an ISD::ADD. 682 !CurDAG->isBaseWithConstantOffset(N)) 683 return false; 684 685 // Leave simple R +/- imm12 operands for LDRi12 686 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) { 687 int RHSC; 688 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1, 689 -0x1000+1, 0x1000, RHSC)) // 12 bits. 690 return false; 691 } 692 693 // Otherwise this is R +/- [possibly shifted] R. 694 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::SUB ? ARM_AM::sub:ARM_AM::add; 695 ARM_AM::ShiftOpc ShOpcVal = 696 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode()); 697 unsigned ShAmt = 0; 698 699 Base = N.getOperand(0); 700 Offset = N.getOperand(1); 701 702 if (ShOpcVal != ARM_AM::no_shift) { 703 // Check to see if the RHS of the shift is a constant, if not, we can't fold 704 // it. 705 if (ConstantSDNode *Sh = 706 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) { 707 ShAmt = Sh->getZExtValue(); 708 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt)) 709 Offset = N.getOperand(1).getOperand(0); 710 else { 711 ShAmt = 0; 712 ShOpcVal = ARM_AM::no_shift; 713 } 714 } else { 715 ShOpcVal = ARM_AM::no_shift; 716 } 717 } 718 719 // Try matching (R shl C) + (R). 720 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift && 721 !(Subtarget->isLikeA9() || Subtarget->isSwift() || 722 N.getOperand(0).hasOneUse())) { 723 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode()); 724 if (ShOpcVal != ARM_AM::no_shift) { 725 // Check to see if the RHS of the shift is a constant, if not, we can't 726 // fold it. 727 if (ConstantSDNode *Sh = 728 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) { 729 ShAmt = Sh->getZExtValue(); 730 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) { 731 Offset = N.getOperand(0).getOperand(0); 732 Base = N.getOperand(1); 733 } else { 734 ShAmt = 0; 735 ShOpcVal = ARM_AM::no_shift; 736 } 737 } else { 738 ShOpcVal = ARM_AM::no_shift; 739 } 740 } 741 } 742 743 // If Offset is a multiply-by-constant and it's profitable to extract a shift 744 // and use it in a shifted operand do so. 745 if (Offset.getOpcode() == ISD::MUL && N.hasOneUse()) { 746 unsigned PowerOfTwo = 0; 747 SDValue NewMulConst; 748 if (canExtractShiftFromMul(Offset, 31, PowerOfTwo, NewMulConst)) { 749 replaceDAGValue(Offset.getOperand(1), NewMulConst); 750 ShAmt = PowerOfTwo; 751 ShOpcVal = ARM_AM::lsl; 752 } 753 } 754 755 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal), 756 SDLoc(N), MVT::i32); 757 return true; 758 } 759 760 761 //----- 762 763 AddrMode2Type ARMDAGToDAGISel::SelectAddrMode2Worker(SDValue N, 764 SDValue &Base, 765 SDValue &Offset, 766 SDValue &Opc) { 767 if (N.getOpcode() == ISD::MUL && 768 (!(Subtarget->isLikeA9() || Subtarget->isSwift()) || N.hasOneUse())) { 769 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) { 770 // X * [3,5,9] -> X + X * [2,4,8] etc. 771 int RHSC = (int)RHS->getZExtValue(); 772 if (RHSC & 1) { 773 RHSC = RHSC & ~1; 774 ARM_AM::AddrOpc AddSub = ARM_AM::add; 775 if (RHSC < 0) { 776 AddSub = ARM_AM::sub; 777 RHSC = - RHSC; 778 } 779 if (isPowerOf2_32(RHSC)) { 780 unsigned ShAmt = Log2_32(RHSC); 781 Base = Offset = N.getOperand(0); 782 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, 783 ARM_AM::lsl), 784 SDLoc(N), MVT::i32); 785 return AM2_SHOP; 786 } 787 } 788 } 789 } 790 791 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB && 792 // ISD::OR that is equivalent to an ADD. 793 !CurDAG->isBaseWithConstantOffset(N)) { 794 Base = N; 795 if (N.getOpcode() == ISD::FrameIndex) { 796 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 797 Base = CurDAG->getTargetFrameIndex( 798 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 799 } else if (N.getOpcode() == ARMISD::Wrapper && 800 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress && 801 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol && 802 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) { 803 Base = N.getOperand(0); 804 } 805 Offset = CurDAG->getRegister(0, MVT::i32); 806 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0, 807 ARM_AM::no_shift), 808 SDLoc(N), MVT::i32); 809 return AM2_BASE; 810 } 811 812 // Match simple R +/- imm12 operands. 813 if (N.getOpcode() != ISD::SUB) { 814 int RHSC; 815 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1, 816 -0x1000+1, 0x1000, RHSC)) { // 12 bits. 817 Base = N.getOperand(0); 818 if (Base.getOpcode() == ISD::FrameIndex) { 819 int FI = cast<FrameIndexSDNode>(Base)->getIndex(); 820 Base = CurDAG->getTargetFrameIndex( 821 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 822 } 823 Offset = CurDAG->getRegister(0, MVT::i32); 824 825 ARM_AM::AddrOpc AddSub = ARM_AM::add; 826 if (RHSC < 0) { 827 AddSub = ARM_AM::sub; 828 RHSC = - RHSC; 829 } 830 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC, 831 ARM_AM::no_shift), 832 SDLoc(N), MVT::i32); 833 return AM2_BASE; 834 } 835 } 836 837 if ((Subtarget->isLikeA9() || Subtarget->isSwift()) && !N.hasOneUse()) { 838 // Compute R +/- (R << N) and reuse it. 839 Base = N; 840 Offset = CurDAG->getRegister(0, MVT::i32); 841 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0, 842 ARM_AM::no_shift), 843 SDLoc(N), MVT::i32); 844 return AM2_BASE; 845 } 846 847 // Otherwise this is R +/- [possibly shifted] R. 848 ARM_AM::AddrOpc AddSub = N.getOpcode() != ISD::SUB ? ARM_AM::add:ARM_AM::sub; 849 ARM_AM::ShiftOpc ShOpcVal = 850 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode()); 851 unsigned ShAmt = 0; 852 853 Base = N.getOperand(0); 854 Offset = N.getOperand(1); 855 856 if (ShOpcVal != ARM_AM::no_shift) { 857 // Check to see if the RHS of the shift is a constant, if not, we can't fold 858 // it. 859 if (ConstantSDNode *Sh = 860 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) { 861 ShAmt = Sh->getZExtValue(); 862 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt)) 863 Offset = N.getOperand(1).getOperand(0); 864 else { 865 ShAmt = 0; 866 ShOpcVal = ARM_AM::no_shift; 867 } 868 } else { 869 ShOpcVal = ARM_AM::no_shift; 870 } 871 } 872 873 // Try matching (R shl C) + (R). 874 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift && 875 !(Subtarget->isLikeA9() || Subtarget->isSwift() || 876 N.getOperand(0).hasOneUse())) { 877 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode()); 878 if (ShOpcVal != ARM_AM::no_shift) { 879 // Check to see if the RHS of the shift is a constant, if not, we can't 880 // fold it. 881 if (ConstantSDNode *Sh = 882 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) { 883 ShAmt = Sh->getZExtValue(); 884 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) { 885 Offset = N.getOperand(0).getOperand(0); 886 Base = N.getOperand(1); 887 } else { 888 ShAmt = 0; 889 ShOpcVal = ARM_AM::no_shift; 890 } 891 } else { 892 ShOpcVal = ARM_AM::no_shift; 893 } 894 } 895 } 896 897 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal), 898 SDLoc(N), MVT::i32); 899 return AM2_SHOP; 900 } 901 902 bool ARMDAGToDAGISel::SelectAddrMode2OffsetReg(SDNode *Op, SDValue N, 903 SDValue &Offset, SDValue &Opc) { 904 unsigned Opcode = Op->getOpcode(); 905 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD) 906 ? cast<LoadSDNode>(Op)->getAddressingMode() 907 : cast<StoreSDNode>(Op)->getAddressingMode(); 908 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC) 909 ? ARM_AM::add : ARM_AM::sub; 910 int Val; 911 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) 912 return false; 913 914 Offset = N; 915 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode()); 916 unsigned ShAmt = 0; 917 if (ShOpcVal != ARM_AM::no_shift) { 918 // Check to see if the RHS of the shift is a constant, if not, we can't fold 919 // it. 920 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) { 921 ShAmt = Sh->getZExtValue(); 922 if (isShifterOpProfitable(N, ShOpcVal, ShAmt)) 923 Offset = N.getOperand(0); 924 else { 925 ShAmt = 0; 926 ShOpcVal = ARM_AM::no_shift; 927 } 928 } else { 929 ShOpcVal = ARM_AM::no_shift; 930 } 931 } 932 933 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal), 934 SDLoc(N), MVT::i32); 935 return true; 936 } 937 938 bool ARMDAGToDAGISel::SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N, 939 SDValue &Offset, SDValue &Opc) { 940 unsigned Opcode = Op->getOpcode(); 941 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD) 942 ? cast<LoadSDNode>(Op)->getAddressingMode() 943 : cast<StoreSDNode>(Op)->getAddressingMode(); 944 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC) 945 ? ARM_AM::add : ARM_AM::sub; 946 int Val; 947 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits. 948 if (AddSub == ARM_AM::sub) Val *= -1; 949 Offset = CurDAG->getRegister(0, MVT::i32); 950 Opc = CurDAG->getTargetConstant(Val, SDLoc(Op), MVT::i32); 951 return true; 952 } 953 954 return false; 955 } 956 957 958 bool ARMDAGToDAGISel::SelectAddrMode2OffsetImm(SDNode *Op, SDValue N, 959 SDValue &Offset, SDValue &Opc) { 960 unsigned Opcode = Op->getOpcode(); 961 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD) 962 ? cast<LoadSDNode>(Op)->getAddressingMode() 963 : cast<StoreSDNode>(Op)->getAddressingMode(); 964 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC) 965 ? ARM_AM::add : ARM_AM::sub; 966 int Val; 967 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits. 968 Offset = CurDAG->getRegister(0, MVT::i32); 969 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val, 970 ARM_AM::no_shift), 971 SDLoc(Op), MVT::i32); 972 return true; 973 } 974 975 return false; 976 } 977 978 bool ARMDAGToDAGISel::SelectAddrOffsetNone(SDValue N, SDValue &Base) { 979 Base = N; 980 return true; 981 } 982 983 bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N, 984 SDValue &Base, SDValue &Offset, 985 SDValue &Opc) { 986 if (N.getOpcode() == ISD::SUB) { 987 // X - C is canonicalize to X + -C, no need to handle it here. 988 Base = N.getOperand(0); 989 Offset = N.getOperand(1); 990 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0), SDLoc(N), 991 MVT::i32); 992 return true; 993 } 994 995 if (!CurDAG->isBaseWithConstantOffset(N)) { 996 Base = N; 997 if (N.getOpcode() == ISD::FrameIndex) { 998 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 999 Base = CurDAG->getTargetFrameIndex( 1000 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 1001 } 1002 Offset = CurDAG->getRegister(0, MVT::i32); 1003 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N), 1004 MVT::i32); 1005 return true; 1006 } 1007 1008 // If the RHS is +/- imm8, fold into addr mode. 1009 int RHSC; 1010 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1, 1011 -256 + 1, 256, RHSC)) { // 8 bits. 1012 Base = N.getOperand(0); 1013 if (Base.getOpcode() == ISD::FrameIndex) { 1014 int FI = cast<FrameIndexSDNode>(Base)->getIndex(); 1015 Base = CurDAG->getTargetFrameIndex( 1016 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 1017 } 1018 Offset = CurDAG->getRegister(0, MVT::i32); 1019 1020 ARM_AM::AddrOpc AddSub = ARM_AM::add; 1021 if (RHSC < 0) { 1022 AddSub = ARM_AM::sub; 1023 RHSC = -RHSC; 1024 } 1025 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC), SDLoc(N), 1026 MVT::i32); 1027 return true; 1028 } 1029 1030 Base = N.getOperand(0); 1031 Offset = N.getOperand(1); 1032 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N), 1033 MVT::i32); 1034 return true; 1035 } 1036 1037 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N, 1038 SDValue &Offset, SDValue &Opc) { 1039 unsigned Opcode = Op->getOpcode(); 1040 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD) 1041 ? cast<LoadSDNode>(Op)->getAddressingMode() 1042 : cast<StoreSDNode>(Op)->getAddressingMode(); 1043 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC) 1044 ? ARM_AM::add : ARM_AM::sub; 1045 int Val; 1046 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 256, Val)) { // 12 bits. 1047 Offset = CurDAG->getRegister(0, MVT::i32); 1048 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), SDLoc(Op), 1049 MVT::i32); 1050 return true; 1051 } 1052 1053 Offset = N; 1054 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), SDLoc(Op), 1055 MVT::i32); 1056 return true; 1057 } 1058 1059 bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N, 1060 SDValue &Base, SDValue &Offset) { 1061 if (!CurDAG->isBaseWithConstantOffset(N)) { 1062 Base = N; 1063 if (N.getOpcode() == ISD::FrameIndex) { 1064 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 1065 Base = CurDAG->getTargetFrameIndex( 1066 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 1067 } else if (N.getOpcode() == ARMISD::Wrapper && 1068 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress && 1069 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol && 1070 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) { 1071 Base = N.getOperand(0); 1072 } 1073 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0), 1074 SDLoc(N), MVT::i32); 1075 return true; 1076 } 1077 1078 // If the RHS is +/- imm8, fold into addr mode. 1079 int RHSC; 1080 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 1081 -256 + 1, 256, RHSC)) { 1082 Base = N.getOperand(0); 1083 if (Base.getOpcode() == ISD::FrameIndex) { 1084 int FI = cast<FrameIndexSDNode>(Base)->getIndex(); 1085 Base = CurDAG->getTargetFrameIndex( 1086 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 1087 } 1088 1089 ARM_AM::AddrOpc AddSub = ARM_AM::add; 1090 if (RHSC < 0) { 1091 AddSub = ARM_AM::sub; 1092 RHSC = -RHSC; 1093 } 1094 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC), 1095 SDLoc(N), MVT::i32); 1096 return true; 1097 } 1098 1099 Base = N; 1100 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0), 1101 SDLoc(N), MVT::i32); 1102 return true; 1103 } 1104 1105 bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr, 1106 SDValue &Align) { 1107 Addr = N; 1108 1109 unsigned Alignment = 0; 1110 1111 MemSDNode *MemN = cast<MemSDNode>(Parent); 1112 1113 if (isa<LSBaseSDNode>(MemN) || 1114 ((MemN->getOpcode() == ARMISD::VST1_UPD || 1115 MemN->getOpcode() == ARMISD::VLD1_UPD) && 1116 MemN->getConstantOperandVal(MemN->getNumOperands() - 1) == 1)) { 1117 // This case occurs only for VLD1-lane/dup and VST1-lane instructions. 1118 // The maximum alignment is equal to the memory size being referenced. 1119 unsigned MMOAlign = MemN->getAlignment(); 1120 unsigned MemSize = MemN->getMemoryVT().getSizeInBits() / 8; 1121 if (MMOAlign >= MemSize && MemSize > 1) 1122 Alignment = MemSize; 1123 } else { 1124 // All other uses of addrmode6 are for intrinsics. For now just record 1125 // the raw alignment value; it will be refined later based on the legal 1126 // alignment operands for the intrinsic. 1127 Alignment = MemN->getAlignment(); 1128 } 1129 1130 Align = CurDAG->getTargetConstant(Alignment, SDLoc(N), MVT::i32); 1131 return true; 1132 } 1133 1134 bool ARMDAGToDAGISel::SelectAddrMode6Offset(SDNode *Op, SDValue N, 1135 SDValue &Offset) { 1136 LSBaseSDNode *LdSt = cast<LSBaseSDNode>(Op); 1137 ISD::MemIndexedMode AM = LdSt->getAddressingMode(); 1138 if (AM != ISD::POST_INC) 1139 return false; 1140 Offset = N; 1141 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N)) { 1142 if (NC->getZExtValue() * 8 == LdSt->getMemoryVT().getSizeInBits()) 1143 Offset = CurDAG->getRegister(0, MVT::i32); 1144 } 1145 return true; 1146 } 1147 1148 bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N, 1149 SDValue &Offset, SDValue &Label) { 1150 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) { 1151 Offset = N.getOperand(0); 1152 SDValue N1 = N.getOperand(1); 1153 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(), 1154 SDLoc(N), MVT::i32); 1155 return true; 1156 } 1157 1158 return false; 1159 } 1160 1161 1162 //===----------------------------------------------------------------------===// 1163 // Thumb Addressing Modes 1164 //===----------------------------------------------------------------------===// 1165 1166 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N, 1167 SDValue &Base, SDValue &Offset){ 1168 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N)) { 1169 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N); 1170 if (!NC || !NC->isNullValue()) 1171 return false; 1172 1173 Base = Offset = N; 1174 return true; 1175 } 1176 1177 Base = N.getOperand(0); 1178 Offset = N.getOperand(1); 1179 return true; 1180 } 1181 1182 bool 1183 ARMDAGToDAGISel::SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, 1184 SDValue &Base, SDValue &OffImm) { 1185 if (!CurDAG->isBaseWithConstantOffset(N)) { 1186 if (N.getOpcode() == ISD::ADD) { 1187 return false; // We want to select register offset instead 1188 } else if (N.getOpcode() == ARMISD::Wrapper && 1189 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress && 1190 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol && 1191 N.getOperand(0).getOpcode() != ISD::TargetConstantPool && 1192 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) { 1193 Base = N.getOperand(0); 1194 } else { 1195 Base = N; 1196 } 1197 1198 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32); 1199 return true; 1200 } 1201 1202 // If the RHS is + imm5 * scale, fold into addr mode. 1203 int RHSC; 1204 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC)) { 1205 Base = N.getOperand(0); 1206 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32); 1207 return true; 1208 } 1209 1210 // Offset is too large, so use register offset instead. 1211 return false; 1212 } 1213 1214 bool 1215 ARMDAGToDAGISel::SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base, 1216 SDValue &OffImm) { 1217 return SelectThumbAddrModeImm5S(N, 4, Base, OffImm); 1218 } 1219 1220 bool 1221 ARMDAGToDAGISel::SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base, 1222 SDValue &OffImm) { 1223 return SelectThumbAddrModeImm5S(N, 2, Base, OffImm); 1224 } 1225 1226 bool 1227 ARMDAGToDAGISel::SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base, 1228 SDValue &OffImm) { 1229 return SelectThumbAddrModeImm5S(N, 1, Base, OffImm); 1230 } 1231 1232 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N, 1233 SDValue &Base, SDValue &OffImm) { 1234 if (N.getOpcode() == ISD::FrameIndex) { 1235 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 1236 // Only multiples of 4 are allowed for the offset, so the frame object 1237 // alignment must be at least 4. 1238 MachineFrameInfo &MFI = MF->getFrameInfo(); 1239 if (MFI.getObjectAlignment(FI) < 4) 1240 MFI.setObjectAlignment(FI, 4); 1241 Base = CurDAG->getTargetFrameIndex( 1242 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 1243 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32); 1244 return true; 1245 } 1246 1247 if (!CurDAG->isBaseWithConstantOffset(N)) 1248 return false; 1249 1250 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0)); 1251 if (N.getOperand(0).getOpcode() == ISD::FrameIndex || 1252 (LHSR && LHSR->getReg() == ARM::SP)) { 1253 // If the RHS is + imm8 * scale, fold into addr mode. 1254 int RHSC; 1255 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 0, 256, RHSC)) { 1256 Base = N.getOperand(0); 1257 if (Base.getOpcode() == ISD::FrameIndex) { 1258 int FI = cast<FrameIndexSDNode>(Base)->getIndex(); 1259 // For LHS+RHS to result in an offset that's a multiple of 4 the object 1260 // indexed by the LHS must be 4-byte aligned. 1261 MachineFrameInfo &MFI = MF->getFrameInfo(); 1262 if (MFI.getObjectAlignment(FI) < 4) 1263 MFI.setObjectAlignment(FI, 4); 1264 Base = CurDAG->getTargetFrameIndex( 1265 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 1266 } 1267 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32); 1268 return true; 1269 } 1270 } 1271 1272 return false; 1273 } 1274 1275 1276 //===----------------------------------------------------------------------===// 1277 // Thumb 2 Addressing Modes 1278 //===----------------------------------------------------------------------===// 1279 1280 1281 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N, 1282 SDValue &Base, SDValue &OffImm) { 1283 // Match simple R + imm12 operands. 1284 1285 // Base only. 1286 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB && 1287 !CurDAG->isBaseWithConstantOffset(N)) { 1288 if (N.getOpcode() == ISD::FrameIndex) { 1289 // Match frame index. 1290 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 1291 Base = CurDAG->getTargetFrameIndex( 1292 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 1293 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32); 1294 return true; 1295 } 1296 1297 if (N.getOpcode() == ARMISD::Wrapper && 1298 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress && 1299 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol && 1300 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) { 1301 Base = N.getOperand(0); 1302 if (Base.getOpcode() == ISD::TargetConstantPool) 1303 return false; // We want to select t2LDRpci instead. 1304 } else 1305 Base = N; 1306 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32); 1307 return true; 1308 } 1309 1310 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) { 1311 if (SelectT2AddrModeImm8(N, Base, OffImm)) 1312 // Let t2LDRi8 handle (R - imm8). 1313 return false; 1314 1315 int RHSC = (int)RHS->getZExtValue(); 1316 if (N.getOpcode() == ISD::SUB) 1317 RHSC = -RHSC; 1318 1319 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned) 1320 Base = N.getOperand(0); 1321 if (Base.getOpcode() == ISD::FrameIndex) { 1322 int FI = cast<FrameIndexSDNode>(Base)->getIndex(); 1323 Base = CurDAG->getTargetFrameIndex( 1324 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 1325 } 1326 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32); 1327 return true; 1328 } 1329 } 1330 1331 // Base only. 1332 Base = N; 1333 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32); 1334 return true; 1335 } 1336 1337 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N, 1338 SDValue &Base, SDValue &OffImm) { 1339 // Match simple R - imm8 operands. 1340 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB && 1341 !CurDAG->isBaseWithConstantOffset(N)) 1342 return false; 1343 1344 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) { 1345 int RHSC = (int)RHS->getSExtValue(); 1346 if (N.getOpcode() == ISD::SUB) 1347 RHSC = -RHSC; 1348 1349 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative) 1350 Base = N.getOperand(0); 1351 if (Base.getOpcode() == ISD::FrameIndex) { 1352 int FI = cast<FrameIndexSDNode>(Base)->getIndex(); 1353 Base = CurDAG->getTargetFrameIndex( 1354 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 1355 } 1356 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32); 1357 return true; 1358 } 1359 } 1360 1361 return false; 1362 } 1363 1364 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N, 1365 SDValue &OffImm){ 1366 unsigned Opcode = Op->getOpcode(); 1367 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD) 1368 ? cast<LoadSDNode>(Op)->getAddressingMode() 1369 : cast<StoreSDNode>(Op)->getAddressingMode(); 1370 int RHSC; 1371 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x100, RHSC)) { // 8 bits. 1372 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC)) 1373 ? CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32) 1374 : CurDAG->getTargetConstant(-RHSC, SDLoc(N), MVT::i32); 1375 return true; 1376 } 1377 1378 return false; 1379 } 1380 1381 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N, 1382 SDValue &Base, 1383 SDValue &OffReg, SDValue &ShImm) { 1384 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12. 1385 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N)) 1386 return false; 1387 1388 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8. 1389 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) { 1390 int RHSC = (int)RHS->getZExtValue(); 1391 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned) 1392 return false; 1393 else if (RHSC < 0 && RHSC >= -255) // 8 bits 1394 return false; 1395 } 1396 1397 // Look for (R + R) or (R + (R << [1,2,3])). 1398 unsigned ShAmt = 0; 1399 Base = N.getOperand(0); 1400 OffReg = N.getOperand(1); 1401 1402 // Swap if it is ((R << c) + R). 1403 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg.getOpcode()); 1404 if (ShOpcVal != ARM_AM::lsl) { 1405 ShOpcVal = ARM_AM::getShiftOpcForNode(Base.getOpcode()); 1406 if (ShOpcVal == ARM_AM::lsl) 1407 std::swap(Base, OffReg); 1408 } 1409 1410 if (ShOpcVal == ARM_AM::lsl) { 1411 // Check to see if the RHS of the shift is a constant, if not, we can't fold 1412 // it. 1413 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) { 1414 ShAmt = Sh->getZExtValue(); 1415 if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt)) 1416 OffReg = OffReg.getOperand(0); 1417 else { 1418 ShAmt = 0; 1419 } 1420 } 1421 } 1422 1423 // If OffReg is a multiply-by-constant and it's profitable to extract a shift 1424 // and use it in a shifted operand do so. 1425 if (OffReg.getOpcode() == ISD::MUL && N.hasOneUse()) { 1426 unsigned PowerOfTwo = 0; 1427 SDValue NewMulConst; 1428 if (canExtractShiftFromMul(OffReg, 3, PowerOfTwo, NewMulConst)) { 1429 replaceDAGValue(OffReg.getOperand(1), NewMulConst); 1430 ShAmt = PowerOfTwo; 1431 } 1432 } 1433 1434 ShImm = CurDAG->getTargetConstant(ShAmt, SDLoc(N), MVT::i32); 1435 1436 return true; 1437 } 1438 1439 bool ARMDAGToDAGISel::SelectT2AddrModeExclusive(SDValue N, SDValue &Base, 1440 SDValue &OffImm) { 1441 // This *must* succeed since it's used for the irreplaceable ldrex and strex 1442 // instructions. 1443 Base = N; 1444 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32); 1445 1446 if (N.getOpcode() != ISD::ADD || !CurDAG->isBaseWithConstantOffset(N)) 1447 return true; 1448 1449 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1)); 1450 if (!RHS) 1451 return true; 1452 1453 uint32_t RHSC = (int)RHS->getZExtValue(); 1454 if (RHSC > 1020 || RHSC % 4 != 0) 1455 return true; 1456 1457 Base = N.getOperand(0); 1458 if (Base.getOpcode() == ISD::FrameIndex) { 1459 int FI = cast<FrameIndexSDNode>(Base)->getIndex(); 1460 Base = CurDAG->getTargetFrameIndex( 1461 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 1462 } 1463 1464 OffImm = CurDAG->getTargetConstant(RHSC/4, SDLoc(N), MVT::i32); 1465 return true; 1466 } 1467 1468 //===--------------------------------------------------------------------===// 1469 1470 /// getAL - Returns a ARMCC::AL immediate node. 1471 static inline SDValue getAL(SelectionDAG *CurDAG, const SDLoc &dl) { 1472 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, dl, MVT::i32); 1473 } 1474 1475 void ARMDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) { 1476 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); 1477 MemOp[0] = cast<MemSDNode>(N)->getMemOperand(); 1478 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1); 1479 } 1480 1481 bool ARMDAGToDAGISel::tryARMIndexedLoad(SDNode *N) { 1482 LoadSDNode *LD = cast<LoadSDNode>(N); 1483 ISD::MemIndexedMode AM = LD->getAddressingMode(); 1484 if (AM == ISD::UNINDEXED) 1485 return false; 1486 1487 EVT LoadedVT = LD->getMemoryVT(); 1488 SDValue Offset, AMOpc; 1489 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC); 1490 unsigned Opcode = 0; 1491 bool Match = false; 1492 if (LoadedVT == MVT::i32 && isPre && 1493 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) { 1494 Opcode = ARM::LDR_PRE_IMM; 1495 Match = true; 1496 } else if (LoadedVT == MVT::i32 && !isPre && 1497 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) { 1498 Opcode = ARM::LDR_POST_IMM; 1499 Match = true; 1500 } else if (LoadedVT == MVT::i32 && 1501 SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) { 1502 Opcode = isPre ? ARM::LDR_PRE_REG : ARM::LDR_POST_REG; 1503 Match = true; 1504 1505 } else if (LoadedVT == MVT::i16 && 1506 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) { 1507 Match = true; 1508 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD) 1509 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST) 1510 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST); 1511 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) { 1512 if (LD->getExtensionType() == ISD::SEXTLOAD) { 1513 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) { 1514 Match = true; 1515 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST; 1516 } 1517 } else { 1518 if (isPre && 1519 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) { 1520 Match = true; 1521 Opcode = ARM::LDRB_PRE_IMM; 1522 } else if (!isPre && 1523 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) { 1524 Match = true; 1525 Opcode = ARM::LDRB_POST_IMM; 1526 } else if (SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) { 1527 Match = true; 1528 Opcode = isPre ? ARM::LDRB_PRE_REG : ARM::LDRB_POST_REG; 1529 } 1530 } 1531 } 1532 1533 if (Match) { 1534 if (Opcode == ARM::LDR_PRE_IMM || Opcode == ARM::LDRB_PRE_IMM) { 1535 SDValue Chain = LD->getChain(); 1536 SDValue Base = LD->getBasePtr(); 1537 SDValue Ops[]= { Base, AMOpc, getAL(CurDAG, SDLoc(N)), 1538 CurDAG->getRegister(0, MVT::i32), Chain }; 1539 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32, 1540 MVT::Other, Ops); 1541 transferMemOperands(N, New); 1542 ReplaceNode(N, New); 1543 return true; 1544 } else { 1545 SDValue Chain = LD->getChain(); 1546 SDValue Base = LD->getBasePtr(); 1547 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG, SDLoc(N)), 1548 CurDAG->getRegister(0, MVT::i32), Chain }; 1549 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32, 1550 MVT::Other, Ops); 1551 transferMemOperands(N, New); 1552 ReplaceNode(N, New); 1553 return true; 1554 } 1555 } 1556 1557 return false; 1558 } 1559 1560 bool ARMDAGToDAGISel::tryT1IndexedLoad(SDNode *N) { 1561 LoadSDNode *LD = cast<LoadSDNode>(N); 1562 EVT LoadedVT = LD->getMemoryVT(); 1563 ISD::MemIndexedMode AM = LD->getAddressingMode(); 1564 if (AM == ISD::UNINDEXED || LD->getExtensionType() != ISD::NON_EXTLOAD || 1565 AM != ISD::POST_INC || LoadedVT.getSimpleVT().SimpleTy != MVT::i32) 1566 return false; 1567 1568 auto *COffs = dyn_cast<ConstantSDNode>(LD->getOffset()); 1569 if (!COffs || COffs->getZExtValue() != 4) 1570 return false; 1571 1572 // A T1 post-indexed load is just a single register LDM: LDM r0!, {r1}. 1573 // The encoding of LDM is not how the rest of ISel expects a post-inc load to 1574 // look however, so we use a pseudo here and switch it for a tLDMIA_UPD after 1575 // ISel. 1576 SDValue Chain = LD->getChain(); 1577 SDValue Base = LD->getBasePtr(); 1578 SDValue Ops[]= { Base, getAL(CurDAG, SDLoc(N)), 1579 CurDAG->getRegister(0, MVT::i32), Chain }; 1580 SDNode *New = CurDAG->getMachineNode(ARM::tLDR_postidx, SDLoc(N), MVT::i32, 1581 MVT::i32, MVT::Other, Ops); 1582 transferMemOperands(N, New); 1583 ReplaceNode(N, New); 1584 return true; 1585 } 1586 1587 bool ARMDAGToDAGISel::tryT2IndexedLoad(SDNode *N) { 1588 LoadSDNode *LD = cast<LoadSDNode>(N); 1589 ISD::MemIndexedMode AM = LD->getAddressingMode(); 1590 if (AM == ISD::UNINDEXED) 1591 return false; 1592 1593 EVT LoadedVT = LD->getMemoryVT(); 1594 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD; 1595 SDValue Offset; 1596 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC); 1597 unsigned Opcode = 0; 1598 bool Match = false; 1599 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) { 1600 switch (LoadedVT.getSimpleVT().SimpleTy) { 1601 case MVT::i32: 1602 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST; 1603 break; 1604 case MVT::i16: 1605 if (isSExtLd) 1606 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST; 1607 else 1608 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST; 1609 break; 1610 case MVT::i8: 1611 case MVT::i1: 1612 if (isSExtLd) 1613 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST; 1614 else 1615 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST; 1616 break; 1617 default: 1618 return false; 1619 } 1620 Match = true; 1621 } 1622 1623 if (Match) { 1624 SDValue Chain = LD->getChain(); 1625 SDValue Base = LD->getBasePtr(); 1626 SDValue Ops[]= { Base, Offset, getAL(CurDAG, SDLoc(N)), 1627 CurDAG->getRegister(0, MVT::i32), Chain }; 1628 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32, 1629 MVT::Other, Ops); 1630 transferMemOperands(N, New); 1631 ReplaceNode(N, New); 1632 return true; 1633 } 1634 1635 return false; 1636 } 1637 1638 /// \brief Form a GPRPair pseudo register from a pair of GPR regs. 1639 SDNode *ARMDAGToDAGISel::createGPRPairNode(EVT VT, SDValue V0, SDValue V1) { 1640 SDLoc dl(V0.getNode()); 1641 SDValue RegClass = 1642 CurDAG->getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32); 1643 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32); 1644 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32); 1645 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 }; 1646 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops); 1647 } 1648 1649 /// \brief Form a D register from a pair of S registers. 1650 SDNode *ARMDAGToDAGISel::createSRegPairNode(EVT VT, SDValue V0, SDValue V1) { 1651 SDLoc dl(V0.getNode()); 1652 SDValue RegClass = 1653 CurDAG->getTargetConstant(ARM::DPR_VFP2RegClassID, dl, MVT::i32); 1654 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32); 1655 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32); 1656 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 }; 1657 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops); 1658 } 1659 1660 /// \brief Form a quad register from a pair of D registers. 1661 SDNode *ARMDAGToDAGISel::createDRegPairNode(EVT VT, SDValue V0, SDValue V1) { 1662 SDLoc dl(V0.getNode()); 1663 SDValue RegClass = CurDAG->getTargetConstant(ARM::QPRRegClassID, dl, 1664 MVT::i32); 1665 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32); 1666 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32); 1667 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 }; 1668 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops); 1669 } 1670 1671 /// \brief Form 4 consecutive D registers from a pair of Q registers. 1672 SDNode *ARMDAGToDAGISel::createQRegPairNode(EVT VT, SDValue V0, SDValue V1) { 1673 SDLoc dl(V0.getNode()); 1674 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl, 1675 MVT::i32); 1676 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32); 1677 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32); 1678 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 }; 1679 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops); 1680 } 1681 1682 /// \brief Form 4 consecutive S registers. 1683 SDNode *ARMDAGToDAGISel::createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1, 1684 SDValue V2, SDValue V3) { 1685 SDLoc dl(V0.getNode()); 1686 SDValue RegClass = 1687 CurDAG->getTargetConstant(ARM::QPR_VFP2RegClassID, dl, MVT::i32); 1688 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32); 1689 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32); 1690 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, dl, MVT::i32); 1691 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, dl, MVT::i32); 1692 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1, 1693 V2, SubReg2, V3, SubReg3 }; 1694 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops); 1695 } 1696 1697 /// \brief Form 4 consecutive D registers. 1698 SDNode *ARMDAGToDAGISel::createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1, 1699 SDValue V2, SDValue V3) { 1700 SDLoc dl(V0.getNode()); 1701 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl, 1702 MVT::i32); 1703 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32); 1704 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32); 1705 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, dl, MVT::i32); 1706 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, dl, MVT::i32); 1707 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1, 1708 V2, SubReg2, V3, SubReg3 }; 1709 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops); 1710 } 1711 1712 /// \brief Form 4 consecutive Q registers. 1713 SDNode *ARMDAGToDAGISel::createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1, 1714 SDValue V2, SDValue V3) { 1715 SDLoc dl(V0.getNode()); 1716 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQQQPRRegClassID, dl, 1717 MVT::i32); 1718 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32); 1719 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32); 1720 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, dl, MVT::i32); 1721 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, dl, MVT::i32); 1722 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1, 1723 V2, SubReg2, V3, SubReg3 }; 1724 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops); 1725 } 1726 1727 /// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand 1728 /// of a NEON VLD or VST instruction. The supported values depend on the 1729 /// number of registers being loaded. 1730 SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, const SDLoc &dl, 1731 unsigned NumVecs, bool is64BitVector) { 1732 unsigned NumRegs = NumVecs; 1733 if (!is64BitVector && NumVecs < 3) 1734 NumRegs *= 2; 1735 1736 unsigned Alignment = cast<ConstantSDNode>(Align)->getZExtValue(); 1737 if (Alignment >= 32 && NumRegs == 4) 1738 Alignment = 32; 1739 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4)) 1740 Alignment = 16; 1741 else if (Alignment >= 8) 1742 Alignment = 8; 1743 else 1744 Alignment = 0; 1745 1746 return CurDAG->getTargetConstant(Alignment, dl, MVT::i32); 1747 } 1748 1749 static bool isVLDfixed(unsigned Opc) 1750 { 1751 switch (Opc) { 1752 default: return false; 1753 case ARM::VLD1d8wb_fixed : return true; 1754 case ARM::VLD1d16wb_fixed : return true; 1755 case ARM::VLD1d64Qwb_fixed : return true; 1756 case ARM::VLD1d32wb_fixed : return true; 1757 case ARM::VLD1d64wb_fixed : return true; 1758 case ARM::VLD1d64TPseudoWB_fixed : return true; 1759 case ARM::VLD1d64QPseudoWB_fixed : return true; 1760 case ARM::VLD1q8wb_fixed : return true; 1761 case ARM::VLD1q16wb_fixed : return true; 1762 case ARM::VLD1q32wb_fixed : return true; 1763 case ARM::VLD1q64wb_fixed : return true; 1764 case ARM::VLD2d8wb_fixed : return true; 1765 case ARM::VLD2d16wb_fixed : return true; 1766 case ARM::VLD2d32wb_fixed : return true; 1767 case ARM::VLD2q8PseudoWB_fixed : return true; 1768 case ARM::VLD2q16PseudoWB_fixed : return true; 1769 case ARM::VLD2q32PseudoWB_fixed : return true; 1770 case ARM::VLD2DUPd8wb_fixed : return true; 1771 case ARM::VLD2DUPd16wb_fixed : return true; 1772 case ARM::VLD2DUPd32wb_fixed : return true; 1773 } 1774 } 1775 1776 static bool isVSTfixed(unsigned Opc) 1777 { 1778 switch (Opc) { 1779 default: return false; 1780 case ARM::VST1d8wb_fixed : return true; 1781 case ARM::VST1d16wb_fixed : return true; 1782 case ARM::VST1d32wb_fixed : return true; 1783 case ARM::VST1d64wb_fixed : return true; 1784 case ARM::VST1q8wb_fixed : return true; 1785 case ARM::VST1q16wb_fixed : return true; 1786 case ARM::VST1q32wb_fixed : return true; 1787 case ARM::VST1q64wb_fixed : return true; 1788 case ARM::VST1d64TPseudoWB_fixed : return true; 1789 case ARM::VST1d64QPseudoWB_fixed : return true; 1790 case ARM::VST2d8wb_fixed : return true; 1791 case ARM::VST2d16wb_fixed : return true; 1792 case ARM::VST2d32wb_fixed : return true; 1793 case ARM::VST2q8PseudoWB_fixed : return true; 1794 case ARM::VST2q16PseudoWB_fixed : return true; 1795 case ARM::VST2q32PseudoWB_fixed : return true; 1796 } 1797 } 1798 1799 // Get the register stride update opcode of a VLD/VST instruction that 1800 // is otherwise equivalent to the given fixed stride updating instruction. 1801 static unsigned getVLDSTRegisterUpdateOpcode(unsigned Opc) { 1802 assert((isVLDfixed(Opc) || isVSTfixed(Opc)) 1803 && "Incorrect fixed stride updating instruction."); 1804 switch (Opc) { 1805 default: break; 1806 case ARM::VLD1d8wb_fixed: return ARM::VLD1d8wb_register; 1807 case ARM::VLD1d16wb_fixed: return ARM::VLD1d16wb_register; 1808 case ARM::VLD1d32wb_fixed: return ARM::VLD1d32wb_register; 1809 case ARM::VLD1d64wb_fixed: return ARM::VLD1d64wb_register; 1810 case ARM::VLD1q8wb_fixed: return ARM::VLD1q8wb_register; 1811 case ARM::VLD1q16wb_fixed: return ARM::VLD1q16wb_register; 1812 case ARM::VLD1q32wb_fixed: return ARM::VLD1q32wb_register; 1813 case ARM::VLD1q64wb_fixed: return ARM::VLD1q64wb_register; 1814 case ARM::VLD1d64Twb_fixed: return ARM::VLD1d64Twb_register; 1815 case ARM::VLD1d64Qwb_fixed: return ARM::VLD1d64Qwb_register; 1816 case ARM::VLD1d64TPseudoWB_fixed: return ARM::VLD1d64TPseudoWB_register; 1817 case ARM::VLD1d64QPseudoWB_fixed: return ARM::VLD1d64QPseudoWB_register; 1818 1819 case ARM::VST1d8wb_fixed: return ARM::VST1d8wb_register; 1820 case ARM::VST1d16wb_fixed: return ARM::VST1d16wb_register; 1821 case ARM::VST1d32wb_fixed: return ARM::VST1d32wb_register; 1822 case ARM::VST1d64wb_fixed: return ARM::VST1d64wb_register; 1823 case ARM::VST1q8wb_fixed: return ARM::VST1q8wb_register; 1824 case ARM::VST1q16wb_fixed: return ARM::VST1q16wb_register; 1825 case ARM::VST1q32wb_fixed: return ARM::VST1q32wb_register; 1826 case ARM::VST1q64wb_fixed: return ARM::VST1q64wb_register; 1827 case ARM::VST1d64TPseudoWB_fixed: return ARM::VST1d64TPseudoWB_register; 1828 case ARM::VST1d64QPseudoWB_fixed: return ARM::VST1d64QPseudoWB_register; 1829 1830 case ARM::VLD2d8wb_fixed: return ARM::VLD2d8wb_register; 1831 case ARM::VLD2d16wb_fixed: return ARM::VLD2d16wb_register; 1832 case ARM::VLD2d32wb_fixed: return ARM::VLD2d32wb_register; 1833 case ARM::VLD2q8PseudoWB_fixed: return ARM::VLD2q8PseudoWB_register; 1834 case ARM::VLD2q16PseudoWB_fixed: return ARM::VLD2q16PseudoWB_register; 1835 case ARM::VLD2q32PseudoWB_fixed: return ARM::VLD2q32PseudoWB_register; 1836 1837 case ARM::VST2d8wb_fixed: return ARM::VST2d8wb_register; 1838 case ARM::VST2d16wb_fixed: return ARM::VST2d16wb_register; 1839 case ARM::VST2d32wb_fixed: return ARM::VST2d32wb_register; 1840 case ARM::VST2q8PseudoWB_fixed: return ARM::VST2q8PseudoWB_register; 1841 case ARM::VST2q16PseudoWB_fixed: return ARM::VST2q16PseudoWB_register; 1842 case ARM::VST2q32PseudoWB_fixed: return ARM::VST2q32PseudoWB_register; 1843 1844 case ARM::VLD2DUPd8wb_fixed: return ARM::VLD2DUPd8wb_register; 1845 case ARM::VLD2DUPd16wb_fixed: return ARM::VLD2DUPd16wb_register; 1846 case ARM::VLD2DUPd32wb_fixed: return ARM::VLD2DUPd32wb_register; 1847 } 1848 return Opc; // If not one we handle, return it unchanged. 1849 } 1850 1851 void ARMDAGToDAGISel::SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs, 1852 const uint16_t *DOpcodes, 1853 const uint16_t *QOpcodes0, 1854 const uint16_t *QOpcodes1) { 1855 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range"); 1856 SDLoc dl(N); 1857 1858 SDValue MemAddr, Align; 1859 unsigned AddrOpIdx = isUpdating ? 1 : 2; 1860 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align)) 1861 return; 1862 1863 SDValue Chain = N->getOperand(0); 1864 EVT VT = N->getValueType(0); 1865 bool is64BitVector = VT.is64BitVector(); 1866 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector); 1867 1868 unsigned OpcodeIndex; 1869 switch (VT.getSimpleVT().SimpleTy) { 1870 default: llvm_unreachable("unhandled vld type"); 1871 // Double-register operations: 1872 case MVT::v8i8: OpcodeIndex = 0; break; 1873 case MVT::v4i16: OpcodeIndex = 1; break; 1874 case MVT::v2f32: 1875 case MVT::v2i32: OpcodeIndex = 2; break; 1876 case MVT::v1i64: OpcodeIndex = 3; break; 1877 // Quad-register operations: 1878 case MVT::v16i8: OpcodeIndex = 0; break; 1879 case MVT::v8i16: OpcodeIndex = 1; break; 1880 case MVT::v4f32: 1881 case MVT::v4i32: OpcodeIndex = 2; break; 1882 case MVT::v2f64: 1883 case MVT::v2i64: OpcodeIndex = 3; 1884 assert(NumVecs == 1 && "v2i64 type only supported for VLD1"); 1885 break; 1886 } 1887 1888 EVT ResTy; 1889 if (NumVecs == 1) 1890 ResTy = VT; 1891 else { 1892 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs; 1893 if (!is64BitVector) 1894 ResTyElts *= 2; 1895 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts); 1896 } 1897 std::vector<EVT> ResTys; 1898 ResTys.push_back(ResTy); 1899 if (isUpdating) 1900 ResTys.push_back(MVT::i32); 1901 ResTys.push_back(MVT::Other); 1902 1903 SDValue Pred = getAL(CurDAG, dl); 1904 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); 1905 SDNode *VLd; 1906 SmallVector<SDValue, 7> Ops; 1907 1908 // Double registers and VLD1/VLD2 quad registers are directly supported. 1909 if (is64BitVector || NumVecs <= 2) { 1910 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] : 1911 QOpcodes0[OpcodeIndex]); 1912 Ops.push_back(MemAddr); 1913 Ops.push_back(Align); 1914 if (isUpdating) { 1915 SDValue Inc = N->getOperand(AddrOpIdx + 1); 1916 // FIXME: VLD1/VLD2 fixed increment doesn't need Reg0. Remove the reg0 1917 // case entirely when the rest are updated to that form, too. 1918 if ((NumVecs <= 2) && !isa<ConstantSDNode>(Inc.getNode())) 1919 Opc = getVLDSTRegisterUpdateOpcode(Opc); 1920 // FIXME: We use a VLD1 for v1i64 even if the pseudo says vld2/3/4, so 1921 // check for that explicitly too. Horribly hacky, but temporary. 1922 if ((NumVecs > 2 && !isVLDfixed(Opc)) || 1923 !isa<ConstantSDNode>(Inc.getNode())) 1924 Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc); 1925 } 1926 Ops.push_back(Pred); 1927 Ops.push_back(Reg0); 1928 Ops.push_back(Chain); 1929 VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops); 1930 1931 } else { 1932 // Otherwise, quad registers are loaded with two separate instructions, 1933 // where one loads the even registers and the other loads the odd registers. 1934 EVT AddrTy = MemAddr.getValueType(); 1935 1936 // Load the even subregs. This is always an updating load, so that it 1937 // provides the address to the second load for the odd subregs. 1938 SDValue ImplDef = 1939 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0); 1940 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain }; 1941 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl, 1942 ResTy, AddrTy, MVT::Other, OpsA); 1943 Chain = SDValue(VLdA, 2); 1944 1945 // Load the odd subregs. 1946 Ops.push_back(SDValue(VLdA, 1)); 1947 Ops.push_back(Align); 1948 if (isUpdating) { 1949 SDValue Inc = N->getOperand(AddrOpIdx + 1); 1950 assert(isa<ConstantSDNode>(Inc.getNode()) && 1951 "only constant post-increment update allowed for VLD3/4"); 1952 (void)Inc; 1953 Ops.push_back(Reg0); 1954 } 1955 Ops.push_back(SDValue(VLdA, 0)); 1956 Ops.push_back(Pred); 1957 Ops.push_back(Reg0); 1958 Ops.push_back(Chain); 1959 VLd = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, Ops); 1960 } 1961 1962 // Transfer memoperands. 1963 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); 1964 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand(); 1965 cast<MachineSDNode>(VLd)->setMemRefs(MemOp, MemOp + 1); 1966 1967 if (NumVecs == 1) { 1968 ReplaceNode(N, VLd); 1969 return; 1970 } 1971 1972 // Extract out the subregisters. 1973 SDValue SuperReg = SDValue(VLd, 0); 1974 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 && 1975 ARM::qsub_3 == ARM::qsub_0 + 3, 1976 "Unexpected subreg numbering"); 1977 unsigned Sub0 = (is64BitVector ? ARM::dsub_0 : ARM::qsub_0); 1978 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) 1979 ReplaceUses(SDValue(N, Vec), 1980 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg)); 1981 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1)); 1982 if (isUpdating) 1983 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLd, 2)); 1984 CurDAG->RemoveDeadNode(N); 1985 } 1986 1987 void ARMDAGToDAGISel::SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs, 1988 const uint16_t *DOpcodes, 1989 const uint16_t *QOpcodes0, 1990 const uint16_t *QOpcodes1) { 1991 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range"); 1992 SDLoc dl(N); 1993 1994 SDValue MemAddr, Align; 1995 unsigned AddrOpIdx = isUpdating ? 1 : 2; 1996 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1) 1997 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align)) 1998 return; 1999 2000 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); 2001 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand(); 2002 2003 SDValue Chain = N->getOperand(0); 2004 EVT VT = N->getOperand(Vec0Idx).getValueType(); 2005 bool is64BitVector = VT.is64BitVector(); 2006 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector); 2007 2008 unsigned OpcodeIndex; 2009 switch (VT.getSimpleVT().SimpleTy) { 2010 default: llvm_unreachable("unhandled vst type"); 2011 // Double-register operations: 2012 case MVT::v8i8: OpcodeIndex = 0; break; 2013 case MVT::v4i16: OpcodeIndex = 1; break; 2014 case MVT::v2f32: 2015 case MVT::v2i32: OpcodeIndex = 2; break; 2016 case MVT::v1i64: OpcodeIndex = 3; break; 2017 // Quad-register operations: 2018 case MVT::v16i8: OpcodeIndex = 0; break; 2019 case MVT::v8i16: OpcodeIndex = 1; break; 2020 case MVT::v4f32: 2021 case MVT::v4i32: OpcodeIndex = 2; break; 2022 case MVT::v2f64: 2023 case MVT::v2i64: OpcodeIndex = 3; 2024 assert(NumVecs == 1 && "v2i64 type only supported for VST1"); 2025 break; 2026 } 2027 2028 std::vector<EVT> ResTys; 2029 if (isUpdating) 2030 ResTys.push_back(MVT::i32); 2031 ResTys.push_back(MVT::Other); 2032 2033 SDValue Pred = getAL(CurDAG, dl); 2034 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); 2035 SmallVector<SDValue, 7> Ops; 2036 2037 // Double registers and VST1/VST2 quad registers are directly supported. 2038 if (is64BitVector || NumVecs <= 2) { 2039 SDValue SrcReg; 2040 if (NumVecs == 1) { 2041 SrcReg = N->getOperand(Vec0Idx); 2042 } else if (is64BitVector) { 2043 // Form a REG_SEQUENCE to force register allocation. 2044 SDValue V0 = N->getOperand(Vec0Idx + 0); 2045 SDValue V1 = N->getOperand(Vec0Idx + 1); 2046 if (NumVecs == 2) 2047 SrcReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0); 2048 else { 2049 SDValue V2 = N->getOperand(Vec0Idx + 2); 2050 // If it's a vst3, form a quad D-register and leave the last part as 2051 // an undef. 2052 SDValue V3 = (NumVecs == 3) 2053 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0) 2054 : N->getOperand(Vec0Idx + 3); 2055 SrcReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0); 2056 } 2057 } else { 2058 // Form a QQ register. 2059 SDValue Q0 = N->getOperand(Vec0Idx); 2060 SDValue Q1 = N->getOperand(Vec0Idx + 1); 2061 SrcReg = SDValue(createQRegPairNode(MVT::v4i64, Q0, Q1), 0); 2062 } 2063 2064 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] : 2065 QOpcodes0[OpcodeIndex]); 2066 Ops.push_back(MemAddr); 2067 Ops.push_back(Align); 2068 if (isUpdating) { 2069 SDValue Inc = N->getOperand(AddrOpIdx + 1); 2070 // FIXME: VST1/VST2 fixed increment doesn't need Reg0. Remove the reg0 2071 // case entirely when the rest are updated to that form, too. 2072 if (NumVecs <= 2 && !isa<ConstantSDNode>(Inc.getNode())) 2073 Opc = getVLDSTRegisterUpdateOpcode(Opc); 2074 // FIXME: We use a VST1 for v1i64 even if the pseudo says vld2/3/4, so 2075 // check for that explicitly too. Horribly hacky, but temporary. 2076 if (!isa<ConstantSDNode>(Inc.getNode())) 2077 Ops.push_back(Inc); 2078 else if (NumVecs > 2 && !isVSTfixed(Opc)) 2079 Ops.push_back(Reg0); 2080 } 2081 Ops.push_back(SrcReg); 2082 Ops.push_back(Pred); 2083 Ops.push_back(Reg0); 2084 Ops.push_back(Chain); 2085 SDNode *VSt = CurDAG->getMachineNode(Opc, dl, ResTys, Ops); 2086 2087 // Transfer memoperands. 2088 cast<MachineSDNode>(VSt)->setMemRefs(MemOp, MemOp + 1); 2089 2090 ReplaceNode(N, VSt); 2091 return; 2092 } 2093 2094 // Otherwise, quad registers are stored with two separate instructions, 2095 // where one stores the even registers and the other stores the odd registers. 2096 2097 // Form the QQQQ REG_SEQUENCE. 2098 SDValue V0 = N->getOperand(Vec0Idx + 0); 2099 SDValue V1 = N->getOperand(Vec0Idx + 1); 2100 SDValue V2 = N->getOperand(Vec0Idx + 2); 2101 SDValue V3 = (NumVecs == 3) 2102 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0) 2103 : N->getOperand(Vec0Idx + 3); 2104 SDValue RegSeq = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0); 2105 2106 // Store the even D registers. This is always an updating store, so that it 2107 // provides the address to the second store for the odd subregs. 2108 const SDValue OpsA[] = { MemAddr, Align, Reg0, RegSeq, Pred, Reg0, Chain }; 2109 SDNode *VStA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl, 2110 MemAddr.getValueType(), 2111 MVT::Other, OpsA); 2112 cast<MachineSDNode>(VStA)->setMemRefs(MemOp, MemOp + 1); 2113 Chain = SDValue(VStA, 1); 2114 2115 // Store the odd D registers. 2116 Ops.push_back(SDValue(VStA, 0)); 2117 Ops.push_back(Align); 2118 if (isUpdating) { 2119 SDValue Inc = N->getOperand(AddrOpIdx + 1); 2120 assert(isa<ConstantSDNode>(Inc.getNode()) && 2121 "only constant post-increment update allowed for VST3/4"); 2122 (void)Inc; 2123 Ops.push_back(Reg0); 2124 } 2125 Ops.push_back(RegSeq); 2126 Ops.push_back(Pred); 2127 Ops.push_back(Reg0); 2128 Ops.push_back(Chain); 2129 SDNode *VStB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, 2130 Ops); 2131 cast<MachineSDNode>(VStB)->setMemRefs(MemOp, MemOp + 1); 2132 ReplaceNode(N, VStB); 2133 } 2134 2135 void ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating, 2136 unsigned NumVecs, 2137 const uint16_t *DOpcodes, 2138 const uint16_t *QOpcodes) { 2139 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range"); 2140 SDLoc dl(N); 2141 2142 SDValue MemAddr, Align; 2143 unsigned AddrOpIdx = isUpdating ? 1 : 2; 2144 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1) 2145 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align)) 2146 return; 2147 2148 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); 2149 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand(); 2150 2151 SDValue Chain = N->getOperand(0); 2152 unsigned Lane = 2153 cast<ConstantSDNode>(N->getOperand(Vec0Idx + NumVecs))->getZExtValue(); 2154 EVT VT = N->getOperand(Vec0Idx).getValueType(); 2155 bool is64BitVector = VT.is64BitVector(); 2156 2157 unsigned Alignment = 0; 2158 if (NumVecs != 3) { 2159 Alignment = cast<ConstantSDNode>(Align)->getZExtValue(); 2160 unsigned NumBytes = NumVecs * VT.getScalarSizeInBits() / 8; 2161 if (Alignment > NumBytes) 2162 Alignment = NumBytes; 2163 if (Alignment < 8 && Alignment < NumBytes) 2164 Alignment = 0; 2165 // Alignment must be a power of two; make sure of that. 2166 Alignment = (Alignment & -Alignment); 2167 if (Alignment == 1) 2168 Alignment = 0; 2169 } 2170 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32); 2171 2172 unsigned OpcodeIndex; 2173 switch (VT.getSimpleVT().SimpleTy) { 2174 default: llvm_unreachable("unhandled vld/vst lane type"); 2175 // Double-register operations: 2176 case MVT::v8i8: OpcodeIndex = 0; break; 2177 case MVT::v4i16: OpcodeIndex = 1; break; 2178 case MVT::v2f32: 2179 case MVT::v2i32: OpcodeIndex = 2; break; 2180 // Quad-register operations: 2181 case MVT::v8i16: OpcodeIndex = 0; break; 2182 case MVT::v4f32: 2183 case MVT::v4i32: OpcodeIndex = 1; break; 2184 } 2185 2186 std::vector<EVT> ResTys; 2187 if (IsLoad) { 2188 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs; 2189 if (!is64BitVector) 2190 ResTyElts *= 2; 2191 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(), 2192 MVT::i64, ResTyElts)); 2193 } 2194 if (isUpdating) 2195 ResTys.push_back(MVT::i32); 2196 ResTys.push_back(MVT::Other); 2197 2198 SDValue Pred = getAL(CurDAG, dl); 2199 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); 2200 2201 SmallVector<SDValue, 8> Ops; 2202 Ops.push_back(MemAddr); 2203 Ops.push_back(Align); 2204 if (isUpdating) { 2205 SDValue Inc = N->getOperand(AddrOpIdx + 1); 2206 Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc); 2207 } 2208 2209 SDValue SuperReg; 2210 SDValue V0 = N->getOperand(Vec0Idx + 0); 2211 SDValue V1 = N->getOperand(Vec0Idx + 1); 2212 if (NumVecs == 2) { 2213 if (is64BitVector) 2214 SuperReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0); 2215 else 2216 SuperReg = SDValue(createQRegPairNode(MVT::v4i64, V0, V1), 0); 2217 } else { 2218 SDValue V2 = N->getOperand(Vec0Idx + 2); 2219 SDValue V3 = (NumVecs == 3) 2220 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0) 2221 : N->getOperand(Vec0Idx + 3); 2222 if (is64BitVector) 2223 SuperReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0); 2224 else 2225 SuperReg = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0); 2226 } 2227 Ops.push_back(SuperReg); 2228 Ops.push_back(getI32Imm(Lane, dl)); 2229 Ops.push_back(Pred); 2230 Ops.push_back(Reg0); 2231 Ops.push_back(Chain); 2232 2233 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] : 2234 QOpcodes[OpcodeIndex]); 2235 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTys, Ops); 2236 cast<MachineSDNode>(VLdLn)->setMemRefs(MemOp, MemOp + 1); 2237 if (!IsLoad) { 2238 ReplaceNode(N, VLdLn); 2239 return; 2240 } 2241 2242 // Extract the subregisters. 2243 SuperReg = SDValue(VLdLn, 0); 2244 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 && 2245 ARM::qsub_3 == ARM::qsub_0 + 3, 2246 "Unexpected subreg numbering"); 2247 unsigned Sub0 = is64BitVector ? ARM::dsub_0 : ARM::qsub_0; 2248 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) 2249 ReplaceUses(SDValue(N, Vec), 2250 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg)); 2251 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdLn, 1)); 2252 if (isUpdating) 2253 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdLn, 2)); 2254 CurDAG->RemoveDeadNode(N); 2255 } 2256 2257 void ARMDAGToDAGISel::SelectVLDDup(SDNode *N, bool isUpdating, unsigned NumVecs, 2258 const uint16_t *Opcodes) { 2259 assert(NumVecs >=2 && NumVecs <= 4 && "VLDDup NumVecs out-of-range"); 2260 SDLoc dl(N); 2261 2262 SDValue MemAddr, Align; 2263 if (!SelectAddrMode6(N, N->getOperand(1), MemAddr, Align)) 2264 return; 2265 2266 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); 2267 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand(); 2268 2269 SDValue Chain = N->getOperand(0); 2270 EVT VT = N->getValueType(0); 2271 2272 unsigned Alignment = 0; 2273 if (NumVecs != 3) { 2274 Alignment = cast<ConstantSDNode>(Align)->getZExtValue(); 2275 unsigned NumBytes = NumVecs * VT.getScalarSizeInBits() / 8; 2276 if (Alignment > NumBytes) 2277 Alignment = NumBytes; 2278 if (Alignment < 8 && Alignment < NumBytes) 2279 Alignment = 0; 2280 // Alignment must be a power of two; make sure of that. 2281 Alignment = (Alignment & -Alignment); 2282 if (Alignment == 1) 2283 Alignment = 0; 2284 } 2285 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32); 2286 2287 unsigned OpcodeIndex; 2288 switch (VT.getSimpleVT().SimpleTy) { 2289 default: llvm_unreachable("unhandled vld-dup type"); 2290 case MVT::v8i8: OpcodeIndex = 0; break; 2291 case MVT::v4i16: OpcodeIndex = 1; break; 2292 case MVT::v2f32: 2293 case MVT::v2i32: OpcodeIndex = 2; break; 2294 } 2295 2296 SDValue Pred = getAL(CurDAG, dl); 2297 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); 2298 SDValue SuperReg; 2299 unsigned Opc = Opcodes[OpcodeIndex]; 2300 SmallVector<SDValue, 6> Ops; 2301 Ops.push_back(MemAddr); 2302 Ops.push_back(Align); 2303 if (isUpdating) { 2304 // fixed-stride update instructions don't have an explicit writeback 2305 // operand. It's implicit in the opcode itself. 2306 SDValue Inc = N->getOperand(2); 2307 if (!isa<ConstantSDNode>(Inc.getNode())) 2308 Ops.push_back(Inc); 2309 // FIXME: VLD3 and VLD4 haven't been updated to that form yet. 2310 else if (NumVecs > 2) 2311 Ops.push_back(Reg0); 2312 } 2313 Ops.push_back(Pred); 2314 Ops.push_back(Reg0); 2315 Ops.push_back(Chain); 2316 2317 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs; 2318 std::vector<EVT> ResTys; 2319 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(), MVT::i64,ResTyElts)); 2320 if (isUpdating) 2321 ResTys.push_back(MVT::i32); 2322 ResTys.push_back(MVT::Other); 2323 SDNode *VLdDup = CurDAG->getMachineNode(Opc, dl, ResTys, Ops); 2324 cast<MachineSDNode>(VLdDup)->setMemRefs(MemOp, MemOp + 1); 2325 SuperReg = SDValue(VLdDup, 0); 2326 2327 // Extract the subregisters. 2328 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7, "Unexpected subreg numbering"); 2329 unsigned SubIdx = ARM::dsub_0; 2330 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) 2331 ReplaceUses(SDValue(N, Vec), 2332 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg)); 2333 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdDup, 1)); 2334 if (isUpdating) 2335 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdDup, 2)); 2336 CurDAG->RemoveDeadNode(N); 2337 } 2338 2339 void ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, 2340 unsigned Opc) { 2341 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range"); 2342 SDLoc dl(N); 2343 EVT VT = N->getValueType(0); 2344 unsigned FirstTblReg = IsExt ? 2 : 1; 2345 2346 // Form a REG_SEQUENCE to force register allocation. 2347 SDValue RegSeq; 2348 SDValue V0 = N->getOperand(FirstTblReg + 0); 2349 SDValue V1 = N->getOperand(FirstTblReg + 1); 2350 if (NumVecs == 2) 2351 RegSeq = SDValue(createDRegPairNode(MVT::v16i8, V0, V1), 0); 2352 else { 2353 SDValue V2 = N->getOperand(FirstTblReg + 2); 2354 // If it's a vtbl3, form a quad D-register and leave the last part as 2355 // an undef. 2356 SDValue V3 = (NumVecs == 3) 2357 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0) 2358 : N->getOperand(FirstTblReg + 3); 2359 RegSeq = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0); 2360 } 2361 2362 SmallVector<SDValue, 6> Ops; 2363 if (IsExt) 2364 Ops.push_back(N->getOperand(1)); 2365 Ops.push_back(RegSeq); 2366 Ops.push_back(N->getOperand(FirstTblReg + NumVecs)); 2367 Ops.push_back(getAL(CurDAG, dl)); // predicate 2368 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register 2369 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops)); 2370 } 2371 2372 bool ARMDAGToDAGISel::tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned) { 2373 if (!Subtarget->hasV6T2Ops()) 2374 return false; 2375 2376 unsigned Opc = isSigned 2377 ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX) 2378 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX); 2379 SDLoc dl(N); 2380 2381 // For unsigned extracts, check for a shift right and mask 2382 unsigned And_imm = 0; 2383 if (N->getOpcode() == ISD::AND) { 2384 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) { 2385 2386 // The immediate is a mask of the low bits iff imm & (imm+1) == 0 2387 if (And_imm & (And_imm + 1)) 2388 return false; 2389 2390 unsigned Srl_imm = 0; 2391 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL, 2392 Srl_imm)) { 2393 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!"); 2394 2395 // Note: The width operand is encoded as width-1. 2396 unsigned Width = countTrailingOnes(And_imm) - 1; 2397 unsigned LSB = Srl_imm; 2398 2399 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); 2400 2401 if ((LSB + Width + 1) == N->getValueType(0).getSizeInBits()) { 2402 // It's cheaper to use a right shift to extract the top bits. 2403 if (Subtarget->isThumb()) { 2404 Opc = isSigned ? ARM::t2ASRri : ARM::t2LSRri; 2405 SDValue Ops[] = { N->getOperand(0).getOperand(0), 2406 CurDAG->getTargetConstant(LSB, dl, MVT::i32), 2407 getAL(CurDAG, dl), Reg0, Reg0 }; 2408 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops); 2409 return true; 2410 } 2411 2412 // ARM models shift instructions as MOVsi with shifter operand. 2413 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(ISD::SRL); 2414 SDValue ShOpc = 2415 CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, LSB), dl, 2416 MVT::i32); 2417 SDValue Ops[] = { N->getOperand(0).getOperand(0), ShOpc, 2418 getAL(CurDAG, dl), Reg0, Reg0 }; 2419 CurDAG->SelectNodeTo(N, ARM::MOVsi, MVT::i32, Ops); 2420 return true; 2421 } 2422 2423 SDValue Ops[] = { N->getOperand(0).getOperand(0), 2424 CurDAG->getTargetConstant(LSB, dl, MVT::i32), 2425 CurDAG->getTargetConstant(Width, dl, MVT::i32), 2426 getAL(CurDAG, dl), Reg0 }; 2427 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops); 2428 return true; 2429 } 2430 } 2431 return false; 2432 } 2433 2434 // Otherwise, we're looking for a shift of a shift 2435 unsigned Shl_imm = 0; 2436 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) { 2437 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!"); 2438 unsigned Srl_imm = 0; 2439 if (isInt32Immediate(N->getOperand(1), Srl_imm)) { 2440 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!"); 2441 // Note: The width operand is encoded as width-1. 2442 unsigned Width = 32 - Srl_imm - 1; 2443 int LSB = Srl_imm - Shl_imm; 2444 if (LSB < 0) 2445 return false; 2446 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); 2447 SDValue Ops[] = { N->getOperand(0).getOperand(0), 2448 CurDAG->getTargetConstant(LSB, dl, MVT::i32), 2449 CurDAG->getTargetConstant(Width, dl, MVT::i32), 2450 getAL(CurDAG, dl), Reg0 }; 2451 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops); 2452 return true; 2453 } 2454 } 2455 2456 // Or we are looking for a shift of an and, with a mask operand 2457 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, And_imm) && 2458 isShiftedMask_32(And_imm)) { 2459 unsigned Srl_imm = 0; 2460 unsigned LSB = countTrailingZeros(And_imm); 2461 // Shift must be the same as the ands lsb 2462 if (isInt32Immediate(N->getOperand(1), Srl_imm) && Srl_imm == LSB) { 2463 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!"); 2464 unsigned MSB = 31 - countLeadingZeros(And_imm); 2465 // Note: The width operand is encoded as width-1. 2466 unsigned Width = MSB - LSB; 2467 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); 2468 SDValue Ops[] = { N->getOperand(0).getOperand(0), 2469 CurDAG->getTargetConstant(Srl_imm, dl, MVT::i32), 2470 CurDAG->getTargetConstant(Width, dl, MVT::i32), 2471 getAL(CurDAG, dl), Reg0 }; 2472 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops); 2473 return true; 2474 } 2475 } 2476 2477 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG) { 2478 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits(); 2479 unsigned LSB = 0; 2480 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL, LSB) && 2481 !isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRA, LSB)) 2482 return false; 2483 2484 if (LSB + Width > 32) 2485 return false; 2486 2487 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); 2488 SDValue Ops[] = { N->getOperand(0).getOperand(0), 2489 CurDAG->getTargetConstant(LSB, dl, MVT::i32), 2490 CurDAG->getTargetConstant(Width - 1, dl, MVT::i32), 2491 getAL(CurDAG, dl), Reg0 }; 2492 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops); 2493 return true; 2494 } 2495 2496 return false; 2497 } 2498 2499 /// Target-specific DAG combining for ISD::XOR. 2500 /// Target-independent combining lowers SELECT_CC nodes of the form 2501 /// select_cc setg[ge] X, 0, X, -X 2502 /// select_cc setgt X, -1, X, -X 2503 /// select_cc setl[te] X, 0, -X, X 2504 /// select_cc setlt X, 1, -X, X 2505 /// which represent Integer ABS into: 2506 /// Y = sra (X, size(X)-1); xor (add (X, Y), Y) 2507 /// ARM instruction selection detects the latter and matches it to 2508 /// ARM::ABS or ARM::t2ABS machine node. 2509 bool ARMDAGToDAGISel::tryABSOp(SDNode *N){ 2510 SDValue XORSrc0 = N->getOperand(0); 2511 SDValue XORSrc1 = N->getOperand(1); 2512 EVT VT = N->getValueType(0); 2513 2514 if (Subtarget->isThumb1Only()) 2515 return false; 2516 2517 if (XORSrc0.getOpcode() != ISD::ADD || XORSrc1.getOpcode() != ISD::SRA) 2518 return false; 2519 2520 SDValue ADDSrc0 = XORSrc0.getOperand(0); 2521 SDValue ADDSrc1 = XORSrc0.getOperand(1); 2522 SDValue SRASrc0 = XORSrc1.getOperand(0); 2523 SDValue SRASrc1 = XORSrc1.getOperand(1); 2524 ConstantSDNode *SRAConstant = dyn_cast<ConstantSDNode>(SRASrc1); 2525 EVT XType = SRASrc0.getValueType(); 2526 unsigned Size = XType.getSizeInBits() - 1; 2527 2528 if (ADDSrc1 == XORSrc1 && ADDSrc0 == SRASrc0 && 2529 XType.isInteger() && SRAConstant != nullptr && 2530 Size == SRAConstant->getZExtValue()) { 2531 unsigned Opcode = Subtarget->isThumb2() ? ARM::t2ABS : ARM::ABS; 2532 CurDAG->SelectNodeTo(N, Opcode, VT, ADDSrc0); 2533 return true; 2534 } 2535 2536 return false; 2537 } 2538 2539 static bool SearchSignedMulShort(SDValue SignExt, unsigned *Opc, SDValue &Src1, 2540 bool Accumulate) { 2541 // For SM*WB, we need to some form of sext. 2542 // For SM*WT, we need to search for (sra X, 16) 2543 // Src1 then gets set to X. 2544 if ((SignExt.getOpcode() == ISD::SIGN_EXTEND || 2545 SignExt.getOpcode() == ISD::SIGN_EXTEND_INREG || 2546 SignExt.getOpcode() == ISD::AssertSext) && 2547 SignExt.getValueType() == MVT::i32) { 2548 2549 *Opc = Accumulate ? ARM::SMLAWB : ARM::SMULWB; 2550 Src1 = SignExt.getOperand(0); 2551 return true; 2552 } 2553 2554 if (SignExt.getOpcode() != ISD::SRA) 2555 return false; 2556 2557 ConstantSDNode *SRASrc1 = dyn_cast<ConstantSDNode>(SignExt.getOperand(1)); 2558 if (!SRASrc1 || SRASrc1->getZExtValue() != 16) 2559 return false; 2560 2561 SDValue Op0 = SignExt.getOperand(0); 2562 2563 // The sign extend operand for SM*WB could be generated by a shl and ashr. 2564 if (Op0.getOpcode() == ISD::SHL) { 2565 SDValue SHL = Op0; 2566 ConstantSDNode *SHLSrc1 = dyn_cast<ConstantSDNode>(SHL.getOperand(1)); 2567 if (!SHLSrc1 || SHLSrc1->getZExtValue() != 16) 2568 return false; 2569 2570 *Opc = Accumulate ? ARM::SMLAWB : ARM::SMULWB; 2571 Src1 = Op0.getOperand(0); 2572 return true; 2573 } 2574 *Opc = Accumulate ? ARM::SMLAWT : ARM::SMULWT; 2575 Src1 = SignExt.getOperand(0); 2576 return true; 2577 } 2578 2579 static bool SearchSignedMulLong(SDValue OR, unsigned *Opc, SDValue &Src0, 2580 SDValue &Src1, bool Accumulate) { 2581 // First we look for: 2582 // (add (or (srl ?, 16), (shl ?, 16))) 2583 if (OR.getOpcode() != ISD::OR) 2584 return false; 2585 2586 SDValue SRL = OR.getOperand(0); 2587 SDValue SHL = OR.getOperand(1); 2588 2589 if (SRL.getOpcode() != ISD::SRL || SHL.getOpcode() != ISD::SHL) { 2590 SRL = OR.getOperand(1); 2591 SHL = OR.getOperand(0); 2592 if (SRL.getOpcode() != ISD::SRL || SHL.getOpcode() != ISD::SHL) 2593 return false; 2594 } 2595 2596 ConstantSDNode *SRLSrc1 = dyn_cast<ConstantSDNode>(SRL.getOperand(1)); 2597 ConstantSDNode *SHLSrc1 = dyn_cast<ConstantSDNode>(SHL.getOperand(1)); 2598 if (!SRLSrc1 || !SHLSrc1 || SRLSrc1->getZExtValue() != 16 || 2599 SHLSrc1->getZExtValue() != 16) 2600 return false; 2601 2602 // The first operands to the shifts need to be the two results from the 2603 // same smul_lohi node. 2604 if ((SRL.getOperand(0).getNode() != SHL.getOperand(0).getNode()) || 2605 SRL.getOperand(0).getOpcode() != ISD::SMUL_LOHI) 2606 return false; 2607 2608 SDNode *SMULLOHI = SRL.getOperand(0).getNode(); 2609 if (SRL.getOperand(0) != SDValue(SMULLOHI, 0) || 2610 SHL.getOperand(0) != SDValue(SMULLOHI, 1)) 2611 return false; 2612 2613 // Now we have: 2614 // (add (or (srl (smul_lohi ?, ?), 16), (shl (smul_lohi ?, ?), 16))) 2615 // For SMLAW[B|T] smul_lohi will take a 32-bit and a 16-bit arguments. 2616 // For SMLAWB the 16-bit value will signed extended somehow. 2617 // For SMLAWT only the SRA is required. 2618 2619 // Check both sides of SMUL_LOHI 2620 if (SearchSignedMulShort(SMULLOHI->getOperand(0), Opc, Src1, Accumulate)) { 2621 Src0 = SMULLOHI->getOperand(1); 2622 } else if (SearchSignedMulShort(SMULLOHI->getOperand(1), Opc, Src1, 2623 Accumulate)) { 2624 Src0 = SMULLOHI->getOperand(0); 2625 } else { 2626 return false; 2627 } 2628 return true; 2629 } 2630 2631 bool ARMDAGToDAGISel::trySMLAWSMULW(SDNode *N) { 2632 if (!Subtarget->hasV6Ops() || 2633 (Subtarget->isThumb() && !Subtarget->hasThumb2())) 2634 return false; 2635 2636 SDLoc dl(N); 2637 SDValue Src0 = N->getOperand(0); 2638 SDValue Src1 = N->getOperand(1); 2639 SDValue A, B; 2640 unsigned Opc = 0; 2641 2642 if (N->getOpcode() == ISD::ADD) { 2643 if (Src0.getOpcode() != ISD::OR && Src1.getOpcode() != ISD::OR) 2644 return false; 2645 2646 SDValue Acc; 2647 if (SearchSignedMulLong(Src0, &Opc, A, B, true)) { 2648 Acc = Src1; 2649 } else if (SearchSignedMulLong(Src1, &Opc, A, B, true)) { 2650 Acc = Src0; 2651 } else { 2652 return false; 2653 } 2654 if (Opc == 0) 2655 return false; 2656 2657 SDValue Ops[] = { A, B, Acc, getAL(CurDAG, dl), 2658 CurDAG->getRegister(0, MVT::i32) }; 2659 CurDAG->SelectNodeTo(N, Opc, MVT::i32, MVT::Other, Ops); 2660 return true; 2661 } else if (N->getOpcode() == ISD::OR && 2662 SearchSignedMulLong(SDValue(N, 0), &Opc, A, B, false)) { 2663 if (Opc == 0) 2664 return false; 2665 2666 SDValue Ops[] = { A, B, getAL(CurDAG, dl), 2667 CurDAG->getRegister(0, MVT::i32)}; 2668 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops); 2669 return true; 2670 } 2671 return false; 2672 } 2673 2674 /// We've got special pseudo-instructions for these 2675 void ARMDAGToDAGISel::SelectCMP_SWAP(SDNode *N) { 2676 unsigned Opcode; 2677 EVT MemTy = cast<MemSDNode>(N)->getMemoryVT(); 2678 if (MemTy == MVT::i8) 2679 Opcode = ARM::CMP_SWAP_8; 2680 else if (MemTy == MVT::i16) 2681 Opcode = ARM::CMP_SWAP_16; 2682 else if (MemTy == MVT::i32) 2683 Opcode = ARM::CMP_SWAP_32; 2684 else 2685 llvm_unreachable("Unknown AtomicCmpSwap type"); 2686 2687 SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3), 2688 N->getOperand(0)}; 2689 SDNode *CmpSwap = CurDAG->getMachineNode( 2690 Opcode, SDLoc(N), 2691 CurDAG->getVTList(MVT::i32, MVT::i32, MVT::Other), Ops); 2692 2693 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); 2694 MemOp[0] = cast<MemSDNode>(N)->getMemOperand(); 2695 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1); 2696 2697 ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0)); 2698 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2)); 2699 CurDAG->RemoveDeadNode(N); 2700 } 2701 2702 void ARMDAGToDAGISel::SelectConcatVector(SDNode *N) { 2703 // The only time a CONCAT_VECTORS operation can have legal types is when 2704 // two 64-bit vectors are concatenated to a 128-bit vector. 2705 EVT VT = N->getValueType(0); 2706 if (!VT.is128BitVector() || N->getNumOperands() != 2) 2707 llvm_unreachable("unexpected CONCAT_VECTORS"); 2708 ReplaceNode(N, createDRegPairNode(VT, N->getOperand(0), N->getOperand(1))); 2709 } 2710 2711 void ARMDAGToDAGISel::Select(SDNode *N) { 2712 SDLoc dl(N); 2713 2714 if (N->isMachineOpcode()) { 2715 N->setNodeId(-1); 2716 return; // Already selected. 2717 } 2718 2719 switch (N->getOpcode()) { 2720 default: break; 2721 case ISD::ADD: 2722 case ISD::OR: 2723 if (trySMLAWSMULW(N)) 2724 return; 2725 break; 2726 case ISD::WRITE_REGISTER: 2727 if (tryWriteRegister(N)) 2728 return; 2729 break; 2730 case ISD::READ_REGISTER: 2731 if (tryReadRegister(N)) 2732 return; 2733 break; 2734 case ISD::INLINEASM: 2735 if (tryInlineAsm(N)) 2736 return; 2737 break; 2738 case ISD::XOR: 2739 // Select special operations if XOR node forms integer ABS pattern 2740 if (tryABSOp(N)) 2741 return; 2742 // Other cases are autogenerated. 2743 break; 2744 case ISD::Constant: { 2745 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue(); 2746 // If we can't materialize the constant we need to use a literal pool 2747 if (ConstantMaterializationCost(Val) > 2) { 2748 SDValue CPIdx = CurDAG->getTargetConstantPool( 2749 ConstantInt::get(Type::getInt32Ty(*CurDAG->getContext()), Val), 2750 TLI->getPointerTy(CurDAG->getDataLayout())); 2751 2752 SDNode *ResNode; 2753 if (Subtarget->isThumb()) { 2754 SDValue Pred = getAL(CurDAG, dl); 2755 SDValue PredReg = CurDAG->getRegister(0, MVT::i32); 2756 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() }; 2757 ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other, 2758 Ops); 2759 } else { 2760 SDValue Ops[] = { 2761 CPIdx, 2762 CurDAG->getTargetConstant(0, dl, MVT::i32), 2763 getAL(CurDAG, dl), 2764 CurDAG->getRegister(0, MVT::i32), 2765 CurDAG->getEntryNode() 2766 }; 2767 ResNode = CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other, 2768 Ops); 2769 } 2770 ReplaceNode(N, ResNode); 2771 return; 2772 } 2773 2774 // Other cases are autogenerated. 2775 break; 2776 } 2777 case ISD::FrameIndex: { 2778 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm. 2779 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 2780 SDValue TFI = CurDAG->getTargetFrameIndex( 2781 FI, TLI->getPointerTy(CurDAG->getDataLayout())); 2782 if (Subtarget->isThumb1Only()) { 2783 // Set the alignment of the frame object to 4, to avoid having to generate 2784 // more than one ADD 2785 MachineFrameInfo &MFI = MF->getFrameInfo(); 2786 if (MFI.getObjectAlignment(FI) < 4) 2787 MFI.setObjectAlignment(FI, 4); 2788 CurDAG->SelectNodeTo(N, ARM::tADDframe, MVT::i32, TFI, 2789 CurDAG->getTargetConstant(0, dl, MVT::i32)); 2790 return; 2791 } else { 2792 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ? 2793 ARM::t2ADDri : ARM::ADDri); 2794 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, dl, MVT::i32), 2795 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32), 2796 CurDAG->getRegister(0, MVT::i32) }; 2797 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops); 2798 return; 2799 } 2800 } 2801 case ISD::SRL: 2802 if (tryV6T2BitfieldExtractOp(N, false)) 2803 return; 2804 break; 2805 case ISD::SIGN_EXTEND_INREG: 2806 case ISD::SRA: 2807 if (tryV6T2BitfieldExtractOp(N, true)) 2808 return; 2809 break; 2810 case ISD::MUL: 2811 if (Subtarget->isThumb1Only()) 2812 break; 2813 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) { 2814 unsigned RHSV = C->getZExtValue(); 2815 if (!RHSV) break; 2816 if (isPowerOf2_32(RHSV-1)) { // 2^n+1? 2817 unsigned ShImm = Log2_32(RHSV-1); 2818 if (ShImm >= 32) 2819 break; 2820 SDValue V = N->getOperand(0); 2821 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm); 2822 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32); 2823 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); 2824 if (Subtarget->isThumb()) { 2825 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 }; 2826 CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops); 2827 return; 2828 } else { 2829 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0, 2830 Reg0 }; 2831 CurDAG->SelectNodeTo(N, ARM::ADDrsi, MVT::i32, Ops); 2832 return; 2833 } 2834 } 2835 if (isPowerOf2_32(RHSV+1)) { // 2^n-1? 2836 unsigned ShImm = Log2_32(RHSV+1); 2837 if (ShImm >= 32) 2838 break; 2839 SDValue V = N->getOperand(0); 2840 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm); 2841 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32); 2842 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); 2843 if (Subtarget->isThumb()) { 2844 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 }; 2845 CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops); 2846 return; 2847 } else { 2848 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0, 2849 Reg0 }; 2850 CurDAG->SelectNodeTo(N, ARM::RSBrsi, MVT::i32, Ops); 2851 return; 2852 } 2853 } 2854 } 2855 break; 2856 case ISD::AND: { 2857 // Check for unsigned bitfield extract 2858 if (tryV6T2BitfieldExtractOp(N, false)) 2859 return; 2860 2861 // If an immediate is used in an AND node, it is possible that the immediate 2862 // can be more optimally materialized when negated. If this is the case we 2863 // can negate the immediate and use a BIC instead. 2864 auto *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2865 if (N1C && N1C->hasOneUse() && Subtarget->isThumb()) { 2866 uint32_t Imm = (uint32_t) N1C->getZExtValue(); 2867 2868 // In Thumb2 mode, an AND can take a 12-bit immediate. If this 2869 // immediate can be negated and fit in the immediate operand of 2870 // a t2BIC, don't do any manual transform here as this can be 2871 // handled by the generic ISel machinery. 2872 bool PreferImmediateEncoding = 2873 Subtarget->hasThumb2() && (is_t2_so_imm(Imm) || is_t2_so_imm_not(Imm)); 2874 if (!PreferImmediateEncoding && 2875 ConstantMaterializationCost(Imm) > 2876 ConstantMaterializationCost(~Imm)) { 2877 // The current immediate costs more to materialize than a negated 2878 // immediate, so negate the immediate and use a BIC. 2879 SDValue NewImm = 2880 CurDAG->getConstant(~N1C->getZExtValue(), dl, MVT::i32); 2881 // If the new constant didn't exist before, reposition it in the topological 2882 // ordering so it is just before N. Otherwise, don't touch its location. 2883 if (NewImm->getNodeId() == -1) 2884 CurDAG->RepositionNode(N->getIterator(), NewImm.getNode()); 2885 2886 if (!Subtarget->hasThumb2()) { 2887 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), 2888 N->getOperand(0), NewImm, getAL(CurDAG, dl), 2889 CurDAG->getRegister(0, MVT::i32)}; 2890 ReplaceNode(N, CurDAG->getMachineNode(ARM::tBIC, dl, MVT::i32, Ops)); 2891 return; 2892 } else { 2893 SDValue Ops[] = {N->getOperand(0), NewImm, getAL(CurDAG, dl), 2894 CurDAG->getRegister(0, MVT::i32), 2895 CurDAG->getRegister(0, MVT::i32)}; 2896 ReplaceNode(N, 2897 CurDAG->getMachineNode(ARM::t2BICrr, dl, MVT::i32, Ops)); 2898 return; 2899 } 2900 } 2901 } 2902 2903 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits 2904 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits 2905 // are entirely contributed by c2 and lower 16-bits are entirely contributed 2906 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)). 2907 // Select it to: "movt x, ((c1 & 0xffff) >> 16) 2908 EVT VT = N->getValueType(0); 2909 if (VT != MVT::i32) 2910 break; 2911 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2()) 2912 ? ARM::t2MOVTi16 2913 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0); 2914 if (!Opc) 2915 break; 2916 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1); 2917 N1C = dyn_cast<ConstantSDNode>(N1); 2918 if (!N1C) 2919 break; 2920 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) { 2921 SDValue N2 = N0.getOperand(1); 2922 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); 2923 if (!N2C) 2924 break; 2925 unsigned N1CVal = N1C->getZExtValue(); 2926 unsigned N2CVal = N2C->getZExtValue(); 2927 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) && 2928 (N1CVal & 0xffffU) == 0xffffU && 2929 (N2CVal & 0xffffU) == 0x0U) { 2930 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16, 2931 dl, MVT::i32); 2932 SDValue Ops[] = { N0.getOperand(0), Imm16, 2933 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32) }; 2934 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops)); 2935 return; 2936 } 2937 } 2938 break; 2939 } 2940 case ARMISD::VMOVRRD: 2941 ReplaceNode(N, CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32, 2942 N->getOperand(0), getAL(CurDAG, dl), 2943 CurDAG->getRegister(0, MVT::i32))); 2944 return; 2945 case ISD::UMUL_LOHI: { 2946 if (Subtarget->isThumb1Only()) 2947 break; 2948 if (Subtarget->isThumb()) { 2949 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), 2950 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32) }; 2951 ReplaceNode( 2952 N, CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32, Ops)); 2953 return; 2954 } else { 2955 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), 2956 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32), 2957 CurDAG->getRegister(0, MVT::i32) }; 2958 ReplaceNode(N, CurDAG->getMachineNode( 2959 Subtarget->hasV6Ops() ? ARM::UMULL : ARM::UMULLv5, dl, 2960 MVT::i32, MVT::i32, Ops)); 2961 return; 2962 } 2963 } 2964 case ISD::SMUL_LOHI: { 2965 if (Subtarget->isThumb1Only()) 2966 break; 2967 if (Subtarget->isThumb()) { 2968 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), 2969 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32) }; 2970 ReplaceNode( 2971 N, CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32, Ops)); 2972 return; 2973 } else { 2974 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), 2975 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32), 2976 CurDAG->getRegister(0, MVT::i32) }; 2977 ReplaceNode(N, CurDAG->getMachineNode( 2978 Subtarget->hasV6Ops() ? ARM::SMULL : ARM::SMULLv5, dl, 2979 MVT::i32, MVT::i32, Ops)); 2980 return; 2981 } 2982 } 2983 case ARMISD::UMAAL: { 2984 unsigned Opc = Subtarget->isThumb() ? ARM::t2UMAAL : ARM::UMAAL; 2985 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), 2986 N->getOperand(2), N->getOperand(3), 2987 getAL(CurDAG, dl), 2988 CurDAG->getRegister(0, MVT::i32) }; 2989 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, MVT::i32, Ops)); 2990 return; 2991 } 2992 case ARMISD::UMLAL:{ 2993 // UMAAL is similar to UMLAL but it adds two 32-bit values to the 2994 // 64-bit multiplication result. 2995 if (Subtarget->hasV6Ops() && N->getOperand(2).getOpcode() == ARMISD::ADDC && 2996 N->getOperand(3).getOpcode() == ARMISD::ADDE) { 2997 2998 SDValue Addc = N->getOperand(2); 2999 SDValue Adde = N->getOperand(3); 3000 3001 if (Adde.getOperand(2).getNode() == Addc.getNode()) { 3002 3003 ConstantSDNode *Op0 = dyn_cast<ConstantSDNode>(Adde.getOperand(0)); 3004 ConstantSDNode *Op1 = dyn_cast<ConstantSDNode>(Adde.getOperand(1)); 3005 3006 if (Op0 && Op1 && Op0->getZExtValue() == 0 && Op1->getZExtValue() == 0) 3007 { 3008 // Select UMAAL instead: UMAAL RdLo, RdHi, Rn, Rm 3009 // RdLo = one operand to be added, lower 32-bits of res 3010 // RdHi = other operand to be added, upper 32-bits of res 3011 // Rn = first multiply operand 3012 // Rm = second multiply operand 3013 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), 3014 Addc.getOperand(0), Addc.getOperand(1), 3015 getAL(CurDAG, dl), 3016 CurDAG->getRegister(0, MVT::i32) }; 3017 unsigned opc = Subtarget->isThumb() ? ARM::t2UMAAL : ARM::UMAAL; 3018 CurDAG->SelectNodeTo(N, opc, MVT::i32, MVT::i32, Ops); 3019 return; 3020 } 3021 } 3022 } 3023 3024 if (Subtarget->isThumb()) { 3025 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2), 3026 N->getOperand(3), getAL(CurDAG, dl), 3027 CurDAG->getRegister(0, MVT::i32)}; 3028 ReplaceNode( 3029 N, CurDAG->getMachineNode(ARM::t2UMLAL, dl, MVT::i32, MVT::i32, Ops)); 3030 return; 3031 }else{ 3032 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2), 3033 N->getOperand(3), getAL(CurDAG, dl), 3034 CurDAG->getRegister(0, MVT::i32), 3035 CurDAG->getRegister(0, MVT::i32) }; 3036 ReplaceNode(N, CurDAG->getMachineNode( 3037 Subtarget->hasV6Ops() ? ARM::UMLAL : ARM::UMLALv5, dl, 3038 MVT::i32, MVT::i32, Ops)); 3039 return; 3040 } 3041 } 3042 case ARMISD::SMLAL:{ 3043 if (Subtarget->isThumb()) { 3044 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2), 3045 N->getOperand(3), getAL(CurDAG, dl), 3046 CurDAG->getRegister(0, MVT::i32)}; 3047 ReplaceNode( 3048 N, CurDAG->getMachineNode(ARM::t2SMLAL, dl, MVT::i32, MVT::i32, Ops)); 3049 return; 3050 }else{ 3051 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2), 3052 N->getOperand(3), getAL(CurDAG, dl), 3053 CurDAG->getRegister(0, MVT::i32), 3054 CurDAG->getRegister(0, MVT::i32) }; 3055 ReplaceNode(N, CurDAG->getMachineNode( 3056 Subtarget->hasV6Ops() ? ARM::SMLAL : ARM::SMLALv5, dl, 3057 MVT::i32, MVT::i32, Ops)); 3058 return; 3059 } 3060 } 3061 case ARMISD::SUBE: { 3062 if (!Subtarget->hasV6Ops()) 3063 break; 3064 // Look for a pattern to match SMMLS 3065 // (sube a, (smul_loHi a, b), (subc 0, (smul_LOhi(a, b)))) 3066 if (N->getOperand(1).getOpcode() != ISD::SMUL_LOHI || 3067 N->getOperand(2).getOpcode() != ARMISD::SUBC || 3068 !SDValue(N, 1).use_empty()) 3069 break; 3070 3071 if (Subtarget->isThumb()) 3072 assert(Subtarget->hasThumb2() && 3073 "This pattern should not be generated for Thumb"); 3074 3075 SDValue SmulLoHi = N->getOperand(1); 3076 SDValue Subc = N->getOperand(2); 3077 auto *Zero = dyn_cast<ConstantSDNode>(Subc.getOperand(0)); 3078 3079 if (!Zero || Zero->getZExtValue() != 0 || 3080 Subc.getOperand(1) != SmulLoHi.getValue(0) || 3081 N->getOperand(1) != SmulLoHi.getValue(1) || 3082 N->getOperand(2) != Subc.getValue(1)) 3083 break; 3084 3085 unsigned Opc = Subtarget->isThumb2() ? ARM::t2SMMLS : ARM::SMMLS; 3086 SDValue Ops[] = { SmulLoHi.getOperand(0), SmulLoHi.getOperand(1), 3087 N->getOperand(0), getAL(CurDAG, dl), 3088 CurDAG->getRegister(0, MVT::i32) }; 3089 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops)); 3090 return; 3091 } 3092 case ISD::LOAD: { 3093 if (Subtarget->isThumb() && Subtarget->hasThumb2()) { 3094 if (tryT2IndexedLoad(N)) 3095 return; 3096 } else if (Subtarget->isThumb()) { 3097 if (tryT1IndexedLoad(N)) 3098 return; 3099 } else if (tryARMIndexedLoad(N)) 3100 return; 3101 // Other cases are autogenerated. 3102 break; 3103 } 3104 case ARMISD::BRCOND: { 3105 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc) 3106 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc) 3107 // Pattern complexity = 6 cost = 1 size = 0 3108 3109 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc) 3110 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc) 3111 // Pattern complexity = 6 cost = 1 size = 0 3112 3113 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc) 3114 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc) 3115 // Pattern complexity = 6 cost = 1 size = 0 3116 3117 unsigned Opc = Subtarget->isThumb() ? 3118 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc; 3119 SDValue Chain = N->getOperand(0); 3120 SDValue N1 = N->getOperand(1); 3121 SDValue N2 = N->getOperand(2); 3122 SDValue N3 = N->getOperand(3); 3123 SDValue InFlag = N->getOperand(4); 3124 assert(N1.getOpcode() == ISD::BasicBlock); 3125 assert(N2.getOpcode() == ISD::Constant); 3126 assert(N3.getOpcode() == ISD::Register); 3127 3128 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned) 3129 cast<ConstantSDNode>(N2)->getZExtValue()), dl, 3130 MVT::i32); 3131 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag }; 3132 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other, 3133 MVT::Glue, Ops); 3134 Chain = SDValue(ResNode, 0); 3135 if (N->getNumValues() == 2) { 3136 InFlag = SDValue(ResNode, 1); 3137 ReplaceUses(SDValue(N, 1), InFlag); 3138 } 3139 ReplaceUses(SDValue(N, 0), 3140 SDValue(Chain.getNode(), Chain.getResNo())); 3141 CurDAG->RemoveDeadNode(N); 3142 return; 3143 } 3144 3145 case ARMISD::CMPZ: { 3146 // select (CMPZ X, #-C) -> (CMPZ (ADDS X, #C), #0) 3147 // This allows us to avoid materializing the expensive negative constant. 3148 // The CMPZ #0 is useless and will be peepholed away but we need to keep it 3149 // for its glue output. 3150 SDValue X = N->getOperand(0); 3151 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1).getNode()); 3152 if (C && C->getSExtValue() < 0 && Subtarget->isThumb()) { 3153 int64_t Addend = -C->getSExtValue(); 3154 3155 SDNode *Add = nullptr; 3156 // In T2 mode, ADDS can be better than CMN if the immediate fits in a 3157 // 16-bit ADDS, which means either [0,256) for tADDi8 or [0,8) for tADDi3. 3158 // Outside that range we can just use a CMN which is 32-bit but has a 3159 // 12-bit immediate range. 3160 if (Subtarget->isThumb2() && Addend < 1<<8) { 3161 SDValue Ops[] = { X, CurDAG->getTargetConstant(Addend, dl, MVT::i32), 3162 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32), 3163 CurDAG->getRegister(0, MVT::i32) }; 3164 Add = CurDAG->getMachineNode(ARM::t2ADDri, dl, MVT::i32, Ops); 3165 } else if (!Subtarget->isThumb2() && Addend < 1<<8) { 3166 // FIXME: Add T1 tADDi8 code. 3167 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), X, 3168 CurDAG->getTargetConstant(Addend, dl, MVT::i32), 3169 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)}; 3170 Add = CurDAG->getMachineNode(ARM::tADDi8, dl, MVT::i32, Ops); 3171 } else if (!Subtarget->isThumb2() && Addend < 1<<3) { 3172 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), X, 3173 CurDAG->getTargetConstant(Addend, dl, MVT::i32), 3174 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)}; 3175 Add = CurDAG->getMachineNode(ARM::tADDi3, dl, MVT::i32, Ops); 3176 } 3177 if (Add) { 3178 SDValue Ops2[] = {SDValue(Add, 0), CurDAG->getConstant(0, dl, MVT::i32)}; 3179 CurDAG->MorphNodeTo(N, ARMISD::CMPZ, CurDAG->getVTList(MVT::Glue), Ops2); 3180 } 3181 } 3182 // Other cases are autogenerated. 3183 break; 3184 } 3185 3186 case ARMISD::VZIP: { 3187 unsigned Opc = 0; 3188 EVT VT = N->getValueType(0); 3189 switch (VT.getSimpleVT().SimpleTy) { 3190 default: return; 3191 case MVT::v8i8: Opc = ARM::VZIPd8; break; 3192 case MVT::v4i16: Opc = ARM::VZIPd16; break; 3193 case MVT::v2f32: 3194 // vzip.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm. 3195 case MVT::v2i32: Opc = ARM::VTRNd32; break; 3196 case MVT::v16i8: Opc = ARM::VZIPq8; break; 3197 case MVT::v8i16: Opc = ARM::VZIPq16; break; 3198 case MVT::v4f32: 3199 case MVT::v4i32: Opc = ARM::VZIPq32; break; 3200 } 3201 SDValue Pred = getAL(CurDAG, dl); 3202 SDValue PredReg = CurDAG->getRegister(0, MVT::i32); 3203 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg }; 3204 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops)); 3205 return; 3206 } 3207 case ARMISD::VUZP: { 3208 unsigned Opc = 0; 3209 EVT VT = N->getValueType(0); 3210 switch (VT.getSimpleVT().SimpleTy) { 3211 default: return; 3212 case MVT::v8i8: Opc = ARM::VUZPd8; break; 3213 case MVT::v4i16: Opc = ARM::VUZPd16; break; 3214 case MVT::v2f32: 3215 // vuzp.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm. 3216 case MVT::v2i32: Opc = ARM::VTRNd32; break; 3217 case MVT::v16i8: Opc = ARM::VUZPq8; break; 3218 case MVT::v8i16: Opc = ARM::VUZPq16; break; 3219 case MVT::v4f32: 3220 case MVT::v4i32: Opc = ARM::VUZPq32; break; 3221 } 3222 SDValue Pred = getAL(CurDAG, dl); 3223 SDValue PredReg = CurDAG->getRegister(0, MVT::i32); 3224 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg }; 3225 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops)); 3226 return; 3227 } 3228 case ARMISD::VTRN: { 3229 unsigned Opc = 0; 3230 EVT VT = N->getValueType(0); 3231 switch (VT.getSimpleVT().SimpleTy) { 3232 default: return; 3233 case MVT::v8i8: Opc = ARM::VTRNd8; break; 3234 case MVT::v4i16: Opc = ARM::VTRNd16; break; 3235 case MVT::v2f32: 3236 case MVT::v2i32: Opc = ARM::VTRNd32; break; 3237 case MVT::v16i8: Opc = ARM::VTRNq8; break; 3238 case MVT::v8i16: Opc = ARM::VTRNq16; break; 3239 case MVT::v4f32: 3240 case MVT::v4i32: Opc = ARM::VTRNq32; break; 3241 } 3242 SDValue Pred = getAL(CurDAG, dl); 3243 SDValue PredReg = CurDAG->getRegister(0, MVT::i32); 3244 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg }; 3245 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops)); 3246 return; 3247 } 3248 case ARMISD::BUILD_VECTOR: { 3249 EVT VecVT = N->getValueType(0); 3250 EVT EltVT = VecVT.getVectorElementType(); 3251 unsigned NumElts = VecVT.getVectorNumElements(); 3252 if (EltVT == MVT::f64) { 3253 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR"); 3254 ReplaceNode( 3255 N, createDRegPairNode(VecVT, N->getOperand(0), N->getOperand(1))); 3256 return; 3257 } 3258 assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR"); 3259 if (NumElts == 2) { 3260 ReplaceNode( 3261 N, createSRegPairNode(VecVT, N->getOperand(0), N->getOperand(1))); 3262 return; 3263 } 3264 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR"); 3265 ReplaceNode(N, 3266 createQuadSRegsNode(VecVT, N->getOperand(0), N->getOperand(1), 3267 N->getOperand(2), N->getOperand(3))); 3268 return; 3269 } 3270 3271 case ARMISD::VLD2DUP: { 3272 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8, ARM::VLD2DUPd16, 3273 ARM::VLD2DUPd32 }; 3274 SelectVLDDup(N, false, 2, Opcodes); 3275 return; 3276 } 3277 3278 case ARMISD::VLD3DUP: { 3279 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo, 3280 ARM::VLD3DUPd16Pseudo, 3281 ARM::VLD3DUPd32Pseudo }; 3282 SelectVLDDup(N, false, 3, Opcodes); 3283 return; 3284 } 3285 3286 case ARMISD::VLD4DUP: { 3287 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo, 3288 ARM::VLD4DUPd16Pseudo, 3289 ARM::VLD4DUPd32Pseudo }; 3290 SelectVLDDup(N, false, 4, Opcodes); 3291 return; 3292 } 3293 3294 case ARMISD::VLD2DUP_UPD: { 3295 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8wb_fixed, 3296 ARM::VLD2DUPd16wb_fixed, 3297 ARM::VLD2DUPd32wb_fixed }; 3298 SelectVLDDup(N, true, 2, Opcodes); 3299 return; 3300 } 3301 3302 case ARMISD::VLD3DUP_UPD: { 3303 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo_UPD, 3304 ARM::VLD3DUPd16Pseudo_UPD, 3305 ARM::VLD3DUPd32Pseudo_UPD }; 3306 SelectVLDDup(N, true, 3, Opcodes); 3307 return; 3308 } 3309 3310 case ARMISD::VLD4DUP_UPD: { 3311 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo_UPD, 3312 ARM::VLD4DUPd16Pseudo_UPD, 3313 ARM::VLD4DUPd32Pseudo_UPD }; 3314 SelectVLDDup(N, true, 4, Opcodes); 3315 return; 3316 } 3317 3318 case ARMISD::VLD1_UPD: { 3319 static const uint16_t DOpcodes[] = { ARM::VLD1d8wb_fixed, 3320 ARM::VLD1d16wb_fixed, 3321 ARM::VLD1d32wb_fixed, 3322 ARM::VLD1d64wb_fixed }; 3323 static const uint16_t QOpcodes[] = { ARM::VLD1q8wb_fixed, 3324 ARM::VLD1q16wb_fixed, 3325 ARM::VLD1q32wb_fixed, 3326 ARM::VLD1q64wb_fixed }; 3327 SelectVLD(N, true, 1, DOpcodes, QOpcodes, nullptr); 3328 return; 3329 } 3330 3331 case ARMISD::VLD2_UPD: { 3332 static const uint16_t DOpcodes[] = { ARM::VLD2d8wb_fixed, 3333 ARM::VLD2d16wb_fixed, 3334 ARM::VLD2d32wb_fixed, 3335 ARM::VLD1q64wb_fixed}; 3336 static const uint16_t QOpcodes[] = { ARM::VLD2q8PseudoWB_fixed, 3337 ARM::VLD2q16PseudoWB_fixed, 3338 ARM::VLD2q32PseudoWB_fixed }; 3339 SelectVLD(N, true, 2, DOpcodes, QOpcodes, nullptr); 3340 return; 3341 } 3342 3343 case ARMISD::VLD3_UPD: { 3344 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo_UPD, 3345 ARM::VLD3d16Pseudo_UPD, 3346 ARM::VLD3d32Pseudo_UPD, 3347 ARM::VLD1d64TPseudoWB_fixed}; 3348 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD, 3349 ARM::VLD3q16Pseudo_UPD, 3350 ARM::VLD3q32Pseudo_UPD }; 3351 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD, 3352 ARM::VLD3q16oddPseudo_UPD, 3353 ARM::VLD3q32oddPseudo_UPD }; 3354 SelectVLD(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1); 3355 return; 3356 } 3357 3358 case ARMISD::VLD4_UPD: { 3359 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo_UPD, 3360 ARM::VLD4d16Pseudo_UPD, 3361 ARM::VLD4d32Pseudo_UPD, 3362 ARM::VLD1d64QPseudoWB_fixed}; 3363 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD, 3364 ARM::VLD4q16Pseudo_UPD, 3365 ARM::VLD4q32Pseudo_UPD }; 3366 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD, 3367 ARM::VLD4q16oddPseudo_UPD, 3368 ARM::VLD4q32oddPseudo_UPD }; 3369 SelectVLD(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1); 3370 return; 3371 } 3372 3373 case ARMISD::VLD2LN_UPD: { 3374 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo_UPD, 3375 ARM::VLD2LNd16Pseudo_UPD, 3376 ARM::VLD2LNd32Pseudo_UPD }; 3377 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo_UPD, 3378 ARM::VLD2LNq32Pseudo_UPD }; 3379 SelectVLDSTLane(N, true, true, 2, DOpcodes, QOpcodes); 3380 return; 3381 } 3382 3383 case ARMISD::VLD3LN_UPD: { 3384 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo_UPD, 3385 ARM::VLD3LNd16Pseudo_UPD, 3386 ARM::VLD3LNd32Pseudo_UPD }; 3387 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo_UPD, 3388 ARM::VLD3LNq32Pseudo_UPD }; 3389 SelectVLDSTLane(N, true, true, 3, DOpcodes, QOpcodes); 3390 return; 3391 } 3392 3393 case ARMISD::VLD4LN_UPD: { 3394 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo_UPD, 3395 ARM::VLD4LNd16Pseudo_UPD, 3396 ARM::VLD4LNd32Pseudo_UPD }; 3397 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo_UPD, 3398 ARM::VLD4LNq32Pseudo_UPD }; 3399 SelectVLDSTLane(N, true, true, 4, DOpcodes, QOpcodes); 3400 return; 3401 } 3402 3403 case ARMISD::VST1_UPD: { 3404 static const uint16_t DOpcodes[] = { ARM::VST1d8wb_fixed, 3405 ARM::VST1d16wb_fixed, 3406 ARM::VST1d32wb_fixed, 3407 ARM::VST1d64wb_fixed }; 3408 static const uint16_t QOpcodes[] = { ARM::VST1q8wb_fixed, 3409 ARM::VST1q16wb_fixed, 3410 ARM::VST1q32wb_fixed, 3411 ARM::VST1q64wb_fixed }; 3412 SelectVST(N, true, 1, DOpcodes, QOpcodes, nullptr); 3413 return; 3414 } 3415 3416 case ARMISD::VST2_UPD: { 3417 static const uint16_t DOpcodes[] = { ARM::VST2d8wb_fixed, 3418 ARM::VST2d16wb_fixed, 3419 ARM::VST2d32wb_fixed, 3420 ARM::VST1q64wb_fixed}; 3421 static const uint16_t QOpcodes[] = { ARM::VST2q8PseudoWB_fixed, 3422 ARM::VST2q16PseudoWB_fixed, 3423 ARM::VST2q32PseudoWB_fixed }; 3424 SelectVST(N, true, 2, DOpcodes, QOpcodes, nullptr); 3425 return; 3426 } 3427 3428 case ARMISD::VST3_UPD: { 3429 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo_UPD, 3430 ARM::VST3d16Pseudo_UPD, 3431 ARM::VST3d32Pseudo_UPD, 3432 ARM::VST1d64TPseudoWB_fixed}; 3433 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD, 3434 ARM::VST3q16Pseudo_UPD, 3435 ARM::VST3q32Pseudo_UPD }; 3436 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD, 3437 ARM::VST3q16oddPseudo_UPD, 3438 ARM::VST3q32oddPseudo_UPD }; 3439 SelectVST(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1); 3440 return; 3441 } 3442 3443 case ARMISD::VST4_UPD: { 3444 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo_UPD, 3445 ARM::VST4d16Pseudo_UPD, 3446 ARM::VST4d32Pseudo_UPD, 3447 ARM::VST1d64QPseudoWB_fixed}; 3448 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD, 3449 ARM::VST4q16Pseudo_UPD, 3450 ARM::VST4q32Pseudo_UPD }; 3451 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD, 3452 ARM::VST4q16oddPseudo_UPD, 3453 ARM::VST4q32oddPseudo_UPD }; 3454 SelectVST(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1); 3455 return; 3456 } 3457 3458 case ARMISD::VST2LN_UPD: { 3459 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo_UPD, 3460 ARM::VST2LNd16Pseudo_UPD, 3461 ARM::VST2LNd32Pseudo_UPD }; 3462 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo_UPD, 3463 ARM::VST2LNq32Pseudo_UPD }; 3464 SelectVLDSTLane(N, false, true, 2, DOpcodes, QOpcodes); 3465 return; 3466 } 3467 3468 case ARMISD::VST3LN_UPD: { 3469 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo_UPD, 3470 ARM::VST3LNd16Pseudo_UPD, 3471 ARM::VST3LNd32Pseudo_UPD }; 3472 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo_UPD, 3473 ARM::VST3LNq32Pseudo_UPD }; 3474 SelectVLDSTLane(N, false, true, 3, DOpcodes, QOpcodes); 3475 return; 3476 } 3477 3478 case ARMISD::VST4LN_UPD: { 3479 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo_UPD, 3480 ARM::VST4LNd16Pseudo_UPD, 3481 ARM::VST4LNd32Pseudo_UPD }; 3482 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo_UPD, 3483 ARM::VST4LNq32Pseudo_UPD }; 3484 SelectVLDSTLane(N, false, true, 4, DOpcodes, QOpcodes); 3485 return; 3486 } 3487 3488 case ISD::INTRINSIC_VOID: 3489 case ISD::INTRINSIC_W_CHAIN: { 3490 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 3491 switch (IntNo) { 3492 default: 3493 break; 3494 3495 case Intrinsic::arm_mrrc: 3496 case Intrinsic::arm_mrrc2: { 3497 SDLoc dl(N); 3498 SDValue Chain = N->getOperand(0); 3499 unsigned Opc; 3500 3501 if (Subtarget->isThumb()) 3502 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::t2MRRC : ARM::t2MRRC2); 3503 else 3504 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::MRRC : ARM::MRRC2); 3505 3506 SmallVector<SDValue, 5> Ops; 3507 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(), dl)); /* coproc */ 3508 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(3))->getZExtValue(), dl)); /* opc */ 3509 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(4))->getZExtValue(), dl)); /* CRm */ 3510 3511 // The mrrc2 instruction in ARM doesn't allow predicates, the top 4 bits of the encoded 3512 // instruction will always be '1111' but it is possible in assembly language to specify 3513 // AL as a predicate to mrrc2 but it doesn't make any difference to the encoded instruction. 3514 if (Opc != ARM::MRRC2) { 3515 Ops.push_back(getAL(CurDAG, dl)); 3516 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); 3517 } 3518 3519 Ops.push_back(Chain); 3520 3521 // Writes to two registers. 3522 const EVT RetType[] = {MVT::i32, MVT::i32, MVT::Other}; 3523 3524 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, RetType, Ops)); 3525 return; 3526 } 3527 case Intrinsic::arm_ldaexd: 3528 case Intrinsic::arm_ldrexd: { 3529 SDLoc dl(N); 3530 SDValue Chain = N->getOperand(0); 3531 SDValue MemAddr = N->getOperand(2); 3532 bool isThumb = Subtarget->isThumb() && Subtarget->hasV8MBaselineOps(); 3533 3534 bool IsAcquire = IntNo == Intrinsic::arm_ldaexd; 3535 unsigned NewOpc = isThumb ? (IsAcquire ? ARM::t2LDAEXD : ARM::t2LDREXD) 3536 : (IsAcquire ? ARM::LDAEXD : ARM::LDREXD); 3537 3538 // arm_ldrexd returns a i64 value in {i32, i32} 3539 std::vector<EVT> ResTys; 3540 if (isThumb) { 3541 ResTys.push_back(MVT::i32); 3542 ResTys.push_back(MVT::i32); 3543 } else 3544 ResTys.push_back(MVT::Untyped); 3545 ResTys.push_back(MVT::Other); 3546 3547 // Place arguments in the right order. 3548 SDValue Ops[] = {MemAddr, getAL(CurDAG, dl), 3549 CurDAG->getRegister(0, MVT::i32), Chain}; 3550 SDNode *Ld = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops); 3551 // Transfer memoperands. 3552 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); 3553 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand(); 3554 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1); 3555 3556 // Remap uses. 3557 SDValue OutChain = isThumb ? SDValue(Ld, 2) : SDValue(Ld, 1); 3558 if (!SDValue(N, 0).use_empty()) { 3559 SDValue Result; 3560 if (isThumb) 3561 Result = SDValue(Ld, 0); 3562 else { 3563 SDValue SubRegIdx = 3564 CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32); 3565 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 3566 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx); 3567 Result = SDValue(ResNode,0); 3568 } 3569 ReplaceUses(SDValue(N, 0), Result); 3570 } 3571 if (!SDValue(N, 1).use_empty()) { 3572 SDValue Result; 3573 if (isThumb) 3574 Result = SDValue(Ld, 1); 3575 else { 3576 SDValue SubRegIdx = 3577 CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32); 3578 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 3579 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx); 3580 Result = SDValue(ResNode,0); 3581 } 3582 ReplaceUses(SDValue(N, 1), Result); 3583 } 3584 ReplaceUses(SDValue(N, 2), OutChain); 3585 CurDAG->RemoveDeadNode(N); 3586 return; 3587 } 3588 case Intrinsic::arm_stlexd: 3589 case Intrinsic::arm_strexd: { 3590 SDLoc dl(N); 3591 SDValue Chain = N->getOperand(0); 3592 SDValue Val0 = N->getOperand(2); 3593 SDValue Val1 = N->getOperand(3); 3594 SDValue MemAddr = N->getOperand(4); 3595 3596 // Store exclusive double return a i32 value which is the return status 3597 // of the issued store. 3598 const EVT ResTys[] = {MVT::i32, MVT::Other}; 3599 3600 bool isThumb = Subtarget->isThumb() && Subtarget->hasThumb2(); 3601 // Place arguments in the right order. 3602 SmallVector<SDValue, 7> Ops; 3603 if (isThumb) { 3604 Ops.push_back(Val0); 3605 Ops.push_back(Val1); 3606 } else 3607 // arm_strexd uses GPRPair. 3608 Ops.push_back(SDValue(createGPRPairNode(MVT::Untyped, Val0, Val1), 0)); 3609 Ops.push_back(MemAddr); 3610 Ops.push_back(getAL(CurDAG, dl)); 3611 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); 3612 Ops.push_back(Chain); 3613 3614 bool IsRelease = IntNo == Intrinsic::arm_stlexd; 3615 unsigned NewOpc = isThumb ? (IsRelease ? ARM::t2STLEXD : ARM::t2STREXD) 3616 : (IsRelease ? ARM::STLEXD : ARM::STREXD); 3617 3618 SDNode *St = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops); 3619 // Transfer memoperands. 3620 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); 3621 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand(); 3622 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1); 3623 3624 ReplaceNode(N, St); 3625 return; 3626 } 3627 3628 case Intrinsic::arm_neon_vld1: { 3629 static const uint16_t DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16, 3630 ARM::VLD1d32, ARM::VLD1d64 }; 3631 static const uint16_t QOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16, 3632 ARM::VLD1q32, ARM::VLD1q64}; 3633 SelectVLD(N, false, 1, DOpcodes, QOpcodes, nullptr); 3634 return; 3635 } 3636 3637 case Intrinsic::arm_neon_vld2: { 3638 static const uint16_t DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16, 3639 ARM::VLD2d32, ARM::VLD1q64 }; 3640 static const uint16_t QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo, 3641 ARM::VLD2q32Pseudo }; 3642 SelectVLD(N, false, 2, DOpcodes, QOpcodes, nullptr); 3643 return; 3644 } 3645 3646 case Intrinsic::arm_neon_vld3: { 3647 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo, 3648 ARM::VLD3d16Pseudo, 3649 ARM::VLD3d32Pseudo, 3650 ARM::VLD1d64TPseudo }; 3651 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD, 3652 ARM::VLD3q16Pseudo_UPD, 3653 ARM::VLD3q32Pseudo_UPD }; 3654 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo, 3655 ARM::VLD3q16oddPseudo, 3656 ARM::VLD3q32oddPseudo }; 3657 SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1); 3658 return; 3659 } 3660 3661 case Intrinsic::arm_neon_vld4: { 3662 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo, 3663 ARM::VLD4d16Pseudo, 3664 ARM::VLD4d32Pseudo, 3665 ARM::VLD1d64QPseudo }; 3666 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD, 3667 ARM::VLD4q16Pseudo_UPD, 3668 ARM::VLD4q32Pseudo_UPD }; 3669 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo, 3670 ARM::VLD4q16oddPseudo, 3671 ARM::VLD4q32oddPseudo }; 3672 SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1); 3673 return; 3674 } 3675 3676 case Intrinsic::arm_neon_vld2lane: { 3677 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo, 3678 ARM::VLD2LNd16Pseudo, 3679 ARM::VLD2LNd32Pseudo }; 3680 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo, 3681 ARM::VLD2LNq32Pseudo }; 3682 SelectVLDSTLane(N, true, false, 2, DOpcodes, QOpcodes); 3683 return; 3684 } 3685 3686 case Intrinsic::arm_neon_vld3lane: { 3687 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo, 3688 ARM::VLD3LNd16Pseudo, 3689 ARM::VLD3LNd32Pseudo }; 3690 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo, 3691 ARM::VLD3LNq32Pseudo }; 3692 SelectVLDSTLane(N, true, false, 3, DOpcodes, QOpcodes); 3693 return; 3694 } 3695 3696 case Intrinsic::arm_neon_vld4lane: { 3697 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo, 3698 ARM::VLD4LNd16Pseudo, 3699 ARM::VLD4LNd32Pseudo }; 3700 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo, 3701 ARM::VLD4LNq32Pseudo }; 3702 SelectVLDSTLane(N, true, false, 4, DOpcodes, QOpcodes); 3703 return; 3704 } 3705 3706 case Intrinsic::arm_neon_vst1: { 3707 static const uint16_t DOpcodes[] = { ARM::VST1d8, ARM::VST1d16, 3708 ARM::VST1d32, ARM::VST1d64 }; 3709 static const uint16_t QOpcodes[] = { ARM::VST1q8, ARM::VST1q16, 3710 ARM::VST1q32, ARM::VST1q64 }; 3711 SelectVST(N, false, 1, DOpcodes, QOpcodes, nullptr); 3712 return; 3713 } 3714 3715 case Intrinsic::arm_neon_vst2: { 3716 static const uint16_t DOpcodes[] = { ARM::VST2d8, ARM::VST2d16, 3717 ARM::VST2d32, ARM::VST1q64 }; 3718 static const uint16_t QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo, 3719 ARM::VST2q32Pseudo }; 3720 SelectVST(N, false, 2, DOpcodes, QOpcodes, nullptr); 3721 return; 3722 } 3723 3724 case Intrinsic::arm_neon_vst3: { 3725 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo, 3726 ARM::VST3d16Pseudo, 3727 ARM::VST3d32Pseudo, 3728 ARM::VST1d64TPseudo }; 3729 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD, 3730 ARM::VST3q16Pseudo_UPD, 3731 ARM::VST3q32Pseudo_UPD }; 3732 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo, 3733 ARM::VST3q16oddPseudo, 3734 ARM::VST3q32oddPseudo }; 3735 SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1); 3736 return; 3737 } 3738 3739 case Intrinsic::arm_neon_vst4: { 3740 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo, 3741 ARM::VST4d16Pseudo, 3742 ARM::VST4d32Pseudo, 3743 ARM::VST1d64QPseudo }; 3744 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD, 3745 ARM::VST4q16Pseudo_UPD, 3746 ARM::VST4q32Pseudo_UPD }; 3747 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo, 3748 ARM::VST4q16oddPseudo, 3749 ARM::VST4q32oddPseudo }; 3750 SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1); 3751 return; 3752 } 3753 3754 case Intrinsic::arm_neon_vst2lane: { 3755 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo, 3756 ARM::VST2LNd16Pseudo, 3757 ARM::VST2LNd32Pseudo }; 3758 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo, 3759 ARM::VST2LNq32Pseudo }; 3760 SelectVLDSTLane(N, false, false, 2, DOpcodes, QOpcodes); 3761 return; 3762 } 3763 3764 case Intrinsic::arm_neon_vst3lane: { 3765 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo, 3766 ARM::VST3LNd16Pseudo, 3767 ARM::VST3LNd32Pseudo }; 3768 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo, 3769 ARM::VST3LNq32Pseudo }; 3770 SelectVLDSTLane(N, false, false, 3, DOpcodes, QOpcodes); 3771 return; 3772 } 3773 3774 case Intrinsic::arm_neon_vst4lane: { 3775 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo, 3776 ARM::VST4LNd16Pseudo, 3777 ARM::VST4LNd32Pseudo }; 3778 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo, 3779 ARM::VST4LNq32Pseudo }; 3780 SelectVLDSTLane(N, false, false, 4, DOpcodes, QOpcodes); 3781 return; 3782 } 3783 } 3784 break; 3785 } 3786 3787 case ISD::INTRINSIC_WO_CHAIN: { 3788 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 3789 switch (IntNo) { 3790 default: 3791 break; 3792 3793 case Intrinsic::arm_neon_vtbl2: 3794 SelectVTBL(N, false, 2, ARM::VTBL2); 3795 return; 3796 case Intrinsic::arm_neon_vtbl3: 3797 SelectVTBL(N, false, 3, ARM::VTBL3Pseudo); 3798 return; 3799 case Intrinsic::arm_neon_vtbl4: 3800 SelectVTBL(N, false, 4, ARM::VTBL4Pseudo); 3801 return; 3802 3803 case Intrinsic::arm_neon_vtbx2: 3804 SelectVTBL(N, true, 2, ARM::VTBX2); 3805 return; 3806 case Intrinsic::arm_neon_vtbx3: 3807 SelectVTBL(N, true, 3, ARM::VTBX3Pseudo); 3808 return; 3809 case Intrinsic::arm_neon_vtbx4: 3810 SelectVTBL(N, true, 4, ARM::VTBX4Pseudo); 3811 return; 3812 } 3813 break; 3814 } 3815 3816 case ARMISD::VTBL1: { 3817 SDLoc dl(N); 3818 EVT VT = N->getValueType(0); 3819 SDValue Ops[] = {N->getOperand(0), N->getOperand(1), 3820 getAL(CurDAG, dl), // Predicate 3821 CurDAG->getRegister(0, MVT::i32)}; // Predicate Register 3822 ReplaceNode(N, CurDAG->getMachineNode(ARM::VTBL1, dl, VT, Ops)); 3823 return; 3824 } 3825 case ARMISD::VTBL2: { 3826 SDLoc dl(N); 3827 EVT VT = N->getValueType(0); 3828 3829 // Form a REG_SEQUENCE to force register allocation. 3830 SDValue V0 = N->getOperand(0); 3831 SDValue V1 = N->getOperand(1); 3832 SDValue RegSeq = SDValue(createDRegPairNode(MVT::v16i8, V0, V1), 0); 3833 3834 SDValue Ops[] = {RegSeq, N->getOperand(2), getAL(CurDAG, dl), // Predicate 3835 CurDAG->getRegister(0, MVT::i32)}; // Predicate Register 3836 ReplaceNode(N, CurDAG->getMachineNode(ARM::VTBL2, dl, VT, Ops)); 3837 return; 3838 } 3839 3840 case ISD::CONCAT_VECTORS: 3841 SelectConcatVector(N); 3842 return; 3843 3844 case ISD::ATOMIC_CMP_SWAP: 3845 SelectCMP_SWAP(N); 3846 return; 3847 } 3848 3849 SelectCode(N); 3850 } 3851 3852 // Inspect a register string of the form 3853 // cp<coprocessor>:<opc1>:c<CRn>:c<CRm>:<opc2> (32bit) or 3854 // cp<coprocessor>:<opc1>:c<CRm> (64bit) inspect the fields of the string 3855 // and obtain the integer operands from them, adding these operands to the 3856 // provided vector. 3857 static void getIntOperandsFromRegisterString(StringRef RegString, 3858 SelectionDAG *CurDAG, 3859 const SDLoc &DL, 3860 std::vector<SDValue> &Ops) { 3861 SmallVector<StringRef, 5> Fields; 3862 RegString.split(Fields, ':'); 3863 3864 if (Fields.size() > 1) { 3865 bool AllIntFields = true; 3866 3867 for (StringRef Field : Fields) { 3868 // Need to trim out leading 'cp' characters and get the integer field. 3869 unsigned IntField; 3870 AllIntFields &= !Field.trim("CPcp").getAsInteger(10, IntField); 3871 Ops.push_back(CurDAG->getTargetConstant(IntField, DL, MVT::i32)); 3872 } 3873 3874 assert(AllIntFields && 3875 "Unexpected non-integer value in special register string."); 3876 } 3877 } 3878 3879 // Maps a Banked Register string to its mask value. The mask value returned is 3880 // for use in the MRSbanked / MSRbanked instruction nodes as the Banked Register 3881 // mask operand, which expresses which register is to be used, e.g. r8, and in 3882 // which mode it is to be used, e.g. usr. Returns -1 to signify that the string 3883 // was invalid. 3884 static inline int getBankedRegisterMask(StringRef RegString) { 3885 return StringSwitch<int>(RegString.lower()) 3886 .Case("r8_usr", 0x00) 3887 .Case("r9_usr", 0x01) 3888 .Case("r10_usr", 0x02) 3889 .Case("r11_usr", 0x03) 3890 .Case("r12_usr", 0x04) 3891 .Case("sp_usr", 0x05) 3892 .Case("lr_usr", 0x06) 3893 .Case("r8_fiq", 0x08) 3894 .Case("r9_fiq", 0x09) 3895 .Case("r10_fiq", 0x0a) 3896 .Case("r11_fiq", 0x0b) 3897 .Case("r12_fiq", 0x0c) 3898 .Case("sp_fiq", 0x0d) 3899 .Case("lr_fiq", 0x0e) 3900 .Case("lr_irq", 0x10) 3901 .Case("sp_irq", 0x11) 3902 .Case("lr_svc", 0x12) 3903 .Case("sp_svc", 0x13) 3904 .Case("lr_abt", 0x14) 3905 .Case("sp_abt", 0x15) 3906 .Case("lr_und", 0x16) 3907 .Case("sp_und", 0x17) 3908 .Case("lr_mon", 0x1c) 3909 .Case("sp_mon", 0x1d) 3910 .Case("elr_hyp", 0x1e) 3911 .Case("sp_hyp", 0x1f) 3912 .Case("spsr_fiq", 0x2e) 3913 .Case("spsr_irq", 0x30) 3914 .Case("spsr_svc", 0x32) 3915 .Case("spsr_abt", 0x34) 3916 .Case("spsr_und", 0x36) 3917 .Case("spsr_mon", 0x3c) 3918 .Case("spsr_hyp", 0x3e) 3919 .Default(-1); 3920 } 3921 3922 // Maps a MClass special register string to its value for use in the 3923 // t2MRS_M / t2MSR_M instruction nodes as the SYSm value operand. 3924 // Returns -1 to signify that the string was invalid. 3925 static inline int getMClassRegisterSYSmValueMask(StringRef RegString) { 3926 return StringSwitch<int>(RegString.lower()) 3927 .Case("apsr", 0x0) 3928 .Case("iapsr", 0x1) 3929 .Case("eapsr", 0x2) 3930 .Case("xpsr", 0x3) 3931 .Case("ipsr", 0x5) 3932 .Case("epsr", 0x6) 3933 .Case("iepsr", 0x7) 3934 .Case("msp", 0x8) 3935 .Case("psp", 0x9) 3936 .Case("primask", 0x10) 3937 .Case("basepri", 0x11) 3938 .Case("basepri_max", 0x12) 3939 .Case("faultmask", 0x13) 3940 .Case("control", 0x14) 3941 .Case("msplim", 0x0a) 3942 .Case("psplim", 0x0b) 3943 .Case("sp", 0x18) 3944 .Default(-1); 3945 } 3946 3947 // The flags here are common to those allowed for apsr in the A class cores and 3948 // those allowed for the special registers in the M class cores. Returns a 3949 // value representing which flags were present, -1 if invalid. 3950 static inline int getMClassFlagsMask(StringRef Flags, bool hasDSP) { 3951 if (Flags.empty()) 3952 return 0x2 | (int)hasDSP; 3953 3954 return StringSwitch<int>(Flags) 3955 .Case("g", 0x1) 3956 .Case("nzcvq", 0x2) 3957 .Case("nzcvqg", 0x3) 3958 .Default(-1); 3959 } 3960 3961 static int getMClassRegisterMask(StringRef Reg, StringRef Flags, bool IsRead, 3962 const ARMSubtarget *Subtarget) { 3963 // Ensure that the register (without flags) was a valid M Class special 3964 // register. 3965 int SYSmvalue = getMClassRegisterSYSmValueMask(Reg); 3966 if (SYSmvalue == -1) 3967 return -1; 3968 3969 // basepri, basepri_max and faultmask are only valid for V7m. 3970 if (!Subtarget->hasV7Ops() && SYSmvalue >= 0x11 && SYSmvalue <= 0x13) 3971 return -1; 3972 3973 if (Subtarget->has8MSecExt() && Flags.lower() == "ns") { 3974 Flags = ""; 3975 SYSmvalue |= 0x80; 3976 } 3977 3978 if (!Subtarget->has8MSecExt() && 3979 (SYSmvalue == 0xa || SYSmvalue == 0xb || SYSmvalue > 0x14)) 3980 return -1; 3981 3982 if (!Subtarget->hasV8MMainlineOps() && 3983 (SYSmvalue == 0x8a || SYSmvalue == 0x8b || SYSmvalue == 0x91 || 3984 SYSmvalue == 0x93)) 3985 return -1; 3986 3987 // If it was a read then we won't be expecting flags and so at this point 3988 // we can return the mask. 3989 if (IsRead) { 3990 if (Flags.empty()) 3991 return SYSmvalue; 3992 else 3993 return -1; 3994 } 3995 3996 // We know we are now handling a write so need to get the mask for the flags. 3997 int Mask = getMClassFlagsMask(Flags, Subtarget->hasDSP()); 3998 3999 // Only apsr, iapsr, eapsr, xpsr can have flags. The other register values 4000 // shouldn't have flags present. 4001 if ((SYSmvalue < 0x4 && Mask == -1) || (SYSmvalue > 0x4 && !Flags.empty())) 4002 return -1; 4003 4004 // The _g and _nzcvqg versions are only valid if the DSP extension is 4005 // available. 4006 if (!Subtarget->hasDSP() && (Mask & 0x1)) 4007 return -1; 4008 4009 // The register was valid so need to put the mask in the correct place 4010 // (the flags need to be in bits 11-10) and combine with the SYSmvalue to 4011 // construct the operand for the instruction node. 4012 if (SYSmvalue < 0x4) 4013 return SYSmvalue | Mask << 10; 4014 4015 return SYSmvalue; 4016 } 4017 4018 static int getARClassRegisterMask(StringRef Reg, StringRef Flags) { 4019 // The mask operand contains the special register (R Bit) in bit 4, whether 4020 // the register is spsr (R bit is 1) or one of cpsr/apsr (R bit is 0), and 4021 // bits 3-0 contains the fields to be accessed in the special register, set by 4022 // the flags provided with the register. 4023 int Mask = 0; 4024 if (Reg == "apsr") { 4025 // The flags permitted for apsr are the same flags that are allowed in 4026 // M class registers. We get the flag value and then shift the flags into 4027 // the correct place to combine with the mask. 4028 Mask = getMClassFlagsMask(Flags, true); 4029 if (Mask == -1) 4030 return -1; 4031 return Mask << 2; 4032 } 4033 4034 if (Reg != "cpsr" && Reg != "spsr") { 4035 return -1; 4036 } 4037 4038 // This is the same as if the flags were "fc" 4039 if (Flags.empty() || Flags == "all") 4040 return Mask | 0x9; 4041 4042 // Inspect the supplied flags string and set the bits in the mask for 4043 // the relevant and valid flags allowed for cpsr and spsr. 4044 for (char Flag : Flags) { 4045 int FlagVal; 4046 switch (Flag) { 4047 case 'c': 4048 FlagVal = 0x1; 4049 break; 4050 case 'x': 4051 FlagVal = 0x2; 4052 break; 4053 case 's': 4054 FlagVal = 0x4; 4055 break; 4056 case 'f': 4057 FlagVal = 0x8; 4058 break; 4059 default: 4060 FlagVal = 0; 4061 } 4062 4063 // This avoids allowing strings where the same flag bit appears twice. 4064 if (!FlagVal || (Mask & FlagVal)) 4065 return -1; 4066 Mask |= FlagVal; 4067 } 4068 4069 // If the register is spsr then we need to set the R bit. 4070 if (Reg == "spsr") 4071 Mask |= 0x10; 4072 4073 return Mask; 4074 } 4075 4076 // Lower the read_register intrinsic to ARM specific DAG nodes 4077 // using the supplied metadata string to select the instruction node to use 4078 // and the registers/masks to construct as operands for the node. 4079 bool ARMDAGToDAGISel::tryReadRegister(SDNode *N){ 4080 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1)); 4081 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0)); 4082 bool IsThumb2 = Subtarget->isThumb2(); 4083 SDLoc DL(N); 4084 4085 std::vector<SDValue> Ops; 4086 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops); 4087 4088 if (!Ops.empty()) { 4089 // If the special register string was constructed of fields (as defined 4090 // in the ACLE) then need to lower to MRC node (32 bit) or 4091 // MRRC node(64 bit), we can make the distinction based on the number of 4092 // operands we have. 4093 unsigned Opcode; 4094 SmallVector<EVT, 3> ResTypes; 4095 if (Ops.size() == 5){ 4096 Opcode = IsThumb2 ? ARM::t2MRC : ARM::MRC; 4097 ResTypes.append({ MVT::i32, MVT::Other }); 4098 } else { 4099 assert(Ops.size() == 3 && 4100 "Invalid number of fields in special register string."); 4101 Opcode = IsThumb2 ? ARM::t2MRRC : ARM::MRRC; 4102 ResTypes.append({ MVT::i32, MVT::i32, MVT::Other }); 4103 } 4104 4105 Ops.push_back(getAL(CurDAG, DL)); 4106 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); 4107 Ops.push_back(N->getOperand(0)); 4108 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, ResTypes, Ops)); 4109 return true; 4110 } 4111 4112 std::string SpecialReg = RegString->getString().lower(); 4113 4114 int BankedReg = getBankedRegisterMask(SpecialReg); 4115 if (BankedReg != -1) { 4116 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32), 4117 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32), 4118 N->getOperand(0) }; 4119 ReplaceNode( 4120 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSbanked : ARM::MRSbanked, 4121 DL, MVT::i32, MVT::Other, Ops)); 4122 return true; 4123 } 4124 4125 // The VFP registers are read by creating SelectionDAG nodes with opcodes 4126 // corresponding to the register that is being read from. So we switch on the 4127 // string to find which opcode we need to use. 4128 unsigned Opcode = StringSwitch<unsigned>(SpecialReg) 4129 .Case("fpscr", ARM::VMRS) 4130 .Case("fpexc", ARM::VMRS_FPEXC) 4131 .Case("fpsid", ARM::VMRS_FPSID) 4132 .Case("mvfr0", ARM::VMRS_MVFR0) 4133 .Case("mvfr1", ARM::VMRS_MVFR1) 4134 .Case("mvfr2", ARM::VMRS_MVFR2) 4135 .Case("fpinst", ARM::VMRS_FPINST) 4136 .Case("fpinst2", ARM::VMRS_FPINST2) 4137 .Default(0); 4138 4139 // If an opcode was found then we can lower the read to a VFP instruction. 4140 if (Opcode) { 4141 if (!Subtarget->hasVFP2()) 4142 return false; 4143 if (Opcode == ARM::VMRS_MVFR2 && !Subtarget->hasFPARMv8()) 4144 return false; 4145 4146 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32), 4147 N->getOperand(0) }; 4148 ReplaceNode(N, 4149 CurDAG->getMachineNode(Opcode, DL, MVT::i32, MVT::Other, Ops)); 4150 return true; 4151 } 4152 4153 // If the target is M Class then need to validate that the register string 4154 // is an acceptable value, so check that a mask can be constructed from the 4155 // string. 4156 if (Subtarget->isMClass()) { 4157 StringRef Flags = "", Reg = SpecialReg; 4158 if (Reg.endswith("_ns")) { 4159 Flags = "ns"; 4160 Reg = Reg.drop_back(3); 4161 } 4162 4163 int SYSmValue = getMClassRegisterMask(Reg, Flags, true, Subtarget); 4164 if (SYSmValue == -1) 4165 return false; 4166 4167 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32), 4168 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32), 4169 N->getOperand(0) }; 4170 ReplaceNode( 4171 N, CurDAG->getMachineNode(ARM::t2MRS_M, DL, MVT::i32, MVT::Other, Ops)); 4172 return true; 4173 } 4174 4175 // Here we know the target is not M Class so we need to check if it is one 4176 // of the remaining possible values which are apsr, cpsr or spsr. 4177 if (SpecialReg == "apsr" || SpecialReg == "cpsr") { 4178 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32), 4179 N->getOperand(0) }; 4180 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRS_AR : ARM::MRS, 4181 DL, MVT::i32, MVT::Other, Ops)); 4182 return true; 4183 } 4184 4185 if (SpecialReg == "spsr") { 4186 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32), 4187 N->getOperand(0) }; 4188 ReplaceNode( 4189 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSsys_AR : ARM::MRSsys, DL, 4190 MVT::i32, MVT::Other, Ops)); 4191 return true; 4192 } 4193 4194 return false; 4195 } 4196 4197 // Lower the write_register intrinsic to ARM specific DAG nodes 4198 // using the supplied metadata string to select the instruction node to use 4199 // and the registers/masks to use in the nodes 4200 bool ARMDAGToDAGISel::tryWriteRegister(SDNode *N){ 4201 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1)); 4202 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0)); 4203 bool IsThumb2 = Subtarget->isThumb2(); 4204 SDLoc DL(N); 4205 4206 std::vector<SDValue> Ops; 4207 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops); 4208 4209 if (!Ops.empty()) { 4210 // If the special register string was constructed of fields (as defined 4211 // in the ACLE) then need to lower to MCR node (32 bit) or 4212 // MCRR node(64 bit), we can make the distinction based on the number of 4213 // operands we have. 4214 unsigned Opcode; 4215 if (Ops.size() == 5) { 4216 Opcode = IsThumb2 ? ARM::t2MCR : ARM::MCR; 4217 Ops.insert(Ops.begin()+2, N->getOperand(2)); 4218 } else { 4219 assert(Ops.size() == 3 && 4220 "Invalid number of fields in special register string."); 4221 Opcode = IsThumb2 ? ARM::t2MCRR : ARM::MCRR; 4222 SDValue WriteValue[] = { N->getOperand(2), N->getOperand(3) }; 4223 Ops.insert(Ops.begin()+2, WriteValue, WriteValue+2); 4224 } 4225 4226 Ops.push_back(getAL(CurDAG, DL)); 4227 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); 4228 Ops.push_back(N->getOperand(0)); 4229 4230 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops)); 4231 return true; 4232 } 4233 4234 std::string SpecialReg = RegString->getString().lower(); 4235 int BankedReg = getBankedRegisterMask(SpecialReg); 4236 if (BankedReg != -1) { 4237 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32), N->getOperand(2), 4238 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32), 4239 N->getOperand(0) }; 4240 ReplaceNode( 4241 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSRbanked : ARM::MSRbanked, 4242 DL, MVT::Other, Ops)); 4243 return true; 4244 } 4245 4246 // The VFP registers are written to by creating SelectionDAG nodes with 4247 // opcodes corresponding to the register that is being written. So we switch 4248 // on the string to find which opcode we need to use. 4249 unsigned Opcode = StringSwitch<unsigned>(SpecialReg) 4250 .Case("fpscr", ARM::VMSR) 4251 .Case("fpexc", ARM::VMSR_FPEXC) 4252 .Case("fpsid", ARM::VMSR_FPSID) 4253 .Case("fpinst", ARM::VMSR_FPINST) 4254 .Case("fpinst2", ARM::VMSR_FPINST2) 4255 .Default(0); 4256 4257 if (Opcode) { 4258 if (!Subtarget->hasVFP2()) 4259 return false; 4260 Ops = { N->getOperand(2), getAL(CurDAG, DL), 4261 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) }; 4262 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops)); 4263 return true; 4264 } 4265 4266 std::pair<StringRef, StringRef> Fields; 4267 Fields = StringRef(SpecialReg).rsplit('_'); 4268 std::string Reg = Fields.first.str(); 4269 StringRef Flags = Fields.second; 4270 4271 // If the target was M Class then need to validate the special register value 4272 // and retrieve the mask for use in the instruction node. 4273 if (Subtarget->isMClass()) { 4274 // basepri_max gets split so need to correct Reg and Flags. 4275 if (SpecialReg == "basepri_max") { 4276 Reg = SpecialReg; 4277 Flags = ""; 4278 } 4279 int SYSmValue = getMClassRegisterMask(Reg, Flags, false, Subtarget); 4280 if (SYSmValue == -1) 4281 return false; 4282 4283 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32), 4284 N->getOperand(2), getAL(CurDAG, DL), 4285 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) }; 4286 ReplaceNode(N, CurDAG->getMachineNode(ARM::t2MSR_M, DL, MVT::Other, Ops)); 4287 return true; 4288 } 4289 4290 // We then check to see if a valid mask can be constructed for one of the 4291 // register string values permitted for the A and R class cores. These values 4292 // are apsr, spsr and cpsr; these are also valid on older cores. 4293 int Mask = getARClassRegisterMask(Reg, Flags); 4294 if (Mask != -1) { 4295 Ops = { CurDAG->getTargetConstant(Mask, DL, MVT::i32), N->getOperand(2), 4296 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32), 4297 N->getOperand(0) }; 4298 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSR_AR : ARM::MSR, 4299 DL, MVT::Other, Ops)); 4300 return true; 4301 } 4302 4303 return false; 4304 } 4305 4306 bool ARMDAGToDAGISel::tryInlineAsm(SDNode *N){ 4307 std::vector<SDValue> AsmNodeOperands; 4308 unsigned Flag, Kind; 4309 bool Changed = false; 4310 unsigned NumOps = N->getNumOperands(); 4311 4312 // Normally, i64 data is bounded to two arbitrary GRPs for "%r" constraint. 4313 // However, some instrstions (e.g. ldrexd/strexd in ARM mode) require 4314 // (even/even+1) GPRs and use %n and %Hn to refer to the individual regs 4315 // respectively. Since there is no constraint to explicitly specify a 4316 // reg pair, we use GPRPair reg class for "%r" for 64-bit data. For Thumb, 4317 // the 64-bit data may be referred by H, Q, R modifiers, so we still pack 4318 // them into a GPRPair. 4319 4320 SDLoc dl(N); 4321 SDValue Glue = N->getGluedNode() ? N->getOperand(NumOps-1) 4322 : SDValue(nullptr,0); 4323 4324 SmallVector<bool, 8> OpChanged; 4325 // Glue node will be appended late. 4326 for(unsigned i = 0, e = N->getGluedNode() ? NumOps - 1 : NumOps; i < e; ++i) { 4327 SDValue op = N->getOperand(i); 4328 AsmNodeOperands.push_back(op); 4329 4330 if (i < InlineAsm::Op_FirstOperand) 4331 continue; 4332 4333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(i))) { 4334 Flag = C->getZExtValue(); 4335 Kind = InlineAsm::getKind(Flag); 4336 } 4337 else 4338 continue; 4339 4340 // Immediate operands to inline asm in the SelectionDAG are modeled with 4341 // two operands. The first is a constant of value InlineAsm::Kind_Imm, and 4342 // the second is a constant with the value of the immediate. If we get here 4343 // and we have a Kind_Imm, skip the next operand, and continue. 4344 if (Kind == InlineAsm::Kind_Imm) { 4345 SDValue op = N->getOperand(++i); 4346 AsmNodeOperands.push_back(op); 4347 continue; 4348 } 4349 4350 unsigned NumRegs = InlineAsm::getNumOperandRegisters(Flag); 4351 if (NumRegs) 4352 OpChanged.push_back(false); 4353 4354 unsigned DefIdx = 0; 4355 bool IsTiedToChangedOp = false; 4356 // If it's a use that is tied with a previous def, it has no 4357 // reg class constraint. 4358 if (Changed && InlineAsm::isUseOperandTiedToDef(Flag, DefIdx)) 4359 IsTiedToChangedOp = OpChanged[DefIdx]; 4360 4361 // Memory operands to inline asm in the SelectionDAG are modeled with two 4362 // operands: a constant of value InlineAsm::Kind_Mem followed by the input 4363 // operand. If we get here and we have a Kind_Mem, skip the next operand (so 4364 // it doesn't get misinterpreted), and continue. We do this here because 4365 // it's important to update the OpChanged array correctly before moving on. 4366 if (Kind == InlineAsm::Kind_Mem) { 4367 SDValue op = N->getOperand(++i); 4368 AsmNodeOperands.push_back(op); 4369 continue; 4370 } 4371 4372 if (Kind != InlineAsm::Kind_RegUse && Kind != InlineAsm::Kind_RegDef 4373 && Kind != InlineAsm::Kind_RegDefEarlyClobber) 4374 continue; 4375 4376 unsigned RC; 4377 bool HasRC = InlineAsm::hasRegClassConstraint(Flag, RC); 4378 if ((!IsTiedToChangedOp && (!HasRC || RC != ARM::GPRRegClassID)) 4379 || NumRegs != 2) 4380 continue; 4381 4382 assert((i+2 < NumOps) && "Invalid number of operands in inline asm"); 4383 SDValue V0 = N->getOperand(i+1); 4384 SDValue V1 = N->getOperand(i+2); 4385 unsigned Reg0 = cast<RegisterSDNode>(V0)->getReg(); 4386 unsigned Reg1 = cast<RegisterSDNode>(V1)->getReg(); 4387 SDValue PairedReg; 4388 MachineRegisterInfo &MRI = MF->getRegInfo(); 4389 4390 if (Kind == InlineAsm::Kind_RegDef || 4391 Kind == InlineAsm::Kind_RegDefEarlyClobber) { 4392 // Replace the two GPRs with 1 GPRPair and copy values from GPRPair to 4393 // the original GPRs. 4394 4395 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass); 4396 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped); 4397 SDValue Chain = SDValue(N,0); 4398 4399 SDNode *GU = N->getGluedUser(); 4400 SDValue RegCopy = CurDAG->getCopyFromReg(Chain, dl, GPVR, MVT::Untyped, 4401 Chain.getValue(1)); 4402 4403 // Extract values from a GPRPair reg and copy to the original GPR reg. 4404 SDValue Sub0 = CurDAG->getTargetExtractSubreg(ARM::gsub_0, dl, MVT::i32, 4405 RegCopy); 4406 SDValue Sub1 = CurDAG->getTargetExtractSubreg(ARM::gsub_1, dl, MVT::i32, 4407 RegCopy); 4408 SDValue T0 = CurDAG->getCopyToReg(Sub0, dl, Reg0, Sub0, 4409 RegCopy.getValue(1)); 4410 SDValue T1 = CurDAG->getCopyToReg(Sub1, dl, Reg1, Sub1, T0.getValue(1)); 4411 4412 // Update the original glue user. 4413 std::vector<SDValue> Ops(GU->op_begin(), GU->op_end()-1); 4414 Ops.push_back(T1.getValue(1)); 4415 CurDAG->UpdateNodeOperands(GU, Ops); 4416 } 4417 else { 4418 // For Kind == InlineAsm::Kind_RegUse, we first copy two GPRs into a 4419 // GPRPair and then pass the GPRPair to the inline asm. 4420 SDValue Chain = AsmNodeOperands[InlineAsm::Op_InputChain]; 4421 4422 // As REG_SEQ doesn't take RegisterSDNode, we copy them first. 4423 SDValue T0 = CurDAG->getCopyFromReg(Chain, dl, Reg0, MVT::i32, 4424 Chain.getValue(1)); 4425 SDValue T1 = CurDAG->getCopyFromReg(Chain, dl, Reg1, MVT::i32, 4426 T0.getValue(1)); 4427 SDValue Pair = SDValue(createGPRPairNode(MVT::Untyped, T0, T1), 0); 4428 4429 // Copy REG_SEQ into a GPRPair-typed VR and replace the original two 4430 // i32 VRs of inline asm with it. 4431 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass); 4432 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped); 4433 Chain = CurDAG->getCopyToReg(T1, dl, GPVR, Pair, T1.getValue(1)); 4434 4435 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain; 4436 Glue = Chain.getValue(1); 4437 } 4438 4439 Changed = true; 4440 4441 if(PairedReg.getNode()) { 4442 OpChanged[OpChanged.size() -1 ] = true; 4443 Flag = InlineAsm::getFlagWord(Kind, 1 /* RegNum*/); 4444 if (IsTiedToChangedOp) 4445 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, DefIdx); 4446 else 4447 Flag = InlineAsm::getFlagWordForRegClass(Flag, ARM::GPRPairRegClassID); 4448 // Replace the current flag. 4449 AsmNodeOperands[AsmNodeOperands.size() -1] = CurDAG->getTargetConstant( 4450 Flag, dl, MVT::i32); 4451 // Add the new register node and skip the original two GPRs. 4452 AsmNodeOperands.push_back(PairedReg); 4453 // Skip the next two GPRs. 4454 i += 2; 4455 } 4456 } 4457 4458 if (Glue.getNode()) 4459 AsmNodeOperands.push_back(Glue); 4460 if (!Changed) 4461 return false; 4462 4463 SDValue New = CurDAG->getNode(ISD::INLINEASM, SDLoc(N), 4464 CurDAG->getVTList(MVT::Other, MVT::Glue), AsmNodeOperands); 4465 New->setNodeId(-1); 4466 ReplaceNode(N, New.getNode()); 4467 return true; 4468 } 4469 4470 4471 bool ARMDAGToDAGISel:: 4472 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, 4473 std::vector<SDValue> &OutOps) { 4474 switch(ConstraintID) { 4475 default: 4476 llvm_unreachable("Unexpected asm memory constraint"); 4477 case InlineAsm::Constraint_i: 4478 // FIXME: It seems strange that 'i' is needed here since it's supposed to 4479 // be an immediate and not a memory constraint. 4480 LLVM_FALLTHROUGH; 4481 case InlineAsm::Constraint_m: 4482 case InlineAsm::Constraint_o: 4483 case InlineAsm::Constraint_Q: 4484 case InlineAsm::Constraint_Um: 4485 case InlineAsm::Constraint_Un: 4486 case InlineAsm::Constraint_Uq: 4487 case InlineAsm::Constraint_Us: 4488 case InlineAsm::Constraint_Ut: 4489 case InlineAsm::Constraint_Uv: 4490 case InlineAsm::Constraint_Uy: 4491 // Require the address to be in a register. That is safe for all ARM 4492 // variants and it is hard to do anything much smarter without knowing 4493 // how the operand is used. 4494 OutOps.push_back(Op); 4495 return false; 4496 } 4497 return true; 4498 } 4499 4500 /// createARMISelDag - This pass converts a legalized DAG into a 4501 /// ARM-specific DAG, ready for instruction scheduling. 4502 /// 4503 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM, 4504 CodeGenOpt::Level OptLevel) { 4505 return new ARMDAGToDAGISel(TM, OptLevel); 4506 } 4507