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 "ARMBaseInstrInfo.h"
18 #include "ARMCallingConv.h"
19 #include "ARMRegisterInfo.h"
20 #include "ARMTargetMachine.h"
21 #include "ARMSubtarget.h"
22 #include "ARMConstantPoolValue.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/IntrinsicInst.h"
28 #include "llvm/Module.h"
29 #include "llvm/CodeGen/Analysis.h"
30 #include "llvm/CodeGen/FastISel.h"
31 #include "llvm/CodeGen/FunctionLoweringInfo.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/MachineModuleInfo.h"
34 #include "llvm/CodeGen/MachineConstantPool.h"
35 #include "llvm/CodeGen/MachineFrameInfo.h"
36 #include "llvm/CodeGen/MachineMemOperand.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/PseudoSourceValue.h"
39 #include "llvm/Support/CallSite.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/GetElementPtrTypeIterator.h"
43 #include "llvm/Target/TargetData.h"
44 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetLowering.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Target/TargetOptions.h"
51 DisableARMFastISel("disable-arm-fast-isel",
52 cl::desc("Turn off experimental ARM fast-isel support"),
53 cl::init(false), cl::Hidden);
57 // All possible address modes, plus some.
58 typedef struct Address {
73 // Innocuous defaults for our address.
75 : BaseType(RegBase), Offset(0), Scale(0), PlusReg(0) {
80 class ARMFastISel : public FastISel {
82 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
83 /// make the right decision when generating code for different targets.
84 const ARMSubtarget *Subtarget;
85 const TargetMachine &TM;
86 const TargetInstrInfo &TII;
87 const TargetLowering &TLI;
90 // Convenience variables to avoid some queries.
95 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
97 TM(funcInfo.MF->getTarget()),
98 TII(*TM.getInstrInfo()),
99 TLI(*TM.getTargetLowering()) {
100 Subtarget = &TM.getSubtarget<ARMSubtarget>();
101 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
102 isThumb = AFI->isThumbFunction();
103 Context = &funcInfo.Fn->getContext();
106 // Code from FastISel.cpp.
107 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
108 const TargetRegisterClass *RC);
109 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC,
111 unsigned Op0, bool Op0IsKill);
112 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
113 const TargetRegisterClass *RC,
114 unsigned Op0, bool Op0IsKill,
115 unsigned Op1, bool Op1IsKill);
116 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
117 const TargetRegisterClass *RC,
118 unsigned Op0, bool Op0IsKill,
120 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
121 const TargetRegisterClass *RC,
122 unsigned Op0, bool Op0IsKill,
123 const ConstantFP *FPImm);
124 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
125 const TargetRegisterClass *RC,
127 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
128 const TargetRegisterClass *RC,
129 unsigned Op0, bool Op0IsKill,
130 unsigned Op1, bool Op1IsKill,
132 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
133 unsigned Op0, bool Op0IsKill,
136 // Backend specific FastISel code.
137 virtual bool TargetSelectInstruction(const Instruction *I);
138 virtual unsigned TargetMaterializeConstant(const Constant *C);
139 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
141 #include "ARMGenFastISel.inc"
143 // Instruction selection routines.
145 bool SelectLoad(const Instruction *I);
146 bool SelectStore(const Instruction *I);
147 bool SelectBranch(const Instruction *I);
148 bool SelectCmp(const Instruction *I);
149 bool SelectFPExt(const Instruction *I);
150 bool SelectFPTrunc(const Instruction *I);
151 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
152 bool SelectSIToFP(const Instruction *I);
153 bool SelectFPToSI(const Instruction *I);
154 bool SelectSDiv(const Instruction *I);
155 bool SelectSRem(const Instruction *I);
156 bool SelectCall(const Instruction *I);
157 bool SelectSelect(const Instruction *I);
158 bool SelectRet(const Instruction *I);
162 bool isTypeLegal(const Type *Ty, MVT &VT);
163 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
164 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
165 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
166 bool ARMComputeAddress(const Value *Obj, Address &Addr);
167 void ARMSimplifyAddress(Address &Addr, EVT VT);
168 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
169 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
170 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
171 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
172 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
174 // Call handling routines.
176 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
177 unsigned &ResultReg);
178 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
179 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
180 SmallVectorImpl<unsigned> &ArgRegs,
181 SmallVectorImpl<MVT> &ArgVTs,
182 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
183 SmallVectorImpl<unsigned> &RegArgs,
186 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
187 const Instruction *I, CallingConv::ID CC,
189 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
191 // OptionalDef handling routines.
193 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
194 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
195 void AddLoadStoreOperands(EVT VT, Address &Addr,
196 const MachineInstrBuilder &MIB);
199 } // end anonymous namespace
201 #include "ARMGenCallingConv.inc"
203 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
204 // we don't care about implicit defs here, just places we'll need to add a
205 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
206 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
207 const TargetInstrDesc &TID = MI->getDesc();
208 if (!TID.hasOptionalDef())
211 // Look to see if our OptionalDef is defining CPSR or CCR.
212 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
213 const MachineOperand &MO = MI->getOperand(i);
214 if (!MO.isReg() || !MO.isDef()) continue;
215 if (MO.getReg() == ARM::CPSR)
221 // If the machine is predicable go ahead and add the predicate operands, if
222 // it needs default CC operands add those.
223 // TODO: If we want to support thumb1 then we'll need to deal with optional
224 // CPSR defs that need to be added before the remaining operands. See s_cc_out
225 // for descriptions why.
226 const MachineInstrBuilder &
227 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
228 MachineInstr *MI = &*MIB;
230 // Do we use a predicate?
231 if (TII.isPredicable(MI))
234 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
235 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
237 if (DefinesOptionalPredicate(MI, &CPSR)) {
246 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
247 const TargetRegisterClass* RC) {
248 unsigned ResultReg = createResultReg(RC);
249 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
251 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
255 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
256 const TargetRegisterClass *RC,
257 unsigned Op0, bool Op0IsKill) {
258 unsigned ResultReg = createResultReg(RC);
259 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
261 if (II.getNumDefs() >= 1)
262 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
263 .addReg(Op0, Op0IsKill * RegState::Kill));
265 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
266 .addReg(Op0, Op0IsKill * RegState::Kill));
267 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
268 TII.get(TargetOpcode::COPY), ResultReg)
269 .addReg(II.ImplicitDefs[0]));
274 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
275 const TargetRegisterClass *RC,
276 unsigned Op0, bool Op0IsKill,
277 unsigned Op1, bool Op1IsKill) {
278 unsigned ResultReg = createResultReg(RC);
279 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
281 if (II.getNumDefs() >= 1)
282 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
283 .addReg(Op0, Op0IsKill * RegState::Kill)
284 .addReg(Op1, Op1IsKill * RegState::Kill));
286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
287 .addReg(Op0, Op0IsKill * RegState::Kill)
288 .addReg(Op1, Op1IsKill * RegState::Kill));
289 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
290 TII.get(TargetOpcode::COPY), ResultReg)
291 .addReg(II.ImplicitDefs[0]));
296 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
297 const TargetRegisterClass *RC,
298 unsigned Op0, bool Op0IsKill,
300 unsigned ResultReg = createResultReg(RC);
301 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
303 if (II.getNumDefs() >= 1)
304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
305 .addReg(Op0, Op0IsKill * RegState::Kill)
308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
309 .addReg(Op0, Op0IsKill * RegState::Kill)
311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
312 TII.get(TargetOpcode::COPY), ResultReg)
313 .addReg(II.ImplicitDefs[0]));
318 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
319 const TargetRegisterClass *RC,
320 unsigned Op0, bool Op0IsKill,
321 const ConstantFP *FPImm) {
322 unsigned ResultReg = createResultReg(RC);
323 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
325 if (II.getNumDefs() >= 1)
326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
327 .addReg(Op0, Op0IsKill * RegState::Kill)
330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
331 .addReg(Op0, Op0IsKill * RegState::Kill)
333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
334 TII.get(TargetOpcode::COPY), ResultReg)
335 .addReg(II.ImplicitDefs[0]));
340 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
341 const TargetRegisterClass *RC,
342 unsigned Op0, bool Op0IsKill,
343 unsigned Op1, bool Op1IsKill,
345 unsigned ResultReg = createResultReg(RC);
346 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
348 if (II.getNumDefs() >= 1)
349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
350 .addReg(Op0, Op0IsKill * RegState::Kill)
351 .addReg(Op1, Op1IsKill * RegState::Kill)
354 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
355 .addReg(Op0, Op0IsKill * RegState::Kill)
356 .addReg(Op1, Op1IsKill * RegState::Kill)
358 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
359 TII.get(TargetOpcode::COPY), ResultReg)
360 .addReg(II.ImplicitDefs[0]));
365 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
366 const TargetRegisterClass *RC,
368 unsigned ResultReg = createResultReg(RC);
369 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
371 if (II.getNumDefs() >= 1)
372 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
375 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
378 TII.get(TargetOpcode::COPY), ResultReg)
379 .addReg(II.ImplicitDefs[0]));
384 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
385 unsigned Op0, bool Op0IsKill,
387 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
388 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
389 "Cannot yet extract from physregs");
390 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
391 DL, TII.get(TargetOpcode::COPY), ResultReg)
392 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
396 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
397 // checks from the various callers.
398 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
399 if (VT == MVT::f64) return 0;
401 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
402 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
403 TII.get(ARM::VMOVRS), MoveReg)
408 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
409 if (VT == MVT::i64) return 0;
411 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
412 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
413 TII.get(ARM::VMOVSR), MoveReg)
418 // For double width floating point we need to materialize two constants
419 // (the high and the low) into integer registers then use a move to get
420 // the combined constant into an FP reg.
421 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
422 const APFloat Val = CFP->getValueAPF();
423 bool is64bit = VT == MVT::f64;
425 // This checks to see if we can use VFP3 instructions to materialize
426 // a constant, otherwise we have to go through the constant pool.
427 if (TLI.isFPImmLegal(Val, VT)) {
428 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
429 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
430 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
436 // Require VFP2 for loading fp constants.
437 if (!Subtarget->hasVFP2()) return false;
439 // MachineConstantPool wants an explicit alignment.
440 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
442 // TODO: Figure out if this is correct.
443 Align = TD.getTypeAllocSize(CFP->getType());
445 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
446 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
447 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
449 // The extra reg is for addrmode5.
450 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
452 .addConstantPoolIndex(Idx)
457 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
459 // For now 32-bit only.
460 if (VT != MVT::i32) return false;
462 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
464 // If we can do this in a single instruction without a constant pool entry
466 const ConstantInt *CI = cast<ConstantInt>(C);
467 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
468 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
469 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
470 TII.get(Opc), DestReg)
471 .addImm(CI->getSExtValue()));
475 // MachineConstantPool wants an explicit alignment.
476 unsigned Align = TD.getPrefTypeAlignment(C->getType());
478 // TODO: Figure out if this is correct.
479 Align = TD.getTypeAllocSize(C->getType());
481 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
484 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
485 TII.get(ARM::t2LDRpci), DestReg)
486 .addConstantPoolIndex(Idx));
488 // The extra immediate is for addrmode2.
489 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
490 TII.get(ARM::LDRcp), DestReg)
491 .addConstantPoolIndex(Idx)
497 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
498 // For now 32-bit only.
499 if (VT != MVT::i32) return 0;
501 Reloc::Model RelocM = TM.getRelocationModel();
503 // TODO: No external globals for now.
504 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
506 // TODO: Need more magic for ARM PIC.
507 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
509 // MachineConstantPool wants an explicit alignment.
510 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
512 // TODO: Figure out if this is correct.
513 Align = TD.getTypeAllocSize(GV->getType());
517 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
518 unsigned Id = AFI->createConstPoolEntryUId();
519 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
520 ARMCP::CPValue, PCAdj);
521 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
524 MachineInstrBuilder MIB;
525 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
527 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
528 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
529 .addConstantPoolIndex(Idx);
530 if (RelocM == Reloc::PIC_)
533 // The extra immediate is for addrmode2.
534 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
536 .addConstantPoolIndex(Idx)
539 AddOptionalDefs(MIB);
543 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
544 EVT VT = TLI.getValueType(C->getType(), true);
546 // Only handle simple types.
547 if (!VT.isSimple()) return 0;
549 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
550 return ARMMaterializeFP(CFP, VT);
551 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
552 return ARMMaterializeGV(GV, VT);
553 else if (isa<ConstantInt>(C))
554 return ARMMaterializeInt(C, VT);
559 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
560 // Don't handle dynamic allocas.
561 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
564 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
566 DenseMap<const AllocaInst*, int>::iterator SI =
567 FuncInfo.StaticAllocaMap.find(AI);
569 // This will get lowered later into the correct offsets and registers
570 // via rewriteXFrameIndex.
571 if (SI != FuncInfo.StaticAllocaMap.end()) {
572 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
573 unsigned ResultReg = createResultReg(RC);
574 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
575 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
576 TII.get(Opc), ResultReg)
577 .addFrameIndex(SI->second)
585 bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
586 EVT evt = TLI.getValueType(Ty, true);
588 // Only handle simple types.
589 if (evt == MVT::Other || !evt.isSimple()) return false;
590 VT = evt.getSimpleVT();
592 // Handle all legal types, i.e. a register that will directly hold this
594 return TLI.isTypeLegal(VT);
597 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
598 if (isTypeLegal(Ty, VT)) return true;
600 // If this is a type than can be sign or zero-extended to a basic operation
601 // go ahead and accept it now.
602 if (VT == MVT::i8 || VT == MVT::i16)
608 // Computes the address to get to an object.
609 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
610 // Some boilerplate from the X86 FastISel.
611 const User *U = NULL;
612 unsigned Opcode = Instruction::UserOp1;
613 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
614 // Don't walk into other basic blocks unless the object is an alloca from
615 // another block, otherwise it may not have a virtual register assigned.
616 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
617 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
618 Opcode = I->getOpcode();
621 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
622 Opcode = C->getOpcode();
626 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
627 if (Ty->getAddressSpace() > 255)
628 // Fast instruction selection doesn't support the special
635 case Instruction::BitCast: {
636 // Look through bitcasts.
637 return ARMComputeAddress(U->getOperand(0), Addr);
639 case Instruction::IntToPtr: {
640 // Look past no-op inttoptrs.
641 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
642 return ARMComputeAddress(U->getOperand(0), Addr);
645 case Instruction::PtrToInt: {
646 // Look past no-op ptrtoints.
647 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
648 return ARMComputeAddress(U->getOperand(0), Addr);
651 case Instruction::GetElementPtr: {
652 Address SavedAddr = Addr;
653 int TmpOffset = Addr.Offset;
655 // Iterate through the GEP folding the constants into offsets where
657 gep_type_iterator GTI = gep_type_begin(U);
658 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
659 i != e; ++i, ++GTI) {
660 const Value *Op = *i;
661 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
662 const StructLayout *SL = TD.getStructLayout(STy);
663 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
664 TmpOffset += SL->getElementOffset(Idx);
666 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
667 SmallVector<const Value *, 4> Worklist;
668 Worklist.push_back(Op);
670 Op = Worklist.pop_back_val();
671 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
672 // Constant-offset addressing.
673 TmpOffset += CI->getSExtValue() * S;
674 } else if (isa<AddOperator>(Op) &&
675 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
676 // An add with a constant operand. Fold the constant.
678 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
679 TmpOffset += CI->getSExtValue() * S;
680 // Add the other operand back to the work list.
681 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
683 goto unsupported_gep;
684 } while (!Worklist.empty());
688 // Try to grab the base operand now.
689 Addr.Offset = TmpOffset;
690 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
692 // We failed, restore everything and try the other options.
698 case Instruction::Alloca: {
699 const AllocaInst *AI = cast<AllocaInst>(Obj);
700 DenseMap<const AllocaInst*, int>::iterator SI =
701 FuncInfo.StaticAllocaMap.find(AI);
702 if (SI != FuncInfo.StaticAllocaMap.end()) {
703 Addr.BaseType = Address::FrameIndexBase;
704 Addr.Base.FI = SI->second;
711 // Materialize the global variable's address into a reg which can
712 // then be used later to load the variable.
713 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
714 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
715 if (Tmp == 0) return false;
721 // Try to get this in a register if nothing else has worked.
722 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
723 return Addr.Base.Reg != 0;
726 void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
728 assert(VT.isSimple() && "Non-simple types are invalid here!");
730 bool needsLowering = false;
731 switch (VT.getSimpleVT().SimpleTy) {
733 assert(false && "Unhandled load/store type!");
738 // Integer loads/stores handle 12-bit offsets.
739 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
743 // Floating point operands handle 8-bit offsets.
744 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
748 // If this is a stack pointer and the offset needs to be simplified then
749 // put the alloca address into a register, set the base type back to
750 // register and continue. This should almost never happen.
751 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
752 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
753 ARM::GPRRegisterClass;
754 unsigned ResultReg = createResultReg(RC);
755 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
756 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
757 TII.get(Opc), ResultReg)
758 .addFrameIndex(Addr.Base.FI)
760 Addr.Base.Reg = ResultReg;
761 Addr.BaseType = Address::RegBase;
764 // Since the offset is too large for the load/store instruction
765 // get the reg+offset into a register.
767 ARMCC::CondCodes Pred = ARMCC::AL;
768 unsigned PredReg = 0;
770 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
771 ARM::GPRRegisterClass;
772 unsigned BaseReg = createResultReg(RC);
775 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
776 BaseReg, Addr.Base.Reg, Addr.Offset,
778 static_cast<const ARMBaseInstrInfo&>(TII));
780 assert(AFI->isThumb2Function());
781 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
782 BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg,
783 static_cast<const ARMBaseInstrInfo&>(TII));
786 Addr.Base.Reg = BaseReg;
790 void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
791 const MachineInstrBuilder &MIB) {
792 // addrmode5 output depends on the selection dag addressing dividing the
793 // offset by 4 that it then later multiplies. Do this here as well.
794 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
795 VT.getSimpleVT().SimpleTy == MVT::f64)
798 // Frame base works a bit differently. Handle it separately.
799 if (Addr.BaseType == Address::FrameIndexBase) {
800 int FI = Addr.Base.FI;
801 int Offset = Addr.Offset;
802 MachineMemOperand *MMO =
803 FuncInfo.MF->getMachineMemOperand(
804 MachinePointerInfo::getFixedStack(FI, Offset),
805 MachineMemOperand::MOLoad,
806 MFI.getObjectSize(FI),
807 MFI.getObjectAlignment(FI));
808 // Now add the rest of the operands.
809 MIB.addFrameIndex(FI);
811 // ARM halfword load/stores need an additional operand.
812 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
814 MIB.addImm(Addr.Offset);
815 MIB.addMemOperand(MMO);
817 // Now add the rest of the operands.
818 MIB.addReg(Addr.Base.Reg);
820 // ARM halfword load/stores need an additional operand.
821 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
823 MIB.addImm(Addr.Offset);
825 AddOptionalDefs(MIB);
828 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
830 assert(VT.isSimple() && "Non-simple types are invalid here!");
832 TargetRegisterClass *RC;
833 switch (VT.getSimpleVT().SimpleTy) {
834 // This is mostly going to be Neon/vector support.
835 default: return false;
837 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
838 RC = ARM::GPRRegisterClass;
841 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
842 RC = ARM::GPRRegisterClass;
845 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
846 RC = ARM::GPRRegisterClass;
850 RC = TLI.getRegClassFor(VT);
854 RC = TLI.getRegClassFor(VT);
857 // Simplify this down to something we can handle.
858 ARMSimplifyAddress(Addr, VT);
860 // Create the base instruction, then add the operands.
861 ResultReg = createResultReg(RC);
862 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
863 TII.get(Opc), ResultReg);
864 AddLoadStoreOperands(VT, Addr, MIB);
868 bool ARMFastISel::SelectLoad(const Instruction *I) {
869 // Verify we have a legal type before going any further.
871 if (!isLoadTypeLegal(I->getType(), VT))
874 // See if we can handle this address.
876 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
879 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
880 UpdateValueMap(I, ResultReg);
884 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
886 switch (VT.getSimpleVT().SimpleTy) {
887 // This is mostly going to be Neon/vector support.
888 default: return false;
890 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
891 ARM::GPRRegisterClass);
892 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
893 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
895 .addReg(SrcReg).addImm(1));
897 } // Fallthrough here.
899 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
902 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
905 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
908 if (!Subtarget->hasVFP2()) return false;
912 if (!Subtarget->hasVFP2()) return false;
916 // Simplify this down to something we can handle.
917 ARMSimplifyAddress(Addr, VT);
919 // Create the base instruction, then add the operands.
920 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
922 .addReg(SrcReg, getKillRegState(true));
923 AddLoadStoreOperands(VT, Addr, MIB);
927 bool ARMFastISel::SelectStore(const Instruction *I) {
928 Value *Op0 = I->getOperand(0);
931 // Verify we have a legal type before going any further.
933 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
936 // Get the value to be stored into a register.
937 SrcReg = getRegForValue(Op0);
938 if (SrcReg == 0) return false;
940 // See if we can handle this address.
942 if (!ARMComputeAddress(I->getOperand(1), Addr))
945 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
949 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
951 // Needs two compares...
952 case CmpInst::FCMP_ONE:
953 case CmpInst::FCMP_UEQ:
955 // AL is our "false" for now. The other two need more compares.
957 case CmpInst::ICMP_EQ:
958 case CmpInst::FCMP_OEQ:
960 case CmpInst::ICMP_SGT:
961 case CmpInst::FCMP_OGT:
963 case CmpInst::ICMP_SGE:
964 case CmpInst::FCMP_OGE:
966 case CmpInst::ICMP_UGT:
967 case CmpInst::FCMP_UGT:
969 case CmpInst::FCMP_OLT:
971 case CmpInst::ICMP_ULE:
972 case CmpInst::FCMP_OLE:
974 case CmpInst::FCMP_ORD:
976 case CmpInst::FCMP_UNO:
978 case CmpInst::FCMP_UGE:
980 case CmpInst::ICMP_SLT:
981 case CmpInst::FCMP_ULT:
983 case CmpInst::ICMP_SLE:
984 case CmpInst::FCMP_ULE:
986 case CmpInst::FCMP_UNE:
987 case CmpInst::ICMP_NE:
989 case CmpInst::ICMP_UGE:
991 case CmpInst::ICMP_ULT:
996 bool ARMFastISel::SelectBranch(const Instruction *I) {
997 const BranchInst *BI = cast<BranchInst>(I);
998 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
999 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1001 // Simple branch support.
1003 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1005 // TODO: Factor this out.
1006 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1007 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1009 const Type *Ty = CI->getOperand(0)->getType();
1010 if (!isTypeLegal(Ty, VT))
1013 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1014 if (isFloat && !Subtarget->hasVFP2())
1019 switch (VT.SimpleTy) {
1020 default: return false;
1021 // TODO: Verify compares.
1023 CmpOpc = ARM::VCMPES;
1024 CondReg = ARM::FPSCR;
1027 CmpOpc = ARM::VCMPED;
1028 CondReg = ARM::FPSCR;
1031 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
1032 CondReg = ARM::CPSR;
1036 // Get the compare predicate.
1037 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1039 // We may not handle every CC for now.
1040 if (ARMPred == ARMCC::AL) return false;
1042 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1043 if (Arg1 == 0) return false;
1045 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1046 if (Arg2 == 0) return false;
1048 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1050 .addReg(Arg1).addReg(Arg2));
1052 // For floating point we need to move the result to a comparison register
1053 // that we can then use for branches.
1055 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1056 TII.get(ARM::FMSTAT)));
1058 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1059 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1060 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1061 FastEmitBranch(FBB, DL);
1062 FuncInfo.MBB->addSuccessor(TBB);
1067 unsigned CmpReg = getRegForValue(BI->getCondition());
1068 if (CmpReg == 0) return false;
1070 // Re-set the flags just in case.
1071 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1072 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1073 .addReg(CmpReg).addImm(0));
1075 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1076 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1077 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
1078 FastEmitBranch(FBB, DL);
1079 FuncInfo.MBB->addSuccessor(TBB);
1083 bool ARMFastISel::SelectCmp(const Instruction *I) {
1084 const CmpInst *CI = cast<CmpInst>(I);
1087 const Type *Ty = CI->getOperand(0)->getType();
1088 if (!isTypeLegal(Ty, VT))
1091 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1092 if (isFloat && !Subtarget->hasVFP2())
1097 switch (VT.SimpleTy) {
1098 default: return false;
1099 // TODO: Verify compares.
1101 CmpOpc = ARM::VCMPES;
1102 CondReg = ARM::FPSCR;
1105 CmpOpc = ARM::VCMPED;
1106 CondReg = ARM::FPSCR;
1109 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
1110 CondReg = ARM::CPSR;
1114 // Get the compare predicate.
1115 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1117 // We may not handle every CC for now.
1118 if (ARMPred == ARMCC::AL) return false;
1120 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1121 if (Arg1 == 0) return false;
1123 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1124 if (Arg2 == 0) return false;
1126 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1127 .addReg(Arg1).addReg(Arg2));
1129 // For floating point we need to move the result to a comparison register
1130 // that we can then use for branches.
1132 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1133 TII.get(ARM::FMSTAT)));
1135 // Now set a register based on the comparison. Explicitly set the predicates
1137 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
1138 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
1139 : ARM::GPRRegisterClass;
1140 unsigned DestReg = createResultReg(RC);
1142 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1143 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1144 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1145 .addReg(ZeroReg).addImm(1)
1146 .addImm(ARMPred).addReg(CondReg);
1148 UpdateValueMap(I, DestReg);
1152 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1153 // Make sure we have VFP and that we're extending float to double.
1154 if (!Subtarget->hasVFP2()) return false;
1156 Value *V = I->getOperand(0);
1157 if (!I->getType()->isDoubleTy() ||
1158 !V->getType()->isFloatTy()) return false;
1160 unsigned Op = getRegForValue(V);
1161 if (Op == 0) return false;
1163 unsigned Result = createResultReg(ARM::DPRRegisterClass);
1164 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1165 TII.get(ARM::VCVTDS), Result)
1167 UpdateValueMap(I, Result);
1171 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1172 // Make sure we have VFP and that we're truncating double to float.
1173 if (!Subtarget->hasVFP2()) return false;
1175 Value *V = I->getOperand(0);
1176 if (!(I->getType()->isFloatTy() &&
1177 V->getType()->isDoubleTy())) return false;
1179 unsigned Op = getRegForValue(V);
1180 if (Op == 0) return false;
1182 unsigned Result = createResultReg(ARM::SPRRegisterClass);
1183 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1184 TII.get(ARM::VCVTSD), Result)
1186 UpdateValueMap(I, Result);
1190 bool ARMFastISel::SelectSIToFP(const Instruction *I) {
1191 // Make sure we have VFP.
1192 if (!Subtarget->hasVFP2()) return false;
1195 const Type *Ty = I->getType();
1196 if (!isTypeLegal(Ty, DstVT))
1199 unsigned Op = getRegForValue(I->getOperand(0));
1200 if (Op == 0) return false;
1202 // The conversion routine works on fp-reg to fp-reg and the operand above
1203 // was an integer, move it to the fp registers if possible.
1204 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
1205 if (FP == 0) return false;
1208 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1209 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1212 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1213 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1216 UpdateValueMap(I, ResultReg);
1220 bool ARMFastISel::SelectFPToSI(const Instruction *I) {
1221 // Make sure we have VFP.
1222 if (!Subtarget->hasVFP2()) return false;
1225 const Type *RetTy = I->getType();
1226 if (!isTypeLegal(RetTy, DstVT))
1229 unsigned Op = getRegForValue(I->getOperand(0));
1230 if (Op == 0) return false;
1233 const Type *OpTy = I->getOperand(0)->getType();
1234 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1235 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1238 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1239 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1240 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1244 // This result needs to be in an integer register, but the conversion only
1245 // takes place in fp-regs.
1246 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1247 if (IntReg == 0) return false;
1249 UpdateValueMap(I, IntReg);
1253 bool ARMFastISel::SelectSelect(const Instruction *I) {
1255 if (!isTypeLegal(I->getType(), VT))
1258 // Things need to be register sized for register moves.
1259 if (VT != MVT::i32) return false;
1260 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1262 unsigned CondReg = getRegForValue(I->getOperand(0));
1263 if (CondReg == 0) return false;
1264 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1265 if (Op1Reg == 0) return false;
1266 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1267 if (Op2Reg == 0) return false;
1269 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1270 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1271 .addReg(CondReg).addImm(1));
1272 unsigned ResultReg = createResultReg(RC);
1273 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1275 .addReg(Op1Reg).addReg(Op2Reg)
1276 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1277 UpdateValueMap(I, ResultReg);
1281 bool ARMFastISel::SelectSDiv(const Instruction *I) {
1283 const Type *Ty = I->getType();
1284 if (!isTypeLegal(Ty, VT))
1287 // If we have integer div support we should have selected this automagically.
1288 // In case we have a real miss go ahead and return false and we'll pick
1290 if (Subtarget->hasDivide()) return false;
1292 // Otherwise emit a libcall.
1293 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1295 LC = RTLIB::SDIV_I8;
1296 else if (VT == MVT::i16)
1297 LC = RTLIB::SDIV_I16;
1298 else if (VT == MVT::i32)
1299 LC = RTLIB::SDIV_I32;
1300 else if (VT == MVT::i64)
1301 LC = RTLIB::SDIV_I64;
1302 else if (VT == MVT::i128)
1303 LC = RTLIB::SDIV_I128;
1304 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1306 return ARMEmitLibcall(I, LC);
1309 bool ARMFastISel::SelectSRem(const Instruction *I) {
1311 const Type *Ty = I->getType();
1312 if (!isTypeLegal(Ty, VT))
1315 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1317 LC = RTLIB::SREM_I8;
1318 else if (VT == MVT::i16)
1319 LC = RTLIB::SREM_I16;
1320 else if (VT == MVT::i32)
1321 LC = RTLIB::SREM_I32;
1322 else if (VT == MVT::i64)
1323 LC = RTLIB::SREM_I64;
1324 else if (VT == MVT::i128)
1325 LC = RTLIB::SREM_I128;
1326 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1328 return ARMEmitLibcall(I, LC);
1331 bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
1332 EVT VT = TLI.getValueType(I->getType(), true);
1334 // We can get here in the case when we want to use NEON for our fp
1335 // operations, but can't figure out how to. Just use the vfp instructions
1337 // FIXME: It'd be nice to use NEON instructions.
1338 const Type *Ty = I->getType();
1339 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1340 if (isFloat && !Subtarget->hasVFP2())
1343 unsigned Op1 = getRegForValue(I->getOperand(0));
1344 if (Op1 == 0) return false;
1346 unsigned Op2 = getRegForValue(I->getOperand(1));
1347 if (Op2 == 0) return false;
1350 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1351 switch (ISDOpcode) {
1352 default: return false;
1354 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1357 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1360 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1363 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1364 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1365 TII.get(Opc), ResultReg)
1366 .addReg(Op1).addReg(Op2));
1367 UpdateValueMap(I, ResultReg);
1371 // Call Handling Code
1373 bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1374 EVT SrcVT, unsigned &ResultReg) {
1375 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1376 Src, /*TODO: Kill=*/false);
1385 // This is largely taken directly from CCAssignFnForNode - we don't support
1386 // varargs in FastISel so that part has been removed.
1387 // TODO: We may not support all of this.
1388 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1391 llvm_unreachable("Unsupported calling convention");
1392 case CallingConv::Fast:
1393 // Ignore fastcc. Silence compiler warnings.
1394 (void)RetFastCC_ARM_APCS;
1395 (void)FastCC_ARM_APCS;
1397 case CallingConv::C:
1398 // Use target triple & subtarget features to do actual dispatch.
1399 if (Subtarget->isAAPCS_ABI()) {
1400 if (Subtarget->hasVFP2() &&
1401 FloatABIType == FloatABI::Hard)
1402 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1404 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1406 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1407 case CallingConv::ARM_AAPCS_VFP:
1408 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1409 case CallingConv::ARM_AAPCS:
1410 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1411 case CallingConv::ARM_APCS:
1412 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1416 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1417 SmallVectorImpl<unsigned> &ArgRegs,
1418 SmallVectorImpl<MVT> &ArgVTs,
1419 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1420 SmallVectorImpl<unsigned> &RegArgs,
1422 unsigned &NumBytes) {
1423 SmallVector<CCValAssign, 16> ArgLocs;
1424 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1425 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1427 // Get a count of how many bytes are to be pushed on the stack.
1428 NumBytes = CCInfo.getNextStackOffset();
1430 // Issue CALLSEQ_START
1431 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1432 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1433 TII.get(AdjStackDown))
1436 // Process the args.
1437 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1438 CCValAssign &VA = ArgLocs[i];
1439 unsigned Arg = ArgRegs[VA.getValNo()];
1440 MVT ArgVT = ArgVTs[VA.getValNo()];
1442 // We don't handle NEON parameters yet.
1443 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() > 64)
1446 // Handle arg promotion, etc.
1447 switch (VA.getLocInfo()) {
1448 case CCValAssign::Full: break;
1449 case CCValAssign::SExt: {
1450 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1452 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
1454 ArgVT = VA.getLocVT();
1457 case CCValAssign::ZExt: {
1458 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1460 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
1462 ArgVT = VA.getLocVT();
1465 case CCValAssign::AExt: {
1466 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1469 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1472 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1475 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
1476 ArgVT = VA.getLocVT();
1479 case CCValAssign::BCvt: {
1480 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
1481 /*TODO: Kill=*/false);
1482 assert(BC != 0 && "Failed to emit a bitcast!");
1484 ArgVT = VA.getLocVT();
1487 default: llvm_unreachable("Unknown arg promotion!");
1490 // Now copy/store arg to correct locations.
1491 if (VA.isRegLoc() && !VA.needsCustom()) {
1492 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1495 RegArgs.push_back(VA.getLocReg());
1496 } else if (VA.needsCustom()) {
1497 // TODO: We need custom lowering for vector (v2f64) args.
1498 if (VA.getLocVT() != MVT::f64) return false;
1500 CCValAssign &NextVA = ArgLocs[++i];
1502 // TODO: Only handle register args for now.
1503 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1505 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1506 TII.get(ARM::VMOVRRD), VA.getLocReg())
1507 .addReg(NextVA.getLocReg(), RegState::Define)
1509 RegArgs.push_back(VA.getLocReg());
1510 RegArgs.push_back(NextVA.getLocReg());
1512 assert(VA.isMemLoc());
1513 // Need to store on the stack.
1515 Addr.BaseType = Address::RegBase;
1516 Addr.Base.Reg = ARM::SP;
1517 Addr.Offset = VA.getLocMemOffset();
1519 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
1525 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1526 const Instruction *I, CallingConv::ID CC,
1527 unsigned &NumBytes) {
1528 // Issue CALLSEQ_END
1529 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1530 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1531 TII.get(AdjStackUp))
1532 .addImm(NumBytes).addImm(0));
1534 // Now the return value.
1535 if (RetVT != MVT::isVoid) {
1536 SmallVector<CCValAssign, 16> RVLocs;
1537 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1538 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1540 // Copy all of the result registers out of their specified physreg.
1541 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
1542 // For this move we copy into two registers and then move into the
1543 // double fp reg we want.
1544 EVT DestVT = RVLocs[0].getValVT();
1545 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1546 unsigned ResultReg = createResultReg(DstRC);
1547 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1548 TII.get(ARM::VMOVDRR), ResultReg)
1549 .addReg(RVLocs[0].getLocReg())
1550 .addReg(RVLocs[1].getLocReg()));
1552 UsedRegs.push_back(RVLocs[0].getLocReg());
1553 UsedRegs.push_back(RVLocs[1].getLocReg());
1555 // Finally update the result.
1556 UpdateValueMap(I, ResultReg);
1558 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
1559 EVT CopyVT = RVLocs[0].getValVT();
1560 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1562 unsigned ResultReg = createResultReg(DstRC);
1563 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1564 ResultReg).addReg(RVLocs[0].getLocReg());
1565 UsedRegs.push_back(RVLocs[0].getLocReg());
1567 // Finally update the result.
1568 UpdateValueMap(I, ResultReg);
1575 bool ARMFastISel::SelectRet(const Instruction *I) {
1576 const ReturnInst *Ret = cast<ReturnInst>(I);
1577 const Function &F = *I->getParent()->getParent();
1579 if (!FuncInfo.CanLowerReturn)
1585 CallingConv::ID CC = F.getCallingConv();
1586 if (Ret->getNumOperands() > 0) {
1587 SmallVector<ISD::OutputArg, 4> Outs;
1588 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1591 // Analyze operands of the call, assigning locations to each operand.
1592 SmallVector<CCValAssign, 16> ValLocs;
1593 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1594 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1596 const Value *RV = Ret->getOperand(0);
1597 unsigned Reg = getRegForValue(RV);
1601 // Only handle a single return value for now.
1602 if (ValLocs.size() != 1)
1605 CCValAssign &VA = ValLocs[0];
1607 // Don't bother handling odd stuff for now.
1608 if (VA.getLocInfo() != CCValAssign::Full)
1610 // Only handle register returns for now.
1613 // TODO: For now, don't try to handle cases where getLocInfo()
1614 // says Full but the types don't match.
1615 if (TLI.getValueType(RV->getType()) != VA.getValVT())
1619 unsigned SrcReg = Reg + VA.getValNo();
1620 unsigned DstReg = VA.getLocReg();
1621 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1622 // Avoid a cross-class copy. This is very unlikely.
1623 if (!SrcRC->contains(DstReg))
1625 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1626 DstReg).addReg(SrcReg);
1628 // Mark the register as live out of the function.
1629 MRI.addLiveOut(VA.getLocReg());
1632 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1633 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1638 // A quick function that will emit a call for a named libcall in F with the
1639 // vector of passed arguments for the Instruction in I. We can assume that we
1640 // can emit a call for any libcall we can produce. This is an abridged version
1641 // of the full call infrastructure since we won't need to worry about things
1642 // like computed function pointers or strange arguments at call sites.
1643 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
1645 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1646 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1648 // Handle *simple* calls for now.
1649 const Type *RetTy = I->getType();
1651 if (RetTy->isVoidTy())
1652 RetVT = MVT::isVoid;
1653 else if (!isTypeLegal(RetTy, RetVT))
1656 // For now we're using BLX etc on the assumption that we have v5t ops.
1657 if (!Subtarget->hasV5TOps()) return false;
1659 // Set up the argument vectors.
1660 SmallVector<Value*, 8> Args;
1661 SmallVector<unsigned, 8> ArgRegs;
1662 SmallVector<MVT, 8> ArgVTs;
1663 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1664 Args.reserve(I->getNumOperands());
1665 ArgRegs.reserve(I->getNumOperands());
1666 ArgVTs.reserve(I->getNumOperands());
1667 ArgFlags.reserve(I->getNumOperands());
1668 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1669 Value *Op = I->getOperand(i);
1670 unsigned Arg = getRegForValue(Op);
1671 if (Arg == 0) return false;
1673 const Type *ArgTy = Op->getType();
1675 if (!isTypeLegal(ArgTy, ArgVT)) return false;
1677 ISD::ArgFlagsTy Flags;
1678 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1679 Flags.setOrigAlign(OriginalAlignment);
1682 ArgRegs.push_back(Arg);
1683 ArgVTs.push_back(ArgVT);
1684 ArgFlags.push_back(Flags);
1687 // Handle the arguments now that we've gotten them.
1688 SmallVector<unsigned, 4> RegArgs;
1690 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1693 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1694 // TODO: Turn this into the table of arm call ops.
1695 MachineInstrBuilder MIB;
1698 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1700 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1701 // Explicitly adding the predicate here.
1702 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1704 .addExternalSymbol(TLI.getLibcallName(Call));
1706 // Add implicit physical register uses to the call.
1707 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1708 MIB.addReg(RegArgs[i]);
1710 // Finish off the call including any return values.
1711 SmallVector<unsigned, 4> UsedRegs;
1712 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1714 // Set all unused physreg defs as dead.
1715 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1720 bool ARMFastISel::SelectCall(const Instruction *I) {
1721 const CallInst *CI = cast<CallInst>(I);
1722 const Value *Callee = CI->getCalledValue();
1724 // Can't handle inline asm or worry about intrinsics yet.
1725 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1727 // Only handle global variable Callees that are direct calls.
1728 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1729 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1732 // Check the calling convention.
1733 ImmutableCallSite CS(CI);
1734 CallingConv::ID CC = CS.getCallingConv();
1736 // TODO: Avoid some calling conventions?
1738 // Let SDISel handle vararg functions.
1739 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1740 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1741 if (FTy->isVarArg())
1744 // Handle *simple* calls for now.
1745 const Type *RetTy = I->getType();
1747 if (RetTy->isVoidTy())
1748 RetVT = MVT::isVoid;
1749 else if (!isTypeLegal(RetTy, RetVT))
1752 // For now we're using BLX etc on the assumption that we have v5t ops.
1754 if (!Subtarget->hasV5TOps()) return false;
1756 // Set up the argument vectors.
1757 SmallVector<Value*, 8> Args;
1758 SmallVector<unsigned, 8> ArgRegs;
1759 SmallVector<MVT, 8> ArgVTs;
1760 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1761 Args.reserve(CS.arg_size());
1762 ArgRegs.reserve(CS.arg_size());
1763 ArgVTs.reserve(CS.arg_size());
1764 ArgFlags.reserve(CS.arg_size());
1765 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1767 unsigned Arg = getRegForValue(*i);
1771 ISD::ArgFlagsTy Flags;
1772 unsigned AttrInd = i - CS.arg_begin() + 1;
1773 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1775 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1778 // FIXME: Only handle *easy* calls for now.
1779 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1780 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1781 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1782 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1785 const Type *ArgTy = (*i)->getType();
1787 if (!isTypeLegal(ArgTy, ArgVT))
1789 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1790 Flags.setOrigAlign(OriginalAlignment);
1793 ArgRegs.push_back(Arg);
1794 ArgVTs.push_back(ArgVT);
1795 ArgFlags.push_back(Flags);
1798 // Handle the arguments now that we've gotten them.
1799 SmallVector<unsigned, 4> RegArgs;
1801 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1804 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1805 // TODO: Turn this into the table of arm call ops.
1806 MachineInstrBuilder MIB;
1809 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1811 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1812 // Explicitly adding the predicate here.
1813 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1815 .addGlobalAddress(GV, 0, 0);
1817 // Add implicit physical register uses to the call.
1818 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1819 MIB.addReg(RegArgs[i]);
1821 // Finish off the call including any return values.
1822 SmallVector<unsigned, 4> UsedRegs;
1823 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1825 // Set all unused physreg defs as dead.
1826 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1832 // TODO: SoftFP support.
1833 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
1835 switch (I->getOpcode()) {
1836 case Instruction::Load:
1837 return SelectLoad(I);
1838 case Instruction::Store:
1839 return SelectStore(I);
1840 case Instruction::Br:
1841 return SelectBranch(I);
1842 case Instruction::ICmp:
1843 case Instruction::FCmp:
1844 return SelectCmp(I);
1845 case Instruction::FPExt:
1846 return SelectFPExt(I);
1847 case Instruction::FPTrunc:
1848 return SelectFPTrunc(I);
1849 case Instruction::SIToFP:
1850 return SelectSIToFP(I);
1851 case Instruction::FPToSI:
1852 return SelectFPToSI(I);
1853 case Instruction::FAdd:
1854 return SelectBinaryOp(I, ISD::FADD);
1855 case Instruction::FSub:
1856 return SelectBinaryOp(I, ISD::FSUB);
1857 case Instruction::FMul:
1858 return SelectBinaryOp(I, ISD::FMUL);
1859 case Instruction::SDiv:
1860 return SelectSDiv(I);
1861 case Instruction::SRem:
1862 return SelectSRem(I);
1863 case Instruction::Call:
1864 return SelectCall(I);
1865 case Instruction::Select:
1866 return SelectSelect(I);
1867 case Instruction::Ret:
1868 return SelectRet(I);
1875 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
1876 // Completely untested on non-darwin.
1877 const TargetMachine &TM = funcInfo.MF->getTarget();
1879 // Darwin and thumb1 only for now.
1880 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
1881 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
1882 !DisableARMFastISel)
1883 return new ARMFastISel(funcInfo);