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);
136 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
137 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
140 } // end anonymous namespace
142 // #include "ARMGenCallingConv.inc"
144 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
145 // we don't care about implicit defs here, just places we'll need to add a
146 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
147 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
148 const TargetInstrDesc &TID = MI->getDesc();
149 if (!TID.hasOptionalDef())
152 // Look to see if our OptionalDef is defining CPSR or CCR.
153 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
154 const MachineOperand &MO = MI->getOperand(i);
155 if (!MO.isReg() || !MO.isDef()) continue;
156 if (MO.getReg() == ARM::CPSR)
162 // If the machine is predicable go ahead and add the predicate operands, if
163 // it needs default CC operands add those.
164 const MachineInstrBuilder &
165 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
166 MachineInstr *MI = &*MIB;
168 // Do we use a predicate?
169 if (TII.isPredicable(MI))
172 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
173 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
175 if (DefinesOptionalPredicate(MI, &CPSR)) {
184 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
185 const TargetRegisterClass* RC) {
186 unsigned ResultReg = createResultReg(RC);
187 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
189 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
193 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
194 const TargetRegisterClass *RC,
195 unsigned Op0, bool Op0IsKill) {
196 unsigned ResultReg = createResultReg(RC);
197 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
199 if (II.getNumDefs() >= 1)
200 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
201 .addReg(Op0, Op0IsKill * RegState::Kill));
203 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
204 .addReg(Op0, Op0IsKill * RegState::Kill));
205 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
206 TII.get(TargetOpcode::COPY), ResultReg)
207 .addReg(II.ImplicitDefs[0]));
212 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
213 const TargetRegisterClass *RC,
214 unsigned Op0, bool Op0IsKill,
215 unsigned Op1, bool Op1IsKill) {
216 unsigned ResultReg = createResultReg(RC);
217 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
219 if (II.getNumDefs() >= 1)
220 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
221 .addReg(Op0, Op0IsKill * RegState::Kill)
222 .addReg(Op1, Op1IsKill * RegState::Kill));
224 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
225 .addReg(Op0, Op0IsKill * RegState::Kill)
226 .addReg(Op1, Op1IsKill * RegState::Kill));
227 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
228 TII.get(TargetOpcode::COPY), ResultReg)
229 .addReg(II.ImplicitDefs[0]));
234 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
235 const TargetRegisterClass *RC,
236 unsigned Op0, bool Op0IsKill,
238 unsigned ResultReg = createResultReg(RC);
239 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
241 if (II.getNumDefs() >= 1)
242 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
243 .addReg(Op0, Op0IsKill * RegState::Kill)
246 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
247 .addReg(Op0, Op0IsKill * RegState::Kill)
249 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
250 TII.get(TargetOpcode::COPY), ResultReg)
251 .addReg(II.ImplicitDefs[0]));
256 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
257 const TargetRegisterClass *RC,
258 unsigned Op0, bool Op0IsKill,
259 const ConstantFP *FPImm) {
260 unsigned ResultReg = createResultReg(RC);
261 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
263 if (II.getNumDefs() >= 1)
264 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
265 .addReg(Op0, Op0IsKill * RegState::Kill)
268 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
269 .addReg(Op0, Op0IsKill * RegState::Kill)
271 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
272 TII.get(TargetOpcode::COPY), ResultReg)
273 .addReg(II.ImplicitDefs[0]));
278 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
279 const TargetRegisterClass *RC,
280 unsigned Op0, bool Op0IsKill,
281 unsigned Op1, bool Op1IsKill,
283 unsigned ResultReg = createResultReg(RC);
284 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
286 if (II.getNumDefs() >= 1)
287 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
288 .addReg(Op0, Op0IsKill * RegState::Kill)
289 .addReg(Op1, Op1IsKill * RegState::Kill)
292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
293 .addReg(Op0, Op0IsKill * RegState::Kill)
294 .addReg(Op1, Op1IsKill * RegState::Kill)
296 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
297 TII.get(TargetOpcode::COPY), ResultReg)
298 .addReg(II.ImplicitDefs[0]));
303 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
304 const TargetRegisterClass *RC,
306 unsigned ResultReg = createResultReg(RC);
307 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
309 if (II.getNumDefs() >= 1)
310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
313 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
315 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
316 TII.get(TargetOpcode::COPY), ResultReg)
317 .addReg(II.ImplicitDefs[0]));
322 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
323 unsigned Op0, bool Op0IsKill,
325 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
326 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
327 "Cannot yet extract from physregs");
328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
329 DL, TII.get(TargetOpcode::COPY), ResultReg)
330 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
334 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
335 // If we have a floating point constant we expect it in a floating point
337 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
338 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
339 TII.get(ARM::VMOVRS), MoveReg)
344 // For double width floating point we need to materialize two constants
345 // (the high and the low) into integer registers then use a move to get
346 // the combined constant into an FP reg.
347 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
348 const APFloat Val = CFP->getValueAPF();
349 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
351 // This checks to see if we can use VFP3 instructions to materialize
352 // a constant, otherwise we have to go through the constant pool.
353 if (TLI.isFPImmLegal(Val, VT)) {
354 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
355 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
356 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
362 // No 64-bit at the moment.
363 if (is64bit) return 0;
365 // Load this from the constant pool.
366 unsigned DestReg = ARMMaterializeInt(cast<Constant>(CFP));
368 // If we have a floating point constant we expect it in a floating point
370 return ARMMoveToFPReg(VT, DestReg);
373 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
374 // MachineConstantPool wants an explicit alignment.
375 unsigned Align = TD.getPrefTypeAlignment(C->getType());
377 // TODO: Figure out if this is correct.
378 Align = TD.getTypeAllocSize(C->getType());
380 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
382 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
384 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
385 TII.get(ARM::t2LDRpci))
386 .addReg(DestReg).addConstantPoolIndex(Idx));
388 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
390 .addReg(DestReg).addConstantPoolIndex(Idx)
391 .addReg(0).addImm(0));
396 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
397 EVT VT = TLI.getValueType(C->getType(), true);
399 // Only handle simple types.
400 if (!VT.isSimple()) return 0;
402 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
403 return ARMMaterializeFP(CFP, VT);
404 return ARMMaterializeInt(C);
407 bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
408 VT = TLI.getValueType(Ty, true);
410 // Only handle simple types.
411 if (VT == MVT::Other || !VT.isSimple()) return false;
413 // Handle all legal types, i.e. a register that will directly hold this
415 return TLI.isTypeLegal(VT);
418 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
419 if (isTypeLegal(Ty, VT)) return true;
421 // If this is a type than can be sign or zero-extended to a basic operation
422 // go ahead and accept it now.
423 if (VT == MVT::i8 || VT == MVT::i16)
429 // Computes the Reg+Offset to get to an object.
430 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
432 // Some boilerplate from the X86 FastISel.
433 const User *U = NULL;
434 unsigned Opcode = Instruction::UserOp1;
435 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
436 // Don't walk into other basic blocks; it's possible we haven't
437 // visited them yet, so the instructions may not yet be assigned
438 // virtual registers.
439 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
442 Opcode = I->getOpcode();
444 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
445 Opcode = C->getOpcode();
449 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
450 if (Ty->getAddressSpace() > 255)
451 // Fast instruction selection doesn't support the special
457 //errs() << "Failing Opcode is: " << *Op1 << "\n";
459 case Instruction::Alloca: {
460 assert(false && "Alloca should have been handled earlier!");
465 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
466 //errs() << "Failing GV is: " << GV << "\n";
471 // Try to get this in a register if nothing else has worked.
472 Reg = getRegForValue(Obj);
473 if (Reg == 0) return false;
475 // Since the offset may be too large for the load instruction
476 // get the reg+offset into a register.
477 // TODO: Verify the additions work, otherwise we'll need to add the
478 // offset instead of 0 to the instructions and do all sorts of operand
480 // TODO: Optimize this somewhat.
482 ARMCC::CondCodes Pred = ARMCC::AL;
483 unsigned PredReg = 0;
486 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
487 Reg, Reg, Offset, Pred, PredReg,
488 static_cast<const ARMBaseInstrInfo&>(TII));
490 assert(AFI->isThumb2Function());
491 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
492 Reg, Reg, Offset, Pred, PredReg,
493 static_cast<const ARMBaseInstrInfo&>(TII));
500 bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
501 Value *Op0 = I->getOperand(0);
503 // Verify it's an alloca.
504 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
505 DenseMap<const AllocaInst*, int>::iterator SI =
506 FuncInfo.StaticAllocaMap.find(AI);
508 if (SI != FuncInfo.StaticAllocaMap.end()) {
509 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
510 unsigned ResultReg = createResultReg(RC);
511 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
512 ResultReg, SI->second, RC,
513 TM.getRegisterInfo());
514 UpdateValueMap(I, ResultReg);
521 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
522 unsigned Reg, int Offset) {
524 assert(VT.isSimple() && "Non-simple types are invalid here!");
527 switch (VT.getSimpleVT().SimpleTy) {
529 assert(false && "Trying to emit for an unhandled type!");
532 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
536 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
540 Opc = isThumb ? ARM::tLDR : ARM::LDR;
544 ResultReg = createResultReg(TLI.getRegClassFor(VT));
546 // TODO: Fix the Addressing modes so that these can share some code.
547 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
549 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
550 TII.get(Opc), ResultReg)
551 .addReg(Reg).addImm(Offset).addReg(0));
553 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
554 TII.get(Opc), ResultReg)
555 .addReg(Reg).addReg(0).addImm(Offset));
559 bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
560 Value *Op1 = I->getOperand(1);
562 // Verify it's an alloca.
563 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
564 DenseMap<const AllocaInst*, int>::iterator SI =
565 FuncInfo.StaticAllocaMap.find(AI);
567 if (SI != FuncInfo.StaticAllocaMap.end()) {
568 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
569 assert(SrcReg != 0 && "Nothing to store!");
570 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
571 SrcReg, true /*isKill*/, SI->second, RC,
572 TM.getRegisterInfo());
579 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
580 unsigned DstReg, int Offset) {
582 switch (VT.getSimpleVT().SimpleTy) {
583 default: return false;
585 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
586 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
587 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
589 if (!Subtarget->hasVFP2()) return false;
593 if (!Subtarget->hasVFP2()) return false;
599 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
600 TII.get(StrOpc), SrcReg)
601 .addReg(DstReg).addImm(Offset).addReg(0));
603 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
604 TII.get(StrOpc), SrcReg)
605 .addReg(DstReg).addReg(0).addImm(Offset));
610 bool ARMFastISel::ARMSelectStore(const Instruction *I) {
611 Value *Op0 = I->getOperand(0);
614 // Yay type legalization
616 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
619 // Get the value to be stored into a register.
620 SrcReg = getRegForValue(Op0);
624 // If we're an alloca we know we have a frame index and can emit the store
626 if (ARMStoreAlloca(I, SrcReg, VT))
629 // Our register and offset with innocuous defaults.
633 // See if we can handle this as Reg + Offset
634 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
637 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
642 bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
643 // Verify we have a legal type before going any further.
645 if (!isLoadTypeLegal(I->getType(), VT))
648 // If we're an alloca we know we have a frame index and can emit the load
649 // directly in short order.
650 if (ARMLoadAlloca(I, VT))
653 // Our register and offset with innocuous defaults.
657 // See if we can handle this as Reg + Offset
658 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
662 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
664 UpdateValueMap(I, ResultReg);
668 bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
669 const BranchInst *BI = cast<BranchInst>(I);
670 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
671 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
673 // Simple branch support.
674 unsigned CondReg = getRegForValue(BI->getCondition());
675 if (CondReg == 0) return false;
677 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
678 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
679 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
680 .addReg(CondReg).addReg(CondReg));
681 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
682 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
683 FastEmitBranch(FBB, DL);
684 FuncInfo.MBB->addSuccessor(TBB);
688 bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
689 const CmpInst *CI = cast<CmpInst>(I);
692 const Type *Ty = CI->getOperand(0)->getType();
693 if (!isTypeLegal(Ty, VT))
696 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
697 if (isFloat && !Subtarget->hasVFP2())
701 switch (VT.getSimpleVT().SimpleTy) {
702 default: return false;
703 // TODO: Verify compares.
705 CmpOpc = ARM::VCMPES;
708 CmpOpc = ARM::VCMPED;
711 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
715 unsigned Arg1 = getRegForValue(CI->getOperand(0));
716 if (Arg1 == 0) return false;
718 unsigned Arg2 = getRegForValue(CI->getOperand(1));
719 if (Arg2 == 0) return false;
721 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
722 .addReg(Arg1).addReg(Arg2));
724 // For floating point we need to move the result to a register we can
725 // actually do something with.
727 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
728 TII.get(ARM::FMSTAT)));
730 // TODO: How to update the value map when there's no result reg?
734 bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
735 // Make sure we have VFP and that we're extending float to double.
736 if (!Subtarget->hasVFP2()) return false;
738 Value *V = I->getOperand(0);
739 if (!I->getType()->isDoubleTy() ||
740 !V->getType()->isFloatTy()) return false;
742 unsigned Op = getRegForValue(V);
743 if (Op == 0) return false;
745 unsigned Result = createResultReg(ARM::DPRRegisterClass);
747 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
748 TII.get(ARM::VCVTDS), Result)
750 UpdateValueMap(I, Result);
754 bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
755 // Make sure we have VFP and that we're truncating double to float.
756 if (!Subtarget->hasVFP2()) return false;
758 Value *V = I->getOperand(0);
759 if (!I->getType()->isFloatTy() ||
760 !V->getType()->isDoubleTy()) return false;
762 unsigned Op = getRegForValue(V);
763 if (Op == 0) return false;
765 unsigned Result = createResultReg(ARM::SPRRegisterClass);
767 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
768 TII.get(ARM::VCVTSD), Result)
770 UpdateValueMap(I, Result);
774 bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
775 // Make sure we have VFP.
776 if (!Subtarget->hasVFP2()) return false;
779 const Type *Ty = I->getType();
780 if (!isTypeLegal(Ty, VT))
783 unsigned Op = getRegForValue(I->getOperand(0));
784 if (Op == 0) return false;
787 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
788 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
791 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
792 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
795 UpdateValueMap(I, ResultReg);
799 bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
800 // Make sure we have VFP.
801 if (!Subtarget->hasVFP2()) return false;
804 const Type *RetTy = I->getType();
805 if (!isTypeLegal(RetTy, VT))
808 unsigned Op = getRegForValue(I->getOperand(0));
809 if (Op == 0) return false;
812 const Type *OpTy = I->getOperand(0)->getType();
813 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
814 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
817 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
818 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
821 UpdateValueMap(I, ResultReg);
825 bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
826 EVT VT = TLI.getValueType(I->getType(), true);
828 // We can get here in the case when we want to use NEON for our fp
829 // operations, but can't figure out how to. Just use the vfp instructions
831 // FIXME: It'd be nice to use NEON instructions.
832 const Type *Ty = I->getType();
833 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
834 if (isFloat && !Subtarget->hasVFP2())
837 unsigned Op1 = getRegForValue(I->getOperand(0));
838 if (Op1 == 0) return false;
840 unsigned Op2 = getRegForValue(I->getOperand(1));
841 if (Op2 == 0) return false;
844 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
845 VT.getSimpleVT().SimpleTy == MVT::i64;
847 default: return false;
849 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
852 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
855 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
858 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
859 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
860 TII.get(Opc), ResultReg)
861 .addReg(Op1).addReg(Op2));
862 UpdateValueMap(I, ResultReg);
866 // TODO: SoftFP support.
867 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
868 // No Thumb-1 for now.
869 if (isThumb && !AFI->isThumb2Function()) return false;
871 switch (I->getOpcode()) {
872 case Instruction::Load:
873 return ARMSelectLoad(I);
874 case Instruction::Store:
875 return ARMSelectStore(I);
876 case Instruction::Br:
877 return ARMSelectBranch(I);
878 case Instruction::ICmp:
879 case Instruction::FCmp:
880 return ARMSelectCmp(I);
881 case Instruction::FPExt:
882 return ARMSelectFPExt(I);
883 case Instruction::FPTrunc:
884 return ARMSelectFPTrunc(I);
885 case Instruction::SIToFP:
886 return ARMSelectSIToFP(I);
887 case Instruction::FPToSI:
888 return ARMSelectFPToSI(I);
889 case Instruction::FAdd:
890 return ARMSelectBinaryOp(I, ISD::FADD);
891 case Instruction::FSub:
892 return ARMSelectBinaryOp(I, ISD::FSUB);
893 case Instruction::FMul:
894 return ARMSelectBinaryOp(I, ISD::FMUL);
901 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
902 if (EnableARMFastISel) return new ARMFastISel(funcInfo);