1 //===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the ARM-specific support for the FastISel class. Some 11 // of the target-specific code is generated by tablegen in the file 12 // ARMGenFastISel.inc, which is #included here. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "ARM.h" 17 #include "ARMBaseInstrInfo.h" 18 #include "ARMCallingConv.h" 19 #include "ARMRegisterInfo.h" 20 #include "ARMTargetMachine.h" 21 #include "ARMSubtarget.h" 22 #include "ARMConstantPoolValue.h" 23 #include "MCTargetDesc/ARMAddressingModes.h" 24 #include "llvm/CallingConv.h" 25 #include "llvm/DerivedTypes.h" 26 #include "llvm/GlobalVariable.h" 27 #include "llvm/Instructions.h" 28 #include "llvm/IntrinsicInst.h" 29 #include "llvm/Module.h" 30 #include "llvm/Operator.h" 31 #include "llvm/CodeGen/Analysis.h" 32 #include "llvm/CodeGen/FastISel.h" 33 #include "llvm/CodeGen/FunctionLoweringInfo.h" 34 #include "llvm/CodeGen/MachineInstrBuilder.h" 35 #include "llvm/CodeGen/MachineModuleInfo.h" 36 #include "llvm/CodeGen/MachineConstantPool.h" 37 #include "llvm/CodeGen/MachineFrameInfo.h" 38 #include "llvm/CodeGen/MachineMemOperand.h" 39 #include "llvm/CodeGen/MachineRegisterInfo.h" 40 #include "llvm/Support/CallSite.h" 41 #include "llvm/Support/CommandLine.h" 42 #include "llvm/Support/ErrorHandling.h" 43 #include "llvm/Support/GetElementPtrTypeIterator.h" 44 #include "llvm/Target/TargetData.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 static cl::opt<bool> 52 DisableARMFastISel("disable-arm-fast-isel", 53 cl::desc("Turn off experimental ARM fast-isel support"), 54 cl::init(false), cl::Hidden); 55 56 extern cl::opt<bool> EnableARMLongCalls; 57 58 namespace { 59 60 // All possible address modes, plus some. 61 typedef struct Address { 62 enum { 63 RegBase, 64 FrameIndexBase 65 } BaseType; 66 67 union { 68 unsigned Reg; 69 int FI; 70 } Base; 71 72 int Offset; 73 74 // Innocuous defaults for our address. 75 Address() 76 : BaseType(RegBase), Offset(0) { 77 Base.Reg = 0; 78 } 79 } Address; 80 81 class ARMFastISel : public FastISel { 82 83 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can 84 /// make the right decision when generating code for different targets. 85 const ARMSubtarget *Subtarget; 86 const TargetMachine &TM; 87 const TargetInstrInfo &TII; 88 const TargetLowering &TLI; 89 ARMFunctionInfo *AFI; 90 91 // Convenience variables to avoid some queries. 92 bool isThumb2; 93 LLVMContext *Context; 94 95 public: 96 explicit ARMFastISel(FunctionLoweringInfo &funcInfo) 97 : FastISel(funcInfo), 98 TM(funcInfo.MF->getTarget()), 99 TII(*TM.getInstrInfo()), 100 TLI(*TM.getTargetLowering()) { 101 Subtarget = &TM.getSubtarget<ARMSubtarget>(); 102 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>(); 103 isThumb2 = AFI->isThumbFunction(); 104 Context = &funcInfo.Fn->getContext(); 105 } 106 107 // Code from FastISel.cpp. 108 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode, 109 const TargetRegisterClass *RC); 110 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode, 111 const TargetRegisterClass *RC, 112 unsigned Op0, bool Op0IsKill); 113 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode, 114 const TargetRegisterClass *RC, 115 unsigned Op0, bool Op0IsKill, 116 unsigned Op1, bool Op1IsKill); 117 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode, 118 const TargetRegisterClass *RC, 119 unsigned Op0, bool Op0IsKill, 120 unsigned Op1, bool Op1IsKill, 121 unsigned Op2, bool Op2IsKill); 122 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode, 123 const TargetRegisterClass *RC, 124 unsigned Op0, bool Op0IsKill, 125 uint64_t Imm); 126 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode, 127 const TargetRegisterClass *RC, 128 unsigned Op0, bool Op0IsKill, 129 const ConstantFP *FPImm); 130 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode, 131 const TargetRegisterClass *RC, 132 unsigned Op0, bool Op0IsKill, 133 unsigned Op1, bool Op1IsKill, 134 uint64_t Imm); 135 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode, 136 const TargetRegisterClass *RC, 137 uint64_t Imm); 138 virtual unsigned FastEmitInst_ii(unsigned MachineInstOpcode, 139 const TargetRegisterClass *RC, 140 uint64_t Imm1, uint64_t Imm2); 141 142 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT, 143 unsigned Op0, bool Op0IsKill, 144 uint32_t Idx); 145 146 // Backend specific FastISel code. 147 virtual bool TargetSelectInstruction(const Instruction *I); 148 virtual unsigned TargetMaterializeConstant(const Constant *C); 149 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI); 150 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo, 151 const LoadInst *LI); 152 153 #include "ARMGenFastISel.inc" 154 155 // Instruction selection routines. 156 private: 157 bool SelectLoad(const Instruction *I); 158 bool SelectStore(const Instruction *I); 159 bool SelectBranch(const Instruction *I); 160 bool SelectCmp(const Instruction *I); 161 bool SelectFPExt(const Instruction *I); 162 bool SelectFPTrunc(const Instruction *I); 163 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode); 164 bool SelectIToFP(const Instruction *I, bool isSigned); 165 bool SelectFPToI(const Instruction *I, bool isSigned); 166 bool SelectDiv(const Instruction *I, bool isSigned); 167 bool SelectRem(const Instruction *I, bool isSigned); 168 bool SelectCall(const Instruction *I, const char *IntrMemName); 169 bool SelectIntrinsicCall(const IntrinsicInst &I); 170 bool SelectSelect(const Instruction *I); 171 bool SelectRet(const Instruction *I); 172 bool SelectTrunc(const Instruction *I); 173 bool SelectIntExt(const Instruction *I); 174 175 // Utility routines. 176 private: 177 bool isTypeLegal(Type *Ty, MVT &VT); 178 bool isLoadTypeLegal(Type *Ty, MVT &VT); 179 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value, 180 bool isZExt); 181 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr, 182 unsigned Alignment = 0, bool isZExt = true, 183 bool allocReg = true); 184 185 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr, 186 unsigned Alignment = 0); 187 bool ARMComputeAddress(const Value *Obj, Address &Addr); 188 void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3); 189 bool ARMIsMemCpySmall(uint64_t Len); 190 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len); 191 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt); 192 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT); 193 unsigned ARMMaterializeInt(const Constant *C, EVT VT); 194 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT); 195 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg); 196 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg); 197 unsigned ARMSelectCallOp(const GlobalValue *GV); 198 199 // Call handling routines. 200 private: 201 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return); 202 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args, 203 SmallVectorImpl<unsigned> &ArgRegs, 204 SmallVectorImpl<MVT> &ArgVTs, 205 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, 206 SmallVectorImpl<unsigned> &RegArgs, 207 CallingConv::ID CC, 208 unsigned &NumBytes); 209 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, 210 const Instruction *I, CallingConv::ID CC, 211 unsigned &NumBytes); 212 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call); 213 214 // OptionalDef handling routines. 215 private: 216 bool isARMNEONPred(const MachineInstr *MI); 217 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR); 218 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB); 219 void AddLoadStoreOperands(EVT VT, Address &Addr, 220 const MachineInstrBuilder &MIB, 221 unsigned Flags, bool useAM3); 222 }; 223 224 } // end anonymous namespace 225 226 #include "ARMGenCallingConv.inc" 227 228 // DefinesOptionalPredicate - This is different from DefinesPredicate in that 229 // we don't care about implicit defs here, just places we'll need to add a 230 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR. 231 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) { 232 if (!MI->hasOptionalDef()) 233 return false; 234 235 // Look to see if our OptionalDef is defining CPSR or CCR. 236 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 237 const MachineOperand &MO = MI->getOperand(i); 238 if (!MO.isReg() || !MO.isDef()) continue; 239 if (MO.getReg() == ARM::CPSR) 240 *CPSR = true; 241 } 242 return true; 243 } 244 245 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) { 246 const MCInstrDesc &MCID = MI->getDesc(); 247 248 // If we're a thumb2 or not NEON function we were handled via isPredicable. 249 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON || 250 AFI->isThumb2Function()) 251 return false; 252 253 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) 254 if (MCID.OpInfo[i].isPredicate()) 255 return true; 256 257 return false; 258 } 259 260 // If the machine is predicable go ahead and add the predicate operands, if 261 // it needs default CC operands add those. 262 // TODO: If we want to support thumb1 then we'll need to deal with optional 263 // CPSR defs that need to be added before the remaining operands. See s_cc_out 264 // for descriptions why. 265 const MachineInstrBuilder & 266 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) { 267 MachineInstr *MI = &*MIB; 268 269 // Do we use a predicate? or... 270 // Are we NEON in ARM mode and have a predicate operand? If so, I know 271 // we're not predicable but add it anyways. 272 if (TII.isPredicable(MI) || isARMNEONPred(MI)) 273 AddDefaultPred(MIB); 274 275 // Do we optionally set a predicate? Preds is size > 0 iff the predicate 276 // defines CPSR. All other OptionalDefines in ARM are the CCR register. 277 bool CPSR = false; 278 if (DefinesOptionalPredicate(MI, &CPSR)) { 279 if (CPSR) 280 AddDefaultT1CC(MIB); 281 else 282 AddDefaultCC(MIB); 283 } 284 return MIB; 285 } 286 287 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode, 288 const TargetRegisterClass* RC) { 289 unsigned ResultReg = createResultReg(RC); 290 const MCInstrDesc &II = TII.get(MachineInstOpcode); 291 292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)); 293 return ResultReg; 294 } 295 296 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode, 297 const TargetRegisterClass *RC, 298 unsigned Op0, bool Op0IsKill) { 299 unsigned ResultReg = createResultReg(RC); 300 const MCInstrDesc &II = TII.get(MachineInstOpcode); 301 302 if (II.getNumDefs() >= 1) 303 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) 304 .addReg(Op0, Op0IsKill * RegState::Kill)); 305 else { 306 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) 307 .addReg(Op0, Op0IsKill * RegState::Kill)); 308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 309 TII.get(TargetOpcode::COPY), ResultReg) 310 .addReg(II.ImplicitDefs[0])); 311 } 312 return ResultReg; 313 } 314 315 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode, 316 const TargetRegisterClass *RC, 317 unsigned Op0, bool Op0IsKill, 318 unsigned Op1, bool Op1IsKill) { 319 unsigned ResultReg = createResultReg(RC); 320 const MCInstrDesc &II = TII.get(MachineInstOpcode); 321 322 if (II.getNumDefs() >= 1) 323 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) 324 .addReg(Op0, Op0IsKill * RegState::Kill) 325 .addReg(Op1, Op1IsKill * RegState::Kill)); 326 else { 327 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) 328 .addReg(Op0, Op0IsKill * RegState::Kill) 329 .addReg(Op1, Op1IsKill * RegState::Kill)); 330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 331 TII.get(TargetOpcode::COPY), ResultReg) 332 .addReg(II.ImplicitDefs[0])); 333 } 334 return ResultReg; 335 } 336 337 unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode, 338 const TargetRegisterClass *RC, 339 unsigned Op0, bool Op0IsKill, 340 unsigned Op1, bool Op1IsKill, 341 unsigned Op2, bool Op2IsKill) { 342 unsigned ResultReg = createResultReg(RC); 343 const MCInstrDesc &II = TII.get(MachineInstOpcode); 344 345 if (II.getNumDefs() >= 1) 346 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) 347 .addReg(Op0, Op0IsKill * RegState::Kill) 348 .addReg(Op1, Op1IsKill * RegState::Kill) 349 .addReg(Op2, Op2IsKill * RegState::Kill)); 350 else { 351 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) 352 .addReg(Op0, Op0IsKill * RegState::Kill) 353 .addReg(Op1, Op1IsKill * RegState::Kill) 354 .addReg(Op2, Op2IsKill * RegState::Kill)); 355 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 356 TII.get(TargetOpcode::COPY), ResultReg) 357 .addReg(II.ImplicitDefs[0])); 358 } 359 return ResultReg; 360 } 361 362 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode, 363 const TargetRegisterClass *RC, 364 unsigned Op0, bool Op0IsKill, 365 uint64_t Imm) { 366 unsigned ResultReg = createResultReg(RC); 367 const MCInstrDesc &II = TII.get(MachineInstOpcode); 368 369 if (II.getNumDefs() >= 1) 370 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) 371 .addReg(Op0, Op0IsKill * RegState::Kill) 372 .addImm(Imm)); 373 else { 374 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) 375 .addReg(Op0, Op0IsKill * RegState::Kill) 376 .addImm(Imm)); 377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 378 TII.get(TargetOpcode::COPY), ResultReg) 379 .addReg(II.ImplicitDefs[0])); 380 } 381 return ResultReg; 382 } 383 384 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode, 385 const TargetRegisterClass *RC, 386 unsigned Op0, bool Op0IsKill, 387 const ConstantFP *FPImm) { 388 unsigned ResultReg = createResultReg(RC); 389 const MCInstrDesc &II = TII.get(MachineInstOpcode); 390 391 if (II.getNumDefs() >= 1) 392 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) 393 .addReg(Op0, Op0IsKill * RegState::Kill) 394 .addFPImm(FPImm)); 395 else { 396 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) 397 .addReg(Op0, Op0IsKill * RegState::Kill) 398 .addFPImm(FPImm)); 399 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 400 TII.get(TargetOpcode::COPY), ResultReg) 401 .addReg(II.ImplicitDefs[0])); 402 } 403 return ResultReg; 404 } 405 406 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode, 407 const TargetRegisterClass *RC, 408 unsigned Op0, bool Op0IsKill, 409 unsigned Op1, bool Op1IsKill, 410 uint64_t Imm) { 411 unsigned ResultReg = createResultReg(RC); 412 const MCInstrDesc &II = TII.get(MachineInstOpcode); 413 414 if (II.getNumDefs() >= 1) 415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) 416 .addReg(Op0, Op0IsKill * RegState::Kill) 417 .addReg(Op1, Op1IsKill * RegState::Kill) 418 .addImm(Imm)); 419 else { 420 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) 421 .addReg(Op0, Op0IsKill * RegState::Kill) 422 .addReg(Op1, Op1IsKill * RegState::Kill) 423 .addImm(Imm)); 424 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 425 TII.get(TargetOpcode::COPY), ResultReg) 426 .addReg(II.ImplicitDefs[0])); 427 } 428 return ResultReg; 429 } 430 431 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode, 432 const TargetRegisterClass *RC, 433 uint64_t Imm) { 434 unsigned ResultReg = createResultReg(RC); 435 const MCInstrDesc &II = TII.get(MachineInstOpcode); 436 437 if (II.getNumDefs() >= 1) 438 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) 439 .addImm(Imm)); 440 else { 441 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) 442 .addImm(Imm)); 443 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 444 TII.get(TargetOpcode::COPY), ResultReg) 445 .addReg(II.ImplicitDefs[0])); 446 } 447 return ResultReg; 448 } 449 450 unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode, 451 const TargetRegisterClass *RC, 452 uint64_t Imm1, uint64_t Imm2) { 453 unsigned ResultReg = createResultReg(RC); 454 const MCInstrDesc &II = TII.get(MachineInstOpcode); 455 456 if (II.getNumDefs() >= 1) 457 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) 458 .addImm(Imm1).addImm(Imm2)); 459 else { 460 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) 461 .addImm(Imm1).addImm(Imm2)); 462 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 463 TII.get(TargetOpcode::COPY), 464 ResultReg) 465 .addReg(II.ImplicitDefs[0])); 466 } 467 return ResultReg; 468 } 469 470 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT, 471 unsigned Op0, bool Op0IsKill, 472 uint32_t Idx) { 473 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); 474 assert(TargetRegisterInfo::isVirtualRegister(Op0) && 475 "Cannot yet extract from physregs"); 476 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, 477 DL, TII.get(TargetOpcode::COPY), ResultReg) 478 .addReg(Op0, getKillRegState(Op0IsKill), Idx)); 479 return ResultReg; 480 } 481 482 // TODO: Don't worry about 64-bit now, but when this is fixed remove the 483 // checks from the various callers. 484 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) { 485 if (VT == MVT::f64) return 0; 486 487 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); 488 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 489 TII.get(ARM::VMOVRS), MoveReg) 490 .addReg(SrcReg)); 491 return MoveReg; 492 } 493 494 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) { 495 if (VT == MVT::i64) return 0; 496 497 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); 498 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 499 TII.get(ARM::VMOVSR), MoveReg) 500 .addReg(SrcReg)); 501 return MoveReg; 502 } 503 504 // For double width floating point we need to materialize two constants 505 // (the high and the low) into integer registers then use a move to get 506 // the combined constant into an FP reg. 507 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) { 508 const APFloat Val = CFP->getValueAPF(); 509 bool is64bit = VT == MVT::f64; 510 511 // This checks to see if we can use VFP3 instructions to materialize 512 // a constant, otherwise we have to go through the constant pool. 513 if (TLI.isFPImmLegal(Val, VT)) { 514 int Imm; 515 unsigned Opc; 516 if (is64bit) { 517 Imm = ARM_AM::getFP64Imm(Val); 518 Opc = ARM::FCONSTD; 519 } else { 520 Imm = ARM_AM::getFP32Imm(Val); 521 Opc = ARM::FCONSTS; 522 } 523 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); 524 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), 525 DestReg) 526 .addImm(Imm)); 527 return DestReg; 528 } 529 530 // Require VFP2 for loading fp constants. 531 if (!Subtarget->hasVFP2()) return false; 532 533 // MachineConstantPool wants an explicit alignment. 534 unsigned Align = TD.getPrefTypeAlignment(CFP->getType()); 535 if (Align == 0) { 536 // TODO: Figure out if this is correct. 537 Align = TD.getTypeAllocSize(CFP->getType()); 538 } 539 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align); 540 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); 541 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS; 542 543 // The extra reg is for addrmode5. 544 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), 545 DestReg) 546 .addConstantPoolIndex(Idx) 547 .addReg(0)); 548 return DestReg; 549 } 550 551 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) { 552 553 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1) 554 return false; 555 556 // If we can do this in a single instruction without a constant pool entry 557 // do so now. 558 const ConstantInt *CI = cast<ConstantInt>(C); 559 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) { 560 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16; 561 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32)); 562 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 563 TII.get(Opc), ImmReg) 564 .addImm(CI->getZExtValue())); 565 return ImmReg; 566 } 567 568 // Use MVN to emit negative constants. 569 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) { 570 unsigned Imm = (unsigned)~(CI->getSExtValue()); 571 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : 572 (ARM_AM::getSOImmVal(Imm) != -1); 573 if (UseImm) { 574 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi; 575 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32)); 576 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 577 TII.get(Opc), ImmReg) 578 .addImm(Imm)); 579 return ImmReg; 580 } 581 } 582 583 // Load from constant pool. For now 32-bit only. 584 if (VT != MVT::i32) 585 return false; 586 587 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); 588 589 // MachineConstantPool wants an explicit alignment. 590 unsigned Align = TD.getPrefTypeAlignment(C->getType()); 591 if (Align == 0) { 592 // TODO: Figure out if this is correct. 593 Align = TD.getTypeAllocSize(C->getType()); 594 } 595 unsigned Idx = MCP.getConstantPoolIndex(C, Align); 596 597 if (isThumb2) 598 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 599 TII.get(ARM::t2LDRpci), DestReg) 600 .addConstantPoolIndex(Idx)); 601 else 602 // The extra immediate is for addrmode2. 603 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 604 TII.get(ARM::LDRcp), DestReg) 605 .addConstantPoolIndex(Idx) 606 .addImm(0)); 607 608 return DestReg; 609 } 610 611 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) { 612 // For now 32-bit only. 613 if (VT != MVT::i32) return 0; 614 615 Reloc::Model RelocM = TM.getRelocationModel(); 616 617 // TODO: Need more magic for ARM PIC. 618 if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0; 619 620 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); 621 622 // Use movw+movt when possible, it avoids constant pool entries. 623 // Darwin targets don't support movt with Reloc::Static, see 624 // ARMTargetLowering::LowerGlobalAddressDarwin. Other targets only support 625 // static movt relocations. 626 if (Subtarget->useMovt() && 627 Subtarget->isTargetDarwin() == (RelocM != Reloc::Static)) { 628 unsigned Opc; 629 switch (RelocM) { 630 case Reloc::PIC_: 631 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel; 632 break; 633 case Reloc::DynamicNoPIC: 634 Opc = isThumb2 ? ARM::t2MOV_ga_dyn : ARM::MOV_ga_dyn; 635 break; 636 default: 637 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm; 638 break; 639 } 640 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), 641 DestReg).addGlobalAddress(GV)); 642 } else { 643 // MachineConstantPool wants an explicit alignment. 644 unsigned Align = TD.getPrefTypeAlignment(GV->getType()); 645 if (Align == 0) { 646 // TODO: Figure out if this is correct. 647 Align = TD.getTypeAllocSize(GV->getType()); 648 } 649 650 // Grab index. 651 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : 652 (Subtarget->isThumb() ? 4 : 8); 653 unsigned Id = AFI->createPICLabelUId(); 654 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id, 655 ARMCP::CPValue, 656 PCAdj); 657 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align); 658 659 // Load value. 660 MachineInstrBuilder MIB; 661 if (isThumb2) { 662 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic; 663 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg) 664 .addConstantPoolIndex(Idx); 665 if (RelocM == Reloc::PIC_) 666 MIB.addImm(Id); 667 } else { 668 // The extra immediate is for addrmode2. 669 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp), 670 DestReg) 671 .addConstantPoolIndex(Idx) 672 .addImm(0); 673 } 674 AddOptionalDefs(MIB); 675 } 676 677 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) { 678 MachineInstrBuilder MIB; 679 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT)); 680 if (isThumb2) 681 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 682 TII.get(ARM::t2LDRi12), NewDestReg) 683 .addReg(DestReg) 684 .addImm(0); 685 else 686 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12), 687 NewDestReg) 688 .addReg(DestReg) 689 .addImm(0); 690 DestReg = NewDestReg; 691 AddOptionalDefs(MIB); 692 } 693 694 return DestReg; 695 } 696 697 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) { 698 EVT VT = TLI.getValueType(C->getType(), true); 699 700 // Only handle simple types. 701 if (!VT.isSimple()) return 0; 702 703 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) 704 return ARMMaterializeFP(CFP, VT); 705 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) 706 return ARMMaterializeGV(GV, VT); 707 else if (isa<ConstantInt>(C)) 708 return ARMMaterializeInt(C, VT); 709 710 return 0; 711 } 712 713 // TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF); 714 715 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) { 716 // Don't handle dynamic allocas. 717 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0; 718 719 MVT VT; 720 if (!isLoadTypeLegal(AI->getType(), VT)) return false; 721 722 DenseMap<const AllocaInst*, int>::iterator SI = 723 FuncInfo.StaticAllocaMap.find(AI); 724 725 // This will get lowered later into the correct offsets and registers 726 // via rewriteXFrameIndex. 727 if (SI != FuncInfo.StaticAllocaMap.end()) { 728 TargetRegisterClass* RC = TLI.getRegClassFor(VT); 729 unsigned ResultReg = createResultReg(RC); 730 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri; 731 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 732 TII.get(Opc), ResultReg) 733 .addFrameIndex(SI->second) 734 .addImm(0)); 735 return ResultReg; 736 } 737 738 return 0; 739 } 740 741 bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) { 742 EVT evt = TLI.getValueType(Ty, true); 743 744 // Only handle simple types. 745 if (evt == MVT::Other || !evt.isSimple()) return false; 746 VT = evt.getSimpleVT(); 747 748 // Handle all legal types, i.e. a register that will directly hold this 749 // value. 750 return TLI.isTypeLegal(VT); 751 } 752 753 bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) { 754 if (isTypeLegal(Ty, VT)) return true; 755 756 // If this is a type than can be sign or zero-extended to a basic operation 757 // go ahead and accept it now. 758 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) 759 return true; 760 761 return false; 762 } 763 764 // Computes the address to get to an object. 765 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) { 766 // Some boilerplate from the X86 FastISel. 767 const User *U = NULL; 768 unsigned Opcode = Instruction::UserOp1; 769 if (const Instruction *I = dyn_cast<Instruction>(Obj)) { 770 // Don't walk into other basic blocks unless the object is an alloca from 771 // another block, otherwise it may not have a virtual register assigned. 772 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) || 773 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) { 774 Opcode = I->getOpcode(); 775 U = I; 776 } 777 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { 778 Opcode = C->getOpcode(); 779 U = C; 780 } 781 782 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType())) 783 if (Ty->getAddressSpace() > 255) 784 // Fast instruction selection doesn't support the special 785 // address spaces. 786 return false; 787 788 switch (Opcode) { 789 default: 790 break; 791 case Instruction::BitCast: { 792 // Look through bitcasts. 793 return ARMComputeAddress(U->getOperand(0), Addr); 794 } 795 case Instruction::IntToPtr: { 796 // Look past no-op inttoptrs. 797 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy()) 798 return ARMComputeAddress(U->getOperand(0), Addr); 799 break; 800 } 801 case Instruction::PtrToInt: { 802 // Look past no-op ptrtoints. 803 if (TLI.getValueType(U->getType()) == TLI.getPointerTy()) 804 return ARMComputeAddress(U->getOperand(0), Addr); 805 break; 806 } 807 case Instruction::GetElementPtr: { 808 Address SavedAddr = Addr; 809 int TmpOffset = Addr.Offset; 810 811 // Iterate through the GEP folding the constants into offsets where 812 // we can. 813 gep_type_iterator GTI = gep_type_begin(U); 814 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); 815 i != e; ++i, ++GTI) { 816 const Value *Op = *i; 817 if (StructType *STy = dyn_cast<StructType>(*GTI)) { 818 const StructLayout *SL = TD.getStructLayout(STy); 819 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); 820 TmpOffset += SL->getElementOffset(Idx); 821 } else { 822 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType()); 823 for (;;) { 824 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { 825 // Constant-offset addressing. 826 TmpOffset += CI->getSExtValue() * S; 827 break; 828 } 829 if (isa<AddOperator>(Op) && 830 (!isa<Instruction>(Op) || 831 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()] 832 == FuncInfo.MBB) && 833 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) { 834 // An add (in the same block) with a constant operand. Fold the 835 // constant. 836 ConstantInt *CI = 837 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1)); 838 TmpOffset += CI->getSExtValue() * S; 839 // Iterate on the other operand. 840 Op = cast<AddOperator>(Op)->getOperand(0); 841 continue; 842 } 843 // Unsupported 844 goto unsupported_gep; 845 } 846 } 847 } 848 849 // Try to grab the base operand now. 850 Addr.Offset = TmpOffset; 851 if (ARMComputeAddress(U->getOperand(0), Addr)) return true; 852 853 // We failed, restore everything and try the other options. 854 Addr = SavedAddr; 855 856 unsupported_gep: 857 break; 858 } 859 case Instruction::Alloca: { 860 const AllocaInst *AI = cast<AllocaInst>(Obj); 861 DenseMap<const AllocaInst*, int>::iterator SI = 862 FuncInfo.StaticAllocaMap.find(AI); 863 if (SI != FuncInfo.StaticAllocaMap.end()) { 864 Addr.BaseType = Address::FrameIndexBase; 865 Addr.Base.FI = SI->second; 866 return true; 867 } 868 break; 869 } 870 } 871 872 // Try to get this in a register if nothing else has worked. 873 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj); 874 return Addr.Base.Reg != 0; 875 } 876 877 void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) { 878 879 assert(VT.isSimple() && "Non-simple types are invalid here!"); 880 881 bool needsLowering = false; 882 switch (VT.getSimpleVT().SimpleTy) { 883 default: 884 assert(false && "Unhandled load/store type!"); 885 break; 886 case MVT::i1: 887 case MVT::i8: 888 case MVT::i16: 889 case MVT::i32: 890 if (!useAM3) { 891 // Integer loads/stores handle 12-bit offsets. 892 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset); 893 // Handle negative offsets. 894 if (needsLowering && isThumb2) 895 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 && 896 Addr.Offset > -256); 897 } else { 898 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets. 899 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255); 900 } 901 break; 902 case MVT::f32: 903 case MVT::f64: 904 // Floating point operands handle 8-bit offsets. 905 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset); 906 break; 907 } 908 909 // If this is a stack pointer and the offset needs to be simplified then 910 // put the alloca address into a register, set the base type back to 911 // register and continue. This should almost never happen. 912 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) { 913 TargetRegisterClass *RC = isThumb2 ? ARM::tGPRRegisterClass : 914 ARM::GPRRegisterClass; 915 unsigned ResultReg = createResultReg(RC); 916 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri; 917 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 918 TII.get(Opc), ResultReg) 919 .addFrameIndex(Addr.Base.FI) 920 .addImm(0)); 921 Addr.Base.Reg = ResultReg; 922 Addr.BaseType = Address::RegBase; 923 } 924 925 // Since the offset is too large for the load/store instruction 926 // get the reg+offset into a register. 927 if (needsLowering) { 928 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg, 929 /*Op0IsKill*/false, Addr.Offset, MVT::i32); 930 Addr.Offset = 0; 931 } 932 } 933 934 void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr, 935 const MachineInstrBuilder &MIB, 936 unsigned Flags, bool useAM3) { 937 // addrmode5 output depends on the selection dag addressing dividing the 938 // offset by 4 that it then later multiplies. Do this here as well. 939 if (VT.getSimpleVT().SimpleTy == MVT::f32 || 940 VT.getSimpleVT().SimpleTy == MVT::f64) 941 Addr.Offset /= 4; 942 943 // Frame base works a bit differently. Handle it separately. 944 if (Addr.BaseType == Address::FrameIndexBase) { 945 int FI = Addr.Base.FI; 946 int Offset = Addr.Offset; 947 MachineMemOperand *MMO = 948 FuncInfo.MF->getMachineMemOperand( 949 MachinePointerInfo::getFixedStack(FI, Offset), 950 Flags, 951 MFI.getObjectSize(FI), 952 MFI.getObjectAlignment(FI)); 953 // Now add the rest of the operands. 954 MIB.addFrameIndex(FI); 955 956 // ARM halfword load/stores and signed byte loads need an additional 957 // operand. 958 if (useAM3) { 959 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset; 960 MIB.addReg(0); 961 MIB.addImm(Imm); 962 } else { 963 MIB.addImm(Addr.Offset); 964 } 965 MIB.addMemOperand(MMO); 966 } else { 967 // Now add the rest of the operands. 968 MIB.addReg(Addr.Base.Reg); 969 970 // ARM halfword load/stores and signed byte loads need an additional 971 // operand. 972 if (useAM3) { 973 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset; 974 MIB.addReg(0); 975 MIB.addImm(Imm); 976 } else { 977 MIB.addImm(Addr.Offset); 978 } 979 } 980 AddOptionalDefs(MIB); 981 } 982 983 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr, 984 unsigned Alignment, bool isZExt, bool allocReg) { 985 assert(VT.isSimple() && "Non-simple types are invalid here!"); 986 unsigned Opc; 987 bool useAM3 = false; 988 bool needVMOV = false; 989 TargetRegisterClass *RC; 990 switch (VT.getSimpleVT().SimpleTy) { 991 // This is mostly going to be Neon/vector support. 992 default: return false; 993 case MVT::i1: 994 case MVT::i8: 995 if (isThumb2) { 996 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 997 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8; 998 else 999 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12; 1000 } else { 1001 if (isZExt) { 1002 Opc = ARM::LDRBi12; 1003 } else { 1004 Opc = ARM::LDRSB; 1005 useAM3 = true; 1006 } 1007 } 1008 RC = ARM::GPRRegisterClass; 1009 break; 1010 case MVT::i16: 1011 if (isThumb2) { 1012 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 1013 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8; 1014 else 1015 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12; 1016 } else { 1017 Opc = isZExt ? ARM::LDRH : ARM::LDRSH; 1018 useAM3 = true; 1019 } 1020 RC = ARM::GPRRegisterClass; 1021 break; 1022 case MVT::i32: 1023 if (isThumb2) { 1024 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 1025 Opc = ARM::t2LDRi8; 1026 else 1027 Opc = ARM::t2LDRi12; 1028 } else { 1029 Opc = ARM::LDRi12; 1030 } 1031 RC = ARM::GPRRegisterClass; 1032 break; 1033 case MVT::f32: 1034 if (!Subtarget->hasVFP2()) return false; 1035 // Unaligned loads need special handling. Floats require word-alignment. 1036 if (Alignment && Alignment < 4) { 1037 needVMOV = true; 1038 VT = MVT::i32; 1039 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12; 1040 RC = ARM::GPRRegisterClass; 1041 } else { 1042 Opc = ARM::VLDRS; 1043 RC = TLI.getRegClassFor(VT); 1044 } 1045 break; 1046 case MVT::f64: 1047 if (!Subtarget->hasVFP2()) return false; 1048 // FIXME: Unaligned loads need special handling. Doublewords require 1049 // word-alignment. 1050 if (Alignment && Alignment < 4) 1051 return false; 1052 1053 Opc = ARM::VLDRD; 1054 RC = TLI.getRegClassFor(VT); 1055 break; 1056 } 1057 // Simplify this down to something we can handle. 1058 ARMSimplifyAddress(Addr, VT, useAM3); 1059 1060 // Create the base instruction, then add the operands. 1061 if (allocReg) 1062 ResultReg = createResultReg(RC); 1063 assert (ResultReg > 255 && "Expected an allocated virtual register."); 1064 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1065 TII.get(Opc), ResultReg); 1066 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3); 1067 1068 // If we had an unaligned load of a float we've converted it to an regular 1069 // load. Now we must move from the GRP to the FP register. 1070 if (needVMOV) { 1071 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32)); 1072 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1073 TII.get(ARM::VMOVSR), MoveReg) 1074 .addReg(ResultReg)); 1075 ResultReg = MoveReg; 1076 } 1077 return true; 1078 } 1079 1080 bool ARMFastISel::SelectLoad(const Instruction *I) { 1081 // Atomic loads need special handling. 1082 if (cast<LoadInst>(I)->isAtomic()) 1083 return false; 1084 1085 // Verify we have a legal type before going any further. 1086 MVT VT; 1087 if (!isLoadTypeLegal(I->getType(), VT)) 1088 return false; 1089 1090 // See if we can handle this address. 1091 Address Addr; 1092 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false; 1093 1094 unsigned ResultReg; 1095 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment())) 1096 return false; 1097 UpdateValueMap(I, ResultReg); 1098 return true; 1099 } 1100 1101 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr, 1102 unsigned Alignment) { 1103 unsigned StrOpc; 1104 bool useAM3 = false; 1105 switch (VT.getSimpleVT().SimpleTy) { 1106 // This is mostly going to be Neon/vector support. 1107 default: return false; 1108 case MVT::i1: { 1109 unsigned Res = createResultReg(isThumb2 ? ARM::tGPRRegisterClass : 1110 ARM::GPRRegisterClass); 1111 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri; 1112 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1113 TII.get(Opc), Res) 1114 .addReg(SrcReg).addImm(1)); 1115 SrcReg = Res; 1116 } // Fallthrough here. 1117 case MVT::i8: 1118 if (isThumb2) { 1119 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 1120 StrOpc = ARM::t2STRBi8; 1121 else 1122 StrOpc = ARM::t2STRBi12; 1123 } else { 1124 StrOpc = ARM::STRBi12; 1125 } 1126 break; 1127 case MVT::i16: 1128 if (isThumb2) { 1129 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 1130 StrOpc = ARM::t2STRHi8; 1131 else 1132 StrOpc = ARM::t2STRHi12; 1133 } else { 1134 StrOpc = ARM::STRH; 1135 useAM3 = true; 1136 } 1137 break; 1138 case MVT::i32: 1139 if (isThumb2) { 1140 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) 1141 StrOpc = ARM::t2STRi8; 1142 else 1143 StrOpc = ARM::t2STRi12; 1144 } else { 1145 StrOpc = ARM::STRi12; 1146 } 1147 break; 1148 case MVT::f32: 1149 if (!Subtarget->hasVFP2()) return false; 1150 // Unaligned stores need special handling. Floats require word-alignment. 1151 if (Alignment && Alignment < 4) { 1152 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32)); 1153 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1154 TII.get(ARM::VMOVRS), MoveReg) 1155 .addReg(SrcReg)); 1156 SrcReg = MoveReg; 1157 VT = MVT::i32; 1158 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12; 1159 } else { 1160 StrOpc = ARM::VSTRS; 1161 } 1162 break; 1163 case MVT::f64: 1164 if (!Subtarget->hasVFP2()) return false; 1165 // FIXME: Unaligned stores need special handling. Doublewords require 1166 // word-alignment. 1167 if (Alignment && Alignment < 4) 1168 return false; 1169 1170 StrOpc = ARM::VSTRD; 1171 break; 1172 } 1173 // Simplify this down to something we can handle. 1174 ARMSimplifyAddress(Addr, VT, useAM3); 1175 1176 // Create the base instruction, then add the operands. 1177 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1178 TII.get(StrOpc)) 1179 .addReg(SrcReg); 1180 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3); 1181 return true; 1182 } 1183 1184 bool ARMFastISel::SelectStore(const Instruction *I) { 1185 Value *Op0 = I->getOperand(0); 1186 unsigned SrcReg = 0; 1187 1188 // Atomic stores need special handling. 1189 if (cast<StoreInst>(I)->isAtomic()) 1190 return false; 1191 1192 // Verify we have a legal type before going any further. 1193 MVT VT; 1194 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT)) 1195 return false; 1196 1197 // Get the value to be stored into a register. 1198 SrcReg = getRegForValue(Op0); 1199 if (SrcReg == 0) return false; 1200 1201 // See if we can handle this address. 1202 Address Addr; 1203 if (!ARMComputeAddress(I->getOperand(1), Addr)) 1204 return false; 1205 1206 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment())) 1207 return false; 1208 return true; 1209 } 1210 1211 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) { 1212 switch (Pred) { 1213 // Needs two compares... 1214 case CmpInst::FCMP_ONE: 1215 case CmpInst::FCMP_UEQ: 1216 default: 1217 // AL is our "false" for now. The other two need more compares. 1218 return ARMCC::AL; 1219 case CmpInst::ICMP_EQ: 1220 case CmpInst::FCMP_OEQ: 1221 return ARMCC::EQ; 1222 case CmpInst::ICMP_SGT: 1223 case CmpInst::FCMP_OGT: 1224 return ARMCC::GT; 1225 case CmpInst::ICMP_SGE: 1226 case CmpInst::FCMP_OGE: 1227 return ARMCC::GE; 1228 case CmpInst::ICMP_UGT: 1229 case CmpInst::FCMP_UGT: 1230 return ARMCC::HI; 1231 case CmpInst::FCMP_OLT: 1232 return ARMCC::MI; 1233 case CmpInst::ICMP_ULE: 1234 case CmpInst::FCMP_OLE: 1235 return ARMCC::LS; 1236 case CmpInst::FCMP_ORD: 1237 return ARMCC::VC; 1238 case CmpInst::FCMP_UNO: 1239 return ARMCC::VS; 1240 case CmpInst::FCMP_UGE: 1241 return ARMCC::PL; 1242 case CmpInst::ICMP_SLT: 1243 case CmpInst::FCMP_ULT: 1244 return ARMCC::LT; 1245 case CmpInst::ICMP_SLE: 1246 case CmpInst::FCMP_ULE: 1247 return ARMCC::LE; 1248 case CmpInst::FCMP_UNE: 1249 case CmpInst::ICMP_NE: 1250 return ARMCC::NE; 1251 case CmpInst::ICMP_UGE: 1252 return ARMCC::HS; 1253 case CmpInst::ICMP_ULT: 1254 return ARMCC::LO; 1255 } 1256 } 1257 1258 bool ARMFastISel::SelectBranch(const Instruction *I) { 1259 const BranchInst *BI = cast<BranchInst>(I); 1260 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; 1261 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; 1262 1263 // Simple branch support. 1264 1265 // If we can, avoid recomputing the compare - redoing it could lead to wonky 1266 // behavior. 1267 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { 1268 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) { 1269 1270 // Get the compare predicate. 1271 // Try to take advantage of fallthrough opportunities. 1272 CmpInst::Predicate Predicate = CI->getPredicate(); 1273 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { 1274 std::swap(TBB, FBB); 1275 Predicate = CmpInst::getInversePredicate(Predicate); 1276 } 1277 1278 ARMCC::CondCodes ARMPred = getComparePred(Predicate); 1279 1280 // We may not handle every CC for now. 1281 if (ARMPred == ARMCC::AL) return false; 1282 1283 // Emit the compare. 1284 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned())) 1285 return false; 1286 1287 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; 1288 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) 1289 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR); 1290 FastEmitBranch(FBB, DL); 1291 FuncInfo.MBB->addSuccessor(TBB); 1292 return true; 1293 } 1294 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) { 1295 MVT SourceVT; 1296 if (TI->hasOneUse() && TI->getParent() == I->getParent() && 1297 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) { 1298 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri; 1299 unsigned OpReg = getRegForValue(TI->getOperand(0)); 1300 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1301 TII.get(TstOpc)) 1302 .addReg(OpReg).addImm(1)); 1303 1304 unsigned CCMode = ARMCC::NE; 1305 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { 1306 std::swap(TBB, FBB); 1307 CCMode = ARMCC::EQ; 1308 } 1309 1310 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; 1311 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) 1312 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR); 1313 1314 FastEmitBranch(FBB, DL); 1315 FuncInfo.MBB->addSuccessor(TBB); 1316 return true; 1317 } 1318 } else if (const ConstantInt *CI = 1319 dyn_cast<ConstantInt>(BI->getCondition())) { 1320 uint64_t Imm = CI->getZExtValue(); 1321 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB; 1322 FastEmitBranch(Target, DL); 1323 return true; 1324 } 1325 1326 unsigned CmpReg = getRegForValue(BI->getCondition()); 1327 if (CmpReg == 0) return false; 1328 1329 // We've been divorced from our compare! Our block was split, and 1330 // now our compare lives in a predecessor block. We musn't 1331 // re-compare here, as the children of the compare aren't guaranteed 1332 // live across the block boundary (we *could* check for this). 1333 // Regardless, the compare has been done in the predecessor block, 1334 // and it left a value for us in a virtual register. Ergo, we test 1335 // the one-bit value left in the virtual register. 1336 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri; 1337 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc)) 1338 .addReg(CmpReg).addImm(1)); 1339 1340 unsigned CCMode = ARMCC::NE; 1341 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { 1342 std::swap(TBB, FBB); 1343 CCMode = ARMCC::EQ; 1344 } 1345 1346 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; 1347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) 1348 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR); 1349 FastEmitBranch(FBB, DL); 1350 FuncInfo.MBB->addSuccessor(TBB); 1351 return true; 1352 } 1353 1354 bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value, 1355 bool isZExt) { 1356 Type *Ty = Src1Value->getType(); 1357 EVT SrcVT = TLI.getValueType(Ty, true); 1358 if (!SrcVT.isSimple()) return false; 1359 1360 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy()); 1361 if (isFloat && !Subtarget->hasVFP2()) 1362 return false; 1363 1364 // Check to see if the 2nd operand is a constant that we can encode directly 1365 // in the compare. 1366 int Imm = 0; 1367 bool UseImm = false; 1368 bool isNegativeImm = false; 1369 // FIXME: At -O0 we don't have anything that canonicalizes operand order. 1370 // Thus, Src1Value may be a ConstantInt, but we're missing it. 1371 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) { 1372 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 || 1373 SrcVT == MVT::i1) { 1374 const APInt &CIVal = ConstInt->getValue(); 1375 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue(); 1376 if (Imm < 0) { 1377 isNegativeImm = true; 1378 Imm = -Imm; 1379 } 1380 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : 1381 (ARM_AM::getSOImmVal(Imm) != -1); 1382 } 1383 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) { 1384 if (SrcVT == MVT::f32 || SrcVT == MVT::f64) 1385 if (ConstFP->isZero() && !ConstFP->isNegative()) 1386 UseImm = true; 1387 } 1388 1389 unsigned CmpOpc; 1390 bool isICmp = true; 1391 bool needsExt = false; 1392 switch (SrcVT.getSimpleVT().SimpleTy) { 1393 default: return false; 1394 // TODO: Verify compares. 1395 case MVT::f32: 1396 isICmp = false; 1397 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES; 1398 break; 1399 case MVT::f64: 1400 isICmp = false; 1401 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED; 1402 break; 1403 case MVT::i1: 1404 case MVT::i8: 1405 case MVT::i16: 1406 needsExt = true; 1407 // Intentional fall-through. 1408 case MVT::i32: 1409 if (isThumb2) { 1410 if (!UseImm) 1411 CmpOpc = ARM::t2CMPrr; 1412 else 1413 CmpOpc = isNegativeImm ? ARM::t2CMNzri : ARM::t2CMPri; 1414 } else { 1415 if (!UseImm) 1416 CmpOpc = ARM::CMPrr; 1417 else 1418 CmpOpc = isNegativeImm ? ARM::CMNzri : ARM::CMPri; 1419 } 1420 break; 1421 } 1422 1423 unsigned SrcReg1 = getRegForValue(Src1Value); 1424 if (SrcReg1 == 0) return false; 1425 1426 unsigned SrcReg2 = 0; 1427 if (!UseImm) { 1428 SrcReg2 = getRegForValue(Src2Value); 1429 if (SrcReg2 == 0) return false; 1430 } 1431 1432 // We have i1, i8, or i16, we need to either zero extend or sign extend. 1433 if (needsExt) { 1434 unsigned ResultReg; 1435 ResultReg = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt); 1436 if (ResultReg == 0) return false; 1437 SrcReg1 = ResultReg; 1438 if (!UseImm) { 1439 ResultReg = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt); 1440 if (ResultReg == 0) return false; 1441 SrcReg2 = ResultReg; 1442 } 1443 } 1444 1445 if (!UseImm) { 1446 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1447 TII.get(CmpOpc)) 1448 .addReg(SrcReg1).addReg(SrcReg2)); 1449 } else { 1450 MachineInstrBuilder MIB; 1451 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) 1452 .addReg(SrcReg1); 1453 1454 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0. 1455 if (isICmp) 1456 MIB.addImm(Imm); 1457 AddOptionalDefs(MIB); 1458 } 1459 1460 // For floating point we need to move the result to a comparison register 1461 // that we can then use for branches. 1462 if (Ty->isFloatTy() || Ty->isDoubleTy()) 1463 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1464 TII.get(ARM::FMSTAT))); 1465 return true; 1466 } 1467 1468 bool ARMFastISel::SelectCmp(const Instruction *I) { 1469 const CmpInst *CI = cast<CmpInst>(I); 1470 Type *Ty = CI->getOperand(0)->getType(); 1471 1472 // Get the compare predicate. 1473 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate()); 1474 1475 // We may not handle every CC for now. 1476 if (ARMPred == ARMCC::AL) return false; 1477 1478 // Emit the compare. 1479 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned())) 1480 return false; 1481 1482 // Now set a register based on the comparison. Explicitly set the predicates 1483 // here. 1484 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi; 1485 TargetRegisterClass *RC = isThumb2 ? ARM::rGPRRegisterClass 1486 : ARM::GPRRegisterClass; 1487 unsigned DestReg = createResultReg(RC); 1488 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0); 1489 unsigned ZeroReg = TargetMaterializeConstant(Zero); 1490 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy()); 1491 unsigned CondReg = isFloat ? ARM::FPSCR : ARM::CPSR; 1492 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg) 1493 .addReg(ZeroReg).addImm(1) 1494 .addImm(ARMPred).addReg(CondReg); 1495 1496 UpdateValueMap(I, DestReg); 1497 return true; 1498 } 1499 1500 bool ARMFastISel::SelectFPExt(const Instruction *I) { 1501 // Make sure we have VFP and that we're extending float to double. 1502 if (!Subtarget->hasVFP2()) return false; 1503 1504 Value *V = I->getOperand(0); 1505 if (!I->getType()->isDoubleTy() || 1506 !V->getType()->isFloatTy()) return false; 1507 1508 unsigned Op = getRegForValue(V); 1509 if (Op == 0) return false; 1510 1511 unsigned Result = createResultReg(ARM::DPRRegisterClass); 1512 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1513 TII.get(ARM::VCVTDS), Result) 1514 .addReg(Op)); 1515 UpdateValueMap(I, Result); 1516 return true; 1517 } 1518 1519 bool ARMFastISel::SelectFPTrunc(const Instruction *I) { 1520 // Make sure we have VFP and that we're truncating double to float. 1521 if (!Subtarget->hasVFP2()) return false; 1522 1523 Value *V = I->getOperand(0); 1524 if (!(I->getType()->isFloatTy() && 1525 V->getType()->isDoubleTy())) return false; 1526 1527 unsigned Op = getRegForValue(V); 1528 if (Op == 0) return false; 1529 1530 unsigned Result = createResultReg(ARM::SPRRegisterClass); 1531 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1532 TII.get(ARM::VCVTSD), Result) 1533 .addReg(Op)); 1534 UpdateValueMap(I, Result); 1535 return true; 1536 } 1537 1538 bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) { 1539 // Make sure we have VFP. 1540 if (!Subtarget->hasVFP2()) return false; 1541 1542 MVT DstVT; 1543 Type *Ty = I->getType(); 1544 if (!isTypeLegal(Ty, DstVT)) 1545 return false; 1546 1547 Value *Src = I->getOperand(0); 1548 EVT SrcVT = TLI.getValueType(Src->getType(), true); 1549 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8) 1550 return false; 1551 1552 unsigned SrcReg = getRegForValue(Src); 1553 if (SrcReg == 0) return false; 1554 1555 // Handle sign-extension. 1556 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) { 1557 EVT DestVT = MVT::i32; 1558 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, 1559 /*isZExt*/!isSigned); 1560 if (ResultReg == 0) return false; 1561 SrcReg = ResultReg; 1562 } 1563 1564 // The conversion routine works on fp-reg to fp-reg and the operand above 1565 // was an integer, move it to the fp registers if possible. 1566 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg); 1567 if (FP == 0) return false; 1568 1569 unsigned Opc; 1570 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS; 1571 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD; 1572 else return false; 1573 1574 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT)); 1575 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), 1576 ResultReg) 1577 .addReg(FP)); 1578 UpdateValueMap(I, ResultReg); 1579 return true; 1580 } 1581 1582 bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) { 1583 // Make sure we have VFP. 1584 if (!Subtarget->hasVFP2()) return false; 1585 1586 MVT DstVT; 1587 Type *RetTy = I->getType(); 1588 if (!isTypeLegal(RetTy, DstVT)) 1589 return false; 1590 1591 unsigned Op = getRegForValue(I->getOperand(0)); 1592 if (Op == 0) return false; 1593 1594 unsigned Opc; 1595 Type *OpTy = I->getOperand(0)->getType(); 1596 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS; 1597 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD; 1598 else return false; 1599 1600 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg. 1601 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32)); 1602 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), 1603 ResultReg) 1604 .addReg(Op)); 1605 1606 // This result needs to be in an integer register, but the conversion only 1607 // takes place in fp-regs. 1608 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg); 1609 if (IntReg == 0) return false; 1610 1611 UpdateValueMap(I, IntReg); 1612 return true; 1613 } 1614 1615 bool ARMFastISel::SelectSelect(const Instruction *I) { 1616 MVT VT; 1617 if (!isTypeLegal(I->getType(), VT)) 1618 return false; 1619 1620 // Things need to be register sized for register moves. 1621 if (VT != MVT::i32) return false; 1622 const TargetRegisterClass *RC = TLI.getRegClassFor(VT); 1623 1624 unsigned CondReg = getRegForValue(I->getOperand(0)); 1625 if (CondReg == 0) return false; 1626 unsigned Op1Reg = getRegForValue(I->getOperand(1)); 1627 if (Op1Reg == 0) return false; 1628 1629 // Check to see if we can use an immediate in the conditional move. 1630 int Imm = 0; 1631 bool UseImm = false; 1632 bool isNegativeImm = false; 1633 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) { 1634 assert (VT == MVT::i32 && "Expecting an i32."); 1635 Imm = (int)ConstInt->getValue().getZExtValue(); 1636 if (Imm < 0) { 1637 isNegativeImm = true; 1638 Imm = ~Imm; 1639 } 1640 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : 1641 (ARM_AM::getSOImmVal(Imm) != -1); 1642 } 1643 1644 unsigned Op2Reg = 0; 1645 if (!UseImm) { 1646 Op2Reg = getRegForValue(I->getOperand(2)); 1647 if (Op2Reg == 0) return false; 1648 } 1649 1650 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri; 1651 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) 1652 .addReg(CondReg).addImm(0)); 1653 1654 unsigned MovCCOpc; 1655 if (!UseImm) { 1656 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr; 1657 } else { 1658 if (!isNegativeImm) { 1659 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi; 1660 } else { 1661 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi; 1662 } 1663 } 1664 unsigned ResultReg = createResultReg(RC); 1665 if (!UseImm) 1666 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg) 1667 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR); 1668 else 1669 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg) 1670 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR); 1671 UpdateValueMap(I, ResultReg); 1672 return true; 1673 } 1674 1675 bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) { 1676 MVT VT; 1677 Type *Ty = I->getType(); 1678 if (!isTypeLegal(Ty, VT)) 1679 return false; 1680 1681 // If we have integer div support we should have selected this automagically. 1682 // In case we have a real miss go ahead and return false and we'll pick 1683 // it up later. 1684 if (Subtarget->hasDivide()) return false; 1685 1686 // Otherwise emit a libcall. 1687 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 1688 if (VT == MVT::i8) 1689 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8; 1690 else if (VT == MVT::i16) 1691 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16; 1692 else if (VT == MVT::i32) 1693 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32; 1694 else if (VT == MVT::i64) 1695 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64; 1696 else if (VT == MVT::i128) 1697 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128; 1698 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!"); 1699 1700 return ARMEmitLibcall(I, LC); 1701 } 1702 1703 bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) { 1704 MVT VT; 1705 Type *Ty = I->getType(); 1706 if (!isTypeLegal(Ty, VT)) 1707 return false; 1708 1709 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 1710 if (VT == MVT::i8) 1711 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8; 1712 else if (VT == MVT::i16) 1713 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16; 1714 else if (VT == MVT::i32) 1715 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32; 1716 else if (VT == MVT::i64) 1717 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64; 1718 else if (VT == MVT::i128) 1719 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128; 1720 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!"); 1721 1722 return ARMEmitLibcall(I, LC); 1723 } 1724 1725 bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) { 1726 EVT VT = TLI.getValueType(I->getType(), true); 1727 1728 // We can get here in the case when we want to use NEON for our fp 1729 // operations, but can't figure out how to. Just use the vfp instructions 1730 // if we have them. 1731 // FIXME: It'd be nice to use NEON instructions. 1732 Type *Ty = I->getType(); 1733 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy()); 1734 if (isFloat && !Subtarget->hasVFP2()) 1735 return false; 1736 1737 unsigned Opc; 1738 bool is64bit = VT == MVT::f64 || VT == MVT::i64; 1739 switch (ISDOpcode) { 1740 default: return false; 1741 case ISD::FADD: 1742 Opc = is64bit ? ARM::VADDD : ARM::VADDS; 1743 break; 1744 case ISD::FSUB: 1745 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS; 1746 break; 1747 case ISD::FMUL: 1748 Opc = is64bit ? ARM::VMULD : ARM::VMULS; 1749 break; 1750 } 1751 unsigned Op1 = getRegForValue(I->getOperand(0)); 1752 if (Op1 == 0) return false; 1753 1754 unsigned Op2 = getRegForValue(I->getOperand(1)); 1755 if (Op2 == 0) return false; 1756 1757 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT)); 1758 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1759 TII.get(Opc), ResultReg) 1760 .addReg(Op1).addReg(Op2)); 1761 UpdateValueMap(I, ResultReg); 1762 return true; 1763 } 1764 1765 // Call Handling Code 1766 1767 // This is largely taken directly from CCAssignFnForNode - we don't support 1768 // varargs in FastISel so that part has been removed. 1769 // TODO: We may not support all of this. 1770 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) { 1771 switch (CC) { 1772 default: 1773 llvm_unreachable("Unsupported calling convention"); 1774 case CallingConv::Fast: 1775 // Ignore fastcc. Silence compiler warnings. 1776 (void)RetFastCC_ARM_APCS; 1777 (void)FastCC_ARM_APCS; 1778 // Fallthrough 1779 case CallingConv::C: 1780 // Use target triple & subtarget features to do actual dispatch. 1781 if (Subtarget->isAAPCS_ABI()) { 1782 if (Subtarget->hasVFP2() && 1783 TM.Options.FloatABIType == FloatABI::Hard) 1784 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); 1785 else 1786 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); 1787 } else 1788 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); 1789 case CallingConv::ARM_AAPCS_VFP: 1790 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); 1791 case CallingConv::ARM_AAPCS: 1792 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); 1793 case CallingConv::ARM_APCS: 1794 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); 1795 } 1796 } 1797 1798 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args, 1799 SmallVectorImpl<unsigned> &ArgRegs, 1800 SmallVectorImpl<MVT> &ArgVTs, 1801 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, 1802 SmallVectorImpl<unsigned> &RegArgs, 1803 CallingConv::ID CC, 1804 unsigned &NumBytes) { 1805 SmallVector<CCValAssign, 16> ArgLocs; 1806 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context); 1807 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false)); 1808 1809 // Get a count of how many bytes are to be pushed on the stack. 1810 NumBytes = CCInfo.getNextStackOffset(); 1811 1812 // Issue CALLSEQ_START 1813 unsigned AdjStackDown = TII.getCallFrameSetupOpcode(); 1814 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1815 TII.get(AdjStackDown)) 1816 .addImm(NumBytes)); 1817 1818 // Process the args. 1819 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1820 CCValAssign &VA = ArgLocs[i]; 1821 unsigned Arg = ArgRegs[VA.getValNo()]; 1822 MVT ArgVT = ArgVTs[VA.getValNo()]; 1823 1824 // We don't handle NEON/vector parameters yet. 1825 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64) 1826 return false; 1827 1828 // Handle arg promotion, etc. 1829 switch (VA.getLocInfo()) { 1830 case CCValAssign::Full: break; 1831 case CCValAssign::SExt: { 1832 MVT DestVT = VA.getLocVT(); 1833 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT, 1834 /*isZExt*/false); 1835 assert (ResultReg != 0 && "Failed to emit a sext"); 1836 Arg = ResultReg; 1837 ArgVT = DestVT; 1838 break; 1839 } 1840 case CCValAssign::AExt: 1841 // Intentional fall-through. Handle AExt and ZExt. 1842 case CCValAssign::ZExt: { 1843 MVT DestVT = VA.getLocVT(); 1844 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT, 1845 /*isZExt*/true); 1846 assert (ResultReg != 0 && "Failed to emit a sext"); 1847 Arg = ResultReg; 1848 ArgVT = DestVT; 1849 break; 1850 } 1851 case CCValAssign::BCvt: { 1852 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg, 1853 /*TODO: Kill=*/false); 1854 assert(BC != 0 && "Failed to emit a bitcast!"); 1855 Arg = BC; 1856 ArgVT = VA.getLocVT(); 1857 break; 1858 } 1859 default: llvm_unreachable("Unknown arg promotion!"); 1860 } 1861 1862 // Now copy/store arg to correct locations. 1863 if (VA.isRegLoc() && !VA.needsCustom()) { 1864 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), 1865 VA.getLocReg()) 1866 .addReg(Arg); 1867 RegArgs.push_back(VA.getLocReg()); 1868 } else if (VA.needsCustom()) { 1869 // TODO: We need custom lowering for vector (v2f64) args. 1870 if (VA.getLocVT() != MVT::f64) return false; 1871 1872 CCValAssign &NextVA = ArgLocs[++i]; 1873 1874 // TODO: Only handle register args for now. 1875 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false; 1876 1877 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1878 TII.get(ARM::VMOVRRD), VA.getLocReg()) 1879 .addReg(NextVA.getLocReg(), RegState::Define) 1880 .addReg(Arg)); 1881 RegArgs.push_back(VA.getLocReg()); 1882 RegArgs.push_back(NextVA.getLocReg()); 1883 } else { 1884 assert(VA.isMemLoc()); 1885 // Need to store on the stack. 1886 Address Addr; 1887 Addr.BaseType = Address::RegBase; 1888 Addr.Base.Reg = ARM::SP; 1889 Addr.Offset = VA.getLocMemOffset(); 1890 1891 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false; 1892 } 1893 } 1894 return true; 1895 } 1896 1897 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, 1898 const Instruction *I, CallingConv::ID CC, 1899 unsigned &NumBytes) { 1900 // Issue CALLSEQ_END 1901 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode(); 1902 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1903 TII.get(AdjStackUp)) 1904 .addImm(NumBytes).addImm(0)); 1905 1906 // Now the return value. 1907 if (RetVT != MVT::isVoid) { 1908 SmallVector<CCValAssign, 16> RVLocs; 1909 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context); 1910 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true)); 1911 1912 // Copy all of the result registers out of their specified physreg. 1913 if (RVLocs.size() == 2 && RetVT == MVT::f64) { 1914 // For this move we copy into two registers and then move into the 1915 // double fp reg we want. 1916 EVT DestVT = RVLocs[0].getValVT(); 1917 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT); 1918 unsigned ResultReg = createResultReg(DstRC); 1919 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 1920 TII.get(ARM::VMOVDRR), ResultReg) 1921 .addReg(RVLocs[0].getLocReg()) 1922 .addReg(RVLocs[1].getLocReg())); 1923 1924 UsedRegs.push_back(RVLocs[0].getLocReg()); 1925 UsedRegs.push_back(RVLocs[1].getLocReg()); 1926 1927 // Finally update the result. 1928 UpdateValueMap(I, ResultReg); 1929 } else { 1930 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!"); 1931 EVT CopyVT = RVLocs[0].getValVT(); 1932 1933 // Special handling for extended integers. 1934 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16) 1935 CopyVT = MVT::i32; 1936 1937 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT); 1938 1939 unsigned ResultReg = createResultReg(DstRC); 1940 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), 1941 ResultReg).addReg(RVLocs[0].getLocReg()); 1942 UsedRegs.push_back(RVLocs[0].getLocReg()); 1943 1944 // Finally update the result. 1945 UpdateValueMap(I, ResultReg); 1946 } 1947 } 1948 1949 return true; 1950 } 1951 1952 bool ARMFastISel::SelectRet(const Instruction *I) { 1953 const ReturnInst *Ret = cast<ReturnInst>(I); 1954 const Function &F = *I->getParent()->getParent(); 1955 1956 if (!FuncInfo.CanLowerReturn) 1957 return false; 1958 1959 if (F.isVarArg()) 1960 return false; 1961 1962 CallingConv::ID CC = F.getCallingConv(); 1963 if (Ret->getNumOperands() > 0) { 1964 SmallVector<ISD::OutputArg, 4> Outs; 1965 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(), 1966 Outs, TLI); 1967 1968 // Analyze operands of the call, assigning locations to each operand. 1969 SmallVector<CCValAssign, 16> ValLocs; 1970 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext()); 1971 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */)); 1972 1973 const Value *RV = Ret->getOperand(0); 1974 unsigned Reg = getRegForValue(RV); 1975 if (Reg == 0) 1976 return false; 1977 1978 // Only handle a single return value for now. 1979 if (ValLocs.size() != 1) 1980 return false; 1981 1982 CCValAssign &VA = ValLocs[0]; 1983 1984 // Don't bother handling odd stuff for now. 1985 if (VA.getLocInfo() != CCValAssign::Full) 1986 return false; 1987 // Only handle register returns for now. 1988 if (!VA.isRegLoc()) 1989 return false; 1990 1991 unsigned SrcReg = Reg + VA.getValNo(); 1992 EVT RVVT = TLI.getValueType(RV->getType()); 1993 EVT DestVT = VA.getValVT(); 1994 // Special handling for extended integers. 1995 if (RVVT != DestVT) { 1996 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16) 1997 return false; 1998 1999 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt()) 2000 return false; 2001 2002 assert(DestVT == MVT::i32 && "ARM should always ext to i32"); 2003 2004 bool isZExt = Outs[0].Flags.isZExt(); 2005 unsigned ResultReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, isZExt); 2006 if (ResultReg == 0) return false; 2007 SrcReg = ResultReg; 2008 } 2009 2010 // Make the copy. 2011 unsigned DstReg = VA.getLocReg(); 2012 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg); 2013 // Avoid a cross-class copy. This is very unlikely. 2014 if (!SrcRC->contains(DstReg)) 2015 return false; 2016 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), 2017 DstReg).addReg(SrcReg); 2018 2019 // Mark the register as live out of the function. 2020 MRI.addLiveOut(VA.getLocReg()); 2021 } 2022 2023 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET; 2024 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 2025 TII.get(RetOpc))); 2026 return true; 2027 } 2028 2029 unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) { 2030 2031 // iOS needs the r9 versions of the opcodes. 2032 bool isiOS = Subtarget->isTargetIOS(); 2033 if (isThumb2) { 2034 return isiOS ? ARM::tBLr9 : ARM::tBL; 2035 } else { 2036 return isiOS ? ARM::BLr9 : ARM::BL; 2037 } 2038 } 2039 2040 // A quick function that will emit a call for a named libcall in F with the 2041 // vector of passed arguments for the Instruction in I. We can assume that we 2042 // can emit a call for any libcall we can produce. This is an abridged version 2043 // of the full call infrastructure since we won't need to worry about things 2044 // like computed function pointers or strange arguments at call sites. 2045 // TODO: Try to unify this and the normal call bits for ARM, then try to unify 2046 // with X86. 2047 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) { 2048 CallingConv::ID CC = TLI.getLibcallCallingConv(Call); 2049 2050 // Handle *simple* calls for now. 2051 Type *RetTy = I->getType(); 2052 MVT RetVT; 2053 if (RetTy->isVoidTy()) 2054 RetVT = MVT::isVoid; 2055 else if (!isTypeLegal(RetTy, RetVT)) 2056 return false; 2057 2058 // TODO: For now if we have long calls specified we don't handle the call. 2059 if (EnableARMLongCalls) return false; 2060 2061 // Set up the argument vectors. 2062 SmallVector<Value*, 8> Args; 2063 SmallVector<unsigned, 8> ArgRegs; 2064 SmallVector<MVT, 8> ArgVTs; 2065 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; 2066 Args.reserve(I->getNumOperands()); 2067 ArgRegs.reserve(I->getNumOperands()); 2068 ArgVTs.reserve(I->getNumOperands()); 2069 ArgFlags.reserve(I->getNumOperands()); 2070 for (unsigned i = 0; i < I->getNumOperands(); ++i) { 2071 Value *Op = I->getOperand(i); 2072 unsigned Arg = getRegForValue(Op); 2073 if (Arg == 0) return false; 2074 2075 Type *ArgTy = Op->getType(); 2076 MVT ArgVT; 2077 if (!isTypeLegal(ArgTy, ArgVT)) return false; 2078 2079 ISD::ArgFlagsTy Flags; 2080 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy); 2081 Flags.setOrigAlign(OriginalAlignment); 2082 2083 Args.push_back(Op); 2084 ArgRegs.push_back(Arg); 2085 ArgVTs.push_back(ArgVT); 2086 ArgFlags.push_back(Flags); 2087 } 2088 2089 // Handle the arguments now that we've gotten them. 2090 SmallVector<unsigned, 4> RegArgs; 2091 unsigned NumBytes; 2092 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes)) 2093 return false; 2094 2095 // Issue the call, BLr9 for iOS, BL otherwise. 2096 // TODO: Turn this into the table of arm call ops. 2097 MachineInstrBuilder MIB; 2098 unsigned CallOpc = ARMSelectCallOp(NULL); 2099 if(isThumb2) 2100 // Explicitly adding the predicate here. 2101 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 2102 TII.get(CallOpc))) 2103 .addExternalSymbol(TLI.getLibcallName(Call)); 2104 else 2105 // Explicitly adding the predicate here. 2106 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 2107 TII.get(CallOpc)) 2108 .addExternalSymbol(TLI.getLibcallName(Call))); 2109 2110 // Add implicit physical register uses to the call. 2111 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) 2112 MIB.addReg(RegArgs[i]); 2113 2114 // Finish off the call including any return values. 2115 SmallVector<unsigned, 4> UsedRegs; 2116 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false; 2117 2118 // Set all unused physreg defs as dead. 2119 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); 2120 2121 return true; 2122 } 2123 2124 bool ARMFastISel::SelectCall(const Instruction *I, 2125 const char *IntrMemName = 0) { 2126 const CallInst *CI = cast<CallInst>(I); 2127 const Value *Callee = CI->getCalledValue(); 2128 2129 // Can't handle inline asm. 2130 if (isa<InlineAsm>(Callee)) return false; 2131 2132 // Only handle global variable Callees. 2133 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee); 2134 if (!GV) 2135 return false; 2136 2137 // Check the calling convention. 2138 ImmutableCallSite CS(CI); 2139 CallingConv::ID CC = CS.getCallingConv(); 2140 2141 // TODO: Avoid some calling conventions? 2142 2143 // Let SDISel handle vararg functions. 2144 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); 2145 FunctionType *FTy = cast<FunctionType>(PT->getElementType()); 2146 if (FTy->isVarArg()) 2147 return false; 2148 2149 // Handle *simple* calls for now. 2150 Type *RetTy = I->getType(); 2151 MVT RetVT; 2152 if (RetTy->isVoidTy()) 2153 RetVT = MVT::isVoid; 2154 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 && 2155 RetVT != MVT::i8 && RetVT != MVT::i1) 2156 return false; 2157 2158 // TODO: For now if we have long calls specified we don't handle the call. 2159 if (EnableARMLongCalls) return false; 2160 2161 // Set up the argument vectors. 2162 SmallVector<Value*, 8> Args; 2163 SmallVector<unsigned, 8> ArgRegs; 2164 SmallVector<MVT, 8> ArgVTs; 2165 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; 2166 Args.reserve(CS.arg_size()); 2167 ArgRegs.reserve(CS.arg_size()); 2168 ArgVTs.reserve(CS.arg_size()); 2169 ArgFlags.reserve(CS.arg_size()); 2170 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); 2171 i != e; ++i) { 2172 // If we're lowering a memory intrinsic instead of a regular call, skip the 2173 // last two arguments, which shouldn't be passed to the underlying function. 2174 if (IntrMemName && e-i <= 2) 2175 break; 2176 2177 ISD::ArgFlagsTy Flags; 2178 unsigned AttrInd = i - CS.arg_begin() + 1; 2179 if (CS.paramHasAttr(AttrInd, Attribute::SExt)) 2180 Flags.setSExt(); 2181 if (CS.paramHasAttr(AttrInd, Attribute::ZExt)) 2182 Flags.setZExt(); 2183 2184 // FIXME: Only handle *easy* calls for now. 2185 if (CS.paramHasAttr(AttrInd, Attribute::InReg) || 2186 CS.paramHasAttr(AttrInd, Attribute::StructRet) || 2187 CS.paramHasAttr(AttrInd, Attribute::Nest) || 2188 CS.paramHasAttr(AttrInd, Attribute::ByVal)) 2189 return false; 2190 2191 Type *ArgTy = (*i)->getType(); 2192 MVT ArgVT; 2193 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 && 2194 ArgVT != MVT::i1) 2195 return false; 2196 2197 unsigned Arg = getRegForValue(*i); 2198 if (Arg == 0) 2199 return false; 2200 2201 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy); 2202 Flags.setOrigAlign(OriginalAlignment); 2203 2204 Args.push_back(*i); 2205 ArgRegs.push_back(Arg); 2206 ArgVTs.push_back(ArgVT); 2207 ArgFlags.push_back(Flags); 2208 } 2209 2210 // Handle the arguments now that we've gotten them. 2211 SmallVector<unsigned, 4> RegArgs; 2212 unsigned NumBytes; 2213 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes)) 2214 return false; 2215 2216 // Issue the call, BLr9 for iOS, BL otherwise. 2217 // TODO: Turn this into the table of arm call ops. 2218 MachineInstrBuilder MIB; 2219 unsigned CallOpc = ARMSelectCallOp(GV); 2220 // Explicitly adding the predicate here. 2221 if(isThumb2) { 2222 // Explicitly adding the predicate here. 2223 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 2224 TII.get(CallOpc))); 2225 if (!IntrMemName) 2226 MIB.addGlobalAddress(GV, 0, 0); 2227 else 2228 MIB.addExternalSymbol(IntrMemName, 0); 2229 } else { 2230 if (!IntrMemName) 2231 // Explicitly adding the predicate here. 2232 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 2233 TII.get(CallOpc)) 2234 .addGlobalAddress(GV, 0, 0)); 2235 else 2236 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, 2237 TII.get(CallOpc)) 2238 .addExternalSymbol(IntrMemName, 0)); 2239 } 2240 2241 // Add implicit physical register uses to the call. 2242 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) 2243 MIB.addReg(RegArgs[i]); 2244 2245 // Finish off the call including any return values. 2246 SmallVector<unsigned, 4> UsedRegs; 2247 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false; 2248 2249 // Set all unused physreg defs as dead. 2250 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); 2251 2252 return true; 2253 } 2254 2255 bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) { 2256 return Len <= 16; 2257 } 2258 2259 bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len) { 2260 // Make sure we don't bloat code by inlining very large memcpy's. 2261 if (!ARMIsMemCpySmall(Len)) 2262 return false; 2263 2264 // We don't care about alignment here since we just emit integer accesses. 2265 while (Len) { 2266 MVT VT; 2267 if (Len >= 4) 2268 VT = MVT::i32; 2269 else if (Len >= 2) 2270 VT = MVT::i16; 2271 else { 2272 assert(Len == 1); 2273 VT = MVT::i8; 2274 } 2275 2276 bool RV; 2277 unsigned ResultReg; 2278 RV = ARMEmitLoad(VT, ResultReg, Src); 2279 assert (RV == true && "Should be able to handle this load."); 2280 RV = ARMEmitStore(VT, ResultReg, Dest); 2281 assert (RV == true && "Should be able to handle this store."); 2282 2283 unsigned Size = VT.getSizeInBits()/8; 2284 Len -= Size; 2285 Dest.Offset += Size; 2286 Src.Offset += Size; 2287 } 2288 2289 return true; 2290 } 2291 2292 bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) { 2293 // FIXME: Handle more intrinsics. 2294 switch (I.getIntrinsicID()) { 2295 default: return false; 2296 case Intrinsic::memcpy: 2297 case Intrinsic::memmove: { 2298 const MemTransferInst &MTI = cast<MemTransferInst>(I); 2299 // Don't handle volatile. 2300 if (MTI.isVolatile()) 2301 return false; 2302 2303 // Disable inlining for memmove before calls to ComputeAddress. Otherwise, 2304 // we would emit dead code because we don't currently handle memmoves. 2305 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy); 2306 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) { 2307 // Small memcpy's are common enough that we want to do them without a call 2308 // if possible. 2309 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue(); 2310 if (ARMIsMemCpySmall(Len)) { 2311 Address Dest, Src; 2312 if (!ARMComputeAddress(MTI.getRawDest(), Dest) || 2313 !ARMComputeAddress(MTI.getRawSource(), Src)) 2314 return false; 2315 if (ARMTryEmitSmallMemCpy(Dest, Src, Len)) 2316 return true; 2317 } 2318 } 2319 2320 if (!MTI.getLength()->getType()->isIntegerTy(32)) 2321 return false; 2322 2323 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255) 2324 return false; 2325 2326 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove"; 2327 return SelectCall(&I, IntrMemName); 2328 } 2329 case Intrinsic::memset: { 2330 const MemSetInst &MSI = cast<MemSetInst>(I); 2331 // Don't handle volatile. 2332 if (MSI.isVolatile()) 2333 return false; 2334 2335 if (!MSI.getLength()->getType()->isIntegerTy(32)) 2336 return false; 2337 2338 if (MSI.getDestAddressSpace() > 255) 2339 return false; 2340 2341 return SelectCall(&I, "memset"); 2342 } 2343 } 2344 } 2345 2346 bool ARMFastISel::SelectTrunc(const Instruction *I) { 2347 // The high bits for a type smaller than the register size are assumed to be 2348 // undefined. 2349 Value *Op = I->getOperand(0); 2350 2351 EVT SrcVT, DestVT; 2352 SrcVT = TLI.getValueType(Op->getType(), true); 2353 DestVT = TLI.getValueType(I->getType(), true); 2354 2355 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8) 2356 return false; 2357 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1) 2358 return false; 2359 2360 unsigned SrcReg = getRegForValue(Op); 2361 if (!SrcReg) return false; 2362 2363 // Because the high bits are undefined, a truncate doesn't generate 2364 // any code. 2365 UpdateValueMap(I, SrcReg); 2366 return true; 2367 } 2368 2369 unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, 2370 bool isZExt) { 2371 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8) 2372 return 0; 2373 2374 unsigned Opc; 2375 bool isBoolZext = false; 2376 if (!SrcVT.isSimple()) return 0; 2377 switch (SrcVT.getSimpleVT().SimpleTy) { 2378 default: return 0; 2379 case MVT::i16: 2380 if (!Subtarget->hasV6Ops()) return 0; 2381 if (isZExt) 2382 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH; 2383 else 2384 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH; 2385 break; 2386 case MVT::i8: 2387 if (!Subtarget->hasV6Ops()) return 0; 2388 if (isZExt) 2389 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB; 2390 else 2391 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB; 2392 break; 2393 case MVT::i1: 2394 if (isZExt) { 2395 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri; 2396 isBoolZext = true; 2397 break; 2398 } 2399 return 0; 2400 } 2401 2402 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32)); 2403 MachineInstrBuilder MIB; 2404 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg) 2405 .addReg(SrcReg); 2406 if (isBoolZext) 2407 MIB.addImm(1); 2408 else 2409 MIB.addImm(0); 2410 AddOptionalDefs(MIB); 2411 return ResultReg; 2412 } 2413 2414 bool ARMFastISel::SelectIntExt(const Instruction *I) { 2415 // On ARM, in general, integer casts don't involve legal types; this code 2416 // handles promotable integers. 2417 Type *DestTy = I->getType(); 2418 Value *Src = I->getOperand(0); 2419 Type *SrcTy = Src->getType(); 2420 2421 EVT SrcVT, DestVT; 2422 SrcVT = TLI.getValueType(SrcTy, true); 2423 DestVT = TLI.getValueType(DestTy, true); 2424 2425 bool isZExt = isa<ZExtInst>(I); 2426 unsigned SrcReg = getRegForValue(Src); 2427 if (!SrcReg) return false; 2428 2429 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt); 2430 if (ResultReg == 0) return false; 2431 UpdateValueMap(I, ResultReg); 2432 return true; 2433 } 2434 2435 // TODO: SoftFP support. 2436 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) { 2437 2438 switch (I->getOpcode()) { 2439 case Instruction::Load: 2440 return SelectLoad(I); 2441 case Instruction::Store: 2442 return SelectStore(I); 2443 case Instruction::Br: 2444 return SelectBranch(I); 2445 case Instruction::ICmp: 2446 case Instruction::FCmp: 2447 return SelectCmp(I); 2448 case Instruction::FPExt: 2449 return SelectFPExt(I); 2450 case Instruction::FPTrunc: 2451 return SelectFPTrunc(I); 2452 case Instruction::SIToFP: 2453 return SelectIToFP(I, /*isSigned*/ true); 2454 case Instruction::UIToFP: 2455 return SelectIToFP(I, /*isSigned*/ false); 2456 case Instruction::FPToSI: 2457 return SelectFPToI(I, /*isSigned*/ true); 2458 case Instruction::FPToUI: 2459 return SelectFPToI(I, /*isSigned*/ false); 2460 case Instruction::FAdd: 2461 return SelectBinaryOp(I, ISD::FADD); 2462 case Instruction::FSub: 2463 return SelectBinaryOp(I, ISD::FSUB); 2464 case Instruction::FMul: 2465 return SelectBinaryOp(I, ISD::FMUL); 2466 case Instruction::SDiv: 2467 return SelectDiv(I, /*isSigned*/ true); 2468 case Instruction::UDiv: 2469 return SelectDiv(I, /*isSigned*/ false); 2470 case Instruction::SRem: 2471 return SelectRem(I, /*isSigned*/ true); 2472 case Instruction::URem: 2473 return SelectRem(I, /*isSigned*/ false); 2474 case Instruction::Call: 2475 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) 2476 return SelectIntrinsicCall(*II); 2477 return SelectCall(I); 2478 case Instruction::Select: 2479 return SelectSelect(I); 2480 case Instruction::Ret: 2481 return SelectRet(I); 2482 case Instruction::Trunc: 2483 return SelectTrunc(I); 2484 case Instruction::ZExt: 2485 case Instruction::SExt: 2486 return SelectIntExt(I); 2487 default: break; 2488 } 2489 return false; 2490 } 2491 2492 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that 2493 /// vreg is being provided by the specified load instruction. If possible, 2494 /// try to fold the load as an operand to the instruction, returning true if 2495 /// successful. 2496 bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo, 2497 const LoadInst *LI) { 2498 // Verify we have a legal type before going any further. 2499 MVT VT; 2500 if (!isLoadTypeLegal(LI->getType(), VT)) 2501 return false; 2502 2503 // Combine load followed by zero- or sign-extend. 2504 // ldrb r1, [r0] ldrb r1, [r0] 2505 // uxtb r2, r1 => 2506 // mov r3, r2 mov r3, r1 2507 bool isZExt = true; 2508 switch(MI->getOpcode()) { 2509 default: return false; 2510 case ARM::SXTH: 2511 case ARM::t2SXTH: 2512 isZExt = false; 2513 case ARM::UXTH: 2514 case ARM::t2UXTH: 2515 if (VT != MVT::i16) 2516 return false; 2517 break; 2518 case ARM::SXTB: 2519 case ARM::t2SXTB: 2520 isZExt = false; 2521 case ARM::UXTB: 2522 case ARM::t2UXTB: 2523 if (VT != MVT::i8) 2524 return false; 2525 break; 2526 } 2527 // See if we can handle this address. 2528 Address Addr; 2529 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false; 2530 2531 unsigned ResultReg = MI->getOperand(0).getReg(); 2532 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false)) 2533 return false; 2534 MI->eraseFromParent(); 2535 return true; 2536 } 2537 2538 namespace llvm { 2539 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) { 2540 // Completely untested on non-iOS. 2541 const TargetMachine &TM = funcInfo.MF->getTarget(); 2542 2543 // Darwin and thumb1 only for now. 2544 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>(); 2545 if (Subtarget->isTargetIOS() && !Subtarget->isThumb1Only() && 2546 !DisableARMFastISel) 2547 return new ARMFastISel(funcInfo); 2548 return 0; 2549 } 2550 } 2551