1 //===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===// 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 the ARM-specific support for the FastISel class. Some 11 // of the target-specific code is generated by tablegen in the file 12 // ARMGenFastISel.inc, which is #included here. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "ARM.h" 17 #include "ARMBaseInstrInfo.h" 18 #include "ARMBaseRegisterInfo.h" 19 #include "ARMCallingConv.h" 20 #include "ARMConstantPoolValue.h" 21 #include "ARMISelLowering.h" 22 #include "ARMMachineFunctionInfo.h" 23 #include "ARMSubtarget.h" 24 #include "MCTargetDesc/ARMAddressingModes.h" 25 #include "MCTargetDesc/ARMBaseInfo.h" 26 #include "llvm/ADT/APFloat.h" 27 #include "llvm/ADT/APInt.h" 28 #include "llvm/ADT/DenseMap.h" 29 #include "llvm/ADT/STLExtras.h" 30 #include "llvm/ADT/SmallVector.h" 31 #include "llvm/CodeGen/CallingConvLower.h" 32 #include "llvm/CodeGen/FastISel.h" 33 #include "llvm/CodeGen/FunctionLoweringInfo.h" 34 #include "llvm/CodeGen/ISDOpcodes.h" 35 #include "llvm/CodeGen/MachineConstantPool.h" 36 #include "llvm/CodeGen/MachineFrameInfo.h" 37 #include "llvm/CodeGen/MachineInstr.h" 38 #include "llvm/CodeGen/MachineInstrBuilder.h" 39 #include "llvm/CodeGen/MachineMemOperand.h" 40 #include "llvm/CodeGen/MachineOperand.h" 41 #include "llvm/CodeGen/MachineRegisterInfo.h" 42 #include "llvm/CodeGen/MachineValueType.h" 43 #include "llvm/CodeGen/RuntimeLibcalls.h" 44 #include "llvm/CodeGen/ValueTypes.h" 45 #include "llvm/IR/Argument.h" 46 #include "llvm/IR/Attributes.h" 47 #include "llvm/IR/CallSite.h" 48 #include "llvm/IR/CallingConv.h" 49 #include "llvm/IR/Constant.h" 50 #include "llvm/IR/Constants.h" 51 #include "llvm/IR/DataLayout.h" 52 #include "llvm/IR/DerivedTypes.h" 53 #include "llvm/IR/Function.h" 54 #include "llvm/IR/GetElementPtrTypeIterator.h" 55 #include "llvm/IR/GlobalValue.h" 56 #include "llvm/IR/GlobalVariable.h" 57 #include "llvm/IR/InstrTypes.h" 58 #include "llvm/IR/Instruction.h" 59 #include "llvm/IR/Instructions.h" 60 #include "llvm/IR/IntrinsicInst.h" 61 #include "llvm/IR/Module.h" 62 #include "llvm/IR/Operator.h" 63 #include "llvm/IR/Type.h" 64 #include "llvm/IR/User.h" 65 #include "llvm/IR/Value.h" 66 #include "llvm/MC/MCInstrDesc.h" 67 #include "llvm/MC/MCRegisterInfo.h" 68 #include "llvm/Support/Casting.h" 69 #include "llvm/Support/Compiler.h" 70 #include "llvm/Support/ErrorHandling.h" 71 #include "llvm/Support/MathExtras.h" 72 #include "llvm/Target/TargetInstrInfo.h" 73 #include "llvm/Target/TargetLowering.h" 74 #include "llvm/Target/TargetMachine.h" 75 #include "llvm/Target/TargetOptions.h" 76 #include <cassert> 77 #include <cstdint> 78 #include <utility> 79 80 using namespace llvm; 81 82 namespace { 83 84 // All possible address modes, plus some. 85 typedef struct Address { 86 enum { 87 RegBase, 88 FrameIndexBase 89 } BaseType = RegBase; 90 91 union { 92 unsigned Reg; 93 int FI; 94 } Base; 95 96 int Offset = 0; 97 98 // Innocuous defaults for our address. 99 Address() { 100 Base.Reg = 0; 101 } 102 } Address; 103 104 class ARMFastISel final : public FastISel { 105 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can 106 /// make the right decision when generating code for different targets. 107 const ARMSubtarget *Subtarget; 108 Module &M; 109 const TargetMachine &TM; 110 const TargetInstrInfo &TII; 111 const TargetLowering &TLI; 112 ARMFunctionInfo *AFI; 113 114 // Convenience variables to avoid some queries. 115 bool isThumb2; 116 LLVMContext *Context; 117 118 public: 119 explicit ARMFastISel(FunctionLoweringInfo &funcInfo, 120 const TargetLibraryInfo *libInfo) 121 : FastISel(funcInfo, libInfo), 122 Subtarget( 123 &static_cast<const ARMSubtarget &>(funcInfo.MF->getSubtarget())), 124 M(const_cast<Module &>(*funcInfo.Fn->getParent())), 125 TM(funcInfo.MF->getTarget()), TII(*Subtarget->getInstrInfo()), 126 TLI(*Subtarget->getTargetLowering()) { 127 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>(); 128 isThumb2 = AFI->isThumbFunction(); 129 Context = &funcInfo.Fn->getContext(); 130 } 131 132 private: 133 // Code from FastISel.cpp. 134 135 unsigned fastEmitInst_r(unsigned MachineInstOpcode, 136 const TargetRegisterClass *RC, 137 unsigned Op0, bool Op0IsKill); 138 unsigned fastEmitInst_rr(unsigned MachineInstOpcode, 139 const TargetRegisterClass *RC, 140 unsigned Op0, bool Op0IsKill, 141 unsigned Op1, bool Op1IsKill); 142 unsigned fastEmitInst_ri(unsigned MachineInstOpcode, 143 const TargetRegisterClass *RC, 144 unsigned Op0, bool Op0IsKill, 145 uint64_t Imm); 146 unsigned fastEmitInst_i(unsigned MachineInstOpcode, 147 const TargetRegisterClass *RC, 148 uint64_t Imm); 149 150 // Backend specific FastISel code. 151 152 bool fastSelectInstruction(const Instruction *I) override; 153 unsigned fastMaterializeConstant(const Constant *C) override; 154 unsigned fastMaterializeAlloca(const AllocaInst *AI) override; 155 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo, 156 const LoadInst *LI) override; 157 bool fastLowerArguments() override; 158 159 #include "ARMGenFastISel.inc" 160 161 // Instruction selection routines. 162 163 bool SelectLoad(const Instruction *I); 164 bool SelectStore(const Instruction *I); 165 bool SelectBranch(const Instruction *I); 166 bool SelectIndirectBr(const Instruction *I); 167 bool SelectCmp(const Instruction *I); 168 bool SelectFPExt(const Instruction *I); 169 bool SelectFPTrunc(const Instruction *I); 170 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode); 171 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode); 172 bool SelectIToFP(const Instruction *I, bool isSigned); 173 bool SelectFPToI(const Instruction *I, bool isSigned); 174 bool SelectDiv(const Instruction *I, bool isSigned); 175 bool SelectRem(const Instruction *I, bool isSigned); 176 bool SelectCall(const Instruction *I, const char *IntrMemName); 177 bool SelectIntrinsicCall(const IntrinsicInst &I); 178 bool SelectSelect(const Instruction *I); 179 bool SelectRet(const Instruction *I); 180 bool SelectTrunc(const Instruction *I); 181 bool SelectIntExt(const Instruction *I); 182 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy); 183 184 // Utility routines. 185 186 bool isPositionIndependent() const; 187 bool isTypeLegal(Type *Ty, MVT &VT); 188 bool isLoadTypeLegal(Type *Ty, MVT &VT); 189 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value, 190 bool isZExt, bool isEquality); 191 bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr, 192 unsigned Alignment = 0, bool isZExt = true, 193 bool allocReg = true); 194 bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr, 195 unsigned Alignment = 0); 196 bool ARMComputeAddress(const Value *Obj, Address &Addr); 197 void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3); 198 bool ARMIsMemCpySmall(uint64_t Len); 199 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len, 200 unsigned Alignment); 201 unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt); 202 unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT); 203 unsigned ARMMaterializeInt(const Constant *C, MVT VT); 204 unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT); 205 unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg); 206 unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg); 207 unsigned ARMSelectCallOp(bool UseReg); 208 unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT); 209 210 const TargetLowering *getTargetLowering() { return &TLI; } 211 212 // Call handling routines. 213 214 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, 215 bool Return, 216 bool isVarArg); 217 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args, 218 SmallVectorImpl<unsigned> &ArgRegs, 219 SmallVectorImpl<MVT> &ArgVTs, 220 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, 221 SmallVectorImpl<unsigned> &RegArgs, 222 CallingConv::ID CC, 223 unsigned &NumBytes, 224 bool isVarArg); 225 unsigned getLibcallReg(const Twine &Name); 226 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, 227 const Instruction *I, CallingConv::ID CC, 228 unsigned &NumBytes, bool isVarArg); 229 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call); 230 231 // OptionalDef handling routines. 232 233 bool isARMNEONPred(const MachineInstr *MI); 234 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR); 235 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB); 236 void AddLoadStoreOperands(MVT VT, Address &Addr, 237 const MachineInstrBuilder &MIB, 238 MachineMemOperand::Flags Flags, bool useAM3); 239 }; 240 241 } // end anonymous namespace 242 243 #include "ARMGenCallingConv.inc" 244 245 // DefinesOptionalPredicate - This is different from DefinesPredicate in that 246 // we don't care about implicit defs here, just places we'll need to add a 247 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR. 248 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) { 249 if (!MI->hasOptionalDef()) 250 return false; 251 252 // Look to see if our OptionalDef is defining CPSR or CCR. 253 for (const MachineOperand &MO : MI->operands()) { 254 if (!MO.isReg() || !MO.isDef()) continue; 255 if (MO.getReg() == ARM::CPSR) 256 *CPSR = true; 257 } 258 return true; 259 } 260 261 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) { 262 const MCInstrDesc &MCID = MI->getDesc(); 263 264 // If we're a thumb2 or not NEON function we'll be handled via isPredicable. 265 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON || 266 AFI->isThumb2Function()) 267 return MI->isPredicable(); 268 269 for (const MCOperandInfo &opInfo : MCID.operands()) 270 if (opInfo.isPredicate()) 271 return true; 272 273 return false; 274 } 275 276 // If the machine is predicable go ahead and add the predicate operands, if 277 // it needs default CC operands add those. 278 // TODO: If we want to support thumb1 then we'll need to deal with optional 279 // CPSR defs that need to be added before the remaining operands. See s_cc_out 280 // for descriptions why. 281 const MachineInstrBuilder & 282 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) { 283 MachineInstr *MI = &*MIB; 284 285 // Do we use a predicate? or... 286 // Are we NEON in ARM mode and have a predicate operand? If so, I know 287 // we're not predicable but add it anyways. 288 if (isARMNEONPred(MI)) 289 MIB.add(predOps(ARMCC::AL)); 290 291 // Do we optionally set a predicate? Preds is size > 0 iff the predicate 292 // defines CPSR. All other OptionalDefines in ARM are the CCR register. 293 bool CPSR = false; 294 if (DefinesOptionalPredicate(MI, &CPSR)) 295 MIB.add(CPSR ? t1CondCodeOp() : condCodeOp()); 296 return MIB; 297 } 298 299 unsigned ARMFastISel::fastEmitInst_r(unsigned MachineInstOpcode, 300 const TargetRegisterClass *RC, 301 unsigned Op0, bool Op0IsKill) { 302 unsigned ResultReg = createResultReg(RC); 303 const MCInstrDesc &II = TII.get(MachineInstOpcode); 304 305 // Make sure the input operand is sufficiently constrained to be legal 306 // for this instruction. 307 Op0 = constrainOperandRegClass(II, Op0, 1); 308 if (II.getNumDefs() >= 1) { 309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, 310 ResultReg).addReg(Op0, Op0IsKill * RegState::Kill)); 311 } else { 312 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 313 .addReg(Op0, Op0IsKill * RegState::Kill)); 314 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 315 TII.get(TargetOpcode::COPY), ResultReg) 316 .addReg(II.ImplicitDefs[0])); 317 } 318 return ResultReg; 319 } 320 321 unsigned ARMFastISel::fastEmitInst_rr(unsigned MachineInstOpcode, 322 const TargetRegisterClass *RC, 323 unsigned Op0, bool Op0IsKill, 324 unsigned Op1, bool Op1IsKill) { 325 unsigned ResultReg = createResultReg(RC); 326 const MCInstrDesc &II = TII.get(MachineInstOpcode); 327 328 // Make sure the input operands are sufficiently constrained to be legal 329 // for this instruction. 330 Op0 = constrainOperandRegClass(II, Op0, 1); 331 Op1 = constrainOperandRegClass(II, Op1, 2); 332 333 if (II.getNumDefs() >= 1) { 334 AddOptionalDefs( 335 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 336 .addReg(Op0, Op0IsKill * RegState::Kill) 337 .addReg(Op1, Op1IsKill * RegState::Kill)); 338 } else { 339 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 340 .addReg(Op0, Op0IsKill * RegState::Kill) 341 .addReg(Op1, Op1IsKill * RegState::Kill)); 342 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 343 TII.get(TargetOpcode::COPY), ResultReg) 344 .addReg(II.ImplicitDefs[0])); 345 } 346 return ResultReg; 347 } 348 349 unsigned ARMFastISel::fastEmitInst_ri(unsigned MachineInstOpcode, 350 const TargetRegisterClass *RC, 351 unsigned Op0, bool Op0IsKill, 352 uint64_t Imm) { 353 unsigned ResultReg = createResultReg(RC); 354 const MCInstrDesc &II = TII.get(MachineInstOpcode); 355 356 // Make sure the input operand is sufficiently constrained to be legal 357 // for this instruction. 358 Op0 = constrainOperandRegClass(II, Op0, 1); 359 if (II.getNumDefs() >= 1) { 360 AddOptionalDefs( 361 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 362 .addReg(Op0, Op0IsKill * RegState::Kill) 363 .addImm(Imm)); 364 } else { 365 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 366 .addReg(Op0, Op0IsKill * RegState::Kill) 367 .addImm(Imm)); 368 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 369 TII.get(TargetOpcode::COPY), ResultReg) 370 .addReg(II.ImplicitDefs[0])); 371 } 372 return ResultReg; 373 } 374 375 unsigned ARMFastISel::fastEmitInst_i(unsigned MachineInstOpcode, 376 const TargetRegisterClass *RC, 377 uint64_t Imm) { 378 unsigned ResultReg = createResultReg(RC); 379 const MCInstrDesc &II = TII.get(MachineInstOpcode); 380 381 if (II.getNumDefs() >= 1) { 382 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, 383 ResultReg).addImm(Imm)); 384 } else { 385 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 386 .addImm(Imm)); 387 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 388 TII.get(TargetOpcode::COPY), ResultReg) 389 .addReg(II.ImplicitDefs[0])); 390 } 391 return ResultReg; 392 } 393 394 // TODO: Don't worry about 64-bit now, but when this is fixed remove the 395 // checks from the various callers. 396 unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) { 397 if (VT == MVT::f64) return 0; 398 399 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); 400 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 401 TII.get(ARM::VMOVSR), MoveReg) 402 .addReg(SrcReg)); 403 return MoveReg; 404 } 405 406 unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) { 407 if (VT == MVT::i64) return 0; 408 409 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); 410 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 411 TII.get(ARM::VMOVRS), MoveReg) 412 .addReg(SrcReg)); 413 return MoveReg; 414 } 415 416 // For double width floating point we need to materialize two constants 417 // (the high and the low) into integer registers then use a move to get 418 // the combined constant into an FP reg. 419 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) { 420 const APFloat Val = CFP->getValueAPF(); 421 bool is64bit = VT == MVT::f64; 422 423 // This checks to see if we can use VFP3 instructions to materialize 424 // a constant, otherwise we have to go through the constant pool. 425 if (TLI.isFPImmLegal(Val, VT)) { 426 int Imm; 427 unsigned Opc; 428 if (is64bit) { 429 Imm = ARM_AM::getFP64Imm(Val); 430 Opc = ARM::FCONSTD; 431 } else { 432 Imm = ARM_AM::getFP32Imm(Val); 433 Opc = ARM::FCONSTS; 434 } 435 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); 436 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 437 TII.get(Opc), DestReg).addImm(Imm)); 438 return DestReg; 439 } 440 441 // Require VFP2 for loading fp constants. 442 if (!Subtarget->hasVFP2()) return false; 443 444 // MachineConstantPool wants an explicit alignment. 445 unsigned Align = DL.getPrefTypeAlignment(CFP->getType()); 446 if (Align == 0) { 447 // TODO: Figure out if this is correct. 448 Align = DL.getTypeAllocSize(CFP->getType()); 449 } 450 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align); 451 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); 452 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS; 453 454 // The extra reg is for addrmode5. 455 AddOptionalDefs( 456 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg) 457 .addConstantPoolIndex(Idx) 458 .addReg(0)); 459 return DestReg; 460 } 461 462 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) { 463 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1) 464 return 0; 465 466 // If we can do this in a single instruction without a constant pool entry 467 // do so now. 468 const ConstantInt *CI = cast<ConstantInt>(C); 469 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) { 470 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16; 471 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass : 472 &ARM::GPRRegClass; 473 unsigned ImmReg = createResultReg(RC); 474 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 475 TII.get(Opc), ImmReg) 476 .addImm(CI->getZExtValue())); 477 return ImmReg; 478 } 479 480 // Use MVN to emit negative constants. 481 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) { 482 unsigned Imm = (unsigned)~(CI->getSExtValue()); 483 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : 484 (ARM_AM::getSOImmVal(Imm) != -1); 485 if (UseImm) { 486 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi; 487 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass : 488 &ARM::GPRRegClass; 489 unsigned ImmReg = createResultReg(RC); 490 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 491 TII.get(Opc), ImmReg) 492 .addImm(Imm)); 493 return ImmReg; 494 } 495 } 496 497 unsigned ResultReg = 0; 498 if (Subtarget->useMovt(*FuncInfo.MF)) 499 ResultReg = fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue()); 500 501 if (ResultReg) 502 return ResultReg; 503 504 // Load from constant pool. For now 32-bit only. 505 if (VT != MVT::i32) 506 return 0; 507 508 // MachineConstantPool wants an explicit alignment. 509 unsigned Align = DL.getPrefTypeAlignment(C->getType()); 510 if (Align == 0) { 511 // TODO: Figure out if this is correct. 512 Align = DL.getTypeAllocSize(C->getType()); 513 } 514 unsigned Idx = MCP.getConstantPoolIndex(C, Align); 515 ResultReg = createResultReg(TLI.getRegClassFor(VT)); 516 if (isThumb2) 517 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 518 TII.get(ARM::t2LDRpci), ResultReg) 519 .addConstantPoolIndex(Idx)); 520 else { 521 // The extra immediate is for addrmode2. 522 ResultReg = constrainOperandRegClass(TII.get(ARM::LDRcp), ResultReg, 0); 523 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 524 TII.get(ARM::LDRcp), ResultReg) 525 .addConstantPoolIndex(Idx) 526 .addImm(0)); 527 } 528 return ResultReg; 529 } 530 531 bool ARMFastISel::isPositionIndependent() const { 532 return TLI.isPositionIndependent(); 533 } 534 535 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) { 536 // For now 32-bit only. 537 if (VT != MVT::i32 || GV->isThreadLocal()) return 0; 538 539 // ROPI/RWPI not currently supported. 540 if (Subtarget->isROPI() || Subtarget->isRWPI()) 541 return 0; 542 543 bool IsIndirect = Subtarget->isGVIndirectSymbol(GV); 544 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass 545 : &ARM::GPRRegClass; 546 unsigned DestReg = createResultReg(RC); 547 548 // FastISel TLS support on non-MachO is broken, punt to SelectionDAG. 549 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 550 bool IsThreadLocal = GVar && GVar->isThreadLocal(); 551 if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0; 552 553 bool IsPositionIndependent = isPositionIndependent(); 554 // Use movw+movt when possible, it avoids constant pool entries. 555 // Non-darwin targets only support static movt relocations in FastISel. 556 if (Subtarget->useMovt(*FuncInfo.MF) && 557 (Subtarget->isTargetMachO() || !IsPositionIndependent)) { 558 unsigned Opc; 559 unsigned char TF = 0; 560 if (Subtarget->isTargetMachO()) 561 TF = ARMII::MO_NONLAZY; 562 563 if (IsPositionIndependent) 564 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel; 565 else 566 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm; 567 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 568 TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF)); 569 } else { 570 // MachineConstantPool wants an explicit alignment. 571 unsigned Align = DL.getPrefTypeAlignment(GV->getType()); 572 if (Align == 0) { 573 // TODO: Figure out if this is correct. 574 Align = DL.getTypeAllocSize(GV->getType()); 575 } 576 577 if (Subtarget->isTargetELF() && IsPositionIndependent) 578 return ARMLowerPICELF(GV, Align, VT); 579 580 // Grab index. 581 unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0; 582 unsigned Id = AFI->createPICLabelUId(); 583 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id, 584 ARMCP::CPValue, 585 PCAdj); 586 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align); 587 588 // Load value. 589 MachineInstrBuilder MIB; 590 if (isThumb2) { 591 unsigned Opc = IsPositionIndependent ? ARM::t2LDRpci_pic : ARM::t2LDRpci; 592 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), 593 DestReg).addConstantPoolIndex(Idx); 594 if (IsPositionIndependent) 595 MIB.addImm(Id); 596 AddOptionalDefs(MIB); 597 } else { 598 // The extra immediate is for addrmode2. 599 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0); 600 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 601 TII.get(ARM::LDRcp), DestReg) 602 .addConstantPoolIndex(Idx) 603 .addImm(0); 604 AddOptionalDefs(MIB); 605 606 if (IsPositionIndependent) { 607 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD; 608 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT)); 609 610 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, 611 DbgLoc, TII.get(Opc), NewDestReg) 612 .addReg(DestReg) 613 .addImm(Id); 614 AddOptionalDefs(MIB); 615 return NewDestReg; 616 } 617 } 618 } 619 620 if (IsIndirect) { 621 MachineInstrBuilder MIB; 622 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT)); 623 if (isThumb2) 624 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 625 TII.get(ARM::t2LDRi12), NewDestReg) 626 .addReg(DestReg) 627 .addImm(0); 628 else 629 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 630 TII.get(ARM::LDRi12), NewDestReg) 631 .addReg(DestReg) 632 .addImm(0); 633 DestReg = NewDestReg; 634 AddOptionalDefs(MIB); 635 } 636 637 return DestReg; 638 } 639 640 unsigned ARMFastISel::fastMaterializeConstant(const Constant *C) { 641 EVT CEVT = TLI.getValueType(DL, C->getType(), true); 642 643 // Only handle simple types. 644 if (!CEVT.isSimple()) return 0; 645 MVT VT = CEVT.getSimpleVT(); 646 647 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) 648 return ARMMaterializeFP(CFP, VT); 649 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) 650 return ARMMaterializeGV(GV, VT); 651 else if (isa<ConstantInt>(C)) 652 return ARMMaterializeInt(C, VT); 653 654 return 0; 655 } 656 657 // TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF); 658 659 unsigned ARMFastISel::fastMaterializeAlloca(const AllocaInst *AI) { 660 // Don't handle dynamic allocas. 661 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0; 662 663 MVT VT; 664 if (!isLoadTypeLegal(AI->getType(), VT)) return 0; 665 666 DenseMap<const AllocaInst*, int>::iterator SI = 667 FuncInfo.StaticAllocaMap.find(AI); 668 669 // This will get lowered later into the correct offsets and registers 670 // via rewriteXFrameIndex. 671 if (SI != FuncInfo.StaticAllocaMap.end()) { 672 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri; 673 const TargetRegisterClass* RC = TLI.getRegClassFor(VT); 674 unsigned ResultReg = createResultReg(RC); 675 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0); 676 677 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 678 TII.get(Opc), ResultReg) 679 .addFrameIndex(SI->second) 680 .addImm(0)); 681 return ResultReg; 682 } 683 684 return 0; 685 } 686 687 bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) { 688 EVT evt = TLI.getValueType(DL, Ty, true); 689 690 // Only handle simple types. 691 if (evt == MVT::Other || !evt.isSimple()) return false; 692 VT = evt.getSimpleVT(); 693 694 // Handle all legal types, i.e. a register that will directly hold this 695 // value. 696 return TLI.isTypeLegal(VT); 697 } 698 699 bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) { 700 if (isTypeLegal(Ty, VT)) return true; 701 702 // If this is a type than can be sign or zero-extended to a basic operation 703 // go ahead and accept it now. 704 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) 705 return true; 706 707 return false; 708 } 709 710 // Computes the address to get to an object. 711 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) { 712 // Some boilerplate from the X86 FastISel. 713 const User *U = nullptr; 714 unsigned Opcode = Instruction::UserOp1; 715 if (const Instruction *I = dyn_cast<Instruction>(Obj)) { 716 // Don't walk into other basic blocks unless the object is an alloca from 717 // another block, otherwise it may not have a virtual register assigned. 718 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) || 719 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) { 720 Opcode = I->getOpcode(); 721 U = I; 722 } 723 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { 724 Opcode = C->getOpcode(); 725 U = C; 726 } 727 728 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType())) 729 if (Ty->getAddressSpace() > 255) 730 // Fast instruction selection doesn't support the special 731 // address spaces. 732 return false; 733 734 switch (Opcode) { 735 default: 736 break; 737 case Instruction::BitCast: 738 // Look through bitcasts. 739 return ARMComputeAddress(U->getOperand(0), Addr); 740 case Instruction::IntToPtr: 741 // Look past no-op inttoptrs. 742 if (TLI.getValueType(DL, U->getOperand(0)->getType()) == 743 TLI.getPointerTy(DL)) 744 return ARMComputeAddress(U->getOperand(0), Addr); 745 break; 746 case Instruction::PtrToInt: 747 // Look past no-op ptrtoints. 748 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL)) 749 return ARMComputeAddress(U->getOperand(0), Addr); 750 break; 751 case Instruction::GetElementPtr: { 752 Address SavedAddr = Addr; 753 int TmpOffset = Addr.Offset; 754 755 // Iterate through the GEP folding the constants into offsets where 756 // we can. 757 gep_type_iterator GTI = gep_type_begin(U); 758 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); 759 i != e; ++i, ++GTI) { 760 const Value *Op = *i; 761 if (StructType *STy = GTI.getStructTypeOrNull()) { 762 const StructLayout *SL = DL.getStructLayout(STy); 763 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); 764 TmpOffset += SL->getElementOffset(Idx); 765 } else { 766 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType()); 767 while (true) { 768 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { 769 // Constant-offset addressing. 770 TmpOffset += CI->getSExtValue() * S; 771 break; 772 } 773 if (canFoldAddIntoGEP(U, Op)) { 774 // A compatible add with a constant operand. Fold the constant. 775 ConstantInt *CI = 776 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1)); 777 TmpOffset += CI->getSExtValue() * S; 778 // Iterate on the other operand. 779 Op = cast<AddOperator>(Op)->getOperand(0); 780 continue; 781 } 782 // Unsupported 783 goto unsupported_gep; 784 } 785 } 786 } 787 788 // Try to grab the base operand now. 789 Addr.Offset = TmpOffset; 790 if (ARMComputeAddress(U->getOperand(0), Addr)) return true; 791 792 // We failed, restore everything and try the other options. 793 Addr = SavedAddr; 794 795 unsupported_gep: 796 break; 797 } 798 case Instruction::Alloca: { 799 const AllocaInst *AI = cast<AllocaInst>(Obj); 800 DenseMap<const AllocaInst*, int>::iterator SI = 801 FuncInfo.StaticAllocaMap.find(AI); 802 if (SI != FuncInfo.StaticAllocaMap.end()) { 803 Addr.BaseType = Address::FrameIndexBase; 804 Addr.Base.FI = SI->second; 805 return true; 806 } 807 break; 808 } 809 } 810 811 // Try to get this in a register if nothing else has worked. 812 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj); 813 return Addr.Base.Reg != 0; 814 } 815 816 void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) { 817 bool needsLowering = false; 818 switch (VT.SimpleTy) { 819 default: llvm_unreachable("Unhandled load/store type!"); 820 case MVT::i1: 821 case MVT::i8: 822 case MVT::i16: 823 case MVT::i32: 824 if (!useAM3) { 825 // Integer loads/stores handle 12-bit offsets. 826 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset); 827 // Handle negative offsets. 828 if (needsLowering && isThumb2) 829 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 && 830 Addr.Offset > -256); 831 } else { 832 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets. 833 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255); 834 } 835 break; 836 case MVT::f32: 837 case MVT::f64: 838 // Floating point operands handle 8-bit offsets. 839 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset); 840 break; 841 } 842 843 // If this is a stack pointer and the offset needs to be simplified then 844 // put the alloca address into a register, set the base type back to 845 // register and continue. This should almost never happen. 846 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) { 847 const TargetRegisterClass *RC = isThumb2 ? &ARM::tGPRRegClass 848 : &ARM::GPRRegClass; 849 unsigned ResultReg = createResultReg(RC); 850 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri; 851 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 852 TII.get(Opc), ResultReg) 853 .addFrameIndex(Addr.Base.FI) 854 .addImm(0)); 855 Addr.Base.Reg = ResultReg; 856 Addr.BaseType = Address::RegBase; 857 } 858 859 // Since the offset is too large for the load/store instruction 860 // get the reg+offset into a register. 861 if (needsLowering) { 862 Addr.Base.Reg = fastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg, 863 /*Op0IsKill*/false, Addr.Offset, MVT::i32); 864 Addr.Offset = 0; 865 } 866 } 867 868 void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr, 869 const MachineInstrBuilder &MIB, 870 MachineMemOperand::Flags Flags, 871 bool useAM3) { 872 // addrmode5 output depends on the selection dag addressing dividing the 873 // offset by 4 that it then later multiplies. Do this here as well. 874 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64) 875 Addr.Offset /= 4; 876 877 // Frame base works a bit differently. Handle it separately. 878 if (Addr.BaseType == Address::FrameIndexBase) { 879 int FI = Addr.Base.FI; 880 int Offset = Addr.Offset; 881 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand( 882 MachinePointerInfo::getFixedStack(*FuncInfo.MF, FI, Offset), Flags, 883 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI)); 884 // Now add the rest of the operands. 885 MIB.addFrameIndex(FI); 886 887 // ARM halfword load/stores and signed byte loads need an additional 888 // operand. 889 if (useAM3) { 890 int Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset; 891 MIB.addReg(0); 892 MIB.addImm(Imm); 893 } else { 894 MIB.addImm(Addr.Offset); 895 } 896 MIB.addMemOperand(MMO); 897 } else { 898 // Now add the rest of the operands. 899 MIB.addReg(Addr.Base.Reg); 900 901 // ARM halfword load/stores and signed byte loads need an additional 902 // operand. 903 if (useAM3) { 904 int Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset; 905 MIB.addReg(0); 906 MIB.addImm(Imm); 907 } else { 908 MIB.addImm(Addr.Offset); 909 } 910 } 911 AddOptionalDefs(MIB); 912 } 913 914 bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr, 915 unsigned Alignment, bool isZExt, bool allocReg) { 916 unsigned Opc; 917 bool useAM3 = false; 918 bool needVMOV = false; 919 const TargetRegisterClass *RC; 920 switch (VT.SimpleTy) { 921 // This is mostly going to be Neon/vector support. 922 default: return false; 923 case MVT::i1: 924 case MVT::i8: 925 if (isThumb2) { 926 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 927 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8; 928 else 929 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12; 930 } else { 931 if (isZExt) { 932 Opc = ARM::LDRBi12; 933 } else { 934 Opc = ARM::LDRSB; 935 useAM3 = true; 936 } 937 } 938 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass; 939 break; 940 case MVT::i16: 941 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem()) 942 return false; 943 944 if (isThumb2) { 945 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 946 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8; 947 else 948 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12; 949 } else { 950 Opc = isZExt ? ARM::LDRH : ARM::LDRSH; 951 useAM3 = true; 952 } 953 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass; 954 break; 955 case MVT::i32: 956 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem()) 957 return false; 958 959 if (isThumb2) { 960 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 961 Opc = ARM::t2LDRi8; 962 else 963 Opc = ARM::t2LDRi12; 964 } else { 965 Opc = ARM::LDRi12; 966 } 967 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass; 968 break; 969 case MVT::f32: 970 if (!Subtarget->hasVFP2()) return false; 971 // Unaligned loads need special handling. Floats require word-alignment. 972 if (Alignment && Alignment < 4) { 973 needVMOV = true; 974 VT = MVT::i32; 975 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12; 976 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass; 977 } else { 978 Opc = ARM::VLDRS; 979 RC = TLI.getRegClassFor(VT); 980 } 981 break; 982 case MVT::f64: 983 if (!Subtarget->hasVFP2()) return false; 984 // FIXME: Unaligned loads need special handling. Doublewords require 985 // word-alignment. 986 if (Alignment && Alignment < 4) 987 return false; 988 989 Opc = ARM::VLDRD; 990 RC = TLI.getRegClassFor(VT); 991 break; 992 } 993 // Simplify this down to something we can handle. 994 ARMSimplifyAddress(Addr, VT, useAM3); 995 996 // Create the base instruction, then add the operands. 997 if (allocReg) 998 ResultReg = createResultReg(RC); 999 assert(ResultReg > 255 && "Expected an allocated virtual register."); 1000 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1001 TII.get(Opc), ResultReg); 1002 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3); 1003 1004 // If we had an unaligned load of a float we've converted it to an regular 1005 // load. Now we must move from the GRP to the FP register. 1006 if (needVMOV) { 1007 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32)); 1008 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1009 TII.get(ARM::VMOVSR), MoveReg) 1010 .addReg(ResultReg)); 1011 ResultReg = MoveReg; 1012 } 1013 return true; 1014 } 1015 1016 bool ARMFastISel::SelectLoad(const Instruction *I) { 1017 // Atomic loads need special handling. 1018 if (cast<LoadInst>(I)->isAtomic()) 1019 return false; 1020 1021 const Value *SV = I->getOperand(0); 1022 if (TLI.supportSwiftError()) { 1023 // Swifterror values can come from either a function parameter with 1024 // swifterror attribute or an alloca with swifterror attribute. 1025 if (const Argument *Arg = dyn_cast<Argument>(SV)) { 1026 if (Arg->hasSwiftErrorAttr()) 1027 return false; 1028 } 1029 1030 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) { 1031 if (Alloca->isSwiftError()) 1032 return false; 1033 } 1034 } 1035 1036 // Verify we have a legal type before going any further. 1037 MVT VT; 1038 if (!isLoadTypeLegal(I->getType(), VT)) 1039 return false; 1040 1041 // See if we can handle this address. 1042 Address Addr; 1043 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false; 1044 1045 unsigned ResultReg; 1046 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment())) 1047 return false; 1048 updateValueMap(I, ResultReg); 1049 return true; 1050 } 1051 1052 bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr, 1053 unsigned Alignment) { 1054 unsigned StrOpc; 1055 bool useAM3 = false; 1056 switch (VT.SimpleTy) { 1057 // This is mostly going to be Neon/vector support. 1058 default: return false; 1059 case MVT::i1: { 1060 unsigned Res = createResultReg(isThumb2 ? &ARM::tGPRRegClass 1061 : &ARM::GPRRegClass); 1062 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri; 1063 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1); 1064 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1065 TII.get(Opc), Res) 1066 .addReg(SrcReg).addImm(1)); 1067 SrcReg = Res; 1068 LLVM_FALLTHROUGH; 1069 } 1070 case MVT::i8: 1071 if (isThumb2) { 1072 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 1073 StrOpc = ARM::t2STRBi8; 1074 else 1075 StrOpc = ARM::t2STRBi12; 1076 } else { 1077 StrOpc = ARM::STRBi12; 1078 } 1079 break; 1080 case MVT::i16: 1081 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem()) 1082 return false; 1083 1084 if (isThumb2) { 1085 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 1086 StrOpc = ARM::t2STRHi8; 1087 else 1088 StrOpc = ARM::t2STRHi12; 1089 } else { 1090 StrOpc = ARM::STRH; 1091 useAM3 = true; 1092 } 1093 break; 1094 case MVT::i32: 1095 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem()) 1096 return false; 1097 1098 if (isThumb2) { 1099 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 1100 StrOpc = ARM::t2STRi8; 1101 else 1102 StrOpc = ARM::t2STRi12; 1103 } else { 1104 StrOpc = ARM::STRi12; 1105 } 1106 break; 1107 case MVT::f32: 1108 if (!Subtarget->hasVFP2()) return false; 1109 // Unaligned stores need special handling. Floats require word-alignment. 1110 if (Alignment && Alignment < 4) { 1111 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32)); 1112 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1113 TII.get(ARM::VMOVRS), MoveReg) 1114 .addReg(SrcReg)); 1115 SrcReg = MoveReg; 1116 VT = MVT::i32; 1117 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12; 1118 } else { 1119 StrOpc = ARM::VSTRS; 1120 } 1121 break; 1122 case MVT::f64: 1123 if (!Subtarget->hasVFP2()) return false; 1124 // FIXME: Unaligned stores need special handling. Doublewords require 1125 // word-alignment. 1126 if (Alignment && Alignment < 4) 1127 return false; 1128 1129 StrOpc = ARM::VSTRD; 1130 break; 1131 } 1132 // Simplify this down to something we can handle. 1133 ARMSimplifyAddress(Addr, VT, useAM3); 1134 1135 // Create the base instruction, then add the operands. 1136 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0); 1137 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1138 TII.get(StrOpc)) 1139 .addReg(SrcReg); 1140 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3); 1141 return true; 1142 } 1143 1144 bool ARMFastISel::SelectStore(const Instruction *I) { 1145 Value *Op0 = I->getOperand(0); 1146 unsigned SrcReg = 0; 1147 1148 // Atomic stores need special handling. 1149 if (cast<StoreInst>(I)->isAtomic()) 1150 return false; 1151 1152 const Value *PtrV = I->getOperand(1); 1153 if (TLI.supportSwiftError()) { 1154 // Swifterror values can come from either a function parameter with 1155 // swifterror attribute or an alloca with swifterror attribute. 1156 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) { 1157 if (Arg->hasSwiftErrorAttr()) 1158 return false; 1159 } 1160 1161 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) { 1162 if (Alloca->isSwiftError()) 1163 return false; 1164 } 1165 } 1166 1167 // Verify we have a legal type before going any further. 1168 MVT VT; 1169 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT)) 1170 return false; 1171 1172 // Get the value to be stored into a register. 1173 SrcReg = getRegForValue(Op0); 1174 if (SrcReg == 0) return false; 1175 1176 // See if we can handle this address. 1177 Address Addr; 1178 if (!ARMComputeAddress(I->getOperand(1), Addr)) 1179 return false; 1180 1181 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment())) 1182 return false; 1183 return true; 1184 } 1185 1186 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) { 1187 switch (Pred) { 1188 // Needs two compares... 1189 case CmpInst::FCMP_ONE: 1190 case CmpInst::FCMP_UEQ: 1191 default: 1192 // AL is our "false" for now. The other two need more compares. 1193 return ARMCC::AL; 1194 case CmpInst::ICMP_EQ: 1195 case CmpInst::FCMP_OEQ: 1196 return ARMCC::EQ; 1197 case CmpInst::ICMP_SGT: 1198 case CmpInst::FCMP_OGT: 1199 return ARMCC::GT; 1200 case CmpInst::ICMP_SGE: 1201 case CmpInst::FCMP_OGE: 1202 return ARMCC::GE; 1203 case CmpInst::ICMP_UGT: 1204 case CmpInst::FCMP_UGT: 1205 return ARMCC::HI; 1206 case CmpInst::FCMP_OLT: 1207 return ARMCC::MI; 1208 case CmpInst::ICMP_ULE: 1209 case CmpInst::FCMP_OLE: 1210 return ARMCC::LS; 1211 case CmpInst::FCMP_ORD: 1212 return ARMCC::VC; 1213 case CmpInst::FCMP_UNO: 1214 return ARMCC::VS; 1215 case CmpInst::FCMP_UGE: 1216 return ARMCC::PL; 1217 case CmpInst::ICMP_SLT: 1218 case CmpInst::FCMP_ULT: 1219 return ARMCC::LT; 1220 case CmpInst::ICMP_SLE: 1221 case CmpInst::FCMP_ULE: 1222 return ARMCC::LE; 1223 case CmpInst::FCMP_UNE: 1224 case CmpInst::ICMP_NE: 1225 return ARMCC::NE; 1226 case CmpInst::ICMP_UGE: 1227 return ARMCC::HS; 1228 case CmpInst::ICMP_ULT: 1229 return ARMCC::LO; 1230 } 1231 } 1232 1233 bool ARMFastISel::SelectBranch(const Instruction *I) { 1234 const BranchInst *BI = cast<BranchInst>(I); 1235 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; 1236 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; 1237 1238 // Simple branch support. 1239 1240 // If we can, avoid recomputing the compare - redoing it could lead to wonky 1241 // behavior. 1242 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { 1243 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) { 1244 // Get the compare predicate. 1245 // Try to take advantage of fallthrough opportunities. 1246 CmpInst::Predicate Predicate = CI->getPredicate(); 1247 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { 1248 std::swap(TBB, FBB); 1249 Predicate = CmpInst::getInversePredicate(Predicate); 1250 } 1251 1252 ARMCC::CondCodes ARMPred = getComparePred(Predicate); 1253 1254 // We may not handle every CC for now. 1255 if (ARMPred == ARMCC::AL) return false; 1256 1257 // Emit the compare. 1258 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(), 1259 CI->isEquality())) 1260 return false; 1261 1262 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; 1263 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc)) 1264 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR); 1265 finishCondBranch(BI->getParent(), TBB, FBB); 1266 return true; 1267 } 1268 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) { 1269 MVT SourceVT; 1270 if (TI->hasOneUse() && TI->getParent() == I->getParent() && 1271 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) { 1272 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri; 1273 unsigned OpReg = getRegForValue(TI->getOperand(0)); 1274 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0); 1275 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1276 TII.get(TstOpc)) 1277 .addReg(OpReg).addImm(1)); 1278 1279 unsigned CCMode = ARMCC::NE; 1280 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { 1281 std::swap(TBB, FBB); 1282 CCMode = ARMCC::EQ; 1283 } 1284 1285 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; 1286 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc)) 1287 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR); 1288 1289 finishCondBranch(BI->getParent(), TBB, FBB); 1290 return true; 1291 } 1292 } else if (const ConstantInt *CI = 1293 dyn_cast<ConstantInt>(BI->getCondition())) { 1294 uint64_t Imm = CI->getZExtValue(); 1295 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB; 1296 fastEmitBranch(Target, DbgLoc); 1297 return true; 1298 } 1299 1300 unsigned CmpReg = getRegForValue(BI->getCondition()); 1301 if (CmpReg == 0) return false; 1302 1303 // We've been divorced from our compare! Our block was split, and 1304 // now our compare lives in a predecessor block. We musn't 1305 // re-compare here, as the children of the compare aren't guaranteed 1306 // live across the block boundary (we *could* check for this). 1307 // Regardless, the compare has been done in the predecessor block, 1308 // and it left a value for us in a virtual register. Ergo, we test 1309 // the one-bit value left in the virtual register. 1310 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri; 1311 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0); 1312 AddOptionalDefs( 1313 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc)) 1314 .addReg(CmpReg) 1315 .addImm(1)); 1316 1317 unsigned CCMode = ARMCC::NE; 1318 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { 1319 std::swap(TBB, FBB); 1320 CCMode = ARMCC::EQ; 1321 } 1322 1323 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; 1324 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc)) 1325 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR); 1326 finishCondBranch(BI->getParent(), TBB, FBB); 1327 return true; 1328 } 1329 1330 bool ARMFastISel::SelectIndirectBr(const Instruction *I) { 1331 unsigned AddrReg = getRegForValue(I->getOperand(0)); 1332 if (AddrReg == 0) return false; 1333 1334 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX; 1335 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1336 TII.get(Opc)).addReg(AddrReg)); 1337 1338 const IndirectBrInst *IB = cast<IndirectBrInst>(I); 1339 for (const BasicBlock *SuccBB : IB->successors()) 1340 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]); 1341 1342 return true; 1343 } 1344 1345 bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value, 1346 bool isZExt, bool isEquality) { 1347 Type *Ty = Src1Value->getType(); 1348 EVT SrcEVT = TLI.getValueType(DL, Ty, true); 1349 if (!SrcEVT.isSimple()) return false; 1350 MVT SrcVT = SrcEVT.getSimpleVT(); 1351 1352 if (Ty->isFloatTy() && !Subtarget->hasVFP2()) 1353 return false; 1354 1355 if (Ty->isDoubleTy() && (!Subtarget->hasVFP2() || Subtarget->isFPOnlySP())) 1356 return false; 1357 1358 // Check to see if the 2nd operand is a constant that we can encode directly 1359 // in the compare. 1360 int Imm = 0; 1361 bool UseImm = false; 1362 bool isNegativeImm = false; 1363 // FIXME: At -O0 we don't have anything that canonicalizes operand order. 1364 // Thus, Src1Value may be a ConstantInt, but we're missing it. 1365 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) { 1366 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 || 1367 SrcVT == MVT::i1) { 1368 const APInt &CIVal = ConstInt->getValue(); 1369 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue(); 1370 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather 1371 // then a cmn, because there is no way to represent 2147483648 as a 1372 // signed 32-bit int. 1373 if (Imm < 0 && Imm != (int)0x80000000) { 1374 isNegativeImm = true; 1375 Imm = -Imm; 1376 } 1377 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : 1378 (ARM_AM::getSOImmVal(Imm) != -1); 1379 } 1380 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) { 1381 if (SrcVT == MVT::f32 || SrcVT == MVT::f64) 1382 if (ConstFP->isZero() && !ConstFP->isNegative()) 1383 UseImm = true; 1384 } 1385 1386 unsigned CmpOpc; 1387 bool isICmp = true; 1388 bool needsExt = false; 1389 switch (SrcVT.SimpleTy) { 1390 default: return false; 1391 // TODO: Verify compares. 1392 case MVT::f32: 1393 isICmp = false; 1394 // Equality comparisons shouldn't raise Invalid on uordered inputs. 1395 if (isEquality) 1396 CmpOpc = UseImm ? ARM::VCMPZS : ARM::VCMPS; 1397 else 1398 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES; 1399 break; 1400 case MVT::f64: 1401 isICmp = false; 1402 // Equality comparisons shouldn't raise Invalid on uordered inputs. 1403 if (isEquality) 1404 CmpOpc = UseImm ? ARM::VCMPZD : ARM::VCMPD; 1405 else 1406 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED; 1407 break; 1408 case MVT::i1: 1409 case MVT::i8: 1410 case MVT::i16: 1411 needsExt = true; 1412 // Intentional fall-through. 1413 case MVT::i32: 1414 if (isThumb2) { 1415 if (!UseImm) 1416 CmpOpc = ARM::t2CMPrr; 1417 else 1418 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri; 1419 } else { 1420 if (!UseImm) 1421 CmpOpc = ARM::CMPrr; 1422 else 1423 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri; 1424 } 1425 break; 1426 } 1427 1428 unsigned SrcReg1 = getRegForValue(Src1Value); 1429 if (SrcReg1 == 0) return false; 1430 1431 unsigned SrcReg2 = 0; 1432 if (!UseImm) { 1433 SrcReg2 = getRegForValue(Src2Value); 1434 if (SrcReg2 == 0) return false; 1435 } 1436 1437 // We have i1, i8, or i16, we need to either zero extend or sign extend. 1438 if (needsExt) { 1439 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt); 1440 if (SrcReg1 == 0) return false; 1441 if (!UseImm) { 1442 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt); 1443 if (SrcReg2 == 0) return false; 1444 } 1445 } 1446 1447 const MCInstrDesc &II = TII.get(CmpOpc); 1448 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0); 1449 if (!UseImm) { 1450 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1); 1451 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1452 .addReg(SrcReg1).addReg(SrcReg2)); 1453 } else { 1454 MachineInstrBuilder MIB; 1455 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1456 .addReg(SrcReg1); 1457 1458 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0. 1459 if (isICmp) 1460 MIB.addImm(Imm); 1461 AddOptionalDefs(MIB); 1462 } 1463 1464 // For floating point we need to move the result to a comparison register 1465 // that we can then use for branches. 1466 if (Ty->isFloatTy() || Ty->isDoubleTy()) 1467 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1468 TII.get(ARM::FMSTAT))); 1469 return true; 1470 } 1471 1472 bool ARMFastISel::SelectCmp(const Instruction *I) { 1473 const CmpInst *CI = cast<CmpInst>(I); 1474 1475 // Get the compare predicate. 1476 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate()); 1477 1478 // We may not handle every CC for now. 1479 if (ARMPred == ARMCC::AL) return false; 1480 1481 // Emit the compare. 1482 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(), 1483 CI->isEquality())) 1484 return false; 1485 1486 // Now set a register based on the comparison. Explicitly set the predicates 1487 // here. 1488 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi; 1489 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass 1490 : &ARM::GPRRegClass; 1491 unsigned DestReg = createResultReg(RC); 1492 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0); 1493 unsigned ZeroReg = fastMaterializeConstant(Zero); 1494 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR. 1495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), DestReg) 1496 .addReg(ZeroReg).addImm(1) 1497 .addImm(ARMPred).addReg(ARM::CPSR); 1498 1499 updateValueMap(I, DestReg); 1500 return true; 1501 } 1502 1503 bool ARMFastISel::SelectFPExt(const Instruction *I) { 1504 // Make sure we have VFP and that we're extending float to double. 1505 if (!Subtarget->hasVFP2() || Subtarget->isFPOnlySP()) return false; 1506 1507 Value *V = I->getOperand(0); 1508 if (!I->getType()->isDoubleTy() || 1509 !V->getType()->isFloatTy()) return false; 1510 1511 unsigned Op = getRegForValue(V); 1512 if (Op == 0) return false; 1513 1514 unsigned Result = createResultReg(&ARM::DPRRegClass); 1515 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1516 TII.get(ARM::VCVTDS), Result) 1517 .addReg(Op)); 1518 updateValueMap(I, Result); 1519 return true; 1520 } 1521 1522 bool ARMFastISel::SelectFPTrunc(const Instruction *I) { 1523 // Make sure we have VFP and that we're truncating double to float. 1524 if (!Subtarget->hasVFP2() || Subtarget->isFPOnlySP()) return false; 1525 1526 Value *V = I->getOperand(0); 1527 if (!(I->getType()->isFloatTy() && 1528 V->getType()->isDoubleTy())) return false; 1529 1530 unsigned Op = getRegForValue(V); 1531 if (Op == 0) return false; 1532 1533 unsigned Result = createResultReg(&ARM::SPRRegClass); 1534 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1535 TII.get(ARM::VCVTSD), Result) 1536 .addReg(Op)); 1537 updateValueMap(I, Result); 1538 return true; 1539 } 1540 1541 bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) { 1542 // Make sure we have VFP. 1543 if (!Subtarget->hasVFP2()) return false; 1544 1545 MVT DstVT; 1546 Type *Ty = I->getType(); 1547 if (!isTypeLegal(Ty, DstVT)) 1548 return false; 1549 1550 Value *Src = I->getOperand(0); 1551 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true); 1552 if (!SrcEVT.isSimple()) 1553 return false; 1554 MVT SrcVT = SrcEVT.getSimpleVT(); 1555 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8) 1556 return false; 1557 1558 unsigned SrcReg = getRegForValue(Src); 1559 if (SrcReg == 0) return false; 1560 1561 // Handle sign-extension. 1562 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) { 1563 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32, 1564 /*isZExt*/!isSigned); 1565 if (SrcReg == 0) return false; 1566 } 1567 1568 // The conversion routine works on fp-reg to fp-reg and the operand above 1569 // was an integer, move it to the fp registers if possible. 1570 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg); 1571 if (FP == 0) return false; 1572 1573 unsigned Opc; 1574 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS; 1575 else if (Ty->isDoubleTy() && !Subtarget->isFPOnlySP()) 1576 Opc = isSigned ? ARM::VSITOD : ARM::VUITOD; 1577 else return false; 1578 1579 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT)); 1580 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1581 TII.get(Opc), ResultReg).addReg(FP)); 1582 updateValueMap(I, ResultReg); 1583 return true; 1584 } 1585 1586 bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) { 1587 // Make sure we have VFP. 1588 if (!Subtarget->hasVFP2()) return false; 1589 1590 MVT DstVT; 1591 Type *RetTy = I->getType(); 1592 if (!isTypeLegal(RetTy, DstVT)) 1593 return false; 1594 1595 unsigned Op = getRegForValue(I->getOperand(0)); 1596 if (Op == 0) return false; 1597 1598 unsigned Opc; 1599 Type *OpTy = I->getOperand(0)->getType(); 1600 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS; 1601 else if (OpTy->isDoubleTy() && !Subtarget->isFPOnlySP()) 1602 Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD; 1603 else return false; 1604 1605 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg. 1606 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32)); 1607 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1608 TII.get(Opc), ResultReg).addReg(Op)); 1609 1610 // This result needs to be in an integer register, but the conversion only 1611 // takes place in fp-regs. 1612 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg); 1613 if (IntReg == 0) return false; 1614 1615 updateValueMap(I, IntReg); 1616 return true; 1617 } 1618 1619 bool ARMFastISel::SelectSelect(const Instruction *I) { 1620 MVT VT; 1621 if (!isTypeLegal(I->getType(), VT)) 1622 return false; 1623 1624 // Things need to be register sized for register moves. 1625 if (VT != MVT::i32) return false; 1626 1627 unsigned CondReg = getRegForValue(I->getOperand(0)); 1628 if (CondReg == 0) return false; 1629 unsigned Op1Reg = getRegForValue(I->getOperand(1)); 1630 if (Op1Reg == 0) return false; 1631 1632 // Check to see if we can use an immediate in the conditional move. 1633 int Imm = 0; 1634 bool UseImm = false; 1635 bool isNegativeImm = false; 1636 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) { 1637 assert(VT == MVT::i32 && "Expecting an i32."); 1638 Imm = (int)ConstInt->getValue().getZExtValue(); 1639 if (Imm < 0) { 1640 isNegativeImm = true; 1641 Imm = ~Imm; 1642 } 1643 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : 1644 (ARM_AM::getSOImmVal(Imm) != -1); 1645 } 1646 1647 unsigned Op2Reg = 0; 1648 if (!UseImm) { 1649 Op2Reg = getRegForValue(I->getOperand(2)); 1650 if (Op2Reg == 0) return false; 1651 } 1652 1653 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri; 1654 CondReg = constrainOperandRegClass(TII.get(TstOpc), CondReg, 0); 1655 AddOptionalDefs( 1656 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc)) 1657 .addReg(CondReg) 1658 .addImm(1)); 1659 1660 unsigned MovCCOpc; 1661 const TargetRegisterClass *RC; 1662 if (!UseImm) { 1663 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 1664 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr; 1665 } else { 1666 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass; 1667 if (!isNegativeImm) 1668 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi; 1669 else 1670 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi; 1671 } 1672 unsigned ResultReg = createResultReg(RC); 1673 if (!UseImm) { 1674 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1); 1675 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2); 1676 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), 1677 ResultReg) 1678 .addReg(Op2Reg) 1679 .addReg(Op1Reg) 1680 .addImm(ARMCC::NE) 1681 .addReg(ARM::CPSR); 1682 } else { 1683 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1); 1684 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), 1685 ResultReg) 1686 .addReg(Op1Reg) 1687 .addImm(Imm) 1688 .addImm(ARMCC::EQ) 1689 .addReg(ARM::CPSR); 1690 } 1691 updateValueMap(I, ResultReg); 1692 return true; 1693 } 1694 1695 bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) { 1696 MVT VT; 1697 Type *Ty = I->getType(); 1698 if (!isTypeLegal(Ty, VT)) 1699 return false; 1700 1701 // If we have integer div support we should have selected this automagically. 1702 // In case we have a real miss go ahead and return false and we'll pick 1703 // it up later. 1704 if (Subtarget->hasDivideInThumbMode()) 1705 return false; 1706 1707 // Otherwise emit a libcall. 1708 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 1709 if (VT == MVT::i8) 1710 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8; 1711 else if (VT == MVT::i16) 1712 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16; 1713 else if (VT == MVT::i32) 1714 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32; 1715 else if (VT == MVT::i64) 1716 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64; 1717 else if (VT == MVT::i128) 1718 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128; 1719 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!"); 1720 1721 return ARMEmitLibcall(I, LC); 1722 } 1723 1724 bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) { 1725 MVT VT; 1726 Type *Ty = I->getType(); 1727 if (!isTypeLegal(Ty, VT)) 1728 return false; 1729 1730 // Many ABIs do not provide a libcall for standalone remainder, so we need to 1731 // use divrem (see the RTABI 4.3.1). Since FastISel can't handle non-double 1732 // multi-reg returns, we'll have to bail out. 1733 if (!TLI.hasStandaloneRem(VT)) { 1734 return false; 1735 } 1736 1737 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 1738 if (VT == MVT::i8) 1739 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8; 1740 else if (VT == MVT::i16) 1741 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16; 1742 else if (VT == MVT::i32) 1743 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32; 1744 else if (VT == MVT::i64) 1745 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64; 1746 else if (VT == MVT::i128) 1747 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128; 1748 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!"); 1749 1750 return ARMEmitLibcall(I, LC); 1751 } 1752 1753 bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) { 1754 EVT DestVT = TLI.getValueType(DL, I->getType(), true); 1755 1756 // We can get here in the case when we have a binary operation on a non-legal 1757 // type and the target independent selector doesn't know how to handle it. 1758 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1) 1759 return false; 1760 1761 unsigned Opc; 1762 switch (ISDOpcode) { 1763 default: return false; 1764 case ISD::ADD: 1765 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr; 1766 break; 1767 case ISD::OR: 1768 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr; 1769 break; 1770 case ISD::SUB: 1771 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr; 1772 break; 1773 } 1774 1775 unsigned SrcReg1 = getRegForValue(I->getOperand(0)); 1776 if (SrcReg1 == 0) return false; 1777 1778 // TODO: Often the 2nd operand is an immediate, which can be encoded directly 1779 // in the instruction, rather then materializing the value in a register. 1780 unsigned SrcReg2 = getRegForValue(I->getOperand(1)); 1781 if (SrcReg2 == 0) return false; 1782 1783 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass); 1784 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1); 1785 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2); 1786 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1787 TII.get(Opc), ResultReg) 1788 .addReg(SrcReg1).addReg(SrcReg2)); 1789 updateValueMap(I, ResultReg); 1790 return true; 1791 } 1792 1793 bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) { 1794 EVT FPVT = TLI.getValueType(DL, I->getType(), true); 1795 if (!FPVT.isSimple()) return false; 1796 MVT VT = FPVT.getSimpleVT(); 1797 1798 // FIXME: Support vector types where possible. 1799 if (VT.isVector()) 1800 return false; 1801 1802 // We can get here in the case when we want to use NEON for our fp 1803 // operations, but can't figure out how to. Just use the vfp instructions 1804 // if we have them. 1805 // FIXME: It'd be nice to use NEON instructions. 1806 Type *Ty = I->getType(); 1807 if (Ty->isFloatTy() && !Subtarget->hasVFP2()) 1808 return false; 1809 if (Ty->isDoubleTy() && (!Subtarget->hasVFP2() || Subtarget->isFPOnlySP())) 1810 return false; 1811 1812 unsigned Opc; 1813 bool is64bit = VT == MVT::f64 || VT == MVT::i64; 1814 switch (ISDOpcode) { 1815 default: return false; 1816 case ISD::FADD: 1817 Opc = is64bit ? ARM::VADDD : ARM::VADDS; 1818 break; 1819 case ISD::FSUB: 1820 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS; 1821 break; 1822 case ISD::FMUL: 1823 Opc = is64bit ? ARM::VMULD : ARM::VMULS; 1824 break; 1825 } 1826 unsigned Op1 = getRegForValue(I->getOperand(0)); 1827 if (Op1 == 0) return false; 1828 1829 unsigned Op2 = getRegForValue(I->getOperand(1)); 1830 if (Op2 == 0) return false; 1831 1832 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy)); 1833 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1834 TII.get(Opc), ResultReg) 1835 .addReg(Op1).addReg(Op2)); 1836 updateValueMap(I, ResultReg); 1837 return true; 1838 } 1839 1840 // Call Handling Code 1841 1842 // This is largely taken directly from CCAssignFnForNode 1843 // TODO: We may not support all of this. 1844 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, 1845 bool Return, 1846 bool isVarArg) { 1847 switch (CC) { 1848 default: 1849 llvm_unreachable("Unsupported calling convention"); 1850 case CallingConv::Fast: 1851 if (Subtarget->hasVFP2() && !isVarArg) { 1852 if (!Subtarget->isAAPCS_ABI()) 1853 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); 1854 // For AAPCS ABI targets, just use VFP variant of the calling convention. 1855 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1856 } 1857 LLVM_FALLTHROUGH; 1858 case CallingConv::C: 1859 case CallingConv::CXX_FAST_TLS: 1860 // Use target triple & subtarget features to do actual dispatch. 1861 if (Subtarget->isAAPCS_ABI()) { 1862 if (Subtarget->hasVFP2() && 1863 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg) 1864 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); 1865 else 1866 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); 1867 } else { 1868 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); 1869 } 1870 case CallingConv::ARM_AAPCS_VFP: 1871 case CallingConv::Swift: 1872 if (!isVarArg) 1873 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); 1874 // Fall through to soft float variant, variadic functions don't 1875 // use hard floating point ABI. 1876 LLVM_FALLTHROUGH; 1877 case CallingConv::ARM_AAPCS: 1878 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); 1879 case CallingConv::ARM_APCS: 1880 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); 1881 case CallingConv::GHC: 1882 if (Return) 1883 llvm_unreachable("Can't return in GHC call convention"); 1884 else 1885 return CC_ARM_APCS_GHC; 1886 } 1887 } 1888 1889 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args, 1890 SmallVectorImpl<unsigned> &ArgRegs, 1891 SmallVectorImpl<MVT> &ArgVTs, 1892 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, 1893 SmallVectorImpl<unsigned> &RegArgs, 1894 CallingConv::ID CC, 1895 unsigned &NumBytes, 1896 bool isVarArg) { 1897 SmallVector<CCValAssign, 16> ArgLocs; 1898 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, ArgLocs, *Context); 1899 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, 1900 CCAssignFnForCall(CC, false, isVarArg)); 1901 1902 // Check that we can handle all of the arguments. If we can't, then bail out 1903 // now before we add code to the MBB. 1904 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1905 CCValAssign &VA = ArgLocs[i]; 1906 MVT ArgVT = ArgVTs[VA.getValNo()]; 1907 1908 // We don't handle NEON/vector parameters yet. 1909 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64) 1910 return false; 1911 1912 // Now copy/store arg to correct locations. 1913 if (VA.isRegLoc() && !VA.needsCustom()) { 1914 continue; 1915 } else if (VA.needsCustom()) { 1916 // TODO: We need custom lowering for vector (v2f64) args. 1917 if (VA.getLocVT() != MVT::f64 || 1918 // TODO: Only handle register args for now. 1919 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc()) 1920 return false; 1921 } else { 1922 switch (ArgVT.SimpleTy) { 1923 default: 1924 return false; 1925 case MVT::i1: 1926 case MVT::i8: 1927 case MVT::i16: 1928 case MVT::i32: 1929 break; 1930 case MVT::f32: 1931 if (!Subtarget->hasVFP2()) 1932 return false; 1933 break; 1934 case MVT::f64: 1935 if (!Subtarget->hasVFP2()) 1936 return false; 1937 break; 1938 } 1939 } 1940 } 1941 1942 // At the point, we are able to handle the call's arguments in fast isel. 1943 1944 // Get a count of how many bytes are to be pushed on the stack. 1945 NumBytes = CCInfo.getNextStackOffset(); 1946 1947 // Issue CALLSEQ_START 1948 unsigned AdjStackDown = TII.getCallFrameSetupOpcode(); 1949 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1950 TII.get(AdjStackDown)) 1951 .addImm(NumBytes).addImm(0)); 1952 1953 // Process the args. 1954 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1955 CCValAssign &VA = ArgLocs[i]; 1956 const Value *ArgVal = Args[VA.getValNo()]; 1957 unsigned Arg = ArgRegs[VA.getValNo()]; 1958 MVT ArgVT = ArgVTs[VA.getValNo()]; 1959 1960 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) && 1961 "We don't handle NEON/vector parameters yet."); 1962 1963 // Handle arg promotion, etc. 1964 switch (VA.getLocInfo()) { 1965 case CCValAssign::Full: break; 1966 case CCValAssign::SExt: { 1967 MVT DestVT = VA.getLocVT(); 1968 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false); 1969 assert(Arg != 0 && "Failed to emit a sext"); 1970 ArgVT = DestVT; 1971 break; 1972 } 1973 case CCValAssign::AExt: 1974 // Intentional fall-through. Handle AExt and ZExt. 1975 case CCValAssign::ZExt: { 1976 MVT DestVT = VA.getLocVT(); 1977 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true); 1978 assert(Arg != 0 && "Failed to emit a zext"); 1979 ArgVT = DestVT; 1980 break; 1981 } 1982 case CCValAssign::BCvt: { 1983 unsigned BC = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg, 1984 /*TODO: Kill=*/false); 1985 assert(BC != 0 && "Failed to emit a bitcast!"); 1986 Arg = BC; 1987 ArgVT = VA.getLocVT(); 1988 break; 1989 } 1990 default: llvm_unreachable("Unknown arg promotion!"); 1991 } 1992 1993 // Now copy/store arg to correct locations. 1994 if (VA.isRegLoc() && !VA.needsCustom()) { 1995 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1996 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg); 1997 RegArgs.push_back(VA.getLocReg()); 1998 } else if (VA.needsCustom()) { 1999 // TODO: We need custom lowering for vector (v2f64) args. 2000 assert(VA.getLocVT() == MVT::f64 && 2001 "Custom lowering for v2f64 args not available"); 2002 2003 // FIXME: ArgLocs[++i] may extend beyond ArgLocs.size() 2004 CCValAssign &NextVA = ArgLocs[++i]; 2005 2006 assert(VA.isRegLoc() && NextVA.isRegLoc() && 2007 "We only handle register args!"); 2008 2009 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2010 TII.get(ARM::VMOVRRD), VA.getLocReg()) 2011 .addReg(NextVA.getLocReg(), RegState::Define) 2012 .addReg(Arg)); 2013 RegArgs.push_back(VA.getLocReg()); 2014 RegArgs.push_back(NextVA.getLocReg()); 2015 } else { 2016 assert(VA.isMemLoc()); 2017 // Need to store on the stack. 2018 2019 // Don't emit stores for undef values. 2020 if (isa<UndefValue>(ArgVal)) 2021 continue; 2022 2023 Address Addr; 2024 Addr.BaseType = Address::RegBase; 2025 Addr.Base.Reg = ARM::SP; 2026 Addr.Offset = VA.getLocMemOffset(); 2027 2028 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet; 2029 assert(EmitRet && "Could not emit a store for argument!"); 2030 } 2031 } 2032 2033 return true; 2034 } 2035 2036 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, 2037 const Instruction *I, CallingConv::ID CC, 2038 unsigned &NumBytes, bool isVarArg) { 2039 // Issue CALLSEQ_END 2040 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode(); 2041 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2042 TII.get(AdjStackUp)) 2043 .addImm(NumBytes).addImm(0)); 2044 2045 // Now the return value. 2046 if (RetVT != MVT::isVoid) { 2047 SmallVector<CCValAssign, 16> RVLocs; 2048 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context); 2049 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg)); 2050 2051 // Copy all of the result registers out of their specified physreg. 2052 if (RVLocs.size() == 2 && RetVT == MVT::f64) { 2053 // For this move we copy into two registers and then move into the 2054 // double fp reg we want. 2055 MVT DestVT = RVLocs[0].getValVT(); 2056 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT); 2057 unsigned ResultReg = createResultReg(DstRC); 2058 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2059 TII.get(ARM::VMOVDRR), ResultReg) 2060 .addReg(RVLocs[0].getLocReg()) 2061 .addReg(RVLocs[1].getLocReg())); 2062 2063 UsedRegs.push_back(RVLocs[0].getLocReg()); 2064 UsedRegs.push_back(RVLocs[1].getLocReg()); 2065 2066 // Finally update the result. 2067 updateValueMap(I, ResultReg); 2068 } else { 2069 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!"); 2070 MVT CopyVT = RVLocs[0].getValVT(); 2071 2072 // Special handling for extended integers. 2073 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16) 2074 CopyVT = MVT::i32; 2075 2076 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT); 2077 2078 unsigned ResultReg = createResultReg(DstRC); 2079 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2080 TII.get(TargetOpcode::COPY), 2081 ResultReg).addReg(RVLocs[0].getLocReg()); 2082 UsedRegs.push_back(RVLocs[0].getLocReg()); 2083 2084 // Finally update the result. 2085 updateValueMap(I, ResultReg); 2086 } 2087 } 2088 2089 return true; 2090 } 2091 2092 bool ARMFastISel::SelectRet(const Instruction *I) { 2093 const ReturnInst *Ret = cast<ReturnInst>(I); 2094 const Function &F = *I->getParent()->getParent(); 2095 2096 if (!FuncInfo.CanLowerReturn) 2097 return false; 2098 2099 if (TLI.supportSwiftError() && 2100 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError)) 2101 return false; 2102 2103 if (TLI.supportSplitCSR(FuncInfo.MF)) 2104 return false; 2105 2106 // Build a list of return value registers. 2107 SmallVector<unsigned, 4> RetRegs; 2108 2109 CallingConv::ID CC = F.getCallingConv(); 2110 if (Ret->getNumOperands() > 0) { 2111 SmallVector<ISD::OutputArg, 4> Outs; 2112 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL); 2113 2114 // Analyze operands of the call, assigning locations to each operand. 2115 SmallVector<CCValAssign, 16> ValLocs; 2116 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext()); 2117 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */, 2118 F.isVarArg())); 2119 2120 const Value *RV = Ret->getOperand(0); 2121 unsigned Reg = getRegForValue(RV); 2122 if (Reg == 0) 2123 return false; 2124 2125 // Only handle a single return value for now. 2126 if (ValLocs.size() != 1) 2127 return false; 2128 2129 CCValAssign &VA = ValLocs[0]; 2130 2131 // Don't bother handling odd stuff for now. 2132 if (VA.getLocInfo() != CCValAssign::Full) 2133 return false; 2134 // Only handle register returns for now. 2135 if (!VA.isRegLoc()) 2136 return false; 2137 2138 unsigned SrcReg = Reg + VA.getValNo(); 2139 EVT RVEVT = TLI.getValueType(DL, RV->getType()); 2140 if (!RVEVT.isSimple()) return false; 2141 MVT RVVT = RVEVT.getSimpleVT(); 2142 MVT DestVT = VA.getValVT(); 2143 // Special handling for extended integers. 2144 if (RVVT != DestVT) { 2145 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16) 2146 return false; 2147 2148 assert(DestVT == MVT::i32 && "ARM should always ext to i32"); 2149 2150 // Perform extension if flagged as either zext or sext. Otherwise, do 2151 // nothing. 2152 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) { 2153 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt()); 2154 if (SrcReg == 0) return false; 2155 } 2156 } 2157 2158 // Make the copy. 2159 unsigned DstReg = VA.getLocReg(); 2160 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg); 2161 // Avoid a cross-class copy. This is very unlikely. 2162 if (!SrcRC->contains(DstReg)) 2163 return false; 2164 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2165 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg); 2166 2167 // Add register to return instruction. 2168 RetRegs.push_back(VA.getLocReg()); 2169 } 2170 2171 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET; 2172 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2173 TII.get(RetOpc)); 2174 AddOptionalDefs(MIB); 2175 for (unsigned R : RetRegs) 2176 MIB.addReg(R, RegState::Implicit); 2177 return true; 2178 } 2179 2180 unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) { 2181 if (UseReg) 2182 return isThumb2 ? ARM::tBLXr : ARM::BLX; 2183 else 2184 return isThumb2 ? ARM::tBL : ARM::BL; 2185 } 2186 2187 unsigned ARMFastISel::getLibcallReg(const Twine &Name) { 2188 // Manually compute the global's type to avoid building it when unnecessary. 2189 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0); 2190 EVT LCREVT = TLI.getValueType(DL, GVTy); 2191 if (!LCREVT.isSimple()) return 0; 2192 2193 GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false, 2194 GlobalValue::ExternalLinkage, nullptr, 2195 Name); 2196 assert(GV->getType() == GVTy && "We miscomputed the type for the global!"); 2197 return ARMMaterializeGV(GV, LCREVT.getSimpleVT()); 2198 } 2199 2200 // A quick function that will emit a call for a named libcall in F with the 2201 // vector of passed arguments for the Instruction in I. We can assume that we 2202 // can emit a call for any libcall we can produce. This is an abridged version 2203 // of the full call infrastructure since we won't need to worry about things 2204 // like computed function pointers or strange arguments at call sites. 2205 // TODO: Try to unify this and the normal call bits for ARM, then try to unify 2206 // with X86. 2207 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) { 2208 CallingConv::ID CC = TLI.getLibcallCallingConv(Call); 2209 2210 // Handle *simple* calls for now. 2211 Type *RetTy = I->getType(); 2212 MVT RetVT; 2213 if (RetTy->isVoidTy()) 2214 RetVT = MVT::isVoid; 2215 else if (!isTypeLegal(RetTy, RetVT)) 2216 return false; 2217 2218 // Can't handle non-double multi-reg retvals. 2219 if (RetVT != MVT::isVoid && RetVT != MVT::i32) { 2220 SmallVector<CCValAssign, 16> RVLocs; 2221 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context); 2222 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false)); 2223 if (RVLocs.size() >= 2 && RetVT != MVT::f64) 2224 return false; 2225 } 2226 2227 // Set up the argument vectors. 2228 SmallVector<Value*, 8> Args; 2229 SmallVector<unsigned, 8> ArgRegs; 2230 SmallVector<MVT, 8> ArgVTs; 2231 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; 2232 Args.reserve(I->getNumOperands()); 2233 ArgRegs.reserve(I->getNumOperands()); 2234 ArgVTs.reserve(I->getNumOperands()); 2235 ArgFlags.reserve(I->getNumOperands()); 2236 for (Value *Op : I->operands()) { 2237 unsigned Arg = getRegForValue(Op); 2238 if (Arg == 0) return false; 2239 2240 Type *ArgTy = Op->getType(); 2241 MVT ArgVT; 2242 if (!isTypeLegal(ArgTy, ArgVT)) return false; 2243 2244 ISD::ArgFlagsTy Flags; 2245 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy); 2246 Flags.setOrigAlign(OriginalAlignment); 2247 2248 Args.push_back(Op); 2249 ArgRegs.push_back(Arg); 2250 ArgVTs.push_back(ArgVT); 2251 ArgFlags.push_back(Flags); 2252 } 2253 2254 // Handle the arguments now that we've gotten them. 2255 SmallVector<unsigned, 4> RegArgs; 2256 unsigned NumBytes; 2257 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, 2258 RegArgs, CC, NumBytes, false)) 2259 return false; 2260 2261 unsigned CalleeReg = 0; 2262 if (Subtarget->genLongCalls()) { 2263 CalleeReg = getLibcallReg(TLI.getLibcallName(Call)); 2264 if (CalleeReg == 0) return false; 2265 } 2266 2267 // Issue the call. 2268 unsigned CallOpc = ARMSelectCallOp(Subtarget->genLongCalls()); 2269 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, 2270 DbgLoc, TII.get(CallOpc)); 2271 // BL / BLX don't take a predicate, but tBL / tBLX do. 2272 if (isThumb2) 2273 MIB.add(predOps(ARMCC::AL)); 2274 if (Subtarget->genLongCalls()) 2275 MIB.addReg(CalleeReg); 2276 else 2277 MIB.addExternalSymbol(TLI.getLibcallName(Call)); 2278 2279 // Add implicit physical register uses to the call. 2280 for (unsigned R : RegArgs) 2281 MIB.addReg(R, RegState::Implicit); 2282 2283 // Add a register mask with the call-preserved registers. 2284 // Proper defs for return values will be added by setPhysRegsDeadExcept(). 2285 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC)); 2286 2287 // Finish off the call including any return values. 2288 SmallVector<unsigned, 4> UsedRegs; 2289 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false; 2290 2291 // Set all unused physreg defs as dead. 2292 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); 2293 2294 return true; 2295 } 2296 2297 bool ARMFastISel::SelectCall(const Instruction *I, 2298 const char *IntrMemName = nullptr) { 2299 const CallInst *CI = cast<CallInst>(I); 2300 const Value *Callee = CI->getCalledValue(); 2301 2302 // Can't handle inline asm. 2303 if (isa<InlineAsm>(Callee)) return false; 2304 2305 // Allow SelectionDAG isel to handle tail calls. 2306 if (CI->isTailCall()) return false; 2307 2308 // Check the calling convention. 2309 ImmutableCallSite CS(CI); 2310 CallingConv::ID CC = CS.getCallingConv(); 2311 2312 // TODO: Avoid some calling conventions? 2313 2314 FunctionType *FTy = CS.getFunctionType(); 2315 bool isVarArg = FTy->isVarArg(); 2316 2317 // Handle *simple* calls for now. 2318 Type *RetTy = I->getType(); 2319 MVT RetVT; 2320 if (RetTy->isVoidTy()) 2321 RetVT = MVT::isVoid; 2322 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 && 2323 RetVT != MVT::i8 && RetVT != MVT::i1) 2324 return false; 2325 2326 // Can't handle non-double multi-reg retvals. 2327 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 && 2328 RetVT != MVT::i16 && RetVT != MVT::i32) { 2329 SmallVector<CCValAssign, 16> RVLocs; 2330 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context); 2331 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg)); 2332 if (RVLocs.size() >= 2 && RetVT != MVT::f64) 2333 return false; 2334 } 2335 2336 // Set up the argument vectors. 2337 SmallVector<Value*, 8> Args; 2338 SmallVector<unsigned, 8> ArgRegs; 2339 SmallVector<MVT, 8> ArgVTs; 2340 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; 2341 unsigned arg_size = CS.arg_size(); 2342 Args.reserve(arg_size); 2343 ArgRegs.reserve(arg_size); 2344 ArgVTs.reserve(arg_size); 2345 ArgFlags.reserve(arg_size); 2346 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); 2347 i != e; ++i) { 2348 // If we're lowering a memory intrinsic instead of a regular call, skip the 2349 // last two arguments, which shouldn't be passed to the underlying function. 2350 if (IntrMemName && e-i <= 2) 2351 break; 2352 2353 ISD::ArgFlagsTy Flags; 2354 unsigned ArgIdx = i - CS.arg_begin(); 2355 if (CS.paramHasAttr(ArgIdx, Attribute::SExt)) 2356 Flags.setSExt(); 2357 if (CS.paramHasAttr(ArgIdx, Attribute::ZExt)) 2358 Flags.setZExt(); 2359 2360 // FIXME: Only handle *easy* calls for now. 2361 if (CS.paramHasAttr(ArgIdx, Attribute::InReg) || 2362 CS.paramHasAttr(ArgIdx, Attribute::StructRet) || 2363 CS.paramHasAttr(ArgIdx, Attribute::SwiftSelf) || 2364 CS.paramHasAttr(ArgIdx, Attribute::SwiftError) || 2365 CS.paramHasAttr(ArgIdx, Attribute::Nest) || 2366 CS.paramHasAttr(ArgIdx, Attribute::ByVal)) 2367 return false; 2368 2369 Type *ArgTy = (*i)->getType(); 2370 MVT ArgVT; 2371 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 && 2372 ArgVT != MVT::i1) 2373 return false; 2374 2375 unsigned Arg = getRegForValue(*i); 2376 if (Arg == 0) 2377 return false; 2378 2379 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy); 2380 Flags.setOrigAlign(OriginalAlignment); 2381 2382 Args.push_back(*i); 2383 ArgRegs.push_back(Arg); 2384 ArgVTs.push_back(ArgVT); 2385 ArgFlags.push_back(Flags); 2386 } 2387 2388 // Handle the arguments now that we've gotten them. 2389 SmallVector<unsigned, 4> RegArgs; 2390 unsigned NumBytes; 2391 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, 2392 RegArgs, CC, NumBytes, isVarArg)) 2393 return false; 2394 2395 bool UseReg = false; 2396 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee); 2397 if (!GV || Subtarget->genLongCalls()) UseReg = true; 2398 2399 unsigned CalleeReg = 0; 2400 if (UseReg) { 2401 if (IntrMemName) 2402 CalleeReg = getLibcallReg(IntrMemName); 2403 else 2404 CalleeReg = getRegForValue(Callee); 2405 2406 if (CalleeReg == 0) return false; 2407 } 2408 2409 // Issue the call. 2410 unsigned CallOpc = ARMSelectCallOp(UseReg); 2411 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, 2412 DbgLoc, TII.get(CallOpc)); 2413 2414 // ARM calls don't take a predicate, but tBL / tBLX do. 2415 if(isThumb2) 2416 MIB.add(predOps(ARMCC::AL)); 2417 if (UseReg) 2418 MIB.addReg(CalleeReg); 2419 else if (!IntrMemName) 2420 MIB.addGlobalAddress(GV, 0, 0); 2421 else 2422 MIB.addExternalSymbol(IntrMemName, 0); 2423 2424 // Add implicit physical register uses to the call. 2425 for (unsigned R : RegArgs) 2426 MIB.addReg(R, RegState::Implicit); 2427 2428 // Add a register mask with the call-preserved registers. 2429 // Proper defs for return values will be added by setPhysRegsDeadExcept(). 2430 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC)); 2431 2432 // Finish off the call including any return values. 2433 SmallVector<unsigned, 4> UsedRegs; 2434 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg)) 2435 return false; 2436 2437 // Set all unused physreg defs as dead. 2438 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); 2439 2440 return true; 2441 } 2442 2443 bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) { 2444 return Len <= 16; 2445 } 2446 2447 bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, 2448 uint64_t Len, unsigned Alignment) { 2449 // Make sure we don't bloat code by inlining very large memcpy's. 2450 if (!ARMIsMemCpySmall(Len)) 2451 return false; 2452 2453 while (Len) { 2454 MVT VT; 2455 if (!Alignment || Alignment >= 4) { 2456 if (Len >= 4) 2457 VT = MVT::i32; 2458 else if (Len >= 2) 2459 VT = MVT::i16; 2460 else { 2461 assert(Len == 1 && "Expected a length of 1!"); 2462 VT = MVT::i8; 2463 } 2464 } else { 2465 // Bound based on alignment. 2466 if (Len >= 2 && Alignment == 2) 2467 VT = MVT::i16; 2468 else { 2469 VT = MVT::i8; 2470 } 2471 } 2472 2473 bool RV; 2474 unsigned ResultReg; 2475 RV = ARMEmitLoad(VT, ResultReg, Src); 2476 assert(RV && "Should be able to handle this load."); 2477 RV = ARMEmitStore(VT, ResultReg, Dest); 2478 assert(RV && "Should be able to handle this store."); 2479 (void)RV; 2480 2481 unsigned Size = VT.getSizeInBits()/8; 2482 Len -= Size; 2483 Dest.Offset += Size; 2484 Src.Offset += Size; 2485 } 2486 2487 return true; 2488 } 2489 2490 bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) { 2491 // FIXME: Handle more intrinsics. 2492 switch (I.getIntrinsicID()) { 2493 default: return false; 2494 case Intrinsic::frameaddress: { 2495 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo(); 2496 MFI.setFrameAddressIsTaken(true); 2497 2498 unsigned LdrOpc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12; 2499 const TargetRegisterClass *RC = isThumb2 ? &ARM::tGPRRegClass 2500 : &ARM::GPRRegClass; 2501 2502 const ARMBaseRegisterInfo *RegInfo = 2503 static_cast<const ARMBaseRegisterInfo *>(Subtarget->getRegisterInfo()); 2504 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF)); 2505 unsigned SrcReg = FramePtr; 2506 2507 // Recursively load frame address 2508 // ldr r0 [fp] 2509 // ldr r0 [r0] 2510 // ldr r0 [r0] 2511 // ... 2512 unsigned DestReg; 2513 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue(); 2514 while (Depth--) { 2515 DestReg = createResultReg(RC); 2516 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2517 TII.get(LdrOpc), DestReg) 2518 .addReg(SrcReg).addImm(0)); 2519 SrcReg = DestReg; 2520 } 2521 updateValueMap(&I, SrcReg); 2522 return true; 2523 } 2524 case Intrinsic::memcpy: 2525 case Intrinsic::memmove: { 2526 const MemTransferInst &MTI = cast<MemTransferInst>(I); 2527 // Don't handle volatile. 2528 if (MTI.isVolatile()) 2529 return false; 2530 2531 // Disable inlining for memmove before calls to ComputeAddress. Otherwise, 2532 // we would emit dead code because we don't currently handle memmoves. 2533 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy); 2534 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) { 2535 // Small memcpy's are common enough that we want to do them without a call 2536 // if possible. 2537 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue(); 2538 if (ARMIsMemCpySmall(Len)) { 2539 Address Dest, Src; 2540 if (!ARMComputeAddress(MTI.getRawDest(), Dest) || 2541 !ARMComputeAddress(MTI.getRawSource(), Src)) 2542 return false; 2543 unsigned Alignment = MTI.getAlignment(); 2544 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment)) 2545 return true; 2546 } 2547 } 2548 2549 if (!MTI.getLength()->getType()->isIntegerTy(32)) 2550 return false; 2551 2552 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255) 2553 return false; 2554 2555 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove"; 2556 return SelectCall(&I, IntrMemName); 2557 } 2558 case Intrinsic::memset: { 2559 const MemSetInst &MSI = cast<MemSetInst>(I); 2560 // Don't handle volatile. 2561 if (MSI.isVolatile()) 2562 return false; 2563 2564 if (!MSI.getLength()->getType()->isIntegerTy(32)) 2565 return false; 2566 2567 if (MSI.getDestAddressSpace() > 255) 2568 return false; 2569 2570 return SelectCall(&I, "memset"); 2571 } 2572 case Intrinsic::trap: { 2573 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get( 2574 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP)); 2575 return true; 2576 } 2577 } 2578 } 2579 2580 bool ARMFastISel::SelectTrunc(const Instruction *I) { 2581 // The high bits for a type smaller than the register size are assumed to be 2582 // undefined. 2583 Value *Op = I->getOperand(0); 2584 2585 EVT SrcVT, DestVT; 2586 SrcVT = TLI.getValueType(DL, Op->getType(), true); 2587 DestVT = TLI.getValueType(DL, I->getType(), true); 2588 2589 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8) 2590 return false; 2591 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1) 2592 return false; 2593 2594 unsigned SrcReg = getRegForValue(Op); 2595 if (!SrcReg) return false; 2596 2597 // Because the high bits are undefined, a truncate doesn't generate 2598 // any code. 2599 updateValueMap(I, SrcReg); 2600 return true; 2601 } 2602 2603 unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 2604 bool isZExt) { 2605 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8) 2606 return 0; 2607 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1) 2608 return 0; 2609 2610 // Table of which combinations can be emitted as a single instruction, 2611 // and which will require two. 2612 static const uint8_t isSingleInstrTbl[3][2][2][2] = { 2613 // ARM Thumb 2614 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops 2615 // ext: s z s z s z s z 2616 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } }, 2617 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }, 2618 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } } 2619 }; 2620 2621 // Target registers for: 2622 // - For ARM can never be PC. 2623 // - For 16-bit Thumb are restricted to lower 8 registers. 2624 // - For 32-bit Thumb are restricted to non-SP and non-PC. 2625 static const TargetRegisterClass *RCTbl[2][2] = { 2626 // Instructions: Two Single 2627 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass }, 2628 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass } 2629 }; 2630 2631 // Table governing the instruction(s) to be emitted. 2632 static const struct InstructionTable { 2633 uint32_t Opc : 16; 2634 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0. 2635 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi. 2636 uint32_t Imm : 8; // All instructions have either a shift or a mask. 2637 } IT[2][2][3][2] = { 2638 { // Two instructions (first is left shift, second is in this table). 2639 { // ARM Opc S Shift Imm 2640 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 }, 2641 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } }, 2642 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 }, 2643 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } }, 2644 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 }, 2645 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } } 2646 }, 2647 { // Thumb Opc S Shift Imm 2648 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 }, 2649 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } }, 2650 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 }, 2651 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } }, 2652 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 }, 2653 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } } 2654 } 2655 }, 2656 { // Single instruction. 2657 { // ARM Opc S Shift Imm 2658 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 }, 2659 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } }, 2660 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 }, 2661 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } }, 2662 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 }, 2663 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } } 2664 }, 2665 { // Thumb Opc S Shift Imm 2666 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 }, 2667 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } }, 2668 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 }, 2669 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } }, 2670 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 }, 2671 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } } 2672 } 2673 } 2674 }; 2675 2676 unsigned SrcBits = SrcVT.getSizeInBits(); 2677 unsigned DestBits = DestVT.getSizeInBits(); 2678 (void) DestBits; 2679 assert((SrcBits < DestBits) && "can only extend to larger types"); 2680 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) && 2681 "other sizes unimplemented"); 2682 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) && 2683 "other sizes unimplemented"); 2684 2685 bool hasV6Ops = Subtarget->hasV6Ops(); 2686 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2} 2687 assert((Bitness < 3) && "sanity-check table bounds"); 2688 2689 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt]; 2690 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr]; 2691 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt]; 2692 unsigned Opc = ITP->Opc; 2693 assert(ARM::KILL != Opc && "Invalid table entry"); 2694 unsigned hasS = ITP->hasS; 2695 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift; 2696 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) && 2697 "only MOVsi has shift operand addressing mode"); 2698 unsigned Imm = ITP->Imm; 2699 2700 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block). 2701 bool setsCPSR = &ARM::tGPRRegClass == RC; 2702 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi; 2703 unsigned ResultReg; 2704 // MOVsi encodes shift and immediate in shift operand addressing mode. 2705 // The following condition has the same value when emitting two 2706 // instruction sequences: both are shifts. 2707 bool ImmIsSO = (Shift != ARM_AM::no_shift); 2708 2709 // Either one or two instructions are emitted. 2710 // They're always of the form: 2711 // dst = in OP imm 2712 // CPSR is set only by 16-bit Thumb instructions. 2713 // Predicate, if any, is AL. 2714 // S bit, if available, is always 0. 2715 // When two are emitted the first's result will feed as the second's input, 2716 // that value is then dead. 2717 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2; 2718 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) { 2719 ResultReg = createResultReg(RC); 2720 bool isLsl = (0 == Instr) && !isSingleInstr; 2721 unsigned Opcode = isLsl ? LSLOpc : Opc; 2722 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift; 2723 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm; 2724 bool isKill = 1 == Instr; 2725 MachineInstrBuilder MIB = BuildMI( 2726 *FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opcode), ResultReg); 2727 if (setsCPSR) 2728 MIB.addReg(ARM::CPSR, RegState::Define); 2729 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR); 2730 MIB.addReg(SrcReg, isKill * RegState::Kill) 2731 .addImm(ImmEnc) 2732 .add(predOps(ARMCC::AL)); 2733 if (hasS) 2734 MIB.add(condCodeOp()); 2735 // Second instruction consumes the first's result. 2736 SrcReg = ResultReg; 2737 } 2738 2739 return ResultReg; 2740 } 2741 2742 bool ARMFastISel::SelectIntExt(const Instruction *I) { 2743 // On ARM, in general, integer casts don't involve legal types; this code 2744 // handles promotable integers. 2745 Type *DestTy = I->getType(); 2746 Value *Src = I->getOperand(0); 2747 Type *SrcTy = Src->getType(); 2748 2749 bool isZExt = isa<ZExtInst>(I); 2750 unsigned SrcReg = getRegForValue(Src); 2751 if (!SrcReg) return false; 2752 2753 EVT SrcEVT, DestEVT; 2754 SrcEVT = TLI.getValueType(DL, SrcTy, true); 2755 DestEVT = TLI.getValueType(DL, DestTy, true); 2756 if (!SrcEVT.isSimple()) return false; 2757 if (!DestEVT.isSimple()) return false; 2758 2759 MVT SrcVT = SrcEVT.getSimpleVT(); 2760 MVT DestVT = DestEVT.getSimpleVT(); 2761 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt); 2762 if (ResultReg == 0) return false; 2763 updateValueMap(I, ResultReg); 2764 return true; 2765 } 2766 2767 bool ARMFastISel::SelectShift(const Instruction *I, 2768 ARM_AM::ShiftOpc ShiftTy) { 2769 // We handle thumb2 mode by target independent selector 2770 // or SelectionDAG ISel. 2771 if (isThumb2) 2772 return false; 2773 2774 // Only handle i32 now. 2775 EVT DestVT = TLI.getValueType(DL, I->getType(), true); 2776 if (DestVT != MVT::i32) 2777 return false; 2778 2779 unsigned Opc = ARM::MOVsr; 2780 unsigned ShiftImm; 2781 Value *Src2Value = I->getOperand(1); 2782 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) { 2783 ShiftImm = CI->getZExtValue(); 2784 2785 // Fall back to selection DAG isel if the shift amount 2786 // is zero or greater than the width of the value type. 2787 if (ShiftImm == 0 || ShiftImm >=32) 2788 return false; 2789 2790 Opc = ARM::MOVsi; 2791 } 2792 2793 Value *Src1Value = I->getOperand(0); 2794 unsigned Reg1 = getRegForValue(Src1Value); 2795 if (Reg1 == 0) return false; 2796 2797 unsigned Reg2 = 0; 2798 if (Opc == ARM::MOVsr) { 2799 Reg2 = getRegForValue(Src2Value); 2800 if (Reg2 == 0) return false; 2801 } 2802 2803 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass); 2804 if(ResultReg == 0) return false; 2805 2806 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2807 TII.get(Opc), ResultReg) 2808 .addReg(Reg1); 2809 2810 if (Opc == ARM::MOVsi) 2811 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm)); 2812 else if (Opc == ARM::MOVsr) { 2813 MIB.addReg(Reg2); 2814 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0)); 2815 } 2816 2817 AddOptionalDefs(MIB); 2818 updateValueMap(I, ResultReg); 2819 return true; 2820 } 2821 2822 // TODO: SoftFP support. 2823 bool ARMFastISel::fastSelectInstruction(const Instruction *I) { 2824 switch (I->getOpcode()) { 2825 case Instruction::Load: 2826 return SelectLoad(I); 2827 case Instruction::Store: 2828 return SelectStore(I); 2829 case Instruction::Br: 2830 return SelectBranch(I); 2831 case Instruction::IndirectBr: 2832 return SelectIndirectBr(I); 2833 case Instruction::ICmp: 2834 case Instruction::FCmp: 2835 return SelectCmp(I); 2836 case Instruction::FPExt: 2837 return SelectFPExt(I); 2838 case Instruction::FPTrunc: 2839 return SelectFPTrunc(I); 2840 case Instruction::SIToFP: 2841 return SelectIToFP(I, /*isSigned*/ true); 2842 case Instruction::UIToFP: 2843 return SelectIToFP(I, /*isSigned*/ false); 2844 case Instruction::FPToSI: 2845 return SelectFPToI(I, /*isSigned*/ true); 2846 case Instruction::FPToUI: 2847 return SelectFPToI(I, /*isSigned*/ false); 2848 case Instruction::Add: 2849 return SelectBinaryIntOp(I, ISD::ADD); 2850 case Instruction::Or: 2851 return SelectBinaryIntOp(I, ISD::OR); 2852 case Instruction::Sub: 2853 return SelectBinaryIntOp(I, ISD::SUB); 2854 case Instruction::FAdd: 2855 return SelectBinaryFPOp(I, ISD::FADD); 2856 case Instruction::FSub: 2857 return SelectBinaryFPOp(I, ISD::FSUB); 2858 case Instruction::FMul: 2859 return SelectBinaryFPOp(I, ISD::FMUL); 2860 case Instruction::SDiv: 2861 return SelectDiv(I, /*isSigned*/ true); 2862 case Instruction::UDiv: 2863 return SelectDiv(I, /*isSigned*/ false); 2864 case Instruction::SRem: 2865 return SelectRem(I, /*isSigned*/ true); 2866 case Instruction::URem: 2867 return SelectRem(I, /*isSigned*/ false); 2868 case Instruction::Call: 2869 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) 2870 return SelectIntrinsicCall(*II); 2871 return SelectCall(I); 2872 case Instruction::Select: 2873 return SelectSelect(I); 2874 case Instruction::Ret: 2875 return SelectRet(I); 2876 case Instruction::Trunc: 2877 return SelectTrunc(I); 2878 case Instruction::ZExt: 2879 case Instruction::SExt: 2880 return SelectIntExt(I); 2881 case Instruction::Shl: 2882 return SelectShift(I, ARM_AM::lsl); 2883 case Instruction::LShr: 2884 return SelectShift(I, ARM_AM::lsr); 2885 case Instruction::AShr: 2886 return SelectShift(I, ARM_AM::asr); 2887 default: break; 2888 } 2889 return false; 2890 } 2891 2892 namespace { 2893 2894 // This table describes sign- and zero-extend instructions which can be 2895 // folded into a preceding load. All of these extends have an immediate 2896 // (sometimes a mask and sometimes a shift) that's applied after 2897 // extension. 2898 const struct FoldableLoadExtendsStruct { 2899 uint16_t Opc[2]; // ARM, Thumb. 2900 uint8_t ExpectedImm; 2901 uint8_t isZExt : 1; 2902 uint8_t ExpectedVT : 7; 2903 } FoldableLoadExtends[] = { 2904 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 }, 2905 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 }, 2906 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 }, 2907 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 }, 2908 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 } 2909 }; 2910 2911 } // end anonymous namespace 2912 2913 /// \brief The specified machine instr operand is a vreg, and that 2914 /// vreg is being provided by the specified load instruction. If possible, 2915 /// try to fold the load as an operand to the instruction, returning true if 2916 /// successful. 2917 bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo, 2918 const LoadInst *LI) { 2919 // Verify we have a legal type before going any further. 2920 MVT VT; 2921 if (!isLoadTypeLegal(LI->getType(), VT)) 2922 return false; 2923 2924 // Combine load followed by zero- or sign-extend. 2925 // ldrb r1, [r0] ldrb r1, [r0] 2926 // uxtb r2, r1 => 2927 // mov r3, r2 mov r3, r1 2928 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm()) 2929 return false; 2930 const uint64_t Imm = MI->getOperand(2).getImm(); 2931 2932 bool Found = false; 2933 bool isZExt; 2934 for (const FoldableLoadExtendsStruct &FLE : FoldableLoadExtends) { 2935 if (FLE.Opc[isThumb2] == MI->getOpcode() && 2936 (uint64_t)FLE.ExpectedImm == Imm && 2937 MVT((MVT::SimpleValueType)FLE.ExpectedVT) == VT) { 2938 Found = true; 2939 isZExt = FLE.isZExt; 2940 } 2941 } 2942 if (!Found) return false; 2943 2944 // See if we can handle this address. 2945 Address Addr; 2946 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false; 2947 2948 unsigned ResultReg = MI->getOperand(0).getReg(); 2949 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false)) 2950 return false; 2951 MI->eraseFromParent(); 2952 return true; 2953 } 2954 2955 unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV, 2956 unsigned Align, MVT VT) { 2957 bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV); 2958 2959 LLVMContext *Context = &MF->getFunction()->getContext(); 2960 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2961 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2962 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create( 2963 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj, 2964 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier, 2965 /*AddCurrentAddress=*/UseGOT_PREL); 2966 2967 unsigned ConstAlign = 2968 MF->getDataLayout().getPrefTypeAlignment(Type::getInt32PtrTy(*Context)); 2969 unsigned Idx = MF->getConstantPool()->getConstantPoolIndex(CPV, ConstAlign); 2970 2971 unsigned TempReg = MF->getRegInfo().createVirtualRegister(&ARM::rGPRRegClass); 2972 unsigned Opc = isThumb2 ? ARM::t2LDRpci : ARM::LDRcp; 2973 MachineInstrBuilder MIB = 2974 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), TempReg) 2975 .addConstantPoolIndex(Idx); 2976 if (Opc == ARM::LDRcp) 2977 MIB.addImm(0); 2978 MIB.add(predOps(ARMCC::AL)); 2979 2980 // Fix the address by adding pc. 2981 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); 2982 Opc = Subtarget->isThumb() ? ARM::tPICADD : UseGOT_PREL ? ARM::PICLDR 2983 : ARM::PICADD; 2984 DestReg = constrainOperandRegClass(TII.get(Opc), DestReg, 0); 2985 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg) 2986 .addReg(TempReg) 2987 .addImm(ARMPCLabelIndex); 2988 if (!Subtarget->isThumb()) 2989 MIB.add(predOps(ARMCC::AL)); 2990 2991 if (UseGOT_PREL && Subtarget->isThumb()) { 2992 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT)); 2993 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2994 TII.get(ARM::t2LDRi12), NewDestReg) 2995 .addReg(DestReg) 2996 .addImm(0); 2997 DestReg = NewDestReg; 2998 AddOptionalDefs(MIB); 2999 } 3000 return DestReg; 3001 } 3002 3003 bool ARMFastISel::fastLowerArguments() { 3004 if (!FuncInfo.CanLowerReturn) 3005 return false; 3006 3007 const Function *F = FuncInfo.Fn; 3008 if (F->isVarArg()) 3009 return false; 3010 3011 CallingConv::ID CC = F->getCallingConv(); 3012 switch (CC) { 3013 default: 3014 return false; 3015 case CallingConv::Fast: 3016 case CallingConv::C: 3017 case CallingConv::ARM_AAPCS_VFP: 3018 case CallingConv::ARM_AAPCS: 3019 case CallingConv::ARM_APCS: 3020 case CallingConv::Swift: 3021 break; 3022 } 3023 3024 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments 3025 // which are passed in r0 - r3. 3026 for (const Argument &Arg : F->args()) { 3027 if (Arg.getArgNo() >= 4) 3028 return false; 3029 3030 if (Arg.hasAttribute(Attribute::InReg) || 3031 Arg.hasAttribute(Attribute::StructRet) || 3032 Arg.hasAttribute(Attribute::SwiftSelf) || 3033 Arg.hasAttribute(Attribute::SwiftError) || 3034 Arg.hasAttribute(Attribute::ByVal)) 3035 return false; 3036 3037 Type *ArgTy = Arg.getType(); 3038 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) 3039 return false; 3040 3041 EVT ArgVT = TLI.getValueType(DL, ArgTy); 3042 if (!ArgVT.isSimple()) return false; 3043 switch (ArgVT.getSimpleVT().SimpleTy) { 3044 case MVT::i8: 3045 case MVT::i16: 3046 case MVT::i32: 3047 break; 3048 default: 3049 return false; 3050 } 3051 } 3052 3053 static const MCPhysReg GPRArgRegs[] = { 3054 ARM::R0, ARM::R1, ARM::R2, ARM::R3 3055 }; 3056 3057 const TargetRegisterClass *RC = &ARM::rGPRRegClass; 3058 for (const Argument &Arg : F->args()) { 3059 unsigned ArgNo = Arg.getArgNo(); 3060 unsigned SrcReg = GPRArgRegs[ArgNo]; 3061 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC); 3062 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy. 3063 // Without this, EmitLiveInCopies may eliminate the livein if its only 3064 // use is a bitcast (which isn't turned into an instruction). 3065 unsigned ResultReg = createResultReg(RC); 3066 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 3067 TII.get(TargetOpcode::COPY), 3068 ResultReg).addReg(DstReg, getKillRegState(true)); 3069 updateValueMap(&Arg, ResultReg); 3070 } 3071 3072 return true; 3073 } 3074 3075 namespace llvm { 3076 3077 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo, 3078 const TargetLibraryInfo *libInfo) { 3079 if (funcInfo.MF->getSubtarget<ARMSubtarget>().useFastISel()) 3080 return new ARMFastISel(funcInfo, libInfo); 3081 3082 return nullptr; 3083 } 3084 3085 } // end namespace llvm 3086