blob: 92ead77d193b88a87f7a0c5a71d56aff7f37a76c [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"
Eric Christopherab695882010-07-21 22:26:11 +000023#include "llvm/CallingConv.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/Instructions.h"
27#include "llvm/IntrinsicInst.h"
Eric Christopherbb3e5da2010-09-14 23:03:37 +000028#include "llvm/Module.h"
Eric Christopherab695882010-07-21 22:26:11 +000029#include "llvm/CodeGen/Analysis.h"
30#include "llvm/CodeGen/FastISel.h"
31#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
33#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000034#include "llvm/CodeGen/MachineConstantPool.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000036#include "llvm/CodeGen/MachineMemOperand.h"
Eric Christopherab695882010-07-21 22:26:11 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000040#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000041#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000043#include "llvm/Target/TargetData.h"
44#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetLowering.h"
46#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000047#include "llvm/Target/TargetOptions.h"
48using namespace llvm;
49
Eric Christopher038fea52010-08-17 00:46:57 +000050static cl::opt<bool>
Eric Christopher6e5367d2010-10-18 22:53:53 +000051DisableARMFastISel("disable-arm-fast-isel",
52 cl::desc("Turn off experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000053 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000054
Eric Christopherab695882010-07-21 22:26:11 +000055namespace {
56
57class ARMFastISel : public FastISel {
58
59 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
60 /// make the right decision when generating code for different targets.
61 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000062 const TargetMachine &TM;
63 const TargetInstrInfo &TII;
64 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000065 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000066
Eric Christopher8cf6c602010-09-29 22:24:45 +000067 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000068 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000069 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000070
Eric Christopherab695882010-07-21 22:26:11 +000071 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000072 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000073 : FastISel(funcInfo),
74 TM(funcInfo.MF->getTarget()),
75 TII(*TM.getInstrInfo()),
76 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000077 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000078 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000079 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +000080 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +000081 }
82
Eric Christophercb592292010-08-20 00:20:31 +000083 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000084 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
85 const TargetRegisterClass *RC);
86 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill);
89 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
90 const TargetRegisterClass *RC,
91 unsigned Op0, bool Op0IsKill,
92 unsigned Op1, bool Op1IsKill);
93 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
94 const TargetRegisterClass *RC,
95 unsigned Op0, bool Op0IsKill,
96 uint64_t Imm);
97 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
98 const TargetRegisterClass *RC,
99 unsigned Op0, bool Op0IsKill,
100 const ConstantFP *FPImm);
101 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
102 const TargetRegisterClass *RC,
103 uint64_t Imm);
104 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
105 const TargetRegisterClass *RC,
106 unsigned Op0, bool Op0IsKill,
107 unsigned Op1, bool Op1IsKill,
108 uint64_t Imm);
109 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
110 unsigned Op0, bool Op0IsKill,
111 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000112
Eric Christophercb592292010-08-20 00:20:31 +0000113 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000114 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000115 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000116 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000117
118 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000119
Eric Christopher83007122010-08-23 21:44:12 +0000120 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000121 private:
Eric Christopher43b62be2010-09-27 06:02:23 +0000122 virtual bool SelectLoad(const Instruction *I);
123 virtual bool SelectStore(const Instruction *I);
124 virtual bool SelectBranch(const Instruction *I);
125 virtual bool SelectCmp(const Instruction *I);
126 virtual bool SelectFPExt(const Instruction *I);
127 virtual bool SelectFPTrunc(const Instruction *I);
128 virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
129 virtual bool SelectSIToFP(const Instruction *I);
130 virtual bool SelectFPToSI(const Instruction *I);
131 virtual bool SelectSDiv(const Instruction *I);
Eric Christopher6a880d62010-10-11 08:37:26 +0000132 virtual bool SelectSRem(const Instruction *I);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000133 virtual bool SelectCall(const Instruction *I);
Eric Christopher3bbd3962010-10-11 08:27:59 +0000134 virtual bool SelectSelect(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000135
Eric Christopher83007122010-08-23 21:44:12 +0000136 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000137 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000138 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000139 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher404be0c2010-10-17 11:08:44 +0000140 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Base, int Offset);
141 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Base, int Offset);
142 bool ARMComputeRegOffset(const Value *Obj, unsigned &Base, int &Offset);
143 void ARMSimplifyRegOffset(unsigned &Base, int &Offset, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000144 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000145 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000146 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000147 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000148 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000149
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000150 // Call handling routines.
151 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000152 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
153 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000154 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000155 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000156 SmallVectorImpl<unsigned> &ArgRegs,
157 SmallVectorImpl<EVT> &ArgVTs,
158 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
159 SmallVectorImpl<unsigned> &RegArgs,
160 CallingConv::ID CC,
161 unsigned &NumBytes);
162 bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
163 const Instruction *I, CallingConv::ID CC,
164 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000165 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000166
167 // OptionalDef handling routines.
168 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000169 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
170 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
171};
Eric Christopherab695882010-07-21 22:26:11 +0000172
173} // end anonymous namespace
174
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000175#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000176
Eric Christopher456144e2010-08-19 00:37:05 +0000177// DefinesOptionalPredicate - This is different from DefinesPredicate in that
178// we don't care about implicit defs here, just places we'll need to add a
179// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
180bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
181 const TargetInstrDesc &TID = MI->getDesc();
182 if (!TID.hasOptionalDef())
183 return false;
184
185 // Look to see if our OptionalDef is defining CPSR or CCR.
186 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
187 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000188 if (!MO.isReg() || !MO.isDef()) continue;
189 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000190 *CPSR = true;
191 }
192 return true;
193}
194
195// If the machine is predicable go ahead and add the predicate operands, if
196// it needs default CC operands add those.
197const MachineInstrBuilder &
198ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
199 MachineInstr *MI = &*MIB;
200
201 // Do we use a predicate?
202 if (TII.isPredicable(MI))
203 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000204
Eric Christopher456144e2010-08-19 00:37:05 +0000205 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
206 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000207 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000208 if (DefinesOptionalPredicate(MI, &CPSR)) {
209 if (CPSR)
210 AddDefaultT1CC(MIB);
211 else
212 AddDefaultCC(MIB);
213 }
214 return MIB;
215}
216
Eric Christopher0fe7d542010-08-17 01:25:29 +0000217unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
218 const TargetRegisterClass* RC) {
219 unsigned ResultReg = createResultReg(RC);
220 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
221
Eric Christopher456144e2010-08-19 00:37:05 +0000222 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000223 return ResultReg;
224}
225
226unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
227 const TargetRegisterClass *RC,
228 unsigned Op0, bool Op0IsKill) {
229 unsigned ResultReg = createResultReg(RC);
230 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
231
232 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000233 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000234 .addReg(Op0, Op0IsKill * RegState::Kill));
235 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000236 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000237 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000238 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000239 TII.get(TargetOpcode::COPY), ResultReg)
240 .addReg(II.ImplicitDefs[0]));
241 }
242 return ResultReg;
243}
244
245unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
246 const TargetRegisterClass *RC,
247 unsigned Op0, bool Op0IsKill,
248 unsigned Op1, bool Op1IsKill) {
249 unsigned ResultReg = createResultReg(RC);
250 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
251
252 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000253 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000254 .addReg(Op0, Op0IsKill * RegState::Kill)
255 .addReg(Op1, Op1IsKill * RegState::Kill));
256 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000257 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000258 .addReg(Op0, Op0IsKill * RegState::Kill)
259 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000260 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000261 TII.get(TargetOpcode::COPY), ResultReg)
262 .addReg(II.ImplicitDefs[0]));
263 }
264 return ResultReg;
265}
266
267unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
268 const TargetRegisterClass *RC,
269 unsigned Op0, bool Op0IsKill,
270 uint64_t Imm) {
271 unsigned ResultReg = createResultReg(RC);
272 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
273
274 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000275 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000276 .addReg(Op0, Op0IsKill * RegState::Kill)
277 .addImm(Imm));
278 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000279 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000280 .addReg(Op0, Op0IsKill * RegState::Kill)
281 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000282 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000283 TII.get(TargetOpcode::COPY), ResultReg)
284 .addReg(II.ImplicitDefs[0]));
285 }
286 return ResultReg;
287}
288
289unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
290 const TargetRegisterClass *RC,
291 unsigned Op0, bool Op0IsKill,
292 const ConstantFP *FPImm) {
293 unsigned ResultReg = createResultReg(RC);
294 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
295
296 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000298 .addReg(Op0, Op0IsKill * RegState::Kill)
299 .addFPImm(FPImm));
300 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000302 .addReg(Op0, Op0IsKill * RegState::Kill)
303 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000305 TII.get(TargetOpcode::COPY), ResultReg)
306 .addReg(II.ImplicitDefs[0]));
307 }
308 return ResultReg;
309}
310
311unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
312 const TargetRegisterClass *RC,
313 unsigned Op0, bool Op0IsKill,
314 unsigned Op1, bool Op1IsKill,
315 uint64_t Imm) {
316 unsigned ResultReg = createResultReg(RC);
317 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
318
319 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000320 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000321 .addReg(Op0, Op0IsKill * RegState::Kill)
322 .addReg(Op1, Op1IsKill * RegState::Kill)
323 .addImm(Imm));
324 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000325 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000326 .addReg(Op0, Op0IsKill * RegState::Kill)
327 .addReg(Op1, Op1IsKill * RegState::Kill)
328 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000330 TII.get(TargetOpcode::COPY), ResultReg)
331 .addReg(II.ImplicitDefs[0]));
332 }
333 return ResultReg;
334}
335
336unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
337 const TargetRegisterClass *RC,
338 uint64_t Imm) {
339 unsigned ResultReg = createResultReg(RC);
340 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000341
Eric Christopher0fe7d542010-08-17 01:25:29 +0000342 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000343 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000344 .addImm(Imm));
345 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000346 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000347 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000348 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000349 TII.get(TargetOpcode::COPY), ResultReg)
350 .addReg(II.ImplicitDefs[0]));
351 }
352 return ResultReg;
353}
354
355unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
356 unsigned Op0, bool Op0IsKill,
357 uint32_t Idx) {
358 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
359 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
360 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000361 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000362 DL, TII.get(TargetOpcode::COPY), ResultReg)
363 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
364 return ResultReg;
365}
366
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000367// TODO: Don't worry about 64-bit now, but when this is fixed remove the
368// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000369unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000370 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000371
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000372 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
373 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
374 TII.get(ARM::VMOVRS), MoveReg)
375 .addReg(SrcReg));
376 return MoveReg;
377}
378
379unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000380 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000381
Eric Christopheraa3ace12010-09-09 20:49:25 +0000382 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
383 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000384 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000385 .addReg(SrcReg));
386 return MoveReg;
387}
388
Eric Christopher9ed58df2010-09-09 00:19:41 +0000389// For double width floating point we need to materialize two constants
390// (the high and the low) into integer registers then use a move to get
391// the combined constant into an FP reg.
392unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
393 const APFloat Val = CFP->getValueAPF();
394 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000395
Eric Christopher9ed58df2010-09-09 00:19:41 +0000396 // This checks to see if we can use VFP3 instructions to materialize
397 // a constant, otherwise we have to go through the constant pool.
398 if (TLI.isFPImmLegal(Val, VT)) {
399 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
400 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
401 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
402 DestReg)
403 .addFPImm(CFP));
404 return DestReg;
405 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000406
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000407 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000408 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000409
Eric Christopher238bb162010-09-09 23:50:00 +0000410 // MachineConstantPool wants an explicit alignment.
411 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
412 if (Align == 0) {
413 // TODO: Figure out if this is correct.
414 Align = TD.getTypeAllocSize(CFP->getType());
415 }
416 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
417 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
418 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000419
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000420 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000421 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
422 DestReg)
423 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000424 .addReg(0));
425 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000426}
427
Eric Christopher744c7c82010-09-28 22:47:54 +0000428unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000429
Eric Christopher744c7c82010-09-28 22:47:54 +0000430 // For now 32-bit only.
431 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000432
Eric Christopher56d2b722010-09-02 23:43:26 +0000433 // MachineConstantPool wants an explicit alignment.
434 unsigned Align = TD.getPrefTypeAlignment(C->getType());
435 if (Align == 0) {
436 // TODO: Figure out if this is correct.
437 Align = TD.getTypeAllocSize(C->getType());
438 }
439 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000440 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000441
Eric Christopher56d2b722010-09-02 23:43:26 +0000442 if (isThumb)
443 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000444 TII.get(ARM::t2LDRpci), DestReg)
445 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000446 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000447 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000448 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000449 TII.get(ARM::LDRcp), DestReg)
450 .addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000451 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000452
Eric Christopher56d2b722010-09-02 23:43:26 +0000453 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000454}
455
Eric Christopherc9932f62010-10-01 23:24:42 +0000456unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000457 // For now 32-bit only.
458 if (VT.getSimpleVT().SimpleTy != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000459
Eric Christopher890dbbe2010-10-02 00:32:44 +0000460 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000461
Eric Christopher890dbbe2010-10-02 00:32:44 +0000462 // TODO: No external globals for now.
463 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000464
Eric Christopher890dbbe2010-10-02 00:32:44 +0000465 // TODO: Need more magic for ARM PIC.
466 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000467
Eric Christopher890dbbe2010-10-02 00:32:44 +0000468 // MachineConstantPool wants an explicit alignment.
469 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
470 if (Align == 0) {
471 // TODO: Figure out if this is correct.
472 Align = TD.getTypeAllocSize(GV->getType());
473 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000474
Eric Christopher890dbbe2010-10-02 00:32:44 +0000475 // Grab index.
476 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
477 unsigned Id = AFI->createConstPoolEntryUId();
478 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
479 ARMCP::CPValue, PCAdj);
480 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000481
Eric Christopher890dbbe2010-10-02 00:32:44 +0000482 // Load value.
483 MachineInstrBuilder MIB;
484 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
485 if (isThumb) {
486 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
487 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
488 .addConstantPoolIndex(Idx);
489 if (RelocM == Reloc::PIC_)
490 MIB.addImm(Id);
491 } else {
492 // The extra reg and immediate are for addrmode2.
493 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
494 DestReg)
495 .addConstantPoolIndex(Idx)
496 .addReg(0).addImm(0);
497 }
498 AddOptionalDefs(MIB);
499 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000500}
501
Eric Christopher9ed58df2010-09-09 00:19:41 +0000502unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
503 EVT VT = TLI.getValueType(C->getType(), true);
504
505 // Only handle simple types.
506 if (!VT.isSimple()) return 0;
507
508 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
509 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000510 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
511 return ARMMaterializeGV(GV, VT);
512 else if (isa<ConstantInt>(C))
513 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000514
Eric Christopherc9932f62010-10-01 23:24:42 +0000515 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000516}
517
Eric Christopherf9764fa2010-09-30 20:49:44 +0000518unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
519 // Don't handle dynamic allocas.
520 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000521
Eric Christopherf9764fa2010-09-30 20:49:44 +0000522 EVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000523 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000524
Eric Christopherf9764fa2010-09-30 20:49:44 +0000525 DenseMap<const AllocaInst*, int>::iterator SI =
526 FuncInfo.StaticAllocaMap.find(AI);
527
528 // This will get lowered later into the correct offsets and registers
529 // via rewriteXFrameIndex.
530 if (SI != FuncInfo.StaticAllocaMap.end()) {
531 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
532 unsigned ResultReg = createResultReg(RC);
533 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
534 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
535 TII.get(Opc), ResultReg)
536 .addFrameIndex(SI->second)
537 .addImm(0));
538 return ResultReg;
539 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000540
Eric Christopherf9764fa2010-09-30 20:49:44 +0000541 return 0;
542}
543
Eric Christopherb1cc8482010-08-25 07:23:49 +0000544bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
545 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000546
Eric Christopherb1cc8482010-08-25 07:23:49 +0000547 // Only handle simple types.
548 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000549
Eric Christopherdc908042010-08-31 01:28:42 +0000550 // Handle all legal types, i.e. a register that will directly hold this
551 // value.
552 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000553}
554
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000555bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
556 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000557
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000558 // If this is a type than can be sign or zero-extended to a basic operation
559 // go ahead and accept it now.
560 if (VT == MVT::i8 || VT == MVT::i16)
561 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000562
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000563 return false;
564}
565
Eric Christophercb0b04b2010-08-24 00:07:24 +0000566// Computes the Reg+Offset to get to an object.
Eric Christopher404be0c2010-10-17 11:08:44 +0000567bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Base,
Eric Christopher83007122010-08-23 21:44:12 +0000568 int &Offset) {
569 // Some boilerplate from the X86 FastISel.
570 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000571 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000572 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000573 // Don't walk into other basic blocks; it's possible we haven't
574 // visited them yet, so the instructions may not yet be assigned
575 // virtual registers.
576 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
577 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000578 Opcode = I->getOpcode();
579 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000580 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000581 Opcode = C->getOpcode();
582 U = C;
583 }
584
Eric Christophercb0b04b2010-08-24 00:07:24 +0000585 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000586 if (Ty->getAddressSpace() > 255)
587 // Fast instruction selection doesn't support the special
588 // address spaces.
589 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000590
Eric Christopher83007122010-08-23 21:44:12 +0000591 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000592 default:
Eric Christopher83007122010-08-23 21:44:12 +0000593 break;
Eric Christopher55324332010-10-12 00:43:21 +0000594 case Instruction::BitCast: {
595 // Look through bitcasts.
Eric Christophera3224252010-10-15 21:32:12 +0000596 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000597 }
598 case Instruction::IntToPtr: {
599 // Look past no-op inttoptrs.
600 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000601 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000602 break;
603 }
604 case Instruction::PtrToInt: {
605 // Look past no-op ptrtoints.
606 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000607 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000608 break;
609 }
Eric Christophereae84392010-10-14 09:29:41 +0000610 case Instruction::GetElementPtr: {
611 int SavedOffset = Offset;
Eric Christopher404be0c2010-10-17 11:08:44 +0000612 unsigned SavedBase = Base;
Eric Christophereae84392010-10-14 09:29:41 +0000613 int TmpOffset = Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000614
Eric Christophereae84392010-10-14 09:29:41 +0000615 // Iterate through the GEP folding the constants into offsets where
616 // we can.
617 gep_type_iterator GTI = gep_type_begin(U);
618 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
619 i != e; ++i, ++GTI) {
620 const Value *Op = *i;
621 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
622 const StructLayout *SL = TD.getStructLayout(STy);
623 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
624 TmpOffset += SL->getElementOffset(Idx);
625 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000626 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
627 SmallVector<const Value *, 4> Worklist;
628 Worklist.push_back(Op);
629 do {
630 Op = Worklist.pop_back_val();
631 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
632 // Constant-offset addressing.
633 TmpOffset += CI->getSExtValue() * S;
Eric Christopherdc0b0ef2010-10-17 01:41:46 +0000634 } else if (isa<AddOperator>(Op) &&
Eric Christopher2896df82010-10-15 18:02:07 +0000635 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
636 // An add with a constant operand. Fold the constant.
637 ConstantInt *CI =
638 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
639 TmpOffset += CI->getSExtValue() * S;
640 // Add the other operand back to the work list.
641 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
642 } else
643 goto unsupported_gep;
644 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000645 }
646 }
Eric Christopher2896df82010-10-15 18:02:07 +0000647
648 // Try to grab the base operand now.
Eric Christophereae84392010-10-14 09:29:41 +0000649 Offset = TmpOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000650 if (ARMComputeRegOffset(U->getOperand(0), Base, Offset)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000651
652 // We failed, restore everything and try the other options.
Eric Christophereae84392010-10-14 09:29:41 +0000653 Offset = SavedOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000654 Base = SavedBase;
Eric Christopher2896df82010-10-15 18:02:07 +0000655
Eric Christophereae84392010-10-14 09:29:41 +0000656 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000657 break;
658 }
Eric Christopher83007122010-08-23 21:44:12 +0000659 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000660 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopherd56d61a2010-10-17 01:51:42 +0000661 unsigned Reg = TargetMaterializeAlloca(AI);
662
663 if (Reg == 0) return false;
664
Eric Christopher404be0c2010-10-17 11:08:44 +0000665 Base = Reg;
Eric Christopherd56d61a2010-10-17 01:51:42 +0000666 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000667 }
668 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000669
Eric Christophera9c57512010-10-13 21:41:51 +0000670 // Materialize the global variable's address into a reg which can
671 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000672 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000673 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
674 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000675
Eric Christopher404be0c2010-10-17 11:08:44 +0000676 Base = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000677 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000678 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000679
Eric Christophercb0b04b2010-08-24 00:07:24 +0000680 // Try to get this in a register if nothing else has worked.
Eric Christopher404be0c2010-10-17 11:08:44 +0000681 if (Base == 0) Base = getRegForValue(Obj);
682 return Base != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000683}
684
Eric Christopher404be0c2010-10-17 11:08:44 +0000685void ARMFastISel::ARMSimplifyRegOffset(unsigned &Base, int &Offset, EVT VT) {
Eric Christopher404be0c2010-10-17 11:08:44 +0000686
Eric Christopher212ae932010-10-21 19:40:30 +0000687 assert(VT.isSimple() && "Non-simple types are invalid here!");
688
689 bool needsLowering = false;
690 switch (VT.getSimpleVT().SimpleTy) {
691 default:
692 assert(false && "Unhandled load/store type!");
693 case MVT::i1:
694 case MVT::i8:
695 case MVT::i16:
696 case MVT::i32:
697 // Integer loads/stores handle 12-bit offsets.
698 needsLowering = ((Offset & 0xfff) != Offset);
699 break;
700 case MVT::f32:
701 case MVT::f64:
702 // Floating point operands handle 8-bit offsets.
703 needsLowering = ((Offset & 0xff) != Offset);
704 break;
705 }
706
707 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000708 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000709 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000710 ARMCC::CondCodes Pred = ARMCC::AL;
711 unsigned PredReg = 0;
712
Eric Christopher2896df82010-10-15 18:02:07 +0000713 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
714 ARM::GPRRegisterClass;
715 unsigned BaseReg = createResultReg(RC);
716
Eric Christophereaa204b2010-09-02 01:39:14 +0000717 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000718 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher404be0c2010-10-17 11:08:44 +0000719 BaseReg, Base, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000720 static_cast<const ARMBaseInstrInfo&>(TII));
721 else {
722 assert(AFI->isThumb2Function());
723 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher404be0c2010-10-17 11:08:44 +0000724 BaseReg, Base, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000725 static_cast<const ARMBaseInstrInfo&>(TII));
726 }
Eric Christophereae84392010-10-14 09:29:41 +0000727 Offset = 0;
Eric Christopher404be0c2010-10-17 11:08:44 +0000728 Base = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000729 }
Eric Christopher83007122010-08-23 21:44:12 +0000730}
731
Eric Christopherb1cc8482010-08-25 07:23:49 +0000732bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
Eric Christopher404be0c2010-10-17 11:08:44 +0000733 unsigned Base, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000734
Eric Christopherb1cc8482010-08-25 07:23:49 +0000735 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000736 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000737 TargetRegisterClass *RC;
Eric Christopher6dab1372010-09-18 01:59:37 +0000738 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000739 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000740 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000741 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000742 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000743 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000744 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000745 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000746 break;
747 case MVT::i8:
Eric Christopher45c60712010-10-17 01:40:27 +0000748 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRB;
Eric Christopher7a56f332010-10-08 01:13:17 +0000749 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000750 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000751 case MVT::i32:
Eric Christopher45c60712010-10-17 01:40:27 +0000752 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDR;
Eric Christopher7a56f332010-10-08 01:13:17 +0000753 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000754 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000755 case MVT::f32:
756 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000757 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000758 isFloat = true;
759 break;
760 case MVT::f64:
761 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000762 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000763 isFloat = true;
764 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000765 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000766
Eric Christopheree56ea62010-10-07 05:50:44 +0000767 ResultReg = createResultReg(RC);
Eric Christopher404be0c2010-10-17 11:08:44 +0000768
Eric Christopher212ae932010-10-21 19:40:30 +0000769 ARMSimplifyRegOffset(Base, Offset, VT);
770
771 // addrmode5 output depends on the selection dag addressing dividing the
772 // offset by 4 that it then later multiplies. Do this here as well.
773 if (isFloat)
774 Offset /= 4;
775
Eric Christopher7a56f332010-10-08 01:13:17 +0000776 // The thumb and floating point instructions both take 2 operands, ARM takes
777 // another register.
Eric Christopher404be0c2010-10-17 11:08:44 +0000778 if (isFloat || isThumb)
Eric Christopher6dab1372010-09-18 01:59:37 +0000779 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
780 TII.get(Opc), ResultReg)
Eric Christopher404be0c2010-10-17 11:08:44 +0000781 .addReg(Base).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000782 else
783 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
784 TII.get(Opc), ResultReg)
Eric Christopher404be0c2010-10-17 11:08:44 +0000785 .addReg(Base).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000786 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000787}
788
Eric Christopher43b62be2010-09-27 06:02:23 +0000789bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000790 // Verify we have a legal type before going any further.
791 EVT VT;
792 if (!isLoadTypeLegal(I->getType(), VT))
793 return false;
794
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000795 // Our register and offset with innocuous defaults.
Eric Christopher404be0c2010-10-17 11:08:44 +0000796 unsigned Base = 0;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000797 int Offset = 0;
798
799 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000800 if (!ARMComputeRegOffset(I->getOperand(0), Base, Offset))
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000801 return false;
802
803 unsigned ResultReg;
Eric Christophera3224252010-10-15 21:32:12 +0000804 if (!ARMEmitLoad(VT, ResultReg, Base, Offset)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000805
806 UpdateValueMap(I, ResultReg);
807 return true;
808}
809
Eric Christopher318b6ee2010-09-02 00:53:56 +0000810bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
Eric Christopher404be0c2010-10-17 11:08:44 +0000811 unsigned Base, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000812 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000813 bool isFloat = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000814 switch (VT.getSimpleVT().SimpleTy) {
815 default: return false;
816 case MVT::i1:
Eric Christopher2896df82010-10-15 18:02:07 +0000817 case MVT::i8:
Eric Christopher45c60712010-10-17 01:40:27 +0000818 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRB;
Eric Christopher15418772010-10-12 05:39:06 +0000819 break;
820 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000821 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000822 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000823 case MVT::i32:
Eric Christopher45c60712010-10-17 01:40:27 +0000824 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STR;
Eric Christopher47650ec2010-10-16 01:10:35 +0000825 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000826 case MVT::f32:
827 if (!Subtarget->hasVFP2()) return false;
828 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000829 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000830 break;
831 case MVT::f64:
832 if (!Subtarget->hasVFP2()) return false;
833 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000834 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000835 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000836 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000837
Eric Christopher212ae932010-10-21 19:40:30 +0000838 ARMSimplifyRegOffset(Base, Offset, VT);
839
840 // addrmode5 output depends on the selection dag addressing dividing the
841 // offset by 4 that it then later multiplies. Do this here as well.
842 if (isFloat)
843 Offset /= 4;
844
Eric Christopherb74558a2010-09-18 01:23:38 +0000845 // The thumb addressing mode has operands swapped from the arm addressing
846 // mode, the floating point one only has two operands.
Eric Christopher404be0c2010-10-17 11:08:44 +0000847 if (isFloat || isThumb)
Eric Christopherb74558a2010-09-18 01:23:38 +0000848 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000849 TII.get(StrOpc))
Eric Christopher404be0c2010-10-17 11:08:44 +0000850 .addReg(SrcReg).addReg(Base).addImm(Offset));
Eric Christopher318b6ee2010-09-02 00:53:56 +0000851 else
852 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000853 TII.get(StrOpc))
Eric Christopher404be0c2010-10-17 11:08:44 +0000854 .addReg(SrcReg).addReg(Base).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000855
Eric Christopher318b6ee2010-09-02 00:53:56 +0000856 return true;
857}
858
Eric Christopher43b62be2010-09-27 06:02:23 +0000859bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000860 Value *Op0 = I->getOperand(0);
861 unsigned SrcReg = 0;
862
Eric Christopher543cf052010-09-01 22:16:27 +0000863 // Yay type legalization
864 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000865 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000866 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000867
Eric Christopher1b61ef42010-09-02 01:48:11 +0000868 // Get the value to be stored into a register.
869 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000870 if (SrcReg == 0)
871 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000872
Eric Christopher318b6ee2010-09-02 00:53:56 +0000873 // Our register and offset with innocuous defaults.
Eric Christopher404be0c2010-10-17 11:08:44 +0000874 unsigned Base = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000875 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000876
Eric Christopher318b6ee2010-09-02 00:53:56 +0000877 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000878 if (!ARMComputeRegOffset(I->getOperand(1), Base, Offset))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000879 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000880
Eric Christophera3224252010-10-15 21:32:12 +0000881 if (!ARMEmitStore(VT, SrcReg, Base, Offset)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000882
Eric Christophera5b1e682010-09-17 22:28:18 +0000883 return true;
884}
885
886static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
887 switch (Pred) {
888 // Needs two compares...
889 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000890 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000891 default:
892 assert(false && "Unhandled CmpInst::Predicate!");
893 return ARMCC::AL;
894 case CmpInst::ICMP_EQ:
895 case CmpInst::FCMP_OEQ:
896 return ARMCC::EQ;
897 case CmpInst::ICMP_SGT:
898 case CmpInst::FCMP_OGT:
899 return ARMCC::GT;
900 case CmpInst::ICMP_SGE:
901 case CmpInst::FCMP_OGE:
902 return ARMCC::GE;
903 case CmpInst::ICMP_UGT:
904 case CmpInst::FCMP_UGT:
905 return ARMCC::HI;
906 case CmpInst::FCMP_OLT:
907 return ARMCC::MI;
908 case CmpInst::ICMP_ULE:
909 case CmpInst::FCMP_OLE:
910 return ARMCC::LS;
911 case CmpInst::FCMP_ORD:
912 return ARMCC::VC;
913 case CmpInst::FCMP_UNO:
914 return ARMCC::VS;
915 case CmpInst::FCMP_UGE:
916 return ARMCC::PL;
917 case CmpInst::ICMP_SLT:
918 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000919 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000920 case CmpInst::ICMP_SLE:
921 case CmpInst::FCMP_ULE:
922 return ARMCC::LE;
923 case CmpInst::FCMP_UNE:
924 case CmpInst::ICMP_NE:
925 return ARMCC::NE;
926 case CmpInst::ICMP_UGE:
927 return ARMCC::HS;
928 case CmpInst::ICMP_ULT:
929 return ARMCC::LO;
930 }
Eric Christopher543cf052010-09-01 22:16:27 +0000931}
932
Eric Christopher43b62be2010-09-27 06:02:23 +0000933bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000934 const BranchInst *BI = cast<BranchInst>(I);
935 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
936 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000937
Eric Christophere5734102010-09-03 00:35:47 +0000938 // Simple branch support.
Eric Christopher229207a2010-09-29 01:14:47 +0000939 // TODO: Try to avoid the re-computation in some places.
940 unsigned CondReg = getRegForValue(BI->getCondition());
Eric Christophere5734102010-09-03 00:35:47 +0000941 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000942
Eric Christopher229207a2010-09-29 01:14:47 +0000943 // Re-set the flags just in case.
944 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
945 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
946 .addReg(CondReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000947
Eric Christophere5734102010-09-03 00:35:47 +0000948 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +0000949 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +0000950 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +0000951 FastEmitBranch(FBB, DL);
952 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000953 return true;
Eric Christophere5734102010-09-03 00:35:47 +0000954}
955
Eric Christopher43b62be2010-09-27 06:02:23 +0000956bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +0000957 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000958
Eric Christopherd43393a2010-09-08 23:13:45 +0000959 EVT VT;
960 const Type *Ty = CI->getOperand(0)->getType();
961 if (!isTypeLegal(Ty, VT))
962 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000963
Eric Christopherd43393a2010-09-08 23:13:45 +0000964 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
965 if (isFloat && !Subtarget->hasVFP2())
966 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000967
Eric Christopherd43393a2010-09-08 23:13:45 +0000968 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +0000969 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +0000970 switch (VT.getSimpleVT().SimpleTy) {
971 default: return false;
972 // TODO: Verify compares.
973 case MVT::f32:
974 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +0000975 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000976 break;
977 case MVT::f64:
978 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +0000979 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000980 break;
981 case MVT::i32:
982 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +0000983 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000984 break;
985 }
986
Eric Christopher229207a2010-09-29 01:14:47 +0000987 // Get the compare predicate.
988 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +0000989
Eric Christopher229207a2010-09-29 01:14:47 +0000990 // We may not handle every CC for now.
991 if (ARMPred == ARMCC::AL) return false;
992
Eric Christopherd43393a2010-09-08 23:13:45 +0000993 unsigned Arg1 = getRegForValue(CI->getOperand(0));
994 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000995
Eric Christopherd43393a2010-09-08 23:13:45 +0000996 unsigned Arg2 = getRegForValue(CI->getOperand(1));
997 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000998
Eric Christopherd43393a2010-09-08 23:13:45 +0000999 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1000 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001001
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001002 // For floating point we need to move the result to a comparison register
1003 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001004 if (isFloat)
1005 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1006 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001007
Eric Christopher229207a2010-09-29 01:14:47 +00001008 // Now set a register based on the comparison. Explicitly set the predicates
1009 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001010 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001011 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001012 : ARM::GPRRegisterClass;
1013 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001014 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001015 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001016 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1017 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1018 .addReg(ZeroReg).addImm(1)
1019 .addImm(ARMPred).addReg(CondReg);
1020
Eric Christophera5b1e682010-09-17 22:28:18 +00001021 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001022 return true;
1023}
1024
Eric Christopher43b62be2010-09-27 06:02:23 +00001025bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001026 // Make sure we have VFP and that we're extending float to double.
1027 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001028
Eric Christopher46203602010-09-09 00:26:48 +00001029 Value *V = I->getOperand(0);
1030 if (!I->getType()->isDoubleTy() ||
1031 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001032
Eric Christopher46203602010-09-09 00:26:48 +00001033 unsigned Op = getRegForValue(V);
1034 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001035
Eric Christopher46203602010-09-09 00:26:48 +00001036 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001037 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001038 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001039 .addReg(Op));
1040 UpdateValueMap(I, Result);
1041 return true;
1042}
1043
Eric Christopher43b62be2010-09-27 06:02:23 +00001044bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001045 // Make sure we have VFP and that we're truncating double to float.
1046 if (!Subtarget->hasVFP2()) return false;
1047
1048 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001049 if (!(I->getType()->isFloatTy() &&
1050 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001051
1052 unsigned Op = getRegForValue(V);
1053 if (Op == 0) return false;
1054
1055 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001056 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001057 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001058 .addReg(Op));
1059 UpdateValueMap(I, Result);
1060 return true;
1061}
1062
Eric Christopher43b62be2010-09-27 06:02:23 +00001063bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001064 // Make sure we have VFP.
1065 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001066
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001067 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001068 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001069 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001070 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001071
Eric Christopher9a040492010-09-09 18:54:59 +00001072 unsigned Op = getRegForValue(I->getOperand(0));
1073 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001074
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001075 // The conversion routine works on fp-reg to fp-reg and the operand above
1076 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001077 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001078 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001079
Eric Christopher9a040492010-09-09 18:54:59 +00001080 unsigned Opc;
1081 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1082 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1083 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001084
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001085 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001086 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1087 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001088 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001089 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001090 return true;
1091}
1092
Eric Christopher43b62be2010-09-27 06:02:23 +00001093bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001094 // Make sure we have VFP.
1095 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001096
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001097 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001098 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001099 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001100 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001101
Eric Christopher9a040492010-09-09 18:54:59 +00001102 unsigned Op = getRegForValue(I->getOperand(0));
1103 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001104
Eric Christopher9a040492010-09-09 18:54:59 +00001105 unsigned Opc;
1106 const Type *OpTy = I->getOperand(0)->getType();
1107 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1108 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1109 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001110
Eric Christopher022b7fb2010-10-05 23:13:24 +00001111 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1112 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001113 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1114 ResultReg)
1115 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001116
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001117 // This result needs to be in an integer register, but the conversion only
1118 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001119 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001120 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001121
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001122 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001123 return true;
1124}
1125
Eric Christopher3bbd3962010-10-11 08:27:59 +00001126bool ARMFastISel::SelectSelect(const Instruction *I) {
1127 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
1128 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
1129 return false;
1130
1131 // Things need to be register sized for register moves.
1132 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
1133 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1134
1135 unsigned CondReg = getRegForValue(I->getOperand(0));
1136 if (CondReg == 0) return false;
1137 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1138 if (Op1Reg == 0) return false;
1139 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1140 if (Op2Reg == 0) return false;
1141
1142 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1143 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1144 .addReg(CondReg).addImm(1));
1145 unsigned ResultReg = createResultReg(RC);
1146 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1147 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1148 .addReg(Op1Reg).addReg(Op2Reg)
1149 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1150 UpdateValueMap(I, ResultReg);
1151 return true;
1152}
1153
Eric Christopher08637852010-09-30 22:34:19 +00001154bool ARMFastISel::SelectSDiv(const Instruction *I) {
1155 EVT VT;
1156 const Type *Ty = I->getType();
1157 if (!isTypeLegal(Ty, VT))
1158 return false;
1159
1160 // If we have integer div support we should have selected this automagically.
1161 // In case we have a real miss go ahead and return false and we'll pick
1162 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001163 if (Subtarget->hasDivide()) return false;
1164
Eric Christopher08637852010-09-30 22:34:19 +00001165 // Otherwise emit a libcall.
1166 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001167 if (VT == MVT::i8)
1168 LC = RTLIB::SDIV_I8;
1169 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001170 LC = RTLIB::SDIV_I16;
1171 else if (VT == MVT::i32)
1172 LC = RTLIB::SDIV_I32;
1173 else if (VT == MVT::i64)
1174 LC = RTLIB::SDIV_I64;
1175 else if (VT == MVT::i128)
1176 LC = RTLIB::SDIV_I128;
1177 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001178
Eric Christopher08637852010-09-30 22:34:19 +00001179 return ARMEmitLibcall(I, LC);
1180}
1181
Eric Christopher6a880d62010-10-11 08:37:26 +00001182bool ARMFastISel::SelectSRem(const Instruction *I) {
1183 EVT VT;
1184 const Type *Ty = I->getType();
1185 if (!isTypeLegal(Ty, VT))
1186 return false;
1187
1188 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1189 if (VT == MVT::i8)
1190 LC = RTLIB::SREM_I8;
1191 else if (VT == MVT::i16)
1192 LC = RTLIB::SREM_I16;
1193 else if (VT == MVT::i32)
1194 LC = RTLIB::SREM_I32;
1195 else if (VT == MVT::i64)
1196 LC = RTLIB::SREM_I64;
1197 else if (VT == MVT::i128)
1198 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001199 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001200
Eric Christopher6a880d62010-10-11 08:37:26 +00001201 return ARMEmitLibcall(I, LC);
1202}
1203
Eric Christopher43b62be2010-09-27 06:02:23 +00001204bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001205 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001206
Eric Christopherbc39b822010-09-09 00:53:57 +00001207 // We can get here in the case when we want to use NEON for our fp
1208 // operations, but can't figure out how to. Just use the vfp instructions
1209 // if we have them.
1210 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001211 const Type *Ty = I->getType();
1212 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1213 if (isFloat && !Subtarget->hasVFP2())
1214 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001215
Eric Christopherbc39b822010-09-09 00:53:57 +00001216 unsigned Op1 = getRegForValue(I->getOperand(0));
1217 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001218
Eric Christopherbc39b822010-09-09 00:53:57 +00001219 unsigned Op2 = getRegForValue(I->getOperand(1));
1220 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001221
Eric Christopherbc39b822010-09-09 00:53:57 +00001222 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +00001223 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1224 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001225 switch (ISDOpcode) {
1226 default: return false;
1227 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001228 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001229 break;
1230 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001231 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001232 break;
1233 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001234 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001235 break;
1236 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001237 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001238 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1239 TII.get(Opc), ResultReg)
1240 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001241 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001242 return true;
1243}
1244
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001245// Call Handling Code
1246
Eric Christopherfa87d662010-10-18 02:17:53 +00001247bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1248 EVT SrcVT, unsigned &ResultReg) {
1249 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1250 Src, /*TODO: Kill=*/false);
1251
1252 if (RR != 0) {
1253 ResultReg = RR;
1254 return true;
1255 } else
1256 return false;
1257}
1258
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001259// This is largely taken directly from CCAssignFnForNode - we don't support
1260// varargs in FastISel so that part has been removed.
1261// TODO: We may not support all of this.
1262CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1263 switch (CC) {
1264 default:
1265 llvm_unreachable("Unsupported calling convention");
1266 case CallingConv::C:
1267 case CallingConv::Fast:
1268 // Use target triple & subtarget features to do actual dispatch.
1269 if (Subtarget->isAAPCS_ABI()) {
1270 if (Subtarget->hasVFP2() &&
1271 FloatABIType == FloatABI::Hard)
1272 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1273 else
1274 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1275 } else
1276 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1277 case CallingConv::ARM_AAPCS_VFP:
1278 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1279 case CallingConv::ARM_AAPCS:
1280 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1281 case CallingConv::ARM_APCS:
1282 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1283 }
1284}
1285
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001286bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1287 SmallVectorImpl<unsigned> &ArgRegs,
1288 SmallVectorImpl<EVT> &ArgVTs,
1289 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1290 SmallVectorImpl<unsigned> &RegArgs,
1291 CallingConv::ID CC,
1292 unsigned &NumBytes) {
1293 SmallVector<CCValAssign, 16> ArgLocs;
1294 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1295 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1296
1297 // Get a count of how many bytes are to be pushed on the stack.
1298 NumBytes = CCInfo.getNextStackOffset();
1299
1300 // Issue CALLSEQ_START
1301 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001302 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1303 TII.get(AdjStackDown))
1304 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001305
1306 // Process the args.
1307 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1308 CCValAssign &VA = ArgLocs[i];
1309 unsigned Arg = ArgRegs[VA.getValNo()];
1310 EVT ArgVT = ArgVTs[VA.getValNo()];
1311
Eric Christopherf9764fa2010-09-30 20:49:44 +00001312 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001313 switch (VA.getLocInfo()) {
1314 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001315 case CCValAssign::SExt: {
1316 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1317 Arg, ArgVT, Arg);
1318 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
1319 Emitted = true;
1320 ArgVT = VA.getLocVT();
1321 break;
1322 }
1323 case CCValAssign::ZExt: {
1324 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1325 Arg, ArgVT, Arg);
1326 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
1327 Emitted = true;
1328 ArgVT = VA.getLocVT();
1329 break;
1330 }
1331 case CCValAssign::AExt: {
1332 // We don't handle NEON or f64 parameters yet.
1333 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() >= 64)
1334 return false;
1335 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1336 Arg, ArgVT, Arg);
1337 if (!Emitted)
1338 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1339 Arg, ArgVT, Arg);
1340 if (!Emitted)
1341 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1342 Arg, ArgVT, Arg);
1343
1344 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
1345 ArgVT = VA.getLocVT();
1346 break;
1347 }
1348 case CCValAssign::BCvt: {
1349 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(),
1350 VA.getLocVT().getSimpleVT(),
1351 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
1352 assert(BC != 0 && "Failed to emit a bitcast!");
1353 Arg = BC;
1354 ArgVT = VA.getLocVT();
1355 break;
1356 }
1357 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001358 }
1359
1360 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001361 // TODO: We need custom lowering for f64 args.
1362 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001363 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001364 VA.getLocReg())
1365 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001366 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001367 } else if (VA.needsCustom()) {
1368 // TODO: We need custom lowering for vector (v2f64) args.
1369 if (VA.getLocVT() != MVT::f64) return false;
1370
1371 CCValAssign &NextVA = ArgLocs[++i];
1372
1373 // TODO: Only handle register args for now.
1374 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1375
1376 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1377 TII.get(ARM::VMOVRRD), VA.getLocReg())
1378 .addReg(NextVA.getLocReg(), RegState::Define)
1379 .addReg(Arg));
1380 RegArgs.push_back(VA.getLocReg());
1381 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001382 } else {
1383 // Need to store
1384 return false;
1385 }
1386 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001387
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001388 return true;
1389}
1390
1391bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1392 const Instruction *I, CallingConv::ID CC,
1393 unsigned &NumBytes) {
1394 // Issue CALLSEQ_END
1395 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001396 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1397 TII.get(AdjStackUp))
1398 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001399
1400 // Now the return value.
1401 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1402 SmallVector<CCValAssign, 16> RVLocs;
1403 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1404 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1405
1406 // Copy all of the result registers out of their specified physreg.
Eric Christopher14df8822010-10-01 00:00:11 +00001407 if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1408 // For this move we copy into two registers and then move into the
1409 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001410 EVT DestVT = RVLocs[0].getValVT();
1411 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1412 unsigned ResultReg = createResultReg(DstRC);
1413 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1414 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001415 .addReg(RVLocs[0].getLocReg())
1416 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001417
Eric Christopher3659ac22010-10-20 08:02:24 +00001418 UsedRegs.push_back(RVLocs[0].getLocReg());
1419 UsedRegs.push_back(RVLocs[1].getLocReg());
1420
Eric Christopherdccd2c32010-10-11 08:38:55 +00001421 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001422 UpdateValueMap(I, ResultReg);
1423 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001424 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001425 EVT CopyVT = RVLocs[0].getValVT();
1426 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001427
Eric Christopher14df8822010-10-01 00:00:11 +00001428 unsigned ResultReg = createResultReg(DstRC);
1429 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1430 ResultReg).addReg(RVLocs[0].getLocReg());
1431 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001432
Eric Christopherdccd2c32010-10-11 08:38:55 +00001433 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001434 UpdateValueMap(I, ResultReg);
1435 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001436 }
1437
Eric Christopherdccd2c32010-10-11 08:38:55 +00001438 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001439}
1440
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001441// A quick function that will emit a call for a named libcall in F with the
1442// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001443// can emit a call for any libcall we can produce. This is an abridged version
1444// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001445// like computed function pointers or strange arguments at call sites.
1446// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1447// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001448bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1449 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001450
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001451 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001452 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001453 EVT RetVT;
1454 if (RetTy->isVoidTy())
1455 RetVT = MVT::isVoid;
1456 else if (!isTypeLegal(RetTy, RetVT))
1457 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001458
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001459 // For now we're using BLX etc on the assumption that we have v5t ops.
1460 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001461
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001462 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001463 SmallVector<Value*, 8> Args;
1464 SmallVector<unsigned, 8> ArgRegs;
1465 SmallVector<EVT, 8> ArgVTs;
1466 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1467 Args.reserve(I->getNumOperands());
1468 ArgRegs.reserve(I->getNumOperands());
1469 ArgVTs.reserve(I->getNumOperands());
1470 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001471 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001472 Value *Op = I->getOperand(i);
1473 unsigned Arg = getRegForValue(Op);
1474 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001475
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001476 const Type *ArgTy = Op->getType();
1477 EVT ArgVT;
1478 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001479
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001480 ISD::ArgFlagsTy Flags;
1481 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1482 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001483
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001484 Args.push_back(Op);
1485 ArgRegs.push_back(Arg);
1486 ArgVTs.push_back(ArgVT);
1487 ArgFlags.push_back(Flags);
1488 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001489
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001490 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001491 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001492 unsigned NumBytes;
1493 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1494 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001495
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001496 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001497 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001498 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001499 unsigned CallOpc;
1500 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001501 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001502 else
1503 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001504 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001505 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001506
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001507 // Add implicit physical register uses to the call.
1508 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1509 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001510
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001511 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001512 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001513 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001514
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001515 // Set all unused physreg defs as dead.
1516 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001517
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001518 return true;
1519}
1520
Eric Christopherf9764fa2010-09-30 20:49:44 +00001521bool ARMFastISel::SelectCall(const Instruction *I) {
1522 const CallInst *CI = cast<CallInst>(I);
1523 const Value *Callee = CI->getCalledValue();
1524
1525 // Can't handle inline asm or worry about intrinsics yet.
1526 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1527
Eric Christophere6ca6772010-10-01 21:33:12 +00001528 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001529 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001530 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1531 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001532
Eric Christopherf9764fa2010-09-30 20:49:44 +00001533 // Check the calling convention.
1534 ImmutableCallSite CS(CI);
1535 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001536
Eric Christopherf9764fa2010-09-30 20:49:44 +00001537 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001538
Eric Christopherf9764fa2010-09-30 20:49:44 +00001539 // Let SDISel handle vararg functions.
1540 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1541 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1542 if (FTy->isVarArg())
1543 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001544
Eric Christopherf9764fa2010-09-30 20:49:44 +00001545 // Handle *simple* calls for now.
1546 const Type *RetTy = I->getType();
1547 EVT RetVT;
1548 if (RetTy->isVoidTy())
1549 RetVT = MVT::isVoid;
1550 else if (!isTypeLegal(RetTy, RetVT))
1551 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001552
Eric Christopherf9764fa2010-09-30 20:49:44 +00001553 // For now we're using BLX etc on the assumption that we have v5t ops.
1554 // TODO: Maybe?
1555 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001556
Eric Christopherf9764fa2010-09-30 20:49:44 +00001557 // Set up the argument vectors.
1558 SmallVector<Value*, 8> Args;
1559 SmallVector<unsigned, 8> ArgRegs;
1560 SmallVector<EVT, 8> ArgVTs;
1561 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1562 Args.reserve(CS.arg_size());
1563 ArgRegs.reserve(CS.arg_size());
1564 ArgVTs.reserve(CS.arg_size());
1565 ArgFlags.reserve(CS.arg_size());
1566 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1567 i != e; ++i) {
1568 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001569
Eric Christopherf9764fa2010-09-30 20:49:44 +00001570 if (Arg == 0)
1571 return false;
1572 ISD::ArgFlagsTy Flags;
1573 unsigned AttrInd = i - CS.arg_begin() + 1;
1574 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1575 Flags.setSExt();
1576 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1577 Flags.setZExt();
1578
1579 // FIXME: Only handle *easy* calls for now.
1580 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1581 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1582 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1583 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1584 return false;
1585
1586 const Type *ArgTy = (*i)->getType();
1587 EVT ArgVT;
1588 if (!isTypeLegal(ArgTy, ArgVT))
1589 return false;
1590 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1591 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001592
Eric Christopherf9764fa2010-09-30 20:49:44 +00001593 Args.push_back(*i);
1594 ArgRegs.push_back(Arg);
1595 ArgVTs.push_back(ArgVT);
1596 ArgFlags.push_back(Flags);
1597 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001598
Eric Christopherf9764fa2010-09-30 20:49:44 +00001599 // Handle the arguments now that we've gotten them.
1600 SmallVector<unsigned, 4> RegArgs;
1601 unsigned NumBytes;
1602 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1603 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001604
Eric Christopherf9764fa2010-09-30 20:49:44 +00001605 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001606 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001607 MachineInstrBuilder MIB;
1608 unsigned CallOpc;
1609 if(isThumb)
1610 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1611 else
1612 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1613 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1614 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001615
Eric Christopherf9764fa2010-09-30 20:49:44 +00001616 // Add implicit physical register uses to the call.
1617 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1618 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001619
Eric Christopherf9764fa2010-09-30 20:49:44 +00001620 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001621 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001622 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001623
Eric Christopherf9764fa2010-09-30 20:49:44 +00001624 // Set all unused physreg defs as dead.
1625 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001626
Eric Christopherf9764fa2010-09-30 20:49:44 +00001627 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001628
Eric Christopherf9764fa2010-09-30 20:49:44 +00001629}
1630
Eric Christopher56d2b722010-09-02 23:43:26 +00001631// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001632bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001633 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001634 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001635
Eric Christopherab695882010-07-21 22:26:11 +00001636 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001637 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001638 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001639 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001640 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001641 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001642 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001643 case Instruction::ICmp:
1644 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001645 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001646 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001647 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001648 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001649 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001650 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001651 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001652 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001653 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001654 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001655 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001656 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001657 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001658 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001659 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001660 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001661 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001662 case Instruction::SRem:
1663 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001664 case Instruction::Call:
1665 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001666 case Instruction::Select:
1667 return SelectSelect(I);
Eric Christopherab695882010-07-21 22:26:11 +00001668 default: break;
1669 }
1670 return false;
1671}
1672
1673namespace llvm {
1674 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001675 // Completely untested on non-darwin.
1676 const TargetMachine &TM = funcInfo.MF->getTarget();
1677 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher6e5367d2010-10-18 22:53:53 +00001678 if (Subtarget->isTargetDarwin() && !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001679 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001680 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001681 }
1682}