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