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/MachineRegisterInfo.h"
37 #include "llvm/Support/CallSite.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/GetElementPtrTypeIterator.h"
41 #include "llvm/Target/TargetData.h"
42 #include "llvm/Target/TargetInstrInfo.h"
43 #include "llvm/Target/TargetLowering.h"
44 #include "llvm/Target/TargetMachine.h"
45 #include "llvm/Target/TargetOptions.h"
49 EnableARMFastISel("arm-fast-isel",
50 cl::desc("Turn on experimental ARM fast-isel support"),
51 cl::init(false), cl::Hidden);
55 class ARMFastISel : public FastISel {
57 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
58 /// make the right decision when generating code for different targets.
59 const ARMSubtarget *Subtarget;
60 const TargetMachine &TM;
61 const TargetInstrInfo &TII;
62 const TargetLowering &TLI;
65 // Convenience variables to avoid some queries.
70 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
72 TM(funcInfo.MF->getTarget()),
73 TII(*TM.getInstrInfo()),
74 TLI(*TM.getTargetLowering()) {
75 Subtarget = &TM.getSubtarget<ARMSubtarget>();
76 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
77 isThumb = AFI->isThumbFunction();
78 Context = &funcInfo.Fn->getContext();
81 // Code from FastISel.cpp.
82 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
83 const TargetRegisterClass *RC);
84 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
85 const TargetRegisterClass *RC,
86 unsigned Op0, bool Op0IsKill);
87 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
88 const TargetRegisterClass *RC,
89 unsigned Op0, bool Op0IsKill,
90 unsigned Op1, bool Op1IsKill);
91 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
92 const TargetRegisterClass *RC,
93 unsigned Op0, bool Op0IsKill,
95 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
96 const TargetRegisterClass *RC,
97 unsigned Op0, bool Op0IsKill,
98 const ConstantFP *FPImm);
99 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
100 const TargetRegisterClass *RC,
102 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
103 const TargetRegisterClass *RC,
104 unsigned Op0, bool Op0IsKill,
105 unsigned Op1, bool Op1IsKill,
107 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
108 unsigned Op0, bool Op0IsKill,
111 // Backend specific FastISel code.
112 virtual bool TargetSelectInstruction(const Instruction *I);
113 virtual unsigned TargetMaterializeConstant(const Constant *C);
114 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
116 #include "ARMGenFastISel.inc"
118 // Instruction selection routines.
120 virtual bool SelectLoad(const Instruction *I);
121 virtual bool SelectStore(const Instruction *I);
122 virtual bool SelectBranch(const Instruction *I);
123 virtual bool SelectCmp(const Instruction *I);
124 virtual bool SelectFPExt(const Instruction *I);
125 virtual bool SelectFPTrunc(const Instruction *I);
126 virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
127 virtual bool SelectSIToFP(const Instruction *I);
128 virtual bool SelectFPToSI(const Instruction *I);
129 virtual bool SelectSDiv(const Instruction *I);
130 virtual bool SelectSRem(const Instruction *I);
131 virtual bool SelectCall(const Instruction *I);
132 virtual bool SelectSelect(const Instruction *I);
136 bool isTypeLegal(const Type *Ty, EVT &VT);
137 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
138 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
139 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
140 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
141 void ARMSimplifyRegOffset(unsigned &Reg, int &Offset, EVT VT);
142 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
143 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
144 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
145 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
146 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
148 // Call handling routines.
150 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
151 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
152 SmallVectorImpl<unsigned> &ArgRegs,
153 SmallVectorImpl<EVT> &ArgVTs,
154 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
155 SmallVectorImpl<unsigned> &RegArgs,
158 bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
159 const Instruction *I, CallingConv::ID CC,
161 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
163 // OptionalDef handling routines.
165 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
166 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
169 } // end anonymous namespace
171 #include "ARMGenCallingConv.inc"
173 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
174 // we don't care about implicit defs here, just places we'll need to add a
175 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
176 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
177 const TargetInstrDesc &TID = MI->getDesc();
178 if (!TID.hasOptionalDef())
181 // Look to see if our OptionalDef is defining CPSR or CCR.
182 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
183 const MachineOperand &MO = MI->getOperand(i);
184 if (!MO.isReg() || !MO.isDef()) continue;
185 if (MO.getReg() == ARM::CPSR)
191 // If the machine is predicable go ahead and add the predicate operands, if
192 // it needs default CC operands add those.
193 const MachineInstrBuilder &
194 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
195 MachineInstr *MI = &*MIB;
197 // Do we use a predicate?
198 if (TII.isPredicable(MI))
201 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
202 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
204 if (DefinesOptionalPredicate(MI, &CPSR)) {
213 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
214 const TargetRegisterClass* RC) {
215 unsigned ResultReg = createResultReg(RC);
216 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
218 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
222 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
223 const TargetRegisterClass *RC,
224 unsigned Op0, bool Op0IsKill) {
225 unsigned ResultReg = createResultReg(RC);
226 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
228 if (II.getNumDefs() >= 1)
229 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
230 .addReg(Op0, Op0IsKill * RegState::Kill));
232 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
233 .addReg(Op0, Op0IsKill * RegState::Kill));
234 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
235 TII.get(TargetOpcode::COPY), ResultReg)
236 .addReg(II.ImplicitDefs[0]));
241 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
242 const TargetRegisterClass *RC,
243 unsigned Op0, bool Op0IsKill,
244 unsigned Op1, bool Op1IsKill) {
245 unsigned ResultReg = createResultReg(RC);
246 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
248 if (II.getNumDefs() >= 1)
249 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
250 .addReg(Op0, Op0IsKill * RegState::Kill)
251 .addReg(Op1, Op1IsKill * RegState::Kill));
253 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
254 .addReg(Op0, Op0IsKill * RegState::Kill)
255 .addReg(Op1, Op1IsKill * RegState::Kill));
256 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
257 TII.get(TargetOpcode::COPY), ResultReg)
258 .addReg(II.ImplicitDefs[0]));
263 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
264 const TargetRegisterClass *RC,
265 unsigned Op0, bool Op0IsKill,
267 unsigned ResultReg = createResultReg(RC);
268 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
270 if (II.getNumDefs() >= 1)
271 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
272 .addReg(Op0, Op0IsKill * RegState::Kill)
275 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
276 .addReg(Op0, Op0IsKill * RegState::Kill)
278 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
279 TII.get(TargetOpcode::COPY), ResultReg)
280 .addReg(II.ImplicitDefs[0]));
285 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
286 const TargetRegisterClass *RC,
287 unsigned Op0, bool Op0IsKill,
288 const ConstantFP *FPImm) {
289 unsigned ResultReg = createResultReg(RC);
290 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
292 if (II.getNumDefs() >= 1)
293 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
294 .addReg(Op0, Op0IsKill * RegState::Kill)
297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
298 .addReg(Op0, Op0IsKill * RegState::Kill)
300 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
301 TII.get(TargetOpcode::COPY), ResultReg)
302 .addReg(II.ImplicitDefs[0]));
307 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
308 const TargetRegisterClass *RC,
309 unsigned Op0, bool Op0IsKill,
310 unsigned Op1, bool Op1IsKill,
312 unsigned ResultReg = createResultReg(RC);
313 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
315 if (II.getNumDefs() >= 1)
316 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
317 .addReg(Op0, Op0IsKill * RegState::Kill)
318 .addReg(Op1, Op1IsKill * RegState::Kill)
321 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
322 .addReg(Op0, Op0IsKill * RegState::Kill)
323 .addReg(Op1, Op1IsKill * RegState::Kill)
325 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
326 TII.get(TargetOpcode::COPY), ResultReg)
327 .addReg(II.ImplicitDefs[0]));
332 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
333 const TargetRegisterClass *RC,
335 unsigned ResultReg = createResultReg(RC);
336 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
338 if (II.getNumDefs() >= 1)
339 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
342 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
344 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
345 TII.get(TargetOpcode::COPY), ResultReg)
346 .addReg(II.ImplicitDefs[0]));
351 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
352 unsigned Op0, bool Op0IsKill,
354 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
355 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
356 "Cannot yet extract from physregs");
357 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
358 DL, TII.get(TargetOpcode::COPY), ResultReg)
359 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
363 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
364 // checks from the various callers.
365 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
366 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
368 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
369 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
370 TII.get(ARM::VMOVRS), MoveReg)
375 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
376 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
378 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
379 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
380 TII.get(ARM::VMOVSR), MoveReg)
385 // For double width floating point we need to materialize two constants
386 // (the high and the low) into integer registers then use a move to get
387 // the combined constant into an FP reg.
388 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
389 const APFloat Val = CFP->getValueAPF();
390 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
392 // This checks to see if we can use VFP3 instructions to materialize
393 // a constant, otherwise we have to go through the constant pool.
394 if (TLI.isFPImmLegal(Val, VT)) {
395 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
396 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
403 // Require VFP2 for loading fp constants.
404 if (!Subtarget->hasVFP2()) return false;
406 // MachineConstantPool wants an explicit alignment.
407 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
409 // TODO: Figure out if this is correct.
410 Align = TD.getTypeAllocSize(CFP->getType());
412 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
413 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
414 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
416 // The extra reg is for addrmode5.
417 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
419 .addConstantPoolIndex(Idx)
424 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
426 // For now 32-bit only.
427 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
429 // MachineConstantPool wants an explicit alignment.
430 unsigned Align = TD.getPrefTypeAlignment(C->getType());
432 // TODO: Figure out if this is correct.
433 Align = TD.getTypeAllocSize(C->getType());
435 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
436 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
439 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
440 TII.get(ARM::t2LDRpci), DestReg)
441 .addConstantPoolIndex(Idx));
443 // The extra reg and immediate are for addrmode2.
444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
445 TII.get(ARM::LDRcp), DestReg)
446 .addConstantPoolIndex(Idx)
447 .addReg(0).addImm(0));
452 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
453 // For now 32-bit only.
454 if (VT.getSimpleVT().SimpleTy != MVT::i32) return 0;
456 Reloc::Model RelocM = TM.getRelocationModel();
458 // TODO: No external globals for now.
459 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
461 // TODO: Need more magic for ARM PIC.
462 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
464 // MachineConstantPool wants an explicit alignment.
465 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
467 // TODO: Figure out if this is correct.
468 Align = TD.getTypeAllocSize(GV->getType());
472 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
473 unsigned Id = AFI->createConstPoolEntryUId();
474 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
475 ARMCP::CPValue, PCAdj);
476 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
479 MachineInstrBuilder MIB;
480 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
482 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
483 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
484 .addConstantPoolIndex(Idx);
485 if (RelocM == Reloc::PIC_)
488 // The extra reg and immediate are for addrmode2.
489 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
491 .addConstantPoolIndex(Idx)
492 .addReg(0).addImm(0);
494 AddOptionalDefs(MIB);
498 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
499 EVT VT = TLI.getValueType(C->getType(), true);
501 // Only handle simple types.
502 if (!VT.isSimple()) return 0;
504 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
505 return ARMMaterializeFP(CFP, VT);
506 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
507 return ARMMaterializeGV(GV, VT);
508 else if (isa<ConstantInt>(C))
509 return ARMMaterializeInt(C, VT);
514 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
515 // Don't handle dynamic allocas.
516 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
519 if (!isTypeLegal(AI->getType(), VT)) return false;
521 DenseMap<const AllocaInst*, int>::iterator SI =
522 FuncInfo.StaticAllocaMap.find(AI);
524 // This will get lowered later into the correct offsets and registers
525 // via rewriteXFrameIndex.
526 if (SI != FuncInfo.StaticAllocaMap.end()) {
527 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
528 unsigned ResultReg = createResultReg(RC);
529 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
530 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
531 TII.get(Opc), ResultReg)
532 .addFrameIndex(SI->second)
540 bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
541 VT = TLI.getValueType(Ty, true);
543 // Only handle simple types.
544 if (VT == MVT::Other || !VT.isSimple()) return false;
546 // Handle all legal types, i.e. a register that will directly hold this
548 return TLI.isTypeLegal(VT);
551 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
552 if (isTypeLegal(Ty, VT)) return true;
554 // If this is a type than can be sign or zero-extended to a basic operation
555 // go ahead and accept it now.
556 if (VT == MVT::i8 || VT == MVT::i16)
562 // Computes the Reg+Offset to get to an object.
563 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
565 // Some boilerplate from the X86 FastISel.
566 const User *U = NULL;
567 unsigned Opcode = Instruction::UserOp1;
568 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
569 // Don't walk into other basic blocks; it's possible we haven't
570 // visited them yet, so the instructions may not yet be assigned
571 // virtual registers.
572 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
574 Opcode = I->getOpcode();
576 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
577 Opcode = C->getOpcode();
581 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
582 if (Ty->getAddressSpace() > 255)
583 // Fast instruction selection doesn't support the special
590 case Instruction::BitCast: {
591 // Look through bitcasts.
592 return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
594 case Instruction::IntToPtr: {
595 // Look past no-op inttoptrs.
596 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
597 return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
600 case Instruction::PtrToInt: {
601 // Look past no-op ptrtoints.
602 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
603 return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
606 case Instruction::GetElementPtr: {
607 int SavedOffset = Offset;
608 unsigned SavedReg = Reg;
609 int TmpOffset = Offset;
611 // Iterate through the GEP folding the constants into offsets where
613 gep_type_iterator GTI = gep_type_begin(U);
614 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
615 i != e; ++i, ++GTI) {
616 const Value *Op = *i;
617 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
618 const StructLayout *SL = TD.getStructLayout(STy);
619 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
620 TmpOffset += SL->getElementOffset(Idx);
622 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
623 SmallVector<const Value *, 4> Worklist;
624 Worklist.push_back(Op);
626 Op = Worklist.pop_back_val();
627 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
628 // Constant-offset addressing.
629 TmpOffset += CI->getSExtValue() * S;
630 } else if (0 && isa<AddOperator>(Op) &&
631 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
632 // An add with a constant operand. Fold the constant.
634 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
635 TmpOffset += CI->getSExtValue() * S;
636 // Add the other operand back to the work list.
637 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
639 goto unsupported_gep;
640 } while (!Worklist.empty());
644 // Try to grab the base operand now.
646 if (ARMComputeRegOffset(U->getOperand(0), Reg, Offset)) return true;
648 // We failed, restore everything and try the other options.
649 Offset = SavedOffset;
655 case Instruction::Alloca: {
656 // TODO: Fix this to do intermediate loads, etc.
657 if (Offset != 0) return false;
659 const AllocaInst *AI = cast<AllocaInst>(Obj);
660 DenseMap<const AllocaInst*, int>::iterator SI =
661 FuncInfo.StaticAllocaMap.find(AI);
662 if (SI != FuncInfo.StaticAllocaMap.end()) {
667 // Don't handle dynamic allocas.
668 assert(!FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Obj)) &&
669 "Alloca should have been handled earlier!");
674 // Materialize the global variable's address into a reg which can
675 // then be used later to load the variable.
676 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
677 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
678 if (Tmp == 0) return false;
684 // Try to get this in a register if nothing else has worked.
685 if (Reg == 0) Reg = getRegForValue(Obj);
689 void ARMFastISel::ARMSimplifyRegOffset(unsigned &Reg, int &Offset, EVT VT) {
691 // Since the offset may be too large for the load instruction
692 // get the reg+offset into a register.
693 if (Reg != ARM::SP && Offset != 0) {
694 ARMCC::CondCodes Pred = ARMCC::AL;
695 unsigned PredReg = 0;
697 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
698 ARM::GPRRegisterClass;
699 unsigned BaseReg = createResultReg(RC);
702 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
703 BaseReg, Reg, Offset, Pred, PredReg,
704 static_cast<const ARMBaseInstrInfo&>(TII));
706 assert(AFI->isThumb2Function());
707 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
708 BaseReg, Reg, Offset, Pred, PredReg,
709 static_cast<const ARMBaseInstrInfo&>(TII));
716 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
717 unsigned Reg, int Offset) {
719 assert(VT.isSimple() && "Non-simple types are invalid here!");
721 TargetRegisterClass *RC;
722 bool isFloat = false;
723 switch (VT.getSimpleVT().SimpleTy) {
725 // This is mostly going to be Neon/vector support.
728 Opc = isThumb ? ARM::t2LDRHi8 : ARM::LDRH;
729 RC = ARM::GPRRegisterClass;
733 Opc = isThumb ? ARM::t2LDRBi8 : ARM::LDRB;
734 RC = ARM::GPRRegisterClass;
738 Opc = isThumb ? ARM::t2LDRi8 : ARM::LDR;
739 RC = ARM::GPRRegisterClass;
743 RC = TLI.getRegClassFor(VT);
748 RC = TLI.getRegClassFor(VT);
753 ResultReg = createResultReg(RC);
755 // For now with the additions above the offset should be zero - thus we
756 // can always fit into an i8.
757 assert((Reg == ARM::SP || Offset == 0) &&
758 "Offset not zero and not a stack load!");
761 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
762 ResultReg, Offset, RC,
763 TM.getRegisterInfo());
764 // The thumb and floating point instructions both take 2 operands, ARM takes
766 else if (isFloat || isThumb)
767 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
768 TII.get(Opc), ResultReg)
769 .addReg(Reg).addImm(Offset));
771 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
772 TII.get(Opc), ResultReg)
773 .addReg(Reg).addReg(0).addImm(Offset));
777 bool ARMFastISel::SelectLoad(const Instruction *I) {
778 // Verify we have a legal type before going any further.
780 if (!isLoadTypeLegal(I->getType(), VT))
783 // Our register and offset with innocuous defaults.
787 // See if we can handle this as Reg + Offset
788 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
791 ARMSimplifyRegOffset(Reg, Offset, VT);
794 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset)) return false;
796 UpdateValueMap(I, ResultReg);
800 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
801 unsigned DstReg, int Offset) {
803 bool isFloat = false;
804 // VT is set here only for use in the alloca stores below - those are promoted
805 // to reg size always.
806 switch (VT.getSimpleVT().SimpleTy) {
807 default: return false;
811 StrOpc = isThumb ? ARM::t2STRBi8 : ARM::STRB;
815 StrOpc = isThumb ? ARM::t2STRHi8 : ARM::STRH;
817 case MVT::i32: StrOpc = isThumb ? ARM::t2STRi8 : ARM::STR; break;
819 if (!Subtarget->hasVFP2()) return false;
824 if (!Subtarget->hasVFP2()) return false;
830 if (DstReg == ARM::SP)
831 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
832 SrcReg, true /*isKill*/, Offset,
833 TLI.getRegClassFor(VT), TM.getRegisterInfo());
834 // The thumb addressing mode has operands swapped from the arm addressing
835 // mode, the floating point one only has two operands.
836 if (isFloat || isThumb)
837 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
839 .addReg(SrcReg).addReg(DstReg).addImm(Offset));
841 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
843 .addReg(SrcReg).addReg(DstReg).addReg(0).addImm(Offset));
848 bool ARMFastISel::SelectStore(const Instruction *I) {
849 Value *Op0 = I->getOperand(0);
852 // Yay type legalization
854 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
857 // Get the value to be stored into a register.
858 SrcReg = getRegForValue(Op0);
862 // Our register and offset with innocuous defaults.
866 // See if we can handle this as Reg + Offset
867 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
870 ARMSimplifyRegOffset(Reg, Offset, VT);
872 if (!ARMEmitStore(VT, SrcReg, Reg, Offset)) return false;
877 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
879 // Needs two compares...
880 case CmpInst::FCMP_ONE:
881 case CmpInst::FCMP_UEQ:
883 assert(false && "Unhandled CmpInst::Predicate!");
885 case CmpInst::ICMP_EQ:
886 case CmpInst::FCMP_OEQ:
888 case CmpInst::ICMP_SGT:
889 case CmpInst::FCMP_OGT:
891 case CmpInst::ICMP_SGE:
892 case CmpInst::FCMP_OGE:
894 case CmpInst::ICMP_UGT:
895 case CmpInst::FCMP_UGT:
897 case CmpInst::FCMP_OLT:
899 case CmpInst::ICMP_ULE:
900 case CmpInst::FCMP_OLE:
902 case CmpInst::FCMP_ORD:
904 case CmpInst::FCMP_UNO:
906 case CmpInst::FCMP_UGE:
908 case CmpInst::ICMP_SLT:
909 case CmpInst::FCMP_ULT:
911 case CmpInst::ICMP_SLE:
912 case CmpInst::FCMP_ULE:
914 case CmpInst::FCMP_UNE:
915 case CmpInst::ICMP_NE:
917 case CmpInst::ICMP_UGE:
919 case CmpInst::ICMP_ULT:
924 bool ARMFastISel::SelectBranch(const Instruction *I) {
925 const BranchInst *BI = cast<BranchInst>(I);
926 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
927 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
929 // Simple branch support.
930 // TODO: Try to avoid the re-computation in some places.
931 unsigned CondReg = getRegForValue(BI->getCondition());
932 if (CondReg == 0) return false;
934 // Re-set the flags just in case.
935 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
936 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
937 .addReg(CondReg).addImm(1));
939 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
940 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
941 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
942 FastEmitBranch(FBB, DL);
943 FuncInfo.MBB->addSuccessor(TBB);
947 bool ARMFastISel::SelectCmp(const Instruction *I) {
948 const CmpInst *CI = cast<CmpInst>(I);
951 const Type *Ty = CI->getOperand(0)->getType();
952 if (!isTypeLegal(Ty, VT))
955 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
956 if (isFloat && !Subtarget->hasVFP2())
961 switch (VT.getSimpleVT().SimpleTy) {
962 default: return false;
963 // TODO: Verify compares.
965 CmpOpc = ARM::VCMPES;
966 CondReg = ARM::FPSCR;
969 CmpOpc = ARM::VCMPED;
970 CondReg = ARM::FPSCR;
973 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
978 // Get the compare predicate.
979 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
981 // We may not handle every CC for now.
982 if (ARMPred == ARMCC::AL) return false;
984 unsigned Arg1 = getRegForValue(CI->getOperand(0));
985 if (Arg1 == 0) return false;
987 unsigned Arg2 = getRegForValue(CI->getOperand(1));
988 if (Arg2 == 0) return false;
990 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
991 .addReg(Arg1).addReg(Arg2));
993 // For floating point we need to move the result to a comparison register
994 // that we can then use for branches.
996 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
997 TII.get(ARM::FMSTAT)));
999 // Now set a register based on the comparison. Explicitly set the predicates
1001 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
1002 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
1003 : ARM::GPRRegisterClass;
1004 unsigned DestReg = createResultReg(RC);
1006 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1007 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1008 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1009 .addReg(ZeroReg).addImm(1)
1010 .addImm(ARMPred).addReg(CondReg);
1012 UpdateValueMap(I, DestReg);
1016 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1017 // Make sure we have VFP and that we're extending float to double.
1018 if (!Subtarget->hasVFP2()) return false;
1020 Value *V = I->getOperand(0);
1021 if (!I->getType()->isDoubleTy() ||
1022 !V->getType()->isFloatTy()) return false;
1024 unsigned Op = getRegForValue(V);
1025 if (Op == 0) return false;
1027 unsigned Result = createResultReg(ARM::DPRRegisterClass);
1028 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1029 TII.get(ARM::VCVTDS), Result)
1031 UpdateValueMap(I, Result);
1035 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1036 // Make sure we have VFP and that we're truncating double to float.
1037 if (!Subtarget->hasVFP2()) return false;
1039 Value *V = I->getOperand(0);
1040 if (!(I->getType()->isFloatTy() &&
1041 V->getType()->isDoubleTy())) return false;
1043 unsigned Op = getRegForValue(V);
1044 if (Op == 0) return false;
1046 unsigned Result = createResultReg(ARM::SPRRegisterClass);
1047 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1048 TII.get(ARM::VCVTSD), Result)
1050 UpdateValueMap(I, Result);
1054 bool ARMFastISel::SelectSIToFP(const Instruction *I) {
1055 // Make sure we have VFP.
1056 if (!Subtarget->hasVFP2()) return false;
1059 const Type *Ty = I->getType();
1060 if (!isTypeLegal(Ty, DstVT))
1063 unsigned Op = getRegForValue(I->getOperand(0));
1064 if (Op == 0) return false;
1066 // The conversion routine works on fp-reg to fp-reg and the operand above
1067 // was an integer, move it to the fp registers if possible.
1068 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
1069 if (FP == 0) return false;
1072 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1073 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1076 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1077 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1080 UpdateValueMap(I, ResultReg);
1084 bool ARMFastISel::SelectFPToSI(const Instruction *I) {
1085 // Make sure we have VFP.
1086 if (!Subtarget->hasVFP2()) return false;
1089 const Type *RetTy = I->getType();
1090 if (!isTypeLegal(RetTy, DstVT))
1093 unsigned Op = getRegForValue(I->getOperand(0));
1094 if (Op == 0) return false;
1097 const Type *OpTy = I->getOperand(0)->getType();
1098 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1099 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1102 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1103 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1104 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1108 // This result needs to be in an integer register, but the conversion only
1109 // takes place in fp-regs.
1110 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1111 if (IntReg == 0) return false;
1113 UpdateValueMap(I, IntReg);
1117 bool ARMFastISel::SelectSelect(const Instruction *I) {
1118 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
1119 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
1122 // Things need to be register sized for register moves.
1123 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
1124 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1126 unsigned CondReg = getRegForValue(I->getOperand(0));
1127 if (CondReg == 0) return false;
1128 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1129 if (Op1Reg == 0) return false;
1130 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1131 if (Op2Reg == 0) return false;
1133 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1134 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1135 .addReg(CondReg).addImm(1));
1136 unsigned ResultReg = createResultReg(RC);
1137 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1138 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1139 .addReg(Op1Reg).addReg(Op2Reg)
1140 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1141 UpdateValueMap(I, ResultReg);
1145 bool ARMFastISel::SelectSDiv(const Instruction *I) {
1147 const Type *Ty = I->getType();
1148 if (!isTypeLegal(Ty, VT))
1151 // If we have integer div support we should have selected this automagically.
1152 // In case we have a real miss go ahead and return false and we'll pick
1154 if (Subtarget->hasDivide()) return false;
1156 // Otherwise emit a libcall.
1157 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1159 LC = RTLIB::SDIV_I8;
1160 else if (VT == MVT::i16)
1161 LC = RTLIB::SDIV_I16;
1162 else if (VT == MVT::i32)
1163 LC = RTLIB::SDIV_I32;
1164 else if (VT == MVT::i64)
1165 LC = RTLIB::SDIV_I64;
1166 else if (VT == MVT::i128)
1167 LC = RTLIB::SDIV_I128;
1168 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1170 return ARMEmitLibcall(I, LC);
1173 bool ARMFastISel::SelectSRem(const Instruction *I) {
1175 const Type *Ty = I->getType();
1176 if (!isTypeLegal(Ty, VT))
1179 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1181 LC = RTLIB::SREM_I8;
1182 else if (VT == MVT::i16)
1183 LC = RTLIB::SREM_I16;
1184 else if (VT == MVT::i32)
1185 LC = RTLIB::SREM_I32;
1186 else if (VT == MVT::i64)
1187 LC = RTLIB::SREM_I64;
1188 else if (VT == MVT::i128)
1189 LC = RTLIB::SREM_I128;
1190 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1192 return ARMEmitLibcall(I, LC);
1195 bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
1196 EVT VT = TLI.getValueType(I->getType(), true);
1198 // We can get here in the case when we want to use NEON for our fp
1199 // operations, but can't figure out how to. Just use the vfp instructions
1201 // FIXME: It'd be nice to use NEON instructions.
1202 const Type *Ty = I->getType();
1203 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1204 if (isFloat && !Subtarget->hasVFP2())
1207 unsigned Op1 = getRegForValue(I->getOperand(0));
1208 if (Op1 == 0) return false;
1210 unsigned Op2 = getRegForValue(I->getOperand(1));
1211 if (Op2 == 0) return false;
1214 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1215 VT.getSimpleVT().SimpleTy == MVT::i64;
1216 switch (ISDOpcode) {
1217 default: return false;
1219 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1222 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1225 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1228 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1229 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1230 TII.get(Opc), ResultReg)
1231 .addReg(Op1).addReg(Op2));
1232 UpdateValueMap(I, ResultReg);
1236 // Call Handling Code
1238 // This is largely taken directly from CCAssignFnForNode - we don't support
1239 // varargs in FastISel so that part has been removed.
1240 // TODO: We may not support all of this.
1241 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1244 llvm_unreachable("Unsupported calling convention");
1245 case CallingConv::C:
1246 case CallingConv::Fast:
1247 // Use target triple & subtarget features to do actual dispatch.
1248 if (Subtarget->isAAPCS_ABI()) {
1249 if (Subtarget->hasVFP2() &&
1250 FloatABIType == FloatABI::Hard)
1251 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1253 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1255 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1256 case CallingConv::ARM_AAPCS_VFP:
1257 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1258 case CallingConv::ARM_AAPCS:
1259 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1260 case CallingConv::ARM_APCS:
1261 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1265 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1266 SmallVectorImpl<unsigned> &ArgRegs,
1267 SmallVectorImpl<EVT> &ArgVTs,
1268 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1269 SmallVectorImpl<unsigned> &RegArgs,
1271 unsigned &NumBytes) {
1272 SmallVector<CCValAssign, 16> ArgLocs;
1273 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1274 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1276 // Get a count of how many bytes are to be pushed on the stack.
1277 NumBytes = CCInfo.getNextStackOffset();
1279 // Issue CALLSEQ_START
1280 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1281 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1282 TII.get(AdjStackDown))
1285 // Process the args.
1286 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1287 CCValAssign &VA = ArgLocs[i];
1288 unsigned Arg = ArgRegs[VA.getValNo()];
1289 EVT ArgVT = ArgVTs[VA.getValNo()];
1291 // Handle arg promotion, etc.
1292 switch (VA.getLocInfo()) {
1293 case CCValAssign::Full: break;
1295 // TODO: Handle arg promotion.
1299 // Now copy/store arg to correct locations.
1300 // TODO: We need custom lowering for f64 args.
1301 if (VA.isRegLoc() && !VA.needsCustom()) {
1302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1305 RegArgs.push_back(VA.getLocReg());
1315 bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1316 const Instruction *I, CallingConv::ID CC,
1317 unsigned &NumBytes) {
1318 // Issue CALLSEQ_END
1319 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1320 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1321 TII.get(AdjStackUp))
1322 .addImm(NumBytes).addImm(0));
1324 // Now the return value.
1325 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1326 SmallVector<CCValAssign, 16> RVLocs;
1327 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1328 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1330 // Copy all of the result registers out of their specified physreg.
1331 if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1332 // For this move we copy into two registers and then move into the
1333 // double fp reg we want.
1334 // TODO: Are the copies necessary?
1335 TargetRegisterClass *CopyRC = TLI.getRegClassFor(MVT::i32);
1336 unsigned Copy1 = createResultReg(CopyRC);
1337 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1338 Copy1).addReg(RVLocs[0].getLocReg());
1339 UsedRegs.push_back(RVLocs[0].getLocReg());
1341 unsigned Copy2 = createResultReg(CopyRC);
1342 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1343 Copy2).addReg(RVLocs[1].getLocReg());
1344 UsedRegs.push_back(RVLocs[1].getLocReg());
1346 EVT DestVT = RVLocs[0].getValVT();
1347 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1348 unsigned ResultReg = createResultReg(DstRC);
1349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1350 TII.get(ARM::VMOVDRR), ResultReg)
1351 .addReg(Copy1).addReg(Copy2));
1353 // Finally update the result.
1354 UpdateValueMap(I, ResultReg);
1356 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
1357 EVT CopyVT = RVLocs[0].getValVT();
1358 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1360 unsigned ResultReg = createResultReg(DstRC);
1361 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1362 ResultReg).addReg(RVLocs[0].getLocReg());
1363 UsedRegs.push_back(RVLocs[0].getLocReg());
1365 // Finally update the result.
1366 UpdateValueMap(I, ResultReg);
1373 // A quick function that will emit a call for a named libcall in F with the
1374 // vector of passed arguments for the Instruction in I. We can assume that we
1375 // can emit a call for any libcall we can produce. This is an abridged version
1376 // of the full call infrastructure since we won't need to worry about things
1377 // like computed function pointers or strange arguments at call sites.
1378 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
1380 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1381 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1383 // Handle *simple* calls for now.
1384 const Type *RetTy = I->getType();
1386 if (RetTy->isVoidTy())
1387 RetVT = MVT::isVoid;
1388 else if (!isTypeLegal(RetTy, RetVT))
1391 // For now we're using BLX etc on the assumption that we have v5t ops.
1392 if (!Subtarget->hasV5TOps()) return false;
1394 // Set up the argument vectors.
1395 SmallVector<Value*, 8> Args;
1396 SmallVector<unsigned, 8> ArgRegs;
1397 SmallVector<EVT, 8> ArgVTs;
1398 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1399 Args.reserve(I->getNumOperands());
1400 ArgRegs.reserve(I->getNumOperands());
1401 ArgVTs.reserve(I->getNumOperands());
1402 ArgFlags.reserve(I->getNumOperands());
1403 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1404 Value *Op = I->getOperand(i);
1405 unsigned Arg = getRegForValue(Op);
1406 if (Arg == 0) return false;
1408 const Type *ArgTy = Op->getType();
1410 if (!isTypeLegal(ArgTy, ArgVT)) return false;
1412 ISD::ArgFlagsTy Flags;
1413 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1414 Flags.setOrigAlign(OriginalAlignment);
1417 ArgRegs.push_back(Arg);
1418 ArgVTs.push_back(ArgVT);
1419 ArgFlags.push_back(Flags);
1422 // Handle the arguments now that we've gotten them.
1423 SmallVector<unsigned, 4> RegArgs;
1425 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1428 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1429 // TODO: Turn this into the table of arm call ops.
1430 MachineInstrBuilder MIB;
1433 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1435 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1436 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1437 .addExternalSymbol(TLI.getLibcallName(Call));
1439 // Add implicit physical register uses to the call.
1440 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1441 MIB.addReg(RegArgs[i]);
1443 // Finish off the call including any return values.
1444 SmallVector<unsigned, 4> UsedRegs;
1445 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1447 // Set all unused physreg defs as dead.
1448 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1453 bool ARMFastISel::SelectCall(const Instruction *I) {
1454 const CallInst *CI = cast<CallInst>(I);
1455 const Value *Callee = CI->getCalledValue();
1457 // Can't handle inline asm or worry about intrinsics yet.
1458 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1460 // Only handle global variable Callees that are direct calls.
1461 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1462 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1465 // Check the calling convention.
1466 ImmutableCallSite CS(CI);
1467 CallingConv::ID CC = CS.getCallingConv();
1468 // TODO: Avoid some calling conventions?
1469 if (CC != CallingConv::C) {
1470 // errs() << "Can't handle calling convention: " << CC << "\n";
1474 // Let SDISel handle vararg functions.
1475 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1476 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1477 if (FTy->isVarArg())
1480 // Handle *simple* calls for now.
1481 const Type *RetTy = I->getType();
1483 if (RetTy->isVoidTy())
1484 RetVT = MVT::isVoid;
1485 else if (!isTypeLegal(RetTy, RetVT))
1488 // For now we're using BLX etc on the assumption that we have v5t ops.
1490 if (!Subtarget->hasV5TOps()) return false;
1492 // Set up the argument vectors.
1493 SmallVector<Value*, 8> Args;
1494 SmallVector<unsigned, 8> ArgRegs;
1495 SmallVector<EVT, 8> ArgVTs;
1496 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1497 Args.reserve(CS.arg_size());
1498 ArgRegs.reserve(CS.arg_size());
1499 ArgVTs.reserve(CS.arg_size());
1500 ArgFlags.reserve(CS.arg_size());
1501 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1503 unsigned Arg = getRegForValue(*i);
1507 ISD::ArgFlagsTy Flags;
1508 unsigned AttrInd = i - CS.arg_begin() + 1;
1509 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1511 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1514 // FIXME: Only handle *easy* calls for now.
1515 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1516 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1517 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1518 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1521 const Type *ArgTy = (*i)->getType();
1523 if (!isTypeLegal(ArgTy, ArgVT))
1525 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1526 Flags.setOrigAlign(OriginalAlignment);
1529 ArgRegs.push_back(Arg);
1530 ArgVTs.push_back(ArgVT);
1531 ArgFlags.push_back(Flags);
1534 // Handle the arguments now that we've gotten them.
1535 SmallVector<unsigned, 4> RegArgs;
1537 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1540 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1541 // TODO: Turn this into the table of arm call ops.
1542 MachineInstrBuilder MIB;
1545 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1547 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1548 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1549 .addGlobalAddress(GV, 0, 0);
1551 // Add implicit physical register uses to the call.
1552 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1553 MIB.addReg(RegArgs[i]);
1555 // Finish off the call including any return values.
1556 SmallVector<unsigned, 4> UsedRegs;
1557 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1559 // Set all unused physreg defs as dead.
1560 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1566 // TODO: SoftFP support.
1567 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
1568 // No Thumb-1 for now.
1569 if (isThumb && !AFI->isThumb2Function()) return false;
1571 switch (I->getOpcode()) {
1572 case Instruction::Load:
1573 return SelectLoad(I);
1574 case Instruction::Store:
1575 return SelectStore(I);
1576 case Instruction::Br:
1577 return SelectBranch(I);
1578 case Instruction::ICmp:
1579 case Instruction::FCmp:
1580 return SelectCmp(I);
1581 case Instruction::FPExt:
1582 return SelectFPExt(I);
1583 case Instruction::FPTrunc:
1584 return SelectFPTrunc(I);
1585 case Instruction::SIToFP:
1586 return SelectSIToFP(I);
1587 case Instruction::FPToSI:
1588 return SelectFPToSI(I);
1589 case Instruction::FAdd:
1590 return SelectBinaryOp(I, ISD::FADD);
1591 case Instruction::FSub:
1592 return SelectBinaryOp(I, ISD::FSUB);
1593 case Instruction::FMul:
1594 return SelectBinaryOp(I, ISD::FMUL);
1595 case Instruction::SDiv:
1596 return SelectSDiv(I);
1597 case Instruction::SRem:
1598 return SelectSRem(I);
1599 case Instruction::Call:
1600 return SelectCall(I);
1601 case Instruction::Select:
1602 return SelectSelect(I);
1609 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
1610 // Completely untested on non-darwin.
1611 const TargetMachine &TM = funcInfo.MF->getTarget();
1612 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
1613 if (Subtarget->isTargetDarwin() && EnableARMFastISel)
1614 return new ARMFastISel(funcInfo);