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 "ARMRegisterInfo.h"
19 #include "ARMTargetMachine.h"
20 #include "ARMSubtarget.h"
21 #include "llvm/CallingConv.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/CodeGen/Analysis.h"
27 #include "llvm/CodeGen/FastISel.h"
28 #include "llvm/CodeGen/FunctionLoweringInfo.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineConstantPool.h"
32 #include "llvm/CodeGen/MachineFrameInfo.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/Support/CallSite.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/GetElementPtrTypeIterator.h"
38 #include "llvm/Target/TargetData.h"
39 #include "llvm/Target/TargetInstrInfo.h"
40 #include "llvm/Target/TargetLowering.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetOptions.h"
46 EnableARMFastISel("arm-fast-isel",
47 cl::desc("Turn on experimental ARM fast-isel support"),
48 cl::init(false), cl::Hidden);
52 class ARMFastISel : public FastISel {
54 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
55 /// make the right decision when generating code for different targets.
56 const ARMSubtarget *Subtarget;
57 const TargetMachine &TM;
58 const TargetInstrInfo &TII;
59 const TargetLowering &TLI;
60 const ARMFunctionInfo *AFI;
62 // Convenience variable to avoid checking all the time.
66 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
68 TM(funcInfo.MF->getTarget()),
69 TII(*TM.getInstrInfo()),
70 TLI(*TM.getTargetLowering()) {
71 Subtarget = &TM.getSubtarget<ARMSubtarget>();
72 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
73 isThumb = AFI->isThumbFunction();
76 // Code from FastISel.cpp.
77 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
78 const TargetRegisterClass *RC);
79 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
80 const TargetRegisterClass *RC,
81 unsigned Op0, bool Op0IsKill);
82 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
83 const TargetRegisterClass *RC,
84 unsigned Op0, bool Op0IsKill,
85 unsigned Op1, bool Op1IsKill);
86 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill,
90 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
91 const TargetRegisterClass *RC,
92 unsigned Op0, bool Op0IsKill,
93 const ConstantFP *FPImm);
94 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
95 const TargetRegisterClass *RC,
97 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
98 const TargetRegisterClass *RC,
99 unsigned Op0, bool Op0IsKill,
100 unsigned Op1, bool Op1IsKill,
102 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
103 unsigned Op0, bool Op0IsKill,
106 // Backend specific FastISel code.
107 virtual bool TargetSelectInstruction(const Instruction *I);
108 virtual unsigned TargetMaterializeConstant(const Constant *C);
110 #include "ARMGenFastISel.inc"
112 // Instruction selection routines.
113 virtual bool ARMSelectLoad(const Instruction *I);
114 virtual bool ARMSelectStore(const Instruction *I);
115 virtual bool ARMSelectBranch(const Instruction *I);
116 virtual bool ARMSelectCmp(const Instruction *I);
117 virtual bool ARMSelectFPExt(const Instruction *I);
118 virtual bool ARMSelectFPTrunc(const Instruction *I);
119 virtual bool ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
120 virtual bool ARMSelectSIToFP(const Instruction *I);
121 virtual bool ARMSelectFPToSI(const Instruction *I);
125 bool isTypeLegal(const Type *Ty, EVT &VT);
126 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
127 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
128 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
129 bool ARMLoadAlloca(const Instruction *I, EVT VT);
130 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
131 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
132 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
133 unsigned ARMMaterializeInt(const Constant *C);
134 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
135 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
137 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
138 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
141 } // end anonymous namespace
143 // #include "ARMGenCallingConv.inc"
145 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
146 // we don't care about implicit defs here, just places we'll need to add a
147 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
148 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
149 const TargetInstrDesc &TID = MI->getDesc();
150 if (!TID.hasOptionalDef())
153 // Look to see if our OptionalDef is defining CPSR or CCR.
154 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
155 const MachineOperand &MO = MI->getOperand(i);
156 if (!MO.isReg() || !MO.isDef()) continue;
157 if (MO.getReg() == ARM::CPSR)
163 // If the machine is predicable go ahead and add the predicate operands, if
164 // it needs default CC operands add those.
165 const MachineInstrBuilder &
166 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
167 MachineInstr *MI = &*MIB;
169 // Do we use a predicate?
170 if (TII.isPredicable(MI))
173 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
174 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
176 if (DefinesOptionalPredicate(MI, &CPSR)) {
185 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
186 const TargetRegisterClass* RC) {
187 unsigned ResultReg = createResultReg(RC);
188 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
190 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
194 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
195 const TargetRegisterClass *RC,
196 unsigned Op0, bool Op0IsKill) {
197 unsigned ResultReg = createResultReg(RC);
198 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
200 if (II.getNumDefs() >= 1)
201 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
202 .addReg(Op0, Op0IsKill * RegState::Kill));
204 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
205 .addReg(Op0, Op0IsKill * RegState::Kill));
206 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
207 TII.get(TargetOpcode::COPY), ResultReg)
208 .addReg(II.ImplicitDefs[0]));
213 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
214 const TargetRegisterClass *RC,
215 unsigned Op0, bool Op0IsKill,
216 unsigned Op1, bool Op1IsKill) {
217 unsigned ResultReg = createResultReg(RC);
218 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
220 if (II.getNumDefs() >= 1)
221 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
222 .addReg(Op0, Op0IsKill * RegState::Kill)
223 .addReg(Op1, Op1IsKill * RegState::Kill));
225 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
226 .addReg(Op0, Op0IsKill * RegState::Kill)
227 .addReg(Op1, Op1IsKill * RegState::Kill));
228 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
229 TII.get(TargetOpcode::COPY), ResultReg)
230 .addReg(II.ImplicitDefs[0]));
235 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
236 const TargetRegisterClass *RC,
237 unsigned Op0, bool Op0IsKill,
239 unsigned ResultReg = createResultReg(RC);
240 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
242 if (II.getNumDefs() >= 1)
243 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
244 .addReg(Op0, Op0IsKill * RegState::Kill)
247 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
248 .addReg(Op0, Op0IsKill * RegState::Kill)
250 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
251 TII.get(TargetOpcode::COPY), ResultReg)
252 .addReg(II.ImplicitDefs[0]));
257 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
258 const TargetRegisterClass *RC,
259 unsigned Op0, bool Op0IsKill,
260 const ConstantFP *FPImm) {
261 unsigned ResultReg = createResultReg(RC);
262 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
264 if (II.getNumDefs() >= 1)
265 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
266 .addReg(Op0, Op0IsKill * RegState::Kill)
269 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
270 .addReg(Op0, Op0IsKill * RegState::Kill)
272 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
273 TII.get(TargetOpcode::COPY), ResultReg)
274 .addReg(II.ImplicitDefs[0]));
279 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
280 const TargetRegisterClass *RC,
281 unsigned Op0, bool Op0IsKill,
282 unsigned Op1, bool Op1IsKill,
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)
290 .addReg(Op1, Op1IsKill * RegState::Kill)
293 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
294 .addReg(Op0, Op0IsKill * RegState::Kill)
295 .addReg(Op1, Op1IsKill * RegState::Kill)
297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
298 TII.get(TargetOpcode::COPY), ResultReg)
299 .addReg(II.ImplicitDefs[0]));
304 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
305 const TargetRegisterClass *RC,
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)
314 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
316 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
317 TII.get(TargetOpcode::COPY), ResultReg)
318 .addReg(II.ImplicitDefs[0]));
323 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
324 unsigned Op0, bool Op0IsKill,
326 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
327 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
328 "Cannot yet extract from physregs");
329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
330 DL, TII.get(TargetOpcode::COPY), ResultReg)
331 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
335 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
336 // checks from the various callers.
337 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
338 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
340 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
341 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
342 TII.get(ARM::VMOVRS), MoveReg)
347 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
348 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
350 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
351 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
352 TII.get(ARM::VMOVSR), MoveReg)
357 // For double width floating point we need to materialize two constants
358 // (the high and the low) into integer registers then use a move to get
359 // the combined constant into an FP reg.
360 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
361 const APFloat Val = CFP->getValueAPF();
362 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
364 // This checks to see if we can use VFP3 instructions to materialize
365 // a constant, otherwise we have to go through the constant pool.
366 if (TLI.isFPImmLegal(Val, VT)) {
367 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
368 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
369 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
375 // Require VFP2 for loading fp constants.
376 if (!Subtarget->hasVFP2()) return false;
378 // MachineConstantPool wants an explicit alignment.
379 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
381 // TODO: Figure out if this is correct.
382 Align = TD.getTypeAllocSize(CFP->getType());
384 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
385 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
386 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
388 // The extra reg is for addrmode5.
389 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
390 .addReg(DestReg).addConstantPoolIndex(Idx)
395 // TODO: Verify 64-bit.
396 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
397 // MachineConstantPool wants an explicit alignment.
398 unsigned Align = TD.getPrefTypeAlignment(C->getType());
400 // TODO: Figure out if this is correct.
401 Align = TD.getTypeAllocSize(C->getType());
403 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
404 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
407 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
408 TII.get(ARM::t2LDRpci))
409 .addReg(DestReg).addConstantPoolIndex(Idx));
411 // The extra reg and immediate are for addrmode2.
412 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
414 .addReg(DestReg).addConstantPoolIndex(Idx)
415 .addReg(0).addImm(0));
420 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
421 EVT VT = TLI.getValueType(C->getType(), true);
423 // Only handle simple types.
424 if (!VT.isSimple()) return 0;
426 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
427 return ARMMaterializeFP(CFP, VT);
428 return ARMMaterializeInt(C);
431 bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
432 VT = TLI.getValueType(Ty, true);
434 // Only handle simple types.
435 if (VT == MVT::Other || !VT.isSimple()) return false;
437 // Handle all legal types, i.e. a register that will directly hold this
439 return TLI.isTypeLegal(VT);
442 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
443 if (isTypeLegal(Ty, VT)) return true;
445 // If this is a type than can be sign or zero-extended to a basic operation
446 // go ahead and accept it now.
447 if (VT == MVT::i8 || VT == MVT::i16)
453 // Computes the Reg+Offset to get to an object.
454 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
456 // Some boilerplate from the X86 FastISel.
457 const User *U = NULL;
458 unsigned Opcode = Instruction::UserOp1;
459 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
460 // Don't walk into other basic blocks; it's possible we haven't
461 // visited them yet, so the instructions may not yet be assigned
462 // virtual registers.
463 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
465 Opcode = I->getOpcode();
467 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
468 Opcode = C->getOpcode();
472 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
473 if (Ty->getAddressSpace() > 255)
474 // Fast instruction selection doesn't support the special
481 case Instruction::Alloca: {
482 assert(false && "Alloca should have been handled earlier!");
487 // FIXME: Handle global variables.
488 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
493 // Try to get this in a register if nothing else has worked.
494 Reg = getRegForValue(Obj);
495 if (Reg == 0) return false;
497 // Since the offset may be too large for the load instruction
498 // get the reg+offset into a register.
499 // TODO: Verify the additions work, otherwise we'll need to add the
500 // offset instead of 0 to the instructions and do all sorts of operand
502 // TODO: Optimize this somewhat.
504 ARMCC::CondCodes Pred = ARMCC::AL;
505 unsigned PredReg = 0;
508 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
509 Reg, Reg, Offset, Pred, PredReg,
510 static_cast<const ARMBaseInstrInfo&>(TII));
512 assert(AFI->isThumb2Function());
513 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
514 Reg, Reg, Offset, Pred, PredReg,
515 static_cast<const ARMBaseInstrInfo&>(TII));
521 bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
522 Value *Op0 = I->getOperand(0);
524 // Verify it's an alloca.
525 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
526 DenseMap<const AllocaInst*, int>::iterator SI =
527 FuncInfo.StaticAllocaMap.find(AI);
529 if (SI != FuncInfo.StaticAllocaMap.end()) {
530 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
531 unsigned ResultReg = createResultReg(RC);
532 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
533 ResultReg, SI->second, RC,
534 TM.getRegisterInfo());
535 UpdateValueMap(I, ResultReg);
542 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
543 unsigned Reg, int Offset) {
545 assert(VT.isSimple() && "Non-simple types are invalid here!");
548 switch (VT.getSimpleVT().SimpleTy) {
550 assert(false && "Trying to emit for an unhandled type!");
553 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
557 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
561 Opc = isThumb ? ARM::tLDR : ARM::LDR;
565 ResultReg = createResultReg(TLI.getRegClassFor(VT));
567 // TODO: Fix the Addressing modes so that these can share some code.
568 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
570 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
571 TII.get(Opc), ResultReg)
572 .addReg(Reg).addImm(Offset).addReg(0));
574 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
575 TII.get(Opc), ResultReg)
576 .addReg(Reg).addReg(0).addImm(Offset));
580 bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
581 // Verify we have a legal type before going any further.
583 if (!isLoadTypeLegal(I->getType(), VT))
586 // If we're an alloca we know we have a frame index and can emit the load
587 // directly in short order.
588 if (ARMLoadAlloca(I, VT))
591 // Our register and offset with innocuous defaults.
595 // See if we can handle this as Reg + Offset
596 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
600 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
602 UpdateValueMap(I, ResultReg);
606 bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
607 Value *Op1 = I->getOperand(1);
609 // Verify it's an alloca.
610 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
611 DenseMap<const AllocaInst*, int>::iterator SI =
612 FuncInfo.StaticAllocaMap.find(AI);
614 if (SI != FuncInfo.StaticAllocaMap.end()) {
615 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
616 assert(SrcReg != 0 && "Nothing to store!");
617 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
618 SrcReg, true /*isKill*/, SI->second, RC,
619 TM.getRegisterInfo());
626 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
627 unsigned DstReg, int Offset) {
629 switch (VT.getSimpleVT().SimpleTy) {
630 default: return false;
632 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
633 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
634 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
636 if (!Subtarget->hasVFP2()) return false;
640 if (!Subtarget->hasVFP2()) return false;
646 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
647 TII.get(StrOpc), SrcReg)
648 .addReg(DstReg).addImm(Offset).addReg(0));
650 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
651 TII.get(StrOpc), SrcReg)
652 .addReg(DstReg).addReg(0).addImm(Offset));
657 bool ARMFastISel::ARMSelectStore(const Instruction *I) {
658 Value *Op0 = I->getOperand(0);
661 // Yay type legalization
663 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
666 // Get the value to be stored into a register.
667 SrcReg = getRegForValue(Op0);
671 // If we're an alloca we know we have a frame index and can emit the store
673 if (ARMStoreAlloca(I, SrcReg, VT))
676 // Our register and offset with innocuous defaults.
680 // See if we can handle this as Reg + Offset
681 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
684 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
689 bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
690 const BranchInst *BI = cast<BranchInst>(I);
691 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
692 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
694 // Simple branch support.
695 unsigned CondReg = getRegForValue(BI->getCondition());
696 if (CondReg == 0) return false;
698 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
699 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
700 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
701 .addReg(CondReg).addReg(CondReg));
702 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
703 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
704 FastEmitBranch(FBB, DL);
705 FuncInfo.MBB->addSuccessor(TBB);
709 bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
710 const CmpInst *CI = cast<CmpInst>(I);
713 const Type *Ty = CI->getOperand(0)->getType();
714 if (!isTypeLegal(Ty, VT))
717 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
718 if (isFloat && !Subtarget->hasVFP2())
722 switch (VT.getSimpleVT().SimpleTy) {
723 default: return false;
724 // TODO: Verify compares.
726 CmpOpc = ARM::VCMPES;
729 CmpOpc = ARM::VCMPED;
732 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
736 unsigned Arg1 = getRegForValue(CI->getOperand(0));
737 if (Arg1 == 0) return false;
739 unsigned Arg2 = getRegForValue(CI->getOperand(1));
740 if (Arg2 == 0) return false;
742 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
743 .addReg(Arg1).addReg(Arg2));
745 // For floating point we need to move the result to a comparison register
746 // that we can then use for branches.
748 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
749 TII.get(ARM::FMSTAT)));
751 // TODO: How to update the value map when there's no result reg?
755 bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
756 // Make sure we have VFP and that we're extending float to double.
757 if (!Subtarget->hasVFP2()) return false;
759 Value *V = I->getOperand(0);
760 if (!I->getType()->isDoubleTy() ||
761 !V->getType()->isFloatTy()) return false;
763 unsigned Op = getRegForValue(V);
764 if (Op == 0) return false;
766 unsigned Result = createResultReg(ARM::DPRRegisterClass);
767 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
768 TII.get(ARM::VCVTDS), Result)
770 UpdateValueMap(I, Result);
774 bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
775 // Make sure we have VFP and that we're truncating double to float.
776 if (!Subtarget->hasVFP2()) return false;
778 Value *V = I->getOperand(0);
779 if (!I->getType()->isFloatTy() ||
780 !V->getType()->isDoubleTy()) return false;
782 unsigned Op = getRegForValue(V);
783 if (Op == 0) return false;
785 unsigned Result = createResultReg(ARM::SPRRegisterClass);
786 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
787 TII.get(ARM::VCVTSD), Result)
789 UpdateValueMap(I, Result);
793 bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
794 // Make sure we have VFP.
795 if (!Subtarget->hasVFP2()) return false;
798 const Type *Ty = I->getType();
799 if (!isTypeLegal(Ty, DstVT))
802 unsigned Op = getRegForValue(I->getOperand(0));
803 if (Op == 0) return false;
805 // The conversion routine works on fp-reg to fp-reg and the operand above
806 // was an integer, move it to the fp registers if possible.
807 unsigned FP = ARMMoveToFPReg(DstVT, Op);
808 if (FP == 0) return false;
811 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
812 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
815 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
816 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
819 UpdateValueMap(I, ResultReg);
823 bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
824 // Make sure we have VFP.
825 if (!Subtarget->hasVFP2()) return false;
828 const Type *RetTy = I->getType();
829 if (!isTypeLegal(RetTy, VT))
832 unsigned Op = getRegForValue(I->getOperand(0));
833 if (Op == 0) return false;
836 const Type *OpTy = I->getOperand(0)->getType();
837 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
838 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
840 EVT OpVT = TLI.getValueType(OpTy, true);
842 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
843 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
847 // This result needs to be in an integer register, but the conversion only
848 // takes place in fp-regs.
849 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
850 if (IntReg == 0) return false;
852 UpdateValueMap(I, IntReg);
856 bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
857 EVT VT = TLI.getValueType(I->getType(), true);
859 // We can get here in the case when we want to use NEON for our fp
860 // operations, but can't figure out how to. Just use the vfp instructions
862 // FIXME: It'd be nice to use NEON instructions.
863 const Type *Ty = I->getType();
864 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
865 if (isFloat && !Subtarget->hasVFP2())
868 unsigned Op1 = getRegForValue(I->getOperand(0));
869 if (Op1 == 0) return false;
871 unsigned Op2 = getRegForValue(I->getOperand(1));
872 if (Op2 == 0) return false;
875 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
876 VT.getSimpleVT().SimpleTy == MVT::i64;
878 default: return false;
880 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
883 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
886 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
889 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
890 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
891 TII.get(Opc), ResultReg)
892 .addReg(Op1).addReg(Op2));
893 UpdateValueMap(I, ResultReg);
897 // TODO: SoftFP support.
898 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
899 // No Thumb-1 for now.
900 if (isThumb && !AFI->isThumb2Function()) return false;
902 switch (I->getOpcode()) {
903 case Instruction::Load:
904 return ARMSelectLoad(I);
905 case Instruction::Store:
906 return ARMSelectStore(I);
907 case Instruction::Br:
908 return ARMSelectBranch(I);
909 case Instruction::ICmp:
910 case Instruction::FCmp:
911 return ARMSelectCmp(I);
912 case Instruction::FPExt:
913 return ARMSelectFPExt(I);
914 case Instruction::FPTrunc:
915 return ARMSelectFPTrunc(I);
916 case Instruction::SIToFP:
917 return ARMSelectSIToFP(I);
918 case Instruction::FPToSI:
919 return ARMSelectFPToSI(I);
920 case Instruction::FAdd:
921 return ARMSelectBinaryOp(I, ISD::FADD);
922 case Instruction::FSub:
923 return ARMSelectBinaryOp(I, ISD::FSUB);
924 case Instruction::FMul:
925 return ARMSelectBinaryOp(I, ISD::FMUL);
932 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
933 if (EnableARMFastISel) return new ARMFastISel(funcInfo);