Fit in 80 columns
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAGISel.cpp
1 //===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the SelectionDAGISel class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "isel"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/CodeGen/SelectionDAGISel.h"
17 #include "llvm/CodeGen/ScheduleDAG.h"
18 #include "llvm/CallingConv.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Function.h"
22 #include "llvm/GlobalVariable.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Intrinsics.h"
26 #include "llvm/IntrinsicInst.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineJumpTableInfo.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/SchedulerRegistry.h"
33 #include "llvm/CodeGen/SelectionDAG.h"
34 #include "llvm/CodeGen/SSARegMap.h"
35 #include "llvm/Target/MRegisterInfo.h"
36 #include "llvm/Target/TargetAsmInfo.h"
37 #include "llvm/Target/TargetData.h"
38 #include "llvm/Target/TargetFrameInfo.h"
39 #include "llvm/Target/TargetInstrInfo.h"
40 #include "llvm/Target/TargetLowering.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetOptions.h"
43 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
44 #include "llvm/Support/MathExtras.h"
45 #include "llvm/Support/Debug.h"
46 #include "llvm/Support/Compiler.h"
47 #include <algorithm>
48 using namespace llvm;
49
50 #ifndef NDEBUG
51 static cl::opt<bool>
52 ViewISelDAGs("view-isel-dags", cl::Hidden,
53           cl::desc("Pop up a window to show isel dags as they are selected"));
54 static cl::opt<bool>
55 ViewSchedDAGs("view-sched-dags", cl::Hidden,
56           cl::desc("Pop up a window to show sched dags as they are processed"));
57 #else
58 static const bool ViewISelDAGs = 0, ViewSchedDAGs = 0;
59 #endif
60
61
62 //===---------------------------------------------------------------------===//
63 ///
64 /// RegisterScheduler class - Track the registration of instruction schedulers.
65 ///
66 //===---------------------------------------------------------------------===//
67 MachinePassRegistry RegisterScheduler::Registry;
68
69 //===---------------------------------------------------------------------===//
70 ///
71 /// ISHeuristic command line option for instruction schedulers.
72 ///
73 //===---------------------------------------------------------------------===//
74 namespace {
75   cl::opt<RegisterScheduler::FunctionPassCtor, false,
76           RegisterPassParser<RegisterScheduler> >
77   ISHeuristic("sched",
78               cl::init(&createDefaultScheduler),
79               cl::desc("Instruction schedulers available:"));
80
81   static RegisterScheduler
82   defaultListDAGScheduler("default", "  Best scheduler for the target",
83                           createDefaultScheduler);
84 } // namespace
85
86 namespace {
87   /// RegsForValue - This struct represents the physical registers that a
88   /// particular value is assigned and the type information about the value.
89   /// This is needed because values can be promoted into larger registers and
90   /// expanded into multiple smaller registers than the value.
91   struct VISIBILITY_HIDDEN RegsForValue {
92     /// Regs - This list hold the register (for legal and promoted values)
93     /// or register set (for expanded values) that the value should be assigned
94     /// to.
95     std::vector<unsigned> Regs;
96     
97     /// RegVT - The value type of each register.
98     ///
99     MVT::ValueType RegVT;
100     
101     /// ValueVT - The value type of the LLVM value, which may be promoted from
102     /// RegVT or made from merging the two expanded parts.
103     MVT::ValueType ValueVT;
104     
105     RegsForValue() : RegVT(MVT::Other), ValueVT(MVT::Other) {}
106     
107     RegsForValue(unsigned Reg, MVT::ValueType regvt, MVT::ValueType valuevt)
108       : RegVT(regvt), ValueVT(valuevt) {
109         Regs.push_back(Reg);
110     }
111     RegsForValue(const std::vector<unsigned> &regs, 
112                  MVT::ValueType regvt, MVT::ValueType valuevt)
113       : Regs(regs), RegVT(regvt), ValueVT(valuevt) {
114     }
115     
116     /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
117     /// this value and returns the result as a ValueVT value.  This uses 
118     /// Chain/Flag as the input and updates them for the output Chain/Flag.
119     SDOperand getCopyFromRegs(SelectionDAG &DAG,
120                               SDOperand &Chain, SDOperand &Flag) const;
121
122     /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
123     /// specified value into the registers specified by this object.  This uses 
124     /// Chain/Flag as the input and updates them for the output Chain/Flag.
125     void getCopyToRegs(SDOperand Val, SelectionDAG &DAG,
126                        SDOperand &Chain, SDOperand &Flag,
127                        MVT::ValueType PtrVT) const;
128     
129     /// AddInlineAsmOperands - Add this value to the specified inlineasm node
130     /// operand list.  This adds the code marker and includes the number of 
131     /// values added into it.
132     void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
133                               std::vector<SDOperand> &Ops) const;
134   };
135 }
136
137 namespace llvm {
138   //===--------------------------------------------------------------------===//
139   /// createDefaultScheduler - This creates an instruction scheduler appropriate
140   /// for the target.
141   ScheduleDAG* createDefaultScheduler(SelectionDAGISel *IS,
142                                       SelectionDAG *DAG,
143                                       MachineBasicBlock *BB) {
144     TargetLowering &TLI = IS->getTargetLowering();
145     
146     if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency) {
147       return createTDListDAGScheduler(IS, DAG, BB);
148     } else {
149       assert(TLI.getSchedulingPreference() ==
150            TargetLowering::SchedulingForRegPressure && "Unknown sched type!");
151       return createBURRListDAGScheduler(IS, DAG, BB);
152     }
153   }
154
155
156   //===--------------------------------------------------------------------===//
157   /// FunctionLoweringInfo - This contains information that is global to a
158   /// function that is used when lowering a region of the function.
159   class FunctionLoweringInfo {
160   public:
161     TargetLowering &TLI;
162     Function &Fn;
163     MachineFunction &MF;
164     SSARegMap *RegMap;
165
166     FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF);
167
168     /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
169     std::map<const BasicBlock*, MachineBasicBlock *> MBBMap;
170
171     /// ValueMap - Since we emit code for the function a basic block at a time,
172     /// we must remember which virtual registers hold the values for
173     /// cross-basic-block values.
174     std::map<const Value*, unsigned> ValueMap;
175
176     /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
177     /// the entry block.  This allows the allocas to be efficiently referenced
178     /// anywhere in the function.
179     std::map<const AllocaInst*, int> StaticAllocaMap;
180
181     unsigned MakeReg(MVT::ValueType VT) {
182       return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
183     }
184     
185     /// isExportedInst - Return true if the specified value is an instruction
186     /// exported from its block.
187     bool isExportedInst(const Value *V) {
188       return ValueMap.count(V);
189     }
190
191     unsigned CreateRegForValue(const Value *V);
192     
193     unsigned InitializeRegForValue(const Value *V) {
194       unsigned &R = ValueMap[V];
195       assert(R == 0 && "Already initialized this value register!");
196       return R = CreateRegForValue(V);
197     }
198   };
199 }
200
201 /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
202 /// PHI nodes or outside of the basic block that defines it, or used by a 
203 /// switch instruction, which may expand to multiple basic blocks.
204 static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
205   if (isa<PHINode>(I)) return true;
206   BasicBlock *BB = I->getParent();
207   for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
208     if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI) ||
209         // FIXME: Remove switchinst special case.
210         isa<SwitchInst>(*UI))
211       return true;
212   return false;
213 }
214
215 /// isOnlyUsedInEntryBlock - If the specified argument is only used in the
216 /// entry block, return true.  This includes arguments used by switches, since
217 /// the switch may expand into multiple basic blocks.
218 static bool isOnlyUsedInEntryBlock(Argument *A) {
219   BasicBlock *Entry = A->getParent()->begin();
220   for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI)
221     if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI))
222       return false;  // Use not in entry block.
223   return true;
224 }
225
226 FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli,
227                                            Function &fn, MachineFunction &mf)
228     : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) {
229
230   // Create a vreg for each argument register that is not dead and is used
231   // outside of the entry block for the function.
232   for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end();
233        AI != E; ++AI)
234     if (!isOnlyUsedInEntryBlock(AI))
235       InitializeRegForValue(AI);
236
237   // Initialize the mapping of values to registers.  This is only set up for
238   // instruction values that are used outside of the block that defines
239   // them.
240   Function::iterator BB = Fn.begin(), EB = Fn.end();
241   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
242     if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
243       if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
244         const Type *Ty = AI->getAllocatedType();
245         uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
246         unsigned Align = 
247           std::max((unsigned)TLI.getTargetData()->getTypeAlignmentPref(Ty),
248                    AI->getAlignment());
249
250         TySize *= CUI->getZExtValue();   // Get total allocated size.
251         if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
252         StaticAllocaMap[AI] =
253           MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
254       }
255
256   for (; BB != EB; ++BB)
257     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
258       if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
259         if (!isa<AllocaInst>(I) ||
260             !StaticAllocaMap.count(cast<AllocaInst>(I)))
261           InitializeRegForValue(I);
262
263   // Create an initial MachineBasicBlock for each LLVM BasicBlock in F.  This
264   // also creates the initial PHI MachineInstrs, though none of the input
265   // operands are populated.
266   for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) {
267     MachineBasicBlock *MBB = new MachineBasicBlock(BB);
268     MBBMap[BB] = MBB;
269     MF.getBasicBlockList().push_back(MBB);
270
271     // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
272     // appropriate.
273     PHINode *PN;
274     for (BasicBlock::iterator I = BB->begin();(PN = dyn_cast<PHINode>(I)); ++I){
275       if (PN->use_empty()) continue;
276       
277       MVT::ValueType VT = TLI.getValueType(PN->getType());
278       unsigned NumElements;
279       if (VT != MVT::Vector)
280         NumElements = TLI.getNumElements(VT);
281       else {
282         MVT::ValueType VT1,VT2;
283         NumElements = 
284           TLI.getPackedTypeBreakdown(cast<PackedType>(PN->getType()),
285                                      VT1, VT2);
286       }
287       unsigned PHIReg = ValueMap[PN];
288       assert(PHIReg && "PHI node does not have an assigned virtual register!");
289       const TargetInstrInfo *TII = TLI.getTargetMachine().getInstrInfo();
290       for (unsigned i = 0; i != NumElements; ++i)
291         BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i);
292     }
293   }
294 }
295
296 /// CreateRegForValue - Allocate the appropriate number of virtual registers of
297 /// the correctly promoted or expanded types.  Assign these registers
298 /// consecutive vreg numbers and return the first assigned number.
299 unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) {
300   MVT::ValueType VT = TLI.getValueType(V->getType());
301   
302   // The number of multiples of registers that we need, to, e.g., split up
303   // a <2 x int64> -> 4 x i32 registers.
304   unsigned NumVectorRegs = 1;
305   
306   // If this is a packed type, figure out what type it will decompose into
307   // and how many of the elements it will use.
308   if (VT == MVT::Vector) {
309     const PackedType *PTy = cast<PackedType>(V->getType());
310     unsigned NumElts = PTy->getNumElements();
311     MVT::ValueType EltTy = TLI.getValueType(PTy->getElementType());
312     
313     // Divide the input until we get to a supported size.  This will always
314     // end with a scalar if the target doesn't support vectors.
315     while (NumElts > 1 && !TLI.isTypeLegal(getVectorType(EltTy, NumElts))) {
316       NumElts >>= 1;
317       NumVectorRegs <<= 1;
318     }
319     if (NumElts == 1)
320       VT = EltTy;
321     else
322       VT = getVectorType(EltTy, NumElts);
323   }
324   
325   // The common case is that we will only create one register for this
326   // value.  If we have that case, create and return the virtual register.
327   unsigned NV = TLI.getNumElements(VT);
328   if (NV == 1) {
329     // If we are promoting this value, pick the next largest supported type.
330     MVT::ValueType PromotedType = TLI.getTypeToTransformTo(VT);
331     unsigned Reg = MakeReg(PromotedType);
332     // If this is a vector of supported or promoted types (e.g. 4 x i16),
333     // create all of the registers.
334     for (unsigned i = 1; i != NumVectorRegs; ++i)
335       MakeReg(PromotedType);
336     return Reg;
337   }
338   
339   // If this value is represented with multiple target registers, make sure
340   // to create enough consecutive registers of the right (smaller) type.
341   VT = TLI.getTypeToExpandTo(VT);
342   unsigned R = MakeReg(VT);
343   for (unsigned i = 1; i != NV*NumVectorRegs; ++i)
344     MakeReg(VT);
345   return R;
346 }
347
348 //===----------------------------------------------------------------------===//
349 /// SelectionDAGLowering - This is the common target-independent lowering
350 /// implementation that is parameterized by a TargetLowering object.
351 /// Also, targets can overload any lowering method.
352 ///
353 namespace llvm {
354 class SelectionDAGLowering {
355   MachineBasicBlock *CurMBB;
356
357   std::map<const Value*, SDOperand> NodeMap;
358
359   /// PendingLoads - Loads are not emitted to the program immediately.  We bunch
360   /// them up and then emit token factor nodes when possible.  This allows us to
361   /// get simple disambiguation between loads without worrying about alias
362   /// analysis.
363   std::vector<SDOperand> PendingLoads;
364
365   /// Case - A pair of values to record the Value for a switch case, and the
366   /// case's target basic block.  
367   typedef std::pair<Constant*, MachineBasicBlock*> Case;
368   typedef std::vector<Case>::iterator              CaseItr;
369   typedef std::pair<CaseItr, CaseItr>              CaseRange;
370
371   /// CaseRec - A struct with ctor used in lowering switches to a binary tree
372   /// of conditional branches.
373   struct CaseRec {
374     CaseRec(MachineBasicBlock *bb, Constant *lt, Constant *ge, CaseRange r) :
375     CaseBB(bb), LT(lt), GE(ge), Range(r) {}
376
377     /// CaseBB - The MBB in which to emit the compare and branch
378     MachineBasicBlock *CaseBB;
379     /// LT, GE - If nonzero, we know the current case value must be less-than or
380     /// greater-than-or-equal-to these Constants.
381     Constant *LT;
382     Constant *GE;
383     /// Range - A pair of iterators representing the range of case values to be
384     /// processed at this point in the binary search tree.
385     CaseRange Range;
386   };
387   
388   /// The comparison function for sorting Case values.
389   struct CaseCmp {
390     bool operator () (const Case& C1, const Case& C2) {
391       assert(isa<ConstantInt>(C1.first) && isa<ConstantInt>(C2.first));
392       return cast<const ConstantInt>(C1.first)->getSExtValue() <
393         cast<const ConstantInt>(C2.first)->getSExtValue();
394     }
395   };
396   
397 public:
398   // TLI - This is information that describes the available target features we
399   // need for lowering.  This indicates when operations are unavailable,
400   // implemented with a libcall, etc.
401   TargetLowering &TLI;
402   SelectionDAG &DAG;
403   const TargetData *TD;
404
405   /// SwitchCases - Vector of CaseBlock structures used to communicate
406   /// SwitchInst code generation information.
407   std::vector<SelectionDAGISel::CaseBlock> SwitchCases;
408   SelectionDAGISel::JumpTable JT;
409   
410   /// FuncInfo - Information about the function as a whole.
411   ///
412   FunctionLoweringInfo &FuncInfo;
413
414   SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli,
415                        FunctionLoweringInfo &funcinfo)
416     : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()),
417       JT(0,0,0,0), FuncInfo(funcinfo) {
418   }
419
420   /// getRoot - Return the current virtual root of the Selection DAG.
421   ///
422   SDOperand getRoot() {
423     if (PendingLoads.empty())
424       return DAG.getRoot();
425
426     if (PendingLoads.size() == 1) {
427       SDOperand Root = PendingLoads[0];
428       DAG.setRoot(Root);
429       PendingLoads.clear();
430       return Root;
431     }
432
433     // Otherwise, we have to make a token factor node.
434     SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
435                                  &PendingLoads[0], PendingLoads.size());
436     PendingLoads.clear();
437     DAG.setRoot(Root);
438     return Root;
439   }
440
441   SDOperand CopyValueToVirtualRegister(Value *V, unsigned Reg);
442
443   void visit(Instruction &I) { visit(I.getOpcode(), I); }
444
445   void visit(unsigned Opcode, User &I) {
446     // Note: this doesn't use InstVisitor, because it has to work with
447     // ConstantExpr's in addition to instructions.
448     switch (Opcode) {
449     default: assert(0 && "Unknown instruction type encountered!");
450              abort();
451       // Build the switch statement using the Instruction.def file.
452 #define HANDLE_INST(NUM, OPCODE, CLASS) \
453     case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
454 #include "llvm/Instruction.def"
455     }
456   }
457
458   void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; }
459
460   SDOperand getLoadFrom(const Type *Ty, SDOperand Ptr,
461                         const Value *SV, SDOperand Root,
462                         bool isVolatile);
463
464   SDOperand getIntPtrConstant(uint64_t Val) {
465     return DAG.getConstant(Val, TLI.getPointerTy());
466   }
467
468   SDOperand getValue(const Value *V);
469
470   const SDOperand &setValue(const Value *V, SDOperand NewN) {
471     SDOperand &N = NodeMap[V];
472     assert(N.Val == 0 && "Already set a value for this node!");
473     return N = NewN;
474   }
475   
476   RegsForValue GetRegistersForValue(const std::string &ConstrCode,
477                                     MVT::ValueType VT,
478                                     bool OutReg, bool InReg,
479                                     std::set<unsigned> &OutputRegs, 
480                                     std::set<unsigned> &InputRegs);
481
482   void FindMergedConditions(Value *Cond, MachineBasicBlock *TBB,
483                             MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
484                             unsigned Opc);
485   bool isExportableFromCurrentBlock(Value *V, const BasicBlock *FromBB);
486   void ExportFromCurrentBlock(Value *V);
487     
488   // Terminator instructions.
489   void visitRet(ReturnInst &I);
490   void visitBr(BranchInst &I);
491   void visitSwitch(SwitchInst &I);
492   void visitUnreachable(UnreachableInst &I) { /* noop */ }
493
494   // Helper for visitSwitch
495   void visitSwitchCase(SelectionDAGISel::CaseBlock &CB);
496   void visitJumpTable(SelectionDAGISel::JumpTable &JT);
497   
498   // These all get lowered before this pass.
499   void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); }
500   void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); }
501
502   void visitScalarBinary(User &I, unsigned OpCode);
503   void visitVectorBinary(User &I, unsigned OpCode);
504   void visitEitherBinary(User &I, unsigned ScalarOp, unsigned VectorOp);
505   void visitShift(User &I, unsigned Opcode);
506   void visitAdd(User &I) { 
507     if (isa<PackedType>(I.getType()))
508       visitVectorBinary(I, ISD::VADD);
509     else if (I.getType()->isFloatingPoint())
510       visitScalarBinary(I, ISD::FADD);
511     else
512       visitScalarBinary(I, ISD::ADD);
513   }
514   void visitSub(User &I);
515   void visitMul(User &I) {
516     if (isa<PackedType>(I.getType()))
517       visitVectorBinary(I, ISD::VMUL);
518     else if (I.getType()->isFloatingPoint())
519       visitScalarBinary(I, ISD::FMUL);
520     else
521       visitScalarBinary(I, ISD::MUL);
522   }
523   void visitURem(User &I) { visitScalarBinary(I, ISD::UREM); }
524   void visitSRem(User &I) { visitScalarBinary(I, ISD::SREM); }
525   void visitFRem(User &I) { visitScalarBinary(I, ISD::FREM); }
526   void visitUDiv(User &I) { visitEitherBinary(I, ISD::UDIV, ISD::VUDIV); }
527   void visitSDiv(User &I) { visitEitherBinary(I, ISD::SDIV, ISD::VSDIV); }
528   void visitFDiv(User &I) { visitEitherBinary(I, ISD::FDIV, ISD::VSDIV); }
529   void visitAnd (User &I) { visitEitherBinary(I, ISD::AND,  ISD::VAND ); }
530   void visitOr  (User &I) { visitEitherBinary(I, ISD::OR,   ISD::VOR  ); }
531   void visitXor (User &I) { visitEitherBinary(I, ISD::XOR,  ISD::VXOR ); }
532   void visitShl (User &I) { visitShift(I, ISD::SHL); }
533   void visitLShr(User &I) { visitShift(I, ISD::SRL); }
534   void visitAShr(User &I) { visitShift(I, ISD::SRA); }
535   void visitICmp(User &I);
536   void visitFCmp(User &I);
537   // Visit the conversion instructions
538   void visitTrunc(User &I);
539   void visitZExt(User &I);
540   void visitSExt(User &I);
541   void visitFPTrunc(User &I);
542   void visitFPExt(User &I);
543   void visitFPToUI(User &I);
544   void visitFPToSI(User &I);
545   void visitUIToFP(User &I);
546   void visitSIToFP(User &I);
547   void visitPtrToInt(User &I);
548   void visitIntToPtr(User &I);
549   void visitBitCast(User &I);
550
551   void visitExtractElement(User &I);
552   void visitInsertElement(User &I);
553   void visitShuffleVector(User &I);
554
555   void visitGetElementPtr(User &I);
556   void visitSelect(User &I);
557
558   void visitMalloc(MallocInst &I);
559   void visitFree(FreeInst &I);
560   void visitAlloca(AllocaInst &I);
561   void visitLoad(LoadInst &I);
562   void visitStore(StoreInst &I);
563   void visitPHI(PHINode &I) { } // PHI nodes are handled specially.
564   void visitCall(CallInst &I);
565   void visitInlineAsm(CallInst &I);
566   const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic);
567   void visitTargetIntrinsic(CallInst &I, unsigned Intrinsic);
568
569   void visitVAStart(CallInst &I);
570   void visitVAArg(VAArgInst &I);
571   void visitVAEnd(CallInst &I);
572   void visitVACopy(CallInst &I);
573
574   void visitMemIntrinsic(CallInst &I, unsigned Op);
575
576   void visitUserOp1(Instruction &I) {
577     assert(0 && "UserOp1 should not exist at instruction selection time!");
578     abort();
579   }
580   void visitUserOp2(Instruction &I) {
581     assert(0 && "UserOp2 should not exist at instruction selection time!");
582     abort();
583   }
584 };
585 } // end namespace llvm
586
587 SDOperand SelectionDAGLowering::getValue(const Value *V) {
588   SDOperand &N = NodeMap[V];
589   if (N.Val) return N;
590   
591   const Type *VTy = V->getType();
592   MVT::ValueType VT = TLI.getValueType(VTy);
593   if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
594     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
595       visit(CE->getOpcode(), *CE);
596       assert(N.Val && "visit didn't populate the ValueMap!");
597       return N;
598     } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
599       return N = DAG.getGlobalAddress(GV, VT);
600     } else if (isa<ConstantPointerNull>(C)) {
601       return N = DAG.getConstant(0, TLI.getPointerTy());
602     } else if (isa<UndefValue>(C)) {
603       if (!isa<PackedType>(VTy))
604         return N = DAG.getNode(ISD::UNDEF, VT);
605
606       // Create a VBUILD_VECTOR of undef nodes.
607       const PackedType *PTy = cast<PackedType>(VTy);
608       unsigned NumElements = PTy->getNumElements();
609       MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
610
611       SmallVector<SDOperand, 8> Ops;
612       Ops.assign(NumElements, DAG.getNode(ISD::UNDEF, PVT));
613       
614       // Create a VConstant node with generic Vector type.
615       Ops.push_back(DAG.getConstant(NumElements, MVT::i32));
616       Ops.push_back(DAG.getValueType(PVT));
617       return N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
618                              &Ops[0], Ops.size());
619     } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
620       return N = DAG.getConstantFP(CFP->getValue(), VT);
621     } else if (const PackedType *PTy = dyn_cast<PackedType>(VTy)) {
622       unsigned NumElements = PTy->getNumElements();
623       MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
624       
625       // Now that we know the number and type of the elements, push a
626       // Constant or ConstantFP node onto the ops list for each element of
627       // the packed constant.
628       SmallVector<SDOperand, 8> Ops;
629       if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) {
630         for (unsigned i = 0; i != NumElements; ++i)
631           Ops.push_back(getValue(CP->getOperand(i)));
632       } else {
633         assert(isa<ConstantAggregateZero>(C) && "Unknown packed constant!");
634         SDOperand Op;
635         if (MVT::isFloatingPoint(PVT))
636           Op = DAG.getConstantFP(0, PVT);
637         else
638           Op = DAG.getConstant(0, PVT);
639         Ops.assign(NumElements, Op);
640       }
641       
642       // Create a VBUILD_VECTOR node with generic Vector type.
643       Ops.push_back(DAG.getConstant(NumElements, MVT::i32));
644       Ops.push_back(DAG.getValueType(PVT));
645       return N = DAG.getNode(ISD::VBUILD_VECTOR,MVT::Vector,&Ops[0],Ops.size());
646     } else {
647       // Canonicalize all constant ints to be unsigned.
648       return N = DAG.getConstant(cast<ConstantInt>(C)->getZExtValue(),VT);
649     }
650   }
651       
652   if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
653     std::map<const AllocaInst*, int>::iterator SI =
654     FuncInfo.StaticAllocaMap.find(AI);
655     if (SI != FuncInfo.StaticAllocaMap.end())
656       return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
657   }
658       
659   std::map<const Value*, unsigned>::const_iterator VMI =
660       FuncInfo.ValueMap.find(V);
661   assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!");
662   
663   unsigned InReg = VMI->second;
664   
665   // If this type is not legal, make it so now.
666   if (VT != MVT::Vector) {
667     if (TLI.getTypeAction(VT) == TargetLowering::Expand) {
668       // Source must be expanded.  This input value is actually coming from the
669       // register pair VMI->second and VMI->second+1.
670       MVT::ValueType DestVT = TLI.getTypeToExpandTo(VT);
671       unsigned NumVals = TLI.getNumElements(VT);
672       N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT);
673       if (NumVals == 1)
674         N = DAG.getNode(ISD::BIT_CONVERT, VT, N);
675       else {
676         assert(NumVals == 2 && "1 to 4 (and more) expansion not implemented!");
677         N = DAG.getNode(ISD::BUILD_PAIR, VT, N,
678                        DAG.getCopyFromReg(DAG.getEntryNode(), InReg+1, DestVT));
679       }
680     } else {
681       MVT::ValueType DestVT = TLI.getTypeToTransformTo(VT);
682       N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT);
683       if (TLI.getTypeAction(VT) == TargetLowering::Promote) // Promotion case
684         N = MVT::isFloatingPoint(VT)
685           ? DAG.getNode(ISD::FP_ROUND, VT, N)
686           : DAG.getNode(ISD::TRUNCATE, VT, N);
687     }
688   } else {
689     // Otherwise, if this is a vector, make it available as a generic vector
690     // here.
691     MVT::ValueType PTyElementVT, PTyLegalElementVT;
692     const PackedType *PTy = cast<PackedType>(VTy);
693     unsigned NE = TLI.getPackedTypeBreakdown(PTy, PTyElementVT,
694                                              PTyLegalElementVT);
695
696     // Build a VBUILD_VECTOR with the input registers.
697     SmallVector<SDOperand, 8> Ops;
698     if (PTyElementVT == PTyLegalElementVT) {
699       // If the value types are legal, just VBUILD the CopyFromReg nodes.
700       for (unsigned i = 0; i != NE; ++i)
701         Ops.push_back(DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, 
702                                          PTyElementVT));
703     } else if (PTyElementVT < PTyLegalElementVT) {
704       // If the register was promoted, use TRUNCATE of FP_ROUND as appropriate.
705       for (unsigned i = 0; i != NE; ++i) {
706         SDOperand Op = DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, 
707                                           PTyElementVT);
708         if (MVT::isFloatingPoint(PTyElementVT))
709           Op = DAG.getNode(ISD::FP_ROUND, PTyElementVT, Op);
710         else
711           Op = DAG.getNode(ISD::TRUNCATE, PTyElementVT, Op);
712         Ops.push_back(Op);
713       }
714     } else {
715       // If the register was expanded, use BUILD_PAIR.
716       assert((NE & 1) == 0 && "Must expand into a multiple of 2 elements!");
717       for (unsigned i = 0; i != NE/2; ++i) {
718         SDOperand Op0 = DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, 
719                                            PTyElementVT);
720         SDOperand Op1 = DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, 
721                                            PTyElementVT);
722         Ops.push_back(DAG.getNode(ISD::BUILD_PAIR, VT, Op0, Op1));
723       }
724     }
725     
726     Ops.push_back(DAG.getConstant(NE, MVT::i32));
727     Ops.push_back(DAG.getValueType(PTyLegalElementVT));
728     N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
729     
730     // Finally, use a VBIT_CONVERT to make this available as the appropriate
731     // vector type.
732     N = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, N, 
733                     DAG.getConstant(PTy->getNumElements(),
734                                     MVT::i32),
735                     DAG.getValueType(TLI.getValueType(PTy->getElementType())));
736   }
737   
738   return N;
739 }
740
741
742 void SelectionDAGLowering::visitRet(ReturnInst &I) {
743   if (I.getNumOperands() == 0) {
744     DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot()));
745     return;
746   }
747   SmallVector<SDOperand, 8> NewValues;
748   NewValues.push_back(getRoot());
749   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
750     SDOperand RetOp = getValue(I.getOperand(i));
751     
752     // If this is an integer return value, we need to promote it ourselves to
753     // the full width of a register, since LegalizeOp will use ANY_EXTEND rather
754     // than sign/zero.
755     // FIXME: C calling convention requires the return type to be promoted to
756     // at least 32-bit. But this is not necessary for non-C calling conventions.
757     if (MVT::isInteger(RetOp.getValueType()) && 
758         RetOp.getValueType() < MVT::i64) {
759       MVT::ValueType TmpVT;
760       if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote)
761         TmpVT = TLI.getTypeToTransformTo(MVT::i32);
762       else
763         TmpVT = MVT::i32;
764       const FunctionType *FTy = I.getParent()->getParent()->getFunctionType();
765       ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
766       if (FTy->paramHasAttr(0, FunctionType::SExtAttribute))
767         ExtendKind = ISD::SIGN_EXTEND;
768       if (FTy->paramHasAttr(0, FunctionType::ZExtAttribute))
769         ExtendKind = ISD::ZERO_EXTEND;
770       RetOp = DAG.getNode(ExtendKind, TmpVT, RetOp);
771     }
772     NewValues.push_back(RetOp);
773     NewValues.push_back(DAG.getConstant(false, MVT::i32));
774   }
775   DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other,
776                           &NewValues[0], NewValues.size()));
777 }
778
779 /// ExportFromCurrentBlock - If this condition isn't known to be exported from
780 /// the current basic block, add it to ValueMap now so that we'll get a
781 /// CopyTo/FromReg.
782 void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) {
783   // No need to export constants.
784   if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
785   
786   // Already exported?
787   if (FuncInfo.isExportedInst(V)) return;
788
789   unsigned Reg = FuncInfo.InitializeRegForValue(V);
790   PendingLoads.push_back(CopyValueToVirtualRegister(V, Reg));
791 }
792
793 bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V,
794                                                     const BasicBlock *FromBB) {
795   // The operands of the setcc have to be in this block.  We don't know
796   // how to export them from some other block.
797   if (Instruction *VI = dyn_cast<Instruction>(V)) {
798     // Can export from current BB.
799     if (VI->getParent() == FromBB)
800       return true;
801     
802     // Is already exported, noop.
803     return FuncInfo.isExportedInst(V);
804   }
805   
806   // If this is an argument, we can export it if the BB is the entry block or
807   // if it is already exported.
808   if (isa<Argument>(V)) {
809     if (FromBB == &FromBB->getParent()->getEntryBlock())
810       return true;
811
812     // Otherwise, can only export this if it is already exported.
813     return FuncInfo.isExportedInst(V);
814   }
815   
816   // Otherwise, constants can always be exported.
817   return true;
818 }
819
820 static bool InBlock(const Value *V, const BasicBlock *BB) {
821   if (const Instruction *I = dyn_cast<Instruction>(V))
822     return I->getParent() == BB;
823   return true;
824 }
825
826 /// FindMergedConditions - If Cond is an expression like 
827 void SelectionDAGLowering::FindMergedConditions(Value *Cond,
828                                                 MachineBasicBlock *TBB,
829                                                 MachineBasicBlock *FBB,
830                                                 MachineBasicBlock *CurBB,
831                                                 unsigned Opc) {
832   // If this node is not part of the or/and tree, emit it as a branch.
833   Instruction *BOp = dyn_cast<Instruction>(Cond);
834
835   if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || 
836       (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
837       BOp->getParent() != CurBB->getBasicBlock() ||
838       !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
839       !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
840     const BasicBlock *BB = CurBB->getBasicBlock();
841     
842     // If the leaf of the tree is a comparison, merge the condition into 
843     // the caseblock.
844     if ((isa<ICmpInst>(Cond) || isa<FCmpInst>(Cond)) &&
845         // The operands of the cmp have to be in this block.  We don't know
846         // how to export them from some other block.  If this is the first block
847         // of the sequence, no exporting is needed.
848         (CurBB == CurMBB ||
849          (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
850           isExportableFromCurrentBlock(BOp->getOperand(1), BB)))) {
851       BOp = cast<Instruction>(Cond);
852       ISD::CondCode Condition;
853       if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
854         switch (IC->getPredicate()) {
855         default: assert(0 && "Unknown icmp predicate opcode!");
856         case ICmpInst::ICMP_EQ:  Condition = ISD::SETEQ;  break;
857         case ICmpInst::ICMP_NE:  Condition = ISD::SETNE;  break;
858         case ICmpInst::ICMP_SLE: Condition = ISD::SETLE;  break;
859         case ICmpInst::ICMP_ULE: Condition = ISD::SETULE; break;
860         case ICmpInst::ICMP_SGE: Condition = ISD::SETGE;  break;
861         case ICmpInst::ICMP_UGE: Condition = ISD::SETUGE; break;
862         case ICmpInst::ICMP_SLT: Condition = ISD::SETLT;  break;
863         case ICmpInst::ICMP_ULT: Condition = ISD::SETULT; break;
864         case ICmpInst::ICMP_SGT: Condition = ISD::SETGT;  break;
865         case ICmpInst::ICMP_UGT: Condition = ISD::SETUGT; break;
866         }
867       } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
868         ISD::CondCode FPC, FOC;
869         switch (FC->getPredicate()) {
870         default: assert(0 && "Unknown fcmp predicate opcode!");
871         case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
872         case FCmpInst::FCMP_OEQ:   FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
873         case FCmpInst::FCMP_OGT:   FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
874         case FCmpInst::FCMP_OGE:   FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
875         case FCmpInst::FCMP_OLT:   FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
876         case FCmpInst::FCMP_OLE:   FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
877         case FCmpInst::FCMP_ONE:   FOC = ISD::SETNE; FPC = ISD::SETONE; break;
878         case FCmpInst::FCMP_ORD:   FOC = ISD::SETEQ; FPC = ISD::SETO;   break;
879         case FCmpInst::FCMP_UNO:   FOC = ISD::SETNE; FPC = ISD::SETUO;  break;
880         case FCmpInst::FCMP_UEQ:   FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
881         case FCmpInst::FCMP_UGT:   FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
882         case FCmpInst::FCMP_UGE:   FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
883         case FCmpInst::FCMP_ULT:   FOC = ISD::SETLT; FPC = ISD::SETULT; break;
884         case FCmpInst::FCMP_ULE:   FOC = ISD::SETLE; FPC = ISD::SETULE; break;
885         case FCmpInst::FCMP_UNE:   FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
886         case FCmpInst::FCMP_TRUE:  FOC = FPC = ISD::SETTRUE; break;
887         }
888         if (FiniteOnlyFPMath())
889           Condition = FOC;
890         else 
891           Condition = FPC;
892       } else {
893         assert(0 && "Unknown compare instruction");
894       }
895       
896       SelectionDAGISel::CaseBlock CB(Condition, BOp->getOperand(0), 
897                                      BOp->getOperand(1), TBB, FBB, CurBB);
898       SwitchCases.push_back(CB);
899       return;
900     }
901     
902     // Create a CaseBlock record representing this branch.
903     SelectionDAGISel::CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(),
904                                    TBB, FBB, CurBB);
905     SwitchCases.push_back(CB);
906     return;
907   }
908   
909   
910   //  Create TmpBB after CurBB.
911   MachineFunction::iterator BBI = CurBB;
912   MachineBasicBlock *TmpBB = new MachineBasicBlock(CurBB->getBasicBlock());
913   CurBB->getParent()->getBasicBlockList().insert(++BBI, TmpBB);
914   
915   if (Opc == Instruction::Or) {
916     // Codegen X | Y as:
917     //   jmp_if_X TBB
918     //   jmp TmpBB
919     // TmpBB:
920     //   jmp_if_Y TBB
921     //   jmp FBB
922     //
923   
924     // Emit the LHS condition.
925     FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
926   
927     // Emit the RHS condition into TmpBB.
928     FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
929   } else {
930     assert(Opc == Instruction::And && "Unknown merge op!");
931     // Codegen X & Y as:
932     //   jmp_if_X TmpBB
933     //   jmp FBB
934     // TmpBB:
935     //   jmp_if_Y TBB
936     //   jmp FBB
937     //
938     //  This requires creation of TmpBB after CurBB.
939     
940     // Emit the LHS condition.
941     FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
942     
943     // Emit the RHS condition into TmpBB.
944     FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
945   }
946 }
947
948 /// If the set of cases should be emitted as a series of branches, return true.
949 /// If we should emit this as a bunch of and/or'd together conditions, return
950 /// false.
951 static bool 
952 ShouldEmitAsBranches(const std::vector<SelectionDAGISel::CaseBlock> &Cases) {
953   if (Cases.size() != 2) return true;
954   
955   // If this is two comparisons of the same values or'd or and'd together, they
956   // will get folded into a single comparison, so don't emit two blocks.
957   if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
958        Cases[0].CmpRHS == Cases[1].CmpRHS) ||
959       (Cases[0].CmpRHS == Cases[1].CmpLHS &&
960        Cases[0].CmpLHS == Cases[1].CmpRHS)) {
961     return false;
962   }
963   
964   return true;
965 }
966
967 void SelectionDAGLowering::visitBr(BranchInst &I) {
968   // Update machine-CFG edges.
969   MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
970
971   // Figure out which block is immediately after the current one.
972   MachineBasicBlock *NextBlock = 0;
973   MachineFunction::iterator BBI = CurMBB;
974   if (++BBI != CurMBB->getParent()->end())
975     NextBlock = BBI;
976
977   if (I.isUnconditional()) {
978     // If this is not a fall-through branch, emit the branch.
979     if (Succ0MBB != NextBlock)
980       DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(),
981                               DAG.getBasicBlock(Succ0MBB)));
982
983     // Update machine-CFG edges.
984     CurMBB->addSuccessor(Succ0MBB);
985
986     return;
987   }
988
989   // If this condition is one of the special cases we handle, do special stuff
990   // now.
991   Value *CondVal = I.getCondition();
992   MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
993
994   // If this is a series of conditions that are or'd or and'd together, emit
995   // this as a sequence of branches instead of setcc's with and/or operations.
996   // For example, instead of something like:
997   //     cmp A, B
998   //     C = seteq 
999   //     cmp D, E
1000   //     F = setle 
1001   //     or C, F
1002   //     jnz foo
1003   // Emit:
1004   //     cmp A, B
1005   //     je foo
1006   //     cmp D, E
1007   //     jle foo
1008   //
1009   if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
1010     if (BOp->hasOneUse() && 
1011         (BOp->getOpcode() == Instruction::And ||
1012          BOp->getOpcode() == Instruction::Or)) {
1013       FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1014       // If the compares in later blocks need to use values not currently
1015       // exported from this block, export them now.  This block should always
1016       // be the first entry.
1017       assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
1018       
1019       // Allow some cases to be rejected.
1020       if (ShouldEmitAsBranches(SwitchCases)) {
1021         for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1022           ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1023           ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1024         }
1025         
1026         // Emit the branch for this block.
1027         visitSwitchCase(SwitchCases[0]);
1028         SwitchCases.erase(SwitchCases.begin());
1029         return;
1030       }
1031       
1032       // Okay, we decided not to do this, remove any inserted MBB's and clear
1033       // SwitchCases.
1034       for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
1035         CurMBB->getParent()->getBasicBlockList().erase(SwitchCases[i].ThisBB);
1036       
1037       SwitchCases.clear();
1038     }
1039   }
1040   
1041   // Create a CaseBlock record representing this branch.
1042   SelectionDAGISel::CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(),
1043                                  Succ0MBB, Succ1MBB, CurMBB);
1044   // Use visitSwitchCase to actually insert the fast branch sequence for this
1045   // cond branch.
1046   visitSwitchCase(CB);
1047 }
1048
1049 /// visitSwitchCase - Emits the necessary code to represent a single node in
1050 /// the binary search tree resulting from lowering a switch instruction.
1051 void SelectionDAGLowering::visitSwitchCase(SelectionDAGISel::CaseBlock &CB) {
1052   SDOperand Cond;
1053   SDOperand CondLHS = getValue(CB.CmpLHS);
1054   
1055   // Build the setcc now, fold "(X == true)" to X and "(X == false)" to !X to
1056   // handle common cases produced by branch lowering.
1057   if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ)
1058     Cond = CondLHS;
1059   else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) {
1060     SDOperand True = DAG.getConstant(1, CondLHS.getValueType());
1061     Cond = DAG.getNode(ISD::XOR, CondLHS.getValueType(), CondLHS, True);
1062   } else
1063     Cond = DAG.getSetCC(MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
1064   
1065   // Set NextBlock to be the MBB immediately after the current one, if any.
1066   // This is used to avoid emitting unnecessary branches to the next block.
1067   MachineBasicBlock *NextBlock = 0;
1068   MachineFunction::iterator BBI = CurMBB;
1069   if (++BBI != CurMBB->getParent()->end())
1070     NextBlock = BBI;
1071   
1072   // If the lhs block is the next block, invert the condition so that we can
1073   // fall through to the lhs instead of the rhs block.
1074   if (CB.TrueBB == NextBlock) {
1075     std::swap(CB.TrueBB, CB.FalseBB);
1076     SDOperand True = DAG.getConstant(1, Cond.getValueType());
1077     Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
1078   }
1079   SDOperand BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), Cond,
1080                                  DAG.getBasicBlock(CB.TrueBB));
1081   if (CB.FalseBB == NextBlock)
1082     DAG.setRoot(BrCond);
1083   else
1084     DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, 
1085                             DAG.getBasicBlock(CB.FalseBB)));
1086   // Update successor info
1087   CurMBB->addSuccessor(CB.TrueBB);
1088   CurMBB->addSuccessor(CB.FalseBB);
1089 }
1090
1091 void SelectionDAGLowering::visitJumpTable(SelectionDAGISel::JumpTable &JT) {
1092   // Emit the code for the jump table
1093   MVT::ValueType PTy = TLI.getPointerTy();
1094   SDOperand Index = DAG.getCopyFromReg(getRoot(), JT.Reg, PTy);
1095   SDOperand Table = DAG.getJumpTable(JT.JTI, PTy);
1096   DAG.setRoot(DAG.getNode(ISD::BR_JT, MVT::Other, Index.getValue(1),
1097                           Table, Index));
1098   return;
1099 }
1100
1101 void SelectionDAGLowering::visitSwitch(SwitchInst &I) {
1102   // Figure out which block is immediately after the current one.
1103   MachineBasicBlock *NextBlock = 0;
1104   MachineFunction::iterator BBI = CurMBB;
1105
1106   if (++BBI != CurMBB->getParent()->end())
1107     NextBlock = BBI;
1108   
1109   MachineBasicBlock *Default = FuncInfo.MBBMap[I.getDefaultDest()];
1110
1111   // If there is only the default destination, branch to it if it is not the
1112   // next basic block.  Otherwise, just fall through.
1113   if (I.getNumOperands() == 2) {
1114     // Update machine-CFG edges.
1115
1116     // If this is not a fall-through branch, emit the branch.
1117     if (Default != NextBlock)
1118       DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(),
1119                               DAG.getBasicBlock(Default)));
1120
1121     CurMBB->addSuccessor(Default);
1122     return;
1123   }
1124   
1125   // If there are any non-default case statements, create a vector of Cases
1126   // representing each one, and sort the vector so that we can efficiently
1127   // create a binary search tree from them.
1128   std::vector<Case> Cases;
1129
1130   for (unsigned i = 1; i < I.getNumSuccessors(); ++i) {
1131     MachineBasicBlock *SMBB = FuncInfo.MBBMap[I.getSuccessor(i)];
1132     Cases.push_back(Case(I.getSuccessorValue(i), SMBB));
1133   }
1134
1135   std::sort(Cases.begin(), Cases.end(), CaseCmp());
1136   
1137   // Get the Value to be switched on and default basic blocks, which will be
1138   // inserted into CaseBlock records, representing basic blocks in the binary
1139   // search tree.
1140   Value *SV = I.getOperand(0);
1141
1142   // Get the MachineFunction which holds the current MBB.  This is used during
1143   // emission of jump tables, and when inserting any additional MBBs necessary
1144   // to represent the switch.
1145   MachineFunction *CurMF = CurMBB->getParent();
1146   const BasicBlock *LLVMBB = CurMBB->getBasicBlock();
1147   
1148   // If the switch has few cases (two or less) emit a series of specific
1149   // tests.
1150   if (Cases.size() < 3) {
1151     // TODO: If any two of the cases has the same destination, and if one value
1152     // is the same as the other, but has one bit unset that the other has set,
1153     // use bit manipulation to do two compares at once.  For example:
1154     // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
1155     
1156     // Rearrange the case blocks so that the last one falls through if possible.
1157     if (NextBlock && Default != NextBlock && Cases.back().second != NextBlock) {
1158       // The last case block won't fall through into 'NextBlock' if we emit the
1159       // branches in this order.  See if rearranging a case value would help.
1160       for (unsigned i = 0, e = Cases.size()-1; i != e; ++i) {
1161         if (Cases[i].second == NextBlock) {
1162           std::swap(Cases[i], Cases.back());
1163           break;
1164         }
1165       }
1166     }
1167     
1168     // Create a CaseBlock record representing a conditional branch to
1169     // the Case's target mbb if the value being switched on SV is equal
1170     // to C.
1171     MachineBasicBlock *CurBlock = CurMBB;
1172     for (unsigned i = 0, e = Cases.size(); i != e; ++i) {
1173       MachineBasicBlock *FallThrough;
1174       if (i != e-1) {
1175         FallThrough = new MachineBasicBlock(CurMBB->getBasicBlock());
1176         CurMF->getBasicBlockList().insert(BBI, FallThrough);
1177       } else {
1178         // If the last case doesn't match, go to the default block.
1179         FallThrough = Default;
1180       }
1181       
1182       SelectionDAGISel::CaseBlock CB(ISD::SETEQ, SV, Cases[i].first,
1183                                      Cases[i].second, FallThrough, CurBlock);
1184     
1185       // If emitting the first comparison, just call visitSwitchCase to emit the
1186       // code into the current block.  Otherwise, push the CaseBlock onto the
1187       // vector to be later processed by SDISel, and insert the node's MBB
1188       // before the next MBB.
1189       if (CurBlock == CurMBB)
1190         visitSwitchCase(CB);
1191       else
1192         SwitchCases.push_back(CB);
1193       
1194       CurBlock = FallThrough;
1195     }
1196     return;
1197   }
1198
1199   // If the switch has more than 5 blocks, and at least 31.25% dense, and the 
1200   // target supports indirect branches, then emit a jump table rather than 
1201   // lowering the switch to a binary tree of conditional branches.
1202   if ((TLI.isOperationLegal(ISD::BR_JT, MVT::Other) ||
1203        TLI.isOperationLegal(ISD::BRIND, MVT::Other)) &&
1204       Cases.size() > 5) {
1205     uint64_t First =cast<ConstantInt>(Cases.front().first)->getZExtValue();
1206     uint64_t Last  = cast<ConstantInt>(Cases.back().first)->getZExtValue();
1207     double Density = (double)Cases.size() / (double)((Last - First) + 1ULL);
1208     
1209     if (Density >= 0.3125) {
1210       // Create a new basic block to hold the code for loading the address
1211       // of the jump table, and jumping to it.  Update successor information;
1212       // we will either branch to the default case for the switch, or the jump
1213       // table.
1214       MachineBasicBlock *JumpTableBB = new MachineBasicBlock(LLVMBB);
1215       CurMF->getBasicBlockList().insert(BBI, JumpTableBB);
1216       CurMBB->addSuccessor(Default);
1217       CurMBB->addSuccessor(JumpTableBB);
1218       
1219       // Subtract the lowest switch case value from the value being switched on
1220       // and conditional branch to default mbb if the result is greater than the
1221       // difference between smallest and largest cases.
1222       SDOperand SwitchOp = getValue(SV);
1223       MVT::ValueType VT = SwitchOp.getValueType();
1224       SDOperand SUB = DAG.getNode(ISD::SUB, VT, SwitchOp, 
1225                                   DAG.getConstant(First, VT));
1226
1227       // The SDNode we just created, which holds the value being switched on
1228       // minus the the smallest case value, needs to be copied to a virtual
1229       // register so it can be used as an index into the jump table in a 
1230       // subsequent basic block.  This value may be smaller or larger than the
1231       // target's pointer type, and therefore require extension or truncating.
1232       if (VT > TLI.getPointerTy())
1233         SwitchOp = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), SUB);
1234       else
1235         SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), SUB);
1236
1237       unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
1238       SDOperand CopyTo = DAG.getCopyToReg(getRoot(), JumpTableReg, SwitchOp);
1239       
1240       // Emit the range check for the jump table, and branch to the default
1241       // block for the switch statement if the value being switched on exceeds
1242       // the largest case in the switch.
1243       SDOperand CMP = DAG.getSetCC(TLI.getSetCCResultTy(), SUB,
1244                                    DAG.getConstant(Last-First,VT), ISD::SETUGT);
1245       DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, CMP, 
1246                               DAG.getBasicBlock(Default)));
1247
1248       // Build a vector of destination BBs, corresponding to each target
1249       // of the jump table.  If the value of the jump table slot corresponds to
1250       // a case statement, push the case's BB onto the vector, otherwise, push
1251       // the default BB.
1252       std::vector<MachineBasicBlock*> DestBBs;
1253       uint64_t TEI = First;
1254       for (CaseItr ii = Cases.begin(), ee = Cases.end(); ii != ee; ++TEI)
1255         if (cast<ConstantInt>(ii->first)->getZExtValue() == TEI) {
1256           DestBBs.push_back(ii->second);
1257           ++ii;
1258         } else {
1259           DestBBs.push_back(Default);
1260         }
1261       
1262       // Update successor info.  Add one edge to each unique successor.
1263       // Vector bool would be better, but vector<bool> is really slow.
1264       std::vector<unsigned char> SuccsHandled;
1265       SuccsHandled.resize(CurMBB->getParent()->getNumBlockIDs());
1266       
1267       for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), 
1268            E = DestBBs.end(); I != E; ++I) {
1269         if (!SuccsHandled[(*I)->getNumber()]) {
1270           SuccsHandled[(*I)->getNumber()] = true;
1271           JumpTableBB->addSuccessor(*I);
1272         }
1273       }
1274       
1275       // Create a jump table index for this jump table, or return an existing
1276       // one.
1277       unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
1278       
1279       // Set the jump table information so that we can codegen it as a second
1280       // MachineBasicBlock
1281       JT.Reg = JumpTableReg;
1282       JT.JTI = JTI;
1283       JT.MBB = JumpTableBB;
1284       JT.Default = Default;
1285       return;
1286     }
1287   }
1288   
1289   // Push the initial CaseRec onto the worklist
1290   std::vector<CaseRec> CaseVec;
1291   CaseVec.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
1292   
1293   while (!CaseVec.empty()) {
1294     // Grab a record representing a case range to process off the worklist
1295     CaseRec CR = CaseVec.back();
1296     CaseVec.pop_back();
1297     
1298     // Size is the number of Cases represented by this range.  If Size is 1,
1299     // then we are processing a leaf of the binary search tree.  Otherwise,
1300     // we need to pick a pivot, and push left and right ranges onto the 
1301     // worklist.
1302     unsigned Size = CR.Range.second - CR.Range.first;
1303     
1304     if (Size == 1) {
1305       // Create a CaseBlock record representing a conditional branch to
1306       // the Case's target mbb if the value being switched on SV is equal
1307       // to C.  Otherwise, branch to default.
1308       Constant *C = CR.Range.first->first;
1309       MachineBasicBlock *Target = CR.Range.first->second;
1310       SelectionDAGISel::CaseBlock CB(ISD::SETEQ, SV, C, Target, Default, 
1311                                      CR.CaseBB);
1312
1313       // If the MBB representing the leaf node is the current MBB, then just
1314       // call visitSwitchCase to emit the code into the current block.
1315       // Otherwise, push the CaseBlock onto the vector to be later processed
1316       // by SDISel, and insert the node's MBB before the next MBB.
1317       if (CR.CaseBB == CurMBB)
1318         visitSwitchCase(CB);
1319       else
1320         SwitchCases.push_back(CB);
1321     } else {
1322       // split case range at pivot
1323       CaseItr Pivot = CR.Range.first + (Size / 2);
1324       CaseRange LHSR(CR.Range.first, Pivot);
1325       CaseRange RHSR(Pivot, CR.Range.second);
1326       Constant *C = Pivot->first;
1327       MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
1328
1329       // We know that we branch to the LHS if the Value being switched on is
1330       // less than the Pivot value, C.  We use this to optimize our binary 
1331       // tree a bit, by recognizing that if SV is greater than or equal to the
1332       // LHS's Case Value, and that Case Value is exactly one less than the 
1333       // Pivot's Value, then we can branch directly to the LHS's Target,
1334       // rather than creating a leaf node for it.
1335       if ((LHSR.second - LHSR.first) == 1 &&
1336           LHSR.first->first == CR.GE &&
1337           cast<ConstantInt>(C)->getZExtValue() ==
1338           (cast<ConstantInt>(CR.GE)->getZExtValue() + 1ULL)) {
1339         TrueBB = LHSR.first->second;
1340       } else {
1341         TrueBB = new MachineBasicBlock(LLVMBB);
1342         CurMF->getBasicBlockList().insert(BBI, TrueBB);
1343         CaseVec.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
1344       }
1345
1346       // Similar to the optimization above, if the Value being switched on is
1347       // known to be less than the Constant CR.LT, and the current Case Value
1348       // is CR.LT - 1, then we can branch directly to the target block for
1349       // the current Case Value, rather than emitting a RHS leaf node for it.
1350       if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
1351           cast<ConstantInt>(RHSR.first->first)->getZExtValue() ==
1352           (cast<ConstantInt>(CR.LT)->getZExtValue() - 1ULL)) {
1353         FalseBB = RHSR.first->second;
1354       } else {
1355         FalseBB = new MachineBasicBlock(LLVMBB);
1356         CurMF->getBasicBlockList().insert(BBI, FalseBB);
1357         CaseVec.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
1358       }
1359
1360       // Create a CaseBlock record representing a conditional branch to
1361       // the LHS node if the value being switched on SV is less than C. 
1362       // Otherwise, branch to LHS.
1363       ISD::CondCode CC =  ISD::SETLT;
1364       SelectionDAGISel::CaseBlock CB(CC, SV, C, TrueBB, FalseBB, CR.CaseBB);
1365
1366       if (CR.CaseBB == CurMBB)
1367         visitSwitchCase(CB);
1368       else
1369         SwitchCases.push_back(CB);
1370     }
1371   }
1372 }
1373
1374 void SelectionDAGLowering::visitSub(User &I) {
1375   // -0.0 - X --> fneg
1376   const Type *Ty = I.getType();
1377   if (isa<PackedType>(Ty)) {
1378     visitVectorBinary(I, ISD::VSUB);
1379   } else if (Ty->isFloatingPoint()) {
1380     if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
1381       if (CFP->isExactlyValue(-0.0)) {
1382         SDOperand Op2 = getValue(I.getOperand(1));
1383         setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
1384         return;
1385       }
1386     visitScalarBinary(I, ISD::FSUB);
1387   } else 
1388     visitScalarBinary(I, ISD::SUB);
1389 }
1390
1391 void SelectionDAGLowering::visitScalarBinary(User &I, unsigned OpCode) {
1392   SDOperand Op1 = getValue(I.getOperand(0));
1393   SDOperand Op2 = getValue(I.getOperand(1));
1394   
1395   setValue(&I, DAG.getNode(OpCode, Op1.getValueType(), Op1, Op2));
1396 }
1397
1398 void
1399 SelectionDAGLowering::visitVectorBinary(User &I, unsigned OpCode) {
1400   assert(isa<PackedType>(I.getType()));
1401   const PackedType *Ty = cast<PackedType>(I.getType());
1402   SDOperand Typ = DAG.getValueType(TLI.getValueType(Ty->getElementType()));
1403
1404   setValue(&I, DAG.getNode(OpCode, MVT::Vector,
1405                            getValue(I.getOperand(0)),
1406                            getValue(I.getOperand(1)),
1407                            DAG.getConstant(Ty->getNumElements(), MVT::i32),
1408                            Typ));
1409 }
1410
1411 void SelectionDAGLowering::visitEitherBinary(User &I, unsigned ScalarOp,
1412                                              unsigned VectorOp) {
1413   if (isa<PackedType>(I.getType()))
1414     visitVectorBinary(I, VectorOp);
1415   else
1416     visitScalarBinary(I, ScalarOp);
1417 }
1418
1419 void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
1420   SDOperand Op1 = getValue(I.getOperand(0));
1421   SDOperand Op2 = getValue(I.getOperand(1));
1422   
1423   Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2);
1424   
1425   setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2));
1426 }
1427
1428 void SelectionDAGLowering::visitICmp(User &I) {
1429   ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
1430   if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
1431     predicate = IC->getPredicate();
1432   else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
1433     predicate = ICmpInst::Predicate(IC->getPredicate());
1434   SDOperand Op1 = getValue(I.getOperand(0));
1435   SDOperand Op2 = getValue(I.getOperand(1));
1436   ISD::CondCode Opcode;
1437   switch (predicate) {
1438     case ICmpInst::ICMP_EQ  : Opcode = ISD::SETEQ; break;
1439     case ICmpInst::ICMP_NE  : Opcode = ISD::SETNE; break;
1440     case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break;
1441     case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break;
1442     case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break;
1443     case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break;
1444     case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break;
1445     case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break;
1446     case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break;
1447     case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break;
1448     default:
1449       assert(!"Invalid ICmp predicate value");
1450       Opcode = ISD::SETEQ;
1451       break;
1452   }
1453   setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode));
1454 }
1455
1456 void SelectionDAGLowering::visitFCmp(User &I) {
1457   FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
1458   if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
1459     predicate = FC->getPredicate();
1460   else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
1461     predicate = FCmpInst::Predicate(FC->getPredicate());
1462   SDOperand Op1 = getValue(I.getOperand(0));
1463   SDOperand Op2 = getValue(I.getOperand(1));
1464   ISD::CondCode Condition, FOC, FPC;
1465   switch (predicate) {
1466     case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1467     case FCmpInst::FCMP_OEQ:   FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1468     case FCmpInst::FCMP_OGT:   FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1469     case FCmpInst::FCMP_OGE:   FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1470     case FCmpInst::FCMP_OLT:   FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1471     case FCmpInst::FCMP_OLE:   FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1472     case FCmpInst::FCMP_ONE:   FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1473     case FCmpInst::FCMP_ORD:   FOC = ISD::SETEQ; FPC = ISD::SETO;   break;
1474     case FCmpInst::FCMP_UNO:   FOC = ISD::SETNE; FPC = ISD::SETUO;  break;
1475     case FCmpInst::FCMP_UEQ:   FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1476     case FCmpInst::FCMP_UGT:   FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1477     case FCmpInst::FCMP_UGE:   FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1478     case FCmpInst::FCMP_ULT:   FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1479     case FCmpInst::FCMP_ULE:   FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1480     case FCmpInst::FCMP_UNE:   FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1481     case FCmpInst::FCMP_TRUE:  FOC = FPC = ISD::SETTRUE; break;
1482     default:
1483       assert(!"Invalid FCmp predicate value");
1484       FOC = FPC = ISD::SETFALSE;
1485       break;
1486   }
1487   if (FiniteOnlyFPMath())
1488     Condition = FOC;
1489   else 
1490     Condition = FPC;
1491   setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Condition));
1492 }
1493
1494 void SelectionDAGLowering::visitSelect(User &I) {
1495   SDOperand Cond     = getValue(I.getOperand(0));
1496   SDOperand TrueVal  = getValue(I.getOperand(1));
1497   SDOperand FalseVal = getValue(I.getOperand(2));
1498   if (!isa<PackedType>(I.getType())) {
1499     setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond,
1500                              TrueVal, FalseVal));
1501   } else {
1502     setValue(&I, DAG.getNode(ISD::VSELECT, MVT::Vector, Cond, TrueVal, FalseVal,
1503                              *(TrueVal.Val->op_end()-2),
1504                              *(TrueVal.Val->op_end()-1)));
1505   }
1506 }
1507
1508
1509 void SelectionDAGLowering::visitTrunc(User &I) {
1510   // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
1511   SDOperand N = getValue(I.getOperand(0));
1512   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1513   setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
1514 }
1515
1516 void SelectionDAGLowering::visitZExt(User &I) {
1517   // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
1518   // ZExt also can't be a cast to bool for same reason. So, nothing much to do
1519   SDOperand N = getValue(I.getOperand(0));
1520   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1521   setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
1522 }
1523
1524 void SelectionDAGLowering::visitSExt(User &I) {
1525   // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
1526   // SExt also can't be a cast to bool for same reason. So, nothing much to do
1527   SDOperand N = getValue(I.getOperand(0));
1528   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1529   setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N));
1530 }
1531
1532 void SelectionDAGLowering::visitFPTrunc(User &I) {
1533   // FPTrunc is never a no-op cast, no need to check
1534   SDOperand N = getValue(I.getOperand(0));
1535   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1536   setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N));
1537 }
1538
1539 void SelectionDAGLowering::visitFPExt(User &I){ 
1540   // FPTrunc is never a no-op cast, no need to check
1541   SDOperand N = getValue(I.getOperand(0));
1542   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1543   setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N));
1544 }
1545
1546 void SelectionDAGLowering::visitFPToUI(User &I) { 
1547   // FPToUI is never a no-op cast, no need to check
1548   SDOperand N = getValue(I.getOperand(0));
1549   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1550   setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N));
1551 }
1552
1553 void SelectionDAGLowering::visitFPToSI(User &I) {
1554   // FPToSI is never a no-op cast, no need to check
1555   SDOperand N = getValue(I.getOperand(0));
1556   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1557   setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N));
1558 }
1559
1560 void SelectionDAGLowering::visitUIToFP(User &I) { 
1561   // UIToFP is never a no-op cast, no need to check
1562   SDOperand N = getValue(I.getOperand(0));
1563   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1564   setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N));
1565 }
1566
1567 void SelectionDAGLowering::visitSIToFP(User &I){ 
1568   // UIToFP is never a no-op cast, no need to check
1569   SDOperand N = getValue(I.getOperand(0));
1570   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1571   setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N));
1572 }
1573
1574 void SelectionDAGLowering::visitPtrToInt(User &I) {
1575   // What to do depends on the size of the integer and the size of the pointer.
1576   // We can either truncate, zero extend, or no-op, accordingly.
1577   SDOperand N = getValue(I.getOperand(0));
1578   MVT::ValueType SrcVT = N.getValueType();
1579   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1580   SDOperand Result;
1581   if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(SrcVT))
1582     Result = DAG.getNode(ISD::TRUNCATE, DestVT, N);
1583   else 
1584     // Note: ZERO_EXTEND can handle cases where the sizes are equal too
1585     Result = DAG.getNode(ISD::ZERO_EXTEND, DestVT, N);
1586   setValue(&I, Result);
1587 }
1588
1589 void SelectionDAGLowering::visitIntToPtr(User &I) {
1590   // What to do depends on the size of the integer and the size of the pointer.
1591   // We can either truncate, zero extend, or no-op, accordingly.
1592   SDOperand N = getValue(I.getOperand(0));
1593   MVT::ValueType SrcVT = N.getValueType();
1594   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1595   if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(SrcVT))
1596     setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
1597   else 
1598     // Note: ZERO_EXTEND can handle cases where the sizes are equal too
1599     setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
1600 }
1601
1602 void SelectionDAGLowering::visitBitCast(User &I) { 
1603   SDOperand N = getValue(I.getOperand(0));
1604   MVT::ValueType DestVT = TLI.getValueType(I.getType());
1605   if (DestVT == MVT::Vector) {
1606     // This is a cast to a vector from something else.  
1607     // Get information about the output vector.
1608     const PackedType *DestTy = cast<PackedType>(I.getType());
1609     MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType());
1610     setValue(&I, DAG.getNode(ISD::VBIT_CONVERT, DestVT, N, 
1611                              DAG.getConstant(DestTy->getNumElements(),MVT::i32),
1612                              DAG.getValueType(EltVT)));
1613     return;
1614   } 
1615   MVT::ValueType SrcVT = N.getValueType();
1616   if (SrcVT == MVT::Vector) {
1617     // This is a cast from a vctor to something else. 
1618     // Get information about the input vector.
1619     setValue(&I, DAG.getNode(ISD::VBIT_CONVERT, DestVT, N));
1620     return;
1621   }
1622
1623   // BitCast assures us that source and destination are the same size so this 
1624   // is either a BIT_CONVERT or a no-op.
1625   if (DestVT != N.getValueType())
1626     setValue(&I, DAG.getNode(ISD::BIT_CONVERT, DestVT, N)); // convert types
1627   else
1628     setValue(&I, N); // noop cast.
1629 }
1630
1631 void SelectionDAGLowering::visitInsertElement(User &I) {
1632   SDOperand InVec = getValue(I.getOperand(0));
1633   SDOperand InVal = getValue(I.getOperand(1));
1634   SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
1635                                 getValue(I.getOperand(2)));
1636
1637   SDOperand Num = *(InVec.Val->op_end()-2);
1638   SDOperand Typ = *(InVec.Val->op_end()-1);
1639   setValue(&I, DAG.getNode(ISD::VINSERT_VECTOR_ELT, MVT::Vector,
1640                            InVec, InVal, InIdx, Num, Typ));
1641 }
1642
1643 void SelectionDAGLowering::visitExtractElement(User &I) {
1644   SDOperand InVec = getValue(I.getOperand(0));
1645   SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
1646                                 getValue(I.getOperand(1)));
1647   SDOperand Typ = *(InVec.Val->op_end()-1);
1648   setValue(&I, DAG.getNode(ISD::VEXTRACT_VECTOR_ELT,
1649                            TLI.getValueType(I.getType()), InVec, InIdx));
1650 }
1651
1652 void SelectionDAGLowering::visitShuffleVector(User &I) {
1653   SDOperand V1   = getValue(I.getOperand(0));
1654   SDOperand V2   = getValue(I.getOperand(1));
1655   SDOperand Mask = getValue(I.getOperand(2));
1656
1657   SDOperand Num = *(V1.Val->op_end()-2);
1658   SDOperand Typ = *(V2.Val->op_end()-1);
1659   setValue(&I, DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
1660                            V1, V2, Mask, Num, Typ));
1661 }
1662
1663
1664 void SelectionDAGLowering::visitGetElementPtr(User &I) {
1665   SDOperand N = getValue(I.getOperand(0));
1666   const Type *Ty = I.getOperand(0)->getType();
1667
1668   for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
1669        OI != E; ++OI) {
1670     Value *Idx = *OI;
1671     if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
1672       unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
1673       if (Field) {
1674         // N = N + Offset
1675         uint64_t Offset = TD->getStructLayout(StTy)->MemberOffsets[Field];
1676         N = DAG.getNode(ISD::ADD, N.getValueType(), N,
1677                         getIntPtrConstant(Offset));
1678       }
1679       Ty = StTy->getElementType(Field);
1680     } else {
1681       Ty = cast<SequentialType>(Ty)->getElementType();
1682
1683       // If this is a constant subscript, handle it quickly.
1684       if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
1685         if (CI->getZExtValue() == 0) continue;
1686         uint64_t Offs = 
1687             TD->getTypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
1688         N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs));
1689         continue;
1690       }
1691       
1692       // N = N + Idx * ElementSize;
1693       uint64_t ElementSize = TD->getTypeSize(Ty);
1694       SDOperand IdxN = getValue(Idx);
1695
1696       // If the index is smaller or larger than intptr_t, truncate or extend
1697       // it.
1698       if (IdxN.getValueType() < N.getValueType()) {
1699         IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN);
1700       } else if (IdxN.getValueType() > N.getValueType())
1701         IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN);
1702
1703       // If this is a multiply by a power of two, turn it into a shl
1704       // immediately.  This is a very common case.
1705       if (isPowerOf2_64(ElementSize)) {
1706         unsigned Amt = Log2_64(ElementSize);
1707         IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN,
1708                            DAG.getConstant(Amt, TLI.getShiftAmountTy()));
1709         N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
1710         continue;
1711       }
1712       
1713       SDOperand Scale = getIntPtrConstant(ElementSize);
1714       IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale);
1715       N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
1716     }
1717   }
1718   setValue(&I, N);
1719 }
1720
1721 void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
1722   // If this is a fixed sized alloca in the entry block of the function,
1723   // allocate it statically on the stack.
1724   if (FuncInfo.StaticAllocaMap.count(&I))
1725     return;   // getValue will auto-populate this.
1726
1727   const Type *Ty = I.getAllocatedType();
1728   uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
1729   unsigned Align =
1730     std::max((unsigned)TLI.getTargetData()->getTypeAlignmentPref(Ty),
1731              I.getAlignment());
1732
1733   SDOperand AllocSize = getValue(I.getArraySize());
1734   MVT::ValueType IntPtr = TLI.getPointerTy();
1735   if (IntPtr < AllocSize.getValueType())
1736     AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize);
1737   else if (IntPtr > AllocSize.getValueType())
1738     AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize);
1739
1740   AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize,
1741                           getIntPtrConstant(TySize));
1742
1743   // Handle alignment.  If the requested alignment is less than or equal to the
1744   // stack alignment, ignore it and round the size of the allocation up to the
1745   // stack alignment size.  If the size is greater than the stack alignment, we
1746   // note this in the DYNAMIC_STACKALLOC node.
1747   unsigned StackAlign =
1748     TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1749   if (Align <= StackAlign) {
1750     Align = 0;
1751     // Add SA-1 to the size.
1752     AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize,
1753                             getIntPtrConstant(StackAlign-1));
1754     // Mask out the low bits for alignment purposes.
1755     AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize,
1756                             getIntPtrConstant(~(uint64_t)(StackAlign-1)));
1757   }
1758
1759   SDOperand Ops[] = { getRoot(), AllocSize, getIntPtrConstant(Align) };
1760   const MVT::ValueType *VTs = DAG.getNodeValueTypes(AllocSize.getValueType(),
1761                                                     MVT::Other);
1762   SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, 2, Ops, 3);
1763   DAG.setRoot(setValue(&I, DSA).getValue(1));
1764
1765   // Inform the Frame Information that we have just allocated a variable-sized
1766   // object.
1767   CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject();
1768 }
1769
1770 void SelectionDAGLowering::visitLoad(LoadInst &I) {
1771   SDOperand Ptr = getValue(I.getOperand(0));
1772
1773   SDOperand Root;
1774   if (I.isVolatile())
1775     Root = getRoot();
1776   else {
1777     // Do not serialize non-volatile loads against each other.
1778     Root = DAG.getRoot();
1779   }
1780
1781   setValue(&I, getLoadFrom(I.getType(), Ptr, I.getOperand(0),
1782                            Root, I.isVolatile()));
1783 }
1784
1785 SDOperand SelectionDAGLowering::getLoadFrom(const Type *Ty, SDOperand Ptr,
1786                                             const Value *SV, SDOperand Root,
1787                                             bool isVolatile) {
1788   SDOperand L;
1789   if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
1790     MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
1791     L = DAG.getVecLoad(PTy->getNumElements(), PVT, Root, Ptr,
1792                        DAG.getSrcValue(SV));
1793   } else {
1794     L = DAG.getLoad(TLI.getValueType(Ty), Root, Ptr, SV, 0, isVolatile);
1795   }
1796
1797   if (isVolatile)
1798     DAG.setRoot(L.getValue(1));
1799   else
1800     PendingLoads.push_back(L.getValue(1));
1801   
1802   return L;
1803 }
1804
1805
1806 void SelectionDAGLowering::visitStore(StoreInst &I) {
1807   Value *SrcV = I.getOperand(0);
1808   SDOperand Src = getValue(SrcV);
1809   SDOperand Ptr = getValue(I.getOperand(1));
1810   DAG.setRoot(DAG.getStore(getRoot(), Src, Ptr, I.getOperand(1), 0,
1811                            I.isVolatile()));
1812 }
1813
1814 /// IntrinsicCannotAccessMemory - Return true if the specified intrinsic cannot
1815 /// access memory and has no other side effects at all.
1816 static bool IntrinsicCannotAccessMemory(unsigned IntrinsicID) {
1817 #define GET_NO_MEMORY_INTRINSICS
1818 #include "llvm/Intrinsics.gen"
1819 #undef GET_NO_MEMORY_INTRINSICS
1820   return false;
1821 }
1822
1823 // IntrinsicOnlyReadsMemory - Return true if the specified intrinsic doesn't
1824 // have any side-effects or if it only reads memory.
1825 static bool IntrinsicOnlyReadsMemory(unsigned IntrinsicID) {
1826 #define GET_SIDE_EFFECT_INFO
1827 #include "llvm/Intrinsics.gen"
1828 #undef GET_SIDE_EFFECT_INFO
1829   return false;
1830 }
1831
1832 /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
1833 /// node.
1834 void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, 
1835                                                 unsigned Intrinsic) {
1836   bool HasChain = !IntrinsicCannotAccessMemory(Intrinsic);
1837   bool OnlyLoad = HasChain && IntrinsicOnlyReadsMemory(Intrinsic);
1838   
1839   // Build the operand list.
1840   SmallVector<SDOperand, 8> Ops;
1841   if (HasChain) {  // If this intrinsic has side-effects, chainify it.
1842     if (OnlyLoad) {
1843       // We don't need to serialize loads against other loads.
1844       Ops.push_back(DAG.getRoot());
1845     } else { 
1846       Ops.push_back(getRoot());
1847     }
1848   }
1849   
1850   // Add the intrinsic ID as an integer operand.
1851   Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
1852
1853   // Add all operands of the call to the operand list.
1854   for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1855     SDOperand Op = getValue(I.getOperand(i));
1856     
1857     // If this is a vector type, force it to the right packed type.
1858     if (Op.getValueType() == MVT::Vector) {
1859       const PackedType *OpTy = cast<PackedType>(I.getOperand(i)->getType());
1860       MVT::ValueType EltVT = TLI.getValueType(OpTy->getElementType());
1861       
1862       MVT::ValueType VVT = MVT::getVectorType(EltVT, OpTy->getNumElements());
1863       assert(VVT != MVT::Other && "Intrinsic uses a non-legal type?");
1864       Op = DAG.getNode(ISD::VBIT_CONVERT, VVT, Op);
1865     }
1866     
1867     assert(TLI.isTypeLegal(Op.getValueType()) &&
1868            "Intrinsic uses a non-legal type?");
1869     Ops.push_back(Op);
1870   }
1871
1872   std::vector<MVT::ValueType> VTs;
1873   if (I.getType() != Type::VoidTy) {
1874     MVT::ValueType VT = TLI.getValueType(I.getType());
1875     if (VT == MVT::Vector) {
1876       const PackedType *DestTy = cast<PackedType>(I.getType());
1877       MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType());
1878       
1879       VT = MVT::getVectorType(EltVT, DestTy->getNumElements());
1880       assert(VT != MVT::Other && "Intrinsic uses a non-legal type?");
1881     }
1882     
1883     assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?");
1884     VTs.push_back(VT);
1885   }
1886   if (HasChain)
1887     VTs.push_back(MVT::Other);
1888
1889   const MVT::ValueType *VTList = DAG.getNodeValueTypes(VTs);
1890
1891   // Create the node.
1892   SDOperand Result;
1893   if (!HasChain)
1894     Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VTList, VTs.size(),
1895                          &Ops[0], Ops.size());
1896   else if (I.getType() != Type::VoidTy)
1897     Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, VTList, VTs.size(),
1898                          &Ops[0], Ops.size());
1899   else
1900     Result = DAG.getNode(ISD::INTRINSIC_VOID, VTList, VTs.size(),
1901                          &Ops[0], Ops.size());
1902
1903   if (HasChain) {
1904     SDOperand Chain = Result.getValue(Result.Val->getNumValues()-1);
1905     if (OnlyLoad)
1906       PendingLoads.push_back(Chain);
1907     else
1908       DAG.setRoot(Chain);
1909   }
1910   if (I.getType() != Type::VoidTy) {
1911     if (const PackedType *PTy = dyn_cast<PackedType>(I.getType())) {
1912       MVT::ValueType EVT = TLI.getValueType(PTy->getElementType());
1913       Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
1914                            DAG.getConstant(PTy->getNumElements(), MVT::i32),
1915                            DAG.getValueType(EVT));
1916     } 
1917     setValue(&I, Result);
1918   }
1919 }
1920
1921 /// visitIntrinsicCall - Lower the call to the specified intrinsic function.  If
1922 /// we want to emit this as a call to a named external function, return the name
1923 /// otherwise lower it and return null.
1924 const char *
1925 SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
1926   switch (Intrinsic) {
1927   default:
1928     // By default, turn this into a target intrinsic node.
1929     visitTargetIntrinsic(I, Intrinsic);
1930     return 0;
1931   case Intrinsic::vastart:  visitVAStart(I); return 0;
1932   case Intrinsic::vaend:    visitVAEnd(I); return 0;
1933   case Intrinsic::vacopy:   visitVACopy(I); return 0;
1934   case Intrinsic::returnaddress:
1935     setValue(&I, DAG.getNode(ISD::RETURNADDR, TLI.getPointerTy(),
1936                              getValue(I.getOperand(1))));
1937     return 0;
1938   case Intrinsic::frameaddress:
1939     setValue(&I, DAG.getNode(ISD::FRAMEADDR, TLI.getPointerTy(),
1940                              getValue(I.getOperand(1))));
1941     return 0;
1942   case Intrinsic::setjmp:
1943     return "_setjmp"+!TLI.usesUnderscoreSetJmp();
1944     break;
1945   case Intrinsic::longjmp:
1946     return "_longjmp"+!TLI.usesUnderscoreLongJmp();
1947     break;
1948   case Intrinsic::memcpy_i32:
1949   case Intrinsic::memcpy_i64:
1950     visitMemIntrinsic(I, ISD::MEMCPY);
1951     return 0;
1952   case Intrinsic::memset_i32:
1953   case Intrinsic::memset_i64:
1954     visitMemIntrinsic(I, ISD::MEMSET);
1955     return 0;
1956   case Intrinsic::memmove_i32:
1957   case Intrinsic::memmove_i64:
1958     visitMemIntrinsic(I, ISD::MEMMOVE);
1959     return 0;
1960     
1961   case Intrinsic::dbg_stoppoint: {
1962     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1963     DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
1964     if (MMI && SPI.getContext() && MMI->Verify(SPI.getContext())) {
1965       SDOperand Ops[5];
1966
1967       Ops[0] = getRoot();
1968       Ops[1] = getValue(SPI.getLineValue());
1969       Ops[2] = getValue(SPI.getColumnValue());
1970
1971       DebugInfoDesc *DD = MMI->getDescFor(SPI.getContext());
1972       assert(DD && "Not a debug information descriptor");
1973       CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD);
1974       
1975       Ops[3] = DAG.getString(CompileUnit->getFileName());
1976       Ops[4] = DAG.getString(CompileUnit->getDirectory());
1977       
1978       DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops, 5));
1979     }
1980
1981     return 0;
1982   }
1983   case Intrinsic::dbg_region_start: {
1984     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1985     DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
1986     if (MMI && RSI.getContext() && MMI->Verify(RSI.getContext())) {
1987       unsigned LabelID = MMI->RecordRegionStart(RSI.getContext());
1988       DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getRoot(),
1989                               DAG.getConstant(LabelID, MVT::i32)));
1990     }
1991
1992     return 0;
1993   }
1994   case Intrinsic::dbg_region_end: {
1995     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1996     DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
1997     if (MMI && REI.getContext() && MMI->Verify(REI.getContext())) {
1998       unsigned LabelID = MMI->RecordRegionEnd(REI.getContext());
1999       DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other,
2000                               getRoot(), DAG.getConstant(LabelID, MVT::i32)));
2001     }
2002
2003     return 0;
2004   }
2005   case Intrinsic::dbg_func_start: {
2006     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
2007     DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
2008     if (MMI && FSI.getSubprogram() &&
2009         MMI->Verify(FSI.getSubprogram())) {
2010       unsigned LabelID = MMI->RecordRegionStart(FSI.getSubprogram());
2011       DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other,
2012                   getRoot(), DAG.getConstant(LabelID, MVT::i32)));
2013     }
2014
2015     return 0;
2016   }
2017   case Intrinsic::dbg_declare: {
2018     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
2019     DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
2020     if (MMI && DI.getVariable() && MMI->Verify(DI.getVariable())) {
2021       SDOperand AddressOp  = getValue(DI.getAddress());
2022       if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(AddressOp))
2023         MMI->RecordVariable(DI.getVariable(), FI->getIndex());
2024     }
2025
2026     return 0;
2027   }
2028     
2029   case Intrinsic::sqrt_f32:
2030   case Intrinsic::sqrt_f64:
2031     setValue(&I, DAG.getNode(ISD::FSQRT,
2032                              getValue(I.getOperand(1)).getValueType(),
2033                              getValue(I.getOperand(1))));
2034     return 0;
2035   case Intrinsic::powi_f32:
2036   case Intrinsic::powi_f64:
2037     setValue(&I, DAG.getNode(ISD::FPOWI,
2038                              getValue(I.getOperand(1)).getValueType(),
2039                              getValue(I.getOperand(1)),
2040                              getValue(I.getOperand(2))));
2041     return 0;
2042   case Intrinsic::pcmarker: {
2043     SDOperand Tmp = getValue(I.getOperand(1));
2044     DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp));
2045     return 0;
2046   }
2047   case Intrinsic::readcyclecounter: {
2048     SDOperand Op = getRoot();
2049     SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER,
2050                                 DAG.getNodeValueTypes(MVT::i64, MVT::Other), 2,
2051                                 &Op, 1);
2052     setValue(&I, Tmp);
2053     DAG.setRoot(Tmp.getValue(1));
2054     return 0;
2055   }
2056   case Intrinsic::bswap_i16:
2057   case Intrinsic::bswap_i32:
2058   case Intrinsic::bswap_i64:
2059     setValue(&I, DAG.getNode(ISD::BSWAP,
2060                              getValue(I.getOperand(1)).getValueType(),
2061                              getValue(I.getOperand(1))));
2062     return 0;
2063   case Intrinsic::cttz_i8:
2064   case Intrinsic::cttz_i16:
2065   case Intrinsic::cttz_i32:
2066   case Intrinsic::cttz_i64:
2067     setValue(&I, DAG.getNode(ISD::CTTZ,
2068                              getValue(I.getOperand(1)).getValueType(),
2069                              getValue(I.getOperand(1))));
2070     return 0;
2071   case Intrinsic::ctlz_i8:
2072   case Intrinsic::ctlz_i16:
2073   case Intrinsic::ctlz_i32:
2074   case Intrinsic::ctlz_i64:
2075     setValue(&I, DAG.getNode(ISD::CTLZ,
2076                              getValue(I.getOperand(1)).getValueType(),
2077                              getValue(I.getOperand(1))));
2078     return 0;
2079   case Intrinsic::ctpop_i8:
2080   case Intrinsic::ctpop_i16:
2081   case Intrinsic::ctpop_i32:
2082   case Intrinsic::ctpop_i64:
2083     setValue(&I, DAG.getNode(ISD::CTPOP,
2084                              getValue(I.getOperand(1)).getValueType(),
2085                              getValue(I.getOperand(1))));
2086     return 0;
2087   case Intrinsic::stacksave: {
2088     SDOperand Op = getRoot();
2089     SDOperand Tmp = DAG.getNode(ISD::STACKSAVE,
2090               DAG.getNodeValueTypes(TLI.getPointerTy(), MVT::Other), 2, &Op, 1);
2091     setValue(&I, Tmp);
2092     DAG.setRoot(Tmp.getValue(1));
2093     return 0;
2094   }
2095   case Intrinsic::stackrestore: {
2096     SDOperand Tmp = getValue(I.getOperand(1));
2097     DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp));
2098     return 0;
2099   }
2100   case Intrinsic::prefetch:
2101     // FIXME: Currently discarding prefetches.
2102     return 0;
2103   }
2104 }
2105
2106
2107 void SelectionDAGLowering::visitCall(CallInst &I) {
2108   const char *RenameFn = 0;
2109   if (Function *F = I.getCalledFunction()) {
2110     if (F->isDeclaration())
2111       if (unsigned IID = F->getIntrinsicID()) {
2112         RenameFn = visitIntrinsicCall(I, IID);
2113         if (!RenameFn)
2114           return;
2115       } else {    // Not an LLVM intrinsic.
2116         const std::string &Name = F->getName();
2117         if (Name[0] == 'c' && (Name == "copysign" || Name == "copysignf")) {
2118           if (I.getNumOperands() == 3 &&   // Basic sanity checks.
2119               I.getOperand(1)->getType()->isFloatingPoint() &&
2120               I.getType() == I.getOperand(1)->getType() &&
2121               I.getType() == I.getOperand(2)->getType()) {
2122             SDOperand LHS = getValue(I.getOperand(1));
2123             SDOperand RHS = getValue(I.getOperand(2));
2124             setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(),
2125                                      LHS, RHS));
2126             return;
2127           }
2128         } else if (Name[0] == 'f' && (Name == "fabs" || Name == "fabsf")) {
2129           if (I.getNumOperands() == 2 &&   // Basic sanity checks.
2130               I.getOperand(1)->getType()->isFloatingPoint() &&
2131               I.getType() == I.getOperand(1)->getType()) {
2132             SDOperand Tmp = getValue(I.getOperand(1));
2133             setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
2134             return;
2135           }
2136         } else if (Name[0] == 's' && (Name == "sin" || Name == "sinf")) {
2137           if (I.getNumOperands() == 2 &&   // Basic sanity checks.
2138               I.getOperand(1)->getType()->isFloatingPoint() &&
2139               I.getType() == I.getOperand(1)->getType()) {
2140             SDOperand Tmp = getValue(I.getOperand(1));
2141             setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
2142             return;
2143           }
2144         } else if (Name[0] == 'c' && (Name == "cos" || Name == "cosf")) {
2145           if (I.getNumOperands() == 2 &&   // Basic sanity checks.
2146               I.getOperand(1)->getType()->isFloatingPoint() &&
2147               I.getType() == I.getOperand(1)->getType()) {
2148             SDOperand Tmp = getValue(I.getOperand(1));
2149             setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
2150             return;
2151           }
2152         }
2153       }
2154   } else if (isa<InlineAsm>(I.getOperand(0))) {
2155     visitInlineAsm(I);
2156     return;
2157   }
2158
2159   const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType());
2160   const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
2161
2162   SDOperand Callee;
2163   if (!RenameFn)
2164     Callee = getValue(I.getOperand(0));
2165   else
2166     Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
2167   TargetLowering::ArgListTy Args;
2168   TargetLowering::ArgListEntry Entry;
2169   Args.reserve(I.getNumOperands());
2170   for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2171     Value *Arg = I.getOperand(i);
2172     SDOperand ArgNode = getValue(Arg);
2173     Entry.Node = ArgNode; Entry.Ty = Arg->getType();
2174     Entry.isSigned = FTy->paramHasAttr(i, FunctionType::SExtAttribute);
2175     Entry.isInReg  = FTy->paramHasAttr(i, FunctionType::InRegAttribute);
2176     Entry.isSRet   = FTy->paramHasAttr(i, FunctionType::StructRetAttribute);
2177     Args.push_back(Entry);
2178   }
2179
2180   std::pair<SDOperand,SDOperand> Result =
2181     TLI.LowerCallTo(getRoot(), I.getType(), 
2182                     FTy->paramHasAttr(0,FunctionType::SExtAttribute),
2183                     FTy->isVarArg(), I.getCallingConv(), I.isTailCall(), 
2184                     Callee, Args, DAG);
2185   if (I.getType() != Type::VoidTy)
2186     setValue(&I, Result.first);
2187   DAG.setRoot(Result.second);
2188 }
2189
2190 SDOperand RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
2191                                         SDOperand &Chain, SDOperand &Flag)const{
2192   SDOperand Val = DAG.getCopyFromReg(Chain, Regs[0], RegVT, Flag);
2193   Chain = Val.getValue(1);
2194   Flag  = Val.getValue(2);
2195   
2196   // If the result was expanded, copy from the top part.
2197   if (Regs.size() > 1) {
2198     assert(Regs.size() == 2 &&
2199            "Cannot expand to more than 2 elts yet!");
2200     SDOperand Hi = DAG.getCopyFromReg(Chain, Regs[1], RegVT, Flag);
2201     Chain = Hi.getValue(1);
2202     Flag  = Hi.getValue(2);
2203     if (DAG.getTargetLoweringInfo().isLittleEndian())
2204       return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Val, Hi);
2205     else
2206       return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Hi, Val);
2207   }
2208
2209   // Otherwise, if the return value was promoted or extended, truncate it to the
2210   // appropriate type.
2211   if (RegVT == ValueVT)
2212     return Val;
2213   
2214   if (MVT::isInteger(RegVT)) {
2215     if (ValueVT < RegVT)
2216       return DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
2217     else
2218       return DAG.getNode(ISD::ANY_EXTEND, ValueVT, Val);
2219   } else {
2220     return DAG.getNode(ISD::FP_ROUND, ValueVT, Val);
2221   }
2222 }
2223
2224 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
2225 /// specified value into the registers specified by this object.  This uses 
2226 /// Chain/Flag as the input and updates them for the output Chain/Flag.
2227 void RegsForValue::getCopyToRegs(SDOperand Val, SelectionDAG &DAG,
2228                                  SDOperand &Chain, SDOperand &Flag,
2229                                  MVT::ValueType PtrVT) const {
2230   if (Regs.size() == 1) {
2231     // If there is a single register and the types differ, this must be
2232     // a promotion.
2233     if (RegVT != ValueVT) {
2234       if (MVT::isInteger(RegVT)) {
2235         if (RegVT < ValueVT)
2236           Val = DAG.getNode(ISD::TRUNCATE, RegVT, Val);
2237         else
2238           Val = DAG.getNode(ISD::ANY_EXTEND, RegVT, Val);
2239       } else
2240         Val = DAG.getNode(ISD::FP_EXTEND, RegVT, Val);
2241     }
2242     Chain = DAG.getCopyToReg(Chain, Regs[0], Val, Flag);
2243     Flag = Chain.getValue(1);
2244   } else {
2245     std::vector<unsigned> R(Regs);
2246     if (!DAG.getTargetLoweringInfo().isLittleEndian())
2247       std::reverse(R.begin(), R.end());
2248     
2249     for (unsigned i = 0, e = R.size(); i != e; ++i) {
2250       SDOperand Part = DAG.getNode(ISD::EXTRACT_ELEMENT, RegVT, Val, 
2251                                    DAG.getConstant(i, PtrVT));
2252       Chain = DAG.getCopyToReg(Chain, R[i], Part, Flag);
2253       Flag = Chain.getValue(1);
2254     }
2255   }
2256 }
2257
2258 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
2259 /// operand list.  This adds the code marker and includes the number of 
2260 /// values added into it.
2261 void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
2262                                         std::vector<SDOperand> &Ops) const {
2263   Ops.push_back(DAG.getConstant(Code | (Regs.size() << 3), MVT::i32));
2264   for (unsigned i = 0, e = Regs.size(); i != e; ++i)
2265     Ops.push_back(DAG.getRegister(Regs[i], RegVT));
2266 }
2267
2268 /// isAllocatableRegister - If the specified register is safe to allocate, 
2269 /// i.e. it isn't a stack pointer or some other special register, return the
2270 /// register class for the register.  Otherwise, return null.
2271 static const TargetRegisterClass *
2272 isAllocatableRegister(unsigned Reg, MachineFunction &MF,
2273                       const TargetLowering &TLI, const MRegisterInfo *MRI) {
2274   MVT::ValueType FoundVT = MVT::Other;
2275   const TargetRegisterClass *FoundRC = 0;
2276   for (MRegisterInfo::regclass_iterator RCI = MRI->regclass_begin(),
2277        E = MRI->regclass_end(); RCI != E; ++RCI) {
2278     MVT::ValueType ThisVT = MVT::Other;
2279
2280     const TargetRegisterClass *RC = *RCI;
2281     // If none of the the value types for this register class are valid, we 
2282     // can't use it.  For example, 64-bit reg classes on 32-bit targets.
2283     for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
2284          I != E; ++I) {
2285       if (TLI.isTypeLegal(*I)) {
2286         // If we have already found this register in a different register class,
2287         // choose the one with the largest VT specified.  For example, on
2288         // PowerPC, we favor f64 register classes over f32.
2289         if (FoundVT == MVT::Other || 
2290             MVT::getSizeInBits(FoundVT) < MVT::getSizeInBits(*I)) {
2291           ThisVT = *I;
2292           break;
2293         }
2294       }
2295     }
2296     
2297     if (ThisVT == MVT::Other) continue;
2298     
2299     // NOTE: This isn't ideal.  In particular, this might allocate the
2300     // frame pointer in functions that need it (due to them not being taken
2301     // out of allocation, because a variable sized allocation hasn't been seen
2302     // yet).  This is a slight code pessimization, but should still work.
2303     for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
2304          E = RC->allocation_order_end(MF); I != E; ++I)
2305       if (*I == Reg) {
2306         // We found a matching register class.  Keep looking at others in case
2307         // we find one with larger registers that this physreg is also in.
2308         FoundRC = RC;
2309         FoundVT = ThisVT;
2310         break;
2311       }
2312   }
2313   return FoundRC;
2314 }    
2315
2316 RegsForValue SelectionDAGLowering::
2317 GetRegistersForValue(const std::string &ConstrCode,
2318                      MVT::ValueType VT, bool isOutReg, bool isInReg,
2319                      std::set<unsigned> &OutputRegs, 
2320                      std::set<unsigned> &InputRegs) {
2321   std::pair<unsigned, const TargetRegisterClass*> PhysReg = 
2322     TLI.getRegForInlineAsmConstraint(ConstrCode, VT);
2323   std::vector<unsigned> Regs;
2324
2325   unsigned NumRegs = VT != MVT::Other ? TLI.getNumElements(VT) : 1;
2326   MVT::ValueType RegVT;
2327   MVT::ValueType ValueVT = VT;
2328   
2329   // If this is a constraint for a specific physical register, like {r17},
2330   // assign it now.
2331   if (PhysReg.first) {
2332     if (VT == MVT::Other)
2333       ValueVT = *PhysReg.second->vt_begin();
2334     
2335     // Get the actual register value type.  This is important, because the user
2336     // may have asked for (e.g.) the AX register in i32 type.  We need to
2337     // remember that AX is actually i16 to get the right extension.
2338     RegVT = *PhysReg.second->vt_begin();
2339     
2340     // This is a explicit reference to a physical register.
2341     Regs.push_back(PhysReg.first);
2342
2343     // If this is an expanded reference, add the rest of the regs to Regs.
2344     if (NumRegs != 1) {
2345       TargetRegisterClass::iterator I = PhysReg.second->begin();
2346       TargetRegisterClass::iterator E = PhysReg.second->end();
2347       for (; *I != PhysReg.first; ++I)
2348         assert(I != E && "Didn't find reg!"); 
2349       
2350       // Already added the first reg.
2351       --NumRegs; ++I;
2352       for (; NumRegs; --NumRegs, ++I) {
2353         assert(I != E && "Ran out of registers to allocate!");
2354         Regs.push_back(*I);
2355       }
2356     }
2357     return RegsForValue(Regs, RegVT, ValueVT);
2358   }
2359   
2360   // Otherwise, if this was a reference to an LLVM register class, create vregs
2361   // for this reference.
2362   std::vector<unsigned> RegClassRegs;
2363   if (PhysReg.second) {
2364     // If this is an early clobber or tied register, our regalloc doesn't know
2365     // how to maintain the constraint.  If it isn't, go ahead and create vreg
2366     // and let the regalloc do the right thing.
2367     if (!isOutReg || !isInReg) {
2368       if (VT == MVT::Other)
2369         ValueVT = *PhysReg.second->vt_begin();
2370       RegVT = *PhysReg.second->vt_begin();
2371
2372       // Create the appropriate number of virtual registers.
2373       SSARegMap *RegMap = DAG.getMachineFunction().getSSARegMap();
2374       for (; NumRegs; --NumRegs)
2375         Regs.push_back(RegMap->createVirtualRegister(PhysReg.second));
2376       
2377       return RegsForValue(Regs, RegVT, ValueVT);
2378     }
2379     
2380     // Otherwise, we can't allocate it.  Let the code below figure out how to
2381     // maintain these constraints.
2382     RegClassRegs.assign(PhysReg.second->begin(), PhysReg.second->end());
2383     
2384   } else {
2385     // This is a reference to a register class that doesn't directly correspond
2386     // to an LLVM register class.  Allocate NumRegs consecutive, available,
2387     // registers from the class.
2388     RegClassRegs = TLI.getRegClassForInlineAsmConstraint(ConstrCode, VT);
2389   }
2390
2391   const MRegisterInfo *MRI = DAG.getTarget().getRegisterInfo();
2392   MachineFunction &MF = *CurMBB->getParent();
2393   unsigned NumAllocated = 0;
2394   for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
2395     unsigned Reg = RegClassRegs[i];
2396     // See if this register is available.
2397     if ((isOutReg && OutputRegs.count(Reg)) ||   // Already used.
2398         (isInReg  && InputRegs.count(Reg))) {    // Already used.
2399       // Make sure we find consecutive registers.
2400       NumAllocated = 0;
2401       continue;
2402     }
2403     
2404     // Check to see if this register is allocatable (i.e. don't give out the
2405     // stack pointer).
2406     const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, MRI);
2407     if (!RC) {
2408       // Make sure we find consecutive registers.
2409       NumAllocated = 0;
2410       continue;
2411     }
2412     
2413     // Okay, this register is good, we can use it.
2414     ++NumAllocated;
2415
2416     // If we allocated enough consecutive   
2417     if (NumAllocated == NumRegs) {
2418       unsigned RegStart = (i-NumAllocated)+1;
2419       unsigned RegEnd   = i+1;
2420       // Mark all of the allocated registers used.
2421       for (unsigned i = RegStart; i != RegEnd; ++i) {
2422         unsigned Reg = RegClassRegs[i];
2423         Regs.push_back(Reg);
2424         if (isOutReg) OutputRegs.insert(Reg);    // Mark reg used.
2425         if (isInReg)  InputRegs.insert(Reg);     // Mark reg used.
2426       }
2427       
2428       return RegsForValue(Regs, *RC->vt_begin(), VT);
2429     }
2430   }
2431   
2432   // Otherwise, we couldn't allocate enough registers for this.
2433   return RegsForValue();
2434 }
2435
2436 /// getConstraintGenerality - Return an integer indicating how general CT is.
2437 static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
2438   switch (CT) {
2439   default: assert(0 && "Unknown constraint type!");
2440   case TargetLowering::C_Other:
2441   case TargetLowering::C_Unknown:
2442     return 0;
2443   case TargetLowering::C_Register:
2444     return 1;
2445   case TargetLowering::C_RegisterClass:
2446     return 2;
2447   case TargetLowering::C_Memory:
2448     return 3;
2449   }
2450 }
2451
2452 static std::string GetMostGeneralConstraint(std::vector<std::string> &C,
2453                                             const TargetLowering &TLI) {
2454   assert(!C.empty() && "Must have at least one constraint");
2455   if (C.size() == 1) return C[0];
2456     
2457   std::string *Current = &C[0];
2458   // If we have multiple constraints, try to pick the most general one ahead
2459   // of time.  This isn't a wonderful solution, but handles common cases.
2460   TargetLowering::ConstraintType Flavor = TLI.getConstraintType(Current[0][0]);
2461   for (unsigned j = 1, e = C.size(); j != e; ++j) {
2462     TargetLowering::ConstraintType ThisFlavor = TLI.getConstraintType(C[j][0]);
2463     if (getConstraintGenerality(ThisFlavor) > 
2464         getConstraintGenerality(Flavor)) {
2465       // This constraint letter is more general than the previous one,
2466       // use it.
2467       Flavor = ThisFlavor;
2468       Current = &C[j];
2469     }
2470   }
2471   return *Current;
2472 }
2473
2474
2475 /// visitInlineAsm - Handle a call to an InlineAsm object.
2476 ///
2477 void SelectionDAGLowering::visitInlineAsm(CallInst &I) {
2478   InlineAsm *IA = cast<InlineAsm>(I.getOperand(0));
2479   
2480   SDOperand AsmStr = DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
2481                                                  MVT::Other);
2482
2483   std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
2484   std::vector<MVT::ValueType> ConstraintVTs;
2485   
2486   /// AsmNodeOperands - A list of pairs.  The first element is a register, the
2487   /// second is a bitfield where bit #0 is set if it is a use and bit #1 is set
2488   /// if it is a def of that register.
2489   std::vector<SDOperand> AsmNodeOperands;
2490   AsmNodeOperands.push_back(SDOperand());  // reserve space for input chain
2491   AsmNodeOperands.push_back(AsmStr);
2492   
2493   SDOperand Chain = getRoot();
2494   SDOperand Flag;
2495   
2496   // We fully assign registers here at isel time.  This is not optimal, but
2497   // should work.  For register classes that correspond to LLVM classes, we
2498   // could let the LLVM RA do its thing, but we currently don't.  Do a prepass
2499   // over the constraints, collecting fixed registers that we know we can't use.
2500   std::set<unsigned> OutputRegs, InputRegs;
2501   unsigned OpNum = 1;
2502   for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
2503     std::string ConstraintCode =
2504       GetMostGeneralConstraint(Constraints[i].Codes, TLI);
2505     
2506     MVT::ValueType OpVT;
2507
2508     // Compute the value type for each operand and add it to ConstraintVTs.
2509     switch (Constraints[i].Type) {
2510     case InlineAsm::isOutput:
2511       if (!Constraints[i].isIndirectOutput) {
2512         assert(I.getType() != Type::VoidTy && "Bad inline asm!");
2513         OpVT = TLI.getValueType(I.getType());
2514       } else {
2515         const Type *OpTy = I.getOperand(OpNum)->getType();
2516         OpVT = TLI.getValueType(cast<PointerType>(OpTy)->getElementType());
2517         OpNum++;  // Consumes a call operand.
2518       }
2519       break;
2520     case InlineAsm::isInput:
2521       OpVT = TLI.getValueType(I.getOperand(OpNum)->getType());
2522       OpNum++;  // Consumes a call operand.
2523       break;
2524     case InlineAsm::isClobber:
2525       OpVT = MVT::Other;
2526       break;
2527     }
2528     
2529     ConstraintVTs.push_back(OpVT);
2530
2531     if (TLI.getRegForInlineAsmConstraint(ConstraintCode, OpVT).first == 0)
2532       continue;  // Not assigned a fixed reg.
2533     
2534     // Build a list of regs that this operand uses.  This always has a single
2535     // element for promoted/expanded operands.
2536     RegsForValue Regs = GetRegistersForValue(ConstraintCode, OpVT,
2537                                              false, false,
2538                                              OutputRegs, InputRegs);
2539     
2540     switch (Constraints[i].Type) {
2541     case InlineAsm::isOutput:
2542       // We can't assign any other output to this register.
2543       OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
2544       // If this is an early-clobber output, it cannot be assigned to the same
2545       // value as the input reg.
2546       if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput)
2547         InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
2548       break;
2549     case InlineAsm::isInput:
2550       // We can't assign any other input to this register.
2551       InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
2552       break;
2553     case InlineAsm::isClobber:
2554       // Clobbered regs cannot be used as inputs or outputs.
2555       InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
2556       OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
2557       break;
2558     }
2559   }      
2560   
2561   // Loop over all of the inputs, copying the operand values into the
2562   // appropriate registers and processing the output regs.
2563   RegsForValue RetValRegs;
2564   std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
2565   OpNum = 1;
2566   
2567   for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
2568     std::string ConstraintCode =
2569       GetMostGeneralConstraint(Constraints[i].Codes, TLI);
2570
2571     switch (Constraints[i].Type) {
2572     case InlineAsm::isOutput: {
2573       TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass;
2574       if (ConstraintCode.size() == 1)   // not a physreg name.
2575         CTy = TLI.getConstraintType(ConstraintCode[0]);
2576       
2577       if (CTy == TargetLowering::C_Memory) {
2578         // Memory output.
2579         SDOperand InOperandVal = getValue(I.getOperand(OpNum));
2580         
2581         // Check that the operand (the address to store to) isn't a float.
2582         if (!MVT::isInteger(InOperandVal.getValueType()))
2583           assert(0 && "MATCH FAIL!");
2584         
2585         if (!Constraints[i].isIndirectOutput)
2586           assert(0 && "MATCH FAIL!");
2587
2588         OpNum++;  // Consumes a call operand.
2589         
2590         // Extend/truncate to the right pointer type if needed.
2591         MVT::ValueType PtrType = TLI.getPointerTy();
2592         if (InOperandVal.getValueType() < PtrType)
2593           InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal);
2594         else if (InOperandVal.getValueType() > PtrType)
2595           InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal);
2596         
2597         // Add information to the INLINEASM node to know about this output.
2598         unsigned ResOpType = 4/*MEM*/ | (1 << 3);
2599         AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
2600         AsmNodeOperands.push_back(InOperandVal);
2601         break;
2602       }
2603
2604       // Otherwise, this is a register output.
2605       assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!");
2606
2607       // If this is an early-clobber output, or if there is an input
2608       // constraint that matches this, we need to reserve the input register
2609       // so no other inputs allocate to it.
2610       bool UsesInputRegister = false;
2611       if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput)
2612         UsesInputRegister = true;
2613       
2614       // Copy the output from the appropriate register.  Find a register that
2615       // we can use.
2616       RegsForValue Regs =
2617         GetRegistersForValue(ConstraintCode, ConstraintVTs[i],
2618                              true, UsesInputRegister, 
2619                              OutputRegs, InputRegs);
2620       if (Regs.Regs.empty()) {
2621         cerr << "Couldn't allocate output reg for contraint '"
2622              << ConstraintCode << "'!\n";
2623         exit(1);
2624       }
2625
2626       if (!Constraints[i].isIndirectOutput) {
2627         assert(RetValRegs.Regs.empty() &&
2628                "Cannot have multiple output constraints yet!");
2629         assert(I.getType() != Type::VoidTy && "Bad inline asm!");
2630         RetValRegs = Regs;
2631       } else {
2632         IndirectStoresToEmit.push_back(std::make_pair(Regs, 
2633                                                       I.getOperand(OpNum)));
2634         OpNum++;  // Consumes a call operand.
2635       }
2636       
2637       // Add information to the INLINEASM node to know that this register is
2638       // set.
2639       Regs.AddInlineAsmOperands(2 /*REGDEF*/, DAG, AsmNodeOperands);
2640       break;
2641     }
2642     case InlineAsm::isInput: {
2643       SDOperand InOperandVal = getValue(I.getOperand(OpNum));
2644       OpNum++;  // Consumes a call operand.
2645       
2646       if (isdigit(ConstraintCode[0])) {    // Matching constraint?
2647         // If this is required to match an output register we have already set,
2648         // just use its register.
2649         unsigned OperandNo = atoi(ConstraintCode.c_str());
2650         
2651         // Scan until we find the definition we already emitted of this operand.
2652         // When we find it, create a RegsForValue operand.
2653         unsigned CurOp = 2;  // The first operand.
2654         for (; OperandNo; --OperandNo) {
2655           // Advance to the next operand.
2656           unsigned NumOps = 
2657             cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue();
2658           assert(((NumOps & 7) == 2 /*REGDEF*/ ||
2659                   (NumOps & 7) == 4 /*MEM*/) &&
2660                  "Skipped past definitions?");
2661           CurOp += (NumOps>>3)+1;
2662         }
2663
2664         unsigned NumOps = 
2665           cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue();
2666         if ((NumOps & 7) == 2 /*REGDEF*/) {
2667           // Add NumOps>>3 registers to MatchedRegs.
2668           RegsForValue MatchedRegs;
2669           MatchedRegs.ValueVT = InOperandVal.getValueType();
2670           MatchedRegs.RegVT   = AsmNodeOperands[CurOp+1].getValueType();
2671           for (unsigned i = 0, e = NumOps>>3; i != e; ++i) {
2672             unsigned Reg =
2673               cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg();
2674             MatchedRegs.Regs.push_back(Reg);
2675           }
2676         
2677           // Use the produced MatchedRegs object to 
2678           MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag,
2679                                     TLI.getPointerTy());
2680           MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands);
2681           break;
2682         } else {
2683           assert((NumOps & 7) == 4/*MEM*/ && "Unknown matching constraint!");
2684           assert(0 && "matching constraints for memory operands unimp");
2685         }
2686       }
2687       
2688       TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass;
2689       if (ConstraintCode.size() == 1)   // not a physreg name.
2690         CTy = TLI.getConstraintType(ConstraintCode[0]);
2691         
2692       if (CTy == TargetLowering::C_Other) {
2693         InOperandVal = TLI.isOperandValidForConstraint(InOperandVal,
2694                                                        ConstraintCode[0], DAG);
2695         if (!InOperandVal.Val) {
2696           cerr << "Invalid operand for inline asm constraint '"
2697                << ConstraintCode << "'!\n";
2698           exit(1);
2699         }
2700         
2701         // Add information to the INLINEASM node to know about this input.
2702         unsigned ResOpType = 3 /*IMM*/ | (1 << 3);
2703         AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
2704         AsmNodeOperands.push_back(InOperandVal);
2705         break;
2706       } else if (CTy == TargetLowering::C_Memory) {
2707         // Memory input.
2708         
2709         // Check that the operand isn't a float.
2710         if (!MVT::isInteger(InOperandVal.getValueType()))
2711           assert(0 && "MATCH FAIL!");
2712         
2713         // Extend/truncate to the right pointer type if needed.
2714         MVT::ValueType PtrType = TLI.getPointerTy();
2715         if (InOperandVal.getValueType() < PtrType)
2716           InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal);
2717         else if (InOperandVal.getValueType() > PtrType)
2718           InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal);
2719
2720         // Add information to the INLINEASM node to know about this input.
2721         unsigned ResOpType = 4/*MEM*/ | (1 << 3);
2722         AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
2723         AsmNodeOperands.push_back(InOperandVal);
2724         break;
2725       }
2726         
2727       assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!");
2728
2729       // Copy the input into the appropriate registers.
2730       RegsForValue InRegs =
2731         GetRegistersForValue(ConstraintCode, ConstraintVTs[i],
2732                              false, true, OutputRegs, InputRegs);
2733       // FIXME: should be match fail.
2734       assert(!InRegs.Regs.empty() && "Couldn't allocate input reg!");
2735
2736       InRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag, TLI.getPointerTy());
2737       
2738       InRegs.AddInlineAsmOperands(1/*REGUSE*/, DAG, AsmNodeOperands);
2739       break;
2740     }
2741     case InlineAsm::isClobber: {
2742       RegsForValue ClobberedRegs =
2743         GetRegistersForValue(ConstraintCode, MVT::Other, false, false,
2744                              OutputRegs, InputRegs);
2745       // Add the clobbered value to the operand list, so that the register
2746       // allocator is aware that the physreg got clobbered.
2747       if (!ClobberedRegs.Regs.empty())
2748         ClobberedRegs.AddInlineAsmOperands(2/*REGDEF*/, DAG, AsmNodeOperands);
2749       break;
2750     }
2751     }
2752   }
2753   
2754   // Finish up input operands.
2755   AsmNodeOperands[0] = Chain;
2756   if (Flag.Val) AsmNodeOperands.push_back(Flag);
2757   
2758   Chain = DAG.getNode(ISD::INLINEASM, 
2759                       DAG.getNodeValueTypes(MVT::Other, MVT::Flag), 2,
2760                       &AsmNodeOperands[0], AsmNodeOperands.size());
2761   Flag = Chain.getValue(1);
2762
2763   // If this asm returns a register value, copy the result from that register
2764   // and set it as the value of the call.
2765   if (!RetValRegs.Regs.empty())
2766     setValue(&I, RetValRegs.getCopyFromRegs(DAG, Chain, Flag));
2767   
2768   std::vector<std::pair<SDOperand, Value*> > StoresToEmit;
2769   
2770   // Process indirect outputs, first output all of the flagged copies out of
2771   // physregs.
2772   for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
2773     RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
2774     Value *Ptr = IndirectStoresToEmit[i].second;
2775     SDOperand OutVal = OutRegs.getCopyFromRegs(DAG, Chain, Flag);
2776     StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
2777   }
2778   
2779   // Emit the non-flagged stores from the physregs.
2780   SmallVector<SDOperand, 8> OutChains;
2781   for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
2782     OutChains.push_back(DAG.getStore(Chain,  StoresToEmit[i].first,
2783                                     getValue(StoresToEmit[i].second),
2784                                     StoresToEmit[i].second, 0));
2785   if (!OutChains.empty())
2786     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2787                         &OutChains[0], OutChains.size());
2788   DAG.setRoot(Chain);
2789 }
2790
2791
2792 void SelectionDAGLowering::visitMalloc(MallocInst &I) {
2793   SDOperand Src = getValue(I.getOperand(0));
2794
2795   MVT::ValueType IntPtr = TLI.getPointerTy();
2796
2797   if (IntPtr < Src.getValueType())
2798     Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src);
2799   else if (IntPtr > Src.getValueType())
2800     Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
2801
2802   // Scale the source by the type size.
2803   uint64_t ElementSize = TD->getTypeSize(I.getType()->getElementType());
2804   Src = DAG.getNode(ISD::MUL, Src.getValueType(),
2805                     Src, getIntPtrConstant(ElementSize));
2806
2807   TargetLowering::ArgListTy Args;
2808   TargetLowering::ArgListEntry Entry;
2809   Entry.Node = Src;
2810   Entry.Ty = TLI.getTargetData()->getIntPtrType();
2811   Entry.isSigned = false;
2812   Entry.isInReg = false;
2813   Entry.isSRet = false;
2814   Args.push_back(Entry);
2815
2816   std::pair<SDOperand,SDOperand> Result =
2817     TLI.LowerCallTo(getRoot(), I.getType(), false, false, CallingConv::C, true,
2818                     DAG.getExternalSymbol("malloc", IntPtr),
2819                     Args, DAG);
2820   setValue(&I, Result.first);  // Pointers always fit in registers
2821   DAG.setRoot(Result.second);
2822 }
2823
2824 void SelectionDAGLowering::visitFree(FreeInst &I) {
2825   TargetLowering::ArgListTy Args;
2826   TargetLowering::ArgListEntry Entry;
2827   Entry.Node = getValue(I.getOperand(0));
2828   Entry.Ty = TLI.getTargetData()->getIntPtrType();
2829   Entry.isSigned = false;
2830   Entry.isInReg = false;
2831   Entry.isSRet = false;
2832   Args.push_back(Entry);
2833   MVT::ValueType IntPtr = TLI.getPointerTy();
2834   std::pair<SDOperand,SDOperand> Result =
2835     TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, CallingConv::C, true,
2836                     DAG.getExternalSymbol("free", IntPtr), Args, DAG);
2837   DAG.setRoot(Result.second);
2838 }
2839
2840 // InsertAtEndOfBasicBlock - This method should be implemented by targets that
2841 // mark instructions with the 'usesCustomDAGSchedInserter' flag.  These
2842 // instructions are special in various ways, which require special support to
2843 // insert.  The specified MachineInstr is created but not inserted into any
2844 // basic blocks, and the scheduler passes ownership of it to this method.
2845 MachineBasicBlock *TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
2846                                                        MachineBasicBlock *MBB) {
2847   cerr << "If a target marks an instruction with "
2848        << "'usesCustomDAGSchedInserter', it must implement "
2849        << "TargetLowering::InsertAtEndOfBasicBlock!\n";
2850   abort();
2851   return 0;  
2852 }
2853
2854 void SelectionDAGLowering::visitVAStart(CallInst &I) {
2855   DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(), 
2856                           getValue(I.getOperand(1)), 
2857                           DAG.getSrcValue(I.getOperand(1))));
2858 }
2859
2860 void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
2861   SDOperand V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(),
2862                              getValue(I.getOperand(0)),
2863                              DAG.getSrcValue(I.getOperand(0)));
2864   setValue(&I, V);
2865   DAG.setRoot(V.getValue(1));
2866 }
2867
2868 void SelectionDAGLowering::visitVAEnd(CallInst &I) {
2869   DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(),
2870                           getValue(I.getOperand(1)), 
2871                           DAG.getSrcValue(I.getOperand(1))));
2872 }
2873
2874 void SelectionDAGLowering::visitVACopy(CallInst &I) {
2875   DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(), 
2876                           getValue(I.getOperand(1)), 
2877                           getValue(I.getOperand(2)),
2878                           DAG.getSrcValue(I.getOperand(1)),
2879                           DAG.getSrcValue(I.getOperand(2))));
2880 }
2881
2882 /// ExpandScalarFormalArgs - Recursively expand the formal_argument node, either
2883 /// bit_convert it or join a pair of them with a BUILD_PAIR when appropriate.
2884 static SDOperand ExpandScalarFormalArgs(MVT::ValueType VT, SDNode *Arg,
2885                                         unsigned &i, SelectionDAG &DAG,
2886                                         TargetLowering &TLI) {
2887   if (TLI.getTypeAction(VT) != TargetLowering::Expand)
2888     return SDOperand(Arg, i++);
2889
2890   MVT::ValueType EVT = TLI.getTypeToTransformTo(VT);
2891   unsigned NumVals = MVT::getSizeInBits(VT) / MVT::getSizeInBits(EVT);
2892   if (NumVals == 1) {
2893     return DAG.getNode(ISD::BIT_CONVERT, VT,
2894                        ExpandScalarFormalArgs(EVT, Arg, i, DAG, TLI));
2895   } else if (NumVals == 2) {
2896     SDOperand Lo = ExpandScalarFormalArgs(EVT, Arg, i, DAG, TLI);
2897     SDOperand Hi = ExpandScalarFormalArgs(EVT, Arg, i, DAG, TLI);
2898     if (!TLI.isLittleEndian())
2899       std::swap(Lo, Hi);
2900     return DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
2901   } else {
2902     // Value scalarized into many values.  Unimp for now.
2903     assert(0 && "Cannot expand i64 -> i16 yet!");
2904   }
2905   return SDOperand();
2906 }
2907
2908 /// TargetLowering::LowerArguments - This is the default LowerArguments
2909 /// implementation, which just inserts a FORMAL_ARGUMENTS node.  FIXME: When all
2910 /// targets are migrated to using FORMAL_ARGUMENTS, this hook should be 
2911 /// integrated into SDISel.
2912 std::vector<SDOperand> 
2913 TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
2914   const FunctionType *FTy = F.getFunctionType();
2915   // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node.
2916   std::vector<SDOperand> Ops;
2917   Ops.push_back(DAG.getRoot());
2918   Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy()));
2919   Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy()));
2920
2921   // Add one result value for each formal argument.
2922   std::vector<MVT::ValueType> RetVals;
2923   unsigned j = 1;
2924   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
2925        I != E; ++I, ++j) {
2926     MVT::ValueType VT = getValueType(I->getType());
2927     bool isInReg = FTy->paramHasAttr(j, FunctionType::InRegAttribute);
2928     bool isSRet  = FTy->paramHasAttr(j, FunctionType::StructRetAttribute);
2929     unsigned Flags = (isInReg << 1) | (isSRet << 2);
2930     
2931     switch (getTypeAction(VT)) {
2932     default: assert(0 && "Unknown type action!");
2933     case Legal: 
2934       RetVals.push_back(VT);
2935       Ops.push_back(DAG.getConstant(Flags, MVT::i32));
2936       break;
2937     case Promote:
2938       RetVals.push_back(getTypeToTransformTo(VT));
2939       Ops.push_back(DAG.getConstant(Flags, MVT::i32));
2940       break;
2941     case Expand:
2942       if (VT != MVT::Vector) {
2943         // If this is a large integer, it needs to be broken up into small
2944         // integers.  Figure out what the destination type is and how many small
2945         // integers it turns into.
2946         MVT::ValueType NVT = getTypeToExpandTo(VT);
2947         unsigned NumVals = getNumElements(VT);
2948         for (unsigned i = 0; i != NumVals; ++i) {
2949           RetVals.push_back(NVT);
2950           Ops.push_back(DAG.getConstant(Flags, MVT::i32));
2951         }
2952       } else {
2953         // Otherwise, this is a vector type.  We only support legal vectors
2954         // right now.
2955         unsigned NumElems = cast<PackedType>(I->getType())->getNumElements();
2956         const Type *EltTy = cast<PackedType>(I->getType())->getElementType();
2957
2958         // Figure out if there is a Packed type corresponding to this Vector
2959         // type.  If so, convert to the packed type.
2960         MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems);
2961         if (TVT != MVT::Other && isTypeLegal(TVT)) {
2962           RetVals.push_back(TVT);
2963           Ops.push_back(DAG.getConstant(Flags, MVT::i32));
2964         } else {
2965           assert(0 && "Don't support illegal by-val vector arguments yet!");
2966         }
2967       }
2968       break;
2969     }
2970   }
2971
2972   RetVals.push_back(MVT::Other);
2973   
2974   // Create the node.
2975   SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS,
2976                                DAG.getNodeValueTypes(RetVals), RetVals.size(),
2977                                &Ops[0], Ops.size()).Val;
2978   
2979   DAG.setRoot(SDOperand(Result, Result->getNumValues()-1));
2980
2981   // Set up the return result vector.
2982   Ops.clear();
2983   unsigned i = 0;
2984   unsigned Idx = 1;
2985   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; 
2986       ++I, ++Idx) {
2987     MVT::ValueType VT = getValueType(I->getType());
2988     
2989     switch (getTypeAction(VT)) {
2990     default: assert(0 && "Unknown type action!");
2991     case Legal: 
2992       Ops.push_back(SDOperand(Result, i++));
2993       break;
2994     case Promote: {
2995       SDOperand Op(Result, i++);
2996       if (MVT::isInteger(VT)) {
2997         if (FTy->paramHasAttr(Idx, FunctionType::SExtAttribute))
2998           Op = DAG.getNode(ISD::AssertSext, Op.getValueType(), Op,
2999                            DAG.getValueType(VT));
3000         else if (FTy->paramHasAttr(Idx, FunctionType::ZExtAttribute))
3001           Op = DAG.getNode(ISD::AssertZext, Op.getValueType(), Op,
3002                            DAG.getValueType(VT));
3003         Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3004       } else {
3005         assert(MVT::isFloatingPoint(VT) && "Not int or FP?");
3006         Op = DAG.getNode(ISD::FP_ROUND, VT, Op);
3007       }
3008       Ops.push_back(Op);
3009       break;
3010     }
3011     case Expand:
3012       if (VT != MVT::Vector) {
3013         // If this is a large integer or a floating point node that needs to be
3014         // expanded, it needs to be reassembled from small integers.  Figure out
3015         // what the source elt type is and how many small integers it is.
3016         Ops.push_back(ExpandScalarFormalArgs(VT, Result, i, DAG, *this));
3017       } else {
3018         // Otherwise, this is a vector type.  We only support legal vectors
3019         // right now.
3020         const PackedType *PTy = cast<PackedType>(I->getType());
3021         unsigned NumElems = PTy->getNumElements();
3022         const Type *EltTy = PTy->getElementType();
3023
3024         // Figure out if there is a Packed type corresponding to this Vector
3025         // type.  If so, convert to the packed type.
3026         MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems);
3027         if (TVT != MVT::Other && isTypeLegal(TVT)) {
3028           SDOperand N = SDOperand(Result, i++);
3029           // Handle copies from generic vectors to registers.
3030           N = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, N,
3031                           DAG.getConstant(NumElems, MVT::i32), 
3032                           DAG.getValueType(getValueType(EltTy)));
3033           Ops.push_back(N);
3034         } else {
3035           assert(0 && "Don't support illegal by-val vector arguments yet!");
3036           abort();
3037         }
3038       }
3039       break;
3040     }
3041   }
3042   return Ops;
3043 }
3044
3045
3046 /// ExpandScalarCallArgs - Recursively expand call argument node by
3047 /// bit_converting it or extract a pair of elements from the larger  node.
3048 static void ExpandScalarCallArgs(MVT::ValueType VT, SDOperand Arg,
3049                                  unsigned Flags, 
3050                                  SmallVector<SDOperand, 32> &Ops,
3051                                  SelectionDAG &DAG,
3052                                  TargetLowering &TLI) {
3053   if (TLI.getTypeAction(VT) != TargetLowering::Expand) {
3054     Ops.push_back(Arg);
3055     Ops.push_back(DAG.getConstant(Flags, MVT::i32));
3056     return;
3057   }
3058
3059   MVT::ValueType EVT = TLI.getTypeToTransformTo(VT);
3060   unsigned NumVals = MVT::getSizeInBits(VT) / MVT::getSizeInBits(EVT);
3061   if (NumVals == 1) {
3062     Arg = DAG.getNode(ISD::BIT_CONVERT, EVT, Arg);
3063     ExpandScalarCallArgs(EVT, Arg, Flags, Ops, DAG, TLI);
3064   } else if (NumVals == 2) {
3065     SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, EVT, Arg,
3066                                DAG.getConstant(0, TLI.getPointerTy()));
3067     SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, EVT, Arg,
3068                                DAG.getConstant(1, TLI.getPointerTy()));
3069     if (!TLI.isLittleEndian())
3070       std::swap(Lo, Hi);
3071     ExpandScalarCallArgs(EVT, Lo, Flags, Ops, DAG, TLI);
3072     ExpandScalarCallArgs(EVT, Hi, Flags, Ops, DAG, TLI);
3073   } else {
3074     // Value scalarized into many values.  Unimp for now.
3075     assert(0 && "Cannot expand i64 -> i16 yet!");
3076   }
3077 }
3078
3079 /// TargetLowering::LowerCallTo - This is the default LowerCallTo
3080 /// implementation, which just inserts an ISD::CALL node, which is later custom
3081 /// lowered by the target to something concrete.  FIXME: When all targets are
3082 /// migrated to using ISD::CALL, this hook should be integrated into SDISel.
3083 std::pair<SDOperand, SDOperand>
3084 TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy, 
3085                             bool RetTyIsSigned, bool isVarArg,
3086                             unsigned CallingConv, bool isTailCall, 
3087                             SDOperand Callee,
3088                             ArgListTy &Args, SelectionDAG &DAG) {
3089   SmallVector<SDOperand, 32> Ops;
3090   Ops.push_back(Chain);   // Op#0 - Chain
3091   Ops.push_back(DAG.getConstant(CallingConv, getPointerTy())); // Op#1 - CC
3092   Ops.push_back(DAG.getConstant(isVarArg, getPointerTy()));    // Op#2 - VarArg
3093   Ops.push_back(DAG.getConstant(isTailCall, getPointerTy()));  // Op#3 - Tail
3094   Ops.push_back(Callee);
3095   
3096   // Handle all of the outgoing arguments.
3097   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
3098     MVT::ValueType VT = getValueType(Args[i].Ty);
3099     SDOperand Op = Args[i].Node;
3100     bool isSigned = Args[i].isSigned;
3101     bool isInReg = Args[i].isInReg;
3102     bool isSRet  = Args[i].isSRet; 
3103     unsigned Flags = (isSRet << 2) | (isInReg << 1) | isSigned;
3104     switch (getTypeAction(VT)) {
3105     default: assert(0 && "Unknown type action!");
3106     case Legal: 
3107       Ops.push_back(Op);
3108       Ops.push_back(DAG.getConstant(Flags, MVT::i32));
3109       break;
3110     case Promote:
3111       if (MVT::isInteger(VT)) {
3112         unsigned ExtOp = isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 
3113         Op = DAG.getNode(ExtOp, getTypeToTransformTo(VT), Op);
3114       } else {
3115         assert(MVT::isFloatingPoint(VT) && "Not int or FP?");
3116         Op = DAG.getNode(ISD::FP_EXTEND, getTypeToTransformTo(VT), Op);
3117       }
3118       Ops.push_back(Op);
3119       Ops.push_back(DAG.getConstant(Flags, MVT::i32));
3120       break;
3121     case Expand:
3122       if (VT != MVT::Vector) {
3123         // If this is a large integer, it needs to be broken down into small
3124         // integers.  Figure out what the source elt type is and how many small
3125         // integers it is.
3126         ExpandScalarCallArgs(VT, Op, Flags, Ops, DAG, *this);
3127       } else {
3128         // Otherwise, this is a vector type.  We only support legal vectors
3129         // right now.
3130         const PackedType *PTy = cast<PackedType>(Args[i].Ty);
3131         unsigned NumElems = PTy->getNumElements();
3132         const Type *EltTy = PTy->getElementType();
3133         
3134         // Figure out if there is a Packed type corresponding to this Vector
3135         // type.  If so, convert to the packed type.
3136         MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems);
3137         if (TVT != MVT::Other && isTypeLegal(TVT)) {
3138           // Insert a VBIT_CONVERT of the MVT::Vector type to the packed type.
3139           Op = DAG.getNode(ISD::VBIT_CONVERT, TVT, Op);
3140           Ops.push_back(Op);
3141           Ops.push_back(DAG.getConstant(Flags, MVT::i32));
3142         } else {
3143           assert(0 && "Don't support illegal by-val vector call args yet!");
3144           abort();
3145         }
3146       }
3147       break;
3148     }
3149   }
3150   
3151   // Figure out the result value types.
3152   SmallVector<MVT::ValueType, 4> RetTys;
3153
3154   if (RetTy != Type::VoidTy) {
3155     MVT::ValueType VT = getValueType(RetTy);
3156     switch (getTypeAction(VT)) {
3157     default: assert(0 && "Unknown type action!");
3158     case Legal:
3159       RetTys.push_back(VT);
3160       break;
3161     case Promote:
3162       RetTys.push_back(getTypeToTransformTo(VT));
3163       break;
3164     case Expand:
3165       if (VT != MVT::Vector) {
3166         // If this is a large integer, it needs to be reassembled from small
3167         // integers.  Figure out what the source elt type is and how many small
3168         // integers it is.
3169         MVT::ValueType NVT = getTypeToExpandTo(VT);
3170         unsigned NumVals = getNumElements(VT);
3171         for (unsigned i = 0; i != NumVals; ++i)
3172           RetTys.push_back(NVT);
3173       } else {
3174         // Otherwise, this is a vector type.  We only support legal vectors
3175         // right now.
3176         const PackedType *PTy = cast<PackedType>(RetTy);
3177         unsigned NumElems = PTy->getNumElements();
3178         const Type *EltTy = PTy->getElementType();
3179         
3180         // Figure out if there is a Packed type corresponding to this Vector
3181         // type.  If so, convert to the packed type.
3182         MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems);
3183         if (TVT != MVT::Other && isTypeLegal(TVT)) {
3184           RetTys.push_back(TVT);
3185         } else {
3186           assert(0 && "Don't support illegal by-val vector call results yet!");
3187           abort();
3188         }
3189       }
3190     }    
3191   }
3192   
3193   RetTys.push_back(MVT::Other);  // Always has a chain.
3194   
3195   // Finally, create the CALL node.
3196   SDOperand Res = DAG.getNode(ISD::CALL,
3197                               DAG.getVTList(&RetTys[0], RetTys.size()),
3198                               &Ops[0], Ops.size());
3199   
3200   // This returns a pair of operands.  The first element is the
3201   // return value for the function (if RetTy is not VoidTy).  The second
3202   // element is the outgoing token chain.
3203   SDOperand ResVal;
3204   if (RetTys.size() != 1) {
3205     MVT::ValueType VT = getValueType(RetTy);
3206     if (RetTys.size() == 2) {
3207       ResVal = Res;
3208       
3209       // If this value was promoted, truncate it down.
3210       if (ResVal.getValueType() != VT) {
3211         if (VT == MVT::Vector) {
3212           // Insert a VBITCONVERT to convert from the packed result type to the
3213           // MVT::Vector type.
3214           unsigned NumElems = cast<PackedType>(RetTy)->getNumElements();
3215           const Type *EltTy = cast<PackedType>(RetTy)->getElementType();
3216           
3217           // Figure out if there is a Packed type corresponding to this Vector
3218           // type.  If so, convert to the packed type.
3219           MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy),NumElems);
3220           if (TVT != MVT::Other && isTypeLegal(TVT)) {
3221             // Insert a VBIT_CONVERT of the FORMAL_ARGUMENTS to a
3222             // "N x PTyElementVT" MVT::Vector type.
3223             ResVal = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, ResVal,
3224                                  DAG.getConstant(NumElems, MVT::i32), 
3225                                  DAG.getValueType(getValueType(EltTy)));
3226           } else {
3227             abort();
3228           }
3229         } else if (MVT::isInteger(VT)) {
3230           unsigned AssertOp = ISD::AssertSext;
3231           if (!RetTyIsSigned)
3232             AssertOp = ISD::AssertZext;
3233           ResVal = DAG.getNode(AssertOp, ResVal.getValueType(), ResVal, 
3234                                DAG.getValueType(VT));
3235           ResVal = DAG.getNode(ISD::TRUNCATE, VT, ResVal);
3236         } else {
3237           assert(MVT::isFloatingPoint(VT));
3238           if (getTypeAction(VT) == Expand)
3239             ResVal = DAG.getNode(ISD::BIT_CONVERT, VT, ResVal);
3240           else
3241             ResVal = DAG.getNode(ISD::FP_ROUND, VT, ResVal);
3242         }
3243       }
3244     } else if (RetTys.size() == 3) {
3245       ResVal = DAG.getNode(ISD::BUILD_PAIR, VT, 
3246                            Res.getValue(0), Res.getValue(1));
3247       
3248     } else {
3249       assert(0 && "Case not handled yet!");
3250     }
3251   }
3252   
3253   return std::make_pair(ResVal, Res.getValue(Res.Val->getNumValues()-1));
3254 }
3255
3256 SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3257   assert(0 && "LowerOperation not implemented for this target!");
3258   abort();
3259   return SDOperand();
3260 }
3261
3262 SDOperand TargetLowering::CustomPromoteOperation(SDOperand Op,
3263                                                  SelectionDAG &DAG) {
3264   assert(0 && "CustomPromoteOperation not implemented for this target!");
3265   abort();
3266   return SDOperand();
3267 }
3268
3269 /// getMemsetValue - Vectorized representation of the memset value
3270 /// operand.
3271 static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT,
3272                                 SelectionDAG &DAG) {
3273   MVT::ValueType CurVT = VT;
3274   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
3275     uint64_t Val   = C->getValue() & 255;
3276     unsigned Shift = 8;
3277     while (CurVT != MVT::i8) {
3278       Val = (Val << Shift) | Val;
3279       Shift <<= 1;
3280       CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
3281     }
3282     return DAG.getConstant(Val, VT);
3283   } else {
3284     Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
3285     unsigned Shift = 8;
3286     while (CurVT != MVT::i8) {
3287       Value =
3288         DAG.getNode(ISD::OR, VT,
3289                     DAG.getNode(ISD::SHL, VT, Value,
3290                                 DAG.getConstant(Shift, MVT::i8)), Value);
3291       Shift <<= 1;
3292       CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
3293     }
3294
3295     return Value;
3296   }
3297 }
3298
3299 /// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3300 /// used when a memcpy is turned into a memset when the source is a constant
3301 /// string ptr.
3302 static SDOperand getMemsetStringVal(MVT::ValueType VT,
3303                                     SelectionDAG &DAG, TargetLowering &TLI,
3304                                     std::string &Str, unsigned Offset) {
3305   uint64_t Val = 0;
3306   unsigned MSB = getSizeInBits(VT) / 8;
3307   if (TLI.isLittleEndian())
3308     Offset = Offset + MSB - 1;
3309   for (unsigned i = 0; i != MSB; ++i) {
3310     Val = (Val << 8) | (unsigned char)Str[Offset];
3311     Offset += TLI.isLittleEndian() ? -1 : 1;
3312   }
3313   return DAG.getConstant(Val, VT);
3314 }
3315
3316 /// getMemBasePlusOffset - Returns base and offset node for the 
3317 static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
3318                                       SelectionDAG &DAG, TargetLowering &TLI) {
3319   MVT::ValueType VT = Base.getValueType();
3320   return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
3321 }
3322
3323 /// MeetsMaxMemopRequirement - Determines if the number of memory ops required
3324 /// to replace the memset / memcpy is below the threshold. It also returns the
3325 /// types of the sequence of  memory ops to perform memset / memcpy.
3326 static bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps,
3327                                      unsigned Limit, uint64_t Size,
3328                                      unsigned Align, TargetLowering &TLI) {
3329   MVT::ValueType VT;
3330
3331   if (TLI.allowsUnalignedMemoryAccesses()) {
3332     VT = MVT::i64;
3333   } else {
3334     switch (Align & 7) {
3335     case 0:
3336       VT = MVT::i64;
3337       break;
3338     case 4:
3339       VT = MVT::i32;
3340       break;
3341     case 2:
3342       VT = MVT::i16;
3343       break;
3344     default:
3345       VT = MVT::i8;
3346       break;
3347     }
3348   }
3349
3350   MVT::ValueType LVT = MVT::i64;
3351   while (!TLI.isTypeLegal(LVT))
3352     LVT = (MVT::ValueType)((unsigned)LVT - 1);
3353   assert(MVT::isInteger(LVT));
3354
3355   if (VT > LVT)
3356     VT = LVT;
3357
3358   unsigned NumMemOps = 0;
3359   while (Size != 0) {
3360     unsigned VTSize = getSizeInBits(VT) / 8;
3361     while (VTSize > Size) {
3362       VT = (MVT::ValueType)((unsigned)VT - 1);
3363       VTSize >>= 1;
3364     }
3365     assert(MVT::isInteger(VT));
3366
3367     if (++NumMemOps > Limit)
3368       return false;
3369     MemOps.push_back(VT);
3370     Size -= VTSize;
3371   }
3372
3373   return true;
3374 }
3375
3376 void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) {
3377   SDOperand Op1 = getValue(I.getOperand(1));
3378   SDOperand Op2 = getValue(I.getOperand(2));
3379   SDOperand Op3 = getValue(I.getOperand(3));
3380   SDOperand Op4 = getValue(I.getOperand(4));
3381   unsigned Align = (unsigned)cast<ConstantSDNode>(Op4)->getValue();
3382   if (Align == 0) Align = 1;
3383
3384   if (ConstantSDNode *Size = dyn_cast<ConstantSDNode>(Op3)) {
3385     std::vector<MVT::ValueType> MemOps;
3386
3387     // Expand memset / memcpy to a series of load / store ops
3388     // if the size operand falls below a certain threshold.
3389     SmallVector<SDOperand, 8> OutChains;
3390     switch (Op) {
3391     default: break;  // Do nothing for now.
3392     case ISD::MEMSET: {
3393       if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemset(),
3394                                    Size->getValue(), Align, TLI)) {
3395         unsigned NumMemOps = MemOps.size();
3396         unsigned Offset = 0;
3397         for (unsigned i = 0; i < NumMemOps; i++) {
3398           MVT::ValueType VT = MemOps[i];
3399           unsigned VTSize = getSizeInBits(VT) / 8;
3400           SDOperand Value = getMemsetValue(Op2, VT, DAG);
3401           SDOperand Store = DAG.getStore(getRoot(), Value,
3402                                     getMemBasePlusOffset(Op1, Offset, DAG, TLI),
3403                                          I.getOperand(1), Offset);
3404           OutChains.push_back(Store);
3405           Offset += VTSize;
3406         }
3407       }
3408       break;
3409     }
3410     case ISD::MEMCPY: {
3411       if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemcpy(),
3412                                    Size->getValue(), Align, TLI)) {
3413         unsigned NumMemOps = MemOps.size();
3414         unsigned SrcOff = 0, DstOff = 0, SrcDelta = 0;
3415         GlobalAddressSDNode *G = NULL;
3416         std::string Str;
3417         bool CopyFromStr = false;
3418
3419         if (Op2.getOpcode() == ISD::GlobalAddress)
3420           G = cast<GlobalAddressSDNode>(Op2);
3421         else if (Op2.getOpcode() == ISD::ADD &&
3422                  Op2.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3423                  Op2.getOperand(1).getOpcode() == ISD::Constant) {
3424           G = cast<GlobalAddressSDNode>(Op2.getOperand(0));
3425           SrcDelta = cast<ConstantSDNode>(Op2.getOperand(1))->getValue();
3426         }
3427         if (G) {
3428           GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
3429           if (GV && GV->isConstant()) {
3430             Str = GV->getStringValue(false);
3431             if (!Str.empty()) {
3432               CopyFromStr = true;
3433               SrcOff += SrcDelta;
3434             }
3435           }
3436         }
3437
3438         for (unsigned i = 0; i < NumMemOps; i++) {
3439           MVT::ValueType VT = MemOps[i];
3440           unsigned VTSize = getSizeInBits(VT) / 8;
3441           SDOperand Value, Chain, Store;
3442
3443           if (CopyFromStr) {
3444             Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
3445             Chain = getRoot();
3446             Store =
3447               DAG.getStore(Chain, Value,
3448                            getMemBasePlusOffset(Op1, DstOff, DAG, TLI),
3449                            I.getOperand(1), DstOff);
3450           } else {
3451             Value = DAG.getLoad(VT, getRoot(),
3452                         getMemBasePlusOffset(Op2, SrcOff, DAG, TLI),
3453                         I.getOperand(2), SrcOff);
3454             Chain = Value.getValue(1);
3455             Store =
3456               DAG.getStore(Chain, Value,
3457                            getMemBasePlusOffset(Op1, DstOff, DAG, TLI),
3458                            I.getOperand(1), DstOff);
3459           }
3460           OutChains.push_back(Store);
3461           SrcOff += VTSize;
3462           DstOff += VTSize;
3463         }
3464       }
3465       break;
3466     }
3467     }
3468
3469     if (!OutChains.empty()) {
3470       DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other,
3471                   &OutChains[0], OutChains.size()));
3472       return;
3473     }
3474   }
3475
3476   DAG.setRoot(DAG.getNode(Op, MVT::Other, getRoot(), Op1, Op2, Op3, Op4));
3477 }
3478
3479 //===----------------------------------------------------------------------===//
3480 // SelectionDAGISel code
3481 //===----------------------------------------------------------------------===//
3482
3483 unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) {
3484   return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
3485 }
3486
3487 void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
3488   // FIXME: we only modify the CFG to split critical edges.  This
3489   // updates dom and loop info.
3490   AU.addRequired<AliasAnalysis>();
3491 }
3492
3493
3494 /// OptimizeNoopCopyExpression - We have determined that the specified cast
3495 /// instruction is a noop copy (e.g. it's casting from one pointer type to
3496 /// another, int->uint, or int->sbyte on PPC.
3497 ///
3498 /// Return true if any changes are made.
3499 static bool OptimizeNoopCopyExpression(CastInst *CI) {
3500   BasicBlock *DefBB = CI->getParent();
3501   
3502   /// InsertedCasts - Only insert a cast in each block once.
3503   std::map<BasicBlock*, CastInst*> InsertedCasts;
3504   
3505   bool MadeChange = false;
3506   for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); 
3507        UI != E; ) {
3508     Use &TheUse = UI.getUse();
3509     Instruction *User = cast<Instruction>(*UI);
3510     
3511     // Figure out which BB this cast is used in.  For PHI's this is the
3512     // appropriate predecessor block.
3513     BasicBlock *UserBB = User->getParent();
3514     if (PHINode *PN = dyn_cast<PHINode>(User)) {
3515       unsigned OpVal = UI.getOperandNo()/2;
3516       UserBB = PN->getIncomingBlock(OpVal);
3517     }
3518     
3519     // Preincrement use iterator so we don't invalidate it.
3520     ++UI;
3521     
3522     // If this user is in the same block as the cast, don't change the cast.
3523     if (UserBB == DefBB) continue;
3524     
3525     // If we have already inserted a cast into this block, use it.
3526     CastInst *&InsertedCast = InsertedCasts[UserBB];
3527
3528     if (!InsertedCast) {
3529       BasicBlock::iterator InsertPt = UserBB->begin();
3530       while (isa<PHINode>(InsertPt)) ++InsertPt;
3531       
3532       InsertedCast = 
3533         CastInst::create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", 
3534                          InsertPt);
3535       MadeChange = true;
3536     }
3537     
3538     // Replace a use of the cast with a use of the new casat.
3539     TheUse = InsertedCast;
3540   }
3541   
3542   // If we removed all uses, nuke the cast.
3543   if (CI->use_empty())
3544     CI->eraseFromParent();
3545   
3546   return MadeChange;
3547 }
3548
3549 /// InsertGEPComputeCode - Insert code into BB to compute Ptr+PtrOffset,
3550 /// casting to the type of GEPI.
3551 static Instruction *InsertGEPComputeCode(Instruction *&V, BasicBlock *BB,
3552                                          Instruction *GEPI, Value *Ptr,
3553                                          Value *PtrOffset) {
3554   if (V) return V;   // Already computed.
3555   
3556   // Figure out the insertion point
3557   BasicBlock::iterator InsertPt;
3558   if (BB == GEPI->getParent()) {
3559     // If GEP is already inserted into BB, insert right after the GEP.
3560     InsertPt = GEPI;
3561     ++InsertPt;
3562   } else {
3563     // Otherwise, insert at the top of BB, after any PHI nodes
3564     InsertPt = BB->begin();
3565     while (isa<PHINode>(InsertPt)) ++InsertPt;
3566   }
3567   
3568   // If Ptr is itself a cast, but in some other BB, emit a copy of the cast into
3569   // BB so that there is only one value live across basic blocks (the cast 
3570   // operand).
3571   if (CastInst *CI = dyn_cast<CastInst>(Ptr))
3572     if (CI->getParent() != BB && isa<PointerType>(CI->getOperand(0)->getType()))
3573       Ptr = CastInst::create(CI->getOpcode(), CI->getOperand(0), CI->getType(),
3574                              "", InsertPt);
3575   
3576   // Add the offset, cast it to the right type.
3577   Ptr = BinaryOperator::createAdd(Ptr, PtrOffset, "", InsertPt);
3578   // Ptr is an integer type, GEPI is pointer type ==> IntToPtr
3579   return V = CastInst::create(Instruction::IntToPtr, Ptr, GEPI->getType(), 
3580                               "", InsertPt);
3581 }
3582
3583 /// ReplaceUsesOfGEPInst - Replace all uses of RepPtr with inserted code to
3584 /// compute its value.  The RepPtr value can be computed with Ptr+PtrOffset. One
3585 /// trivial way of doing this would be to evaluate Ptr+PtrOffset in RepPtr's
3586 /// block, then ReplaceAllUsesWith'ing everything.  However, we would prefer to
3587 /// sink PtrOffset into user blocks where doing so will likely allow us to fold
3588 /// the constant add into a load or store instruction.  Additionally, if a user
3589 /// is a pointer-pointer cast, we look through it to find its users.
3590 static void ReplaceUsesOfGEPInst(Instruction *RepPtr, Value *Ptr, 
3591                                  Constant *PtrOffset, BasicBlock *DefBB,
3592                                  GetElementPtrInst *GEPI,
3593                            std::map<BasicBlock*,Instruction*> &InsertedExprs) {
3594   while (!RepPtr->use_empty()) {
3595     Instruction *User = cast<Instruction>(RepPtr->use_back());
3596     
3597     // If the user is a Pointer-Pointer cast, recurse. Only BitCast can be
3598     // used for a Pointer-Pointer cast.
3599     if (isa<BitCastInst>(User)) {
3600       ReplaceUsesOfGEPInst(User, Ptr, PtrOffset, DefBB, GEPI, InsertedExprs);
3601       
3602       // Drop the use of RepPtr. The cast is dead.  Don't delete it now, else we
3603       // could invalidate an iterator.
3604       User->setOperand(0, UndefValue::get(RepPtr->getType()));
3605       continue;
3606     }
3607     
3608     // If this is a load of the pointer, or a store through the pointer, emit
3609     // the increment into the load/store block.
3610     Instruction *NewVal;
3611     if (isa<LoadInst>(User) ||
3612         (isa<StoreInst>(User) && User->getOperand(0) != RepPtr)) {
3613       NewVal = InsertGEPComputeCode(InsertedExprs[User->getParent()], 
3614                                     User->getParent(), GEPI,
3615                                     Ptr, PtrOffset);
3616     } else {
3617       // If this use is not foldable into the addressing mode, use a version 
3618       // emitted in the GEP block.
3619       NewVal = InsertGEPComputeCode(InsertedExprs[DefBB], DefBB, GEPI, 
3620                                     Ptr, PtrOffset);
3621     }
3622     
3623     if (GEPI->getType() != RepPtr->getType()) {
3624       BasicBlock::iterator IP = NewVal;
3625       ++IP;
3626       // NewVal must be a GEP which must be pointer type, so BitCast
3627       NewVal = new BitCastInst(NewVal, RepPtr->getType(), "", IP);
3628     }
3629     User->replaceUsesOfWith(RepPtr, NewVal);
3630   }
3631 }
3632
3633
3634 /// OptimizeGEPExpression - Since we are doing basic-block-at-a-time instruction
3635 /// selection, we want to be a bit careful about some things.  In particular, if
3636 /// we have a GEP instruction that is used in a different block than it is
3637 /// defined, the addressing expression of the GEP cannot be folded into loads or
3638 /// stores that use it.  In this case, decompose the GEP and move constant
3639 /// indices into blocks that use it.
3640 static bool OptimizeGEPExpression(GetElementPtrInst *GEPI,
3641                                   const TargetData *TD) {
3642   // If this GEP is only used inside the block it is defined in, there is no
3643   // need to rewrite it.
3644   bool isUsedOutsideDefBB = false;
3645   BasicBlock *DefBB = GEPI->getParent();
3646   for (Value::use_iterator UI = GEPI->use_begin(), E = GEPI->use_end(); 
3647        UI != E; ++UI) {
3648     if (cast<Instruction>(*UI)->getParent() != DefBB) {
3649       isUsedOutsideDefBB = true;
3650       break;
3651     }
3652   }
3653   if (!isUsedOutsideDefBB) return false;
3654
3655   // If this GEP has no non-zero constant indices, there is nothing we can do,
3656   // ignore it.
3657   bool hasConstantIndex = false;
3658   bool hasVariableIndex = false;
3659   for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1,
3660        E = GEPI->op_end(); OI != E; ++OI) {
3661     if (ConstantInt *CI = dyn_cast<ConstantInt>(*OI)) {
3662       if (CI->getZExtValue()) {
3663         hasConstantIndex = true;
3664         break;
3665       }
3666     } else {
3667       hasVariableIndex = true;
3668     }
3669   }
3670   
3671   // If this is a "GEP X, 0, 0, 0", turn this into a cast.
3672   if (!hasConstantIndex && !hasVariableIndex) {
3673     /// The GEP operand must be a pointer, so must its result -> BitCast
3674     Value *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), 
3675                              GEPI->getName(), GEPI);
3676     GEPI->replaceAllUsesWith(NC);
3677     GEPI->eraseFromParent();
3678     return true;
3679   }
3680   
3681   // If this is a GEP &Alloca, 0, 0, forward subst the frame index into uses.
3682   if (!hasConstantIndex && !isa<AllocaInst>(GEPI->getOperand(0)))
3683     return false;
3684   
3685   // Otherwise, decompose the GEP instruction into multiplies and adds.  Sum the
3686   // constant offset (which we now know is non-zero) and deal with it later.
3687   uint64_t ConstantOffset = 0;
3688   const Type *UIntPtrTy = TD->getIntPtrType();
3689   Value *Ptr = new PtrToIntInst(GEPI->getOperand(0), UIntPtrTy, "", GEPI);
3690   const Type *Ty = GEPI->getOperand(0)->getType();
3691
3692   for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1,
3693        E = GEPI->op_end(); OI != E; ++OI) {
3694     Value *Idx = *OI;
3695     if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
3696       unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
3697       if (Field)
3698         ConstantOffset += TD->getStructLayout(StTy)->MemberOffsets[Field];
3699       Ty = StTy->getElementType(Field);
3700     } else {
3701       Ty = cast<SequentialType>(Ty)->getElementType();
3702
3703       // Handle constant subscripts.
3704       if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
3705         if (CI->getZExtValue() == 0) continue;
3706         ConstantOffset += (int64_t)TD->getTypeSize(Ty)*CI->getSExtValue();
3707         continue;
3708       }
3709       
3710       // Ptr = Ptr + Idx * ElementSize;
3711       
3712       // Cast Idx to UIntPtrTy if needed.
3713       Idx = CastInst::createIntegerCast(Idx, UIntPtrTy, true/*SExt*/, "", GEPI);
3714       
3715       uint64_t ElementSize = TD->getTypeSize(Ty);
3716       // Mask off bits that should not be set.
3717       ElementSize &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits());
3718       Constant *SizeCst = ConstantInt::get(UIntPtrTy, ElementSize);
3719
3720       // Multiply by the element size and add to the base.
3721       Idx = BinaryOperator::createMul(Idx, SizeCst, "", GEPI);
3722       Ptr = BinaryOperator::createAdd(Ptr, Idx, "", GEPI);
3723     }
3724   }
3725   
3726   // Make sure that the offset fits in uintptr_t.
3727   ConstantOffset &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits());
3728   Constant *PtrOffset = ConstantInt::get(UIntPtrTy, ConstantOffset);
3729   
3730   // Okay, we have now emitted all of the variable index parts to the BB that
3731   // the GEP is defined in.  Loop over all of the using instructions, inserting
3732   // an "add Ptr, ConstantOffset" into each block that uses it and update the
3733   // instruction to use the newly computed value, making GEPI dead.  When the
3734   // user is a load or store instruction address, we emit the add into the user
3735   // block, otherwise we use a canonical version right next to the gep (these 
3736   // won't be foldable as addresses, so we might as well share the computation).
3737   
3738   std::map<BasicBlock*,Instruction*> InsertedExprs;
3739   ReplaceUsesOfGEPInst(GEPI, Ptr, PtrOffset, DefBB, GEPI, InsertedExprs);
3740   
3741   // Finally, the GEP is dead, remove it.
3742   GEPI->eraseFromParent();
3743   
3744   return true;
3745 }
3746
3747
3748 /// SplitEdgeNicely - Split the critical edge from TI to it's specified
3749 /// successor if it will improve codegen.  We only do this if the successor has
3750 /// phi nodes (otherwise critical edges are ok).  If there is already another
3751 /// predecessor of the succ that is empty (and thus has no phi nodes), use it
3752 /// instead of introducing a new block.
3753 static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
3754   BasicBlock *TIBB = TI->getParent();
3755   BasicBlock *Dest = TI->getSuccessor(SuccNum);
3756   assert(isa<PHINode>(Dest->begin()) &&
3757          "This should only be called if Dest has a PHI!");
3758
3759   /// TIPHIValues - This array is lazily computed to determine the values of
3760   /// PHIs in Dest that TI would provide.
3761   std::vector<Value*> TIPHIValues;
3762   
3763   // Check to see if Dest has any blocks that can be used as a split edge for
3764   // this terminator.
3765   for (pred_iterator PI = pred_begin(Dest), E = pred_end(Dest); PI != E; ++PI) {
3766     BasicBlock *Pred = *PI;
3767     // To be usable, the pred has to end with an uncond branch to the dest.
3768     BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator());
3769     if (!PredBr || !PredBr->isUnconditional() ||
3770         // Must be empty other than the branch.
3771         &Pred->front() != PredBr)
3772       continue;
3773     
3774     // Finally, since we know that Dest has phi nodes in it, we have to make
3775     // sure that jumping to Pred will have the same affect as going to Dest in
3776     // terms of PHI values.
3777     PHINode *PN;
3778     unsigned PHINo = 0;
3779     bool FoundMatch = true;
3780     for (BasicBlock::iterator I = Dest->begin();
3781          (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) {
3782       if (PHINo == TIPHIValues.size())
3783         TIPHIValues.push_back(PN->getIncomingValueForBlock(TIBB));
3784
3785       // If the PHI entry doesn't work, we can't use this pred.
3786       if (TIPHIValues[PHINo] != PN->getIncomingValueForBlock(Pred)) {
3787         FoundMatch = false;
3788         break;
3789       }
3790     }
3791     
3792     // If we found a workable predecessor, change TI to branch to Succ.
3793     if (FoundMatch) {
3794       Dest->removePredecessor(TIBB);
3795       TI->setSuccessor(SuccNum, Pred);
3796       return;
3797     }
3798   }
3799   
3800   SplitCriticalEdge(TI, SuccNum, P, true);  
3801 }
3802
3803
3804 bool SelectionDAGISel::runOnFunction(Function &Fn) {
3805   MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine());
3806   RegMap = MF.getSSARegMap();
3807   DOUT << "\n\n\n=== " << Fn.getName() << "\n";
3808
3809   // First, split all critical edges.
3810   //
3811   // In this pass we also look for GEP and cast instructions that are used
3812   // across basic blocks and rewrite them to improve basic-block-at-a-time
3813   // selection.
3814   //
3815   bool MadeChange = true;
3816   while (MadeChange) {
3817     MadeChange = false;
3818   for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
3819     // Split all critical edges where the dest block has a PHI.
3820     TerminatorInst *BBTI = BB->getTerminator();
3821     if (BBTI->getNumSuccessors() > 1) {
3822       for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i)
3823         if (isa<PHINode>(BBTI->getSuccessor(i)->begin()) &&
3824             isCriticalEdge(BBTI, i, true))
3825           SplitEdgeNicely(BBTI, i, this);
3826     }
3827     
3828     
3829     for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
3830       Instruction *I = BBI++;
3831       
3832       if (CallInst *CI = dyn_cast<CallInst>(I)) {
3833         // If we found an inline asm expession, and if the target knows how to
3834         // lower it to normal LLVM code, do so now.
3835         if (isa<InlineAsm>(CI->getCalledValue()))
3836           if (const TargetAsmInfo *TAI = 
3837                 TLI.getTargetMachine().getTargetAsmInfo()) {
3838             if (TAI->ExpandInlineAsm(CI))
3839               BBI = BB->begin();
3840           }
3841       } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
3842         MadeChange |= OptimizeGEPExpression(GEPI, TLI.getTargetData());
3843       } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
3844         // If the source of the cast is a constant, then this should have
3845         // already been constant folded.  The only reason NOT to constant fold
3846         // it is if something (e.g. LSR) was careful to place the constant
3847         // evaluation in a block other than then one that uses it (e.g. to hoist
3848         // the address of globals out of a loop).  If this is the case, we don't
3849         // want to forward-subst the cast.
3850         if (isa<Constant>(CI->getOperand(0)))
3851           continue;
3852         
3853         // If this is a noop copy, sink it into user blocks to reduce the number
3854         // of virtual registers that must be created and coallesced.
3855         MVT::ValueType SrcVT = TLI.getValueType(CI->getOperand(0)->getType());
3856         MVT::ValueType DstVT = TLI.getValueType(CI->getType());
3857         
3858         // This is an fp<->int conversion?
3859         if (MVT::isInteger(SrcVT) != MVT::isInteger(DstVT))
3860           continue;
3861         
3862         // If this is an extension, it will be a zero or sign extension, which
3863         // isn't a noop.
3864         if (SrcVT < DstVT) continue;
3865         
3866         // If these values will be promoted, find out what they will be promoted
3867         // to.  This helps us consider truncates on PPC as noop copies when they
3868         // are.
3869         if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote)
3870           SrcVT = TLI.getTypeToTransformTo(SrcVT);
3871         if (TLI.getTypeAction(DstVT) == TargetLowering::Promote)
3872           DstVT = TLI.getTypeToTransformTo(DstVT);
3873
3874         // If, after promotion, these are the same types, this is a noop copy.
3875         if (SrcVT == DstVT)
3876           MadeChange |= OptimizeNoopCopyExpression(CI);
3877       }
3878     }
3879   }
3880   }
3881   
3882   FunctionLoweringInfo FuncInfo(TLI, Fn, MF);
3883
3884   for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
3885     SelectBasicBlock(I, MF, FuncInfo);
3886
3887   return true;
3888 }
3889
3890 SDOperand SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, 
3891                                                            unsigned Reg) {
3892   SDOperand Op = getValue(V);
3893   assert((Op.getOpcode() != ISD::CopyFromReg ||
3894           cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
3895          "Copy from a reg to the same reg!");
3896   
3897   // If this type is not legal, we must make sure to not create an invalid
3898   // register use.
3899   MVT::ValueType SrcVT = Op.getValueType();
3900   MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT);
3901   if (SrcVT == DestVT) {
3902     return DAG.getCopyToReg(getRoot(), Reg, Op);
3903   } else if (SrcVT == MVT::Vector) {
3904     // Handle copies from generic vectors to registers.
3905     MVT::ValueType PTyElementVT, PTyLegalElementVT;
3906     unsigned NE = TLI.getPackedTypeBreakdown(cast<PackedType>(V->getType()),
3907                                              PTyElementVT, PTyLegalElementVT);
3908     
3909     // Insert a VBIT_CONVERT of the input vector to a "N x PTyElementVT" 
3910     // MVT::Vector type.
3911     Op = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Op,
3912                      DAG.getConstant(NE, MVT::i32), 
3913                      DAG.getValueType(PTyElementVT));
3914
3915     // Loop over all of the elements of the resultant vector,
3916     // VEXTRACT_VECTOR_ELT'ing them, converting them to PTyLegalElementVT, then
3917     // copying them into output registers.
3918     SmallVector<SDOperand, 8> OutChains;
3919     SDOperand Root = getRoot();
3920     for (unsigned i = 0; i != NE; ++i) {
3921       SDOperand Elt = DAG.getNode(ISD::VEXTRACT_VECTOR_ELT, PTyElementVT,
3922                                   Op, DAG.getConstant(i, TLI.getPointerTy()));
3923       if (PTyElementVT == PTyLegalElementVT) {
3924         // Elements are legal.
3925         OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Elt));
3926       } else if (PTyLegalElementVT > PTyElementVT) {
3927         // Elements are promoted.
3928         if (MVT::isFloatingPoint(PTyLegalElementVT))
3929           Elt = DAG.getNode(ISD::FP_EXTEND, PTyLegalElementVT, Elt);
3930         else
3931           Elt = DAG.getNode(ISD::ANY_EXTEND, PTyLegalElementVT, Elt);
3932         OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Elt));
3933       } else {
3934         // Elements are expanded.
3935         // The src value is expanded into multiple registers.
3936         SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, PTyLegalElementVT,
3937                                    Elt, DAG.getConstant(0, TLI.getPointerTy()));
3938         SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, PTyLegalElementVT,
3939                                    Elt, DAG.getConstant(1, TLI.getPointerTy()));
3940         OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Lo));
3941         OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Hi));
3942       }
3943     }
3944     return DAG.getNode(ISD::TokenFactor, MVT::Other,
3945                        &OutChains[0], OutChains.size());
3946   } else if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote) {
3947     // The src value is promoted to the register.
3948     if (MVT::isFloatingPoint(SrcVT))
3949       Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op);
3950     else
3951       Op = DAG.getNode(ISD::ANY_EXTEND, DestVT, Op);
3952     return DAG.getCopyToReg(getRoot(), Reg, Op);
3953   } else  {
3954     DestVT = TLI.getTypeToExpandTo(SrcVT);
3955     unsigned NumVals = TLI.getNumElements(SrcVT);
3956     if (NumVals == 1)
3957       return DAG.getCopyToReg(getRoot(), Reg,
3958                               DAG.getNode(ISD::BIT_CONVERT, DestVT, Op));
3959     assert(NumVals == 2 && "1 to 4 (and more) expansion not implemented!");
3960     // The src value is expanded into multiple registers.
3961     SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT,
3962                                Op, DAG.getConstant(0, TLI.getPointerTy()));
3963     SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT,
3964                                Op, DAG.getConstant(1, TLI.getPointerTy()));
3965     Op = DAG.getCopyToReg(getRoot(), Reg, Lo);
3966     return DAG.getCopyToReg(Op, Reg+1, Hi);
3967   }
3968 }
3969
3970 void SelectionDAGISel::
3971 LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL,
3972                std::vector<SDOperand> &UnorderedChains) {
3973   // If this is the entry block, emit arguments.
3974   Function &F = *BB->getParent();
3975   FunctionLoweringInfo &FuncInfo = SDL.FuncInfo;
3976   SDOperand OldRoot = SDL.DAG.getRoot();
3977   std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG);
3978
3979   unsigned a = 0;
3980   for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
3981        AI != E; ++AI, ++a)
3982     if (!AI->use_empty()) {
3983       SDL.setValue(AI, Args[a]);
3984
3985       // If this argument is live outside of the entry block, insert a copy from
3986       // whereever we got it to the vreg that other BB's will reference it as.
3987       if (FuncInfo.ValueMap.count(AI)) {
3988         SDOperand Copy =
3989           SDL.CopyValueToVirtualRegister(AI, FuncInfo.ValueMap[AI]);
3990         UnorderedChains.push_back(Copy);
3991       }
3992     }
3993
3994   // Finally, if the target has anything special to do, allow it to do so.
3995   // FIXME: this should insert code into the DAG!
3996   EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction());
3997 }
3998
3999 void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
4000        std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
4001                                          FunctionLoweringInfo &FuncInfo) {
4002   SelectionDAGLowering SDL(DAG, TLI, FuncInfo);
4003
4004   std::vector<SDOperand> UnorderedChains;
4005
4006   // Lower any arguments needed in this block if this is the entry block.
4007   if (LLVMBB == &LLVMBB->getParent()->front())
4008     LowerArguments(LLVMBB, SDL, UnorderedChains);
4009
4010   BB = FuncInfo.MBBMap[LLVMBB];
4011   SDL.setCurrentBasicBlock(BB);
4012
4013   // Lower all of the non-terminator instructions.
4014   for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end();
4015        I != E; ++I)
4016     SDL.visit(*I);
4017   
4018   // Ensure that all instructions which are used outside of their defining
4019   // blocks are available as virtual registers.
4020   for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I)
4021     if (!I->use_empty() && !isa<PHINode>(I)) {
4022       std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I);
4023       if (VMI != FuncInfo.ValueMap.end())
4024         UnorderedChains.push_back(
4025                                 SDL.CopyValueToVirtualRegister(I, VMI->second));
4026     }
4027
4028   // Handle PHI nodes in successor blocks.  Emit code into the SelectionDAG to
4029   // ensure constants are generated when needed.  Remember the virtual registers
4030   // that need to be added to the Machine PHI nodes as input.  We cannot just
4031   // directly add them, because expansion might result in multiple MBB's for one
4032   // BB.  As such, the start of the BB might correspond to a different MBB than
4033   // the end.
4034   //
4035   TerminatorInst *TI = LLVMBB->getTerminator();
4036
4037   // Emit constants only once even if used by multiple PHI nodes.
4038   std::map<Constant*, unsigned> ConstantsOut;
4039   
4040   // Vector bool would be better, but vector<bool> is really slow.
4041   std::vector<unsigned char> SuccsHandled;
4042   if (TI->getNumSuccessors())
4043     SuccsHandled.resize(BB->getParent()->getNumBlockIDs());
4044     
4045   // Check successor nodes PHI nodes that expect a constant to be available from
4046   // this block.
4047   for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
4048     BasicBlock *SuccBB = TI->getSuccessor(succ);
4049     if (!isa<PHINode>(SuccBB->begin())) continue;
4050     MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
4051     
4052     // If this terminator has multiple identical successors (common for
4053     // switches), only handle each succ once.
4054     unsigned SuccMBBNo = SuccMBB->getNumber();
4055     if (SuccsHandled[SuccMBBNo]) continue;
4056     SuccsHandled[SuccMBBNo] = true;
4057     
4058     MachineBasicBlock::iterator MBBI = SuccMBB->begin();
4059     PHINode *PN;
4060
4061     // At this point we know that there is a 1-1 correspondence between LLVM PHI
4062     // nodes and Machine PHI nodes, but the incoming operands have not been
4063     // emitted yet.
4064     for (BasicBlock::iterator I = SuccBB->begin();
4065          (PN = dyn_cast<PHINode>(I)); ++I) {
4066       // Ignore dead phi's.
4067       if (PN->use_empty()) continue;
4068       
4069       unsigned Reg;
4070       Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
4071       
4072       if (Constant *C = dyn_cast<Constant>(PHIOp)) {
4073         unsigned &RegOut = ConstantsOut[C];
4074         if (RegOut == 0) {
4075           RegOut = FuncInfo.CreateRegForValue(C);
4076           UnorderedChains.push_back(
4077                            SDL.CopyValueToVirtualRegister(C, RegOut));
4078         }
4079         Reg = RegOut;
4080       } else {
4081         Reg = FuncInfo.ValueMap[PHIOp];
4082         if (Reg == 0) {
4083           assert(isa<AllocaInst>(PHIOp) &&
4084                  FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
4085                  "Didn't codegen value into a register!??");
4086           Reg = FuncInfo.CreateRegForValue(PHIOp);
4087           UnorderedChains.push_back(
4088                            SDL.CopyValueToVirtualRegister(PHIOp, Reg));
4089         }
4090       }
4091
4092       // Remember that this register needs to added to the machine PHI node as
4093       // the input for this MBB.
4094       MVT::ValueType VT = TLI.getValueType(PN->getType());
4095       unsigned NumElements;
4096       if (VT != MVT::Vector)
4097         NumElements = TLI.getNumElements(VT);
4098       else {
4099         MVT::ValueType VT1,VT2;
4100         NumElements = 
4101           TLI.getPackedTypeBreakdown(cast<PackedType>(PN->getType()),
4102                                      VT1, VT2);
4103       }
4104       for (unsigned i = 0, e = NumElements; i != e; ++i)
4105         PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
4106     }
4107   }
4108   ConstantsOut.clear();
4109
4110   // Turn all of the unordered chains into one factored node.
4111   if (!UnorderedChains.empty()) {
4112     SDOperand Root = SDL.getRoot();
4113     if (Root.getOpcode() != ISD::EntryToken) {
4114       unsigned i = 0, e = UnorderedChains.size();
4115       for (; i != e; ++i) {
4116         assert(UnorderedChains[i].Val->getNumOperands() > 1);
4117         if (UnorderedChains[i].Val->getOperand(0) == Root)
4118           break;  // Don't add the root if we already indirectly depend on it.
4119       }
4120         
4121       if (i == e)
4122         UnorderedChains.push_back(Root);
4123     }
4124     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other,
4125                             &UnorderedChains[0], UnorderedChains.size()));
4126   }
4127
4128   // Lower the terminator after the copies are emitted.
4129   SDL.visit(*LLVMBB->getTerminator());
4130
4131   // Copy over any CaseBlock records that may now exist due to SwitchInst
4132   // lowering, as well as any jump table information.
4133   SwitchCases.clear();
4134   SwitchCases = SDL.SwitchCases;
4135   JT = SDL.JT;
4136   
4137   // Make sure the root of the DAG is up-to-date.
4138   DAG.setRoot(SDL.getRoot());
4139 }
4140
4141 void SelectionDAGISel::CodeGenAndEmitDAG(SelectionDAG &DAG) {
4142   // Get alias analysis for load/store combining.
4143   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
4144
4145   // Run the DAG combiner in pre-legalize mode.
4146   DAG.Combine(false, AA);
4147   
4148   DOUT << "Lowered selection DAG:\n";
4149   DEBUG(DAG.dump());
4150   
4151   // Second step, hack on the DAG until it only uses operations and types that
4152   // the target supports.
4153   DAG.Legalize();
4154   
4155   DOUT << "Legalized selection DAG:\n";
4156   DEBUG(DAG.dump());
4157   
4158   // Run the DAG combiner in post-legalize mode.
4159   DAG.Combine(true, AA);
4160   
4161   if (ViewISelDAGs) DAG.viewGraph();
4162
4163   // Third, instruction select all of the operations to machine code, adding the
4164   // code to the MachineBasicBlock.
4165   InstructionSelectBasicBlock(DAG);
4166   
4167   DOUT << "Selected machine code:\n";
4168   DEBUG(BB->dump());
4169 }  
4170
4171 void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF,
4172                                         FunctionLoweringInfo &FuncInfo) {
4173   std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
4174   {
4175     SelectionDAG DAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>());
4176     CurDAG = &DAG;
4177   
4178     // First step, lower LLVM code to some DAG.  This DAG may use operations and
4179     // types that are not supported by the target.
4180     BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo);
4181
4182     // Second step, emit the lowered DAG as machine code.
4183     CodeGenAndEmitDAG(DAG);
4184   }
4185   
4186   // Next, now that we know what the last MBB the LLVM BB expanded is, update
4187   // PHI nodes in successors.
4188   if (SwitchCases.empty() && JT.Reg == 0) {
4189     for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) {
4190       MachineInstr *PHI = PHINodesToUpdate[i].first;
4191       assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
4192              "This is not a machine PHI node that we are updating!");
4193       PHI->addRegOperand(PHINodesToUpdate[i].second, false);
4194       PHI->addMachineBasicBlockOperand(BB);
4195     }
4196     return;
4197   }
4198   
4199   // If the JumpTable record is filled in, then we need to emit a jump table.
4200   // Updating the PHI nodes is tricky in this case, since we need to determine
4201   // whether the PHI is a successor of the range check MBB or the jump table MBB
4202   if (JT.Reg) {
4203     assert(SwitchCases.empty() && "Cannot have jump table and lowered switch");
4204     SelectionDAG SDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>());
4205     CurDAG = &SDAG;
4206     SelectionDAGLowering SDL(SDAG, TLI, FuncInfo);
4207     MachineBasicBlock *RangeBB = BB;
4208     // Set the current basic block to the mbb we wish to insert the code into
4209     BB = JT.MBB;
4210     SDL.setCurrentBasicBlock(BB);
4211     // Emit the code
4212     SDL.visitJumpTable(JT);
4213     SDAG.setRoot(SDL.getRoot());
4214     CodeGenAndEmitDAG(SDAG);
4215     // Update PHI Nodes
4216     for (unsigned pi = 0, pe = PHINodesToUpdate.size(); pi != pe; ++pi) {
4217       MachineInstr *PHI = PHINodesToUpdate[pi].first;
4218       MachineBasicBlock *PHIBB = PHI->getParent();
4219       assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
4220              "This is not a machine PHI node that we are updating!");
4221       if (PHIBB == JT.Default) {
4222         PHI->addRegOperand(PHINodesToUpdate[pi].second, false);
4223         PHI->addMachineBasicBlockOperand(RangeBB);
4224       }
4225       if (BB->succ_end() != std::find(BB->succ_begin(),BB->succ_end(), PHIBB)) {
4226         PHI->addRegOperand(PHINodesToUpdate[pi].second, false);
4227         PHI->addMachineBasicBlockOperand(BB);
4228       }
4229     }
4230     return;
4231   }
4232   
4233   // If the switch block involved a branch to one of the actual successors, we
4234   // need to update PHI nodes in that block.
4235   for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) {
4236     MachineInstr *PHI = PHINodesToUpdate[i].first;
4237     assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
4238            "This is not a machine PHI node that we are updating!");
4239     if (BB->isSuccessor(PHI->getParent())) {
4240       PHI->addRegOperand(PHINodesToUpdate[i].second, false);
4241       PHI->addMachineBasicBlockOperand(BB);
4242     }
4243   }
4244   
4245   // If we generated any switch lowering information, build and codegen any
4246   // additional DAGs necessary.
4247   for (unsigned i = 0, e = SwitchCases.size(); i != e; ++i) {
4248     SelectionDAG SDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>());
4249     CurDAG = &SDAG;
4250     SelectionDAGLowering SDL(SDAG, TLI, FuncInfo);
4251     
4252     // Set the current basic block to the mbb we wish to insert the code into
4253     BB = SwitchCases[i].ThisBB;
4254     SDL.setCurrentBasicBlock(BB);
4255     
4256     // Emit the code
4257     SDL.visitSwitchCase(SwitchCases[i]);
4258     SDAG.setRoot(SDL.getRoot());
4259     CodeGenAndEmitDAG(SDAG);
4260     
4261     // Handle any PHI nodes in successors of this chunk, as if we were coming
4262     // from the original BB before switch expansion.  Note that PHI nodes can
4263     // occur multiple times in PHINodesToUpdate.  We have to be very careful to
4264     // handle them the right number of times.
4265     while ((BB = SwitchCases[i].TrueBB)) {  // Handle LHS and RHS.
4266       for (MachineBasicBlock::iterator Phi = BB->begin();
4267            Phi != BB->end() && Phi->getOpcode() == TargetInstrInfo::PHI; ++Phi){
4268         // This value for this PHI node is recorded in PHINodesToUpdate, get it.
4269         for (unsigned pn = 0; ; ++pn) {
4270           assert(pn != PHINodesToUpdate.size() && "Didn't find PHI entry!");
4271           if (PHINodesToUpdate[pn].first == Phi) {
4272             Phi->addRegOperand(PHINodesToUpdate[pn].second, false);
4273             Phi->addMachineBasicBlockOperand(SwitchCases[i].ThisBB);
4274             break;
4275           }
4276         }
4277       }
4278       
4279       // Don't process RHS if same block as LHS.
4280       if (BB == SwitchCases[i].FalseBB)
4281         SwitchCases[i].FalseBB = 0;
4282       
4283       // If we haven't handled the RHS, do so now.  Otherwise, we're done.
4284       SwitchCases[i].TrueBB = SwitchCases[i].FalseBB;
4285       SwitchCases[i].FalseBB = 0;
4286     }
4287     assert(SwitchCases[i].TrueBB == 0 && SwitchCases[i].FalseBB == 0);
4288   }
4289 }
4290
4291
4292 //===----------------------------------------------------------------------===//
4293 /// ScheduleAndEmitDAG - Pick a safe ordering and emit instructions for each
4294 /// target node in the graph.
4295 void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &DAG) {
4296   if (ViewSchedDAGs) DAG.viewGraph();
4297
4298   RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault();
4299   
4300   if (!Ctor) {
4301     Ctor = ISHeuristic;
4302     RegisterScheduler::setDefault(Ctor);
4303   }
4304   
4305   ScheduleDAG *SL = Ctor(this, &DAG, BB);
4306   BB = SL->Run();
4307   delete SL;
4308 }
4309
4310
4311 HazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() {
4312   return new HazardRecognizer();
4313 }
4314
4315 //===----------------------------------------------------------------------===//
4316 // Helper functions used by the generated instruction selector.
4317 //===----------------------------------------------------------------------===//
4318 // Calls to these methods are generated by tblgen.
4319
4320 /// CheckAndMask - The isel is trying to match something like (and X, 255).  If
4321 /// the dag combiner simplified the 255, we still want to match.  RHS is the
4322 /// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value
4323 /// specified in the .td file (e.g. 255).
4324 bool SelectionDAGISel::CheckAndMask(SDOperand LHS, ConstantSDNode *RHS, 
4325                                     int64_t DesiredMaskS) {
4326   uint64_t ActualMask = RHS->getValue();
4327   uint64_t DesiredMask =DesiredMaskS & MVT::getIntVTBitMask(LHS.getValueType());
4328   
4329   // If the actual mask exactly matches, success!
4330   if (ActualMask == DesiredMask)
4331     return true;
4332   
4333   // If the actual AND mask is allowing unallowed bits, this doesn't match.
4334   if (ActualMask & ~DesiredMask)
4335     return false;
4336   
4337   // Otherwise, the DAG Combiner may have proven that the value coming in is
4338   // either already zero or is not demanded.  Check for known zero input bits.
4339   uint64_t NeededMask = DesiredMask & ~ActualMask;
4340   if (getTargetLowering().MaskedValueIsZero(LHS, NeededMask))
4341     return true;
4342   
4343   // TODO: check to see if missing bits are just not demanded.
4344
4345   // Otherwise, this pattern doesn't match.
4346   return false;
4347 }
4348
4349 /// CheckOrMask - The isel is trying to match something like (or X, 255).  If
4350 /// the dag combiner simplified the 255, we still want to match.  RHS is the
4351 /// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value
4352 /// specified in the .td file (e.g. 255).
4353 bool SelectionDAGISel::CheckOrMask(SDOperand LHS, ConstantSDNode *RHS, 
4354                                     int64_t DesiredMaskS) {
4355   uint64_t ActualMask = RHS->getValue();
4356   uint64_t DesiredMask =DesiredMaskS & MVT::getIntVTBitMask(LHS.getValueType());
4357   
4358   // If the actual mask exactly matches, success!
4359   if (ActualMask == DesiredMask)
4360     return true;
4361   
4362   // If the actual AND mask is allowing unallowed bits, this doesn't match.
4363   if (ActualMask & ~DesiredMask)
4364     return false;
4365   
4366   // Otherwise, the DAG Combiner may have proven that the value coming in is
4367   // either already zero or is not demanded.  Check for known zero input bits.
4368   uint64_t NeededMask = DesiredMask & ~ActualMask;
4369   
4370   uint64_t KnownZero, KnownOne;
4371   getTargetLowering().ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne);
4372   
4373   // If all the missing bits in the or are already known to be set, match!
4374   if ((NeededMask & KnownOne) == NeededMask)
4375     return true;
4376   
4377   // TODO: check to see if missing bits are just not demanded.
4378   
4379   // Otherwise, this pattern doesn't match.
4380   return false;
4381 }
4382
4383
4384 /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
4385 /// by tblgen.  Others should not call it.
4386 void SelectionDAGISel::
4387 SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops, SelectionDAG &DAG) {
4388   std::vector<SDOperand> InOps;
4389   std::swap(InOps, Ops);
4390
4391   Ops.push_back(InOps[0]);  // input chain.
4392   Ops.push_back(InOps[1]);  // input asm string.
4393
4394   unsigned i = 2, e = InOps.size();
4395   if (InOps[e-1].getValueType() == MVT::Flag)
4396     --e;  // Don't process a flag operand if it is here.
4397   
4398   while (i != e) {
4399     unsigned Flags = cast<ConstantSDNode>(InOps[i])->getValue();
4400     if ((Flags & 7) != 4 /*MEM*/) {
4401       // Just skip over this operand, copying the operands verbatim.
4402       Ops.insert(Ops.end(), InOps.begin()+i, InOps.begin()+i+(Flags >> 3) + 1);
4403       i += (Flags >> 3) + 1;
4404     } else {
4405       assert((Flags >> 3) == 1 && "Memory operand with multiple values?");
4406       // Otherwise, this is a memory operand.  Ask the target to select it.
4407       std::vector<SDOperand> SelOps;
4408       if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps, DAG)) {
4409         cerr << "Could not match memory address.  Inline asm failure!\n";
4410         exit(1);
4411       }
4412       
4413       // Add this to the output node.
4414       Ops.push_back(DAG.getTargetConstant(4/*MEM*/ | (SelOps.size() << 3),
4415                                           MVT::i32));
4416       Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
4417       i += 2;
4418     }
4419   }
4420   
4421   // Add the flag input back if present.
4422   if (e != InOps.size())
4423     Ops.push_back(InOps.back());
4424 }