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 "llvm/CallingConv.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/IntrinsicInst.h"
27 #include "llvm/Module.h"
28 #include "llvm/CodeGen/Analysis.h"
29 #include "llvm/CodeGen/FastISel.h"
30 #include "llvm/CodeGen/FunctionLoweringInfo.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineModuleInfo.h"
33 #include "llvm/CodeGen/MachineConstantPool.h"
34 #include "llvm/CodeGen/MachineFrameInfo.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/Support/CallSite.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/GetElementPtrTypeIterator.h"
40 #include "llvm/Target/TargetData.h"
41 #include "llvm/Target/TargetInstrInfo.h"
42 #include "llvm/Target/TargetLowering.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include "llvm/Target/TargetOptions.h"
48 EnableARMFastISel("arm-fast-isel",
49 cl::desc("Turn on experimental ARM fast-isel support"),
50 cl::init(false), cl::Hidden);
54 class ARMFastISel : public FastISel {
56 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
57 /// make the right decision when generating code for different targets.
58 const ARMSubtarget *Subtarget;
59 const TargetMachine &TM;
60 const TargetInstrInfo &TII;
61 const TargetLowering &TLI;
62 const ARMFunctionInfo *AFI;
64 // Convenience variables to avoid some queries.
69 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
71 TM(funcInfo.MF->getTarget()),
72 TII(*TM.getInstrInfo()),
73 TLI(*TM.getTargetLowering()) {
74 Subtarget = &TM.getSubtarget<ARMSubtarget>();
75 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
76 isThumb = AFI->isThumbFunction();
77 Context = &funcInfo.Fn->getContext();
80 // Code from FastISel.cpp.
81 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
82 const TargetRegisterClass *RC);
83 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
84 const TargetRegisterClass *RC,
85 unsigned Op0, bool Op0IsKill);
86 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill,
89 unsigned Op1, bool Op1IsKill);
90 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
91 const TargetRegisterClass *RC,
92 unsigned Op0, bool Op0IsKill,
94 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
95 const TargetRegisterClass *RC,
96 unsigned Op0, bool Op0IsKill,
97 const ConstantFP *FPImm);
98 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
99 const TargetRegisterClass *RC,
101 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
102 const TargetRegisterClass *RC,
103 unsigned Op0, bool Op0IsKill,
104 unsigned Op1, bool Op1IsKill,
106 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
107 unsigned Op0, bool Op0IsKill,
110 // Backend specific FastISel code.
111 virtual bool TargetSelectInstruction(const Instruction *I);
112 virtual unsigned TargetMaterializeConstant(const Constant *C);
114 #include "ARMGenFastISel.inc"
116 // Instruction selection routines.
118 virtual bool SelectLoad(const Instruction *I);
119 virtual bool SelectStore(const Instruction *I);
120 virtual bool SelectBranch(const Instruction *I);
121 virtual bool SelectCmp(const Instruction *I);
122 virtual bool SelectFPExt(const Instruction *I);
123 virtual bool SelectFPTrunc(const Instruction *I);
124 virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
125 virtual bool SelectSIToFP(const Instruction *I);
126 virtual bool SelectFPToSI(const Instruction *I);
127 virtual bool SelectSDiv(const Instruction *I);
131 bool isTypeLegal(const Type *Ty, EVT &VT);
132 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
133 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
134 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
135 bool ARMLoadAlloca(const Instruction *I, EVT VT);
136 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
137 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
138 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
139 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
140 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
141 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
143 // Call handling routines.
145 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
146 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
147 SmallVectorImpl<unsigned> &ArgRegs,
148 SmallVectorImpl<EVT> &ArgVTs,
149 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
150 SmallVectorImpl<unsigned> &RegArgs,
153 bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
154 const Instruction *I, CallingConv::ID CC,
156 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
158 // OptionalDef handling routines.
160 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
161 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
164 } // end anonymous namespace
166 #include "ARMGenCallingConv.inc"
168 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
169 // we don't care about implicit defs here, just places we'll need to add a
170 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
171 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
172 const TargetInstrDesc &TID = MI->getDesc();
173 if (!TID.hasOptionalDef())
176 // Look to see if our OptionalDef is defining CPSR or CCR.
177 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
178 const MachineOperand &MO = MI->getOperand(i);
179 if (!MO.isReg() || !MO.isDef()) continue;
180 if (MO.getReg() == ARM::CPSR)
186 // If the machine is predicable go ahead and add the predicate operands, if
187 // it needs default CC operands add those.
188 const MachineInstrBuilder &
189 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
190 MachineInstr *MI = &*MIB;
192 // Do we use a predicate?
193 if (TII.isPredicable(MI))
196 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
197 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
199 if (DefinesOptionalPredicate(MI, &CPSR)) {
208 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
209 const TargetRegisterClass* RC) {
210 unsigned ResultReg = createResultReg(RC);
211 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
213 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
217 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
218 const TargetRegisterClass *RC,
219 unsigned Op0, bool Op0IsKill) {
220 unsigned ResultReg = createResultReg(RC);
221 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
223 if (II.getNumDefs() >= 1)
224 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
225 .addReg(Op0, Op0IsKill * RegState::Kill));
227 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
228 .addReg(Op0, Op0IsKill * RegState::Kill));
229 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
230 TII.get(TargetOpcode::COPY), ResultReg)
231 .addReg(II.ImplicitDefs[0]));
236 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
237 const TargetRegisterClass *RC,
238 unsigned Op0, bool Op0IsKill,
239 unsigned Op1, bool Op1IsKill) {
240 unsigned ResultReg = createResultReg(RC);
241 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
243 if (II.getNumDefs() >= 1)
244 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
245 .addReg(Op0, Op0IsKill * RegState::Kill)
246 .addReg(Op1, Op1IsKill * RegState::Kill));
248 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
249 .addReg(Op0, Op0IsKill * RegState::Kill)
250 .addReg(Op1, Op1IsKill * RegState::Kill));
251 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
252 TII.get(TargetOpcode::COPY), ResultReg)
253 .addReg(II.ImplicitDefs[0]));
258 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
259 const TargetRegisterClass *RC,
260 unsigned Op0, bool Op0IsKill,
262 unsigned ResultReg = createResultReg(RC);
263 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
265 if (II.getNumDefs() >= 1)
266 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
267 .addReg(Op0, Op0IsKill * RegState::Kill)
270 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
271 .addReg(Op0, Op0IsKill * RegState::Kill)
273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
274 TII.get(TargetOpcode::COPY), ResultReg)
275 .addReg(II.ImplicitDefs[0]));
280 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
281 const TargetRegisterClass *RC,
282 unsigned Op0, bool Op0IsKill,
283 const ConstantFP *FPImm) {
284 unsigned ResultReg = createResultReg(RC);
285 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
287 if (II.getNumDefs() >= 1)
288 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
289 .addReg(Op0, Op0IsKill * RegState::Kill)
292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
293 .addReg(Op0, Op0IsKill * RegState::Kill)
295 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
296 TII.get(TargetOpcode::COPY), ResultReg)
297 .addReg(II.ImplicitDefs[0]));
302 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
303 const TargetRegisterClass *RC,
304 unsigned Op0, bool Op0IsKill,
305 unsigned Op1, bool Op1IsKill,
307 unsigned ResultReg = createResultReg(RC);
308 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
310 if (II.getNumDefs() >= 1)
311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
312 .addReg(Op0, Op0IsKill * RegState::Kill)
313 .addReg(Op1, Op1IsKill * RegState::Kill)
316 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
317 .addReg(Op0, Op0IsKill * RegState::Kill)
318 .addReg(Op1, Op1IsKill * RegState::Kill)
320 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
321 TII.get(TargetOpcode::COPY), ResultReg)
322 .addReg(II.ImplicitDefs[0]));
327 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
328 const TargetRegisterClass *RC,
330 unsigned ResultReg = createResultReg(RC);
331 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
333 if (II.getNumDefs() >= 1)
334 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
337 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
339 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
340 TII.get(TargetOpcode::COPY), ResultReg)
341 .addReg(II.ImplicitDefs[0]));
346 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
347 unsigned Op0, bool Op0IsKill,
349 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
350 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
351 "Cannot yet extract from physregs");
352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
353 DL, TII.get(TargetOpcode::COPY), ResultReg)
354 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
358 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
359 // checks from the various callers.
360 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
361 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
363 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
364 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
365 TII.get(ARM::VMOVRS), MoveReg)
370 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
371 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
373 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
374 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
375 TII.get(ARM::VMOVSR), MoveReg)
380 // For double width floating point we need to materialize two constants
381 // (the high and the low) into integer registers then use a move to get
382 // the combined constant into an FP reg.
383 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
384 const APFloat Val = CFP->getValueAPF();
385 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
387 // This checks to see if we can use VFP3 instructions to materialize
388 // a constant, otherwise we have to go through the constant pool.
389 if (TLI.isFPImmLegal(Val, VT)) {
390 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
391 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
392 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
398 // Require VFP2 for loading fp constants.
399 if (!Subtarget->hasVFP2()) return false;
401 // MachineConstantPool wants an explicit alignment.
402 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
404 // TODO: Figure out if this is correct.
405 Align = TD.getTypeAllocSize(CFP->getType());
407 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
408 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
409 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
411 // The extra reg is for addrmode5.
412 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
414 .addConstantPoolIndex(Idx)
419 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
421 // For now 32-bit only.
422 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
424 // MachineConstantPool wants an explicit alignment.
425 unsigned Align = TD.getPrefTypeAlignment(C->getType());
427 // TODO: Figure out if this is correct.
428 Align = TD.getTypeAllocSize(C->getType());
430 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
431 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
434 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
435 TII.get(ARM::t2LDRpci), DestReg)
436 .addConstantPoolIndex(Idx));
438 // The extra reg and immediate are for addrmode2.
439 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
440 TII.get(ARM::LDRcp), DestReg)
441 .addConstantPoolIndex(Idx)
442 .addReg(0).addImm(0));
447 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
448 EVT VT = TLI.getValueType(C->getType(), true);
450 // Only handle simple types.
451 if (!VT.isSimple()) return 0;
453 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
454 return ARMMaterializeFP(CFP, VT);
455 return ARMMaterializeInt(C, VT);
458 bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
459 VT = TLI.getValueType(Ty, true);
461 // Only handle simple types.
462 if (VT == MVT::Other || !VT.isSimple()) return false;
464 // Handle all legal types, i.e. a register that will directly hold this
466 return TLI.isTypeLegal(VT);
469 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
470 if (isTypeLegal(Ty, VT)) return true;
472 // If this is a type than can be sign or zero-extended to a basic operation
473 // go ahead and accept it now.
474 if (VT == MVT::i8 || VT == MVT::i16)
480 // Computes the Reg+Offset to get to an object.
481 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
483 // Some boilerplate from the X86 FastISel.
484 const User *U = NULL;
485 unsigned Opcode = Instruction::UserOp1;
486 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
487 // Don't walk into other basic blocks; it's possible we haven't
488 // visited them yet, so the instructions may not yet be assigned
489 // virtual registers.
490 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
492 Opcode = I->getOpcode();
494 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
495 Opcode = C->getOpcode();
499 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
500 if (Ty->getAddressSpace() > 255)
501 // Fast instruction selection doesn't support the special
508 case Instruction::Alloca: {
509 assert(false && "Alloca should have been handled earlier!");
514 // FIXME: Handle global variables.
515 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
520 // Try to get this in a register if nothing else has worked.
521 Reg = getRegForValue(Obj);
522 if (Reg == 0) return false;
524 // Since the offset may be too large for the load instruction
525 // get the reg+offset into a register.
526 // TODO: Verify the additions work, otherwise we'll need to add the
527 // offset instead of 0 to the instructions and do all sorts of operand
529 // TODO: Optimize this somewhat.
531 ARMCC::CondCodes Pred = ARMCC::AL;
532 unsigned PredReg = 0;
535 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
536 Reg, Reg, Offset, Pred, PredReg,
537 static_cast<const ARMBaseInstrInfo&>(TII));
539 assert(AFI->isThumb2Function());
540 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
541 Reg, Reg, Offset, Pred, PredReg,
542 static_cast<const ARMBaseInstrInfo&>(TII));
548 bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
549 Value *Op0 = I->getOperand(0);
551 // Verify it's an alloca.
552 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
553 DenseMap<const AllocaInst*, int>::iterator SI =
554 FuncInfo.StaticAllocaMap.find(AI);
556 if (SI != FuncInfo.StaticAllocaMap.end()) {
557 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
558 unsigned ResultReg = createResultReg(RC);
559 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
560 ResultReg, SI->second, RC,
561 TM.getRegisterInfo());
562 UpdateValueMap(I, ResultReg);
569 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
570 unsigned Reg, int Offset) {
572 assert(VT.isSimple() && "Non-simple types are invalid here!");
574 bool isFloat = false;
575 switch (VT.getSimpleVT().SimpleTy) {
577 // This is mostly going to be Neon/vector support.
580 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
584 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
588 Opc = isThumb ? ARM::tLDR : ARM::LDR;
600 ResultReg = createResultReg(TLI.getRegClassFor(VT));
602 // TODO: Fix the Addressing modes so that these can share some code.
603 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
604 // The thumb addressing mode has operands swapped from the arm addressing
605 // mode, the floating point one only has two operands.
607 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
608 TII.get(Opc), ResultReg)
609 .addReg(Reg).addImm(Offset));
611 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
612 TII.get(Opc), ResultReg)
613 .addReg(Reg).addImm(Offset).addReg(0));
615 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
616 TII.get(Opc), ResultReg)
617 .addReg(Reg).addReg(0).addImm(Offset));
621 bool ARMFastISel::SelectLoad(const Instruction *I) {
622 // Verify we have a legal type before going any further.
624 if (!isLoadTypeLegal(I->getType(), VT))
627 // If we're an alloca we know we have a frame index and can emit the load
628 // directly in short order.
629 if (ARMLoadAlloca(I, VT))
632 // Our register and offset with innocuous defaults.
636 // See if we can handle this as Reg + Offset
637 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
641 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
643 UpdateValueMap(I, ResultReg);
647 bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
648 Value *Op1 = I->getOperand(1);
650 // Verify it's an alloca.
651 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
652 DenseMap<const AllocaInst*, int>::iterator SI =
653 FuncInfo.StaticAllocaMap.find(AI);
655 if (SI != FuncInfo.StaticAllocaMap.end()) {
656 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
657 assert(SrcReg != 0 && "Nothing to store!");
658 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
659 SrcReg, true /*isKill*/, SI->second, RC,
660 TM.getRegisterInfo());
667 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
668 unsigned DstReg, int Offset) {
670 bool isFloat = false;
671 switch (VT.getSimpleVT().SimpleTy) {
672 default: return false;
674 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
675 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
676 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
678 if (!Subtarget->hasVFP2()) return false;
683 if (!Subtarget->hasVFP2()) return false;
689 // The thumb addressing mode has operands swapped from the arm addressing
690 // mode, the floating point one only has two operands.
692 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
693 TII.get(StrOpc), SrcReg)
694 .addReg(DstReg).addImm(Offset));
696 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
697 TII.get(StrOpc), SrcReg)
698 .addReg(DstReg).addImm(Offset).addReg(0));
701 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
702 TII.get(StrOpc), SrcReg)
703 .addReg(DstReg).addReg(0).addImm(Offset));
708 bool ARMFastISel::SelectStore(const Instruction *I) {
709 Value *Op0 = I->getOperand(0);
712 // Yay type legalization
714 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
717 // Get the value to be stored into a register.
718 SrcReg = getRegForValue(Op0);
722 // If we're an alloca we know we have a frame index and can emit the store
724 if (ARMStoreAlloca(I, SrcReg, VT))
727 // Our register and offset with innocuous defaults.
731 // See if we can handle this as Reg + Offset
732 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
735 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
740 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
742 // Needs two compares...
743 case CmpInst::FCMP_ONE:
744 case CmpInst::FCMP_UEQ:
746 assert(false && "Unhandled CmpInst::Predicate!");
748 case CmpInst::ICMP_EQ:
749 case CmpInst::FCMP_OEQ:
751 case CmpInst::ICMP_SGT:
752 case CmpInst::FCMP_OGT:
754 case CmpInst::ICMP_SGE:
755 case CmpInst::FCMP_OGE:
757 case CmpInst::ICMP_UGT:
758 case CmpInst::FCMP_UGT:
760 case CmpInst::FCMP_OLT:
762 case CmpInst::ICMP_ULE:
763 case CmpInst::FCMP_OLE:
765 case CmpInst::FCMP_ORD:
767 case CmpInst::FCMP_UNO:
769 case CmpInst::FCMP_UGE:
771 case CmpInst::ICMP_SLT:
772 case CmpInst::FCMP_ULT:
774 case CmpInst::ICMP_SLE:
775 case CmpInst::FCMP_ULE:
777 case CmpInst::FCMP_UNE:
778 case CmpInst::ICMP_NE:
780 case CmpInst::ICMP_UGE:
782 case CmpInst::ICMP_ULT:
787 bool ARMFastISel::SelectBranch(const Instruction *I) {
788 const BranchInst *BI = cast<BranchInst>(I);
789 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
790 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
792 // Simple branch support.
793 // TODO: Try to avoid the re-computation in some places.
794 unsigned CondReg = getRegForValue(BI->getCondition());
795 if (CondReg == 0) return false;
797 // Re-set the flags just in case.
798 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
799 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
800 .addReg(CondReg).addImm(1));
802 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
803 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
804 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
805 FastEmitBranch(FBB, DL);
806 FuncInfo.MBB->addSuccessor(TBB);
810 bool ARMFastISel::SelectCmp(const Instruction *I) {
811 const CmpInst *CI = cast<CmpInst>(I);
814 const Type *Ty = CI->getOperand(0)->getType();
815 if (!isTypeLegal(Ty, VT))
818 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
819 if (isFloat && !Subtarget->hasVFP2())
824 switch (VT.getSimpleVT().SimpleTy) {
825 default: return false;
826 // TODO: Verify compares.
828 CmpOpc = ARM::VCMPES;
829 CondReg = ARM::FPSCR;
832 CmpOpc = ARM::VCMPED;
833 CondReg = ARM::FPSCR;
836 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
841 // Get the compare predicate.
842 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
844 // We may not handle every CC for now.
845 if (ARMPred == ARMCC::AL) return false;
847 unsigned Arg1 = getRegForValue(CI->getOperand(0));
848 if (Arg1 == 0) return false;
850 unsigned Arg2 = getRegForValue(CI->getOperand(1));
851 if (Arg2 == 0) return false;
853 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
854 .addReg(Arg1).addReg(Arg2));
856 // For floating point we need to move the result to a comparison register
857 // that we can then use for branches.
859 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
860 TII.get(ARM::FMSTAT)));
862 // Now set a register based on the comparison. Explicitly set the predicates
864 unsigned MovCCOpc = isThumb ? ARM::tMOVCCi : ARM::MOVCCi;
865 unsigned DestReg = createResultReg(ARM::GPRRegisterClass);
867 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
868 unsigned ZeroReg = TargetMaterializeConstant(Zero);
869 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
870 .addReg(ZeroReg).addImm(1)
871 .addImm(ARMPred).addReg(CondReg);
873 UpdateValueMap(I, DestReg);
877 bool ARMFastISel::SelectFPExt(const Instruction *I) {
878 // Make sure we have VFP and that we're extending float to double.
879 if (!Subtarget->hasVFP2()) return false;
881 Value *V = I->getOperand(0);
882 if (!I->getType()->isDoubleTy() ||
883 !V->getType()->isFloatTy()) return false;
885 unsigned Op = getRegForValue(V);
886 if (Op == 0) return false;
888 unsigned Result = createResultReg(ARM::DPRRegisterClass);
889 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
890 TII.get(ARM::VCVTDS), Result)
892 UpdateValueMap(I, Result);
896 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
897 // Make sure we have VFP and that we're truncating double to float.
898 if (!Subtarget->hasVFP2()) return false;
900 Value *V = I->getOperand(0);
901 if (!I->getType()->isFloatTy() ||
902 !V->getType()->isDoubleTy()) return false;
904 unsigned Op = getRegForValue(V);
905 if (Op == 0) return false;
907 unsigned Result = createResultReg(ARM::SPRRegisterClass);
908 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
909 TII.get(ARM::VCVTSD), Result)
911 UpdateValueMap(I, Result);
915 bool ARMFastISel::SelectSIToFP(const Instruction *I) {
916 // Make sure we have VFP.
917 if (!Subtarget->hasVFP2()) return false;
920 const Type *Ty = I->getType();
921 if (!isTypeLegal(Ty, DstVT))
924 unsigned Op = getRegForValue(I->getOperand(0));
925 if (Op == 0) return false;
927 // The conversion routine works on fp-reg to fp-reg and the operand above
928 // was an integer, move it to the fp registers if possible.
929 unsigned FP = ARMMoveToFPReg(DstVT, Op);
930 if (FP == 0) return false;
933 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
934 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
937 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
938 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
941 UpdateValueMap(I, ResultReg);
945 bool ARMFastISel::SelectFPToSI(const Instruction *I) {
946 // Make sure we have VFP.
947 if (!Subtarget->hasVFP2()) return false;
950 const Type *RetTy = I->getType();
951 if (!isTypeLegal(RetTy, DstVT))
954 unsigned Op = getRegForValue(I->getOperand(0));
955 if (Op == 0) return false;
958 const Type *OpTy = I->getOperand(0)->getType();
959 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
960 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
962 EVT OpVT = TLI.getValueType(OpTy, true);
964 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
965 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
969 // This result needs to be in an integer register, but the conversion only
970 // takes place in fp-regs.
971 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
972 if (IntReg == 0) return false;
974 UpdateValueMap(I, IntReg);
978 bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
979 EVT VT = TLI.getValueType(I->getType(), true);
981 // We can get here in the case when we want to use NEON for our fp
982 // operations, but can't figure out how to. Just use the vfp instructions
984 // FIXME: It'd be nice to use NEON instructions.
985 const Type *Ty = I->getType();
986 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
987 if (isFloat && !Subtarget->hasVFP2())
990 unsigned Op1 = getRegForValue(I->getOperand(0));
991 if (Op1 == 0) return false;
993 unsigned Op2 = getRegForValue(I->getOperand(1));
994 if (Op2 == 0) return false;
997 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
998 VT.getSimpleVT().SimpleTy == MVT::i64;
1000 default: return false;
1002 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1005 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1008 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1011 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1012 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1013 TII.get(Opc), ResultReg)
1014 .addReg(Op1).addReg(Op2));
1015 UpdateValueMap(I, ResultReg);
1019 // Call Handling Code
1021 // This is largely taken directly from CCAssignFnForNode - we don't support
1022 // varargs in FastISel so that part has been removed.
1023 // TODO: We may not support all of this.
1024 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1027 llvm_unreachable("Unsupported calling convention");
1028 case CallingConv::C:
1029 case CallingConv::Fast:
1030 // Use target triple & subtarget features to do actual dispatch.
1031 if (Subtarget->isAAPCS_ABI()) {
1032 if (Subtarget->hasVFP2() &&
1033 FloatABIType == FloatABI::Hard)
1034 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1036 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1038 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1039 case CallingConv::ARM_AAPCS_VFP:
1040 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1041 case CallingConv::ARM_AAPCS:
1042 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1043 case CallingConv::ARM_APCS:
1044 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1048 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1049 SmallVectorImpl<unsigned> &ArgRegs,
1050 SmallVectorImpl<EVT> &ArgVTs,
1051 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1052 SmallVectorImpl<unsigned> &RegArgs,
1054 unsigned &NumBytes) {
1055 SmallVector<CCValAssign, 16> ArgLocs;
1056 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1057 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1059 // Get a count of how many bytes are to be pushed on the stack.
1060 NumBytes = CCInfo.getNextStackOffset();
1062 // Issue CALLSEQ_START
1063 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1064 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1067 // Process the args.
1068 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1069 CCValAssign &VA = ArgLocs[i];
1070 unsigned Arg = ArgRegs[VA.getValNo()];
1071 EVT ArgVT = ArgVTs[VA.getValNo()];
1073 // Should we ever have to promote?
1074 switch (VA.getLocInfo()) {
1075 case CCValAssign::Full: break;
1077 assert(false && "Handle arg promotion for libcalls?");
1081 // Now copy/store arg to correct locations.
1082 if (VA.isRegLoc()) {
1083 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1086 RegArgs.push_back(VA.getLocReg());
1096 bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1097 const Instruction *I, CallingConv::ID CC,
1098 unsigned &NumBytes) {
1099 // Issue CALLSEQ_END
1100 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1101 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1102 .addImm(NumBytes).addImm(0);
1104 // Now the return value.
1105 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1106 SmallVector<CCValAssign, 16> RVLocs;
1107 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1108 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1110 // Copy all of the result registers out of their specified physreg.
1111 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1112 EVT CopyVT = RVLocs[0].getValVT();
1113 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1115 unsigned ResultReg = createResultReg(DstRC);
1116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1117 ResultReg).addReg(RVLocs[0].getLocReg());
1118 UsedRegs.push_back(RVLocs[0].getLocReg());
1120 // Finally update the result.
1121 UpdateValueMap(I, ResultReg);
1127 // A quick function that will emit a call for a named libcall in F with the
1128 // vector of passed arguments for the Instruction in I. We can assume that we
1129 // can emit a call for any libcall we can produce. This is an abridged version
1130 // of the full call infrastructure since we won't need to worry about things
1131 // like computed function pointers or strange arguments at call sites.
1132 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
1134 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1135 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1137 // Handle *simple* calls for now.
1138 const Type *RetTy = I->getType();
1140 if (RetTy->isVoidTy())
1141 RetVT = MVT::isVoid;
1142 else if (!isTypeLegal(RetTy, RetVT))
1145 // For now we're using BLX etc on the assumption that we have v5t ops.
1146 if (!Subtarget->hasV5TOps()) return false;
1148 // Set up the argument vectors.
1149 SmallVector<Value*, 8> Args;
1150 SmallVector<unsigned, 8> ArgRegs;
1151 SmallVector<EVT, 8> ArgVTs;
1152 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1153 Args.reserve(I->getNumOperands());
1154 ArgRegs.reserve(I->getNumOperands());
1155 ArgVTs.reserve(I->getNumOperands());
1156 ArgFlags.reserve(I->getNumOperands());
1157 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1158 Value *Op = I->getOperand(i);
1159 unsigned Arg = getRegForValue(Op);
1160 if (Arg == 0) return false;
1162 const Type *ArgTy = Op->getType();
1164 if (!isTypeLegal(ArgTy, ArgVT)) return false;
1166 ISD::ArgFlagsTy Flags;
1167 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1168 Flags.setOrigAlign(OriginalAlignment);
1171 ArgRegs.push_back(Arg);
1172 ArgVTs.push_back(ArgVT);
1173 ArgFlags.push_back(Flags);
1176 // Handle the arguments now that we've gotten them.
1177 SmallVector<unsigned, 4> RegArgs;
1179 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1182 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1183 // TODO: Turn this into the table of arm call ops.
1184 MachineInstrBuilder MIB;
1187 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1189 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1190 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1191 .addExternalSymbol(TLI.getLibcallName(Call));
1193 // Add implicit physical register uses to the call.
1194 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1195 MIB.addReg(RegArgs[i]);
1197 // Finish off the call including any return values.
1198 SmallVector<unsigned, 4> UsedRegs;
1199 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1201 // Set all unused physreg defs as dead.
1202 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1207 bool ARMFastISel::SelectSDiv(const Instruction *I) {
1209 const Type *Ty = I->getType();
1210 if (!isTypeLegal(Ty, VT))
1213 // If we have integer div support we should have selected this automagically.
1214 // In case we have a real miss go ahead and return false and we'll pick
1216 if (Subtarget->hasDivide()) return false;
1218 // Otherwise emit a libcall.
1219 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1221 LC = RTLIB::SDIV_I16;
1222 else if (VT == MVT::i32)
1223 LC = RTLIB::SDIV_I32;
1224 else if (VT == MVT::i64)
1225 LC = RTLIB::SDIV_I64;
1226 else if (VT == MVT::i128)
1227 LC = RTLIB::SDIV_I128;
1228 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1230 return ARMEmitLibcall(I, LC);
1233 // TODO: SoftFP support.
1234 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
1235 // No Thumb-1 for now.
1236 if (isThumb && !AFI->isThumb2Function()) return false;
1238 switch (I->getOpcode()) {
1239 case Instruction::Load:
1240 return SelectLoad(I);
1241 case Instruction::Store:
1242 return SelectStore(I);
1243 case Instruction::Br:
1244 return SelectBranch(I);
1245 case Instruction::ICmp:
1246 case Instruction::FCmp:
1247 return SelectCmp(I);
1248 case Instruction::FPExt:
1249 return SelectFPExt(I);
1250 case Instruction::FPTrunc:
1251 return SelectFPTrunc(I);
1252 case Instruction::SIToFP:
1253 return SelectSIToFP(I);
1254 case Instruction::FPToSI:
1255 return SelectFPToSI(I);
1256 case Instruction::FAdd:
1257 return SelectBinaryOp(I, ISD::FADD);
1258 case Instruction::FSub:
1259 return SelectBinaryOp(I, ISD::FSUB);
1260 case Instruction::FMul:
1261 return SelectBinaryOp(I, ISD::FMUL);
1262 case Instruction::SDiv:
1263 return SelectSDiv(I);
1270 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
1271 if (EnableARMFastISel) return new ARMFastISel(funcInfo);