s/ParameterAttributes/Attributes/g
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAGBuild.cpp
1 //===-- SelectionDAGBuild.cpp - Selection-DAG building --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements routines for translating from LLVM IR into SelectionDAG IR.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "isel"
15 #include "SelectionDAGBuild.h"
16 #include "llvm/ADT/BitVector.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/Constants.h"
20 #include "llvm/CallingConv.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/InlineAsm.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/Intrinsics.h"
27 #include "llvm/IntrinsicInst.h"
28 #include "llvm/CodeGen/FastISel.h"
29 #include "llvm/CodeGen/GCStrategy.h"
30 #include "llvm/CodeGen/GCMetadata.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineFrameInfo.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineJumpTableInfo.h"
35 #include "llvm/CodeGen/MachineModuleInfo.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/CodeGen/SelectionDAG.h"
38 #include "llvm/Target/TargetRegisterInfo.h"
39 #include "llvm/Target/TargetData.h"
40 #include "llvm/Target/TargetFrameInfo.h"
41 #include "llvm/Target/TargetInstrInfo.h"
42 #include "llvm/Target/TargetLowering.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include "llvm/Target/TargetOptions.h"
45 #include "llvm/Support/Compiler.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/MathExtras.h"
48 #include <algorithm>
49 using namespace llvm;
50
51 /// LimitFloatPrecision - Generate low-precision inline sequences for
52 /// some float libcalls (6, 8 or 12 bits).
53 static unsigned LimitFloatPrecision;
54
55 static cl::opt<unsigned, true>
56 LimitFPPrecision("limit-float-precision",
57                  cl::desc("Generate low-precision inline sequences "
58                           "for some float libcalls"),
59                  cl::location(LimitFloatPrecision),
60                  cl::init(0));
61
62 /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence
63 /// insertvalue or extractvalue indices that identify a member, return
64 /// the linearized index of the start of the member.
65 ///
66 static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty,
67                                    const unsigned *Indices,
68                                    const unsigned *IndicesEnd,
69                                    unsigned CurIndex = 0) {
70   // Base case: We're done.
71   if (Indices && Indices == IndicesEnd)
72     return CurIndex;
73
74   // Given a struct type, recursively traverse the elements.
75   if (const StructType *STy = dyn_cast<StructType>(Ty)) {
76     for (StructType::element_iterator EB = STy->element_begin(),
77                                       EI = EB,
78                                       EE = STy->element_end();
79         EI != EE; ++EI) {
80       if (Indices && *Indices == unsigned(EI - EB))
81         return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex);
82       CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex);
83     }
84   }
85   // Given an array type, recursively traverse the elements.
86   else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
87     const Type *EltTy = ATy->getElementType();
88     for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
89       if (Indices && *Indices == i)
90         return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex);
91       CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex);
92     }
93   }
94   // We haven't found the type we're looking for, so keep searching.
95   return CurIndex + 1;
96 }
97
98 /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
99 /// MVTs that represent all the individual underlying
100 /// non-aggregate types that comprise it.
101 ///
102 /// If Offsets is non-null, it points to a vector to be filled in
103 /// with the in-memory offsets of each of the individual values.
104 ///
105 static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty,
106                             SmallVectorImpl<MVT> &ValueVTs,
107                             SmallVectorImpl<uint64_t> *Offsets = 0,
108                             uint64_t StartingOffset = 0) {
109   // Given a struct type, recursively traverse the elements.
110   if (const StructType *STy = dyn_cast<StructType>(Ty)) {
111     const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy);
112     for (StructType::element_iterator EB = STy->element_begin(),
113                                       EI = EB,
114                                       EE = STy->element_end();
115          EI != EE; ++EI)
116       ComputeValueVTs(TLI, *EI, ValueVTs, Offsets,
117                       StartingOffset + SL->getElementOffset(EI - EB));
118     return;
119   }
120   // Given an array type, recursively traverse the elements.
121   if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
122     const Type *EltTy = ATy->getElementType();
123     uint64_t EltSize = TLI.getTargetData()->getABITypeSize(EltTy);
124     for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
125       ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets,
126                       StartingOffset + i * EltSize);
127     return;
128   }
129   // Base case: we can get an MVT for this LLVM IR type.
130   ValueVTs.push_back(TLI.getValueType(Ty));
131   if (Offsets)
132     Offsets->push_back(StartingOffset);
133 }
134
135 namespace llvm {
136   /// RegsForValue - This struct represents the registers (physical or virtual)
137   /// that a particular set of values is assigned, and the type information about
138   /// the value. The most common situation is to represent one value at a time,
139   /// but struct or array values are handled element-wise as multiple values.
140   /// The splitting of aggregates is performed recursively, so that we never
141   /// have aggregate-typed registers. The values at this point do not necessarily
142   /// have legal types, so each value may require one or more registers of some
143   /// legal type.
144   /// 
145   struct VISIBILITY_HIDDEN RegsForValue {
146     /// TLI - The TargetLowering object.
147     ///
148     const TargetLowering *TLI;
149
150     /// ValueVTs - The value types of the values, which may not be legal, and
151     /// may need be promoted or synthesized from one or more registers.
152     ///
153     SmallVector<MVT, 4> ValueVTs;
154     
155     /// RegVTs - The value types of the registers. This is the same size as
156     /// ValueVTs and it records, for each value, what the type of the assigned
157     /// register or registers are. (Individual values are never synthesized
158     /// from more than one type of register.)
159     ///
160     /// With virtual registers, the contents of RegVTs is redundant with TLI's
161     /// getRegisterType member function, however when with physical registers
162     /// it is necessary to have a separate record of the types.
163     ///
164     SmallVector<MVT, 4> RegVTs;
165     
166     /// Regs - This list holds the registers assigned to the values.
167     /// Each legal or promoted value requires one register, and each
168     /// expanded value requires multiple registers.
169     ///
170     SmallVector<unsigned, 4> Regs;
171     
172     RegsForValue() : TLI(0) {}
173     
174     RegsForValue(const TargetLowering &tli,
175                  const SmallVector<unsigned, 4> &regs, 
176                  MVT regvt, MVT valuevt)
177       : TLI(&tli),  ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
178     RegsForValue(const TargetLowering &tli,
179                  const SmallVector<unsigned, 4> &regs, 
180                  const SmallVector<MVT, 4> &regvts,
181                  const SmallVector<MVT, 4> &valuevts)
182       : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
183     RegsForValue(const TargetLowering &tli,
184                  unsigned Reg, const Type *Ty) : TLI(&tli) {
185       ComputeValueVTs(tli, Ty, ValueVTs);
186
187       for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
188         MVT ValueVT = ValueVTs[Value];
189         unsigned NumRegs = TLI->getNumRegisters(ValueVT);
190         MVT RegisterVT = TLI->getRegisterType(ValueVT);
191         for (unsigned i = 0; i != NumRegs; ++i)
192           Regs.push_back(Reg + i);
193         RegVTs.push_back(RegisterVT);
194         Reg += NumRegs;
195       }
196     }
197     
198     /// append - Add the specified values to this one.
199     void append(const RegsForValue &RHS) {
200       TLI = RHS.TLI;
201       ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
202       RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
203       Regs.append(RHS.Regs.begin(), RHS.Regs.end());
204     }
205     
206     
207     /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
208     /// this value and returns the result as a ValueVTs value.  This uses 
209     /// Chain/Flag as the input and updates them for the output Chain/Flag.
210     /// If the Flag pointer is NULL, no flag is used.
211     SDValue getCopyFromRegs(SelectionDAG &DAG,
212                               SDValue &Chain, SDValue *Flag) const;
213
214     /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
215     /// specified value into the registers specified by this object.  This uses 
216     /// Chain/Flag as the input and updates them for the output Chain/Flag.
217     /// If the Flag pointer is NULL, no flag is used.
218     void getCopyToRegs(SDValue Val, SelectionDAG &DAG,
219                        SDValue &Chain, SDValue *Flag) const;
220     
221     /// AddInlineAsmOperands - Add this value to the specified inlineasm node
222     /// operand list.  This adds the code marker and includes the number of 
223     /// values added into it.
224     void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
225                               std::vector<SDValue> &Ops) const;
226   };
227 }
228
229 /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
230 /// PHI nodes or outside of the basic block that defines it, or used by a 
231 /// switch or atomic instruction, which may expand to multiple basic blocks.
232 static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
233   if (isa<PHINode>(I)) return true;
234   BasicBlock *BB = I->getParent();
235   for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
236     if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI) ||
237         // FIXME: Remove switchinst special case.
238         isa<SwitchInst>(*UI))
239       return true;
240   return false;
241 }
242
243 /// isOnlyUsedInEntryBlock - If the specified argument is only used in the
244 /// entry block, return true.  This includes arguments used by switches, since
245 /// the switch may expand into multiple basic blocks.
246 static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) {
247   // With FastISel active, we may be splitting blocks, so force creation
248   // of virtual registers for all non-dead arguments.
249   if (EnableFastISel)
250     return A->use_empty();
251
252   BasicBlock *Entry = A->getParent()->begin();
253   for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI)
254     if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI))
255       return false;  // Use not in entry block.
256   return true;
257 }
258
259 FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli)
260   : TLI(tli) {
261 }
262
263 void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf,
264                                bool EnableFastISel) {
265   Fn = &fn;
266   MF = &mf;
267   RegInfo = &MF->getRegInfo();
268
269   // Create a vreg for each argument register that is not dead and is used
270   // outside of the entry block for the function.
271   for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end();
272        AI != E; ++AI)
273     if (!isOnlyUsedInEntryBlock(AI, EnableFastISel))
274       InitializeRegForValue(AI);
275
276   // Initialize the mapping of values to registers.  This is only set up for
277   // instruction values that are used outside of the block that defines
278   // them.
279   Function::iterator BB = Fn->begin(), EB = Fn->end();
280   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
281     if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
282       if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
283         const Type *Ty = AI->getAllocatedType();
284         uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
285         unsigned Align = 
286           std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
287                    AI->getAlignment());
288
289         TySize *= CUI->getZExtValue();   // Get total allocated size.
290         if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
291         StaticAllocaMap[AI] =
292           MF->getFrameInfo()->CreateStackObject(TySize, Align);
293       }
294
295   for (; BB != EB; ++BB)
296     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
297       if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
298         if (!isa<AllocaInst>(I) ||
299             !StaticAllocaMap.count(cast<AllocaInst>(I)))
300           InitializeRegForValue(I);
301
302   // Create an initial MachineBasicBlock for each LLVM BasicBlock in F.  This
303   // also creates the initial PHI MachineInstrs, though none of the input
304   // operands are populated.
305   for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) {
306     MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
307     MBBMap[BB] = MBB;
308     MF->push_back(MBB);
309
310     // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
311     // appropriate.
312     PHINode *PN;
313     for (BasicBlock::iterator I = BB->begin();(PN = dyn_cast<PHINode>(I)); ++I){
314       if (PN->use_empty()) continue;
315       
316       unsigned PHIReg = ValueMap[PN];
317       assert(PHIReg && "PHI node does not have an assigned virtual register!");
318
319       SmallVector<MVT, 4> ValueVTs;
320       ComputeValueVTs(TLI, PN->getType(), ValueVTs);
321       for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
322         MVT VT = ValueVTs[vti];
323         unsigned NumRegisters = TLI.getNumRegisters(VT);
324         const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
325         for (unsigned i = 0; i != NumRegisters; ++i)
326           BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i);
327         PHIReg += NumRegisters;
328       }
329     }
330   }
331 }
332
333 unsigned FunctionLoweringInfo::MakeReg(MVT VT) {
334   return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
335 }
336
337 /// CreateRegForValue - Allocate the appropriate number of virtual registers of
338 /// the correctly promoted or expanded types.  Assign these registers
339 /// consecutive vreg numbers and return the first assigned number.
340 ///
341 /// In the case that the given value has struct or array type, this function
342 /// will assign registers for each member or element.
343 ///
344 unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) {
345   SmallVector<MVT, 4> ValueVTs;
346   ComputeValueVTs(TLI, V->getType(), ValueVTs);
347
348   unsigned FirstReg = 0;
349   for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
350     MVT ValueVT = ValueVTs[Value];
351     MVT RegisterVT = TLI.getRegisterType(ValueVT);
352
353     unsigned NumRegs = TLI.getNumRegisters(ValueVT);
354     for (unsigned i = 0; i != NumRegs; ++i) {
355       unsigned R = MakeReg(RegisterVT);
356       if (!FirstReg) FirstReg = R;
357     }
358   }
359   return FirstReg;
360 }
361
362 /// getCopyFromParts - Create a value that contains the specified legal parts
363 /// combined into the value they represent.  If the parts combine to a type
364 /// larger then ValueVT then AssertOp can be used to specify whether the extra
365 /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
366 /// (ISD::AssertSext).
367 static SDValue getCopyFromParts(SelectionDAG &DAG,
368                                   const SDValue *Parts,
369                                   unsigned NumParts,
370                                   MVT PartVT,
371                                   MVT ValueVT,
372                                   ISD::NodeType AssertOp = ISD::DELETED_NODE) {
373   assert(NumParts > 0 && "No parts to assemble!");
374   TargetLowering &TLI = DAG.getTargetLoweringInfo();
375   SDValue Val = Parts[0];
376
377   if (NumParts > 1) {
378     // Assemble the value from multiple parts.
379     if (!ValueVT.isVector()) {
380       unsigned PartBits = PartVT.getSizeInBits();
381       unsigned ValueBits = ValueVT.getSizeInBits();
382
383       // Assemble the power of 2 part.
384       unsigned RoundParts = NumParts & (NumParts - 1) ?
385         1 << Log2_32(NumParts) : NumParts;
386       unsigned RoundBits = PartBits * RoundParts;
387       MVT RoundVT = RoundBits == ValueBits ?
388         ValueVT : MVT::getIntegerVT(RoundBits);
389       SDValue Lo, Hi;
390
391       if (RoundParts > 2) {
392         MVT HalfVT = MVT::getIntegerVT(RoundBits/2);
393         Lo = getCopyFromParts(DAG, Parts, RoundParts/2, PartVT, HalfVT);
394         Hi = getCopyFromParts(DAG, Parts+RoundParts/2, RoundParts/2,
395                               PartVT, HalfVT);
396       } else {
397         Lo = Parts[0];
398         Hi = Parts[1];
399       }
400       if (TLI.isBigEndian())
401         std::swap(Lo, Hi);
402       Val = DAG.getNode(ISD::BUILD_PAIR, RoundVT, Lo, Hi);
403
404       if (RoundParts < NumParts) {
405         // Assemble the trailing non-power-of-2 part.
406         unsigned OddParts = NumParts - RoundParts;
407         MVT OddVT = MVT::getIntegerVT(OddParts * PartBits);
408         Hi = getCopyFromParts(DAG, Parts+RoundParts, OddParts, PartVT, OddVT);
409
410         // Combine the round and odd parts.
411         Lo = Val;
412         if (TLI.isBigEndian())
413           std::swap(Lo, Hi);
414         MVT TotalVT = MVT::getIntegerVT(NumParts * PartBits);
415         Hi = DAG.getNode(ISD::ANY_EXTEND, TotalVT, Hi);
416         Hi = DAG.getNode(ISD::SHL, TotalVT, Hi,
417                          DAG.getConstant(Lo.getValueType().getSizeInBits(),
418                                          TLI.getShiftAmountTy()));
419         Lo = DAG.getNode(ISD::ZERO_EXTEND, TotalVT, Lo);
420         Val = DAG.getNode(ISD::OR, TotalVT, Lo, Hi);
421       }
422     } else {
423       // Handle a multi-element vector.
424       MVT IntermediateVT, RegisterVT;
425       unsigned NumIntermediates;
426       unsigned NumRegs =
427         TLI.getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates,
428                                    RegisterVT);
429       assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
430       NumParts = NumRegs; // Silence a compiler warning.
431       assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
432       assert(RegisterVT == Parts[0].getValueType() &&
433              "Part type doesn't match part!");
434
435       // Assemble the parts into intermediate operands.
436       SmallVector<SDValue, 8> Ops(NumIntermediates);
437       if (NumIntermediates == NumParts) {
438         // If the register was not expanded, truncate or copy the value,
439         // as appropriate.
440         for (unsigned i = 0; i != NumParts; ++i)
441           Ops[i] = getCopyFromParts(DAG, &Parts[i], 1,
442                                     PartVT, IntermediateVT);
443       } else if (NumParts > 0) {
444         // If the intermediate type was expanded, build the intermediate operands
445         // from the parts.
446         assert(NumParts % NumIntermediates == 0 &&
447                "Must expand into a divisible number of parts!");
448         unsigned Factor = NumParts / NumIntermediates;
449         for (unsigned i = 0; i != NumIntermediates; ++i)
450           Ops[i] = getCopyFromParts(DAG, &Parts[i * Factor], Factor,
451                                     PartVT, IntermediateVT);
452       }
453
454       // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
455       // operands.
456       Val = DAG.getNode(IntermediateVT.isVector() ?
457                         ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR,
458                         ValueVT, &Ops[0], NumIntermediates);
459     }
460   }
461
462   // There is now one part, held in Val.  Correct it to match ValueVT.
463   PartVT = Val.getValueType();
464
465   if (PartVT == ValueVT)
466     return Val;
467
468   if (PartVT.isVector()) {
469     assert(ValueVT.isVector() && "Unknown vector conversion!");
470     return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val);
471   }
472
473   if (ValueVT.isVector()) {
474     assert(ValueVT.getVectorElementType() == PartVT &&
475            ValueVT.getVectorNumElements() == 1 &&
476            "Only trivial scalar-to-vector conversions should get here!");
477     return DAG.getNode(ISD::BUILD_VECTOR, ValueVT, Val);
478   }
479
480   if (PartVT.isInteger() &&
481       ValueVT.isInteger()) {
482     if (ValueVT.bitsLT(PartVT)) {
483       // For a truncate, see if we have any information to
484       // indicate whether the truncated bits will always be
485       // zero or sign-extension.
486       if (AssertOp != ISD::DELETED_NODE)
487         Val = DAG.getNode(AssertOp, PartVT, Val,
488                           DAG.getValueType(ValueVT));
489       return DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
490     } else {
491       return DAG.getNode(ISD::ANY_EXTEND, ValueVT, Val);
492     }
493   }
494
495   if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
496     if (ValueVT.bitsLT(Val.getValueType()))
497       // FP_ROUND's are always exact here.
498       return DAG.getNode(ISD::FP_ROUND, ValueVT, Val,
499                          DAG.getIntPtrConstant(1));
500     return DAG.getNode(ISD::FP_EXTEND, ValueVT, Val);
501   }
502
503   if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
504     return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val);
505
506   assert(0 && "Unknown mismatch!");
507   return SDValue();
508 }
509
510 /// getCopyToParts - Create a series of nodes that contain the specified value
511 /// split into legal parts.  If the parts contain more bits than Val, then, for
512 /// integers, ExtendKind can be used to specify how to generate the extra bits.
513 static void getCopyToParts(SelectionDAG &DAG,
514                            SDValue Val,
515                            SDValue *Parts,
516                            unsigned NumParts,
517                            MVT PartVT,
518                            ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
519   TargetLowering &TLI = DAG.getTargetLoweringInfo();
520   MVT PtrVT = TLI.getPointerTy();
521   MVT ValueVT = Val.getValueType();
522   unsigned PartBits = PartVT.getSizeInBits();
523   assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
524
525   if (!NumParts)
526     return;
527
528   if (!ValueVT.isVector()) {
529     if (PartVT == ValueVT) {
530       assert(NumParts == 1 && "No-op copy with multiple parts!");
531       Parts[0] = Val;
532       return;
533     }
534
535     if (NumParts * PartBits > ValueVT.getSizeInBits()) {
536       // If the parts cover more bits than the value has, promote the value.
537       if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
538         assert(NumParts == 1 && "Do not know what to promote to!");
539         Val = DAG.getNode(ISD::FP_EXTEND, PartVT, Val);
540       } else if (PartVT.isInteger() && ValueVT.isInteger()) {
541         ValueVT = MVT::getIntegerVT(NumParts * PartBits);
542         Val = DAG.getNode(ExtendKind, ValueVT, Val);
543       } else {
544         assert(0 && "Unknown mismatch!");
545       }
546     } else if (PartBits == ValueVT.getSizeInBits()) {
547       // Different types of the same size.
548       assert(NumParts == 1 && PartVT != ValueVT);
549       Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val);
550     } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
551       // If the parts cover less bits than value has, truncate the value.
552       if (PartVT.isInteger() && ValueVT.isInteger()) {
553         ValueVT = MVT::getIntegerVT(NumParts * PartBits);
554         Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
555       } else {
556         assert(0 && "Unknown mismatch!");
557       }
558     }
559
560     // The value may have changed - recompute ValueVT.
561     ValueVT = Val.getValueType();
562     assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
563            "Failed to tile the value with PartVT!");
564
565     if (NumParts == 1) {
566       assert(PartVT == ValueVT && "Type conversion failed!");
567       Parts[0] = Val;
568       return;
569     }
570
571     // Expand the value into multiple parts.
572     if (NumParts & (NumParts - 1)) {
573       // The number of parts is not a power of 2.  Split off and copy the tail.
574       assert(PartVT.isInteger() && ValueVT.isInteger() &&
575              "Do not know what to expand to!");
576       unsigned RoundParts = 1 << Log2_32(NumParts);
577       unsigned RoundBits = RoundParts * PartBits;
578       unsigned OddParts = NumParts - RoundParts;
579       SDValue OddVal = DAG.getNode(ISD::SRL, ValueVT, Val,
580                                      DAG.getConstant(RoundBits,
581                                                      TLI.getShiftAmountTy()));
582       getCopyToParts(DAG, OddVal, Parts + RoundParts, OddParts, PartVT);
583       if (TLI.isBigEndian())
584         // The odd parts were reversed by getCopyToParts - unreverse them.
585         std::reverse(Parts + RoundParts, Parts + NumParts);
586       NumParts = RoundParts;
587       ValueVT = MVT::getIntegerVT(NumParts * PartBits);
588       Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
589     }
590
591     // The number of parts is a power of 2.  Repeatedly bisect the value using
592     // EXTRACT_ELEMENT.
593     Parts[0] = DAG.getNode(ISD::BIT_CONVERT,
594                            MVT::getIntegerVT(ValueVT.getSizeInBits()),
595                            Val);
596     for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
597       for (unsigned i = 0; i < NumParts; i += StepSize) {
598         unsigned ThisBits = StepSize * PartBits / 2;
599         MVT ThisVT = MVT::getIntegerVT (ThisBits);
600         SDValue &Part0 = Parts[i];
601         SDValue &Part1 = Parts[i+StepSize/2];
602
603         Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0,
604                             DAG.getConstant(1, PtrVT));
605         Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0,
606                             DAG.getConstant(0, PtrVT));
607
608         if (ThisBits == PartBits && ThisVT != PartVT) {
609           Part0 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part0);
610           Part1 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part1);
611         }
612       }
613     }
614
615     if (TLI.isBigEndian())
616       std::reverse(Parts, Parts + NumParts);
617
618     return;
619   }
620
621   // Vector ValueVT.
622   if (NumParts == 1) {
623     if (PartVT != ValueVT) {
624       if (PartVT.isVector()) {
625         Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val);
626       } else {
627         assert(ValueVT.getVectorElementType() == PartVT &&
628                ValueVT.getVectorNumElements() == 1 &&
629                "Only trivial vector-to-scalar conversions should get here!");
630         Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, PartVT, Val,
631                           DAG.getConstant(0, PtrVT));
632       }
633     }
634
635     Parts[0] = Val;
636     return;
637   }
638
639   // Handle a multi-element vector.
640   MVT IntermediateVT, RegisterVT;
641   unsigned NumIntermediates;
642   unsigned NumRegs =
643     DAG.getTargetLoweringInfo()
644       .getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates,
645                               RegisterVT);
646   unsigned NumElements = ValueVT.getVectorNumElements();
647
648   assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
649   NumParts = NumRegs; // Silence a compiler warning.
650   assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
651
652   // Split the vector into intermediate operands.
653   SmallVector<SDValue, 8> Ops(NumIntermediates);
654   for (unsigned i = 0; i != NumIntermediates; ++i)
655     if (IntermediateVT.isVector())
656       Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR,
657                            IntermediateVT, Val,
658                            DAG.getConstant(i * (NumElements / NumIntermediates),
659                                            PtrVT));
660     else
661       Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
662                            IntermediateVT, Val, 
663                            DAG.getConstant(i, PtrVT));
664
665   // Split the intermediate operands into legal parts.
666   if (NumParts == NumIntermediates) {
667     // If the register was not expanded, promote or copy the value,
668     // as appropriate.
669     for (unsigned i = 0; i != NumParts; ++i)
670       getCopyToParts(DAG, Ops[i], &Parts[i], 1, PartVT);
671   } else if (NumParts > 0) {
672     // If the intermediate type was expanded, split each the value into
673     // legal parts.
674     assert(NumParts % NumIntermediates == 0 &&
675            "Must expand into a divisible number of parts!");
676     unsigned Factor = NumParts / NumIntermediates;
677     for (unsigned i = 0; i != NumIntermediates; ++i)
678       getCopyToParts(DAG, Ops[i], &Parts[i * Factor], Factor, PartVT);
679   }
680 }
681
682
683 void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
684   AA = &aa;
685   GFI = gfi;
686   TD = DAG.getTarget().getTargetData();
687 }
688
689 /// clear - Clear out the curret SelectionDAG and the associated
690 /// state and prepare this SelectionDAGLowering object to be used
691 /// for a new block. This doesn't clear out information about
692 /// additional blocks that are needed to complete switch lowering
693 /// or PHI node updating; that information is cleared out as it is
694 /// consumed.
695 void SelectionDAGLowering::clear() {
696   NodeMap.clear();
697   PendingLoads.clear();
698   PendingExports.clear();
699   DAG.clear();
700 }
701
702 /// getRoot - Return the current virtual root of the Selection DAG,
703 /// flushing any PendingLoad items. This must be done before emitting
704 /// a store or any other node that may need to be ordered after any
705 /// prior load instructions.
706 ///
707 SDValue SelectionDAGLowering::getRoot() {
708   if (PendingLoads.empty())
709     return DAG.getRoot();
710
711   if (PendingLoads.size() == 1) {
712     SDValue Root = PendingLoads[0];
713     DAG.setRoot(Root);
714     PendingLoads.clear();
715     return Root;
716   }
717
718   // Otherwise, we have to make a token factor node.
719   SDValue Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
720                                &PendingLoads[0], PendingLoads.size());
721   PendingLoads.clear();
722   DAG.setRoot(Root);
723   return Root;
724 }
725
726 /// getControlRoot - Similar to getRoot, but instead of flushing all the
727 /// PendingLoad items, flush all the PendingExports items. It is necessary
728 /// to do this before emitting a terminator instruction.
729 ///
730 SDValue SelectionDAGLowering::getControlRoot() {
731   SDValue Root = DAG.getRoot();
732
733   if (PendingExports.empty())
734     return Root;
735
736   // Turn all of the CopyToReg chains into one factored node.
737   if (Root.getOpcode() != ISD::EntryToken) {
738     unsigned i = 0, e = PendingExports.size();
739     for (; i != e; ++i) {
740       assert(PendingExports[i].getNode()->getNumOperands() > 1);
741       if (PendingExports[i].getNode()->getOperand(0) == Root)
742         break;  // Don't add the root if we already indirectly depend on it.
743     }
744
745     if (i == e)
746       PendingExports.push_back(Root);
747   }
748
749   Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
750                      &PendingExports[0],
751                      PendingExports.size());
752   PendingExports.clear();
753   DAG.setRoot(Root);
754   return Root;
755 }
756
757 void SelectionDAGLowering::visit(Instruction &I) {
758   visit(I.getOpcode(), I);
759 }
760
761 void SelectionDAGLowering::visit(unsigned Opcode, User &I) {
762   // Note: this doesn't use InstVisitor, because it has to work with
763   // ConstantExpr's in addition to instructions.
764   switch (Opcode) {
765   default: assert(0 && "Unknown instruction type encountered!");
766            abort();
767     // Build the switch statement using the Instruction.def file.
768 #define HANDLE_INST(NUM, OPCODE, CLASS) \
769   case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
770 #include "llvm/Instruction.def"
771   }
772
773
774 void SelectionDAGLowering::visitAdd(User &I) {
775   if (I.getType()->isFPOrFPVector())
776     visitBinary(I, ISD::FADD);
777   else
778     visitBinary(I, ISD::ADD);
779 }
780
781 void SelectionDAGLowering::visitMul(User &I) {
782   if (I.getType()->isFPOrFPVector())
783     visitBinary(I, ISD::FMUL);
784   else
785     visitBinary(I, ISD::MUL);
786 }
787
788 SDValue SelectionDAGLowering::getValue(const Value *V) {
789   SDValue &N = NodeMap[V];
790   if (N.getNode()) return N;
791   
792   if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
793     MVT VT = TLI.getValueType(V->getType(), true);
794     
795     if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
796       return N = DAG.getConstant(*CI, VT);
797
798     if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
799       return N = DAG.getGlobalAddress(GV, VT);
800     
801     if (isa<ConstantPointerNull>(C))
802       return N = DAG.getConstant(0, TLI.getPointerTy());
803     
804     if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
805       return N = DAG.getConstantFP(*CFP, VT);
806     
807     if (isa<UndefValue>(C) && !isa<VectorType>(V->getType()) &&
808         !V->getType()->isAggregateType())
809       return N = DAG.getNode(ISD::UNDEF, VT);
810
811     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
812       visit(CE->getOpcode(), *CE);
813       SDValue N1 = NodeMap[V];
814       assert(N1.getNode() && "visit didn't populate the ValueMap!");
815       return N1;
816     }
817     
818     if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
819       SmallVector<SDValue, 4> Constants;
820       for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
821            OI != OE; ++OI) {
822         SDNode *Val = getValue(*OI).getNode();
823         for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
824           Constants.push_back(SDValue(Val, i));
825       }
826       return DAG.getMergeValues(&Constants[0], Constants.size());
827     }
828
829     if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
830       assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
831              "Unknown struct or array constant!");
832
833       SmallVector<MVT, 4> ValueVTs;
834       ComputeValueVTs(TLI, C->getType(), ValueVTs);
835       unsigned NumElts = ValueVTs.size();
836       if (NumElts == 0)
837         return SDValue(); // empty struct
838       SmallVector<SDValue, 4> Constants(NumElts);
839       for (unsigned i = 0; i != NumElts; ++i) {
840         MVT EltVT = ValueVTs[i];
841         if (isa<UndefValue>(C))
842           Constants[i] = DAG.getNode(ISD::UNDEF, EltVT);
843         else if (EltVT.isFloatingPoint())
844           Constants[i] = DAG.getConstantFP(0, EltVT);
845         else
846           Constants[i] = DAG.getConstant(0, EltVT);
847       }
848       return DAG.getMergeValues(&Constants[0], NumElts);
849     }
850
851     const VectorType *VecTy = cast<VectorType>(V->getType());
852     unsigned NumElements = VecTy->getNumElements();
853     
854     // Now that we know the number and type of the elements, get that number of
855     // elements into the Ops array based on what kind of constant it is.
856     SmallVector<SDValue, 16> Ops;
857     if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
858       for (unsigned i = 0; i != NumElements; ++i)
859         Ops.push_back(getValue(CP->getOperand(i)));
860     } else {
861       assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
862              "Unknown vector constant!");
863       MVT EltVT = TLI.getValueType(VecTy->getElementType());
864
865       SDValue Op;
866       if (isa<UndefValue>(C))
867         Op = DAG.getNode(ISD::UNDEF, EltVT);
868       else if (EltVT.isFloatingPoint())
869         Op = DAG.getConstantFP(0, EltVT);
870       else
871         Op = DAG.getConstant(0, EltVT);
872       Ops.assign(NumElements, Op);
873     }
874     
875     // Create a BUILD_VECTOR node.
876     return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
877   }
878       
879   // If this is a static alloca, generate it as the frameindex instead of
880   // computation.
881   if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
882     DenseMap<const AllocaInst*, int>::iterator SI =
883       FuncInfo.StaticAllocaMap.find(AI);
884     if (SI != FuncInfo.StaticAllocaMap.end())
885       return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
886   }
887       
888   unsigned InReg = FuncInfo.ValueMap[V];
889   assert(InReg && "Value not in map!");
890   
891   RegsForValue RFV(TLI, InReg, V->getType());
892   SDValue Chain = DAG.getEntryNode();
893   return RFV.getCopyFromRegs(DAG, Chain, NULL);
894 }
895
896
897 void SelectionDAGLowering::visitRet(ReturnInst &I) {
898   if (I.getNumOperands() == 0) {
899     DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getControlRoot()));
900     return;
901   }
902   
903   SmallVector<SDValue, 8> NewValues;
904   NewValues.push_back(getControlRoot());
905   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {  
906     SDValue RetOp = getValue(I.getOperand(i));
907
908     SmallVector<MVT, 4> ValueVTs;
909     ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
910     for (unsigned j = 0, f = ValueVTs.size(); j != f; ++j) {
911       MVT VT = ValueVTs[j];
912
913       // FIXME: C calling convention requires the return type to be promoted to
914       // at least 32-bit. But this is not necessary for non-C calling conventions.
915       if (VT.isInteger()) {
916         MVT MinVT = TLI.getRegisterType(MVT::i32);
917         if (VT.bitsLT(MinVT))
918           VT = MinVT;
919       }
920
921       unsigned NumParts = TLI.getNumRegisters(VT);
922       MVT PartVT = TLI.getRegisterType(VT);
923       SmallVector<SDValue, 4> Parts(NumParts);
924       ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
925   
926       const Function *F = I.getParent()->getParent();
927       if (F->paramHasAttr(0, ParamAttr::SExt))
928         ExtendKind = ISD::SIGN_EXTEND;
929       else if (F->paramHasAttr(0, ParamAttr::ZExt))
930         ExtendKind = ISD::ZERO_EXTEND;
931
932       getCopyToParts(DAG, SDValue(RetOp.getNode(), RetOp.getResNo() + j),
933                      &Parts[0], NumParts, PartVT, ExtendKind);
934
935       for (unsigned i = 0; i < NumParts; ++i) {
936         NewValues.push_back(Parts[i]);
937         NewValues.push_back(DAG.getArgFlags(ISD::ArgFlagsTy()));
938       }
939     }
940   }
941   DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other,
942                           &NewValues[0], NewValues.size()));
943 }
944
945 /// ExportFromCurrentBlock - If this condition isn't known to be exported from
946 /// the current basic block, add it to ValueMap now so that we'll get a
947 /// CopyTo/FromReg.
948 void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) {
949   // No need to export constants.
950   if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
951   
952   // Already exported?
953   if (FuncInfo.isExportedInst(V)) return;
954
955   unsigned Reg = FuncInfo.InitializeRegForValue(V);
956   CopyValueToVirtualRegister(V, Reg);
957 }
958
959 bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V,
960                                                     const BasicBlock *FromBB) {
961   // The operands of the setcc have to be in this block.  We don't know
962   // how to export them from some other block.
963   if (Instruction *VI = dyn_cast<Instruction>(V)) {
964     // Can export from current BB.
965     if (VI->getParent() == FromBB)
966       return true;
967     
968     // Is already exported, noop.
969     return FuncInfo.isExportedInst(V);
970   }
971   
972   // If this is an argument, we can export it if the BB is the entry block or
973   // if it is already exported.
974   if (isa<Argument>(V)) {
975     if (FromBB == &FromBB->getParent()->getEntryBlock())
976       return true;
977
978     // Otherwise, can only export this if it is already exported.
979     return FuncInfo.isExportedInst(V);
980   }
981   
982   // Otherwise, constants can always be exported.
983   return true;
984 }
985
986 static bool InBlock(const Value *V, const BasicBlock *BB) {
987   if (const Instruction *I = dyn_cast<Instruction>(V))
988     return I->getParent() == BB;
989   return true;
990 }
991
992 /// FindMergedConditions - If Cond is an expression like 
993 void SelectionDAGLowering::FindMergedConditions(Value *Cond,
994                                                 MachineBasicBlock *TBB,
995                                                 MachineBasicBlock *FBB,
996                                                 MachineBasicBlock *CurBB,
997                                                 unsigned Opc) {
998   // If this node is not part of the or/and tree, emit it as a branch.
999   Instruction *BOp = dyn_cast<Instruction>(Cond);
1000
1001   if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || 
1002       (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1003       BOp->getParent() != CurBB->getBasicBlock() ||
1004       !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1005       !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1006     const BasicBlock *BB = CurBB->getBasicBlock();
1007     
1008     // If the leaf of the tree is a comparison, merge the condition into 
1009     // the caseblock.
1010     if ((isa<ICmpInst>(Cond) || isa<FCmpInst>(Cond)) &&
1011         // The operands of the cmp have to be in this block.  We don't know
1012         // how to export them from some other block.  If this is the first block
1013         // of the sequence, no exporting is needed.
1014         (CurBB == CurMBB ||
1015          (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1016           isExportableFromCurrentBlock(BOp->getOperand(1), BB)))) {
1017       BOp = cast<Instruction>(Cond);
1018       ISD::CondCode Condition;
1019       if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
1020         switch (IC->getPredicate()) {
1021         default: assert(0 && "Unknown icmp predicate opcode!");
1022         case ICmpInst::ICMP_EQ:  Condition = ISD::SETEQ;  break;
1023         case ICmpInst::ICMP_NE:  Condition = ISD::SETNE;  break;
1024         case ICmpInst::ICMP_SLE: Condition = ISD::SETLE;  break;
1025         case ICmpInst::ICMP_ULE: Condition = ISD::SETULE; break;
1026         case ICmpInst::ICMP_SGE: Condition = ISD::SETGE;  break;
1027         case ICmpInst::ICMP_UGE: Condition = ISD::SETUGE; break;
1028         case ICmpInst::ICMP_SLT: Condition = ISD::SETLT;  break;
1029         case ICmpInst::ICMP_ULT: Condition = ISD::SETULT; break;
1030         case ICmpInst::ICMP_SGT: Condition = ISD::SETGT;  break;
1031         case ICmpInst::ICMP_UGT: Condition = ISD::SETUGT; break;
1032         }
1033       } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
1034         ISD::CondCode FPC, FOC;
1035         switch (FC->getPredicate()) {
1036         default: assert(0 && "Unknown fcmp predicate opcode!");
1037         case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1038         case FCmpInst::FCMP_OEQ:   FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1039         case FCmpInst::FCMP_OGT:   FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1040         case FCmpInst::FCMP_OGE:   FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1041         case FCmpInst::FCMP_OLT:   FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1042         case FCmpInst::FCMP_OLE:   FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1043         case FCmpInst::FCMP_ONE:   FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1044         case FCmpInst::FCMP_ORD:   FOC = FPC = ISD::SETO;   break;
1045         case FCmpInst::FCMP_UNO:   FOC = FPC = ISD::SETUO;  break;
1046         case FCmpInst::FCMP_UEQ:   FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1047         case FCmpInst::FCMP_UGT:   FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1048         case FCmpInst::FCMP_UGE:   FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1049         case FCmpInst::FCMP_ULT:   FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1050         case FCmpInst::FCMP_ULE:   FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1051         case FCmpInst::FCMP_UNE:   FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1052         case FCmpInst::FCMP_TRUE:  FOC = FPC = ISD::SETTRUE; break;
1053         }
1054         if (FiniteOnlyFPMath())
1055           Condition = FOC;
1056         else 
1057           Condition = FPC;
1058       } else {
1059         Condition = ISD::SETEQ; // silence warning.
1060         assert(0 && "Unknown compare instruction");
1061       }
1062       
1063       CaseBlock CB(Condition, BOp->getOperand(0), 
1064                    BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1065       SwitchCases.push_back(CB);
1066       return;
1067     }
1068     
1069     // Create a CaseBlock record representing this branch.
1070     CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(),
1071                  NULL, TBB, FBB, CurBB);
1072     SwitchCases.push_back(CB);
1073     return;
1074   }
1075   
1076   
1077   //  Create TmpBB after CurBB.
1078   MachineFunction::iterator BBI = CurBB;
1079   MachineFunction &MF = DAG.getMachineFunction();
1080   MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1081   CurBB->getParent()->insert(++BBI, TmpBB);
1082   
1083   if (Opc == Instruction::Or) {
1084     // Codegen X | Y as:
1085     //   jmp_if_X TBB
1086     //   jmp TmpBB
1087     // TmpBB:
1088     //   jmp_if_Y TBB
1089     //   jmp FBB
1090     //
1091   
1092     // Emit the LHS condition.
1093     FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
1094   
1095     // Emit the RHS condition into TmpBB.
1096     FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1097   } else {
1098     assert(Opc == Instruction::And && "Unknown merge op!");
1099     // Codegen X & Y as:
1100     //   jmp_if_X TmpBB
1101     //   jmp FBB
1102     // TmpBB:
1103     //   jmp_if_Y TBB
1104     //   jmp FBB
1105     //
1106     //  This requires creation of TmpBB after CurBB.
1107     
1108     // Emit the LHS condition.
1109     FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
1110     
1111     // Emit the RHS condition into TmpBB.
1112     FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1113   }
1114 }
1115
1116 /// If the set of cases should be emitted as a series of branches, return true.
1117 /// If we should emit this as a bunch of and/or'd together conditions, return
1118 /// false.
1119 bool 
1120 SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
1121   if (Cases.size() != 2) return true;
1122   
1123   // If this is two comparisons of the same values or'd or and'd together, they
1124   // will get folded into a single comparison, so don't emit two blocks.
1125   if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1126        Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1127       (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1128        Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1129     return false;
1130   }
1131   
1132   return true;
1133 }
1134
1135 void SelectionDAGLowering::visitBr(BranchInst &I) {
1136   // Update machine-CFG edges.
1137   MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1138
1139   // Figure out which block is immediately after the current one.
1140   MachineBasicBlock *NextBlock = 0;
1141   MachineFunction::iterator BBI = CurMBB;
1142   if (++BBI != CurMBB->getParent()->end())
1143     NextBlock = BBI;
1144
1145   if (I.isUnconditional()) {
1146     // Update machine-CFG edges.
1147     CurMBB->addSuccessor(Succ0MBB);
1148     
1149     // If this is not a fall-through branch, emit the branch.
1150     if (Succ0MBB != NextBlock)
1151       DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
1152                               DAG.getBasicBlock(Succ0MBB)));
1153     return;
1154   }
1155
1156   // If this condition is one of the special cases we handle, do special stuff
1157   // now.
1158   Value *CondVal = I.getCondition();
1159   MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1160
1161   // If this is a series of conditions that are or'd or and'd together, emit
1162   // this as a sequence of branches instead of setcc's with and/or operations.
1163   // For example, instead of something like:
1164   //     cmp A, B
1165   //     C = seteq 
1166   //     cmp D, E
1167   //     F = setle 
1168   //     or C, F
1169   //     jnz foo
1170   // Emit:
1171   //     cmp A, B
1172   //     je foo
1173   //     cmp D, E
1174   //     jle foo
1175   //
1176   if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
1177     if (BOp->hasOneUse() && 
1178         (BOp->getOpcode() == Instruction::And ||
1179          BOp->getOpcode() == Instruction::Or)) {
1180       FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1181       // If the compares in later blocks need to use values not currently
1182       // exported from this block, export them now.  This block should always
1183       // be the first entry.
1184       assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
1185       
1186       // Allow some cases to be rejected.
1187       if (ShouldEmitAsBranches(SwitchCases)) {
1188         for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1189           ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1190           ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1191         }
1192         
1193         // Emit the branch for this block.
1194         visitSwitchCase(SwitchCases[0]);
1195         SwitchCases.erase(SwitchCases.begin());
1196         return;
1197       }
1198       
1199       // Okay, we decided not to do this, remove any inserted MBB's and clear
1200       // SwitchCases.
1201       for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
1202         CurMBB->getParent()->erase(SwitchCases[i].ThisBB);
1203       
1204       SwitchCases.clear();
1205     }
1206   }
1207   
1208   // Create a CaseBlock record representing this branch.
1209   CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(),
1210                NULL, Succ0MBB, Succ1MBB, CurMBB);
1211   // Use visitSwitchCase to actually insert the fast branch sequence for this
1212   // cond branch.
1213   visitSwitchCase(CB);
1214 }
1215
1216 /// visitSwitchCase - Emits the necessary code to represent a single node in
1217 /// the binary search tree resulting from lowering a switch instruction.
1218 void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) {
1219   SDValue Cond;
1220   SDValue CondLHS = getValue(CB.CmpLHS);
1221   
1222   // Build the setcc now. 
1223   if (CB.CmpMHS == NULL) {
1224     // Fold "(X == true)" to X and "(X == false)" to !X to
1225     // handle common cases produced by branch lowering.
1226     if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ)
1227       Cond = CondLHS;
1228     else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) {
1229       SDValue True = DAG.getConstant(1, CondLHS.getValueType());
1230       Cond = DAG.getNode(ISD::XOR, CondLHS.getValueType(), CondLHS, True);
1231     } else
1232       Cond = DAG.getSetCC(MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
1233   } else {
1234     assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1235
1236     uint64_t Low = cast<ConstantInt>(CB.CmpLHS)->getSExtValue();
1237     uint64_t High  = cast<ConstantInt>(CB.CmpRHS)->getSExtValue();
1238
1239     SDValue CmpOp = getValue(CB.CmpMHS);
1240     MVT VT = CmpOp.getValueType();
1241
1242     if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
1243       Cond = DAG.getSetCC(MVT::i1, CmpOp, DAG.getConstant(High, VT), ISD::SETLE);
1244     } else {
1245       SDValue SUB = DAG.getNode(ISD::SUB, VT, CmpOp, DAG.getConstant(Low, VT));
1246       Cond = DAG.getSetCC(MVT::i1, SUB,
1247                           DAG.getConstant(High-Low, VT), ISD::SETULE);
1248     }
1249   }
1250   
1251   // Update successor info
1252   CurMBB->addSuccessor(CB.TrueBB);
1253   CurMBB->addSuccessor(CB.FalseBB);
1254   
1255   // Set NextBlock to be the MBB immediately after the current one, if any.
1256   // This is used to avoid emitting unnecessary branches to the next block.
1257   MachineBasicBlock *NextBlock = 0;
1258   MachineFunction::iterator BBI = CurMBB;
1259   if (++BBI != CurMBB->getParent()->end())
1260     NextBlock = BBI;
1261   
1262   // If the lhs block is the next block, invert the condition so that we can
1263   // fall through to the lhs instead of the rhs block.
1264   if (CB.TrueBB == NextBlock) {
1265     std::swap(CB.TrueBB, CB.FalseBB);
1266     SDValue True = DAG.getConstant(1, Cond.getValueType());
1267     Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
1268   }
1269   SDValue BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(), Cond,
1270                                  DAG.getBasicBlock(CB.TrueBB));
1271   
1272   // If the branch was constant folded, fix up the CFG.
1273   if (BrCond.getOpcode() == ISD::BR) {
1274     CurMBB->removeSuccessor(CB.FalseBB);
1275     DAG.setRoot(BrCond);
1276   } else {
1277     // Otherwise, go ahead and insert the false branch.
1278     if (BrCond == getControlRoot()) 
1279       CurMBB->removeSuccessor(CB.TrueBB);
1280     
1281     if (CB.FalseBB == NextBlock)
1282       DAG.setRoot(BrCond);
1283     else
1284       DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, 
1285                               DAG.getBasicBlock(CB.FalseBB)));
1286   }
1287 }
1288
1289 /// visitJumpTable - Emit JumpTable node in the current MBB
1290 void SelectionDAGLowering::visitJumpTable(JumpTable &JT) {
1291   // Emit the code for the jump table
1292   assert(JT.Reg != -1U && "Should lower JT Header first!");
1293   MVT PTy = TLI.getPointerTy();
1294   SDValue Index = DAG.getCopyFromReg(getControlRoot(), JT.Reg, PTy);
1295   SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
1296   DAG.setRoot(DAG.getNode(ISD::BR_JT, MVT::Other, Index.getValue(1),
1297                           Table, Index));
1298   return;
1299 }
1300
1301 /// visitJumpTableHeader - This function emits necessary code to produce index
1302 /// in the JumpTable from switch case.
1303 void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT,
1304                                                 JumpTableHeader &JTH) {
1305   // Subtract the lowest switch case value from the value being switched on
1306   // and conditional branch to default mbb if the result is greater than the
1307   // difference between smallest and largest cases.
1308   SDValue SwitchOp = getValue(JTH.SValue);
1309   MVT VT = SwitchOp.getValueType();
1310   SDValue SUB = DAG.getNode(ISD::SUB, VT, SwitchOp,
1311                               DAG.getConstant(JTH.First, VT));
1312   
1313   // The SDNode we just created, which holds the value being switched on
1314   // minus the the smallest case value, needs to be copied to a virtual
1315   // register so it can be used as an index into the jump table in a 
1316   // subsequent basic block.  This value may be smaller or larger than the
1317   // target's pointer type, and therefore require extension or truncating.
1318   if (VT.bitsGT(TLI.getPointerTy()))
1319     SwitchOp = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), SUB);
1320   else
1321     SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), SUB);
1322   
1323   unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
1324   SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), JumpTableReg, SwitchOp);
1325   JT.Reg = JumpTableReg;
1326
1327   // Emit the range check for the jump table, and branch to the default
1328   // block for the switch statement if the value being switched on exceeds
1329   // the largest case in the switch.
1330   SDValue CMP = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB,
1331                                DAG.getConstant(JTH.Last-JTH.First,VT),
1332                                ISD::SETUGT);
1333
1334   // Set NextBlock to be the MBB immediately after the current one, if any.
1335   // This is used to avoid emitting unnecessary branches to the next block.
1336   MachineBasicBlock *NextBlock = 0;
1337   MachineFunction::iterator BBI = CurMBB;
1338   if (++BBI != CurMBB->getParent()->end())
1339     NextBlock = BBI;
1340
1341   SDValue BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, CMP,
1342                                  DAG.getBasicBlock(JT.Default));
1343
1344   if (JT.MBB == NextBlock)
1345     DAG.setRoot(BrCond);
1346   else
1347     DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, 
1348                             DAG.getBasicBlock(JT.MBB)));
1349
1350   return;
1351 }
1352
1353 /// visitBitTestHeader - This function emits necessary code to produce value
1354 /// suitable for "bit tests"
1355 void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) {
1356   // Subtract the minimum value
1357   SDValue SwitchOp = getValue(B.SValue);
1358   MVT VT = SwitchOp.getValueType();
1359   SDValue SUB = DAG.getNode(ISD::SUB, VT, SwitchOp,
1360                               DAG.getConstant(B.First, VT));
1361
1362   // Check range
1363   SDValue RangeCmp = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB,
1364                                     DAG.getConstant(B.Range, VT),
1365                                     ISD::SETUGT);
1366
1367   SDValue ShiftOp;
1368   if (VT.bitsGT(TLI.getShiftAmountTy()))
1369     ShiftOp = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), SUB);
1370   else
1371     ShiftOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getShiftAmountTy(), SUB);
1372
1373   // Make desired shift
1374   SDValue SwitchVal = DAG.getNode(ISD::SHL, TLI.getPointerTy(),
1375                                     DAG.getConstant(1, TLI.getPointerTy()),
1376                                     ShiftOp);
1377
1378   unsigned SwitchReg = FuncInfo.MakeReg(TLI.getPointerTy());
1379   SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), SwitchReg, SwitchVal);
1380   B.Reg = SwitchReg;
1381
1382   // Set NextBlock to be the MBB immediately after the current one, if any.
1383   // This is used to avoid emitting unnecessary branches to the next block.
1384   MachineBasicBlock *NextBlock = 0;
1385   MachineFunction::iterator BBI = CurMBB;
1386   if (++BBI != CurMBB->getParent()->end())
1387     NextBlock = BBI;
1388
1389   MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1390
1391   CurMBB->addSuccessor(B.Default);
1392   CurMBB->addSuccessor(MBB);
1393
1394   SDValue BrRange = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, RangeCmp,
1395                                   DAG.getBasicBlock(B.Default));
1396   
1397   if (MBB == NextBlock)
1398     DAG.setRoot(BrRange);
1399   else
1400     DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, CopyTo,
1401                             DAG.getBasicBlock(MBB)));
1402
1403   return;
1404 }
1405
1406 /// visitBitTestCase - this function produces one "bit test"
1407 void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB,
1408                                             unsigned Reg,
1409                                             BitTestCase &B) {
1410   // Emit bit tests and jumps
1411   SDValue SwitchVal = DAG.getCopyFromReg(getControlRoot(), Reg, 
1412                                            TLI.getPointerTy());
1413   
1414   SDValue AndOp = DAG.getNode(ISD::AND, TLI.getPointerTy(), SwitchVal,
1415                                 DAG.getConstant(B.Mask, TLI.getPointerTy()));
1416   SDValue AndCmp = DAG.getSetCC(TLI.getSetCCResultType(AndOp), AndOp,
1417                                   DAG.getConstant(0, TLI.getPointerTy()),
1418                                   ISD::SETNE);
1419
1420   CurMBB->addSuccessor(B.TargetBB);
1421   CurMBB->addSuccessor(NextMBB);
1422   
1423   SDValue BrAnd = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(),
1424                                 AndCmp, DAG.getBasicBlock(B.TargetBB));
1425
1426   // Set NextBlock to be the MBB immediately after the current one, if any.
1427   // This is used to avoid emitting unnecessary branches to the next block.
1428   MachineBasicBlock *NextBlock = 0;
1429   MachineFunction::iterator BBI = CurMBB;
1430   if (++BBI != CurMBB->getParent()->end())
1431     NextBlock = BBI;
1432
1433   if (NextMBB == NextBlock)
1434     DAG.setRoot(BrAnd);
1435   else
1436     DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrAnd,
1437                             DAG.getBasicBlock(NextMBB)));
1438
1439   return;
1440 }
1441
1442 void SelectionDAGLowering::visitInvoke(InvokeInst &I) {
1443   // Retrieve successors.
1444   MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1445   MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1446
1447   if (isa<InlineAsm>(I.getCalledValue()))
1448     visitInlineAsm(&I);
1449   else
1450     LowerCallTo(&I, getValue(I.getOperand(0)), false, LandingPad);
1451
1452   // If the value of the invoke is used outside of its defining block, make it
1453   // available as a virtual register.
1454   if (!I.use_empty()) {
1455     DenseMap<const Value*, unsigned>::iterator VMI = FuncInfo.ValueMap.find(&I);
1456     if (VMI != FuncInfo.ValueMap.end())
1457       CopyValueToVirtualRegister(&I, VMI->second);
1458   }
1459
1460   // Update successor info
1461   CurMBB->addSuccessor(Return);
1462   CurMBB->addSuccessor(LandingPad);
1463
1464   // Drop into normal successor.
1465   DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
1466                           DAG.getBasicBlock(Return)));
1467 }
1468
1469 void SelectionDAGLowering::visitUnwind(UnwindInst &I) {
1470 }
1471
1472 /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1473 /// small case ranges).
1474 bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR,
1475                                                   CaseRecVector& WorkList,
1476                                                   Value* SV,
1477                                                   MachineBasicBlock* Default) {
1478   Case& BackCase  = *(CR.Range.second-1);
1479   
1480   // Size is the number of Cases represented by this range.
1481   unsigned Size = CR.Range.second - CR.Range.first;
1482   if (Size > 3)
1483     return false;  
1484   
1485   // Get the MachineFunction which holds the current MBB.  This is used when
1486   // inserting any additional MBBs necessary to represent the switch.
1487   MachineFunction *CurMF = CurMBB->getParent();  
1488
1489   // Figure out which block is immediately after the current one.
1490   MachineBasicBlock *NextBlock = 0;
1491   MachineFunction::iterator BBI = CR.CaseBB;
1492
1493   if (++BBI != CurMBB->getParent()->end())
1494     NextBlock = BBI;
1495
1496   // TODO: If any two of the cases has the same destination, and if one value
1497   // is the same as the other, but has one bit unset that the other has set,
1498   // use bit manipulation to do two compares at once.  For example:
1499   // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
1500     
1501   // Rearrange the case blocks so that the last one falls through if possible.
1502   if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1503     // The last case block won't fall through into 'NextBlock' if we emit the
1504     // branches in this order.  See if rearranging a case value would help.
1505     for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1506       if (I->BB == NextBlock) {
1507         std::swap(*I, BackCase);
1508         break;
1509       }
1510     }
1511   }
1512   
1513   // Create a CaseBlock record representing a conditional branch to
1514   // the Case's target mbb if the value being switched on SV is equal
1515   // to C.
1516   MachineBasicBlock *CurBlock = CR.CaseBB;
1517   for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1518     MachineBasicBlock *FallThrough;
1519     if (I != E-1) {
1520       FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1521       CurMF->insert(BBI, FallThrough);
1522     } else {
1523       // If the last case doesn't match, go to the default block.
1524       FallThrough = Default;
1525     }
1526
1527     Value *RHS, *LHS, *MHS;
1528     ISD::CondCode CC;
1529     if (I->High == I->Low) {
1530       // This is just small small case range :) containing exactly 1 case
1531       CC = ISD::SETEQ;
1532       LHS = SV; RHS = I->High; MHS = NULL;
1533     } else {
1534       CC = ISD::SETLE;
1535       LHS = I->Low; MHS = SV; RHS = I->High;
1536     }
1537     CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
1538     
1539     // If emitting the first comparison, just call visitSwitchCase to emit the
1540     // code into the current block.  Otherwise, push the CaseBlock onto the
1541     // vector to be later processed by SDISel, and insert the node's MBB
1542     // before the next MBB.
1543     if (CurBlock == CurMBB)
1544       visitSwitchCase(CB);
1545     else
1546       SwitchCases.push_back(CB);
1547     
1548     CurBlock = FallThrough;
1549   }
1550
1551   return true;
1552 }
1553
1554 static inline bool areJTsAllowed(const TargetLowering &TLI) {
1555   return !DisableJumpTables &&
1556           (TLI.isOperationLegal(ISD::BR_JT, MVT::Other) ||
1557            TLI.isOperationLegal(ISD::BRIND, MVT::Other));
1558 }
1559   
1560 /// handleJTSwitchCase - Emit jumptable for current switch case range
1561 bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR,
1562                                               CaseRecVector& WorkList,
1563                                               Value* SV,
1564                                               MachineBasicBlock* Default) {
1565   Case& FrontCase = *CR.Range.first;
1566   Case& BackCase  = *(CR.Range.second-1);
1567
1568   int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue();
1569   int64_t Last  = cast<ConstantInt>(BackCase.High)->getSExtValue();
1570
1571   uint64_t TSize = 0;
1572   for (CaseItr I = CR.Range.first, E = CR.Range.second;
1573        I!=E; ++I)
1574     TSize += I->size();
1575
1576   if (!areJTsAllowed(TLI) || TSize <= 3)
1577     return false;
1578   
1579   double Density = (double)TSize / (double)((Last - First) + 1ULL);  
1580   if (Density < 0.4)
1581     return false;
1582
1583   DOUT << "Lowering jump table\n"
1584        << "First entry: " << First << ". Last entry: " << Last << "\n"
1585        << "Size: " << TSize << ". Density: " << Density << "\n\n";
1586
1587   // Get the MachineFunction which holds the current MBB.  This is used when
1588   // inserting any additional MBBs necessary to represent the switch.
1589   MachineFunction *CurMF = CurMBB->getParent();
1590
1591   // Figure out which block is immediately after the current one.
1592   MachineBasicBlock *NextBlock = 0;
1593   MachineFunction::iterator BBI = CR.CaseBB;
1594
1595   if (++BBI != CurMBB->getParent()->end())
1596     NextBlock = BBI;
1597
1598   const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1599
1600   // Create a new basic block to hold the code for loading the address
1601   // of the jump table, and jumping to it.  Update successor information;
1602   // we will either branch to the default case for the switch, or the jump
1603   // table.
1604   MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1605   CurMF->insert(BBI, JumpTableBB);
1606   CR.CaseBB->addSuccessor(Default);
1607   CR.CaseBB->addSuccessor(JumpTableBB);
1608                 
1609   // Build a vector of destination BBs, corresponding to each target
1610   // of the jump table. If the value of the jump table slot corresponds to
1611   // a case statement, push the case's BB onto the vector, otherwise, push
1612   // the default BB.
1613   std::vector<MachineBasicBlock*> DestBBs;
1614   int64_t TEI = First;
1615   for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
1616     int64_t Low = cast<ConstantInt>(I->Low)->getSExtValue();
1617     int64_t High = cast<ConstantInt>(I->High)->getSExtValue();
1618     
1619     if ((Low <= TEI) && (TEI <= High)) {
1620       DestBBs.push_back(I->BB);
1621       if (TEI==High)
1622         ++I;
1623     } else {
1624       DestBBs.push_back(Default);
1625     }
1626   }
1627   
1628   // Update successor info. Add one edge to each unique successor.
1629   BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());  
1630   for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), 
1631          E = DestBBs.end(); I != E; ++I) {
1632     if (!SuccsHandled[(*I)->getNumber()]) {
1633       SuccsHandled[(*I)->getNumber()] = true;
1634       JumpTableBB->addSuccessor(*I);
1635     }
1636   }
1637       
1638   // Create a jump table index for this jump table, or return an existing
1639   // one.
1640   unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
1641   
1642   // Set the jump table information so that we can codegen it as a second
1643   // MachineBasicBlock
1644   JumpTable JT(-1U, JTI, JumpTableBB, Default);
1645   JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1646   if (CR.CaseBB == CurMBB)
1647     visitJumpTableHeader(JT, JTH);
1648         
1649   JTCases.push_back(JumpTableBlock(JTH, JT));
1650
1651   return true;
1652 }
1653
1654 /// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1655 /// 2 subtrees.
1656 bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR,
1657                                                    CaseRecVector& WorkList,
1658                                                    Value* SV,
1659                                                    MachineBasicBlock* Default) {
1660   // Get the MachineFunction which holds the current MBB.  This is used when
1661   // inserting any additional MBBs necessary to represent the switch.
1662   MachineFunction *CurMF = CurMBB->getParent();  
1663
1664   // Figure out which block is immediately after the current one.
1665   MachineBasicBlock *NextBlock = 0;
1666   MachineFunction::iterator BBI = CR.CaseBB;
1667
1668   if (++BBI != CurMBB->getParent()->end())
1669     NextBlock = BBI;
1670
1671   Case& FrontCase = *CR.Range.first;
1672   Case& BackCase  = *(CR.Range.second-1);
1673   const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1674
1675   // Size is the number of Cases represented by this range.
1676   unsigned Size = CR.Range.second - CR.Range.first;
1677
1678   int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue();
1679   int64_t Last  = cast<ConstantInt>(BackCase.High)->getSExtValue();
1680   double FMetric = 0;
1681   CaseItr Pivot = CR.Range.first + Size/2;
1682
1683   // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1684   // (heuristically) allow us to emit JumpTable's later.
1685   uint64_t TSize = 0;
1686   for (CaseItr I = CR.Range.first, E = CR.Range.second;
1687        I!=E; ++I)
1688     TSize += I->size();
1689
1690   uint64_t LSize = FrontCase.size();
1691   uint64_t RSize = TSize-LSize;
1692   DOUT << "Selecting best pivot: \n"
1693        << "First: " << First << ", Last: " << Last <<"\n"
1694        << "LSize: " << LSize << ", RSize: " << RSize << "\n";
1695   for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1696        J!=E; ++I, ++J) {
1697     int64_t LEnd = cast<ConstantInt>(I->High)->getSExtValue();
1698     int64_t RBegin = cast<ConstantInt>(J->Low)->getSExtValue();
1699     assert((RBegin-LEnd>=1) && "Invalid case distance");
1700     double LDensity = (double)LSize / (double)((LEnd - First) + 1ULL);
1701     double RDensity = (double)RSize / (double)((Last - RBegin) + 1ULL);
1702     double Metric = Log2_64(RBegin-LEnd)*(LDensity+RDensity);
1703     // Should always split in some non-trivial place
1704     DOUT <<"=>Step\n"
1705          << "LEnd: " << LEnd << ", RBegin: " << RBegin << "\n"
1706          << "LDensity: " << LDensity << ", RDensity: " << RDensity << "\n"
1707          << "Metric: " << Metric << "\n"; 
1708     if (FMetric < Metric) {
1709       Pivot = J;
1710       FMetric = Metric;
1711       DOUT << "Current metric set to: " << FMetric << "\n";
1712     }
1713
1714     LSize += J->size();
1715     RSize -= J->size();
1716   }
1717   if (areJTsAllowed(TLI)) {
1718     // If our case is dense we *really* should handle it earlier!
1719     assert((FMetric > 0) && "Should handle dense range earlier!");
1720   } else {
1721     Pivot = CR.Range.first + Size/2;
1722   }
1723   
1724   CaseRange LHSR(CR.Range.first, Pivot);
1725   CaseRange RHSR(Pivot, CR.Range.second);
1726   Constant *C = Pivot->Low;
1727   MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
1728       
1729   // We know that we branch to the LHS if the Value being switched on is
1730   // less than the Pivot value, C.  We use this to optimize our binary 
1731   // tree a bit, by recognizing that if SV is greater than or equal to the
1732   // LHS's Case Value, and that Case Value is exactly one less than the 
1733   // Pivot's Value, then we can branch directly to the LHS's Target,
1734   // rather than creating a leaf node for it.
1735   if ((LHSR.second - LHSR.first) == 1 &&
1736       LHSR.first->High == CR.GE &&
1737       cast<ConstantInt>(C)->getSExtValue() ==
1738       (cast<ConstantInt>(CR.GE)->getSExtValue() + 1LL)) {
1739     TrueBB = LHSR.first->BB;
1740   } else {
1741     TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1742     CurMF->insert(BBI, TrueBB);
1743     WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
1744   }
1745   
1746   // Similar to the optimization above, if the Value being switched on is
1747   // known to be less than the Constant CR.LT, and the current Case Value
1748   // is CR.LT - 1, then we can branch directly to the target block for
1749   // the current Case Value, rather than emitting a RHS leaf node for it.
1750   if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
1751       cast<ConstantInt>(RHSR.first->Low)->getSExtValue() ==
1752       (cast<ConstantInt>(CR.LT)->getSExtValue() - 1LL)) {
1753     FalseBB = RHSR.first->BB;
1754   } else {
1755     FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1756     CurMF->insert(BBI, FalseBB);
1757     WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
1758   }
1759
1760   // Create a CaseBlock record representing a conditional branch to
1761   // the LHS node if the value being switched on SV is less than C. 
1762   // Otherwise, branch to LHS.
1763   CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1764
1765   if (CR.CaseBB == CurMBB)
1766     visitSwitchCase(CB);
1767   else
1768     SwitchCases.push_back(CB);
1769
1770   return true;
1771 }
1772
1773 /// handleBitTestsSwitchCase - if current case range has few destination and
1774 /// range span less, than machine word bitwidth, encode case range into series
1775 /// of masks and emit bit tests with these masks.
1776 bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR,
1777                                                     CaseRecVector& WorkList,
1778                                                     Value* SV,
1779                                                     MachineBasicBlock* Default){
1780   unsigned IntPtrBits = TLI.getPointerTy().getSizeInBits();
1781
1782   Case& FrontCase = *CR.Range.first;
1783   Case& BackCase  = *(CR.Range.second-1);
1784
1785   // Get the MachineFunction which holds the current MBB.  This is used when
1786   // inserting any additional MBBs necessary to represent the switch.
1787   MachineFunction *CurMF = CurMBB->getParent();  
1788
1789   unsigned numCmps = 0;
1790   for (CaseItr I = CR.Range.first, E = CR.Range.second;
1791        I!=E; ++I) {
1792     // Single case counts one, case range - two.
1793     if (I->Low == I->High)
1794       numCmps +=1;
1795     else
1796       numCmps +=2;
1797   }
1798     
1799   // Count unique destinations
1800   SmallSet<MachineBasicBlock*, 4> Dests;
1801   for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1802     Dests.insert(I->BB);
1803     if (Dests.size() > 3)
1804       // Don't bother the code below, if there are too much unique destinations
1805       return false;
1806   }
1807   DOUT << "Total number of unique destinations: " << Dests.size() << "\n"
1808        << "Total number of comparisons: " << numCmps << "\n";
1809   
1810   // Compute span of values.
1811   Constant* minValue = FrontCase.Low;
1812   Constant* maxValue = BackCase.High;
1813   uint64_t range = cast<ConstantInt>(maxValue)->getSExtValue() -
1814                    cast<ConstantInt>(minValue)->getSExtValue();
1815   DOUT << "Compare range: " << range << "\n"
1816        << "Low bound: " << cast<ConstantInt>(minValue)->getSExtValue() << "\n"
1817        << "High bound: " << cast<ConstantInt>(maxValue)->getSExtValue() << "\n";
1818   
1819   if (range>=IntPtrBits ||
1820       (!(Dests.size() == 1 && numCmps >= 3) &&
1821        !(Dests.size() == 2 && numCmps >= 5) &&
1822        !(Dests.size() >= 3 && numCmps >= 6)))
1823     return false;
1824   
1825   DOUT << "Emitting bit tests\n";
1826   int64_t lowBound = 0;
1827     
1828   // Optimize the case where all the case values fit in a
1829   // word without having to subtract minValue. In this case,
1830   // we can optimize away the subtraction.
1831   if (cast<ConstantInt>(minValue)->getSExtValue() >= 0 &&
1832       cast<ConstantInt>(maxValue)->getSExtValue() <  IntPtrBits) {
1833     range = cast<ConstantInt>(maxValue)->getSExtValue();
1834   } else {
1835     lowBound = cast<ConstantInt>(minValue)->getSExtValue();
1836   }
1837     
1838   CaseBitsVector CasesBits;
1839   unsigned i, count = 0;
1840
1841   for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1842     MachineBasicBlock* Dest = I->BB;
1843     for (i = 0; i < count; ++i)
1844       if (Dest == CasesBits[i].BB)
1845         break;
1846     
1847     if (i == count) {
1848       assert((count < 3) && "Too much destinations to test!");
1849       CasesBits.push_back(CaseBits(0, Dest, 0));
1850       count++;
1851     }
1852     
1853     uint64_t lo = cast<ConstantInt>(I->Low)->getSExtValue() - lowBound;
1854     uint64_t hi = cast<ConstantInt>(I->High)->getSExtValue() - lowBound;
1855     
1856     for (uint64_t j = lo; j <= hi; j++) {
1857       CasesBits[i].Mask |=  1ULL << j;
1858       CasesBits[i].Bits++;
1859     }
1860       
1861   }
1862   std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
1863   
1864   BitTestInfo BTC;
1865
1866   // Figure out which block is immediately after the current one.
1867   MachineFunction::iterator BBI = CR.CaseBB;
1868   ++BBI;
1869
1870   const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1871
1872   DOUT << "Cases:\n";
1873   for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
1874     DOUT << "Mask: " << CasesBits[i].Mask << ", Bits: " << CasesBits[i].Bits
1875          << ", BB: " << CasesBits[i].BB << "\n";
1876
1877     MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1878     CurMF->insert(BBI, CaseBB);
1879     BTC.push_back(BitTestCase(CasesBits[i].Mask,
1880                               CaseBB,
1881                               CasesBits[i].BB));
1882   }
1883   
1884   BitTestBlock BTB(lowBound, range, SV,
1885                    -1U, (CR.CaseBB == CurMBB),
1886                    CR.CaseBB, Default, BTC);
1887
1888   if (CR.CaseBB == CurMBB)
1889     visitBitTestHeader(BTB);
1890   
1891   BitTestCases.push_back(BTB);
1892
1893   return true;
1894 }
1895
1896
1897 /// Clusterify - Transform simple list of Cases into list of CaseRange's
1898 unsigned SelectionDAGLowering::Clusterify(CaseVector& Cases,
1899                                           const SwitchInst& SI) {
1900   unsigned numCmps = 0;
1901
1902   // Start with "simple" cases
1903   for (unsigned i = 1; i < SI.getNumSuccessors(); ++i) {
1904     MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1905     Cases.push_back(Case(SI.getSuccessorValue(i),
1906                          SI.getSuccessorValue(i),
1907                          SMBB));
1908   }
1909   std::sort(Cases.begin(), Cases.end(), CaseCmp());
1910
1911   // Merge case into clusters
1912   if (Cases.size()>=2)
1913     // Must recompute end() each iteration because it may be
1914     // invalidated by erase if we hold on to it
1915     for (CaseItr I=Cases.begin(), J=++(Cases.begin()); J!=Cases.end(); ) {
1916       int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue();
1917       int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue();
1918       MachineBasicBlock* nextBB = J->BB;
1919       MachineBasicBlock* currentBB = I->BB;
1920
1921       // If the two neighboring cases go to the same destination, merge them
1922       // into a single case.
1923       if ((nextValue-currentValue==1) && (currentBB == nextBB)) {
1924         I->High = J->High;
1925         J = Cases.erase(J);
1926       } else {
1927         I = J++;
1928       }
1929     }
1930
1931   for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1932     if (I->Low != I->High)
1933       // A range counts double, since it requires two compares.
1934       ++numCmps;
1935   }
1936
1937   return numCmps;
1938 }
1939
1940 void SelectionDAGLowering::visitSwitch(SwitchInst &SI) {  
1941   // Figure out which block is immediately after the current one.
1942   MachineBasicBlock *NextBlock = 0;
1943   MachineFunction::iterator BBI = CurMBB;
1944
1945   MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1946
1947   // If there is only the default destination, branch to it if it is not the
1948   // next basic block.  Otherwise, just fall through.
1949   if (SI.getNumOperands() == 2) {
1950     // Update machine-CFG edges.
1951
1952     // If this is not a fall-through branch, emit the branch.
1953     CurMBB->addSuccessor(Default);
1954     if (Default != NextBlock)
1955       DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
1956                               DAG.getBasicBlock(Default)));
1957     
1958     return;
1959   }
1960   
1961   // If there are any non-default case statements, create a vector of Cases
1962   // representing each one, and sort the vector so that we can efficiently
1963   // create a binary search tree from them.
1964   CaseVector Cases;
1965   unsigned numCmps = Clusterify(Cases, SI);
1966   DOUT << "Clusterify finished. Total clusters: " << Cases.size()
1967        << ". Total compares: " << numCmps << "\n";
1968
1969   // Get the Value to be switched on and default basic blocks, which will be
1970   // inserted into CaseBlock records, representing basic blocks in the binary
1971   // search tree.
1972   Value *SV = SI.getOperand(0);
1973
1974   // Push the initial CaseRec onto the worklist
1975   CaseRecVector WorkList;
1976   WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
1977
1978   while (!WorkList.empty()) {
1979     // Grab a record representing a case range to process off the worklist
1980     CaseRec CR = WorkList.back();
1981     WorkList.pop_back();
1982
1983     if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
1984       continue;
1985     
1986     // If the range has few cases (two or less) emit a series of specific
1987     // tests.
1988     if (handleSmallSwitchRange(CR, WorkList, SV, Default))
1989       continue;
1990     
1991     // If the switch has more than 5 blocks, and at least 40% dense, and the 
1992     // target supports indirect branches, then emit a jump table rather than 
1993     // lowering the switch to a binary tree of conditional branches.
1994     if (handleJTSwitchCase(CR, WorkList, SV, Default))
1995       continue;
1996           
1997     // Emit binary tree. We need to pick a pivot, and push left and right ranges
1998     // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
1999     handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2000   }
2001 }
2002
2003
2004 void SelectionDAGLowering::visitSub(User &I) {
2005   // -0.0 - X --> fneg
2006   const Type *Ty = I.getType();
2007   if (isa<VectorType>(Ty)) {
2008     if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2009       const VectorType *DestTy = cast<VectorType>(I.getType());
2010       const Type *ElTy = DestTy->getElementType();
2011       if (ElTy->isFloatingPoint()) {
2012         unsigned VL = DestTy->getNumElements();
2013         std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
2014         Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
2015         if (CV == CNZ) {
2016           SDValue Op2 = getValue(I.getOperand(1));
2017           setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
2018           return;
2019         }
2020       }
2021     }
2022   }
2023   if (Ty->isFloatingPoint()) {
2024     if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
2025       if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
2026         SDValue Op2 = getValue(I.getOperand(1));
2027         setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
2028         return;
2029       }
2030   }
2031
2032   visitBinary(I, Ty->isFPOrFPVector() ? ISD::FSUB : ISD::SUB);
2033 }
2034
2035 void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) {
2036   SDValue Op1 = getValue(I.getOperand(0));
2037   SDValue Op2 = getValue(I.getOperand(1));
2038   
2039   setValue(&I, DAG.getNode(OpCode, Op1.getValueType(), Op1, Op2));
2040 }
2041
2042 void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
2043   SDValue Op1 = getValue(I.getOperand(0));
2044   SDValue Op2 = getValue(I.getOperand(1));
2045   if (!isa<VectorType>(I.getType())) {
2046     if (TLI.getShiftAmountTy().bitsLT(Op2.getValueType()))
2047       Op2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Op2);
2048     else if (TLI.getShiftAmountTy().bitsGT(Op2.getValueType()))
2049       Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2);
2050   }
2051   
2052   setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2));
2053 }
2054
2055 void SelectionDAGLowering::visitICmp(User &I) {
2056   ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2057   if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2058     predicate = IC->getPredicate();
2059   else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2060     predicate = ICmpInst::Predicate(IC->getPredicate());
2061   SDValue Op1 = getValue(I.getOperand(0));
2062   SDValue Op2 = getValue(I.getOperand(1));
2063   ISD::CondCode Opcode;
2064   switch (predicate) {
2065     case ICmpInst::ICMP_EQ  : Opcode = ISD::SETEQ; break;
2066     case ICmpInst::ICMP_NE  : Opcode = ISD::SETNE; break;
2067     case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break;
2068     case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break;
2069     case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break;
2070     case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break;
2071     case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break;
2072     case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break;
2073     case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break;
2074     case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break;
2075     default:
2076       assert(!"Invalid ICmp predicate value");
2077       Opcode = ISD::SETEQ;
2078       break;
2079   }
2080   setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode));
2081 }
2082
2083 void SelectionDAGLowering::visitFCmp(User &I) {
2084   FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2085   if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2086     predicate = FC->getPredicate();
2087   else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2088     predicate = FCmpInst::Predicate(FC->getPredicate());
2089   SDValue Op1 = getValue(I.getOperand(0));
2090   SDValue Op2 = getValue(I.getOperand(1));
2091   ISD::CondCode Condition, FOC, FPC;
2092   switch (predicate) {
2093     case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
2094     case FCmpInst::FCMP_OEQ:   FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
2095     case FCmpInst::FCMP_OGT:   FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
2096     case FCmpInst::FCMP_OGE:   FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
2097     case FCmpInst::FCMP_OLT:   FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
2098     case FCmpInst::FCMP_OLE:   FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
2099     case FCmpInst::FCMP_ONE:   FOC = ISD::SETNE; FPC = ISD::SETONE; break;
2100     case FCmpInst::FCMP_ORD:   FOC = FPC = ISD::SETO;   break;
2101     case FCmpInst::FCMP_UNO:   FOC = FPC = ISD::SETUO;  break;
2102     case FCmpInst::FCMP_UEQ:   FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
2103     case FCmpInst::FCMP_UGT:   FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
2104     case FCmpInst::FCMP_UGE:   FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
2105     case FCmpInst::FCMP_ULT:   FOC = ISD::SETLT; FPC = ISD::SETULT; break;
2106     case FCmpInst::FCMP_ULE:   FOC = ISD::SETLE; FPC = ISD::SETULE; break;
2107     case FCmpInst::FCMP_UNE:   FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
2108     case FCmpInst::FCMP_TRUE:  FOC = FPC = ISD::SETTRUE; break;
2109     default:
2110       assert(!"Invalid FCmp predicate value");
2111       FOC = FPC = ISD::SETFALSE;
2112       break;
2113   }
2114   if (FiniteOnlyFPMath())
2115     Condition = FOC;
2116   else 
2117     Condition = FPC;
2118   setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Condition));
2119 }
2120
2121 void SelectionDAGLowering::visitVICmp(User &I) {
2122   ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2123   if (VICmpInst *IC = dyn_cast<VICmpInst>(&I))
2124     predicate = IC->getPredicate();
2125   else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2126     predicate = ICmpInst::Predicate(IC->getPredicate());
2127   SDValue Op1 = getValue(I.getOperand(0));
2128   SDValue Op2 = getValue(I.getOperand(1));
2129   ISD::CondCode Opcode;
2130   switch (predicate) {
2131     case ICmpInst::ICMP_EQ  : Opcode = ISD::SETEQ; break;
2132     case ICmpInst::ICMP_NE  : Opcode = ISD::SETNE; break;
2133     case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break;
2134     case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break;
2135     case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break;
2136     case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break;
2137     case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break;
2138     case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break;
2139     case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break;
2140     case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break;
2141     default:
2142       assert(!"Invalid ICmp predicate value");
2143       Opcode = ISD::SETEQ;
2144       break;
2145   }
2146   setValue(&I, DAG.getVSetCC(Op1.getValueType(), Op1, Op2, Opcode));
2147 }
2148
2149 void SelectionDAGLowering::visitVFCmp(User &I) {
2150   FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2151   if (VFCmpInst *FC = dyn_cast<VFCmpInst>(&I))
2152     predicate = FC->getPredicate();
2153   else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2154     predicate = FCmpInst::Predicate(FC->getPredicate());
2155   SDValue Op1 = getValue(I.getOperand(0));
2156   SDValue Op2 = getValue(I.getOperand(1));
2157   ISD::CondCode Condition, FOC, FPC;
2158   switch (predicate) {
2159     case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
2160     case FCmpInst::FCMP_OEQ:   FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
2161     case FCmpInst::FCMP_OGT:   FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
2162     case FCmpInst::FCMP_OGE:   FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
2163     case FCmpInst::FCMP_OLT:   FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
2164     case FCmpInst::FCMP_OLE:   FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
2165     case FCmpInst::FCMP_ONE:   FOC = ISD::SETNE; FPC = ISD::SETONE; break;
2166     case FCmpInst::FCMP_ORD:   FOC = FPC = ISD::SETO;   break;
2167     case FCmpInst::FCMP_UNO:   FOC = FPC = ISD::SETUO;  break;
2168     case FCmpInst::FCMP_UEQ:   FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
2169     case FCmpInst::FCMP_UGT:   FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
2170     case FCmpInst::FCMP_UGE:   FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
2171     case FCmpInst::FCMP_ULT:   FOC = ISD::SETLT; FPC = ISD::SETULT; break;
2172     case FCmpInst::FCMP_ULE:   FOC = ISD::SETLE; FPC = ISD::SETULE; break;
2173     case FCmpInst::FCMP_UNE:   FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
2174     case FCmpInst::FCMP_TRUE:  FOC = FPC = ISD::SETTRUE; break;
2175     default:
2176       assert(!"Invalid VFCmp predicate value");
2177       FOC = FPC = ISD::SETFALSE;
2178       break;
2179   }
2180   if (FiniteOnlyFPMath())
2181     Condition = FOC;
2182   else 
2183     Condition = FPC;
2184     
2185   MVT DestVT = TLI.getValueType(I.getType());
2186     
2187   setValue(&I, DAG.getVSetCC(DestVT, Op1, Op2, Condition));
2188 }
2189
2190 void SelectionDAGLowering::visitSelect(User &I) {
2191   SDValue Cond     = getValue(I.getOperand(0));
2192   SDValue TrueVal  = getValue(I.getOperand(1));
2193   SDValue FalseVal = getValue(I.getOperand(2));
2194   setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond,
2195                            TrueVal, FalseVal));
2196 }
2197
2198
2199 void SelectionDAGLowering::visitTrunc(User &I) {
2200   // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2201   SDValue N = getValue(I.getOperand(0));
2202   MVT DestVT = TLI.getValueType(I.getType());
2203   setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
2204 }
2205
2206 void SelectionDAGLowering::visitZExt(User &I) {
2207   // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2208   // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2209   SDValue N = getValue(I.getOperand(0));
2210   MVT DestVT = TLI.getValueType(I.getType());
2211   setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
2212 }
2213
2214 void SelectionDAGLowering::visitSExt(User &I) {
2215   // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2216   // SExt also can't be a cast to bool for same reason. So, nothing much to do
2217   SDValue N = getValue(I.getOperand(0));
2218   MVT DestVT = TLI.getValueType(I.getType());
2219   setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N));
2220 }
2221
2222 void SelectionDAGLowering::visitFPTrunc(User &I) {
2223   // FPTrunc is never a no-op cast, no need to check
2224   SDValue N = getValue(I.getOperand(0));
2225   MVT DestVT = TLI.getValueType(I.getType());
2226   setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N, DAG.getIntPtrConstant(0)));
2227 }
2228
2229 void SelectionDAGLowering::visitFPExt(User &I){ 
2230   // FPTrunc is never a no-op cast, no need to check
2231   SDValue N = getValue(I.getOperand(0));
2232   MVT DestVT = TLI.getValueType(I.getType());
2233   setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N));
2234 }
2235
2236 void SelectionDAGLowering::visitFPToUI(User &I) { 
2237   // FPToUI is never a no-op cast, no need to check
2238   SDValue N = getValue(I.getOperand(0));
2239   MVT DestVT = TLI.getValueType(I.getType());
2240   setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N));
2241 }
2242
2243 void SelectionDAGLowering::visitFPToSI(User &I) {
2244   // FPToSI is never a no-op cast, no need to check
2245   SDValue N = getValue(I.getOperand(0));
2246   MVT DestVT = TLI.getValueType(I.getType());
2247   setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N));
2248 }
2249
2250 void SelectionDAGLowering::visitUIToFP(User &I) { 
2251   // UIToFP is never a no-op cast, no need to check
2252   SDValue N = getValue(I.getOperand(0));
2253   MVT DestVT = TLI.getValueType(I.getType());
2254   setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N));
2255 }
2256
2257 void SelectionDAGLowering::visitSIToFP(User &I){ 
2258   // UIToFP is never a no-op cast, no need to check
2259   SDValue N = getValue(I.getOperand(0));
2260   MVT DestVT = TLI.getValueType(I.getType());
2261   setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N));
2262 }
2263
2264 void SelectionDAGLowering::visitPtrToInt(User &I) {
2265   // What to do depends on the size of the integer and the size of the pointer.
2266   // We can either truncate, zero extend, or no-op, accordingly.
2267   SDValue N = getValue(I.getOperand(0));
2268   MVT SrcVT = N.getValueType();
2269   MVT DestVT = TLI.getValueType(I.getType());
2270   SDValue Result;
2271   if (DestVT.bitsLT(SrcVT))
2272     Result = DAG.getNode(ISD::TRUNCATE, DestVT, N);
2273   else 
2274     // Note: ZERO_EXTEND can handle cases where the sizes are equal too
2275     Result = DAG.getNode(ISD::ZERO_EXTEND, DestVT, N);
2276   setValue(&I, Result);
2277 }
2278
2279 void SelectionDAGLowering::visitIntToPtr(User &I) {
2280   // What to do depends on the size of the integer and the size of the pointer.
2281   // We can either truncate, zero extend, or no-op, accordingly.
2282   SDValue N = getValue(I.getOperand(0));
2283   MVT SrcVT = N.getValueType();
2284   MVT DestVT = TLI.getValueType(I.getType());
2285   if (DestVT.bitsLT(SrcVT))
2286     setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
2287   else 
2288     // Note: ZERO_EXTEND can handle cases where the sizes are equal too
2289     setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
2290 }
2291
2292 void SelectionDAGLowering::visitBitCast(User &I) { 
2293   SDValue N = getValue(I.getOperand(0));
2294   MVT DestVT = TLI.getValueType(I.getType());
2295
2296   // BitCast assures us that source and destination are the same size so this 
2297   // is either a BIT_CONVERT or a no-op.
2298   if (DestVT != N.getValueType())
2299     setValue(&I, DAG.getNode(ISD::BIT_CONVERT, DestVT, N)); // convert types
2300   else
2301     setValue(&I, N); // noop cast.
2302 }
2303
2304 void SelectionDAGLowering::visitInsertElement(User &I) {
2305   SDValue InVec = getValue(I.getOperand(0));
2306   SDValue InVal = getValue(I.getOperand(1));
2307   SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
2308                                 getValue(I.getOperand(2)));
2309
2310   setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT,
2311                            TLI.getValueType(I.getType()),
2312                            InVec, InVal, InIdx));
2313 }
2314
2315 void SelectionDAGLowering::visitExtractElement(User &I) {
2316   SDValue InVec = getValue(I.getOperand(0));
2317   SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
2318                                 getValue(I.getOperand(1)));
2319   setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
2320                            TLI.getValueType(I.getType()), InVec, InIdx));
2321 }
2322
2323 void SelectionDAGLowering::visitShuffleVector(User &I) {
2324   SDValue V1   = getValue(I.getOperand(0));
2325   SDValue V2   = getValue(I.getOperand(1));
2326   SDValue Mask = getValue(I.getOperand(2));
2327
2328   setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE,
2329                            TLI.getValueType(I.getType()),
2330                            V1, V2, Mask));
2331 }
2332
2333 void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) {
2334   const Value *Op0 = I.getOperand(0);
2335   const Value *Op1 = I.getOperand(1);
2336   const Type *AggTy = I.getType();
2337   const Type *ValTy = Op1->getType();
2338   bool IntoUndef = isa<UndefValue>(Op0);
2339   bool FromUndef = isa<UndefValue>(Op1);
2340
2341   unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2342                                             I.idx_begin(), I.idx_end());
2343
2344   SmallVector<MVT, 4> AggValueVTs;
2345   ComputeValueVTs(TLI, AggTy, AggValueVTs);
2346   SmallVector<MVT, 4> ValValueVTs;
2347   ComputeValueVTs(TLI, ValTy, ValValueVTs);
2348
2349   unsigned NumAggValues = AggValueVTs.size();
2350   unsigned NumValValues = ValValueVTs.size();
2351   SmallVector<SDValue, 4> Values(NumAggValues);
2352
2353   SDValue Agg = getValue(Op0);
2354   SDValue Val = getValue(Op1);
2355   unsigned i = 0;
2356   // Copy the beginning value(s) from the original aggregate.
2357   for (; i != LinearIndex; ++i)
2358     Values[i] = IntoUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) :
2359                 SDValue(Agg.getNode(), Agg.getResNo() + i);
2360   // Copy values from the inserted value(s).
2361   for (; i != LinearIndex + NumValValues; ++i)
2362     Values[i] = FromUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) :
2363                 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2364   // Copy remaining value(s) from the original aggregate.
2365   for (; i != NumAggValues; ++i)
2366     Values[i] = IntoUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) :
2367                 SDValue(Agg.getNode(), Agg.getResNo() + i);
2368
2369   setValue(&I, DAG.getMergeValues(DAG.getVTList(&AggValueVTs[0], NumAggValues),
2370                                   &Values[0], NumAggValues));
2371 }
2372
2373 void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) {
2374   const Value *Op0 = I.getOperand(0);
2375   const Type *AggTy = Op0->getType();
2376   const Type *ValTy = I.getType();
2377   bool OutOfUndef = isa<UndefValue>(Op0);
2378
2379   unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2380                                             I.idx_begin(), I.idx_end());
2381
2382   SmallVector<MVT, 4> ValValueVTs;
2383   ComputeValueVTs(TLI, ValTy, ValValueVTs);
2384
2385   unsigned NumValValues = ValValueVTs.size();
2386   SmallVector<SDValue, 4> Values(NumValValues);
2387
2388   SDValue Agg = getValue(Op0);
2389   // Copy out the selected value(s).
2390   for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2391     Values[i - LinearIndex] =
2392       OutOfUndef ? DAG.getNode(ISD::UNDEF, Agg.getNode()->getValueType(Agg.getResNo() + i)) :
2393                    SDValue(Agg.getNode(), Agg.getResNo() + i);
2394
2395   setValue(&I, DAG.getMergeValues(DAG.getVTList(&ValValueVTs[0], NumValValues),
2396                                   &Values[0], NumValValues));
2397 }
2398
2399
2400 void SelectionDAGLowering::visitGetElementPtr(User &I) {
2401   SDValue N = getValue(I.getOperand(0));
2402   const Type *Ty = I.getOperand(0)->getType();
2403
2404   for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2405        OI != E; ++OI) {
2406     Value *Idx = *OI;
2407     if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2408       unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2409       if (Field) {
2410         // N = N + Offset
2411         uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
2412         N = DAG.getNode(ISD::ADD, N.getValueType(), N,
2413                         DAG.getIntPtrConstant(Offset));
2414       }
2415       Ty = StTy->getElementType(Field);
2416     } else {
2417       Ty = cast<SequentialType>(Ty)->getElementType();
2418
2419       // If this is a constant subscript, handle it quickly.
2420       if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2421         if (CI->getZExtValue() == 0) continue;
2422         uint64_t Offs = 
2423             TD->getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
2424         N = DAG.getNode(ISD::ADD, N.getValueType(), N,
2425                         DAG.getIntPtrConstant(Offs));
2426         continue;
2427       }
2428       
2429       // N = N + Idx * ElementSize;
2430       uint64_t ElementSize = TD->getABITypeSize(Ty);
2431       SDValue IdxN = getValue(Idx);
2432
2433       // If the index is smaller or larger than intptr_t, truncate or extend
2434       // it.
2435       if (IdxN.getValueType().bitsLT(N.getValueType()))
2436         IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN);
2437       else if (IdxN.getValueType().bitsGT(N.getValueType()))
2438         IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN);
2439
2440       // If this is a multiply by a power of two, turn it into a shl
2441       // immediately.  This is a very common case.
2442       if (ElementSize != 1) {
2443         if (isPowerOf2_64(ElementSize)) {
2444           unsigned Amt = Log2_64(ElementSize);
2445           IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN,
2446                              DAG.getConstant(Amt, TLI.getShiftAmountTy()));
2447         } else {
2448           SDValue Scale = DAG.getIntPtrConstant(ElementSize);
2449           IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale);
2450         }
2451       }
2452
2453       N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
2454     }
2455   }
2456   setValue(&I, N);
2457 }
2458
2459 void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
2460   // If this is a fixed sized alloca in the entry block of the function,
2461   // allocate it statically on the stack.
2462   if (FuncInfo.StaticAllocaMap.count(&I))
2463     return;   // getValue will auto-populate this.
2464
2465   const Type *Ty = I.getAllocatedType();
2466   uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
2467   unsigned Align =
2468     std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2469              I.getAlignment());
2470
2471   SDValue AllocSize = getValue(I.getArraySize());
2472   MVT IntPtr = TLI.getPointerTy();
2473   if (IntPtr.bitsLT(AllocSize.getValueType()))
2474     AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize);
2475   else if (IntPtr.bitsGT(AllocSize.getValueType()))
2476     AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize);
2477
2478   AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize,
2479                           DAG.getIntPtrConstant(TySize));
2480
2481   // Handle alignment.  If the requested alignment is less than or equal to
2482   // the stack alignment, ignore it.  If the size is greater than or equal to
2483   // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2484   unsigned StackAlign =
2485     TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2486   if (Align <= StackAlign)
2487     Align = 0;
2488
2489   // Round the size of the allocation up to the stack alignment size
2490   // by add SA-1 to the size.
2491   AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize,
2492                           DAG.getIntPtrConstant(StackAlign-1));
2493   // Mask out the low bits for alignment purposes.
2494   AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize,
2495                           DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2496
2497   SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
2498   const MVT *VTs = DAG.getNodeValueTypes(AllocSize.getValueType(),
2499                                                     MVT::Other);
2500   SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, 2, Ops, 3);
2501   setValue(&I, DSA);
2502   DAG.setRoot(DSA.getValue(1));
2503
2504   // Inform the Frame Information that we have just allocated a variable-sized
2505   // object.
2506   CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject();
2507 }
2508
2509 void SelectionDAGLowering::visitLoad(LoadInst &I) {
2510   const Value *SV = I.getOperand(0);
2511   SDValue Ptr = getValue(SV);
2512
2513   const Type *Ty = I.getType();
2514   bool isVolatile = I.isVolatile();
2515   unsigned Alignment = I.getAlignment();
2516
2517   SmallVector<MVT, 4> ValueVTs;
2518   SmallVector<uint64_t, 4> Offsets;
2519   ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2520   unsigned NumValues = ValueVTs.size();
2521   if (NumValues == 0)
2522     return;
2523
2524   SDValue Root;
2525   bool ConstantMemory = false;
2526   if (I.isVolatile())
2527     // Serialize volatile loads with other side effects.
2528     Root = getRoot();
2529   else if (AA->pointsToConstantMemory(SV)) {
2530     // Do not serialize (non-volatile) loads of constant memory with anything.
2531     Root = DAG.getEntryNode();
2532     ConstantMemory = true;
2533   } else {
2534     // Do not serialize non-volatile loads against each other.
2535     Root = DAG.getRoot();
2536   }
2537
2538   SmallVector<SDValue, 4> Values(NumValues);
2539   SmallVector<SDValue, 4> Chains(NumValues);
2540   MVT PtrVT = Ptr.getValueType();
2541   for (unsigned i = 0; i != NumValues; ++i) {
2542     SDValue L = DAG.getLoad(ValueVTs[i], Root,
2543                               DAG.getNode(ISD::ADD, PtrVT, Ptr,
2544                                           DAG.getConstant(Offsets[i], PtrVT)),
2545                               SV, Offsets[i],
2546                               isVolatile, Alignment);
2547     Values[i] = L;
2548     Chains[i] = L.getValue(1);
2549   }
2550   
2551   if (!ConstantMemory) {
2552     SDValue Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2553                                   &Chains[0], NumValues);
2554     if (isVolatile)
2555       DAG.setRoot(Chain);
2556     else
2557       PendingLoads.push_back(Chain);
2558   }
2559
2560   setValue(&I, DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], NumValues),
2561                                   &Values[0], NumValues));
2562 }
2563
2564
2565 void SelectionDAGLowering::visitStore(StoreInst &I) {
2566   Value *SrcV = I.getOperand(0);
2567   Value *PtrV = I.getOperand(1);
2568
2569   SmallVector<MVT, 4> ValueVTs;
2570   SmallVector<uint64_t, 4> Offsets;
2571   ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2572   unsigned NumValues = ValueVTs.size();
2573   if (NumValues == 0)
2574     return;
2575
2576   // Get the lowered operands. Note that we do this after
2577   // checking if NumResults is zero, because with zero results
2578   // the operands won't have values in the map.
2579   SDValue Src = getValue(SrcV);
2580   SDValue Ptr = getValue(PtrV);
2581
2582   SDValue Root = getRoot();
2583   SmallVector<SDValue, 4> Chains(NumValues);
2584   MVT PtrVT = Ptr.getValueType();
2585   bool isVolatile = I.isVolatile();
2586   unsigned Alignment = I.getAlignment();
2587   for (unsigned i = 0; i != NumValues; ++i)
2588     Chains[i] = DAG.getStore(Root, SDValue(Src.getNode(), Src.getResNo() + i),
2589                              DAG.getNode(ISD::ADD, PtrVT, Ptr,
2590                                          DAG.getConstant(Offsets[i], PtrVT)),
2591                              PtrV, Offsets[i],
2592                              isVolatile, Alignment);
2593
2594   DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, &Chains[0], NumValues));
2595 }
2596
2597 /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2598 /// node.
2599 void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, 
2600                                                 unsigned Intrinsic) {
2601   bool HasChain = !I.doesNotAccessMemory();
2602   bool OnlyLoad = HasChain && I.onlyReadsMemory();
2603
2604   // Build the operand list.
2605   SmallVector<SDValue, 8> Ops;
2606   if (HasChain) {  // If this intrinsic has side-effects, chainify it.
2607     if (OnlyLoad) {
2608       // We don't need to serialize loads against other loads.
2609       Ops.push_back(DAG.getRoot());
2610     } else { 
2611       Ops.push_back(getRoot());
2612     }
2613   }
2614   
2615   // Add the intrinsic ID as an integer operand.
2616   Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
2617
2618   // Add all operands of the call to the operand list.
2619   for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2620     SDValue Op = getValue(I.getOperand(i));
2621     assert(TLI.isTypeLegal(Op.getValueType()) &&
2622            "Intrinsic uses a non-legal type?");
2623     Ops.push_back(Op);
2624   }
2625
2626   std::vector<MVT> VTs;
2627   if (I.getType() != Type::VoidTy) {
2628     MVT VT = TLI.getValueType(I.getType());
2629     if (VT.isVector()) {
2630       const VectorType *DestTy = cast<VectorType>(I.getType());
2631       MVT EltVT = TLI.getValueType(DestTy->getElementType());
2632       
2633       VT = MVT::getVectorVT(EltVT, DestTy->getNumElements());
2634       assert(VT != MVT::Other && "Intrinsic uses a non-legal type?");
2635     }
2636     
2637     assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?");
2638     VTs.push_back(VT);
2639   }
2640   if (HasChain)
2641     VTs.push_back(MVT::Other);
2642
2643   const MVT *VTList = DAG.getNodeValueTypes(VTs);
2644
2645   // Create the node.
2646   SDValue Result;
2647   if (!HasChain)
2648     Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VTList, VTs.size(),
2649                          &Ops[0], Ops.size());
2650   else if (I.getType() != Type::VoidTy)
2651     Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, VTList, VTs.size(),
2652                          &Ops[0], Ops.size());
2653   else
2654     Result = DAG.getNode(ISD::INTRINSIC_VOID, VTList, VTs.size(),
2655                          &Ops[0], Ops.size());
2656
2657   if (HasChain) {
2658     SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2659     if (OnlyLoad)
2660       PendingLoads.push_back(Chain);
2661     else
2662       DAG.setRoot(Chain);
2663   }
2664   if (I.getType() != Type::VoidTy) {
2665     if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
2666       MVT VT = TLI.getValueType(PTy);
2667       Result = DAG.getNode(ISD::BIT_CONVERT, VT, Result);
2668     } 
2669     setValue(&I, Result);
2670   }
2671 }
2672
2673 /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
2674 static GlobalVariable *ExtractTypeInfo(Value *V) {
2675   V = V->stripPointerCasts();
2676   GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
2677   assert ((GV || isa<ConstantPointerNull>(V)) &&
2678           "TypeInfo must be a global variable or NULL");
2679   return GV;
2680 }
2681
2682 namespace llvm {
2683
2684 /// AddCatchInfo - Extract the personality and type infos from an eh.selector
2685 /// call, and add them to the specified machine basic block.
2686 void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI,
2687                   MachineBasicBlock *MBB) {
2688   // Inform the MachineModuleInfo of the personality for this landing pad.
2689   ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2));
2690   assert(CE->getOpcode() == Instruction::BitCast &&
2691          isa<Function>(CE->getOperand(0)) &&
2692          "Personality should be a function");
2693   MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
2694
2695   // Gather all the type infos for this landing pad and pass them along to
2696   // MachineModuleInfo.
2697   std::vector<GlobalVariable *> TyInfo;
2698   unsigned N = I.getNumOperands();
2699
2700   for (unsigned i = N - 1; i > 2; --i) {
2701     if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) {
2702       unsigned FilterLength = CI->getZExtValue();
2703       unsigned FirstCatch = i + FilterLength + !FilterLength;
2704       assert (FirstCatch <= N && "Invalid filter length");
2705
2706       if (FirstCatch < N) {
2707         TyInfo.reserve(N - FirstCatch);
2708         for (unsigned j = FirstCatch; j < N; ++j)
2709           TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2710         MMI->addCatchTypeInfo(MBB, TyInfo);
2711         TyInfo.clear();
2712       }
2713
2714       if (!FilterLength) {
2715         // Cleanup.
2716         MMI->addCleanup(MBB);
2717       } else {
2718         // Filter.
2719         TyInfo.reserve(FilterLength - 1);
2720         for (unsigned j = i + 1; j < FirstCatch; ++j)
2721           TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2722         MMI->addFilterTypeInfo(MBB, TyInfo);
2723         TyInfo.clear();
2724       }
2725
2726       N = i;
2727     }
2728   }
2729
2730   if (N > 3) {
2731     TyInfo.reserve(N - 3);
2732     for (unsigned j = 3; j < N; ++j)
2733       TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2734     MMI->addCatchTypeInfo(MBB, TyInfo);
2735   }
2736 }
2737
2738 }
2739
2740 /// GetSignificand - Get the significand and build it into a floating-point
2741 /// number with exponent of 1:
2742 ///
2743 ///   Op = (Op & 0x007fffff) | 0x3f800000;
2744 ///
2745 /// where Op is the hexidecimal representation of floating point value.
2746 static SDValue
2747 GetSignificand(SelectionDAG &DAG, SDValue Op) {
2748     SDValue t1 = DAG.getNode(ISD::AND, MVT::i32, Op,
2749                              DAG.getConstant(0x007fffff, MVT::i32));
2750     SDValue t2 = DAG.getNode(ISD::OR, MVT::i32, t1,
2751                              DAG.getConstant(0x3f800000, MVT::i32));
2752     return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t2);
2753 }
2754
2755 /// GetExponent - Get the exponent:
2756 ///
2757 ///   (float)((Op1 >> 23) - 127);
2758 ///
2759 /// where Op is the hexidecimal representation of floating point value.
2760 static SDValue
2761 GetExponent(SelectionDAG &DAG, SDValue Op) {
2762     SDValue t1 = DAG.getNode(ISD::SRL, MVT::i32, Op,
2763                              DAG.getConstant(23, MVT::i32));
2764     SDValue t2 = DAG.getNode(ISD::SUB, MVT::i32, t1,
2765                              DAG.getConstant(127, MVT::i32));
2766     return DAG.getNode(ISD::UINT_TO_FP, MVT::f32, t2);
2767 }
2768
2769 /// getF32Constant - Get 32-bit floating point constant.
2770 static SDValue
2771 getF32Constant(SelectionDAG &DAG, unsigned Flt) {
2772   return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
2773 }
2774
2775 /// Inlined utility function to implement binary input atomic intrinsics for 
2776 /// visitIntrinsicCall: I is a call instruction
2777 ///                     Op is the associated NodeType for I
2778 const char *
2779 SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
2780   SDValue Root = getRoot();   
2781   SDValue L = DAG.getAtomic(Op, Root, 
2782                               getValue(I.getOperand(1)), 
2783                               getValue(I.getOperand(2)),
2784                               I.getOperand(1));
2785   setValue(&I, L);
2786   DAG.setRoot(L.getValue(1));
2787   return 0;
2788 }
2789
2790 /// visitExp - Lower an exp intrinsic. Handles the special sequences for
2791 /// limited-precision mode.
2792 void
2793 SelectionDAGLowering::visitExp(CallInst &I) {
2794   SDValue result;
2795
2796   if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
2797       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2798     SDValue Op = getValue(I.getOperand(1));
2799
2800     // Put the exponent in the right bit position for later addition to the
2801     // final result:
2802     //
2803     //   #define LOG2OFe 1.4426950f
2804     //   IntegerPartOfX = ((int32_t)(X * LOG2OFe));
2805     SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, Op,
2806                              getF32Constant(DAG, 0x3fb8aa3b));
2807     SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, t0);
2808
2809     //   FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
2810     SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX);
2811     SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, t0, t1);
2812
2813     //   IntegerPartOfX <<= 23;
2814     IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX,
2815                                  DAG.getConstant(23, MVT::i32));
2816
2817     if (LimitFloatPrecision <= 6) {
2818       // For floating-point precision of 6:
2819       //
2820       //   TwoToFractionalPartOfX =
2821       //     0.997535578f +
2822       //       (0.735607626f + 0.252464424f * x) * x;
2823       //
2824       // error 0.0144103317, which is 6 bits
2825       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
2826                                getF32Constant(DAG, 0x3e814304));
2827       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
2828                                getF32Constant(DAG, 0x3f3c50c8));
2829       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2830       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
2831                                getF32Constant(DAG, 0x3f7f5e7e));
2832       SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5);
2833
2834       // Add the exponent into the result in integer domain.
2835       SDValue t6 = DAG.getNode(ISD::ADD, MVT::i32,
2836                                TwoToFracPartOfX, IntegerPartOfX);
2837
2838       result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t6);
2839     } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2840       // For floating-point precision of 12:
2841       //
2842       //   TwoToFractionalPartOfX =
2843       //     0.999892986f +
2844       //       (0.696457318f +
2845       //         (0.224338339f + 0.792043434e-1f * x) * x) * x;
2846       //
2847       // 0.000107046256 error, which is 13 to 14 bits
2848       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
2849                                getF32Constant(DAG, 0x3da235e3));
2850       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
2851                                getF32Constant(DAG, 0x3e65b8f3));
2852       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2853       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
2854                                getF32Constant(DAG, 0x3f324b07));
2855       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2856       SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
2857                                getF32Constant(DAG, 0x3f7ff8fd));
2858       SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7);
2859
2860       // Add the exponent into the result in integer domain.
2861       SDValue t8 = DAG.getNode(ISD::ADD, MVT::i32,
2862                                TwoToFracPartOfX, IntegerPartOfX);
2863
2864       result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t8);
2865     } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2866       // For floating-point precision of 18:
2867       //
2868       //   TwoToFractionalPartOfX =
2869       //     0.999999982f +
2870       //       (0.693148872f +
2871       //         (0.240227044f +
2872       //           (0.554906021e-1f +
2873       //             (0.961591928e-2f +
2874       //               (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
2875       //
2876       // error 2.47208000*10^(-7), which is better than 18 bits
2877       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
2878                                getF32Constant(DAG, 0x3924b03e));
2879       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
2880                                getF32Constant(DAG, 0x3ab24b87));
2881       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2882       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
2883                                getF32Constant(DAG, 0x3c1d8c17));
2884       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2885       SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
2886                                getF32Constant(DAG, 0x3d634a1d));
2887       SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
2888       SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
2889                                getF32Constant(DAG, 0x3e75fe14));
2890       SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
2891       SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10,
2892                                 getF32Constant(DAG, 0x3f317234));
2893       SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X);
2894       SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12,
2895                                 getF32Constant(DAG, 0x3f800000));
2896       SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13);
2897
2898       // Add the exponent into the result in integer domain.
2899       SDValue t14 = DAG.getNode(ISD::ADD, MVT::i32,
2900                                 TwoToFracPartOfX, IntegerPartOfX);
2901
2902       result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t14);
2903     }
2904   } else {
2905     // No special expansion.
2906     result = DAG.getNode(ISD::FEXP,
2907                          getValue(I.getOperand(1)).getValueType(),
2908                          getValue(I.getOperand(1)));
2909   }
2910
2911   setValue(&I, result);
2912 }
2913
2914 /// visitLog - Lower a log intrinsic. Handles the special sequences for
2915 /// limited-precision mode.
2916 void
2917 SelectionDAGLowering::visitLog(CallInst &I) {
2918   SDValue result;
2919
2920   if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
2921       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2922     SDValue Op = getValue(I.getOperand(1));
2923     SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
2924
2925     // Scale the exponent by log(2) [0.69314718f].
2926     SDValue Exp = GetExponent(DAG, Op1);
2927     SDValue LogOfExponent = DAG.getNode(ISD::FMUL, MVT::f32, Exp,
2928                                         getF32Constant(DAG, 0x3f317218));
2929
2930     // Get the significand and build it into a floating-point number with
2931     // exponent of 1.
2932     SDValue X = GetSignificand(DAG, Op1);
2933
2934     if (LimitFloatPrecision <= 6) {
2935       // For floating-point precision of 6:
2936       //
2937       //   LogofMantissa =
2938       //     -1.1609546f +
2939       //       (1.4034025f - 0.23903021f * x) * x;
2940       // 
2941       // error 0.0034276066, which is better than 8 bits
2942       SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
2943                                getF32Constant(DAG, 0xbe74c456));
2944       SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
2945                                getF32Constant(DAG, 0x3fb3a2b1));
2946       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
2947       SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2,
2948                                           getF32Constant(DAG, 0x3f949a29));
2949
2950       result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa);
2951     } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2952       // For floating-point precision of 12:
2953       //
2954       //   LogOfMantissa =
2955       //     -1.7417939f +
2956       //       (2.8212026f +
2957       //         (-1.4699568f +
2958       //           (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
2959       //
2960       // error 0.000061011436, which is 14 bits
2961       SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
2962                                getF32Constant(DAG, 0xbd67b6d6));
2963       SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
2964                                getF32Constant(DAG, 0x3ee4f4b8));
2965       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
2966       SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
2967                                getF32Constant(DAG, 0x3fbc278b));
2968       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2969       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
2970                                getF32Constant(DAG, 0x40348e95));
2971       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2972       SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t6,
2973                                           getF32Constant(DAG, 0x3fdef31a));
2974
2975       result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa);
2976     } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2977       // For floating-point precision of 18:
2978       //
2979       //   LogOfMantissa =
2980       //     -2.1072184f +
2981       //       (4.2372794f +
2982       //         (-3.7029485f +
2983       //           (2.2781945f +
2984       //             (-0.87823314f +
2985       //               (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
2986       //
2987       // error 0.0000023660568, which is better than 18 bits
2988       SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
2989                                getF32Constant(DAG, 0xbc91e5ac));
2990       SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
2991                                getF32Constant(DAG, 0x3e4350aa));
2992       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
2993       SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
2994                                getF32Constant(DAG, 0x3f60d3e3));
2995       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2996       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
2997                                getF32Constant(DAG, 0x4011cdf0));
2998       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2999       SDValue t7 = DAG.getNode(ISD::FSUB, MVT::f32, t6,
3000                                getF32Constant(DAG, 0x406cfd1c));
3001       SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3002       SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
3003                                getF32Constant(DAG, 0x408797cb));
3004       SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
3005       SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t10,
3006                                           getF32Constant(DAG, 0x4006dcab));
3007
3008       result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa);
3009     }
3010   } else {
3011     // No special expansion.
3012     result = DAG.getNode(ISD::FLOG,
3013                          getValue(I.getOperand(1)).getValueType(),
3014                          getValue(I.getOperand(1)));
3015   }
3016
3017   setValue(&I, result);
3018 }
3019
3020 /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3021 /// limited-precision mode.
3022 void
3023 SelectionDAGLowering::visitLog2(CallInst &I) {
3024   SDValue result;
3025
3026   if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
3027       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3028     SDValue Op = getValue(I.getOperand(1));
3029     SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
3030
3031     // Get the exponent.
3032     SDValue LogOfExponent = GetExponent(DAG, Op1);
3033
3034     // Get the significand and build it into a floating-point number with
3035     // exponent of 1.
3036     SDValue X = GetSignificand(DAG, Op1);
3037     
3038     // Different possible minimax approximations of significand in
3039     // floating-point for various degrees of accuracy over [1,2].
3040     if (LimitFloatPrecision <= 6) {
3041       // For floating-point precision of 6:
3042       //
3043       //   Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3044       //
3045       // error 0.0049451742, which is more than 7 bits
3046       SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3047                                getF32Constant(DAG, 0xbeb08fe0));
3048       SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
3049                                getF32Constant(DAG, 0x40019463));
3050       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3051       SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2,
3052                                            getF32Constant(DAG, 0x3fd6633d));
3053
3054       result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa);
3055     } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3056       // For floating-point precision of 12:
3057       //
3058       //   Log2ofMantissa =
3059       //     -2.51285454f +
3060       //       (4.07009056f +
3061       //         (-2.12067489f +
3062       //           (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
3063       //   
3064       // error 0.0000876136000, which is better than 13 bits
3065       SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3066                                getF32Constant(DAG, 0xbda7262e));
3067       SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
3068                                getF32Constant(DAG, 0x3f25280b));
3069       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3070       SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
3071                                getF32Constant(DAG, 0x4007b923));
3072       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3073       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
3074                                getF32Constant(DAG, 0x40823e2f));
3075       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3076       SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t6,
3077                                            getF32Constant(DAG, 0x4020d29c));
3078
3079       result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa);
3080     } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3081       // For floating-point precision of 18:
3082       //
3083       //   Log2ofMantissa =
3084       //     -3.0400495f +
3085       //       (6.1129976f +
3086       //         (-5.3420409f +
3087       //           (3.2865683f +
3088       //             (-1.2669343f +
3089       //               (0.27515199f -
3090       //                 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3091       //
3092       // error 0.0000018516, which is better than 18 bits
3093       SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3094                                getF32Constant(DAG, 0xbcd2769e));
3095       SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
3096                                getF32Constant(DAG, 0x3e8ce0b9));
3097       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3098       SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
3099                                getF32Constant(DAG, 0x3fa22ae7));
3100       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3101       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
3102                                getF32Constant(DAG, 0x40525723));
3103       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3104       SDValue t7 = DAG.getNode(ISD::FSUB, MVT::f32, t6,
3105                                getF32Constant(DAG, 0x40aaf200));
3106       SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3107       SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
3108                                getF32Constant(DAG, 0x40c39dad));
3109       SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
3110       SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t10,
3111                                            getF32Constant(DAG, 0x4042902c));
3112
3113       result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa);
3114     }
3115   } else {
3116     // No special expansion.
3117     result = DAG.getNode(ISD::FLOG2,
3118                          getValue(I.getOperand(1)).getValueType(),
3119                          getValue(I.getOperand(1)));
3120   }
3121
3122   setValue(&I, result);
3123 }
3124
3125 /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3126 /// limited-precision mode.
3127 void
3128 SelectionDAGLowering::visitLog10(CallInst &I) {
3129   SDValue result;
3130   if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
3131       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3132     SDValue Op = getValue(I.getOperand(1));
3133     SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
3134
3135     // Scale the exponent by log10(2) [0.30102999f].
3136     SDValue Exp = GetExponent(DAG, Op1);
3137     SDValue LogOfExponent = DAG.getNode(ISD::FMUL, MVT::f32, Exp,
3138                                         getF32Constant(DAG, 0x3e9a209a));
3139
3140     // Get the significand and build it into a floating-point number with
3141     // exponent of 1.
3142     SDValue X = GetSignificand(DAG, Op1);
3143
3144     if (LimitFloatPrecision <= 6) {
3145       // For floating-point precision of 6:
3146       // 
3147       //   Log10ofMantissa =
3148       //     -0.50419619f +
3149       //       (0.60948995f - 0.10380950f * x) * x;
3150       //
3151       // error 0.0014886165, which is 6 bits
3152       SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3153                                getF32Constant(DAG, 0xbdd49a13));
3154       SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
3155                                getF32Constant(DAG, 0x3f1c0789));
3156       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3157       SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2,
3158                                             getF32Constant(DAG, 0x3f011300));
3159
3160       result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa);
3161     } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3162       // For floating-point precision of 12:
3163       //
3164       //   Log10ofMantissa =
3165       //     -0.64831180f +
3166       //       (0.91751397f +
3167       //         (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3168       //
3169       // error 0.00019228036, which is better than 12 bits
3170       SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3171                                getF32Constant(DAG, 0x3d431f31));
3172       SDValue t1 = DAG.getNode(ISD::FSUB, MVT::f32, t0,
3173                                getF32Constant(DAG, 0x3ea21fb2));
3174       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3175       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
3176                                getF32Constant(DAG, 0x3f6ae232));
3177       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3178       SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t4,
3179                                             getF32Constant(DAG, 0x3f25f7c3));
3180
3181       result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa);
3182     } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3183       // For floating-point precision of 18:
3184       //
3185       //   Log10ofMantissa =
3186       //     -0.84299375f +
3187       //       (1.5327582f +
3188       //         (-1.0688956f +
3189       //           (0.49102474f +
3190       //             (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3191       //
3192       // error 0.0000037995730, which is better than 18 bits
3193       SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3194                                getF32Constant(DAG, 0x3c5d51ce));
3195       SDValue t1 = DAG.getNode(ISD::FSUB, MVT::f32, t0,
3196                                getF32Constant(DAG, 0x3e00685a));
3197       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3198       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
3199                                getF32Constant(DAG, 0x3efb6798));
3200       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3201       SDValue t5 = DAG.getNode(ISD::FSUB, MVT::f32, t4,
3202                                getF32Constant(DAG, 0x3f88d192));
3203       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3204       SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
3205                                getF32Constant(DAG, 0x3fc4316c));
3206       SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3207       SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t8,
3208                                             getF32Constant(DAG, 0x3f57ce70));
3209
3210       result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa);
3211     }
3212   } else {
3213     // No special expansion.
3214     result = DAG.getNode(ISD::FLOG10,
3215                          getValue(I.getOperand(1)).getValueType(),
3216                          getValue(I.getOperand(1)));
3217   }
3218
3219   setValue(&I, result);
3220 }
3221
3222 /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3223 /// limited-precision mode.
3224 void
3225 SelectionDAGLowering::visitExp2(CallInst &I) {
3226   SDValue result;
3227
3228   if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
3229       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3230     SDValue Op = getValue(I.getOperand(1));
3231
3232     SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, Op);
3233
3234     //   FractionalPartOfX = x - (float)IntegerPartOfX;
3235     SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX);
3236     SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, Op, t1);
3237
3238     //   IntegerPartOfX <<= 23;
3239     IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX,
3240                                  DAG.getConstant(23, MVT::i32));
3241
3242     if (LimitFloatPrecision <= 6) {
3243       // For floating-point precision of 6:
3244       // 
3245       //   TwoToFractionalPartOfX =
3246       //     0.997535578f +
3247       //       (0.735607626f + 0.252464424f * x) * x;
3248       //
3249       // error 0.0144103317, which is 6 bits
3250       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3251                                getF32Constant(DAG, 0x3e814304));
3252       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
3253                                getF32Constant(DAG, 0x3f3c50c8));
3254       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3255       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, 
3256                                getF32Constant(DAG, 0x3f7f5e7e));
3257       SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5);
3258       SDValue TwoToFractionalPartOfX =
3259         DAG.getNode(ISD::ADD, MVT::i32, t6, IntegerPartOfX);
3260
3261       result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3262     } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3263       // For floating-point precision of 12:
3264       //
3265       //   TwoToFractionalPartOfX =
3266       //     0.999892986f +
3267       //       (0.696457318f +
3268       //         (0.224338339f + 0.792043434e-1f * x) * x) * x;
3269       //
3270       // error 0.000107046256, which is 13 to 14 bits
3271       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3272                                getF32Constant(DAG, 0x3da235e3));
3273       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
3274                                getF32Constant(DAG, 0x3e65b8f3));
3275       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3276       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, 
3277                                getF32Constant(DAG, 0x3f324b07));
3278       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3279       SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
3280                                getF32Constant(DAG, 0x3f7ff8fd));
3281       SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7);
3282       SDValue TwoToFractionalPartOfX =
3283         DAG.getNode(ISD::ADD, MVT::i32, t8, IntegerPartOfX);
3284
3285       result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3286     } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3287       // For floating-point precision of 18:
3288       //
3289       //   TwoToFractionalPartOfX =
3290       //     0.999999982f +
3291       //       (0.693148872f +
3292       //         (0.240227044f +
3293       //           (0.554906021e-1f +
3294       //             (0.961591928e-2f +
3295       //               (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3296       // error 2.47208000*10^(-7), which is better than 18 bits
3297       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3298                                getF32Constant(DAG, 0x3924b03e));
3299       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
3300                                getF32Constant(DAG, 0x3ab24b87));
3301       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3302       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, 
3303                                getF32Constant(DAG, 0x3c1d8c17));
3304       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3305       SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
3306                                getF32Constant(DAG, 0x3d634a1d));
3307       SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3308       SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
3309                                getF32Constant(DAG, 0x3e75fe14));
3310       SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
3311       SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10,
3312                                 getF32Constant(DAG, 0x3f317234));
3313       SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X);
3314       SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12,
3315                                 getF32Constant(DAG, 0x3f800000));
3316       SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13);
3317       SDValue TwoToFractionalPartOfX =
3318         DAG.getNode(ISD::ADD, MVT::i32, t14, IntegerPartOfX);
3319
3320       result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3321     }
3322   } else {
3323     // No special expansion.
3324     result = DAG.getNode(ISD::FEXP2,
3325                          getValue(I.getOperand(1)).getValueType(),
3326                          getValue(I.getOperand(1)));
3327   }
3328
3329   setValue(&I, result);
3330 }
3331
3332 /// visitPow - Lower a pow intrinsic. Handles the special sequences for
3333 /// limited-precision mode with x == 10.0f.
3334 void
3335 SelectionDAGLowering::visitPow(CallInst &I) {
3336   SDValue result;
3337   Value *Val = I.getOperand(1);
3338   bool IsExp10 = false;
3339
3340   if (getValue(Val).getValueType() == MVT::f32 &&
3341       getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
3342       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3343     if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3344       if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3345         APFloat Ten(10.0f);
3346         IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3347       }
3348     }
3349   }
3350
3351   if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3352     SDValue Op = getValue(I.getOperand(2));
3353
3354     // Put the exponent in the right bit position for later addition to the
3355     // final result:
3356     //
3357     //   #define LOG2OF10 3.3219281f
3358     //   IntegerPartOfX = (int32_t)(x * LOG2OF10);
3359     SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, Op,
3360                              getF32Constant(DAG, 0x40549a78));
3361     SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, t0);
3362
3363     //   FractionalPartOfX = x - (float)IntegerPartOfX;
3364     SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX);
3365     SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, t0, t1);
3366
3367     //   IntegerPartOfX <<= 23;
3368     IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX,
3369                                  DAG.getConstant(23, MVT::i32));
3370
3371     if (LimitFloatPrecision <= 6) {
3372       // For floating-point precision of 6:
3373       // 
3374       //   twoToFractionalPartOfX =
3375       //     0.997535578f +
3376       //       (0.735607626f + 0.252464424f * x) * x;
3377       // 
3378       // error 0.0144103317, which is 6 bits
3379       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3380                                getF32Constant(DAG, 0x3e814304));
3381       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
3382                                getF32Constant(DAG, 0x3f3c50c8));
3383       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3384       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, 
3385                                getF32Constant(DAG, 0x3f7f5e7e));
3386       SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5);
3387       SDValue TwoToFractionalPartOfX =
3388         DAG.getNode(ISD::ADD, MVT::i32, t6, IntegerPartOfX);
3389
3390       result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3391     } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3392       // For floating-point precision of 12:
3393       //
3394       //   TwoToFractionalPartOfX =
3395       //     0.999892986f +
3396       //       (0.696457318f +
3397       //         (0.224338339f + 0.792043434e-1f * x) * x) * x;
3398       //
3399       // error 0.000107046256, which is 13 to 14 bits
3400       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3401                                getF32Constant(DAG, 0x3da235e3));
3402       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
3403                                getF32Constant(DAG, 0x3e65b8f3));
3404       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3405       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, 
3406                                getF32Constant(DAG, 0x3f324b07));
3407       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3408       SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
3409                                getF32Constant(DAG, 0x3f7ff8fd));
3410       SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7);
3411       SDValue TwoToFractionalPartOfX =
3412         DAG.getNode(ISD::ADD, MVT::i32, t8, IntegerPartOfX);
3413
3414       result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3415     } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3416       // For floating-point precision of 18:
3417       //
3418       //   TwoToFractionalPartOfX =
3419       //     0.999999982f +
3420       //       (0.693148872f +
3421       //         (0.240227044f +
3422       //           (0.554906021e-1f +
3423       //             (0.961591928e-2f +
3424       //               (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3425       // error 2.47208000*10^(-7), which is better than 18 bits
3426       SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
3427                                getF32Constant(DAG, 0x3924b03e));
3428       SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
3429                                getF32Constant(DAG, 0x3ab24b87));
3430       SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3431       SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, 
3432                                getF32Constant(DAG, 0x3c1d8c17));
3433       SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3434       SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
3435                                getF32Constant(DAG, 0x3d634a1d));
3436       SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3437       SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
3438                                getF32Constant(DAG, 0x3e75fe14));
3439       SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
3440       SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10,
3441                                 getF32Constant(DAG, 0x3f317234));
3442       SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X);
3443       SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12,
3444                                 getF32Constant(DAG, 0x3f800000));
3445       SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13);
3446       SDValue TwoToFractionalPartOfX =
3447         DAG.getNode(ISD::ADD, MVT::i32, t14, IntegerPartOfX);
3448
3449       result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3450     }
3451   } else {
3452     // No special expansion.
3453     result = DAG.getNode(ISD::FPOW,
3454                          getValue(I.getOperand(1)).getValueType(),
3455                          getValue(I.getOperand(1)),
3456                          getValue(I.getOperand(2)));
3457   }
3458
3459   setValue(&I, result);
3460 }
3461
3462 /// visitIntrinsicCall - Lower the call to the specified intrinsic function.  If
3463 /// we want to emit this as a call to a named external function, return the name
3464 /// otherwise lower it and return null.
3465 const char *
3466 SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
3467   switch (Intrinsic) {
3468   default:
3469     // By default, turn this into a target intrinsic node.
3470     visitTargetIntrinsic(I, Intrinsic);
3471     return 0;
3472   case Intrinsic::vastart:  visitVAStart(I); return 0;
3473   case Intrinsic::vaend:    visitVAEnd(I); return 0;
3474   case Intrinsic::vacopy:   visitVACopy(I); return 0;
3475   case Intrinsic::returnaddress:
3476     setValue(&I, DAG.getNode(ISD::RETURNADDR, TLI.getPointerTy(),
3477                              getValue(I.getOperand(1))));
3478     return 0;
3479   case Intrinsic::frameaddress:
3480     setValue(&I, DAG.getNode(ISD::FRAMEADDR, TLI.getPointerTy(),
3481                              getValue(I.getOperand(1))));
3482     return 0;
3483   case Intrinsic::setjmp:
3484     return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3485     break;
3486   case Intrinsic::longjmp:
3487     return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3488     break;
3489   case Intrinsic::memcpy_i32:
3490   case Intrinsic::memcpy_i64: {
3491     SDValue Op1 = getValue(I.getOperand(1));
3492     SDValue Op2 = getValue(I.getOperand(2));
3493     SDValue Op3 = getValue(I.getOperand(3));
3494     unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3495     DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false,
3496                               I.getOperand(1), 0, I.getOperand(2), 0));
3497     return 0;
3498   }
3499   case Intrinsic::memset_i32:
3500   case Intrinsic::memset_i64: {
3501     SDValue Op1 = getValue(I.getOperand(1));
3502     SDValue Op2 = getValue(I.getOperand(2));
3503     SDValue Op3 = getValue(I.getOperand(3));
3504     unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3505     DAG.setRoot(DAG.getMemset(getRoot(), Op1, Op2, Op3, Align,
3506                               I.getOperand(1), 0));
3507     return 0;
3508   }
3509   case Intrinsic::memmove_i32:
3510   case Intrinsic::memmove_i64: {
3511     SDValue Op1 = getValue(I.getOperand(1));
3512     SDValue Op2 = getValue(I.getOperand(2));
3513     SDValue Op3 = getValue(I.getOperand(3));
3514     unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3515
3516     // If the source and destination are known to not be aliases, we can
3517     // lower memmove as memcpy.
3518     uint64_t Size = -1ULL;
3519     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
3520       Size = C->getZExtValue();
3521     if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3522         AliasAnalysis::NoAlias) {
3523       DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false,
3524                                 I.getOperand(1), 0, I.getOperand(2), 0));
3525       return 0;
3526     }
3527
3528     DAG.setRoot(DAG.getMemmove(getRoot(), Op1, Op2, Op3, Align,
3529                                I.getOperand(1), 0, I.getOperand(2), 0));
3530     return 0;
3531   }
3532   case Intrinsic::dbg_stoppoint: {
3533     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3534     DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
3535     if (MMI && SPI.getContext() && MMI->Verify(SPI.getContext())) {
3536       DebugInfoDesc *DD = MMI->getDescFor(SPI.getContext());
3537       assert(DD && "Not a debug information descriptor");
3538       DAG.setRoot(DAG.getDbgStopPoint(getRoot(),
3539                                       SPI.getLine(),
3540                                       SPI.getColumn(),
3541                                       cast<CompileUnitDesc>(DD)));
3542     }
3543
3544     return 0;
3545   }
3546   case Intrinsic::dbg_region_start: {
3547     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3548     DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
3549     if (MMI && RSI.getContext() && MMI->Verify(RSI.getContext())) {
3550       unsigned LabelID = MMI->RecordRegionStart(RSI.getContext());
3551       DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID));
3552     }
3553
3554     return 0;
3555   }
3556   case Intrinsic::dbg_region_end: {
3557     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3558     DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
3559     if (MMI && REI.getContext() && MMI->Verify(REI.getContext())) {
3560       unsigned LabelID = MMI->RecordRegionEnd(REI.getContext());
3561       DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID));
3562     }
3563
3564     return 0;
3565   }
3566   case Intrinsic::dbg_func_start: {
3567     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3568     if (!MMI) return 0;
3569     DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
3570     Value *SP = FSI.getSubprogram();
3571     if (SP && MMI->Verify(SP)) {
3572       // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is
3573       // what (most?) gdb expects.
3574       DebugInfoDesc *DD = MMI->getDescFor(SP);
3575       assert(DD && "Not a debug information descriptor");
3576       SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD);
3577       const CompileUnitDesc *CompileUnit = Subprogram->getFile();
3578       unsigned SrcFile = MMI->RecordSource(CompileUnit);
3579       // Record the source line but does create a label. It will be emitted
3580       // at asm emission time.
3581       MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile);
3582     }
3583
3584     return 0;
3585   }
3586   case Intrinsic::dbg_declare: {
3587     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3588     DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
3589     Value *Variable = DI.getVariable();
3590     if (MMI && Variable && MMI->Verify(Variable))
3591       DAG.setRoot(DAG.getNode(ISD::DECLARE, MVT::Other, getRoot(),
3592                               getValue(DI.getAddress()), getValue(Variable)));
3593     return 0;
3594   }
3595     
3596   case Intrinsic::eh_exception: {
3597     if (!CurMBB->isLandingPad()) {
3598       // FIXME: Mark exception register as live in.  Hack for PR1508.
3599       unsigned Reg = TLI.getExceptionAddressRegister();
3600       if (Reg) CurMBB->addLiveIn(Reg);
3601     }
3602     // Insert the EXCEPTIONADDR instruction.
3603     SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3604     SDValue Ops[1];
3605     Ops[0] = DAG.getRoot();
3606     SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, VTs, Ops, 1);
3607     setValue(&I, Op);
3608     DAG.setRoot(Op.getValue(1));
3609     return 0;
3610   }
3611
3612   case Intrinsic::eh_selector_i32:
3613   case Intrinsic::eh_selector_i64: {
3614     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3615     MVT VT = (Intrinsic == Intrinsic::eh_selector_i32 ?
3616                          MVT::i32 : MVT::i64);
3617     
3618     if (MMI) {
3619       if (CurMBB->isLandingPad())
3620         AddCatchInfo(I, MMI, CurMBB);
3621       else {
3622 #ifndef NDEBUG
3623         FuncInfo.CatchInfoLost.insert(&I);
3624 #endif
3625         // FIXME: Mark exception selector register as live in.  Hack for PR1508.
3626         unsigned Reg = TLI.getExceptionSelectorRegister();
3627         if (Reg) CurMBB->addLiveIn(Reg);
3628       }
3629
3630       // Insert the EHSELECTION instruction.
3631       SDVTList VTs = DAG.getVTList(VT, MVT::Other);
3632       SDValue Ops[2];
3633       Ops[0] = getValue(I.getOperand(1));
3634       Ops[1] = getRoot();
3635       SDValue Op = DAG.getNode(ISD::EHSELECTION, VTs, Ops, 2);
3636       setValue(&I, Op);
3637       DAG.setRoot(Op.getValue(1));
3638     } else {
3639       setValue(&I, DAG.getConstant(0, VT));
3640     }
3641     
3642     return 0;
3643   }
3644
3645   case Intrinsic::eh_typeid_for_i32:
3646   case Intrinsic::eh_typeid_for_i64: {
3647     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3648     MVT VT = (Intrinsic == Intrinsic::eh_typeid_for_i32 ?
3649                          MVT::i32 : MVT::i64);
3650
3651     if (MMI) {
3652       // Find the type id for the given typeinfo.
3653       GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
3654
3655       unsigned TypeID = MMI->getTypeIDFor(GV);
3656       setValue(&I, DAG.getConstant(TypeID, VT));
3657     } else {
3658       // Return something different to eh_selector.
3659       setValue(&I, DAG.getConstant(1, VT));
3660     }
3661
3662     return 0;
3663   }
3664
3665   case Intrinsic::eh_return_i32:
3666   case Intrinsic::eh_return_i64:
3667     if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
3668       MMI->setCallsEHReturn(true);
3669       DAG.setRoot(DAG.getNode(ISD::EH_RETURN,
3670                               MVT::Other,
3671                               getControlRoot(),
3672                               getValue(I.getOperand(1)),
3673                               getValue(I.getOperand(2))));
3674     } else {
3675       setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
3676     }
3677
3678     return 0;
3679   case Intrinsic::eh_unwind_init:
3680     if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
3681       MMI->setCallsUnwindInit(true);
3682     }
3683
3684     return 0;
3685
3686   case Intrinsic::eh_dwarf_cfa: {
3687     MVT VT = getValue(I.getOperand(1)).getValueType();
3688     SDValue CfaArg;
3689     if (VT.bitsGT(TLI.getPointerTy()))
3690       CfaArg = DAG.getNode(ISD::TRUNCATE,
3691                            TLI.getPointerTy(), getValue(I.getOperand(1)));
3692     else
3693       CfaArg = DAG.getNode(ISD::SIGN_EXTEND,
3694                            TLI.getPointerTy(), getValue(I.getOperand(1)));
3695
3696     SDValue Offset = DAG.getNode(ISD::ADD,
3697                                  TLI.getPointerTy(),
3698                                  DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET,
3699                                              TLI.getPointerTy()),
3700                                  CfaArg);
3701     setValue(&I, DAG.getNode(ISD::ADD,
3702                              TLI.getPointerTy(),
3703                              DAG.getNode(ISD::FRAMEADDR,
3704                                          TLI.getPointerTy(),
3705                                          DAG.getConstant(0,
3706                                                          TLI.getPointerTy())),
3707                              Offset));
3708     return 0;
3709   }
3710
3711   case Intrinsic::sqrt:
3712     setValue(&I, DAG.getNode(ISD::FSQRT,
3713                              getValue(I.getOperand(1)).getValueType(),
3714                              getValue(I.getOperand(1))));
3715     return 0;
3716   case Intrinsic::powi:
3717     setValue(&I, DAG.getNode(ISD::FPOWI,
3718                              getValue(I.getOperand(1)).getValueType(),
3719                              getValue(I.getOperand(1)),
3720                              getValue(I.getOperand(2))));
3721     return 0;
3722   case Intrinsic::sin:
3723     setValue(&I, DAG.getNode(ISD::FSIN,
3724                              getValue(I.getOperand(1)).getValueType(),
3725                              getValue(I.getOperand(1))));
3726     return 0;
3727   case Intrinsic::cos:
3728     setValue(&I, DAG.getNode(ISD::FCOS,
3729                              getValue(I.getOperand(1)).getValueType(),
3730                              getValue(I.getOperand(1))));
3731     return 0;
3732   case Intrinsic::log:
3733     visitLog(I);
3734     return 0;
3735   case Intrinsic::log2:
3736     visitLog2(I);
3737     return 0;
3738   case Intrinsic::log10:
3739     visitLog10(I);
3740     return 0;
3741   case Intrinsic::exp:
3742     visitExp(I);
3743     return 0;
3744   case Intrinsic::exp2:
3745     visitExp2(I);
3746     return 0;
3747   case Intrinsic::pow:
3748     visitPow(I);
3749     return 0;
3750   case Intrinsic::pcmarker: {
3751     SDValue Tmp = getValue(I.getOperand(1));
3752     DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp));
3753     return 0;
3754   }
3755   case Intrinsic::readcyclecounter: {
3756     SDValue Op = getRoot();
3757     SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER,
3758                                 DAG.getNodeValueTypes(MVT::i64, MVT::Other), 2,
3759                                 &Op, 1);
3760     setValue(&I, Tmp);
3761     DAG.setRoot(Tmp.getValue(1));
3762     return 0;
3763   }
3764   case Intrinsic::part_select: {
3765     // Currently not implemented: just abort
3766     assert(0 && "part_select intrinsic not implemented");
3767     abort();
3768   }
3769   case Intrinsic::part_set: {
3770     // Currently not implemented: just abort
3771     assert(0 && "part_set intrinsic not implemented");
3772     abort();
3773   }
3774   case Intrinsic::bswap:
3775     setValue(&I, DAG.getNode(ISD::BSWAP,
3776                              getValue(I.getOperand(1)).getValueType(),
3777                              getValue(I.getOperand(1))));
3778     return 0;
3779   case Intrinsic::cttz: {
3780     SDValue Arg = getValue(I.getOperand(1));
3781     MVT Ty = Arg.getValueType();
3782     SDValue result = DAG.getNode(ISD::CTTZ, Ty, Arg);
3783     setValue(&I, result);
3784     return 0;
3785   }
3786   case Intrinsic::ctlz: {
3787     SDValue Arg = getValue(I.getOperand(1));
3788     MVT Ty = Arg.getValueType();
3789     SDValue result = DAG.getNode(ISD::CTLZ, Ty, Arg);
3790     setValue(&I, result);
3791     return 0;
3792   }
3793   case Intrinsic::ctpop: {
3794     SDValue Arg = getValue(I.getOperand(1));
3795     MVT Ty = Arg.getValueType();
3796     SDValue result = DAG.getNode(ISD::CTPOP, Ty, Arg);
3797     setValue(&I, result);
3798     return 0;
3799   }
3800   case Intrinsic::stacksave: {
3801     SDValue Op = getRoot();
3802     SDValue Tmp = DAG.getNode(ISD::STACKSAVE,
3803               DAG.getNodeValueTypes(TLI.getPointerTy(), MVT::Other), 2, &Op, 1);
3804     setValue(&I, Tmp);
3805     DAG.setRoot(Tmp.getValue(1));
3806     return 0;
3807   }
3808   case Intrinsic::stackrestore: {
3809     SDValue Tmp = getValue(I.getOperand(1));
3810     DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp));
3811     return 0;
3812   }
3813   case Intrinsic::var_annotation:
3814     // Discard annotate attributes
3815     return 0;
3816
3817   case Intrinsic::init_trampoline: {
3818     const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
3819
3820     SDValue Ops[6];
3821     Ops[0] = getRoot();
3822     Ops[1] = getValue(I.getOperand(1));
3823     Ops[2] = getValue(I.getOperand(2));
3824     Ops[3] = getValue(I.getOperand(3));
3825     Ops[4] = DAG.getSrcValue(I.getOperand(1));
3826     Ops[5] = DAG.getSrcValue(F);
3827
3828     SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE,
3829                                 DAG.getNodeValueTypes(TLI.getPointerTy(),
3830                                                       MVT::Other), 2,
3831                                 Ops, 6);
3832
3833     setValue(&I, Tmp);
3834     DAG.setRoot(Tmp.getValue(1));
3835     return 0;
3836   }
3837
3838   case Intrinsic::gcroot:
3839     if (GFI) {
3840       Value *Alloca = I.getOperand(1);
3841       Constant *TypeMap = cast<Constant>(I.getOperand(2));
3842       
3843       FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
3844       GFI->addStackRoot(FI->getIndex(), TypeMap);
3845     }
3846     return 0;
3847
3848   case Intrinsic::gcread:
3849   case Intrinsic::gcwrite:
3850     assert(0 && "GC failed to lower gcread/gcwrite intrinsics!");
3851     return 0;
3852
3853   case Intrinsic::flt_rounds: {
3854     setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, MVT::i32));
3855     return 0;
3856   }
3857
3858   case Intrinsic::trap: {
3859     DAG.setRoot(DAG.getNode(ISD::TRAP, MVT::Other, getRoot()));
3860     return 0;
3861   }
3862   case Intrinsic::prefetch: {
3863     SDValue Ops[4];
3864     Ops[0] = getRoot();
3865     Ops[1] = getValue(I.getOperand(1));
3866     Ops[2] = getValue(I.getOperand(2));
3867     Ops[3] = getValue(I.getOperand(3));
3868     DAG.setRoot(DAG.getNode(ISD::PREFETCH, MVT::Other, &Ops[0], 4));
3869     return 0;
3870   }
3871   
3872   case Intrinsic::memory_barrier: {
3873     SDValue Ops[6];
3874     Ops[0] = getRoot();
3875     for (int x = 1; x < 6; ++x)
3876       Ops[x] = getValue(I.getOperand(x));
3877
3878     DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, MVT::Other, &Ops[0], 6));
3879     return 0;
3880   }
3881   case Intrinsic::atomic_cmp_swap: {
3882     SDValue Root = getRoot();   
3883     SDValue L;
3884     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3885       case MVT::i8:
3886         L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_8, Root, 
3887                           getValue(I.getOperand(1)), 
3888                           getValue(I.getOperand(2)),
3889                           getValue(I.getOperand(3)),
3890                           I.getOperand(1));
3891         break;
3892       case MVT::i16:
3893         L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_16, Root, 
3894                           getValue(I.getOperand(1)), 
3895                           getValue(I.getOperand(2)),
3896                           getValue(I.getOperand(3)),
3897                           I.getOperand(1));
3898         break;
3899       case MVT::i32:
3900         L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_32, Root, 
3901                           getValue(I.getOperand(1)), 
3902                           getValue(I.getOperand(2)),
3903                           getValue(I.getOperand(3)),
3904                           I.getOperand(1));
3905         break;
3906       case MVT::i64:
3907         L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_64, Root, 
3908                           getValue(I.getOperand(1)), 
3909                           getValue(I.getOperand(2)),
3910                           getValue(I.getOperand(3)),
3911                           I.getOperand(1));
3912         break;
3913       default:
3914        assert(0 && "Invalid atomic type");
3915        abort();
3916     }
3917     setValue(&I, L);
3918     DAG.setRoot(L.getValue(1));
3919     return 0;
3920   }
3921   case Intrinsic::atomic_load_add:
3922     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3923       case MVT::i8:
3924         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_8);
3925       case MVT::i16:
3926         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_16);
3927       case MVT::i32:
3928         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_32);
3929       case MVT::i64:
3930         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_64);
3931       default:
3932        assert(0 && "Invalid atomic type");
3933        abort();
3934     }
3935   case Intrinsic::atomic_load_sub:
3936     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3937       case MVT::i8:
3938         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_8);
3939       case MVT::i16:
3940         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_16);
3941       case MVT::i32:
3942         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_32);
3943       case MVT::i64:
3944         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_64);
3945       default:
3946        assert(0 && "Invalid atomic type");
3947        abort();
3948     }
3949   case Intrinsic::atomic_load_or:
3950     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3951       case MVT::i8:
3952         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_8);
3953       case MVT::i16:
3954         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_16);
3955       case MVT::i32:
3956         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_32);
3957       case MVT::i64:
3958         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_64);
3959       default:
3960        assert(0 && "Invalid atomic type");
3961        abort();
3962     }
3963   case Intrinsic::atomic_load_xor:
3964     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3965       case MVT::i8:
3966         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_8);
3967       case MVT::i16:
3968         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_16);
3969       case MVT::i32:
3970         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_32);
3971       case MVT::i64:
3972         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_64);
3973       default:
3974        assert(0 && "Invalid atomic type");
3975        abort();
3976     }
3977   case Intrinsic::atomic_load_and:
3978     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3979       case MVT::i8:
3980         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_8);
3981       case MVT::i16:
3982         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_16);
3983       case MVT::i32:
3984         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_32);
3985       case MVT::i64:
3986         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_64);
3987       default:
3988        assert(0 && "Invalid atomic type");
3989        abort();
3990     }
3991   case Intrinsic::atomic_load_nand:
3992     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3993       case MVT::i8:
3994         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_8);
3995       case MVT::i16:
3996         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_16);
3997       case MVT::i32:
3998         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_32);
3999       case MVT::i64:
4000         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_64);
4001       default:
4002        assert(0 && "Invalid atomic type");
4003        abort();
4004     }
4005   case Intrinsic::atomic_load_max:
4006     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4007       case MVT::i8:
4008         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_8);
4009       case MVT::i16:
4010         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_16);
4011       case MVT::i32:
4012         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_32);
4013       case MVT::i64:
4014         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_64);
4015       default:
4016        assert(0 && "Invalid atomic type");
4017        abort();
4018     }
4019   case Intrinsic::atomic_load_min:
4020     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4021       case MVT::i8:
4022         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_8);
4023       case MVT::i16:
4024         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_16);
4025       case MVT::i32:
4026         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_32);
4027       case MVT::i64:
4028         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_64);
4029       default:
4030        assert(0 && "Invalid atomic type");
4031        abort();
4032     }
4033   case Intrinsic::atomic_load_umin:
4034     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4035       case MVT::i8:
4036         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_8);
4037       case MVT::i16:
4038         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_16);
4039       case MVT::i32:
4040         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_32);
4041       case MVT::i64:
4042         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_64);
4043       default:
4044        assert(0 && "Invalid atomic type");
4045        abort();
4046     }
4047   case Intrinsic::atomic_load_umax:
4048     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4049       case MVT::i8:
4050         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_8);
4051       case MVT::i16:
4052         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_16);
4053       case MVT::i32:
4054         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_32);
4055       case MVT::i64:
4056         return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_64);
4057       default:
4058        assert(0 && "Invalid atomic type");
4059        abort();
4060     }
4061   case Intrinsic::atomic_swap:
4062     switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4063       case MVT::i8:
4064         return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_8);
4065       case MVT::i16:
4066         return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_16);
4067       case MVT::i32:
4068         return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_32);
4069       case MVT::i64:
4070         return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_64);
4071       default:
4072        assert(0 && "Invalid atomic type");
4073        abort();
4074     }
4075   }
4076 }
4077
4078
4079 void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee,
4080                                        bool IsTailCall,
4081                                        MachineBasicBlock *LandingPad) {
4082   const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4083   const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
4084   MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4085   unsigned BeginLabel = 0, EndLabel = 0;
4086
4087   TargetLowering::ArgListTy Args;
4088   TargetLowering::ArgListEntry Entry;
4089   Args.reserve(CS.arg_size());
4090   for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
4091        i != e; ++i) {
4092     SDValue ArgNode = getValue(*i);
4093     Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4094
4095     unsigned attrInd = i - CS.arg_begin() + 1;
4096     Entry.isSExt  = CS.paramHasAttr(attrInd, ParamAttr::SExt);
4097     Entry.isZExt  = CS.paramHasAttr(attrInd, ParamAttr::ZExt);
4098     Entry.isInReg = CS.paramHasAttr(attrInd, ParamAttr::InReg);
4099     Entry.isSRet  = CS.paramHasAttr(attrInd, ParamAttr::StructRet);
4100     Entry.isNest  = CS.paramHasAttr(attrInd, ParamAttr::Nest);
4101     Entry.isByVal = CS.paramHasAttr(attrInd, ParamAttr::ByVal);
4102     Entry.Alignment = CS.getParamAlignment(attrInd);
4103     Args.push_back(Entry);
4104   }
4105
4106   if (LandingPad && MMI) {
4107     // Insert a label before the invoke call to mark the try range.  This can be
4108     // used to detect deletion of the invoke via the MachineModuleInfo.
4109     BeginLabel = MMI->NextLabelID();
4110     // Both PendingLoads and PendingExports must be flushed here;
4111     // this call might not return.
4112     (void)getRoot();
4113     DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getControlRoot(), BeginLabel));
4114   }
4115
4116   std::pair<SDValue,SDValue> Result =
4117     TLI.LowerCallTo(getRoot(), CS.getType(),
4118                     CS.paramHasAttr(0, ParamAttr::SExt),
4119                     CS.paramHasAttr(0, ParamAttr::ZExt),
4120                     FTy->isVarArg(), CS.getCallingConv(),
4121                     IsTailCall && PerformTailCallOpt,
4122                     Callee, Args, DAG);
4123   if (CS.getType() != Type::VoidTy)
4124     setValue(CS.getInstruction(), Result.first);
4125   DAG.setRoot(Result.second);
4126
4127   if (LandingPad && MMI) {
4128     // Insert a label at the end of the invoke call to mark the try range.  This
4129     // can be used to detect deletion of the invoke via the MachineModuleInfo.
4130     EndLabel = MMI->NextLabelID();
4131     DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getRoot(), EndLabel));
4132
4133     // Inform MachineModuleInfo of range.
4134     MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4135   }
4136 }
4137
4138
4139 void SelectionDAGLowering::visitCall(CallInst &I) {
4140   const char *RenameFn = 0;
4141   if (Function *F = I.getCalledFunction()) {
4142     if (F->isDeclaration()) {
4143       if (unsigned IID = F->getIntrinsicID()) {
4144         RenameFn = visitIntrinsicCall(I, IID);
4145         if (!RenameFn)
4146           return;
4147       }
4148     }
4149
4150     // Check for well-known libc/libm calls.  If the function is internal, it
4151     // can't be a library call.
4152     unsigned NameLen = F->getNameLen();
4153     if (!F->hasInternalLinkage() && NameLen) {
4154       const char *NameStr = F->getNameStart();
4155       if (NameStr[0] == 'c' &&
4156           ((NameLen == 8 && !strcmp(NameStr, "copysign")) ||
4157            (NameLen == 9 && !strcmp(NameStr, "copysignf")))) {
4158         if (I.getNumOperands() == 3 &&   // Basic sanity checks.
4159             I.getOperand(1)->getType()->isFloatingPoint() &&
4160             I.getType() == I.getOperand(1)->getType() &&
4161             I.getType() == I.getOperand(2)->getType()) {
4162           SDValue LHS = getValue(I.getOperand(1));
4163           SDValue RHS = getValue(I.getOperand(2));
4164           setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(),
4165                                    LHS, RHS));
4166           return;
4167         }
4168       } else if (NameStr[0] == 'f' &&
4169                  ((NameLen == 4 && !strcmp(NameStr, "fabs")) ||
4170                   (NameLen == 5 && !strcmp(NameStr, "fabsf")) ||
4171                   (NameLen == 5 && !strcmp(NameStr, "fabsl")))) {
4172         if (I.getNumOperands() == 2 &&   // Basic sanity checks.
4173             I.getOperand(1)->getType()->isFloatingPoint() &&
4174             I.getType() == I.getOperand(1)->getType()) {
4175           SDValue Tmp = getValue(I.getOperand(1));
4176           setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
4177           return;
4178         }
4179       } else if (NameStr[0] == 's' && 
4180                  ((NameLen == 3 && !strcmp(NameStr, "sin")) ||
4181                   (NameLen == 4 && !strcmp(NameStr, "sinf")) ||
4182                   (NameLen == 4 && !strcmp(NameStr, "sinl")))) {
4183         if (I.getNumOperands() == 2 &&   // Basic sanity checks.
4184             I.getOperand(1)->getType()->isFloatingPoint() &&
4185             I.getType() == I.getOperand(1)->getType()) {
4186           SDValue Tmp = getValue(I.getOperand(1));
4187           setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
4188           return;
4189         }
4190       } else if (NameStr[0] == 'c' &&
4191                  ((NameLen == 3 && !strcmp(NameStr, "cos")) ||
4192                   (NameLen == 4 && !strcmp(NameStr, "cosf")) ||
4193                   (NameLen == 4 && !strcmp(NameStr, "cosl")))) {
4194         if (I.getNumOperands() == 2 &&   // Basic sanity checks.
4195             I.getOperand(1)->getType()->isFloatingPoint() &&
4196             I.getType() == I.getOperand(1)->getType()) {
4197           SDValue Tmp = getValue(I.getOperand(1));
4198           setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
4199           return;
4200         }
4201       }
4202     }
4203   } else if (isa<InlineAsm>(I.getOperand(0))) {
4204     visitInlineAsm(&I);
4205     return;
4206   }
4207
4208   SDValue Callee;
4209   if (!RenameFn)
4210     Callee = getValue(I.getOperand(0));
4211   else
4212     Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
4213
4214   LowerCallTo(&I, Callee, I.isTailCall());
4215 }
4216
4217
4218 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
4219 /// this value and returns the result as a ValueVT value.  This uses 
4220 /// Chain/Flag as the input and updates them for the output Chain/Flag.
4221 /// If the Flag pointer is NULL, no flag is used.
4222 SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, 
4223                                       SDValue &Chain,
4224                                       SDValue *Flag) const {
4225   // Assemble the legal parts into the final values.
4226   SmallVector<SDValue, 4> Values(ValueVTs.size());
4227   SmallVector<SDValue, 8> Parts;
4228   for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4229     // Copy the legal parts from the registers.
4230     MVT ValueVT = ValueVTs[Value];
4231     unsigned NumRegs = TLI->getNumRegisters(ValueVT);
4232     MVT RegisterVT = RegVTs[Value];
4233
4234     Parts.resize(NumRegs);
4235     for (unsigned i = 0; i != NumRegs; ++i) {
4236       SDValue P;
4237       if (Flag == 0)
4238         P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT);
4239       else {
4240         P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT, *Flag);
4241         *Flag = P.getValue(2);
4242       }
4243       Chain = P.getValue(1);
4244       
4245       // If the source register was virtual and if we know something about it,
4246       // add an assert node.
4247       if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4248           RegisterVT.isInteger() && !RegisterVT.isVector()) {
4249         unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4250         FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4251         if (FLI.LiveOutRegInfo.size() > SlotNo) {
4252           FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
4253           
4254           unsigned RegSize = RegisterVT.getSizeInBits();
4255           unsigned NumSignBits = LOI.NumSignBits;
4256           unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
4257           
4258           // FIXME: We capture more information than the dag can represent.  For
4259           // now, just use the tightest assertzext/assertsext possible.
4260           bool isSExt = true;
4261           MVT FromVT(MVT::Other);
4262           if (NumSignBits == RegSize)
4263             isSExt = true, FromVT = MVT::i1;   // ASSERT SEXT 1
4264           else if (NumZeroBits >= RegSize-1)
4265             isSExt = false, FromVT = MVT::i1;  // ASSERT ZEXT 1
4266           else if (NumSignBits > RegSize-8)
4267             isSExt = true, FromVT = MVT::i8;   // ASSERT SEXT 8
4268           else if (NumZeroBits >= RegSize-9)
4269             isSExt = false, FromVT = MVT::i8;  // ASSERT ZEXT 8
4270           else if (NumSignBits > RegSize-16)
4271             isSExt = true, FromVT = MVT::i16;   // ASSERT SEXT 16
4272           else if (NumZeroBits >= RegSize-17)
4273             isSExt = false, FromVT = MVT::i16;  // ASSERT ZEXT 16
4274           else if (NumSignBits > RegSize-32)
4275             isSExt = true, FromVT = MVT::i32;   // ASSERT SEXT 32
4276           else if (NumZeroBits >= RegSize-33)
4277             isSExt = false, FromVT = MVT::i32;  // ASSERT ZEXT 32
4278           
4279           if (FromVT != MVT::Other) {
4280             P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext,
4281                             RegisterVT, P, DAG.getValueType(FromVT));
4282
4283           }
4284         }
4285       }
4286       
4287       Parts[i] = P;
4288     }
4289   
4290     Values[Value] = getCopyFromParts(DAG, Parts.begin(), NumRegs, RegisterVT,
4291                                      ValueVT);
4292     Part += NumRegs;
4293     Parts.clear();
4294   }
4295
4296   return DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4297                             &Values[0], ValueVTs.size());
4298 }
4299
4300 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
4301 /// specified value into the registers specified by this object.  This uses 
4302 /// Chain/Flag as the input and updates them for the output Chain/Flag.
4303 /// If the Flag pointer is NULL, no flag is used.
4304 void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG,
4305                                  SDValue &Chain, SDValue *Flag) const {
4306   // Get the list of the values's legal parts.
4307   unsigned NumRegs = Regs.size();
4308   SmallVector<SDValue, 8> Parts(NumRegs);
4309   for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4310     MVT ValueVT = ValueVTs[Value];
4311     unsigned NumParts = TLI->getNumRegisters(ValueVT);
4312     MVT RegisterVT = RegVTs[Value];
4313
4314     getCopyToParts(DAG, Val.getValue(Val.getResNo() + Value),
4315                    &Parts[Part], NumParts, RegisterVT);
4316     Part += NumParts;
4317   }
4318
4319   // Copy the parts into the registers.
4320   SmallVector<SDValue, 8> Chains(NumRegs);
4321   for (unsigned i = 0; i != NumRegs; ++i) {
4322     SDValue Part;
4323     if (Flag == 0)
4324       Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i]);
4325     else {
4326       Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i], *Flag);
4327       *Flag = Part.getValue(1);
4328     }
4329     Chains[i] = Part.getValue(0);
4330   }
4331   
4332   if (NumRegs == 1 || Flag)
4333     // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is 
4334     // flagged to it. That is the CopyToReg nodes and the user are considered
4335     // a single scheduling unit. If we create a TokenFactor and return it as
4336     // chain, then the TokenFactor is both a predecessor (operand) of the
4337     // user as well as a successor (the TF operands are flagged to the user).
4338     // c1, f1 = CopyToReg
4339     // c2, f2 = CopyToReg
4340     // c3     = TokenFactor c1, c2
4341     // ...
4342     //        = op c3, ..., f2
4343     Chain = Chains[NumRegs-1];
4344   else
4345     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Chains[0], NumRegs);
4346 }
4347
4348 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
4349 /// operand list.  This adds the code marker and includes the number of 
4350 /// values added into it.
4351 void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
4352                                         std::vector<SDValue> &Ops) const {
4353   MVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
4354   Ops.push_back(DAG.getTargetConstant(Code | (Regs.size() << 3), IntPtrTy));
4355   for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
4356     unsigned NumRegs = TLI->getNumRegisters(ValueVTs[Value]);
4357     MVT RegisterVT = RegVTs[Value];
4358     for (unsigned i = 0; i != NumRegs; ++i)
4359       Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
4360   }
4361 }
4362
4363 /// isAllocatableRegister - If the specified register is safe to allocate, 
4364 /// i.e. it isn't a stack pointer or some other special register, return the
4365 /// register class for the register.  Otherwise, return null.
4366 static const TargetRegisterClass *
4367 isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4368                       const TargetLowering &TLI,
4369                       const TargetRegisterInfo *TRI) {
4370   MVT FoundVT = MVT::Other;
4371   const TargetRegisterClass *FoundRC = 0;
4372   for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4373        E = TRI->regclass_end(); RCI != E; ++RCI) {
4374     MVT ThisVT = MVT::Other;
4375
4376     const TargetRegisterClass *RC = *RCI;
4377     // If none of the the value types for this register class are valid, we 
4378     // can't use it.  For example, 64-bit reg classes on 32-bit targets.
4379     for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4380          I != E; ++I) {
4381       if (TLI.isTypeLegal(*I)) {
4382         // If we have already found this register in a different register class,
4383         // choose the one with the largest VT specified.  For example, on
4384         // PowerPC, we favor f64 register classes over f32.
4385         if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
4386           ThisVT = *I;
4387           break;
4388         }
4389       }
4390     }
4391     
4392     if (ThisVT == MVT::Other) continue;
4393     
4394     // NOTE: This isn't ideal.  In particular, this might allocate the
4395     // frame pointer in functions that need it (due to them not being taken
4396     // out of allocation, because a variable sized allocation hasn't been seen
4397     // yet).  This is a slight code pessimization, but should still work.
4398     for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4399          E = RC->allocation_order_end(MF); I != E; ++I)
4400       if (*I == Reg) {
4401         // We found a matching register class.  Keep looking at others in case
4402         // we find one with larger registers that this physreg is also in.
4403         FoundRC = RC;
4404         FoundVT = ThisVT;
4405         break;
4406       }
4407   }
4408   return FoundRC;
4409 }    
4410
4411
4412 namespace llvm {
4413 /// AsmOperandInfo - This contains information for each constraint that we are
4414 /// lowering.
4415 struct VISIBILITY_HIDDEN SDISelAsmOperandInfo : 
4416     public TargetLowering::AsmOperandInfo {
4417   /// CallOperand - If this is the result output operand or a clobber
4418   /// this is null, otherwise it is the incoming operand to the CallInst.
4419   /// This gets modified as the asm is processed.
4420   SDValue CallOperand;
4421
4422   /// AssignedRegs - If this is a register or register class operand, this
4423   /// contains the set of register corresponding to the operand.
4424   RegsForValue AssignedRegs;
4425   
4426   explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4427     : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4428   }
4429   
4430   /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4431   /// busy in OutputRegs/InputRegs.
4432   void MarkAllocatedRegs(bool isOutReg, bool isInReg,
4433                          std::set<unsigned> &OutputRegs, 
4434                          std::set<unsigned> &InputRegs,
4435                          const TargetRegisterInfo &TRI) const {
4436     if (isOutReg) {
4437       for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4438         MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4439     }
4440     if (isInReg) {
4441       for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4442         MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4443     }
4444   }
4445   
4446 private:
4447   /// MarkRegAndAliases - Mark the specified register and all aliases in the
4448   /// specified set.
4449   static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, 
4450                                 const TargetRegisterInfo &TRI) {
4451     assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4452     Regs.insert(Reg);
4453     if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4454       for (; *Aliases; ++Aliases)
4455         Regs.insert(*Aliases);
4456   }
4457 };
4458 } // end llvm namespace.
4459
4460
4461 /// GetRegistersForValue - Assign registers (virtual or physical) for the
4462 /// specified operand.  We prefer to assign virtual registers, to allow the
4463 /// register allocator handle the assignment process.  However, if the asm uses
4464 /// features that we can't model on machineinstrs, we have SDISel do the
4465 /// allocation.  This produces generally horrible, but correct, code.
4466 ///
4467 ///   OpInfo describes the operand.
4468 ///   HasEarlyClobber is true if there are any early clobber constraints (=&r)
4469 ///     or any explicitly clobbered registers.
4470 ///   Input and OutputRegs are the set of already allocated physical registers.
4471 ///
4472 void SelectionDAGLowering::
4473 GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, bool HasEarlyClobber,
4474                      std::set<unsigned> &OutputRegs, 
4475                      std::set<unsigned> &InputRegs) {
4476   // Compute whether this value requires an input register, an output register,
4477   // or both.
4478   bool isOutReg = false;
4479   bool isInReg = false;
4480   switch (OpInfo.Type) {
4481   case InlineAsm::isOutput:
4482     isOutReg = true;
4483     
4484     // If this is an early-clobber output, or if there is an input
4485     // constraint that matches this, we need to reserve the input register
4486     // so no other inputs allocate to it.
4487     isInReg = OpInfo.isEarlyClobber || OpInfo.hasMatchingInput;
4488     break;
4489   case InlineAsm::isInput:
4490     isInReg = true;
4491     isOutReg = false;
4492     break;
4493   case InlineAsm::isClobber:
4494     isOutReg = true;
4495     isInReg = true;
4496     break;
4497   }
4498   
4499   
4500   MachineFunction &MF = DAG.getMachineFunction();
4501   SmallVector<unsigned, 4> Regs;
4502   
4503   // If this is a constraint for a single physreg, or a constraint for a
4504   // register class, find it.
4505   std::pair<unsigned, const TargetRegisterClass*> PhysReg = 
4506     TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4507                                      OpInfo.ConstraintVT);
4508
4509   unsigned NumRegs = 1;
4510   if (OpInfo.ConstraintVT != MVT::Other)
4511     NumRegs = TLI.getNumRegisters(OpInfo.ConstraintVT);
4512   MVT RegVT;
4513   MVT ValueVT = OpInfo.ConstraintVT;
4514   
4515
4516   // If this is a constraint for a specific physical register, like {r17},
4517   // assign it now.
4518   if (PhysReg.first) {
4519     if (OpInfo.ConstraintVT == MVT::Other)
4520       ValueVT = *PhysReg.second->vt_begin();
4521     
4522     // Get the actual register value type.  This is important, because the user
4523     // may have asked for (e.g.) the AX register in i32 type.  We need to
4524     // remember that AX is actually i16 to get the right extension.
4525     RegVT = *PhysReg.second->vt_begin();
4526     
4527     // This is a explicit reference to a physical register.
4528     Regs.push_back(PhysReg.first);
4529
4530     // If this is an expanded reference, add the rest of the regs to Regs.
4531     if (NumRegs != 1) {
4532       TargetRegisterClass::iterator I = PhysReg.second->begin();
4533       for (; *I != PhysReg.first; ++I)
4534         assert(I != PhysReg.second->end() && "Didn't find reg!"); 
4535       
4536       // Already added the first reg.
4537       --NumRegs; ++I;
4538       for (; NumRegs; --NumRegs, ++I) {
4539         assert(I != PhysReg.second->end() && "Ran out of registers to allocate!");
4540         Regs.push_back(*I);
4541       }
4542     }
4543     OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4544     const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4545     OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4546     return;
4547   }
4548   
4549   // Otherwise, if this was a reference to an LLVM register class, create vregs
4550   // for this reference.
4551   std::vector<unsigned> RegClassRegs;
4552   const TargetRegisterClass *RC = PhysReg.second;
4553   if (RC) {
4554     // If this is an early clobber or tied register, our regalloc doesn't know
4555     // how to maintain the constraint.  If it isn't, go ahead and create vreg
4556     // and let the regalloc do the right thing.
4557     if (!OpInfo.hasMatchingInput && !OpInfo.isEarlyClobber &&
4558         // If there is some other early clobber and this is an input register,
4559         // then we are forced to pre-allocate the input reg so it doesn't
4560         // conflict with the earlyclobber.
4561         !(OpInfo.Type == InlineAsm::isInput && HasEarlyClobber)) {
4562       RegVT = *PhysReg.second->vt_begin();
4563       
4564       if (OpInfo.ConstraintVT == MVT::Other)
4565         ValueVT = RegVT;
4566
4567       // Create the appropriate number of virtual registers.
4568       MachineRegisterInfo &RegInfo = MF.getRegInfo();
4569       for (; NumRegs; --NumRegs)
4570         Regs.push_back(RegInfo.createVirtualRegister(PhysReg.second));
4571       
4572       OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4573       return;
4574     }
4575     
4576     // Otherwise, we can't allocate it.  Let the code below figure out how to
4577     // maintain these constraints.
4578     RegClassRegs.assign(PhysReg.second->begin(), PhysReg.second->end());
4579     
4580   } else {
4581     // This is a reference to a register class that doesn't directly correspond
4582     // to an LLVM register class.  Allocate NumRegs consecutive, available,
4583     // registers from the class.
4584     RegClassRegs = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
4585                                                          OpInfo.ConstraintVT);
4586   }
4587   
4588   const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4589   unsigned NumAllocated = 0;
4590   for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
4591     unsigned Reg = RegClassRegs[i];
4592     // See if this register is available.
4593     if ((isOutReg && OutputRegs.count(Reg)) ||   // Already used.
4594         (isInReg  && InputRegs.count(Reg))) {    // Already used.
4595       // Make sure we find consecutive registers.
4596       NumAllocated = 0;
4597       continue;
4598     }
4599     
4600     // Check to see if this register is allocatable (i.e. don't give out the
4601     // stack pointer).
4602     if (RC == 0) {
4603       RC = isAllocatableRegister(Reg, MF, TLI, TRI);
4604       if (!RC) {        // Couldn't allocate this register.
4605         // Reset NumAllocated to make sure we return consecutive registers.
4606         NumAllocated = 0;
4607         continue;
4608       }
4609     }
4610     
4611     // Okay, this register is good, we can use it.
4612     ++NumAllocated;
4613
4614     // If we allocated enough consecutive registers, succeed.
4615     if (NumAllocated == NumRegs) {
4616       unsigned RegStart = (i-NumAllocated)+1;
4617       unsigned RegEnd   = i+1;
4618       // Mark all of the allocated registers used.
4619       for (unsigned i = RegStart; i != RegEnd; ++i)
4620         Regs.push_back(RegClassRegs[i]);
4621       
4622       OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), 
4623                                          OpInfo.ConstraintVT);
4624       OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4625       return;
4626     }
4627   }
4628   
4629   // Otherwise, we couldn't allocate enough registers for this.
4630 }
4631
4632
4633 /// visitInlineAsm - Handle a call to an InlineAsm object.
4634 ///
4635 void SelectionDAGLowering::visitInlineAsm(CallSite CS) {
4636   InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
4637
4638   /// ConstraintOperands - Information about all of the constraints.
4639   std::vector<SDISelAsmOperandInfo> ConstraintOperands;
4640   
4641   SDValue Chain = getRoot();
4642   SDValue Flag;
4643   
4644   std::set<unsigned> OutputRegs, InputRegs;
4645
4646   // Do a prepass over the constraints, canonicalizing them, and building up the
4647   // ConstraintOperands list.
4648   std::vector<InlineAsm::ConstraintInfo>
4649     ConstraintInfos = IA->ParseConstraints();
4650
4651   // SawEarlyClobber - Keep track of whether we saw an earlyclobber output
4652   // constraint.  If so, we can't let the register allocator allocate any input
4653   // registers, because it will not know to avoid the earlyclobbered output reg.
4654   bool SawEarlyClobber = false;
4655   
4656   unsigned ArgNo = 0;   // ArgNo - The argument of the CallInst.
4657   unsigned ResNo = 0;   // ResNo - The result number of the next output.
4658   for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4659     ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
4660     SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
4661     
4662     MVT OpVT = MVT::Other;
4663
4664     // Compute the value type for each operand.
4665     switch (OpInfo.Type) {
4666     case InlineAsm::isOutput:
4667       // Indirect outputs just consume an argument.
4668       if (OpInfo.isIndirect) {
4669         OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4670         break;
4671       }
4672       // The return value of the call is this value.  As such, there is no
4673       // corresponding argument.
4674       assert(CS.getType() != Type::VoidTy && "Bad inline asm!");
4675       if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
4676         OpVT = TLI.getValueType(STy->getElementType(ResNo));
4677       } else {
4678         assert(ResNo == 0 && "Asm only has one result!");
4679         OpVT = TLI.getValueType(CS.getType());
4680       }
4681       ++ResNo;
4682       break;
4683     case InlineAsm::isInput:
4684       OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4685       break;
4686     case InlineAsm::isClobber:
4687       // Nothing to do.
4688       break;
4689     }
4690
4691     // If this is an input or an indirect output, process the call argument.
4692     // BasicBlocks are labels, currently appearing only in asm's.
4693     if (OpInfo.CallOperandVal) {
4694       if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal))
4695         OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
4696       else {
4697         OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
4698         const Type *OpTy = OpInfo.CallOperandVal->getType();
4699         // If this is an indirect operand, the operand is a pointer to the
4700         // accessed type.
4701         if (OpInfo.isIndirect)
4702           OpTy = cast<PointerType>(OpTy)->getElementType();
4703
4704         // If OpTy is not a single value, it may be a struct/union that we
4705         // can tile with integers.
4706         if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4707           unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4708           switch (BitSize) {
4709           default: break;
4710           case 1:
4711           case 8:
4712           case 16:
4713           case 32:
4714           case 64:
4715             OpTy = IntegerType::get(BitSize);
4716             break;
4717           }
4718         }
4719
4720         OpVT = TLI.getValueType(OpTy, true);
4721       }
4722     }
4723     
4724     OpInfo.ConstraintVT = OpVT;
4725     
4726     // Compute the constraint code and ConstraintType to use.
4727     TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
4728
4729     // Keep track of whether we see an earlyclobber.
4730     SawEarlyClobber |= OpInfo.isEarlyClobber;
4731     
4732     // If we see a clobber of a register, it is an early clobber.
4733     if (!SawEarlyClobber &&
4734         OpInfo.Type == InlineAsm::isClobber &&
4735         OpInfo.ConstraintType == TargetLowering::C_Register) {
4736       // Note that we want to ignore things that we don't track here, like
4737       // dirflag, fpsr, flags, etc.
4738       std::pair<unsigned, const TargetRegisterClass*> PhysReg = 
4739         TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4740                                          OpInfo.ConstraintVT);
4741       if (PhysReg.first || PhysReg.second) {
4742         // This is a register we know of.
4743         SawEarlyClobber = true;
4744       }
4745     }
4746     
4747     // If this is a memory input, and if the operand is not indirect, do what we
4748     // need to to provide an address for the memory input.
4749     if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
4750         !OpInfo.isIndirect) {
4751       assert(OpInfo.Type == InlineAsm::isInput &&
4752              "Can only indirectify direct input operands!");
4753       
4754       // Memory operands really want the address of the value.  If we don't have
4755       // an indirect input, put it in the constpool if we can, otherwise spill
4756       // it to a stack slot.
4757       
4758       // If the operand is a float, integer, or vector constant, spill to a
4759       // constant pool entry to get its address.
4760       Value *OpVal = OpInfo.CallOperandVal;
4761       if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
4762           isa<ConstantVector>(OpVal)) {
4763         OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
4764                                                  TLI.getPointerTy());
4765       } else {
4766         // Otherwise, create a stack slot and emit a store to it before the
4767         // asm.
4768         const Type *Ty = OpVal->getType();
4769         uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
4770         unsigned Align  = TLI.getTargetData()->getPrefTypeAlignment(Ty);
4771         MachineFunction &MF = DAG.getMachineFunction();
4772         int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align);
4773         SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4774         Chain = DAG.getStore(Chain, OpInfo.CallOperand, StackSlot, NULL, 0);
4775         OpInfo.CallOperand = StackSlot;
4776       }
4777      
4778       // There is no longer a Value* corresponding to this operand.
4779       OpInfo.CallOperandVal = 0;
4780       // It is now an indirect operand.
4781       OpInfo.isIndirect = true;
4782     }
4783     
4784     // If this constraint is for a specific register, allocate it before
4785     // anything else.
4786     if (OpInfo.ConstraintType == TargetLowering::C_Register)
4787       GetRegistersForValue(OpInfo, SawEarlyClobber, OutputRegs, InputRegs);
4788   }
4789   ConstraintInfos.clear();
4790   
4791   
4792   // Second pass - Loop over all of the operands, assigning virtual or physregs
4793   // to registerclass operands.
4794   for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
4795     SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
4796     
4797     // C_Register operands have already been allocated, Other/Memory don't need
4798     // to be.
4799     if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
4800       GetRegistersForValue(OpInfo, SawEarlyClobber, OutputRegs, InputRegs);
4801   }    
4802   
4803   // AsmNodeOperands - The operands for the ISD::INLINEASM node.
4804   std::vector<SDValue> AsmNodeOperands;
4805   AsmNodeOperands.push_back(SDValue());  // reserve space for input chain
4806   AsmNodeOperands.push_back(
4807           DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
4808   
4809   
4810   // Loop over all of the inputs, copying the operand values into the
4811   // appropriate registers and processing the output regs.
4812   RegsForValue RetValRegs;
4813  
4814   // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
4815   std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
4816   
4817   for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
4818     SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
4819
4820     switch (OpInfo.Type) {
4821     case InlineAsm::isOutput: {
4822       if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
4823           OpInfo.ConstraintType != TargetLowering::C_Register) {
4824         // Memory output, or 'other' output (e.g. 'X' constraint).
4825         assert(OpInfo.isIndirect && "Memory output must be indirect operand");
4826
4827         // Add information to the INLINEASM node to know about this output.
4828         unsigned ResOpType = SawEarlyClobber ? 
4829                                   7 /* MEM OVERLAPS EARLYCLOBBER */ :
4830                                   4/*MEM*/;
4831         AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType | (1<<3), 
4832                                                         TLI.getPointerTy()));
4833         AsmNodeOperands.push_back(OpInfo.CallOperand);
4834         break;
4835       }
4836
4837       // Otherwise, this is a register or register class output.
4838
4839       // Copy the output from the appropriate register.  Find a register that
4840       // we can use.
4841       if (OpInfo.AssignedRegs.Regs.empty()) {
4842         cerr << "Couldn't allocate output reg for constraint '"
4843              << OpInfo.ConstraintCode << "'!\n";
4844         exit(1);
4845       }
4846
4847       // If this is an indirect operand, store through the pointer after the
4848       // asm.
4849       if (OpInfo.isIndirect) {
4850         IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
4851                                                       OpInfo.CallOperandVal));
4852       } else {
4853         // This is the result value of the call.
4854         assert(CS.getType() != Type::VoidTy && "Bad inline asm!");
4855         // Concatenate this output onto the outputs list.
4856         RetValRegs.append(OpInfo.AssignedRegs);
4857       }
4858       
4859       // Add information to the INLINEASM node to know that this register is
4860       // set.
4861       OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
4862                                                6 /* EARLYCLOBBER REGDEF */ :
4863                                                2 /* REGDEF */ ,
4864                                                DAG, AsmNodeOperands);
4865       break;
4866     }
4867     case InlineAsm::isInput: {
4868       SDValue InOperandVal = OpInfo.CallOperand;
4869       
4870       if (isdigit(OpInfo.ConstraintCode[0])) {    // Matching constraint?
4871         // If this is required to match an output register we have already set,
4872         // just use its register.
4873         unsigned OperandNo = atoi(OpInfo.ConstraintCode.c_str());
4874         
4875         // Scan until we find the definition we already emitted of this operand.
4876         // When we find it, create a RegsForValue operand.
4877         unsigned CurOp = 2;  // The first operand.
4878         for (; OperandNo; --OperandNo) {
4879           // Advance to the next operand.
4880           unsigned NumOps = 
4881             cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
4882           assert(((NumOps & 7) == 2 /*REGDEF*/ ||
4883                   (NumOps & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
4884                   (NumOps & 7) == 4 /*MEM*/ ||
4885                   (NumOps & 7) == 7 /*MEM OVERLAPS EARLYCLOBBER*/) &&
4886                  "Skipped past definitions?");
4887           CurOp += (NumOps>>3)+1;
4888         }
4889
4890         unsigned NumOps = 
4891           cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
4892         if ((NumOps & 7) == 2 /*REGDEF*/ 
4893             || (NumOps & 7) == 6 /* EARLYCLOBBER REGDEF */) {
4894           // Add NumOps>>3 registers to MatchedRegs.
4895           RegsForValue MatchedRegs;
4896           MatchedRegs.TLI = &TLI;
4897           MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
4898           MatchedRegs.RegVTs.push_back(AsmNodeOperands[CurOp+1].getValueType());
4899           for (unsigned i = 0, e = NumOps>>3; i != e; ++i) {
4900             unsigned Reg =
4901               cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg();
4902             MatchedRegs.Regs.push_back(Reg);
4903           }
4904         
4905           // Use the produced MatchedRegs object to 
4906           MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag);
4907           MatchedRegs.AddInlineAsmOperands(SawEarlyClobber ? 
4908                                            1 /*REGUSE*/ :
4909                                            5 /*REGUSE OVERLAPS EARLYCLOBBER*/, 
4910                                            DAG, AsmNodeOperands);
4911           break;
4912         } else {
4913           assert(((NumOps & 7) == 7/*MEM OVERLAPS EARLYCLOBBER */ ||
4914                   (NumOps & 7) == 4) && "Unknown matching constraint!");
4915           assert((NumOps >> 3) == 1 && "Unexpected number of operands"); 
4916           // Add information to the INLINEASM node to know about this input.
4917           AsmNodeOperands.push_back(DAG.getTargetConstant(NumOps,
4918                                                           TLI.getPointerTy()));
4919           AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
4920           break;
4921         }
4922       }
4923       
4924       if (OpInfo.ConstraintType == TargetLowering::C_Other) {
4925         assert(!OpInfo.isIndirect && 
4926                "Don't know how to handle indirect other inputs yet!");
4927         
4928         std::vector<SDValue> Ops;
4929         TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
4930                                          Ops, DAG);
4931         if (Ops.empty()) {
4932           cerr << "Invalid operand for inline asm constraint '"
4933                << OpInfo.ConstraintCode << "'!\n";
4934           exit(1);
4935         }
4936         
4937         // Add information to the INLINEASM node to know about this input.
4938         unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
4939         AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, 
4940                                                         TLI.getPointerTy()));
4941         AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
4942         break;
4943       } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
4944         assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
4945         assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
4946                "Memory operands expect pointer values");
4947                
4948         // Add information to the INLINEASM node to know about this input.
4949         unsigned ResOpType = SawEarlyClobber ? 
4950                                 7 /* MEM OVERLAPS EARLYCLOBBER */ : 
4951                                 4/*MEM*/;
4952         AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType | (1<<3),
4953                                                         TLI.getPointerTy()));
4954         AsmNodeOperands.push_back(InOperandVal);
4955         break;
4956       }
4957         
4958       assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
4959               OpInfo.ConstraintType == TargetLowering::C_Register) &&
4960              "Unknown constraint type!");
4961       assert(!OpInfo.isIndirect && 
4962              "Don't know how to handle indirect register inputs yet!");
4963
4964       // Copy the input into the appropriate registers.
4965       assert(!OpInfo.AssignedRegs.Regs.empty() &&
4966              "Couldn't allocate input reg!");
4967
4968       OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag);
4969       
4970       OpInfo.AssignedRegs.AddInlineAsmOperands(SawEarlyClobber ?
4971                                            5 /*REGUSE OVERLAPS EARLYCLOBBER*/:
4972                                            1/*REGUSE*/,
4973                                            DAG, AsmNodeOperands);
4974       break;
4975     }
4976     case InlineAsm::isClobber: {
4977       // Add the clobbered value to the operand list, so that the register
4978       // allocator is aware that the physreg got clobbered.
4979       if (!OpInfo.AssignedRegs.Regs.empty())
4980         OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
4981                                                  DAG, AsmNodeOperands);
4982       break;
4983     }
4984     }
4985   }
4986   
4987   // Finish up input operands.
4988   AsmNodeOperands[0] = Chain;
4989   if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
4990   
4991   Chain = DAG.getNode(ISD::INLINEASM, 
4992                       DAG.getNodeValueTypes(MVT::Other, MVT::Flag), 2,
4993                       &AsmNodeOperands[0], AsmNodeOperands.size());
4994   Flag = Chain.getValue(1);
4995
4996   // If this asm returns a register value, copy the result from that register
4997   // and set it as the value of the call.
4998   if (!RetValRegs.Regs.empty()) {
4999     SDValue Val = RetValRegs.getCopyFromRegs(DAG, Chain, &Flag);
5000
5001     // If any of the results of the inline asm is a vector, it may have the
5002     // wrong width/num elts.  This can happen for register classes that can
5003     // contain multiple different value types.  The preg or vreg allocated may
5004     // not have the same VT as was expected.  Convert it to the right type with
5005     // bit_convert.
5006     if (const StructType *ResSTy = dyn_cast<StructType>(CS.getType())) {
5007       for (unsigned i = 0, e = ResSTy->getNumElements(); i != e; ++i) {
5008         if (Val.getNode()->getValueType(i).isVector())
5009           Val = DAG.getNode(ISD::BIT_CONVERT,
5010                             TLI.getValueType(ResSTy->getElementType(i)), Val);
5011       }
5012     } else {
5013       if (Val.getValueType().isVector())
5014         Val = DAG.getNode(ISD::BIT_CONVERT, TLI.getValueType(CS.getType()),
5015                           Val);
5016     }
5017
5018     setValue(CS.getInstruction(), Val);
5019   }
5020   
5021   std::vector<std::pair<SDValue, Value*> > StoresToEmit;
5022   
5023   // Process indirect outputs, first output all of the flagged copies out of
5024   // physregs.
5025   for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5026     RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5027     Value *Ptr = IndirectStoresToEmit[i].second;
5028     SDValue OutVal = OutRegs.getCopyFromRegs(DAG, Chain, &Flag);
5029     StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5030   }
5031   
5032   // Emit the non-flagged stores from the physregs.
5033   SmallVector<SDValue, 8> OutChains;
5034   for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
5035     OutChains.push_back(DAG.getStore(Chain, StoresToEmit[i].first,
5036                                     getValue(StoresToEmit[i].second),
5037                                     StoresToEmit[i].second, 0));
5038   if (!OutChains.empty())
5039     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5040                         &OutChains[0], OutChains.size());
5041   DAG.setRoot(Chain);
5042 }
5043
5044
5045 void SelectionDAGLowering::visitMalloc(MallocInst &I) {
5046   SDValue Src = getValue(I.getOperand(0));
5047
5048   MVT IntPtr = TLI.getPointerTy();
5049
5050   if (IntPtr.bitsLT(Src.getValueType()))
5051     Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src);
5052   else if (IntPtr.bitsGT(Src.getValueType()))
5053     Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
5054
5055   // Scale the source by the type size.
5056   uint64_t ElementSize = TD->getABITypeSize(I.getType()->getElementType());
5057   Src = DAG.getNode(ISD::MUL, Src.getValueType(),
5058                     Src, DAG.getIntPtrConstant(ElementSize));
5059
5060   TargetLowering::ArgListTy Args;
5061   TargetLowering::ArgListEntry Entry;
5062   Entry.Node = Src;
5063   Entry.Ty = TLI.getTargetData()->getIntPtrType();
5064   Args.push_back(Entry);
5065
5066   std::pair<SDValue,SDValue> Result =
5067     TLI.LowerCallTo(getRoot(), I.getType(), false, false, false, CallingConv::C,
5068                     PerformTailCallOpt, DAG.getExternalSymbol("malloc", IntPtr),
5069                     Args, DAG);
5070   setValue(&I, Result.first);  // Pointers always fit in registers
5071   DAG.setRoot(Result.second);
5072 }
5073
5074 void SelectionDAGLowering::visitFree(FreeInst &I) {
5075   TargetLowering::ArgListTy Args;
5076   TargetLowering::ArgListEntry Entry;
5077   Entry.Node = getValue(I.getOperand(0));
5078   Entry.Ty = TLI.getTargetData()->getIntPtrType();
5079   Args.push_back(Entry);
5080   MVT IntPtr = TLI.getPointerTy();
5081   std::pair<SDValue,SDValue> Result =
5082     TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, false,
5083                     CallingConv::C, PerformTailCallOpt,
5084                     DAG.getExternalSymbol("free", IntPtr), Args, DAG);
5085   DAG.setRoot(Result.second);
5086 }
5087
5088 void SelectionDAGLowering::visitVAStart(CallInst &I) {
5089   DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(), 
5090                           getValue(I.getOperand(1)), 
5091                           DAG.getSrcValue(I.getOperand(1))));
5092 }
5093
5094 void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
5095   SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(),
5096                              getValue(I.getOperand(0)),
5097                              DAG.getSrcValue(I.getOperand(0)));
5098   setValue(&I, V);
5099   DAG.setRoot(V.getValue(1));
5100 }
5101
5102 void SelectionDAGLowering::visitVAEnd(CallInst &I) {
5103   DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(),
5104                           getValue(I.getOperand(1)), 
5105                           DAG.getSrcValue(I.getOperand(1))));
5106 }
5107
5108 void SelectionDAGLowering::visitVACopy(CallInst &I) {
5109   DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(), 
5110                           getValue(I.getOperand(1)), 
5111                           getValue(I.getOperand(2)),
5112                           DAG.getSrcValue(I.getOperand(1)),
5113                           DAG.getSrcValue(I.getOperand(2))));
5114 }
5115
5116 /// TargetLowering::LowerArguments - This is the default LowerArguments
5117 /// implementation, which just inserts a FORMAL_ARGUMENTS node.  FIXME: When all
5118 /// targets are migrated to using FORMAL_ARGUMENTS, this hook should be 
5119 /// integrated into SDISel.
5120 void TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG,
5121                                     SmallVectorImpl<SDValue> &ArgValues) {
5122   // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node.
5123   SmallVector<SDValue, 3+16> Ops;
5124   Ops.push_back(DAG.getRoot());
5125   Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy()));
5126   Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy()));
5127
5128   // Add one result value for each formal argument.
5129   SmallVector<MVT, 16> RetVals;
5130   unsigned j = 1;
5131   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5132        I != E; ++I, ++j) {
5133     SmallVector<MVT, 4> ValueVTs;
5134     ComputeValueVTs(*this, I->getType(), ValueVTs);
5135     for (unsigned Value = 0, NumValues = ValueVTs.size();
5136          Value != NumValues; ++Value) {
5137       MVT VT = ValueVTs[Value];
5138       const Type *ArgTy = VT.getTypeForMVT();
5139       ISD::ArgFlagsTy Flags;
5140       unsigned OriginalAlignment =
5141         getTargetData()->getABITypeAlignment(ArgTy);
5142
5143       if (F.paramHasAttr(j, ParamAttr::ZExt))
5144         Flags.setZExt();
5145       if (F.paramHasAttr(j, ParamAttr::SExt))
5146         Flags.setSExt();
5147       if (F.paramHasAttr(j, ParamAttr::InReg))
5148         Flags.setInReg();
5149       if (F.paramHasAttr(j, ParamAttr::StructRet))
5150         Flags.setSRet();
5151       if (F.paramHasAttr(j, ParamAttr::ByVal)) {
5152         Flags.setByVal();
5153         const PointerType *Ty = cast<PointerType>(I->getType());
5154         const Type *ElementTy = Ty->getElementType();
5155         unsigned FrameAlign = getByValTypeAlignment(ElementTy);
5156         unsigned FrameSize  = getTargetData()->getABITypeSize(ElementTy);
5157         // For ByVal, alignment should be passed from FE.  BE will guess if
5158         // this info is not there but there are cases it cannot get right.
5159         if (F.getParamAlignment(j))
5160           FrameAlign = F.getParamAlignment(j);
5161         Flags.setByValAlign(FrameAlign);
5162         Flags.setByValSize(FrameSize);
5163       }
5164       if (F.paramHasAttr(j, ParamAttr::Nest))
5165         Flags.setNest();
5166       Flags.setOrigAlign(OriginalAlignment);
5167
5168       MVT RegisterVT = getRegisterType(VT);
5169       unsigned NumRegs = getNumRegisters(VT);
5170       for (unsigned i = 0; i != NumRegs; ++i) {
5171         RetVals.push_back(RegisterVT);
5172         ISD::ArgFlagsTy MyFlags = Flags;
5173         if (NumRegs > 1 && i == 0)
5174           MyFlags.setSplit();
5175         // if it isn't first piece, alignment must be 1
5176         else if (i > 0)
5177           MyFlags.setOrigAlign(1);
5178         Ops.push_back(DAG.getArgFlags(MyFlags));
5179       }
5180     }
5181   }
5182
5183   RetVals.push_back(MVT::Other);
5184   
5185   // Create the node.
5186   SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS,
5187                                DAG.getVTList(&RetVals[0], RetVals.size()),
5188                                &Ops[0], Ops.size()).getNode();
5189   
5190   // Prelower FORMAL_ARGUMENTS.  This isn't required for functionality, but
5191   // allows exposing the loads that may be part of the argument access to the
5192   // first DAGCombiner pass.
5193   SDValue TmpRes = LowerOperation(SDValue(Result, 0), DAG);
5194   
5195   // The number of results should match up, except that the lowered one may have
5196   // an extra flag result.
5197   assert((Result->getNumValues() == TmpRes.getNode()->getNumValues() ||
5198           (Result->getNumValues()+1 == TmpRes.getNode()->getNumValues() &&
5199            TmpRes.getValue(Result->getNumValues()).getValueType() == MVT::Flag))
5200          && "Lowering produced unexpected number of results!");
5201
5202   // The FORMAL_ARGUMENTS node itself is likely no longer needed.
5203   if (Result != TmpRes.getNode() && Result->use_empty()) {
5204     HandleSDNode Dummy(DAG.getRoot());
5205     DAG.RemoveDeadNode(Result);
5206   }
5207
5208   Result = TmpRes.getNode();
5209   
5210   unsigned NumArgRegs = Result->getNumValues() - 1;
5211   DAG.setRoot(SDValue(Result, NumArgRegs));
5212
5213   // Set up the return result vector.
5214   unsigned i = 0;
5215   unsigned Idx = 1;
5216   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; 
5217       ++I, ++Idx) {
5218     SmallVector<MVT, 4> ValueVTs;
5219     ComputeValueVTs(*this, I->getType(), ValueVTs);
5220     for (unsigned Value = 0, NumValues = ValueVTs.size();
5221          Value != NumValues; ++Value) {
5222       MVT VT = ValueVTs[Value];
5223       MVT PartVT = getRegisterType(VT);
5224
5225       unsigned NumParts = getNumRegisters(VT);
5226       SmallVector<SDValue, 4> Parts(NumParts);
5227       for (unsigned j = 0; j != NumParts; ++j)
5228         Parts[j] = SDValue(Result, i++);
5229
5230       ISD::NodeType AssertOp = ISD::DELETED_NODE;
5231       if (F.paramHasAttr(Idx, ParamAttr::SExt))
5232         AssertOp = ISD::AssertSext;
5233       else if (F.paramHasAttr(Idx, ParamAttr::ZExt))
5234         AssertOp = ISD::AssertZext;
5235
5236       ArgValues.push_back(getCopyFromParts(DAG, &Parts[0], NumParts, PartVT, VT,
5237                                            AssertOp));
5238     }
5239   }
5240   assert(i == NumArgRegs && "Argument register count mismatch!");
5241 }
5242
5243
5244 /// TargetLowering::LowerCallTo - This is the default LowerCallTo
5245 /// implementation, which just inserts an ISD::CALL node, which is later custom
5246 /// lowered by the target to something concrete.  FIXME: When all targets are
5247 /// migrated to using ISD::CALL, this hook should be integrated into SDISel.
5248 std::pair<SDValue, SDValue>
5249 TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5250                             bool RetSExt, bool RetZExt, bool isVarArg,
5251                             unsigned CallingConv, bool isTailCall,
5252                             SDValue Callee,
5253                             ArgListTy &Args, SelectionDAG &DAG) {
5254   assert((!isTailCall || PerformTailCallOpt) &&
5255          "isTailCall set when tail-call optimizations are disabled!");
5256
5257   SmallVector<SDValue, 32> Ops;
5258   Ops.push_back(Chain);   // Op#0 - Chain
5259   Ops.push_back(Callee);
5260   
5261   // Handle all of the outgoing arguments.
5262   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
5263     SmallVector<MVT, 4> ValueVTs;
5264     ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5265     for (unsigned Value = 0, NumValues = ValueVTs.size();
5266          Value != NumValues; ++Value) {
5267       MVT VT = ValueVTs[Value];
5268       const Type *ArgTy = VT.getTypeForMVT();
5269       SDValue Op = SDValue(Args[i].Node.getNode(), Args[i].Node.getResNo() + Value);
5270       ISD::ArgFlagsTy Flags;
5271       unsigned OriginalAlignment =
5272         getTargetData()->getABITypeAlignment(ArgTy);
5273
5274       if (Args[i].isZExt)
5275         Flags.setZExt();
5276       if (Args[i].isSExt)
5277         Flags.setSExt();
5278       if (Args[i].isInReg)
5279         Flags.setInReg();
5280       if (Args[i].isSRet)
5281         Flags.setSRet();
5282       if (Args[i].isByVal) {
5283         Flags.setByVal();
5284         const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5285         const Type *ElementTy = Ty->getElementType();
5286         unsigned FrameAlign = getByValTypeAlignment(ElementTy);
5287         unsigned FrameSize  = getTargetData()->getABITypeSize(ElementTy);
5288         // For ByVal, alignment should come from FE.  BE will guess if this
5289         // info is not there but there are cases it cannot get right.
5290         if (Args[i].Alignment)
5291           FrameAlign = Args[i].Alignment;
5292         Flags.setByValAlign(FrameAlign);
5293         Flags.setByValSize(FrameSize);
5294       }
5295       if (Args[i].isNest)
5296         Flags.setNest();
5297       Flags.setOrigAlign(OriginalAlignment);
5298
5299       MVT PartVT = getRegisterType(VT);
5300       unsigned NumParts = getNumRegisters(VT);
5301       SmallVector<SDValue, 4> Parts(NumParts);
5302       ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5303
5304       if (Args[i].isSExt)
5305         ExtendKind = ISD::SIGN_EXTEND;
5306       else if (Args[i].isZExt)
5307         ExtendKind = ISD::ZERO_EXTEND;
5308
5309       getCopyToParts(DAG, Op, &Parts[0], NumParts, PartVT, ExtendKind);
5310
5311       for (unsigned i = 0; i != NumParts; ++i) {
5312         // if it isn't first piece, alignment must be 1
5313         ISD::ArgFlagsTy MyFlags = Flags;
5314         if (NumParts > 1 && i == 0)
5315           MyFlags.setSplit();
5316         else if (i != 0)
5317           MyFlags.setOrigAlign(1);
5318
5319         Ops.push_back(Parts[i]);
5320         Ops.push_back(DAG.getArgFlags(MyFlags));
5321       }
5322     }
5323   }
5324   
5325   // Figure out the result value types. We start by making a list of
5326   // the potentially illegal return value types.
5327   SmallVector<MVT, 4> LoweredRetTys;
5328   SmallVector<MVT, 4> RetTys;
5329   ComputeValueVTs(*this, RetTy, RetTys);
5330
5331   // Then we translate that to a list of legal types.
5332   for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
5333     MVT VT = RetTys[I];
5334     MVT RegisterVT = getRegisterType(VT);
5335     unsigned NumRegs = getNumRegisters(VT);
5336     for (unsigned i = 0; i != NumRegs; ++i)
5337       LoweredRetTys.push_back(RegisterVT);
5338   }
5339   
5340   LoweredRetTys.push_back(MVT::Other);  // Always has a chain.
5341   
5342   // Create the CALL node.
5343   SDValue Res = DAG.getCall(CallingConv, isVarArg, isTailCall,
5344                             DAG.getVTList(&LoweredRetTys[0],
5345                                           LoweredRetTys.size()),
5346                             &Ops[0], Ops.size());
5347   Chain = Res.getValue(LoweredRetTys.size() - 1);
5348
5349   // Gather up the call result into a single value.
5350   if (RetTy != Type::VoidTy) {
5351     ISD::NodeType AssertOp = ISD::DELETED_NODE;
5352
5353     if (RetSExt)
5354       AssertOp = ISD::AssertSext;
5355     else if (RetZExt)
5356       AssertOp = ISD::AssertZext;
5357
5358     SmallVector<SDValue, 4> ReturnValues;
5359     unsigned RegNo = 0;
5360     for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
5361       MVT VT = RetTys[I];
5362       MVT RegisterVT = getRegisterType(VT);
5363       unsigned NumRegs = getNumRegisters(VT);
5364       unsigned RegNoEnd = NumRegs + RegNo;
5365       SmallVector<SDValue, 4> Results;
5366       for (; RegNo != RegNoEnd; ++RegNo)
5367         Results.push_back(Res.getValue(RegNo));
5368       SDValue ReturnValue =
5369         getCopyFromParts(DAG, &Results[0], NumRegs, RegisterVT, VT,
5370                          AssertOp);
5371       ReturnValues.push_back(ReturnValue);
5372     }
5373     Res = DAG.getMergeValues(DAG.getVTList(&RetTys[0], RetTys.size()),
5374                              &ReturnValues[0], ReturnValues.size());
5375   }
5376
5377   return std::make_pair(Res, Chain);
5378 }
5379
5380 SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
5381   assert(0 && "LowerOperation not implemented for this target!");
5382   abort();
5383   return SDValue();
5384 }
5385
5386
5387 void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
5388   SDValue Op = getValue(V);
5389   assert((Op.getOpcode() != ISD::CopyFromReg ||
5390           cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5391          "Copy from a reg to the same reg!");
5392   assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5393
5394   RegsForValue RFV(TLI, Reg, V->getType());
5395   SDValue Chain = DAG.getEntryNode();
5396   RFV.getCopyToRegs(Op, DAG, Chain, 0);
5397   PendingExports.push_back(Chain);
5398 }
5399
5400 #include "llvm/CodeGen/SelectionDAGISel.h"
5401
5402 void SelectionDAGISel::
5403 LowerArguments(BasicBlock *LLVMBB) {
5404   // If this is the entry block, emit arguments.
5405   Function &F = *LLVMBB->getParent();
5406   SDValue OldRoot = SDL->DAG.getRoot();
5407   SmallVector<SDValue, 16> Args;
5408   TLI.LowerArguments(F, SDL->DAG, Args);
5409
5410   unsigned a = 0;
5411   for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
5412        AI != E; ++AI) {
5413     SmallVector<MVT, 4> ValueVTs;
5414     ComputeValueVTs(TLI, AI->getType(), ValueVTs);
5415     unsigned NumValues = ValueVTs.size();
5416     if (!AI->use_empty()) {
5417       SDL->setValue(AI, SDL->DAG.getMergeValues(&Args[a], NumValues));
5418       // If this argument is live outside of the entry block, insert a copy from
5419       // whereever we got it to the vreg that other BB's will reference it as.
5420       DenseMap<const Value*, unsigned>::iterator VMI=FuncInfo->ValueMap.find(AI);
5421       if (VMI != FuncInfo->ValueMap.end()) {
5422         SDL->CopyValueToVirtualRegister(AI, VMI->second);
5423       }
5424     }
5425     a += NumValues;
5426   }
5427
5428   // Finally, if the target has anything special to do, allow it to do so.
5429   // FIXME: this should insert code into the DAG!
5430   EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction());
5431 }
5432
5433 /// Handle PHI nodes in successor blocks.  Emit code into the SelectionDAG to
5434 /// ensure constants are generated when needed.  Remember the virtual registers
5435 /// that need to be added to the Machine PHI nodes as input.  We cannot just
5436 /// directly add them, because expansion might result in multiple MBB's for one
5437 /// BB.  As such, the start of the BB might correspond to a different MBB than
5438 /// the end.
5439 ///
5440 void
5441 SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5442   TerminatorInst *TI = LLVMBB->getTerminator();
5443
5444   SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5445
5446   // Check successor nodes' PHI nodes that expect a constant to be available
5447   // from this block.
5448   for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5449     BasicBlock *SuccBB = TI->getSuccessor(succ);
5450     if (!isa<PHINode>(SuccBB->begin())) continue;
5451     MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
5452     
5453     // If this terminator has multiple identical successors (common for
5454     // switches), only handle each succ once.
5455     if (!SuccsHandled.insert(SuccMBB)) continue;
5456     
5457     MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5458     PHINode *PN;
5459
5460     // At this point we know that there is a 1-1 correspondence between LLVM PHI
5461     // nodes and Machine PHI nodes, but the incoming operands have not been
5462     // emitted yet.
5463     for (BasicBlock::iterator I = SuccBB->begin();
5464          (PN = dyn_cast<PHINode>(I)); ++I) {
5465       // Ignore dead phi's.
5466       if (PN->use_empty()) continue;
5467
5468       unsigned Reg;
5469       Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5470
5471       if (Constant *C = dyn_cast<Constant>(PHIOp)) {
5472         unsigned &RegOut = SDL->ConstantsOut[C];
5473         if (RegOut == 0) {
5474           RegOut = FuncInfo->CreateRegForValue(C);
5475           SDL->CopyValueToVirtualRegister(C, RegOut);
5476         }
5477         Reg = RegOut;
5478       } else {
5479         Reg = FuncInfo->ValueMap[PHIOp];
5480         if (Reg == 0) {
5481           assert(isa<AllocaInst>(PHIOp) &&
5482                  FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5483                  "Didn't codegen value into a register!??");
5484           Reg = FuncInfo->CreateRegForValue(PHIOp);
5485           SDL->CopyValueToVirtualRegister(PHIOp, Reg);
5486         }
5487       }
5488
5489       // Remember that this register needs to added to the machine PHI node as
5490       // the input for this MBB.
5491       SmallVector<MVT, 4> ValueVTs;
5492       ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5493       for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
5494         MVT VT = ValueVTs[vti];
5495         unsigned NumRegisters = TLI.getNumRegisters(VT);
5496         for (unsigned i = 0, e = NumRegisters; i != e; ++i)
5497           SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
5498         Reg += NumRegisters;
5499       }
5500     }
5501   }
5502   SDL->ConstantsOut.clear();
5503 }
5504
5505 /// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
5506 /// supports legal types, and it emits MachineInstrs directly instead of
5507 /// creating SelectionDAG nodes.
5508 ///
5509 bool
5510 SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
5511                                                       FastISel *F) {
5512   TerminatorInst *TI = LLVMBB->getTerminator();
5513
5514   SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5515   unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size();
5516
5517   // Check successor nodes' PHI nodes that expect a constant to be available
5518   // from this block.
5519   for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5520     BasicBlock *SuccBB = TI->getSuccessor(succ);
5521     if (!isa<PHINode>(SuccBB->begin())) continue;
5522     MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
5523     
5524     // If this terminator has multiple identical successors (common for
5525     // switches), only handle each succ once.
5526     if (!SuccsHandled.insert(SuccMBB)) continue;
5527     
5528     MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5529     PHINode *PN;
5530
5531     // At this point we know that there is a 1-1 correspondence between LLVM PHI
5532     // nodes and Machine PHI nodes, but the incoming operands have not been
5533     // emitted yet.
5534     for (BasicBlock::iterator I = SuccBB->begin();
5535          (PN = dyn_cast<PHINode>(I)); ++I) {
5536       // Ignore dead phi's.
5537       if (PN->use_empty()) continue;
5538
5539       // Only handle legal types. Two interesting things to note here. First,
5540       // by bailing out early, we may leave behind some dead instructions,
5541       // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
5542       // own moves. Second, this check is necessary becuase FastISel doesn't
5543       // use CreateRegForValue to create registers, so it always creates
5544       // exactly one register for each non-void instruction.
5545       MVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
5546       if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
5547         // Promote MVT::i1.
5548         if (VT == MVT::i1)
5549           VT = TLI.getTypeToTransformTo(VT);
5550         else {
5551           SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
5552           return false;
5553         }
5554       }
5555
5556       Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5557
5558       unsigned Reg = F->getRegForValue(PHIOp);
5559       if (Reg == 0) {
5560         SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
5561         return false;
5562       }
5563       SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
5564     }
5565   }
5566
5567   return true;
5568 }