1 //===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ARM-specific support for the FastISel class. Some
11 // of the target-specific code is generated by tablegen in the file
12 // ARMGenFastISel.inc, which is #included here.
14 //===----------------------------------------------------------------------===//
17 #include "ARMBaseInstrInfo.h"
18 #include "ARMCallingConv.h"
19 #include "ARMRegisterInfo.h"
20 #include "ARMTargetMachine.h"
21 #include "ARMSubtarget.h"
22 #include "ARMConstantPoolValue.h"
23 #include "MCTargetDesc/ARMAddressingModes.h"
24 #include "llvm/CallingConv.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/GlobalVariable.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/IntrinsicInst.h"
29 #include "llvm/Module.h"
30 #include "llvm/Operator.h"
31 #include "llvm/CodeGen/Analysis.h"
32 #include "llvm/CodeGen/FastISel.h"
33 #include "llvm/CodeGen/FunctionLoweringInfo.h"
34 #include "llvm/CodeGen/MachineInstrBuilder.h"
35 #include "llvm/CodeGen/MachineModuleInfo.h"
36 #include "llvm/CodeGen/MachineConstantPool.h"
37 #include "llvm/CodeGen/MachineFrameInfo.h"
38 #include "llvm/CodeGen/MachineMemOperand.h"
39 #include "llvm/CodeGen/MachineRegisterInfo.h"
40 #include "llvm/Support/CallSite.h"
41 #include "llvm/Support/CommandLine.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/GetElementPtrTypeIterator.h"
44 #include "llvm/Target/TargetData.h"
45 #include "llvm/Target/TargetInstrInfo.h"
46 #include "llvm/Target/TargetLowering.h"
47 #include "llvm/Target/TargetMachine.h"
48 #include "llvm/Target/TargetOptions.h"
52 DisableARMFastISel("disable-arm-fast-isel",
53 cl::desc("Turn off experimental ARM fast-isel support"),
54 cl::init(false), cl::Hidden);
56 extern cl::opt<bool> EnableARMLongCalls;
60 // All possible address modes, plus some.
61 typedef struct Address {
74 // Innocuous defaults for our address.
76 : BaseType(RegBase), Offset(0) {
81 class ARMFastISel : public FastISel {
83 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
84 /// make the right decision when generating code for different targets.
85 const ARMSubtarget *Subtarget;
86 const TargetMachine &TM;
87 const TargetInstrInfo &TII;
88 const TargetLowering &TLI;
91 // Convenience variables to avoid some queries.
96 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
98 TM(funcInfo.MF->getTarget()),
99 TII(*TM.getInstrInfo()),
100 TLI(*TM.getTargetLowering()) {
101 Subtarget = &TM.getSubtarget<ARMSubtarget>();
102 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
103 isThumb2 = AFI->isThumbFunction();
104 Context = &funcInfo.Fn->getContext();
107 // Code from FastISel.cpp.
108 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
109 const TargetRegisterClass *RC);
110 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
111 const TargetRegisterClass *RC,
112 unsigned Op0, bool Op0IsKill);
113 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
114 const TargetRegisterClass *RC,
115 unsigned Op0, bool Op0IsKill,
116 unsigned Op1, bool Op1IsKill);
117 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
118 const TargetRegisterClass *RC,
119 unsigned Op0, bool Op0IsKill,
120 unsigned Op1, bool Op1IsKill,
121 unsigned Op2, bool Op2IsKill);
122 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
123 const TargetRegisterClass *RC,
124 unsigned Op0, bool Op0IsKill,
126 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
127 const TargetRegisterClass *RC,
128 unsigned Op0, bool Op0IsKill,
129 const ConstantFP *FPImm);
130 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
131 const TargetRegisterClass *RC,
132 unsigned Op0, bool Op0IsKill,
133 unsigned Op1, bool Op1IsKill,
135 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
136 const TargetRegisterClass *RC,
138 virtual unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
139 const TargetRegisterClass *RC,
140 uint64_t Imm1, uint64_t Imm2);
142 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
143 unsigned Op0, bool Op0IsKill,
146 // Backend specific FastISel code.
147 virtual bool TargetSelectInstruction(const Instruction *I);
148 virtual unsigned TargetMaterializeConstant(const Constant *C);
149 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
150 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
153 #include "ARMGenFastISel.inc"
155 // Instruction selection routines.
157 bool SelectLoad(const Instruction *I);
158 bool SelectStore(const Instruction *I);
159 bool SelectBranch(const Instruction *I);
160 bool SelectIndirectBr(const Instruction *I);
161 bool SelectCmp(const Instruction *I);
162 bool SelectFPExt(const Instruction *I);
163 bool SelectFPTrunc(const Instruction *I);
164 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
165 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
166 bool SelectIToFP(const Instruction *I, bool isSigned);
167 bool SelectFPToI(const Instruction *I, bool isSigned);
168 bool SelectDiv(const Instruction *I, bool isSigned);
169 bool SelectRem(const Instruction *I, bool isSigned);
170 bool SelectCall(const Instruction *I, const char *IntrMemName);
171 bool SelectIntrinsicCall(const IntrinsicInst &I);
172 bool SelectSelect(const Instruction *I);
173 bool SelectRet(const Instruction *I);
174 bool SelectTrunc(const Instruction *I);
175 bool SelectIntExt(const Instruction *I);
179 bool isTypeLegal(Type *Ty, MVT &VT);
180 bool isLoadTypeLegal(Type *Ty, MVT &VT);
181 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
183 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
184 unsigned Alignment = 0, bool isZExt = true,
185 bool allocReg = true);
187 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr,
188 unsigned Alignment = 0);
189 bool ARMComputeAddress(const Value *Obj, Address &Addr);
190 void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3);
191 bool ARMIsMemCpySmall(uint64_t Len);
192 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len);
193 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
194 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
195 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
196 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
197 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
198 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
199 unsigned ARMSelectCallOp(const GlobalValue *GV);
201 // Call handling routines.
203 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
204 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
205 SmallVectorImpl<unsigned> &ArgRegs,
206 SmallVectorImpl<MVT> &ArgVTs,
207 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
208 SmallVectorImpl<unsigned> &RegArgs,
211 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
212 const Instruction *I, CallingConv::ID CC,
214 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
216 // OptionalDef handling routines.
218 bool isARMNEONPred(const MachineInstr *MI);
219 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
220 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
221 void AddLoadStoreOperands(EVT VT, Address &Addr,
222 const MachineInstrBuilder &MIB,
223 unsigned Flags, bool useAM3);
226 } // end anonymous namespace
228 #include "ARMGenCallingConv.inc"
230 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
231 // we don't care about implicit defs here, just places we'll need to add a
232 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
233 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
234 if (!MI->hasOptionalDef())
237 // Look to see if our OptionalDef is defining CPSR or CCR.
238 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
239 const MachineOperand &MO = MI->getOperand(i);
240 if (!MO.isReg() || !MO.isDef()) continue;
241 if (MO.getReg() == ARM::CPSR)
247 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
248 const MCInstrDesc &MCID = MI->getDesc();
250 // If we're a thumb2 or not NEON function we were handled via isPredicable.
251 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
252 AFI->isThumb2Function())
255 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
256 if (MCID.OpInfo[i].isPredicate())
262 // If the machine is predicable go ahead and add the predicate operands, if
263 // it needs default CC operands add those.
264 // TODO: If we want to support thumb1 then we'll need to deal with optional
265 // CPSR defs that need to be added before the remaining operands. See s_cc_out
266 // for descriptions why.
267 const MachineInstrBuilder &
268 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
269 MachineInstr *MI = &*MIB;
271 // Do we use a predicate? or...
272 // Are we NEON in ARM mode and have a predicate operand? If so, I know
273 // we're not predicable but add it anyways.
274 if (TII.isPredicable(MI) || isARMNEONPred(MI))
277 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
278 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
280 if (DefinesOptionalPredicate(MI, &CPSR)) {
289 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
290 const TargetRegisterClass* RC) {
291 unsigned ResultReg = createResultReg(RC);
292 const MCInstrDesc &II = TII.get(MachineInstOpcode);
294 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
298 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
299 const TargetRegisterClass *RC,
300 unsigned Op0, bool Op0IsKill) {
301 unsigned ResultReg = createResultReg(RC);
302 const MCInstrDesc &II = TII.get(MachineInstOpcode);
304 if (II.getNumDefs() >= 1)
305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
306 .addReg(Op0, Op0IsKill * RegState::Kill));
308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
309 .addReg(Op0, Op0IsKill * RegState::Kill));
310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
311 TII.get(TargetOpcode::COPY), ResultReg)
312 .addReg(II.ImplicitDefs[0]));
317 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
318 const TargetRegisterClass *RC,
319 unsigned Op0, bool Op0IsKill,
320 unsigned Op1, bool Op1IsKill) {
321 unsigned ResultReg = createResultReg(RC);
322 const MCInstrDesc &II = TII.get(MachineInstOpcode);
324 if (II.getNumDefs() >= 1)
325 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
326 .addReg(Op0, Op0IsKill * RegState::Kill)
327 .addReg(Op1, Op1IsKill * RegState::Kill));
329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
330 .addReg(Op0, Op0IsKill * RegState::Kill)
331 .addReg(Op1, Op1IsKill * RegState::Kill));
332 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
333 TII.get(TargetOpcode::COPY), ResultReg)
334 .addReg(II.ImplicitDefs[0]));
339 unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
340 const TargetRegisterClass *RC,
341 unsigned Op0, bool Op0IsKill,
342 unsigned Op1, bool Op1IsKill,
343 unsigned Op2, bool Op2IsKill) {
344 unsigned ResultReg = createResultReg(RC);
345 const MCInstrDesc &II = TII.get(MachineInstOpcode);
347 if (II.getNumDefs() >= 1)
348 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
349 .addReg(Op0, Op0IsKill * RegState::Kill)
350 .addReg(Op1, Op1IsKill * RegState::Kill)
351 .addReg(Op2, Op2IsKill * RegState::Kill));
353 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
354 .addReg(Op0, Op0IsKill * RegState::Kill)
355 .addReg(Op1, Op1IsKill * RegState::Kill)
356 .addReg(Op2, Op2IsKill * RegState::Kill));
357 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
358 TII.get(TargetOpcode::COPY), ResultReg)
359 .addReg(II.ImplicitDefs[0]));
364 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
365 const TargetRegisterClass *RC,
366 unsigned Op0, bool Op0IsKill,
368 unsigned ResultReg = createResultReg(RC);
369 const MCInstrDesc &II = TII.get(MachineInstOpcode);
371 if (II.getNumDefs() >= 1)
372 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
373 .addReg(Op0, Op0IsKill * RegState::Kill)
376 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
377 .addReg(Op0, Op0IsKill * RegState::Kill)
379 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
380 TII.get(TargetOpcode::COPY), ResultReg)
381 .addReg(II.ImplicitDefs[0]));
386 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
387 const TargetRegisterClass *RC,
388 unsigned Op0, bool Op0IsKill,
389 const ConstantFP *FPImm) {
390 unsigned ResultReg = createResultReg(RC);
391 const MCInstrDesc &II = TII.get(MachineInstOpcode);
393 if (II.getNumDefs() >= 1)
394 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
395 .addReg(Op0, Op0IsKill * RegState::Kill)
398 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
399 .addReg(Op0, Op0IsKill * RegState::Kill)
401 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
402 TII.get(TargetOpcode::COPY), ResultReg)
403 .addReg(II.ImplicitDefs[0]));
408 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
409 const TargetRegisterClass *RC,
410 unsigned Op0, bool Op0IsKill,
411 unsigned Op1, bool Op1IsKill,
413 unsigned ResultReg = createResultReg(RC);
414 const MCInstrDesc &II = TII.get(MachineInstOpcode);
416 if (II.getNumDefs() >= 1)
417 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
418 .addReg(Op0, Op0IsKill * RegState::Kill)
419 .addReg(Op1, Op1IsKill * RegState::Kill)
422 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
423 .addReg(Op0, Op0IsKill * RegState::Kill)
424 .addReg(Op1, Op1IsKill * RegState::Kill)
426 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
427 TII.get(TargetOpcode::COPY), ResultReg)
428 .addReg(II.ImplicitDefs[0]));
433 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
434 const TargetRegisterClass *RC,
436 unsigned ResultReg = createResultReg(RC);
437 const MCInstrDesc &II = TII.get(MachineInstOpcode);
439 if (II.getNumDefs() >= 1)
440 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
443 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
445 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
446 TII.get(TargetOpcode::COPY), ResultReg)
447 .addReg(II.ImplicitDefs[0]));
452 unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
453 const TargetRegisterClass *RC,
454 uint64_t Imm1, uint64_t Imm2) {
455 unsigned ResultReg = createResultReg(RC);
456 const MCInstrDesc &II = TII.get(MachineInstOpcode);
458 if (II.getNumDefs() >= 1)
459 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
460 .addImm(Imm1).addImm(Imm2));
462 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
463 .addImm(Imm1).addImm(Imm2));
464 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
465 TII.get(TargetOpcode::COPY),
467 .addReg(II.ImplicitDefs[0]));
472 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
473 unsigned Op0, bool Op0IsKill,
475 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
476 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
477 "Cannot yet extract from physregs");
478 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
479 DL, TII.get(TargetOpcode::COPY), ResultReg)
480 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
484 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
485 // checks from the various callers.
486 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
487 if (VT == MVT::f64) return 0;
489 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
490 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
491 TII.get(ARM::VMOVRS), MoveReg)
496 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
497 if (VT == MVT::i64) return 0;
499 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
500 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
501 TII.get(ARM::VMOVSR), MoveReg)
506 // For double width floating point we need to materialize two constants
507 // (the high and the low) into integer registers then use a move to get
508 // the combined constant into an FP reg.
509 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
510 const APFloat Val = CFP->getValueAPF();
511 bool is64bit = VT == MVT::f64;
513 // This checks to see if we can use VFP3 instructions to materialize
514 // a constant, otherwise we have to go through the constant pool.
515 if (TLI.isFPImmLegal(Val, VT)) {
519 Imm = ARM_AM::getFP64Imm(Val);
522 Imm = ARM_AM::getFP32Imm(Val);
525 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
526 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
532 // Require VFP2 for loading fp constants.
533 if (!Subtarget->hasVFP2()) return false;
535 // MachineConstantPool wants an explicit alignment.
536 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
538 // TODO: Figure out if this is correct.
539 Align = TD.getTypeAllocSize(CFP->getType());
541 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
542 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
543 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
545 // The extra reg is for addrmode5.
546 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
548 .addConstantPoolIndex(Idx)
553 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
555 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
558 // If we can do this in a single instruction without a constant pool entry
560 const ConstantInt *CI = cast<ConstantInt>(C);
561 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
562 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
563 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
564 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
565 TII.get(Opc), ImmReg)
566 .addImm(CI->getZExtValue()));
570 // Use MVN to emit negative constants.
571 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
572 unsigned Imm = (unsigned)~(CI->getSExtValue());
573 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
574 (ARM_AM::getSOImmVal(Imm) != -1);
576 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
577 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
578 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
579 TII.get(Opc), ImmReg)
585 // Load from constant pool. For now 32-bit only.
589 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
591 // MachineConstantPool wants an explicit alignment.
592 unsigned Align = TD.getPrefTypeAlignment(C->getType());
594 // TODO: Figure out if this is correct.
595 Align = TD.getTypeAllocSize(C->getType());
597 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
600 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
601 TII.get(ARM::t2LDRpci), DestReg)
602 .addConstantPoolIndex(Idx));
604 // The extra immediate is for addrmode2.
605 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
606 TII.get(ARM::LDRcp), DestReg)
607 .addConstantPoolIndex(Idx)
613 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
614 // For now 32-bit only.
615 if (VT != MVT::i32) return 0;
617 Reloc::Model RelocM = TM.getRelocationModel();
619 // TODO: Need more magic for ARM PIC.
620 if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0;
622 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
624 // Use movw+movt when possible, it avoids constant pool entries.
625 // Darwin targets don't support movt with Reloc::Static, see
626 // ARMTargetLowering::LowerGlobalAddressDarwin. Other targets only support
627 // static movt relocations.
628 if (Subtarget->useMovt() &&
629 Subtarget->isTargetDarwin() == (RelocM != Reloc::Static)) {
633 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
635 case Reloc::DynamicNoPIC:
636 Opc = isThumb2 ? ARM::t2MOV_ga_dyn : ARM::MOV_ga_dyn;
639 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
642 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
643 DestReg).addGlobalAddress(GV));
645 // MachineConstantPool wants an explicit alignment.
646 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
648 // TODO: Figure out if this is correct.
649 Align = TD.getTypeAllocSize(GV->getType());
653 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
654 (Subtarget->isThumb() ? 4 : 8);
655 unsigned Id = AFI->createPICLabelUId();
656 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
659 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
662 MachineInstrBuilder MIB;
664 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
665 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
666 .addConstantPoolIndex(Idx);
667 if (RelocM == Reloc::PIC_)
670 // The extra immediate is for addrmode2.
671 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
673 .addConstantPoolIndex(Idx)
676 AddOptionalDefs(MIB);
679 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
680 MachineInstrBuilder MIB;
681 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
683 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
684 TII.get(ARM::t2LDRi12), NewDestReg)
688 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
692 DestReg = NewDestReg;
693 AddOptionalDefs(MIB);
699 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
700 EVT VT = TLI.getValueType(C->getType(), true);
702 // Only handle simple types.
703 if (!VT.isSimple()) return 0;
705 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
706 return ARMMaterializeFP(CFP, VT);
707 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
708 return ARMMaterializeGV(GV, VT);
709 else if (isa<ConstantInt>(C))
710 return ARMMaterializeInt(C, VT);
715 // TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
717 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
718 // Don't handle dynamic allocas.
719 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
722 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
724 DenseMap<const AllocaInst*, int>::iterator SI =
725 FuncInfo.StaticAllocaMap.find(AI);
727 // This will get lowered later into the correct offsets and registers
728 // via rewriteXFrameIndex.
729 if (SI != FuncInfo.StaticAllocaMap.end()) {
730 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
731 unsigned ResultReg = createResultReg(RC);
732 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
733 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
734 TII.get(Opc), ResultReg)
735 .addFrameIndex(SI->second)
743 bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
744 EVT evt = TLI.getValueType(Ty, true);
746 // Only handle simple types.
747 if (evt == MVT::Other || !evt.isSimple()) return false;
748 VT = evt.getSimpleVT();
750 // Handle all legal types, i.e. a register that will directly hold this
752 return TLI.isTypeLegal(VT);
755 bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
756 if (isTypeLegal(Ty, VT)) return true;
758 // If this is a type than can be sign or zero-extended to a basic operation
759 // go ahead and accept it now.
760 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
766 // Computes the address to get to an object.
767 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
768 // Some boilerplate from the X86 FastISel.
769 const User *U = NULL;
770 unsigned Opcode = Instruction::UserOp1;
771 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
772 // Don't walk into other basic blocks unless the object is an alloca from
773 // another block, otherwise it may not have a virtual register assigned.
774 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
775 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
776 Opcode = I->getOpcode();
779 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
780 Opcode = C->getOpcode();
784 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
785 if (Ty->getAddressSpace() > 255)
786 // Fast instruction selection doesn't support the special
793 case Instruction::BitCast: {
794 // Look through bitcasts.
795 return ARMComputeAddress(U->getOperand(0), Addr);
797 case Instruction::IntToPtr: {
798 // Look past no-op inttoptrs.
799 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
800 return ARMComputeAddress(U->getOperand(0), Addr);
803 case Instruction::PtrToInt: {
804 // Look past no-op ptrtoints.
805 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
806 return ARMComputeAddress(U->getOperand(0), Addr);
809 case Instruction::GetElementPtr: {
810 Address SavedAddr = Addr;
811 int TmpOffset = Addr.Offset;
813 // Iterate through the GEP folding the constants into offsets where
815 gep_type_iterator GTI = gep_type_begin(U);
816 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
817 i != e; ++i, ++GTI) {
818 const Value *Op = *i;
819 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
820 const StructLayout *SL = TD.getStructLayout(STy);
821 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
822 TmpOffset += SL->getElementOffset(Idx);
824 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
826 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
827 // Constant-offset addressing.
828 TmpOffset += CI->getSExtValue() * S;
831 if (isa<AddOperator>(Op) &&
832 (!isa<Instruction>(Op) ||
833 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
835 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
836 // An add (in the same block) with a constant operand. Fold the
839 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
840 TmpOffset += CI->getSExtValue() * S;
841 // Iterate on the other operand.
842 Op = cast<AddOperator>(Op)->getOperand(0);
846 goto unsupported_gep;
851 // Try to grab the base operand now.
852 Addr.Offset = TmpOffset;
853 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
855 // We failed, restore everything and try the other options.
861 case Instruction::Alloca: {
862 const AllocaInst *AI = cast<AllocaInst>(Obj);
863 DenseMap<const AllocaInst*, int>::iterator SI =
864 FuncInfo.StaticAllocaMap.find(AI);
865 if (SI != FuncInfo.StaticAllocaMap.end()) {
866 Addr.BaseType = Address::FrameIndexBase;
867 Addr.Base.FI = SI->second;
874 // Try to get this in a register if nothing else has worked.
875 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
876 return Addr.Base.Reg != 0;
879 void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) {
881 assert(VT.isSimple() && "Non-simple types are invalid here!");
883 bool needsLowering = false;
884 switch (VT.getSimpleVT().SimpleTy) {
885 default: llvm_unreachable("Unhandled load/store type!");
891 // Integer loads/stores handle 12-bit offsets.
892 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
893 // Handle negative offsets.
894 if (needsLowering && isThumb2)
895 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
898 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
899 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
904 // Floating point operands handle 8-bit offsets.
905 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
909 // If this is a stack pointer and the offset needs to be simplified then
910 // put the alloca address into a register, set the base type back to
911 // register and continue. This should almost never happen.
912 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
913 TargetRegisterClass *RC = isThumb2 ? ARM::tGPRRegisterClass :
914 ARM::GPRRegisterClass;
915 unsigned ResultReg = createResultReg(RC);
916 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
917 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
918 TII.get(Opc), ResultReg)
919 .addFrameIndex(Addr.Base.FI)
921 Addr.Base.Reg = ResultReg;
922 Addr.BaseType = Address::RegBase;
925 // Since the offset is too large for the load/store instruction
926 // get the reg+offset into a register.
928 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
929 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
934 void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
935 const MachineInstrBuilder &MIB,
936 unsigned Flags, bool useAM3) {
937 // addrmode5 output depends on the selection dag addressing dividing the
938 // offset by 4 that it then later multiplies. Do this here as well.
939 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
940 VT.getSimpleVT().SimpleTy == MVT::f64)
943 // Frame base works a bit differently. Handle it separately.
944 if (Addr.BaseType == Address::FrameIndexBase) {
945 int FI = Addr.Base.FI;
946 int Offset = Addr.Offset;
947 MachineMemOperand *MMO =
948 FuncInfo.MF->getMachineMemOperand(
949 MachinePointerInfo::getFixedStack(FI, Offset),
951 MFI.getObjectSize(FI),
952 MFI.getObjectAlignment(FI));
953 // Now add the rest of the operands.
954 MIB.addFrameIndex(FI);
956 // ARM halfword load/stores and signed byte loads need an additional
959 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
963 MIB.addImm(Addr.Offset);
965 MIB.addMemOperand(MMO);
967 // Now add the rest of the operands.
968 MIB.addReg(Addr.Base.Reg);
970 // ARM halfword load/stores and signed byte loads need an additional
973 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
977 MIB.addImm(Addr.Offset);
980 AddOptionalDefs(MIB);
983 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
984 unsigned Alignment, bool isZExt, bool allocReg) {
985 assert(VT.isSimple() && "Non-simple types are invalid here!");
988 bool needVMOV = false;
989 TargetRegisterClass *RC;
990 switch (VT.getSimpleVT().SimpleTy) {
991 // This is mostly going to be Neon/vector support.
992 default: return false;
996 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
997 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
999 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
1008 RC = ARM::GPRRegisterClass;
1012 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1013 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
1015 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1017 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1020 RC = ARM::GPRRegisterClass;
1024 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1027 Opc = ARM::t2LDRi12;
1031 RC = ARM::GPRRegisterClass;
1034 if (!Subtarget->hasVFP2()) return false;
1035 // Unaligned loads need special handling. Floats require word-alignment.
1036 if (Alignment && Alignment < 4) {
1039 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
1040 RC = ARM::GPRRegisterClass;
1043 RC = TLI.getRegClassFor(VT);
1047 if (!Subtarget->hasVFP2()) return false;
1048 // FIXME: Unaligned loads need special handling. Doublewords require
1050 if (Alignment && Alignment < 4)
1054 RC = TLI.getRegClassFor(VT);
1057 // Simplify this down to something we can handle.
1058 ARMSimplifyAddress(Addr, VT, useAM3);
1060 // Create the base instruction, then add the operands.
1062 ResultReg = createResultReg(RC);
1063 assert (ResultReg > 255 && "Expected an allocated virtual register.");
1064 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1065 TII.get(Opc), ResultReg);
1066 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
1068 // If we had an unaligned load of a float we've converted it to an regular
1069 // load. Now we must move from the GRP to the FP register.
1071 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1072 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1073 TII.get(ARM::VMOVSR), MoveReg)
1074 .addReg(ResultReg));
1075 ResultReg = MoveReg;
1080 bool ARMFastISel::SelectLoad(const Instruction *I) {
1081 // Atomic loads need special handling.
1082 if (cast<LoadInst>(I)->isAtomic())
1085 // Verify we have a legal type before going any further.
1087 if (!isLoadTypeLegal(I->getType(), VT))
1090 // See if we can handle this address.
1092 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
1095 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1097 UpdateValueMap(I, ResultReg);
1101 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr,
1102 unsigned Alignment) {
1104 bool useAM3 = false;
1105 switch (VT.getSimpleVT().SimpleTy) {
1106 // This is mostly going to be Neon/vector support.
1107 default: return false;
1109 unsigned Res = createResultReg(isThumb2 ? ARM::tGPRRegisterClass :
1110 ARM::GPRRegisterClass);
1111 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
1112 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1114 .addReg(SrcReg).addImm(1));
1116 } // Fallthrough here.
1119 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1120 StrOpc = ARM::t2STRBi8;
1122 StrOpc = ARM::t2STRBi12;
1124 StrOpc = ARM::STRBi12;
1129 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1130 StrOpc = ARM::t2STRHi8;
1132 StrOpc = ARM::t2STRHi12;
1140 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1141 StrOpc = ARM::t2STRi8;
1143 StrOpc = ARM::t2STRi12;
1145 StrOpc = ARM::STRi12;
1149 if (!Subtarget->hasVFP2()) return false;
1150 // Unaligned stores need special handling. Floats require word-alignment.
1151 if (Alignment && Alignment < 4) {
1152 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1153 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1154 TII.get(ARM::VMOVRS), MoveReg)
1158 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
1160 StrOpc = ARM::VSTRS;
1164 if (!Subtarget->hasVFP2()) return false;
1165 // FIXME: Unaligned stores need special handling. Doublewords require
1167 if (Alignment && Alignment < 4)
1170 StrOpc = ARM::VSTRD;
1173 // Simplify this down to something we can handle.
1174 ARMSimplifyAddress(Addr, VT, useAM3);
1176 // Create the base instruction, then add the operands.
1177 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1180 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
1184 bool ARMFastISel::SelectStore(const Instruction *I) {
1185 Value *Op0 = I->getOperand(0);
1186 unsigned SrcReg = 0;
1188 // Atomic stores need special handling.
1189 if (cast<StoreInst>(I)->isAtomic())
1192 // Verify we have a legal type before going any further.
1194 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
1197 // Get the value to be stored into a register.
1198 SrcReg = getRegForValue(Op0);
1199 if (SrcReg == 0) return false;
1201 // See if we can handle this address.
1203 if (!ARMComputeAddress(I->getOperand(1), Addr))
1206 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1211 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1213 // Needs two compares...
1214 case CmpInst::FCMP_ONE:
1215 case CmpInst::FCMP_UEQ:
1217 // AL is our "false" for now. The other two need more compares.
1219 case CmpInst::ICMP_EQ:
1220 case CmpInst::FCMP_OEQ:
1222 case CmpInst::ICMP_SGT:
1223 case CmpInst::FCMP_OGT:
1225 case CmpInst::ICMP_SGE:
1226 case CmpInst::FCMP_OGE:
1228 case CmpInst::ICMP_UGT:
1229 case CmpInst::FCMP_UGT:
1231 case CmpInst::FCMP_OLT:
1233 case CmpInst::ICMP_ULE:
1234 case CmpInst::FCMP_OLE:
1236 case CmpInst::FCMP_ORD:
1238 case CmpInst::FCMP_UNO:
1240 case CmpInst::FCMP_UGE:
1242 case CmpInst::ICMP_SLT:
1243 case CmpInst::FCMP_ULT:
1245 case CmpInst::ICMP_SLE:
1246 case CmpInst::FCMP_ULE:
1248 case CmpInst::FCMP_UNE:
1249 case CmpInst::ICMP_NE:
1251 case CmpInst::ICMP_UGE:
1253 case CmpInst::ICMP_ULT:
1258 bool ARMFastISel::SelectBranch(const Instruction *I) {
1259 const BranchInst *BI = cast<BranchInst>(I);
1260 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1261 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1263 // Simple branch support.
1265 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1267 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1268 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1270 // Get the compare predicate.
1271 // Try to take advantage of fallthrough opportunities.
1272 CmpInst::Predicate Predicate = CI->getPredicate();
1273 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1274 std::swap(TBB, FBB);
1275 Predicate = CmpInst::getInversePredicate(Predicate);
1278 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
1280 // We may not handle every CC for now.
1281 if (ARMPred == ARMCC::AL) return false;
1283 // Emit the compare.
1284 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1287 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1288 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1289 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1290 FastEmitBranch(FBB, DL);
1291 FuncInfo.MBB->addSuccessor(TBB);
1294 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1296 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1297 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
1298 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1299 unsigned OpReg = getRegForValue(TI->getOperand(0));
1300 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1302 .addReg(OpReg).addImm(1));
1304 unsigned CCMode = ARMCC::NE;
1305 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1306 std::swap(TBB, FBB);
1310 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1311 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1312 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1314 FastEmitBranch(FBB, DL);
1315 FuncInfo.MBB->addSuccessor(TBB);
1318 } else if (const ConstantInt *CI =
1319 dyn_cast<ConstantInt>(BI->getCondition())) {
1320 uint64_t Imm = CI->getZExtValue();
1321 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1322 FastEmitBranch(Target, DL);
1326 unsigned CmpReg = getRegForValue(BI->getCondition());
1327 if (CmpReg == 0) return false;
1329 // We've been divorced from our compare! Our block was split, and
1330 // now our compare lives in a predecessor block. We musn't
1331 // re-compare here, as the children of the compare aren't guaranteed
1332 // live across the block boundary (we *could* check for this).
1333 // Regardless, the compare has been done in the predecessor block,
1334 // and it left a value for us in a virtual register. Ergo, we test
1335 // the one-bit value left in the virtual register.
1336 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1337 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1338 .addReg(CmpReg).addImm(1));
1340 unsigned CCMode = ARMCC::NE;
1341 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1342 std::swap(TBB, FBB);
1346 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1348 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1349 FastEmitBranch(FBB, DL);
1350 FuncInfo.MBB->addSuccessor(TBB);
1354 bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1355 unsigned AddrReg = getRegForValue(I->getOperand(0));
1356 if (AddrReg == 0) return false;
1358 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
1359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
1364 bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1366 Type *Ty = Src1Value->getType();
1367 EVT SrcVT = TLI.getValueType(Ty, true);
1368 if (!SrcVT.isSimple()) return false;
1370 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1371 if (isFloat && !Subtarget->hasVFP2())
1374 // Check to see if the 2nd operand is a constant that we can encode directly
1377 bool UseImm = false;
1378 bool isNegativeImm = false;
1379 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1380 // Thus, Src1Value may be a ConstantInt, but we're missing it.
1381 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1382 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1384 const APInt &CIVal = ConstInt->getValue();
1385 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1387 isNegativeImm = true;
1390 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1391 (ARM_AM::getSOImmVal(Imm) != -1);
1393 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1394 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1395 if (ConstFP->isZero() && !ConstFP->isNegative())
1401 bool needsExt = false;
1402 switch (SrcVT.getSimpleVT().SimpleTy) {
1403 default: return false;
1404 // TODO: Verify compares.
1407 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
1411 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
1417 // Intentional fall-through.
1421 CmpOpc = ARM::t2CMPrr;
1423 CmpOpc = isNegativeImm ? ARM::t2CMNzri : ARM::t2CMPri;
1426 CmpOpc = ARM::CMPrr;
1428 CmpOpc = isNegativeImm ? ARM::CMNzri : ARM::CMPri;
1433 unsigned SrcReg1 = getRegForValue(Src1Value);
1434 if (SrcReg1 == 0) return false;
1436 unsigned SrcReg2 = 0;
1438 SrcReg2 = getRegForValue(Src2Value);
1439 if (SrcReg2 == 0) return false;
1442 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1445 ResultReg = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1446 if (ResultReg == 0) return false;
1447 SrcReg1 = ResultReg;
1449 ResultReg = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1450 if (ResultReg == 0) return false;
1451 SrcReg2 = ResultReg;
1456 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1458 .addReg(SrcReg1).addReg(SrcReg2));
1460 MachineInstrBuilder MIB;
1461 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1464 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1467 AddOptionalDefs(MIB);
1470 // For floating point we need to move the result to a comparison register
1471 // that we can then use for branches.
1472 if (Ty->isFloatTy() || Ty->isDoubleTy())
1473 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1474 TII.get(ARM::FMSTAT)));
1478 bool ARMFastISel::SelectCmp(const Instruction *I) {
1479 const CmpInst *CI = cast<CmpInst>(I);
1480 Type *Ty = CI->getOperand(0)->getType();
1482 // Get the compare predicate.
1483 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1485 // We may not handle every CC for now.
1486 if (ARMPred == ARMCC::AL) return false;
1488 // Emit the compare.
1489 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1492 // Now set a register based on the comparison. Explicitly set the predicates
1494 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1495 TargetRegisterClass *RC = isThumb2 ? ARM::rGPRRegisterClass
1496 : ARM::GPRRegisterClass;
1497 unsigned DestReg = createResultReg(RC);
1498 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1499 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1500 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1501 unsigned CondReg = isFloat ? ARM::FPSCR : ARM::CPSR;
1502 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1503 .addReg(ZeroReg).addImm(1)
1504 .addImm(ARMPred).addReg(CondReg);
1506 UpdateValueMap(I, DestReg);
1510 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1511 // Make sure we have VFP and that we're extending float to double.
1512 if (!Subtarget->hasVFP2()) return false;
1514 Value *V = I->getOperand(0);
1515 if (!I->getType()->isDoubleTy() ||
1516 !V->getType()->isFloatTy()) return false;
1518 unsigned Op = getRegForValue(V);
1519 if (Op == 0) return false;
1521 unsigned Result = createResultReg(ARM::DPRRegisterClass);
1522 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1523 TII.get(ARM::VCVTDS), Result)
1525 UpdateValueMap(I, Result);
1529 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1530 // Make sure we have VFP and that we're truncating double to float.
1531 if (!Subtarget->hasVFP2()) return false;
1533 Value *V = I->getOperand(0);
1534 if (!(I->getType()->isFloatTy() &&
1535 V->getType()->isDoubleTy())) return false;
1537 unsigned Op = getRegForValue(V);
1538 if (Op == 0) return false;
1540 unsigned Result = createResultReg(ARM::SPRRegisterClass);
1541 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1542 TII.get(ARM::VCVTSD), Result)
1544 UpdateValueMap(I, Result);
1548 bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
1549 // Make sure we have VFP.
1550 if (!Subtarget->hasVFP2()) return false;
1553 Type *Ty = I->getType();
1554 if (!isTypeLegal(Ty, DstVT))
1557 Value *Src = I->getOperand(0);
1558 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1559 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1562 unsigned SrcReg = getRegForValue(Src);
1563 if (SrcReg == 0) return false;
1565 // Handle sign-extension.
1566 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1567 EVT DestVT = MVT::i32;
1568 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT,
1569 /*isZExt*/!isSigned);
1570 if (ResultReg == 0) return false;
1574 // The conversion routine works on fp-reg to fp-reg and the operand above
1575 // was an integer, move it to the fp registers if possible.
1576 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
1577 if (FP == 0) return false;
1580 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1581 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
1584 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1585 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1588 UpdateValueMap(I, ResultReg);
1592 bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
1593 // Make sure we have VFP.
1594 if (!Subtarget->hasVFP2()) return false;
1597 Type *RetTy = I->getType();
1598 if (!isTypeLegal(RetTy, DstVT))
1601 unsigned Op = getRegForValue(I->getOperand(0));
1602 if (Op == 0) return false;
1605 Type *OpTy = I->getOperand(0)->getType();
1606 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1607 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
1610 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
1611 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1612 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1616 // This result needs to be in an integer register, but the conversion only
1617 // takes place in fp-regs.
1618 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1619 if (IntReg == 0) return false;
1621 UpdateValueMap(I, IntReg);
1625 bool ARMFastISel::SelectSelect(const Instruction *I) {
1627 if (!isTypeLegal(I->getType(), VT))
1630 // Things need to be register sized for register moves.
1631 if (VT != MVT::i32) return false;
1632 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1634 unsigned CondReg = getRegForValue(I->getOperand(0));
1635 if (CondReg == 0) return false;
1636 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1637 if (Op1Reg == 0) return false;
1639 // Check to see if we can use an immediate in the conditional move.
1641 bool UseImm = false;
1642 bool isNegativeImm = false;
1643 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1644 assert (VT == MVT::i32 && "Expecting an i32.");
1645 Imm = (int)ConstInt->getValue().getZExtValue();
1647 isNegativeImm = true;
1650 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1651 (ARM_AM::getSOImmVal(Imm) != -1);
1654 unsigned Op2Reg = 0;
1656 Op2Reg = getRegForValue(I->getOperand(2));
1657 if (Op2Reg == 0) return false;
1660 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
1661 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1662 .addReg(CondReg).addImm(0));
1666 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1668 if (!isNegativeImm) {
1669 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1671 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1674 unsigned ResultReg = createResultReg(RC);
1676 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1677 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1679 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1680 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
1681 UpdateValueMap(I, ResultReg);
1685 bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
1687 Type *Ty = I->getType();
1688 if (!isTypeLegal(Ty, VT))
1691 // If we have integer div support we should have selected this automagically.
1692 // In case we have a real miss go ahead and return false and we'll pick
1694 if (Subtarget->hasDivide()) return false;
1696 // Otherwise emit a libcall.
1697 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1699 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
1700 else if (VT == MVT::i16)
1701 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
1702 else if (VT == MVT::i32)
1703 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
1704 else if (VT == MVT::i64)
1705 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
1706 else if (VT == MVT::i128)
1707 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
1708 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1710 return ARMEmitLibcall(I, LC);
1713 bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
1715 Type *Ty = I->getType();
1716 if (!isTypeLegal(Ty, VT))
1719 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1721 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
1722 else if (VT == MVT::i16)
1723 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
1724 else if (VT == MVT::i32)
1725 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
1726 else if (VT == MVT::i64)
1727 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
1728 else if (VT == MVT::i128)
1729 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
1730 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1732 return ARMEmitLibcall(I, LC);
1735 bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1736 EVT DestVT = TLI.getValueType(I->getType(), true);
1738 // We can get here in the case when we have a binary operation on a non-legal
1739 // type and the target independent selector doesn't know how to handle it.
1740 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1744 switch (ISDOpcode) {
1745 default: return false;
1747 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1750 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1753 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1757 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1758 if (SrcReg1 == 0) return false;
1760 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1761 // in the instruction, rather then materializing the value in a register.
1762 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1763 if (SrcReg2 == 0) return false;
1765 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1766 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1767 TII.get(Opc), ResultReg)
1768 .addReg(SrcReg1).addReg(SrcReg2));
1769 UpdateValueMap(I, ResultReg);
1773 bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
1774 EVT VT = TLI.getValueType(I->getType(), true);
1776 // We can get here in the case when we want to use NEON for our fp
1777 // operations, but can't figure out how to. Just use the vfp instructions
1779 // FIXME: It'd be nice to use NEON instructions.
1780 Type *Ty = I->getType();
1781 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1782 if (isFloat && !Subtarget->hasVFP2())
1786 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1787 switch (ISDOpcode) {
1788 default: return false;
1790 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1793 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1796 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1799 unsigned Op1 = getRegForValue(I->getOperand(0));
1800 if (Op1 == 0) return false;
1802 unsigned Op2 = getRegForValue(I->getOperand(1));
1803 if (Op2 == 0) return false;
1805 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1806 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1807 TII.get(Opc), ResultReg)
1808 .addReg(Op1).addReg(Op2));
1809 UpdateValueMap(I, ResultReg);
1813 // Call Handling Code
1815 // This is largely taken directly from CCAssignFnForNode - we don't support
1816 // varargs in FastISel so that part has been removed.
1817 // TODO: We may not support all of this.
1818 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1821 llvm_unreachable("Unsupported calling convention");
1822 case CallingConv::Fast:
1823 // Ignore fastcc. Silence compiler warnings.
1824 (void)RetFastCC_ARM_APCS;
1825 (void)FastCC_ARM_APCS;
1827 case CallingConv::C:
1828 // Use target triple & subtarget features to do actual dispatch.
1829 if (Subtarget->isAAPCS_ABI()) {
1830 if (Subtarget->hasVFP2() &&
1831 TM.Options.FloatABIType == FloatABI::Hard)
1832 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1834 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1836 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1837 case CallingConv::ARM_AAPCS_VFP:
1838 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1839 case CallingConv::ARM_AAPCS:
1840 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1841 case CallingConv::ARM_APCS:
1842 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1846 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1847 SmallVectorImpl<unsigned> &ArgRegs,
1848 SmallVectorImpl<MVT> &ArgVTs,
1849 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1850 SmallVectorImpl<unsigned> &RegArgs,
1852 unsigned &NumBytes) {
1853 SmallVector<CCValAssign, 16> ArgLocs;
1854 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
1855 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1857 // Get a count of how many bytes are to be pushed on the stack.
1858 NumBytes = CCInfo.getNextStackOffset();
1860 // Issue CALLSEQ_START
1861 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1862 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1863 TII.get(AdjStackDown))
1866 // Process the args.
1867 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1868 CCValAssign &VA = ArgLocs[i];
1869 unsigned Arg = ArgRegs[VA.getValNo()];
1870 MVT ArgVT = ArgVTs[VA.getValNo()];
1872 // We don't handle NEON/vector parameters yet.
1873 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1876 // Handle arg promotion, etc.
1877 switch (VA.getLocInfo()) {
1878 case CCValAssign::Full: break;
1879 case CCValAssign::SExt: {
1880 MVT DestVT = VA.getLocVT();
1881 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
1882 assert (Arg != 0 && "Failed to emit a sext");
1886 case CCValAssign::AExt:
1887 // Intentional fall-through. Handle AExt and ZExt.
1888 case CCValAssign::ZExt: {
1889 MVT DestVT = VA.getLocVT();
1890 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
1891 assert (Arg != 0 && "Failed to emit a sext");
1895 case CCValAssign::BCvt: {
1896 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
1897 /*TODO: Kill=*/false);
1898 assert(BC != 0 && "Failed to emit a bitcast!");
1900 ArgVT = VA.getLocVT();
1903 default: llvm_unreachable("Unknown arg promotion!");
1906 // Now copy/store arg to correct locations.
1907 if (VA.isRegLoc() && !VA.needsCustom()) {
1908 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1911 RegArgs.push_back(VA.getLocReg());
1912 } else if (VA.needsCustom()) {
1913 // TODO: We need custom lowering for vector (v2f64) args.
1914 if (VA.getLocVT() != MVT::f64) return false;
1916 CCValAssign &NextVA = ArgLocs[++i];
1918 // TODO: Only handle register args for now.
1919 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1921 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1922 TII.get(ARM::VMOVRRD), VA.getLocReg())
1923 .addReg(NextVA.getLocReg(), RegState::Define)
1925 RegArgs.push_back(VA.getLocReg());
1926 RegArgs.push_back(NextVA.getLocReg());
1928 assert(VA.isMemLoc());
1929 // Need to store on the stack.
1931 Addr.BaseType = Address::RegBase;
1932 Addr.Base.Reg = ARM::SP;
1933 Addr.Offset = VA.getLocMemOffset();
1935 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
1941 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1942 const Instruction *I, CallingConv::ID CC,
1943 unsigned &NumBytes) {
1944 // Issue CALLSEQ_END
1945 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1946 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1947 TII.get(AdjStackUp))
1948 .addImm(NumBytes).addImm(0));
1950 // Now the return value.
1951 if (RetVT != MVT::isVoid) {
1952 SmallVector<CCValAssign, 16> RVLocs;
1953 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
1954 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1956 // Copy all of the result registers out of their specified physreg.
1957 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
1958 // For this move we copy into two registers and then move into the
1959 // double fp reg we want.
1960 EVT DestVT = RVLocs[0].getValVT();
1961 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1962 unsigned ResultReg = createResultReg(DstRC);
1963 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1964 TII.get(ARM::VMOVDRR), ResultReg)
1965 .addReg(RVLocs[0].getLocReg())
1966 .addReg(RVLocs[1].getLocReg()));
1968 UsedRegs.push_back(RVLocs[0].getLocReg());
1969 UsedRegs.push_back(RVLocs[1].getLocReg());
1971 // Finally update the result.
1972 UpdateValueMap(I, ResultReg);
1974 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
1975 EVT CopyVT = RVLocs[0].getValVT();
1977 // Special handling for extended integers.
1978 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1981 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1983 unsigned ResultReg = createResultReg(DstRC);
1984 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1985 ResultReg).addReg(RVLocs[0].getLocReg());
1986 UsedRegs.push_back(RVLocs[0].getLocReg());
1988 // Finally update the result.
1989 UpdateValueMap(I, ResultReg);
1996 bool ARMFastISel::SelectRet(const Instruction *I) {
1997 const ReturnInst *Ret = cast<ReturnInst>(I);
1998 const Function &F = *I->getParent()->getParent();
2000 if (!FuncInfo.CanLowerReturn)
2006 CallingConv::ID CC = F.getCallingConv();
2007 if (Ret->getNumOperands() > 0) {
2008 SmallVector<ISD::OutputArg, 4> Outs;
2009 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
2012 // Analyze operands of the call, assigning locations to each operand.
2013 SmallVector<CCValAssign, 16> ValLocs;
2014 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
2015 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
2017 const Value *RV = Ret->getOperand(0);
2018 unsigned Reg = getRegForValue(RV);
2022 // Only handle a single return value for now.
2023 if (ValLocs.size() != 1)
2026 CCValAssign &VA = ValLocs[0];
2028 // Don't bother handling odd stuff for now.
2029 if (VA.getLocInfo() != CCValAssign::Full)
2031 // Only handle register returns for now.
2035 unsigned SrcReg = Reg + VA.getValNo();
2036 EVT RVVT = TLI.getValueType(RV->getType());
2037 EVT DestVT = VA.getValVT();
2038 // Special handling for extended integers.
2039 if (RVVT != DestVT) {
2040 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2043 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
2046 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2048 bool isZExt = Outs[0].Flags.isZExt();
2049 unsigned ResultReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, isZExt);
2050 if (ResultReg == 0) return false;
2055 unsigned DstReg = VA.getLocReg();
2056 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2057 // Avoid a cross-class copy. This is very unlikely.
2058 if (!SrcRC->contains(DstReg))
2060 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2061 DstReg).addReg(SrcReg);
2063 // Mark the register as live out of the function.
2064 MRI.addLiveOut(VA.getLocReg());
2067 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
2068 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2073 unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
2075 // iOS needs the r9 versions of the opcodes.
2076 bool isiOS = Subtarget->isTargetIOS();
2078 return isiOS ? ARM::tBLr9 : ARM::tBL;
2080 return isiOS ? ARM::BLr9 : ARM::BL;
2084 // A quick function that will emit a call for a named libcall in F with the
2085 // vector of passed arguments for the Instruction in I. We can assume that we
2086 // can emit a call for any libcall we can produce. This is an abridged version
2087 // of the full call infrastructure since we won't need to worry about things
2088 // like computed function pointers or strange arguments at call sites.
2089 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
2091 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2092 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
2094 // Handle *simple* calls for now.
2095 Type *RetTy = I->getType();
2097 if (RetTy->isVoidTy())
2098 RetVT = MVT::isVoid;
2099 else if (!isTypeLegal(RetTy, RetVT))
2102 // TODO: For now if we have long calls specified we don't handle the call.
2103 if (EnableARMLongCalls) return false;
2105 // Set up the argument vectors.
2106 SmallVector<Value*, 8> Args;
2107 SmallVector<unsigned, 8> ArgRegs;
2108 SmallVector<MVT, 8> ArgVTs;
2109 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2110 Args.reserve(I->getNumOperands());
2111 ArgRegs.reserve(I->getNumOperands());
2112 ArgVTs.reserve(I->getNumOperands());
2113 ArgFlags.reserve(I->getNumOperands());
2114 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
2115 Value *Op = I->getOperand(i);
2116 unsigned Arg = getRegForValue(Op);
2117 if (Arg == 0) return false;
2119 Type *ArgTy = Op->getType();
2121 if (!isTypeLegal(ArgTy, ArgVT)) return false;
2123 ISD::ArgFlagsTy Flags;
2124 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2125 Flags.setOrigAlign(OriginalAlignment);
2128 ArgRegs.push_back(Arg);
2129 ArgVTs.push_back(ArgVT);
2130 ArgFlags.push_back(Flags);
2133 // Handle the arguments now that we've gotten them.
2134 SmallVector<unsigned, 4> RegArgs;
2136 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2139 // Issue the call, BLr9 for iOS, BL otherwise.
2140 // TODO: Turn this into the table of arm call ops.
2141 MachineInstrBuilder MIB;
2142 unsigned CallOpc = ARMSelectCallOp(NULL);
2144 // Explicitly adding the predicate here.
2145 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2147 .addExternalSymbol(TLI.getLibcallName(Call));
2149 // Explicitly adding the predicate here.
2150 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2152 .addExternalSymbol(TLI.getLibcallName(Call)));
2154 // Add implicit physical register uses to the call.
2155 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2156 MIB.addReg(RegArgs[i]);
2158 // Finish off the call including any return values.
2159 SmallVector<unsigned, 4> UsedRegs;
2160 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
2162 // Set all unused physreg defs as dead.
2163 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2168 bool ARMFastISel::SelectCall(const Instruction *I,
2169 const char *IntrMemName = 0) {
2170 const CallInst *CI = cast<CallInst>(I);
2171 const Value *Callee = CI->getCalledValue();
2173 // Can't handle inline asm.
2174 if (isa<InlineAsm>(Callee)) return false;
2176 // Only handle global variable Callees.
2177 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
2181 // Check the calling convention.
2182 ImmutableCallSite CS(CI);
2183 CallingConv::ID CC = CS.getCallingConv();
2185 // TODO: Avoid some calling conventions?
2187 // Let SDISel handle vararg functions.
2188 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2189 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
2190 if (FTy->isVarArg())
2193 // Handle *simple* calls for now.
2194 Type *RetTy = I->getType();
2196 if (RetTy->isVoidTy())
2197 RetVT = MVT::isVoid;
2198 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2199 RetVT != MVT::i8 && RetVT != MVT::i1)
2202 // TODO: For now if we have long calls specified we don't handle the call.
2203 if (EnableARMLongCalls) return false;
2205 // Set up the argument vectors.
2206 SmallVector<Value*, 8> Args;
2207 SmallVector<unsigned, 8> ArgRegs;
2208 SmallVector<MVT, 8> ArgVTs;
2209 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2210 unsigned arg_size = CS.arg_size();
2211 Args.reserve(arg_size);
2212 ArgRegs.reserve(arg_size);
2213 ArgVTs.reserve(arg_size);
2214 ArgFlags.reserve(arg_size);
2215 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2217 // If we're lowering a memory intrinsic instead of a regular call, skip the
2218 // last two arguments, which shouldn't be passed to the underlying function.
2219 if (IntrMemName && e-i <= 2)
2222 ISD::ArgFlagsTy Flags;
2223 unsigned AttrInd = i - CS.arg_begin() + 1;
2224 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2226 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2229 // FIXME: Only handle *easy* calls for now.
2230 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2231 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2232 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2233 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2236 Type *ArgTy = (*i)->getType();
2238 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2242 unsigned Arg = getRegForValue(*i);
2246 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2247 Flags.setOrigAlign(OriginalAlignment);
2250 ArgRegs.push_back(Arg);
2251 ArgVTs.push_back(ArgVT);
2252 ArgFlags.push_back(Flags);
2255 // Handle the arguments now that we've gotten them.
2256 SmallVector<unsigned, 4> RegArgs;
2258 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2261 // Issue the call, BLr9 for iOS, BL otherwise.
2262 // TODO: Turn this into the table of arm call ops.
2263 MachineInstrBuilder MIB;
2264 unsigned CallOpc = ARMSelectCallOp(GV);
2265 // Explicitly adding the predicate here.
2267 // Explicitly adding the predicate here.
2268 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2271 MIB.addGlobalAddress(GV, 0, 0);
2273 MIB.addExternalSymbol(IntrMemName, 0);
2276 // Explicitly adding the predicate here.
2277 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2279 .addGlobalAddress(GV, 0, 0));
2281 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2283 .addExternalSymbol(IntrMemName, 0));
2286 // Add implicit physical register uses to the call.
2287 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2288 MIB.addReg(RegArgs[i]);
2290 // Finish off the call including any return values.
2291 SmallVector<unsigned, 4> UsedRegs;
2292 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
2294 // Set all unused physreg defs as dead.
2295 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2300 bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
2304 bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len) {
2305 // Make sure we don't bloat code by inlining very large memcpy's.
2306 if (!ARMIsMemCpySmall(Len))
2309 // We don't care about alignment here since we just emit integer accesses.
2323 RV = ARMEmitLoad(VT, ResultReg, Src);
2324 assert (RV == true && "Should be able to handle this load.");
2325 RV = ARMEmitStore(VT, ResultReg, Dest);
2326 assert (RV == true && "Should be able to handle this store.");
2329 unsigned Size = VT.getSizeInBits()/8;
2331 Dest.Offset += Size;
2338 bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2339 // FIXME: Handle more intrinsics.
2340 switch (I.getIntrinsicID()) {
2341 default: return false;
2342 case Intrinsic::memcpy:
2343 case Intrinsic::memmove: {
2344 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2345 // Don't handle volatile.
2346 if (MTI.isVolatile())
2349 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2350 // we would emit dead code because we don't currently handle memmoves.
2351 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2352 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
2353 // Small memcpy's are common enough that we want to do them without a call
2355 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
2356 if (ARMIsMemCpySmall(Len)) {
2358 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2359 !ARMComputeAddress(MTI.getRawSource(), Src))
2361 if (ARMTryEmitSmallMemCpy(Dest, Src, Len))
2366 if (!MTI.getLength()->getType()->isIntegerTy(32))
2369 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2372 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2373 return SelectCall(&I, IntrMemName);
2375 case Intrinsic::memset: {
2376 const MemSetInst &MSI = cast<MemSetInst>(I);
2377 // Don't handle volatile.
2378 if (MSI.isVolatile())
2381 if (!MSI.getLength()->getType()->isIntegerTy(32))
2384 if (MSI.getDestAddressSpace() > 255)
2387 return SelectCall(&I, "memset");
2392 bool ARMFastISel::SelectTrunc(const Instruction *I) {
2393 // The high bits for a type smaller than the register size are assumed to be
2395 Value *Op = I->getOperand(0);
2398 SrcVT = TLI.getValueType(Op->getType(), true);
2399 DestVT = TLI.getValueType(I->getType(), true);
2401 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2403 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2406 unsigned SrcReg = getRegForValue(Op);
2407 if (!SrcReg) return false;
2409 // Because the high bits are undefined, a truncate doesn't generate
2411 UpdateValueMap(I, SrcReg);
2415 unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2417 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
2421 bool isBoolZext = false;
2422 if (!SrcVT.isSimple()) return 0;
2423 switch (SrcVT.getSimpleVT().SimpleTy) {
2426 if (!Subtarget->hasV6Ops()) return 0;
2428 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
2430 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
2433 if (!Subtarget->hasV6Ops()) return 0;
2435 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
2437 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
2441 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
2448 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
2449 MachineInstrBuilder MIB;
2450 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
2456 AddOptionalDefs(MIB);
2460 bool ARMFastISel::SelectIntExt(const Instruction *I) {
2461 // On ARM, in general, integer casts don't involve legal types; this code
2462 // handles promotable integers.
2463 Type *DestTy = I->getType();
2464 Value *Src = I->getOperand(0);
2465 Type *SrcTy = Src->getType();
2468 SrcVT = TLI.getValueType(SrcTy, true);
2469 DestVT = TLI.getValueType(DestTy, true);
2471 bool isZExt = isa<ZExtInst>(I);
2472 unsigned SrcReg = getRegForValue(Src);
2473 if (!SrcReg) return false;
2475 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2476 if (ResultReg == 0) return false;
2477 UpdateValueMap(I, ResultReg);
2481 // TODO: SoftFP support.
2482 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
2484 switch (I->getOpcode()) {
2485 case Instruction::Load:
2486 return SelectLoad(I);
2487 case Instruction::Store:
2488 return SelectStore(I);
2489 case Instruction::Br:
2490 return SelectBranch(I);
2491 case Instruction::IndirectBr:
2492 return SelectIndirectBr(I);
2493 case Instruction::ICmp:
2494 case Instruction::FCmp:
2495 return SelectCmp(I);
2496 case Instruction::FPExt:
2497 return SelectFPExt(I);
2498 case Instruction::FPTrunc:
2499 return SelectFPTrunc(I);
2500 case Instruction::SIToFP:
2501 return SelectIToFP(I, /*isSigned*/ true);
2502 case Instruction::UIToFP:
2503 return SelectIToFP(I, /*isSigned*/ false);
2504 case Instruction::FPToSI:
2505 return SelectFPToI(I, /*isSigned*/ true);
2506 case Instruction::FPToUI:
2507 return SelectFPToI(I, /*isSigned*/ false);
2508 case Instruction::Add:
2509 return SelectBinaryIntOp(I, ISD::ADD);
2510 case Instruction::Or:
2511 return SelectBinaryIntOp(I, ISD::OR);
2512 case Instruction::Sub:
2513 return SelectBinaryIntOp(I, ISD::SUB);
2514 case Instruction::FAdd:
2515 return SelectBinaryFPOp(I, ISD::FADD);
2516 case Instruction::FSub:
2517 return SelectBinaryFPOp(I, ISD::FSUB);
2518 case Instruction::FMul:
2519 return SelectBinaryFPOp(I, ISD::FMUL);
2520 case Instruction::SDiv:
2521 return SelectDiv(I, /*isSigned*/ true);
2522 case Instruction::UDiv:
2523 return SelectDiv(I, /*isSigned*/ false);
2524 case Instruction::SRem:
2525 return SelectRem(I, /*isSigned*/ true);
2526 case Instruction::URem:
2527 return SelectRem(I, /*isSigned*/ false);
2528 case Instruction::Call:
2529 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2530 return SelectIntrinsicCall(*II);
2531 return SelectCall(I);
2532 case Instruction::Select:
2533 return SelectSelect(I);
2534 case Instruction::Ret:
2535 return SelectRet(I);
2536 case Instruction::Trunc:
2537 return SelectTrunc(I);
2538 case Instruction::ZExt:
2539 case Instruction::SExt:
2540 return SelectIntExt(I);
2546 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2547 /// vreg is being provided by the specified load instruction. If possible,
2548 /// try to fold the load as an operand to the instruction, returning true if
2550 bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2551 const LoadInst *LI) {
2552 // Verify we have a legal type before going any further.
2554 if (!isLoadTypeLegal(LI->getType(), VT))
2557 // Combine load followed by zero- or sign-extend.
2558 // ldrb r1, [r0] ldrb r1, [r0]
2560 // mov r3, r2 mov r3, r1
2562 switch(MI->getOpcode()) {
2563 default: return false;
2581 // See if we can handle this address.
2583 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2585 unsigned ResultReg = MI->getOperand(0).getReg();
2586 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
2588 MI->eraseFromParent();
2593 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
2594 // Completely untested on non-iOS.
2595 const TargetMachine &TM = funcInfo.MF->getTarget();
2597 // Darwin and thumb1 only for now.
2598 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
2599 if (Subtarget->isTargetIOS() && !Subtarget->isThumb1Only() &&
2600 !DisableARMFastISel)
2601 return new ARMFastISel(funcInfo);