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 "ARMTargetMachine.h"
20 #include "ARMSubtarget.h"
21 #include "ARMConstantPoolValue.h"
22 #include "MCTargetDesc/ARMAddressingModes.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/IntrinsicInst.h"
28 #include "llvm/Module.h"
29 #include "llvm/Operator.h"
30 #include "llvm/CodeGen/Analysis.h"
31 #include "llvm/CodeGen/FastISel.h"
32 #include "llvm/CodeGen/FunctionLoweringInfo.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineModuleInfo.h"
35 #include "llvm/CodeGen/MachineConstantPool.h"
36 #include "llvm/CodeGen/MachineFrameInfo.h"
37 #include "llvm/CodeGen/MachineMemOperand.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/Support/CallSite.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/GetElementPtrTypeIterator.h"
43 #include "llvm/Target/TargetData.h"
44 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetLowering.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Target/TargetOptions.h"
50 extern cl::opt<bool> EnableARMLongCalls;
54 // All possible address modes, plus some.
55 typedef struct Address {
68 // Innocuous defaults for our address.
70 : BaseType(RegBase), Offset(0) {
75 class ARMFastISel : public FastISel {
77 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
78 /// make the right decision when generating code for different targets.
79 const ARMSubtarget *Subtarget;
80 const TargetMachine &TM;
81 const TargetInstrInfo &TII;
82 const TargetLowering &TLI;
85 // Convenience variables to avoid some queries.
90 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
92 TM(funcInfo.MF->getTarget()),
93 TII(*TM.getInstrInfo()),
94 TLI(*TM.getTargetLowering()) {
95 Subtarget = &TM.getSubtarget<ARMSubtarget>();
96 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
97 isThumb2 = AFI->isThumbFunction();
98 Context = &funcInfo.Fn->getContext();
101 // Code from FastISel.cpp.
102 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
103 const TargetRegisterClass *RC);
104 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
105 const TargetRegisterClass *RC,
106 unsigned Op0, bool Op0IsKill);
107 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
108 const TargetRegisterClass *RC,
109 unsigned Op0, bool Op0IsKill,
110 unsigned Op1, bool Op1IsKill);
111 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill,
114 unsigned Op1, bool Op1IsKill,
115 unsigned Op2, bool Op2IsKill);
116 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
117 const TargetRegisterClass *RC,
118 unsigned Op0, bool Op0IsKill,
120 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
121 const TargetRegisterClass *RC,
122 unsigned Op0, bool Op0IsKill,
123 const ConstantFP *FPImm);
124 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
125 const TargetRegisterClass *RC,
126 unsigned Op0, bool Op0IsKill,
127 unsigned Op1, bool Op1IsKill,
129 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
130 const TargetRegisterClass *RC,
132 virtual unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
133 const TargetRegisterClass *RC,
134 uint64_t Imm1, uint64_t Imm2);
136 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
137 unsigned Op0, bool Op0IsKill,
140 // Backend specific FastISel code.
141 virtual bool TargetSelectInstruction(const Instruction *I);
142 virtual unsigned TargetMaterializeConstant(const Constant *C);
143 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
144 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
147 #include "ARMGenFastISel.inc"
149 // Instruction selection routines.
151 bool SelectLoad(const Instruction *I);
152 bool SelectStore(const Instruction *I);
153 bool SelectBranch(const Instruction *I);
154 bool SelectIndirectBr(const Instruction *I);
155 bool SelectCmp(const Instruction *I);
156 bool SelectFPExt(const Instruction *I);
157 bool SelectFPTrunc(const Instruction *I);
158 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
159 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
160 bool SelectIToFP(const Instruction *I, bool isSigned);
161 bool SelectFPToI(const Instruction *I, bool isSigned);
162 bool SelectDiv(const Instruction *I, bool isSigned);
163 bool SelectRem(const Instruction *I, bool isSigned);
164 bool SelectCall(const Instruction *I, const char *IntrMemName);
165 bool SelectIntrinsicCall(const IntrinsicInst &I);
166 bool SelectSelect(const Instruction *I);
167 bool SelectRet(const Instruction *I);
168 bool SelectTrunc(const Instruction *I);
169 bool SelectIntExt(const Instruction *I);
173 bool isTypeLegal(Type *Ty, MVT &VT);
174 bool isLoadTypeLegal(Type *Ty, MVT &VT);
175 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
177 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
178 unsigned Alignment = 0, bool isZExt = true,
179 bool allocReg = true);
180 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr,
181 unsigned Alignment = 0);
182 bool ARMComputeAddress(const Value *Obj, Address &Addr);
183 void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3);
184 bool ARMIsMemCpySmall(uint64_t Len);
185 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len);
186 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
187 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
188 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
189 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
190 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
191 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
192 unsigned ARMSelectCallOp(bool UseReg);
194 // Call handling routines.
196 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
199 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
200 SmallVectorImpl<unsigned> &ArgRegs,
201 SmallVectorImpl<MVT> &ArgVTs,
202 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
203 SmallVectorImpl<unsigned> &RegArgs,
207 unsigned getLibcallReg(const Twine &Name);
208 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
209 const Instruction *I, CallingConv::ID CC,
210 unsigned &NumBytes, bool isVarArg);
211 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
213 // OptionalDef handling routines.
215 bool isARMNEONPred(const MachineInstr *MI);
216 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
217 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
218 void AddLoadStoreOperands(EVT VT, Address &Addr,
219 const MachineInstrBuilder &MIB,
220 unsigned Flags, bool useAM3);
223 } // end anonymous namespace
225 #include "ARMGenCallingConv.inc"
227 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
228 // we don't care about implicit defs here, just places we'll need to add a
229 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
230 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
231 if (!MI->hasOptionalDef())
234 // Look to see if our OptionalDef is defining CPSR or CCR.
235 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
236 const MachineOperand &MO = MI->getOperand(i);
237 if (!MO.isReg() || !MO.isDef()) continue;
238 if (MO.getReg() == ARM::CPSR)
244 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
245 const MCInstrDesc &MCID = MI->getDesc();
247 // If we're a thumb2 or not NEON function we were handled via isPredicable.
248 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
249 AFI->isThumb2Function())
252 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
253 if (MCID.OpInfo[i].isPredicate())
259 // If the machine is predicable go ahead and add the predicate operands, if
260 // it needs default CC operands add those.
261 // TODO: If we want to support thumb1 then we'll need to deal with optional
262 // CPSR defs that need to be added before the remaining operands. See s_cc_out
263 // for descriptions why.
264 const MachineInstrBuilder &
265 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
266 MachineInstr *MI = &*MIB;
268 // Do we use a predicate? or...
269 // Are we NEON in ARM mode and have a predicate operand? If so, I know
270 // we're not predicable but add it anyways.
271 if (TII.isPredicable(MI) || isARMNEONPred(MI))
274 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
275 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
277 if (DefinesOptionalPredicate(MI, &CPSR)) {
286 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
287 const TargetRegisterClass* RC) {
288 unsigned ResultReg = createResultReg(RC);
289 const MCInstrDesc &II = TII.get(MachineInstOpcode);
291 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
295 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
296 const TargetRegisterClass *RC,
297 unsigned Op0, bool Op0IsKill) {
298 unsigned ResultReg = createResultReg(RC);
299 const MCInstrDesc &II = TII.get(MachineInstOpcode);
301 if (II.getNumDefs() >= 1) {
302 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
303 .addReg(Op0, Op0IsKill * RegState::Kill));
305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
306 .addReg(Op0, Op0IsKill * RegState::Kill));
307 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
308 TII.get(TargetOpcode::COPY), ResultReg)
309 .addReg(II.ImplicitDefs[0]));
314 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
315 const TargetRegisterClass *RC,
316 unsigned Op0, bool Op0IsKill,
317 unsigned Op1, bool Op1IsKill) {
318 unsigned ResultReg = createResultReg(RC);
319 const MCInstrDesc &II = TII.get(MachineInstOpcode);
321 if (II.getNumDefs() >= 1) {
322 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
323 .addReg(Op0, Op0IsKill * RegState::Kill)
324 .addReg(Op1, Op1IsKill * RegState::Kill));
326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
327 .addReg(Op0, Op0IsKill * RegState::Kill)
328 .addReg(Op1, Op1IsKill * RegState::Kill));
329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
330 TII.get(TargetOpcode::COPY), ResultReg)
331 .addReg(II.ImplicitDefs[0]));
336 unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
337 const TargetRegisterClass *RC,
338 unsigned Op0, bool Op0IsKill,
339 unsigned Op1, bool Op1IsKill,
340 unsigned Op2, bool Op2IsKill) {
341 unsigned ResultReg = createResultReg(RC);
342 const MCInstrDesc &II = TII.get(MachineInstOpcode);
344 if (II.getNumDefs() >= 1) {
345 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
346 .addReg(Op0, Op0IsKill * RegState::Kill)
347 .addReg(Op1, Op1IsKill * RegState::Kill)
348 .addReg(Op2, Op2IsKill * RegState::Kill));
350 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
351 .addReg(Op0, Op0IsKill * RegState::Kill)
352 .addReg(Op1, Op1IsKill * RegState::Kill)
353 .addReg(Op2, Op2IsKill * RegState::Kill));
354 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
355 TII.get(TargetOpcode::COPY), ResultReg)
356 .addReg(II.ImplicitDefs[0]));
361 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
362 const TargetRegisterClass *RC,
363 unsigned Op0, bool Op0IsKill,
365 unsigned ResultReg = createResultReg(RC);
366 const MCInstrDesc &II = TII.get(MachineInstOpcode);
368 if (II.getNumDefs() >= 1) {
369 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
370 .addReg(Op0, Op0IsKill * RegState::Kill)
373 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
374 .addReg(Op0, Op0IsKill * RegState::Kill)
376 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
377 TII.get(TargetOpcode::COPY), ResultReg)
378 .addReg(II.ImplicitDefs[0]));
383 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
384 const TargetRegisterClass *RC,
385 unsigned Op0, bool Op0IsKill,
386 const ConstantFP *FPImm) {
387 unsigned ResultReg = createResultReg(RC);
388 const MCInstrDesc &II = TII.get(MachineInstOpcode);
390 if (II.getNumDefs() >= 1) {
391 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
392 .addReg(Op0, Op0IsKill * RegState::Kill)
395 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
396 .addReg(Op0, Op0IsKill * RegState::Kill)
398 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
399 TII.get(TargetOpcode::COPY), ResultReg)
400 .addReg(II.ImplicitDefs[0]));
405 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
406 const TargetRegisterClass *RC,
407 unsigned Op0, bool Op0IsKill,
408 unsigned Op1, bool Op1IsKill,
410 unsigned ResultReg = createResultReg(RC);
411 const MCInstrDesc &II = TII.get(MachineInstOpcode);
413 if (II.getNumDefs() >= 1) {
414 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
415 .addReg(Op0, Op0IsKill * RegState::Kill)
416 .addReg(Op1, Op1IsKill * RegState::Kill)
419 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
420 .addReg(Op0, Op0IsKill * RegState::Kill)
421 .addReg(Op1, Op1IsKill * RegState::Kill)
423 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
424 TII.get(TargetOpcode::COPY), ResultReg)
425 .addReg(II.ImplicitDefs[0]));
430 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
431 const TargetRegisterClass *RC,
433 unsigned ResultReg = createResultReg(RC);
434 const MCInstrDesc &II = TII.get(MachineInstOpcode);
436 if (II.getNumDefs() >= 1) {
437 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
440 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
442 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
443 TII.get(TargetOpcode::COPY), ResultReg)
444 .addReg(II.ImplicitDefs[0]));
449 unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
450 const TargetRegisterClass *RC,
451 uint64_t Imm1, uint64_t Imm2) {
452 unsigned ResultReg = createResultReg(RC);
453 const MCInstrDesc &II = TII.get(MachineInstOpcode);
455 if (II.getNumDefs() >= 1) {
456 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
457 .addImm(Imm1).addImm(Imm2));
459 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
460 .addImm(Imm1).addImm(Imm2));
461 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
462 TII.get(TargetOpcode::COPY),
464 .addReg(II.ImplicitDefs[0]));
469 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
470 unsigned Op0, bool Op0IsKill,
472 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
473 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
474 "Cannot yet extract from physregs");
476 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
477 DL, TII.get(TargetOpcode::COPY), ResultReg)
478 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
482 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
483 // checks from the various callers.
484 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
485 if (VT == MVT::f64) return 0;
487 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
488 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
489 TII.get(ARM::VMOVSR), MoveReg)
494 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
495 if (VT == MVT::i64) return 0;
497 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
498 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
499 TII.get(ARM::VMOVRS), MoveReg)
504 // For double width floating point we need to materialize two constants
505 // (the high and the low) into integer registers then use a move to get
506 // the combined constant into an FP reg.
507 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
508 const APFloat Val = CFP->getValueAPF();
509 bool is64bit = VT == MVT::f64;
511 // This checks to see if we can use VFP3 instructions to materialize
512 // a constant, otherwise we have to go through the constant pool.
513 if (TLI.isFPImmLegal(Val, VT)) {
517 Imm = ARM_AM::getFP64Imm(Val);
520 Imm = ARM_AM::getFP32Imm(Val);
523 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
524 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
530 // Require VFP2 for loading fp constants.
531 if (!Subtarget->hasVFP2()) return false;
533 // MachineConstantPool wants an explicit alignment.
534 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
536 // TODO: Figure out if this is correct.
537 Align = TD.getTypeAllocSize(CFP->getType());
539 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
540 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
541 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
543 // The extra reg is for addrmode5.
544 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
546 .addConstantPoolIndex(Idx)
551 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
553 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
556 // If we can do this in a single instruction without a constant pool entry
558 const ConstantInt *CI = cast<ConstantInt>(C);
559 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
560 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
561 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
562 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
563 TII.get(Opc), ImmReg)
564 .addImm(CI->getZExtValue()));
568 // Use MVN to emit negative constants.
569 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
570 unsigned Imm = (unsigned)~(CI->getSExtValue());
571 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
572 (ARM_AM::getSOImmVal(Imm) != -1);
574 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
575 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
576 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
577 TII.get(Opc), ImmReg)
583 // Load from constant pool. For now 32-bit only.
587 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
589 // MachineConstantPool wants an explicit alignment.
590 unsigned Align = TD.getPrefTypeAlignment(C->getType());
592 // TODO: Figure out if this is correct.
593 Align = TD.getTypeAllocSize(C->getType());
595 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
598 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
599 TII.get(ARM::t2LDRpci), DestReg)
600 .addConstantPoolIndex(Idx));
602 // The extra immediate is for addrmode2.
603 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
604 TII.get(ARM::LDRcp), DestReg)
605 .addConstantPoolIndex(Idx)
611 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
612 // For now 32-bit only.
613 if (VT != MVT::i32) return 0;
615 Reloc::Model RelocM = TM.getRelocationModel();
617 // TODO: Need more magic for ARM PIC.
618 if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0;
620 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
622 // Use movw+movt when possible, it avoids constant pool entries.
623 // Darwin targets don't support movt with Reloc::Static, see
624 // ARMTargetLowering::LowerGlobalAddressDarwin. Other targets only support
625 // static movt relocations.
626 if (Subtarget->useMovt() &&
627 Subtarget->isTargetDarwin() == (RelocM != Reloc::Static)) {
631 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
633 case Reloc::DynamicNoPIC:
634 Opc = isThumb2 ? ARM::t2MOV_ga_dyn : ARM::MOV_ga_dyn;
637 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
640 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
641 DestReg).addGlobalAddress(GV));
643 // MachineConstantPool wants an explicit alignment.
644 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
646 // TODO: Figure out if this is correct.
647 Align = TD.getTypeAllocSize(GV->getType());
651 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
652 (Subtarget->isThumb() ? 4 : 8);
653 unsigned Id = AFI->createPICLabelUId();
654 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
657 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
660 MachineInstrBuilder MIB;
662 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
663 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
664 .addConstantPoolIndex(Idx);
665 if (RelocM == Reloc::PIC_)
668 // The extra immediate is for addrmode2.
669 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
671 .addConstantPoolIndex(Idx)
674 AddOptionalDefs(MIB);
677 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
678 MachineInstrBuilder MIB;
679 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
681 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
682 TII.get(ARM::t2LDRi12), NewDestReg)
686 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
690 DestReg = NewDestReg;
691 AddOptionalDefs(MIB);
697 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
698 EVT VT = TLI.getValueType(C->getType(), true);
700 // Only handle simple types.
701 if (!VT.isSimple()) return 0;
703 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
704 return ARMMaterializeFP(CFP, VT);
705 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
706 return ARMMaterializeGV(GV, VT);
707 else if (isa<ConstantInt>(C))
708 return ARMMaterializeInt(C, VT);
713 // TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
715 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
716 // Don't handle dynamic allocas.
717 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
720 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
722 DenseMap<const AllocaInst*, int>::iterator SI =
723 FuncInfo.StaticAllocaMap.find(AI);
725 // This will get lowered later into the correct offsets and registers
726 // via rewriteXFrameIndex.
727 if (SI != FuncInfo.StaticAllocaMap.end()) {
728 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
729 unsigned ResultReg = createResultReg(RC);
730 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
731 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
732 TII.get(Opc), ResultReg)
733 .addFrameIndex(SI->second)
741 bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
742 EVT evt = TLI.getValueType(Ty, true);
744 // Only handle simple types.
745 if (evt == MVT::Other || !evt.isSimple()) return false;
746 VT = evt.getSimpleVT();
748 // Handle all legal types, i.e. a register that will directly hold this
750 return TLI.isTypeLegal(VT);
753 bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
754 if (isTypeLegal(Ty, VT)) return true;
756 // If this is a type than can be sign or zero-extended to a basic operation
757 // go ahead and accept it now.
758 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
764 // Computes the address to get to an object.
765 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
766 // Some boilerplate from the X86 FastISel.
767 const User *U = NULL;
768 unsigned Opcode = Instruction::UserOp1;
769 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
770 // Don't walk into other basic blocks unless the object is an alloca from
771 // another block, otherwise it may not have a virtual register assigned.
772 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
773 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
774 Opcode = I->getOpcode();
777 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
778 Opcode = C->getOpcode();
782 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
783 if (Ty->getAddressSpace() > 255)
784 // Fast instruction selection doesn't support the special
791 case Instruction::BitCast: {
792 // Look through bitcasts.
793 return ARMComputeAddress(U->getOperand(0), Addr);
795 case Instruction::IntToPtr: {
796 // Look past no-op inttoptrs.
797 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
798 return ARMComputeAddress(U->getOperand(0), Addr);
801 case Instruction::PtrToInt: {
802 // Look past no-op ptrtoints.
803 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
804 return ARMComputeAddress(U->getOperand(0), Addr);
807 case Instruction::GetElementPtr: {
808 Address SavedAddr = Addr;
809 int TmpOffset = Addr.Offset;
811 // Iterate through the GEP folding the constants into offsets where
813 gep_type_iterator GTI = gep_type_begin(U);
814 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
815 i != e; ++i, ++GTI) {
816 const Value *Op = *i;
817 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
818 const StructLayout *SL = TD.getStructLayout(STy);
819 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
820 TmpOffset += SL->getElementOffset(Idx);
822 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
824 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
825 // Constant-offset addressing.
826 TmpOffset += CI->getSExtValue() * S;
829 if (isa<AddOperator>(Op) &&
830 (!isa<Instruction>(Op) ||
831 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
833 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
834 // An add (in the same block) with a constant operand. Fold the
837 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
838 TmpOffset += CI->getSExtValue() * S;
839 // Iterate on the other operand.
840 Op = cast<AddOperator>(Op)->getOperand(0);
844 goto unsupported_gep;
849 // Try to grab the base operand now.
850 Addr.Offset = TmpOffset;
851 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
853 // We failed, restore everything and try the other options.
859 case Instruction::Alloca: {
860 const AllocaInst *AI = cast<AllocaInst>(Obj);
861 DenseMap<const AllocaInst*, int>::iterator SI =
862 FuncInfo.StaticAllocaMap.find(AI);
863 if (SI != FuncInfo.StaticAllocaMap.end()) {
864 Addr.BaseType = Address::FrameIndexBase;
865 Addr.Base.FI = SI->second;
872 // Try to get this in a register if nothing else has worked.
873 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
874 return Addr.Base.Reg != 0;
877 void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) {
879 assert(VT.isSimple() && "Non-simple types are invalid here!");
881 bool needsLowering = false;
882 switch (VT.getSimpleVT().SimpleTy) {
883 default: llvm_unreachable("Unhandled load/store type!");
889 // Integer loads/stores handle 12-bit offsets.
890 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
891 // Handle negative offsets.
892 if (needsLowering && isThumb2)
893 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
896 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
897 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
902 // Floating point operands handle 8-bit offsets.
903 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
907 // If this is a stack pointer and the offset needs to be simplified then
908 // put the alloca address into a register, set the base type back to
909 // register and continue. This should almost never happen.
910 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
911 const TargetRegisterClass *RC = isThumb2 ?
912 (const TargetRegisterClass*)&ARM::tGPRRegClass :
913 (const TargetRegisterClass*)&ARM::GPRRegClass;
914 unsigned ResultReg = createResultReg(RC);
915 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
916 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
917 TII.get(Opc), ResultReg)
918 .addFrameIndex(Addr.Base.FI)
920 Addr.Base.Reg = ResultReg;
921 Addr.BaseType = Address::RegBase;
924 // Since the offset is too large for the load/store instruction
925 // get the reg+offset into a register.
927 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
928 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
933 void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
934 const MachineInstrBuilder &MIB,
935 unsigned Flags, bool useAM3) {
936 // addrmode5 output depends on the selection dag addressing dividing the
937 // offset by 4 that it then later multiplies. Do this here as well.
938 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
939 VT.getSimpleVT().SimpleTy == MVT::f64)
942 // Frame base works a bit differently. Handle it separately.
943 if (Addr.BaseType == Address::FrameIndexBase) {
944 int FI = Addr.Base.FI;
945 int Offset = Addr.Offset;
946 MachineMemOperand *MMO =
947 FuncInfo.MF->getMachineMemOperand(
948 MachinePointerInfo::getFixedStack(FI, Offset),
950 MFI.getObjectSize(FI),
951 MFI.getObjectAlignment(FI));
952 // Now add the rest of the operands.
953 MIB.addFrameIndex(FI);
955 // ARM halfword load/stores and signed byte loads need an additional
958 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
962 MIB.addImm(Addr.Offset);
964 MIB.addMemOperand(MMO);
966 // Now add the rest of the operands.
967 MIB.addReg(Addr.Base.Reg);
969 // ARM halfword load/stores and signed byte loads need an additional
972 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
976 MIB.addImm(Addr.Offset);
979 AddOptionalDefs(MIB);
982 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
983 unsigned Alignment, bool isZExt, bool allocReg) {
984 assert(VT.isSimple() && "Non-simple types are invalid here!");
987 bool needVMOV = false;
988 const TargetRegisterClass *RC;
989 switch (VT.getSimpleVT().SimpleTy) {
990 // This is mostly going to be Neon/vector support.
991 default: return false;
995 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
996 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
998 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
1007 RC = &ARM::GPRRegClass;
1011 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1012 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
1014 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1016 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1019 RC = &ARM::GPRRegClass;
1023 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1026 Opc = ARM::t2LDRi12;
1030 RC = &ARM::GPRRegClass;
1033 if (!Subtarget->hasVFP2()) return false;
1034 // Unaligned loads need special handling. Floats require word-alignment.
1035 if (Alignment && Alignment < 4) {
1038 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
1039 RC = &ARM::GPRRegClass;
1042 RC = TLI.getRegClassFor(VT);
1046 if (!Subtarget->hasVFP2()) return false;
1047 // FIXME: Unaligned loads need special handling. Doublewords require
1049 if (Alignment && Alignment < 4)
1053 RC = TLI.getRegClassFor(VT);
1056 // Simplify this down to something we can handle.
1057 ARMSimplifyAddress(Addr, VT, useAM3);
1059 // Create the base instruction, then add the operands.
1061 ResultReg = createResultReg(RC);
1062 assert (ResultReg > 255 && "Expected an allocated virtual register.");
1063 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1064 TII.get(Opc), ResultReg);
1065 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
1067 // If we had an unaligned load of a float we've converted it to an regular
1068 // load. Now we must move from the GRP to the FP register.
1070 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1071 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1072 TII.get(ARM::VMOVSR), MoveReg)
1073 .addReg(ResultReg));
1074 ResultReg = MoveReg;
1079 bool ARMFastISel::SelectLoad(const Instruction *I) {
1080 // Atomic loads need special handling.
1081 if (cast<LoadInst>(I)->isAtomic())
1084 // Verify we have a legal type before going any further.
1086 if (!isLoadTypeLegal(I->getType(), VT))
1089 // See if we can handle this address.
1091 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
1094 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1096 UpdateValueMap(I, ResultReg);
1100 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr,
1101 unsigned Alignment) {
1103 bool useAM3 = false;
1104 switch (VT.getSimpleVT().SimpleTy) {
1105 // This is mostly going to be Neon/vector support.
1106 default: return false;
1108 unsigned Res = createResultReg(isThumb2 ?
1109 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1110 (const TargetRegisterClass*)&ARM::GPRRegClass);
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();
1386 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
1387 // then a cmn, because there is no way to represent 2147483648 as a
1388 // signed 32-bit int.
1389 if (Imm < 0 && Imm != (int)0x80000000) {
1390 isNegativeImm = true;
1393 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1394 (ARM_AM::getSOImmVal(Imm) != -1);
1396 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1397 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1398 if (ConstFP->isZero() && !ConstFP->isNegative())
1404 bool needsExt = false;
1405 switch (SrcVT.getSimpleVT().SimpleTy) {
1406 default: return false;
1407 // TODO: Verify compares.
1410 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
1414 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
1420 // Intentional fall-through.
1424 CmpOpc = ARM::t2CMPrr;
1426 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
1429 CmpOpc = ARM::CMPrr;
1431 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
1436 unsigned SrcReg1 = getRegForValue(Src1Value);
1437 if (SrcReg1 == 0) return false;
1439 unsigned SrcReg2 = 0;
1441 SrcReg2 = getRegForValue(Src2Value);
1442 if (SrcReg2 == 0) return false;
1445 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1447 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1448 if (SrcReg1 == 0) return false;
1450 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1451 if (SrcReg2 == 0) return false;
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);
1481 // Get the compare predicate.
1482 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1484 // We may not handle every CC for now.
1485 if (ARMPred == ARMCC::AL) return false;
1487 // Emit the compare.
1488 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1491 // Now set a register based on the comparison. Explicitly set the predicates
1493 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1494 const TargetRegisterClass *RC = isThumb2 ?
1495 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1496 (const TargetRegisterClass*)&ARM::GPRRegClass;
1497 unsigned DestReg = createResultReg(RC);
1498 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1499 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1500 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
1501 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1502 .addReg(ZeroReg).addImm(1)
1503 .addImm(ARMPred).addReg(ARM::CPSR);
1505 UpdateValueMap(I, DestReg);
1509 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1510 // Make sure we have VFP and that we're extending float to double.
1511 if (!Subtarget->hasVFP2()) return false;
1513 Value *V = I->getOperand(0);
1514 if (!I->getType()->isDoubleTy() ||
1515 !V->getType()->isFloatTy()) return false;
1517 unsigned Op = getRegForValue(V);
1518 if (Op == 0) return false;
1520 unsigned Result = createResultReg(&ARM::DPRRegClass);
1521 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1522 TII.get(ARM::VCVTDS), Result)
1524 UpdateValueMap(I, Result);
1528 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1529 // Make sure we have VFP and that we're truncating double to float.
1530 if (!Subtarget->hasVFP2()) return false;
1532 Value *V = I->getOperand(0);
1533 if (!(I->getType()->isFloatTy() &&
1534 V->getType()->isDoubleTy())) return false;
1536 unsigned Op = getRegForValue(V);
1537 if (Op == 0) return false;
1539 unsigned Result = createResultReg(&ARM::SPRRegClass);
1540 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1541 TII.get(ARM::VCVTSD), Result)
1543 UpdateValueMap(I, Result);
1547 bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
1548 // Make sure we have VFP.
1549 if (!Subtarget->hasVFP2()) return false;
1552 Type *Ty = I->getType();
1553 if (!isTypeLegal(Ty, DstVT))
1556 Value *Src = I->getOperand(0);
1557 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1558 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1561 unsigned SrcReg = getRegForValue(Src);
1562 if (SrcReg == 0) return false;
1564 // Handle sign-extension.
1565 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1566 EVT DestVT = MVT::i32;
1567 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT,
1568 /*isZExt*/!isSigned);
1569 if (SrcReg == 0) return false;
1572 // The conversion routine works on fp-reg to fp-reg and the operand above
1573 // was an integer, move it to the fp registers if possible.
1574 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
1575 if (FP == 0) return false;
1578 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1579 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
1582 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1583 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1586 UpdateValueMap(I, ResultReg);
1590 bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
1591 // Make sure we have VFP.
1592 if (!Subtarget->hasVFP2()) return false;
1595 Type *RetTy = I->getType();
1596 if (!isTypeLegal(RetTy, DstVT))
1599 unsigned Op = getRegForValue(I->getOperand(0));
1600 if (Op == 0) return false;
1603 Type *OpTy = I->getOperand(0)->getType();
1604 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1605 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
1608 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
1609 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1610 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1614 // This result needs to be in an integer register, but the conversion only
1615 // takes place in fp-regs.
1616 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1617 if (IntReg == 0) return false;
1619 UpdateValueMap(I, IntReg);
1623 bool ARMFastISel::SelectSelect(const Instruction *I) {
1625 if (!isTypeLegal(I->getType(), VT))
1628 // Things need to be register sized for register moves.
1629 if (VT != MVT::i32) return false;
1630 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1632 unsigned CondReg = getRegForValue(I->getOperand(0));
1633 if (CondReg == 0) return false;
1634 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1635 if (Op1Reg == 0) return false;
1637 // Check to see if we can use an immediate in the conditional move.
1639 bool UseImm = false;
1640 bool isNegativeImm = false;
1641 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1642 assert (VT == MVT::i32 && "Expecting an i32.");
1643 Imm = (int)ConstInt->getValue().getZExtValue();
1645 isNegativeImm = true;
1648 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1649 (ARM_AM::getSOImmVal(Imm) != -1);
1652 unsigned Op2Reg = 0;
1654 Op2Reg = getRegForValue(I->getOperand(2));
1655 if (Op2Reg == 0) return false;
1658 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
1659 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1660 .addReg(CondReg).addImm(0));
1664 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1666 if (!isNegativeImm) {
1667 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1669 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1672 unsigned ResultReg = createResultReg(RC);
1674 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1675 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1677 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1678 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
1679 UpdateValueMap(I, ResultReg);
1683 bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
1685 Type *Ty = I->getType();
1686 if (!isTypeLegal(Ty, VT))
1689 // If we have integer div support we should have selected this automagically.
1690 // In case we have a real miss go ahead and return false and we'll pick
1692 if (Subtarget->hasDivide()) return false;
1694 // Otherwise emit a libcall.
1695 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1697 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
1698 else if (VT == MVT::i16)
1699 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
1700 else if (VT == MVT::i32)
1701 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
1702 else if (VT == MVT::i64)
1703 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
1704 else if (VT == MVT::i128)
1705 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
1706 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1708 return ARMEmitLibcall(I, LC);
1711 bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
1713 Type *Ty = I->getType();
1714 if (!isTypeLegal(Ty, VT))
1717 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1719 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
1720 else if (VT == MVT::i16)
1721 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
1722 else if (VT == MVT::i32)
1723 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
1724 else if (VT == MVT::i64)
1725 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
1726 else if (VT == MVT::i128)
1727 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
1728 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1730 return ARMEmitLibcall(I, LC);
1733 bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1734 EVT DestVT = TLI.getValueType(I->getType(), true);
1736 // We can get here in the case when we have a binary operation on a non-legal
1737 // type and the target independent selector doesn't know how to handle it.
1738 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1742 switch (ISDOpcode) {
1743 default: return false;
1745 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1748 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1751 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1755 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1756 if (SrcReg1 == 0) return false;
1758 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1759 // in the instruction, rather then materializing the value in a register.
1760 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1761 if (SrcReg2 == 0) return false;
1763 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1764 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1765 TII.get(Opc), ResultReg)
1766 .addReg(SrcReg1).addReg(SrcReg2));
1767 UpdateValueMap(I, ResultReg);
1771 bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
1772 EVT VT = TLI.getValueType(I->getType(), true);
1774 // We can get here in the case when we want to use NEON for our fp
1775 // operations, but can't figure out how to. Just use the vfp instructions
1777 // FIXME: It'd be nice to use NEON instructions.
1778 Type *Ty = I->getType();
1779 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1780 if (isFloat && !Subtarget->hasVFP2())
1784 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1785 switch (ISDOpcode) {
1786 default: return false;
1788 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1791 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1794 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1797 unsigned Op1 = getRegForValue(I->getOperand(0));
1798 if (Op1 == 0) return false;
1800 unsigned Op2 = getRegForValue(I->getOperand(1));
1801 if (Op2 == 0) return false;
1803 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1804 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1805 TII.get(Opc), ResultReg)
1806 .addReg(Op1).addReg(Op2));
1807 UpdateValueMap(I, ResultReg);
1811 // Call Handling Code
1813 // This is largely taken directly from CCAssignFnForNode - we don't support
1814 // varargs in FastISel so that part has been removed.
1815 // TODO: We may not support all of this.
1816 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
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 && !isVarArg)
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:
1839 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1840 case CallingConv::ARM_AAPCS:
1841 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1842 case CallingConv::ARM_APCS:
1843 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1847 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1848 SmallVectorImpl<unsigned> &ArgRegs,
1849 SmallVectorImpl<MVT> &ArgVTs,
1850 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1851 SmallVectorImpl<unsigned> &RegArgs,
1855 SmallVector<CCValAssign, 16> ArgLocs;
1856 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
1857 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1858 CCAssignFnForCall(CC, false, isVarArg));
1860 // Check that we can handle all of the arguments. If we can't, then bail out
1861 // now before we add code to the MBB.
1862 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1863 CCValAssign &VA = ArgLocs[i];
1864 MVT ArgVT = ArgVTs[VA.getValNo()];
1866 // We don't handle NEON/vector parameters yet.
1867 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1870 // Now copy/store arg to correct locations.
1871 if (VA.isRegLoc() && !VA.needsCustom()) {
1873 } else if (VA.needsCustom()) {
1874 // TODO: We need custom lowering for vector (v2f64) args.
1875 if (VA.getLocVT() != MVT::f64 ||
1876 // TODO: Only handle register args for now.
1877 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1880 switch (static_cast<EVT>(ArgVT).getSimpleVT().SimpleTy) {
1889 if (!Subtarget->hasVFP2())
1893 if (!Subtarget->hasVFP2())
1900 // At the point, we are able to handle the call's arguments in fast isel.
1902 // Get a count of how many bytes are to be pushed on the stack.
1903 NumBytes = CCInfo.getNextStackOffset();
1905 // Issue CALLSEQ_START
1906 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1907 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1908 TII.get(AdjStackDown))
1911 // Process the args.
1912 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1913 CCValAssign &VA = ArgLocs[i];
1914 unsigned Arg = ArgRegs[VA.getValNo()];
1915 MVT ArgVT = ArgVTs[VA.getValNo()];
1917 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
1918 "We don't handle NEON/vector parameters yet.");
1920 // Handle arg promotion, etc.
1921 switch (VA.getLocInfo()) {
1922 case CCValAssign::Full: break;
1923 case CCValAssign::SExt: {
1924 MVT DestVT = VA.getLocVT();
1925 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
1926 assert (Arg != 0 && "Failed to emit a sext");
1930 case CCValAssign::AExt:
1931 // Intentional fall-through. Handle AExt and ZExt.
1932 case CCValAssign::ZExt: {
1933 MVT DestVT = VA.getLocVT();
1934 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
1935 assert (Arg != 0 && "Failed to emit a sext");
1939 case CCValAssign::BCvt: {
1940 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
1941 /*TODO: Kill=*/false);
1942 assert(BC != 0 && "Failed to emit a bitcast!");
1944 ArgVT = VA.getLocVT();
1947 default: llvm_unreachable("Unknown arg promotion!");
1950 // Now copy/store arg to correct locations.
1951 if (VA.isRegLoc() && !VA.needsCustom()) {
1952 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1955 RegArgs.push_back(VA.getLocReg());
1956 } else if (VA.needsCustom()) {
1957 // TODO: We need custom lowering for vector (v2f64) args.
1958 assert(VA.getLocVT() == MVT::f64 &&
1959 "Custom lowering for v2f64 args not available");
1961 CCValAssign &NextVA = ArgLocs[++i];
1963 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
1964 "We only handle register args!");
1966 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1967 TII.get(ARM::VMOVRRD), VA.getLocReg())
1968 .addReg(NextVA.getLocReg(), RegState::Define)
1970 RegArgs.push_back(VA.getLocReg());
1971 RegArgs.push_back(NextVA.getLocReg());
1973 assert(VA.isMemLoc());
1974 // Need to store on the stack.
1976 Addr.BaseType = Address::RegBase;
1977 Addr.Base.Reg = ARM::SP;
1978 Addr.Offset = VA.getLocMemOffset();
1980 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
1981 assert(EmitRet && "Could not emit a store for argument!");
1988 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1989 const Instruction *I, CallingConv::ID CC,
1990 unsigned &NumBytes, bool isVarArg) {
1991 // Issue CALLSEQ_END
1992 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1993 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1994 TII.get(AdjStackUp))
1995 .addImm(NumBytes).addImm(0));
1997 // Now the return value.
1998 if (RetVT != MVT::isVoid) {
1999 SmallVector<CCValAssign, 16> RVLocs;
2000 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2001 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
2003 // Copy all of the result registers out of their specified physreg.
2004 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
2005 // For this move we copy into two registers and then move into the
2006 // double fp reg we want.
2007 EVT DestVT = RVLocs[0].getValVT();
2008 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
2009 unsigned ResultReg = createResultReg(DstRC);
2010 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2011 TII.get(ARM::VMOVDRR), ResultReg)
2012 .addReg(RVLocs[0].getLocReg())
2013 .addReg(RVLocs[1].getLocReg()));
2015 UsedRegs.push_back(RVLocs[0].getLocReg());
2016 UsedRegs.push_back(RVLocs[1].getLocReg());
2018 // Finally update the result.
2019 UpdateValueMap(I, ResultReg);
2021 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
2022 EVT CopyVT = RVLocs[0].getValVT();
2024 // Special handling for extended integers.
2025 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2028 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
2030 unsigned ResultReg = createResultReg(DstRC);
2031 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2032 ResultReg).addReg(RVLocs[0].getLocReg());
2033 UsedRegs.push_back(RVLocs[0].getLocReg());
2035 // Finally update the result.
2036 UpdateValueMap(I, ResultReg);
2043 bool ARMFastISel::SelectRet(const Instruction *I) {
2044 const ReturnInst *Ret = cast<ReturnInst>(I);
2045 const Function &F = *I->getParent()->getParent();
2047 if (!FuncInfo.CanLowerReturn)
2050 CallingConv::ID CC = F.getCallingConv();
2051 if (Ret->getNumOperands() > 0) {
2052 SmallVector<ISD::OutputArg, 4> Outs;
2053 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
2056 // Analyze operands of the call, assigning locations to each operand.
2057 SmallVector<CCValAssign, 16> ValLocs;
2058 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
2059 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2062 const Value *RV = Ret->getOperand(0);
2063 unsigned Reg = getRegForValue(RV);
2067 // Only handle a single return value for now.
2068 if (ValLocs.size() != 1)
2071 CCValAssign &VA = ValLocs[0];
2073 // Don't bother handling odd stuff for now.
2074 if (VA.getLocInfo() != CCValAssign::Full)
2076 // Only handle register returns for now.
2080 unsigned SrcReg = Reg + VA.getValNo();
2081 EVT RVVT = TLI.getValueType(RV->getType());
2082 EVT DestVT = VA.getValVT();
2083 // Special handling for extended integers.
2084 if (RVVT != DestVT) {
2085 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2088 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2090 // Perform extension if flagged as either zext or sext. Otherwise, do
2092 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2093 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2094 if (SrcReg == 0) return false;
2099 unsigned DstReg = VA.getLocReg();
2100 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2101 // Avoid a cross-class copy. This is very unlikely.
2102 if (!SrcRC->contains(DstReg))
2104 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2105 DstReg).addReg(SrcReg);
2107 // Mark the register as live out of the function.
2108 MRI.addLiveOut(VA.getLocReg());
2111 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
2112 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2117 unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2119 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2121 return isThumb2 ? ARM::tBL : ARM::BL;
2124 unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
2125 GlobalValue *GV = new GlobalVariable(Type::getInt32Ty(*Context), false,
2126 GlobalValue::ExternalLinkage, 0, Name);
2127 return ARMMaterializeGV(GV, TLI.getValueType(GV->getType()));
2130 // A quick function that will emit a call for a named libcall in F with the
2131 // vector of passed arguments for the Instruction in I. We can assume that we
2132 // can emit a call for any libcall we can produce. This is an abridged version
2133 // of the full call infrastructure since we won't need to worry about things
2134 // like computed function pointers or strange arguments at call sites.
2135 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
2137 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2138 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
2140 // Handle *simple* calls for now.
2141 Type *RetTy = I->getType();
2143 if (RetTy->isVoidTy())
2144 RetVT = MVT::isVoid;
2145 else if (!isTypeLegal(RetTy, RetVT))
2148 // Can't handle non-double multi-reg retvals.
2149 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
2150 SmallVector<CCValAssign, 16> RVLocs;
2151 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
2152 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
2153 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2157 // Set up the argument vectors.
2158 SmallVector<Value*, 8> Args;
2159 SmallVector<unsigned, 8> ArgRegs;
2160 SmallVector<MVT, 8> ArgVTs;
2161 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2162 Args.reserve(I->getNumOperands());
2163 ArgRegs.reserve(I->getNumOperands());
2164 ArgVTs.reserve(I->getNumOperands());
2165 ArgFlags.reserve(I->getNumOperands());
2166 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
2167 Value *Op = I->getOperand(i);
2168 unsigned Arg = getRegForValue(Op);
2169 if (Arg == 0) return false;
2171 Type *ArgTy = Op->getType();
2173 if (!isTypeLegal(ArgTy, ArgVT)) return false;
2175 ISD::ArgFlagsTy Flags;
2176 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2177 Flags.setOrigAlign(OriginalAlignment);
2180 ArgRegs.push_back(Arg);
2181 ArgVTs.push_back(ArgVT);
2182 ArgFlags.push_back(Flags);
2185 // Handle the arguments now that we've gotten them.
2186 SmallVector<unsigned, 4> RegArgs;
2188 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2189 RegArgs, CC, NumBytes, false))
2192 unsigned CalleeReg = 0;
2193 if (EnableARMLongCalls) {
2194 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2195 if (CalleeReg == 0) return false;
2199 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2200 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2201 DL, TII.get(CallOpc));
2203 // Explicitly adding the predicate here.
2204 AddDefaultPred(MIB);
2205 if (EnableARMLongCalls)
2206 MIB.addReg(CalleeReg);
2208 MIB.addExternalSymbol(TLI.getLibcallName(Call));
2210 if (EnableARMLongCalls)
2211 MIB.addReg(CalleeReg);
2213 MIB.addExternalSymbol(TLI.getLibcallName(Call));
2215 // Explicitly adding the predicate here.
2216 AddDefaultPred(MIB);
2218 // Add implicit physical register uses to the call.
2219 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2220 MIB.addReg(RegArgs[i]);
2222 // Add a register mask with the call-preserved registers.
2223 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2224 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2226 // Finish off the call including any return values.
2227 SmallVector<unsigned, 4> UsedRegs;
2228 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
2230 // Set all unused physreg defs as dead.
2231 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2236 bool ARMFastISel::SelectCall(const Instruction *I,
2237 const char *IntrMemName = 0) {
2238 const CallInst *CI = cast<CallInst>(I);
2239 const Value *Callee = CI->getCalledValue();
2241 // Can't handle inline asm.
2242 if (isa<InlineAsm>(Callee)) return false;
2244 // Check the calling convention.
2245 ImmutableCallSite CS(CI);
2246 CallingConv::ID CC = CS.getCallingConv();
2248 // TODO: Avoid some calling conventions?
2250 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2251 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
2252 bool isVarArg = FTy->isVarArg();
2254 // Handle *simple* calls for now.
2255 Type *RetTy = I->getType();
2257 if (RetTy->isVoidTy())
2258 RetVT = MVT::isVoid;
2259 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2260 RetVT != MVT::i8 && RetVT != MVT::i1)
2263 // Can't handle non-double multi-reg retvals.
2264 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2265 RetVT != MVT::i16 && RetVT != MVT::i32) {
2266 SmallVector<CCValAssign, 16> RVLocs;
2267 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2268 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
2269 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2273 // Set up the argument vectors.
2274 SmallVector<Value*, 8> Args;
2275 SmallVector<unsigned, 8> ArgRegs;
2276 SmallVector<MVT, 8> ArgVTs;
2277 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2278 unsigned arg_size = CS.arg_size();
2279 Args.reserve(arg_size);
2280 ArgRegs.reserve(arg_size);
2281 ArgVTs.reserve(arg_size);
2282 ArgFlags.reserve(arg_size);
2283 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2285 // If we're lowering a memory intrinsic instead of a regular call, skip the
2286 // last two arguments, which shouldn't be passed to the underlying function.
2287 if (IntrMemName && e-i <= 2)
2290 ISD::ArgFlagsTy Flags;
2291 unsigned AttrInd = i - CS.arg_begin() + 1;
2292 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2294 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2297 // FIXME: Only handle *easy* calls for now.
2298 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2299 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2300 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2301 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2304 Type *ArgTy = (*i)->getType();
2306 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2310 unsigned Arg = getRegForValue(*i);
2314 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2315 Flags.setOrigAlign(OriginalAlignment);
2318 ArgRegs.push_back(Arg);
2319 ArgVTs.push_back(ArgVT);
2320 ArgFlags.push_back(Flags);
2323 // Handle the arguments now that we've gotten them.
2324 SmallVector<unsigned, 4> RegArgs;
2326 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2327 RegArgs, CC, NumBytes, isVarArg))
2330 bool UseReg = false;
2331 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
2332 if (!GV || EnableARMLongCalls) UseReg = true;
2334 unsigned CalleeReg = 0;
2337 CalleeReg = getLibcallReg(IntrMemName);
2339 CalleeReg = getRegForValue(Callee);
2341 if (CalleeReg == 0) return false;
2345 unsigned CallOpc = ARMSelectCallOp(UseReg);
2346 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2347 DL, TII.get(CallOpc));
2349 // Explicitly adding the predicate here.
2350 AddDefaultPred(MIB);
2352 MIB.addReg(CalleeReg);
2353 else if (!IntrMemName)
2354 MIB.addGlobalAddress(GV, 0, 0);
2356 MIB.addExternalSymbol(IntrMemName, 0);
2359 MIB.addReg(CalleeReg);
2360 else if (!IntrMemName)
2361 MIB.addGlobalAddress(GV, 0, 0);
2363 MIB.addExternalSymbol(IntrMemName, 0);
2365 // Explicitly adding the predicate here.
2366 AddDefaultPred(MIB);
2369 // Add implicit physical register uses to the call.
2370 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2371 MIB.addReg(RegArgs[i]);
2373 // Add a register mask with the call-preserved registers.
2374 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2375 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2377 // Finish off the call including any return values.
2378 SmallVector<unsigned, 4> UsedRegs;
2379 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2382 // Set all unused physreg defs as dead.
2383 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2388 bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
2392 bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
2394 // Make sure we don't bloat code by inlining very large memcpy's.
2395 if (!ARMIsMemCpySmall(Len))
2398 // We don't care about alignment here since we just emit integer accesses.
2412 RV = ARMEmitLoad(VT, ResultReg, Src);
2413 assert (RV == true && "Should be able to handle this load.");
2414 RV = ARMEmitStore(VT, ResultReg, Dest);
2415 assert (RV == true && "Should be able to handle this store.");
2418 unsigned Size = VT.getSizeInBits()/8;
2420 Dest.Offset += Size;
2427 bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2428 // FIXME: Handle more intrinsics.
2429 switch (I.getIntrinsicID()) {
2430 default: return false;
2431 case Intrinsic::frameaddress: {
2432 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2433 MFI->setFrameAddressIsTaken(true);
2436 const TargetRegisterClass *RC;
2438 LdrOpc = ARM::t2LDRi12;
2439 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2441 LdrOpc = ARM::LDRi12;
2442 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2445 const ARMBaseRegisterInfo *RegInfo =
2446 static_cast<const ARMBaseRegisterInfo*>(TM.getRegisterInfo());
2447 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2448 unsigned SrcReg = FramePtr;
2450 // Recursively load frame address
2456 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2458 DestReg = createResultReg(RC);
2459 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2460 TII.get(LdrOpc), DestReg)
2461 .addReg(SrcReg).addImm(0));
2464 UpdateValueMap(&I, SrcReg);
2467 case Intrinsic::memcpy:
2468 case Intrinsic::memmove: {
2469 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2470 // Don't handle volatile.
2471 if (MTI.isVolatile())
2474 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2475 // we would emit dead code because we don't currently handle memmoves.
2476 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2477 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
2478 // Small memcpy's are common enough that we want to do them without a call
2480 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
2481 if (ARMIsMemCpySmall(Len)) {
2483 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2484 !ARMComputeAddress(MTI.getRawSource(), Src))
2486 if (ARMTryEmitSmallMemCpy(Dest, Src, Len))
2491 if (!MTI.getLength()->getType()->isIntegerTy(32))
2494 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2497 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2498 return SelectCall(&I, IntrMemName);
2500 case Intrinsic::memset: {
2501 const MemSetInst &MSI = cast<MemSetInst>(I);
2502 // Don't handle volatile.
2503 if (MSI.isVolatile())
2506 if (!MSI.getLength()->getType()->isIntegerTy(32))
2509 if (MSI.getDestAddressSpace() > 255)
2512 return SelectCall(&I, "memset");
2514 case Intrinsic::trap: {
2515 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::TRAP));
2521 bool ARMFastISel::SelectTrunc(const Instruction *I) {
2522 // The high bits for a type smaller than the register size are assumed to be
2524 Value *Op = I->getOperand(0);
2527 SrcVT = TLI.getValueType(Op->getType(), true);
2528 DestVT = TLI.getValueType(I->getType(), true);
2530 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2532 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2535 unsigned SrcReg = getRegForValue(Op);
2536 if (!SrcReg) return false;
2538 // Because the high bits are undefined, a truncate doesn't generate
2540 UpdateValueMap(I, SrcReg);
2544 unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2546 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
2550 bool isBoolZext = false;
2551 if (!SrcVT.isSimple()) return 0;
2552 switch (SrcVT.getSimpleVT().SimpleTy) {
2555 if (!Subtarget->hasV6Ops()) return 0;
2557 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
2559 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
2562 if (!Subtarget->hasV6Ops()) return 0;
2564 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
2566 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
2570 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
2577 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
2578 MachineInstrBuilder MIB;
2579 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
2585 AddOptionalDefs(MIB);
2589 bool ARMFastISel::SelectIntExt(const Instruction *I) {
2590 // On ARM, in general, integer casts don't involve legal types; this code
2591 // handles promotable integers.
2592 Type *DestTy = I->getType();
2593 Value *Src = I->getOperand(0);
2594 Type *SrcTy = Src->getType();
2597 SrcVT = TLI.getValueType(SrcTy, true);
2598 DestVT = TLI.getValueType(DestTy, true);
2600 bool isZExt = isa<ZExtInst>(I);
2601 unsigned SrcReg = getRegForValue(Src);
2602 if (!SrcReg) return false;
2604 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2605 if (ResultReg == 0) return false;
2606 UpdateValueMap(I, ResultReg);
2610 // TODO: SoftFP support.
2611 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
2613 switch (I->getOpcode()) {
2614 case Instruction::Load:
2615 return SelectLoad(I);
2616 case Instruction::Store:
2617 return SelectStore(I);
2618 case Instruction::Br:
2619 return SelectBranch(I);
2620 case Instruction::IndirectBr:
2621 return SelectIndirectBr(I);
2622 case Instruction::ICmp:
2623 case Instruction::FCmp:
2624 return SelectCmp(I);
2625 case Instruction::FPExt:
2626 return SelectFPExt(I);
2627 case Instruction::FPTrunc:
2628 return SelectFPTrunc(I);
2629 case Instruction::SIToFP:
2630 return SelectIToFP(I, /*isSigned*/ true);
2631 case Instruction::UIToFP:
2632 return SelectIToFP(I, /*isSigned*/ false);
2633 case Instruction::FPToSI:
2634 return SelectFPToI(I, /*isSigned*/ true);
2635 case Instruction::FPToUI:
2636 return SelectFPToI(I, /*isSigned*/ false);
2637 case Instruction::Add:
2638 return SelectBinaryIntOp(I, ISD::ADD);
2639 case Instruction::Or:
2640 return SelectBinaryIntOp(I, ISD::OR);
2641 case Instruction::Sub:
2642 return SelectBinaryIntOp(I, ISD::SUB);
2643 case Instruction::FAdd:
2644 return SelectBinaryFPOp(I, ISD::FADD);
2645 case Instruction::FSub:
2646 return SelectBinaryFPOp(I, ISD::FSUB);
2647 case Instruction::FMul:
2648 return SelectBinaryFPOp(I, ISD::FMUL);
2649 case Instruction::SDiv:
2650 return SelectDiv(I, /*isSigned*/ true);
2651 case Instruction::UDiv:
2652 return SelectDiv(I, /*isSigned*/ false);
2653 case Instruction::SRem:
2654 return SelectRem(I, /*isSigned*/ true);
2655 case Instruction::URem:
2656 return SelectRem(I, /*isSigned*/ false);
2657 case Instruction::Call:
2658 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2659 return SelectIntrinsicCall(*II);
2660 return SelectCall(I);
2661 case Instruction::Select:
2662 return SelectSelect(I);
2663 case Instruction::Ret:
2664 return SelectRet(I);
2665 case Instruction::Trunc:
2666 return SelectTrunc(I);
2667 case Instruction::ZExt:
2668 case Instruction::SExt:
2669 return SelectIntExt(I);
2675 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2676 /// vreg is being provided by the specified load instruction. If possible,
2677 /// try to fold the load as an operand to the instruction, returning true if
2679 bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2680 const LoadInst *LI) {
2681 // Verify we have a legal type before going any further.
2683 if (!isLoadTypeLegal(LI->getType(), VT))
2686 // Combine load followed by zero- or sign-extend.
2687 // ldrb r1, [r0] ldrb r1, [r0]
2689 // mov r3, r2 mov r3, r1
2691 switch(MI->getOpcode()) {
2692 default: return false;
2710 // See if we can handle this address.
2712 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2714 unsigned ResultReg = MI->getOperand(0).getReg();
2715 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
2717 MI->eraseFromParent();
2722 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
2723 // Completely untested on non-iOS.
2724 const TargetMachine &TM = funcInfo.MF->getTarget();
2726 // Darwin and thumb1 only for now.
2727 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
2728 if (Subtarget->isTargetIOS() && !Subtarget->isThumb1Only())
2729 return new ARMFastISel(funcInfo);