blob: 81a93b1ed3ba7fae1cf4dfb46d8d8244572d0dfd [file] [log] [blame]
Eric Christopherab695882010-07-21 22:26:11 +00001//===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ARM.h"
Eric Christopher456144e2010-08-19 00:37:05 +000017#include "ARMBaseInstrInfo.h"
Eric Christopherd10cd7b2010-09-10 23:18:12 +000018#include "ARMCallingConv.h"
Eric Christopherab695882010-07-21 22:26:11 +000019#include "ARMRegisterInfo.h"
20#include "ARMTargetMachine.h"
21#include "ARMSubtarget.h"
Eric Christopherc9932f62010-10-01 23:24:42 +000022#include "ARMConstantPoolValue.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Eric Christopherab695882010-07-21 22:26:11 +000024#include "llvm/CallingConv.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/GlobalVariable.h"
27#include "llvm/Instructions.h"
28#include "llvm/IntrinsicInst.h"
Eric Christopherbb3e5da2010-09-14 23:03:37 +000029#include "llvm/Module.h"
Jay Foad562b84b2011-04-11 09:35:34 +000030#include "llvm/Operator.h"
Eric Christopherab695882010-07-21 22:26:11 +000031#include "llvm/CodeGen/Analysis.h"
32#include "llvm/CodeGen/FastISel.h"
33#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
35#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000036#include "llvm/CodeGen/MachineConstantPool.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000038#include "llvm/CodeGen/MachineMemOperand.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000040#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000041#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000042#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000043#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000045#include "llvm/Target/TargetData.h"
46#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetLowering.h"
48#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000049#include "llvm/Target/TargetOptions.h"
50using namespace llvm;
51
Eric Christopher038fea52010-08-17 00:46:57 +000052static cl::opt<bool>
Eric Christopher6e5367d2010-10-18 22:53:53 +000053DisableARMFastISel("disable-arm-fast-isel",
54 cl::desc("Turn off experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000055 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000056
Eric Christopher836c6242010-12-15 23:47:29 +000057extern cl::opt<bool> EnableARMLongCalls;
58
Eric Christopherab695882010-07-21 22:26:11 +000059namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000060
Eric Christopher0d581222010-11-19 22:30:02 +000061 // All possible address modes, plus some.
62 typedef struct Address {
63 enum {
64 RegBase,
65 FrameIndexBase
66 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000067
Eric Christopher0d581222010-11-19 22:30:02 +000068 union {
69 unsigned Reg;
70 int FI;
71 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000072
Eric Christopher0d581222010-11-19 22:30:02 +000073 int Offset;
Eric Christopher827656d2010-11-20 22:38:27 +000074
Eric Christopher0d581222010-11-19 22:30:02 +000075 // Innocuous defaults for our address.
76 Address()
Jim Grosbach0c720762011-05-16 22:24:07 +000077 : BaseType(RegBase), Offset(0) {
Eric Christopher0d581222010-11-19 22:30:02 +000078 Base.Reg = 0;
79 }
80 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000081
82class ARMFastISel : public FastISel {
83
84 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
85 /// make the right decision when generating code for different targets.
86 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000087 const TargetMachine &TM;
88 const TargetInstrInfo &TII;
89 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000090 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000091
Eric Christopher8cf6c602010-09-29 22:24:45 +000092 // Convenience variables to avoid some queries.
Chad Rosier66dc8ca2011-11-08 21:12:00 +000093 bool isThumb2;
Eric Christopher8cf6c602010-09-29 22:24:45 +000094 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000095
Eric Christopherab695882010-07-21 22:26:11 +000096 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000097 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000098 : FastISel(funcInfo),
99 TM(funcInfo.MF->getTarget()),
100 TII(*TM.getInstrInfo()),
101 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +0000102 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +0000103 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000104 isThumb2 = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000105 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000106 }
107
Eric Christophercb592292010-08-20 00:20:31 +0000108 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +0000109 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC);
111 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill);
114 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 unsigned Op1, bool Op1IsKill);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000118 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill,
121 unsigned Op1, bool Op1IsKill,
122 unsigned Op2, bool Op2IsKill);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000123 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
124 const TargetRegisterClass *RC,
125 unsigned Op0, bool Op0IsKill,
126 uint64_t Imm);
127 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
128 const TargetRegisterClass *RC,
129 unsigned Op0, bool Op0IsKill,
130 const ConstantFP *FPImm);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000131 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
132 const TargetRegisterClass *RC,
133 unsigned Op0, bool Op0IsKill,
134 unsigned Op1, bool Op1IsKill,
135 uint64_t Imm);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000136 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
137 const TargetRegisterClass *RC,
138 uint64_t Imm);
Eric Christopherd94bc542011-04-29 22:07:50 +0000139 virtual unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
140 const TargetRegisterClass *RC,
141 uint64_t Imm1, uint64_t Imm2);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000142
Eric Christopher0fe7d542010-08-17 01:25:29 +0000143 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
144 unsigned Op0, bool Op0IsKill,
145 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000146
Eric Christophercb592292010-08-20 00:20:31 +0000147 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000148 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000149 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000150 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Chad Rosierb29b9502011-11-13 02:23:59 +0000151 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
152 const LoadInst *LI);
Eric Christopherab695882010-07-21 22:26:11 +0000153
154 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000155
Eric Christopher83007122010-08-23 21:44:12 +0000156 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000157 private:
Eric Christopher17787722010-10-21 21:47:51 +0000158 bool SelectLoad(const Instruction *I);
159 bool SelectStore(const Instruction *I);
160 bool SelectBranch(const Instruction *I);
161 bool SelectCmp(const Instruction *I);
162 bool SelectFPExt(const Instruction *I);
163 bool SelectFPTrunc(const Instruction *I);
164 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
165 bool SelectSIToFP(const Instruction *I);
166 bool SelectFPToSI(const Instruction *I);
167 bool SelectSDiv(const Instruction *I);
168 bool SelectSRem(const Instruction *I);
Chad Rosier11add262011-11-11 23:31:03 +0000169 bool SelectCall(const Instruction *I, const char *IntrMemName);
170 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher17787722010-10-21 21:47:51 +0000171 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000172 bool SelectRet(const Instruction *I);
Chad Rosier0d7b2312011-11-02 00:18:48 +0000173 bool SelectTrunc(const Instruction *I);
174 bool SelectIntExt(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000175
Eric Christopher83007122010-08-23 21:44:12 +0000176 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000177 private:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000178 bool isTypeLegal(Type *Ty, MVT &VT);
179 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosiere07cd5e2011-11-02 18:08:25 +0000180 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
181 bool isZExt);
Chad Rosierb29b9502011-11-13 02:23:59 +0000182 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr, bool isZExt,
183 bool allocReg);
184
Eric Christopher0d581222010-11-19 22:30:02 +0000185 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
186 bool ARMComputeAddress(const Value *Obj, Address &Addr);
Chad Rosierb29b9502011-11-13 02:23:59 +0000187 void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3);
Chad Rosier87633022011-11-02 17:20:24 +0000188 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000189 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000190 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000191 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000192 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000193 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000194 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000195
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000196 // Call handling routines.
197 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000198 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
199 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000200 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000201 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000202 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000203 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000204 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
205 SmallVectorImpl<unsigned> &RegArgs,
206 CallingConv::ID CC,
207 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000208 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000209 const Instruction *I, CallingConv::ID CC,
210 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000211 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000212
213 // OptionalDef handling routines.
214 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000215 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000216 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
217 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000218 void AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000219 const MachineInstrBuilder &MIB,
Chad Rosierb29b9502011-11-13 02:23:59 +0000220 unsigned Flags, bool useAM3);
Eric Christopher456144e2010-08-19 00:37:05 +0000221};
Eric Christopherab695882010-07-21 22:26:11 +0000222
223} // end anonymous namespace
224
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000225#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000226
Eric Christopher456144e2010-08-19 00:37:05 +0000227// 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.
230bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Chenge837dea2011-06-28 19:10:37 +0000231 const MCInstrDesc &MCID = MI->getDesc();
232 if (!MCID.hasOptionalDef())
Eric Christopher456144e2010-08-19 00:37:05 +0000233 return false;
234
235 // Look to see if our OptionalDef is defining CPSR or CCR.
236 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
237 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000238 if (!MO.isReg() || !MO.isDef()) continue;
239 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000240 *CPSR = true;
241 }
242 return true;
243}
244
Eric Christopheraf3dce52011-03-12 01:09:29 +0000245bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000246 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher299bbb22011-04-29 00:03:10 +0000247
Eric Christopheraf3dce52011-03-12 01:09:29 +0000248 // If we're a thumb2 or not NEON function we were handled via isPredicable.
Evan Chenge837dea2011-06-28 19:10:37 +0000249 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopheraf3dce52011-03-12 01:09:29 +0000250 AFI->isThumb2Function())
251 return false;
Eric Christopher299bbb22011-04-29 00:03:10 +0000252
Evan Chenge837dea2011-06-28 19:10:37 +0000253 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
254 if (MCID.OpInfo[i].isPredicate())
Eric Christopheraf3dce52011-03-12 01:09:29 +0000255 return true;
Eric Christopher299bbb22011-04-29 00:03:10 +0000256
Eric Christopheraf3dce52011-03-12 01:09:29 +0000257 return false;
258}
259
Eric Christopher456144e2010-08-19 00:37:05 +0000260// If the machine is predicable go ahead and add the predicate operands, if
261// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000262// TODO: If we want to support thumb1 then we'll need to deal with optional
263// CPSR defs that need to be added before the remaining operands. See s_cc_out
264// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000265const MachineInstrBuilder &
266ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
267 MachineInstr *MI = &*MIB;
268
Eric Christopheraf3dce52011-03-12 01:09:29 +0000269 // Do we use a predicate? or...
270 // Are we NEON in ARM mode and have a predicate operand? If so, I know
271 // we're not predicable but add it anyways.
272 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000273 AddDefaultPred(MIB);
Eric Christopher299bbb22011-04-29 00:03:10 +0000274
Eric Christopher456144e2010-08-19 00:37:05 +0000275 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
276 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000277 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000278 if (DefinesOptionalPredicate(MI, &CPSR)) {
279 if (CPSR)
280 AddDefaultT1CC(MIB);
281 else
282 AddDefaultCC(MIB);
283 }
284 return MIB;
285}
286
Eric Christopher0fe7d542010-08-17 01:25:29 +0000287unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
288 const TargetRegisterClass* RC) {
289 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000290 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000291
Eric Christopher456144e2010-08-19 00:37:05 +0000292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000293 return ResultReg;
294}
295
296unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
297 const TargetRegisterClass *RC,
298 unsigned Op0, bool Op0IsKill) {
299 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000300 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000301
302 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000303 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000304 .addReg(Op0, Op0IsKill * RegState::Kill));
305 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000306 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000307 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 TII.get(TargetOpcode::COPY), ResultReg)
310 .addReg(II.ImplicitDefs[0]));
311 }
312 return ResultReg;
313}
314
315unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
316 const TargetRegisterClass *RC,
317 unsigned Op0, bool Op0IsKill,
318 unsigned Op1, bool Op1IsKill) {
319 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000320 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000321
322 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000323 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000324 .addReg(Op0, Op0IsKill * RegState::Kill)
325 .addReg(Op1, Op1IsKill * RegState::Kill));
326 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000327 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000328 .addReg(Op0, Op0IsKill * RegState::Kill)
329 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000331 TII.get(TargetOpcode::COPY), ResultReg)
332 .addReg(II.ImplicitDefs[0]));
333 }
334 return ResultReg;
335}
336
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000337unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
338 const TargetRegisterClass *RC,
339 unsigned Op0, bool Op0IsKill,
340 unsigned Op1, bool Op1IsKill,
341 unsigned Op2, bool Op2IsKill) {
342 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000343 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000344
345 if (II.getNumDefs() >= 1)
346 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
347 .addReg(Op0, Op0IsKill * RegState::Kill)
348 .addReg(Op1, Op1IsKill * RegState::Kill)
349 .addReg(Op2, Op2IsKill * RegState::Kill));
350 else {
351 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
352 .addReg(Op0, Op0IsKill * RegState::Kill)
353 .addReg(Op1, Op1IsKill * RegState::Kill)
354 .addReg(Op2, Op2IsKill * RegState::Kill));
355 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
356 TII.get(TargetOpcode::COPY), ResultReg)
357 .addReg(II.ImplicitDefs[0]));
358 }
359 return ResultReg;
360}
361
Eric Christopher0fe7d542010-08-17 01:25:29 +0000362unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
363 const TargetRegisterClass *RC,
364 unsigned Op0, bool Op0IsKill,
365 uint64_t Imm) {
366 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000367 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000368
369 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000370 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000371 .addReg(Op0, Op0IsKill * RegState::Kill)
372 .addImm(Imm));
373 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000374 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000375 .addReg(Op0, Op0IsKill * RegState::Kill)
376 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000378 TII.get(TargetOpcode::COPY), ResultReg)
379 .addReg(II.ImplicitDefs[0]));
380 }
381 return ResultReg;
382}
383
384unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
385 const TargetRegisterClass *RC,
386 unsigned Op0, bool Op0IsKill,
387 const ConstantFP *FPImm) {
388 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000389 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000390
391 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000392 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000393 .addReg(Op0, Op0IsKill * RegState::Kill)
394 .addFPImm(FPImm));
395 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000396 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000397 .addReg(Op0, Op0IsKill * RegState::Kill)
398 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000399 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000400 TII.get(TargetOpcode::COPY), ResultReg)
401 .addReg(II.ImplicitDefs[0]));
402 }
403 return ResultReg;
404}
405
406unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
407 const TargetRegisterClass *RC,
408 unsigned Op0, bool Op0IsKill,
409 unsigned Op1, bool Op1IsKill,
410 uint64_t Imm) {
411 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000412 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000413
414 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000416 .addReg(Op0, Op0IsKill * RegState::Kill)
417 .addReg(Op1, Op1IsKill * RegState::Kill)
418 .addImm(Imm));
419 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000420 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000421 .addReg(Op0, Op0IsKill * RegState::Kill)
422 .addReg(Op1, Op1IsKill * RegState::Kill)
423 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000424 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000425 TII.get(TargetOpcode::COPY), ResultReg)
426 .addReg(II.ImplicitDefs[0]));
427 }
428 return ResultReg;
429}
430
431unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
432 const TargetRegisterClass *RC,
433 uint64_t Imm) {
434 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000435 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000436
Eric Christopher0fe7d542010-08-17 01:25:29 +0000437 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000438 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000439 .addImm(Imm));
440 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000441 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000442 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000443 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000444 TII.get(TargetOpcode::COPY), ResultReg)
445 .addReg(II.ImplicitDefs[0]));
446 }
447 return ResultReg;
448}
449
Eric Christopherd94bc542011-04-29 22:07:50 +0000450unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
451 const TargetRegisterClass *RC,
452 uint64_t Imm1, uint64_t Imm2) {
453 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000454 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher471e4222011-06-08 23:55:35 +0000455
Eric Christopherd94bc542011-04-29 22:07:50 +0000456 if (II.getNumDefs() >= 1)
457 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
458 .addImm(Imm1).addImm(Imm2));
459 else {
460 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
461 .addImm(Imm1).addImm(Imm2));
Eric Christopher471e4222011-06-08 23:55:35 +0000462 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherd94bc542011-04-29 22:07:50 +0000463 TII.get(TargetOpcode::COPY),
464 ResultReg)
465 .addReg(II.ImplicitDefs[0]));
466 }
467 return ResultReg;
468}
469
Eric Christopher0fe7d542010-08-17 01:25:29 +0000470unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
471 unsigned Op0, bool Op0IsKill,
472 uint32_t Idx) {
473 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
474 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
475 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000476 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000477 DL, TII.get(TargetOpcode::COPY), ResultReg)
478 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
479 return ResultReg;
480}
481
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000482// TODO: Don't worry about 64-bit now, but when this is fixed remove the
483// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000484unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000485 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000486
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000487 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
488 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
489 TII.get(ARM::VMOVRS), MoveReg)
490 .addReg(SrcReg));
491 return MoveReg;
492}
493
494unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000495 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000496
Eric Christopheraa3ace12010-09-09 20:49:25 +0000497 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
498 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000499 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000500 .addReg(SrcReg));
501 return MoveReg;
502}
503
Eric Christopher9ed58df2010-09-09 00:19:41 +0000504// 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.
507unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
508 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000509 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000510
Eric Christopher9ed58df2010-09-09 00:19:41 +0000511 // 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)) {
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000514 int Imm;
515 unsigned Opc;
516 if (is64bit) {
517 Imm = ARM_AM::getFP64Imm(Val);
518 Opc = ARM::FCONSTD;
519 } else {
520 Imm = ARM_AM::getFP32Imm(Val);
521 Opc = ARM::FCONSTS;
522 }
Eric Christopher9ed58df2010-09-09 00:19:41 +0000523 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
524 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
525 DestReg)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000526 .addImm(Imm));
Eric Christopher9ed58df2010-09-09 00:19:41 +0000527 return DestReg;
528 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000529
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000530 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000531 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000532
Eric Christopher238bb162010-09-09 23:50:00 +0000533 // MachineConstantPool wants an explicit alignment.
534 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
535 if (Align == 0) {
536 // TODO: Figure out if this is correct.
537 Align = TD.getTypeAllocSize(CFP->getType());
538 }
539 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
540 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
541 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000542
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000543 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000544 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
545 DestReg)
546 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000547 .addReg(0));
548 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000549}
550
Eric Christopher744c7c82010-09-28 22:47:54 +0000551unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000552
Chad Rosier44e89572011-11-04 22:29:00 +0000553 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
554 return false;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000555
556 // If we can do this in a single instruction without a constant pool entry
557 // do so now.
558 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiera4e07272011-11-04 23:09:49 +0000559 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000560 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier4e89d972011-11-11 00:36:21 +0000561 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000562 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier44e89572011-11-04 22:29:00 +0000563 TII.get(Opc), ImmReg)
Chad Rosier42536af2011-11-05 20:16:15 +0000564 .addImm(CI->getZExtValue()));
Chad Rosier44e89572011-11-04 22:29:00 +0000565 return ImmReg;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000566 }
567
Chad Rosier4e89d972011-11-11 00:36:21 +0000568 // Use MVN to emit negative constants.
569 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
570 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosier1c47de82011-11-11 06:27:41 +0000571 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier4e89d972011-11-11 00:36:21 +0000572 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier1c47de82011-11-11 06:27:41 +0000573 if (UseImm) {
Chad Rosier4e89d972011-11-11 00:36:21 +0000574 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)
578 .addImm(Imm));
579 return ImmReg;
580 }
581 }
582
583 // Load from constant pool. For now 32-bit only.
Chad Rosier44e89572011-11-04 22:29:00 +0000584 if (VT != MVT::i32)
585 return false;
586
587 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
588
Eric Christopher56d2b722010-09-02 23:43:26 +0000589 // MachineConstantPool wants an explicit alignment.
590 unsigned Align = TD.getPrefTypeAlignment(C->getType());
591 if (Align == 0) {
592 // TODO: Figure out if this is correct.
593 Align = TD.getTypeAllocSize(C->getType());
594 }
595 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000596
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000597 if (isThumb2)
Eric Christopher56d2b722010-09-02 23:43:26 +0000598 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000599 TII.get(ARM::t2LDRpci), DestReg)
600 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000601 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000602 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000603 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000604 TII.get(ARM::LDRcp), DestReg)
605 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000606 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000607
Eric Christopher56d2b722010-09-02 23:43:26 +0000608 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000609}
610
Eric Christopherc9932f62010-10-01 23:24:42 +0000611unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000612 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000613 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000614
Eric Christopher890dbbe2010-10-02 00:32:44 +0000615 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000616
Eric Christopher890dbbe2010-10-02 00:32:44 +0000617 // TODO: Need more magic for ARM PIC.
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000618 if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000619
Eric Christopher890dbbe2010-10-02 00:32:44 +0000620 // MachineConstantPool wants an explicit alignment.
621 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
622 if (Align == 0) {
623 // TODO: Figure out if this is correct.
624 Align = TD.getTypeAllocSize(GV->getType());
625 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000626
Eric Christopher890dbbe2010-10-02 00:32:44 +0000627 // Grab index.
628 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000629 unsigned Id = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +0000630 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
631 ARMCP::CPValue,
632 PCAdj);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000633 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000634
Eric Christopher890dbbe2010-10-02 00:32:44 +0000635 // Load value.
636 MachineInstrBuilder MIB;
637 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000638 if (isThumb2) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000639 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
640 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
641 .addConstantPoolIndex(Idx);
642 if (RelocM == Reloc::PIC_)
643 MIB.addImm(Id);
644 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000645 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000646 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
647 DestReg)
648 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000649 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000650 }
651 AddOptionalDefs(MIB);
Eli Friedmand6412c92011-06-03 01:13:19 +0000652
653 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
654 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000655 if (isThumb2)
Jim Grosbachb04546f2011-09-13 20:30:37 +0000656 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
657 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedmand6412c92011-06-03 01:13:19 +0000658 .addReg(DestReg)
659 .addImm(0);
660 else
661 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
662 NewDestReg)
663 .addReg(DestReg)
664 .addImm(0);
665 DestReg = NewDestReg;
666 AddOptionalDefs(MIB);
667 }
668
Eric Christopher890dbbe2010-10-02 00:32:44 +0000669 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000670}
671
Eric Christopher9ed58df2010-09-09 00:19:41 +0000672unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
673 EVT VT = TLI.getValueType(C->getType(), true);
674
675 // Only handle simple types.
676 if (!VT.isSimple()) return 0;
677
678 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
679 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000680 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
681 return ARMMaterializeGV(GV, VT);
682 else if (isa<ConstantInt>(C))
683 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000684
Eric Christopherc9932f62010-10-01 23:24:42 +0000685 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000686}
687
Eric Christopherf9764fa2010-09-30 20:49:44 +0000688unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
689 // Don't handle dynamic allocas.
690 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000691
Duncan Sands1440e8b2010-11-03 11:35:31 +0000692 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000693 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000694
Eric Christopherf9764fa2010-09-30 20:49:44 +0000695 DenseMap<const AllocaInst*, int>::iterator SI =
696 FuncInfo.StaticAllocaMap.find(AI);
697
698 // This will get lowered later into the correct offsets and registers
699 // via rewriteXFrameIndex.
700 if (SI != FuncInfo.StaticAllocaMap.end()) {
701 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
702 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000703 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Eric Christopherf9764fa2010-09-30 20:49:44 +0000704 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
705 TII.get(Opc), ResultReg)
706 .addFrameIndex(SI->second)
707 .addImm(0));
708 return ResultReg;
709 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000710
Eric Christopherf9764fa2010-09-30 20:49:44 +0000711 return 0;
712}
713
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000714bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000715 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000716
Eric Christopherb1cc8482010-08-25 07:23:49 +0000717 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000718 if (evt == MVT::Other || !evt.isSimple()) return false;
719 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000720
Eric Christopherdc908042010-08-31 01:28:42 +0000721 // Handle all legal types, i.e. a register that will directly hold this
722 // value.
723 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000724}
725
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000726bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000727 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000728
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000729 // If this is a type than can be sign or zero-extended to a basic operation
730 // go ahead and accept it now.
Chad Rosierb29b9502011-11-13 02:23:59 +0000731 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000732 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000733
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000734 return false;
735}
736
Eric Christopher88de86b2010-11-19 22:36:41 +0000737// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000738bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000739 // Some boilerplate from the X86 FastISel.
740 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000741 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000742 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000743 // Don't walk into other basic blocks unless the object is an alloca from
744 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000745 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
746 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
747 Opcode = I->getOpcode();
748 U = I;
749 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000750 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000751 Opcode = C->getOpcode();
752 U = C;
753 }
754
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000755 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000756 if (Ty->getAddressSpace() > 255)
757 // Fast instruction selection doesn't support the special
758 // address spaces.
759 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000760
Eric Christopher83007122010-08-23 21:44:12 +0000761 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000762 default:
Eric Christopher83007122010-08-23 21:44:12 +0000763 break;
Eric Christopher55324332010-10-12 00:43:21 +0000764 case Instruction::BitCast: {
765 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000766 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000767 }
768 case Instruction::IntToPtr: {
769 // Look past no-op inttoptrs.
770 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000771 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000772 break;
773 }
774 case Instruction::PtrToInt: {
775 // Look past no-op ptrtoints.
776 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000777 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000778 break;
779 }
Eric Christophereae84392010-10-14 09:29:41 +0000780 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000781 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000782 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000783
Eric Christophereae84392010-10-14 09:29:41 +0000784 // Iterate through the GEP folding the constants into offsets where
785 // we can.
786 gep_type_iterator GTI = gep_type_begin(U);
787 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
788 i != e; ++i, ++GTI) {
789 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000790 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Eric Christophereae84392010-10-14 09:29:41 +0000791 const StructLayout *SL = TD.getStructLayout(STy);
792 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
793 TmpOffset += SL->getElementOffset(Idx);
794 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000795 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000796 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000797 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
798 // Constant-offset addressing.
799 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000800 break;
801 }
802 if (isa<AddOperator>(Op) &&
803 (!isa<Instruction>(Op) ||
804 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
805 == FuncInfo.MBB) &&
806 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
Eric Christopher299bbb22011-04-29 00:03:10 +0000807 // An add (in the same block) with a constant operand. Fold the
Eric Christopher7244d7c2011-03-22 19:39:17 +0000808 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000809 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000810 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000811 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000812 // Iterate on the other operand.
813 Op = cast<AddOperator>(Op)->getOperand(0);
814 continue;
Eric Christopher299bbb22011-04-29 00:03:10 +0000815 }
Eric Christopher7244d7c2011-03-22 19:39:17 +0000816 // Unsupported
817 goto unsupported_gep;
818 }
Eric Christophereae84392010-10-14 09:29:41 +0000819 }
820 }
Eric Christopher2896df82010-10-15 18:02:07 +0000821
822 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000823 Addr.Offset = TmpOffset;
824 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000825
826 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000827 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000828
Eric Christophereae84392010-10-14 09:29:41 +0000829 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000830 break;
831 }
Eric Christopher83007122010-08-23 21:44:12 +0000832 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000833 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000834 DenseMap<const AllocaInst*, int>::iterator SI =
835 FuncInfo.StaticAllocaMap.find(AI);
836 if (SI != FuncInfo.StaticAllocaMap.end()) {
837 Addr.BaseType = Address::FrameIndexBase;
838 Addr.Base.FI = SI->second;
839 return true;
840 }
841 break;
Eric Christopher83007122010-08-23 21:44:12 +0000842 }
843 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000844
Eric Christophera9c57512010-10-13 21:41:51 +0000845 // Materialize the global variable's address into a reg which can
846 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000847 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000848 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
849 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000850
Eric Christopher0d581222010-11-19 22:30:02 +0000851 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000852 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000853 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000854
Eric Christophercb0b04b2010-08-24 00:07:24 +0000855 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000856 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
857 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000858}
859
Chad Rosierb29b9502011-11-13 02:23:59 +0000860void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000861
Eric Christopher212ae932010-10-21 19:40:30 +0000862 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000863
Eric Christopher212ae932010-10-21 19:40:30 +0000864 bool needsLowering = false;
865 switch (VT.getSimpleVT().SimpleTy) {
866 default:
867 assert(false && "Unhandled load/store type!");
Chad Rosier73463472011-11-09 21:30:12 +0000868 break;
Eric Christopher212ae932010-10-21 19:40:30 +0000869 case MVT::i1:
870 case MVT::i8:
Chad Rosierb29b9502011-11-13 02:23:59 +0000871 case MVT::i16:
Eric Christopher212ae932010-10-21 19:40:30 +0000872 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +0000873 if (!useAM3) {
Chad Rosierb29b9502011-11-13 02:23:59 +0000874 // Integer loads/stores handle 12-bit offsets.
875 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosier57b29972011-11-14 20:22:27 +0000876 // Handle negative offsets.
877 if (isThumb2)
878 needsLowering = !(needsLowering && Subtarget->hasV6T2Ops() &&
879 Addr.Offset < 0 && Addr.Offset > -256);
880 } else {
Chad Rosier5be833d2011-11-13 04:25:02 +0000881 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000882 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosier57b29972011-11-14 20:22:27 +0000883 }
Eric Christopher212ae932010-10-21 19:40:30 +0000884 break;
885 case MVT::f32:
886 case MVT::f64:
887 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000888 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000889 break;
890 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000891
Eric Christopher827656d2010-11-20 22:38:27 +0000892 // If this is a stack pointer and the offset needs to be simplified then
893 // put the alloca address into a register, set the base type back to
894 // register and continue. This should almost never happen.
895 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000896 TargetRegisterClass *RC = isThumb2 ? ARM::tGPRRegisterClass :
Eric Christopher827656d2010-11-20 22:38:27 +0000897 ARM::GPRRegisterClass;
898 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000899 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Eric Christopher827656d2010-11-20 22:38:27 +0000900 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
901 TII.get(Opc), ResultReg)
902 .addFrameIndex(Addr.Base.FI)
903 .addImm(0));
904 Addr.Base.Reg = ResultReg;
905 Addr.BaseType = Address::RegBase;
906 }
907
Eric Christopher212ae932010-10-21 19:40:30 +0000908 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000909 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000910 if (needsLowering) {
Eli Friedman9ebf57a2011-04-29 21:22:56 +0000911 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
912 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopher0d581222010-11-19 22:30:02 +0000913 Addr.Offset = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000914 }
Eric Christopher83007122010-08-23 21:44:12 +0000915}
916
Eric Christopher564857f2010-12-01 01:40:24 +0000917void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000918 const MachineInstrBuilder &MIB,
Chad Rosierb29b9502011-11-13 02:23:59 +0000919 unsigned Flags, bool useAM3) {
Eric Christopher564857f2010-12-01 01:40:24 +0000920 // addrmode5 output depends on the selection dag addressing dividing the
921 // offset by 4 that it then later multiplies. Do this here as well.
922 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
923 VT.getSimpleVT().SimpleTy == MVT::f64)
924 Addr.Offset /= 4;
Eric Christopher299bbb22011-04-29 00:03:10 +0000925
Eric Christopher564857f2010-12-01 01:40:24 +0000926 // Frame base works a bit differently. Handle it separately.
927 if (Addr.BaseType == Address::FrameIndexBase) {
928 int FI = Addr.Base.FI;
929 int Offset = Addr.Offset;
930 MachineMemOperand *MMO =
931 FuncInfo.MF->getMachineMemOperand(
932 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000933 Flags,
Eric Christopher564857f2010-12-01 01:40:24 +0000934 MFI.getObjectSize(FI),
935 MFI.getObjectAlignment(FI));
936 // Now add the rest of the operands.
937 MIB.addFrameIndex(FI);
938
Chad Rosier5be833d2011-11-13 04:25:02 +0000939 // ARM halfword load/stores and signed byte loads need an additional operand.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000940 if (useAM3) {
941 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
942 MIB.addReg(0);
943 MIB.addImm(Imm);
944 } else {
945 MIB.addImm(Addr.Offset);
946 }
Eric Christopher564857f2010-12-01 01:40:24 +0000947 MIB.addMemOperand(MMO);
948 } else {
949 // Now add the rest of the operands.
950 MIB.addReg(Addr.Base.Reg);
Eric Christopher299bbb22011-04-29 00:03:10 +0000951
Chad Rosier5be833d2011-11-13 04:25:02 +0000952 // ARM halfword load/stores and signed byte loads need an additional operand.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000953 if (useAM3) {
954 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
955 MIB.addReg(0);
956 MIB.addImm(Imm);
957 } else {
958 MIB.addImm(Addr.Offset);
959 }
Eric Christopher564857f2010-12-01 01:40:24 +0000960 }
961 AddOptionalDefs(MIB);
962}
963
Chad Rosierb29b9502011-11-13 02:23:59 +0000964bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
965 bool isZExt = true, bool allocReg = true) {
Eric Christopherb1cc8482010-08-25 07:23:49 +0000966 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000967 unsigned Opc;
Chad Rosierb29b9502011-11-13 02:23:59 +0000968 bool useAM3 = false;
969 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000970 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000971 // This is mostly going to be Neon/vector support.
972 default: return false;
Chad Rosier646abbf2011-11-11 02:38:59 +0000973 case MVT::i1:
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000974 case MVT::i8:
Chad Rosier57b29972011-11-14 20:22:27 +0000975 if (isThumb2) {
976 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
977 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
978 else
979 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierb29b9502011-11-13 02:23:59 +0000980 } else {
Chad Rosier57b29972011-11-14 20:22:27 +0000981 if (isZExt) {
982 Opc = ARM::LDRBi12;
983 } else {
984 Opc = ARM::LDRSB;
985 useAM3 = true;
986 }
Chad Rosierb29b9502011-11-13 02:23:59 +0000987 }
Eric Christopher7a56f332010-10-08 01:13:17 +0000988 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000989 break;
Chad Rosier73463472011-11-09 21:30:12 +0000990 case MVT::i16:
Chad Rosier57b29972011-11-14 20:22:27 +0000991 if (isThumb2) {
992 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
993 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
994 else
995 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
996 } else {
997 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
998 useAM3 = true;
999 }
Chad Rosier73463472011-11-09 21:30:12 +00001000 RC = ARM::GPRRegisterClass;
1001 break;
Eric Christopherdc908042010-08-31 01:28:42 +00001002 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +00001003 if (isThumb2) {
1004 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1005 Opc = ARM::t2LDRi8;
1006 else
1007 Opc = ARM::t2LDRi12;
1008 } else {
1009 Opc = ARM::LDRi12;
1010 }
Eric Christopher7a56f332010-10-08 01:13:17 +00001011 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +00001012 break;
Eric Christopher6dab1372010-09-18 01:59:37 +00001013 case MVT::f32:
1014 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +00001015 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +00001016 break;
1017 case MVT::f64:
1018 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +00001019 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +00001020 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +00001021 }
Eric Christopher564857f2010-12-01 01:40:24 +00001022 // Simplify this down to something we can handle.
Chad Rosierb29b9502011-11-13 02:23:59 +00001023 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach6b156392010-10-27 21:39:08 +00001024
Eric Christopher564857f2010-12-01 01:40:24 +00001025 // Create the base instruction, then add the operands.
Chad Rosierb29b9502011-11-13 02:23:59 +00001026 if (allocReg)
1027 ResultReg = createResultReg(RC);
1028 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Eric Christopher564857f2010-12-01 01:40:24 +00001029 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1030 TII.get(Opc), ResultReg);
Chad Rosierb29b9502011-11-13 02:23:59 +00001031 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Eric Christopherdc908042010-08-31 01:28:42 +00001032 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +00001033}
1034
Eric Christopher43b62be2010-09-27 06:02:23 +00001035bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +00001036 // Atomic loads need special handling.
1037 if (cast<LoadInst>(I)->isAtomic())
1038 return false;
1039
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001040 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001041 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001042 if (!isLoadTypeLegal(I->getType(), VT))
1043 return false;
1044
Eric Christopher564857f2010-12-01 01:40:24 +00001045 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001046 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +00001047 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001048
1049 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +00001050 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001051 UpdateValueMap(I, ResultReg);
1052 return true;
1053}
1054
Eric Christopher0d581222010-11-19 22:30:02 +00001055bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001056 unsigned StrOpc;
Chad Rosierb29b9502011-11-13 02:23:59 +00001057 bool useAM3 = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001058 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +00001059 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +00001060 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +00001061 case MVT::i1: {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001062 unsigned Res = createResultReg(isThumb2 ? ARM::tGPRRegisterClass :
Eric Christopher4c914122010-11-02 23:59:09 +00001063 ARM::GPRRegisterClass);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001064 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eric Christopher4c914122010-11-02 23:59:09 +00001065 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1066 TII.get(Opc), Res)
1067 .addReg(SrcReg).addImm(1));
1068 SrcReg = Res;
1069 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +00001070 case MVT::i8:
Chad Rosier57b29972011-11-14 20:22:27 +00001071 if (isThumb2) {
1072 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1073 StrOpc = ARM::t2STRBi8;
1074 else
1075 StrOpc = ARM::t2STRBi12;
1076 } else {
1077 StrOpc = ARM::STRBi12;
1078 }
Eric Christopher15418772010-10-12 05:39:06 +00001079 break;
1080 case MVT::i16:
Chad Rosier57b29972011-11-14 20:22:27 +00001081 if (isThumb2) {
1082 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1083 StrOpc = ARM::t2STRHi8;
1084 else
1085 StrOpc = ARM::t2STRHi12;
1086 } else {
1087 StrOpc = ARM::STRH;
1088 useAM3 = true;
1089 }
Eric Christopher15418772010-10-12 05:39:06 +00001090 break;
Eric Christopher47650ec2010-10-16 01:10:35 +00001091 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +00001092 if (isThumb2) {
1093 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1094 StrOpc = ARM::t2STRi8;
1095 else
1096 StrOpc = ARM::t2STRi12;
1097 } else {
1098 StrOpc = ARM::STRi12;
1099 }
Eric Christopher47650ec2010-10-16 01:10:35 +00001100 break;
Eric Christopher56d2b722010-09-02 23:43:26 +00001101 case MVT::f32:
1102 if (!Subtarget->hasVFP2()) return false;
1103 StrOpc = ARM::VSTRS;
1104 break;
1105 case MVT::f64:
1106 if (!Subtarget->hasVFP2()) return false;
1107 StrOpc = ARM::VSTRD;
1108 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001109 }
Eric Christopher564857f2010-12-01 01:40:24 +00001110 // Simplify this down to something we can handle.
Chad Rosierb29b9502011-11-13 02:23:59 +00001111 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach6b156392010-10-27 21:39:08 +00001112
Eric Christopher564857f2010-12-01 01:40:24 +00001113 // Create the base instruction, then add the operands.
1114 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1115 TII.get(StrOpc))
1116 .addReg(SrcReg, getKillRegState(true));
Chad Rosierb29b9502011-11-13 02:23:59 +00001117 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher318b6ee2010-09-02 00:53:56 +00001118 return true;
1119}
1120
Eric Christopher43b62be2010-09-27 06:02:23 +00001121bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001122 Value *Op0 = I->getOperand(0);
1123 unsigned SrcReg = 0;
1124
Eli Friedman4136d232011-09-02 22:33:24 +00001125 // Atomic stores need special handling.
1126 if (cast<StoreInst>(I)->isAtomic())
1127 return false;
1128
Eric Christopher564857f2010-12-01 01:40:24 +00001129 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001130 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001131 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +00001132 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001133
Eric Christopher1b61ef42010-09-02 01:48:11 +00001134 // Get the value to be stored into a register.
1135 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +00001136 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001137
Eric Christopher564857f2010-12-01 01:40:24 +00001138 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001139 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +00001140 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +00001141 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001142
Eric Christopher0d581222010-11-19 22:30:02 +00001143 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +00001144 return true;
1145}
1146
1147static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1148 switch (Pred) {
1149 // Needs two compares...
1150 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001151 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +00001152 default:
Eric Christopher4053e632010-11-02 01:24:49 +00001153 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +00001154 return ARMCC::AL;
1155 case CmpInst::ICMP_EQ:
1156 case CmpInst::FCMP_OEQ:
1157 return ARMCC::EQ;
1158 case CmpInst::ICMP_SGT:
1159 case CmpInst::FCMP_OGT:
1160 return ARMCC::GT;
1161 case CmpInst::ICMP_SGE:
1162 case CmpInst::FCMP_OGE:
1163 return ARMCC::GE;
1164 case CmpInst::ICMP_UGT:
1165 case CmpInst::FCMP_UGT:
1166 return ARMCC::HI;
1167 case CmpInst::FCMP_OLT:
1168 return ARMCC::MI;
1169 case CmpInst::ICMP_ULE:
1170 case CmpInst::FCMP_OLE:
1171 return ARMCC::LS;
1172 case CmpInst::FCMP_ORD:
1173 return ARMCC::VC;
1174 case CmpInst::FCMP_UNO:
1175 return ARMCC::VS;
1176 case CmpInst::FCMP_UGE:
1177 return ARMCC::PL;
1178 case CmpInst::ICMP_SLT:
1179 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001180 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001181 case CmpInst::ICMP_SLE:
1182 case CmpInst::FCMP_ULE:
1183 return ARMCC::LE;
1184 case CmpInst::FCMP_UNE:
1185 case CmpInst::ICMP_NE:
1186 return ARMCC::NE;
1187 case CmpInst::ICMP_UGE:
1188 return ARMCC::HS;
1189 case CmpInst::ICMP_ULT:
1190 return ARMCC::LO;
1191 }
Eric Christopher543cf052010-09-01 22:16:27 +00001192}
1193
Eric Christopher43b62be2010-09-27 06:02:23 +00001194bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001195 const BranchInst *BI = cast<BranchInst>(I);
1196 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1197 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001198
Eric Christophere5734102010-09-03 00:35:47 +00001199 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001200
Eric Christopher0e6233b2010-10-29 21:08:19 +00001201 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1202 // behavior.
Eric Christopher0e6233b2010-10-29 21:08:19 +00001203 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosier75698f32011-10-26 23:17:28 +00001204 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001205
1206 // Get the compare predicate.
Eric Christopher632ae892011-04-29 21:56:31 +00001207 // Try to take advantage of fallthrough opportunities.
1208 CmpInst::Predicate Predicate = CI->getPredicate();
1209 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1210 std::swap(TBB, FBB);
1211 Predicate = CmpInst::getInversePredicate(Predicate);
1212 }
1213
1214 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher0e6233b2010-10-29 21:08:19 +00001215
1216 // We may not handle every CC for now.
1217 if (ARMPred == ARMCC::AL) return false;
1218
Chad Rosier75698f32011-10-26 23:17:28 +00001219 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001220 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier75698f32011-10-26 23:17:28 +00001221 return false;
Jim Grosbach16cb3762010-11-09 19:22:26 +00001222
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001223 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001224 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1225 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1226 FastEmitBranch(FBB, DL);
1227 FuncInfo.MBB->addSuccessor(TBB);
1228 return true;
1229 }
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001230 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1231 MVT SourceVT;
1232 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedman76927d732011-05-25 23:49:02 +00001233 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001234 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001235 unsigned OpReg = getRegForValue(TI->getOperand(0));
1236 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1237 TII.get(TstOpc))
1238 .addReg(OpReg).addImm(1));
1239
1240 unsigned CCMode = ARMCC::NE;
1241 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1242 std::swap(TBB, FBB);
1243 CCMode = ARMCC::EQ;
1244 }
1245
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001246 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001247 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1248 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1249
1250 FastEmitBranch(FBB, DL);
1251 FuncInfo.MBB->addSuccessor(TBB);
1252 return true;
1253 }
Chad Rosier6d64b3a2011-10-27 00:21:16 +00001254 } else if (const ConstantInt *CI =
1255 dyn_cast<ConstantInt>(BI->getCondition())) {
1256 uint64_t Imm = CI->getZExtValue();
1257 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1258 FastEmitBranch(Target, DL);
1259 return true;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001260 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001261
Eric Christopher0e6233b2010-10-29 21:08:19 +00001262 unsigned CmpReg = getRegForValue(BI->getCondition());
1263 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001264
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001265 // We've been divorced from our compare! Our block was split, and
1266 // now our compare lives in a predecessor block. We musn't
1267 // re-compare here, as the children of the compare aren't guaranteed
1268 // live across the block boundary (we *could* check for this).
1269 // Regardless, the compare has been done in the predecessor block,
1270 // and it left a value for us in a virtual register. Ergo, we test
1271 // the one-bit value left in the virtual register.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001272 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1274 .addReg(CmpReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001275
Eric Christopher7a20a372011-04-28 16:52:09 +00001276 unsigned CCMode = ARMCC::NE;
1277 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1278 std::swap(TBB, FBB);
1279 CCMode = ARMCC::EQ;
1280 }
1281
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001282 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001283 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher7a20a372011-04-28 16:52:09 +00001284 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001285 FastEmitBranch(FBB, DL);
1286 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001287 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001288}
1289
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001290bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1291 bool isZExt) {
Chad Rosierade62002011-10-26 23:25:44 +00001292 Type *Ty = Src1Value->getType();
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001293 EVT SrcVT = TLI.getValueType(Ty, true);
1294 if (!SrcVT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001295
Chad Rosierade62002011-10-26 23:25:44 +00001296 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1297 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherd43393a2010-09-08 23:13:45 +00001298 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001299
Chad Rosier2f2fe412011-11-09 03:22:02 +00001300 // Check to see if the 2nd operand is a constant that we can encode directly
1301 // in the compare.
Chad Rosier1c47de82011-11-11 06:27:41 +00001302 int Imm = 0;
1303 bool UseImm = false;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001304 bool isNegativeImm = false;
1305 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1306 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1307 SrcVT == MVT::i1) {
1308 const APInt &CIVal = ConstInt->getValue();
Chad Rosier1c47de82011-11-11 06:27:41 +00001309 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1310 if (Imm < 0) {
Chad Rosier6cba97c2011-11-10 01:30:39 +00001311 isNegativeImm = true;
Chad Rosier1c47de82011-11-11 06:27:41 +00001312 Imm = -Imm;
Chad Rosier6cba97c2011-11-10 01:30:39 +00001313 }
Chad Rosier1c47de82011-11-11 06:27:41 +00001314 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1315 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier2f2fe412011-11-09 03:22:02 +00001316 }
1317 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1318 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1319 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosier1c47de82011-11-11 06:27:41 +00001320 UseImm = true;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001321 }
1322
Eric Christopherd43393a2010-09-08 23:13:45 +00001323 unsigned CmpOpc;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001324 bool isICmp = true;
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001325 bool needsExt = false;
1326 switch (SrcVT.getSimpleVT().SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001327 default: return false;
1328 // TODO: Verify compares.
1329 case MVT::f32:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001330 isICmp = false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001331 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherd43393a2010-09-08 23:13:45 +00001332 break;
1333 case MVT::f64:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001334 isICmp = false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001335 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherd43393a2010-09-08 23:13:45 +00001336 break;
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001337 case MVT::i1:
1338 case MVT::i8:
1339 case MVT::i16:
1340 needsExt = true;
1341 // Intentional fall-through.
Eric Christopherd43393a2010-09-08 23:13:45 +00001342 case MVT::i32:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001343 if (isThumb2) {
Chad Rosier1c47de82011-11-11 06:27:41 +00001344 if (!UseImm)
Chad Rosier2f2fe412011-11-09 03:22:02 +00001345 CmpOpc = ARM::t2CMPrr;
1346 else
1347 CmpOpc = isNegativeImm ? ARM::t2CMNzri : ARM::t2CMPri;
1348 } else {
Chad Rosier1c47de82011-11-11 06:27:41 +00001349 if (!UseImm)
Chad Rosier2f2fe412011-11-09 03:22:02 +00001350 CmpOpc = ARM::CMPrr;
1351 else
1352 CmpOpc = isNegativeImm ? ARM::CMNzri : ARM::CMPri;
1353 }
Eric Christopherd43393a2010-09-08 23:13:45 +00001354 break;
1355 }
1356
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001357 unsigned SrcReg1 = getRegForValue(Src1Value);
1358 if (SrcReg1 == 0) return false;
Chad Rosier530f7ce2011-10-26 22:47:55 +00001359
Chad Rosier2f2fe412011-11-09 03:22:02 +00001360 unsigned SrcReg2;
Chad Rosier1c47de82011-11-11 06:27:41 +00001361 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001362 SrcReg2 = getRegForValue(Src2Value);
1363 if (SrcReg2 == 0) return false;
1364 }
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001365
1366 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1367 if (needsExt) {
1368 unsigned ResultReg;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001369 ResultReg = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001370 if (ResultReg == 0) return false;
1371 SrcReg1 = ResultReg;
Chad Rosier1c47de82011-11-11 06:27:41 +00001372 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001373 ResultReg = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1374 if (ResultReg == 0) return false;
1375 SrcReg2 = ResultReg;
1376 }
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001377 }
Chad Rosier530f7ce2011-10-26 22:47:55 +00001378
Chad Rosier1c47de82011-11-11 06:27:41 +00001379 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001380 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1381 TII.get(CmpOpc))
1382 .addReg(SrcReg1).addReg(SrcReg2));
1383 } else {
1384 MachineInstrBuilder MIB;
1385 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1386 .addReg(SrcReg1);
1387
1388 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1389 if (isICmp)
Chad Rosier1c47de82011-11-11 06:27:41 +00001390 MIB.addImm(Imm);
Chad Rosier2f2fe412011-11-09 03:22:02 +00001391 AddOptionalDefs(MIB);
1392 }
Chad Rosierade62002011-10-26 23:25:44 +00001393
1394 // For floating point we need to move the result to a comparison register
1395 // that we can then use for branches.
1396 if (Ty->isFloatTy() || Ty->isDoubleTy())
1397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1398 TII.get(ARM::FMSTAT)));
Chad Rosier530f7ce2011-10-26 22:47:55 +00001399 return true;
1400}
1401
1402bool ARMFastISel::SelectCmp(const Instruction *I) {
1403 const CmpInst *CI = cast<CmpInst>(I);
Chad Rosierade62002011-10-26 23:25:44 +00001404 Type *Ty = CI->getOperand(0)->getType();
Chad Rosier530f7ce2011-10-26 22:47:55 +00001405
Eric Christopher229207a2010-09-29 01:14:47 +00001406 // Get the compare predicate.
1407 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001408
Eric Christopher229207a2010-09-29 01:14:47 +00001409 // We may not handle every CC for now.
1410 if (ARMPred == ARMCC::AL) return false;
1411
Chad Rosier530f7ce2011-10-26 22:47:55 +00001412 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001413 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier530f7ce2011-10-26 22:47:55 +00001414 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001415
Eric Christopher229207a2010-09-29 01:14:47 +00001416 // Now set a register based on the comparison. Explicitly set the predicates
1417 // here.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001418 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1419 TargetRegisterClass *RC = isThumb2 ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001420 : ARM::GPRRegisterClass;
1421 unsigned DestReg = createResultReg(RC);
Chad Rosierade62002011-10-26 23:25:44 +00001422 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001423 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosierade62002011-10-26 23:25:44 +00001424 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
Chad Rosier530f7ce2011-10-26 22:47:55 +00001425 unsigned CondReg = isFloat ? ARM::FPSCR : ARM::CPSR;
Eric Christopher229207a2010-09-29 01:14:47 +00001426 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1427 .addReg(ZeroReg).addImm(1)
1428 .addImm(ARMPred).addReg(CondReg);
1429
Eric Christophera5b1e682010-09-17 22:28:18 +00001430 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001431 return true;
1432}
1433
Eric Christopher43b62be2010-09-27 06:02:23 +00001434bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001435 // Make sure we have VFP and that we're extending float to double.
1436 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001437
Eric Christopher46203602010-09-09 00:26:48 +00001438 Value *V = I->getOperand(0);
1439 if (!I->getType()->isDoubleTy() ||
1440 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001441
Eric Christopher46203602010-09-09 00:26:48 +00001442 unsigned Op = getRegForValue(V);
1443 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001444
Eric Christopher46203602010-09-09 00:26:48 +00001445 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001446 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001447 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001448 .addReg(Op));
1449 UpdateValueMap(I, Result);
1450 return true;
1451}
1452
Eric Christopher43b62be2010-09-27 06:02:23 +00001453bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001454 // Make sure we have VFP and that we're truncating double to float.
1455 if (!Subtarget->hasVFP2()) return false;
1456
1457 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001458 if (!(I->getType()->isFloatTy() &&
1459 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001460
1461 unsigned Op = getRegForValue(V);
1462 if (Op == 0) return false;
1463
1464 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001465 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001466 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001467 .addReg(Op));
1468 UpdateValueMap(I, Result);
1469 return true;
1470}
1471
Eric Christopher43b62be2010-09-27 06:02:23 +00001472bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001473 // Make sure we have VFP.
1474 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001475
Duncan Sands1440e8b2010-11-03 11:35:31 +00001476 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001477 Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001478 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001479 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001480
Chad Rosier463fe242011-11-03 02:04:59 +00001481 Value *Src = I->getOperand(0);
1482 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1483 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman783c6642011-05-25 19:09:45 +00001484 return false;
1485
Chad Rosier463fe242011-11-03 02:04:59 +00001486 unsigned SrcReg = getRegForValue(Src);
1487 if (SrcReg == 0) return false;
1488
1489 // Handle sign-extension.
1490 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1491 EVT DestVT = MVT::i32;
1492 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, /*isZExt*/ false);
1493 if (ResultReg == 0) return false;
1494 SrcReg = ResultReg;
1495 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001496
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001497 // The conversion routine works on fp-reg to fp-reg and the operand above
1498 // was an integer, move it to the fp registers if possible.
Chad Rosier463fe242011-11-03 02:04:59 +00001499 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001500 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001501
Eric Christopher9a040492010-09-09 18:54:59 +00001502 unsigned Opc;
1503 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1504 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001505 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001506
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001507 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001508 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1509 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001510 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001511 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001512 return true;
1513}
1514
Eric Christopher43b62be2010-09-27 06:02:23 +00001515bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001516 // Make sure we have VFP.
1517 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001518
Duncan Sands1440e8b2010-11-03 11:35:31 +00001519 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001520 Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001521 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001522 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001523
Eric Christopher9a040492010-09-09 18:54:59 +00001524 unsigned Op = getRegForValue(I->getOperand(0));
1525 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001526
Eric Christopher9a040492010-09-09 18:54:59 +00001527 unsigned Opc;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001528 Type *OpTy = I->getOperand(0)->getType();
Eric Christopher9a040492010-09-09 18:54:59 +00001529 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1530 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001531 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001532
Eric Christopher022b7fb2010-10-05 23:13:24 +00001533 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1534 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001535 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1536 ResultReg)
1537 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001538
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001539 // This result needs to be in an integer register, but the conversion only
1540 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001541 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001542 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001543
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001544 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001545 return true;
1546}
1547
Eric Christopher3bbd3962010-10-11 08:27:59 +00001548bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001549 MVT VT;
1550 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001551 return false;
1552
1553 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001554 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001555 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1556
1557 unsigned CondReg = getRegForValue(I->getOperand(0));
1558 if (CondReg == 0) return false;
1559 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1560 if (Op1Reg == 0) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001561
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001562 // Check to see if we can use an immediate in the conditional move.
1563 int Imm = 0;
1564 bool UseImm = false;
1565 bool isNegativeImm = false;
1566 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1567 assert (VT == MVT::i32 && "Expecting an i32.");
1568 Imm = (int)ConstInt->getValue().getZExtValue();
1569 if (Imm < 0) {
1570 isNegativeImm = true;
1571 Imm = ~Imm;
1572 }
1573 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1574 (ARM_AM::getSOImmVal(Imm) != -1);
1575 }
1576
1577 unsigned Op2Reg;
1578 if (!UseImm) {
1579 Op2Reg = getRegForValue(I->getOperand(2));
1580 if (Op2Reg == 0) return false;
1581 }
1582
1583 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001584 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001585 .addReg(CondReg).addImm(0));
1586
1587 unsigned MovCCOpc;
1588 if (!UseImm) {
1589 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1590 } else {
1591 if (!isNegativeImm) {
1592 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1593 } else {
1594 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1595 }
1596 }
Eric Christopher3bbd3962010-10-11 08:27:59 +00001597 unsigned ResultReg = createResultReg(RC);
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001598 if (!UseImm)
1599 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1600 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1601 else
1602 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1603 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001604 UpdateValueMap(I, ResultReg);
1605 return true;
1606}
1607
Eric Christopher08637852010-09-30 22:34:19 +00001608bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001609 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001610 Type *Ty = I->getType();
Eric Christopher08637852010-09-30 22:34:19 +00001611 if (!isTypeLegal(Ty, VT))
1612 return false;
1613
1614 // If we have integer div support we should have selected this automagically.
1615 // In case we have a real miss go ahead and return false and we'll pick
1616 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001617 if (Subtarget->hasDivide()) return false;
1618
Eric Christopher08637852010-09-30 22:34:19 +00001619 // Otherwise emit a libcall.
1620 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001621 if (VT == MVT::i8)
1622 LC = RTLIB::SDIV_I8;
1623 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001624 LC = RTLIB::SDIV_I16;
1625 else if (VT == MVT::i32)
1626 LC = RTLIB::SDIV_I32;
1627 else if (VT == MVT::i64)
1628 LC = RTLIB::SDIV_I64;
1629 else if (VT == MVT::i128)
1630 LC = RTLIB::SDIV_I128;
1631 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001632
Eric Christopher08637852010-09-30 22:34:19 +00001633 return ARMEmitLibcall(I, LC);
1634}
1635
Eric Christopher6a880d62010-10-11 08:37:26 +00001636bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001637 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001638 Type *Ty = I->getType();
Eric Christopher6a880d62010-10-11 08:37:26 +00001639 if (!isTypeLegal(Ty, VT))
1640 return false;
1641
1642 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1643 if (VT == MVT::i8)
1644 LC = RTLIB::SREM_I8;
1645 else if (VT == MVT::i16)
1646 LC = RTLIB::SREM_I16;
1647 else if (VT == MVT::i32)
1648 LC = RTLIB::SREM_I32;
1649 else if (VT == MVT::i64)
1650 LC = RTLIB::SREM_I64;
1651 else if (VT == MVT::i128)
1652 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001653 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001654
Eric Christopher6a880d62010-10-11 08:37:26 +00001655 return ARMEmitLibcall(I, LC);
1656}
1657
Eric Christopher43b62be2010-09-27 06:02:23 +00001658bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001659 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001660
Eric Christopherbc39b822010-09-09 00:53:57 +00001661 // We can get here in the case when we want to use NEON for our fp
1662 // operations, but can't figure out how to. Just use the vfp instructions
1663 // if we have them.
1664 // FIXME: It'd be nice to use NEON instructions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001665 Type *Ty = I->getType();
Eric Christopherbd6bf082010-09-09 01:02:03 +00001666 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1667 if (isFloat && !Subtarget->hasVFP2())
1668 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001669
Eric Christopherbc39b822010-09-09 00:53:57 +00001670 unsigned Op1 = getRegForValue(I->getOperand(0));
1671 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001672
Eric Christopherbc39b822010-09-09 00:53:57 +00001673 unsigned Op2 = getRegForValue(I->getOperand(1));
1674 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001675
Eric Christopherbc39b822010-09-09 00:53:57 +00001676 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001677 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001678 switch (ISDOpcode) {
1679 default: return false;
1680 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001681 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001682 break;
1683 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001684 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001685 break;
1686 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001687 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001688 break;
1689 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001690 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001691 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1692 TII.get(Opc), ResultReg)
1693 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001694 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001695 return true;
1696}
1697
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001698// Call Handling Code
1699
Eric Christopherfa87d662010-10-18 02:17:53 +00001700bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1701 EVT SrcVT, unsigned &ResultReg) {
1702 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1703 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001704
Eric Christopherfa87d662010-10-18 02:17:53 +00001705 if (RR != 0) {
1706 ResultReg = RR;
1707 return true;
1708 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001709 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001710}
1711
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001712// This is largely taken directly from CCAssignFnForNode - we don't support
1713// varargs in FastISel so that part has been removed.
1714// TODO: We may not support all of this.
1715CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1716 switch (CC) {
1717 default:
1718 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001719 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001720 // Ignore fastcc. Silence compiler warnings.
1721 (void)RetFastCC_ARM_APCS;
1722 (void)FastCC_ARM_APCS;
1723 // Fallthrough
1724 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001725 // Use target triple & subtarget features to do actual dispatch.
1726 if (Subtarget->isAAPCS_ABI()) {
1727 if (Subtarget->hasVFP2() &&
1728 FloatABIType == FloatABI::Hard)
1729 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1730 else
1731 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1732 } else
1733 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1734 case CallingConv::ARM_AAPCS_VFP:
1735 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1736 case CallingConv::ARM_AAPCS:
1737 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1738 case CallingConv::ARM_APCS:
1739 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1740 }
1741}
1742
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001743bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1744 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001745 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001746 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1747 SmallVectorImpl<unsigned> &RegArgs,
1748 CallingConv::ID CC,
1749 unsigned &NumBytes) {
1750 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001751 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001752 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1753
1754 // Get a count of how many bytes are to be pushed on the stack.
1755 NumBytes = CCInfo.getNextStackOffset();
1756
1757 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001758 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001759 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1760 TII.get(AdjStackDown))
1761 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001762
1763 // Process the args.
1764 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1765 CCValAssign &VA = ArgLocs[i];
1766 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001767 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001768
Eric Christopher4a2b3162011-01-27 05:44:56 +00001769 // We don't handle NEON/vector parameters yet.
1770 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001771 return false;
1772
Eric Christopherf9764fa2010-09-30 20:49:44 +00001773 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001774 switch (VA.getLocInfo()) {
1775 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001776 case CCValAssign::SExt: {
Chad Rosier42536af2011-11-05 20:16:15 +00001777 EVT DestVT = VA.getLocVT();
1778 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1779 /*isZExt*/false);
1780 assert (ResultReg != 0 && "Failed to emit a sext");
1781 Arg = ResultReg;
Eric Christopherfa87d662010-10-18 02:17:53 +00001782 break;
1783 }
Chad Rosier42536af2011-11-05 20:16:15 +00001784 case CCValAssign::AExt:
1785 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherfa87d662010-10-18 02:17:53 +00001786 case CCValAssign::ZExt: {
Chad Rosier42536af2011-11-05 20:16:15 +00001787 EVT DestVT = VA.getLocVT();
1788 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1789 /*isZExt*/true);
1790 assert (ResultReg != 0 && "Failed to emit a sext");
1791 Arg = ResultReg;
Eric Christopherfa87d662010-10-18 02:17:53 +00001792 break;
1793 }
1794 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001795 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001796 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001797 assert(BC != 0 && "Failed to emit a bitcast!");
1798 Arg = BC;
1799 ArgVT = VA.getLocVT();
1800 break;
1801 }
1802 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001803 }
1804
1805 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001806 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001807 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001808 VA.getLocReg())
Chad Rosier42536af2011-11-05 20:16:15 +00001809 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001810 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001811 } else if (VA.needsCustom()) {
1812 // TODO: We need custom lowering for vector (v2f64) args.
1813 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001814
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001815 CCValAssign &NextVA = ArgLocs[++i];
1816
1817 // TODO: Only handle register args for now.
1818 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1819
1820 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1821 TII.get(ARM::VMOVRRD), VA.getLocReg())
1822 .addReg(NextVA.getLocReg(), RegState::Define)
1823 .addReg(Arg));
1824 RegArgs.push_back(VA.getLocReg());
1825 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001826 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001827 assert(VA.isMemLoc());
1828 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001829 Address Addr;
1830 Addr.BaseType = Address::RegBase;
1831 Addr.Base.Reg = ARM::SP;
1832 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001833
Eric Christopher0d581222010-11-19 22:30:02 +00001834 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001835 }
1836 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001837 return true;
1838}
1839
Duncan Sands1440e8b2010-11-03 11:35:31 +00001840bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001841 const Instruction *I, CallingConv::ID CC,
1842 unsigned &NumBytes) {
1843 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001844 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001845 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1846 TII.get(AdjStackUp))
1847 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001848
1849 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001850 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001851 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001852 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001853 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1854
1855 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001856 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001857 // For this move we copy into two registers and then move into the
1858 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001859 EVT DestVT = RVLocs[0].getValVT();
1860 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1861 unsigned ResultReg = createResultReg(DstRC);
1862 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1863 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001864 .addReg(RVLocs[0].getLocReg())
1865 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001866
Eric Christopher3659ac22010-10-20 08:02:24 +00001867 UsedRegs.push_back(RVLocs[0].getLocReg());
1868 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001869
Eric Christopherdccd2c32010-10-11 08:38:55 +00001870 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001871 UpdateValueMap(I, ResultReg);
1872 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001873 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001874 EVT CopyVT = RVLocs[0].getValVT();
Chad Rosier0eff39f2011-11-08 00:03:32 +00001875
1876 // Special handling for extended integers.
1877 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1878 CopyVT = MVT::i32;
1879
Eric Christopher14df8822010-10-01 00:00:11 +00001880 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001881
Eric Christopher14df8822010-10-01 00:00:11 +00001882 unsigned ResultReg = createResultReg(DstRC);
1883 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1884 ResultReg).addReg(RVLocs[0].getLocReg());
1885 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001886
Eric Christopherdccd2c32010-10-11 08:38:55 +00001887 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001888 UpdateValueMap(I, ResultReg);
1889 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001890 }
1891
Eric Christopherdccd2c32010-10-11 08:38:55 +00001892 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001893}
1894
Eric Christopher4f512ef2010-10-22 01:28:00 +00001895bool ARMFastISel::SelectRet(const Instruction *I) {
1896 const ReturnInst *Ret = cast<ReturnInst>(I);
1897 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001898
Eric Christopher4f512ef2010-10-22 01:28:00 +00001899 if (!FuncInfo.CanLowerReturn)
1900 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001901
Eric Christopher4f512ef2010-10-22 01:28:00 +00001902 if (F.isVarArg())
1903 return false;
1904
1905 CallingConv::ID CC = F.getCallingConv();
1906 if (Ret->getNumOperands() > 0) {
1907 SmallVector<ISD::OutputArg, 4> Outs;
1908 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1909 Outs, TLI);
1910
1911 // Analyze operands of the call, assigning locations to each operand.
1912 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbachb04546f2011-09-13 20:30:37 +00001913 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Eric Christopher4f512ef2010-10-22 01:28:00 +00001914 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1915
1916 const Value *RV = Ret->getOperand(0);
1917 unsigned Reg = getRegForValue(RV);
1918 if (Reg == 0)
1919 return false;
1920
1921 // Only handle a single return value for now.
1922 if (ValLocs.size() != 1)
1923 return false;
1924
1925 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001926
Eric Christopher4f512ef2010-10-22 01:28:00 +00001927 // Don't bother handling odd stuff for now.
1928 if (VA.getLocInfo() != CCValAssign::Full)
1929 return false;
1930 // Only handle register returns for now.
1931 if (!VA.isRegLoc())
1932 return false;
Chad Rosierf470cbb2011-11-04 00:50:21 +00001933
1934 unsigned SrcReg = Reg + VA.getValNo();
1935 EVT RVVT = TLI.getValueType(RV->getType());
1936 EVT DestVT = VA.getValVT();
1937 // Special handling for extended integers.
1938 if (RVVT != DestVT) {
1939 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1940 return false;
1941
1942 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1943 return false;
1944
1945 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
1946
1947 bool isZExt = Outs[0].Flags.isZExt();
1948 unsigned ResultReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1949 if (ResultReg == 0) return false;
1950 SrcReg = ResultReg;
1951 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001952
Eric Christopher4f512ef2010-10-22 01:28:00 +00001953 // Make the copy.
Eric Christopher4f512ef2010-10-22 01:28:00 +00001954 unsigned DstReg = VA.getLocReg();
1955 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1956 // Avoid a cross-class copy. This is very unlikely.
1957 if (!SrcRC->contains(DstReg))
1958 return false;
1959 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1960 DstReg).addReg(SrcReg);
1961
1962 // Mark the register as live out of the function.
1963 MRI.addLiveOut(VA.getLocReg());
1964 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001965
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001966 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Eric Christopher4f512ef2010-10-22 01:28:00 +00001967 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1968 TII.get(RetOpc)));
1969 return true;
1970}
1971
Eric Christopher872f4a22011-02-22 01:37:10 +00001972unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1973
Eric Christopher872f4a22011-02-22 01:37:10 +00001974 // Darwin needs the r9 versions of the opcodes.
1975 bool isDarwin = Subtarget->isTargetDarwin();
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001976 if (isThumb2) {
Eric Christopher872f4a22011-02-22 01:37:10 +00001977 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1978 } else {
1979 return isDarwin ? ARM::BLr9 : ARM::BL;
1980 }
1981}
1982
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001983// A quick function that will emit a call for a named libcall in F with the
1984// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001985// can emit a call for any libcall we can produce. This is an abridged version
1986// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001987// like computed function pointers or strange arguments at call sites.
1988// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1989// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001990bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1991 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001992
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001993 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001994 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001995 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001996 if (RetTy->isVoidTy())
1997 RetVT = MVT::isVoid;
1998 else if (!isTypeLegal(RetTy, RetVT))
1999 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002000
Eric Christopher836c6242010-12-15 23:47:29 +00002001 // TODO: For now if we have long calls specified we don't handle the call.
2002 if (EnableARMLongCalls) return false;
2003
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002004 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002005 SmallVector<Value*, 8> Args;
2006 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002007 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002008 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2009 Args.reserve(I->getNumOperands());
2010 ArgRegs.reserve(I->getNumOperands());
2011 ArgVTs.reserve(I->getNumOperands());
2012 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00002013 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002014 Value *Op = I->getOperand(i);
2015 unsigned Arg = getRegForValue(Op);
2016 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002017
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002018 Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002019 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002020 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002021
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002022 ISD::ArgFlagsTy Flags;
2023 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2024 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002025
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002026 Args.push_back(Op);
2027 ArgRegs.push_back(Arg);
2028 ArgVTs.push_back(ArgVT);
2029 ArgFlags.push_back(Flags);
2030 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002031
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002032 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002033 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002034 unsigned NumBytes;
2035 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2036 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002037
Eric Christopher6344a5f2011-04-29 00:07:20 +00002038 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002039 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002040 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00002041 unsigned CallOpc = ARMSelectCallOp(NULL);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002042 if(isThumb2)
Eric Christopherc19aadb2010-12-21 03:50:43 +00002043 // Explicitly adding the predicate here.
2044 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2045 TII.get(CallOpc)))
2046 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00002047 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00002048 // Explicitly adding the predicate here.
2049 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2050 TII.get(CallOpc))
2051 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00002052
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002053 // Add implicit physical register uses to the call.
2054 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2055 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002056
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002057 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002058 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002059 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002060
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002061 // Set all unused physreg defs as dead.
2062 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002063
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002064 return true;
2065}
2066
Chad Rosier11add262011-11-11 23:31:03 +00002067bool ARMFastISel::SelectCall(const Instruction *I,
2068 const char *IntrMemName = 0) {
Eric Christopherf9764fa2010-09-30 20:49:44 +00002069 const CallInst *CI = cast<CallInst>(I);
2070 const Value *Callee = CI->getCalledValue();
2071
Chad Rosier11add262011-11-11 23:31:03 +00002072 // Can't handle inline asm.
2073 if (isa<InlineAsm>(Callee)) return false;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002074
Eric Christopher52f6c032011-05-02 20:16:33 +00002075 // Only handle global variable Callees.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002076 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christopher52f6c032011-05-02 20:16:33 +00002077 if (!GV)
Eric Christophere6ca6772010-10-01 21:33:12 +00002078 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002079
Eric Christopherf9764fa2010-09-30 20:49:44 +00002080 // Check the calling convention.
2081 ImmutableCallSite CS(CI);
2082 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00002083
Eric Christopherf9764fa2010-09-30 20:49:44 +00002084 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00002085
Eric Christopherf9764fa2010-09-30 20:49:44 +00002086 // Let SDISel handle vararg functions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002087 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2088 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eric Christopherf9764fa2010-09-30 20:49:44 +00002089 if (FTy->isVarArg())
2090 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002091
Eric Christopherf9764fa2010-09-30 20:49:44 +00002092 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002093 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002094 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002095 if (RetTy->isVoidTy())
2096 RetVT = MVT::isVoid;
Chad Rosier0eff39f2011-11-08 00:03:32 +00002097 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2098 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002099 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002100
Eric Christopher836c6242010-12-15 23:47:29 +00002101 // TODO: For now if we have long calls specified we don't handle the call.
2102 if (EnableARMLongCalls) return false;
Eric Christopher299bbb22011-04-29 00:03:10 +00002103
Eric Christopherf9764fa2010-09-30 20:49:44 +00002104 // Set up the argument vectors.
2105 SmallVector<Value*, 8> Args;
2106 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002107 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002108 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2109 Args.reserve(CS.arg_size());
2110 ArgRegs.reserve(CS.arg_size());
2111 ArgVTs.reserve(CS.arg_size());
2112 ArgFlags.reserve(CS.arg_size());
2113 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2114 i != e; ++i) {
Chad Rosier11add262011-11-11 23:31:03 +00002115 // If we're lowering a memory intrinsic instead of a regular call, skip the
2116 // last two arguments, which shouldn't be passed to the underlying function.
2117 if (IntrMemName && e-i <= 2)
2118 break;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002119
Chad Rosier11add262011-11-11 23:31:03 +00002120 unsigned Arg = getRegForValue(*i);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002121 if (Arg == 0)
2122 return false;
2123 ISD::ArgFlagsTy Flags;
2124 unsigned AttrInd = i - CS.arg_begin() + 1;
2125 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2126 Flags.setSExt();
2127 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2128 Flags.setZExt();
2129
Chad Rosier8e4a2e42011-11-04 00:58:10 +00002130 // FIXME: Only handle *easy* calls for now.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002131 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2132 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2133 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2134 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2135 return false;
2136
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002137 Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002138 MVT ArgVT;
Chad Rosier42536af2011-11-05 20:16:15 +00002139 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2140 ArgVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002141 return false;
2142 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2143 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002144
Eric Christopherf9764fa2010-09-30 20:49:44 +00002145 Args.push_back(*i);
2146 ArgRegs.push_back(Arg);
2147 ArgVTs.push_back(ArgVT);
2148 ArgFlags.push_back(Flags);
2149 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002150
Eric Christopherf9764fa2010-09-30 20:49:44 +00002151 // Handle the arguments now that we've gotten them.
2152 SmallVector<unsigned, 4> RegArgs;
2153 unsigned NumBytes;
2154 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2155 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002156
Eric Christopher6344a5f2011-04-29 00:07:20 +00002157 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002158 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002159 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00002160 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00002161 // Explicitly adding the predicate here.
Chad Rosier9eb67482011-11-13 09:44:21 +00002162 if(isThumb2) {
Eric Christopherc19aadb2010-12-21 03:50:43 +00002163 // Explicitly adding the predicate here.
2164 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier11add262011-11-11 23:31:03 +00002165 TII.get(CallOpc)));
Chad Rosier9eb67482011-11-13 09:44:21 +00002166 if (!IntrMemName)
2167 MIB.addGlobalAddress(GV, 0, 0);
2168 else
2169 MIB.addExternalSymbol(IntrMemName, 0);
2170 } else {
2171 if (!IntrMemName)
2172 // Explicitly adding the predicate here.
2173 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2174 TII.get(CallOpc))
2175 .addGlobalAddress(GV, 0, 0));
2176 else
2177 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2178 TII.get(CallOpc))
2179 .addExternalSymbol(IntrMemName, 0));
2180 }
Chad Rosier11add262011-11-11 23:31:03 +00002181
Eric Christopherf9764fa2010-09-30 20:49:44 +00002182 // Add implicit physical register uses to the call.
2183 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2184 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002185
Eric Christopherf9764fa2010-09-30 20:49:44 +00002186 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002187 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002188 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002189
Eric Christopherf9764fa2010-09-30 20:49:44 +00002190 // Set all unused physreg defs as dead.
2191 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002192
Eric Christopherf9764fa2010-09-30 20:49:44 +00002193 return true;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002194}
2195
Chad Rosier11add262011-11-11 23:31:03 +00002196bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2197 // FIXME: Handle more intrinsics.
2198 switch (I.getIntrinsicID()) {
2199 default: return false;
2200 case Intrinsic::memcpy:
2201 case Intrinsic::memmove: {
2202 // FIXME: Small memcpy/memmove's are common enough that we want to do them
2203 // without a call if possible.
2204 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2205 // Don't handle volatile.
2206 if (MTI.isVolatile())
2207 return false;
2208
2209 if (!MTI.getLength()->getType()->isIntegerTy(32))
2210 return false;
2211
2212 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2213 return false;
2214
2215 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2216 return SelectCall(&I, IntrMemName);
2217 }
2218 case Intrinsic::memset: {
2219 const MemSetInst &MSI = cast<MemSetInst>(I);
2220 // Don't handle volatile.
2221 if (MSI.isVolatile())
2222 return false;
2223
2224 if (!MSI.getLength()->getType()->isIntegerTy(32))
2225 return false;
2226
2227 if (MSI.getDestAddressSpace() > 255)
2228 return false;
2229
2230 return SelectCall(&I, "memset");
2231 }
2232 }
2233 return false;
2234}
2235
Chad Rosier0d7b2312011-11-02 00:18:48 +00002236bool ARMFastISel::SelectTrunc(const Instruction *I) {
2237 // The high bits for a type smaller than the register size are assumed to be
2238 // undefined.
2239 Value *Op = I->getOperand(0);
2240
2241 EVT SrcVT, DestVT;
2242 SrcVT = TLI.getValueType(Op->getType(), true);
2243 DestVT = TLI.getValueType(I->getType(), true);
2244
2245 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2246 return false;
2247 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2248 return false;
2249
2250 unsigned SrcReg = getRegForValue(Op);
2251 if (!SrcReg) return false;
2252
2253 // Because the high bits are undefined, a truncate doesn't generate
2254 // any code.
2255 UpdateValueMap(I, SrcReg);
2256 return true;
2257}
2258
Chad Rosier87633022011-11-02 17:20:24 +00002259unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2260 bool isZExt) {
Eli Friedman76927d732011-05-25 23:49:02 +00002261 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier87633022011-11-02 17:20:24 +00002262 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002263
2264 unsigned Opc;
Eli Friedman76927d732011-05-25 23:49:02 +00002265 bool isBoolZext = false;
Chad Rosier87633022011-11-02 17:20:24 +00002266 if (!SrcVT.isSimple()) return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002267 switch (SrcVT.getSimpleVT().SimpleTy) {
Chad Rosier87633022011-11-02 17:20:24 +00002268 default: return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002269 case MVT::i16:
Chad Rosier87633022011-11-02 17:20:24 +00002270 if (!Subtarget->hasV6Ops()) return 0;
2271 if (isZExt)
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002272 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002273 else
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002274 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002275 break;
2276 case MVT::i8:
Chad Rosier87633022011-11-02 17:20:24 +00002277 if (!Subtarget->hasV6Ops()) return 0;
2278 if (isZExt)
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002279 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002280 else
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002281 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002282 break;
2283 case MVT::i1:
Chad Rosier87633022011-11-02 17:20:24 +00002284 if (isZExt) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002285 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eli Friedman76927d732011-05-25 23:49:02 +00002286 isBoolZext = true;
2287 break;
2288 }
Chad Rosier87633022011-11-02 17:20:24 +00002289 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002290 }
2291
Chad Rosier87633022011-11-02 17:20:24 +00002292 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eli Friedman76927d732011-05-25 23:49:02 +00002293 MachineInstrBuilder MIB;
Chad Rosier87633022011-11-02 17:20:24 +00002294 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
Eli Friedman76927d732011-05-25 23:49:02 +00002295 .addReg(SrcReg);
2296 if (isBoolZext)
2297 MIB.addImm(1);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00002298 else
2299 MIB.addImm(0);
Eli Friedman76927d732011-05-25 23:49:02 +00002300 AddOptionalDefs(MIB);
Chad Rosier87633022011-11-02 17:20:24 +00002301 return ResultReg;
2302}
2303
2304bool ARMFastISel::SelectIntExt(const Instruction *I) {
2305 // On ARM, in general, integer casts don't involve legal types; this code
2306 // handles promotable integers.
Chad Rosier87633022011-11-02 17:20:24 +00002307 Type *DestTy = I->getType();
2308 Value *Src = I->getOperand(0);
2309 Type *SrcTy = Src->getType();
2310
2311 EVT SrcVT, DestVT;
2312 SrcVT = TLI.getValueType(SrcTy, true);
2313 DestVT = TLI.getValueType(DestTy, true);
2314
2315 bool isZExt = isa<ZExtInst>(I);
2316 unsigned SrcReg = getRegForValue(Src);
2317 if (!SrcReg) return false;
2318
2319 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2320 if (ResultReg == 0) return false;
2321 UpdateValueMap(I, ResultReg);
Eli Friedman76927d732011-05-25 23:49:02 +00002322 return true;
2323}
2324
Eric Christopher56d2b722010-09-02 23:43:26 +00002325// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00002326bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00002327
Eric Christopherab695882010-07-21 22:26:11 +00002328 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00002329 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00002330 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00002331 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00002332 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00002333 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00002334 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00002335 case Instruction::ICmp:
2336 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00002337 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00002338 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00002339 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00002340 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00002341 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002342 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00002343 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002344 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00002345 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00002346 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00002347 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00002348 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00002349 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00002350 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00002351 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002352 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00002353 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00002354 case Instruction::SRem:
2355 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002356 case Instruction::Call:
Chad Rosier11add262011-11-11 23:31:03 +00002357 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2358 return SelectIntrinsicCall(*II);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002359 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00002360 case Instruction::Select:
2361 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00002362 case Instruction::Ret:
2363 return SelectRet(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002364 case Instruction::Trunc:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002365 return SelectTrunc(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002366 case Instruction::ZExt:
2367 case Instruction::SExt:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002368 return SelectIntExt(I);
Eric Christopherab695882010-07-21 22:26:11 +00002369 default: break;
2370 }
2371 return false;
2372}
2373
Chad Rosierb29b9502011-11-13 02:23:59 +00002374/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2375/// vreg is being provided by the specified load instruction. If possible,
2376/// try to fold the load as an operand to the instruction, returning true if
2377/// successful.
2378bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2379 const LoadInst *LI) {
2380 // Verify we have a legal type before going any further.
2381 MVT VT;
2382 if (!isLoadTypeLegal(LI->getType(), VT))
2383 return false;
2384
2385 // Combine load followed by zero- or sign-extend.
2386 // ldrb r1, [r0] ldrb r1, [r0]
2387 // uxtb r2, r1 =>
2388 // mov r3, r2 mov r3, r1
2389 bool isZExt = true;
2390 switch(MI->getOpcode()) {
2391 default: return false;
2392 case ARM::SXTH:
2393 case ARM::t2SXTH:
2394 isZExt = false;
2395 case ARM::UXTH:
2396 case ARM::t2UXTH:
2397 if (VT != MVT::i16)
2398 return false;
2399 break;
2400 case ARM::SXTB:
2401 case ARM::t2SXTB:
2402 isZExt = false;
2403 case ARM::UXTB:
2404 case ARM::t2UXTB:
2405 if (VT != MVT::i8)
2406 return false;
2407 break;
2408 }
2409 // See if we can handle this address.
2410 Address Addr;
2411 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2412
2413 unsigned ResultReg = MI->getOperand(0).getReg();
2414 if (!ARMEmitLoad(VT, ResultReg, Addr, isZExt, false))
2415 return false;
2416 MI->eraseFromParent();
2417 return true;
2418}
2419
Eric Christopherab695882010-07-21 22:26:11 +00002420namespace llvm {
2421 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00002422 // Completely untested on non-darwin.
2423 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002424
Eric Christopheraaa8df42010-11-02 01:21:28 +00002425 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00002426 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002427 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00002428 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00002429 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00002430 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00002431 }
2432}