1 //===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ARM-specific support for the FastISel class. Some
11 // of the target-specific code is generated by tablegen in the file
12 // ARMGenFastISel.inc, which is #included here.
14 //===----------------------------------------------------------------------===//
17 #include "ARMBaseRegisterInfo.h"
18 #include "ARMCallingConv.h"
19 #include "ARMConstantPoolValue.h"
20 #include "ARMISelLowering.h"
21 #include "ARMMachineFunctionInfo.h"
22 #include "ARMSubtarget.h"
23 #include "MCTargetDesc/ARMAddressingModes.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/CodeGen/Analysis.h"
26 #include "llvm/CodeGen/FastISel.h"
27 #include "llvm/CodeGen/FunctionLoweringInfo.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineMemOperand.h"
32 #include "llvm/CodeGen/MachineModuleInfo.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/IR/CallSite.h"
35 #include "llvm/IR/CallingConv.h"
36 #include "llvm/IR/DataLayout.h"
37 #include "llvm/IR/DerivedTypes.h"
38 #include "llvm/IR/GetElementPtrTypeIterator.h"
39 #include "llvm/IR/GlobalVariable.h"
40 #include "llvm/IR/Instructions.h"
41 #include "llvm/IR/IntrinsicInst.h"
42 #include "llvm/IR/Module.h"
43 #include "llvm/IR/Operator.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/ErrorHandling.h"
46 #include "llvm/Target/TargetInstrInfo.h"
47 #include "llvm/Target/TargetLowering.h"
48 #include "llvm/Target/TargetMachine.h"
49 #include "llvm/Target/TargetOptions.h"
52 extern cl::opt<bool> EnableARMLongCalls;
56 // All possible address modes, plus some.
57 typedef struct Address {
70 // Innocuous defaults for our address.
72 : BaseType(RegBase), Offset(0) {
77 class ARMFastISel final : public FastISel {
79 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
80 /// make the right decision when generating code for different targets.
81 const ARMSubtarget *Subtarget;
83 const TargetMachine &TM;
84 const TargetInstrInfo &TII;
85 const TargetLowering &TLI;
88 // Convenience variables to avoid some queries.
93 explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
94 const TargetLibraryInfo *libInfo)
95 : FastISel(funcInfo, libInfo),
96 M(const_cast<Module &>(*funcInfo.Fn->getParent())),
97 TM(funcInfo.MF->getTarget()),
98 TII(*TM.getSubtargetImpl()->getInstrInfo()),
99 TLI(*TM.getSubtargetImpl()->getTargetLowering()) {
100 Subtarget = &TM.getSubtarget<ARMSubtarget>();
101 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
102 isThumb2 = AFI->isThumbFunction();
103 Context = &funcInfo.Fn->getContext();
106 // Code from FastISel.cpp.
108 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
109 const TargetRegisterClass *RC,
110 unsigned Op0, bool Op0IsKill);
111 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill,
114 unsigned Op1, bool Op1IsKill);
115 unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
116 const TargetRegisterClass *RC,
117 unsigned Op0, bool Op0IsKill,
118 unsigned Op1, bool Op1IsKill,
119 unsigned Op2, bool Op2IsKill);
120 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
121 const TargetRegisterClass *RC,
122 unsigned Op0, bool Op0IsKill,
124 unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
125 const TargetRegisterClass *RC,
126 unsigned Op0, bool Op0IsKill,
127 unsigned Op1, bool Op1IsKill,
129 unsigned FastEmitInst_i(unsigned MachineInstOpcode,
130 const TargetRegisterClass *RC,
133 // Backend specific FastISel code.
135 bool TargetSelectInstruction(const Instruction *I) override;
136 unsigned TargetMaterializeConstant(const Constant *C) override;
137 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
138 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
139 const LoadInst *LI) override;
140 bool FastLowerArguments() override;
142 #include "ARMGenFastISel.inc"
144 // Instruction selection routines.
146 bool SelectLoad(const Instruction *I);
147 bool SelectStore(const Instruction *I);
148 bool SelectBranch(const Instruction *I);
149 bool SelectIndirectBr(const Instruction *I);
150 bool SelectCmp(const Instruction *I);
151 bool SelectFPExt(const Instruction *I);
152 bool SelectFPTrunc(const Instruction *I);
153 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
154 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
155 bool SelectIToFP(const Instruction *I, bool isSigned);
156 bool SelectFPToI(const Instruction *I, bool isSigned);
157 bool SelectDiv(const Instruction *I, bool isSigned);
158 bool SelectRem(const Instruction *I, bool isSigned);
159 bool SelectCall(const Instruction *I, const char *IntrMemName);
160 bool SelectIntrinsicCall(const IntrinsicInst &I);
161 bool SelectSelect(const Instruction *I);
162 bool SelectRet(const Instruction *I);
163 bool SelectTrunc(const Instruction *I);
164 bool SelectIntExt(const Instruction *I);
165 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
169 bool isTypeLegal(Type *Ty, MVT &VT);
170 bool isLoadTypeLegal(Type *Ty, MVT &VT);
171 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
173 bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
174 unsigned Alignment = 0, bool isZExt = true,
175 bool allocReg = true);
176 bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
177 unsigned Alignment = 0);
178 bool ARMComputeAddress(const Value *Obj, Address &Addr);
179 void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3);
180 bool ARMIsMemCpySmall(uint64_t Len);
181 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
183 unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
184 unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT);
185 unsigned ARMMaterializeInt(const Constant *C, MVT VT);
186 unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT);
187 unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg);
188 unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg);
189 unsigned ARMSelectCallOp(bool UseReg);
190 unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT);
192 const TargetLowering *getTargetLowering() {
193 return TM.getSubtargetImpl()->getTargetLowering();
196 // Call handling routines.
198 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
201 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
202 SmallVectorImpl<unsigned> &ArgRegs,
203 SmallVectorImpl<MVT> &ArgVTs,
204 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
205 SmallVectorImpl<unsigned> &RegArgs,
209 unsigned getLibcallReg(const Twine &Name);
210 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
211 const Instruction *I, CallingConv::ID CC,
212 unsigned &NumBytes, bool isVarArg);
213 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
215 // OptionalDef handling routines.
217 bool isARMNEONPred(const MachineInstr *MI);
218 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
219 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
220 void AddLoadStoreOperands(MVT VT, Address &Addr,
221 const MachineInstrBuilder &MIB,
222 unsigned Flags, bool useAM3);
225 } // end anonymous namespace
227 #include "ARMGenCallingConv.inc"
229 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
230 // we don't care about implicit defs here, just places we'll need to add a
231 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
232 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
233 if (!MI->hasOptionalDef())
236 // Look to see if our OptionalDef is defining CPSR or CCR.
237 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
238 const MachineOperand &MO = MI->getOperand(i);
239 if (!MO.isReg() || !MO.isDef()) continue;
240 if (MO.getReg() == ARM::CPSR)
246 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
247 const MCInstrDesc &MCID = MI->getDesc();
249 // If we're a thumb2 or not NEON function we'll be handled via isPredicable.
250 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
251 AFI->isThumb2Function())
252 return MI->isPredicable();
254 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
255 if (MCID.OpInfo[i].isPredicate())
261 // If the machine is predicable go ahead and add the predicate operands, if
262 // it needs default CC operands add those.
263 // TODO: If we want to support thumb1 then we'll need to deal with optional
264 // CPSR defs that need to be added before the remaining operands. See s_cc_out
265 // for descriptions why.
266 const MachineInstrBuilder &
267 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
268 MachineInstr *MI = &*MIB;
270 // Do we use a predicate? or...
271 // Are we NEON in ARM mode and have a predicate operand? If so, I know
272 // we're not predicable but add it anyways.
273 if (isARMNEONPred(MI))
276 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
277 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
279 if (DefinesOptionalPredicate(MI, &CPSR)) {
288 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
289 const TargetRegisterClass *RC,
290 unsigned Op0, bool Op0IsKill) {
291 unsigned ResultReg = createResultReg(RC);
292 const MCInstrDesc &II = TII.get(MachineInstOpcode);
294 // Make sure the input operand is sufficiently constrained to be legal
295 // for this instruction.
296 Op0 = constrainOperandRegClass(II, Op0, 1);
297 if (II.getNumDefs() >= 1) {
298 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
299 ResultReg).addReg(Op0, Op0IsKill * RegState::Kill));
301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
302 .addReg(Op0, Op0IsKill * RegState::Kill));
303 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
304 TII.get(TargetOpcode::COPY), ResultReg)
305 .addReg(II.ImplicitDefs[0]));
310 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
311 const TargetRegisterClass *RC,
312 unsigned Op0, bool Op0IsKill,
313 unsigned Op1, bool Op1IsKill) {
314 unsigned ResultReg = createResultReg(RC);
315 const MCInstrDesc &II = TII.get(MachineInstOpcode);
317 // Make sure the input operands are sufficiently constrained to be legal
318 // for this instruction.
319 Op0 = constrainOperandRegClass(II, Op0, 1);
320 Op1 = constrainOperandRegClass(II, Op1, 2);
322 if (II.getNumDefs() >= 1) {
324 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
325 .addReg(Op0, Op0IsKill * RegState::Kill)
326 .addReg(Op1, Op1IsKill * RegState::Kill));
328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
329 .addReg(Op0, Op0IsKill * RegState::Kill)
330 .addReg(Op1, Op1IsKill * RegState::Kill));
331 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
332 TII.get(TargetOpcode::COPY), ResultReg)
333 .addReg(II.ImplicitDefs[0]));
338 unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
339 const TargetRegisterClass *RC,
340 unsigned Op0, bool Op0IsKill,
341 unsigned Op1, bool Op1IsKill,
342 unsigned Op2, bool Op2IsKill) {
343 unsigned ResultReg = createResultReg(RC);
344 const MCInstrDesc &II = TII.get(MachineInstOpcode);
346 // Make sure the input operands are sufficiently constrained to be legal
347 // for this instruction.
348 Op0 = constrainOperandRegClass(II, Op0, 1);
349 Op1 = constrainOperandRegClass(II, Op1, 2);
350 Op2 = constrainOperandRegClass(II, Op1, 3);
352 if (II.getNumDefs() >= 1) {
354 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
355 .addReg(Op0, Op0IsKill * RegState::Kill)
356 .addReg(Op1, Op1IsKill * RegState::Kill)
357 .addReg(Op2, Op2IsKill * RegState::Kill));
359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
360 .addReg(Op0, Op0IsKill * RegState::Kill)
361 .addReg(Op1, Op1IsKill * RegState::Kill)
362 .addReg(Op2, Op2IsKill * RegState::Kill));
363 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
364 TII.get(TargetOpcode::COPY), ResultReg)
365 .addReg(II.ImplicitDefs[0]));
370 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
371 const TargetRegisterClass *RC,
372 unsigned Op0, bool Op0IsKill,
374 unsigned ResultReg = createResultReg(RC);
375 const MCInstrDesc &II = TII.get(MachineInstOpcode);
377 // Make sure the input operand is sufficiently constrained to be legal
378 // for this instruction.
379 Op0 = constrainOperandRegClass(II, Op0, 1);
380 if (II.getNumDefs() >= 1) {
382 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
383 .addReg(Op0, Op0IsKill * RegState::Kill)
386 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
387 .addReg(Op0, Op0IsKill * RegState::Kill)
389 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
390 TII.get(TargetOpcode::COPY), ResultReg)
391 .addReg(II.ImplicitDefs[0]));
396 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
397 const TargetRegisterClass *RC,
398 unsigned Op0, bool Op0IsKill,
399 unsigned Op1, bool Op1IsKill,
401 unsigned ResultReg = createResultReg(RC);
402 const MCInstrDesc &II = TII.get(MachineInstOpcode);
404 // Make sure the input operands are sufficiently constrained to be legal
405 // for this instruction.
406 Op0 = constrainOperandRegClass(II, Op0, 1);
407 Op1 = constrainOperandRegClass(II, Op1, 2);
408 if (II.getNumDefs() >= 1) {
410 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
411 .addReg(Op0, Op0IsKill * RegState::Kill)
412 .addReg(Op1, Op1IsKill * RegState::Kill)
415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
416 .addReg(Op0, Op0IsKill * RegState::Kill)
417 .addReg(Op1, Op1IsKill * RegState::Kill)
419 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
420 TII.get(TargetOpcode::COPY), ResultReg)
421 .addReg(II.ImplicitDefs[0]));
426 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
427 const TargetRegisterClass *RC,
429 unsigned ResultReg = createResultReg(RC);
430 const MCInstrDesc &II = TII.get(MachineInstOpcode);
432 if (II.getNumDefs() >= 1) {
433 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
434 ResultReg).addImm(Imm));
436 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
438 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
439 TII.get(TargetOpcode::COPY), ResultReg)
440 .addReg(II.ImplicitDefs[0]));
445 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
446 // checks from the various callers.
447 unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) {
448 if (VT == MVT::f64) return 0;
450 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
451 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
452 TII.get(ARM::VMOVSR), MoveReg)
457 unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) {
458 if (VT == MVT::i64) return 0;
460 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
461 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
462 TII.get(ARM::VMOVRS), MoveReg)
467 // For double width floating point we need to materialize two constants
468 // (the high and the low) into integer registers then use a move to get
469 // the combined constant into an FP reg.
470 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
471 const APFloat Val = CFP->getValueAPF();
472 bool is64bit = VT == MVT::f64;
474 // This checks to see if we can use VFP3 instructions to materialize
475 // a constant, otherwise we have to go through the constant pool.
476 if (TLI.isFPImmLegal(Val, VT)) {
480 Imm = ARM_AM::getFP64Imm(Val);
483 Imm = ARM_AM::getFP32Imm(Val);
486 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
487 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
488 TII.get(Opc), DestReg).addImm(Imm));
492 // Require VFP2 for loading fp constants.
493 if (!Subtarget->hasVFP2()) return false;
495 // MachineConstantPool wants an explicit alignment.
496 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
498 // TODO: Figure out if this is correct.
499 Align = DL.getTypeAllocSize(CFP->getType());
501 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
502 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
503 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
505 // The extra reg is for addrmode5.
507 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
508 .addConstantPoolIndex(Idx)
513 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
515 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
518 // If we can do this in a single instruction without a constant pool entry
520 const ConstantInt *CI = cast<ConstantInt>(C);
521 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
522 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
523 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
525 unsigned ImmReg = createResultReg(RC);
526 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
527 TII.get(Opc), ImmReg)
528 .addImm(CI->getZExtValue()));
532 // Use MVN to emit negative constants.
533 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
534 unsigned Imm = (unsigned)~(CI->getSExtValue());
535 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
536 (ARM_AM::getSOImmVal(Imm) != -1);
538 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
539 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
541 unsigned ImmReg = createResultReg(RC);
542 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
543 TII.get(Opc), ImmReg)
549 if (Subtarget->useMovt(*FuncInfo.MF))
550 return FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
552 // Load from constant pool. For now 32-bit only.
556 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
558 // MachineConstantPool wants an explicit alignment.
559 unsigned Align = DL.getPrefTypeAlignment(C->getType());
561 // TODO: Figure out if this is correct.
562 Align = DL.getTypeAllocSize(C->getType());
564 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
567 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
568 TII.get(ARM::t2LDRpci), DestReg)
569 .addConstantPoolIndex(Idx));
571 // The extra immediate is for addrmode2.
572 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
573 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
574 TII.get(ARM::LDRcp), DestReg)
575 .addConstantPoolIndex(Idx)
582 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
583 // For now 32-bit only.
584 if (VT != MVT::i32) return 0;
586 Reloc::Model RelocM = TM.getRelocationModel();
587 bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
588 const TargetRegisterClass *RC = isThumb2 ?
589 (const TargetRegisterClass*)&ARM::rGPRRegClass :
590 (const TargetRegisterClass*)&ARM::GPRRegClass;
591 unsigned DestReg = createResultReg(RC);
593 // FastISel TLS support on non-MachO is broken, punt to SelectionDAG.
594 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
595 bool IsThreadLocal = GVar && GVar->isThreadLocal();
596 if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0;
598 // Use movw+movt when possible, it avoids constant pool entries.
599 // Non-darwin targets only support static movt relocations in FastISel.
600 if (Subtarget->useMovt(*FuncInfo.MF) &&
601 (Subtarget->isTargetMachO() || RelocM == Reloc::Static)) {
603 unsigned char TF = 0;
604 if (Subtarget->isTargetMachO())
605 TF = ARMII::MO_NONLAZY;
609 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
612 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
615 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
616 TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF));
618 // MachineConstantPool wants an explicit alignment.
619 unsigned Align = DL.getPrefTypeAlignment(GV->getType());
621 // TODO: Figure out if this is correct.
622 Align = DL.getTypeAllocSize(GV->getType());
625 if (Subtarget->isTargetELF() && RelocM == Reloc::PIC_)
626 return ARMLowerPICELF(GV, Align, VT);
629 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
630 (Subtarget->isThumb() ? 4 : 8);
631 unsigned Id = AFI->createPICLabelUId();
632 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
635 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
638 MachineInstrBuilder MIB;
640 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
641 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
642 DestReg).addConstantPoolIndex(Idx);
643 if (RelocM == Reloc::PIC_)
645 AddOptionalDefs(MIB);
647 // The extra immediate is for addrmode2.
648 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
649 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
650 TII.get(ARM::LDRcp), DestReg)
651 .addConstantPoolIndex(Idx)
653 AddOptionalDefs(MIB);
655 if (RelocM == Reloc::PIC_) {
656 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
657 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
659 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
660 DbgLoc, TII.get(Opc), NewDestReg)
663 AddOptionalDefs(MIB);
670 MachineInstrBuilder MIB;
671 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
673 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
674 TII.get(ARM::t2LDRi12), NewDestReg)
678 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
679 TII.get(ARM::LDRi12), NewDestReg)
682 DestReg = NewDestReg;
683 AddOptionalDefs(MIB);
689 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
690 EVT CEVT = TLI.getValueType(C->getType(), true);
692 // Only handle simple types.
693 if (!CEVT.isSimple()) return 0;
694 MVT VT = CEVT.getSimpleVT();
696 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
697 return ARMMaterializeFP(CFP, VT);
698 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
699 return ARMMaterializeGV(GV, VT);
700 else if (isa<ConstantInt>(C))
701 return ARMMaterializeInt(C, VT);
706 // TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
708 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
709 // Don't handle dynamic allocas.
710 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
713 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
715 DenseMap<const AllocaInst*, int>::iterator SI =
716 FuncInfo.StaticAllocaMap.find(AI);
718 // This will get lowered later into the correct offsets and registers
719 // via rewriteXFrameIndex.
720 if (SI != FuncInfo.StaticAllocaMap.end()) {
721 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
722 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
723 unsigned ResultReg = createResultReg(RC);
724 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
726 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
727 TII.get(Opc), ResultReg)
728 .addFrameIndex(SI->second)
736 bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
737 EVT evt = TLI.getValueType(Ty, true);
739 // Only handle simple types.
740 if (evt == MVT::Other || !evt.isSimple()) return false;
741 VT = evt.getSimpleVT();
743 // Handle all legal types, i.e. a register that will directly hold this
745 return TLI.isTypeLegal(VT);
748 bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
749 if (isTypeLegal(Ty, VT)) return true;
751 // If this is a type than can be sign or zero-extended to a basic operation
752 // go ahead and accept it now.
753 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
759 // Computes the address to get to an object.
760 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
761 // Some boilerplate from the X86 FastISel.
762 const User *U = nullptr;
763 unsigned Opcode = Instruction::UserOp1;
764 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
765 // Don't walk into other basic blocks unless the object is an alloca from
766 // another block, otherwise it may not have a virtual register assigned.
767 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
768 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
769 Opcode = I->getOpcode();
772 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
773 Opcode = C->getOpcode();
777 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
778 if (Ty->getAddressSpace() > 255)
779 // Fast instruction selection doesn't support the special
786 case Instruction::BitCast:
787 // Look through bitcasts.
788 return ARMComputeAddress(U->getOperand(0), Addr);
789 case Instruction::IntToPtr:
790 // Look past no-op inttoptrs.
791 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
792 return ARMComputeAddress(U->getOperand(0), Addr);
794 case Instruction::PtrToInt:
795 // Look past no-op ptrtoints.
796 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
797 return ARMComputeAddress(U->getOperand(0), Addr);
799 case Instruction::GetElementPtr: {
800 Address SavedAddr = Addr;
801 int TmpOffset = Addr.Offset;
803 // Iterate through the GEP folding the constants into offsets where
805 gep_type_iterator GTI = gep_type_begin(U);
806 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
807 i != e; ++i, ++GTI) {
808 const Value *Op = *i;
809 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
810 const StructLayout *SL = DL.getStructLayout(STy);
811 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
812 TmpOffset += SL->getElementOffset(Idx);
814 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
816 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
817 // Constant-offset addressing.
818 TmpOffset += CI->getSExtValue() * S;
821 if (canFoldAddIntoGEP(U, Op)) {
822 // A compatible add with a constant operand. Fold the constant.
824 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
825 TmpOffset += CI->getSExtValue() * S;
826 // Iterate on the other operand.
827 Op = cast<AddOperator>(Op)->getOperand(0);
831 goto unsupported_gep;
836 // Try to grab the base operand now.
837 Addr.Offset = TmpOffset;
838 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
840 // We failed, restore everything and try the other options.
846 case Instruction::Alloca: {
847 const AllocaInst *AI = cast<AllocaInst>(Obj);
848 DenseMap<const AllocaInst*, int>::iterator SI =
849 FuncInfo.StaticAllocaMap.find(AI);
850 if (SI != FuncInfo.StaticAllocaMap.end()) {
851 Addr.BaseType = Address::FrameIndexBase;
852 Addr.Base.FI = SI->second;
859 // Try to get this in a register if nothing else has worked.
860 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
861 return Addr.Base.Reg != 0;
864 void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
865 bool needsLowering = false;
866 switch (VT.SimpleTy) {
867 default: llvm_unreachable("Unhandled load/store type!");
873 // Integer loads/stores handle 12-bit offsets.
874 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
875 // Handle negative offsets.
876 if (needsLowering && isThumb2)
877 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
880 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
881 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
886 // Floating point operands handle 8-bit offsets.
887 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
891 // If this is a stack pointer and the offset needs to be simplified then
892 // put the alloca address into a register, set the base type back to
893 // register and continue. This should almost never happen.
894 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
895 const TargetRegisterClass *RC = isThumb2 ?
896 (const TargetRegisterClass*)&ARM::tGPRRegClass :
897 (const TargetRegisterClass*)&ARM::GPRRegClass;
898 unsigned ResultReg = createResultReg(RC);
899 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
900 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
901 TII.get(Opc), ResultReg)
902 .addFrameIndex(Addr.Base.FI)
904 Addr.Base.Reg = ResultReg;
905 Addr.BaseType = Address::RegBase;
908 // Since the offset is too large for the load/store instruction
909 // get the reg+offset into a register.
911 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
912 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
917 void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
918 const MachineInstrBuilder &MIB,
919 unsigned Flags, bool useAM3) {
920 // addrmode5 output depends on the selection dag addressing dividing the
921 // offset by 4 that it then later multiplies. Do this here as well.
922 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
925 // Frame base works a bit differently. Handle it separately.
926 if (Addr.BaseType == Address::FrameIndexBase) {
927 int FI = Addr.Base.FI;
928 int Offset = Addr.Offset;
929 MachineMemOperand *MMO =
930 FuncInfo.MF->getMachineMemOperand(
931 MachinePointerInfo::getFixedStack(FI, Offset),
933 MFI.getObjectSize(FI),
934 MFI.getObjectAlignment(FI));
935 // Now add the rest of the operands.
936 MIB.addFrameIndex(FI);
938 // ARM halfword load/stores and signed byte loads need an additional
941 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
945 MIB.addImm(Addr.Offset);
947 MIB.addMemOperand(MMO);
949 // Now add the rest of the operands.
950 MIB.addReg(Addr.Base.Reg);
952 // ARM halfword load/stores and signed byte loads need an additional
955 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
959 MIB.addImm(Addr.Offset);
962 AddOptionalDefs(MIB);
965 bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
966 unsigned Alignment, bool isZExt, bool allocReg) {
969 bool needVMOV = false;
970 const TargetRegisterClass *RC;
971 switch (VT.SimpleTy) {
972 // This is mostly going to be Neon/vector support.
973 default: return false;
977 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
978 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
980 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
989 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
992 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
996 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
997 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
999 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1001 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1004 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
1007 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
1011 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1014 Opc = ARM::t2LDRi12;
1018 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
1021 if (!Subtarget->hasVFP2()) return false;
1022 // Unaligned loads need special handling. Floats require word-alignment.
1023 if (Alignment && Alignment < 4) {
1026 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
1027 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
1030 RC = TLI.getRegClassFor(VT);
1034 if (!Subtarget->hasVFP2()) return false;
1035 // FIXME: Unaligned loads need special handling. Doublewords require
1037 if (Alignment && Alignment < 4)
1041 RC = TLI.getRegClassFor(VT);
1044 // Simplify this down to something we can handle.
1045 ARMSimplifyAddress(Addr, VT, useAM3);
1047 // Create the base instruction, then add the operands.
1049 ResultReg = createResultReg(RC);
1050 assert (ResultReg > 255 && "Expected an allocated virtual register.");
1051 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1052 TII.get(Opc), ResultReg);
1053 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
1055 // If we had an unaligned load of a float we've converted it to an regular
1056 // load. Now we must move from the GRP to the FP register.
1058 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1059 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1060 TII.get(ARM::VMOVSR), MoveReg)
1061 .addReg(ResultReg));
1062 ResultReg = MoveReg;
1067 bool ARMFastISel::SelectLoad(const Instruction *I) {
1068 // Atomic loads need special handling.
1069 if (cast<LoadInst>(I)->isAtomic())
1072 // Verify we have a legal type before going any further.
1074 if (!isLoadTypeLegal(I->getType(), VT))
1077 // See if we can handle this address.
1079 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
1082 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1084 UpdateValueMap(I, ResultReg);
1088 bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
1089 unsigned Alignment) {
1091 bool useAM3 = false;
1092 switch (VT.SimpleTy) {
1093 // This is mostly going to be Neon/vector support.
1094 default: return false;
1096 unsigned Res = createResultReg(isThumb2 ?
1097 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1098 (const TargetRegisterClass*)&ARM::GPRRegClass);
1099 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
1100 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
1101 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1103 .addReg(SrcReg).addImm(1));
1105 } // Fallthrough here.
1108 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1109 StrOpc = ARM::t2STRBi8;
1111 StrOpc = ARM::t2STRBi12;
1113 StrOpc = ARM::STRBi12;
1117 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
1121 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1122 StrOpc = ARM::t2STRHi8;
1124 StrOpc = ARM::t2STRHi12;
1131 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
1135 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1136 StrOpc = ARM::t2STRi8;
1138 StrOpc = ARM::t2STRi12;
1140 StrOpc = ARM::STRi12;
1144 if (!Subtarget->hasVFP2()) return false;
1145 // Unaligned stores need special handling. Floats require word-alignment.
1146 if (Alignment && Alignment < 4) {
1147 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1148 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1149 TII.get(ARM::VMOVRS), MoveReg)
1153 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
1155 StrOpc = ARM::VSTRS;
1159 if (!Subtarget->hasVFP2()) return false;
1160 // FIXME: Unaligned stores need special handling. Doublewords require
1162 if (Alignment && Alignment < 4)
1165 StrOpc = ARM::VSTRD;
1168 // Simplify this down to something we can handle.
1169 ARMSimplifyAddress(Addr, VT, useAM3);
1171 // Create the base instruction, then add the operands.
1172 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
1173 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1176 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
1180 bool ARMFastISel::SelectStore(const Instruction *I) {
1181 Value *Op0 = I->getOperand(0);
1182 unsigned SrcReg = 0;
1184 // Atomic stores need special handling.
1185 if (cast<StoreInst>(I)->isAtomic())
1188 // Verify we have a legal type before going any further.
1190 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
1193 // Get the value to be stored into a register.
1194 SrcReg = getRegForValue(Op0);
1195 if (SrcReg == 0) return false;
1197 // See if we can handle this address.
1199 if (!ARMComputeAddress(I->getOperand(1), Addr))
1202 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1207 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1209 // Needs two compares...
1210 case CmpInst::FCMP_ONE:
1211 case CmpInst::FCMP_UEQ:
1213 // AL is our "false" for now. The other two need more compares.
1215 case CmpInst::ICMP_EQ:
1216 case CmpInst::FCMP_OEQ:
1218 case CmpInst::ICMP_SGT:
1219 case CmpInst::FCMP_OGT:
1221 case CmpInst::ICMP_SGE:
1222 case CmpInst::FCMP_OGE:
1224 case CmpInst::ICMP_UGT:
1225 case CmpInst::FCMP_UGT:
1227 case CmpInst::FCMP_OLT:
1229 case CmpInst::ICMP_ULE:
1230 case CmpInst::FCMP_OLE:
1232 case CmpInst::FCMP_ORD:
1234 case CmpInst::FCMP_UNO:
1236 case CmpInst::FCMP_UGE:
1238 case CmpInst::ICMP_SLT:
1239 case CmpInst::FCMP_ULT:
1241 case CmpInst::ICMP_SLE:
1242 case CmpInst::FCMP_ULE:
1244 case CmpInst::FCMP_UNE:
1245 case CmpInst::ICMP_NE:
1247 case CmpInst::ICMP_UGE:
1249 case CmpInst::ICMP_ULT:
1254 bool ARMFastISel::SelectBranch(const Instruction *I) {
1255 const BranchInst *BI = cast<BranchInst>(I);
1256 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1257 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1259 // Simple branch support.
1261 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1263 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1264 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1266 // Get the compare predicate.
1267 // Try to take advantage of fallthrough opportunities.
1268 CmpInst::Predicate Predicate = CI->getPredicate();
1269 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1270 std::swap(TBB, FBB);
1271 Predicate = CmpInst::getInversePredicate(Predicate);
1274 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
1276 // We may not handle every CC for now.
1277 if (ARMPred == ARMCC::AL) return false;
1279 // Emit the compare.
1280 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1283 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1284 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
1285 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1286 FastEmitBranch(FBB, DbgLoc);
1287 FuncInfo.MBB->addSuccessor(TBB);
1290 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1292 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1293 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
1294 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1295 unsigned OpReg = getRegForValue(TI->getOperand(0));
1296 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
1297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1299 .addReg(OpReg).addImm(1));
1301 unsigned CCMode = ARMCC::NE;
1302 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1303 std::swap(TBB, FBB);
1307 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
1309 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1311 FastEmitBranch(FBB, DbgLoc);
1312 FuncInfo.MBB->addSuccessor(TBB);
1315 } else if (const ConstantInt *CI =
1316 dyn_cast<ConstantInt>(BI->getCondition())) {
1317 uint64_t Imm = CI->getZExtValue();
1318 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1319 FastEmitBranch(Target, DbgLoc);
1323 unsigned CmpReg = getRegForValue(BI->getCondition());
1324 if (CmpReg == 0) return false;
1326 // We've been divorced from our compare! Our block was split, and
1327 // now our compare lives in a predecessor block. We musn't
1328 // re-compare here, as the children of the compare aren't guaranteed
1329 // live across the block boundary (we *could* check for this).
1330 // Regardless, the compare has been done in the predecessor block,
1331 // and it left a value for us in a virtual register. Ergo, we test
1332 // the one-bit value left in the virtual register.
1333 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1334 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
1336 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc))
1340 unsigned CCMode = ARMCC::NE;
1341 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1342 std::swap(TBB, FBB);
1346 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
1348 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1349 FastEmitBranch(FBB, DbgLoc);
1350 FuncInfo.MBB->addSuccessor(TBB);
1354 bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1355 unsigned AddrReg = getRegForValue(I->getOperand(0));
1356 if (AddrReg == 0) return false;
1358 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
1359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1360 TII.get(Opc)).addReg(AddrReg));
1362 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1363 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1364 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1369 bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1371 Type *Ty = Src1Value->getType();
1372 EVT SrcEVT = TLI.getValueType(Ty, true);
1373 if (!SrcEVT.isSimple()) return false;
1374 MVT SrcVT = SrcEVT.getSimpleVT();
1376 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1377 if (isFloat && !Subtarget->hasVFP2())
1380 // Check to see if the 2nd operand is a constant that we can encode directly
1383 bool UseImm = false;
1384 bool isNegativeImm = false;
1385 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1386 // Thus, Src1Value may be a ConstantInt, but we're missing it.
1387 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1388 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1390 const APInt &CIVal = ConstInt->getValue();
1391 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1392 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
1393 // then a cmn, because there is no way to represent 2147483648 as a
1394 // signed 32-bit int.
1395 if (Imm < 0 && Imm != (int)0x80000000) {
1396 isNegativeImm = true;
1399 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1400 (ARM_AM::getSOImmVal(Imm) != -1);
1402 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1403 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1404 if (ConstFP->isZero() && !ConstFP->isNegative())
1410 bool needsExt = false;
1411 switch (SrcVT.SimpleTy) {
1412 default: return false;
1413 // TODO: Verify compares.
1416 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
1420 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
1426 // Intentional fall-through.
1430 CmpOpc = ARM::t2CMPrr;
1432 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
1435 CmpOpc = ARM::CMPrr;
1437 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
1442 unsigned SrcReg1 = getRegForValue(Src1Value);
1443 if (SrcReg1 == 0) return false;
1445 unsigned SrcReg2 = 0;
1447 SrcReg2 = getRegForValue(Src2Value);
1448 if (SrcReg2 == 0) return false;
1451 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1453 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1454 if (SrcReg1 == 0) return false;
1456 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1457 if (SrcReg2 == 0) return false;
1461 const MCInstrDesc &II = TII.get(CmpOpc);
1462 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
1464 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
1465 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
1466 .addReg(SrcReg1).addReg(SrcReg2));
1468 MachineInstrBuilder MIB;
1469 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
1472 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1475 AddOptionalDefs(MIB);
1478 // For floating point we need to move the result to a comparison register
1479 // that we can then use for branches.
1480 if (Ty->isFloatTy() || Ty->isDoubleTy())
1481 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1482 TII.get(ARM::FMSTAT)));
1486 bool ARMFastISel::SelectCmp(const Instruction *I) {
1487 const CmpInst *CI = cast<CmpInst>(I);
1489 // Get the compare predicate.
1490 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1492 // We may not handle every CC for now.
1493 if (ARMPred == ARMCC::AL) return false;
1495 // Emit the compare.
1496 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1499 // Now set a register based on the comparison. Explicitly set the predicates
1501 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1502 const TargetRegisterClass *RC = isThumb2 ?
1503 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1504 (const TargetRegisterClass*)&ARM::GPRRegClass;
1505 unsigned DestReg = createResultReg(RC);
1506 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1507 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1508 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
1509 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), DestReg)
1510 .addReg(ZeroReg).addImm(1)
1511 .addImm(ARMPred).addReg(ARM::CPSR);
1513 UpdateValueMap(I, DestReg);
1517 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1518 // Make sure we have VFP and that we're extending float to double.
1519 if (!Subtarget->hasVFP2()) return false;
1521 Value *V = I->getOperand(0);
1522 if (!I->getType()->isDoubleTy() ||
1523 !V->getType()->isFloatTy()) return false;
1525 unsigned Op = getRegForValue(V);
1526 if (Op == 0) return false;
1528 unsigned Result = createResultReg(&ARM::DPRRegClass);
1529 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1530 TII.get(ARM::VCVTDS), Result)
1532 UpdateValueMap(I, Result);
1536 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1537 // Make sure we have VFP and that we're truncating double to float.
1538 if (!Subtarget->hasVFP2()) return false;
1540 Value *V = I->getOperand(0);
1541 if (!(I->getType()->isFloatTy() &&
1542 V->getType()->isDoubleTy())) return false;
1544 unsigned Op = getRegForValue(V);
1545 if (Op == 0) return false;
1547 unsigned Result = createResultReg(&ARM::SPRRegClass);
1548 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1549 TII.get(ARM::VCVTSD), Result)
1551 UpdateValueMap(I, Result);
1555 bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
1556 // Make sure we have VFP.
1557 if (!Subtarget->hasVFP2()) return false;
1560 Type *Ty = I->getType();
1561 if (!isTypeLegal(Ty, DstVT))
1564 Value *Src = I->getOperand(0);
1565 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
1566 if (!SrcEVT.isSimple())
1568 MVT SrcVT = SrcEVT.getSimpleVT();
1569 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1572 unsigned SrcReg = getRegForValue(Src);
1573 if (SrcReg == 0) return false;
1575 // Handle sign-extension.
1576 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1577 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
1578 /*isZExt*/!isSigned);
1579 if (SrcReg == 0) return false;
1582 // The conversion routine works on fp-reg to fp-reg and the operand above
1583 // was an integer, move it to the fp registers if possible.
1584 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
1585 if (FP == 0) return false;
1588 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1589 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
1592 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1593 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1594 TII.get(Opc), ResultReg).addReg(FP));
1595 UpdateValueMap(I, ResultReg);
1599 bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
1600 // Make sure we have VFP.
1601 if (!Subtarget->hasVFP2()) return false;
1604 Type *RetTy = I->getType();
1605 if (!isTypeLegal(RetTy, DstVT))
1608 unsigned Op = getRegForValue(I->getOperand(0));
1609 if (Op == 0) return false;
1612 Type *OpTy = I->getOperand(0)->getType();
1613 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1614 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
1617 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
1618 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1619 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1620 TII.get(Opc), ResultReg).addReg(Op));
1622 // This result needs to be in an integer register, but the conversion only
1623 // takes place in fp-regs.
1624 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1625 if (IntReg == 0) return false;
1627 UpdateValueMap(I, IntReg);
1631 bool ARMFastISel::SelectSelect(const Instruction *I) {
1633 if (!isTypeLegal(I->getType(), VT))
1636 // Things need to be register sized for register moves.
1637 if (VT != MVT::i32) return false;
1639 unsigned CondReg = getRegForValue(I->getOperand(0));
1640 if (CondReg == 0) return false;
1641 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1642 if (Op1Reg == 0) return false;
1644 // Check to see if we can use an immediate in the conditional move.
1646 bool UseImm = false;
1647 bool isNegativeImm = false;
1648 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1649 assert (VT == MVT::i32 && "Expecting an i32.");
1650 Imm = (int)ConstInt->getValue().getZExtValue();
1652 isNegativeImm = true;
1655 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1656 (ARM_AM::getSOImmVal(Imm) != -1);
1659 unsigned Op2Reg = 0;
1661 Op2Reg = getRegForValue(I->getOperand(2));
1662 if (Op2Reg == 0) return false;
1665 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
1666 CondReg = constrainOperandRegClass(TII.get(CmpOpc), CondReg, 0);
1668 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1673 const TargetRegisterClass *RC;
1675 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
1676 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1678 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1680 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1682 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1684 unsigned ResultReg = createResultReg(RC);
1686 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
1687 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
1688 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1695 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
1696 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1703 UpdateValueMap(I, ResultReg);
1707 bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
1709 Type *Ty = I->getType();
1710 if (!isTypeLegal(Ty, VT))
1713 // If we have integer div support we should have selected this automagically.
1714 // In case we have a real miss go ahead and return false and we'll pick
1716 if (Subtarget->hasDivide()) return false;
1718 // Otherwise emit a libcall.
1719 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1721 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
1722 else if (VT == MVT::i16)
1723 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
1724 else if (VT == MVT::i32)
1725 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
1726 else if (VT == MVT::i64)
1727 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
1728 else if (VT == MVT::i128)
1729 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
1730 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1732 return ARMEmitLibcall(I, LC);
1735 bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
1737 Type *Ty = I->getType();
1738 if (!isTypeLegal(Ty, VT))
1741 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1743 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
1744 else if (VT == MVT::i16)
1745 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
1746 else if (VT == MVT::i32)
1747 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
1748 else if (VT == MVT::i64)
1749 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
1750 else if (VT == MVT::i128)
1751 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
1752 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1754 return ARMEmitLibcall(I, LC);
1757 bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1758 EVT DestVT = TLI.getValueType(I->getType(), true);
1760 // We can get here in the case when we have a binary operation on a non-legal
1761 // type and the target independent selector doesn't know how to handle it.
1762 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1766 switch (ISDOpcode) {
1767 default: return false;
1769 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1772 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1775 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1779 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1780 if (SrcReg1 == 0) return false;
1782 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1783 // in the instruction, rather then materializing the value in a register.
1784 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1785 if (SrcReg2 == 0) return false;
1787 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
1788 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
1789 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
1790 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1791 TII.get(Opc), ResultReg)
1792 .addReg(SrcReg1).addReg(SrcReg2));
1793 UpdateValueMap(I, ResultReg);
1797 bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
1798 EVT FPVT = TLI.getValueType(I->getType(), true);
1799 if (!FPVT.isSimple()) return false;
1800 MVT VT = FPVT.getSimpleVT();
1802 // We can get here in the case when we want to use NEON for our fp
1803 // operations, but can't figure out how to. Just use the vfp instructions
1805 // FIXME: It'd be nice to use NEON instructions.
1806 Type *Ty = I->getType();
1807 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1808 if (isFloat && !Subtarget->hasVFP2())
1812 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1813 switch (ISDOpcode) {
1814 default: return false;
1816 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1819 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1822 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1825 unsigned Op1 = getRegForValue(I->getOperand(0));
1826 if (Op1 == 0) return false;
1828 unsigned Op2 = getRegForValue(I->getOperand(1));
1829 if (Op2 == 0) return false;
1831 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
1832 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1833 TII.get(Opc), ResultReg)
1834 .addReg(Op1).addReg(Op2));
1835 UpdateValueMap(I, ResultReg);
1839 // Call Handling Code
1841 // This is largely taken directly from CCAssignFnForNode
1842 // TODO: We may not support all of this.
1843 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1848 llvm_unreachable("Unsupported calling convention");
1849 case CallingConv::Fast:
1850 if (Subtarget->hasVFP2() && !isVarArg) {
1851 if (!Subtarget->isAAPCS_ABI())
1852 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1853 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1854 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1857 case CallingConv::C:
1858 // Use target triple & subtarget features to do actual dispatch.
1859 if (Subtarget->isAAPCS_ABI()) {
1860 if (Subtarget->hasVFP2() &&
1861 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
1862 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1864 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1866 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1867 case CallingConv::ARM_AAPCS_VFP:
1869 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1870 // Fall through to soft float variant, variadic functions don't
1871 // use hard floating point ABI.
1872 case CallingConv::ARM_AAPCS:
1873 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1874 case CallingConv::ARM_APCS:
1875 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1876 case CallingConv::GHC:
1878 llvm_unreachable("Can't return in GHC call convention");
1880 return CC_ARM_APCS_GHC;
1884 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1885 SmallVectorImpl<unsigned> &ArgRegs,
1886 SmallVectorImpl<MVT> &ArgVTs,
1887 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1888 SmallVectorImpl<unsigned> &RegArgs,
1892 SmallVector<CCValAssign, 16> ArgLocs;
1893 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, ArgLocs, *Context);
1894 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1895 CCAssignFnForCall(CC, false, isVarArg));
1897 // Check that we can handle all of the arguments. If we can't, then bail out
1898 // now before we add code to the MBB.
1899 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1900 CCValAssign &VA = ArgLocs[i];
1901 MVT ArgVT = ArgVTs[VA.getValNo()];
1903 // We don't handle NEON/vector parameters yet.
1904 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1907 // Now copy/store arg to correct locations.
1908 if (VA.isRegLoc() && !VA.needsCustom()) {
1910 } else if (VA.needsCustom()) {
1911 // TODO: We need custom lowering for vector (v2f64) args.
1912 if (VA.getLocVT() != MVT::f64 ||
1913 // TODO: Only handle register args for now.
1914 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1917 switch (ArgVT.SimpleTy) {
1926 if (!Subtarget->hasVFP2())
1930 if (!Subtarget->hasVFP2())
1937 // At the point, we are able to handle the call's arguments in fast isel.
1939 // Get a count of how many bytes are to be pushed on the stack.
1940 NumBytes = CCInfo.getNextStackOffset();
1942 // Issue CALLSEQ_START
1943 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1944 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1945 TII.get(AdjStackDown))
1948 // Process the args.
1949 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1950 CCValAssign &VA = ArgLocs[i];
1951 const Value *ArgVal = Args[VA.getValNo()];
1952 unsigned Arg = ArgRegs[VA.getValNo()];
1953 MVT ArgVT = ArgVTs[VA.getValNo()];
1955 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
1956 "We don't handle NEON/vector parameters yet.");
1958 // Handle arg promotion, etc.
1959 switch (VA.getLocInfo()) {
1960 case CCValAssign::Full: break;
1961 case CCValAssign::SExt: {
1962 MVT DestVT = VA.getLocVT();
1963 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
1964 assert (Arg != 0 && "Failed to emit a sext");
1968 case CCValAssign::AExt:
1969 // Intentional fall-through. Handle AExt and ZExt.
1970 case CCValAssign::ZExt: {
1971 MVT DestVT = VA.getLocVT();
1972 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
1973 assert (Arg != 0 && "Failed to emit a zext");
1977 case CCValAssign::BCvt: {
1978 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
1979 /*TODO: Kill=*/false);
1980 assert(BC != 0 && "Failed to emit a bitcast!");
1982 ArgVT = VA.getLocVT();
1985 default: llvm_unreachable("Unknown arg promotion!");
1988 // Now copy/store arg to correct locations.
1989 if (VA.isRegLoc() && !VA.needsCustom()) {
1990 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1991 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
1992 RegArgs.push_back(VA.getLocReg());
1993 } else if (VA.needsCustom()) {
1994 // TODO: We need custom lowering for vector (v2f64) args.
1995 assert(VA.getLocVT() == MVT::f64 &&
1996 "Custom lowering for v2f64 args not available");
1998 CCValAssign &NextVA = ArgLocs[++i];
2000 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
2001 "We only handle register args!");
2003 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2004 TII.get(ARM::VMOVRRD), VA.getLocReg())
2005 .addReg(NextVA.getLocReg(), RegState::Define)
2007 RegArgs.push_back(VA.getLocReg());
2008 RegArgs.push_back(NextVA.getLocReg());
2010 assert(VA.isMemLoc());
2011 // Need to store on the stack.
2013 // Don't emit stores for undef values.
2014 if (isa<UndefValue>(ArgVal))
2018 Addr.BaseType = Address::RegBase;
2019 Addr.Base.Reg = ARM::SP;
2020 Addr.Offset = VA.getLocMemOffset();
2022 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2023 assert(EmitRet && "Could not emit a store for argument!");
2030 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
2031 const Instruction *I, CallingConv::ID CC,
2032 unsigned &NumBytes, bool isVarArg) {
2033 // Issue CALLSEQ_END
2034 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
2035 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2036 TII.get(AdjStackUp))
2037 .addImm(NumBytes).addImm(0));
2039 // Now the return value.
2040 if (RetVT != MVT::isVoid) {
2041 SmallVector<CCValAssign, 16> RVLocs;
2042 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
2043 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
2045 // Copy all of the result registers out of their specified physreg.
2046 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
2047 // For this move we copy into two registers and then move into the
2048 // double fp reg we want.
2049 MVT DestVT = RVLocs[0].getValVT();
2050 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
2051 unsigned ResultReg = createResultReg(DstRC);
2052 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2053 TII.get(ARM::VMOVDRR), ResultReg)
2054 .addReg(RVLocs[0].getLocReg())
2055 .addReg(RVLocs[1].getLocReg()));
2057 UsedRegs.push_back(RVLocs[0].getLocReg());
2058 UsedRegs.push_back(RVLocs[1].getLocReg());
2060 // Finally update the result.
2061 UpdateValueMap(I, ResultReg);
2063 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
2064 MVT CopyVT = RVLocs[0].getValVT();
2066 // Special handling for extended integers.
2067 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2070 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
2072 unsigned ResultReg = createResultReg(DstRC);
2073 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2074 TII.get(TargetOpcode::COPY),
2075 ResultReg).addReg(RVLocs[0].getLocReg());
2076 UsedRegs.push_back(RVLocs[0].getLocReg());
2078 // Finally update the result.
2079 UpdateValueMap(I, ResultReg);
2086 bool ARMFastISel::SelectRet(const Instruction *I) {
2087 const ReturnInst *Ret = cast<ReturnInst>(I);
2088 const Function &F = *I->getParent()->getParent();
2090 if (!FuncInfo.CanLowerReturn)
2093 // Build a list of return value registers.
2094 SmallVector<unsigned, 4> RetRegs;
2096 CallingConv::ID CC = F.getCallingConv();
2097 if (Ret->getNumOperands() > 0) {
2098 SmallVector<ISD::OutputArg, 4> Outs;
2099 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
2101 // Analyze operands of the call, assigning locations to each operand.
2102 SmallVector<CCValAssign, 16> ValLocs;
2103 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
2104 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2107 const Value *RV = Ret->getOperand(0);
2108 unsigned Reg = getRegForValue(RV);
2112 // Only handle a single return value for now.
2113 if (ValLocs.size() != 1)
2116 CCValAssign &VA = ValLocs[0];
2118 // Don't bother handling odd stuff for now.
2119 if (VA.getLocInfo() != CCValAssign::Full)
2121 // Only handle register returns for now.
2125 unsigned SrcReg = Reg + VA.getValNo();
2126 EVT RVEVT = TLI.getValueType(RV->getType());
2127 if (!RVEVT.isSimple()) return false;
2128 MVT RVVT = RVEVT.getSimpleVT();
2129 MVT DestVT = VA.getValVT();
2130 // Special handling for extended integers.
2131 if (RVVT != DestVT) {
2132 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2135 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2137 // Perform extension if flagged as either zext or sext. Otherwise, do
2139 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2140 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2141 if (SrcReg == 0) return false;
2146 unsigned DstReg = VA.getLocReg();
2147 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2148 // Avoid a cross-class copy. This is very unlikely.
2149 if (!SrcRC->contains(DstReg))
2151 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2152 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
2154 // Add register to return instruction.
2155 RetRegs.push_back(VA.getLocReg());
2158 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
2159 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2161 AddOptionalDefs(MIB);
2162 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2163 MIB.addReg(RetRegs[i], RegState::Implicit);
2167 unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2169 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2171 return isThumb2 ? ARM::tBL : ARM::BL;
2174 unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
2175 // Manually compute the global's type to avoid building it when unnecessary.
2176 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
2177 EVT LCREVT = TLI.getValueType(GVTy);
2178 if (!LCREVT.isSimple()) return 0;
2180 GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
2181 GlobalValue::ExternalLinkage, nullptr,
2183 assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
2184 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
2187 // A quick function that will emit a call for a named libcall in F with the
2188 // vector of passed arguments for the Instruction in I. We can assume that we
2189 // can emit a call for any libcall we can produce. This is an abridged version
2190 // of the full call infrastructure since we won't need to worry about things
2191 // like computed function pointers or strange arguments at call sites.
2192 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
2194 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2195 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
2197 // Handle *simple* calls for now.
2198 Type *RetTy = I->getType();
2200 if (RetTy->isVoidTy())
2201 RetVT = MVT::isVoid;
2202 else if (!isTypeLegal(RetTy, RetVT))
2205 // Can't handle non-double multi-reg retvals.
2206 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
2207 SmallVector<CCValAssign, 16> RVLocs;
2208 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
2209 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
2210 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2214 // Set up the argument vectors.
2215 SmallVector<Value*, 8> Args;
2216 SmallVector<unsigned, 8> ArgRegs;
2217 SmallVector<MVT, 8> ArgVTs;
2218 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2219 Args.reserve(I->getNumOperands());
2220 ArgRegs.reserve(I->getNumOperands());
2221 ArgVTs.reserve(I->getNumOperands());
2222 ArgFlags.reserve(I->getNumOperands());
2223 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
2224 Value *Op = I->getOperand(i);
2225 unsigned Arg = getRegForValue(Op);
2226 if (Arg == 0) return false;
2228 Type *ArgTy = Op->getType();
2230 if (!isTypeLegal(ArgTy, ArgVT)) return false;
2232 ISD::ArgFlagsTy Flags;
2233 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
2234 Flags.setOrigAlign(OriginalAlignment);
2237 ArgRegs.push_back(Arg);
2238 ArgVTs.push_back(ArgVT);
2239 ArgFlags.push_back(Flags);
2242 // Handle the arguments now that we've gotten them.
2243 SmallVector<unsigned, 4> RegArgs;
2245 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2246 RegArgs, CC, NumBytes, false))
2249 unsigned CalleeReg = 0;
2250 if (EnableARMLongCalls) {
2251 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2252 if (CalleeReg == 0) return false;
2256 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2257 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2258 DbgLoc, TII.get(CallOpc));
2259 // BL / BLX don't take a predicate, but tBL / tBLX do.
2261 AddDefaultPred(MIB);
2262 if (EnableARMLongCalls)
2263 MIB.addReg(CalleeReg);
2265 MIB.addExternalSymbol(TLI.getLibcallName(Call));
2267 // Add implicit physical register uses to the call.
2268 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2269 MIB.addReg(RegArgs[i], RegState::Implicit);
2271 // Add a register mask with the call-preserved registers.
2272 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2273 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2275 // Finish off the call including any return values.
2276 SmallVector<unsigned, 4> UsedRegs;
2277 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
2279 // Set all unused physreg defs as dead.
2280 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2285 bool ARMFastISel::SelectCall(const Instruction *I,
2286 const char *IntrMemName = nullptr) {
2287 const CallInst *CI = cast<CallInst>(I);
2288 const Value *Callee = CI->getCalledValue();
2290 // Can't handle inline asm.
2291 if (isa<InlineAsm>(Callee)) return false;
2293 // Allow SelectionDAG isel to handle tail calls.
2294 if (CI->isTailCall()) return false;
2296 // Check the calling convention.
2297 ImmutableCallSite CS(CI);
2298 CallingConv::ID CC = CS.getCallingConv();
2300 // TODO: Avoid some calling conventions?
2302 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2303 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
2304 bool isVarArg = FTy->isVarArg();
2306 // Handle *simple* calls for now.
2307 Type *RetTy = I->getType();
2309 if (RetTy->isVoidTy())
2310 RetVT = MVT::isVoid;
2311 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2312 RetVT != MVT::i8 && RetVT != MVT::i1)
2315 // Can't handle non-double multi-reg retvals.
2316 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2317 RetVT != MVT::i16 && RetVT != MVT::i32) {
2318 SmallVector<CCValAssign, 16> RVLocs;
2319 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
2320 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
2321 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2325 // Set up the argument vectors.
2326 SmallVector<Value*, 8> Args;
2327 SmallVector<unsigned, 8> ArgRegs;
2328 SmallVector<MVT, 8> ArgVTs;
2329 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2330 unsigned arg_size = CS.arg_size();
2331 Args.reserve(arg_size);
2332 ArgRegs.reserve(arg_size);
2333 ArgVTs.reserve(arg_size);
2334 ArgFlags.reserve(arg_size);
2335 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2337 // If we're lowering a memory intrinsic instead of a regular call, skip the
2338 // last two arguments, which shouldn't be passed to the underlying function.
2339 if (IntrMemName && e-i <= 2)
2342 ISD::ArgFlagsTy Flags;
2343 unsigned AttrInd = i - CS.arg_begin() + 1;
2344 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2346 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2349 // FIXME: Only handle *easy* calls for now.
2350 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2351 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2352 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2353 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2356 Type *ArgTy = (*i)->getType();
2358 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2362 unsigned Arg = getRegForValue(*i);
2366 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
2367 Flags.setOrigAlign(OriginalAlignment);
2370 ArgRegs.push_back(Arg);
2371 ArgVTs.push_back(ArgVT);
2372 ArgFlags.push_back(Flags);
2375 // Handle the arguments now that we've gotten them.
2376 SmallVector<unsigned, 4> RegArgs;
2378 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2379 RegArgs, CC, NumBytes, isVarArg))
2382 bool UseReg = false;
2383 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
2384 if (!GV || EnableARMLongCalls) UseReg = true;
2386 unsigned CalleeReg = 0;
2389 CalleeReg = getLibcallReg(IntrMemName);
2391 CalleeReg = getRegForValue(Callee);
2393 if (CalleeReg == 0) return false;
2397 unsigned CallOpc = ARMSelectCallOp(UseReg);
2398 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2399 DbgLoc, TII.get(CallOpc));
2401 unsigned char OpFlags = 0;
2403 // Add MO_PLT for global address or external symbol in the PIC relocation
2405 if (Subtarget->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_)
2406 OpFlags = ARMII::MO_PLT;
2408 // ARM calls don't take a predicate, but tBL / tBLX do.
2410 AddDefaultPred(MIB);
2412 MIB.addReg(CalleeReg);
2413 else if (!IntrMemName)
2414 MIB.addGlobalAddress(GV, 0, OpFlags);
2416 MIB.addExternalSymbol(IntrMemName, OpFlags);
2418 // Add implicit physical register uses to the call.
2419 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2420 MIB.addReg(RegArgs[i], RegState::Implicit);
2422 // Add a register mask with the call-preserved registers.
2423 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2424 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2426 // Finish off the call including any return values.
2427 SmallVector<unsigned, 4> UsedRegs;
2428 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2431 // Set all unused physreg defs as dead.
2432 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2437 bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
2441 bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
2442 uint64_t Len, unsigned Alignment) {
2443 // Make sure we don't bloat code by inlining very large memcpy's.
2444 if (!ARMIsMemCpySmall(Len))
2449 if (!Alignment || Alignment >= 4) {
2455 assert (Len == 1 && "Expected a length of 1!");
2459 // Bound based on alignment.
2460 if (Len >= 2 && Alignment == 2)
2469 RV = ARMEmitLoad(VT, ResultReg, Src);
2470 assert (RV == true && "Should be able to handle this load.");
2471 RV = ARMEmitStore(VT, ResultReg, Dest);
2472 assert (RV == true && "Should be able to handle this store.");
2475 unsigned Size = VT.getSizeInBits()/8;
2477 Dest.Offset += Size;
2484 bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2485 // FIXME: Handle more intrinsics.
2486 switch (I.getIntrinsicID()) {
2487 default: return false;
2488 case Intrinsic::frameaddress: {
2489 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2490 MFI->setFrameAddressIsTaken(true);
2493 const TargetRegisterClass *RC;
2495 LdrOpc = ARM::t2LDRi12;
2496 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2498 LdrOpc = ARM::LDRi12;
2499 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2502 const ARMBaseRegisterInfo *RegInfo =
2503 static_cast<const ARMBaseRegisterInfo *>(
2504 TM.getSubtargetImpl()->getRegisterInfo());
2505 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2506 unsigned SrcReg = FramePtr;
2508 // Recursively load frame address
2514 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2516 DestReg = createResultReg(RC);
2517 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2518 TII.get(LdrOpc), DestReg)
2519 .addReg(SrcReg).addImm(0));
2522 UpdateValueMap(&I, SrcReg);
2525 case Intrinsic::memcpy:
2526 case Intrinsic::memmove: {
2527 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2528 // Don't handle volatile.
2529 if (MTI.isVolatile())
2532 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2533 // we would emit dead code because we don't currently handle memmoves.
2534 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2535 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
2536 // Small memcpy's are common enough that we want to do them without a call
2538 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
2539 if (ARMIsMemCpySmall(Len)) {
2541 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2542 !ARMComputeAddress(MTI.getRawSource(), Src))
2544 unsigned Alignment = MTI.getAlignment();
2545 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
2550 if (!MTI.getLength()->getType()->isIntegerTy(32))
2553 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2556 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2557 return SelectCall(&I, IntrMemName);
2559 case Intrinsic::memset: {
2560 const MemSetInst &MSI = cast<MemSetInst>(I);
2561 // Don't handle volatile.
2562 if (MSI.isVolatile())
2565 if (!MSI.getLength()->getType()->isIntegerTy(32))
2568 if (MSI.getDestAddressSpace() > 255)
2571 return SelectCall(&I, "memset");
2573 case Intrinsic::trap: {
2574 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(
2575 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
2581 bool ARMFastISel::SelectTrunc(const Instruction *I) {
2582 // The high bits for a type smaller than the register size are assumed to be
2584 Value *Op = I->getOperand(0);
2587 SrcVT = TLI.getValueType(Op->getType(), true);
2588 DestVT = TLI.getValueType(I->getType(), true);
2590 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2592 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2595 unsigned SrcReg = getRegForValue(Op);
2596 if (!SrcReg) return false;
2598 // Because the high bits are undefined, a truncate doesn't generate
2600 UpdateValueMap(I, SrcReg);
2604 unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
2606 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
2608 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
2611 // Table of which combinations can be emitted as a single instruction,
2612 // and which will require two.
2613 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2615 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2616 // ext: s z s z s z s z
2617 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2618 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2619 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2622 // Target registers for:
2623 // - For ARM can never be PC.
2624 // - For 16-bit Thumb are restricted to lower 8 registers.
2625 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2626 static const TargetRegisterClass *RCTbl[2][2] = {
2627 // Instructions: Two Single
2628 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2629 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2632 // Table governing the instruction(s) to be emitted.
2633 static const struct InstructionTable {
2635 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2636 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2637 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2638 } IT[2][2][3][2] = {
2639 { // Two instructions (first is left shift, second is in this table).
2640 { // ARM Opc S Shift Imm
2641 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2642 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2643 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2644 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2645 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2646 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
2648 { // Thumb Opc S Shift Imm
2649 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2650 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2651 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2652 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2653 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2654 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
2657 { // Single instruction.
2658 { // ARM Opc S Shift Imm
2659 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2660 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2661 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2662 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2663 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2664 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
2666 { // Thumb Opc S Shift Imm
2667 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2668 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2669 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2670 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2671 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2672 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
2677 unsigned SrcBits = SrcVT.getSizeInBits();
2678 unsigned DestBits = DestVT.getSizeInBits();
2680 assert((SrcBits < DestBits) && "can only extend to larger types");
2681 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2682 "other sizes unimplemented");
2683 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2684 "other sizes unimplemented");
2686 bool hasV6Ops = Subtarget->hasV6Ops();
2687 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
2688 assert((Bitness < 3) && "sanity-check table bounds");
2690 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2691 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
2692 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2693 unsigned Opc = ITP->Opc;
2694 assert(ARM::KILL != Opc && "Invalid table entry");
2695 unsigned hasS = ITP->hasS;
2696 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2697 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2698 "only MOVsi has shift operand addressing mode");
2699 unsigned Imm = ITP->Imm;
2701 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2702 bool setsCPSR = &ARM::tGPRRegClass == RC;
2703 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
2705 // MOVsi encodes shift and immediate in shift operand addressing mode.
2706 // The following condition has the same value when emitting two
2707 // instruction sequences: both are shifts.
2708 bool ImmIsSO = (Shift != ARM_AM::no_shift);
2710 // Either one or two instructions are emitted.
2711 // They're always of the form:
2713 // CPSR is set only by 16-bit Thumb instructions.
2714 // Predicate, if any, is AL.
2715 // S bit, if available, is always 0.
2716 // When two are emitted the first's result will feed as the second's input,
2717 // that value is then dead.
2718 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2719 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2720 ResultReg = createResultReg(RC);
2721 bool isLsl = (0 == Instr) && !isSingleInstr;
2722 unsigned Opcode = isLsl ? LSLOpc : Opc;
2723 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2724 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
2725 bool isKill = 1 == Instr;
2726 MachineInstrBuilder MIB = BuildMI(
2727 *FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opcode), ResultReg);
2729 MIB.addReg(ARM::CPSR, RegState::Define);
2730 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
2731 AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc));
2734 // Second instruction consumes the first's result.
2741 bool ARMFastISel::SelectIntExt(const Instruction *I) {
2742 // On ARM, in general, integer casts don't involve legal types; this code
2743 // handles promotable integers.
2744 Type *DestTy = I->getType();
2745 Value *Src = I->getOperand(0);
2746 Type *SrcTy = Src->getType();
2748 bool isZExt = isa<ZExtInst>(I);
2749 unsigned SrcReg = getRegForValue(Src);
2750 if (!SrcReg) return false;
2752 EVT SrcEVT, DestEVT;
2753 SrcEVT = TLI.getValueType(SrcTy, true);
2754 DestEVT = TLI.getValueType(DestTy, true);
2755 if (!SrcEVT.isSimple()) return false;
2756 if (!DestEVT.isSimple()) return false;
2758 MVT SrcVT = SrcEVT.getSimpleVT();
2759 MVT DestVT = DestEVT.getSimpleVT();
2760 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2761 if (ResultReg == 0) return false;
2762 UpdateValueMap(I, ResultReg);
2766 bool ARMFastISel::SelectShift(const Instruction *I,
2767 ARM_AM::ShiftOpc ShiftTy) {
2768 // We handle thumb2 mode by target independent selector
2769 // or SelectionDAG ISel.
2773 // Only handle i32 now.
2774 EVT DestVT = TLI.getValueType(I->getType(), true);
2775 if (DestVT != MVT::i32)
2778 unsigned Opc = ARM::MOVsr;
2780 Value *Src2Value = I->getOperand(1);
2781 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2782 ShiftImm = CI->getZExtValue();
2784 // Fall back to selection DAG isel if the shift amount
2785 // is zero or greater than the width of the value type.
2786 if (ShiftImm == 0 || ShiftImm >=32)
2792 Value *Src1Value = I->getOperand(0);
2793 unsigned Reg1 = getRegForValue(Src1Value);
2794 if (Reg1 == 0) return false;
2797 if (Opc == ARM::MOVsr) {
2798 Reg2 = getRegForValue(Src2Value);
2799 if (Reg2 == 0) return false;
2802 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
2803 if(ResultReg == 0) return false;
2805 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2806 TII.get(Opc), ResultReg)
2809 if (Opc == ARM::MOVsi)
2810 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2811 else if (Opc == ARM::MOVsr) {
2813 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2816 AddOptionalDefs(MIB);
2817 UpdateValueMap(I, ResultReg);
2821 // TODO: SoftFP support.
2822 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
2824 switch (I->getOpcode()) {
2825 case Instruction::Load:
2826 return SelectLoad(I);
2827 case Instruction::Store:
2828 return SelectStore(I);
2829 case Instruction::Br:
2830 return SelectBranch(I);
2831 case Instruction::IndirectBr:
2832 return SelectIndirectBr(I);
2833 case Instruction::ICmp:
2834 case Instruction::FCmp:
2835 return SelectCmp(I);
2836 case Instruction::FPExt:
2837 return SelectFPExt(I);
2838 case Instruction::FPTrunc:
2839 return SelectFPTrunc(I);
2840 case Instruction::SIToFP:
2841 return SelectIToFP(I, /*isSigned*/ true);
2842 case Instruction::UIToFP:
2843 return SelectIToFP(I, /*isSigned*/ false);
2844 case Instruction::FPToSI:
2845 return SelectFPToI(I, /*isSigned*/ true);
2846 case Instruction::FPToUI:
2847 return SelectFPToI(I, /*isSigned*/ false);
2848 case Instruction::Add:
2849 return SelectBinaryIntOp(I, ISD::ADD);
2850 case Instruction::Or:
2851 return SelectBinaryIntOp(I, ISD::OR);
2852 case Instruction::Sub:
2853 return SelectBinaryIntOp(I, ISD::SUB);
2854 case Instruction::FAdd:
2855 return SelectBinaryFPOp(I, ISD::FADD);
2856 case Instruction::FSub:
2857 return SelectBinaryFPOp(I, ISD::FSUB);
2858 case Instruction::FMul:
2859 return SelectBinaryFPOp(I, ISD::FMUL);
2860 case Instruction::SDiv:
2861 return SelectDiv(I, /*isSigned*/ true);
2862 case Instruction::UDiv:
2863 return SelectDiv(I, /*isSigned*/ false);
2864 case Instruction::SRem:
2865 return SelectRem(I, /*isSigned*/ true);
2866 case Instruction::URem:
2867 return SelectRem(I, /*isSigned*/ false);
2868 case Instruction::Call:
2869 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2870 return SelectIntrinsicCall(*II);
2871 return SelectCall(I);
2872 case Instruction::Select:
2873 return SelectSelect(I);
2874 case Instruction::Ret:
2875 return SelectRet(I);
2876 case Instruction::Trunc:
2877 return SelectTrunc(I);
2878 case Instruction::ZExt:
2879 case Instruction::SExt:
2880 return SelectIntExt(I);
2881 case Instruction::Shl:
2882 return SelectShift(I, ARM_AM::lsl);
2883 case Instruction::LShr:
2884 return SelectShift(I, ARM_AM::lsr);
2885 case Instruction::AShr:
2886 return SelectShift(I, ARM_AM::asr);
2893 // This table describes sign- and zero-extend instructions which can be
2894 // folded into a preceding load. All of these extends have an immediate
2895 // (sometimes a mask and sometimes a shift) that's applied after
2897 const struct FoldableLoadExtendsStruct {
2898 uint16_t Opc[2]; // ARM, Thumb.
2899 uint8_t ExpectedImm;
2901 uint8_t ExpectedVT : 7;
2902 } FoldableLoadExtends[] = {
2903 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2904 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2905 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2906 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2907 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2911 /// \brief The specified machine instr operand is a vreg, and that
2912 /// vreg is being provided by the specified load instruction. If possible,
2913 /// try to fold the load as an operand to the instruction, returning true if
2915 bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2916 const LoadInst *LI) {
2917 // Verify we have a legal type before going any further.
2919 if (!isLoadTypeLegal(LI->getType(), VT))
2922 // Combine load followed by zero- or sign-extend.
2923 // ldrb r1, [r0] ldrb r1, [r0]
2925 // mov r3, r2 mov r3, r1
2926 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
2928 const uint64_t Imm = MI->getOperand(2).getImm();
2932 for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends);
2934 if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() &&
2935 (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm &&
2936 MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) {
2938 isZExt = FoldableLoadExtends[i].isZExt;
2941 if (!Found) return false;
2943 // See if we can handle this address.
2945 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2947 unsigned ResultReg = MI->getOperand(0).getReg();
2948 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
2950 MI->eraseFromParent();
2954 unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
2955 unsigned Align, MVT VT) {
2956 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
2957 ARMConstantPoolConstant *CPV =
2958 ARMConstantPoolConstant::Create(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
2959 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
2962 unsigned DestReg1 = createResultReg(TLI.getRegClassFor(VT));
2965 DestReg1 = constrainOperandRegClass(TII.get(ARM::t2LDRpci), DestReg1, 0);
2966 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2967 TII.get(ARM::t2LDRpci), DestReg1)
2968 .addConstantPoolIndex(Idx));
2969 Opc = UseGOTOFF ? ARM::t2ADDrr : ARM::t2LDRs;
2971 // The extra immediate is for addrmode2.
2972 DestReg1 = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg1, 0);
2973 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2974 DbgLoc, TII.get(ARM::LDRcp), DestReg1)
2975 .addConstantPoolIndex(Idx).addImm(0));
2976 Opc = UseGOTOFF ? ARM::ADDrr : ARM::LDRrs;
2979 unsigned GlobalBaseReg = AFI->getGlobalBaseReg();
2980 if (GlobalBaseReg == 0) {
2981 GlobalBaseReg = MRI.createVirtualRegister(TLI.getRegClassFor(VT));
2982 AFI->setGlobalBaseReg(GlobalBaseReg);
2985 unsigned DestReg2 = createResultReg(TLI.getRegClassFor(VT));
2986 DestReg2 = constrainOperandRegClass(TII.get(Opc), DestReg2, 0);
2987 DestReg1 = constrainOperandRegClass(TII.get(Opc), DestReg1, 1);
2988 GlobalBaseReg = constrainOperandRegClass(TII.get(Opc), GlobalBaseReg, 2);
2989 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2990 DbgLoc, TII.get(Opc), DestReg2)
2992 .addReg(GlobalBaseReg);
2995 AddOptionalDefs(MIB);
3000 bool ARMFastISel::FastLowerArguments() {
3001 if (!FuncInfo.CanLowerReturn)
3004 const Function *F = FuncInfo.Fn;
3008 CallingConv::ID CC = F->getCallingConv();
3012 case CallingConv::Fast:
3013 case CallingConv::C:
3014 case CallingConv::ARM_AAPCS_VFP:
3015 case CallingConv::ARM_AAPCS:
3016 case CallingConv::ARM_APCS:
3020 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
3021 // which are passed in r0 - r3.
3023 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3024 I != E; ++I, ++Idx) {
3028 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
3029 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
3030 F->getAttributes().hasAttribute(Idx, Attribute::ByVal))
3033 Type *ArgTy = I->getType();
3034 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3037 EVT ArgVT = TLI.getValueType(ArgTy);
3038 if (!ArgVT.isSimple()) return false;
3039 switch (ArgVT.getSimpleVT().SimpleTy) {
3050 static const uint16_t GPRArgRegs[] = {
3051 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3054 const TargetRegisterClass *RC = &ARM::rGPRRegClass;
3056 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3057 I != E; ++I, ++Idx) {
3058 unsigned SrcReg = GPRArgRegs[Idx];
3059 unsigned 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 unsigned ResultReg = createResultReg(RC);
3064 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3065 TII.get(TargetOpcode::COPY),
3066 ResultReg).addReg(DstReg, getKillRegState(true));
3067 UpdateValueMap(I, ResultReg);
3074 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
3075 const TargetLibraryInfo *libInfo) {
3076 const TargetMachine &TM = funcInfo.MF->getTarget();
3078 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
3079 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
3080 bool UseFastISel = false;
3081 UseFastISel |= Subtarget->isTargetMachO() && !Subtarget->isThumb1Only();
3082 UseFastISel |= Subtarget->isTargetLinux() && !Subtarget->isThumb();
3083 UseFastISel |= Subtarget->isTargetNaCl() && !Subtarget->isThumb();
3086 // iOS always has a FP for backtracking, force other targets
3087 // to keep their FP when doing FastISel. The emitted code is
3088 // currently superior, and in cases like test-suite's lencod
3089 // FastISel isn't quite correct when FP is eliminated.
3090 TM.Options.NoFramePointerElim = true;
3091 return new ARMFastISel(funcInfo, libInfo);