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