Expand GEP handling for constant offsets.
[oota-llvm.git] / lib / Target / ARM / ARMFastISel.cpp
1 //===-- 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"
17 #include "ARMBaseInstrInfo.h"
18 #include "ARMCallingConv.h"
19 #include "ARMRegisterInfo.h"
20 #include "ARMTargetMachine.h"
21 #include "ARMSubtarget.h"
22 #include "ARMConstantPoolValue.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/IntrinsicInst.h"
28 #include "llvm/Module.h"
29 #include "llvm/CodeGen/Analysis.h"
30 #include "llvm/CodeGen/FastISel.h"
31 #include "llvm/CodeGen/FunctionLoweringInfo.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/MachineModuleInfo.h"
34 #include "llvm/CodeGen/MachineConstantPool.h"
35 #include "llvm/CodeGen/MachineFrameInfo.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/Support/CallSite.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/GetElementPtrTypeIterator.h"
41 #include "llvm/Target/TargetData.h"
42 #include "llvm/Target/TargetInstrInfo.h"
43 #include "llvm/Target/TargetLowering.h"
44 #include "llvm/Target/TargetMachine.h"
45 #include "llvm/Target/TargetOptions.h"
46 using namespace llvm;
47
48 static cl::opt<bool>
49 EnableARMFastISel("arm-fast-isel",
50                     cl::desc("Turn on experimental ARM fast-isel support"),
51                     cl::init(false), cl::Hidden);
52
53 namespace {
54
55 class ARMFastISel : public FastISel {
56
57   /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
58   /// make the right decision when generating code for different targets.
59   const ARMSubtarget *Subtarget;
60   const TargetMachine &TM;
61   const TargetInstrInfo &TII;
62   const TargetLowering &TLI;
63   ARMFunctionInfo *AFI;
64
65   // Convenience variables to avoid some queries.
66   bool isThumb;
67   LLVMContext *Context;
68
69   public:
70     explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
71     : FastISel(funcInfo),
72       TM(funcInfo.MF->getTarget()),
73       TII(*TM.getInstrInfo()),
74       TLI(*TM.getTargetLowering()) {
75       Subtarget = &TM.getSubtarget<ARMSubtarget>();
76       AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
77       isThumb = AFI->isThumbFunction();
78       Context = &funcInfo.Fn->getContext();
79     }
80
81     // Code from FastISel.cpp.
82     virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
83                                    const TargetRegisterClass *RC);
84     virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
85                                     const TargetRegisterClass *RC,
86                                     unsigned Op0, bool Op0IsKill);
87     virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
88                                      const TargetRegisterClass *RC,
89                                      unsigned Op0, bool Op0IsKill,
90                                      unsigned Op1, bool Op1IsKill);
91     virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
92                                      const TargetRegisterClass *RC,
93                                      unsigned Op0, bool Op0IsKill,
94                                      uint64_t Imm);
95     virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
96                                      const TargetRegisterClass *RC,
97                                      unsigned Op0, bool Op0IsKill,
98                                      const ConstantFP *FPImm);
99     virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
100                                     const TargetRegisterClass *RC,
101                                     uint64_t Imm);
102     virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
103                                       const TargetRegisterClass *RC,
104                                       unsigned Op0, bool Op0IsKill,
105                                       unsigned Op1, bool Op1IsKill,
106                                       uint64_t Imm);
107     virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
108                                                 unsigned Op0, bool Op0IsKill,
109                                                 uint32_t Idx);
110
111     // Backend specific FastISel code.
112     virtual bool TargetSelectInstruction(const Instruction *I);
113     virtual unsigned TargetMaterializeConstant(const Constant *C);
114     virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
115
116   #include "ARMGenFastISel.inc"
117
118     // Instruction selection routines.
119   private:
120     virtual bool SelectLoad(const Instruction *I);
121     virtual bool SelectStore(const Instruction *I);
122     virtual bool SelectBranch(const Instruction *I);
123     virtual bool SelectCmp(const Instruction *I);
124     virtual bool SelectFPExt(const Instruction *I);
125     virtual bool SelectFPTrunc(const Instruction *I);
126     virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
127     virtual bool SelectSIToFP(const Instruction *I);
128     virtual bool SelectFPToSI(const Instruction *I);
129     virtual bool SelectSDiv(const Instruction *I);
130     virtual bool SelectSRem(const Instruction *I);
131     virtual bool SelectCall(const Instruction *I);
132     virtual bool SelectSelect(const Instruction *I);
133
134     // Utility routines.
135   private:
136     bool isTypeLegal(const Type *Ty, EVT &VT);
137     bool isLoadTypeLegal(const Type *Ty, EVT &VT);
138     bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
139     bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
140     bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
141     void ARMSimplifyRegOffset(unsigned &Reg, int &Offset, EVT VT);
142     unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
143     unsigned ARMMaterializeInt(const Constant *C, EVT VT);
144     unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
145     unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
146     unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
147
148     // Call handling routines.
149   private:
150     CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
151     bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
152                          SmallVectorImpl<unsigned> &ArgRegs,
153                          SmallVectorImpl<EVT> &ArgVTs,
154                          SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
155                          SmallVectorImpl<unsigned> &RegArgs,
156                          CallingConv::ID CC,
157                          unsigned &NumBytes);
158     bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
159                     const Instruction *I, CallingConv::ID CC,
160                     unsigned &NumBytes);
161     bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
162
163     // OptionalDef handling routines.
164   private:
165     bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
166     const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
167 };
168
169 } // end anonymous namespace
170
171 #include "ARMGenCallingConv.inc"
172
173 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
174 // we don't care about implicit defs here, just places we'll need to add a
175 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
176 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
177   const TargetInstrDesc &TID = MI->getDesc();
178   if (!TID.hasOptionalDef())
179     return false;
180
181   // Look to see if our OptionalDef is defining CPSR or CCR.
182   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
183     const MachineOperand &MO = MI->getOperand(i);
184     if (!MO.isReg() || !MO.isDef()) continue;
185     if (MO.getReg() == ARM::CPSR)
186       *CPSR = true;
187   }
188   return true;
189 }
190
191 // If the machine is predicable go ahead and add the predicate operands, if
192 // it needs default CC operands add those.
193 const MachineInstrBuilder &
194 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
195   MachineInstr *MI = &*MIB;
196
197   // Do we use a predicate?
198   if (TII.isPredicable(MI))
199     AddDefaultPred(MIB);
200
201   // Do we optionally set a predicate?  Preds is size > 0 iff the predicate
202   // defines CPSR. All other OptionalDefines in ARM are the CCR register.
203   bool CPSR = false;
204   if (DefinesOptionalPredicate(MI, &CPSR)) {
205     if (CPSR)
206       AddDefaultT1CC(MIB);
207     else
208       AddDefaultCC(MIB);
209   }
210   return MIB;
211 }
212
213 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
214                                     const TargetRegisterClass* RC) {
215   unsigned ResultReg = createResultReg(RC);
216   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
217
218   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
219   return ResultReg;
220 }
221
222 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
223                                      const TargetRegisterClass *RC,
224                                      unsigned Op0, bool Op0IsKill) {
225   unsigned ResultReg = createResultReg(RC);
226   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
227
228   if (II.getNumDefs() >= 1)
229     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
230                    .addReg(Op0, Op0IsKill * RegState::Kill));
231   else {
232     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
233                    .addReg(Op0, Op0IsKill * RegState::Kill));
234     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
235                    TII.get(TargetOpcode::COPY), ResultReg)
236                    .addReg(II.ImplicitDefs[0]));
237   }
238   return ResultReg;
239 }
240
241 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
242                                       const TargetRegisterClass *RC,
243                                       unsigned Op0, bool Op0IsKill,
244                                       unsigned Op1, bool Op1IsKill) {
245   unsigned ResultReg = createResultReg(RC);
246   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
247
248   if (II.getNumDefs() >= 1)
249     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
250                    .addReg(Op0, Op0IsKill * RegState::Kill)
251                    .addReg(Op1, Op1IsKill * RegState::Kill));
252   else {
253     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
254                    .addReg(Op0, Op0IsKill * RegState::Kill)
255                    .addReg(Op1, Op1IsKill * RegState::Kill));
256     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
257                            TII.get(TargetOpcode::COPY), ResultReg)
258                    .addReg(II.ImplicitDefs[0]));
259   }
260   return ResultReg;
261 }
262
263 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
264                                       const TargetRegisterClass *RC,
265                                       unsigned Op0, bool Op0IsKill,
266                                       uint64_t Imm) {
267   unsigned ResultReg = createResultReg(RC);
268   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
269
270   if (II.getNumDefs() >= 1)
271     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
272                    .addReg(Op0, Op0IsKill * RegState::Kill)
273                    .addImm(Imm));
274   else {
275     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
276                    .addReg(Op0, Op0IsKill * RegState::Kill)
277                    .addImm(Imm));
278     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
279                            TII.get(TargetOpcode::COPY), ResultReg)
280                    .addReg(II.ImplicitDefs[0]));
281   }
282   return ResultReg;
283 }
284
285 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
286                                       const TargetRegisterClass *RC,
287                                       unsigned Op0, bool Op0IsKill,
288                                       const ConstantFP *FPImm) {
289   unsigned ResultReg = createResultReg(RC);
290   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
291
292   if (II.getNumDefs() >= 1)
293     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
294                    .addReg(Op0, Op0IsKill * RegState::Kill)
295                    .addFPImm(FPImm));
296   else {
297     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
298                    .addReg(Op0, Op0IsKill * RegState::Kill)
299                    .addFPImm(FPImm));
300     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
301                            TII.get(TargetOpcode::COPY), ResultReg)
302                    .addReg(II.ImplicitDefs[0]));
303   }
304   return ResultReg;
305 }
306
307 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
308                                        const TargetRegisterClass *RC,
309                                        unsigned Op0, bool Op0IsKill,
310                                        unsigned Op1, bool Op1IsKill,
311                                        uint64_t Imm) {
312   unsigned ResultReg = createResultReg(RC);
313   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
314
315   if (II.getNumDefs() >= 1)
316     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
317                    .addReg(Op0, Op0IsKill * RegState::Kill)
318                    .addReg(Op1, Op1IsKill * RegState::Kill)
319                    .addImm(Imm));
320   else {
321     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
322                    .addReg(Op0, Op0IsKill * RegState::Kill)
323                    .addReg(Op1, Op1IsKill * RegState::Kill)
324                    .addImm(Imm));
325     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
326                            TII.get(TargetOpcode::COPY), ResultReg)
327                    .addReg(II.ImplicitDefs[0]));
328   }
329   return ResultReg;
330 }
331
332 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
333                                      const TargetRegisterClass *RC,
334                                      uint64_t Imm) {
335   unsigned ResultReg = createResultReg(RC);
336   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
337
338   if (II.getNumDefs() >= 1)
339     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
340                    .addImm(Imm));
341   else {
342     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
343                    .addImm(Imm));
344     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
345                            TII.get(TargetOpcode::COPY), ResultReg)
346                    .addReg(II.ImplicitDefs[0]));
347   }
348   return ResultReg;
349 }
350
351 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
352                                                  unsigned Op0, bool Op0IsKill,
353                                                  uint32_t Idx) {
354   unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
355   assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
356          "Cannot yet extract from physregs");
357   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
358                          DL, TII.get(TargetOpcode::COPY), ResultReg)
359                  .addReg(Op0, getKillRegState(Op0IsKill), Idx));
360   return ResultReg;
361 }
362
363 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
364 // checks from the various callers.
365 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
366   if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
367
368   unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
369   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
370                           TII.get(ARM::VMOVRS), MoveReg)
371                   .addReg(SrcReg));
372   return MoveReg;
373 }
374
375 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
376   if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
377
378   unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
379   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
380                           TII.get(ARM::VMOVSR), MoveReg)
381                   .addReg(SrcReg));
382   return MoveReg;
383 }
384
385 // For double width floating point we need to materialize two constants
386 // (the high and the low) into integer registers then use a move to get
387 // the combined constant into an FP reg.
388 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
389   const APFloat Val = CFP->getValueAPF();
390   bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
391
392   // This checks to see if we can use VFP3 instructions to materialize
393   // a constant, otherwise we have to go through the constant pool.
394   if (TLI.isFPImmLegal(Val, VT)) {
395     unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
396     unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
397     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
398                             DestReg)
399                     .addFPImm(CFP));
400     return DestReg;
401   }
402
403   // Require VFP2 for loading fp constants.
404   if (!Subtarget->hasVFP2()) return false;
405
406   // MachineConstantPool wants an explicit alignment.
407   unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
408   if (Align == 0) {
409     // TODO: Figure out if this is correct.
410     Align = TD.getTypeAllocSize(CFP->getType());
411   }
412   unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
413   unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
414   unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
415
416   // The extra reg is for addrmode5.
417   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
418                           DestReg)
419                   .addConstantPoolIndex(Idx)
420                   .addReg(0));
421   return DestReg;
422 }
423
424 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
425
426   // For now 32-bit only.
427   if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
428
429   // MachineConstantPool wants an explicit alignment.
430   unsigned Align = TD.getPrefTypeAlignment(C->getType());
431   if (Align == 0) {
432     // TODO: Figure out if this is correct.
433     Align = TD.getTypeAllocSize(C->getType());
434   }
435   unsigned Idx = MCP.getConstantPoolIndex(C, Align);
436   unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
437
438   if (isThumb)
439     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
440                             TII.get(ARM::t2LDRpci), DestReg)
441                     .addConstantPoolIndex(Idx));
442   else
443     // The extra reg and immediate are for addrmode2.
444     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
445                             TII.get(ARM::LDRcp), DestReg)
446                     .addConstantPoolIndex(Idx)
447                     .addReg(0).addImm(0));
448
449   return DestReg;
450 }
451
452 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
453   // For now 32-bit only.
454   if (VT.getSimpleVT().SimpleTy != MVT::i32) return 0;
455
456   Reloc::Model RelocM = TM.getRelocationModel();
457
458   // TODO: No external globals for now.
459   if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
460
461   // TODO: Need more magic for ARM PIC.
462   if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
463
464   // MachineConstantPool wants an explicit alignment.
465   unsigned Align = TD.getPrefTypeAlignment(GV->getType());
466   if (Align == 0) {
467     // TODO: Figure out if this is correct.
468     Align = TD.getTypeAllocSize(GV->getType());
469   }
470
471   // Grab index.
472   unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
473   unsigned Id = AFI->createConstPoolEntryUId();
474   ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
475                                                        ARMCP::CPValue, PCAdj);
476   unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
477
478   // Load value.
479   MachineInstrBuilder MIB;
480   unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
481   if (isThumb) {
482     unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
483     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
484           .addConstantPoolIndex(Idx);
485     if (RelocM == Reloc::PIC_)
486       MIB.addImm(Id);
487   } else {
488     // The extra reg and immediate are for addrmode2.
489     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
490                   DestReg)
491           .addConstantPoolIndex(Idx)
492           .addReg(0).addImm(0);
493   }
494   AddOptionalDefs(MIB);
495   return DestReg;
496 }
497
498 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
499   EVT VT = TLI.getValueType(C->getType(), true);
500
501   // Only handle simple types.
502   if (!VT.isSimple()) return 0;
503
504   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
505     return ARMMaterializeFP(CFP, VT);
506   else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
507     return ARMMaterializeGV(GV, VT);
508   else if (isa<ConstantInt>(C))
509     return ARMMaterializeInt(C, VT);
510
511   return 0;
512 }
513
514 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
515   // Don't handle dynamic allocas.
516   if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
517
518   EVT VT;
519   if (!isTypeLegal(AI->getType(), VT)) return false;
520
521   DenseMap<const AllocaInst*, int>::iterator SI =
522     FuncInfo.StaticAllocaMap.find(AI);
523
524   // This will get lowered later into the correct offsets and registers
525   // via rewriteXFrameIndex.
526   if (SI != FuncInfo.StaticAllocaMap.end()) {
527     TargetRegisterClass* RC = TLI.getRegClassFor(VT);
528     unsigned ResultReg = createResultReg(RC);
529     unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
530     AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
531                             TII.get(Opc), ResultReg)
532                             .addFrameIndex(SI->second)
533                             .addImm(0));
534     return ResultReg;
535   }
536
537   return 0;
538 }
539
540 bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
541   VT = TLI.getValueType(Ty, true);
542
543   // Only handle simple types.
544   if (VT == MVT::Other || !VT.isSimple()) return false;
545
546   // Handle all legal types, i.e. a register that will directly hold this
547   // value.
548   return TLI.isTypeLegal(VT);
549 }
550
551 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
552   if (isTypeLegal(Ty, VT)) return true;
553
554   // If this is a type than can be sign or zero-extended to a basic operation
555   // go ahead and accept it now.
556   if (VT == MVT::i8 || VT == MVT::i16)
557     return true;
558
559   return false;
560 }
561
562 // Computes the Reg+Offset to get to an object.
563 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
564                                       int &Offset) {
565   // Some boilerplate from the X86 FastISel.
566   const User *U = NULL;
567   unsigned Opcode = Instruction::UserOp1;
568   if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
569     // Don't walk into other basic blocks; it's possible we haven't
570     // visited them yet, so the instructions may not yet be assigned
571     // virtual registers.
572     if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
573       return false;
574     Opcode = I->getOpcode();
575     U = I;
576   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
577     Opcode = C->getOpcode();
578     U = C;
579   }
580
581   if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
582     if (Ty->getAddressSpace() > 255)
583       // Fast instruction selection doesn't support the special
584       // address spaces.
585       return false;
586
587   switch (Opcode) {
588     default:
589     break;
590     case Instruction::BitCast: {
591       // Look through bitcasts.
592       return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
593     }
594     case Instruction::IntToPtr: {
595       // Look past no-op inttoptrs.
596       if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
597         return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
598       break;
599     }
600     case Instruction::PtrToInt: {
601       // Look past no-op ptrtoints.
602       if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
603         return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
604       break;
605     }
606     case Instruction::GetElementPtr: {
607       int SavedOffset = Offset;
608       unsigned SavedReg = Reg;
609       int TmpOffset = Offset;
610
611       // Iterate through the GEP folding the constants into offsets where
612       // we can.
613       gep_type_iterator GTI = gep_type_begin(U);
614       for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
615            i != e; ++i, ++GTI) {
616         const Value *Op = *i;
617         if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
618           const StructLayout *SL = TD.getStructLayout(STy);
619           unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
620           TmpOffset += SL->getElementOffset(Idx);
621         } else {
622           uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
623           SmallVector<const Value *, 4> Worklist;
624           Worklist.push_back(Op);
625           do {
626             Op = Worklist.pop_back_val();
627             if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
628               // Constant-offset addressing.
629               TmpOffset += CI->getSExtValue() * S;
630             } else if (0 && isa<AddOperator>(Op) &&
631                        isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
632               // An add with a constant operand. Fold the constant.
633               ConstantInt *CI =
634                 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
635               TmpOffset += CI->getSExtValue() * S;
636               // Add the other operand back to the work list.
637               Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
638             } else
639               goto unsupported_gep;
640           } while (!Worklist.empty());
641         }
642       }
643
644       // Try to grab the base operand now.
645       Offset = TmpOffset;
646       if (ARMComputeRegOffset(U->getOperand(0), Reg, Offset)) return true;
647
648       // We failed, restore everything and try the other options.
649       Offset = SavedOffset;
650       Reg = SavedReg;
651
652       unsupported_gep:
653       break;
654     }
655     case Instruction::Alloca: {
656       // TODO: Fix this to do intermediate loads, etc.
657       if (Offset != 0) return false;
658
659       const AllocaInst *AI = cast<AllocaInst>(Obj);
660       DenseMap<const AllocaInst*, int>::iterator SI =
661         FuncInfo.StaticAllocaMap.find(AI);
662       if (SI != FuncInfo.StaticAllocaMap.end()) {
663         Reg = ARM::SP;
664         Offset = SI->second;
665         return true;
666       }
667       // Don't handle dynamic allocas.
668       assert(!FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Obj)) &&
669              "Alloca should have been handled earlier!");
670       return false;
671     }
672   }
673
674   // Materialize the global variable's address into a reg which can
675   // then be used later to load the variable.
676   if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
677     unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
678     if (Tmp == 0) return false;
679
680     Reg = Tmp;
681     return true;
682   }
683
684   // Try to get this in a register if nothing else has worked.
685   if (Reg == 0) Reg = getRegForValue(Obj);
686   return Reg != 0;
687 }
688
689 void ARMFastISel::ARMSimplifyRegOffset(unsigned &Reg, int &Offset, EVT VT) {
690
691   // Since the offset may be too large for the load instruction
692   // get the reg+offset into a register.
693   if (Reg != ARM::SP && Offset != 0) {
694     ARMCC::CondCodes Pred = ARMCC::AL;
695     unsigned PredReg = 0;
696
697     TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
698       ARM::GPRRegisterClass;
699     unsigned BaseReg = createResultReg(RC);
700
701     if (!isThumb)
702       emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
703                               BaseReg, Reg, Offset, Pred, PredReg,
704                               static_cast<const ARMBaseInstrInfo&>(TII));
705     else {
706       assert(AFI->isThumb2Function());
707       emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
708                              BaseReg, Reg, Offset, Pred, PredReg,
709                              static_cast<const ARMBaseInstrInfo&>(TII));
710     }
711     Offset = 0;
712     Reg = BaseReg;
713   }
714 }
715
716 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
717                               unsigned Reg, int Offset) {
718
719   assert(VT.isSimple() && "Non-simple types are invalid here!");
720   unsigned Opc;
721   TargetRegisterClass *RC;
722   bool isFloat = false;
723   switch (VT.getSimpleVT().SimpleTy) {
724     default:
725       // This is mostly going to be Neon/vector support.
726       return false;
727     case MVT::i16:
728       Opc = isThumb ? ARM::t2LDRHi8 : ARM::LDRH;
729       RC = ARM::GPRRegisterClass;
730       VT = MVT::i32;
731       break;
732     case MVT::i8:
733       Opc = isThumb ? ARM::t2LDRBi8 : ARM::LDRB;
734       RC = ARM::GPRRegisterClass;
735       VT = MVT::i32;
736       break;
737     case MVT::i32:
738       Opc = isThumb ? ARM::t2LDRi8 : ARM::LDR;
739       RC = ARM::GPRRegisterClass;
740       break;
741     case MVT::f32:
742       Opc = ARM::VLDRS;
743       RC = TLI.getRegClassFor(VT);
744       isFloat = true;
745       break;
746     case MVT::f64:
747       Opc = ARM::VLDRD;
748       RC = TLI.getRegClassFor(VT);
749       isFloat = true;
750       break;
751   }
752
753   ResultReg = createResultReg(RC);
754
755   // For now with the additions above the offset should be zero - thus we
756   // can always fit into an i8.
757   assert((Reg == ARM::SP || Offset == 0) &&
758           "Offset not zero and not a stack load!");
759
760   if (Reg == ARM::SP)
761     TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
762                              ResultReg, Offset, RC,
763                              TM.getRegisterInfo());
764   // The thumb and floating point instructions both take 2 operands, ARM takes
765   // another register.
766   else if (isFloat || isThumb)
767     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
768                             TII.get(Opc), ResultReg)
769                     .addReg(Reg).addImm(Offset));
770   else
771     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
772                             TII.get(Opc), ResultReg)
773                     .addReg(Reg).addReg(0).addImm(Offset));
774   return true;
775 }
776
777 bool ARMFastISel::SelectLoad(const Instruction *I) {
778   // Verify we have a legal type before going any further.
779   EVT VT;
780   if (!isLoadTypeLegal(I->getType(), VT))
781     return false;
782
783   // Our register and offset with innocuous defaults.
784   unsigned Reg = 0;
785   int Offset = 0;
786
787   // See if we can handle this as Reg + Offset
788   if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
789     return false;
790
791   ARMSimplifyRegOffset(Reg, Offset, VT);
792
793   unsigned ResultReg;
794   if (!ARMEmitLoad(VT, ResultReg, Reg, Offset)) return false;
795
796   UpdateValueMap(I, ResultReg);
797   return true;
798 }
799
800 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
801                                unsigned DstReg, int Offset) {
802   unsigned StrOpc;
803   bool isFloat = false;
804   // VT is set here only for use in the alloca stores below - those are promoted
805   // to reg size always.
806   switch (VT.getSimpleVT().SimpleTy) {
807     default: return false;
808     case MVT::i1:
809     case MVT::i8:
810       VT = MVT::i32;
811       StrOpc = isThumb ? ARM::t2STRBi8 : ARM::STRB;
812       break;
813     case MVT::i16:
814       VT = MVT::i32;
815       StrOpc = isThumb ? ARM::t2STRHi8 : ARM::STRH;
816       break;
817     case MVT::i32: StrOpc = isThumb ? ARM::t2STRi8 : ARM::STR; break;
818     case MVT::f32:
819       if (!Subtarget->hasVFP2()) return false;
820       StrOpc = ARM::VSTRS;
821       isFloat = true;
822       break;
823     case MVT::f64:
824       if (!Subtarget->hasVFP2()) return false;
825       StrOpc = ARM::VSTRD;
826       isFloat = true;
827       break;
828   }
829
830   if (DstReg == ARM::SP)
831     TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
832                             SrcReg, true /*isKill*/, Offset,
833                             TLI.getRegClassFor(VT), TM.getRegisterInfo());
834   // The thumb addressing mode has operands swapped from the arm addressing
835   // mode, the floating point one only has two operands.
836   if (isFloat || isThumb)
837     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
838                             TII.get(StrOpc))
839                     .addReg(SrcReg).addReg(DstReg).addImm(Offset));
840   else
841     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
842                             TII.get(StrOpc))
843                     .addReg(SrcReg).addReg(DstReg).addReg(0).addImm(Offset));
844
845   return true;
846 }
847
848 bool ARMFastISel::SelectStore(const Instruction *I) {
849   Value *Op0 = I->getOperand(0);
850   unsigned SrcReg = 0;
851
852   // Yay type legalization
853   EVT VT;
854   if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
855     return false;
856
857   // Get the value to be stored into a register.
858   SrcReg = getRegForValue(Op0);
859   if (SrcReg == 0)
860     return false;
861
862   // Our register and offset with innocuous defaults.
863   unsigned Reg = 0;
864   int Offset = 0;
865
866   // See if we can handle this as Reg + Offset
867   if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
868     return false;
869
870   ARMSimplifyRegOffset(Reg, Offset, VT);
871
872   if (!ARMEmitStore(VT, SrcReg, Reg, Offset)) return false;
873
874   return true;
875 }
876
877 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
878   switch (Pred) {
879     // Needs two compares...
880     case CmpInst::FCMP_ONE:
881     case CmpInst::FCMP_UEQ:
882     default:
883       assert(false && "Unhandled CmpInst::Predicate!");
884       return ARMCC::AL;
885     case CmpInst::ICMP_EQ:
886     case CmpInst::FCMP_OEQ:
887       return ARMCC::EQ;
888     case CmpInst::ICMP_SGT:
889     case CmpInst::FCMP_OGT:
890       return ARMCC::GT;
891     case CmpInst::ICMP_SGE:
892     case CmpInst::FCMP_OGE:
893       return ARMCC::GE;
894     case CmpInst::ICMP_UGT:
895     case CmpInst::FCMP_UGT:
896       return ARMCC::HI;
897     case CmpInst::FCMP_OLT:
898       return ARMCC::MI;
899     case CmpInst::ICMP_ULE:
900     case CmpInst::FCMP_OLE:
901       return ARMCC::LS;
902     case CmpInst::FCMP_ORD:
903       return ARMCC::VC;
904     case CmpInst::FCMP_UNO:
905       return ARMCC::VS;
906     case CmpInst::FCMP_UGE:
907       return ARMCC::PL;
908     case CmpInst::ICMP_SLT:
909     case CmpInst::FCMP_ULT:
910       return ARMCC::LT;
911     case CmpInst::ICMP_SLE:
912     case CmpInst::FCMP_ULE:
913       return ARMCC::LE;
914     case CmpInst::FCMP_UNE:
915     case CmpInst::ICMP_NE:
916       return ARMCC::NE;
917     case CmpInst::ICMP_UGE:
918       return ARMCC::HS;
919     case CmpInst::ICMP_ULT:
920       return ARMCC::LO;
921   }
922 }
923
924 bool ARMFastISel::SelectBranch(const Instruction *I) {
925   const BranchInst *BI = cast<BranchInst>(I);
926   MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
927   MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
928
929   // Simple branch support.
930   // TODO: Try to avoid the re-computation in some places.
931   unsigned CondReg = getRegForValue(BI->getCondition());
932   if (CondReg == 0) return false;
933
934   // Re-set the flags just in case.
935   unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
936   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
937                   .addReg(CondReg).addImm(1));
938
939   unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
940   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
941                   .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
942   FastEmitBranch(FBB, DL);
943   FuncInfo.MBB->addSuccessor(TBB);
944   return true;
945 }
946
947 bool ARMFastISel::SelectCmp(const Instruction *I) {
948   const CmpInst *CI = cast<CmpInst>(I);
949
950   EVT VT;
951   const Type *Ty = CI->getOperand(0)->getType();
952   if (!isTypeLegal(Ty, VT))
953     return false;
954
955   bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
956   if (isFloat && !Subtarget->hasVFP2())
957     return false;
958
959   unsigned CmpOpc;
960   unsigned CondReg;
961   switch (VT.getSimpleVT().SimpleTy) {
962     default: return false;
963     // TODO: Verify compares.
964     case MVT::f32:
965       CmpOpc = ARM::VCMPES;
966       CondReg = ARM::FPSCR;
967       break;
968     case MVT::f64:
969       CmpOpc = ARM::VCMPED;
970       CondReg = ARM::FPSCR;
971       break;
972     case MVT::i32:
973       CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
974       CondReg = ARM::CPSR;
975       break;
976   }
977
978   // Get the compare predicate.
979   ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
980
981   // We may not handle every CC for now.
982   if (ARMPred == ARMCC::AL) return false;
983
984   unsigned Arg1 = getRegForValue(CI->getOperand(0));
985   if (Arg1 == 0) return false;
986
987   unsigned Arg2 = getRegForValue(CI->getOperand(1));
988   if (Arg2 == 0) return false;
989
990   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
991                   .addReg(Arg1).addReg(Arg2));
992
993   // For floating point we need to move the result to a comparison register
994   // that we can then use for branches.
995   if (isFloat)
996     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
997                             TII.get(ARM::FMSTAT)));
998
999   // Now set a register based on the comparison. Explicitly set the predicates
1000   // here.
1001   unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
1002   TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
1003                                     : ARM::GPRRegisterClass;
1004   unsigned DestReg = createResultReg(RC);
1005   Constant *Zero
1006     = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1007   unsigned ZeroReg = TargetMaterializeConstant(Zero);
1008   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1009           .addReg(ZeroReg).addImm(1)
1010           .addImm(ARMPred).addReg(CondReg);
1011
1012   UpdateValueMap(I, DestReg);
1013   return true;
1014 }
1015
1016 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1017   // Make sure we have VFP and that we're extending float to double.
1018   if (!Subtarget->hasVFP2()) return false;
1019
1020   Value *V = I->getOperand(0);
1021   if (!I->getType()->isDoubleTy() ||
1022       !V->getType()->isFloatTy()) return false;
1023
1024   unsigned Op = getRegForValue(V);
1025   if (Op == 0) return false;
1026
1027   unsigned Result = createResultReg(ARM::DPRRegisterClass);
1028   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1029                           TII.get(ARM::VCVTDS), Result)
1030                   .addReg(Op));
1031   UpdateValueMap(I, Result);
1032   return true;
1033 }
1034
1035 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1036   // Make sure we have VFP and that we're truncating double to float.
1037   if (!Subtarget->hasVFP2()) return false;
1038
1039   Value *V = I->getOperand(0);
1040   if (!(I->getType()->isFloatTy() &&
1041         V->getType()->isDoubleTy())) return false;
1042
1043   unsigned Op = getRegForValue(V);
1044   if (Op == 0) return false;
1045
1046   unsigned Result = createResultReg(ARM::SPRRegisterClass);
1047   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1048                           TII.get(ARM::VCVTSD), Result)
1049                   .addReg(Op));
1050   UpdateValueMap(I, Result);
1051   return true;
1052 }
1053
1054 bool ARMFastISel::SelectSIToFP(const Instruction *I) {
1055   // Make sure we have VFP.
1056   if (!Subtarget->hasVFP2()) return false;
1057
1058   EVT DstVT;
1059   const Type *Ty = I->getType();
1060   if (!isTypeLegal(Ty, DstVT))
1061     return false;
1062
1063   unsigned Op = getRegForValue(I->getOperand(0));
1064   if (Op == 0) return false;
1065
1066   // The conversion routine works on fp-reg to fp-reg and the operand above
1067   // was an integer, move it to the fp registers if possible.
1068   unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
1069   if (FP == 0) return false;
1070
1071   unsigned Opc;
1072   if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1073   else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1074   else return 0;
1075
1076   unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1077   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1078                           ResultReg)
1079                   .addReg(FP));
1080   UpdateValueMap(I, ResultReg);
1081   return true;
1082 }
1083
1084 bool ARMFastISel::SelectFPToSI(const Instruction *I) {
1085   // Make sure we have VFP.
1086   if (!Subtarget->hasVFP2()) return false;
1087
1088   EVT DstVT;
1089   const Type *RetTy = I->getType();
1090   if (!isTypeLegal(RetTy, DstVT))
1091     return false;
1092
1093   unsigned Op = getRegForValue(I->getOperand(0));
1094   if (Op == 0) return false;
1095
1096   unsigned Opc;
1097   const Type *OpTy = I->getOperand(0)->getType();
1098   if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1099   else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1100   else return 0;
1101
1102   // f64->s32 or f32->s32 both need an intermediate f32 reg.
1103   unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1104   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1105                           ResultReg)
1106                   .addReg(Op));
1107
1108   // This result needs to be in an integer register, but the conversion only
1109   // takes place in fp-regs.
1110   unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1111   if (IntReg == 0) return false;
1112
1113   UpdateValueMap(I, IntReg);
1114   return true;
1115 }
1116
1117 bool ARMFastISel::SelectSelect(const Instruction *I) {
1118   EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
1119   if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
1120     return false;
1121
1122   // Things need to be register sized for register moves.
1123   if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
1124   const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1125
1126   unsigned CondReg = getRegForValue(I->getOperand(0));
1127   if (CondReg == 0) return false;
1128   unsigned Op1Reg = getRegForValue(I->getOperand(1));
1129   if (Op1Reg == 0) return false;
1130   unsigned Op2Reg = getRegForValue(I->getOperand(2));
1131   if (Op2Reg == 0) return false;
1132
1133   unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1134   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1135                   .addReg(CondReg).addImm(1));
1136   unsigned ResultReg = createResultReg(RC);
1137   unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1138   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1139     .addReg(Op1Reg).addReg(Op2Reg)
1140     .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1141   UpdateValueMap(I, ResultReg);
1142   return true;
1143 }
1144
1145 bool ARMFastISel::SelectSDiv(const Instruction *I) {
1146   EVT VT;
1147   const Type *Ty = I->getType();
1148   if (!isTypeLegal(Ty, VT))
1149     return false;
1150
1151   // If we have integer div support we should have selected this automagically.
1152   // In case we have a real miss go ahead and return false and we'll pick
1153   // it up later.
1154   if (Subtarget->hasDivide()) return false;
1155
1156   // Otherwise emit a libcall.
1157   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1158   if (VT == MVT::i8)
1159     LC = RTLIB::SDIV_I8;
1160   else if (VT == MVT::i16)
1161     LC = RTLIB::SDIV_I16;
1162   else if (VT == MVT::i32)
1163     LC = RTLIB::SDIV_I32;
1164   else if (VT == MVT::i64)
1165     LC = RTLIB::SDIV_I64;
1166   else if (VT == MVT::i128)
1167     LC = RTLIB::SDIV_I128;
1168   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1169
1170   return ARMEmitLibcall(I, LC);
1171 }
1172
1173 bool ARMFastISel::SelectSRem(const Instruction *I) {
1174   EVT VT;
1175   const Type *Ty = I->getType();
1176   if (!isTypeLegal(Ty, VT))
1177     return false;
1178
1179   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1180   if (VT == MVT::i8)
1181     LC = RTLIB::SREM_I8;
1182   else if (VT == MVT::i16)
1183     LC = RTLIB::SREM_I16;
1184   else if (VT == MVT::i32)
1185     LC = RTLIB::SREM_I32;
1186   else if (VT == MVT::i64)
1187     LC = RTLIB::SREM_I64;
1188   else if (VT == MVT::i128)
1189     LC = RTLIB::SREM_I128;
1190   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1191
1192   return ARMEmitLibcall(I, LC);
1193 }
1194
1195 bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
1196   EVT VT  = TLI.getValueType(I->getType(), true);
1197
1198   // We can get here in the case when we want to use NEON for our fp
1199   // operations, but can't figure out how to. Just use the vfp instructions
1200   // if we have them.
1201   // FIXME: It'd be nice to use NEON instructions.
1202   const Type *Ty = I->getType();
1203   bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1204   if (isFloat && !Subtarget->hasVFP2())
1205     return false;
1206
1207   unsigned Op1 = getRegForValue(I->getOperand(0));
1208   if (Op1 == 0) return false;
1209
1210   unsigned Op2 = getRegForValue(I->getOperand(1));
1211   if (Op2 == 0) return false;
1212
1213   unsigned Opc;
1214   bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1215                  VT.getSimpleVT().SimpleTy == MVT::i64;
1216   switch (ISDOpcode) {
1217     default: return false;
1218     case ISD::FADD:
1219       Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1220       break;
1221     case ISD::FSUB:
1222       Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1223       break;
1224     case ISD::FMUL:
1225       Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1226       break;
1227   }
1228   unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1229   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1230                           TII.get(Opc), ResultReg)
1231                   .addReg(Op1).addReg(Op2));
1232   UpdateValueMap(I, ResultReg);
1233   return true;
1234 }
1235
1236 // Call Handling Code
1237
1238 // This is largely taken directly from CCAssignFnForNode - we don't support
1239 // varargs in FastISel so that part has been removed.
1240 // TODO: We may not support all of this.
1241 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1242   switch (CC) {
1243   default:
1244     llvm_unreachable("Unsupported calling convention");
1245   case CallingConv::C:
1246   case CallingConv::Fast:
1247     // Use target triple & subtarget features to do actual dispatch.
1248     if (Subtarget->isAAPCS_ABI()) {
1249       if (Subtarget->hasVFP2() &&
1250           FloatABIType == FloatABI::Hard)
1251         return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1252       else
1253         return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1254     } else
1255         return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1256   case CallingConv::ARM_AAPCS_VFP:
1257     return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1258   case CallingConv::ARM_AAPCS:
1259     return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1260   case CallingConv::ARM_APCS:
1261     return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1262   }
1263 }
1264
1265 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1266                                   SmallVectorImpl<unsigned> &ArgRegs,
1267                                   SmallVectorImpl<EVT> &ArgVTs,
1268                                   SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1269                                   SmallVectorImpl<unsigned> &RegArgs,
1270                                   CallingConv::ID CC,
1271                                   unsigned &NumBytes) {
1272   SmallVector<CCValAssign, 16> ArgLocs;
1273   CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1274   CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1275
1276   // Get a count of how many bytes are to be pushed on the stack.
1277   NumBytes = CCInfo.getNextStackOffset();
1278
1279   // Issue CALLSEQ_START
1280   unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1281   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1282                           TII.get(AdjStackDown))
1283                   .addImm(NumBytes));
1284
1285   // Process the args.
1286   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1287     CCValAssign &VA = ArgLocs[i];
1288     unsigned Arg = ArgRegs[VA.getValNo()];
1289     EVT ArgVT = ArgVTs[VA.getValNo()];
1290
1291     // Handle arg promotion, etc.
1292     switch (VA.getLocInfo()) {
1293       case CCValAssign::Full: break;
1294       default:
1295       // TODO: Handle arg promotion.
1296       return false;
1297     }
1298
1299     // Now copy/store arg to correct locations.
1300     // TODO: We need custom lowering for f64 args.
1301     if (VA.isRegLoc() && !VA.needsCustom()) {
1302       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1303               VA.getLocReg())
1304       .addReg(Arg);
1305       RegArgs.push_back(VA.getLocReg());
1306     } else {
1307       // Need to store
1308       return false;
1309     }
1310   }
1311
1312   return true;
1313 }
1314
1315 bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1316                              const Instruction *I, CallingConv::ID CC,
1317                              unsigned &NumBytes) {
1318   // Issue CALLSEQ_END
1319   unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1320   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1321                           TII.get(AdjStackUp))
1322                   .addImm(NumBytes).addImm(0));
1323
1324   // Now the return value.
1325   if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1326     SmallVector<CCValAssign, 16> RVLocs;
1327     CCState CCInfo(CC, false, TM, RVLocs, *Context);
1328     CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1329
1330     // Copy all of the result registers out of their specified physreg.
1331     if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1332       // For this move we copy into two registers and then move into the
1333       // double fp reg we want.
1334       // TODO: Are the copies necessary?
1335       TargetRegisterClass *CopyRC = TLI.getRegClassFor(MVT::i32);
1336       unsigned Copy1 = createResultReg(CopyRC);
1337       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1338               Copy1).addReg(RVLocs[0].getLocReg());
1339       UsedRegs.push_back(RVLocs[0].getLocReg());
1340
1341       unsigned Copy2 = createResultReg(CopyRC);
1342       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1343               Copy2).addReg(RVLocs[1].getLocReg());
1344       UsedRegs.push_back(RVLocs[1].getLocReg());
1345
1346       EVT DestVT = RVLocs[0].getValVT();
1347       TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1348       unsigned ResultReg = createResultReg(DstRC);
1349       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1350                               TII.get(ARM::VMOVDRR), ResultReg)
1351                       .addReg(Copy1).addReg(Copy2));
1352
1353       // Finally update the result.
1354       UpdateValueMap(I, ResultReg);
1355     } else {
1356       assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
1357       EVT CopyVT = RVLocs[0].getValVT();
1358       TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1359
1360       unsigned ResultReg = createResultReg(DstRC);
1361       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1362               ResultReg).addReg(RVLocs[0].getLocReg());
1363       UsedRegs.push_back(RVLocs[0].getLocReg());
1364
1365       // Finally update the result.
1366       UpdateValueMap(I, ResultReg);
1367     }
1368   }
1369
1370   return true;
1371 }
1372
1373 // A quick function that will emit a call for a named libcall in F with the
1374 // vector of passed arguments for the Instruction in I. We can assume that we
1375 // can emit a call for any libcall we can produce. This is an abridged version
1376 // of the full call infrastructure since we won't need to worry about things
1377 // like computed function pointers or strange arguments at call sites.
1378 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
1379 // with X86.
1380 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1381   CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1382
1383   // Handle *simple* calls for now.
1384   const Type *RetTy = I->getType();
1385   EVT RetVT;
1386   if (RetTy->isVoidTy())
1387     RetVT = MVT::isVoid;
1388   else if (!isTypeLegal(RetTy, RetVT))
1389     return false;
1390
1391   // For now we're using BLX etc on the assumption that we have v5t ops.
1392   if (!Subtarget->hasV5TOps()) return false;
1393
1394   // Set up the argument vectors.
1395   SmallVector<Value*, 8> Args;
1396   SmallVector<unsigned, 8> ArgRegs;
1397   SmallVector<EVT, 8> ArgVTs;
1398   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1399   Args.reserve(I->getNumOperands());
1400   ArgRegs.reserve(I->getNumOperands());
1401   ArgVTs.reserve(I->getNumOperands());
1402   ArgFlags.reserve(I->getNumOperands());
1403   for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1404     Value *Op = I->getOperand(i);
1405     unsigned Arg = getRegForValue(Op);
1406     if (Arg == 0) return false;
1407
1408     const Type *ArgTy = Op->getType();
1409     EVT ArgVT;
1410     if (!isTypeLegal(ArgTy, ArgVT)) return false;
1411
1412     ISD::ArgFlagsTy Flags;
1413     unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1414     Flags.setOrigAlign(OriginalAlignment);
1415
1416     Args.push_back(Op);
1417     ArgRegs.push_back(Arg);
1418     ArgVTs.push_back(ArgVT);
1419     ArgFlags.push_back(Flags);
1420   }
1421
1422   // Handle the arguments now that we've gotten them.
1423   SmallVector<unsigned, 4> RegArgs;
1424   unsigned NumBytes;
1425   if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1426     return false;
1427
1428   // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1429   // TODO: Turn this into the table of arm call ops.
1430   MachineInstrBuilder MIB;
1431   unsigned CallOpc;
1432   if(isThumb)
1433     CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1434   else
1435     CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1436   MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1437         .addExternalSymbol(TLI.getLibcallName(Call));
1438
1439   // Add implicit physical register uses to the call.
1440   for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1441     MIB.addReg(RegArgs[i]);
1442
1443   // Finish off the call including any return values.
1444   SmallVector<unsigned, 4> UsedRegs;
1445   if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1446
1447   // Set all unused physreg defs as dead.
1448   static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1449
1450   return true;
1451 }
1452
1453 bool ARMFastISel::SelectCall(const Instruction *I) {
1454   const CallInst *CI = cast<CallInst>(I);
1455   const Value *Callee = CI->getCalledValue();
1456
1457   // Can't handle inline asm or worry about intrinsics yet.
1458   if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1459
1460   // Only handle global variable Callees that are direct calls.
1461   const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1462   if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1463     return false;
1464
1465   // Check the calling convention.
1466   ImmutableCallSite CS(CI);
1467   CallingConv::ID CC = CS.getCallingConv();
1468   // TODO: Avoid some calling conventions?
1469   if (CC != CallingConv::C) {
1470     // errs() << "Can't handle calling convention: " << CC << "\n";
1471     return false;
1472   }
1473
1474   // Let SDISel handle vararg functions.
1475   const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1476   const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1477   if (FTy->isVarArg())
1478     return false;
1479
1480   // Handle *simple* calls for now.
1481   const Type *RetTy = I->getType();
1482   EVT RetVT;
1483   if (RetTy->isVoidTy())
1484     RetVT = MVT::isVoid;
1485   else if (!isTypeLegal(RetTy, RetVT))
1486     return false;
1487
1488   // For now we're using BLX etc on the assumption that we have v5t ops.
1489   // TODO: Maybe?
1490   if (!Subtarget->hasV5TOps()) return false;
1491
1492   // Set up the argument vectors.
1493   SmallVector<Value*, 8> Args;
1494   SmallVector<unsigned, 8> ArgRegs;
1495   SmallVector<EVT, 8> ArgVTs;
1496   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1497   Args.reserve(CS.arg_size());
1498   ArgRegs.reserve(CS.arg_size());
1499   ArgVTs.reserve(CS.arg_size());
1500   ArgFlags.reserve(CS.arg_size());
1501   for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1502        i != e; ++i) {
1503     unsigned Arg = getRegForValue(*i);
1504
1505     if (Arg == 0)
1506       return false;
1507     ISD::ArgFlagsTy Flags;
1508     unsigned AttrInd = i - CS.arg_begin() + 1;
1509     if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1510       Flags.setSExt();
1511     if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1512       Flags.setZExt();
1513
1514          // FIXME: Only handle *easy* calls for now.
1515     if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1516         CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1517         CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1518         CS.paramHasAttr(AttrInd, Attribute::ByVal))
1519       return false;
1520
1521     const Type *ArgTy = (*i)->getType();
1522     EVT ArgVT;
1523     if (!isTypeLegal(ArgTy, ArgVT))
1524       return false;
1525     unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1526     Flags.setOrigAlign(OriginalAlignment);
1527
1528     Args.push_back(*i);
1529     ArgRegs.push_back(Arg);
1530     ArgVTs.push_back(ArgVT);
1531     ArgFlags.push_back(Flags);
1532   }
1533
1534   // Handle the arguments now that we've gotten them.
1535   SmallVector<unsigned, 4> RegArgs;
1536   unsigned NumBytes;
1537   if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1538     return false;
1539
1540   // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1541   // TODO: Turn this into the table of arm call ops.
1542   MachineInstrBuilder MIB;
1543   unsigned CallOpc;
1544   if(isThumb)
1545     CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1546   else
1547     CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1548   MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1549               .addGlobalAddress(GV, 0, 0);
1550
1551   // Add implicit physical register uses to the call.
1552   for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1553     MIB.addReg(RegArgs[i]);
1554
1555   // Finish off the call including any return values.
1556   SmallVector<unsigned, 4> UsedRegs;
1557   if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1558
1559   // Set all unused physreg defs as dead.
1560   static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1561
1562   return true;
1563
1564 }
1565
1566 // TODO: SoftFP support.
1567 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
1568   // No Thumb-1 for now.
1569   if (isThumb && !AFI->isThumb2Function()) return false;
1570
1571   switch (I->getOpcode()) {
1572     case Instruction::Load:
1573       return SelectLoad(I);
1574     case Instruction::Store:
1575       return SelectStore(I);
1576     case Instruction::Br:
1577       return SelectBranch(I);
1578     case Instruction::ICmp:
1579     case Instruction::FCmp:
1580       return SelectCmp(I);
1581     case Instruction::FPExt:
1582       return SelectFPExt(I);
1583     case Instruction::FPTrunc:
1584       return SelectFPTrunc(I);
1585     case Instruction::SIToFP:
1586       return SelectSIToFP(I);
1587     case Instruction::FPToSI:
1588       return SelectFPToSI(I);
1589     case Instruction::FAdd:
1590       return SelectBinaryOp(I, ISD::FADD);
1591     case Instruction::FSub:
1592       return SelectBinaryOp(I, ISD::FSUB);
1593     case Instruction::FMul:
1594       return SelectBinaryOp(I, ISD::FMUL);
1595     case Instruction::SDiv:
1596       return SelectSDiv(I);
1597     case Instruction::SRem:
1598       return SelectSRem(I);
1599     case Instruction::Call:
1600       return SelectCall(I);
1601     case Instruction::Select:
1602       return SelectSelect(I);
1603     default: break;
1604   }
1605   return false;
1606 }
1607
1608 namespace llvm {
1609   llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
1610     // Completely untested on non-darwin.
1611     const TargetMachine &TM = funcInfo.MF->getTarget();
1612     const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
1613     if (Subtarget->isTargetDarwin() && EnableARMFastISel)
1614       return new ARMFastISel(funcInfo);
1615     return 0;
1616   }
1617 }