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/CodeGen/Analysis.h"
28 #include "llvm/CodeGen/FastISel.h"
29 #include "llvm/CodeGen/FunctionLoweringInfo.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineModuleInfo.h"
32 #include "llvm/CodeGen/MachineConstantPool.h"
33 #include "llvm/CodeGen/MachineFrameInfo.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/Support/CallSite.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/GetElementPtrTypeIterator.h"
39 #include "llvm/Target/TargetData.h"
40 #include "llvm/Target/TargetInstrInfo.h"
41 #include "llvm/Target/TargetLowering.h"
42 #include "llvm/Target/TargetMachine.h"
43 #include "llvm/Target/TargetOptions.h"
47 EnableARMFastISel("arm-fast-isel",
48 cl::desc("Turn on experimental ARM fast-isel support"),
49 cl::init(false), cl::Hidden);
53 class ARMFastISel : public FastISel {
55 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
56 /// make the right decision when generating code for different targets.
57 const ARMSubtarget *Subtarget;
58 const TargetMachine &TM;
59 const TargetInstrInfo &TII;
60 const TargetLowering &TLI;
61 const ARMFunctionInfo *AFI;
63 // Convenience variable to avoid checking all the time.
67 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
69 TM(funcInfo.MF->getTarget()),
70 TII(*TM.getInstrInfo()),
71 TLI(*TM.getTargetLowering()) {
72 Subtarget = &TM.getSubtarget<ARMSubtarget>();
73 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
74 isThumb = AFI->isThumbFunction();
77 // Code from FastISel.cpp.
78 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
79 const TargetRegisterClass *RC);
80 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
81 const TargetRegisterClass *RC,
82 unsigned Op0, bool Op0IsKill);
83 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
84 const TargetRegisterClass *RC,
85 unsigned Op0, bool Op0IsKill,
86 unsigned Op1, bool Op1IsKill);
87 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
88 const TargetRegisterClass *RC,
89 unsigned Op0, bool Op0IsKill,
91 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
92 const TargetRegisterClass *RC,
93 unsigned Op0, bool Op0IsKill,
94 const ConstantFP *FPImm);
95 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
96 const TargetRegisterClass *RC,
98 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
99 const TargetRegisterClass *RC,
100 unsigned Op0, bool Op0IsKill,
101 unsigned Op1, bool Op1IsKill,
103 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
104 unsigned Op0, bool Op0IsKill,
107 // Backend specific FastISel code.
108 virtual bool TargetSelectInstruction(const Instruction *I);
109 virtual unsigned TargetMaterializeConstant(const Constant *C);
111 #include "ARMGenFastISel.inc"
113 // Instruction selection routines.
115 virtual bool ARMSelectLoad(const Instruction *I);
116 virtual bool ARMSelectStore(const Instruction *I);
117 virtual bool ARMSelectBranch(const Instruction *I);
118 virtual bool ARMSelectCmp(const Instruction *I);
119 virtual bool ARMSelectFPExt(const Instruction *I);
120 virtual bool ARMSelectFPTrunc(const Instruction *I);
121 virtual bool ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
122 virtual bool ARMSelectSIToFP(const Instruction *I);
123 virtual bool ARMSelectFPToSI(const Instruction *I);
127 bool isTypeLegal(const Type *Ty, EVT &VT);
128 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
129 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
130 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
131 bool ARMLoadAlloca(const Instruction *I, EVT VT);
132 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
133 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
134 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
135 unsigned ARMMaterializeInt(const Constant *C);
136 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
137 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
139 // Call handling routines.
141 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
143 // OptionalDef handling routines.
145 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
146 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
149 } // end anonymous namespace
151 #include "ARMGenCallingConv.inc"
153 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
154 // we don't care about implicit defs here, just places we'll need to add a
155 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
156 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
157 const TargetInstrDesc &TID = MI->getDesc();
158 if (!TID.hasOptionalDef())
161 // Look to see if our OptionalDef is defining CPSR or CCR.
162 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
163 const MachineOperand &MO = MI->getOperand(i);
164 if (!MO.isReg() || !MO.isDef()) continue;
165 if (MO.getReg() == ARM::CPSR)
171 // If the machine is predicable go ahead and add the predicate operands, if
172 // it needs default CC operands add those.
173 const MachineInstrBuilder &
174 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
175 MachineInstr *MI = &*MIB;
177 // Do we use a predicate?
178 if (TII.isPredicable(MI))
181 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
182 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
184 if (DefinesOptionalPredicate(MI, &CPSR)) {
193 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
194 const TargetRegisterClass* RC) {
195 unsigned ResultReg = createResultReg(RC);
196 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
198 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
202 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
203 const TargetRegisterClass *RC,
204 unsigned Op0, bool Op0IsKill) {
205 unsigned ResultReg = createResultReg(RC);
206 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
208 if (II.getNumDefs() >= 1)
209 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
210 .addReg(Op0, Op0IsKill * RegState::Kill));
212 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
213 .addReg(Op0, Op0IsKill * RegState::Kill));
214 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
215 TII.get(TargetOpcode::COPY), ResultReg)
216 .addReg(II.ImplicitDefs[0]));
221 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
222 const TargetRegisterClass *RC,
223 unsigned Op0, bool Op0IsKill,
224 unsigned Op1, bool Op1IsKill) {
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)
231 .addReg(Op1, Op1IsKill * RegState::Kill));
233 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
234 .addReg(Op0, Op0IsKill * RegState::Kill)
235 .addReg(Op1, Op1IsKill * RegState::Kill));
236 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
237 TII.get(TargetOpcode::COPY), ResultReg)
238 .addReg(II.ImplicitDefs[0]));
243 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
244 const TargetRegisterClass *RC,
245 unsigned Op0, bool Op0IsKill,
247 unsigned ResultReg = createResultReg(RC);
248 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
250 if (II.getNumDefs() >= 1)
251 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
252 .addReg(Op0, Op0IsKill * RegState::Kill)
255 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
256 .addReg(Op0, Op0IsKill * RegState::Kill)
258 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
259 TII.get(TargetOpcode::COPY), ResultReg)
260 .addReg(II.ImplicitDefs[0]));
265 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
266 const TargetRegisterClass *RC,
267 unsigned Op0, bool Op0IsKill,
268 const ConstantFP *FPImm) {
269 unsigned ResultReg = createResultReg(RC);
270 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
272 if (II.getNumDefs() >= 1)
273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
274 .addReg(Op0, Op0IsKill * RegState::Kill)
277 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
278 .addReg(Op0, Op0IsKill * RegState::Kill)
280 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
281 TII.get(TargetOpcode::COPY), ResultReg)
282 .addReg(II.ImplicitDefs[0]));
287 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
288 const TargetRegisterClass *RC,
289 unsigned Op0, bool Op0IsKill,
290 unsigned Op1, bool Op1IsKill,
292 unsigned ResultReg = createResultReg(RC);
293 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
295 if (II.getNumDefs() >= 1)
296 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
297 .addReg(Op0, Op0IsKill * RegState::Kill)
298 .addReg(Op1, Op1IsKill * RegState::Kill)
301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
302 .addReg(Op0, Op0IsKill * RegState::Kill)
303 .addReg(Op1, Op1IsKill * RegState::Kill)
305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
306 TII.get(TargetOpcode::COPY), ResultReg)
307 .addReg(II.ImplicitDefs[0]));
312 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
313 const TargetRegisterClass *RC,
315 unsigned ResultReg = createResultReg(RC);
316 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
318 if (II.getNumDefs() >= 1)
319 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
322 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
325 TII.get(TargetOpcode::COPY), ResultReg)
326 .addReg(II.ImplicitDefs[0]));
331 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
332 unsigned Op0, bool Op0IsKill,
334 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
335 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
336 "Cannot yet extract from physregs");
337 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
338 DL, TII.get(TargetOpcode::COPY), ResultReg)
339 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
343 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
344 // checks from the various callers.
345 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
346 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
348 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
350 TII.get(ARM::VMOVRS), MoveReg)
355 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
356 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
358 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
360 TII.get(ARM::VMOVSR), MoveReg)
365 // For double width floating point we need to materialize two constants
366 // (the high and the low) into integer registers then use a move to get
367 // the combined constant into an FP reg.
368 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
369 const APFloat Val = CFP->getValueAPF();
370 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
372 // This checks to see if we can use VFP3 instructions to materialize
373 // a constant, otherwise we have to go through the constant pool.
374 if (TLI.isFPImmLegal(Val, VT)) {
375 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
376 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
383 // Require VFP2 for loading fp constants.
384 if (!Subtarget->hasVFP2()) return false;
386 // MachineConstantPool wants an explicit alignment.
387 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
389 // TODO: Figure out if this is correct.
390 Align = TD.getTypeAllocSize(CFP->getType());
392 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
393 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
394 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
396 // The extra reg is for addrmode5.
397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
398 .addReg(DestReg).addConstantPoolIndex(Idx)
403 // TODO: Verify 64-bit.
404 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
405 // MachineConstantPool wants an explicit alignment.
406 unsigned Align = TD.getPrefTypeAlignment(C->getType());
408 // TODO: Figure out if this is correct.
409 Align = TD.getTypeAllocSize(C->getType());
411 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
412 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
416 TII.get(ARM::t2LDRpci))
417 .addReg(DestReg).addConstantPoolIndex(Idx));
419 // The extra reg and immediate are for addrmode2.
420 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
422 .addReg(DestReg).addConstantPoolIndex(Idx)
423 .addReg(0).addImm(0));
428 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
429 EVT VT = TLI.getValueType(C->getType(), true);
431 // Only handle simple types.
432 if (!VT.isSimple()) return 0;
434 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
435 return ARMMaterializeFP(CFP, VT);
436 return ARMMaterializeInt(C);
439 bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
440 VT = TLI.getValueType(Ty, true);
442 // Only handle simple types.
443 if (VT == MVT::Other || !VT.isSimple()) return false;
445 // Handle all legal types, i.e. a register that will directly hold this
447 return TLI.isTypeLegal(VT);
450 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
451 if (isTypeLegal(Ty, VT)) return true;
453 // If this is a type than can be sign or zero-extended to a basic operation
454 // go ahead and accept it now.
455 if (VT == MVT::i8 || VT == MVT::i16)
461 // Computes the Reg+Offset to get to an object.
462 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
464 // Some boilerplate from the X86 FastISel.
465 const User *U = NULL;
466 unsigned Opcode = Instruction::UserOp1;
467 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
468 // Don't walk into other basic blocks; it's possible we haven't
469 // visited them yet, so the instructions may not yet be assigned
470 // virtual registers.
471 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
473 Opcode = I->getOpcode();
475 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
476 Opcode = C->getOpcode();
480 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
481 if (Ty->getAddressSpace() > 255)
482 // Fast instruction selection doesn't support the special
489 case Instruction::Alloca: {
490 assert(false && "Alloca should have been handled earlier!");
495 // FIXME: Handle global variables.
496 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
501 // Try to get this in a register if nothing else has worked.
502 Reg = getRegForValue(Obj);
503 if (Reg == 0) return false;
505 // Since the offset may be too large for the load instruction
506 // get the reg+offset into a register.
507 // TODO: Verify the additions work, otherwise we'll need to add the
508 // offset instead of 0 to the instructions and do all sorts of operand
510 // TODO: Optimize this somewhat.
512 ARMCC::CondCodes Pred = ARMCC::AL;
513 unsigned PredReg = 0;
516 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
517 Reg, Reg, Offset, Pred, PredReg,
518 static_cast<const ARMBaseInstrInfo&>(TII));
520 assert(AFI->isThumb2Function());
521 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
522 Reg, Reg, Offset, Pred, PredReg,
523 static_cast<const ARMBaseInstrInfo&>(TII));
529 bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
530 Value *Op0 = I->getOperand(0);
532 // Verify it's an alloca.
533 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
534 DenseMap<const AllocaInst*, int>::iterator SI =
535 FuncInfo.StaticAllocaMap.find(AI);
537 if (SI != FuncInfo.StaticAllocaMap.end()) {
538 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
539 unsigned ResultReg = createResultReg(RC);
540 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
541 ResultReg, SI->second, RC,
542 TM.getRegisterInfo());
543 UpdateValueMap(I, ResultReg);
550 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
551 unsigned Reg, int Offset) {
553 assert(VT.isSimple() && "Non-simple types are invalid here!");
556 switch (VT.getSimpleVT().SimpleTy) {
558 assert(false && "Trying to emit for an unhandled type!");
561 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
565 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
569 Opc = isThumb ? ARM::tLDR : ARM::LDR;
573 ResultReg = createResultReg(TLI.getRegClassFor(VT));
575 // TODO: Fix the Addressing modes so that these can share some code.
576 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
578 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
579 TII.get(Opc), ResultReg)
580 .addReg(Reg).addImm(Offset).addReg(0));
582 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
583 TII.get(Opc), ResultReg)
584 .addReg(Reg).addReg(0).addImm(Offset));
588 bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
589 // Verify we have a legal type before going any further.
591 if (!isLoadTypeLegal(I->getType(), VT))
594 // If we're an alloca we know we have a frame index and can emit the load
595 // directly in short order.
596 if (ARMLoadAlloca(I, VT))
599 // Our register and offset with innocuous defaults.
603 // See if we can handle this as Reg + Offset
604 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
608 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
610 UpdateValueMap(I, ResultReg);
614 bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
615 Value *Op1 = I->getOperand(1);
617 // Verify it's an alloca.
618 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
619 DenseMap<const AllocaInst*, int>::iterator SI =
620 FuncInfo.StaticAllocaMap.find(AI);
622 if (SI != FuncInfo.StaticAllocaMap.end()) {
623 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
624 assert(SrcReg != 0 && "Nothing to store!");
625 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
626 SrcReg, true /*isKill*/, SI->second, RC,
627 TM.getRegisterInfo());
634 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
635 unsigned DstReg, int Offset) {
637 switch (VT.getSimpleVT().SimpleTy) {
638 default: return false;
640 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
641 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
642 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
644 if (!Subtarget->hasVFP2()) return false;
648 if (!Subtarget->hasVFP2()) return false;
654 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
655 TII.get(StrOpc), SrcReg)
656 .addReg(DstReg).addImm(Offset).addReg(0));
658 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
659 TII.get(StrOpc), SrcReg)
660 .addReg(DstReg).addReg(0).addImm(Offset));
665 bool ARMFastISel::ARMSelectStore(const Instruction *I) {
666 Value *Op0 = I->getOperand(0);
669 // Yay type legalization
671 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
674 // Get the value to be stored into a register.
675 SrcReg = getRegForValue(Op0);
679 // If we're an alloca we know we have a frame index and can emit the store
681 if (ARMStoreAlloca(I, SrcReg, VT))
684 // Our register and offset with innocuous defaults.
688 // See if we can handle this as Reg + Offset
689 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
692 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
697 bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
698 const BranchInst *BI = cast<BranchInst>(I);
699 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
700 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
702 // Simple branch support.
703 unsigned CondReg = getRegForValue(BI->getCondition());
704 if (CondReg == 0) return false;
706 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
707 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
708 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
709 .addReg(CondReg).addReg(CondReg));
710 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
711 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
712 FastEmitBranch(FBB, DL);
713 FuncInfo.MBB->addSuccessor(TBB);
717 bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
718 const CmpInst *CI = cast<CmpInst>(I);
721 const Type *Ty = CI->getOperand(0)->getType();
722 if (!isTypeLegal(Ty, VT))
725 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
726 if (isFloat && !Subtarget->hasVFP2())
730 switch (VT.getSimpleVT().SimpleTy) {
731 default: return false;
732 // TODO: Verify compares.
734 CmpOpc = ARM::VCMPES;
737 CmpOpc = ARM::VCMPED;
740 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
744 unsigned Arg1 = getRegForValue(CI->getOperand(0));
745 if (Arg1 == 0) return false;
747 unsigned Arg2 = getRegForValue(CI->getOperand(1));
748 if (Arg2 == 0) return false;
750 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
751 .addReg(Arg1).addReg(Arg2));
753 // For floating point we need to move the result to a comparison register
754 // that we can then use for branches.
756 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
757 TII.get(ARM::FMSTAT)));
759 // TODO: How to update the value map when there's no result reg?
763 bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
764 // Make sure we have VFP and that we're extending float to double.
765 if (!Subtarget->hasVFP2()) return false;
767 Value *V = I->getOperand(0);
768 if (!I->getType()->isDoubleTy() ||
769 !V->getType()->isFloatTy()) return false;
771 unsigned Op = getRegForValue(V);
772 if (Op == 0) return false;
774 unsigned Result = createResultReg(ARM::DPRRegisterClass);
775 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
776 TII.get(ARM::VCVTDS), Result)
778 UpdateValueMap(I, Result);
782 bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
783 // Make sure we have VFP and that we're truncating double to float.
784 if (!Subtarget->hasVFP2()) return false;
786 Value *V = I->getOperand(0);
787 if (!I->getType()->isFloatTy() ||
788 !V->getType()->isDoubleTy()) return false;
790 unsigned Op = getRegForValue(V);
791 if (Op == 0) return false;
793 unsigned Result = createResultReg(ARM::SPRRegisterClass);
794 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
795 TII.get(ARM::VCVTSD), Result)
797 UpdateValueMap(I, Result);
801 bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
802 // Make sure we have VFP.
803 if (!Subtarget->hasVFP2()) return false;
806 const Type *Ty = I->getType();
807 if (!isTypeLegal(Ty, DstVT))
810 unsigned Op = getRegForValue(I->getOperand(0));
811 if (Op == 0) return false;
813 // The conversion routine works on fp-reg to fp-reg and the operand above
814 // was an integer, move it to the fp registers if possible.
815 unsigned FP = ARMMoveToFPReg(DstVT, Op);
816 if (FP == 0) return false;
819 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
820 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
823 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
824 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
827 UpdateValueMap(I, ResultReg);
831 bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
832 // Make sure we have VFP.
833 if (!Subtarget->hasVFP2()) return false;
836 const Type *RetTy = I->getType();
837 if (!isTypeLegal(RetTy, DstVT))
840 unsigned Op = getRegForValue(I->getOperand(0));
841 if (Op == 0) return false;
844 const Type *OpTy = I->getOperand(0)->getType();
845 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
846 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
848 EVT OpVT = TLI.getValueType(OpTy, true);
850 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
851 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
855 // This result needs to be in an integer register, but the conversion only
856 // takes place in fp-regs.
857 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
858 if (IntReg == 0) return false;
860 UpdateValueMap(I, IntReg);
864 bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
865 EVT VT = TLI.getValueType(I->getType(), true);
867 // We can get here in the case when we want to use NEON for our fp
868 // operations, but can't figure out how to. Just use the vfp instructions
870 // FIXME: It'd be nice to use NEON instructions.
871 const Type *Ty = I->getType();
872 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
873 if (isFloat && !Subtarget->hasVFP2())
876 unsigned Op1 = getRegForValue(I->getOperand(0));
877 if (Op1 == 0) return false;
879 unsigned Op2 = getRegForValue(I->getOperand(1));
880 if (Op2 == 0) return false;
883 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
884 VT.getSimpleVT().SimpleTy == MVT::i64;
886 default: return false;
888 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
891 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
894 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
897 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
898 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
899 TII.get(Opc), ResultReg)
900 .addReg(Op1).addReg(Op2));
901 UpdateValueMap(I, ResultReg);
905 // Call Handling Code
907 // This is largely taken directly from CCAssignFnForNode - we don't support
908 // varargs in FastISel so that part has been removed.
909 // TODO: We may not support all of this.
910 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
913 llvm_unreachable("Unsupported calling convention");
915 case CallingConv::Fast:
916 // Use target triple & subtarget features to do actual dispatch.
917 if (Subtarget->isAAPCS_ABI()) {
918 if (Subtarget->hasVFP2() &&
919 FloatABIType == FloatABI::Hard)
920 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
922 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
924 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
925 case CallingConv::ARM_AAPCS_VFP:
926 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
927 case CallingConv::ARM_AAPCS:
928 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
929 case CallingConv::ARM_APCS:
930 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
934 // TODO: SoftFP support.
935 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
936 // No Thumb-1 for now.
937 if (isThumb && !AFI->isThumb2Function()) return false;
939 switch (I->getOpcode()) {
940 case Instruction::Load:
941 return ARMSelectLoad(I);
942 case Instruction::Store:
943 return ARMSelectStore(I);
944 case Instruction::Br:
945 return ARMSelectBranch(I);
946 case Instruction::ICmp:
947 case Instruction::FCmp:
948 return ARMSelectCmp(I);
949 case Instruction::FPExt:
950 return ARMSelectFPExt(I);
951 case Instruction::FPTrunc:
952 return ARMSelectFPTrunc(I);
953 case Instruction::SIToFP:
954 return ARMSelectSIToFP(I);
955 case Instruction::FPToSI:
956 return ARMSelectFPToSI(I);
957 case Instruction::FAdd:
958 return ARMSelectBinaryOp(I, ISD::FADD);
959 case Instruction::FSub:
960 return ARMSelectBinaryOp(I, ISD::FSUB);
961 case Instruction::FMul:
962 return ARMSelectBinaryOp(I, ISD::FMUL);
969 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
970 if (EnableARMFastISel) return new ARMFastISel(funcInfo);