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