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