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