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,
91 const TargetLibraryInfo *libInfo)
92 : FastISel(funcInfo, libInfo),
93 TM(funcInfo.MF->getTarget()),
94 TII(*TM.getInstrInfo()),
95 TLI(*TM.getTargetLowering()) {
96 Subtarget = &TM.getSubtarget<ARMSubtarget>();
97 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
98 isThumb2 = AFI->isThumbFunction();
99 Context = &funcInfo.Fn->getContext();
102 // Code from FastISel.cpp.
104 unsigned FastEmitInst_(unsigned MachineInstOpcode,
105 const TargetRegisterClass *RC);
106 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
107 const TargetRegisterClass *RC,
108 unsigned Op0, bool Op0IsKill);
109 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC,
111 unsigned Op0, bool Op0IsKill,
112 unsigned Op1, bool Op1IsKill);
113 unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
114 const TargetRegisterClass *RC,
115 unsigned Op0, bool Op0IsKill,
116 unsigned Op1, bool Op1IsKill,
117 unsigned Op2, bool Op2IsKill);
118 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill,
122 unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
123 const TargetRegisterClass *RC,
124 unsigned Op0, bool Op0IsKill,
125 const ConstantFP *FPImm);
126 unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
127 const TargetRegisterClass *RC,
128 unsigned Op0, bool Op0IsKill,
129 unsigned Op1, bool Op1IsKill,
131 unsigned FastEmitInst_i(unsigned MachineInstOpcode,
132 const TargetRegisterClass *RC,
134 unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
135 const TargetRegisterClass *RC,
136 uint64_t Imm1, uint64_t Imm2);
138 unsigned FastEmitInst_extractsubreg(MVT RetVT,
139 unsigned Op0, bool Op0IsKill,
142 // Backend specific FastISel code.
144 virtual bool TargetSelectInstruction(const Instruction *I);
145 virtual unsigned TargetMaterializeConstant(const Constant *C);
146 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
147 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
150 #include "ARMGenFastISel.inc"
152 // Instruction selection routines.
154 bool SelectLoad(const Instruction *I);
155 bool SelectStore(const Instruction *I);
156 bool SelectBranch(const Instruction *I);
157 bool SelectIndirectBr(const Instruction *I);
158 bool SelectCmp(const Instruction *I);
159 bool SelectFPExt(const Instruction *I);
160 bool SelectFPTrunc(const Instruction *I);
161 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
162 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
163 bool SelectIToFP(const Instruction *I, bool isSigned);
164 bool SelectFPToI(const Instruction *I, bool isSigned);
165 bool SelectDiv(const Instruction *I, bool isSigned);
166 bool SelectRem(const Instruction *I, bool isSigned);
167 bool SelectCall(const Instruction *I, const char *IntrMemName);
168 bool SelectIntrinsicCall(const IntrinsicInst &I);
169 bool SelectSelect(const Instruction *I);
170 bool SelectRet(const Instruction *I);
171 bool SelectTrunc(const Instruction *I);
172 bool SelectIntExt(const Instruction *I);
173 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
177 bool isTypeLegal(Type *Ty, MVT &VT);
178 bool isLoadTypeLegal(Type *Ty, MVT &VT);
179 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
181 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
182 unsigned Alignment = 0, bool isZExt = true,
183 bool allocReg = true);
184 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr,
185 unsigned Alignment = 0);
186 bool ARMComputeAddress(const Value *Obj, Address &Addr);
187 void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3);
188 bool ARMIsMemCpySmall(uint64_t Len);
189 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len);
190 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
191 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
192 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
193 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
194 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
195 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
196 unsigned ARMSelectCallOp(bool UseReg);
198 // Call handling routines.
200 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
203 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
204 SmallVectorImpl<unsigned> &ArgRegs,
205 SmallVectorImpl<MVT> &ArgVTs,
206 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
207 SmallVectorImpl<unsigned> &RegArgs,
211 unsigned getLibcallReg(const Twine &Name);
212 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
213 const Instruction *I, CallingConv::ID CC,
214 unsigned &NumBytes, bool isVarArg);
215 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
217 // OptionalDef handling routines.
219 bool isARMNEONPred(const MachineInstr *MI);
220 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
221 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
222 void AddLoadStoreOperands(EVT VT, Address &Addr,
223 const MachineInstrBuilder &MIB,
224 unsigned Flags, bool useAM3);
227 } // end anonymous namespace
229 #include "ARMGenCallingConv.inc"
231 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
232 // we don't care about implicit defs here, just places we'll need to add a
233 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
234 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
235 if (!MI->hasOptionalDef())
238 // Look to see if our OptionalDef is defining CPSR or CCR.
239 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
240 const MachineOperand &MO = MI->getOperand(i);
241 if (!MO.isReg() || !MO.isDef()) continue;
242 if (MO.getReg() == ARM::CPSR)
248 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
249 const MCInstrDesc &MCID = MI->getDesc();
251 // If we're a thumb2 or not NEON function we were handled via isPredicable.
252 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
253 AFI->isThumb2Function())
256 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
257 if (MCID.OpInfo[i].isPredicate())
263 // If the machine is predicable go ahead and add the predicate operands, if
264 // it needs default CC operands add those.
265 // TODO: If we want to support thumb1 then we'll need to deal with optional
266 // CPSR defs that need to be added before the remaining operands. See s_cc_out
267 // for descriptions why.
268 const MachineInstrBuilder &
269 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
270 MachineInstr *MI = &*MIB;
272 // Do we use a predicate? or...
273 // Are we NEON in ARM mode and have a predicate operand? If so, I know
274 // we're not predicable but add it anyways.
275 if (TII.isPredicable(MI) || isARMNEONPred(MI))
278 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
279 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
281 if (DefinesOptionalPredicate(MI, &CPSR)) {
290 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
291 const TargetRegisterClass* RC) {
292 unsigned ResultReg = createResultReg(RC);
293 const MCInstrDesc &II = TII.get(MachineInstOpcode);
295 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
299 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
300 const TargetRegisterClass *RC,
301 unsigned Op0, bool Op0IsKill) {
302 unsigned ResultReg = createResultReg(RC);
303 const MCInstrDesc &II = TII.get(MachineInstOpcode);
305 if (II.getNumDefs() >= 1) {
306 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
307 .addReg(Op0, Op0IsKill * RegState::Kill));
309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
310 .addReg(Op0, Op0IsKill * RegState::Kill));
311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
312 TII.get(TargetOpcode::COPY), ResultReg)
313 .addReg(II.ImplicitDefs[0]));
318 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
319 const TargetRegisterClass *RC,
320 unsigned Op0, bool Op0IsKill,
321 unsigned Op1, bool Op1IsKill) {
322 unsigned ResultReg = createResultReg(RC);
323 const MCInstrDesc &II = TII.get(MachineInstOpcode);
325 if (II.getNumDefs() >= 1) {
326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
327 .addReg(Op0, Op0IsKill * RegState::Kill)
328 .addReg(Op1, Op1IsKill * RegState::Kill));
330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
331 .addReg(Op0, Op0IsKill * RegState::Kill)
332 .addReg(Op1, Op1IsKill * RegState::Kill));
333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
334 TII.get(TargetOpcode::COPY), ResultReg)
335 .addReg(II.ImplicitDefs[0]));
340 unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
341 const TargetRegisterClass *RC,
342 unsigned Op0, bool Op0IsKill,
343 unsigned Op1, bool Op1IsKill,
344 unsigned Op2, bool Op2IsKill) {
345 unsigned ResultReg = createResultReg(RC);
346 const MCInstrDesc &II = TII.get(MachineInstOpcode);
348 if (II.getNumDefs() >= 1) {
349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
350 .addReg(Op0, Op0IsKill * RegState::Kill)
351 .addReg(Op1, Op1IsKill * RegState::Kill)
352 .addReg(Op2, Op2IsKill * RegState::Kill));
354 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
355 .addReg(Op0, Op0IsKill * RegState::Kill)
356 .addReg(Op1, Op1IsKill * RegState::Kill)
357 .addReg(Op2, Op2IsKill * RegState::Kill));
358 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
359 TII.get(TargetOpcode::COPY), ResultReg)
360 .addReg(II.ImplicitDefs[0]));
365 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
366 const TargetRegisterClass *RC,
367 unsigned Op0, bool Op0IsKill,
369 unsigned ResultReg = createResultReg(RC);
370 const MCInstrDesc &II = TII.get(MachineInstOpcode);
372 if (II.getNumDefs() >= 1) {
373 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
374 .addReg(Op0, Op0IsKill * RegState::Kill)
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
378 .addReg(Op0, Op0IsKill * RegState::Kill)
380 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
381 TII.get(TargetOpcode::COPY), ResultReg)
382 .addReg(II.ImplicitDefs[0]));
387 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
388 const TargetRegisterClass *RC,
389 unsigned Op0, bool Op0IsKill,
390 const ConstantFP *FPImm) {
391 unsigned ResultReg = createResultReg(RC);
392 const MCInstrDesc &II = TII.get(MachineInstOpcode);
394 if (II.getNumDefs() >= 1) {
395 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
396 .addReg(Op0, Op0IsKill * RegState::Kill)
399 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
400 .addReg(Op0, Op0IsKill * RegState::Kill)
402 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
403 TII.get(TargetOpcode::COPY), ResultReg)
404 .addReg(II.ImplicitDefs[0]));
409 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
410 const TargetRegisterClass *RC,
411 unsigned Op0, bool Op0IsKill,
412 unsigned Op1, bool Op1IsKill,
414 unsigned ResultReg = createResultReg(RC);
415 const MCInstrDesc &II = TII.get(MachineInstOpcode);
417 if (II.getNumDefs() >= 1) {
418 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
419 .addReg(Op0, Op0IsKill * RegState::Kill)
420 .addReg(Op1, Op1IsKill * RegState::Kill)
423 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
424 .addReg(Op0, Op0IsKill * RegState::Kill)
425 .addReg(Op1, Op1IsKill * RegState::Kill)
427 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
428 TII.get(TargetOpcode::COPY), ResultReg)
429 .addReg(II.ImplicitDefs[0]));
434 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
435 const TargetRegisterClass *RC,
437 unsigned ResultReg = createResultReg(RC);
438 const MCInstrDesc &II = TII.get(MachineInstOpcode);
440 if (II.getNumDefs() >= 1) {
441 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
446 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
447 TII.get(TargetOpcode::COPY), ResultReg)
448 .addReg(II.ImplicitDefs[0]));
453 unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
454 const TargetRegisterClass *RC,
455 uint64_t Imm1, uint64_t Imm2) {
456 unsigned ResultReg = createResultReg(RC);
457 const MCInstrDesc &II = TII.get(MachineInstOpcode);
459 if (II.getNumDefs() >= 1) {
460 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
461 .addImm(Imm1).addImm(Imm2));
463 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
464 .addImm(Imm1).addImm(Imm2));
465 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
466 TII.get(TargetOpcode::COPY),
468 .addReg(II.ImplicitDefs[0]));
473 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
474 unsigned Op0, bool Op0IsKill,
476 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
477 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
478 "Cannot yet extract from physregs");
480 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
481 DL, TII.get(TargetOpcode::COPY), ResultReg)
482 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
486 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
487 // checks from the various callers.
488 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
489 if (VT == MVT::f64) return 0;
491 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
492 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
493 TII.get(ARM::VMOVSR), MoveReg)
498 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
499 if (VT == MVT::i64) return 0;
501 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
502 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
503 TII.get(ARM::VMOVRS), MoveReg)
508 // For double width floating point we need to materialize two constants
509 // (the high and the low) into integer registers then use a move to get
510 // the combined constant into an FP reg.
511 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
512 const APFloat Val = CFP->getValueAPF();
513 bool is64bit = VT == MVT::f64;
515 // This checks to see if we can use VFP3 instructions to materialize
516 // a constant, otherwise we have to go through the constant pool.
517 if (TLI.isFPImmLegal(Val, VT)) {
521 Imm = ARM_AM::getFP64Imm(Val);
524 Imm = ARM_AM::getFP32Imm(Val);
527 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
528 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
534 // Require VFP2 for loading fp constants.
535 if (!Subtarget->hasVFP2()) return false;
537 // MachineConstantPool wants an explicit alignment.
538 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
540 // TODO: Figure out if this is correct.
541 Align = TD.getTypeAllocSize(CFP->getType());
543 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
544 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
545 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
547 // The extra reg is for addrmode5.
548 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
550 .addConstantPoolIndex(Idx)
555 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
557 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
560 // If we can do this in a single instruction without a constant pool entry
562 const ConstantInt *CI = cast<ConstantInt>(C);
563 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
564 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
565 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
566 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
567 TII.get(Opc), ImmReg)
568 .addImm(CI->getZExtValue()));
572 // Use MVN to emit negative constants.
573 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
574 unsigned Imm = (unsigned)~(CI->getSExtValue());
575 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
576 (ARM_AM::getSOImmVal(Imm) != -1);
578 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
579 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
580 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
581 TII.get(Opc), ImmReg)
587 // Load from constant pool. For now 32-bit only.
591 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
593 // MachineConstantPool wants an explicit alignment.
594 unsigned Align = TD.getPrefTypeAlignment(C->getType());
596 // TODO: Figure out if this is correct.
597 Align = TD.getTypeAllocSize(C->getType());
599 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
602 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
603 TII.get(ARM::t2LDRpci), DestReg)
604 .addConstantPoolIndex(Idx));
606 // The extra immediate is for addrmode2.
607 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
608 TII.get(ARM::LDRcp), DestReg)
609 .addConstantPoolIndex(Idx)
615 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
616 // For now 32-bit only.
617 if (VT != MVT::i32) return 0;
619 Reloc::Model RelocM = TM.getRelocationModel();
620 bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
621 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
623 // Use movw+movt when possible, it avoids constant pool entries.
624 // Darwin targets don't support movt with Reloc::Static, see
625 // ARMTargetLowering::LowerGlobalAddressDarwin. Other targets only support
626 // static movt relocations.
627 if (Subtarget->useMovt() &&
628 Subtarget->isTargetDarwin() == (RelocM != Reloc::Static)) {
632 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
634 case Reloc::DynamicNoPIC:
635 Opc = isThumb2 ? ARM::t2MOV_ga_dyn : ARM::MOV_ga_dyn;
638 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
641 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
642 DestReg).addGlobalAddress(GV));
644 // MachineConstantPool wants an explicit alignment.
645 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
647 // TODO: Figure out if this is correct.
648 Align = TD.getTypeAllocSize(GV->getType());
652 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
653 (Subtarget->isThumb() ? 4 : 8);
654 unsigned Id = AFI->createPICLabelUId();
655 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
658 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
661 MachineInstrBuilder MIB;
663 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
664 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
665 .addConstantPoolIndex(Idx);
666 if (RelocM == Reloc::PIC_)
668 AddOptionalDefs(MIB);
670 // The extra immediate is for addrmode2.
671 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
673 .addConstantPoolIndex(Idx)
675 AddOptionalDefs(MIB);
677 if (RelocM == Reloc::PIC_) {
678 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
679 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
681 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
682 DL, TII.get(Opc), NewDestReg)
685 AddOptionalDefs(MIB);
692 MachineInstrBuilder MIB;
693 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
695 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
696 TII.get(ARM::t2LDRi12), NewDestReg)
700 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
704 DestReg = NewDestReg;
705 AddOptionalDefs(MIB);
711 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
712 EVT VT = TLI.getValueType(C->getType(), true);
714 // Only handle simple types.
715 if (!VT.isSimple()) return 0;
717 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
718 return ARMMaterializeFP(CFP, VT);
719 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
720 return ARMMaterializeGV(GV, VT);
721 else if (isa<ConstantInt>(C))
722 return ARMMaterializeInt(C, VT);
727 // TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
729 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
730 // Don't handle dynamic allocas.
731 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
734 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
736 DenseMap<const AllocaInst*, int>::iterator SI =
737 FuncInfo.StaticAllocaMap.find(AI);
739 // This will get lowered later into the correct offsets and registers
740 // via rewriteXFrameIndex.
741 if (SI != FuncInfo.StaticAllocaMap.end()) {
742 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
743 unsigned ResultReg = createResultReg(RC);
744 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
745 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
746 TII.get(Opc), ResultReg)
747 .addFrameIndex(SI->second)
755 bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
756 EVT evt = TLI.getValueType(Ty, true);
758 // Only handle simple types.
759 if (evt == MVT::Other || !evt.isSimple()) return false;
760 VT = evt.getSimpleVT();
762 // Handle all legal types, i.e. a register that will directly hold this
764 return TLI.isTypeLegal(VT);
767 bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
768 if (isTypeLegal(Ty, VT)) return true;
770 // If this is a type than can be sign or zero-extended to a basic operation
771 // go ahead and accept it now.
772 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
778 // Computes the address to get to an object.
779 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
780 // Some boilerplate from the X86 FastISel.
781 const User *U = NULL;
782 unsigned Opcode = Instruction::UserOp1;
783 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
784 // Don't walk into other basic blocks unless the object is an alloca from
785 // another block, otherwise it may not have a virtual register assigned.
786 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
787 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
788 Opcode = I->getOpcode();
791 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
792 Opcode = C->getOpcode();
796 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
797 if (Ty->getAddressSpace() > 255)
798 // Fast instruction selection doesn't support the special
805 case Instruction::BitCast: {
806 // Look through bitcasts.
807 return ARMComputeAddress(U->getOperand(0), Addr);
809 case Instruction::IntToPtr: {
810 // Look past no-op inttoptrs.
811 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
812 return ARMComputeAddress(U->getOperand(0), Addr);
815 case Instruction::PtrToInt: {
816 // Look past no-op ptrtoints.
817 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
818 return ARMComputeAddress(U->getOperand(0), Addr);
821 case Instruction::GetElementPtr: {
822 Address SavedAddr = Addr;
823 int TmpOffset = Addr.Offset;
825 // Iterate through the GEP folding the constants into offsets where
827 gep_type_iterator GTI = gep_type_begin(U);
828 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
829 i != e; ++i, ++GTI) {
830 const Value *Op = *i;
831 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
832 const StructLayout *SL = TD.getStructLayout(STy);
833 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
834 TmpOffset += SL->getElementOffset(Idx);
836 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
838 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
839 // Constant-offset addressing.
840 TmpOffset += CI->getSExtValue() * S;
843 if (isa<AddOperator>(Op) &&
844 (!isa<Instruction>(Op) ||
845 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
847 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
848 // An add (in the same block) with a constant operand. Fold the
851 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
852 TmpOffset += CI->getSExtValue() * S;
853 // Iterate on the other operand.
854 Op = cast<AddOperator>(Op)->getOperand(0);
858 goto unsupported_gep;
863 // Try to grab the base operand now.
864 Addr.Offset = TmpOffset;
865 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
867 // We failed, restore everything and try the other options.
873 case Instruction::Alloca: {
874 const AllocaInst *AI = cast<AllocaInst>(Obj);
875 DenseMap<const AllocaInst*, int>::iterator SI =
876 FuncInfo.StaticAllocaMap.find(AI);
877 if (SI != FuncInfo.StaticAllocaMap.end()) {
878 Addr.BaseType = Address::FrameIndexBase;
879 Addr.Base.FI = SI->second;
886 // Try to get this in a register if nothing else has worked.
887 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
888 return Addr.Base.Reg != 0;
891 void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) {
893 assert(VT.isSimple() && "Non-simple types are invalid here!");
895 bool needsLowering = false;
896 switch (VT.getSimpleVT().SimpleTy) {
897 default: llvm_unreachable("Unhandled load/store type!");
903 // Integer loads/stores handle 12-bit offsets.
904 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
905 // Handle negative offsets.
906 if (needsLowering && isThumb2)
907 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
910 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
911 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
916 // Floating point operands handle 8-bit offsets.
917 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
921 // If this is a stack pointer and the offset needs to be simplified then
922 // put the alloca address into a register, set the base type back to
923 // register and continue. This should almost never happen.
924 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
925 const TargetRegisterClass *RC = isThumb2 ?
926 (const TargetRegisterClass*)&ARM::tGPRRegClass :
927 (const TargetRegisterClass*)&ARM::GPRRegClass;
928 unsigned ResultReg = createResultReg(RC);
929 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
930 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
931 TII.get(Opc), ResultReg)
932 .addFrameIndex(Addr.Base.FI)
934 Addr.Base.Reg = ResultReg;
935 Addr.BaseType = Address::RegBase;
938 // Since the offset is too large for the load/store instruction
939 // get the reg+offset into a register.
941 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
942 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
947 void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
948 const MachineInstrBuilder &MIB,
949 unsigned Flags, bool useAM3) {
950 // addrmode5 output depends on the selection dag addressing dividing the
951 // offset by 4 that it then later multiplies. Do this here as well.
952 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
953 VT.getSimpleVT().SimpleTy == MVT::f64)
956 // Frame base works a bit differently. Handle it separately.
957 if (Addr.BaseType == Address::FrameIndexBase) {
958 int FI = Addr.Base.FI;
959 int Offset = Addr.Offset;
960 MachineMemOperand *MMO =
961 FuncInfo.MF->getMachineMemOperand(
962 MachinePointerInfo::getFixedStack(FI, Offset),
964 MFI.getObjectSize(FI),
965 MFI.getObjectAlignment(FI));
966 // Now add the rest of the operands.
967 MIB.addFrameIndex(FI);
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);
978 MIB.addMemOperand(MMO);
980 // Now add the rest of the operands.
981 MIB.addReg(Addr.Base.Reg);
983 // ARM halfword load/stores and signed byte loads need an additional
986 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
990 MIB.addImm(Addr.Offset);
993 AddOptionalDefs(MIB);
996 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
997 unsigned Alignment, bool isZExt, bool allocReg) {
998 assert(VT.isSimple() && "Non-simple types are invalid here!");
1000 bool useAM3 = false;
1001 bool needVMOV = false;
1002 const TargetRegisterClass *RC;
1003 switch (VT.getSimpleVT().SimpleTy) {
1004 // This is mostly going to be Neon/vector support.
1005 default: return false;
1009 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1010 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
1012 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
1021 RC = &ARM::GPRRegClass;
1025 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1026 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
1028 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1030 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1033 RC = &ARM::GPRRegClass;
1037 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1040 Opc = ARM::t2LDRi12;
1044 RC = &ARM::GPRRegClass;
1047 if (!Subtarget->hasVFP2()) return false;
1048 // Unaligned loads need special handling. Floats require word-alignment.
1049 if (Alignment && Alignment < 4) {
1052 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
1053 RC = &ARM::GPRRegClass;
1056 RC = TLI.getRegClassFor(VT);
1060 if (!Subtarget->hasVFP2()) return false;
1061 // FIXME: Unaligned loads need special handling. Doublewords require
1063 if (Alignment && Alignment < 4)
1067 RC = TLI.getRegClassFor(VT);
1070 // Simplify this down to something we can handle.
1071 ARMSimplifyAddress(Addr, VT, useAM3);
1073 // Create the base instruction, then add the operands.
1075 ResultReg = createResultReg(RC);
1076 assert (ResultReg > 255 && "Expected an allocated virtual register.");
1077 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1078 TII.get(Opc), ResultReg);
1079 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
1081 // If we had an unaligned load of a float we've converted it to an regular
1082 // load. Now we must move from the GRP to the FP register.
1084 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1085 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1086 TII.get(ARM::VMOVSR), MoveReg)
1087 .addReg(ResultReg));
1088 ResultReg = MoveReg;
1093 bool ARMFastISel::SelectLoad(const Instruction *I) {
1094 // Atomic loads need special handling.
1095 if (cast<LoadInst>(I)->isAtomic())
1098 // Verify we have a legal type before going any further.
1100 if (!isLoadTypeLegal(I->getType(), VT))
1103 // See if we can handle this address.
1105 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
1108 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1110 UpdateValueMap(I, ResultReg);
1114 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr,
1115 unsigned Alignment) {
1117 bool useAM3 = false;
1118 switch (VT.getSimpleVT().SimpleTy) {
1119 // This is mostly going to be Neon/vector support.
1120 default: return false;
1122 unsigned Res = createResultReg(isThumb2 ?
1123 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1124 (const TargetRegisterClass*)&ARM::GPRRegClass);
1125 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
1126 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1128 .addReg(SrcReg).addImm(1));
1130 } // Fallthrough here.
1133 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1134 StrOpc = ARM::t2STRBi8;
1136 StrOpc = ARM::t2STRBi12;
1138 StrOpc = ARM::STRBi12;
1143 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1144 StrOpc = ARM::t2STRHi8;
1146 StrOpc = ARM::t2STRHi12;
1154 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1155 StrOpc = ARM::t2STRi8;
1157 StrOpc = ARM::t2STRi12;
1159 StrOpc = ARM::STRi12;
1163 if (!Subtarget->hasVFP2()) return false;
1164 // Unaligned stores need special handling. Floats require word-alignment.
1165 if (Alignment && Alignment < 4) {
1166 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1167 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1168 TII.get(ARM::VMOVRS), MoveReg)
1172 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
1174 StrOpc = ARM::VSTRS;
1178 if (!Subtarget->hasVFP2()) return false;
1179 // FIXME: Unaligned stores need special handling. Doublewords require
1181 if (Alignment && Alignment < 4)
1184 StrOpc = ARM::VSTRD;
1187 // Simplify this down to something we can handle.
1188 ARMSimplifyAddress(Addr, VT, useAM3);
1190 // Create the base instruction, then add the operands.
1191 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1194 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
1198 bool ARMFastISel::SelectStore(const Instruction *I) {
1199 Value *Op0 = I->getOperand(0);
1200 unsigned SrcReg = 0;
1202 // Atomic stores need special handling.
1203 if (cast<StoreInst>(I)->isAtomic())
1206 // Verify we have a legal type before going any further.
1208 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
1211 // Get the value to be stored into a register.
1212 SrcReg = getRegForValue(Op0);
1213 if (SrcReg == 0) return false;
1215 // See if we can handle this address.
1217 if (!ARMComputeAddress(I->getOperand(1), Addr))
1220 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1225 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1227 // Needs two compares...
1228 case CmpInst::FCMP_ONE:
1229 case CmpInst::FCMP_UEQ:
1231 // AL is our "false" for now. The other two need more compares.
1233 case CmpInst::ICMP_EQ:
1234 case CmpInst::FCMP_OEQ:
1236 case CmpInst::ICMP_SGT:
1237 case CmpInst::FCMP_OGT:
1239 case CmpInst::ICMP_SGE:
1240 case CmpInst::FCMP_OGE:
1242 case CmpInst::ICMP_UGT:
1243 case CmpInst::FCMP_UGT:
1245 case CmpInst::FCMP_OLT:
1247 case CmpInst::ICMP_ULE:
1248 case CmpInst::FCMP_OLE:
1250 case CmpInst::FCMP_ORD:
1252 case CmpInst::FCMP_UNO:
1254 case CmpInst::FCMP_UGE:
1256 case CmpInst::ICMP_SLT:
1257 case CmpInst::FCMP_ULT:
1259 case CmpInst::ICMP_SLE:
1260 case CmpInst::FCMP_ULE:
1262 case CmpInst::FCMP_UNE:
1263 case CmpInst::ICMP_NE:
1265 case CmpInst::ICMP_UGE:
1267 case CmpInst::ICMP_ULT:
1272 bool ARMFastISel::SelectBranch(const Instruction *I) {
1273 const BranchInst *BI = cast<BranchInst>(I);
1274 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1275 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1277 // Simple branch support.
1279 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1281 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1282 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1284 // Get the compare predicate.
1285 // Try to take advantage of fallthrough opportunities.
1286 CmpInst::Predicate Predicate = CI->getPredicate();
1287 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1288 std::swap(TBB, FBB);
1289 Predicate = CmpInst::getInversePredicate(Predicate);
1292 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
1294 // We may not handle every CC for now.
1295 if (ARMPred == ARMCC::AL) return false;
1297 // Emit the compare.
1298 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1301 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1303 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1304 FastEmitBranch(FBB, DL);
1305 FuncInfo.MBB->addSuccessor(TBB);
1308 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1310 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1311 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
1312 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1313 unsigned OpReg = getRegForValue(TI->getOperand(0));
1314 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1316 .addReg(OpReg).addImm(1));
1318 unsigned CCMode = ARMCC::NE;
1319 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1320 std::swap(TBB, FBB);
1324 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1325 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1326 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1328 FastEmitBranch(FBB, DL);
1329 FuncInfo.MBB->addSuccessor(TBB);
1332 } else if (const ConstantInt *CI =
1333 dyn_cast<ConstantInt>(BI->getCondition())) {
1334 uint64_t Imm = CI->getZExtValue();
1335 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1336 FastEmitBranch(Target, DL);
1340 unsigned CmpReg = getRegForValue(BI->getCondition());
1341 if (CmpReg == 0) return false;
1343 // We've been divorced from our compare! Our block was split, and
1344 // now our compare lives in a predecessor block. We musn't
1345 // re-compare here, as the children of the compare aren't guaranteed
1346 // live across the block boundary (we *could* check for this).
1347 // Regardless, the compare has been done in the predecessor block,
1348 // and it left a value for us in a virtual register. Ergo, we test
1349 // the one-bit value left in the virtual register.
1350 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1351 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1352 .addReg(CmpReg).addImm(1));
1354 unsigned CCMode = ARMCC::NE;
1355 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1356 std::swap(TBB, FBB);
1360 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1361 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1362 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1363 FastEmitBranch(FBB, DL);
1364 FuncInfo.MBB->addSuccessor(TBB);
1368 bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1369 unsigned AddrReg = getRegForValue(I->getOperand(0));
1370 if (AddrReg == 0) return false;
1372 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
1373 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
1378 bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1380 Type *Ty = Src1Value->getType();
1381 EVT SrcVT = TLI.getValueType(Ty, true);
1382 if (!SrcVT.isSimple()) return false;
1384 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1385 if (isFloat && !Subtarget->hasVFP2())
1388 // Check to see if the 2nd operand is a constant that we can encode directly
1391 bool UseImm = false;
1392 bool isNegativeImm = false;
1393 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1394 // Thus, Src1Value may be a ConstantInt, but we're missing it.
1395 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1396 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1398 const APInt &CIVal = ConstInt->getValue();
1399 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1400 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
1401 // then a cmn, because there is no way to represent 2147483648 as a
1402 // signed 32-bit int.
1403 if (Imm < 0 && Imm != (int)0x80000000) {
1404 isNegativeImm = true;
1407 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1408 (ARM_AM::getSOImmVal(Imm) != -1);
1410 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1411 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1412 if (ConstFP->isZero() && !ConstFP->isNegative())
1418 bool needsExt = false;
1419 switch (SrcVT.getSimpleVT().SimpleTy) {
1420 default: return false;
1421 // TODO: Verify compares.
1424 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
1428 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
1434 // Intentional fall-through.
1438 CmpOpc = ARM::t2CMPrr;
1440 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
1443 CmpOpc = ARM::CMPrr;
1445 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
1450 unsigned SrcReg1 = getRegForValue(Src1Value);
1451 if (SrcReg1 == 0) return false;
1453 unsigned SrcReg2 = 0;
1455 SrcReg2 = getRegForValue(Src2Value);
1456 if (SrcReg2 == 0) return false;
1459 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1461 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1462 if (SrcReg1 == 0) return false;
1464 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1465 if (SrcReg2 == 0) return false;
1470 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1472 .addReg(SrcReg1).addReg(SrcReg2));
1474 MachineInstrBuilder MIB;
1475 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1478 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1481 AddOptionalDefs(MIB);
1484 // For floating point we need to move the result to a comparison register
1485 // that we can then use for branches.
1486 if (Ty->isFloatTy() || Ty->isDoubleTy())
1487 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1488 TII.get(ARM::FMSTAT)));
1492 bool ARMFastISel::SelectCmp(const Instruction *I) {
1493 const CmpInst *CI = cast<CmpInst>(I);
1495 // Get the compare predicate.
1496 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1498 // We may not handle every CC for now.
1499 if (ARMPred == ARMCC::AL) return false;
1501 // Emit the compare.
1502 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1505 // Now set a register based on the comparison. Explicitly set the predicates
1507 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1508 const TargetRegisterClass *RC = isThumb2 ?
1509 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1510 (const TargetRegisterClass*)&ARM::GPRRegClass;
1511 unsigned DestReg = createResultReg(RC);
1512 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1513 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1514 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
1515 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1516 .addReg(ZeroReg).addImm(1)
1517 .addImm(ARMPred).addReg(ARM::CPSR);
1519 UpdateValueMap(I, DestReg);
1523 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1524 // Make sure we have VFP and that we're extending float to double.
1525 if (!Subtarget->hasVFP2()) return false;
1527 Value *V = I->getOperand(0);
1528 if (!I->getType()->isDoubleTy() ||
1529 !V->getType()->isFloatTy()) return false;
1531 unsigned Op = getRegForValue(V);
1532 if (Op == 0) return false;
1534 unsigned Result = createResultReg(&ARM::DPRRegClass);
1535 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1536 TII.get(ARM::VCVTDS), Result)
1538 UpdateValueMap(I, Result);
1542 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1543 // Make sure we have VFP and that we're truncating double to float.
1544 if (!Subtarget->hasVFP2()) return false;
1546 Value *V = I->getOperand(0);
1547 if (!(I->getType()->isFloatTy() &&
1548 V->getType()->isDoubleTy())) return false;
1550 unsigned Op = getRegForValue(V);
1551 if (Op == 0) return false;
1553 unsigned Result = createResultReg(&ARM::SPRRegClass);
1554 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1555 TII.get(ARM::VCVTSD), Result)
1557 UpdateValueMap(I, Result);
1561 bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
1562 // Make sure we have VFP.
1563 if (!Subtarget->hasVFP2()) return false;
1566 Type *Ty = I->getType();
1567 if (!isTypeLegal(Ty, DstVT))
1570 Value *Src = I->getOperand(0);
1571 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1572 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1575 unsigned SrcReg = getRegForValue(Src);
1576 if (SrcReg == 0) return false;
1578 // Handle sign-extension.
1579 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1580 EVT DestVT = MVT::i32;
1581 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT,
1582 /*isZExt*/!isSigned);
1583 if (SrcReg == 0) return false;
1586 // The conversion routine works on fp-reg to fp-reg and the operand above
1587 // was an integer, move it to the fp registers if possible.
1588 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
1589 if (FP == 0) return false;
1592 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1593 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
1596 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1597 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1600 UpdateValueMap(I, ResultReg);
1604 bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
1605 // Make sure we have VFP.
1606 if (!Subtarget->hasVFP2()) return false;
1609 Type *RetTy = I->getType();
1610 if (!isTypeLegal(RetTy, DstVT))
1613 unsigned Op = getRegForValue(I->getOperand(0));
1614 if (Op == 0) return false;
1617 Type *OpTy = I->getOperand(0)->getType();
1618 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1619 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
1622 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
1623 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1624 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1628 // This result needs to be in an integer register, but the conversion only
1629 // takes place in fp-regs.
1630 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1631 if (IntReg == 0) return false;
1633 UpdateValueMap(I, IntReg);
1637 bool ARMFastISel::SelectSelect(const Instruction *I) {
1639 if (!isTypeLegal(I->getType(), VT))
1642 // Things need to be register sized for register moves.
1643 if (VT != MVT::i32) return false;
1644 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1646 unsigned CondReg = getRegForValue(I->getOperand(0));
1647 if (CondReg == 0) return false;
1648 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1649 if (Op1Reg == 0) return false;
1651 // Check to see if we can use an immediate in the conditional move.
1653 bool UseImm = false;
1654 bool isNegativeImm = false;
1655 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1656 assert (VT == MVT::i32 && "Expecting an i32.");
1657 Imm = (int)ConstInt->getValue().getZExtValue();
1659 isNegativeImm = true;
1662 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1663 (ARM_AM::getSOImmVal(Imm) != -1);
1666 unsigned Op2Reg = 0;
1668 Op2Reg = getRegForValue(I->getOperand(2));
1669 if (Op2Reg == 0) return false;
1672 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
1673 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1674 .addReg(CondReg).addImm(0));
1678 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1680 if (!isNegativeImm) {
1681 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1683 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1686 unsigned ResultReg = createResultReg(RC);
1688 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1689 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1691 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1692 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
1693 UpdateValueMap(I, ResultReg);
1697 bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
1699 Type *Ty = I->getType();
1700 if (!isTypeLegal(Ty, VT))
1703 // If we have integer div support we should have selected this automagically.
1704 // In case we have a real miss go ahead and return false and we'll pick
1706 if (Subtarget->hasDivide()) return false;
1708 // Otherwise emit a libcall.
1709 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1711 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
1712 else if (VT == MVT::i16)
1713 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
1714 else if (VT == MVT::i32)
1715 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
1716 else if (VT == MVT::i64)
1717 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
1718 else if (VT == MVT::i128)
1719 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
1720 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1722 return ARMEmitLibcall(I, LC);
1725 bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
1727 Type *Ty = I->getType();
1728 if (!isTypeLegal(Ty, VT))
1731 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1733 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
1734 else if (VT == MVT::i16)
1735 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
1736 else if (VT == MVT::i32)
1737 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
1738 else if (VT == MVT::i64)
1739 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
1740 else if (VT == MVT::i128)
1741 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
1742 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1744 return ARMEmitLibcall(I, LC);
1747 bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1748 EVT DestVT = TLI.getValueType(I->getType(), true);
1750 // We can get here in the case when we have a binary operation on a non-legal
1751 // type and the target independent selector doesn't know how to handle it.
1752 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1756 switch (ISDOpcode) {
1757 default: return false;
1759 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1762 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1765 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1769 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1770 if (SrcReg1 == 0) return false;
1772 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1773 // in the instruction, rather then materializing the value in a register.
1774 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1775 if (SrcReg2 == 0) return false;
1777 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1778 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1779 TII.get(Opc), ResultReg)
1780 .addReg(SrcReg1).addReg(SrcReg2));
1781 UpdateValueMap(I, ResultReg);
1785 bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
1786 EVT VT = TLI.getValueType(I->getType(), true);
1788 // We can get here in the case when we want to use NEON for our fp
1789 // operations, but can't figure out how to. Just use the vfp instructions
1791 // FIXME: It'd be nice to use NEON instructions.
1792 Type *Ty = I->getType();
1793 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1794 if (isFloat && !Subtarget->hasVFP2())
1798 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1799 switch (ISDOpcode) {
1800 default: return false;
1802 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1805 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1808 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1811 unsigned Op1 = getRegForValue(I->getOperand(0));
1812 if (Op1 == 0) return false;
1814 unsigned Op2 = getRegForValue(I->getOperand(1));
1815 if (Op2 == 0) return false;
1817 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1818 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1819 TII.get(Opc), ResultReg)
1820 .addReg(Op1).addReg(Op2));
1821 UpdateValueMap(I, ResultReg);
1825 // Call Handling Code
1827 // This is largely taken directly from CCAssignFnForNode
1828 // TODO: We may not support all of this.
1829 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1834 llvm_unreachable("Unsupported calling convention");
1835 case CallingConv::Fast:
1836 if (Subtarget->hasVFP2() && !isVarArg) {
1837 if (!Subtarget->isAAPCS_ABI())
1838 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1839 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1840 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1843 case CallingConv::C:
1844 // Use target triple & subtarget features to do actual dispatch.
1845 if (Subtarget->isAAPCS_ABI()) {
1846 if (Subtarget->hasVFP2() &&
1847 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
1848 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1850 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1852 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1853 case CallingConv::ARM_AAPCS_VFP:
1855 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1856 // Fall through to soft float variant, variadic functions don't
1857 // use hard floating point ABI.
1858 case CallingConv::ARM_AAPCS:
1859 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1860 case CallingConv::ARM_APCS:
1861 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1862 case CallingConv::GHC:
1864 llvm_unreachable("Can't return in GHC call convention");
1866 return CC_ARM_APCS_GHC;
1870 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1871 SmallVectorImpl<unsigned> &ArgRegs,
1872 SmallVectorImpl<MVT> &ArgVTs,
1873 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1874 SmallVectorImpl<unsigned> &RegArgs,
1878 SmallVector<CCValAssign, 16> ArgLocs;
1879 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
1880 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1881 CCAssignFnForCall(CC, false, isVarArg));
1883 // Check that we can handle all of the arguments. If we can't, then bail out
1884 // now before we add code to the MBB.
1885 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1886 CCValAssign &VA = ArgLocs[i];
1887 MVT ArgVT = ArgVTs[VA.getValNo()];
1889 // We don't handle NEON/vector parameters yet.
1890 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1893 // Now copy/store arg to correct locations.
1894 if (VA.isRegLoc() && !VA.needsCustom()) {
1896 } else if (VA.needsCustom()) {
1897 // TODO: We need custom lowering for vector (v2f64) args.
1898 if (VA.getLocVT() != MVT::f64 ||
1899 // TODO: Only handle register args for now.
1900 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1903 switch (static_cast<EVT>(ArgVT).getSimpleVT().SimpleTy) {
1912 if (!Subtarget->hasVFP2())
1916 if (!Subtarget->hasVFP2())
1923 // At the point, we are able to handle the call's arguments in fast isel.
1925 // Get a count of how many bytes are to be pushed on the stack.
1926 NumBytes = CCInfo.getNextStackOffset();
1928 // Issue CALLSEQ_START
1929 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1930 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1931 TII.get(AdjStackDown))
1934 // Process the args.
1935 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1936 CCValAssign &VA = ArgLocs[i];
1937 unsigned Arg = ArgRegs[VA.getValNo()];
1938 MVT ArgVT = ArgVTs[VA.getValNo()];
1940 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
1941 "We don't handle NEON/vector parameters yet.");
1943 // Handle arg promotion, etc.
1944 switch (VA.getLocInfo()) {
1945 case CCValAssign::Full: break;
1946 case CCValAssign::SExt: {
1947 MVT DestVT = VA.getLocVT();
1948 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
1949 assert (Arg != 0 && "Failed to emit a sext");
1953 case CCValAssign::AExt:
1954 // Intentional fall-through. Handle AExt and ZExt.
1955 case CCValAssign::ZExt: {
1956 MVT DestVT = VA.getLocVT();
1957 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
1958 assert (Arg != 0 && "Failed to emit a sext");
1962 case CCValAssign::BCvt: {
1963 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
1964 /*TODO: Kill=*/false);
1965 assert(BC != 0 && "Failed to emit a bitcast!");
1967 ArgVT = VA.getLocVT();
1970 default: llvm_unreachable("Unknown arg promotion!");
1973 // Now copy/store arg to correct locations.
1974 if (VA.isRegLoc() && !VA.needsCustom()) {
1975 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1978 RegArgs.push_back(VA.getLocReg());
1979 } else if (VA.needsCustom()) {
1980 // TODO: We need custom lowering for vector (v2f64) args.
1981 assert(VA.getLocVT() == MVT::f64 &&
1982 "Custom lowering for v2f64 args not available");
1984 CCValAssign &NextVA = ArgLocs[++i];
1986 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
1987 "We only handle register args!");
1989 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1990 TII.get(ARM::VMOVRRD), VA.getLocReg())
1991 .addReg(NextVA.getLocReg(), RegState::Define)
1993 RegArgs.push_back(VA.getLocReg());
1994 RegArgs.push_back(NextVA.getLocReg());
1996 assert(VA.isMemLoc());
1997 // Need to store on the stack.
1999 Addr.BaseType = Address::RegBase;
2000 Addr.Base.Reg = ARM::SP;
2001 Addr.Offset = VA.getLocMemOffset();
2003 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2004 assert(EmitRet && "Could not emit a store for argument!");
2011 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
2012 const Instruction *I, CallingConv::ID CC,
2013 unsigned &NumBytes, bool isVarArg) {
2014 // Issue CALLSEQ_END
2015 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
2016 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2017 TII.get(AdjStackUp))
2018 .addImm(NumBytes).addImm(0));
2020 // Now the return value.
2021 if (RetVT != MVT::isVoid) {
2022 SmallVector<CCValAssign, 16> RVLocs;
2023 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2024 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
2026 // Copy all of the result registers out of their specified physreg.
2027 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
2028 // For this move we copy into two registers and then move into the
2029 // double fp reg we want.
2030 EVT DestVT = RVLocs[0].getValVT();
2031 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
2032 unsigned ResultReg = createResultReg(DstRC);
2033 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2034 TII.get(ARM::VMOVDRR), ResultReg)
2035 .addReg(RVLocs[0].getLocReg())
2036 .addReg(RVLocs[1].getLocReg()));
2038 UsedRegs.push_back(RVLocs[0].getLocReg());
2039 UsedRegs.push_back(RVLocs[1].getLocReg());
2041 // Finally update the result.
2042 UpdateValueMap(I, ResultReg);
2044 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
2045 EVT CopyVT = RVLocs[0].getValVT();
2047 // Special handling for extended integers.
2048 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2051 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
2053 unsigned ResultReg = createResultReg(DstRC);
2054 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2055 ResultReg).addReg(RVLocs[0].getLocReg());
2056 UsedRegs.push_back(RVLocs[0].getLocReg());
2058 // Finally update the result.
2059 UpdateValueMap(I, ResultReg);
2066 bool ARMFastISel::SelectRet(const Instruction *I) {
2067 const ReturnInst *Ret = cast<ReturnInst>(I);
2068 const Function &F = *I->getParent()->getParent();
2070 if (!FuncInfo.CanLowerReturn)
2073 CallingConv::ID CC = F.getCallingConv();
2074 if (Ret->getNumOperands() > 0) {
2075 SmallVector<ISD::OutputArg, 4> Outs;
2076 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
2079 // Analyze operands of the call, assigning locations to each operand.
2080 SmallVector<CCValAssign, 16> ValLocs;
2081 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
2082 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2085 const Value *RV = Ret->getOperand(0);
2086 unsigned Reg = getRegForValue(RV);
2090 // Only handle a single return value for now.
2091 if (ValLocs.size() != 1)
2094 CCValAssign &VA = ValLocs[0];
2096 // Don't bother handling odd stuff for now.
2097 if (VA.getLocInfo() != CCValAssign::Full)
2099 // Only handle register returns for now.
2103 unsigned SrcReg = Reg + VA.getValNo();
2104 EVT RVVT = TLI.getValueType(RV->getType());
2105 EVT DestVT = VA.getValVT();
2106 // Special handling for extended integers.
2107 if (RVVT != DestVT) {
2108 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2111 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2113 // Perform extension if flagged as either zext or sext. Otherwise, do
2115 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2116 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2117 if (SrcReg == 0) return false;
2122 unsigned DstReg = VA.getLocReg();
2123 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2124 // Avoid a cross-class copy. This is very unlikely.
2125 if (!SrcRC->contains(DstReg))
2127 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2128 DstReg).addReg(SrcReg);
2130 // Mark the register as live out of the function.
2131 MRI.addLiveOut(VA.getLocReg());
2134 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
2135 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2140 unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2142 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2144 return isThumb2 ? ARM::tBL : ARM::BL;
2147 unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
2148 GlobalValue *GV = new GlobalVariable(Type::getInt32Ty(*Context), false,
2149 GlobalValue::ExternalLinkage, 0, Name);
2150 return ARMMaterializeGV(GV, TLI.getValueType(GV->getType()));
2153 // A quick function that will emit a call for a named libcall in F with the
2154 // vector of passed arguments for the Instruction in I. We can assume that we
2155 // can emit a call for any libcall we can produce. This is an abridged version
2156 // of the full call infrastructure since we won't need to worry about things
2157 // like computed function pointers or strange arguments at call sites.
2158 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
2160 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2161 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
2163 // Handle *simple* calls for now.
2164 Type *RetTy = I->getType();
2166 if (RetTy->isVoidTy())
2167 RetVT = MVT::isVoid;
2168 else if (!isTypeLegal(RetTy, RetVT))
2171 // Can't handle non-double multi-reg retvals.
2172 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
2173 SmallVector<CCValAssign, 16> RVLocs;
2174 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
2175 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
2176 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2180 // Set up the argument vectors.
2181 SmallVector<Value*, 8> Args;
2182 SmallVector<unsigned, 8> ArgRegs;
2183 SmallVector<MVT, 8> ArgVTs;
2184 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2185 Args.reserve(I->getNumOperands());
2186 ArgRegs.reserve(I->getNumOperands());
2187 ArgVTs.reserve(I->getNumOperands());
2188 ArgFlags.reserve(I->getNumOperands());
2189 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
2190 Value *Op = I->getOperand(i);
2191 unsigned Arg = getRegForValue(Op);
2192 if (Arg == 0) return false;
2194 Type *ArgTy = Op->getType();
2196 if (!isTypeLegal(ArgTy, ArgVT)) return false;
2198 ISD::ArgFlagsTy Flags;
2199 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2200 Flags.setOrigAlign(OriginalAlignment);
2203 ArgRegs.push_back(Arg);
2204 ArgVTs.push_back(ArgVT);
2205 ArgFlags.push_back(Flags);
2208 // Handle the arguments now that we've gotten them.
2209 SmallVector<unsigned, 4> RegArgs;
2211 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2212 RegArgs, CC, NumBytes, false))
2215 unsigned CalleeReg = 0;
2216 if (EnableARMLongCalls) {
2217 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2218 if (CalleeReg == 0) return false;
2222 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2223 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2224 DL, TII.get(CallOpc));
2225 // BL / BLX don't take a predicate, but tBL / tBLX do.
2227 AddDefaultPred(MIB);
2228 if (EnableARMLongCalls)
2229 MIB.addReg(CalleeReg);
2231 MIB.addExternalSymbol(TLI.getLibcallName(Call));
2233 // Add implicit physical register uses to the call.
2234 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2235 MIB.addReg(RegArgs[i], RegState::Implicit);
2237 // Add a register mask with the call-preserved registers.
2238 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2239 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2241 // Finish off the call including any return values.
2242 SmallVector<unsigned, 4> UsedRegs;
2243 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
2245 // Set all unused physreg defs as dead.
2246 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2251 bool ARMFastISel::SelectCall(const Instruction *I,
2252 const char *IntrMemName = 0) {
2253 const CallInst *CI = cast<CallInst>(I);
2254 const Value *Callee = CI->getCalledValue();
2256 // Can't handle inline asm.
2257 if (isa<InlineAsm>(Callee)) return false;
2259 // Check the calling convention.
2260 ImmutableCallSite CS(CI);
2261 CallingConv::ID CC = CS.getCallingConv();
2263 // TODO: Avoid some calling conventions?
2265 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2266 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
2267 bool isVarArg = FTy->isVarArg();
2269 // Handle *simple* calls for now.
2270 Type *RetTy = I->getType();
2272 if (RetTy->isVoidTy())
2273 RetVT = MVT::isVoid;
2274 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2275 RetVT != MVT::i8 && RetVT != MVT::i1)
2278 // Can't handle non-double multi-reg retvals.
2279 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2280 RetVT != MVT::i16 && RetVT != MVT::i32) {
2281 SmallVector<CCValAssign, 16> RVLocs;
2282 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2283 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
2284 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2288 // Set up the argument vectors.
2289 SmallVector<Value*, 8> Args;
2290 SmallVector<unsigned, 8> ArgRegs;
2291 SmallVector<MVT, 8> ArgVTs;
2292 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2293 unsigned arg_size = CS.arg_size();
2294 Args.reserve(arg_size);
2295 ArgRegs.reserve(arg_size);
2296 ArgVTs.reserve(arg_size);
2297 ArgFlags.reserve(arg_size);
2298 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2300 // If we're lowering a memory intrinsic instead of a regular call, skip the
2301 // last two arguments, which shouldn't be passed to the underlying function.
2302 if (IntrMemName && e-i <= 2)
2305 ISD::ArgFlagsTy Flags;
2306 unsigned AttrInd = i - CS.arg_begin() + 1;
2307 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2309 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2312 // FIXME: Only handle *easy* calls for now.
2313 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2314 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2315 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2316 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2319 Type *ArgTy = (*i)->getType();
2321 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2325 unsigned Arg = getRegForValue(*i);
2329 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2330 Flags.setOrigAlign(OriginalAlignment);
2333 ArgRegs.push_back(Arg);
2334 ArgVTs.push_back(ArgVT);
2335 ArgFlags.push_back(Flags);
2338 // Handle the arguments now that we've gotten them.
2339 SmallVector<unsigned, 4> RegArgs;
2341 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2342 RegArgs, CC, NumBytes, isVarArg))
2345 bool UseReg = false;
2346 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
2347 if (!GV || EnableARMLongCalls) UseReg = true;
2349 unsigned CalleeReg = 0;
2352 CalleeReg = getLibcallReg(IntrMemName);
2354 CalleeReg = getRegForValue(Callee);
2356 if (CalleeReg == 0) return false;
2360 unsigned CallOpc = ARMSelectCallOp(UseReg);
2361 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2362 DL, TII.get(CallOpc));
2364 // ARM calls don't take a predicate, but tBL / tBLX do.
2366 AddDefaultPred(MIB);
2368 MIB.addReg(CalleeReg);
2369 else if (!IntrMemName)
2370 MIB.addGlobalAddress(GV, 0, 0);
2372 MIB.addExternalSymbol(IntrMemName, 0);
2374 // Add implicit physical register uses to the call.
2375 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2376 MIB.addReg(RegArgs[i], RegState::Implicit);
2378 // Add a register mask with the call-preserved registers.
2379 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2380 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2382 // Finish off the call including any return values.
2383 SmallVector<unsigned, 4> UsedRegs;
2384 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2387 // Set all unused physreg defs as dead.
2388 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2393 bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
2397 bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
2399 // Make sure we don't bloat code by inlining very large memcpy's.
2400 if (!ARMIsMemCpySmall(Len))
2403 // We don't care about alignment here since we just emit integer accesses.
2417 RV = ARMEmitLoad(VT, ResultReg, Src);
2418 assert (RV == true && "Should be able to handle this load.");
2419 RV = ARMEmitStore(VT, ResultReg, Dest);
2420 assert (RV == true && "Should be able to handle this store.");
2423 unsigned Size = VT.getSizeInBits()/8;
2425 Dest.Offset += Size;
2432 bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2433 // FIXME: Handle more intrinsics.
2434 switch (I.getIntrinsicID()) {
2435 default: return false;
2436 case Intrinsic::frameaddress: {
2437 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2438 MFI->setFrameAddressIsTaken(true);
2441 const TargetRegisterClass *RC;
2443 LdrOpc = ARM::t2LDRi12;
2444 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2446 LdrOpc = ARM::LDRi12;
2447 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2450 const ARMBaseRegisterInfo *RegInfo =
2451 static_cast<const ARMBaseRegisterInfo*>(TM.getRegisterInfo());
2452 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2453 unsigned SrcReg = FramePtr;
2455 // Recursively load frame address
2461 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2463 DestReg = createResultReg(RC);
2464 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2465 TII.get(LdrOpc), DestReg)
2466 .addReg(SrcReg).addImm(0));
2469 UpdateValueMap(&I, SrcReg);
2472 case Intrinsic::memcpy:
2473 case Intrinsic::memmove: {
2474 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2475 // Don't handle volatile.
2476 if (MTI.isVolatile())
2479 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2480 // we would emit dead code because we don't currently handle memmoves.
2481 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2482 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
2483 // Small memcpy's are common enough that we want to do them without a call
2485 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
2486 if (ARMIsMemCpySmall(Len)) {
2488 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2489 !ARMComputeAddress(MTI.getRawSource(), Src))
2491 if (ARMTryEmitSmallMemCpy(Dest, Src, Len))
2496 if (!MTI.getLength()->getType()->isIntegerTy(32))
2499 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2502 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2503 return SelectCall(&I, IntrMemName);
2505 case Intrinsic::memset: {
2506 const MemSetInst &MSI = cast<MemSetInst>(I);
2507 // Don't handle volatile.
2508 if (MSI.isVolatile())
2511 if (!MSI.getLength()->getType()->isIntegerTy(32))
2514 if (MSI.getDestAddressSpace() > 255)
2517 return SelectCall(&I, "memset");
2519 case Intrinsic::trap: {
2520 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::TRAP));
2526 bool ARMFastISel::SelectTrunc(const Instruction *I) {
2527 // The high bits for a type smaller than the register size are assumed to be
2529 Value *Op = I->getOperand(0);
2532 SrcVT = TLI.getValueType(Op->getType(), true);
2533 DestVT = TLI.getValueType(I->getType(), true);
2535 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2537 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2540 unsigned SrcReg = getRegForValue(Op);
2541 if (!SrcReg) return false;
2543 // Because the high bits are undefined, a truncate doesn't generate
2545 UpdateValueMap(I, SrcReg);
2549 unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2551 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
2555 bool isBoolZext = false;
2556 if (!SrcVT.isSimple()) return 0;
2557 switch (SrcVT.getSimpleVT().SimpleTy) {
2560 if (!Subtarget->hasV6Ops()) return 0;
2562 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
2564 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
2567 if (!Subtarget->hasV6Ops()) return 0;
2569 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
2571 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
2575 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
2582 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
2583 MachineInstrBuilder MIB;
2584 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
2590 AddOptionalDefs(MIB);
2594 bool ARMFastISel::SelectIntExt(const Instruction *I) {
2595 // On ARM, in general, integer casts don't involve legal types; this code
2596 // handles promotable integers.
2597 Type *DestTy = I->getType();
2598 Value *Src = I->getOperand(0);
2599 Type *SrcTy = Src->getType();
2602 SrcVT = TLI.getValueType(SrcTy, true);
2603 DestVT = TLI.getValueType(DestTy, true);
2605 bool isZExt = isa<ZExtInst>(I);
2606 unsigned SrcReg = getRegForValue(Src);
2607 if (!SrcReg) return false;
2609 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2610 if (ResultReg == 0) return false;
2611 UpdateValueMap(I, ResultReg);
2615 bool ARMFastISel::SelectShift(const Instruction *I,
2616 ARM_AM::ShiftOpc ShiftTy) {
2617 // We handle thumb2 mode by target independent selector
2618 // or SelectionDAG ISel.
2622 // Only handle i32 now.
2623 EVT DestVT = TLI.getValueType(I->getType(), true);
2624 if (DestVT != MVT::i32)
2627 unsigned Opc = ARM::MOVsr;
2629 Value *Src2Value = I->getOperand(1);
2630 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2631 ShiftImm = CI->getZExtValue();
2633 // Fall back to selection DAG isel if the shift amount
2634 // is zero or greater than the width of the value type.
2635 if (ShiftImm == 0 || ShiftImm >=32)
2641 Value *Src1Value = I->getOperand(0);
2642 unsigned Reg1 = getRegForValue(Src1Value);
2643 if (Reg1 == 0) return false;
2646 if (Opc == ARM::MOVsr) {
2647 Reg2 = getRegForValue(Src2Value);
2648 if (Reg2 == 0) return false;
2651 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
2652 if(ResultReg == 0) return false;
2654 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2655 TII.get(Opc), ResultReg)
2658 if (Opc == ARM::MOVsi)
2659 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2660 else if (Opc == ARM::MOVsr) {
2662 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2665 AddOptionalDefs(MIB);
2666 UpdateValueMap(I, ResultReg);
2670 // TODO: SoftFP support.
2671 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
2673 switch (I->getOpcode()) {
2674 case Instruction::Load:
2675 return SelectLoad(I);
2676 case Instruction::Store:
2677 return SelectStore(I);
2678 case Instruction::Br:
2679 return SelectBranch(I);
2680 case Instruction::IndirectBr:
2681 return SelectIndirectBr(I);
2682 case Instruction::ICmp:
2683 case Instruction::FCmp:
2684 return SelectCmp(I);
2685 case Instruction::FPExt:
2686 return SelectFPExt(I);
2687 case Instruction::FPTrunc:
2688 return SelectFPTrunc(I);
2689 case Instruction::SIToFP:
2690 return SelectIToFP(I, /*isSigned*/ true);
2691 case Instruction::UIToFP:
2692 return SelectIToFP(I, /*isSigned*/ false);
2693 case Instruction::FPToSI:
2694 return SelectFPToI(I, /*isSigned*/ true);
2695 case Instruction::FPToUI:
2696 return SelectFPToI(I, /*isSigned*/ false);
2697 case Instruction::Add:
2698 return SelectBinaryIntOp(I, ISD::ADD);
2699 case Instruction::Or:
2700 return SelectBinaryIntOp(I, ISD::OR);
2701 case Instruction::Sub:
2702 return SelectBinaryIntOp(I, ISD::SUB);
2703 case Instruction::FAdd:
2704 return SelectBinaryFPOp(I, ISD::FADD);
2705 case Instruction::FSub:
2706 return SelectBinaryFPOp(I, ISD::FSUB);
2707 case Instruction::FMul:
2708 return SelectBinaryFPOp(I, ISD::FMUL);
2709 case Instruction::SDiv:
2710 return SelectDiv(I, /*isSigned*/ true);
2711 case Instruction::UDiv:
2712 return SelectDiv(I, /*isSigned*/ false);
2713 case Instruction::SRem:
2714 return SelectRem(I, /*isSigned*/ true);
2715 case Instruction::URem:
2716 return SelectRem(I, /*isSigned*/ false);
2717 case Instruction::Call:
2718 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2719 return SelectIntrinsicCall(*II);
2720 return SelectCall(I);
2721 case Instruction::Select:
2722 return SelectSelect(I);
2723 case Instruction::Ret:
2724 return SelectRet(I);
2725 case Instruction::Trunc:
2726 return SelectTrunc(I);
2727 case Instruction::ZExt:
2728 case Instruction::SExt:
2729 return SelectIntExt(I);
2730 case Instruction::Shl:
2731 return SelectShift(I, ARM_AM::lsl);
2732 case Instruction::LShr:
2733 return SelectShift(I, ARM_AM::lsr);
2734 case Instruction::AShr:
2735 return SelectShift(I, ARM_AM::asr);
2741 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2742 /// vreg is being provided by the specified load instruction. If possible,
2743 /// try to fold the load as an operand to the instruction, returning true if
2745 bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2746 const LoadInst *LI) {
2747 // Verify we have a legal type before going any further.
2749 if (!isLoadTypeLegal(LI->getType(), VT))
2752 // Combine load followed by zero- or sign-extend.
2753 // ldrb r1, [r0] ldrb r1, [r0]
2755 // mov r3, r2 mov r3, r1
2757 switch(MI->getOpcode()) {
2758 default: return false;
2776 // See if we can handle this address.
2778 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2780 unsigned ResultReg = MI->getOperand(0).getReg();
2781 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
2783 MI->eraseFromParent();
2788 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
2789 const TargetLibraryInfo *libInfo) {
2790 // Completely untested on non-iOS.
2791 const TargetMachine &TM = funcInfo.MF->getTarget();
2793 // Darwin and thumb1 only for now.
2794 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
2795 if (Subtarget->isTargetIOS() && !Subtarget->isThumb1Only())
2796 return new ARMFastISel(funcInfo, libInfo);