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