LLVM API Documentation

LegalizeDAG.cpp

Go to the documentation of this file.
00001 //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the SelectionDAG::Legalize method.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/CodeGen/SelectionDAG.h"
00015 #include "llvm/CodeGen/MachineFunction.h"
00016 #include "llvm/CodeGen/MachineFrameInfo.h"
00017 #include "llvm/CodeGen/MachineJumpTableInfo.h"
00018 #include "llvm/CodeGen/MachineModuleInfo.h"
00019 #include "llvm/CodeGen/DwarfWriter.h"
00020 #include "llvm/Analysis/DebugInfo.h"
00021 #include "llvm/CodeGen/PseudoSourceValue.h"
00022 #include "llvm/Target/TargetFrameInfo.h"
00023 #include "llvm/Target/TargetLowering.h"
00024 #include "llvm/Target/TargetData.h"
00025 #include "llvm/Target/TargetMachine.h"
00026 #include "llvm/Target/TargetOptions.h"
00027 #include "llvm/Target/TargetSubtarget.h"
00028 #include "llvm/CallingConv.h"
00029 #include "llvm/Constants.h"
00030 #include "llvm/DerivedTypes.h"
00031 #include "llvm/GlobalVariable.h"
00032 #include "llvm/Support/CommandLine.h"
00033 #include "llvm/Support/Compiler.h"
00034 #include "llvm/Support/MathExtras.h"
00035 #include "llvm/ADT/DenseMap.h"
00036 #include "llvm/ADT/SmallVector.h"
00037 #include "llvm/ADT/SmallPtrSet.h"
00038 #include <map>
00039 using namespace llvm;
00040 
00041 //===----------------------------------------------------------------------===//
00042 /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
00043 /// hacks on it until the target machine can handle it.  This involves
00044 /// eliminating value sizes the machine cannot handle (promoting small sizes to
00045 /// large sizes or splitting up large values into small values) as well as
00046 /// eliminating operations the machine cannot handle.
00047 ///
00048 /// This code also does a small amount of optimization and recognition of idioms
00049 /// as part of its processing.  For example, if a target does not support a
00050 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
00051 /// will attempt merge setcc and brc instructions into brcc's.
00052 ///
00053 namespace {
00054 class VISIBILITY_HIDDEN SelectionDAGLegalize {
00055   TargetLowering &TLI;
00056   SelectionDAG &DAG;
00057   bool TypesNeedLegalizing;
00058 
00059   // Libcall insertion helpers.
00060   
00061   /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
00062   /// legalized.  We use this to ensure that calls are properly serialized
00063   /// against each other, including inserted libcalls.
00064   SDValue LastCALLSEQ_END;
00065   
00066   /// IsLegalizingCall - This member is used *only* for purposes of providing
00067   /// helpful assertions that a libcall isn't created while another call is 
00068   /// being legalized (which could lead to non-serialized call sequences).
00069   bool IsLegalizingCall;
00070   
00071   enum LegalizeAction {
00072     Legal,      // The target natively supports this operation.
00073     Promote,    // This operation should be executed in a larger type.
00074     Expand      // Try to expand this to other ops, otherwise use a libcall.
00075   };
00076   
00077   /// ValueTypeActions - This is a bitvector that contains two bits for each
00078   /// value type, where the two bits correspond to the LegalizeAction enum.
00079   /// This can be queried with "getTypeAction(VT)".
00080   TargetLowering::ValueTypeActionImpl ValueTypeActions;
00081 
00082   /// LegalizedNodes - For nodes that are of legal width, and that have more
00083   /// than one use, this map indicates what regularized operand to use.  This
00084   /// allows us to avoid legalizing the same thing more than once.
00085   DenseMap<SDValue, SDValue> LegalizedNodes;
00086 
00087   /// PromotedNodes - For nodes that are below legal width, and that have more
00088   /// than one use, this map indicates what promoted value to use.  This allows
00089   /// us to avoid promoting the same thing more than once.
00090   DenseMap<SDValue, SDValue> PromotedNodes;
00091 
00092   /// ExpandedNodes - For nodes that need to be expanded this map indicates
00093   /// which operands are the expanded version of the input.  This allows
00094   /// us to avoid expanding the same node more than once.
00095   DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
00096 
00097   /// SplitNodes - For vector nodes that need to be split, this map indicates
00098   /// which operands are the split version of the input.  This allows us
00099   /// to avoid splitting the same node more than once.
00100   std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
00101   
00102   /// ScalarizedNodes - For nodes that need to be converted from vector types to
00103   /// scalar types, this contains the mapping of ones we have already
00104   /// processed to the result.
00105   std::map<SDValue, SDValue> ScalarizedNodes;
00106   
00107   /// WidenNodes - For nodes that need to be widened from one vector type to
00108   /// another, this contains the mapping of those that we have already widen.
00109   /// This allows us to avoid widening more than once.
00110   std::map<SDValue, SDValue> WidenNodes;
00111 
00112   void AddLegalizedOperand(SDValue From, SDValue To) {
00113     LegalizedNodes.insert(std::make_pair(From, To));
00114     // If someone requests legalization of the new node, return itself.
00115     if (From != To)
00116       LegalizedNodes.insert(std::make_pair(To, To));
00117   }
00118   void AddPromotedOperand(SDValue From, SDValue To) {
00119     bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
00120     assert(isNew && "Got into the map somehow?");
00121     isNew = isNew;
00122     // If someone requests legalization of the new node, return itself.
00123     LegalizedNodes.insert(std::make_pair(To, To));
00124   }
00125   void AddWidenedOperand(SDValue From, SDValue To) {
00126     bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
00127     assert(isNew && "Got into the map somehow?");
00128     isNew = isNew;
00129     // If someone requests legalization of the new node, return itself.
00130     LegalizedNodes.insert(std::make_pair(To, To));
00131   }
00132 
00133 public:
00134   explicit SelectionDAGLegalize(SelectionDAG &DAG, bool TypesNeedLegalizing);
00135 
00136   /// getTypeAction - Return how we should legalize values of this type, either
00137   /// it is already legal or we need to expand it into multiple registers of
00138   /// smaller integer type, or we need to promote it to a larger type.
00139   LegalizeAction getTypeAction(MVT VT) const {
00140     return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
00141   }
00142 
00143   /// isTypeLegal - Return true if this type is legal on this target.
00144   ///
00145   bool isTypeLegal(MVT VT) const {
00146     return getTypeAction(VT) == Legal;
00147   }
00148 
00149   void LegalizeDAG();
00150 
00151 private:
00152   /// HandleOp - Legalize, Promote, or Expand the specified operand as
00153   /// appropriate for its type.
00154   void HandleOp(SDValue Op);
00155     
00156   /// LegalizeOp - We know that the specified value has a legal type.
00157   /// Recursively ensure that the operands have legal types, then return the
00158   /// result.
00159   SDValue LegalizeOp(SDValue O);
00160   
00161   /// UnrollVectorOp - We know that the given vector has a legal type, however
00162   /// the operation it performs is not legal and is an operation that we have
00163   /// no way of lowering.  "Unroll" the vector, splitting out the scalars and
00164   /// operating on each element individually.
00165   SDValue UnrollVectorOp(SDValue O);
00166   
00167   /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
00168   /// insertion index for the INSERT_VECTOR_ELT instruction.  In this case, it
00169   /// is necessary to spill the vector being inserted into to memory, perform
00170   /// the insert there, and then read the result back.
00171   SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
00172                                            SDValue Idx, DebugLoc dl);
00173 
00174   /// PromoteOp - Given an operation that produces a value in an invalid type,
00175   /// promote it to compute the value into a larger type.  The produced value
00176   /// will have the correct bits for the low portion of the register, but no
00177   /// guarantee is made about the top bits: it may be zero, sign-extended, or
00178   /// garbage.
00179   SDValue PromoteOp(SDValue O);
00180 
00181   /// ExpandOp - Expand the specified SDValue into its two component pieces
00182   /// Lo&Hi.  Note that the Op MUST be an expanded type.  As a result of this,
00183   /// the LegalizedNodes map is filled in for any results that are not expanded,
00184   /// the ExpandedNodes map is filled in for any results that are expanded, and
00185   /// the Lo/Hi values are returned.   This applies to integer types and Vector
00186   /// types.
00187   void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
00188 
00189   /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT 
00190   /// (e.g., v3i32 to v4i32).  The produced value will have the correct value
00191   /// for the existing elements but no guarantee is made about the new elements
00192   /// at the end of the vector: it may be zero, ones, or garbage. This is useful
00193   /// when we have an instruction operating on an illegal vector type and we
00194   /// want to widen it to do the computation on a legal wider vector type.
00195   SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
00196 
00197   /// SplitVectorOp - Given an operand of vector type, break it down into
00198   /// two smaller values.
00199   void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
00200   
00201   /// ScalarizeVectorOp - Given an operand of single-element vector type
00202   /// (e.g. v1f32), convert it into the equivalent operation that returns a
00203   /// scalar (e.g. f32) value.
00204   SDValue ScalarizeVectorOp(SDValue O);
00205   
00206   /// Useful 16 element vector type that is used to pass operands for widening.
00207   typedef SmallVector<SDValue, 16> SDValueVector;  
00208   
00209   /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
00210   /// the LdChain contains a single load and false if it contains a token
00211   /// factor for multiple loads. It takes
00212   ///   Result:  location to return the result
00213   ///   LdChain: location to return the load chain
00214   ///   Op:      load operation to widen
00215   ///   NVT:     widen vector result type we want for the load
00216   bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain, 
00217                          SDValue Op, MVT NVT);
00218                         
00219   /// Helper genWidenVectorLoads - Helper function to generate a set of
00220   /// loads to load a vector with a resulting wider type. It takes
00221   ///   LdChain: list of chains for the load we have generated
00222   ///   Chain:   incoming chain for the ld vector
00223   ///   BasePtr: base pointer to load from
00224   ///   SV:      memory disambiguation source value
00225   ///   SVOffset:  memory disambiugation offset
00226   ///   Alignment: alignment of the memory
00227   ///   isVolatile: volatile load
00228   ///   LdWidth:    width of memory that we want to load 
00229   ///   ResType:    the wider result result type for the resulting loaded vector
00230   SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
00231                                 SDValue BasePtr, const Value *SV,
00232                                 int SVOffset, unsigned Alignment,
00233                                 bool isVolatile, unsigned LdWidth,
00234                                 MVT ResType, DebugLoc dl);
00235   
00236   /// StoreWidenVectorOp - Stores a widen vector into non widen memory
00237   /// location. It takes
00238   ///     ST:      store node that we want to replace
00239   ///     Chain:   incoming store chain
00240   ///     BasePtr: base address of where we want to store into
00241   SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain, 
00242                                SDValue BasePtr);
00243   
00244   /// Helper genWidenVectorStores - Helper function to generate a set of
00245   /// stores to store a widen vector into non widen memory
00246   // It takes
00247   //   StChain: list of chains for the stores we have generated
00248   //   Chain:   incoming chain for the ld vector
00249   //   BasePtr: base pointer to load from
00250   //   SV:      memory disambiguation source value
00251   //   SVOffset:   memory disambiugation offset
00252   //   Alignment:  alignment of the memory
00253   //   isVolatile: volatile lod
00254   //   ValOp:   value to store  
00255   //   StWidth: width of memory that we want to store 
00256   void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
00257                             SDValue BasePtr, const Value *SV,
00258                             int SVOffset, unsigned Alignment,
00259                             bool isVolatile, SDValue ValOp,
00260                             unsigned StWidth, DebugLoc dl);
00261  
00262   /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
00263   /// specified mask and type.  Targets can specify exactly which masks they
00264   /// support and the code generator is tasked with not creating illegal masks.
00265   ///
00266   /// Note that this will also return true for shuffles that are promoted to a
00267   /// different type.
00268   ///
00269   /// If this is a legal shuffle, this method returns the (possibly promoted)
00270   /// build_vector Mask.  If it's not a legal shuffle, it returns null.
00271   SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
00272   
00273   bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
00274                                     SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
00275 
00276   void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC,
00277                              DebugLoc dl);
00278   void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
00279                              DebugLoc dl);
00280   void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
00281                      DebugLoc dl) {
00282     LegalizeSetCCOperands(LHS, RHS, CC, dl);
00283     LegalizeSetCCCondCode(VT, LHS, RHS, CC, dl);
00284   }
00285     
00286   SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
00287                           SDValue &Hi);
00288   SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl);
00289 
00290   SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl);
00291   SDValue ExpandBUILD_VECTOR(SDNode *Node);
00292   SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
00293   SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, 
00294                             SDValue Op, DebugLoc dl);
00295   SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
00296                                DebugLoc dl);
00297   SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
00298                                 DebugLoc dl);
00299   SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
00300                                 DebugLoc dl);
00301 
00302   SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
00303   SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
00304   bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
00305                    SDValue &Lo, SDValue &Hi, DebugLoc dl);
00306   void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
00307                         SDValue &Lo, SDValue &Hi, DebugLoc dl);
00308 
00309   SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
00310   SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
00311 };
00312 }
00313 
00314 /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
00315 /// specified mask and type.  Targets can specify exactly which masks they
00316 /// support and the code generator is tasked with not creating illegal masks.
00317 ///
00318 /// Note that this will also return true for shuffles that are promoted to a
00319 /// different type.
00320 SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
00321   switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
00322   default: return 0;
00323   case TargetLowering::Legal:
00324   case TargetLowering::Custom:
00325     break;
00326   case TargetLowering::Promote: {
00327     // If this is promoted to a different type, convert the shuffle mask and
00328     // ask if it is legal in the promoted type!
00329     MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
00330     MVT EltVT = NVT.getVectorElementType();
00331 
00332     // If we changed # elements, change the shuffle mask.
00333     unsigned NumEltsGrowth =
00334       NVT.getVectorNumElements() / VT.getVectorNumElements();
00335     assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
00336     if (NumEltsGrowth > 1) {
00337       // Renumber the elements.
00338       SmallVector<SDValue, 8> Ops;
00339       for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
00340         SDValue InOp = Mask.getOperand(i);
00341         for (unsigned j = 0; j != NumEltsGrowth; ++j) {
00342           if (InOp.getOpcode() == ISD::UNDEF)
00343             Ops.push_back(DAG.getNode(ISD::UNDEF, 
00344                                       InOp.getNode()->getDebugLoc(), EltVT));
00345           else {
00346             unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
00347             Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
00348           }
00349         }
00350       }
00351       Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getNode()->getDebugLoc(),
00352                          NVT, &Ops[0], Ops.size());
00353     }
00354     VT = NVT;
00355     break;
00356   }
00357   }
00358   return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
00359 }
00360 
00361 SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, bool types)
00362   : TLI(dag.getTargetLoweringInfo()), DAG(dag), TypesNeedLegalizing(types),
00363     ValueTypeActions(TLI.getValueTypeActions()) {
00364   assert(MVT::LAST_VALUETYPE <= 32 &&
00365          "Too many value types for ValueTypeActions to hold!");
00366 }
00367 
00368 void SelectionDAGLegalize::LegalizeDAG() {
00369   LastCALLSEQ_END = DAG.getEntryNode();
00370   IsLegalizingCall = false;
00371   
00372   // The legalize process is inherently a bottom-up recursive process (users
00373   // legalize their uses before themselves).  Given infinite stack space, we
00374   // could just start legalizing on the root and traverse the whole graph.  In
00375   // practice however, this causes us to run out of stack space on large basic
00376   // blocks.  To avoid this problem, compute an ordering of the nodes where each
00377   // node is only legalized after all of its operands are legalized.
00378   DAG.AssignTopologicalOrder();
00379   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
00380        E = prior(DAG.allnodes_end()); I != next(E); ++I)
00381     HandleOp(SDValue(I, 0));
00382 
00383   // Finally, it's possible the root changed.  Get the new root.
00384   SDValue OldRoot = DAG.getRoot();
00385   assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
00386   DAG.setRoot(LegalizedNodes[OldRoot]);
00387 
00388   ExpandedNodes.clear();
00389   LegalizedNodes.clear();
00390   PromotedNodes.clear();
00391   SplitNodes.clear();
00392   ScalarizedNodes.clear();
00393   WidenNodes.clear();
00394 
00395   // Remove dead nodes now.
00396   DAG.RemoveDeadNodes();
00397 }
00398 
00399 
00400 /// FindCallEndFromCallStart - Given a chained node that is part of a call
00401 /// sequence, find the CALLSEQ_END node that terminates the call sequence.
00402 static SDNode *FindCallEndFromCallStart(SDNode *Node) {
00403   if (Node->getOpcode() == ISD::CALLSEQ_END)
00404     return Node;
00405   if (Node->use_empty())
00406     return 0;   // No CallSeqEnd
00407   
00408   // The chain is usually at the end.
00409   SDValue TheChain(Node, Node->getNumValues()-1);
00410   if (TheChain.getValueType() != MVT::Other) {
00411     // Sometimes it's at the beginning.
00412     TheChain = SDValue(Node, 0);
00413     if (TheChain.getValueType() != MVT::Other) {
00414       // Otherwise, hunt for it.
00415       for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
00416         if (Node->getValueType(i) == MVT::Other) {
00417           TheChain = SDValue(Node, i);
00418           break;
00419         }
00420           
00421       // Otherwise, we walked into a node without a chain.  
00422       if (TheChain.getValueType() != MVT::Other)
00423         return 0;
00424     }
00425   }
00426   
00427   for (SDNode::use_iterator UI = Node->use_begin(),
00428        E = Node->use_end(); UI != E; ++UI) {
00429     
00430     // Make sure to only follow users of our token chain.
00431     SDNode *User = *UI;
00432     for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
00433       if (User->getOperand(i) == TheChain)
00434         if (SDNode *Result = FindCallEndFromCallStart(User))
00435           return Result;
00436   }
00437   return 0;
00438 }
00439 
00440 /// FindCallStartFromCallEnd - Given a chained node that is part of a call 
00441 /// sequence, find the CALLSEQ_START node that initiates the call sequence.
00442 static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
00443   assert(Node && "Didn't find callseq_start for a call??");
00444   if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
00445   
00446   assert(Node->getOperand(0).getValueType() == MVT::Other &&
00447          "Node doesn't have a token chain argument!");
00448   return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
00449 }
00450 
00451 /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
00452 /// see if any uses can reach Dest.  If no dest operands can get to dest, 
00453 /// legalize them, legalize ourself, and return false, otherwise, return true.
00454 ///
00455 /// Keep track of the nodes we fine that actually do lead to Dest in
00456 /// NodesLeadingTo.  This avoids retraversing them exponential number of times.
00457 ///
00458 bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
00459                                      SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
00460   if (N == Dest) return true;  // N certainly leads to Dest :)
00461   
00462   // If we've already processed this node and it does lead to Dest, there is no
00463   // need to reprocess it.
00464   if (NodesLeadingTo.count(N)) return true;
00465   
00466   // If the first result of this node has been already legalized, then it cannot
00467   // reach N.
00468   switch (getTypeAction(N->getValueType(0))) {
00469   case Legal: 
00470     if (LegalizedNodes.count(SDValue(N, 0))) return false;
00471     break;
00472   case Promote:
00473     if (PromotedNodes.count(SDValue(N, 0))) return false;
00474     break;
00475   case Expand:
00476     if (ExpandedNodes.count(SDValue(N, 0))) return false;
00477     break;
00478   }
00479   
00480   // Okay, this node has not already been legalized.  Check and legalize all
00481   // operands.  If none lead to Dest, then we can legalize this node.
00482   bool OperandsLeadToDest = false;
00483   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
00484     OperandsLeadToDest |=     // If an operand leads to Dest, so do we.
00485       LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
00486 
00487   if (OperandsLeadToDest) {
00488     NodesLeadingTo.insert(N);
00489     return true;
00490   }
00491 
00492   // Okay, this node looks safe, legalize it and return false.
00493   HandleOp(SDValue(N, 0));
00494   return false;
00495 }
00496 
00497 /// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
00498 /// appropriate for its type.
00499 void SelectionDAGLegalize::HandleOp(SDValue Op) {
00500   MVT VT = Op.getValueType();
00501   // If the type legalizer was run then we should never see any illegal result
00502   // types here except for target constants (the type legalizer does not touch
00503   // those) or for build vector used as a mask for a vector shuffle.
00504   // FIXME: We can removed the BUILD_VECTOR case when we fix PR2957.
00505   assert((TypesNeedLegalizing || getTypeAction(VT) == Legal ||
00506           Op.getOpcode() == ISD::TargetConstant ||
00507           Op.getOpcode() == ISD::BUILD_VECTOR) &&
00508          "Illegal type introduced after type legalization?");
00509   switch (getTypeAction(VT)) {
00510   default: assert(0 && "Bad type action!");
00511   case Legal:   (void)LegalizeOp(Op); break;
00512   case Promote:
00513     if (!VT.isVector()) {
00514       (void)PromoteOp(Op);
00515       break;
00516     }
00517     else  {
00518       // See if we can widen otherwise use Expand to either scalarize or split
00519       MVT WidenVT = TLI.getWidenVectorType(VT);
00520       if (WidenVT != MVT::Other) {
00521         (void) WidenVectorOp(Op, WidenVT);
00522         break;
00523       }
00524       // else fall thru to expand since we can't widen the vector
00525     }
00526   case Expand:
00527     if (!VT.isVector()) {
00528       // If this is an illegal scalar, expand it into its two component
00529       // pieces.
00530       SDValue X, Y;
00531       if (Op.getOpcode() == ISD::TargetConstant)
00532         break;  // Allow illegal target nodes.
00533       ExpandOp(Op, X, Y);
00534     } else if (VT.getVectorNumElements() == 1) {
00535       // If this is an illegal single element vector, convert it to a
00536       // scalar operation.
00537       (void)ScalarizeVectorOp(Op);
00538     } else {
00539       // This is an illegal multiple element vector.
00540       // Split it in half and legalize both parts.
00541       SDValue X, Y;
00542       SplitVectorOp(Op, X, Y);
00543     }
00544     break;
00545   }
00546 }
00547 
00548 /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
00549 /// a load from the constant pool.
00550 static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
00551                                 SelectionDAG &DAG, const TargetLowering &TLI) {
00552   bool Extend = false;
00553 
00554   // If a FP immediate is precise when represented as a float and if the
00555   // target can do an extending load from float to double, we put it into
00556   // the constant pool as a float, even if it's is statically typed as a
00557   // double.  This shrinks FP constants and canonicalizes them for targets where
00558   // an FP extending load is the same cost as a normal load (such as on the x87
00559   // fp stack or PPC FP unit).
00560   MVT VT = CFP->getValueType(0);
00561   ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
00562   if (!UseCP) {
00563     if (VT!=MVT::f64 && VT!=MVT::f32)
00564       assert(0 && "Invalid type expansion");
00565     return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
00566                            (VT == MVT::f64) ? MVT::i64 : MVT::i32);
00567   }
00568 
00569   MVT OrigVT = VT;
00570   MVT SVT = VT;
00571   while (SVT != MVT::f32) {
00572     SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
00573     if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
00574         // Only do this if the target has a native EXTLOAD instruction from
00575         // smaller type.
00576         TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
00577         TLI.ShouldShrinkFPConstant(OrigVT)) {
00578       const Type *SType = SVT.getTypeForMVT();
00579       LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
00580       VT = SVT;
00581       Extend = true;
00582     }
00583   }
00584 
00585   SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
00586   unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
00587   if (Extend)
00588     return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
00589                           CPIdx, PseudoSourceValue::getConstantPool(),
00590                           0, VT, false, Alignment);
00591   return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
00592                      PseudoSourceValue::getConstantPool(), 0, false, Alignment);
00593 }
00594 
00595 
00596 /// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
00597 /// operations.
00598 static
00599 SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
00600                                     SelectionDAG &DAG,
00601                                     const TargetLowering &TLI) {
00602   DebugLoc dl = Node->getDebugLoc();
00603   MVT VT = Node->getValueType(0);
00604   MVT SrcVT = Node->getOperand(1).getValueType();
00605   assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
00606          "fcopysign expansion only supported for f32 and f64");
00607   MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
00608 
00609   // First get the sign bit of second operand.
00610   SDValue Mask1 = (SrcVT == MVT::f64)
00611     ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
00612     : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
00613   Mask1 = DAG.getNode(ISD::BIT_CONVERT, dl, SrcNVT, Mask1);
00614   SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, dl, SrcNVT,  
00615                                Node->getOperand(1));
00616   SignBit = DAG.getNode(ISD::AND, dl, SrcNVT, SignBit, Mask1);
00617   // Shift right or sign-extend it if the two operands have different types.
00618   int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
00619   if (SizeDiff > 0) {
00620     SignBit = DAG.getNode(ISD::SRL, dl, SrcNVT, SignBit,
00621                           DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
00622     SignBit = DAG.getNode(ISD::TRUNCATE, dl, NVT, SignBit);
00623   } else if (SizeDiff < 0) {
00624     SignBit = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, SignBit);
00625     SignBit = DAG.getNode(ISD::SHL, dl, NVT, SignBit,
00626                           DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
00627   }
00628 
00629   // Clear the sign bit of first operand.
00630   SDValue Mask2 = (VT == MVT::f64)
00631     ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
00632     : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
00633   Mask2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask2);
00634   SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
00635   Result = DAG.getNode(ISD::AND, dl, NVT, Result, Mask2);
00636 
00637   // Or the value with the sign bit.
00638   Result = DAG.getNode(ISD::OR, dl, NVT, Result, SignBit);
00639   return Result;
00640 }
00641 
00642 /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
00643 static
00644 SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
00645                              const TargetLowering &TLI) {
00646   SDValue Chain = ST->getChain();
00647   SDValue Ptr = ST->getBasePtr();
00648   SDValue Val = ST->getValue();
00649   MVT VT = Val.getValueType();
00650   int Alignment = ST->getAlignment();
00651   int SVOffset = ST->getSrcValueOffset();
00652   DebugLoc dl = ST->getDebugLoc();
00653   if (ST->getMemoryVT().isFloatingPoint() ||
00654       ST->getMemoryVT().isVector()) {
00655     MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
00656     if (TLI.isTypeLegal(intVT)) {
00657       // Expand to a bitconvert of the value to the integer type of the
00658       // same size, then a (misaligned) int store.
00659       // FIXME: Does not handle truncating floating point stores!
00660       SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
00661       return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
00662                           SVOffset, ST->isVolatile(), Alignment);
00663     } else {
00664       // Do a (aligned) store to a stack slot, then copy from the stack slot
00665       // to the final destination using (unaligned) integer loads and stores.
00666       MVT StoredVT = ST->getMemoryVT();
00667       MVT RegVT =
00668         TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
00669       unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
00670       unsigned RegBytes = RegVT.getSizeInBits() / 8;
00671       unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
00672 
00673       // Make sure the stack slot is also aligned for the register type.
00674       SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
00675 
00676       // Perform the original store, only redirected to the stack slot.
00677       SDValue Store = DAG.getTruncStore(Chain, dl, 
00678                                         Val, StackPtr, NULL, 0,StoredVT);
00679       SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
00680       SmallVector<SDValue, 8> Stores;
00681       unsigned Offset = 0;
00682 
00683       // Do all but one copies using the full register width.
00684       for (unsigned i = 1; i < NumRegs; i++) {
00685         // Load one integer register's worth from the stack slot.
00686         SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
00687         // Store it to the final location.  Remember the store.
00688         Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
00689                                       ST->getSrcValue(), SVOffset + Offset,
00690                                       ST->isVolatile(),
00691                                       MinAlign(ST->getAlignment(), Offset)));
00692         // Increment the pointers.
00693         Offset += RegBytes;
00694         StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
00695                                Increment);
00696         Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
00697       }
00698 
00699       // The last store may be partial.  Do a truncating store.  On big-endian
00700       // machines this requires an extending load from the stack slot to ensure
00701       // that the bits are in the right place.
00702       MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
00703 
00704       // Load from the stack slot.
00705       SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
00706                                     NULL, 0, MemVT);
00707 
00708       Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
00709                                          ST->getSrcValue(), SVOffset + Offset,
00710                                          MemVT, ST->isVolatile(),
00711                                          MinAlign(ST->getAlignment(), Offset)));
00712       // The order of the stores doesn't matter - say it with a TokenFactor.
00713       return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
00714                          Stores.size());
00715     }
00716   }
00717   assert(ST->getMemoryVT().isInteger() &&
00718          !ST->getMemoryVT().isVector() &&
00719          "Unaligned store of unknown type.");
00720   // Get the half-size VT
00721   MVT NewStoredVT =
00722     (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
00723   int NumBits = NewStoredVT.getSizeInBits();
00724   int IncrementSize = NumBits / 8;
00725 
00726   // Divide the stored value in two parts.
00727   SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
00728   SDValue Lo = Val;
00729   SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
00730 
00731   // Store the two parts
00732   SDValue Store1, Store2;
00733   Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
00734                              ST->getSrcValue(), SVOffset, NewStoredVT,
00735                              ST->isVolatile(), Alignment);
00736   Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
00737                     DAG.getConstant(IncrementSize, TLI.getPointerTy()));
00738   Alignment = MinAlign(Alignment, IncrementSize);
00739   Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
00740                              ST->getSrcValue(), SVOffset + IncrementSize,
00741                              NewStoredVT, ST->isVolatile(), Alignment);
00742 
00743   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
00744 }
00745 
00746 /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
00747 static
00748 SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
00749                             const TargetLowering &TLI) {
00750   int SVOffset = LD->getSrcValueOffset();
00751   SDValue Chain = LD->getChain();
00752   SDValue Ptr = LD->getBasePtr();
00753   MVT VT = LD->getValueType(0);
00754   MVT LoadedVT = LD->getMemoryVT();
00755   DebugLoc dl = LD->getDebugLoc();
00756   if (VT.isFloatingPoint() || VT.isVector()) {
00757     MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
00758     if (TLI.isTypeLegal(intVT)) {
00759       // Expand to a (misaligned) integer load of the same size,
00760       // then bitconvert to floating point or vector.
00761       SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
00762                                     SVOffset, LD->isVolatile(),
00763                                     LD->getAlignment());
00764       SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
00765       if (VT.isFloatingPoint() && LoadedVT != VT)
00766         Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
00767 
00768       SDValue Ops[] = { Result, Chain };
00769       return DAG.getMergeValues(Ops, 2, dl);
00770     } else {
00771       // Copy the value to a (aligned) stack slot using (unaligned) integer
00772       // loads and stores, then do a (aligned) load from the stack slot.
00773       MVT RegVT = TLI.getRegisterType(intVT);
00774       unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
00775       unsigned RegBytes = RegVT.getSizeInBits() / 8;
00776       unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
00777 
00778       // Make sure the stack slot is also aligned for the register type.
00779       SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
00780 
00781       SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
00782       SmallVector<SDValue, 8> Stores;
00783       SDValue StackPtr = StackBase;
00784       unsigned Offset = 0;
00785 
00786       // Do all but one copies using the full register width.
00787       for (unsigned i = 1; i < NumRegs; i++) {
00788         // Load one integer register's worth from the original location.
00789         SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
00790                                    SVOffset + Offset, LD->isVolatile(),
00791                                    MinAlign(LD->getAlignment(), Offset));
00792         // Follow the load with a store to the stack slot.  Remember the store.
00793         Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
00794                                       NULL, 0));
00795         // Increment the pointers.
00796         Offset += RegBytes;
00797         Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
00798         StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
00799                                Increment);
00800       }
00801 
00802       // The last copy may be partial.  Do an extending load.
00803       MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
00804       SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
00805                                     LD->getSrcValue(), SVOffset + Offset,
00806                                     MemVT, LD->isVolatile(),
00807                                     MinAlign(LD->getAlignment(), Offset));
00808       // Follow the load with a store to the stack slot.  Remember the store.
00809       // On big-endian machines this requires a truncating store to ensure
00810       // that the bits end up in the right place.
00811       Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
00812                                          NULL, 0, MemVT));
00813 
00814       // The order of the stores doesn't matter - say it with a TokenFactor.
00815       SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
00816                                Stores.size());
00817 
00818       // Finally, perform the original load only redirected to the stack slot.
00819       Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
00820                             NULL, 0, LoadedVT);
00821 
00822       // Callers expect a MERGE_VALUES node.
00823       SDValue Ops[] = { Load, TF };
00824       return DAG.getMergeValues(Ops, 2, dl);
00825     }
00826   }
00827   assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
00828          "Unaligned load of unsupported type.");
00829 
00830   // Compute the new VT that is half the size of the old one.  This is an
00831   // integer MVT.
00832   unsigned NumBits = LoadedVT.getSizeInBits();
00833   MVT NewLoadedVT;
00834   NewLoadedVT = MVT::getIntegerVT(NumBits/2);
00835   NumBits >>= 1;
00836   
00837   unsigned Alignment = LD->getAlignment();
00838   unsigned IncrementSize = NumBits / 8;
00839   ISD::LoadExtType HiExtType = LD->getExtensionType();
00840 
00841   // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
00842   if (HiExtType == ISD::NON_EXTLOAD)
00843     HiExtType = ISD::ZEXTLOAD;
00844 
00845   // Load the value in two parts
00846   SDValue Lo, Hi;
00847   if (TLI.isLittleEndian()) {
00848     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
00849                         SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
00850     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
00851                       DAG.getConstant(IncrementSize, TLI.getPointerTy()));
00852     Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
00853                         SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
00854                         MinAlign(Alignment, IncrementSize));
00855   } else {
00856     Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
00857                         SVOffset, NewLoadedVT,LD->isVolatile(), Alignment);
00858     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
00859                       DAG.getConstant(IncrementSize, TLI.getPointerTy()));
00860     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
00861                         SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
00862                         MinAlign(Alignment, IncrementSize));
00863   }
00864 
00865   // aggregate the two parts
00866   SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
00867   SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
00868   Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
00869 
00870   SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
00871                              Hi.getValue(1));
00872 
00873   SDValue Ops[] = { Result, TF };
00874   return DAG.getMergeValues(Ops, 2, dl);
00875 }
00876 
00877 /// UnrollVectorOp - We know that the given vector has a legal type, however
00878 /// the operation it performs is not legal and is an operation that we have
00879 /// no way of lowering.  "Unroll" the vector, splitting out the scalars and
00880 /// operating on each element individually.
00881 SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
00882   MVT VT = Op.getValueType();
00883   assert(isTypeLegal(VT) &&
00884          "Caller should expand or promote operands that are not legal!");
00885   assert(Op.getNode()->getNumValues() == 1 &&
00886          "Can't unroll a vector with multiple results!");
00887   unsigned NE = VT.getVectorNumElements();
00888   MVT EltVT = VT.getVectorElementType();
00889   DebugLoc dl = Op.getNode()->getDebugLoc();
00890 
00891   SmallVector<SDValue, 8> Scalars;
00892   SmallVector<SDValue, 4> Operands(Op.getNumOperands());
00893   for (unsigned i = 0; i != NE; ++i) {
00894     for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
00895       SDValue Operand = Op.getOperand(j);
00896       MVT OperandVT = Operand.getValueType();
00897       if (OperandVT.isVector()) {
00898         // A vector operand; extract a single element.
00899         MVT OperandEltVT = OperandVT.getVectorElementType();
00900         Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
00901                                   OperandEltVT,
00902                                   Operand,
00903                                   DAG.getConstant(i, MVT::i32));
00904       } else {
00905         // A scalar operand; just use it as is.
00906         Operands[j] = Operand;
00907       }
00908     }
00909 
00910     switch (Op.getOpcode()) {
00911     default:
00912       Scalars.push_back(DAG.getNode(Op.getOpcode(), dl, EltVT,
00913                                     &Operands[0], Operands.size()));
00914       break;
00915     case ISD::SHL:
00916     case ISD::SRA:
00917     case ISD::SRL:
00918     case ISD::ROTL:
00919     case ISD::ROTR:
00920       Scalars.push_back(DAG.getNode(Op.getOpcode(), dl, EltVT, Operands[0],
00921                                     DAG.getShiftAmountOperand(Operands[1])));
00922       break;
00923     }
00924   }
00925 
00926   return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Scalars[0], Scalars.size());
00927 }
00928 
00929 /// GetFPLibCall - Return the right libcall for the given floating point type.
00930 static RTLIB::Libcall GetFPLibCall(MVT VT,
00931                                    RTLIB::Libcall Call_F32,
00932                                    RTLIB::Libcall Call_F64,
00933                                    RTLIB::Libcall Call_F80,
00934                                    RTLIB::Libcall Call_PPCF128) {
00935   return
00936     VT == MVT::f32 ? Call_F32 :
00937     VT == MVT::f64 ? Call_F64 :
00938     VT == MVT::f80 ? Call_F80 :
00939     VT == MVT::ppcf128 ? Call_PPCF128 :
00940     RTLIB::UNKNOWN_LIBCALL;
00941 }
00942 
00943 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
00944 /// insertion index for the INSERT_VECTOR_ELT instruction.  In this case, it
00945 /// is necessary to spill the vector being inserted into to memory, perform
00946 /// the insert there, and then read the result back.
00947 SDValue SelectionDAGLegalize::
00948 PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
00949                                DebugLoc dl) {
00950   SDValue Tmp1 = Vec;
00951   SDValue Tmp2 = Val;
00952   SDValue Tmp3 = Idx;
00953   
00954   // If the target doesn't support this, we have to spill the input vector
00955   // to a temporary stack slot, update the element, then reload it.  This is
00956   // badness.  We could also load the value into a vector register (either
00957   // with a "move to register" or "extload into register" instruction, then
00958   // permute it into place, if the idx is a constant and if the idx is
00959   // supported by the target.
00960   MVT VT    = Tmp1.getValueType();
00961   MVT EltVT = VT.getVectorElementType();
00962   MVT IdxVT = Tmp3.getValueType();
00963   MVT PtrVT = TLI.getPointerTy();
00964   SDValue StackPtr = DAG.CreateStackTemporary(VT);
00965 
00966   int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
00967 
00968   // Store the vector.
00969   SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
00970                             PseudoSourceValue::getFixedStack(SPFI), 0);
00971 
00972   // Truncate or zero extend offset to target pointer type.
00973   unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
00974   Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
00975   // Add the offset to the index.
00976   unsigned EltSize = EltVT.getSizeInBits()/8;
00977   Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
00978   SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
00979   // Store the scalar value.
00980   Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
00981                          PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
00982   // Load the updated vector.
00983   return DAG.getLoad(VT, dl, Ch, StackPtr,
00984                      PseudoSourceValue::getFixedStack(SPFI), 0);
00985 }
00986 
00987 
00988 /// LegalizeOp - We know that the specified value has a legal type, and
00989 /// that its operands are legal.  Now ensure that the operation itself
00990 /// is legal, recursively ensuring that the operands' operations remain
00991 /// legal.
00992 SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
00993   if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
00994     return Op;
00995   
00996   assert(isTypeLegal(Op.getValueType()) &&
00997          "Caller should expand or promote operands that are not legal!");
00998   SDNode *Node = Op.getNode();
00999   DebugLoc dl = Node->getDebugLoc();
01000 
01001   // If this operation defines any values that cannot be represented in a
01002   // register on this target, make sure to expand or promote them.
01003   if (Node->getNumValues() > 1) {
01004     for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
01005       if (getTypeAction(Node->getValueType(i)) != Legal) {
01006         HandleOp(Op.getValue(i));
01007         assert(LegalizedNodes.count(Op) &&
01008                "Handling didn't add legal operands!");
01009         return LegalizedNodes[Op];
01010       }
01011   }
01012 
01013   // Note that LegalizeOp may be reentered even from single-use nodes, which
01014   // means that we always must cache transformed nodes.
01015   DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
01016   if (I != LegalizedNodes.end()) return I->second;
01017 
01018   SDValue Tmp1, Tmp2, Tmp3, Tmp4;
01019   SDValue Result = Op;
01020   bool isCustom = false;
01021   
01022   switch (Node->getOpcode()) {
01023   case ISD::FrameIndex:
01024   case ISD::EntryToken:
01025   case ISD::Register:
01026   case ISD::BasicBlock:
01027   case ISD::TargetFrameIndex:
01028   case ISD::TargetJumpTable:
01029   case ISD::TargetConstant:
01030   case ISD::TargetConstantFP:
01031   case ISD::TargetConstantPool:
01032   case ISD::TargetGlobalAddress:
01033   case ISD::TargetGlobalTLSAddress:
01034   case ISD::TargetExternalSymbol:
01035   case ISD::VALUETYPE:
01036   case ISD::SRCVALUE:
01037   case ISD::MEMOPERAND:
01038   case ISD::CONDCODE:
01039   case ISD::ARG_FLAGS:
01040     // Primitives must all be legal.
01041     assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
01042            "This must be legal!");
01043     break;
01044   default:
01045     if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
01046       // If this is a target node, legalize it by legalizing the operands then
01047       // passing it through.
01048       SmallVector<SDValue, 8> Ops;
01049       for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
01050         Ops.push_back(LegalizeOp(Node->getOperand(i)));
01051 
01052       Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
01053 
01054       for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
01055         AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
01056       return Result.getValue(Op.getResNo());
01057     }
01058     // Otherwise this is an unhandled builtin node.  splat.
01059 #ifndef NDEBUG
01060     cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
01061 #endif
01062     assert(0 && "Do not know how to legalize this operator!");
01063     abort();
01064   case ISD::GLOBAL_OFFSET_TABLE:
01065   case ISD::GlobalAddress:
01066   case ISD::GlobalTLSAddress:
01067   case ISD::ExternalSymbol:
01068   case ISD::ConstantPool:
01069   case ISD::JumpTable: // Nothing to do.
01070     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
01071     default: assert(0 && "This action is not supported yet!");
01072     case TargetLowering::Custom:
01073       Tmp1 = TLI.LowerOperation(Op, DAG);
01074       if (Tmp1.getNode()) Result = Tmp1;
01075       // FALLTHROUGH if the target doesn't want to lower this op after all.
01076     case TargetLowering::Legal:
01077       break;
01078     }
01079     break;
01080   case ISD::FRAMEADDR:
01081   case ISD::RETURNADDR:
01082     // The only option for these nodes is to custom lower them.  If the target
01083     // does not custom lower them, then return zero.
01084     Tmp1 = TLI.LowerOperation(Op, DAG);
01085     if (Tmp1.getNode()) 
01086       Result = Tmp1;
01087     else
01088       Result = DAG.getConstant(0, TLI.getPointerTy());
01089     break;
01090   case ISD::FRAME_TO_ARGS_OFFSET: {
01091     MVT VT = Node->getValueType(0);
01092     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
01093     default: assert(0 && "This action is not supported yet!");
01094     case TargetLowering::Custom:
01095       Result = TLI.LowerOperation(Op, DAG);
01096       if (Result.getNode()) break;
01097       // Fall Thru
01098     case TargetLowering::Legal:
01099       Result = DAG.getConstant(0, VT);
01100       break;
01101     }
01102     }
01103     break;
01104   case ISD::EXCEPTIONADDR: {
01105     Tmp1 = LegalizeOp(Node->getOperand(0));
01106     MVT VT = Node->getValueType(0);
01107     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
01108     default: assert(0 && "This action is not supported yet!");
01109     case TargetLowering::Expand: {
01110         unsigned Reg = TLI.getExceptionAddressRegister();
01111         Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
01112       }
01113       break;
01114     case TargetLowering::Custom:
01115       Result = TLI.LowerOperation(Op, DAG);
01116       if (Result.getNode()) break;
01117       // Fall Thru
01118     case TargetLowering::Legal: {
01119       SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
01120       Result = DAG.getMergeValues(Ops, 2, dl);
01121       break;
01122     }
01123     }
01124     }
01125     if (Result.getNode()->getNumValues() == 1) break;
01126 
01127     assert(Result.getNode()->getNumValues() == 2 &&
01128            "Cannot return more than two values!");
01129 
01130     // Since we produced two values, make sure to remember that we
01131     // legalized both of them.
01132     Tmp1 = LegalizeOp(Result);
01133     Tmp2 = LegalizeOp(Result.getValue(1));
01134     AddLegalizedOperand(Op.getValue(0), Tmp1);
01135     AddLegalizedOperand(Op.getValue(1), Tmp2);
01136     return Op.getResNo() ? Tmp2 : Tmp1;
01137   case ISD::EHSELECTION: {
01138     Tmp1 = LegalizeOp(Node->getOperand(0));
01139     Tmp2 = LegalizeOp(Node->getOperand(1));
01140     MVT VT = Node->getValueType(0);
01141     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
01142     default: assert(0 && "This action is not supported yet!");
01143     case TargetLowering::Expand: {
01144         unsigned Reg = TLI.getExceptionSelectorRegister();
01145         Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
01146       }
01147       break;
01148     case TargetLowering::Custom:
01149       Result = TLI.LowerOperation(Op, DAG);
01150       if (Result.getNode()) break;
01151       // Fall Thru
01152     case TargetLowering::Legal: {
01153       SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
01154       Result = DAG.getMergeValues(Ops, 2, dl);
01155       break;
01156     }
01157     }
01158     }
01159     if (Result.getNode()->getNumValues() == 1) break;
01160 
01161     assert(Result.getNode()->getNumValues() == 2 &&
01162            "Cannot return more than two values!");
01163 
01164     // Since we produced two values, make sure to remember that we
01165     // legalized both of them.
01166     Tmp1 = LegalizeOp(Result);
01167     Tmp2 = LegalizeOp(Result.getValue(1));
01168     AddLegalizedOperand(Op.getValue(0), Tmp1);
01169     AddLegalizedOperand(Op.getValue(1), Tmp2);
01170     return Op.getResNo() ? Tmp2 : Tmp1;
01171   case ISD::EH_RETURN: {
01172     MVT VT = Node->getValueType(0);
01173     // The only "good" option for this node is to custom lower it.
01174     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
01175     default: assert(0 && "This action is not supported at all!");
01176     case TargetLowering::Custom:
01177       Result = TLI.LowerOperation(Op, DAG);
01178       if (Result.getNode()) break;
01179       // Fall Thru
01180     case TargetLowering::Legal:
01181       // Target does not know, how to lower this, lower to noop
01182       Result = LegalizeOp(Node->getOperand(0));
01183       break;
01184     }
01185     }
01186     break;
01187   case ISD::AssertSext:
01188   case ISD::AssertZext:
01189     Tmp1 = LegalizeOp(Node->getOperand(0));
01190     Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
01191     break;
01192   case ISD::MERGE_VALUES:
01193     // Legalize eliminates MERGE_VALUES nodes.
01194     Result = Node->getOperand(Op.getResNo());
01195     break;
01196   case ISD::CopyFromReg:
01197     Tmp1 = LegalizeOp(Node->getOperand(0));
01198     Result = Op.getValue(0);
01199     if (Node->getNumValues() == 2) {
01200       Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
01201     } else {
01202       assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
01203       if (Node->getNumOperands() == 3) {
01204         Tmp2 = LegalizeOp(Node->getOperand(2));
01205         Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
01206       } else {
01207         Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
01208       }
01209       AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
01210     }
01211     // Since CopyFromReg produces two values, make sure to remember that we
01212     // legalized both of them.
01213     AddLegalizedOperand(Op.getValue(0), Result);
01214     AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
01215     return Result.getValue(Op.getResNo());
01216   case ISD::UNDEF: {
01217     MVT VT = Op.getValueType();
01218     switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
01219     default: assert(0 && "This action is not supported yet!");
01220     case TargetLowering::Expand:
01221       if (VT.isInteger())
01222         Result = DAG.getConstant(0, VT);
01223       else if (VT.isFloatingPoint())
01224         Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
01225                                    VT);
01226       else
01227         assert(0 && "Unknown value type!");
01228       break;
01229     case TargetLowering::Legal:
01230       break;
01231     }
01232     break;
01233   }
01234     
01235   case ISD::INTRINSIC_W_CHAIN:
01236   case ISD::INTRINSIC_WO_CHAIN:
01237   case ISD::INTRINSIC_VOID: {
01238     SmallVector<SDValue, 8> Ops;
01239     for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
01240       Ops.push_back(LegalizeOp(Node->getOperand(i)));
01241     Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
01242     
01243     // Allow the target to custom lower its intrinsics if it wants to.
01244     if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) == 
01245         TargetLowering::Custom) {
01246       Tmp3 = TLI.LowerOperation(Result, DAG);
01247       if (Tmp3.getNode()) Result = Tmp3;
01248     }
01249 
01250     if (Result.getNode()->getNumValues() == 1) break;
01251 
01252     // Must have return value and chain result.
01253     assert(Result.getNode()->getNumValues() == 2 &&
01254            "Cannot return more than two values!");
01255 
01256     // Since loads produce two values, make sure to remember that we 
01257     // legalized both of them.
01258     AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
01259     AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
01260     return Result.getValue(Op.getResNo());
01261   }    
01262 
01263   case ISD::DBG_STOPPOINT:
01264     assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
01265     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the input chain.
01266     
01267     switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
01268     case TargetLowering::Promote:
01269     default: assert(0 && "This action is not supported yet!");
01270     case TargetLowering::Expand: {
01271       DwarfWriter *DW = DAG.getDwarfWriter();
01272       bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
01273                                                        MVT::Other);
01274       bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
01275       
01276       const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
01277       GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
01278       if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
01279         DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
01280         unsigned SrcFile = DW->RecordSource(CU.getDirectory(),
01281                                             CU.getFilename());
01282         
01283         unsigned Line = DSP->getLine();
01284         unsigned Col = DSP->getColumn();
01285         
01286         // A bit self-referential to have DebugLoc on Debug_Loc nodes, but
01287         // it won't hurt anything.
01288         if (useDEBUG_LOC) {
01289           SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
01290                               DAG.getConstant(Col, MVT::i32),
01291                               DAG.getConstant(SrcFile, MVT::i32) };
01292           Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
01293         } else {
01294           unsigned ID = DW->RecordSourceLine(Line, Col, SrcFile);
01295           Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
01296         }
01297       } else {
01298         Result = Tmp1;  // chain
01299       }
01300       break;
01301     }
01302     case TargetLowering::Legal: {
01303       LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
01304       if (Action == Legal && Tmp1 == Node->getOperand(0))
01305         break;
01306 
01307       SmallVector<SDValue, 8> Ops;
01308       Ops.push_back(Tmp1);
01309       if (Action == Legal) {
01310         Ops.push_back(Node->getOperand(1));  // line # must be legal.
01311         Ops.push_back(Node->getOperand(2));  // col # must be legal.
01312       } else {
01313         // Otherwise promote them.
01314         Ops.push_back(PromoteOp(Node->getOperand(1)));
01315         Ops.push_back(PromoteOp(Node->getOperand(2)));
01316       }
01317       Ops.push_back(Node->getOperand(3));  // filename must be legal.
01318       Ops.push_back(Node->getOperand(4));  // working dir # must be legal.
01319       Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
01320       break;
01321     }
01322     }
01323     break;
01324 
01325   case ISD::DECLARE:
01326     assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
01327     switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
01328     default: assert(0 && "This action is not supported yet!");
01329     case TargetLowering::Legal:
01330       Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
01331       Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the address.
01332       Tmp3 = LegalizeOp(Node->getOperand(2));  // Legalize the variable.
01333       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
01334       break;
01335     case TargetLowering::Expand:
01336       Result = LegalizeOp(Node->getOperand(0));
01337       break;
01338     }
01339     break;    
01340     
01341   case ISD::DEBUG_LOC:
01342     assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
01343     switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
01344     default: assert(0 && "This action is not supported yet!");
01345     case TargetLowering::Legal: {
01346       LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
01347       Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
01348       if (Action == Legal && Tmp1 == Node->getOperand(0))
01349         break;
01350       if (Action == Legal) {
01351         Tmp2 = Node->getOperand(1);
01352         Tmp3 = Node->getOperand(2);
01353         Tmp4 = Node->getOperand(3);
01354       } else {
01355         Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the line #.
01356         Tmp3 = LegalizeOp(Node->getOperand(2));  // Legalize the col #.
01357         Tmp4 = LegalizeOp(Node->getOperand(3));  // Legalize the source file id.
01358       }
01359       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
01360       break;
01361     }
01362     }
01363     break;    
01364 
01365   case ISD::DBG_LABEL:
01366   case ISD::EH_LABEL:
01367     assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
01368     switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
01369     default: assert(0 && "This action is not supported yet!");
01370     case TargetLowering::Legal:
01371       Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
01372       Result = DAG.UpdateNodeOperands(Result, Tmp1);
01373       break;
01374     case TargetLowering::Expand:
01375       Result = LegalizeOp(Node->getOperand(0));
01376       break;
01377     }
01378     break;
01379 
01380   case ISD::PREFETCH:
01381     assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
01382     switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
01383     default: assert(0 && "This action is not supported yet!");
01384     case TargetLowering::Legal:
01385       Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
01386       Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the address.
01387       Tmp3 = LegalizeOp(Node->getOperand(2));  // Legalize the rw specifier.
01388       Tmp4 = LegalizeOp(Node->getOperand(3));  // Legalize locality specifier.
01389       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
01390       break;
01391     case TargetLowering::Expand:
01392       // It's a noop.
01393       Result = LegalizeOp(Node->getOperand(0));
01394       break;
01395     }
01396     break;
01397 
01398   case ISD::MEMBARRIER: {
01399     assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
01400     switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
01401     default: assert(0 && "This action is not supported yet!");
01402     case TargetLowering::Legal: {
01403       SDValue Ops[6];
01404       Ops[0] = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
01405       for (int x = 1; x < 6; ++x) {
01406         Ops[x] = Node->getOperand(x);
01407         if (!isTypeLegal(Ops[x].getValueType()))
01408           Ops[x] = PromoteOp(Ops[x]);
01409       }
01410       Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
01411       break;
01412     }
01413     case TargetLowering::Expand:
01414       //There is no libgcc call for this op
01415       Result = Node->getOperand(0);  // Noop
01416     break;
01417     }
01418     break;
01419   }
01420 
01421   case ISD::ATOMIC_CMP_SWAP: {
01422     unsigned int num_operands = 4;
01423     assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
01424     SDValue Ops[4];
01425     for (unsigned int x = 0; x < num_operands; ++x)
01426       Ops[x] = LegalizeOp(Node->getOperand(x));
01427     Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
01428     
01429     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
01430       default: assert(0 && "This action is not supported yet!");
01431       case TargetLowering::Custom:
01432         Result = TLI.LowerOperation(Result, DAG);
01433         break;
01434       case TargetLowering::Legal:
01435         break;
01436     }
01437     AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
01438     AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
01439     return Result.getValue(Op.getResNo());
01440   }
01441   case ISD::ATOMIC_LOAD_ADD:
01442   case ISD::ATOMIC_LOAD_SUB:
01443   case ISD::ATOMIC_LOAD_AND:
01444   case ISD::ATOMIC_LOAD_OR:
01445   case ISD::ATOMIC_LOAD_XOR:
01446   case ISD::ATOMIC_LOAD_NAND:
01447   case ISD::ATOMIC_LOAD_MIN:
01448   case ISD::ATOMIC_LOAD_MAX:
01449   case ISD::ATOMIC_LOAD_UMIN:
01450   case ISD::ATOMIC_LOAD_UMAX:
01451   case ISD::ATOMIC_SWAP: {
01452     unsigned int num_operands = 3;
01453     assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
01454     SDValue Ops[3];
01455     for (unsigned int x = 0; x < num_operands; ++x)
01456       Ops[x] = LegalizeOp(Node->getOperand(x));
01457     Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
01458 
01459     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
01460     default: assert(0 && "This action is not supported yet!");
01461     case TargetLowering::Custom:
01462       Result = TLI.LowerOperation(Result, DAG);
01463       break;
01464     case TargetLowering::Legal:
01465       break;
01466     }
01467     AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
01468     AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
01469     return Result.getValue(Op.getResNo());
01470   }
01471   case ISD::Constant: {
01472     ConstantSDNode *CN = cast<ConstantSDNode>(Node);
01473     unsigned opAction =
01474       TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
01475 
01476     // We know we don't need to expand constants here, constants only have one
01477     // value and we check that it is fine above.
01478 
01479     if (opAction == TargetLowering::Custom) {
01480       Tmp1 = TLI.LowerOperation(Result, DAG);
01481       if (Tmp1.getNode())
01482         Result = Tmp1;
01483     }
01484     break;
01485   }
01486   case ISD::ConstantFP: {
01487     // Spill FP immediates to the constant pool if the target cannot directly
01488     // codegen them.  Targets often have some immediate values that can be
01489     // efficiently generated into an FP register without a load.  We explicitly
01490     // leave these constants as ConstantFP nodes for the target to deal with.
01491     ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
01492 
01493     switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
01494     default: assert(0 && "This action is not supported yet!");
01495     case TargetLowering::Legal:
01496       break;
01497     case TargetLowering::Custom:
01498       Tmp3 = TLI.LowerOperation(Result, DAG);
01499       if (Tmp3.getNode()) {
01500         Result = Tmp3;
01501         break;
01502       }
01503       // FALLTHROUGH
01504     case TargetLowering::Expand: {
01505       // Check to see if this FP immediate is already legal.
01506       bool isLegal = false;
01507       for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
01508              E = TLI.legal_fpimm_end(); I != E; ++I) {
01509         if (CFP->isExactlyValue(*I)) {
01510           isLegal = true;
01511           break;
01512         }
01513       }
01514       // If this is a legal constant, turn it into a TargetConstantFP node.
01515       if (isLegal)
01516         break;
01517       Result = ExpandConstantFP(CFP, true, DAG, TLI);
01518     }
01519     }
01520     break;
01521   }
01522   case ISD::TokenFactor:
01523     if (Node->getNumOperands() == 2) {
01524       Tmp1 = LegalizeOp(Node->getOperand(0));
01525       Tmp2 = LegalizeOp(Node->getOperand(1));
01526       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
01527     } else if (Node->getNumOperands() == 3) {
01528       Tmp1 = LegalizeOp(Node->getOperand(0));
01529       Tmp2 = LegalizeOp(Node->getOperand(1));
01530       Tmp3 = LegalizeOp(Node->getOperand(2));
01531       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
01532     } else {
01533       SmallVector<SDValue, 8> Ops;
01534       // Legalize the operands.
01535       for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
01536         Ops.push_back(LegalizeOp(Node->getOperand(i)));
01537       Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
01538     }
01539     break;
01540     
01541   case ISD::FORMAL_ARGUMENTS:
01542   case ISD::CALL:
01543     // The only option for this is to custom lower it.
01544     Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
01545     assert(Tmp3.getNode() && "Target didn't custom lower this node!");
01546     // A call within a calling sequence must be legalized to something
01547     // other than the normal CALLSEQ_END.  Violating this gets Legalize
01548     // into an infinite loop.
01549     assert ((!IsLegalizingCall ||
01550              Node->getOpcode() != ISD::CALL ||
01551              Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
01552             "Nested CALLSEQ_START..CALLSEQ_END not supported.");
01553 
01554     // The number of incoming and outgoing values should match; unless the final
01555     // outgoing value is a flag.
01556     assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
01557             (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
01558              Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
01559                MVT::Flag)) &&
01560            "Lowering call/formal_arguments produced unexpected # results!");
01561     
01562     // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
01563     // remember that we legalized all of them, so it doesn't get relegalized.
01564     for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
01565       if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
01566         continue;
01567       Tmp1 = LegalizeOp(Tmp3.getValue(i));
01568       if (Op.getResNo() == i)
01569         Tmp2 = Tmp1;
01570       AddLegalizedOperand(SDValue(Node, i), Tmp1);
01571     }
01572     return Tmp2;
01573    case ISD::EXTRACT_SUBREG: {
01574       Tmp1 = LegalizeOp(Node->getOperand(0));
01575       ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
01576       assert(idx && "Operand must be a constant");
01577       Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
01578       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
01579     }
01580     break;
01581   case ISD::INSERT_SUBREG: {
01582       Tmp1 = LegalizeOp(Node->getOperand(0));
01583       Tmp2 = LegalizeOp(Node->getOperand(1));      
01584       ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
01585       assert(idx && "Operand must be a constant");
01586       Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
01587       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
01588     }
01589     break;      
01590   case ISD::BUILD_VECTOR:
01591     switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
01592     default: assert(0 && "This action is not supported yet!");
01593     case TargetLowering::Custom:
01594       Tmp3 = TLI.LowerOperation(Result, DAG);
01595       if (Tmp3.getNode()) {
01596         Result = Tmp3;
01597         break;
01598       }
01599       // FALLTHROUGH
01600     case TargetLowering::Expand:
01601       Result = ExpandBUILD_VECTOR(Result.getNode());
01602       break;
01603     }
01604     break;
01605   case ISD::INSERT_VECTOR_ELT:
01606     Tmp1 = LegalizeOp(Node->getOperand(0));  // InVec
01607     Tmp3 = LegalizeOp(Node->getOperand(2));  // InEltNo
01608 
01609     // The type of the value to insert may not be legal, even though the vector
01610     // type is legal.  Legalize/Promote accordingly.  We do not handle Expand
01611     // here.
01612     switch (getTypeAction(Node->getOperand(1).getValueType())) {
01613     default: assert(0 && "Cannot expand insert element operand");
01614     case Legal:   Tmp2 = LegalizeOp(Node->getOperand(1)); break;
01615     case Promote: Tmp2 = PromoteOp(Node->getOperand(1));  break;
01616     case Expand:
01617       // FIXME: An alternative would be to check to see if the target is not
01618       // going to custom lower this operation, we could bitcast to half elt 
01619       // width and perform two inserts at that width, if that is legal.
01620       Tmp2 = Node->getOperand(1);
01621       break;
01622     }
01623     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
01624     
01625     switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
01626                                    Node->getValueType(0))) {
01627     default: assert(0 && "This action is not supported yet!");
01628     case TargetLowering::Legal:
01629       break;
01630     case TargetLowering::Custom:
01631       Tmp4 = TLI.LowerOperation(Result, DAG);
01632       if (Tmp4.getNode()) {
01633         Result = Tmp4;
01634         break;
01635       }
01636       // FALLTHROUGH
01637     case TargetLowering::Promote:
01638       // Fall thru for vector case
01639     case TargetLowering::Expand: {
01640       // If the insert index is a constant, codegen this as a scalar_to_vector,
01641       // then a shuffle that inserts it into the right position in the vector.
01642       if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
01643         // SCALAR_TO_VECTOR requires that the type of the value being inserted
01644         // match the element type of the vector being created.
01645         if (Tmp2.getValueType() == 
01646             Op.getValueType().getVectorElementType()) {
01647           SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
01648                                         Tmp1.getValueType(), Tmp2);
01649           
01650           unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
01651           MVT ShufMaskVT =
01652             MVT::getIntVectorWithNumElements(NumElts);
01653           MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
01654           
01655           // We generate a shuffle of InVec and ScVec, so the shuffle mask
01656           // should be 0,1,2,3,4,5... with the appropriate element replaced with
01657           // elt 0 of the RHS.
01658           SmallVector<SDValue, 8> ShufOps;
01659           for (unsigned i = 0; i != NumElts; ++i) {
01660             if (i != InsertPos->getZExtValue())
01661               ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
01662             else
01663               ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
01664           }
01665           SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, ShufMaskVT,
01666                                            &ShufOps[0], ShufOps.size());
01667           
01668           Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Tmp1.getValueType(),
01669                                Tmp1, ScVec, ShufMask);
01670           Result = LegalizeOp(Result);
01671           break;
01672         }
01673       }
01674       Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3, dl);
01675       break;
01676     }
01677     }
01678     break;
01679   case ISD::SCALAR_TO_VECTOR:
01680     if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
01681       Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
01682       break;
01683     }
01684     
01685     Tmp1 = LegalizeOp(Node->getOperand(0));  // InVal
01686     Result = DAG.UpdateNodeOperands(Result, Tmp1);
01687     switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
01688                                    Node->getValueType(0))) {
01689     default: assert(0 && "This action is not supported yet!");
01690     case TargetLowering::Legal:
01691       break;
01692     case TargetLowering::Custom:
01693       Tmp3 = TLI.LowerOperation(Result, DAG);
01694       if (Tmp3.getNode()) {
01695         Result = Tmp3;
01696         break;
01697       }
01698       // FALLTHROUGH
01699     case TargetLowering::Expand:
01700       Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
01701       break;
01702     }
01703     break;
01704   case ISD::VECTOR_SHUFFLE:
01705     Tmp1 = LegalizeOp(Node->getOperand(0));   // Legalize the input vectors,
01706     Tmp2 = LegalizeOp(Node->getOperand(1));   // but not the shuffle mask.
01707     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
01708 
01709     // Allow targets to custom lower the SHUFFLEs they support.
01710     switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
01711     default: assert(0 && "Unknown operation action!");
01712     case TargetLowering::Legal:
01713       assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
01714              "vector shuffle should not be created if not legal!");
01715       break;
01716     case TargetLowering::Custom:
01717       Tmp3 = TLI.LowerOperation(Result, DAG);
01718       if (Tmp3.getNode()) {
01719         Result = Tmp3;
01720         break;
01721       }
01722       // FALLTHROUGH
01723     case TargetLowering::Expand: {
01724       MVT VT = Node->getValueType(0);
01725       MVT EltVT = VT.getVectorElementType();
01726       MVT PtrVT = TLI.getPointerTy();
01727       SDValue Mask = Node->getOperand(2);
01728       unsigned NumElems = Mask.getNumOperands();
01729       SmallVector<SDValue,8> Ops;
01730       for (unsigned i = 0; i != NumElems; ++i) {
01731         SDValue Arg = Mask.getOperand(i);
01732         if (Arg.getOpcode() == ISD::UNDEF) {
01733           Ops.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
01734         } else {
01735           assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
01736           unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
01737           if (Idx < NumElems)
01738             Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
01739                                       DAG.getConstant(Idx, PtrVT)));
01740           else
01741             Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
01742                                       DAG.getConstant(Idx - NumElems, PtrVT)));
01743         }
01744       }
01745       Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
01746       break;
01747     }
01748     case TargetLowering::Promote: {
01749       // Change base type to a different vector type.
01750       MVT OVT = Node->getValueType(0);
01751       MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
01752 
01753       // Cast the two input vectors.
01754       Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
01755       Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
01756       
01757       // Convert the shuffle mask to the right # elements.
01758       Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
01759       assert(Tmp3.getNode() && "Shuffle not legal?");
01760       Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, NVT, Tmp1, Tmp2, Tmp3);
01761       Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
01762       break;
01763     }
01764     }
01765     break;
01766   
01767   case ISD::EXTRACT_VECTOR_ELT:
01768     Tmp1 = Node->getOperand(0);
01769     Tmp2 = LegalizeOp(Node->getOperand(1));
01770     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
01771     Result = ExpandEXTRACT_VECTOR_ELT(Result);
01772     break;
01773 
01774   case ISD::EXTRACT_SUBVECTOR: 
01775     Tmp1 = Node->getOperand(0);
01776     Tmp2 = LegalizeOp(Node->getOperand(1));
01777     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
01778     Result = ExpandEXTRACT_SUBVECTOR(Result);
01779     break;
01780     
01781   case ISD::CONCAT_VECTORS: {
01782     // Use extract/insert/build vector for now. We might try to be
01783     // more clever later.
01784     MVT PtrVT = TLI.getPointerTy();
01785     SmallVector<SDValue, 8> Ops;
01786     unsigned NumOperands = Node->getNumOperands();
01787     for (unsigned i=0; i < NumOperands; ++i) {
01788       SDValue SubOp = Node->getOperand(i);
01789       MVT VVT = SubOp.getNode()->getValueType(0);
01790       MVT EltVT = VVT.getVectorElementType();
01791       unsigned NumSubElem = VVT.getVectorNumElements();
01792       for (unsigned j=0; j < NumSubElem; ++j) {
01793         Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
01794                                   DAG.getConstant(j, PtrVT)));
01795       }
01796     }
01797     return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
01798                       &Ops[0], Ops.size()));
01799   }
01800 
01801   case ISD::CALLSEQ_START: {
01802     SDNode *CallEnd = FindCallEndFromCallStart(Node);
01803     
01804     // Recursively Legalize all of the inputs of the call end that do not lead
01805     // to this call start.  This ensures that any libcalls that need be inserted
01806     // are inserted *before* the CALLSEQ_START.
01807     {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
01808     for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
01809       LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
01810                                    NodesLeadingTo);
01811     }
01812 
01813     // Now that we legalized all of the inputs (which may have inserted
01814     // libcalls) create the new CALLSEQ_START node.
01815     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
01816 
01817     // Merge in the last call, to ensure that this call start after the last
01818     // call ended.
01819     if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
01820       Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 
01821                          Tmp1, LastCALLSEQ_END);
01822       Tmp1 = LegalizeOp(Tmp1);
01823     }
01824       
01825     // Do not try to legalize the target-specific arguments (#1+).
01826     if (Tmp1 != Node->getOperand(0)) {
01827       SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
01828       Ops[0] = Tmp1;
01829       Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
01830     }
01831     
01832     // Remember that the CALLSEQ_START is legalized.
01833     AddLegalizedOperand(Op.getValue(0), Result);
01834     if (Node->getNumValues() == 2)    // If this has a flag result, remember it.
01835       AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
01836     
01837     // Now that the callseq_start and all of the non-call nodes above this call
01838     // sequence have been legalized, legalize the call itself.  During this 
01839     // process, no libcalls can/will be inserted, guaranteeing that no calls
01840     // can overlap.
01841     assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
01842     // Note that we are selecting this call!
01843     LastCALLSEQ_END = SDValue(CallEnd, 0);
01844     IsLegalizingCall = true;
01845     
01846     // Legalize the call, starting from the CALLSEQ_END.
01847     LegalizeOp(LastCALLSEQ_END);
01848     assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
01849     return Result;
01850   }
01851   case ISD::CALLSEQ_END:
01852     // If the CALLSEQ_START node hasn't been legalized first, legalize it.  This
01853     // will cause this node to be legalized as well as handling libcalls right.
01854     if (LastCALLSEQ_END.getNode() != Node) {
01855       LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
01856       DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
01857       assert(I != LegalizedNodes.end() &&
01858              "Legalizing the call start should have legalized this node!");
01859       return I->second;
01860     }
01861     
01862     // Otherwise, the call start has been legalized and everything is going 
01863     // according to plan.  Just legalize ourselves normally here.
01864     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
01865     // Do not try to legalize the target-specific arguments (#1+), except for
01866     // an optional flag input.
01867     if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
01868       if (Tmp1 != Node->getOperand(0)) {
01869         SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
01870         Ops[0] = Tmp1;
01871         Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
01872       }
01873     } else {
01874       Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
01875       if (Tmp1 != Node->getOperand(0) ||
01876           Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
01877         SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
01878         Ops[0] = Tmp1;
01879         Ops.back() = Tmp2;
01880         Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
01881       }
01882     }
01883     assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
01884     // This finishes up call legalization.
01885     IsLegalizingCall = false;
01886     
01887     // If the CALLSEQ_END node has a flag, remember that we legalized it.
01888     AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
01889     if (Node->getNumValues() == 2)
01890       AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
01891     return Result.getValue(Op.getResNo());
01892   case ISD::DYNAMIC_STACKALLOC: {
01893     MVT VT = Node->getValueType(0);
01894     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
01895     Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the size.
01896     Tmp3 = LegalizeOp(Node->getOperand(2));  // Legalize the alignment.
01897     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
01898 
01899     Tmp1 = Result.getValue(0);
01900     Tmp2 = Result.getValue(1);
01901     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
01902     default: assert(0 && "This action is not supported yet!");
01903     case TargetLowering::Expand: {
01904       unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
01905       assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
01906              " not tell us which reg is the stack pointer!");
01907       SDValue Chain = Tmp1.getOperand(0);
01908 
01909       // Chain the dynamic stack allocation so that it doesn't modify the stack
01910       // pointer when other instructions are using the stack.
01911       Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
01912 
01913       SDValue Size  = Tmp2.getOperand(1);
01914       SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
01915       Chain = SP.getValue(1);
01916       unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
01917       unsigned StackAlign =
01918         TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
01919       if (Align > StackAlign)
01920         SP = DAG.getNode(ISD::AND, dl, VT, SP,
01921                          DAG.getConstant(-(uint64_t)Align, VT));
01922       Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size);       // Value
01923       Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1);     // Output chain
01924 
01925       Tmp2 = DAG.getCALLSEQ_END(Chain,  DAG.getIntPtrConstant(0, true),
01926                                 DAG.getIntPtrConstant(0, true), SDValue());
01927 
01928       Tmp1 = LegalizeOp(Tmp1);
01929       Tmp2 = LegalizeOp(Tmp2);
01930       break;
01931     }
01932     case TargetLowering::Custom:
01933       Tmp3 = TLI.LowerOperation(Tmp1, DAG);
01934       if (Tmp3.getNode()) {
01935         Tmp1 = LegalizeOp(Tmp3);
01936         Tmp2 = LegalizeOp(Tmp3.getValue(1));
01937       }
01938       break;
01939     case TargetLowering::Legal:
01940       break;
01941     }
01942     // Since this op produce two values, make sure to remember that we
01943     // legalized both of them.
01944     AddLegalizedOperand(SDValue(Node, 0), Tmp1);
01945     AddLegalizedOperand(SDValue(Node, 1), Tmp2);
01946     return Op.getResNo() ? Tmp2 : Tmp1;
01947   }
01948   case ISD::INLINEASM: {
01949     SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
01950     bool Changed = false;
01951     // Legalize all of the operands of the inline asm, in case they are nodes
01952     // that need to be expanded or something.  Note we skip the asm string and
01953     // all of the TargetConstant flags.
01954     SDValue Op = LegalizeOp(Ops[0]);
01955     Changed = Op != Ops[0];
01956     Ops[0] = Op;
01957 
01958     bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
01959     for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
01960       unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
01961       for (++i; NumVals; ++i, --NumVals) {
01962         SDValue Op = LegalizeOp(Ops[i]);
01963         if (Op != Ops[i]) {
01964           Changed = true;
01965           Ops[i] = Op;
01966         }
01967       }
01968     }
01969 
01970     if (HasInFlag) {
01971       Op = LegalizeOp(Ops.back());
01972       Changed |= Op != Ops.back();
01973       Ops.back() = Op;
01974     }
01975     
01976     if (Changed)
01977       Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
01978       
01979     // INLINE asm returns a chain and flag, make sure to add both to the map.
01980     AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
01981     AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
01982     return Result.getValue(Op.getResNo());
01983   }
01984   case ISD::BR:
01985     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
01986     // Ensure that libcalls are emitted before a branch.
01987     Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
01988     Tmp1 = LegalizeOp(Tmp1);
01989     LastCALLSEQ_END = DAG.getEntryNode();
01990     
01991     Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
01992     break;
01993   case ISD::BRIND:
01994     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
01995     // Ensure that libcalls are emitted before a branch.
01996     Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
01997     Tmp1 = LegalizeOp(Tmp1);
01998     LastCALLSEQ_END = DAG.getEntryNode();
01999     
02000     switch (getTypeAction(Node->getOperand(1).getValueType())) {
02001     default: assert(0 && "Indirect target must be legal type (pointer)!");
02002     case Legal:
02003       Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
02004       break;
02005     }
02006     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
02007     break;
02008   case ISD::BR_JT:
02009     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
02010     // Ensure that libcalls are emitted before a branch.
02011     Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
02012     Tmp1 = LegalizeOp(Tmp1);
02013     LastCALLSEQ_END = DAG.getEntryNode();
02014 
02015     Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the jumptable node.
02016     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
02017 
02018     switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {  
02019     default: assert(0 && "This action is not supported yet!");
02020     case TargetLowering::Legal: break;
02021     case TargetLowering::Custom:
02022       Tmp1 = TLI.LowerOperation(Result, DAG);
02023       if (Tmp1.getNode()) Result = Tmp1;
02024       break;
02025     case TargetLowering::Expand: {
02026       SDValue Chain = Result.getOperand(0);
02027       SDValue Table = Result.getOperand(1);
02028       SDValue Index = Result.getOperand(2);
02029 
02030       MVT PTy = TLI.getPointerTy();
02031       MachineFunction &MF = DAG.getMachineFunction();
02032       unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
02033       Index= DAG.getNode(ISD::MUL, dl, PTy, 
02034                          Index, DAG.getConstant(EntrySize, PTy));
02035       SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
02036 
02037       MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
02038       SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
02039                                   PseudoSourceValue::getJumpTable(), 0, MemVT);
02040       Addr = LD;
02041       if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
02042         // For PIC, the sequence is:
02043         // BRIND(load(Jumptable + index) + RelocBase)
02044         // RelocBase can be JumpTable, GOT or some sort of global base.
02045         Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
02046                            TLI.getPICJumpTableRelocBase(Table, DAG));
02047       }
02048       Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
02049     }
02050     }
02051     break;
02052   case ISD::BRCOND:
02053     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
02054     // Ensure that libcalls are emitted before a return.
02055     Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
02056     Tmp1 = LegalizeOp(Tmp1);
02057     LastCALLSEQ_END = DAG.getEntryNode();
02058 
02059     switch (getTypeAction(Node->getOperand(1).getValueType())) {
02060     case Expand: assert(0 && "It's impossible to expand bools");
02061     case Legal:
02062       Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
02063       break;
02064     case Promote: {
02065       Tmp2 = PromoteOp(Node->getOperand(1));  // Promote the condition.
02066       
02067       // The top bits of the promoted condition are not necessarily zero, ensure
02068       // that the value is properly zero extended.
02069       unsigned BitWidth = Tmp2.getValueSizeInBits();
02070       if (!DAG.MaskedValueIsZero(Tmp2, 
02071                                  APInt::getHighBitsSet(BitWidth, BitWidth-1)))
02072         Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, MVT::i1);
02073       break;
02074     }
02075     }
02076 
02077     // Basic block destination (Op#2) is always legal.
02078     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
02079       
02080     switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {  
02081     default: assert(0 && "This action is not supported yet!");
02082     case TargetLowering::Legal: break;
02083     case TargetLowering::Custom:
02084       Tmp1 = TLI.LowerOperation(Result, DAG);
02085       if (Tmp1.getNode()) Result = Tmp1;
02086       break;
02087     case TargetLowering::Expand:
02088       // Expand brcond's setcc into its constituent parts and create a BR_CC
02089       // Node.
02090       if (Tmp2.getOpcode() == ISD::SETCC) {
02091         Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, 
02092                              Tmp1, Tmp2.getOperand(2),
02093                              Tmp2.getOperand(0), Tmp2.getOperand(1),
02094                              Node->getOperand(2));
02095       } else {
02096         Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, 
02097                              DAG.getCondCode(ISD::SETNE), Tmp2,
02098                              DAG.getConstant(0, Tmp2.getValueType()),
02099                              Node->getOperand(2));
02100       }
02101       break;
02102     }
02103     break;
02104   case ISD::BR_CC:
02105     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
02106     // Ensure that libcalls are emitted before a branch.
02107     Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
02108     Tmp1 = LegalizeOp(Tmp1);
02109     Tmp2 = Node->getOperand(2);              // LHS 
02110     Tmp3 = Node->getOperand(3);              // RHS
02111     Tmp4 = Node->getOperand(1);              // CC
02112 
02113     LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()), 
02114                   Tmp2, Tmp3, Tmp4, dl);
02115     LastCALLSEQ_END = DAG.getEntryNode();
02116 
02117     // If we didn't get both a LHS and RHS back from LegalizeSetCC,
02118     // the LHS is a legal SETCC itself.  In this case, we need to compare
02119     // the result against zero to select between true and false values.
02120     if (Tmp3.getNode() == 0) {
02121       Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
02122       Tmp4 = DAG.getCondCode(ISD::SETNE);
02123     }
02124     
02125     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, 
02126                                     Node->getOperand(4));
02127       
02128     switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
02129     default: assert(0 && "Unexpected action for BR_CC!");
02130     case TargetLowering::Legal: break;
02131     case TargetLowering::Custom:
02132       Tmp4 = TLI.LowerOperation(Result, DAG);
02133       if (Tmp4.getNode()) Result = Tmp4;
02134       break;
02135     }
02136     break;
02137   case ISD::LOAD: {
02138     LoadSDNode *LD = cast<LoadSDNode>(Node);
02139     Tmp1 = LegalizeOp(LD->getChain());   // Legalize the chain.
02140     Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
02141 
02142     ISD::LoadExtType ExtType = LD->getExtensionType();
02143     if (ExtType == ISD::NON_EXTLOAD) {
02144       MVT VT = Node->getValueType(0);
02145       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
02146       Tmp3 = Result.getValue(0);
02147       Tmp4 = Result.getValue(1);
02148     
02149       switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
02150       default: assert(0 && "This action is not supported yet!");
02151       case TargetLowering::Legal:
02152         // If this is an unaligned load and the target doesn't support it,
02153         // expand it.
02154         if (!TLI.allowsUnalignedMemoryAccesses()) {
02155           unsigned ABIAlignment = TLI.getTargetData()->
02156             getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
02157           if (LD->getAlignment() < ABIAlignment){
02158             Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
02159                                          TLI);
02160             Tmp3 = Result.getOperand(0);
02161             Tmp4 = Result.getOperand(1);
02162             Tmp3 = LegalizeOp(Tmp3);
02163             Tmp4 = LegalizeOp(Tmp4);
02164           }
02165         }
02166         break;
02167       case TargetLowering::Custom:
02168         Tmp1 = TLI.LowerOperation(Tmp3, DAG);
02169         if (Tmp1.getNode()) {
02170           Tmp3 = LegalizeOp(Tmp1);
02171           Tmp4 = LegalizeOp(Tmp1.getValue(1));
02172         }
02173         break;
02174       case TargetLowering::Promote: {
02175         // Only promote a load of vector type to another.
02176         assert(VT.isVector() && "Cannot promote this load!");
02177         // Change base type to a different vector type.
02178         MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
02179 
02180         Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
02181                            LD->getSrcValueOffset(),
02182                            LD->isVolatile(), LD->getAlignment());
02183         Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
02184         Tmp4 = LegalizeOp(Tmp1.getValue(1));
02185         break;
02186       }
02187       }
02188       // Since loads produce two values, make sure to remember that we 
02189       // legalized both of them.
02190       AddLegalizedOperand(SDValue(Node, 0), Tmp3);
02191       AddLegalizedOperand(SDValue(Node, 1), Tmp4);
02192       return Op.getResNo() ? Tmp4 : Tmp3;
02193     } else {
02194       MVT SrcVT = LD->getMemoryVT();
02195       unsigned SrcWidth = SrcVT.getSizeInBits();
02196       int SVOffset = LD->getSrcValueOffset();
02197       unsigned Alignment = LD->getAlignment();
02198       bool isVolatile = LD->isVolatile();
02199 
02200       if (SrcWidth != SrcVT.getStoreSizeInBits() &&
02201           // Some targets pretend to have an i1 loading operation, and actually
02202           // load an i8.  This trick is correct for ZEXTLOAD because the top 7
02203           // bits are guaranteed to be zero; it helps the optimizers understand
02204           // that these bits are zero.  It is also useful for EXTLOAD, since it
02205           // tells the optimizers that those bits are undefined.  It would be
02206           // nice to have an effective generic way of getting these benefits...
02207           // Until such a way is found, don't insist on promoting i1 here.
02208           (SrcVT != MVT::i1 ||
02209            TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
02210         // Promote to a byte-sized load if not loading an integral number of
02211         // bytes.  For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
02212         unsigned NewWidth = SrcVT.getStoreSizeInBits();
02213         MVT NVT = MVT::getIntegerVT(NewWidth);
02214         SDValue Ch;
02215 
02216         // The extra bits are guaranteed to be zero, since we stored them that
02217         // way.  A zext load from NVT thus automatically gives zext from SrcVT.
02218 
02219         ISD::LoadExtType NewExtType =
02220           ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
02221 
02222         Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
02223                                 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
02224                                 NVT, isVolatile, Alignment);
02225 
02226         Ch = Result.getValue(1); // The chain.
02227 
02228         if (ExtType == ISD::SEXTLOAD)
02229           // Having the top bits zero doesn't help when sign extending.
02230           Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 
02231                                Result.getValueType(),
02232                                Result, DAG.getValueType(SrcVT));
02233         else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
02234           // All the top bits are guaranteed to be zero - inform the optimizers.
02235           Result = DAG.getNode(ISD::AssertZext, dl, 
02236                                Result.getValueType(), Result,
02237                                DAG.getValueType(SrcVT));
02238 
02239         Tmp1 = LegalizeOp(Result);
02240         Tmp2 = LegalizeOp(Ch);
02241       } else if (SrcWidth & (SrcWidth - 1)) {
02242         // If not loading a power-of-2 number of bits, expand as two loads.
02243         assert(SrcVT.isExtended() && !SrcVT.isVector() &&
02244                "Unsupported extload!");
02245         unsigned RoundWidth = 1 << Log2_32(SrcWidth);
02246         assert(RoundWidth < SrcWidth);
02247         unsigned ExtraWidth = SrcWidth - RoundWidth;
02248         assert(ExtraWidth < RoundWidth);
02249         assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
02250                "Load size not an integral number of bytes!");
02251         MVT RoundVT = MVT::getIntegerVT(RoundWidth);
02252         MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
02253         SDValue Lo, Hi, Ch;
02254         unsigned IncrementSize;
02255 
02256         if (TLI.isLittleEndian()) {
02257           // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
02258           // Load the bottom RoundWidth bits.
02259           Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
02260                               Node->getValueType(0), Tmp1, Tmp2,
02261                               LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
02262                               Alignment);
02263 
02264           // Load the remaining ExtraWidth bits.
02265           IncrementSize = RoundWidth / 8;
02266           Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
02267                              DAG.getIntPtrConstant(IncrementSize));
02268           Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
02269                               LD->getSrcValue(), SVOffset + IncrementSize,
02270                               ExtraVT, isVolatile,
02271                               MinAlign(Alignment, IncrementSize));
02272 
02273           // Build a factor node to remember that this load is independent of the
02274           // other one.
02275           Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
02276                            Hi.getValue(1));
02277 
02278           // Move the top bits to the right place.
02279           Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
02280                            DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
02281 
02282           // Join the hi and lo parts.
02283           Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
02284         } else {
02285           // Big endian - avoid unaligned loads.
02286           // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
02287           // Load the top RoundWidth bits.
02288           Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
02289                               LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
02290                               Alignment);
02291 
02292           // Load the remaining ExtraWidth bits.
02293           IncrementSize = RoundWidth / 8;
02294           Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
02295                              DAG.getIntPtrConstant(IncrementSize));
02296           Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, 
02297                               Node->getValueType(0), Tmp1, Tmp2,
02298                               LD->getSrcValue(), SVOffset + IncrementSize,
02299                               ExtraVT, isVolatile,
02300                               MinAlign(Alignment, IncrementSize));
02301 
02302           // Build a factor node to remember that this load is independent of the
02303           // other one.
02304           Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
02305                            Hi.getValue(1));
02306 
02307           // Move the top bits to the right place.
02308           Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
02309                            DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
02310 
02311           // Join the hi and lo parts.
02312           Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
02313         }
02314 
02315         Tmp1 = LegalizeOp(Result);
02316         Tmp2 = LegalizeOp(Ch);
02317       } else {
02318         switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
02319         default: assert(0 && "This action is not supported yet!");
02320         case TargetLowering::Custom:
02321           isCustom = true;
02322           // FALLTHROUGH
02323         case TargetLowering::Legal:
02324           Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
02325           Tmp1 = Result.getValue(0);
02326           Tmp2 = Result.getValue(1);
02327 
02328           if (isCustom) {
02329             Tmp3 = TLI.LowerOperation(Result, DAG);
02330             if (Tmp3.getNode()) {
02331               Tmp1 = LegalizeOp(Tmp3);
02332               Tmp2 = LegalizeOp(Tmp3.getValue(1));
02333             }
02334           } else {
02335             // If this is an unaligned load and the target doesn't support it,
02336             // expand it.
02337             if (!TLI.allowsUnalignedMemoryAccesses()) {
02338               unsigned ABIAlignment = TLI.getTargetData()->
02339                 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
02340               if (LD->getAlignment() < ABIAlignment){
02341                 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
02342                                              TLI);
02343                 Tmp1 = Result.getOperand(0);
02344                 Tmp2 = Result.getOperand(1);
02345                 Tmp1 = LegalizeOp(Tmp1);
02346                 Tmp2 = LegalizeOp(Tmp2);
02347               }
02348             }
02349           }
02350           break;
02351         case TargetLowering::Expand:
02352           // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
02353           if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
02354             SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
02355                                          LD->getSrcValueOffset(),
02356                                          LD->isVolatile(), LD->getAlignment());
02357             Result = DAG.getNode(ISD::FP_EXTEND, dl, 
02358                                  Node->getValueType(0), Load);
02359             Tmp1 = LegalizeOp(Result);  // Relegalize new nodes.
02360             Tmp2 = LegalizeOp(Load.getValue(1));
02361             break;
02362           }
02363           assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
02364           // Turn the unsupported load into an EXTLOAD followed by an explicit
02365           // zero/sign extend inreg.
02366           Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
02367                                   Tmp1, Tmp2, LD->getSrcValue(),
02368                                   LD->getSrcValueOffset(), SrcVT,
02369                                   LD->isVolatile(), LD->getAlignment());
02370           SDValue ValRes;
02371           if (ExtType == ISD::SEXTLOAD)
02372             ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
02373                                  Result.getValueType(),
02374                                  Result, DAG.getValueType(SrcVT));
02375           else
02376             ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
02377           Tmp1 = LegalizeOp(ValRes);  // Relegalize new nodes.
02378           Tmp2 = LegalizeOp(Result.getValue(1));  // Relegalize new nodes.
02379           break;
02380         }
02381       }
02382 
02383       // Since loads produce two values, make sure to remember that we legalized
02384       // both of them.
02385       AddLegalizedOperand(SDValue(Node, 0), Tmp1);
02386       AddLegalizedOperand(SDValue(Node, 1), Tmp2);
02387       return Op.getResNo() ? Tmp2 : Tmp1;
02388     }
02389   }
02390   case ISD::EXTRACT_ELEMENT: {
02391     MVT OpTy = Node->getOperand(0).getValueType();
02392     switch (getTypeAction(OpTy)) {
02393     default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
02394     case Legal:
02395       if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
02396         // 1 -> Hi
02397         Result = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
02398                              DAG.getConstant(OpTy.getSizeInBits()/2,
02399                                              TLI.getShiftAmountTy()));
02400         Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
02401       } else {
02402         // 0 -> Lo
02403         Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), 
02404                              Node->getOperand(0));
02405       }
02406       break;
02407     case Expand:
02408       // Get both the low and high parts.
02409       ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
02410       if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
02411         Result = Tmp2;  // 1 -> Hi
02412       else
02413         Result = Tmp1;  // 0 -> Lo
02414       break;
02415     }
02416     break;
02417   }
02418 
02419   case ISD::CopyToReg:
02420     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
02421 
02422     assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
02423            "Register type must be legal!");
02424     // Legalize the incoming value (must be a legal type).
02425     Tmp2 = LegalizeOp(Node->getOperand(2));
02426     if (Node->getNumValues() == 1) {
02427       Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
02428     } else {
02429       assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
02430       if (Node->getNumOperands() == 4) {
02431         Tmp3 = LegalizeOp(Node->getOperand(3));
02432         Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
02433                                         Tmp3);
02434       } else {
02435         Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
02436       }
02437       
02438       // Since this produces two values, make sure to remember that we legalized
02439       // both of them.
02440       AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
02441       AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
02442       return Result;
02443     }
02444     break;
02445 
02446   case ISD::RET:
02447     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
02448 
02449     // Ensure that libcalls are emitted before a return.
02450     Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
02451     Tmp1 = LegalizeOp(Tmp1);
02452     LastCALLSEQ_END = DAG.getEntryNode();
02453       
02454     switch (Node->getNumOperands()) {
02455     case 3:  // ret val
02456       Tmp2 = Node->getOperand(1);
02457       Tmp3 = Node->getOperand(2);  // Signness
02458       switch (getTypeAction(Tmp2.getValueType())) {
02459       case Legal:
02460         Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
02461         break;
02462       case Expand:
02463         if (!Tmp2.getValueType().isVector()) {
02464           SDValue Lo, Hi;
02465           ExpandOp(Tmp2, Lo, Hi);
02466 
02467           // Big endian systems want the hi reg first.
02468           if (TLI.isBigEndian())
02469             std::swap(Lo, Hi);
02470           
02471           if (Hi.getNode())
02472             Result = DAG.getNode(ISD::RET, dl, MVT::Other, 
02473                                  Tmp1, Lo, Tmp3, Hi,Tmp3);
02474           else
02475             Result = DAG.getNode(ISD::RET, dl, MVT::Other, Tmp1, Lo, Tmp3);
02476           Result = LegalizeOp(Result);
02477         } else {
02478           SDNode *InVal = Tmp2.getNode();
02479           int InIx = Tmp2.getResNo();
02480           unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
02481           MVT EVT = InVal->getValueType(InIx).getVectorElementType();
02482           
02483           // Figure out if there is a simple type corresponding to this Vector
02484           // type.  If so, convert to the vector type.
02485           MVT TVT = MVT::getVectorVT(EVT, NumElems);
02486           if (TLI.isTypeLegal(TVT)) {
02487             // Turn this into a return of the vector type.
02488             Tmp2 = LegalizeOp(Tmp2);
02489             Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
02490           } else if (NumElems == 1) {
02491             // Turn this into a return of the scalar type.
02492             Tmp2 = ScalarizeVectorOp(Tmp2);
02493             Tmp2 = LegalizeOp(Tmp2);
02494             Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
02495             
02496             // FIXME: Returns of gcc generic vectors smaller than a legal type
02497             // should be returned in integer registers!
02498             
02499             // The scalarized value type may not be legal, e.g. it might require
02500             // promotion or expansion.  Relegalize the return.
02501             Result = LegalizeOp(Result);
02502           } else {
02503             // FIXME: Returns of gcc generic vectors larger than a legal vector
02504             // type should be returned by reference!
02505             SDValue Lo, Hi;
02506             SplitVectorOp(Tmp2, Lo, Hi);
02507             Result = DAG.getNode(ISD::RET, dl, MVT::Other, 
02508                                  Tmp1, Lo, Tmp3, Hi,Tmp3);
02509             Result = LegalizeOp(Result);
02510           }
02511         }
02512         break;
02513       case Promote:
02514         Tmp2 = PromoteOp(Node->getOperand(1));
02515         Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
02516         Result = LegalizeOp(Result);
02517         break;
02518       }
02519       break;
02520     case 1:  // ret void
02521       Result = DAG.UpdateNodeOperands(Result, Tmp1);
02522       break;
02523     default: { // ret <values>
02524       SmallVector<SDValue, 8> NewValues;
02525       NewValues.push_back(Tmp1);
02526       for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
02527         switch (getTypeAction(Node->getOperand(i).getValueType())) {
02528         case Legal:
02529           NewValues.push_back(LegalizeOp(Node->getOperand(i)));
02530           NewValues.push_back(Node->getOperand(i+1));
02531           break;
02532         case Expand: {
02533           SDValue Lo, Hi;
02534           assert(!Node->getOperand(i).getValueType().isExtended() &&
02535                  "FIXME: TODO: implement returning non-legal vector types!");
02536           ExpandOp(Node->getOperand(i), Lo, Hi);
02537           NewValues.push_back(Lo);
02538           NewValues.push_back(Node->getOperand(i+1));
02539           if (Hi.getNode()) {
02540             NewValues.push_back(Hi);
02541             NewValues.push_back(Node->getOperand(i+1));
02542           }
02543           break;
02544         }
02545         case Promote:
02546           assert(0 && "Can't promote multiple return value yet!");
02547         }
02548           
02549       if (NewValues.size() == Node->getNumOperands())
02550         Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
02551       else
02552         Result = DAG.getNode(ISD::RET, dl, MVT::Other,
02553                              &NewValues[0], NewValues.size());
02554       break;
02555     }
02556     }
02557 
02558     if (Result.getOpcode() == ISD::RET) {
02559       switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
02560       default: assert(0 && "This action is not supported yet!");
02561       case TargetLowering::Legal: break;
02562       case TargetLowering::Custom:
02563         Tmp1 = TLI.LowerOperation(Result, DAG);
02564         if (Tmp1.getNode()) Result = Tmp1;
02565         break;
02566       }
02567     }
02568     break;
02569   case ISD::STORE: {
02570     StoreSDNode *ST = cast<StoreSDNode>(Node);
02571     Tmp1 = LegalizeOp(ST->getChain());    // Legalize the chain.
02572     Tmp2 = LegalizeOp(ST->getBasePtr());  // Legalize the pointer.
02573     int SVOffset = ST->getSrcValueOffset();
02574     unsigned Alignment = ST->getAlignment();
02575     bool isVolatile = ST->isVolatile();
02576 
02577     if (!ST->isTruncatingStore()) {
02578       // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
02579       // FIXME: We shouldn't do this for TargetConstantFP's.
02580       // FIXME: move this to the DAG Combiner!  Note that we can't regress due
02581       // to phase ordering between legalized code and the dag combiner.  This
02582       // probably means that we need to integrate dag combiner and legalizer
02583       // together.
02584       // We generally can't do this one for long doubles.
02585       if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
02586         if (CFP->getValueType(0) == MVT::f32 && 
02587             getTypeAction(MVT::i32) == Legal) {
02588           Tmp3 = DAG.getConstant(CFP->getValueAPF().
02589                                           bitcastToAPInt().zextOrTrunc(32),
02590                                   MVT::i32);
02591           Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
02592                                 SVOffset, isVolatile, Alignment);
02593           break;
02594         } else if (CFP->getValueType(0) == MVT::f64) {
02595           // If this target supports 64-bit registers, do a single 64-bit store.
02596           if (getTypeAction(MVT::i64) == Legal) {
02597             Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
02598                                      zextOrTrunc(64), MVT::i64);
02599             Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
02600                                   SVOffset, isVolatile, Alignment);
02601             break;
02602           } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
02603             // Otherwise, if the target supports 32-bit registers, use 2 32-bit
02604             // stores.  If the target supports neither 32- nor 64-bits, this
02605             // xform is certainly not worth it.
02606             const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
02607             SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
02608             SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
02609             if (TLI.isBigEndian()) std::swap(Lo, Hi);
02610 
02611             Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
02612                               SVOffset, isVolatile, Alignment);
02613             Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
02614                                DAG.getIntPtrConstant(4));
02615             Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
02616                               isVolatile, MinAlign(Alignment, 4U));
02617 
02618             Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
02619             break;
02620           }
02621         }
02622       }
02623       
02624       switch (getTypeAction(ST->getMemoryVT())) {
02625       case Legal: {
02626         Tmp3 = LegalizeOp(ST->getValue());
02627         Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, 
02628                                         ST->getOffset());
02629 
02630         MVT VT = Tmp3.getValueType();
02631         switch (TLI.getOperationAction(ISD::STORE, VT)) {
02632         default: assert(0 && "This action is not supported yet!");
02633         case TargetLowering::Legal:
02634           // If this is an unaligned store and the target doesn't support it,
02635           // expand it.
02636           if (!TLI.allowsUnalignedMemoryAccesses()) {
02637             unsigned ABIAlignment = TLI.getTargetData()->
02638               getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
02639             if (ST->getAlignment() < ABIAlignment)
02640               Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
02641                                             TLI);
02642           }
02643           break;
02644         case TargetLowering::Custom:
02645           Tmp1 = TLI.LowerOperation(Result, DAG);
02646           if (Tmp1.getNode()) Result = Tmp1;
02647           break;
02648         case TargetLowering::Promote:
02649           assert(VT.isVector() && "Unknown legal promote case!");
02650           Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl, 
02651                              TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
02652           Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
02653                                 ST->getSrcValue(), SVOffset, isVolatile,
02654                                 Alignment);
02655           break;
02656         }
02657         break;
02658       }
02659       case Promote:
02660         if (!ST->getMemoryVT().isVector()) {
02661           // Truncate the value and store the result.
02662           Tmp3 = PromoteOp(ST->getValue());
02663           Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
02664                                      SVOffset, ST->getMemoryVT(),
02665                                      isVolatile, Alignment);
02666           break;
02667         }
02668         // Fall thru to expand for vector
02669       case Expand: {
02670         unsigned IncrementSize = 0;
02671         SDValue Lo, Hi;
02672       
02673         // If this is a vector type, then we have to calculate the increment as
02674         // the product of the element size in bytes, and the number of elements
02675         // in the high half of the vector.
02676         if (ST->getValue().getValueType().isVector()) {
02677           SDNode *InVal = ST->getValue().getNode();
02678           int InIx = ST->getValue().getResNo();
02679           MVT InVT = InVal->getValueType(InIx);
02680           unsigned NumElems = InVT.getVectorNumElements();
02681           MVT EVT = InVT.getVectorElementType();
02682 
02683           // Figure out if there is a simple type corresponding to this Vector
02684           // type.  If so, convert to the vector type.
02685           MVT TVT = MVT::getVectorVT(EVT, NumElems);
02686           if (TLI.isTypeLegal(TVT)) {
02687             // Turn this into a normal store of the vector type.
02688             Tmp3 = LegalizeOp(ST->getValue());
02689             Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
02690                                   SVOffset, isVolatile, Alignment);
02691             Result = LegalizeOp(Result);
02692             break;
02693           } else if (NumElems == 1) {
02694             // Turn this into a normal store of the scalar type.
02695             Tmp3 = ScalarizeVectorOp(ST->getValue());
02696             Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
02697                                   SVOffset, isVolatile, Alignment);
02698             // The scalarized value type may not be legal, e.g. it might require
02699             // promotion or expansion.  Relegalize the scalar store.
02700             Result = LegalizeOp(Result);
02701             break;
02702           } else {
02703             // Check if we have widen this node with another value
02704             std::map<SDValue, SDValue>::iterator I =
02705               WidenNodes.find(ST->getValue());
02706             if (I != WidenNodes.end()) {
02707               Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
02708               break;
02709             }
02710             else {
02711               SplitVectorOp(ST->getValue(), Lo, Hi);
02712               IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
02713                               EVT.getSizeInBits()/8;
02714             }
02715           }
02716         } else {
02717           ExpandOp(ST->getValue(), Lo, Hi);
02718           IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
02719 
02720           if (Hi.getNode() && TLI.isBigEndian())
02721             std::swap(Lo, Hi);
02722         }
02723 
02724         Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
02725                           SVOffset, isVolatile, Alignment);
02726 
02727         if (Hi.getNode() == NULL) {
02728           // Must be int <-> float one-to-one expansion.
02729           Result = Lo;
02730           break;
02731         }
02732 
02733         Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
02734                            DAG.getIntPtrConstant(IncrementSize));
02735         assert(isTypeLegal(Tmp2.getValueType()) &&
02736                "Pointers must be legal!");
02737         SVOffset += IncrementSize;
02738         Alignment = MinAlign(Alignment, IncrementSize);
02739         Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
02740                           SVOffset, isVolatile, Alignment);
02741         Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
02742         break;
02743       }  // case Expand
02744       }
02745     } else {
02746       switch (getTypeAction(ST->getValue().getValueType())) {
02747       case Legal:
02748         Tmp3 = LegalizeOp(ST->getValue());
02749         break;
02750       case Promote:
02751         if (!ST->getValue().getValueType().isVector()) {
02752           // We can promote the value, the truncstore will still take care of it.
02753           Tmp3 = PromoteOp(ST->getValue());
02754           break;
02755         }
02756         // Vector case falls through to expand
02757       case Expand:
02758         // Just store the low part.  This may become a non-trunc store, so make
02759         // sure to use getTruncStore, not UpdateNodeOperands below.
02760         ExpandOp(ST->getValue(), Tmp3, Tmp4);
02761         return DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
02762                                  SVOffset, MVT::i8, isVolatile, Alignment);
02763       }
02764 
02765       MVT StVT = ST->getMemoryVT();
02766       unsigned StWidth = StVT.getSizeInBits();
02767 
02768       if (StWidth != StVT.getStoreSizeInBits()) {
02769         // Promote to a byte-sized store with upper bits zero if not
02770         // storing an integral number of bytes.  For example, promote
02771         // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
02772         MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
02773         Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
02774         Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
02775                                    SVOffset, NVT, isVolatile, Alignment);
02776       } else if (StWidth & (StWidth - 1)) {
02777         // If not storing a power-of-2 number of bits, expand as two stores.
02778         assert(StVT.isExtended() && !StVT.isVector() &&
02779                "Unsupported truncstore!");
02780         unsigned RoundWidth = 1 << Log2_32(StWidth);
02781         assert(RoundWidth < StWidth);
02782         unsigned ExtraWidth = StWidth - RoundWidth;
02783         assert(ExtraWidth < RoundWidth);
02784         assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
02785                "Store size not an integral number of bytes!");
02786         MVT RoundVT = MVT::getIntegerVT(RoundWidth);
02787         MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
02788         SDValue Lo, Hi;
02789         unsigned IncrementSize;
02790 
02791         if (TLI.isLittleEndian()) {
02792           // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
02793           // Store the bottom RoundWidth bits.
02794           Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
02795                                  SVOffset, RoundVT,
02796                                  isVolatile, Alignment);
02797 
02798           // Store the remaining ExtraWidth bits.
02799           IncrementSize = RoundWidth / 8;
02800           Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
02801                              DAG.getIntPtrConstant(IncrementSize));
02802           Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
02803                            DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
02804           Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
02805                                  SVOffset + IncrementSize, ExtraVT, isVolatile,
02806                                  MinAlign(Alignment, IncrementSize));
02807         } else {
02808           // Big endian - avoid unaligned stores.
02809           // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
02810           // Store the top RoundWidth bits.
02811           Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
02812                            DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
02813           Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
02814                                  SVOffset, RoundVT, isVolatile, Alignment);
02815 
02816           // Store the remaining ExtraWidth bits.
02817           IncrementSize = RoundWidth / 8;
02818           Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
02819                              DAG.getIntPtrConstant(IncrementSize));
02820           Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
02821                                  SVOffset + IncrementSize, ExtraVT, isVolatile,
02822                                  MinAlign(Alignment, IncrementSize));
02823         }
02824 
02825         // The order of the stores doesn't matter.
02826         Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
02827       } else {
02828         if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
02829             Tmp2 != ST->getBasePtr())
02830           Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
02831                                           ST->getOffset());
02832 
02833         switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
02834         default: assert(0 && "This action is not supported yet!");
02835         case TargetLowering::Legal:
02836           // If this is an unaligned store and the target doesn't support it,
02837           // expand it.
02838           if (!TLI.allowsUnalignedMemoryAccesses()) {
02839             unsigned ABIAlignment = TLI.getTargetData()->
02840               getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
02841             if (ST->getAlignment() < ABIAlignment)
02842               Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
02843                                             TLI);
02844           }
02845           break;
02846         case TargetLowering::Custom:
02847           Result = TLI.LowerOperation(Result, DAG);
02848           break;
02849         case Expand:
02850           // TRUNCSTORE:i16 i32 -> STORE i16
02851           assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
02852           Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
02853           Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
02854                                 SVOffset, isVolatile, Alignment);
02855           break;
02856         }
02857       }
02858     }
02859     break;
02860   }
02861   case ISD::PCMARKER:
02862     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
02863     Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
02864     break;
02865   case ISD::STACKSAVE:
02866     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
02867     Result = DAG.UpdateNodeOperands(Result, Tmp1);
02868     Tmp1 = Result.getValue(0);
02869     Tmp2 = Result.getValue(1);
02870     
02871     switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
02872     default: assert(0 && "This action is not supported yet!");
02873     case TargetLowering::Legal: break;
02874     case TargetLowering::Custom:
02875       Tmp3 = TLI.LowerOperation(Result, DAG);
02876       if (Tmp3.getNode()) {
02877         Tmp1 = LegalizeOp(Tmp3);
02878         Tmp2 = LegalizeOp(Tmp3.getValue(1));
02879       }
02880       break;
02881     case TargetLowering::Expand:
02882       // Expand to CopyFromReg if the target set 
02883       // StackPointerRegisterToSaveRestore.
02884       if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
02885         Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
02886                                   Node->getValueType(0));
02887         Tmp2 = Tmp1.getValue(1);
02888       } else {
02889         Tmp1 = DAG.getNode(ISD::UNDEF, dl, Node->getValueType(0));
02890         Tmp2 = Node->getOperand(0);
02891       }
02892       break;
02893     }
02894 
02895     // Since stacksave produce two values, make sure to remember that we
02896     // legalized both of them.
02897     AddLegalizedOperand(SDValue(Node, 0), Tmp1);
02898     AddLegalizedOperand(SDValue(Node, 1), Tmp2);
02899     return Op.getResNo() ? Tmp2 : Tmp1;
02900 
02901   case ISD::STACKRESTORE:
02902     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
02903     Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the pointer.
02904     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
02905       
02906     switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
02907     default: assert(0 && "This action is not supported yet!");
02908     case TargetLowering::Legal: break;
02909     case TargetLowering::Custom:
02910       Tmp1 = TLI.LowerOperation(Result, DAG);
02911       if (Tmp1.getNode()) Result = Tmp1;
02912       break;
02913     case TargetLowering::Expand:
02914       // Expand to CopyToReg if the target set 
02915       // StackPointerRegisterToSaveRestore.
02916       if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
02917         Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
02918       } else {
02919         Result = Tmp1;
02920       }
02921       break;
02922     }
02923     break;
02924 
02925   case ISD::READCYCLECOUNTER:
02926     Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
02927     Result = DAG.UpdateNodeOperands(Result, Tmp1);
02928     switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
02929                                    Node->getValueType(0))) {
02930     default: assert(0 && "This action is not supported yet!");
02931     case TargetLowering::Legal:
02932       Tmp1 = Result.getValue(0);
02933       Tmp2 = Result.getValue(1);
02934       break;
02935     case TargetLowering::Custom:
02936       Result = TLI.LowerOperation(Result, DAG);
02937       Tmp1 = LegalizeOp(Result.getValue(0));
02938       Tmp2 = LegalizeOp(Result.getValue(1));
02939       break;
02940     }
02941 
02942     // Since rdcc produce two values, make sure to remember that we legalized
02943     // both of them.
02944     AddLegalizedOperand(SDValue(Node, 0), Tmp1);
02945     AddLegalizedOperand(SDValue(Node, 1), Tmp2);
02946     return Result;
02947 
02948   case ISD::SELECT:
02949     switch (getTypeAction(Node->getOperand(0).getValueType())) {
02950     case Expand: assert(0 && "It's impossible to expand bools");
02951     case Legal:
02952       Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
02953       break;
02954     case Promote: {
02955       assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
02956       Tmp1 = PromoteOp(Node->getOperand(0));  // Promote the condition.
02957       // Make sure the condition is either zero or one.
02958       unsigned BitWidth = Tmp1.getValueSizeInBits();
02959       if (!DAG.MaskedValueIsZero(Tmp1,
02960                                  APInt::getHighBitsSet(BitWidth, BitWidth-1)))
02961         Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, MVT::i1);
02962       break;
02963     }
02964     }
02965     Tmp2 = LegalizeOp(Node->getOperand(1));   // TrueVal
02966     Tmp3 = LegalizeOp(Node->getOperand(2));   // FalseVal
02967 
02968     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
02969       
02970     switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
02971     default: assert(0 && "This action is not supported yet!");
02972     case TargetLowering::Legal: break;
02973     case TargetLowering::Custom: {
02974       Tmp1 = TLI.LowerOperation(Result, DAG);
02975       if (Tmp1.getNode()) Result = Tmp1;
02976       break;
02977     }
02978     case TargetLowering::Expand:
02979       if (Tmp1.getOpcode() == ISD::SETCC) {
02980         Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1), 
02981                               Tmp2, Tmp3,
02982                               cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
02983       } else {
02984         Result = DAG.getSelectCC(dl, Tmp1, 
02985                                  DAG.getConstant(0, Tmp1.getValueType()),
02986                                  Tmp2, Tmp3, ISD::SETNE);
02987       }
02988       break;
02989     case TargetLowering::Promote: {
02990       MVT NVT =
02991         TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
02992       unsigned ExtOp, TruncOp;
02993       if (Tmp2.getValueType().isVector()) {
02994         ExtOp   = ISD::BIT_CONVERT;
02995         TruncOp = ISD::BIT_CONVERT;
02996       } else if (Tmp2.getValueType().isInteger()) {
02997         ExtOp   = ISD::ANY_EXTEND;
02998         TruncOp = ISD::TRUNCATE;
02999       } else {
03000         ExtOp   = ISD::FP_EXTEND;
03001         TruncOp = ISD::FP_ROUND;
03002       }
03003       // Promote each of the values to the new type.
03004       Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
03005       Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
03006       // Perform the larger operation, then round down.
03007       Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2,Tmp3);
03008       if (TruncOp != ISD::FP_ROUND)
03009         Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
03010       else
03011         Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
03012                              DAG.getIntPtrConstant(0));
03013       break;
03014     }
03015     }
03016     break;
03017   case ISD::SELECT_CC: {
03018     Tmp1 = Node->getOperand(0);               // LHS
03019     Tmp2 = Node->getOperand(1);               // RHS
03020     Tmp3 = LegalizeOp(Node->getOperand(2));   // True
03021     Tmp4 = LegalizeOp(Node->getOperand(3));   // False
03022     SDValue CC = Node->getOperand(4);
03023     
03024     LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), 
03025                   Tmp1, Tmp2, CC, dl);
03026     
03027     // If we didn't get both a LHS and RHS back from LegalizeSetCC,
03028     // the LHS is a legal SETCC itself.  In this case, we need to compare
03029     // the result against zero to select between true and false values.
03030     if (Tmp2.getNode() == 0) {
03031       Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
03032       CC = DAG.getCondCode(ISD::SETNE);
03033     }
03034     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
03035 
03036     // Everything is legal, see if we should expand this op or something.
03037     switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
03038     default: assert(0 && "This action is not supported yet!");
03039     case TargetLowering::Legal: break;
03040     case TargetLowering::Custom:
03041       Tmp1 = TLI.LowerOperation(Result, DAG);
03042       if (Tmp1.getNode()) Result = Tmp1;
03043       break;
03044     }
03045     break;
03046   }
03047   case ISD::SETCC:
03048     Tmp1 = Node->getOperand(0);
03049     Tmp2 = Node->getOperand(1);
03050     Tmp3 = Node->getOperand(2);
03051     LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
03052     
03053     // If we had to Expand the SetCC operands into a SELECT node, then it may 
03054     // not always be possible to return a true LHS & RHS.  In this case, just 
03055     // return the value we legalized, returned in the LHS
03056     if (Tmp2.getNode() == 0) {
03057       Result = Tmp1;
03058       break;
03059     }
03060 
03061     switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
03062     default: assert(0 && "Cannot handle this action for SETCC yet!");
03063     case TargetLowering::Custom:
03064       isCustom = true;
03065       // FALLTHROUGH.
03066     case TargetLowering::Legal:
03067       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
03068       if (isCustom) {
03069         Tmp4 = TLI.LowerOperation(Result, DAG);
03070         if (Tmp4.getNode()) Result = Tmp4;
03071       }
03072       break;
03073     case TargetLowering::Promote: {
03074       // First step, figure out the appropriate operation to use.
03075       // Allow SETCC to not be supported for all legal data types
03076       // Mostly this targets FP
03077       MVT NewInTy = Node->getOperand(0).getValueType();
03078       MVT OldVT = NewInTy; OldVT = OldVT;
03079 
03080       // Scan for the appropriate larger type to use.
03081       while (1) {
03082         NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
03083 
03084         assert(NewInTy.isInteger() == OldVT.isInteger() &&
03085                "Fell off of the edge of the integer world");
03086         assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
03087                "Fell off of the edge of the floating point world");
03088           
03089         // If the target supports SETCC of this type, use it.
03090         if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
03091           break;
03092       }
03093       if (NewInTy.isInteger())
03094         assert(0 && "Cannot promote Legal Integer SETCC yet");
03095       else {
03096         Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
03097         Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
03098       }
03099       Tmp1 = LegalizeOp(Tmp1);
03100       Tmp2 = LegalizeOp(Tmp2);
03101       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
03102       Result = LegalizeOp(Result);
03103       break;
03104     }
03105     case TargetLowering::Expand:
03106       // Expand a setcc node into a select_cc of the same condition, lhs, and
03107       // rhs that selects between const 1 (true) and const 0 (false).
03108       MVT VT = Node->getValueType(0);
03109       Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2, 
03110                            DAG.getConstant(1, VT), DAG.getConstant(0, VT),
03111                            Tmp3);
03112       break;
03113     }
03114     break;
03115   case ISD::VSETCC: {
03116     Tmp1 = LegalizeOp(Node->getOperand(0));   // LHS
03117     Tmp2 = LegalizeOp(Node->getOperand(1));   // RHS
03118     SDValue CC = Node->getOperand(2);
03119     
03120     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
03121 
03122     // Everything is legal, see if we should expand this op or something.
03123     switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
03124     default: assert(0 && "This action is not supported yet!");
03125     case TargetLowering::Legal: break;
03126     case TargetLowering::Custom:
03127       Tmp1 = TLI.LowerOperation(Result, DAG);
03128       if (Tmp1.getNode()) Result = Tmp1;
03129       break;
03130     case TargetLowering::Expand: {
03131       // Unroll into a nasty set of scalar code for now.
03132       MVT VT = Node->getValueType(0);
03133       unsigned NumElems = VT.getVectorNumElements();
03134       MVT EltVT = VT.getVectorElementType();
03135       MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
03136       SmallVector<SDValue, 8> Ops(NumElems);
03137       for (unsigned i = 0; i < NumElems; ++i) {
03138         SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT,
03139                                   Tmp1, DAG.getIntPtrConstant(i));
03140         Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
03141                              In1, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
03142                                               TmpEltVT, Tmp2,
03143                                               DAG.getIntPtrConstant(i)),
03144                              CC);
03145         Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i], DAG.getConstant(
03146                                   APInt::getAllOnesValue(EltVT.getSizeInBits()),
03147                                   EltVT), DAG.getConstant(0, EltVT));
03148       }
03149       Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
03150       break;
03151     }
03152     }
03153     break;
03154   }
03155 
03156   case ISD::SHL_PARTS:
03157   case ISD::SRA_PARTS:
03158   case ISD::SRL_PARTS: {
03159     SmallVector<SDValue, 8> Ops;
03160     bool Changed = false;
03161     unsigned N = Node->getNumOperands();
03162     for (unsigned i = 0; i + 1 < N; ++i) {
03163       Ops.push_back(LegalizeOp(Node->getOperand(i)));
03164       Changed |= Ops.back() != Node->getOperand(i);
03165     }
03166     Ops.push_back(LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(N-1))));
03167     Changed |= Ops.back() != Node->getOperand(N-1);
03168     if (Changed)
03169       Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
03170 
03171     switch (TLI.getOperationAction(Node->getOpcode(),
03172                                    Node->getValueType(0))) {
03173     default: assert(0 && "This action is not supported yet!");
03174     case TargetLowering::Legal: break;
03175     case TargetLowering::Custom:
03176       Tmp1 = TLI.LowerOperation(Result, DAG);
03177       if (Tmp1.getNode()) {
03178         SDValue Tmp2, RetVal(0, 0);
03179         for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
03180           Tmp2 = LegalizeOp(Tmp1.getValue(i));
03181           AddLegalizedOperand(SDValue(Node, i), Tmp2);
03182           if (i == Op.getResNo())
03183             RetVal = Tmp2;
03184         }
03185         assert(RetVal.getNode() && "Illegal result number");
03186         return RetVal;
03187       }
03188       break;
03189     }
03190 
03191     // Since these produce multiple values, make sure to remember that we
03192     // legalized all of them.
03193     for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
03194       AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
03195     return Result.getValue(Op.getResNo());
03196   }
03197 
03198     // Binary operators
03199   case ISD::ADD:
03200   case ISD::SUB:
03201   case ISD::MUL:
03202   case ISD::MULHS:
03203   case ISD::MULHU:
03204   case ISD::UDIV:
03205   case ISD::SDIV:
03206   case ISD::AND:
03207   case ISD::OR:
03208   case ISD::XOR:
03209   case ISD::SHL:
03210   case ISD::SRL:
03211   case ISD::SRA:
03212   case ISD::FADD:
03213   case ISD::FSUB:
03214   case ISD::FMUL:
03215   case ISD::FDIV:
03216   case ISD::FPOW:
03217     Tmp1 = LegalizeOp(Node->getOperand(0));   // LHS
03218     Tmp2 = LegalizeOp(Node->getOperand(1));   // RHS
03219 
03220     if ((Node->getOpcode() == ISD::SHL ||
03221          Node->getOpcode() == ISD::SRL ||
03222          Node->getOpcode() == ISD::SRA) &&
03223         !Node->getValueType(0).isVector())
03224       Tmp2 = DAG.getShiftAmountOperand(Tmp2);
03225 
03226     switch (getTypeAction(Tmp2.getValueType())) {
03227     case Expand: assert(0 && "Not possible");
03228     case Legal:
03229       Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS.
03230       break;
03231     case Promote:
03232       Tmp2 = PromoteOp(Tmp2);  // Promote the RHS.
03233       break;
03234     }
03235 
03236     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
03237 
03238     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
03239     default: assert(0 && "BinOp legalize operation not supported");
03240     case TargetLowering::Legal: break;
03241     case TargetLowering::Custom:
03242       Tmp1 = TLI.LowerOperation(Result, DAG);
03243       if (Tmp1.getNode()) {
03244         Result = Tmp1;
03245         break;
03246       }
03247       // Fall through if the custom lower can't deal with the operation
03248     case TargetLowering::Expand: {
03249       MVT VT = Op.getValueType();
03250       
03251       // See if multiply or divide can be lowered using two-result operations.
03252       SDVTList VTs = DAG.getVTList(VT, VT);
03253       if (Node->getOpcode() == ISD::MUL) {
03254         // We just need the low half of the multiply; try both the signed
03255         // and unsigned forms. If the target supports both SMUL_LOHI and
03256         // UMUL_LOHI, form a preference by checking which forms of plain
03257         // MULH it supports.
03258         bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
03259         bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
03260         bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
03261         bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
03262         unsigned OpToUse = 0;
03263         if (HasSMUL_LOHI && !HasMULHS) {
03264           OpToUse = ISD::SMUL_LOHI;
03265         } else if (HasUMUL_LOHI && !HasMULHU) {
03266           OpToUse = ISD::UMUL_LOHI;
03267         } else if (HasSMUL_LOHI) {
03268           OpToUse = ISD::SMUL_LOHI;
03269         } else if (HasUMUL_LOHI) {
03270           OpToUse = ISD::UMUL_LOHI;
03271         }
03272         if (OpToUse) {
03273           Result = SDValue(DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2).getNode(),
03274                            0);
03275           break;
03276         }
03277       }
03278       if (Node->getOpcode() == ISD::MULHS &&
03279           TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
03280         Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, 
03281                                      VTs, Tmp1, Tmp2).getNode(),
03282                          1);
03283         break;
03284       }
03285       if (Node->getOpcode() == ISD::MULHU && 
03286           TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
03287         Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl,
03288                                      VTs, Tmp1, Tmp2).getNode(),
03289                          1);
03290         break;
03291       }
03292       if (Node->getOpcode() == ISD::SDIV &&
03293           TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
03294         Result = SDValue(DAG.getNode(ISD::SDIVREM, dl, 
03295                                      VTs, Tmp1, Tmp2).getNode(),
03296                          0);
03297         break;
03298       }
03299       if (Node->getOpcode() == ISD::UDIV &&
03300           TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
03301         Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
03302                                      VTs, Tmp1, Tmp2).getNode(),
03303                          0);
03304         break;
03305       }
03306 
03307       // Check to see if we have a libcall for this operator.
03308       RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
03309       bool isSigned = false;
03310       switch (Node->getOpcode()) {
03311       case ISD::UDIV:
03312       case ISD::SDIV:
03313         if (VT == MVT::i32) {
03314           LC = Node->getOpcode() == ISD::UDIV
03315                ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
03316           isSigned = Node->getOpcode() == ISD::SDIV;
03317         }
03318         break;
03319       case ISD::MUL:
03320         if (VT == MVT::i32)
03321           LC = RTLIB::MUL_I32;
03322         else if (VT == MVT::i64)
03323           LC = RTLIB::MUL_I64;
03324         break;
03325       case ISD::FPOW:
03326         LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
03327                           RTLIB::POW_PPCF128);
03328         break;
03329       case ISD::FDIV:
03330         LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
03331                           RTLIB::DIV_PPCF128);
03332         break;
03333       default: break;
03334       }
03335       if (LC != RTLIB::UNKNOWN_LIBCALL) {
03336         SDValue Dummy;
03337         Result = ExpandLibCall(LC, Node, isSigned, Dummy);
03338         break;
03339       }
03340       
03341       assert(Node->getValueType(0).isVector() &&
03342              "Cannot expand this binary operator!");
03343       // Expand the operation into a bunch of nasty scalar code.
03344       Result = LegalizeOp(UnrollVectorOp(Op));
03345       break;
03346     }
03347     case TargetLowering::Promote: {
03348       switch (Node->getOpcode()) {
03349       default:  assert(0 && "Do not know how to promote this BinOp!");
03350       case ISD::AND:
03351       case ISD::OR:
03352       case ISD::XOR: {
03353         MVT OVT = Node->getValueType(0);
03354         MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
03355         assert(OVT.isVector() && "Cannot promote this BinOp!");
03356         // Bit convert each of the values to the new type.
03357         Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
03358         Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
03359         Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
03360         // Bit convert the result back the original type.
03361         Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
03362         break;
03363       }
03364       }
03365     }
03366     }
03367     break;
03368     
03369   case ISD::SMUL_LOHI:
03370   case ISD::UMUL_LOHI:
03371   case ISD::SDIVREM:
03372   case ISD::UDIVREM:
03373     // These nodes will only be produced by target-specific lowering, so
03374     // they shouldn't be here if they aren't legal.
03375     assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
03376            "This must be legal!");
03377 
03378     Tmp1 = LegalizeOp(Node->getOperand(0));   // LHS
03379     Tmp2 = LegalizeOp(Node->getOperand(1));   // RHS
03380     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
03381     break;
03382 
03383   case ISD::FCOPYSIGN:  // FCOPYSIGN does not require LHS/RHS to match type!
03384     Tmp1 = LegalizeOp(Node->getOperand(0));   // LHS
03385     switch (getTypeAction(Node->getOperand(1).getValueType())) {
03386       case Expand: assert(0 && "Not possible");
03387       case Legal:
03388         Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
03389         break;
03390       case Promote:
03391         Tmp2 = PromoteOp(Node->getOperand(1));  // Promote the RHS.
03392         break;
03393     }
03394       
03395     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
03396     
03397     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
03398     default: assert(0 && "Operation not supported");
03399     case TargetLowering::Custom:
03400       Tmp1 = TLI.LowerOperation(Result, DAG);
03401       if (Tmp1.getNode()) Result = Tmp1;
03402       break;
03403     case TargetLowering::Legal: break;
03404     case TargetLowering::Expand: {
03405       // If this target supports fabs/fneg natively and select is cheap,
03406       // do this efficiently.
03407       if (!TLI.isSelectExpensive() &&
03408           TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
03409           TargetLowering::Legal &&
03410           TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
03411           TargetLowering::Legal) {
03412         // Get the sign bit of the RHS.
03413         MVT IVT =
03414           Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
03415         SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
03416         SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(IVT),
03417                                SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
03418         // Get the absolute value of the result.
03419         SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
03420         // Select between the nabs and abs value based on the sign bit of
03421         // the input.
03422         Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
03423                              DAG.getNode(ISD::FNEG, AbsVal.getValueType(), 
03424                                          AbsVal),
03425                              AbsVal);
03426         Result = LegalizeOp(Result);
03427         break;
03428       }
03429       
03430       // Otherwise, do bitwise ops!
03431       MVT NVT =
03432         Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
03433       Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
03434       Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0), Result);
03435       Result = LegalizeOp(Result);
03436       break;
03437     }
03438     }
03439     break;
03440     
03441   case ISD::ADDC:
03442   case ISD::SUBC:
03443     Tmp1 = LegalizeOp(Node->getOperand(0));
03444     Tmp2 = LegalizeOp(Node->getOperand(1));
03445     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
03446     Tmp3 = Result.getValue(0);
03447     Tmp4 = Result.getValue(1);
03448 
03449     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
03450     default: assert(0 && "This action is not supported yet!");
03451     case TargetLowering::Legal:
03452       break;
03453     case TargetLowering::Custom:
03454       Tmp1 = TLI.LowerOperation(Tmp3, DAG);
03455       if (Tmp1.getNode() != NULL) {
03456         Tmp3 = LegalizeOp(Tmp1);
03457         Tmp4 = LegalizeOp(Tmp1.getValue(1));
03458       }
03459       break;
03460     }
03461     // Since this produces two values, make sure to remember that we legalized
03462     // both of them.
03463     AddLegalizedOperand(SDValue(Node, 0), Tmp3);
03464     AddLegalizedOperand(SDValue(Node, 1), Tmp4);
03465     return Op.getResNo() ? Tmp4 : Tmp3;
03466 
03467   case ISD::ADDE:
03468   case ISD::SUBE:
03469     Tmp1 = LegalizeOp(Node->getOperand(0));
03470     Tmp2 = LegalizeOp(Node->getOperand(1));
03471     Tmp3 = LegalizeOp(Node->getOperand(2));
03472     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
03473     Tmp3 = Result.getValue(0);
03474     Tmp4 = Result.getValue(1);
03475 
03476     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
03477     default: assert(0 && "This action is not supported yet!");
03478     case TargetLowering::Legal:
03479       break;
03480     case TargetLowering::Custom:
03481       Tmp1 = TLI.LowerOperation(Tmp3, DAG);
03482       if (Tmp1.getNode() != NULL) {
03483         Tmp3 = LegalizeOp(Tmp1);
03484         Tmp4 = LegalizeOp(Tmp1.getValue(1));
03485       }
03486       break;
03487     }
03488     // Since this produces two values, make sure to remember that we legalized
03489     // both of them.
03490     AddLegalizedOperand(SDValue(Node, 0), Tmp3);
03491     AddLegalizedOperand(SDValue(Node, 1), Tmp4);
03492     return Op.getResNo() ? Tmp4 : Tmp3;
03493     
03494   case ISD::BUILD_PAIR: {
03495     MVT PairTy = Node->getValueType(0);
03496     // TODO: handle the case where the Lo and Hi operands are not of legal type
03497     Tmp1 = LegalizeOp(Node->getOperand(0));   // Lo
03498     Tmp2 = LegalizeOp(Node->getOperand(1));   // Hi
03499     switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
03500     case TargetLowering::Promote:
03501     case TargetLowering::Custom:
03502       assert(0 && "Cannot promote/custom this yet!");
03503     case TargetLowering::Legal:
03504       if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
03505         Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
03506       break;
03507     case TargetLowering::Expand:
03508       Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
03509       Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
03510       Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
03511                          DAG.getConstant(PairTy.getSizeInBits()/2,
03512                                          TLI.getShiftAmountTy()));
03513       Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
03514       break;
03515     }
03516     break;
03517   }
03518 
03519   case ISD::UREM:
03520   case ISD::SREM:
03521   case ISD::FREM:
03522     Tmp1 = LegalizeOp(Node->getOperand(0));   // LHS
03523     Tmp2 = LegalizeOp(Node->getOperand(1));   // RHS
03524 
03525     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
03526     case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
03527     case TargetLowering::Custom:
03528       isCustom = true;
03529       // FALLTHROUGH
03530     case TargetLowering::Legal:
03531       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
03532       if (isCustom) {
03533         Tmp1 = TLI.LowerOperation(Result, DAG);
03534         if (Tmp1.getNode()) Result = Tmp1;
03535       }
03536       break;
03537     case TargetLowering::Expand: {
03538       unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
03539       bool isSigned = DivOpc == ISD::SDIV;
03540       MVT VT = Node->getValueType(0);
03541  
03542       // See if remainder can be lowered using two-result operations.
03543       SDVTList VTs = DAG.getVTList(VT, VT);
03544       if (Node->getOpcode() == ISD::SREM &&
03545           TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
03546         Result = SDValue(DAG.getNode(ISD::SDIVREM, dl, 
03547                                      VTs, Tmp1, Tmp2).getNode(), 1);
03548         break;
03549       }
03550       if (Node->getOpcode() == ISD::UREM &&
03551           TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
03552         Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
03553                                      VTs, Tmp1, Tmp2).getNode(), 1);
03554         break;
03555       }
03556 
03557       if (VT.isInteger()) {
03558         if (TLI.getOperationAction(DivOpc, VT) ==
03559             TargetLowering::Legal) {
03560           // X % Y -> X-X/Y*Y
03561           Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
03562           Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
03563           Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
03564         } else if (VT.isVector()) {
03565           Result = LegalizeOp(UnrollVectorOp(Op));
03566         } else {
03567           assert(VT == MVT::i32 &&
03568                  "Cannot expand this binary operator!");
03569           RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
03570             ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
03571           SDValue Dummy;
03572           Result = ExpandLibCall(LC, Node, isSigned, Dummy);
03573         }
03574       } else {
03575         assert(VT.isFloatingPoint() &&
03576                "remainder op must have integer or floating-point type");
03577         if (VT.isVector()) {
03578           Result = LegalizeOp(UnrollVectorOp(Op));
03579         } else {
03580           // Floating point mod -> fmod libcall.
03581           RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
03582                                            RTLIB::REM_F80, RTLIB::REM_PPCF128);
03583           SDValue Dummy;
03584           Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
03585         }
03586       }
03587       break;
03588     }
03589     }
03590     break;
03591   case ISD::VAARG: {
03592     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
03593     Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the pointer.
03594 
03595     MVT VT = Node->getValueType(0);
03596     switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
03597     default: assert(0 && "This action is not supported yet!");
03598     case TargetLowering::Custom:
03599       isCustom = true;
03600       // FALLTHROUGH
03601     case TargetLowering::Legal:
03602       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
03603       Result = Result.getValue(0);
03604       Tmp1 = Result.getValue(1);
03605 
03606       if (isCustom) {
03607         Tmp2 = TLI.LowerOperation(Result, DAG);
03608         if (Tmp2.getNode()) {
03609           Result = LegalizeOp(Tmp2);
03610           Tmp1 = LegalizeOp(Tmp2.getValue(1));
03611         }
03612       }
03613       break;
03614     case TargetLowering::Expand: {
03615       const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
03616       SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
03617       // Increment the pointer, VAList, to the next vaarg
03618       Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
03619                          DAG.getConstant(TLI.getTargetData()->
03620                                          getTypePaddedSize(VT.getTypeForMVT()),
03621                                          TLI.getPointerTy()));
03622       // Store the incremented VAList to the legalized pointer
03623       Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
03624       // Load the actual argument out of the pointer VAList
03625       Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
03626       Tmp1 = LegalizeOp(Result.getValue(1));
03627       Result = LegalizeOp(Result);
03628       break;
03629     }
03630     }
03631     // Since VAARG produces two values, make sure to remember that we 
03632     // legalized both of them.
03633     AddLegalizedOperand(SDValue(Node, 0), Result);
03634     AddLegalizedOperand(SDValue(Node, 1), Tmp1);
03635     return Op.getResNo() ? Tmp1 : Result;
03636   }
03637     
03638   case ISD::VACOPY: 
03639     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
03640     Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the dest pointer.
03641     Tmp3 = LegalizeOp(Node->getOperand(2));  // Legalize the source pointer.
03642 
03643     switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
03644     default: assert(0 && "This action is not supported yet!");
03645     case TargetLowering::Custom:
03646       isCustom = true;
03647       // FALLTHROUGH
03648     case TargetLowering::Legal:
03649       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
03650                                       Node->getOperand(3), Node->getOperand(4));
03651       if (isCustom) {
03652         Tmp1 = TLI.LowerOperation(Result, DAG);
03653         if (Tmp1.getNode()) Result = Tmp1;
03654       }
03655       break;
03656     case TargetLowering::Expand:
03657       // This defaults to loading a pointer from the input and storing it to the
03658       // output, returning the chain.
03659       const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
03660       const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
03661       Tmp4 = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp3, VS, 0);
03662       Result = DAG.getStore(Tmp4.getValue(1), dl, Tmp4, Tmp2, VD, 0);
03663       break;
03664     }
03665     break;
03666 
03667   case ISD::VAEND: 
03668     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
03669     Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the pointer.
03670 
03671     switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
03672     default: assert(0 && "This action is not supported yet!");
03673     case TargetLowering::Custom:
03674       isCustom = true;
03675       // FALLTHROUGH
03676     case TargetLowering::Legal:
03677       Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
03678       if (isCustom) {
03679         Tmp1 = TLI.LowerOperation(Tmp1, DAG);
03680         if (Tmp1.getNode()) Result = Tmp1;
03681       }
03682       break;
03683     case TargetLowering::Expand:
03684       Result = Tmp1; // Default to a no-op, return the chain
03685       break;
03686     }
03687     break;
03688     
03689   case ISD::VASTART: 
03690     Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
03691     Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the pointer.
03692 
03693     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
03694     
03695     switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
03696     default: assert(0 && "This action is not supported yet!");
03697     case TargetLowering::Legal: break;
03698     case TargetLowering::Custom:
03699       Tmp1 = TLI.LowerOperation(Result, DAG);
03700       if (Tmp1.getNode()) Result = Tmp1;
03701       break;
03702     }
03703     break;
03704     
03705   case ISD::ROTL:
03706   case ISD::ROTR:
03707     Tmp1 = LegalizeOp(Node->getOperand(0));   // LHS
03708     Tmp2 = LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(1)));   // RHS
03709     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
03710     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
03711     default:
03712       assert(0 && "ROTL/ROTR legalize operation not supported");
03713       break;
03714     case TargetLowering::Legal:
03715       break;
03716     case TargetLowering::Custom:
03717       Tmp1 = TLI.LowerOperation(Result, DAG);
03718       if (Tmp1.getNode()) Result = Tmp1;
03719       break;
03720     case TargetLowering::Promote:
03721       assert(0 && "Do not know how to promote ROTL/ROTR");
03722       break;
03723     case TargetLowering::Expand:
03724       assert(0 && "Do not know how to expand ROTL/ROTR");
03725       break;
03726     }
03727     break;
03728     
03729   case ISD::BSWAP:
03730     Tmp1 = LegalizeOp(Node->getOperand(0));   // Op
03731     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
03732     case TargetLowering::Custom:
03733       assert(0 && "Cannot custom legalize this yet!");
03734     case TargetLowering::Legal:
03735       Result = DAG.UpdateNodeOperands(Result, Tmp1);
03736       break;
03737     case TargetLowering::Promote: {
03738       MVT OVT = Tmp1.getValueType();
03739       MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
03740       unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
03741 
03742       Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
03743       Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
03744       Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
03745                            DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
03746       break;
03747     }
03748     case TargetLowering::Expand:
03749       Result = ExpandBSWAP(Tmp1, dl);
03750       break;
03751     }
03752     break;
03753     
03754   case ISD::CTPOP:
03755   case ISD::CTTZ:
03756   case ISD::CTLZ:
03757     Tmp1 = LegalizeOp(Node->getOperand(0));   // Op
03758     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
03759     case TargetLowering::Custom:
03760     case TargetLowering::Legal:
03761       Result = DAG.UpdateNodeOperands(Result, Tmp1);
03762       if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
03763           TargetLowering::Custom) {
03764         Tmp1 = TLI.LowerOperation(Result, DAG);
03765         if (Tmp1.getNode()) {
03766           Result = Tmp1;
03767         }
03768       }
03769       break;
03770     case TargetLowering::Promote: {
03771       MVT OVT = Tmp1.getValueType();
03772       MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
03773 
03774       // Zero extend the argument.
03775       Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
03776       // Perform the larger operation, then subtract if needed.
03777       Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
03778       switch (Node->getOpcode()) {
03779       case ISD::CTPOP:
03780         Result = Tmp1;
03781         break;
03782       case ISD::CTTZ:
03783         //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
03784         Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
03785                             Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
03786                             ISD::SETEQ);
03787         Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
03788                              DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
03789         break;
03790       case ISD::CTLZ:
03791         // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
03792         Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
03793                              DAG.getConstant(NVT.getSizeInBits() -
03794                                              OVT.getSizeInBits(), NVT));
03795         break;
03796       }
03797       break;
03798     }
03799     case TargetLowering::Expand:
03800       Result = ExpandBitCount(Node->getOpcode(), Tmp1, dl);
03801       break;
03802     }
03803     break;
03804 
03805     // Unary operators
03806   case ISD::FABS:
03807   case ISD::FNEG:
03808   case ISD::FSQRT:
03809   case ISD::FSIN:
03810   case ISD::FCOS:
03811   case ISD::FLOG:
03812   case ISD::FLOG2:
03813   case ISD::FLOG10:
03814   case ISD::FEXP:
03815   case ISD::FEXP2:
03816   case ISD::FTRUNC:
03817   case ISD::FFLOOR:
03818   case ISD::FCEIL:
03819   case ISD::FRINT:
03820   case ISD::FNEARBYINT:
03821     Tmp1 = LegalizeOp(Node->getOperand(0));
03822     switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
03823     case TargetLowering::Promote:
03824     case TargetLowering::Custom:
03825      isCustom = true;
03826      // FALLTHROUGH
03827     case TargetLowering::Legal:
03828       Result = DAG.UpdateNodeOperands(Result, Tmp1);
03829       if (isCustom) {
03830         Tmp1 = TLI.LowerOperation(Result, DAG);
03831         if (Tmp1.getNode()) Result = Tmp1;
03832       }
03833       break;
03834     case TargetLowering::Expand:
03835       switch (Node->getOpcode()) {
03836       default: assert(0 && "Unreachable!");
03837       case ISD::FNEG:
03838         // Expand Y = FNEG(X) ->  Y = SUB -0.0, X
03839         Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
03840         Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1);
03841         break;
03842       case ISD::FABS: {
03843         // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
03844         MVT VT = Node->getValueType(0);
03845         Tmp2 = DAG.getConstantFP(0.0, VT);
03846         Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
03847                             Tmp1, Tmp2, ISD::SETUGT);
03848         Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
03849         Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
03850         break;
03851       }
03852       case ISD::FSQRT:
03853       case ISD::FSIN:
03854       case ISD::FCOS: 
03855       case ISD::FLOG:
03856       case ISD::FLOG2:
03857       case ISD::FLOG10:
03858       case ISD::FEXP:
03859       case ISD::FEXP2:
03860       case ISD::FTRUNC:
03861       case ISD::FFLOOR:
03862       case ISD::FCEIL:
03863       case ISD::FRINT:
03864       case ISD::FNEARBYINT: {
03865         MVT VT = Node->getValueType(0);
03866 
03867         // Expand unsupported unary vector operators by unrolling them.
03868         if (VT.isVector()) {
03869           Result = LegalizeOp(UnrollVectorOp(Op));
03870           break;
03871         }
03872 
03873         RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
03874         switch(Node->getOpcode()) {
03875         case ISD::FSQRT:
03876           LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
03877                             RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
03878           break;
03879         case ISD::FSIN:
03880           LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
03881                             RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
03882           break;
03883         case ISD::FCOS:
03884           LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
03885                             RTLIB::COS_F80, RTLIB::COS_PPCF128);
03886           break;
03887         case ISD::FLOG:
03888           LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
03889                             RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
03890           break;
03891         case ISD::FLOG2:
03892           LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
03893                             RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
03894           break;
03895         case ISD::FLOG10:
03896           LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
03897                             RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
03898           break;
03899         case ISD::FEXP:
03900           LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
03901                             RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
03902           break;
03903         case ISD::FEXP2:
03904           LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
03905                             RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
03906           break;
03907         case ISD::FTRUNC:
03908           LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
03909                             RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
03910           break;
03911         case ISD::FFLOOR:
03912           LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
03913                             RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
03914           break;
03915         case ISD::FCEIL:
03916           LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
03917                             RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
03918           break;
03919         case ISD::FRINT:
03920           LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
03921                             RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
03922           break;
03923         case ISD::FNEARBYINT:
03924           LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
03925                             RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
03926           break;
03927       break;
03928         default: assert(0 && "Unreachable!");
03929         }
03930         SDValue Dummy;
03931         Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
03932         break;
03933       }
03934       }
03935       break;
03936     }
03937     break;
03938   case ISD::FPOWI: {
03939     MVT VT = Node->getValueType(0);
03940 
03941     // Expand unsupported unary vector operators by unrolling them.
03942     if (VT.isVector()) {
03943       Result = LegalizeOp(UnrollVectorOp(Op));
03944       break;
03945     }
03946 
03947     // We always lower FPOWI into a libcall.  No target support for it yet.
03948     RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
03949                                      RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
03950     SDValue Dummy;
03951     Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
03952     break;
03953   }
03954   case ISD::BIT_CONVERT:
03955     if (!isTypeLegal(Node->getOperand(0).getValueType())) {
03956       Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
03957                                 Node->getValueType(0), dl);
03958     } else if (Op.getOperand(0).getValueType().isVector()) {
03959       // The input has to be a vector type, we have to either scalarize it, pack
03960       // it, or convert it based on whether the input vector type is legal.
03961       SDNode *InVal = Node->getOperand(0).getNode();
03962       int InIx = Node->getOperand(0).getResNo();
03963       unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
03964       MVT EVT = InVal->getValueType(InIx).getVectorElementType();
03965     
03966       // Figure out if there is a simple type corresponding to this Vector
03967       // type.  If so, convert to the vector type.
03968       MVT TVT = MVT::getVectorVT(EVT, NumElems);
03969       if (TLI.isTypeLegal(TVT)) {
03970         // Turn this into a bit convert of the vector input.
03971         Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0), 
03972                              LegalizeOp(Node->getOperand(0)));
03973         break;
03974       } else if (NumElems == 1) {
03975         // Turn this into a bit convert of the scalar input.
03976         Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0), 
03977                              ScalarizeVectorOp(Node->getOperand(0)));
03978         break;
03979       } else {
03980         // FIXME: UNIMP!  Store then reload
03981         assert(0 && "Cast from unsupported vector type not implemented yet!");
03982       }
03983     } else {
03984       switch (TLI.getOperationAction(ISD::BIT_CONVERT,
03985                                      Node->getOperand(0).getValueType())) {
03986       default: assert(0 && "Unknown operation action!");
03987       case TargetLowering::Expand:
03988         Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
03989                                   Node->getValueType(0), dl);
03990         break;
03991       case TargetLowering::Legal:
03992         Tmp1 = LegalizeOp(Node->getOperand(0));
03993         Result = DAG.UpdateNodeOperands(Result, Tmp1);
03994         break;
03995       }
03996     }
03997     break;
03998   case ISD::CONVERT_RNDSAT: {
03999     ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
04000     switch (CvtCode) {
04001     default: assert(0 && "Unknown cvt code!");
04002     case ISD::CVT_SF:
04003     case ISD::CVT_UF:
04004     case ISD::CVT_FF:
04005       break;
04006     case ISD::CVT_FS:
04007     case ISD::CVT_FU:
04008     case ISD::CVT_SS:
04009     case ISD::CVT_SU:
04010     case ISD::CVT_US:
04011     case ISD::CVT_UU: {
04012       SDValue DTyOp = Node->getOperand(1);
04013       SDValue STyOp = Node->getOperand(2);
04014       SDValue RndOp = Node->getOperand(3);
04015       SDValue SatOp = Node->getOperand(4);
04016       switch (getTypeAction(Node->getOperand(0).getValueType())) {
04017       case Expand: assert(0 && "Shouldn't need to expand other operators here!");
04018       case Legal:
04019         Tmp1 = LegalizeOp(Node->getOperand(0));
04020         Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
04021                                         RndOp, SatOp);
04022         if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
04023             TargetLowering::Custom) {
04024           Tmp1 = TLI.LowerOperation(Result, DAG);
04025           if (Tmp1.getNode()) Result = Tmp1;
04026         }
04027         break;
04028       case Promote:
04029         Result = PromoteOp(Node->getOperand(0));
04030         // For FP, make Op1 a i32
04031         
04032         Result = DAG.getConvertRndSat(Op.getValueType(), Result,
04033                                       DTyOp, STyOp, RndOp, SatOp, CvtCode);
04034         break;
04035       }
04036       break;
04037     }
04038     } // end switch CvtCode
04039     break;
04040   }
04041     // Conversion operators.  The source and destination have different types.
04042   case ISD::SINT_TO_FP:
04043   case ISD::UINT_TO_FP: {
04044     bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
04045     Result = LegalizeINT_TO_FP(Result, isSigned,
04046                                Node->getValueType(0), Node->getOperand(0), dl);
04047     break;
04048   }
04049   case ISD::TRUNCATE:
04050     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04051     case Legal:
04052       Tmp1 = LegalizeOp(Node->getOperand(0));
04053       switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
04054       default: assert(0 && "Unknown TRUNCATE legalization operation action!");
04055       case TargetLowering::Custom:
04056         isCustom = true;
04057         // FALLTHROUGH
04058       case TargetLowering::Legal:
04059         Result = DAG.UpdateNodeOperands(Result, Tmp1);
04060         if (isCustom) {
04061           Tmp1 = TLI.LowerOperation(Result, DAG);
04062           if (Tmp1.getNode()) Result = Tmp1;
04063         }
04064         break;
04065       case TargetLowering::Expand:
04066         assert(Result.getValueType().isVector() && "must be vector type");
04067         // Unroll the truncate.  We should do better.
04068         Result = LegalizeOp(UnrollVectorOp(Result));
04069       }
04070       break;
04071     case Expand:
04072       ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
04073 
04074       // Since the result is legal, we should just be able to truncate the low
04075       // part of the source.
04076       Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
04077       break;
04078     case Promote:
04079       Result = PromoteOp(Node->getOperand(0));
04080       Result = DAG.getNode(ISD::TRUNCATE, dl, Op.getValueType(), Result);
04081       break;
04082     }
04083     break;
04084 
04085   case ISD::FP_TO_SINT:
04086   case ISD::FP_TO_UINT:
04087     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04088     case Legal:
04089       Tmp1 = LegalizeOp(Node->getOperand(0));
04090 
04091       switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
04092       default: assert(0 && "Unknown operation action!");
04093       case TargetLowering::Custom:
04094         isCustom = true;
04095         // FALLTHROUGH
04096       case TargetLowering::Legal:
04097         Result = DAG.UpdateNodeOperands(Result, Tmp1);
04098         if (isCustom) {
04099           Tmp1 = TLI.LowerOperation(Result, DAG);
04100           if (Tmp1.getNode()) Result = Tmp1;
04101         }
04102         break;
04103       case TargetLowering::Promote:
04104         Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
04105                                        Node->getOpcode() == ISD::FP_TO_SINT, 
04106                                        dl);
04107         break;
04108       case TargetLowering::Expand:
04109         if (Node->getOpcode() == ISD::FP_TO_UINT) {
04110           SDValue True, False;
04111           MVT VT =  Node->getOperand(0).getValueType();
04112           MVT NVT = Node->getValueType(0);
04113           const uint64_t zero[] = {0, 0};
04114           APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
04115           APInt x = APInt::getSignBit(NVT.getSizeInBits());
04116           (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
04117           Tmp2 = DAG.getConstantFP(apf, VT);
04118           Tmp3 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), 
04119                               Node->getOperand(0),
04120                               Tmp2, ISD::SETLT);
04121           True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
04122           False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
04123                               DAG.getNode(ISD::FSUB, dl, VT, 
04124                                           Node->getOperand(0), Tmp2));
04125           False = DAG.getNode(ISD::XOR, dl, NVT, False,
04126                               DAG.getConstant(x, NVT));
04127           Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp3, True, False);
04128           break;
04129         } else {
04130           assert(0 && "Do not know how to expand FP_TO_SINT yet!");
04131         }
04132         break;
04133       }
04134       break;
04135     case Expand: {
04136       MVT VT = Op.getValueType();
04137       MVT OVT = Node->getOperand(0).getValueType();
04138       // Convert ppcf128 to i32
04139       if (OVT == MVT::ppcf128 && VT == MVT::i32) {
04140         if (Node->getOpcode() == ISD::FP_TO_SINT) {
04141           Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128, 
04142                                Node->getOperand(0), DAG.getValueType(MVT::f64));
04143           Result = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Result, 
04144                                DAG.getIntPtrConstant(1));
04145           Result = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Result);
04146         } else {
04147           const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
04148           APFloat apf = APFloat(APInt(128, 2, TwoE31));
04149           Tmp2 = DAG.getConstantFP(apf, OVT);
04150           //  X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
04151           // FIXME: generated code sucks.
04152           Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Node->getOperand(0), 
04153                                Tmp2,
04154                                DAG.getNode(ISD::ADD, dl, MVT::i32,
04155                                  DAG.getNode(ISD::FP_TO_SINT, dl, VT,
04156                                    DAG.getNode(ISD::FSUB, dl, OVT,
04157                                                  Node->getOperand(0), Tmp2)),
04158                                  DAG.getConstant(0x80000000, MVT::i32)),
04159                                DAG.getNode(ISD::FP_TO_SINT, dl, VT, 
04160                                            Node->getOperand(0)),
04161                                DAG.getCondCode(ISD::SETGE));
04162         }
04163         break;
04164       }
04165       // Convert f32 / f64 to i32 / i64 / i128.
04166       RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
04167         RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
04168       assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
04169       SDValue Dummy;
04170       Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
04171       break;
04172     }
04173     case Promote:
04174       Tmp1 = PromoteOp(Node->getOperand(0));
04175       Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
04176       Result = LegalizeOp(Result);
04177       break;
04178     }
04179     break;
04180 
04181   case ISD::FP_EXTEND: {
04182     MVT DstVT = Op.getValueType();
04183     MVT SrcVT = Op.getOperand(0).getValueType();
04184     if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
04185       // The only other way we can lower this is to turn it into a STORE,
04186       // LOAD pair, targetting a temporary location (a stack slot).
04187       Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT, dl);
04188       break;
04189     }
04190     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04191     case Expand: assert(0 && "Shouldn't need to expand other operators here!");
04192     case Legal:
04193       Tmp1 = LegalizeOp(Node->getOperand(0));
04194       Result = DAG.UpdateNodeOperands(Result, Tmp1);
04195       break;
04196     case Promote:
04197       Tmp1 = PromoteOp(Node->getOperand(0));
04198       Result = DAG.getNode(ISD::FP_EXTEND, dl, Op.getValueType(), Tmp1);
04199       break;
04200     }
04201     break;
04202   }
04203   case ISD::FP_ROUND: {
04204     MVT DstVT = Op.getValueType();
04205     MVT SrcVT = Op.getOperand(0).getValueType();
04206     if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
04207       if (SrcVT == MVT::ppcf128) {
04208         SDValue Lo;
04209         ExpandOp(Node->getOperand(0), Lo, Result);
04210         // Round it the rest of the way (e.g. to f32) if needed.
04211         if (DstVT!=MVT::f64)
04212           Result = DAG.getNode(ISD::FP_ROUND, dl,
04213                                DstVT, Result, Op.getOperand(1));
04214         break;
04215       }
04216       // The only other way we can lower this is to turn it into a STORE,
04217       // LOAD pair, targetting a temporary location (a stack slot).
04218       Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT, dl);
04219       break;
04220     }
04221     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04222     case Expand: assert(0 && "Shouldn't need to expand other operators here!");
04223     case Legal:
04224       Tmp1 = LegalizeOp(Node->getOperand(0));
04225       Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
04226       break;
04227     case Promote:
04228       Tmp1 = PromoteOp(Node->getOperand(0));
04229       Result = DAG.getNode(ISD::FP_ROUND, dl, Op.getValueType(), Tmp1,
04230                            Node->getOperand(1));
04231       break;
04232     }
04233     break;
04234   }
04235   case ISD::ANY_EXTEND:
04236   case ISD::ZERO_EXTEND:
04237   case ISD::SIGN_EXTEND:
04238     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04239     case Expand: assert(0 && "Shouldn't need to expand other operators here!");
04240     case Legal:
04241       Tmp1 = LegalizeOp(Node->getOperand(0));
04242       Result = DAG.UpdateNodeOperands(Result, Tmp1);
04243       if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
04244           TargetLowering::Custom) {
04245         Tmp1 = TLI.LowerOperation(Result, DAG);
04246         if (Tmp1.getNode()) Result = Tmp1;
04247       }
04248       break;
04249     case Promote:
04250       switch (Node->getOpcode()) {
04251       case ISD::ANY_EXTEND:
04252         Tmp1 = PromoteOp(Node->getOperand(0));
04253         Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Tmp1);
04254         break;
04255       case ISD::ZERO_EXTEND:
04256         Result = PromoteOp(Node->getOperand(0));
04257         Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Result);
04258         Result = DAG.getZeroExtendInReg(Result, dl,
04259                                         Node->getOperand(0).getValueType());
04260         break;
04261       case ISD::SIGN_EXTEND:
04262         Result = PromoteOp(Node->getOperand(0));
04263         Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Result);
04264         Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Result.getValueType(),
04265                              Result,
04266                           DAG.getValueType(Node->getOperand(0).getValueType()));
04267         break;
04268       }
04269     }
04270     break;
04271   case ISD::FP_ROUND_INREG:
04272   case ISD::SIGN_EXTEND_INREG: {
04273     Tmp1 = LegalizeOp(Node->getOperand(0));
04274     MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
04275 
04276     // If this operation is not supported, convert it to a shl/shr or load/store
04277     // pair.
04278     switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
04279     default: assert(0 && "This action not supported for this op yet!");
04280     case TargetLowering::Legal:
04281       Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
04282       break;
04283     case TargetLowering::Expand:
04284       // If this is an integer extend and shifts are supported, do that.
04285       if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
04286         // NOTE: we could fall back on load/store here too for targets without
04287         // SAR.  However, it is doubtful that any exist.
04288         unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
04289                             ExtraVT.getSizeInBits();
04290         SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
04291         Result = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
04292                              Node->getOperand(0), ShiftCst);
04293         Result = DAG.getNode(ISD::SRA, dl, Node->getValueType(0),
04294                              Result, ShiftCst);
04295       } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
04296         // The only way we can lower this is to turn it into a TRUNCSTORE,
04297         // EXTLOAD pair, targetting a temporary location (a stack slot).
04298 
04299         // NOTE: there is a choice here between constantly creating new stack
04300         // slots and always reusing the same one.  We currently always create
04301         // new ones, as reuse may inhibit scheduling.
04302         Result = EmitStackConvert(Node->getOperand(0), ExtraVT, 
04303                                   Node->getValueType(0), dl);
04304       } else {
04305         assert(0 && "Unknown op");
04306       }
04307       break;
04308     }
04309     break;
04310   }
04311   case ISD::TRAMPOLINE: {
04312     SDValue Ops[6];
04313     for (unsigned i = 0; i != 6; ++i)
04314       Ops[i] = LegalizeOp(Node->getOperand(i));
04315     Result = DAG.UpdateNodeOperands(Result, Ops, 6);
04316     // The only option for this node is to custom lower it.
04317     Result = TLI.LowerOperation(Result, DAG);
04318     assert(Result.getNode() && "Should always custom lower!");
04319 
04320     // Since trampoline produces two values, make sure to remember that we
04321     // legalized both of them.
04322     Tmp1 = LegalizeOp(Result.getValue(1));
04323     Result = LegalizeOp(Result);
04324     AddLegalizedOperand(SDValue(Node, 0), Result);
04325     AddLegalizedOperand(SDValue(Node, 1), Tmp1);
04326     return Op.getResNo() ? Tmp1 : Result;
04327   }
04328   case ISD::FLT_ROUNDS_: {
04329     MVT VT = Node->getValueType(0);
04330     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
04331     default: assert(0 && "This action not supported for this op yet!");
04332     case TargetLowering::Custom:
04333       Result = TLI.LowerOperation(Op, DAG);
04334       if (Result.getNode()) break;
04335       // Fall Thru
04336     case TargetLowering::Legal:
04337       // If this operation is not supported, lower it to constant 1
04338       Result = DAG.getConstant(1, VT);
04339       break;
04340     }
04341     break;
04342   }
04343   case ISD::TRAP: {
04344     MVT VT = Node->getValueType(0);
04345     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
04346     default: assert(0 && "This action not supported for this op yet!");
04347     case TargetLowering::Legal:
04348       Tmp1 = LegalizeOp(Node->getOperand(0));
04349       Result = DAG.UpdateNodeOperands(Result, Tmp1);
04350       break;
04351     case TargetLowering::Custom:
04352       Result = TLI.LowerOperation(Op, DAG);
04353       if (Result.getNode()) break;
04354       // Fall Thru
04355     case TargetLowering::Expand:
04356       // If this operation is not supported, lower it to 'abort()' call
04357       Tmp1 = LegalizeOp(Node->getOperand(0));
04358       TargetLowering::ArgListTy Args;
04359       std::pair<SDValue,SDValue> CallResult =
04360         TLI.LowerCallTo(Tmp1, Type::VoidTy,
04361                         false, false, false, false, CallingConv::C, false,
04362                         DAG.getExternalSymbol("abort", TLI.getPointerTy()),
04363                         Args, DAG, dl);
04364       Result = CallResult.second;
04365       break;
04366     }
04367     break;
04368   }
04369 
04370   case ISD::SADDO:
04371   case ISD::SSUBO: {
04372     MVT VT = Node->getValueType(0);
04373     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
04374     default: assert(0 && "This action not supported for this op yet!");
04375     case TargetLowering::Custom:
04376       Result = TLI.LowerOperation(Op, DAG);
04377       if (Result.getNode()) break;
04378       // FALLTHROUGH
04379     case TargetLowering::Legal: {
04380       SDValue LHS = LegalizeOp(Node->getOperand(0));
04381       SDValue RHS = LegalizeOp(Node->getOperand(1));
04382 
04383       SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ? 
04384                                 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
04385                                 LHS, RHS);
04386       MVT OType = Node->getValueType(1);
04387 
04388       SDValue Zero = DAG.getConstant(0, LHS.getValueType());
04389 
04390       //   LHSSign -> LHS >= 0
04391       //   RHSSign -> RHS >= 0
04392       //   SumSign -> Sum >= 0
04393       //
04394       //   Add:
04395       //   Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
04396       //   Sub:
04397       //   Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
04398       //
04399       SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
04400       SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
04401       SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign, 
04402                                         Node->getOpcode() == ISD::SADDO ? 
04403                                         ISD::SETEQ : ISD::SETNE);
04404 
04405       SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
04406       SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
04407 
04408       SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
04409 
04410       MVT ValueVTs[] = { LHS.getValueType(), OType };
04411       SDValue Ops[] = { Sum, Cmp };
04412 
04413       Result = DAG.getNode(ISD::MERGE_VALUES, dl, 
04414                            DAG.getVTList(&ValueVTs[0], 2),
04415                            &Ops[0], 2);
04416       SDNode *RNode = Result.getNode();
04417       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
04418       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
04419       break;
04420     }
04421     }
04422 
04423     break;
04424   }
04425   case ISD::UADDO:
04426   case ISD::USUBO: {
04427     MVT VT = Node->getValueType(0);
04428     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
04429     default: assert(0 && "This action not supported for this op yet!");
04430     case TargetLowering::Custom:
04431       Result = TLI.LowerOperation(Op, DAG);
04432       if (Result.getNode()) break;
04433       // FALLTHROUGH
04434     case TargetLowering::Legal: {
04435       SDValue LHS = LegalizeOp(Node->getOperand(0));
04436       SDValue RHS = LegalizeOp(Node->getOperand(1));
04437 
04438       SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
04439                                 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
04440                                 LHS, RHS);
04441       MVT OType = Node->getValueType(1);
04442       SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
04443                                  Node->getOpcode () == ISD::UADDO ? 
04444                                  ISD::SETULT : ISD::SETUGT);
04445 
04446       MVT ValueVTs[] = { LHS.getValueType(), OType };
04447       SDValue Ops[] = { Sum, Cmp };
04448 
04449       Result = DAG.getNode(ISD::MERGE_VALUES, dl, 
04450                            DAG.getVTList(&ValueVTs[0], 2),
04451                            &Ops[0], 2);
04452       SDNode *RNode = Result.getNode();
04453       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
04454       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
04455       break;
04456     }
04457     }
04458 
04459     break;
04460   }
04461   case ISD::SMULO:
04462   case ISD::UMULO: {
04463     MVT VT = Node->getValueType(0);
04464     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
04465     default: assert(0 && "This action is not supported at all!");
04466     case TargetLowering::Custom:
04467       Result = TLI.LowerOperation(Op, DAG);
04468       if (Result.getNode()) break;
04469       // Fall Thru
04470     case TargetLowering::Legal:
04471       // FIXME: According to Hacker's Delight, this can be implemented in
04472       // target independent lowering, but it would be inefficient, since it
04473       // requires a division + a branch.
04474       assert(0 && "Target independent lowering is not supported for SMULO/UMULO!"); 
04475     break;
04476     }
04477     break;
04478   }
04479 
04480   }
04481   
04482   assert(Result.getValueType() == Op.getValueType() &&
04483          "Bad legalization!");
04484   
04485   // Make sure that the generated code is itself legal.
04486   if (Result != Op)
04487     Result = LegalizeOp(Result);
04488 
04489   // Note that LegalizeOp may be reentered even from single-use nodes, which
04490   // means that we always must cache transformed nodes.
04491   AddLegalizedOperand(Op, Result);
04492   return Result;
04493 }
04494 
04495 /// PromoteOp - Given an operation that produces a value in an invalid type,
04496 /// promote it to compute the value into a larger type.  The produced value will
04497 /// have the correct bits for the low portion of the register, but no guarantee
04498 /// is made about the top bits: it may be zero, sign-extended, or garbage.
04499 SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
04500   MVT VT = Op.getValueType();
04501   MVT NVT = TLI.getTypeToTransformTo(VT);
04502   assert(getTypeAction(VT) == Promote &&
04503          "Caller should expand or legalize operands that are not promotable!");
04504   assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
04505          "Cannot promote to smaller type!");
04506 
04507   SDValue Tmp1, Tmp2, Tmp3;
04508   SDValue Result;
04509   SDNode *Node = Op.getNode();
04510   DebugLoc dl = Node->getDebugLoc();
04511 
04512   DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
04513   if (I != PromotedNodes.end()) return I->second;
04514 
04515   switch (Node->getOpcode()) {
04516   case ISD::CopyFromReg:
04517     assert(0 && "CopyFromReg must be legal!");
04518   default:
04519 #ifndef NDEBUG
04520     cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
04521 #endif
04522     assert(0 && "Do not know how to promote this operator!");
04523     abort();
04524   case ISD::UNDEF:
04525     Result = DAG.getNode(ISD::UNDEF, dl, NVT);
04526     break;
04527   case ISD::Constant:
04528     if (VT != MVT::i1)
04529       Result = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Op);
04530     else
04531       Result = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Op);
04532     assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
04533     break;
04534   case ISD::ConstantFP:
04535     Result = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Op);
04536     assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
04537     break;
04538 
04539   case ISD::SETCC: {
04540     MVT VT0 = Node->getOperand(0).getValueType();
04541     assert(isTypeLegal(TLI.getSetCCResultType(VT0))
04542            && "SetCC type is not legal??");
04543     Result = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(VT0),
04544                          Node->getOperand(0), Node->getOperand(1),
04545                          Node->getOperand(2));
04546     break;
04547   }
04548   case ISD::TRUNCATE:
04549     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04550     case Legal:
04551       Result = LegalizeOp(Node->getOperand(0));
04552       assert(Result.getValueType().bitsGE(NVT) &&
04553              "This truncation doesn't make sense!");
04554       if (Result.getValueType().bitsGT(NVT))    // Truncate to NVT instead of VT
04555         Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Result);
04556       break;
04557     case Promote:
04558       // The truncation is not required, because we don't guarantee anything
04559       // about high bits anyway.
04560       Result = PromoteOp(Node->getOperand(0));
04561       break;
04562     case Expand:
04563       ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
04564       // Truncate the low part of the expanded value to the result type
04565       Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Tmp1);
04566     }
04567     break;
04568   case ISD::SIGN_EXTEND:
04569   case ISD::ZERO_EXTEND:
04570   case ISD::ANY_EXTEND:
04571     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04572     case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
04573     case Legal:
04574       // Input is legal?  Just do extend all the way to the larger type.
04575       Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
04576       break;
04577     case Promote:
04578       // Promote the reg if it's smaller.
04579       Result = PromoteOp(Node->getOperand(0));
04580       // The high bits are not guaranteed to be anything.  Insert an extend.
04581       if (Node->getOpcode() == ISD::SIGN_EXTEND)
04582         Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
04583                          DAG.getValueType(Node->getOperand(0).getValueType()));
04584       else if (Node->getOpcode() == ISD::ZERO_EXTEND)
04585         Result = DAG.getZeroExtendInReg(Result, dl,
04586                                         Node->getOperand(0).getValueType());
04587       break;
04588     }
04589     break;
04590   case ISD::CONVERT_RNDSAT: {
04591     ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
04592     assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
04593              CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
04594              CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
04595             "can only promote integers");
04596     Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
04597                                   Node->getOperand(1), Node->getOperand(2),
04598                                   Node->getOperand(3), Node->getOperand(4),
04599                                   CvtCode);
04600     break;
04601 
04602   }
04603   case ISD::BIT_CONVERT:
04604     Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
04605                               Node->getValueType(0), dl);
04606     Result = PromoteOp(Result);
04607     break;
04608     
04609   case ISD::FP_EXTEND:
04610     assert(0 && "Case not implemented.  Dynamically dead with 2 FP types!");
04611   case ISD::FP_ROUND:
04612     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04613     case Expand: assert(0 && "BUG: Cannot expand FP regs!");
04614     case Promote:  assert(0 && "Unreachable with 2 FP types!");
04615     case Legal:
04616       if (Node->getConstantOperandVal(1) == 0) {
04617         // Input is legal?  Do an FP_ROUND_INREG.
04618         Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Node->getOperand(0),
04619                              DAG.getValueType(VT));
04620       } else {
04621         // Just remove the truncate, it isn't affecting the value.
04622         Result = DAG.getNode(ISD::FP_ROUND, dl, NVT, Node->getOperand(0), 
04623                              Node->getOperand(1));
04624       }
04625       break;
04626     }
04627     break;
04628   case ISD::SINT_TO_FP:
04629   case ISD::UINT_TO_FP:
04630     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04631     case Legal:
04632       // No extra round required here.
04633       Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
04634       break;
04635 
04636     case Promote:
04637       Result = PromoteOp(Node->getOperand(0));
04638       if (Node->getOpcode() == ISD::SINT_TO_FP)
04639         Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Result.getValueType(),
04640                              Result,
04641                          DAG.getValueType(Node->getOperand(0).getValueType()));
04642       else
04643         Result = DAG.getZeroExtendInReg(Result, dl,
04644                                         Node->getOperand(0).getValueType());
04645       // No extra round required here.
04646       Result = DAG.getNode(Node->getOpcode(), dl, NVT, Result);
04647       break;
04648     case Expand:
04649       Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
04650                              Node->getOperand(0), dl);
04651       // Round if we cannot tolerate excess precision.
04652       if (NoExcessFPPrecision)
04653         Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
04654                              DAG.getValueType(VT));
04655       break;
04656     }
04657     break;
04658 
04659   case ISD::SIGN_EXTEND_INREG:
04660     Result = PromoteOp(Node->getOperand(0));
04661     Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result, 
04662                          Node->getOperand(1));
04663     break;
04664   case ISD::FP_TO_SINT:
04665   case ISD::FP_TO_UINT:
04666     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04667     case Legal:
04668     case Expand:
04669       Tmp1 = Node->getOperand(0);
04670       break;
04671     case Promote:
04672       // The input result is prerounded, so we don't have to do anything
04673       // special.
04674       Tmp1 = PromoteOp(Node->getOperand(0));
04675       break;
04676     }
04677     // If we're promoting a UINT to a larger size, check to see if the new node
04678     // will be legal.  If it isn't, check to see if FP_TO_SINT is legal, since
04679     // we can use that instead.  This allows us to generate better code for
04680     // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
04681     // legal, such as PowerPC.
04682     if (Node->getOpcode() == ISD::FP_TO_UINT && 
04683         !TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NVT) &&
04684         (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT) ||
04685          TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
04686       Result = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Tmp1);
04687     } else {
04688       Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
04689     }
04690     break;
04691 
04692   case ISD::FABS:
04693   case ISD::FNEG:
04694     Tmp1 = PromoteOp(Node->getOperand(0));
04695     assert(Tmp1.getValueType() == NVT);
04696     Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
04697     // NOTE: we do not have to do any extra rounding here for
04698     // NoExcessFPPrecision, because we know the input will have the appropriate
04699     // precision, and these operations don't modify precision at all.
04700     break;
04701 
04702   case ISD::FLOG:
04703   case ISD::FLOG2:
04704   case ISD::FLOG10:
04705   case ISD::FEXP:
04706   case ISD::FEXP2:
04707   case ISD::FSQRT:
04708   case ISD::FSIN:
04709   case ISD::FCOS:
04710   case ISD::FTRUNC:
04711   case ISD::FFLOOR:
04712   case ISD::FCEIL:
04713   case ISD::FRINT:
04714   case ISD::FNEARBYINT:
04715     Tmp1 = PromoteOp(Node->getOperand(0));
04716     assert(Tmp1.getValueType() == NVT);
04717     Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
04718     if (NoExcessFPPrecision)
04719       Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
04720                            DAG.getValueType(VT));
04721     break;
04722 
04723   case ISD::FPOW:
04724   case ISD::FPOWI: {
04725     // Promote f32 pow(i) to f64 pow(i).  Note that this could insert a libcall
04726     // directly as well, which may be better.
04727     Tmp1 = PromoteOp(Node->getOperand(0));
04728     Tmp2 = Node->getOperand(1);
04729     if (Node->getOpcode() == ISD::FPOW)
04730       Tmp2 = PromoteOp(Tmp2);
04731     assert(Tmp1.getValueType() == NVT);
04732     Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
04733     if (NoExcessFPPrecision)
04734       Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
04735                            DAG.getValueType(VT));
04736     break;
04737   }
04738     
04739   case ISD::ATOMIC_CMP_SWAP: {
04740     AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
04741     Tmp2 = PromoteOp(Node->getOperand(2));
04742     Tmp3 = PromoteOp(Node->getOperand(3));
04743     Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(), 
04744                            AtomNode->getChain(), 
04745                            AtomNode->getBasePtr(), Tmp2, Tmp3,
04746                            AtomNode->getSrcValue(),
04747                            AtomNode->getAlignment());
04748     // Remember that we legalized the chain.
04749     AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
04750     break;
04751   }
04752   case ISD::ATOMIC_LOAD_ADD:
04753   case ISD::ATOMIC_LOAD_SUB:
04754   case ISD::ATOMIC_LOAD_AND:
04755   case ISD::ATOMIC_LOAD_OR:
04756   case ISD::ATOMIC_LOAD_XOR:
04757   case ISD::ATOMIC_LOAD_NAND:
04758   case ISD::ATOMIC_LOAD_MIN:
04759   case ISD::ATOMIC_LOAD_MAX:
04760   case ISD::ATOMIC_LOAD_UMIN:
04761   case ISD::ATOMIC_LOAD_UMAX:
04762   case ISD::ATOMIC_SWAP: {
04763     AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
04764     Tmp2 = PromoteOp(Node->getOperand(2));
04765     Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
04766                            AtomNode->getChain(), 
04767                            AtomNode->getBasePtr(), Tmp2,
04768                            AtomNode->getSrcValue(),
04769                            AtomNode->getAlignment());
04770     // Remember that we legalized the chain.
04771     AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
04772     break;
04773   }
04774 
04775   case ISD::AND:
04776   case ISD::OR:
04777   case ISD::XOR:
04778   case ISD::ADD:
04779   case ISD::SUB:
04780   case ISD::MUL:
04781     // The input may have strange things in the top bits of the registers, but
04782     // these operations don't care.  They may have weird bits going out, but
04783     // that too is okay if they are integer operations.
04784     Tmp1 = PromoteOp(Node->getOperand(0));
04785     Tmp2 = PromoteOp(Node->getOperand(1));
04786     assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
04787     Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
04788     break;
04789   case ISD::FADD:
04790   case ISD::FSUB:
04791   case ISD::FMUL:
04792     Tmp1 = PromoteOp(Node->getOperand(0));
04793     Tmp2 = PromoteOp(Node->getOperand(1));
04794     assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
04795     Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
04796     
04797     // Floating point operations will give excess precision that we may not be
04798     // able to tolerate.  If we DO allow excess precision, just leave it,
04799     // otherwise excise it.
04800     // FIXME: Why would we need to round FP ops more than integer ones?
04801     //     Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
04802     if (NoExcessFPPrecision)
04803       Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
04804                            DAG.getValueType(VT));
04805     break;
04806 
04807   case ISD::SDIV:
04808   case ISD::SREM:
04809     // These operators require that their input be sign extended.
04810     Tmp1 = PromoteOp(Node->getOperand(0));
04811     Tmp2 = PromoteOp(Node->getOperand(1));
04812     if (NVT.isInteger()) {
04813       Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
04814                          DAG.getValueType(VT));
04815       Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
04816                          DAG.getValueType(VT));
04817     }
04818     Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
04819 
04820     // Perform FP_ROUND: this is probably overly pessimistic.
04821     if (NVT.isFloatingPoint() && NoExcessFPPrecision)
04822       Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
04823                            DAG.getValueType(VT));
04824     break;
04825   case ISD::FDIV:
04826   case ISD::FREM:
04827   case ISD::FCOPYSIGN:
04828     // These operators require that their input be fp extended.
04829     switch (getTypeAction(Node->getOperand(0).getValueType())) {
04830     case Expand: assert(0 && "not implemented");
04831     case Legal:   Tmp1 = LegalizeOp(Node->getOperand(0)); break;
04832     case Promote: Tmp1 = PromoteOp(Node->getOperand(0));  break;
04833     }
04834     switch (getTypeAction(Node->getOperand(1).getValueType())) {
04835     case Expand: assert(0 && "not implemented");
04836     case Legal:   Tmp2 = LegalizeOp(Node->getOperand(1)); break;
04837     case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
04838     }
04839     Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
04840     
04841     // Perform FP_ROUND: this is probably overly pessimistic.
04842     if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
04843       Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
04844                            DAG.getValueType(VT));
04845     break;
04846 
04847   case ISD::UDIV:
04848   case ISD::UREM:
04849     // These operators require that their input be zero extended.
04850     Tmp1 = PromoteOp(Node->getOperand(0));
04851     Tmp2 = PromoteOp(Node->getOperand(1));
04852     assert(NVT.isInteger() && "Operators don't apply to FP!");
04853     Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
04854     Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
04855     Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
04856     break;
04857 
04858   case ISD::SHL:
04859     Tmp1 = PromoteOp(Node->getOperand(0));
04860     Result = DAG.getNode(ISD::SHL, dl, NVT, Tmp1, Node->getOperand(1));
04861     break;
04862   case ISD::SRA:
04863     // The input value must be properly sign extended.
04864     Tmp1 = PromoteOp(Node->getOperand(0));
04865     Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
04866                        DAG.getValueType(VT));
04867     Result = DAG.getNode(ISD::SRA, dl, NVT, Tmp1, Node->getOperand(1));
04868     break;
04869   case ISD::SRL:
04870     // The input value must be properly zero extended.
04871     Tmp1 = PromoteOp(Node->getOperand(0));
04872     Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
04873     Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1, Node->getOperand(1));
04874     break;
04875 
04876   case ISD::VAARG:
04877     Tmp1 = Node->getOperand(0);   // Get the chain.
04878     Tmp2 = Node->getOperand(1);   // Get the pointer.
04879     if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
04880       Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
04881       Result = TLI.LowerOperation(Tmp3, DAG);
04882     } else {
04883       const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
04884       SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
04885       // Increment the pointer, VAList, to the next vaarg
04886       Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList, 
04887                          DAG.getConstant(VT.getSizeInBits()/8,
04888                                          TLI.getPointerTy()));
04889       // Store the incremented VAList to the legalized pointer
04890       Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
04891       // Load the actual argument out of the pointer VAList
04892       Result = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Tmp3, VAList, NULL, 0, VT);
04893     }
04894     // Remember that we legalized the chain.
04895     AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
04896     break;
04897 
04898   case ISD::LOAD: {
04899     LoadSDNode *LD = cast<LoadSDNode>(Node);
04900     ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
04901       ? ISD::EXTLOAD : LD->getExtensionType();
04902     Result = DAG.getExtLoad(ExtType, dl, NVT,
04903                             LD->getChain(), LD->getBasePtr(),
04904                             LD->getSrcValue(), LD->getSrcValueOffset(),
04905                             LD->getMemoryVT(),
04906                             LD->isVolatile(),
04907                             LD->getAlignment());
04908     // Remember that we legalized the chain.
04909     AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
04910     break;
04911   }
04912   case ISD::SELECT: {
04913     Tmp2 = PromoteOp(Node->getOperand(1));   // Legalize the op0
04914     Tmp3 = PromoteOp(Node->getOperand(2));   // Legalize the op1
04915 
04916     MVT VT2 = Tmp2.getValueType();
04917     assert(VT2 == Tmp3.getValueType()
04918            && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
04919     // Ensure that the resulting node is at least the same size as the operands'
04920     // value types, because we cannot assume that TLI.getSetCCValueType() is
04921     // constant.
04922     Result = DAG.getNode(ISD::SELECT, dl, VT2, Node->getOperand(0), Tmp2, Tmp3);
04923     break;
04924   }
04925   case ISD::SELECT_CC:
04926     Tmp2 = PromoteOp(Node->getOperand(2));   // True
04927     Tmp3 = PromoteOp(Node->getOperand(3));   // False
04928     Result = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
04929                          Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
04930     break;
04931   case ISD::BSWAP:
04932     Tmp1 = Node->getOperand(0);
04933     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
04934     Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
04935     Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
04936                          DAG.getConstant(NVT.getSizeInBits() -
04937                                          VT.getSizeInBits(),
04938                                          TLI.getShiftAmountTy()));
04939     break;
04940   case ISD::CTPOP:
04941   case ISD::CTTZ:
04942   case ISD::CTLZ:
04943     // Zero extend the argument
04944     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
04945     // Perform the larger operation, then subtract if needed.
04946     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
04947     switch(Node->getOpcode()) {
04948     case ISD::CTPOP:
04949       Result = Tmp1;
04950       break;
04951     case ISD::CTTZ:
04952       // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
04953       Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
04954                           DAG.getConstant(NVT.getSizeInBits(), NVT),
04955                           ISD::SETEQ);
04956       Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
04957                            DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
04958       break;
04959     case ISD::CTLZ:
04960       //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
04961       Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
04962                            DAG.getConstant(NVT.getSizeInBits() -
04963                                            VT.getSizeInBits(), NVT));
04964       break;
04965     }
04966     break;
04967   case ISD::EXTRACT_SUBVECTOR:
04968     Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
04969     break;
04970   case ISD::EXTRACT_VECTOR_ELT:
04971     Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
04972     break;
04973   }
04974 
04975   assert(Result.getNode() && "Didn't set a result!");
04976 
04977   // Make sure the result is itself legal.
04978   Result = LegalizeOp(Result);
04979   
04980   // Remember that we promoted this!
04981   AddPromotedOperand(Op, Result);
04982   return Result;
04983 }
04984 
04985 /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
04986 /// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
04987 /// based on the vector type. The return type of this matches the element type
04988 /// of the vector, which may not be legal for the target.
04989 SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
04990   // We know that operand #0 is the Vec vector.  If the index is a constant
04991   // or if the invec is a supported hardware type, we can use it.  Otherwise,
04992   // lower to a store then an indexed load.
04993   SDValue Vec = Op.getOperand(0);
04994   SDValue Idx = Op.getOperand(1);
04995   DebugLoc dl = Op.getNode()->getDebugLoc();
04996   
04997   MVT TVT = Vec.getValueType();
04998   unsigned NumElems = TVT.getVectorNumElements();
04999   
05000   switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
05001   default: assert(0 && "This action is not supported yet!");
05002   case TargetLowering::Custom: {
05003     Vec = LegalizeOp(Vec);
05004     Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
05005     SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
05006     if (Tmp3.getNode())
05007       return Tmp3;
05008     break;
05009   }
05010   case TargetLowering::Legal:
05011     if (isTypeLegal(TVT)) {
05012       Vec = LegalizeOp(Vec);
05013       Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
05014       return Op;
05015     }
05016     break;
05017   case TargetLowering::Promote:
05018     assert(TVT.isVector() && "not vector type");
05019     // fall thru to expand since vectors are by default are promote
05020   case TargetLowering::Expand:
05021     break;
05022   }
05023 
05024   if (NumElems == 1) {
05025     // This must be an access of the only element.  Return it.
05026     Op = ScalarizeVectorOp(Vec);
05027   } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
05028     unsigned NumLoElts =  1 << Log2_32(NumElems-1);
05029     ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
05030     SDValue Lo, Hi;
05031     SplitVectorOp(Vec, Lo, Hi);
05032     if (CIdx->getZExtValue() < NumLoElts) {
05033       Vec = Lo;
05034     } else {
05035       Vec = Hi;
05036       Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
05037                             Idx.getValueType());
05038     }
05039   
05040     // It's now an extract from the appropriate high or low part.  Recurse.
05041     Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
05042     Op = ExpandEXTRACT_VECTOR_ELT(Op);
05043   } else {
05044     // Store the value to a temporary stack slot, then LOAD the scalar
05045     // element back out.
05046     SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
05047     SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
05048 
05049     // Add the offset to the index.
05050     unsigned EltSize = Op.getValueType().getSizeInBits()/8;
05051     Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
05052                       DAG.getConstant(EltSize, Idx.getValueType()));
05053 
05054     if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
05055       Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
05056     else
05057       Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
05058 
05059     StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
05060 
05061     Op = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
05062   }
05063   return Op;
05064 }
05065 
05066 /// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation.  For now
05067 /// we assume the operation can be split if it is not already legal.
05068 SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
05069   // We know that operand #0 is the Vec vector.  For now we assume the index
05070   // is a constant and that the extracted result is a supported hardware type.
05071   SDValue Vec = Op.getOperand(0);
05072   SDValue Idx = LegalizeOp(Op.getOperand(1));
05073   
05074   unsigned NumElems = Vec.getValueType().getVectorNumElements();
05075   
05076   if (NumElems == Op.getValueType().getVectorNumElements()) {
05077     // This must be an access of the desired vector length.  Return it.
05078     return Vec;
05079   }
05080 
05081   ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
05082   SDValue Lo, Hi;
05083   SplitVectorOp(Vec, Lo, Hi);
05084   if (CIdx->getZExtValue() < NumElems/2) {
05085     Vec = Lo;
05086   } else {
05087     Vec = Hi;
05088     Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
05089                           Idx.getValueType());
05090   }
05091   
05092   // It's now an extract from the appropriate high or low part.  Recurse.
05093   Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
05094   return ExpandEXTRACT_SUBVECTOR(Op);
05095 }
05096 
05097 /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
05098 /// with condition CC on the current target.  This usually involves legalizing
05099 /// or promoting the arguments.  In the case where LHS and RHS must be expanded,
05100 /// there may be no choice but to create a new SetCC node to represent the
05101 /// legalized value of setcc lhs, rhs.  In this case, the value is returned in
05102 /// LHS, and the SDValue returned in RHS has a nil SDNode value.
05103 void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
05104                                                  SDValue &RHS,
05105                                                  SDValue &CC,
05106                                                  DebugLoc dl) {
05107   SDValue Tmp1, Tmp2, Tmp3, Result;    
05108   
05109   switch (getTypeAction(LHS.getValueType())) {
05110   case Legal:
05111     Tmp1 = LegalizeOp(LHS);   // LHS
05112     Tmp2 = LegalizeOp(RHS);   // RHS
05113     break;
05114   case Promote:
05115     Tmp1 = PromoteOp(LHS);   // LHS
05116     Tmp2 = PromoteOp(RHS);   // RHS
05117 
05118     // If this is an FP compare, the operands have already been extended.
05119     if (LHS.getValueType().isInteger()) {
05120       MVT VT = LHS.getValueType();
05121       MVT NVT = TLI.getTypeToTransformTo(VT);
05122 
05123       // Otherwise, we have to insert explicit sign or zero extends.  Note
05124       // that we could insert sign extends for ALL conditions, but zero extend
05125       // is cheaper on many machines (an AND instead of two shifts), so prefer
05126       // it.
05127       switch (cast<CondCodeSDNode>(CC)->get()) {
05128       default: assert(0 && "Unknown integer comparison!");
05129       case ISD::SETEQ:
05130       case ISD::SETNE:
05131       case ISD::SETUGE:
05132       case ISD::SETUGT:
05133       case ISD::SETULE:
05134       case ISD::SETULT:
05135         // ALL of these operations will work if we either sign or zero extend
05136         // the operands (including the unsigned comparisons!).  Zero extend is
05137         // usually a simpler/cheaper operation, so prefer it.
05138         Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
05139         Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
05140         break;
05141       case ISD::SETGE:
05142       case ISD::SETGT:
05143       case ISD::SETLT:
05144       case ISD::SETLE:
05145         Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
05146                            DAG.getValueType(VT));
05147         Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
05148                            DAG.getValueType(VT));
05149         Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
05150         Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
05151         break;
05152       }
05153     }
05154     break;
05155   case Expand: {
05156     MVT VT = LHS.getValueType();
05157     if (VT == MVT::f32 || VT == MVT::f64) {
05158       // Expand into one or more soft-fp libcall(s).
05159       RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
05160       switch (cast<CondCodeSDNode>(CC)->get()) {
05161       case ISD::SETEQ:
05162       case ISD::SETOEQ:
05163         LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
05164         break;
05165       case ISD::SETNE:
05166       case ISD::SETUNE:
05167         LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
05168         break;
05169       case ISD::SETGE:
05170       case ISD::SETOGE:
05171         LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
05172         break;
05173       case ISD::SETLT:
05174       case ISD::SETOLT:
05175         LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
05176         break;
05177       case ISD::SETLE:
05178       case ISD::SETOLE:
05179         LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
05180         break;
05181       case ISD::SETGT:
05182       case ISD::SETOGT:
05183         LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
05184         break;
05185       case ISD::SETUO:
05186         LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
05187         break;
05188       case ISD::SETO:
05189         LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
05190         break;
05191       default:
05192         LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
05193         switch (cast<CondCodeSDNode>(CC)->get()) {
05194         case ISD::SETONE:
05195           // SETONE = SETOLT | SETOGT
05196           LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
05197           // Fallthrough
05198         case ISD::SETUGT:
05199           LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
05200           break;
05201         case ISD::SETUGE:
05202           LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
05203           break;
05204         case ISD::SETULT:
05205           LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
05206           break;
05207         case ISD::SETULE:
05208           LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
05209           break;
05210         case ISD::SETUEQ:
05211           LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
05212           break;
05213         default: assert(0 && "Unsupported FP setcc!");
05214         }
05215       }
05216 
05217       SDValue Dummy;
05218       SDValue Ops[2] = { LHS, RHS };
05219       Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2, dl).getNode(),
05220                            false /*sign irrelevant*/, Dummy);
05221       Tmp2 = DAG.getConstant(0, MVT::i32);
05222       CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
05223       if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
05224         Tmp1 = DAG.getNode(ISD::SETCC, dl,
05225                            TLI.getSetCCResultType(Tmp1.getValueType()),
05226                            Tmp1, Tmp2, CC);
05227         LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2, dl).getNode(),
05228                             false /*sign irrelevant*/, Dummy);
05229         Tmp2 = DAG.getNode(ISD::SETCC, dl,
05230                            TLI.getSetCCResultType(LHS.getValueType()), LHS,
05231                            Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
05232         Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
05233         Tmp2 = SDValue();
05234       }
05235       LHS = LegalizeOp(Tmp1);
05236       RHS = Tmp2;
05237       return;
05238     }
05239 
05240     SDValue LHSLo, LHSHi, RHSLo, RHSHi;
05241     ExpandOp(LHS, LHSLo, LHSHi);
05242     ExpandOp(RHS, RHSLo, RHSHi);
05243     ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
05244 
05245     if (VT==MVT::ppcf128) {
05246       // FIXME:  This generated code sucks.  We want to generate
05247       //         FCMPU crN, hi1, hi2
05248       //         BNE crN, L:
05249       //         FCMPU crN, lo1, lo2
05250       // The following can be improved, but not that much.
05251       Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
05252                           LHSHi, RHSHi, ISD::SETOEQ);
05253       Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
05254                           LHSLo, RHSLo, CCCode);
05255       Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
05256       Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
05257                           LHSHi, RHSHi, ISD::SETUNE);
05258       Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
05259                           LHSHi, RHSHi, CCCode);
05260       Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
05261       Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
05262       Tmp2 = SDValue();
05263       break;
05264     }
05265 
05266     switch (CCCode) {
05267     case ISD::SETEQ:
05268     case ISD::SETNE:
05269       if (RHSLo == RHSHi)
05270         if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
05271           if (RHSCST->isAllOnesValue()) {
05272             // Comparison to -1.
05273             Tmp1 = DAG.getNode(ISD::AND, dl,LHSLo.getValueType(), LHSLo, LHSHi);
05274             Tmp2 = RHSLo;
05275             break;
05276           }
05277 
05278       Tmp1 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
05279       Tmp2 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
05280       Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
05281       Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
05282       break;
05283     default:
05284       // If this is a comparison of the sign bit, just look at the top part.
05285       // X > -1,  x < 0
05286       if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
05287         if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && 
05288              CST->isNullValue()) ||               // X < 0
05289             (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
05290              CST->isAllOnesValue())) {            // X > -1
05291           Tmp1 = LHSHi;
05292           Tmp2 = RHSHi;
05293           break;
05294         }
05295 
05296       // FIXME: This generated code sucks.
05297       ISD::CondCode LowCC;
05298       switch (CCCode) {
05299       default: assert(0 && "Unknown integer setcc!");
05300       case ISD::SETLT:
05301       case ISD::SETULT: LowCC = ISD::SETULT; break;
05302       case ISD::SETGT:
05303       case ISD::SETUGT: LowCC = ISD::SETUGT; break;
05304       case ISD::SETLE:
05305       case ISD::SETULE: LowCC = ISD::SETULE; break;
05306       case ISD::SETGE:
05307       case ISD::SETUGE: LowCC = ISD::SETUGE; break;
05308       }
05309 
05310       // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison
05311       // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands
05312       // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
05313 
05314       // NOTE: on targets without efficient SELECT of bools, we can always use
05315       // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
05316       TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
05317       Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
05318                                LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
05319       if (!Tmp1.getNode())
05320         Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
05321                             LHSLo, RHSLo, LowCC);
05322       Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
05323                                LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
05324       if (!Tmp2.getNode())
05325         Tmp2 = DAG.getNode(ISD::SETCC, dl,
05326                            TLI.getSetCCResultType(LHSHi.getValueType()),
05327                            LHSHi, RHSHi,CC);
05328       
05329       ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
05330       ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
05331       if ((Tmp1C && Tmp1C->isNullValue()) ||
05332           (Tmp2C && Tmp2C->isNullValue() &&
05333            (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
05334             CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
05335           (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
05336            (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
05337             CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
05338         // low part is known false, returns high part.
05339         // For LE / GE, if high part is known false, ignore the low part.
05340         // For LT / GT, if high part is known true, ignore the low part.
05341         Tmp1 = Tmp2;
05342         Tmp2 = SDValue();
05343       } else {
05344         Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
05345                                    LHSHi, RHSHi, ISD::SETEQ, false,
05346                                    DagCombineInfo, dl);
05347         if (!Result.getNode())
05348           Result=DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
05349                               LHSHi, RHSHi, ISD::SETEQ);
05350         Result = LegalizeOp(DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
05351                                         Result, Tmp1, Tmp2));
05352         Tmp1 = Result;
05353         Tmp2 = SDValue();
05354       }
05355     }
05356   }
05357   }
05358   LHS = Tmp1;
05359   RHS = Tmp2;
05360 }
05361 
05362 /// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
05363 /// condition code CC on the current target. This routine assumes LHS and rHS
05364 /// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
05365 /// illegal condition code into AND / OR of multiple SETCC values.
05366 void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
05367                                                  SDValue &LHS, SDValue &RHS,
05368                                                  SDValue &CC,
05369                                                  DebugLoc dl) {
05370   MVT OpVT = LHS.getValueType();
05371   ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
05372   switch (TLI.getCondCodeAction(CCCode, OpVT)) {
05373   default: assert(0 && "Unknown condition code action!");
05374   case TargetLowering::Legal:
05375     // Nothing to do.
05376     break;
05377   case TargetLowering::Expand: {
05378     ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
05379     unsigned Opc = 0;
05380     switch (CCCode) {
05381     default: assert(0 && "Don't know how to expand this condition!"); abort();
05382     case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO;  Opc = ISD::AND; break;
05383     case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO;  Opc = ISD::AND; break;
05384     case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO;  Opc = ISD::AND; break;
05385     case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO;  Opc = ISD::AND; break;
05386     case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO;  Opc = ISD::AND; break;
05387     case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO;  Opc = ISD::AND; break;
05388     case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR;  break;
05389     case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR;  break;
05390     case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR;  break;
05391     case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR;  break;
05392     case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR;  break;
05393     case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR;  break;
05394     // FIXME: Implement more expansions.
05395     }
05396 
05397     SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
05398     SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
05399     LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
05400     RHS = SDValue();
05401     CC  = SDValue();
05402     break;
05403   }
05404   }
05405 }
05406 
05407 /// EmitStackConvert - Emit a store/load combination to the stack.  This stores
05408 /// SrcOp to a stack slot of type SlotVT, truncating it if needed.  It then does
05409 /// a load from the stack slot to DestVT, extending it if needed.
05410 /// The resultant code need not be legal.
05411 SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
05412                                                MVT SlotVT,
05413                                                MVT DestVT,
05414                                                DebugLoc dl) {
05415   // Create the stack frame object.
05416   unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
05417                                           SrcOp.getValueType().getTypeForMVT());
05418   SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
05419   
05420   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
05421   int SPFI = StackPtrFI->getIndex();
05422   const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
05423 
05424   unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
05425   unsigned SlotSize = SlotVT.getSizeInBits();
05426   unsigned DestSize = DestVT.getSizeInBits();
05427   unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
05428                                                         DestVT.getTypeForMVT());
05429   
05430   // Emit a store to the stack slot.  Use a truncstore if the input value is
05431   // later than DestVT.
05432   SDValue Store;
05433   
05434   if (SrcSize > SlotSize)
05435     Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
05436                               SV, 0, SlotVT, false, SrcAlign);
05437   else {
05438     assert(SrcSize == SlotSize && "Invalid store");
05439     Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
05440                          SV, 0, false, SrcAlign);
05441   }
05442   
05443   // Result is a load from the stack slot.
05444   if (SlotSize == DestSize)
05445     return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
05446   
05447   assert(SlotSize < DestSize && "Unknown extension!");
05448   return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
05449                         false, DestAlign);
05450 }
05451 
05452 SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
05453   DebugLoc dl = Node->getDebugLoc();
05454   // Create a vector sized/aligned stack slot, store the value to element #0,
05455   // then load the whole vector back out.
05456   SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
05457 
05458   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
05459   int SPFI = StackPtrFI->getIndex();
05460 
05461   SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(0), 
05462                               StackPtr,
05463                               PseudoSourceValue::getFixedStack(SPFI), 0);
05464   return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
05465                      PseudoSourceValue::getFixedStack(SPFI), 0);
05466 }
05467 
05468 
05469 /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
05470 /// support the operation, but do support the resultant vector type.
05471 SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
05472   
05473   // If the only non-undef value is the low element, turn this into a 
05474   // SCALAR_TO_VECTOR node.  If this is { X, X, X, X }, determine X.
05475   unsigned NumElems = Node->getNumOperands();
05476   bool isOnlyLowElement = true;
05477   SDValue SplatValue = Node->getOperand(0);
05478   DebugLoc dl = Node->getDebugLoc();
05479   
05480   // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
05481   // and use a bitmask instead of a list of elements.
05482   std::map<SDValue, std::vector<unsigned> > Values;
05483   Values[SplatValue].push_back(0);
05484   bool isConstant = true;
05485   if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
05486       SplatValue.getOpcode() != ISD::UNDEF)
05487     isConstant = false;
05488   
05489   for (unsigned i = 1; i < NumElems; ++i) {
05490     SDValue V = Node->getOperand(i);
05491     Values[V].push_back(i);
05492     if (V.getOpcode() != ISD::UNDEF)
05493       isOnlyLowElement = false;
05494     if (SplatValue != V)
05495       SplatValue = SDValue(0,0);
05496 
05497     // If this isn't a constant element or an undef, we can't use a constant
05498     // pool load.
05499     if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
05500         V.getOpcode() != ISD::UNDEF)
05501       isConstant = false;
05502   }
05503   
05504   if (isOnlyLowElement) {
05505     // If the low element is an undef too, then this whole things is an undef.
05506     if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
05507       return DAG.getNode(ISD::UNDEF, dl, Node->getValueType(0));
05508     // Otherwise, turn this into a scalar_to_vector node.
05509     return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, Node->getValueType(0),
05510                        Node->getOperand(0));
05511   }
05512   
05513   // If all elements are constants, create a load from the constant pool.
05514   if (isConstant) {
05515     MVT VT = Node->getValueType(0);
05516     std::vector<Constant*> CV;
05517     for (unsigned i = 0, e = NumElems; i != e; ++i) {
05518       if (ConstantFPSDNode *V = 
05519           dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
05520         CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
05521       } else if (ConstantSDNode *V = 
05522                    dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
05523         CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
05524       } else {
05525         assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
05526         const Type *OpNTy = 
05527           Node->getOperand(0).getValueType().getTypeForMVT();
05528         CV.push_back(UndefValue::get(OpNTy));
05529       }
05530     }
05531     Constant *CP = ConstantVector::get(CV);
05532     SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
05533     unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
05534     return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
05535                        PseudoSourceValue::getConstantPool(), 0,
05536                        false, Alignment);
05537   }
05538   
05539   if (SplatValue.getNode()) {   // Splat of one value?
05540     // Build the shuffle constant vector: <0, 0, 0, 0>
05541     MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
05542     SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
05543     std::vector<SDValue> ZeroVec(NumElems, Zero);
05544     SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
05545                                       &ZeroVec[0], ZeroVec.size());
05546 
05547     // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
05548     if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
05549       // Get the splatted value into the low element of a vector register.
05550       SDValue LowValVec = 
05551         DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, 
05552                     Node->getValueType(0), SplatValue);
05553     
05554       // Return shuffle(LowValVec, undef, <0,0,0,0>)
05555       return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, 
05556                          Node->getValueType(0), LowValVec,
05557                          DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
05558                          SplatMask);
05559     }
05560   }
05561   
05562   // If there are only two unique elements, we may be able to turn this into a
05563   // vector shuffle.
05564   if (Values.size() == 2) {
05565     // Get the two values in deterministic order.
05566     SDValue Val1 = Node->getOperand(1);
05567     SDValue Val2;
05568     std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
05569     if (MI->first != Val1)
05570       Val2 = MI->first;
05571     else
05572       Val2 = (++MI)->first;
05573     
05574     // If Val1 is an undef, make sure end ends up as Val2, to ensure that our 
05575     // vector shuffle has the undef vector on the RHS.
05576     if (Val1.getOpcode() == ISD::UNDEF)
05577       std::swap(Val1, Val2);
05578     
05579     // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
05580     MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
05581     MVT MaskEltVT = MaskVT.getVectorElementType();
05582     std::vector<SDValue> MaskVec(NumElems);
05583 
05584     // Set elements of the shuffle mask for Val1.
05585     std::vector<unsigned> &Val1Elts = Values[Val1];
05586     for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
05587       MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
05588 
05589     // Set elements of the shuffle mask for Val2.
05590     std::vector<unsigned> &Val2Elts = Values[Val2];
05591     for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
05592       if (Val2.getOpcode() != ISD::UNDEF)
05593         MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
05594       else
05595         MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, dl, MaskEltVT);
05596     
05597     SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
05598                                         &MaskVec[0], MaskVec.size());
05599 
05600     // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
05601     if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR,
05602                                      Node->getValueType(0)) &&
05603         isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
05604       Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,Node->getValueType(0), Val1);
05605       Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,Node->getValueType(0), Val2);
05606       SDValue Ops[] = { Val1, Val2, ShuffleMask };
05607 
05608       // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
05609       return DAG.getNode(ISD::VECTOR_SHUFFLE, dl,Node->getValueType(0), Ops, 3);
05610     }
05611   }
05612   
05613   // Otherwise, we can't handle this case efficiently.  Allocate a sufficiently
05614   // aligned object on the stack, store each element into it, then load
05615   // the result as a vector.
05616   MVT VT = Node->getValueType(0);
05617   // Create the stack frame object.
05618   SDValue FIPtr = DAG.CreateStackTemporary(VT);
05619   int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
05620   const Value *SV = PseudoSourceValue::getFixedStack(FI);
05621 
05622   // Emit a store of each element to the stack slot.
05623   SmallVector<SDValue, 8> Stores;
05624   unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
05625   // Store (in the right endianness) the elements to memory.
05626   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
05627     // Ignore undef elements.
05628     if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
05629     
05630     unsigned Offset = TypeByteSize*i;
05631     
05632     SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
05633     Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
05634     
05635     Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
05636                                   Idx, SV, Offset));
05637   }
05638   
05639   SDValue StoreChain;
05640   if (!Stores.empty())    // Not all undef elements?
05641     StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
05642                              &Stores[0], Stores.size());
05643   else
05644     StoreChain = DAG.getEntryNode();
05645   
05646   // Result is a load from the stack slot.
05647   return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
05648 }
05649 
05650 void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
05651                                             SDValue Op, SDValue Amt,
05652                                             SDValue &Lo, SDValue &Hi,
05653                                             DebugLoc dl) {
05654   // Expand the subcomponents.
05655   SDValue LHSL, LHSH;
05656   ExpandOp(Op, LHSL, LHSH);
05657 
05658   SDValue Ops[] = { LHSL, LHSH, Amt };
05659   MVT VT = LHSL.getValueType();
05660   Lo = DAG.getNode(NodeOp, dl, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
05661   Hi = Lo.getValue(1);
05662 }
05663 
05664 
05665 /// ExpandShift - Try to find a clever way to expand this shift operation out to
05666 /// smaller elements.  If we can't find a way that is more efficient than a
05667 /// libcall on this target, return false.  Otherwise, return true with the
05668 /// low-parts expanded into Lo and Hi.
05669 bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
05670                                        SDValue &Lo, SDValue &Hi,
05671                                        DebugLoc dl) {
05672   assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
05673          "This is not a shift!");
05674 
05675   MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
05676   SDValue ShAmt = LegalizeOp(Amt);
05677   MVT ShTy = ShAmt.getValueType();
05678   unsigned ShBits = ShTy.getSizeInBits();
05679   unsigned VTBits = Op.getValueType().getSizeInBits();
05680   unsigned NVTBits = NVT.getSizeInBits();
05681 
05682   // Handle the case when Amt is an immediate.
05683   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
05684     unsigned Cst = CN->getZExtValue();
05685     // Expand the incoming operand to be shifted, so that we have its parts
05686     SDValue InL, InH;
05687     ExpandOp(Op, InL, InH);
05688     switch(Opc) {
05689     case ISD::SHL:
05690       if (Cst > VTBits) {
05691         Lo = DAG.getConstant(0, NVT);
05692         Hi = DAG.getConstant(0, NVT);
05693       } else if (Cst > NVTBits) {
05694         Lo = DAG.getConstant(0, NVT);
05695         Hi = DAG.getNode(ISD::SHL, dl,
05696                          NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
05697       } else if (Cst == NVTBits) {
05698         Lo = DAG.getConstant(0, NVT);
05699         Hi = InL;
05700       } else {
05701         Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, DAG.getConstant(Cst, ShTy));
05702         Hi = DAG.getNode(ISD::OR, dl, NVT,
05703            DAG.getNode(ISD::SHL, dl, NVT, InH, DAG.getConstant(Cst, ShTy)),
05704            DAG.getNode(ISD::SRL, dl, NVT, InL, 
05705                        DAG.getConstant(NVTBits-Cst, ShTy)));
05706       }
05707       return true;
05708     case ISD::SRL:
05709       if (Cst > VTBits) {
05710         Lo = DAG.getConstant(0, NVT);
05711         Hi = DAG.getConstant(0, NVT);
05712       } else if (Cst > NVTBits) {
05713         Lo = DAG.getNode(ISD::SRL, dl, NVT, 
05714                          InH, DAG.getConstant(Cst-NVTBits,ShTy));
05715         Hi = DAG.getConstant(0, NVT);
05716       } else if (Cst == NVTBits) {
05717         Lo = InH;
05718         Hi = DAG.getConstant(0, NVT);
05719       } else {
05720         Lo = DAG.getNode(ISD::OR, dl, NVT,
05721            DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
05722            DAG.getNode(ISD::SHL, dl, NVT, InH, 
05723                        DAG.getConstant(NVTBits-Cst, ShTy)));
05724         Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
05725       }
05726       return true;
05727     case ISD::SRA:
05728       if (Cst > VTBits) {
05729         Hi = Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
05730                               DAG.getConstant(NVTBits-1, ShTy));
05731       } else if (Cst > NVTBits) {
05732         Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
05733                            DAG.getConstant(Cst-NVTBits, ShTy));
05734         Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
05735                               DAG.getConstant(NVTBits-1, ShTy));
05736       } else if (Cst == NVTBits) {
05737         Lo = InH;
05738         Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
05739                               DAG.getConstant(NVTBits-1, ShTy));
05740       } else {
05741         Lo = DAG.getNode(ISD::OR, dl, NVT,
05742            DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
05743            DAG.getNode(ISD::SHL, dl, 
05744                        NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
05745         Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
05746       }
05747       return true;
05748     }
05749   }
05750   
05751   // Okay, the shift amount isn't constant.  However, if we can tell that it is
05752   // >= 32 or < 32, we can still simplify it, without knowing the actual value.
05753   APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
05754   APInt KnownZero, KnownOne;
05755   DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
05756   
05757   // If we know that if any of the high bits of the shift amount are one, then
05758   // we can do this as a couple of simple shifts.
05759   if (KnownOne.intersects(Mask)) {
05760     // Mask out the high bit, which we know is set.
05761     Amt = DAG.getNode(ISD::AND, dl, Amt.getValueType(), Amt,
05762                       DAG.getConstant(~Mask, Amt.getValueType()));
05763     
05764     // Expand the incoming operand to be shifted, so that we have its parts
05765     SDValue InL, InH;
05766     ExpandOp(Op, InL, InH);
05767     switch(Opc) {
05768     case ISD::SHL:
05769       Lo = DAG.getConstant(0, NVT);              // Low part is zero.
05770       Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
05771       return true;
05772     case ISD::SRL:
05773       Hi = DAG.getConstant(0, NVT);              // Hi part is zero.
05774       Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
05775       return true;
05776     case ISD::SRA:
05777       Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,       // Sign extend high part.
05778                        DAG.getConstant(NVTBits-1, Amt.getValueType()));
05779       Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
05780       return true;
05781     }
05782   }
05783   
05784   // If we know that the high bits of the shift amount are all zero, then we can
05785   // do this as a couple of simple shifts.
05786   if ((KnownZero & Mask) == Mask) {
05787     // Compute 32-amt.
05788     SDValue Amt2 = DAG.getNode(ISD::SUB, dl, Amt.getValueType(),
05789                                  DAG.getConstant(NVTBits, Amt.getValueType()),
05790                                  Amt);
05791     
05792     // Expand the incoming operand to be shifted, so that we have its parts
05793     SDValue InL, InH;
05794     ExpandOp(Op, InL, InH);
05795     switch(Opc) {
05796     case ISD::SHL:
05797       Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
05798       Hi = DAG.getNode(ISD::OR, dl, NVT,
05799                        DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
05800                        DAG.getNode(ISD::SRL, dl, NVT, InL, Amt2));
05801       return true;
05802     case ISD::SRL:
05803       Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
05804       Lo = DAG.getNode(ISD::OR, dl, NVT,
05805                        DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
05806                        DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
05807       return true;
05808     case ISD::SRA:
05809       Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
05810       Lo = DAG.getNode(ISD::OR, dl, NVT,
05811                        DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
05812                        DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
05813       return true;
05814     }
05815   }
05816   
05817   return false;
05818 }
05819 
05820 
05821 // ExpandLibCall - Expand a node into a call to a libcall.  If the result value
05822 // does not fit into a register, return the lo part and set the hi part to the
05823 // by-reg argument.  If it does fit into a single register, return the result
05824 // and leave the Hi part unset.
05825 SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
05826                                             bool isSigned, SDValue &Hi) {
05827   assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
05828   // The input chain to this libcall is the entry node of the function. 
05829   // Legalizing the call will automatically add the previous call to the
05830   // dependence.
05831   SDValue InChain = DAG.getEntryNode();
05832   
05833   TargetLowering::ArgListTy Args;
05834   TargetLowering::ArgListEntry Entry;
05835   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
05836     MVT ArgVT = Node->getOperand(i).getValueType();
05837     const Type *ArgTy = ArgVT.getTypeForMVT();
05838     Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; 
05839     Entry.isSExt = isSigned;
05840     Entry.isZExt = !isSigned;
05841     Args.push_back(Entry);
05842   }
05843   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
05844                                          TLI.getPointerTy());
05845 
05846   // Splice the libcall in wherever FindInputOutputChains tells us to.
05847   const Type *RetTy = Node->getValueType(0).getTypeForMVT();
05848   std::pair<SDValue,SDValue> CallInfo =
05849     TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
05850                     CallingConv::C, false, Callee, Args, DAG,
05851                     Node->getDebugLoc());
05852 
05853   // Legalize the call sequence, starting with the chain.  This will advance
05854   // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
05855   // was added by LowerCallTo (guaranteeing proper serialization of calls).
05856   LegalizeOp(CallInfo.second);
05857   SDValue Result;
05858   switch (getTypeAction(CallInfo.first.getValueType())) {
05859   default: assert(0 && "Unknown thing");
05860   case Legal:
05861     Result = CallInfo.first;
05862     break;
05863   case Expand:
05864     ExpandOp(CallInfo.first, Result, Hi);
05865     break;
05866   }
05867   return Result;
05868 }
05869 
05870 /// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
05871 ///
05872 SDValue SelectionDAGLegalize::
05873 LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op,
05874                   DebugLoc dl) {
05875   bool isCustom = false;
05876   SDValue Tmp1;
05877   switch (getTypeAction(Op.getValueType())) {
05878   case Legal:
05879     switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
05880                                    Op.getValueType())) {
05881     default: assert(0 && "Unknown operation action!");
05882     case TargetLowering::Custom:
05883       isCustom = true;
05884       // FALLTHROUGH
05885     case TargetLowering::Legal:
05886       Tmp1 = LegalizeOp(Op);
05887       if (Result.getNode())
05888         Result = DAG.UpdateNodeOperands(Result, Tmp1);
05889       else
05890         Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
05891                              DestTy, Tmp1);
05892       if (isCustom) {
05893         Tmp1 = TLI.LowerOperation(Result, DAG);
05894         if (Tmp1.getNode()) Result = Tmp1;
05895       }
05896       break;
05897     case TargetLowering::Expand:
05898       Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy, dl);
05899       break;
05900     case TargetLowering::Promote:
05901       Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned, dl);
05902       break;
05903     }
05904     break;
05905   case Expand:
05906     Result = ExpandIntToFP(isSigned, DestTy, Op, dl) ;
05907     break;
05908   case Promote:
05909     Tmp1 = PromoteOp(Op);
05910     if (isSigned) {
05911       Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp1.getValueType(),
05912                Tmp1, DAG.getValueType(Op.getValueType()));
05913     } else {
05914       Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, 
05915                                     Op.getValueType());
05916     }
05917     if (Result.getNode())
05918       Result = DAG.UpdateNodeOperands(Result, Tmp1);
05919     else
05920       Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
05921                            DestTy, Tmp1);
05922     Result = LegalizeOp(Result);  // The 'op' is not necessarily legal!
05923     break;
05924   }
05925   return Result;
05926 }
05927 
05928 /// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
05929 ///
05930 SDValue SelectionDAGLegalize::
05931 ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl) {
05932   MVT SourceVT = Source.getValueType();
05933   bool ExpandSource = getTypeAction(SourceVT) == Expand;
05934 
05935   // Expand unsupported int-to-fp vector casts by unrolling them.
05936   if (DestTy.isVector()) {
05937     if (!ExpandSource)
05938       return LegalizeOp(UnrollVectorOp(Source));
05939     MVT DestEltTy = DestTy.getVectorElementType();
05940     if (DestTy.getVectorNumElements() == 1) {
05941       SDValue Scalar = ScalarizeVectorOp(Source);
05942       SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
05943                                          DestEltTy, Scalar, dl);
05944       return DAG.getNode(ISD::BUILD_VECTOR, dl, DestTy, Result);
05945     }
05946     SDValue Lo, Hi;
05947     SplitVectorOp(Source, Lo, Hi);
05948     MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
05949                                        DestTy.getVectorNumElements() / 2);
05950     SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, 
05951                                          Lo, dl);
05952     SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, 
05953                                          Hi, dl);
05954     return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, DestTy, LoResult,
05955                                   HiResult));
05956   }
05957 
05958   // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
05959   if (!isSigned && SourceVT != MVT::i32) {
05960     // The integer value loaded will be incorrectly if the 'sign bit' of the
05961     // incoming integer is set.  To handle this, we dynamically test to see if
05962     // it is set, and, if so, add a fudge factor.
05963     SDValue Hi;
05964     if (ExpandSource) {
05965       SDValue Lo;
05966       ExpandOp(Source, Lo, Hi);
05967       Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, Lo, Hi);
05968     } else {
05969       // The comparison for the sign bit will use the entire operand.
05970       Hi = Source;
05971     }
05972 
05973     // Check to see if the target has a custom way to lower this.  If so, use
05974     // it.  (Note we've already expanded the operand in this case.)
05975     switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
05976     default: assert(0 && "This action not implemented for this operation!");
05977     case TargetLowering::Legal:
05978     case TargetLowering::Expand:
05979       break;   // This case is handled below.
05980     case TargetLowering::Custom: {
05981       SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
05982                                                     Source), DAG);
05983       if (NV.getNode())
05984         return LegalizeOp(NV);
05985       break;   // The target decided this was legal after all
05986     }
05987     }
05988 
05989     // If this is unsigned, and not supported, first perform the conversion to
05990     // signed, then adjust the result if the sign bit is set.
05991     SDValue SignedConv = ExpandIntToFP(true, DestTy, Source, dl);
05992 
05993     SDValue SignSet = DAG.getSetCC(dl, 
05994                                    TLI.getSetCCResultType(Hi.getValueType()),
05995                                    Hi, DAG.getConstant(0, Hi.getValueType()),
05996                                    ISD::SETLT);
05997     SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
05998     SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
05999                                       SignSet, Four, Zero);
06000     uint64_t FF = 0x5f800000ULL;
06001     if (TLI.isLittleEndian()) FF <<= 32;
06002     static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
06003 
06004     SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
06005     unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
06006     CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
06007     Alignment = std::min(Alignment, 4u);
06008     SDValue FudgeInReg;
06009     if (DestTy == MVT::f32)
06010       FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
06011                                PseudoSourceValue::getConstantPool(), 0,
06012                                false, Alignment);
06013     else if (DestTy.bitsGT(MVT::f32))
06014       // FIXME: Avoid the extend by construction the right constantpool?
06015       FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, dl, DestTy, DAG.getEntryNode(),
06016                                   CPIdx,
06017                                   PseudoSourceValue::getConstantPool(), 0,
06018                                   MVT::f32, false, Alignment);
06019     else 
06020       assert(0 && "Unexpected conversion");
06021 
06022     MVT SCVT = SignedConv.getValueType();
06023     if (SCVT != DestTy) {
06024       // Destination type needs to be expanded as well. The FADD now we are
06025       // constructing will be expanded into a libcall.
06026       if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
06027         assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
06028         SignedConv = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy,
06029                                  SignedConv, SignedConv.getValue(1));
06030       }
06031       SignedConv = DAG.getNode(ISD::BIT_CONVERT, dl, DestTy, SignedConv);
06032     }
06033     return DAG.getNode(ISD::FADD, dl, DestTy, SignedConv, FudgeInReg);
06034   }
06035 
06036   // Check to see if the target has a custom way to lower this.  If so, use it.
06037   switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
06038   default: assert(0 && "This action not implemented for this operation!");
06039   case TargetLowering::Legal:
06040   case TargetLowering::Expand:
06041     break;   // This case is handled below.
06042   case TargetLowering::Custom: {
06043     SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, dl, DestTy,
06044                                                   Source), DAG);
06045     if (NV.getNode())
06046       return LegalizeOp(NV);
06047     break;   // The target decided this was legal after all
06048   }
06049   }
06050 
06051   // Expand the source, then glue it back together for the call.  We must expand
06052   // the source in case it is shared (this pass of legalize must traverse it).
06053   if (ExpandSource) {
06054     SDValue SrcLo, SrcHi;
06055     ExpandOp(Source, SrcLo, SrcHi);
06056     Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, SrcLo, SrcHi);
06057   }
06058 
06059   RTLIB::Libcall LC = isSigned ?
06060     RTLIB::getSINTTOFP(SourceVT, DestTy) :
06061     RTLIB::getUINTTOFP(SourceVT, DestTy);
06062   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
06063 
06064   Source = DAG.getNode(ISD::SINT_TO_FP, dl, DestTy, Source);
06065   SDValue HiPart;
06066   SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
06067   if (Result.getValueType() != DestTy && HiPart.getNode())
06068     Result = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy, Result, HiPart);
06069   return Result;
06070 }
06071 
06072 /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
06073 /// INT_TO_FP operation of the specified operand when the target requests that
06074 /// we expand it.  At this point, we know that the result and operand types are
06075 /// legal for the target.
06076 SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
06077                                                    SDValue Op0,
06078                                                    MVT DestVT,
06079                                                    DebugLoc dl) {
06080   if (Op0.getValueType() == MVT::i32) {
06081     // simple 32-bit [signed|unsigned] integer to float/double expansion
06082     
06083     // Get the stack frame index of a 8 byte buffer.
06084     SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
06085     
06086     // word offset constant for Hi/Lo address computation
06087     SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
06088     // set up Hi and Lo (into buffer) address based on endian
06089     SDValue Hi = StackSlot;
06090     SDValue Lo = DAG.getNode(ISD::ADD, dl, 
06091                              TLI.getPointerTy(), StackSlot,WordOff);
06092     if (TLI.isLittleEndian())
06093       std::swap(Hi, Lo);
06094     
06095     // if signed map to unsigned space
06096     SDValue Op0Mapped;
06097     if (isSigned) {
06098       // constant used to invert sign bit (signed to unsigned mapping)
06099       SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
06100       Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
06101     } else {
06102       Op0Mapped = Op0;
06103     }
06104     // store the lo of the constructed double - based on integer input
06105     SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
06106                                     Op0Mapped, Lo, NULL, 0);
06107     // initial hi portion of constructed double
06108     SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
06109     // store the hi of the constructed double - biased exponent
06110     SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
06111     // load the constructed double
06112     SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
06113     // FP constant to bias correct the final result
06114     SDValue Bias = DAG.getConstantFP(isSigned ?
06115                                             BitsToDouble(0x4330000080000000ULL)
06116                                           : BitsToDouble(0x4330000000000000ULL),
06117                                      MVT::f64);
06118     // subtract the bias
06119     SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
06120     // final result
06121     SDValue Result;
06122     // handle final rounding
06123     if (DestVT == MVT::f64) {
06124       // do nothing
06125       Result = Sub;
06126     } else if (DestVT.bitsLT(MVT::f64)) {
06127       Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
06128                            DAG.getIntPtrConstant(0));
06129     } else if (DestVT.bitsGT(MVT::f64)) {
06130       Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
06131     }
06132     return Result;
06133   }
06134   assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
06135   SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
06136 
06137   SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
06138                                  Op0, DAG.getConstant(0, Op0.getValueType()),
06139                                  ISD::SETLT);
06140   SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
06141   SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
06142                                     SignSet, Four, Zero);
06143 
06144   // If the sign bit of the integer is set, the large number will be treated
06145   // as a negative number.  To counteract this, the dynamic code adds an
06146   // offset depending on the data type.
06147   uint64_t FF;
06148   switch (Op0.getValueType().getSimpleVT()) {
06149   default: assert(0 && "Unsupported integer type!");
06150   case MVT::i8 : FF = 0x43800000ULL; break;  // 2^8  (as a float)
06151   case MVT::i16: FF = 0x47800000ULL; break;  // 2^16 (as a float)
06152   case MVT::i32: FF = 0x4F800000ULL; break;  // 2^32 (as a float)
06153   case MVT::i64: FF = 0x5F800000ULL; break;  // 2^64 (as a float)
06154   }
06155   if (TLI.isLittleEndian()) FF <<= 32;
06156   static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
06157 
06158   SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
06159   unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
06160   CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
06161   Alignment = std::min(Alignment, 4u);
06162   SDValue FudgeInReg;
06163   if (DestVT == MVT::f32)
06164     FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
06165                              PseudoSourceValue::getConstantPool(), 0,
06166                              false, Alignment);
06167   else {
06168     FudgeInReg =
06169       LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
06170                                 DAG.getEntryNode(), CPIdx,
06171                                 PseudoSourceValue::getConstantPool(), 0,
06172                                 MVT::f32, false, Alignment));
06173   }
06174 
06175   return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
06176 }
06177 
06178 /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
06179 /// *INT_TO_FP operation of the specified operand when the target requests that
06180 /// we promote it.  At this point, we know that the result and operand types are
06181 /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
06182 /// operation that takes a larger input.
06183 SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
06184                                                     MVT DestVT,
06185                                                     bool isSigned,
06186                                                     DebugLoc dl) {
06187   // First step, figure out the appropriate *INT_TO_FP operation to use.
06188   MVT NewInTy = LegalOp.getValueType();
06189 
06190   unsigned OpToUse = 0;
06191 
06192   // Scan for the appropriate larger type to use.
06193   while (1) {
06194     NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
06195     assert(NewInTy.isInteger() && "Ran out of possibilities!");
06196 
06197     // If the target supports SINT_TO_FP of this type, use it.
06198     switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
06199       default: break;
06200       case TargetLowering::Legal:
06201         if (!TLI.isTypeLegal(NewInTy))
06202           break;  // Can't use this datatype.
06203         // FALL THROUGH.
06204       case TargetLowering::Custom:
06205         OpToUse = ISD::SINT_TO_FP;
06206         break;
06207     }
06208     if (OpToUse) break;
06209     if (isSigned) continue;
06210 
06211     // If the target supports UINT_TO_FP of this type, use it.
06212     switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
06213       default: break;
06214       case TargetLowering::Legal:
06215         if (!TLI.isTypeLegal(NewInTy))
06216           break;  // Can't use this datatype.
06217         // FALL THROUGH.
06218       case TargetLowering::Custom:
06219         OpToUse = ISD::UINT_TO_FP;
06220         break;
06221     }
06222     if (OpToUse) break;
06223 
06224     // Otherwise, try a larger type.
06225   }
06226 
06227   // Okay, we found the operation and type to use.  Zero extend our input to the
06228   // desired type then run the operation on it.
06229   return DAG.getNode(OpToUse, dl, DestVT,
06230                      DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
06231                                  dl, NewInTy, LegalOp));
06232 }
06233 
06234 /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
06235 /// FP_TO_*INT operation of the specified operand when the target requests that
06236 /// we promote it.  At this point, we know that the result and operand types are
06237 /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
06238 /// operation that returns a larger result.
06239 SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
06240                                                     MVT DestVT,
06241                                                     bool isSigned,
06242                                                     DebugLoc dl) {
06243   // First step, figure out the appropriate FP_TO*INT operation to use.
06244   MVT NewOutTy = DestVT;
06245 
06246   unsigned OpToUse = 0;
06247 
06248   // Scan for the appropriate larger type to use.
06249   while (1) {
06250     NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
06251     assert(NewOutTy.isInteger() && "Ran out of possibilities!");
06252 
06253     // If the target supports FP_TO_SINT returning this type, use it.
06254     switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
06255     default: break;
06256     case TargetLowering::Legal:
06257       if (!TLI.isTypeLegal(NewOutTy))
06258         break;  // Can't use this datatype.
06259       // FALL THROUGH.
06260     case TargetLowering::Custom:
06261       OpToUse = ISD::FP_TO_SINT;
06262       break;
06263     }
06264     if (OpToUse) break;
06265 
06266     // If the target supports FP_TO_UINT of this type, use it.
06267     switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
06268     default: break;
06269     case TargetLowering::Legal:
06270       if (!TLI.isTypeLegal(NewOutTy))
06271         break;  // Can't use this datatype.
06272       // FALL THROUGH.
06273     case TargetLowering::Custom:
06274       OpToUse = ISD::FP_TO_UINT;
06275       break;
06276     }
06277     if (OpToUse) break;
06278 
06279     // Otherwise, try a larger type.
06280   }
06281 
06282   
06283   // Okay, we found the operation and type to use.
06284   SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
06285 
06286   // If the operation produces an invalid type, it must be custom lowered.  Use
06287   // the target lowering hooks to expand it.  Just keep the low part of the
06288   // expanded operation, we know that we're truncating anyway.
06289   if (getTypeAction(NewOutTy) == Expand) {
06290     SmallVector<SDValue, 2> Results;
06291     TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
06292     assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
06293     Operation = Results[0];
06294   }
06295 
06296   // Truncate the result of the extended FP_TO_*INT operation to the desired
06297   // size.
06298   return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
06299 }
06300 
06301 /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
06302 ///
06303 SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
06304   MVT VT = Op.getValueType();
06305   MVT SHVT = TLI.getShiftAmountTy();
06306   SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
06307   switch (VT.getSimpleVT()) {
06308   default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
06309   case MVT::i16:
06310     Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
06311     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
06312     return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
06313   case MVT::i32:
06314     Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
06315     Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
06316     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
06317     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
06318     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
06319     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
06320     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
06321     Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
06322     return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
06323   case MVT::i64:
06324     Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
06325     Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
06326     Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
06327     Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
06328     Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
06329     Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
06330     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
06331     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
06332     Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
06333     Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
06334     Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
06335     Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
06336     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
06337     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
06338     Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
06339     Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
06340     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
06341     Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
06342     Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
06343     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
06344     return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
06345   }
06346 }
06347 
06348 /// ExpandBitCount - Expand the specified bitcount instruction into operations.
06349 ///
06350 SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op, 
06351                                              DebugLoc dl) {
06352   switch (Opc) {
06353   default: assert(0 && "Cannot expand this yet!");
06354   case ISD::CTPOP: {
06355     static const uint64_t mask[6] = {
06356       0x5555555555555555ULL, 0x3333333333333333ULL,
06357       0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
06358       0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
06359     };
06360     MVT VT = Op.getValueType();
06361     MVT ShVT = TLI.getShiftAmountTy();
06362     unsigned len = VT.getSizeInBits();
06363     for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
06364       //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
06365       unsigned EltSize = VT.isVector() ?
06366         VT.getVectorElementType().getSizeInBits() : len;
06367       SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
06368       SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
06369       Op = DAG.getNode(ISD::ADD, dl, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
06370                        DAG.getNode(ISD::AND, dl, VT,
06371                                    DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
06372                                    Tmp2));
06373     }
06374     return Op;
06375   }
06376   case ISD::CTLZ: {
06377     // for now, we do this:
06378     // x = x | (x >> 1);
06379     // x = x | (x >> 2);
06380     // ...
06381     // x = x | (x >>16);
06382     // x = x | (x >>32); // for 64-bit input
06383     // return popcount(~x);
06384     //
06385     // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
06386     MVT VT = Op.getValueType();
06387     MVT ShVT = TLI.getShiftAmountTy();
06388     unsigned len = VT.getSizeInBits();
06389     for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
06390       SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
06391       Op = DAG.getNode(ISD::OR, dl, VT, Op, 
06392                        DAG.getNode(ISD::SRL, VT, Op, Tmp3));
06393     }
06394     Op = DAG.getNOT(dl, Op, VT);
06395     return DAG.getNode(ISD::CTPOP, dl, VT, Op);
06396   }
06397   case ISD::CTTZ: {
06398     // for now, we use: { return popcount(~x & (x - 1)); }
06399     // unless the target has ctlz but not ctpop, in which case we use:
06400     // { return 32 - nlz(~x & (x-1)); }
06401     // see also http://www.hackersdelight.org/HDcode/ntz.cc
06402     MVT VT = Op.getValueType();
06403     SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
06404                                DAG.getNOT(dl, Op, VT),
06405                                DAG.getNode(ISD::SUB, dl, VT, Op,
06406                                            DAG.getConstant(1, VT)));
06407     // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
06408     if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
06409         TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
06410       return DAG.getNode(ISD::SUB, dl, VT,
06411                          DAG.getConstant(VT.getSizeInBits(), VT),
06412                          DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
06413     return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
06414   }
06415   }
06416 }
06417 
06418 /// ExpandOp - Expand the specified SDValue into its two component pieces
06419 /// Lo&Hi.  Note that the Op MUST be an expanded type.  As a result of this, the
06420 /// LegalizedNodes map is filled in for any results that are not expanded, the
06421 /// ExpandedNodes map is filled in for any results that are expanded, and the
06422 /// Lo/Hi values are returned.
06423 void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
06424   MVT VT = Op.getValueType();
06425   MVT NVT = TLI.getTypeToTransformTo(VT);
06426   SDNode *Node = Op.getNode();
06427   DebugLoc dl = Node->getDebugLoc();
06428   assert(getTypeAction(VT) == Expand && "Not an expanded type!");
06429   assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
06430          VT.isVector()) && "Cannot expand to FP value or to larger int value!");
06431 
06432   // See if we already expanded it.
06433   DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
06434     = ExpandedNodes.find(Op);
06435   if (I != ExpandedNodes.end()) {
06436     Lo = I->second.first;
06437     Hi = I->second.second;
06438     return;
06439   }
06440 
06441   switch (Node->getOpcode()) {
06442   case ISD::CopyFromReg:
06443     assert(0 && "CopyFromReg must be legal!");
06444   case ISD::FP_ROUND_INREG:
06445     if (VT == MVT::ppcf128 && 
06446         TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) == 
06447             TargetLowering::Custom) {
06448       SDValue SrcLo, SrcHi, Src;
06449       ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
06450       Src = DAG.getNode(ISD::BUILD_PAIR, dl, VT, SrcLo, SrcHi);
06451       SDValue Result = TLI.LowerOperation(
06452         DAG.getNode(ISD::FP_ROUND_INREG, dl, VT, Src, Op.getOperand(1)), DAG);
06453       assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
06454       Lo = Result.getNode()->getOperand(0);
06455       Hi = Result.getNode()->getOperand(1);
06456       break;
06457     }
06458     // fall through
06459   default:
06460 #ifndef NDEBUG
06461     cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
06462 #endif
06463     assert(0 && "Do not know how to expand this operator!");
06464     abort();
06465   case ISD::EXTRACT_ELEMENT:
06466     ExpandOp(Node->getOperand(0), Lo, Hi);
06467     if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
06468       return ExpandOp(Hi, Lo, Hi);
06469     return ExpandOp(Lo, Lo, Hi);
06470   case ISD::EXTRACT_VECTOR_ELT:
06471     // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
06472     Lo  = ExpandEXTRACT_VECTOR_ELT(Op);
06473     return ExpandOp(Lo, Lo, Hi);
06474   case ISD::UNDEF:
06475     Lo = DAG.getNode(ISD::UNDEF, dl, NVT);
06476     Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
06477     break;
06478   case ISD::Constant: {
06479     unsigned NVTBits = NVT.getSizeInBits();
06480     const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
06481     Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
06482     Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
06483     break;
06484   }
06485   case ISD::ConstantFP: {
06486     ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
06487     if (CFP->getValueType(0) == MVT::ppcf128) {
06488       APInt api = CFP->getValueAPF().bitcastToAPInt();
06489       Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
06490                              MVT::f64);
06491       Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])), 
06492                              MVT::f64);
06493       break;
06494     }
06495     Lo = ExpandConstantFP(CFP, false, DAG, TLI);
06496     if (getTypeAction(Lo.getValueType()) == Expand)
06497       ExpandOp(Lo, Lo, Hi);
06498     break;
06499   }
06500   case ISD::BUILD_PAIR:
06501     // Return the operands.
06502     Lo = Node->getOperand(0);
06503     Hi = Node->getOperand(1);
06504     break;
06505       
06506   case ISD::MERGE_VALUES:
06507     if (Node->getNumValues() == 1) {
06508       ExpandOp(Op.getOperand(0), Lo, Hi);
06509       break;
06510     }
06511     // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
06512     assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
06513            Op.getValue(1).getValueType() == MVT::Other &&
06514            "unhandled MERGE_VALUES");
06515     ExpandOp(Op.getOperand(0), Lo, Hi);
06516     // Remember that we legalized the chain.
06517     AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
06518     break;
06519     
06520   case ISD::SIGN_EXTEND_INREG:
06521     ExpandOp(Node->getOperand(0), Lo, Hi);
06522     // sext_inreg the low part if needed.
06523     Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Lo, Node->getOperand(1));
06524     
06525     // The high part gets the sign extension from the lo-part.  This handles
06526     // things like sextinreg V:i64 from i8.
06527     Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
06528                      DAG.getConstant(NVT.getSizeInBits()-1,
06529                                      TLI.getShiftAmountTy()));
06530     break;
06531 
06532   case ISD::BSWAP: {
06533     ExpandOp(Node->getOperand(0), Lo, Hi);
06534     SDValue TempLo = DAG.getNode(ISD::BSWAP, dl, NVT, Hi);
06535     Hi = DAG.getNode(ISD::BSWAP, dl, NVT, Lo);
06536     Lo = TempLo;
06537     break;
06538   }
06539     
06540   case ISD::CTPOP:
06541     ExpandOp(Node->getOperand(0), Lo, Hi);
06542     Lo = DAG.getNode(ISD::ADD, dl, NVT,      // ctpop(HL) -> ctpop(H)+ctpop(L)
06543                      DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
06544                      DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
06545     Hi = DAG.getConstant(0, NVT);
06546     break;
06547 
06548   case ISD::CTLZ: {
06549     // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
06550     ExpandOp(Node->getOperand(0), Lo, Hi);
06551     SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
06552     SDValue HLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
06553     SDValue TopNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), HLZ,
06554                                       BitsC, ISD::SETNE);
06555     SDValue LowPart = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
06556     LowPart = DAG.getNode(ISD::ADD, dl, NVT, LowPart, BitsC);
06557 
06558     Lo = DAG.getNode(ISD::SELECT, dl, NVT, TopNotZero, HLZ, LowPart);
06559     Hi = DAG.getConstant(0, NVT);
06560     break;
06561   }
06562 
06563   case ISD::CTTZ: {
06564     // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
06565     ExpandOp(Node->getOperand(0), Lo, Hi);
06566     SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
06567     SDValue LTZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
06568     SDValue BotNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), LTZ,
06569                                       BitsC, ISD::SETNE);
06570     SDValue HiPart = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
06571     HiPart = DAG.getNode(ISD::ADD, dl, NVT, HiPart, BitsC);
06572 
06573     Lo = DAG.getNode(ISD::SELECT, dl, NVT, BotNotZero, LTZ, HiPart);
06574     Hi = DAG.getConstant(0, NVT);
06575     break;
06576   }
06577 
06578   case ISD::VAARG: {
06579     SDValue Ch = Node->getOperand(0);   // Legalize the chain.
06580     SDValue Ptr = Node->getOperand(1);  // Legalize the pointer.
06581     Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
06582     Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
06583 
06584     // Remember that we legalized the chain.
06585     Hi = LegalizeOp(Hi);
06586     AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
06587     if (TLI.isBigEndian())
06588       std::swap(Lo, Hi);
06589     break;
06590   }
06591     
06592   case ISD::LOAD: {
06593     LoadSDNode *LD = cast<LoadSDNode>(Node);
06594     SDValue Ch  = LD->getChain();    // Legalize the chain.
06595     SDValue Ptr = LD->getBasePtr();  // Legalize the pointer.
06596     ISD::LoadExtType ExtType = LD->getExtensionType();
06597     const Value *SV = LD->getSrcValue();
06598     int SVOffset = LD->getSrcValueOffset();
06599     unsigned Alignment = LD->getAlignment();
06600     bool isVolatile = LD->isVolatile();
06601 
06602     if (ExtType == ISD::NON_EXTLOAD) {
06603       Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
06604                        isVolatile, Alignment);
06605       if (VT == MVT::f32 || VT == MVT::f64) {
06606         // f32->i32 or f64->i64 one to one expansion.
06607         // Remember that we legalized the chain.
06608         AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
06609         // Recursively expand the new load.
06610         if (getTypeAction(NVT) == Expand)
06611           ExpandOp(Lo, Lo, Hi);
06612         break;
06613       }
06614 
06615       // Increment the pointer to the other half.
06616       unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
06617       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
06618                         DAG.getIntPtrConstant(IncrementSize));
06619       SVOffset += IncrementSize;
06620       Alignment = MinAlign(Alignment, IncrementSize);
06621       Hi = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
06622                        isVolatile, Alignment);
06623 
06624       // Build a factor node to remember that this load is independent of the
06625       // other one.
06626       SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
06627                                  Hi.getValue(1));
06628 
06629       // Remember that we legalized the chain.
06630       AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
06631       if (TLI.isBigEndian())
06632         std::swap(Lo, Hi);
06633     } else {
06634       MVT EVT = LD->getMemoryVT();
06635 
06636       if ((VT == MVT::f64 && EVT == MVT::f32) ||
06637           (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
06638         // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
06639         SDValue Load = DAG.getLoad(EVT, dl, Ch, Ptr, SV,
06640                                      SVOffset, isVolatile, Alignment);
06641         // Remember that we legalized the chain.
06642         AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
06643         ExpandOp(DAG.getNode(ISD::FP_EXTEND, dl, VT, Load), Lo, Hi);
06644         break;
06645       }
06646     
06647       if (EVT == NVT)
06648         Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV,
06649                          SVOffset, isVolatile, Alignment);
06650       else
06651         Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, SV,
06652                             SVOffset, EVT, isVolatile,
06653                             Alignment);
06654     
06655       // Remember that we legalized the chain.
06656       AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
06657 
06658       if (ExtType == ISD::SEXTLOAD) {
06659         // The high part is obtained by SRA'ing all but one of the bits of the
06660         // lo part.
06661         unsigned LoSize = Lo.getValueType().getSizeInBits();
06662         Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
06663                          DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
06664       } else if (ExtType == ISD::ZEXTLOAD) {
06665         // The high part is just a zero.
06666         Hi = DAG.getConstant(0, NVT);
06667       } else /* if (ExtType == ISD::EXTLOAD) */ {
06668         // The high part is undefined.
06669         Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
06670       }
06671     }
06672     break;
06673   }
06674   case ISD::AND:
06675   case ISD::OR:
06676   case ISD::XOR: {   // Simple logical operators -> two trivial pieces.
06677     SDValue LL, LH, RL, RH;
06678     ExpandOp(Node->getOperand(0), LL, LH);
06679     ExpandOp(Node->getOperand(1), RL, RH);
06680     Lo = DAG.getNode(Node->getOpcode(), dl, NVT, LL, RL);
06681     Hi = DAG.getNode(Node->getOpcode(), dl, NVT, LH, RH);
06682     break;
06683   }
06684   case ISD::SELECT: {
06685     SDValue LL, LH, RL, RH;
06686     ExpandOp(Node->getOperand(1), LL, LH);
06687     ExpandOp(Node->getOperand(2), RL, RH);
06688     if (getTypeAction(NVT) == Expand)
06689       NVT = TLI.getTypeToExpandTo(NVT);
06690     Lo = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LL, RL);
06691     if (VT != MVT::f32)
06692       Hi = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LH, RH);
06693     break;
06694   }
06695   case ISD::SELECT_CC: {
06696     SDValue TL, TH, FL, FH;
06697     ExpandOp(Node->getOperand(2), TL, TH);
06698     ExpandOp(Node->getOperand(3), FL, FH);
06699     if (getTypeAction(NVT) == Expand)
06700       NVT = TLI.getTypeToExpandTo(NVT);
06701     Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
06702                      Node->getOperand(1), TL, FL, Node->getOperand(4));
06703     if (VT != MVT::f32)
06704       Hi = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
06705                        Node->getOperand(1), TH, FH, Node->getOperand(4));
06706     break;
06707   }
06708   case ISD::ANY_EXTEND:
06709     // The low part is any extension of the input (which degenerates to a copy).
06710     Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
06711     // The high part is undefined.
06712     Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
06713     break;
06714   case ISD::SIGN_EXTEND: {
06715     // The low part is just a sign extension of the input (which degenerates to
06716     // a copy).
06717     Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Node->getOperand(0));
06718 
06719     // The high part is obtained by SRA'ing all but one of the bits of the lo
06720     // part.
06721     unsigned LoSize = Lo.getValueType().getSizeInBits();
06722     Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
06723                      DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
06724     break;
06725   }
06726   case ISD::ZERO_EXTEND:
06727     // The low part is just a zero extension of the input (which degenerates to
06728     // a copy).
06729     Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
06730 
06731     // The high part is just a zero.
06732     Hi = DAG.getConstant(0, NVT);
06733     break;
06734     
06735   case ISD::TRUNCATE: {
06736     // The input value must be larger than this value.  Expand *it*.
06737     SDValue NewLo;
06738     ExpandOp(Node->getOperand(0), NewLo, Hi);
06739     
06740     // The low part is now either the right size, or it is closer.  If not the
06741     // right size, make an illegal truncate so we recursively expand it.
06742     if (NewLo.getValueType() != Node->getValueType(0))
06743       NewLo = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), NewLo);
06744     ExpandOp(NewLo, Lo, Hi);
06745     break;
06746   }
06747     
06748   case ISD::BIT_CONVERT: {
06749     SDValue Tmp;
06750     if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
06751       // If the target wants to, allow it to lower this itself.
06752       switch (getTypeAction(Node->getOperand(0).getValueType())) {
06753       case Expand: assert(0 && "cannot expand FP!");
06754       case Legal:   Tmp = LegalizeOp(Node->getOperand(0)); break;
06755       case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
06756       }
06757       Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp), DAG);
06758     }
06759 
06760     // f32 / f64 must be expanded to i32 / i64.
06761     if (VT == MVT::f32 || VT == MVT::f64) {
06762       Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
06763       if (getTypeAction(NVT) == Expand)
06764         ExpandOp(Lo, Lo, Hi);
06765       break;
06766     }
06767 
06768     // If source operand will be expanded to the same type as VT, i.e.
06769     // i64 <- f64, i32 <- f32, expand the source operand instead.
06770     MVT VT0 = Node->getOperand(0).getValueType();
06771     if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
06772       ExpandOp(Node->getOperand(0), Lo, Hi);
06773       break;
06774     }
06775 
06776     // Turn this into a load/store pair by default.
06777     if (Tmp.getNode() == 0)
06778       Tmp = EmitStackConvert(Node->getOperand(0), VT, VT, dl);
06779     
06780     ExpandOp(Tmp, Lo, Hi);
06781     break;
06782   }
06783 
06784   case ISD::READCYCLECOUNTER: {
06785     assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == 
06786                  TargetLowering::Custom &&
06787            "Must custom expand ReadCycleCounter");
06788     SDValue Tmp = TLI.LowerOperation(Op, DAG);
06789     assert(Tmp.getNode() && "Node must be custom expanded!");
06790     ExpandOp(Tmp.getValue(0), Lo, Hi);
06791     AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
06792                         LegalizeOp(Tmp.getValue(1)));
06793     break;
06794   }
06795 
06796   case ISD::ATOMIC_CMP_SWAP: {
06797     // This operation does not need a loop.
06798     SDValue Tmp = TLI.LowerOperation(Op, DAG);
06799     assert(Tmp.getNode() && "Node must be custom expanded!");
06800     ExpandOp(Tmp.getValue(0), Lo, Hi);
06801     AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
06802                         LegalizeOp(Tmp.getValue(1)));
06803     break;
06804   }
06805 
06806   case ISD::ATOMIC_LOAD_ADD:
06807   case ISD::ATOMIC_LOAD_SUB:
06808   case ISD::ATOMIC_LOAD_AND:
06809   case ISD::ATOMIC_LOAD_OR:
06810   case ISD::ATOMIC_LOAD_XOR:
06811   case ISD::ATOMIC_LOAD_NAND:
06812   case ISD::ATOMIC_SWAP: {
06813     // These operations require a loop to be generated.  We can't do that yet,
06814     // so substitute a target-dependent pseudo and expand that later.
06815     SDValue In2Lo, In2Hi, In2;
06816     ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
06817     In2 = DAG.getNode(ISD::BUILD_PAIR, dl, VT, In2Lo, In2Hi);
06818     AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
06819     SDValue Replace = 
06820       DAG.getAtomic(Op.getOpcode(), dl, Anode->getMemoryVT(),
06821                     Op.getOperand(0), Op.getOperand(1), In2,
06822                     Anode->getSrcValue(), Anode->getAlignment());
06823     SDValue Result = TLI.LowerOperation(Replace, DAG);
06824     ExpandOp(Result.getValue(0), Lo, Hi);
06825     // Remember that we legalized the chain.
06826     AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
06827     break;
06828   }
06829 
06830     // These operators cannot be expanded directly, emit them as calls to
06831     // library functions.
06832   case ISD::FP_TO_SINT: {
06833     if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
06834       SDValue Op;
06835       switch (getTypeAction(Node->getOperand(0).getValueType())) {
06836       case Expand: assert(0 && "cannot expand FP!");
06837       case Legal:   Op = LegalizeOp(Node->getOperand(0)); break;
06838       case Promote: Op = PromoteOp (Node->getOperand(0)); break;
06839       }
06840 
06841       Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op), DAG);
06842 
06843       // Now that the custom expander is done, expand the result, which is still
06844       // VT.
06845       if (Op.getNode()) {
06846         ExpandOp(Op, Lo, Hi);
06847         break;
06848       }
06849     }
06850 
06851     RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
06852                                            VT);
06853     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
06854     Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
06855     break;
06856   }
06857 
06858   case ISD::FP_TO_UINT: {
06859     if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
06860       SDValue Op;
06861       switch (getTypeAction(Node->getOperand(0).getValueType())) {
06862         case Expand: assert(0 && "cannot expand FP!");
06863         case Legal:   Op = LegalizeOp(Node->getOperand(0)); break;
06864         case Promote: Op = PromoteOp (Node->getOperand(0)); break;
06865       }
06866         
06867       Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, dl, VT, Op), DAG);
06868 
06869       // Now that the custom expander is done, expand the result.
06870       if (Op.getNode()) {
06871         ExpandOp(Op, Lo, Hi);
06872         break;
06873       }
06874     }
06875 
06876     RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
06877                                            VT);
06878     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
06879     Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
06880     break;
06881   }
06882 
06883   case ISD::SHL: {
06884     // If the target wants custom lowering, do so.
06885     SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
06886     if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
06887       SDValue Op = DAG.getNode(ISD::SHL, dl, VT, Node->getOperand(0), ShiftAmt);
06888       Op = TLI.LowerOperation(Op, DAG);
06889       if (Op.getNode()) {
06890         // Now that the custom expander is done, expand the result, which is
06891         // still VT.
06892         ExpandOp(Op, Lo, Hi);
06893         break;
06894       }
06895     }
06896     
06897     // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit 
06898     // this X << 1 as X+X.
06899     if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
06900       if (ShAmt->getAPIntValue() == 1 &&
06901           TLI.isOperationLegalOrCustom(ISD::ADDC, NVT) && 
06902           TLI.isOperationLegalOrCustom(ISD::ADDE, NVT)) {
06903         SDValue LoOps[2], HiOps[3];
06904         ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
06905         SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
06906         LoOps[1] = LoOps[0];
06907         Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
06908 
06909         HiOps[1] = HiOps[0];
06910         HiOps[2] = Lo.getValue(1);
06911         Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
06912         break;
06913       }
06914     }
06915     
06916     // If we can emit an efficient shift operation, do so now.
06917     if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
06918       break;
06919 
06920     // If this target supports SHL_PARTS, use it.
06921     TargetLowering::LegalizeAction Action =
06922       TLI.getOperationAction(ISD::SHL_PARTS, NVT);
06923     if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
06924         Action == TargetLowering::Custom) {
06925       ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), 
06926                        ShiftAmt, Lo, Hi, dl);
06927       break;
06928     }
06929 
06930     // Otherwise, emit a libcall.
06931     Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
06932     break;
06933   }
06934 
06935   case ISD::SRA: {
06936     // If the target wants custom lowering, do so.
06937     SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
06938     if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
06939       SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
06940       Op = TLI.LowerOperation(Op, DAG);
06941       if (Op.getNode()) {
06942         // Now that the custom expander is done, expand the result, which is
06943         // still VT.
06944         ExpandOp(Op, Lo, Hi);
06945         break;
06946       }
06947     }
06948     
06949     // If we can emit an efficient shift operation, do so now.
06950     if (ExpandShift(ISD::SRA,  Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
06951       break;
06952 
06953     // If this target supports SRA_PARTS, use it.
06954     TargetLowering::LegalizeAction Action =
06955       TLI.getOperationAction(ISD::SRA_PARTS, NVT);
06956     if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
06957         Action == TargetLowering::Custom) {
06958       ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), 
06959                        ShiftAmt, Lo, Hi, dl);
06960       break;
06961     }
06962 
06963     // Otherwise, emit a libcall.
06964     Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
06965     break;
06966   }
06967 
06968   case ISD::SRL: {
06969     // If the target wants custom lowering, do so.
06970     SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
06971     if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
06972       SDValue Op = DAG.getNode(ISD::SRL, dl, VT, Node->getOperand(0), ShiftAmt);
06973       Op = TLI.LowerOperation(Op, DAG);
06974       if (Op.getNode()) {
06975         // Now that the custom expander is done, expand the result, which is
06976         // still VT.
06977         ExpandOp(Op, Lo, Hi);
06978         break;
06979       }
06980     }
06981 
06982     // If we can emit an efficient shift operation, do so now.
06983     if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
06984       break;
06985 
06986     // If this target supports SRL_PARTS, use it.
06987     TargetLowering::LegalizeAction Action =
06988       TLI.getOperationAction(ISD::SRL_PARTS, NVT);
06989     if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
06990         Action == TargetLowering::Custom) {
06991       ExpandShiftParts(ISD::SRL_PARTS,
06992                        Node->getOperand(0), ShiftAmt, Lo, Hi, dl);
06993       break;
06994     }
06995 
06996     // Otherwise, emit a libcall.
06997     Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
06998     break;
06999   }
07000 
07001   case ISD::ADD:
07002   case ISD::SUB: {
07003     // If the target wants to custom expand this, let them.
07004     if (TLI.getOperationAction(Node->getOpcode(), VT) ==
07005             TargetLowering::Custom) {
07006       SDValue Result = TLI.LowerOperation(Op, DAG);
07007       if (Result.getNode()) {
07008         ExpandOp(Result, Lo, Hi);
07009         break;
07010       }
07011     }
07012     // Expand the subcomponents.
07013     SDValue LHSL, LHSH, RHSL, RHSH;
07014     ExpandOp(Node->getOperand(0), LHSL, LHSH);
07015     ExpandOp(Node->getOperand(1), RHSL, RHSH);
07016     SDValue LoOps[2], HiOps[3];
07017     LoOps[0] = LHSL;
07018     LoOps[1] = RHSL;
07019     HiOps[0] = LHSH;
07020     HiOps[1] = RHSH;
07021 
07022     //cascaded check to see if any smaller size has a a carry flag.
07023     unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
07024     bool hasCarry = false;
07025     for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
07026       MVT AVT = MVT::getIntegerVT(BitSize);
07027       if (TLI.isOperationLegalOrCustom(OpV, AVT)) {
07028         hasCarry = true;
07029         break;
07030       }
07031     }
07032 
07033     if(hasCarry) {
07034       SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
07035       if (Node->getOpcode() == ISD::ADD) {
07036         Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
07037         HiOps[2] = Lo.getValue(1);
07038         Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
07039       } else {
07040         Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
07041         HiOps[2] = Lo.getValue(1);
07042         Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
07043       }
07044       break;
07045     } else {
07046       if (Node->getOpcode() == ISD::ADD) {
07047         Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
07048         Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
07049         SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
07050                                     Lo, LoOps[0], ISD::SETULT);
07051         SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
07052                                      DAG.getConstant(1, NVT), 
07053                                      DAG.getConstant(0, NVT));
07054         SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
07055                                     Lo, LoOps[1], ISD::SETULT);
07056         SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
07057                                     DAG.getConstant(1, NVT), 
07058                                     Carry1);
07059         Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
07060       } else {
07061         Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
07062         Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
07063         SDValue Cmp = DAG.getSetCC(dl, NVT, LoOps[0], LoOps[1], ISD::SETULT);
07064         SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
07065                                      DAG.getConstant(1, NVT), 
07066                                      DAG.getConstant(0, NVT));
07067         Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
07068       }
07069       break;
07070     }
07071   }
07072     
07073   case ISD::ADDC:
07074   case ISD::SUBC: {
07075     // Expand the subcomponents.
07076     SDValue LHSL, LHSH, RHSL, RHSH;
07077     ExpandOp(Node->getOperand(0), LHSL, LHSH);
07078     ExpandOp(Node->getOperand(1), RHSL, RHSH);
07079     SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
07080     SDValue LoOps[2] = { LHSL, RHSL };
07081     SDValue HiOps[3] = { LHSH, RHSH };
07082     
07083     if (Node->getOpcode() == ISD::ADDC) {
07084       Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
07085       HiOps[2] = Lo.getValue(1);
07086       Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
07087     } else {
07088       Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
07089       HiOps[2] = Lo.getValue(1);
07090       Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
07091     }
07092     // Remember that we legalized the flag.
07093     AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
07094     break;
07095   }
07096   case ISD::ADDE:
07097   case ISD::SUBE: {
07098     // Expand the subcomponents.
07099     SDValue LHSL, LHSH, RHSL, RHSH;
07100     ExpandOp(Node->getOperand(0), LHSL, LHSH);
07101     ExpandOp(Node->getOperand(1), RHSL, RHSH);
07102     SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
07103     SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
07104     SDValue HiOps[3] = { LHSH, RHSH };
07105     
07106     Lo = DAG.getNode(Node->getOpcode(), dl, VTList, LoOps, 3);
07107     HiOps[2] = Lo.getValue(1);
07108     Hi = DAG.getNode(Node->getOpcode(), dl, VTList, HiOps, 3);
07109     
07110     // Remember that we legalized the flag.
07111     AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
07112     break;
07113   }
07114   case ISD::MUL: {
07115     // If the target wants to custom expand this, let them.
07116     if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
07117       SDValue New = TLI.LowerOperation(Op, DAG);
07118       if (New.getNode()) {
07119         ExpandOp(New, Lo, Hi);
07120         break;
07121       }
07122     }
07123     
07124     bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
07125     bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
07126     bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
07127     bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
07128     if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
07129       SDValue LL, LH, RL, RH;
07130       ExpandOp(Node->getOperand(0), LL, LH);
07131       ExpandOp(Node->getOperand(1), RL, RH);
07132       unsigned OuterBitSize = Op.getValueSizeInBits();
07133       unsigned InnerBitSize = RH.getValueSizeInBits();
07134       unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
07135       unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
07136       APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
07137       if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
07138           DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
07139         // The inputs are both zero-extended.
07140         if (HasUMUL_LOHI) {
07141           // We can emit a umul_lohi.
07142           Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
07143           Hi = SDValue(Lo.getNode(), 1);
07144           break;
07145         }
07146         if (HasMULHU) {
07147           // We can emit a mulhu+mul.
07148           Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
07149           Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
07150           break;
07151         }
07152       }
07153       if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
07154         // The input values are both sign-extended.
07155         if (HasSMUL_LOHI) {
07156           // We can emit a smul_lohi.
07157           Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
07158           Hi = SDValue(Lo.getNode(), 1);
07159           break;
07160         }
07161         if (HasMULHS) {
07162           // We can emit a mulhs+mul.
07163           Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
07164           Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
07165           break;
07166         }
07167       }
07168       if (HasUMUL_LOHI) {
07169         // Lo,Hi = umul LHS, RHS.
07170         SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
07171                                          DAG.getVTList(NVT, NVT), LL, RL);
07172         Lo = UMulLOHI;
07173         Hi = UMulLOHI.getValue(1);
07174         RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
07175         LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
07176         Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
07177         Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
07178         break;
07179       }
07180       if (HasMULHU) {
07181         Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
07182         Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
07183         RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
07184         LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
07185         Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
07186         Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
07187         break;
07188       }
07189     }
07190 
07191     // If nothing else, we can make a libcall.
07192     Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
07193     break;
07194   }
07195   case ISD::SDIV:
07196     Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
07197     break;
07198   case ISD::UDIV:
07199     Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
07200     break;
07201   case ISD::SREM:
07202     Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
07203     break;
07204   case ISD::UREM:
07205     Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
07206     break;
07207 
07208   case ISD::FADD:
07209     Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
07210                                         RTLIB::ADD_F64,
07211                                         RTLIB::ADD_F80,
07212                                         RTLIB::ADD_PPCF128),
07213                        Node, false, Hi);
07214     break;
07215   case ISD::FSUB:
07216     Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
07217                                         RTLIB::SUB_F64,
07218                                         RTLIB::SUB_F80,
07219                                         RTLIB::SUB_PPCF128),
07220                        Node, false, Hi);
07221     break;
07222   case ISD::FMUL:
07223     Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
07224                                         RTLIB::MUL_F64,
07225                                         RTLIB::MUL_F80,
07226                                         RTLIB::MUL_PPCF128),
07227                        Node, false, Hi);
07228     break;
07229   case ISD::FDIV:
07230     Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
07231                                         RTLIB::DIV_F64,
07232                                         RTLIB::DIV_F80,
07233                                         RTLIB::DIV_PPCF128),
07234                        Node, false, Hi);
07235     break;
07236   case ISD::FP_EXTEND: {
07237     if (VT == MVT::ppcf128) {
07238       assert(Node->getOperand(0).getValueType()==MVT::f32 ||
07239              Node->getOperand(0).getValueType()==MVT::f64);
07240       const uint64_t zero = 0;
07241       if (Node->getOperand(0).getValueType()==MVT::f32)
07242         Hi = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Node->getOperand(0));
07243       else
07244         Hi = Node->getOperand(0);
07245       Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
07246       break;
07247     }
07248     RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
07249     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
07250     Lo = ExpandLibCall(LC, Node, true, Hi);
07251     break;
07252   }
07253   case ISD::FP_ROUND: {
07254     RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
07255                                           VT);
07256     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
07257     Lo = ExpandLibCall(LC, Node, true, Hi);
07258     break;
07259   }
07260   case ISD::FSQRT:
07261   case ISD::FSIN:
07262   case ISD::FCOS: 
07263   case ISD::FLOG:
07264   case ISD::FLOG2:
07265   case ISD::FLOG10:
07266   case ISD::FEXP:
07267   case ISD::FEXP2:
07268   case ISD::FTRUNC:
07269   case ISD::FFLOOR:
07270   case ISD::FCEIL:
07271   case ISD::FRINT:
07272   case ISD::FNEARBYINT:
07273   case ISD::FPOW:
07274   case ISD::FPOWI: {
07275     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
07276     switch(Node->getOpcode()) {
07277     case ISD::FSQRT:
07278       LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
07279                         RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
07280       break;
07281     case ISD::FSIN:
07282       LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
07283                         RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
07284       break;
07285     case ISD::FCOS:
07286       LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
07287                         RTLIB::COS_F80, RTLIB::COS_PPCF128);
07288       break;
07289     case ISD::FLOG:
07290       LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
07291                         RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
07292       break;
07293     case ISD::FLOG2:
07294       LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
07295                         RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
07296       break;
07297     case ISD::FLOG10:
07298       LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
07299                         RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
07300       break;
07301     case ISD::FEXP:
07302       LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
07303                         RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
07304       break;
07305     case ISD::FEXP2:
07306       LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
07307                         RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
07308       break;
07309     case ISD::FTRUNC:
07310       LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
07311                         RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
07312       break;
07313     case ISD::FFLOOR:
07314       LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
07315                         RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
07316       break;
07317     case ISD::FCEIL:
07318       LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
07319                         RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
07320       break;
07321     case ISD::FRINT:
07322       LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
07323                         RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
07324       break;
07325     case ISD::FNEARBYINT:
07326       LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
07327                         RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
07328       break;
07329     case ISD::FPOW:
07330       LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
07331                         RTLIB::POW_PPCF128);
07332       break;
07333     case ISD::FPOWI:
07334       LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
07335                         RTLIB::POWI_PPCF128);
07336       break;
07337     default: assert(0 && "Unreachable!");
07338     }
07339     Lo = ExpandLibCall(LC, Node, false, Hi);
07340     break;
07341   }
07342   case ISD::FABS: {
07343     if (VT == MVT::ppcf128) {
07344       SDValue Tmp;
07345       ExpandOp(Node->getOperand(0), Lo, Tmp);
07346       Hi = DAG.getNode(ISD::FABS, dl, NVT, Tmp);
07347       // lo = hi==fabs(hi) ? lo : -lo;
07348       Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Hi, Tmp,
07349                     Lo, DAG.getNode(ISD::FNEG, dl, NVT, Lo),
07350                     DAG.getCondCode(ISD::SETEQ));
07351       break;
07352     }
07353     SDValue Mask = (VT == MVT::f64)
07354       ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
07355       : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
07356     Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
07357     Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
07358     Lo = DAG.getNode(ISD::AND, dl, NVT, Lo, Mask);
07359     if (getTypeAction(NVT) == Expand)
07360       ExpandOp(Lo, Lo, Hi);
07361     break;
07362   }
07363   case ISD::FNEG: {
07364     if (VT == MVT::ppcf128) {
07365       ExpandOp(Node->getOperand(0), Lo, Hi);
07366       Lo = DAG.getNode(ISD::FNEG, dl, MVT::f64, Lo);
07367       Hi = DAG.getNode(ISD::FNEG, dl, MVT::f64, Hi);
07368       break;
07369     }
07370     SDValue Mask = (VT == MVT::f64)
07371       ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
07372       : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
07373     Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
07374     Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
07375     Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Mask);
07376     if (getTypeAction(NVT) == Expand)
07377       ExpandOp(Lo, Lo, Hi);
07378     break;
07379   }
07380   case ISD::FCOPYSIGN: {
07381     Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
07382     if (getTypeAction(NVT) == Expand)
07383       ExpandOp(Lo, Lo, Hi);
07384     break;
07385   }
07386   case ISD::SINT_TO_FP:
07387   case ISD::UINT_TO_FP: {
07388     bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
07389     MVT SrcVT = Node->getOperand(0).getValueType();
07390 
07391     // Promote the operand if needed.  Do this before checking for
07392     // ppcf128 so conversions of i16 and i8 work.
07393     if (getTypeAction(SrcVT) == Promote) {
07394       SDValue Tmp = PromoteOp(Node->getOperand(0));
07395       Tmp = isSigned
07396         ? DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp.getValueType(), Tmp,
07397                       DAG.getValueType(SrcVT))
07398         : DAG.getZeroExtendInReg(Tmp, dl, SrcVT);
07399       Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
07400       SrcVT = Node->getOperand(0).getValueType();
07401     }
07402 
07403     if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
07404       static const uint64_t zero = 0;
07405       if (isSigned) {
07406         Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64, 
07407                                     Node->getOperand(0)));
07408         Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
07409       } else {
07410         static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
07411         Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64, 
07412                                     Node->getOperand(0)));
07413         Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
07414         Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
07415         // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
07416         ExpandOp(DAG.getNode(ISD::SELECT_CC, dl,
07417                              MVT::ppcf128, Node->getOperand(0),
07418                              DAG.getConstant(0, MVT::i32), 
07419                              DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
07420                                          DAG.getConstantFP(
07421                                             APFloat(APInt(128, 2, TwoE32)),
07422                                             MVT::ppcf128)),
07423                              Hi,
07424                              DAG.getCondCode(ISD::SETLT)),
07425                  Lo, Hi);
07426       }
07427       break;
07428     }
07429     if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
07430       // si64->ppcf128 done by libcall, below
07431       static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
07432       ExpandOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::ppcf128,
07433                Node->getOperand(0)), Lo, Hi);
07434       Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
07435       // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
07436       ExpandOp(DAG.getNode(ISD::SELECT_CC, dl, MVT::ppcf128,
07437                            Node->getOperand(0),
07438                            DAG.getConstant(0, MVT::i64), 
07439                            DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
07440                                        DAG.getConstantFP(
07441                                           APFloat(APInt(128, 2, TwoE64)),
07442                                           MVT::ppcf128)),
07443                            Hi,
07444                            DAG.getCondCode(ISD::SETLT)),
07445                Lo, Hi);
07446       break;
07447     }
07448 
07449     Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
07450                        Node->getOperand(0), dl);
07451     if (getTypeAction(Lo.getValueType()) == Expand)
07452       // float to i32 etc. can be 'expanded' to a single node.
07453       ExpandOp(Lo, Lo, Hi);
07454     break;
07455   }
07456   }
07457 
07458   // Make sure the resultant values have been legalized themselves, unless this
07459   // is a type that requires multi-step expansion.
07460   if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
07461     Lo = LegalizeOp(Lo);
07462     if (Hi.getNode())
07463       // Don't legalize the high part if it is expanded to a single node.
07464       Hi = LegalizeOp(Hi);
07465   }
07466 
07467   // Remember in a map if the values will be reused later.
07468   bool isNew =
07469     ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
07470   assert(isNew && "Value already expanded?!?");
07471   isNew = isNew;
07472 }
07473 
07474 /// SplitVectorOp - Given an operand of vector type, break it down into
07475 /// two smaller values, still of vector type.
07476 void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
07477                                          SDValue &Hi) {
07478   assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
07479   SDNode *Node = Op.getNode();
07480   DebugLoc dl = Node->getDebugLoc();
07481   unsigned NumElements = Op.getValueType().getVectorNumElements();
07482   assert(NumElements > 1 && "Cannot split a single element vector!");
07483 
07484   MVT NewEltVT = Op.getValueType().getVectorElementType();
07485 
07486   unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
07487   unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
07488 
07489   MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
07490   MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
07491 
07492   // See if we already split it.
07493   std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
07494     = SplitNodes.find(Op);
07495   if (I != SplitNodes.end()) {
07496     Lo = I->second.first;
07497     Hi = I->second.second;
07498     return;
07499   }
07500   
07501   switch (Node->getOpcode()) {
07502   default: 
07503 #ifndef NDEBUG
07504     Node->dump(&DAG);
07505 #endif
07506     assert(0 && "Unhandled operation in SplitVectorOp!");
07507   case ISD::UNDEF:
07508     Lo = DAG.getNode(ISD::UNDEF, dl, NewVT_Lo);
07509     Hi = DAG.getNode(ISD::UNDEF, dl, NewVT_Hi);
07510     break;
07511   case ISD::BUILD_PAIR:
07512     Lo = Node->getOperand(0);
07513     Hi = Node->getOperand(1);
07514     break;
07515   case ISD::INSERT_VECTOR_ELT: {
07516     if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
07517       SplitVectorOp(Node->getOperand(0), Lo, Hi);
07518       unsigned Index = Idx->getZExtValue();
07519       SDValue ScalarOp = Node->getOperand(1);
07520       if (Index < NewNumElts_Lo)
07521         Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Lo, Lo, ScalarOp,
07522                          DAG.getIntPtrConstant(Index));
07523       else
07524         Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Hi, Hi, ScalarOp,
07525                          DAG.getIntPtrConstant(Index - NewNumElts_Lo));
07526       break;
07527     }
07528     SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
07529                                                    Node->getOperand(1),
07530                                                    Node->getOperand(2), dl);
07531     SplitVectorOp(Tmp, Lo, Hi);
07532     break;
07533   }
07534   case ISD::VECTOR_SHUFFLE: {
07535     // Build the low part.
07536     SDValue Mask = Node->getOperand(2);
07537     SmallVector<SDValue, 8> Ops;
07538     MVT PtrVT = TLI.getPointerTy();
07539     
07540     // Insert all of the elements from the input that are needed.  We use 
07541     // buildvector of extractelement here because the input vectors will have
07542     // to be legalized, so this makes the code simpler.
07543     for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
07544       SDValue IdxNode = Mask.getOperand(i);
07545       if (IdxNode.getOpcode() == ISD::UNDEF) {
07546         Ops.push_back(DAG.getNode(ISD::UNDEF, dl, NewEltVT));
07547         continue;
07548       }
07549       unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
07550       SDValue InVec = Node->getOperand(0);
07551       if (Idx >= NumElements) {
07552         InVec = Node->getOperand(1);
07553         Idx -= NumElements;
07554       }
07555       Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
07556                                 DAG.getConstant(Idx, PtrVT)));
07557     }
07558     Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &Ops[0], Ops.size());
07559     Ops.clear();
07560     
07561     for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
07562       SDValue IdxNode = Mask.getOperand(i);
07563       if (IdxNode.getOpcode() == ISD::UNDEF) {
07564         Ops.push_back(DAG.getNode(ISD::UNDEF, dl, NewEltVT));
07565         continue;
07566       }
07567       unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
07568       SDValue InVec = Node->getOperand(0);
07569       if (Idx >= NumElements) {
07570         InVec = Node->getOperand(1);
07571         Idx -= NumElements;
07572       }
07573       Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
07574                                 DAG.getConstant(Idx, PtrVT)));
07575     }
07576     Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &Ops[0], Ops.size());
07577     break;
07578   }
07579   case ISD::BUILD_VECTOR: {
07580     SmallVector<SDValue, 8> LoOps(Node->op_begin(), 
07581                                     Node->op_begin()+NewNumElts_Lo);
07582     Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &LoOps[0], LoOps.size());
07583 
07584     SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo, 
07585                                     Node->op_end());
07586     Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &HiOps[0], HiOps.size());
07587     break;
07588   }
07589   case ISD::CONCAT_VECTORS: {
07590     // FIXME: Handle non-power-of-two vectors?
07591     unsigned NewNumSubvectors = Node->getNumOperands() / 2;
07592     if (NewNumSubvectors == 1) {
07593       Lo = Node->getOperand(0);
07594       Hi = Node->getOperand(1);
07595     } else {
07596       SmallVector<SDValue, 8> LoOps(Node->op_begin(),
07597                                     Node->op_begin()+NewNumSubvectors);
07598       Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Lo, 
07599                        &LoOps[0], LoOps.size());
07600 
07601       SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
07602                                       Node->op_end());
07603       Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Hi, 
07604                        &HiOps[0], HiOps.size());
07605     }
07606     break;
07607   }
07608   case ISD::EXTRACT_SUBVECTOR: {
07609     SDValue Vec = Op.getOperand(0);
07610     SDValue Idx = Op.getOperand(1);
07611     MVT     IdxVT = Idx.getValueType();
07612 
07613     Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Lo, Vec, Idx);
07614     ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
07615     if (CIdx) {
07616       Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec, 
07617                        DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
07618                                        IdxVT));
07619     } else {
07620       Idx = DAG.getNode(ISD::ADD, dl, IdxVT, Idx,
07621                         DAG.getConstant(NewNumElts_Lo, IdxVT));
07622       Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec, Idx);
07623     }
07624     break;
07625   }
07626   case ISD::SELECT: {
07627     SDValue Cond = Node->getOperand(0);
07628 
07629     SDValue LL, LH, RL, RH;
07630     SplitVectorOp(Node->getOperand(1), LL, LH);
07631     SplitVectorOp(Node->getOperand(2), RL, RH);
07632 
07633     if (Cond.getValueType().isVector()) {
07634       // Handle a vector merge.
07635       SDValue CL, CH;
07636       SplitVectorOp(Cond, CL, CH);
07637       Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, CL, LL, RL);
07638       Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, CH, LH, RH);
07639     } else {
07640       // Handle a simple select with vector operands.
07641       Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, Cond, LL, RL);
07642       Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, Cond, LH, RH);
07643     }
07644     break;
07645   }
07646   case ISD::SELECT_CC: {
07647     SDValue CondLHS = Node->getOperand(0);
07648     SDValue CondRHS = Node->getOperand(1);
07649     SDValue CondCode = Node->getOperand(4);
07650     
07651     SDValue LL, LH, RL, RH;
07652     SplitVectorOp(Node->getOperand(2), LL, LH);
07653     SplitVectorOp(Node->getOperand(3), RL, RH);
07654     
07655     // Handle a simple select with vector operands.
07656     Lo = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Lo, CondLHS, CondRHS,
07657                      LL, RL, CondCode);
07658     Hi = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Hi, CondLHS, CondRHS, 
07659                      LH, RH, CondCode);
07660     break;
07661   }
07662   case ISD::VSETCC: {
07663     SDValue LL, LH, RL, RH;
07664     SplitVectorOp(Node->getOperand(0), LL, LH);
07665     SplitVectorOp(Node->getOperand(1), RL, RH);
07666     Lo = DAG.getNode(ISD::VSETCC, dl, NewVT_Lo, LL, RL, Node->getOperand(2));
07667     Hi = DAG.getNode(ISD::VSETCC, dl, NewVT_Hi, LH, RH, Node->getOperand(2));
07668     break;
07669   }
07670   case ISD::ADD:
07671   case ISD::SUB:
07672   case ISD::MUL:
07673   case ISD::FADD:
07674   case ISD::FSUB:
07675   case ISD::FMUL:
07676   case ISD::SDIV:
07677   case ISD::UDIV:
07678   case ISD::FDIV:
07679   case ISD::FPOW:
07680   case ISD::AND:
07681   case ISD::OR:
07682   case ISD::XOR:
07683   case ISD::UREM:
07684   case ISD::SREM:
07685   case ISD::FREM:
07686   case ISD::SHL:
07687   case ISD::SRA:
07688   case ISD::SRL: {
07689     SDValue LL, LH, RL, RH;
07690     SplitVectorOp(Node->getOperand(0), LL, LH);
07691     SplitVectorOp(Node->getOperand(1), RL, RH);
07692     
07693     Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, LL, RL);
07694     Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, LH, RH);
07695     break;
07696   }
07697   case ISD::FP_ROUND:
07698   case ISD::FPOWI: {
07699     SDValue L, H;
07700     SplitVectorOp(Node->getOperand(0), L, H);
07701 
07702     Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L, Node->getOperand(1));
07703     Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H, Node->getOperand(1));
07704     break;
07705   }
07706   case ISD::CTTZ:
07707   case ISD::CTLZ:
07708   case ISD::CTPOP:
07709   case ISD::FNEG:
07710   case ISD::FABS:
07711   case ISD::FSQRT:
07712   case ISD::FSIN:
07713   case ISD::FCOS:
07714   case ISD::FLOG:
07715   case ISD::FLOG2:
07716   case ISD::FLOG10:
07717   case ISD::FEXP:
07718   case ISD::FEXP2:
07719   case ISD::FP_TO_SINT:
07720   case ISD::FP_TO_UINT:
07721   case ISD::SINT_TO_FP:
07722   case ISD::UINT_TO_FP:
07723   case ISD::TRUNCATE:
07724   case ISD::ANY_EXTEND:
07725   case ISD::SIGN_EXTEND:
07726   case ISD::ZERO_EXTEND:
07727   case ISD::FP_EXTEND: {
07728     SDValue L, H;
07729     SplitVectorOp(Node->getOperand(0), L, H);
07730 
07731     Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L);
07732     Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H);
07733     break;
07734   }
07735   case ISD::CONVERT_RNDSAT: {
07736     ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
07737     SDValue L, H;
07738     SplitVectorOp(Node->getOperand(0), L, H);
07739     SDValue DTyOpL =  DAG.getValueType(NewVT_Lo);
07740     SDValue DTyOpH =  DAG.getValueType(NewVT_Hi);
07741     SDValue STyOpL =  DAG.getValueType(L.getValueType());
07742     SDValue STyOpH =  DAG.getValueType(H.getValueType());
07743 
07744     SDValue RndOp = Node->getOperand(3);
07745     SDValue SatOp = Node->getOperand(4);
07746 
07747     Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
07748                               RndOp, SatOp, CvtCode);
07749     Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
07750                               RndOp, SatOp, CvtCode);
07751     break;
07752   }
07753   case ISD::LOAD: {
07754     LoadSDNode *LD = cast<LoadSDNode>(Node);
07755     SDValue Ch = LD->getChain();
07756     SDValue Ptr = LD->getBasePtr();
07757     ISD::LoadExtType ExtType = LD->getExtensionType();
07758     const Value *SV = LD->getSrcValue();
07759     int SVOffset = LD->getSrcValueOffset();
07760     MVT MemoryVT = LD->getMemoryVT();
07761     unsigned Alignment = LD->getAlignment();
07762     bool isVolatile = LD->isVolatile();
07763 
07764     assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
07765     SDValue Offset = DAG.getNode(ISD::UNDEF, dl, Ptr.getValueType());
07766 
07767     MVT MemNewEltVT = MemoryVT.getVectorElementType();
07768     MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
07769     MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
07770 
07771     Lo = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
07772                      NewVT_Lo, Ch, Ptr, Offset,
07773                      SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
07774     unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
07775     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
07776                       DAG.getIntPtrConstant(IncrementSize));
07777     SVOffset += IncrementSize;
07778     Alignment = MinAlign(Alignment, IncrementSize);
07779     Hi = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
07780                      NewVT_Hi, Ch, Ptr, Offset,
07781                      SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
07782     
07783     // Build a factor node to remember that this load is independent of the
07784     // other one.
07785     SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
07786                                Hi.getValue(1));
07787     
07788     // Remember that we legalized the chain.
07789     AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
07790     break;
07791   }
07792   case ISD::BIT_CONVERT: {
07793     // We know the result is a vector.  The input may be either a vector or a
07794     // scalar value.
07795     SDValue InOp = Node->getOperand(0);
07796     if (!InOp.getValueType().isVector() ||
07797         InOp.getValueType().getVectorNumElements() == 1) {
07798       // The input is a scalar or single-element vector.
07799       // Lower to a store/load so that it can be split.
07800       // FIXME: this could be improved probably.
07801       unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
07802                                             Op.getValueType().getTypeForMVT());
07803       SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
07804       int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
07805 
07806       SDValue St = DAG.getStore(DAG.getEntryNode(), dl,
07807                                   InOp, Ptr,
07808                                   PseudoSourceValue::getFixedStack(FI), 0);
07809       InOp = DAG.getLoad(Op.getValueType(), dl, St, Ptr,
07810                          PseudoSourceValue::getFixedStack(FI), 0);
07811     }
07812     // Split the vector and convert each of the pieces now.
07813     SplitVectorOp(InOp, Lo, Hi);
07814     Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Lo, Lo);
07815     Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Hi, Hi);
07816     break;
07817   }
07818   }
07819       
07820   // Remember in a map if the values will be reused later.
07821   bool isNew = 
07822     SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
07823   assert(isNew && "Value already split?!?");
07824   isNew = isNew;
07825 }
07826 
07827 
07828 /// ScalarizeVectorOp - Given an operand of single-element vector type
07829 /// (e.g. v1f32), convert it into the equivalent operation that returns a
07830 /// scalar (e.g. f32) value.
07831 SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
07832   assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
07833   SDNode *Node = Op.getNode();
07834   DebugLoc dl = Node->getDebugLoc();
07835   MVT NewVT = Op.getValueType().getVectorElementType();
07836   assert(Op.getValueType().getVectorNumElements() == 1);
07837   
07838   // See if we already scalarized it.
07839   std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
07840   if (I != ScalarizedNodes.end()) return I->second;
07841   
07842   SDValue Result;
07843   switch (Node->getOpcode()) {
07844   default: 
07845 #ifndef NDEBUG
07846     Node->dump(&DAG); cerr << "\n";
07847 #endif
07848     assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
07849   case ISD::ADD:
07850   case ISD::FADD:
07851   case ISD::SUB:
07852   case ISD::FSUB:
07853   case ISD::MUL:
07854   case ISD::FMUL:
07855   case ISD::SDIV:
07856   case ISD::UDIV:
07857   case ISD::FDIV:
07858   case ISD::SREM:
07859   case ISD::UREM:
07860   case ISD::FREM:
07861   case ISD::FPOW:
07862   case ISD::AND:
07863   case ISD::OR:
07864   case ISD::XOR:
07865     Result = DAG.getNode(Node->getOpcode(), dl,
07866                          NewVT, 
07867                          ScalarizeVectorOp(Node->getOperand(0)),
07868                          ScalarizeVectorOp(Node->getOperand(1)));
07869     break;
07870   case ISD::FNEG:
07871   case ISD::FABS:
07872   case ISD::FSQRT:
07873   case ISD::FSIN:
07874   case ISD::FCOS:
07875   case ISD::FLOG:
07876   case ISD::FLOG2:
07877   case ISD::FLOG10:
07878   case ISD::FEXP:
07879   case ISD::FEXP2:
07880   case ISD::FP_TO_SINT:
07881   case ISD::FP_TO_UINT:
07882   case ISD::SINT_TO_FP:
07883   case ISD::UINT_TO_FP:
07884   case ISD::SIGN_EXTEND:
07885   case ISD::ZERO_EXTEND:
07886   case ISD::ANY_EXTEND:
07887   case ISD::TRUNCATE:
07888   case ISD::FP_EXTEND:
07889     Result = DAG.getNode(Node->getOpcode(), dl,
07890                          NewVT, 
07891                          ScalarizeVectorOp(Node->getOperand(0)));
07892     break;
07893   case ISD::CONVERT_RNDSAT: {
07894     SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
07895     Result = DAG.getConvertRndSat(NewVT, Op0,
07896                                   DAG.getValueType(NewVT),
07897                                   DAG.getValueType(Op0.getValueType()),
07898                                   Node->getOperand(3),
07899                                   Node->getOperand(4),
07900                                   cast<CvtRndSatSDNode>(Node)->getCvtCode());
07901     break;
07902   }
07903   case ISD::FPOWI:
07904   case ISD::FP_ROUND:
07905     Result = DAG.getNode(Node->getOpcode(), dl,
07906                          NewVT, 
07907                          ScalarizeVectorOp(Node->getOperand(0)),
07908                          Node->getOperand(1));
07909     break;
07910   case ISD::LOAD: {
07911     LoadSDNode *LD = cast<LoadSDNode>(Node);
07912     SDValue Ch = LegalizeOp(LD->getChain());     // Legalize the chain.
07913     SDValue Ptr = LegalizeOp(LD->getBasePtr());  // Legalize the pointer.
07914     ISD::LoadExtType ExtType = LD->getExtensionType();
07915     const Value *SV = LD->getSrcValue();
07916     int SVOffset = LD->getSrcValueOffset();
07917     MVT MemoryVT = LD->getMemoryVT();
07918     unsigned Alignment = LD->getAlignment();
07919     bool isVolatile = LD->isVolatile();
07920 
07921     assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
07922     SDValue Offset = DAG.getNode(ISD::UNDEF, dl, Ptr.getValueType());
07923     
07924     Result = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
07925                          NewVT, Ch, Ptr, Offset, SV, SVOffset,
07926                          MemoryVT.getVectorElementType(),
07927                          isVolatile, Alignment);
07928 
07929     // Remember that we legalized the chain.
07930     AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
07931     break;
07932   }
07933   case ISD::BUILD_VECTOR:
07934     Result = Node->getOperand(0);
07935     break;
07936   case ISD::INSERT_VECTOR_ELT:
07937     // Returning the inserted scalar element.
07938     Result = Node->getOperand(1);
07939     break;
07940   case ISD::CONCAT_VECTORS:
07941     assert(Node->getOperand(0).getValueType() == NewVT &&
07942            "Concat of non-legal vectors not yet supported!");
07943     Result = Node->getOperand(0);
07944     break;
07945   case ISD::VECTOR_SHUFFLE: {
07946     // Figure out if the scalar is the LHS or RHS and return it.
07947     SDValue EltNum = Node->getOperand(2).getOperand(0);
07948     if (cast<ConstantSDNode>(EltNum)->getZExtValue())
07949       Result = ScalarizeVectorOp(Node->getOperand(1));
07950     else
07951       Result = ScalarizeVectorOp(Node->getOperand(0));
07952     break;
07953   }
07954   case ISD::EXTRACT_SUBVECTOR:
07955     Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, 
07956                          Node->getOperand(0), Node->getOperand(1));
07957     break;
07958   case ISD::BIT_CONVERT: {
07959     SDValue Op0 = Op.getOperand(0);
07960     if (Op0.getValueType().getVectorNumElements() == 1)
07961       Op0 = ScalarizeVectorOp(Op0);
07962     Result = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, Op0);
07963     break;
07964   }
07965   case ISD::SELECT:
07966     Result = DAG.getNode(ISD::SELECT, dl, NewVT, Op.getOperand(0),
07967                          ScalarizeVectorOp(Op.getOperand(1)),
07968                          ScalarizeVectorOp(Op.getOperand(2)));
07969     break;
07970   case ISD::SELECT_CC:
07971     Result = DAG.getNode(ISD::SELECT_CC, dl, NewVT, Node->getOperand(0), 
07972                          Node->getOperand(1),
07973                          ScalarizeVectorOp(Op.getOperand(2)),
07974                          ScalarizeVectorOp(Op.getOperand(3)),
07975                          Node->getOperand(4));
07976     break;
07977   case ISD::VSETCC: {
07978     SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
07979     SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
07980     Result = DAG.getNode(ISD::SETCC, dl,
07981                          TLI.getSetCCResultType(Op0.getValueType()),
07982                          Op0, Op1, Op.getOperand(2));
07983     Result = DAG.getNode(ISD::SELECT, dl, NewVT, Result,
07984                          DAG.getConstant(-1ULL, NewVT),
07985                          DAG.getConstant(0ULL, NewVT));
07986     break;
07987   }
07988   }
07989 
07990   if (TLI.isTypeLegal(NewVT))
07991     Result = LegalizeOp(Result);
07992   bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
07993   assert(isNew && "Value already scalarized?");
07994   isNew = isNew;
07995   return Result;
07996 }
07997 
07998 
07999 SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
08000   std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
08001   if (I != WidenNodes.end()) return I->second;
08002   
08003   MVT VT = Op.getValueType();
08004   assert(VT.isVector() && "Cannot widen non-vector type!");
08005 
08006   SDValue Result;
08007   SDNode *Node = Op.getNode();
08008   DebugLoc dl = Node->getDebugLoc();
08009   MVT EVT = VT.getVectorElementType();
08010 
08011   unsigned NumElts = VT.getVectorNumElements();
08012   unsigned NewNumElts = WidenVT.getVectorNumElements();
08013   assert(NewNumElts > NumElts  && "Cannot widen to smaller type!");
08014   assert(NewNumElts < 17);
08015 
08016   // When widen is called, it is assumed that it is more efficient to use a
08017   // wide type.  The default action is to widen to operation to a wider legal
08018   // vector type and then do the operation if it is legal by calling LegalizeOp
08019   // again.  If there is no vector equivalent, we will unroll the operation, do
08020   // it, and rebuild the vector.  If most of the operations are vectorizible to
08021   // the legal type, the resulting code will be more efficient.  If this is not
08022   // the case, the resulting code will preform badly as we end up generating
08023   // code to pack/unpack the results. It is the function that calls widen
08024   // that is responsible for seeing this doesn't happen.
08025   switch (Node->getOpcode()) {
08026   default: 
08027 #ifndef NDEBUG
08028       Node->dump(&DAG);
08029 #endif
08030       assert(0 && "Unexpected operation in WidenVectorOp!");
08031       break;
08032   case ISD::CopyFromReg:
08033     assert(0 && "CopyFromReg doesn't need widening!");
08034   case ISD::Constant:
08035   case ISD::ConstantFP:
08036     // To build a vector of these elements, clients should call BuildVector
08037     // and with each element instead of creating a node with a vector type
08038     assert(0 && "Unexpected operation in WidenVectorOp!");
08039   case ISD::VAARG:
08040     // Variable Arguments with vector types doesn't make any sense to me
08041     assert(0 && "Unexpected operation in WidenVectorOp!");
08042     break;
08043   case ISD::UNDEF:
08044     Result = DAG.getNode(ISD::UNDEF, dl, WidenVT);
08045     break;
08046   case ISD::BUILD_VECTOR: {
08047     // Build a vector with undefined for the new nodes
08048     SDValueVector NewOps(Node->op_begin(), Node->op_end());
08049     for (unsigned i = NumElts; i < NewNumElts; ++i) {
08050       NewOps.push_back(DAG.getNode(ISD::UNDEF, dl, EVT));
08051     }
08052     Result = DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, 
08053                          &NewOps[0], NewOps.size());    
08054     break;
08055   }
08056   case ISD::INSERT_VECTOR_ELT: {
08057     SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
08058     Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, WidenVT, Tmp1,
08059                          Node->getOperand(1), Node->getOperand(2));
08060     break;
08061   }
08062   case ISD::VECTOR_SHUFFLE: {
08063     SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
08064     SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
08065     // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
08066     // used as permutation array. We build the vector here instead of widening
08067     // because we don't want to legalize and have it turned to something else.
08068     SDValue PermOp = Node->getOperand(2);
08069     SDValueVector NewOps;
08070     MVT PVT = PermOp.getValueType().getVectorElementType();
08071     for (unsigned i = 0; i < NumElts; ++i) {
08072       if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
08073         NewOps.push_back(PermOp.getOperand(i));
08074       } else {
08075         unsigned Idx =
08076           cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
08077         if (Idx < NumElts) {
08078           NewOps.push_back(PermOp.getOperand(i));
08079         }
08080         else {
08081           NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
08082                                            PermOp.getOperand(i).getValueType()));
08083         } 
08084       }
08085     }
08086     for (unsigned i = NumElts; i < NewNumElts; ++i) {
08087       NewOps.push_back(DAG.getNode(ISD::UNDEF, dl, PVT));
08088     }
08089     
08090     SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR, dl, 
08091                                MVT::getVectorVT(PVT, NewOps.size()),
08092                                &NewOps[0], NewOps.size()); 
08093     
08094     Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, WidenVT, Tmp1, Tmp2, Tmp3);
08095     break;
08096   }
08097   case ISD::LOAD: {
08098     // If the load widen returns true, we can use a single load for the
08099     // vector.  Otherwise, it is returning a token factor for multiple
08100     // loads.
08101     SDValue TFOp;
08102     if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
08103       AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
08104     else
08105       AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
08106     break;
08107   }
08108 
08109   case ISD::BIT_CONVERT: {
08110     SDValue Tmp1 = Node->getOperand(0);
08111     // Converts between two different types so we need to determine
08112     // the correct widen type for the input operand.
08113     MVT InVT = Tmp1.getValueType();
08114     unsigned WidenSize = WidenVT.getSizeInBits();    
08115     if (InVT.isVector()) {
08116       MVT InEltVT = InVT.getVectorElementType();
08117       unsigned InEltSize = InEltVT.getSizeInBits();
08118       assert(WidenSize % InEltSize == 0 &&
08119              "can not widen bit convert that are not multiple of element type");
08120       MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
08121       Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
08122       assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
08123       Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Tmp1);
08124     } else {
08125       // If the result size is a multiple of the input size, widen the input
08126       // and then convert.
08127       unsigned InSize = InVT.getSizeInBits();
08128       assert(WidenSize % InSize == 0 &&
08129              "can not widen bit convert that are not multiple of element type");
08130       unsigned NewNumElts = WidenSize / InSize;
08131       SmallVector<SDValue, 16> Ops(NewNumElts);
08132       SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, InVT);
08133       Ops[0] = Tmp1;
08134       for (unsigned i = 1; i < NewNumElts; ++i)
08135         Ops[i] = UndefVal;
08136 
08137       MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
08138       Result = DAG.getNode(ISD::BUILD_VECTOR, dl, NewInVT, &Ops[0], NewNumElts);
08139       Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Result);
08140     }
08141     break;
08142   }
08143 
08144   case ISD::SINT_TO_FP:
08145   case ISD::UINT_TO_FP:
08146   case ISD::FP_TO_SINT:
08147   case ISD::FP_TO_UINT:
08148   case ISD::FP_ROUND: {
08149     SDValue Tmp1 = Node->getOperand(0);
08150     // Converts between two different types so we need to determine
08151     // the correct widen type for the input operand.
08152     MVT TVT = Tmp1.getValueType();
08153     assert(TVT.isVector() && "can not widen non vector type");
08154     MVT TEVT = TVT.getVectorElementType();
08155     MVT TWidenVT =  MVT::getVectorVT(TEVT, NewNumElts);
08156     Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
08157     assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
08158     Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
08159     break;
08160   }
08161 
08162   case ISD::FP_EXTEND:
08163     assert(0 && "Case not implemented.  Dynamically dead with 2 FP types!");
08164   case ISD::TRUNCATE:
08165   case ISD::SIGN_EXTEND:
08166   case ISD::ZERO_EXTEND:
08167   case ISD::ANY_EXTEND:
08168   case ISD::SIGN_EXTEND_INREG:
08169   case ISD::FABS:
08170   case ISD::FNEG:
08171   case ISD::FSQRT:
08172   case ISD::FSIN:
08173   case ISD::FCOS:
08174   case ISD::CTPOP:
08175   case ISD::CTTZ:
08176   case ISD::CTLZ: {
08177     // Unary op widening
08178     SDValue Tmp1;
08179     Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
08180     assert(Tmp1.getValueType() == WidenVT);
08181     Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
08182     break;
08183   }
08184   case ISD::CONVERT_RNDSAT: {
08185     SDValue RndOp = Node->getOperand(3);
08186     SDValue SatOp = Node->getOperand(4);
08187     SDValue SrcOp = Node->getOperand(0);
08188 
08189     // Converts between two different types so we need to determine
08190     // the correct widen type for the input operand.
08191     MVT SVT = SrcOp.getValueType();
08192     assert(SVT.isVector() && "can not widen non vector type");
08193     MVT SEVT = SVT.getVectorElementType();
08194     MVT SWidenVT =  MVT::getVectorVT(SEVT, NewNumElts);
08195 
08196     SrcOp = WidenVectorOp(SrcOp, SWidenVT);
08197     assert(SrcOp.getValueType() == WidenVT);
08198     SDValue DTyOp = DAG.getValueType(WidenVT);
08199     SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
08200     ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
08201 
08202     Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
08203                                   RndOp, SatOp, CvtCode);
08204     break;
08205   }
08206   case ISD::FPOW:
08207   case ISD::FPOWI: 
08208   case ISD::ADD:
08209   case ISD::SUB:
08210   case ISD::MUL:
08211   case ISD::MULHS:
08212   case ISD::MULHU:
08213   case ISD::AND:
08214   case ISD::OR:
08215   case ISD::XOR:
08216   case ISD::FADD:
08217   case ISD::FSUB:
08218   case ISD::FMUL:
08219   case ISD::SDIV:
08220   case ISD::SREM:
08221   case ISD::FDIV:
08222   case ISD::FREM:
08223   case ISD::FCOPYSIGN:
08224   case ISD::UDIV:
08225   case ISD::UREM:
08226   case ISD::BSWAP: {
08227     // Binary op widening
08228     SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
08229     SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
08230     assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
08231     Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2);
08232     break;
08233   }
08234 
08235   case ISD::SHL:
08236   case ISD::SRA:
08237   case ISD::SRL: {
08238     SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
08239     assert(Tmp1.getValueType() == WidenVT);
08240     SDValue ShOp = Node->getOperand(1);
08241     MVT ShVT = ShOp.getValueType();
08242     MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
08243                                    WidenVT.getVectorNumElements());
08244     ShOp = WidenVectorOp(ShOp, NewShVT);
08245     assert(ShOp.getValueType() == NewShVT);
08246     Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, ShOp);
08247     break;
08248   }
08249 
08250   case ISD::EXTRACT_VECTOR_ELT: {
08251     SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
08252     assert(Tmp1.getValueType() == WidenVT);
08253     Result = DAG.getNode(Node->getOpcode(), dl, EVT, Tmp1, Node->getOperand(1));
08254     break;
08255   }
08256   case ISD::CONCAT_VECTORS: {
08257     // We concurrently support only widen on a multiple of the incoming vector.
08258     // We could widen on a multiple of the incoming operand if necessary.
08259     unsigned NumConcat = NewNumElts / NumElts;
08260     assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
08261     SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, VT);
08262     SmallVector<SDValue, 8> MOps;
08263     MOps.push_back(Op);
08264     for (unsigned i = 1; i != NumConcat; ++i) {
08265       MOps.push_back(UndefVal);
08266     }
08267     Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
08268                                     &MOps[0], MOps.size()));
08269     break;
08270   }
08271   case ISD::EXTRACT_SUBVECTOR: {
08272     SDValue Tmp1 = Node->getOperand(0);
08273     SDValue Idx = Node->getOperand(1);
08274     ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
08275     if (CIdx && CIdx->getZExtValue() == 0) {
08276       // Since we are access the start of the vector, the incoming
08277       // vector type might be the proper.
08278       MVT Tmp1VT = Tmp1.getValueType();
08279       if (Tmp1VT == WidenVT)
08280         return Tmp1;
08281       else {
08282         unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
08283         if (Tmp1VTNumElts < NewNumElts)
08284           Result = WidenVectorOp(Tmp1, WidenVT);
08285         else
08286           Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, Tmp1, Idx);
08287       }
08288     } else if (NewNumElts % NumElts == 0) {
08289       // Widen the extracted subvector.
08290       unsigned NumConcat = NewNumElts / NumElts;
08291       SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, VT);
08292       SmallVector<SDValue, 8> MOps;
08293       MOps.push_back(Op);
08294       for (unsigned i = 1; i != NumConcat; ++i) {
08295         MOps.push_back(UndefVal);
08296       }
08297       Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
08298                                       &MOps[0], MOps.size()));
08299     } else {
08300       assert(0 && "can not widen extract subvector");
08301      // This could be implemented using insert and build vector but I would
08302      // like to see when this happens.
08303     }
08304     break;
08305   }
08306 
08307   case ISD::SELECT: {
08308     // Determine new condition widen type and widen
08309     SDValue Cond1 = Node->getOperand(0);
08310     MVT CondVT = Cond1.getValueType();
08311     assert(CondVT.isVector() && "can not widen non vector type");
08312     MVT CondEVT = CondVT.getVectorElementType();
08313     MVT CondWidenVT =  MVT::getVectorVT(CondEVT, NewNumElts);
08314     Cond1 = WidenVectorOp(Cond1, CondWidenVT);
08315     assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
08316 
08317     SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
08318     SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
08319     assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
08320     Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Tmp1, Tmp2);
08321     break;
08322   }
08323   
08324   case ISD::SELECT_CC: {
08325     // Determine new condition widen type and widen
08326     SDValue Cond1 = Node->getOperand(0);
08327     SDValue Cond2 = Node->getOperand(1);
08328     MVT CondVT = Cond1.getValueType();
08329     assert(CondVT.isVector() && "can not widen non vector type");
08330     assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
08331     MVT CondEVT = CondVT.getVectorElementType();
08332     MVT CondWidenVT =  MVT::getVectorVT(CondEVT, NewNumElts);
08333     Cond1 = WidenVectorOp(Cond1, CondWidenVT);
08334     Cond2 = WidenVectorOp(Cond2, CondWidenVT);
08335     assert(Cond1.getValueType() == CondWidenVT &&
08336            Cond2.getValueType() == CondWidenVT && "condition not widen");
08337 
08338     SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
08339     SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
08340     assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
08341            "operands not widen");
08342     Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Cond2, Tmp1,
08343                          Tmp2, Node->getOperand(4));
08344     break;
08345   }
08346   case ISD::VSETCC: {
08347     // Determine widen for the operand
08348     SDValue Tmp1 = Node->getOperand(0);
08349     MVT TmpVT = Tmp1.getValueType();
08350     assert(TmpVT.isVector() && "can not widen non vector type");
08351     MVT TmpEVT = TmpVT.getVectorElementType();
08352     MVT TmpWidenVT =  MVT::getVectorVT(TmpEVT, NewNumElts);
08353     Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
08354     SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
08355     Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2,
08356                          Node->getOperand(2));
08357     break;
08358   }
08359   case ISD::ATOMIC_CMP_SWAP:
08360   case ISD::ATOMIC_LOAD_ADD:
08361   case ISD::ATOMIC_LOAD_SUB:
08362   case ISD::ATOMIC_LOAD_AND:
08363   case ISD::ATOMIC_LOAD_OR:
08364   case ISD::ATOMIC_LOAD_XOR:
08365   case ISD::ATOMIC_LOAD_NAND:
08366   case ISD::ATOMIC_LOAD_MIN:
08367   case ISD::ATOMIC_LOAD_MAX:
08368   case ISD::ATOMIC_LOAD_UMIN:
08369   case ISD::ATOMIC_LOAD_UMAX:
08370   case ISD::ATOMIC_SWAP: {
08371     // For now, we assume that using vectors for these operations don't make
08372     // much sense so we just split it.  We return an empty result
08373     SDValue X, Y;
08374     SplitVectorOp(Op, X, Y);
08375     return Result;
08376     break;
08377   }
08378 
08379   } // end switch (Node->getOpcode())
08380 
08381   assert(Result.getNode() && "Didn't set a result!");  
08382   if (Result != Op)
08383     Result = LegalizeOp(Result);
08384 
08385   AddWidenedOperand(Op, Result);
08386   return Result;
08387 }
08388 
08389 // Utility function to find a legal vector type and its associated element
08390 // type from a preferred width and whose vector type must be the same size
08391 // as the VVT.
08392 //  TLI:   Target lowering used to determine legal types
08393 //  Width: Preferred width of element type
08394 //  VVT:   Vector value type whose size we must match.
08395 // Returns VecEVT and EVT - the vector type and its associated element type
08396 static void FindWidenVecType(const TargetLowering &TLI, unsigned Width, MVT VVT,
08397                              MVT& EVT, MVT& VecEVT) {
08398   // We start with the preferred width, make it a power of 2 and see if
08399   // we can find a vector type of that width. If not, we reduce it by
08400   // another power of 2.  If we have widen the type, a vector of bytes should
08401   // always be legal.
08402   assert(TLI.isTypeLegal(VVT));
08403   unsigned EWidth = Width + 1;
08404   do {
08405     assert(EWidth > 0);
08406     EWidth =  (1 << Log2_32(EWidth-1));
08407     EVT = MVT::getIntegerVT(EWidth);
08408     unsigned NumEVT = VVT.getSizeInBits()/EWidth;
08409     VecEVT = MVT::getVectorVT(EVT, NumEVT);
08410   } while (!TLI.isTypeLegal(VecEVT) ||
08411            VVT.getSizeInBits() != VecEVT.getSizeInBits());
08412 }
08413 
08414 SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
08415                                                     SDValue   Chain,
08416                                                     SDValue   BasePtr,
08417                                                     const Value *SV,
08418                                                     int         SVOffset,
08419                                                     unsigned    Alignment,
08420                                                     bool        isVolatile,
08421                                                     unsigned    LdWidth,
08422                                                     MVT         ResType,
08423                                                     DebugLoc    dl) {
08424   // We assume that we have good rules to handle loading power of two loads so
08425   // we break down the operations to power of 2 loads.  The strategy is to
08426   // load the largest power of 2 that we can easily transform to a legal vector
08427   // and then insert into that vector, and the cast the result into the legal
08428   // vector that we want.  This avoids unnecessary stack converts.
08429   // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
08430   //       the load is nonvolatile, we an use a wider load for the value.
08431   // Find a vector length we can load a large chunk
08432   MVT EVT, VecEVT;
08433   unsigned EVTWidth;
08434   FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
08435   EVTWidth = EVT.getSizeInBits();
08436 
08437   SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV, SVOffset,
08438                                isVolatile, Alignment);
08439   SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecEVT, LdOp);
08440   LdChain.push_back(LdOp.getValue(1));
08441   
08442   // Check if we can load the element with one instruction
08443   if (LdWidth == EVTWidth) {
08444     return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
08445   }
08446 
08447   // The vector element order is endianness dependent.
08448   unsigned Idx = 1;
08449   LdWidth -= EVTWidth;
08450   unsigned Offset = 0;
08451     
08452   while (LdWidth > 0) {
08453     unsigned Increment = EVTWidth / 8;
08454     Offset += Increment;
08455     BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
08456                           DAG.getIntPtrConstant(Increment));
08457 
08458     if (LdWidth < EVTWidth) {
08459       // Our current type we are using is too large, use a smaller size by
08460       // using a smaller power of 2
08461       unsigned oEVTWidth = EVTWidth;
08462       FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
08463       EVTWidth = EVT.getSizeInBits();
08464       // Readjust position and vector position based on new load type
08465       Idx = Idx * (oEVTWidth/EVTWidth);
08466       VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
08467     }
08468       
08469     SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV,
08470                                  SVOffset+Offset, isVolatile,
08471                                  MinAlign(Alignment, Offset));
08472     LdChain.push_back(LdOp.getValue(1));
08473     VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecEVT, VecOp, LdOp,
08474                         DAG.getIntPtrConstant(Idx++));
08475     
08476     LdWidth -= EVTWidth;
08477   }
08478 
08479   return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
08480 }
08481 
08482 bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
08483                                              SDValue& TFOp,
08484                                              SDValue Op,
08485                                              MVT NVT) {
08486   // TODO: Add support for ConcatVec and the ability to load many vector
08487   //       types (e.g., v4i8).  This will not work when a vector register
08488   //       to memory mapping is strange (e.g., vector elements are not
08489   //       stored in some sequential order).
08490 
08491   // It must be true that the widen vector type is bigger than where 
08492   // we need to load from.
08493   LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
08494   MVT LdVT = LD->getMemoryVT();
08495   DebugLoc dl = LD->getDebugLoc();
08496   assert(LdVT.isVector() && NVT.isVector());
08497   assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
08498   
08499   // Load information
08500   SDValue Chain = LD->getChain();
08501   SDValue BasePtr = LD->getBasePtr();
08502   int       SVOffset = LD->getSrcValueOffset();
08503   unsigned  Alignment = LD->getAlignment();
08504   bool      isVolatile = LD->isVolatile();
08505   const Value *SV = LD->getSrcValue();
08506   unsigned int LdWidth = LdVT.getSizeInBits();
08507   
08508   // Load value as a large register
08509   SDValueVector LdChain;
08510   Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
08511                                Alignment, isVolatile, LdWidth, NVT, dl);
08512 
08513   if (LdChain.size() == 1) {
08514     TFOp = LdChain[0];
08515     return true;
08516   }
08517   else {
08518     TFOp=DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 
08519                      &LdChain[0], LdChain.size());
08520     return false;
08521   }
08522 }
08523 
08524 
08525 void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
08526                                                 SDValue   Chain,
08527                                                 SDValue   BasePtr,
08528                                                 const Value *SV,
08529                                                 int         SVOffset,
08530                                                 unsigned    Alignment,
08531                                                 bool        isVolatile,
08532                                                 SDValue     ValOp,
08533                                                 unsigned    StWidth,
08534                                                 DebugLoc    dl) {
08535   // Breaks the stores into a series of power of 2 width stores.  For any
08536   // width, we convert the vector to the vector of element size that we
08537   // want to store.  This avoids requiring a stack convert.
08538   
08539   // Find a width of the element type we can store with
08540   MVT VVT = ValOp.getValueType();
08541   MVT EVT, VecEVT;
08542   unsigned EVTWidth;
08543   FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
08544   EVTWidth = EVT.getSizeInBits();
08545 
08546   SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, ValOp);
08547   SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
08548                             DAG.getIntPtrConstant(0));
08549   SDValue StOp = DAG.getStore(Chain, dl, EOp, BasePtr, SV, SVOffset,
08550                                isVolatile, Alignment);
08551   StChain.push_back(StOp);
08552 
08553   // Check if we are done
08554   if (StWidth == EVTWidth) {
08555     return;
08556   }
08557   
08558   unsigned Idx = 1;
08559   StWidth -= EVTWidth;
08560   unsigned Offset = 0;
08561     
08562   while (StWidth > 0) {
08563     unsigned Increment = EVTWidth / 8;
08564     Offset += Increment;
08565     BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
08566                           DAG.getIntPtrConstant(Increment));
08567                           
08568     if (StWidth < EVTWidth) {
08569       // Our current type we are using is too large, use a smaller size by
08570       // using a smaller power of 2
08571       unsigned oEVTWidth = EVTWidth;
08572       FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
08573       EVTWidth = EVT.getSizeInBits();
08574       // Readjust position and vector position based on new load type
08575       Idx = Idx * (oEVTWidth/EVTWidth);
08576       VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
08577     }
08578     
08579     EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
08580                       DAG.getIntPtrConstant(Idx++));
08581     StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr, SV,
08582                                    SVOffset + Offset, isVolatile,
08583                                    MinAlign(Alignment, Offset)));
08584     StWidth -= EVTWidth;
08585   }
08586 }
08587 
08588 
08589 SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
08590                                                    SDValue Chain,
08591                                                    SDValue BasePtr) {
08592   // TODO: It might be cleaner if we can use SplitVector and have more legal
08593   //        vector types that can be stored into memory (e.g., v4xi8 can
08594   //        be stored as a word). This will not work when a vector register
08595   //        to memory mapping is strange (e.g., vector elements are not
08596   //        stored in some sequential order).
08597   
08598   MVT StVT = ST->getMemoryVT();
08599   SDValue ValOp = ST->getValue();
08600   DebugLoc dl = ST->getDebugLoc();
08601 
08602   // Check if we have widen this node with another value
08603   std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
08604   if (I != WidenNodes.end())
08605     ValOp = I->second;
08606     
08607   MVT VVT = ValOp.getValueType();
08608 
08609   // It must be true that we the widen vector type is bigger than where
08610   // we need to store.
08611   assert(StVT.isVector() && VVT.isVector());
08612   assert(StVT.bitsLT(VVT));
08613   assert(StVT.getVectorElementType() == VVT.getVectorElementType());
08614 
08615   // Store value
08616   SDValueVector StChain;
08617   genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
08618                        ST->getSrcValueOffset(), ST->getAlignment(),
08619                        ST->isVolatile(), ValOp, StVT.getSizeInBits(), dl);
08620   if (StChain.size() == 1)
08621     return StChain[0];
08622   else 
08623     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
08624                        &StChain[0], StChain.size());
08625 }
08626 
08627 
08628 // SelectionDAG::Legalize - This is the entry point for the file.
08629 //
08630 void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
08631   /// run - This is the main entry point to this class.
08632   ///
08633   SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
08634 }
08635