Fix up the logic for result expanding the various extension
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeDAGTypes.cpp
1 //===-- LegalizeDAGTypes.cpp - Implement SelectionDAG::LegalizeTypes ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the SelectionDAG::LegalizeTypes method.  It transforms
11 // an arbitrary well-formed SelectionDAG to only consist of legal types.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "legalize-types"
16 #include "llvm/CodeGen/SelectionDAG.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Target/TargetLowering.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Debug.h"
23 using namespace llvm;
24
25 //===----------------------------------------------------------------------===//
26 /// DAGTypeLegalizer - This takes an arbitrary SelectionDAG as input and
27 /// hacks on it until the target machine can handle it.  This involves
28 /// eliminating value sizes the machine cannot handle (promoting small sizes to
29 /// large sizes or splitting up large values into small values) as well as
30 /// eliminating operations the machine cannot handle.
31 ///
32 /// This code also does a small amount of optimization and recognition of idioms
33 /// as part of its processing.  For example, if a target does not support a
34 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35 /// will attempt merge setcc and brc instructions into brcc's.
36 ///
37 namespace {
38 class VISIBILITY_HIDDEN DAGTypeLegalizer {
39   TargetLowering &TLI;
40   SelectionDAG &DAG;
41   
42   // NodeIDFlags - This pass uses the NodeID on the SDNodes to hold information
43   // about the state of the node.  The enum has all the values.
44   enum NodeIDFlags {
45     /// ReadyToProcess - All operands have been processed, so this node is ready
46     /// to be handled.
47     ReadyToProcess = 0,
48     
49     /// NewNode - This is a new node that was created in the process of
50     /// legalizing some other node.
51     NewNode = -1,
52     
53     /// Processed - This is a node that has already been processed.
54     Processed = -2
55     
56     // 1+ - This is a node which has this many unlegalized operands.
57   };
58   
59   enum LegalizeAction {
60     Legal,      // The target natively supports this type.
61     Promote,    // This type should be executed in a larger type.
62     Expand      // This type should be split into two types of half the size.
63   };
64   
65   /// ValueTypeActions - This is a bitvector that contains two bits for each
66   /// simple value type, where the two bits correspond to the LegalizeAction
67   /// enum.  This can be queried with "getTypeAction(VT)".
68   TargetLowering::ValueTypeActionImpl ValueTypeActions;
69   
70   /// getTypeAction - Return how we should legalize values of this type, either
71   /// it is already legal or we need to expand it into multiple registers of
72   /// smaller integer type, or we need to promote it to a larger type.
73   LegalizeAction getTypeAction(MVT::ValueType VT) const {
74     return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
75   }
76   
77   /// isTypeLegal - Return true if this type is legal on this target.
78   ///
79   bool isTypeLegal(MVT::ValueType VT) const {
80     return getTypeAction(VT) == Legal;
81   }
82   
83   SDOperand getIntPtrConstant(uint64_t Val) {
84     return DAG.getConstant(Val, TLI.getPointerTy());
85   }
86   
87   /// PromotedNodes - For nodes that are below legal width, and that have more
88   /// than one use, this map indicates what promoted value to use.
89   DenseMap<SDOperand, SDOperand> PromotedNodes;
90   
91   /// ExpandedNodes - For nodes that need to be expanded this map indicates
92   /// which operands are the expanded version of the input.
93   DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
94   
95   /// Worklist - This defines a worklist of nodes to process.  In order to be
96   /// pushed onto this worklist, all operands of a node must have already been
97   /// processed.
98   SmallVector<SDNode*, 128> Worklist;
99   
100 public:
101   DAGTypeLegalizer(SelectionDAG &dag)
102     : TLI(dag.getTargetLoweringInfo()), DAG(dag),
103     ValueTypeActions(TLI.getValueTypeActions()) {
104     assert(MVT::LAST_VALUETYPE <= 32 &&
105            "Too many value types for ValueTypeActions to hold!");
106   }      
107   
108   void run();
109   
110 private:
111   void MarkNewNodes(SDNode *N);
112   
113   void ReplaceLegalValueWith(SDOperand From, SDOperand To);
114   
115   SDOperand GetPromotedOp(SDOperand Op) {
116     Op = PromotedNodes[Op];
117     assert(Op.Val && "Operand wasn't promoted?");
118     return Op;
119   }    
120   void SetPromotedOp(SDOperand Op, SDOperand Result);
121
122   /// GetPromotedZExtOp - Get a promoted operand and zero extend it to the final
123   /// size.
124   SDOperand GetPromotedZExtOp(SDOperand Op) {
125     MVT::ValueType OldVT = Op.getValueType();
126     Op = GetPromotedOp(Op);
127     return DAG.getZeroExtendInReg(Op, OldVT);
128   }    
129   
130   void GetExpandedOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
131   void SetExpandedOp(SDOperand Op, SDOperand Lo, SDOperand Hi);
132   
133   // Common routines.
134   SDOperand CreateStackStoreLoad(SDOperand Op, MVT::ValueType DestVT);
135   SDOperand HandleMemIntrinsic(SDNode *N);
136   void SplitOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
137
138   // Result Promotion.
139   void PromoteResult(SDNode *N, unsigned ResNo);
140   SDOperand PromoteResult_UNDEF(SDNode *N);
141   SDOperand PromoteResult_Constant(SDNode *N);
142   SDOperand PromoteResult_TRUNCATE(SDNode *N);
143   SDOperand PromoteResult_INT_EXTEND(SDNode *N);
144   SDOperand PromoteResult_FP_ROUND(SDNode *N);
145   SDOperand PromoteResult_FP_TO_XINT(SDNode *N);
146   SDOperand PromoteResult_SETCC(SDNode *N);
147   SDOperand PromoteResult_LOAD(LoadSDNode *N);
148   SDOperand PromoteResult_SimpleIntBinOp(SDNode *N);
149   SDOperand PromoteResult_SHL(SDNode *N);
150   SDOperand PromoteResult_SRA(SDNode *N);
151   SDOperand PromoteResult_SRL(SDNode *N);
152   SDOperand PromoteResult_SELECT   (SDNode *N);
153   SDOperand PromoteResult_SELECT_CC(SDNode *N);
154   
155   // Result Expansion.
156   void ExpandResult(SDNode *N, unsigned ResNo);
157   void ExpandResult_UNDEF      (SDNode *N, SDOperand &Lo, SDOperand &Hi);
158   void ExpandResult_Constant   (SDNode *N, SDOperand &Lo, SDOperand &Hi);
159   void ExpandResult_BUILD_PAIR (SDNode *N, SDOperand &Lo, SDOperand &Hi);
160   void ExpandResult_ANY_EXTEND (SDNode *N, SDOperand &Lo, SDOperand &Hi);
161   void ExpandResult_ZERO_EXTEND(SDNode *N, SDOperand &Lo, SDOperand &Hi);
162   void ExpandResult_SIGN_EXTEND(SDNode *N, SDOperand &Lo, SDOperand &Hi);
163   void ExpandResult_BIT_CONVERT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
164   void ExpandResult_SIGN_EXTEND_INREG(SDNode *N, SDOperand &Lo, SDOperand &Hi);
165   void ExpandResult_LOAD       (LoadSDNode *N, SDOperand &Lo, SDOperand &Hi);
166
167   void ExpandResult_Logical    (SDNode *N, SDOperand &Lo, SDOperand &Hi);
168   void ExpandResult_BSWAP      (SDNode *N, SDOperand &Lo, SDOperand &Hi);
169   void ExpandResult_ADDSUB     (SDNode *N, SDOperand &Lo, SDOperand &Hi);
170   void ExpandResult_ADDSUBC    (SDNode *N, SDOperand &Lo, SDOperand &Hi);
171   void ExpandResult_ADDSUBE    (SDNode *N, SDOperand &Lo, SDOperand &Hi);
172   void ExpandResult_SELECT     (SDNode *N, SDOperand &Lo, SDOperand &Hi);
173   void ExpandResult_SELECT_CC  (SDNode *N, SDOperand &Lo, SDOperand &Hi);
174   void ExpandResult_MUL        (SDNode *N, SDOperand &Lo, SDOperand &Hi);
175   void ExpandResult_Shift      (SDNode *N, SDOperand &Lo, SDOperand &Hi);
176   
177   void ExpandShiftByConstant(SDNode *N, unsigned Amt, 
178                              SDOperand &Lo, SDOperand &Hi);
179   bool ExpandShiftWithKnownAmountBit(SDNode *N, SDOperand &Lo, SDOperand &Hi);
180
181   // Operand Promotion.
182   bool PromoteOperand(SDNode *N, unsigned OperandNo);
183   SDOperand PromoteOperand_ANY_EXTEND(SDNode *N);
184   SDOperand PromoteOperand_ZERO_EXTEND(SDNode *N);
185   SDOperand PromoteOperand_SIGN_EXTEND(SDNode *N);
186   SDOperand PromoteOperand_TRUNCATE(SDNode *N);
187   SDOperand PromoteOperand_FP_EXTEND(SDNode *N);
188   SDOperand PromoteOperand_FP_ROUND(SDNode *N);
189   SDOperand PromoteOperand_INT_TO_FP(SDNode *N);
190   SDOperand PromoteOperand_SELECT(SDNode *N, unsigned OpNo);
191   SDOperand PromoteOperand_BRCOND(SDNode *N, unsigned OpNo);
192   SDOperand PromoteOperand_BR_CC(SDNode *N, unsigned OpNo);
193   SDOperand PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo);
194
195   void PromoteSetCCOperands(SDOperand &LHS,SDOperand &RHS, ISD::CondCode Code);
196
197   // Operand Expansion.
198   bool ExpandOperand(SDNode *N, unsigned OperandNo);
199   SDOperand ExpandOperand_TRUNCATE(SDNode *N);
200   SDOperand ExpandOperand_BIT_CONVERT(SDNode *N);
201   SDOperand ExpandOperand_UINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
202   SDOperand ExpandOperand_SINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
203   SDOperand ExpandOperand_EXTRACT_ELEMENT(SDNode *N);
204   SDOperand ExpandOperand_SETCC(SDNode *N);
205   SDOperand ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo);
206
207   void ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
208                            ISD::CondCode &CCCode);
209 };
210 }  // end anonymous namespace
211
212
213
214 /// run - This is the main entry point for the type legalizer.  This does a
215 /// top-down traversal of the dag, legalizing types as it goes.
216 void DAGTypeLegalizer::run() {
217   // Create a dummy node (which is not added to allnodes), that adds a reference
218   // to the root node, preventing it from being deleted, and tracking any
219   // changes of the root.
220   HandleSDNode Dummy(DAG.getRoot());
221
222   // The root of the dag may dangle to deleted nodes until the type legalizer is
223   // done.  Set it to null to avoid confusion.
224   DAG.setRoot(SDOperand());
225   
226   // Walk all nodes in the graph, assigning them a NodeID of 'ReadyToProcess'
227   // (and remembering them) if they are leaves and assigning 'NewNode' if
228   // non-leaves.
229   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
230        E = DAG.allnodes_end(); I != E; ++I) {
231     if (I->getNumOperands() == 0) {
232       I->setNodeId(ReadyToProcess);
233       Worklist.push_back(I);
234     } else {
235       I->setNodeId(NewNode);
236     }
237   }
238   
239   // Now that we have a set of nodes to process, handle them all.
240   while (!Worklist.empty()) {
241     SDNode *N = Worklist.back();
242     Worklist.pop_back();
243     assert(N->getNodeId() == ReadyToProcess &&
244            "Node should be ready if on worklist!");
245     
246     // Scan the values produced by the node, checking to see if any result
247     // types are illegal.
248     unsigned i = 0;
249     unsigned NumResults = N->getNumValues();
250     do {
251       LegalizeAction Action = getTypeAction(N->getValueType(i));
252       if (Action == Promote) {
253         PromoteResult(N, i);
254         goto NodeDone;
255       } else if (Action == Expand) {
256         ExpandResult(N, i);
257         goto NodeDone;
258       } else {
259         assert(Action == Legal && "Unknown action!");
260       }
261     } while (++i < NumResults);
262     
263     // Scan the operand list for the node, handling any nodes with operands that
264     // are illegal.
265     {
266     unsigned NumOperands = N->getNumOperands();
267     bool NeedsRevisit = false;
268     for (i = 0; i != NumOperands; ++i) {
269       LegalizeAction Action = getTypeAction(N->getOperand(i).getValueType());
270       if (Action == Promote) {
271         NeedsRevisit = PromoteOperand(N, i);
272         break;
273       } else if (Action == Expand) {
274         NeedsRevisit = ExpandOperand(N, i);
275         break;
276       } else {
277         assert(Action == Legal && "Unknown action!");
278       }
279     }
280
281     // If the node needs revisiting, don't add all users to the worklist etc.
282     if (NeedsRevisit)
283       continue;
284     
285     if (i == NumOperands)
286       DEBUG(cerr << "Legally typed node: "; N->dump(&DAG); cerr << "\n");
287     }
288 NodeDone:
289
290     // If we reach here, the node was processed, potentially creating new nodes.
291     // Mark it as processed and add its users to the worklist as appropriate.
292     N->setNodeId(Processed);
293     
294     for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
295          UI != E; ++UI) {
296       SDNode *User = *UI;
297       int NodeID = User->getNodeId();
298       assert(NodeID != ReadyToProcess && NodeID != Processed &&
299              "Invalid node id for user of unprocessed node!");
300       
301       // This node has two options: it can either be a new node or its Node ID
302       // may be a count of the number of operands it has that are not ready.
303       if (NodeID > 0) {
304         User->setNodeId(NodeID-1);
305         
306         // If this was the last use it was waiting on, add it to the ready list.
307         if (NodeID-1 == ReadyToProcess)
308           Worklist.push_back(User);
309         continue;
310       }
311       
312       // Otherwise, this node is new: this is the first operand of it that
313       // became ready.  Its new NodeID is the number of operands it has minus 1
314       // (as this node is now processed).
315       assert(NodeID == NewNode && "Unknown node ID!");
316       User->setNodeId(User->getNumOperands()-1);
317       
318       // If the node only has a single operand, it is now ready.
319       if (User->getNumOperands() == 1)
320         Worklist.push_back(User);
321     }
322   }
323   
324   // If the root changed (e.g. it was a dead load, update the root).
325   DAG.setRoot(Dummy.getValue());
326
327   //DAG.viewGraph();
328
329   // Remove dead nodes.  This is important to do for cleanliness but also before
330   // the checking loop below.  Implicit folding by the DAG.getNode operators can
331   // cause unreachable nodes to be around with their flags set to new.
332   DAG.RemoveDeadNodes();
333
334   // In a debug build, scan all the nodes to make sure we found them all.  This
335   // ensures that there are no cycles and that everything got processed.
336 #ifndef NDEBUG
337   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
338        E = DAG.allnodes_end(); I != E; ++I) {
339     if (I->getNodeId() == Processed)
340       continue;
341     cerr << "Unprocessed node: ";
342     I->dump(&DAG); cerr << "\n";
343
344     if (I->getNodeId() == NewNode)
345       cerr << "New node not 'noticed'?\n";
346     else if (I->getNodeId() > 0)
347       cerr << "Operand not processed?\n";
348     else if (I->getNodeId() == ReadyToProcess)
349       cerr << "Not added to worklist?\n";
350     abort();
351   }
352 #endif
353 }
354
355 /// MarkNewNodes - The specified node is the root of a subtree of potentially
356 /// new nodes.  Add the correct NodeId to mark it.
357 void DAGTypeLegalizer::MarkNewNodes(SDNode *N) {
358   // If this was an existing node that is already done, we're done.
359   if (N->getNodeId() != NewNode)
360     return;
361
362   // Okay, we know that this node is new.  Recursively walk all of its operands
363   // to see if they are new also.  The depth of this walk is bounded by the size
364   // of the new tree that was constructed (usually 2-3 nodes), so we don't worry
365   // about revisiting of nodes.
366   //
367   // As we walk the operands, keep track of the number of nodes that are
368   // processed.  If non-zero, this will become the new nodeid of this node.
369   unsigned NumProcessed = 0;
370   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
371     int OpId = N->getOperand(i).Val->getNodeId();
372     if (OpId == NewNode)
373       MarkNewNodes(N->getOperand(i).Val);
374     else if (OpId == Processed)
375       ++NumProcessed;
376   }
377   
378   N->setNodeId(N->getNumOperands()-NumProcessed);
379   if (N->getNodeId() == ReadyToProcess)
380     Worklist.push_back(N);
381 }
382
383 /// ReplaceLegalValueWith - The specified value with a legal type was legalized
384 /// to the specified other value.  If they are different, update the DAG and
385 /// NodeIDs replacing any uses of From to use To instead.
386 void DAGTypeLegalizer::ReplaceLegalValueWith(SDOperand From, SDOperand To) {
387   if (From == To) return;
388   
389   // If expansion produced new nodes, make sure they are properly marked.
390   if (To.Val->getNodeId() == NewNode)
391     MarkNewNodes(To.Val);
392   
393   // Anything that used the old node should now use the new one.  Note that this
394   // can potentially cause recursive merging.
395   DAG.ReplaceAllUsesOfValueWith(From, To);
396   
397   // Since we just made an unstructured update to the DAG, which could wreak
398   // general havoc on anything that once used N and now uses Res, walk all users
399   // of the result, updating their flags.
400   for (SDNode::use_iterator I = To.Val->use_begin(), E = To.Val->use_end();
401        I != E; ++I) {
402     SDNode *User = *I;
403     // If the node isn't already processed or in the worklist, mark it as new,
404     // then use MarkNewNodes to recompute its ID.
405     int NodeId = User->getNodeId();
406     if (NodeId != ReadyToProcess && NodeId != Processed) {
407       User->setNodeId(NewNode);
408       MarkNewNodes(User);
409     }
410   }
411 }
412
413 void DAGTypeLegalizer::SetPromotedOp(SDOperand Op, SDOperand Result) {
414   if (Result.Val->getNodeId() == NewNode) 
415     MarkNewNodes(Result.Val);
416
417   SDOperand &OpEntry = PromotedNodes[Op];
418   assert(OpEntry.Val == 0 && "Node is already promoted!");
419   OpEntry = Result;
420 }
421
422 void DAGTypeLegalizer::GetExpandedOp(SDOperand Op, SDOperand &Lo, 
423                                      SDOperand &Hi) {
424   std::pair<SDOperand, SDOperand> &Entry = ExpandedNodes[Op];
425   assert(Entry.first.Val && "Operand isn't expanded");
426   Lo = Entry.first;
427   Hi = Entry.second;
428 }
429
430 void DAGTypeLegalizer::SetExpandedOp(SDOperand Op, SDOperand Lo, 
431                                      SDOperand Hi) {
432   // Remember that this is the result of the node.
433   std::pair<SDOperand, SDOperand> &Entry = ExpandedNodes[Op];
434   assert(Entry.first.Val == 0 && "Node already expanded");
435   Entry.first = Lo;
436   Entry.second = Hi;
437   
438   // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant.
439   if (Lo.Val->getNodeId() == NewNode) 
440     MarkNewNodes(Lo.Val);
441   if (Hi.Val->getNodeId() == NewNode) 
442     MarkNewNodes(Hi.Val);
443 }
444
445 SDOperand DAGTypeLegalizer::CreateStackStoreLoad(SDOperand Op, 
446                                                  MVT::ValueType DestVT) {
447   // Create the stack frame object.
448   SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
449   
450   // Emit a store to the stack slot.
451   SDOperand Store = DAG.getStore(DAG.getEntryNode(), Op, FIPtr, NULL, 0);
452   // Result is a load from the stack slot.
453   return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
454 }
455
456
457 /// HandleMemIntrinsic - This handles memcpy/memset/memmove with invalid
458 /// operands.  This promotes or expands the operands as required.
459 SDOperand DAGTypeLegalizer::HandleMemIntrinsic(SDNode *N) {
460   // The chain and pointer [operands #0 and #1] are always valid types.
461   SDOperand Chain = N->getOperand(0);
462   SDOperand Ptr   = N->getOperand(1);
463   SDOperand Op2   = N->getOperand(2);
464   
465   // Op #2 is either a value (memset) or a pointer.  Promote it if required.
466   switch (getTypeAction(Op2.getValueType())) {
467   default: assert(0 && "Unknown action for pointer/value operand");
468   case Legal: break;
469   case Promote: Op2 = GetPromotedOp(Op2); break;
470   }
471   
472   // The length could have any action required.
473   SDOperand Length = N->getOperand(3);
474   switch (getTypeAction(Length.getValueType())) {
475   default: assert(0 && "Unknown action for memop operand");
476   case Legal: break;
477   case Promote: Length = GetPromotedZExtOp(Length); break;
478   case Expand:
479     SDOperand Dummy;  // discard the high part.
480     GetExpandedOp(Length, Length, Dummy);
481     break;
482   }
483   
484   SDOperand Align = N->getOperand(4);
485   switch (getTypeAction(Align.getValueType())) {
486   default: assert(0 && "Unknown action for memop operand");
487   case Legal: break;
488   case Promote: Align = GetPromotedZExtOp(Align); break;
489   }
490   
491   SDOperand AlwaysInline = N->getOperand(5);
492   switch (getTypeAction(AlwaysInline.getValueType())) {
493   default: assert(0 && "Unknown action for memop operand");
494   case Legal: break;
495   case Promote: AlwaysInline = GetPromotedZExtOp(AlwaysInline); break;
496   }
497   
498   SDOperand Ops[] = { Chain, Ptr, Op2, Length, Align, AlwaysInline };
499   return DAG.UpdateNodeOperands(SDOperand(N, 0), Ops, 6);
500 }
501
502 /// SplitOp - Return the lower and upper halves of Op's bits in a value type
503 /// half the size of Op's.
504 void DAGTypeLegalizer::SplitOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi) {
505   unsigned NVTBits = MVT::getSizeInBits(Op.getValueType())/2;
506   assert(MVT::getSizeInBits(Op.getValueType()) == 2*NVTBits &&
507          "Cannot split odd sized integer type");
508   MVT::ValueType NVT = MVT::getIntegerType(NVTBits);
509   Lo = DAG.getNode(ISD::TRUNCATE, NVT, Op);
510   Hi = DAG.getNode(ISD::SRL, Op.getValueType(), Op,
511                    DAG.getConstant(NVTBits, TLI.getShiftAmountTy()));
512   Hi = DAG.getNode(ISD::TRUNCATE, NVT, Hi);
513 }
514
515
516 //===----------------------------------------------------------------------===//
517 //  Result Promotion
518 //===----------------------------------------------------------------------===//
519
520 /// PromoteResult - This method is called when a result of a node is found to be
521 /// in need of promotion to a larger type.  At this point, the node may also
522 /// have invalid operands or may have other results that need expansion, we just
523 /// know that (at least) one result needs promotion.
524 void DAGTypeLegalizer::PromoteResult(SDNode *N, unsigned ResNo) {
525   DEBUG(cerr << "Promote node result: "; N->dump(&DAG); cerr << "\n");
526   SDOperand Result = SDOperand();
527   
528   switch (N->getOpcode()) {
529   default:
530 #ifndef NDEBUG
531     cerr << "PromoteResult #" << ResNo << ": ";
532     N->dump(&DAG); cerr << "\n";
533 #endif
534     assert(0 && "Do not know how to promote this operator!");
535     abort();
536   case ISD::UNDEF:    Result = PromoteResult_UNDEF(N); break;
537   case ISD::Constant: Result = PromoteResult_Constant(N); break;
538
539   case ISD::TRUNCATE:    Result = PromoteResult_TRUNCATE(N); break;
540   case ISD::SIGN_EXTEND:
541   case ISD::ZERO_EXTEND:
542   case ISD::ANY_EXTEND:  Result = PromoteResult_INT_EXTEND(N); break;
543   case ISD::FP_ROUND:    Result = PromoteResult_FP_ROUND(N); break;
544   case ISD::FP_TO_SINT:
545   case ISD::FP_TO_UINT:  Result = PromoteResult_FP_TO_XINT(N); break;
546   case ISD::SETCC:    Result = PromoteResult_SETCC(N); break;
547   case ISD::LOAD:     Result = PromoteResult_LOAD(cast<LoadSDNode>(N)); break;
548
549   case ISD::AND:
550   case ISD::OR:
551   case ISD::XOR:
552   case ISD::ADD:
553   case ISD::SUB:
554   case ISD::MUL:      Result = PromoteResult_SimpleIntBinOp(N); break;
555
556   case ISD::SHL:      Result = PromoteResult_SHL(N); break;
557   case ISD::SRA:      Result = PromoteResult_SRA(N); break;
558   case ISD::SRL:      Result = PromoteResult_SRL(N); break;
559
560   case ISD::SELECT:    Result = PromoteResult_SELECT(N); break;
561   case ISD::SELECT_CC: Result = PromoteResult_SELECT_CC(N); break;
562
563   }      
564   
565   // If Result is null, the sub-method took care of registering the result.
566   if (Result.Val)
567     SetPromotedOp(SDOperand(N, ResNo), Result);
568 }
569
570 SDOperand DAGTypeLegalizer::PromoteResult_UNDEF(SDNode *N) {
571   return DAG.getNode(ISD::UNDEF, TLI.getTypeToTransformTo(N->getValueType(0)));
572 }
573
574 SDOperand DAGTypeLegalizer::PromoteResult_Constant(SDNode *N) {
575   MVT::ValueType VT = N->getValueType(0);
576   // Zero extend things like i1, sign extend everything else.  It shouldn't
577   // matter in theory which one we pick, but this tends to give better code?
578   unsigned Opc = VT != MVT::i1 ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
579   SDOperand Result = DAG.getNode(Opc, TLI.getTypeToTransformTo(VT),
580                                  SDOperand(N, 0));
581   assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
582   return Result;
583 }
584
585 SDOperand DAGTypeLegalizer::PromoteResult_TRUNCATE(SDNode *N) {
586   SDOperand Res;
587
588   switch (getTypeAction(N->getOperand(0).getValueType())) {
589   default: assert(0 && "Unknown type action!");
590   case Legal:
591   case Expand:
592     Res = N->getOperand(0);
593     break;
594   case Promote:
595     Res = GetPromotedOp(N->getOperand(0));
596     break;
597   }
598
599   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
600   assert(MVT::getSizeInBits(Res.getValueType()) >= MVT::getSizeInBits(NVT) &&
601          "Truncation doesn't make sense!");
602   if (Res.getValueType() == NVT)
603     return Res;
604
605   // Truncate to NVT instead of VT
606   return DAG.getNode(ISD::TRUNCATE, NVT, Res);
607 }
608
609 SDOperand DAGTypeLegalizer::PromoteResult_INT_EXTEND(SDNode *N) {
610   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
611
612   if (getTypeAction(N->getOperand(0).getValueType()) == Promote) {
613     SDOperand Res = GetPromotedOp(N->getOperand(0));
614     assert(MVT::getSizeInBits(Res.getValueType()) <= MVT::getSizeInBits(NVT) &&
615            "Extension doesn't make sense!");
616
617     // If the result and operand types are the same after promotion, simplify
618     // to an in-register extension.
619     if (NVT == Res.getValueType()) {
620       // The high bits are not guaranteed to be anything.  Insert an extend.
621       if (N->getOpcode() == ISD::SIGN_EXTEND)
622         return DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res,
623                            DAG.getValueType(N->getOperand(0).getValueType()));
624       if (N->getOpcode() == ISD::ZERO_EXTEND)
625         return DAG.getZeroExtendInReg(Res, N->getOperand(0).getValueType());
626       assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
627       return Res;
628     }
629   }
630
631   // Otherwise, just extend the original operand all the way to the larger type.
632   return DAG.getNode(N->getOpcode(), NVT, N->getOperand(0));
633 }
634
635 SDOperand DAGTypeLegalizer::PromoteResult_FP_ROUND(SDNode *N) {
636   // NOTE: Assumes input is legal.
637   return DAG.getNode(ISD::FP_ROUND_INREG, N->getOperand(0).getValueType(),
638                      N->getOperand(0), DAG.getValueType(N->getValueType(0)));
639 }
640
641 SDOperand DAGTypeLegalizer::PromoteResult_FP_TO_XINT(SDNode *N) {
642   SDOperand Op = N->getOperand(0);
643   // If the operand needed to be promoted, do so now.
644   if (getTypeAction(Op.getValueType()) == Promote)
645     // The input result is prerounded, so we don't have to do anything special.
646     Op = GetPromotedOp(Op);
647   
648   unsigned NewOpc = N->getOpcode();
649   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
650   
651   // If we're promoting a UINT to a larger size, check to see if the new node
652   // will be legal.  If it isn't, check to see if FP_TO_SINT is legal, since
653   // we can use that instead.  This allows us to generate better code for
654   // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
655   // legal, such as PowerPC.
656   if (N->getOpcode() == ISD::FP_TO_UINT) {
657     if (!TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
658         (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
659          TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom))
660       NewOpc = ISD::FP_TO_SINT;
661   }
662
663   return DAG.getNode(NewOpc, NVT, Op);
664 }
665
666
667 SDOperand DAGTypeLegalizer::PromoteResult_SETCC(SDNode *N) {
668   assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
669   return DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), N->getOperand(0),
670                      N->getOperand(1), N->getOperand(2));
671 }
672
673 SDOperand DAGTypeLegalizer::PromoteResult_LOAD(LoadSDNode *N) {
674   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
675   ISD::LoadExtType ExtType =
676     ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
677   SDOperand Res = DAG.getExtLoad(ExtType, NVT, N->getChain(), N->getBasePtr(),
678                                  N->getSrcValue(), N->getSrcValueOffset(),
679                                  N->getLoadedVT(), N->isVolatile(),
680                                  N->getAlignment());
681
682   // Legalized the chain result - switch anything that used the old chain to
683   // use the new one.
684   ReplaceLegalValueWith(SDOperand(N, 1), Res.getValue(1));
685   return Res;
686 }
687
688 SDOperand DAGTypeLegalizer::PromoteResult_SimpleIntBinOp(SDNode *N) {
689   // The input may have strange things in the top bits of the registers, but
690   // these operations don't care.  They may have weird bits going out, but
691   // that too is okay if they are integer operations.
692   SDOperand LHS = GetPromotedOp(N->getOperand(0));
693   SDOperand RHS = GetPromotedOp(N->getOperand(1));
694   return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
695 }
696
697 SDOperand DAGTypeLegalizer::PromoteResult_SHL(SDNode *N) {
698   return DAG.getNode(ISD::SHL, TLI.getTypeToTransformTo(N->getValueType(0)),
699                      GetPromotedOp(N->getOperand(0)), N->getOperand(1));
700 }
701
702 SDOperand DAGTypeLegalizer::PromoteResult_SRA(SDNode *N) {
703   // The input value must be properly sign extended.
704   MVT::ValueType VT = N->getValueType(0);
705   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
706   SDOperand Res = GetPromotedOp(N->getOperand(0));
707   Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res, DAG.getValueType(VT));
708   return DAG.getNode(ISD::SRA, NVT, Res, N->getOperand(1));
709 }
710
711 SDOperand DAGTypeLegalizer::PromoteResult_SRL(SDNode *N) {
712   // The input value must be properly zero extended.
713   MVT::ValueType VT = N->getValueType(0);
714   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
715   SDOperand Res = GetPromotedZExtOp(N->getOperand(0));
716   return DAG.getNode(ISD::SRL, NVT, Res, N->getOperand(1));
717 }
718
719 SDOperand DAGTypeLegalizer::PromoteResult_SELECT(SDNode *N) {
720   SDOperand LHS = GetPromotedOp(N->getOperand(1));
721   SDOperand RHS = GetPromotedOp(N->getOperand(2));
722   return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
723 }
724
725 SDOperand DAGTypeLegalizer::PromoteResult_SELECT_CC(SDNode *N) {
726   SDOperand LHS = GetPromotedOp(N->getOperand(2));
727   SDOperand RHS = GetPromotedOp(N->getOperand(3));
728   return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
729                      N->getOperand(1), LHS, RHS, N->getOperand(4));
730 }
731
732 //===----------------------------------------------------------------------===//
733 //  Result Expansion
734 //===----------------------------------------------------------------------===//
735
736 /// ExpandResult - This method is called when the specified result of the
737 /// specified node is found to need expansion.  At this point, the node may also
738 /// have invalid operands or may have other results that need promotion, we just
739 /// know that (at least) one result needs expansion.
740 void DAGTypeLegalizer::ExpandResult(SDNode *N, unsigned ResNo) {
741   DEBUG(cerr << "Expand node result: "; N->dump(&DAG); cerr << "\n");
742   SDOperand Lo, Hi;
743   Lo = Hi = SDOperand();
744
745   // If this is a single-result node, see if the target wants to custom expand
746   // it.
747   if (N->getNumValues() == 1 &&
748       TLI.getOperationAction(N->getOpcode(),
749                              N->getValueType(0)) == TargetLowering::Custom) {
750     // If the target wants to, allow it to lower this itself.
751     std::pair<SDOperand,SDOperand> P = TLI.ExpandOperationResult(N, DAG);
752     if (P.first.Val) {
753       Lo = P.first;
754       Hi = P.second;
755       return;
756     }
757   }
758   
759   switch (N->getOpcode()) {
760   default:
761 #ifndef NDEBUG
762     cerr << "ExpandResult #" << ResNo << ": ";
763     N->dump(&DAG); cerr << "\n";
764 #endif
765     assert(0 && "Do not know how to expand this operator!");
766     abort();
767       
768   case ISD::UNDEF:       ExpandResult_UNDEF(N, Lo, Hi); break;
769   case ISD::Constant:    ExpandResult_Constant(N, Lo, Hi); break;
770   case ISD::BUILD_PAIR:  ExpandResult_BUILD_PAIR(N, Lo, Hi); break;
771   case ISD::ANY_EXTEND:  ExpandResult_ANY_EXTEND(N, Lo, Hi); break;
772   case ISD::ZERO_EXTEND: ExpandResult_ZERO_EXTEND(N, Lo, Hi); break;
773   case ISD::SIGN_EXTEND: ExpandResult_SIGN_EXTEND(N, Lo, Hi); break;
774   case ISD::BIT_CONVERT: ExpandResult_BIT_CONVERT(N, Lo, Hi); break;
775   case ISD::SIGN_EXTEND_INREG: ExpandResult_SIGN_EXTEND_INREG(N, Lo, Hi); break;
776   case ISD::LOAD:        ExpandResult_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
777     
778   case ISD::AND:
779   case ISD::OR:
780   case ISD::XOR:         ExpandResult_Logical(N, Lo, Hi); break;
781   case ISD::BSWAP:       ExpandResult_BSWAP(N, Lo, Hi); break;
782   case ISD::ADD:
783   case ISD::SUB:         ExpandResult_ADDSUB(N, Lo, Hi); break;
784   case ISD::ADDC:
785   case ISD::SUBC:        ExpandResult_ADDSUBC(N, Lo, Hi); break;
786   case ISD::ADDE:
787   case ISD::SUBE:        ExpandResult_ADDSUBE(N, Lo, Hi); break;
788   case ISD::SELECT:      ExpandResult_SELECT(N, Lo, Hi); break;
789   case ISD::SELECT_CC:   ExpandResult_SELECT_CC(N, Lo, Hi); break;
790   case ISD::MUL:         ExpandResult_MUL(N, Lo, Hi); break;
791   case ISD::SHL:
792   case ISD::SRA:
793   case ISD::SRL:         ExpandResult_Shift(N, Lo, Hi); break;
794
795   }
796   
797   // If Lo/Hi is null, the sub-method took care of registering results etc.
798   if (Lo.Val)
799     SetExpandedOp(SDOperand(N, ResNo), Lo, Hi);
800 }
801
802 void DAGTypeLegalizer::ExpandResult_UNDEF(SDNode *N,
803                                           SDOperand &Lo, SDOperand &Hi) {
804   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
805   Lo = Hi = DAG.getNode(ISD::UNDEF, NVT);
806 }
807
808 void DAGTypeLegalizer::ExpandResult_Constant(SDNode *N,
809                                              SDOperand &Lo, SDOperand &Hi) {
810   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
811   uint64_t Cst = cast<ConstantSDNode>(N)->getValue();
812   Lo = DAG.getConstant(Cst, NVT);
813   Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
814 }
815
816 void DAGTypeLegalizer::ExpandResult_BUILD_PAIR(SDNode *N,
817                                                SDOperand &Lo, SDOperand &Hi) {
818   // Return the operands.
819   Lo = N->getOperand(0);
820   Hi = N->getOperand(1);
821 }
822
823 void DAGTypeLegalizer::ExpandResult_ANY_EXTEND(SDNode *N,
824                                                SDOperand &Lo, SDOperand &Hi) {
825   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
826   SDOperand Op = N->getOperand(0);
827   if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
828     // The low part is any extension of the input (which degenerates to a copy).
829     Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Op);
830     Hi = DAG.getNode(ISD::UNDEF, NVT);   // The high part is undefined.
831   } else {
832     // For example, extension of an i48 to an i64.  The operand type necessarily
833     // promotes to the result type, so will end up being expanded too.
834     assert(getTypeAction(Op.getValueType()) == Promote &&
835            "Don't know how to expand this result!");
836     SDOperand Res = GetPromotedOp(Op);
837     assert(Res.getValueType() == N->getValueType(0) &&
838            "Operand over promoted?");
839     // Split the promoted operand.  This will simplify when it is expanded.
840     SplitOp(Res, Lo, Hi);
841   }
842 }
843
844 void DAGTypeLegalizer::ExpandResult_ZERO_EXTEND(SDNode *N,
845                                                 SDOperand &Lo, SDOperand &Hi) {
846   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
847   SDOperand Op = N->getOperand(0);
848   if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
849     // The low part is zero extension of the input (which degenerates to a copy).
850     Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, N->getOperand(0));
851     Hi = DAG.getConstant(0, NVT);   // The high part is just a zero.
852   } else {
853     // For example, extension of an i48 to an i64.  The operand type necessarily
854     // promotes to the result type, so will end up being expanded too.
855     assert(getTypeAction(Op.getValueType()) == Promote &&
856            "Don't know how to expand this result!");
857     SDOperand Res = GetPromotedOp(Op);
858     assert(Res.getValueType() == N->getValueType(0) &&
859            "Operand over promoted?");
860     // Split the promoted operand.  This will simplify when it is expanded.
861     SplitOp(Res, Lo, Hi);
862     unsigned ExcessBits =
863       MVT::getSizeInBits(Op.getValueType()) - MVT::getSizeInBits(NVT);
864     Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerType(ExcessBits));
865   }
866 }
867
868 void DAGTypeLegalizer::ExpandResult_SIGN_EXTEND(SDNode *N,
869                                                 SDOperand &Lo, SDOperand &Hi) {
870   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
871   SDOperand Op = N->getOperand(0);
872   if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
873     // The low part is sign extension of the input (which degenerates to a copy).
874     Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, N->getOperand(0));
875     // The high part is obtained by SRA'ing all but one of the bits of low part.
876     unsigned LoSize = MVT::getSizeInBits(NVT);
877     Hi = DAG.getNode(ISD::SRA, NVT, Lo,
878                      DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
879   } else {
880     // For example, extension of an i48 to an i64.  The operand type necessarily
881     // promotes to the result type, so will end up being expanded too.
882     assert(getTypeAction(Op.getValueType()) == Promote &&
883            "Don't know how to expand this result!");
884     SDOperand Res = GetPromotedOp(Op);
885     assert(Res.getValueType() == N->getValueType(0) &&
886            "Operand over promoted?");
887     // Split the promoted operand.  This will simplify when it is expanded.
888     SplitOp(Res, Lo, Hi);
889     unsigned ExcessBits =
890       MVT::getSizeInBits(Op.getValueType()) - MVT::getSizeInBits(NVT);
891     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
892                      DAG.getValueType(MVT::getIntegerType(ExcessBits)));
893   }
894 }
895
896 void DAGTypeLegalizer::ExpandResult_BIT_CONVERT(SDNode *N,
897                                                 SDOperand &Lo, SDOperand &Hi) {
898   // Lower the bit-convert to a store/load from the stack, then expand the load.
899   SDOperand Op = CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
900   ExpandResult_LOAD(cast<LoadSDNode>(Op.Val), Lo, Hi);
901 }
902
903 void DAGTypeLegalizer::
904 ExpandResult_SIGN_EXTEND_INREG(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
905   GetExpandedOp(N->getOperand(0), Lo, Hi);
906   MVT::ValueType EVT = N->getOperand(1).getValueType();
907
908   if (MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(Lo.getValueType())) {
909     // sext_inreg the low part if needed.
910     Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, Lo.getValueType(), Lo,
911                      N->getOperand(1));
912
913     // The high part gets the sign extension from the lo-part.  This handles
914     // things like sextinreg V:i64 from i8.
915     Hi = DAG.getNode(ISD::SRA, Hi.getValueType(), Lo,
916                      DAG.getConstant(MVT::getSizeInBits(Hi.getValueType())-1,
917                                      TLI.getShiftAmountTy()));
918   } else {
919     // For example, extension of an i48 to an i64.  Leave the low part alone,
920     // sext_inreg the high part.
921     unsigned ExcessBits =
922       MVT::getSizeInBits(EVT) - MVT::getSizeInBits(Lo.getValueType());
923     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
924                      DAG.getValueType(MVT::getIntegerType(ExcessBits)));
925   }
926 }
927
928 void DAGTypeLegalizer::ExpandResult_LOAD(LoadSDNode *N,
929                                          SDOperand &Lo, SDOperand &Hi) {
930   MVT::ValueType VT = N->getValueType(0);
931   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
932   SDOperand Ch  = N->getChain();    // Legalize the chain.
933   SDOperand Ptr = N->getBasePtr();  // Legalize the pointer.
934   ISD::LoadExtType ExtType = N->getExtensionType();
935   int SVOffset = N->getSrcValueOffset();
936   unsigned Alignment = N->getAlignment();
937   bool isVolatile = N->isVolatile();
938   
939   if (ExtType == ISD::NON_EXTLOAD) {
940     Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
941                      isVolatile, Alignment);
942     // Increment the pointer to the other half.
943     unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
944     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
945                       getIntPtrConstant(IncrementSize));
946     Hi = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
947                      isVolatile, std::max(Alignment, IncrementSize));
948     
949     // Build a factor node to remember that this load is independent of the
950     // other one.
951     Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
952                      Hi.getValue(1));
953
954     // Handle endianness of the load.
955     if (!TLI.isLittleEndian())
956       std::swap(Lo, Hi);
957   } else {
958     MVT::ValueType EVT = N->getLoadedVT();
959     
960     if (EVT == NVT)
961       Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(),
962                        SVOffset, isVolatile, Alignment);
963     else
964       Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(),
965                           SVOffset, EVT, isVolatile,
966                           Alignment);
967     // Remember the chain.
968     Ch = Lo.getValue(1);
969     
970     if (ExtType == ISD::SEXTLOAD) {
971       // The high part is obtained by SRA'ing all but one of the bits of the
972       // lo part.
973       unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
974       Hi = DAG.getNode(ISD::SRA, NVT, Lo,
975                        DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
976     } else if (ExtType == ISD::ZEXTLOAD) {
977       // The high part is just a zero.
978       Hi = DAG.getConstant(0, NVT);
979     } else {
980       assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
981       // The high part is undefined.
982       Hi = DAG.getNode(ISD::UNDEF, NVT);
983     }
984   }
985   
986   // Legalized the chain result - switch anything that used the old chain to
987   // use the new one.
988   ReplaceLegalValueWith(SDOperand(N, 1), Ch);
989 }  
990
991
992 void DAGTypeLegalizer::ExpandResult_Logical(SDNode *N,
993                                             SDOperand &Lo, SDOperand &Hi) {
994   SDOperand LL, LH, RL, RH;
995   GetExpandedOp(N->getOperand(0), LL, LH);
996   GetExpandedOp(N->getOperand(1), RL, RH);
997   Lo = DAG.getNode(N->getOpcode(), LL.getValueType(), LL, RL);
998   Hi = DAG.getNode(N->getOpcode(), LL.getValueType(), LH, RH);
999 }
1000
1001 void DAGTypeLegalizer::ExpandResult_BSWAP(SDNode *N,
1002                                           SDOperand &Lo, SDOperand &Hi) {
1003   GetExpandedOp(N->getOperand(0), Hi, Lo);  // Note swapped operands.
1004   Lo = DAG.getNode(ISD::BSWAP, Lo.getValueType(), Lo);
1005   Hi = DAG.getNode(ISD::BSWAP, Hi.getValueType(), Hi);
1006 }
1007
1008
1009 void DAGTypeLegalizer::ExpandResult_SELECT(SDNode *N,
1010                                            SDOperand &Lo, SDOperand &Hi) {
1011   SDOperand LL, LH, RL, RH;
1012   GetExpandedOp(N->getOperand(1), LL, LH);
1013   GetExpandedOp(N->getOperand(2), RL, RH);
1014   Lo = DAG.getNode(ISD::SELECT, LL.getValueType(), N->getOperand(0), LL, RL);
1015   
1016   assert(N->getOperand(0).getValueType() != MVT::f32 &&
1017          "FIXME: softfp shouldn't use expand!");
1018   Hi = DAG.getNode(ISD::SELECT, LL.getValueType(), N->getOperand(0), LH, RH);
1019 }
1020
1021 void DAGTypeLegalizer::ExpandResult_SELECT_CC(SDNode *N,
1022                                               SDOperand &Lo, SDOperand &Hi) {
1023   SDOperand LL, LH, RL, RH;
1024   GetExpandedOp(N->getOperand(2), LL, LH);
1025   GetExpandedOp(N->getOperand(3), RL, RH);
1026   Lo = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0), 
1027                    N->getOperand(1), LL, RL, N->getOperand(4));
1028   
1029   assert(N->getOperand(0).getValueType() != MVT::f32 &&
1030          "FIXME: softfp shouldn't use expand!");
1031   Hi = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0), 
1032                    N->getOperand(1), LH, RH, N->getOperand(4));
1033 }
1034
1035 void DAGTypeLegalizer::ExpandResult_ADDSUB(SDNode *N,
1036                                            SDOperand &Lo, SDOperand &Hi) {
1037   // Expand the subcomponents.
1038   SDOperand LHSL, LHSH, RHSL, RHSH;
1039   GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1040   GetExpandedOp(N->getOperand(1), RHSL, RHSH);
1041   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1042   SDOperand LoOps[2] = { LHSL, RHSL };
1043   SDOperand HiOps[3] = { LHSH, RHSH };
1044
1045   if (N->getOpcode() == ISD::ADD) {
1046     Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1047     HiOps[2] = Lo.getValue(1);
1048     Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1049   } else {
1050     Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1051     HiOps[2] = Lo.getValue(1);
1052     Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1053   }
1054 }
1055
1056 void DAGTypeLegalizer::ExpandResult_ADDSUBC(SDNode *N,
1057                                             SDOperand &Lo, SDOperand &Hi) {
1058   // Expand the subcomponents.
1059   SDOperand LHSL, LHSH, RHSL, RHSH;
1060   GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1061   GetExpandedOp(N->getOperand(1), RHSL, RHSH);
1062   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1063   SDOperand LoOps[2] = { LHSL, RHSL };
1064   SDOperand HiOps[3] = { LHSH, RHSH };
1065
1066   if (N->getOpcode() == ISD::ADDC) {
1067     Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1068     HiOps[2] = Lo.getValue(1);
1069     Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1070   } else {
1071     Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1072     HiOps[2] = Lo.getValue(1);
1073     Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1074   }
1075
1076   // Legalized the flag result - switch anything that used the old flag to
1077   // use the new one.
1078   ReplaceLegalValueWith(SDOperand(N, 1), Hi.getValue(1));
1079 }
1080
1081 void DAGTypeLegalizer::ExpandResult_ADDSUBE(SDNode *N,
1082                                             SDOperand &Lo, SDOperand &Hi) {
1083   // Expand the subcomponents.
1084   SDOperand LHSL, LHSH, RHSL, RHSH;
1085   GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1086   GetExpandedOp(N->getOperand(1), RHSL, RHSH);
1087   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1088   SDOperand LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1089   SDOperand HiOps[3] = { LHSH, RHSH };
1090
1091   Lo = DAG.getNode(N->getOpcode(), VTList, LoOps, 3);
1092   HiOps[2] = Lo.getValue(1);
1093   Hi = DAG.getNode(N->getOpcode(), VTList, HiOps, 3);
1094
1095   // Legalized the flag result - switch anything that used the old flag to
1096   // use the new one.
1097   ReplaceLegalValueWith(SDOperand(N, 1), Hi.getValue(1));
1098 }
1099
1100 void DAGTypeLegalizer::ExpandResult_MUL(SDNode *N,
1101                                         SDOperand &Lo, SDOperand &Hi) {
1102   MVT::ValueType VT = N->getValueType(0);
1103   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1104   
1105   bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
1106   bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
1107   bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
1108   bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
1109   if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1110     SDOperand LL, LH, RL, RH;
1111     GetExpandedOp(N->getOperand(0), LL, LH);
1112     GetExpandedOp(N->getOperand(1), RL, RH);
1113     unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
1114     unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1115     unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1116     
1117     // FIXME: generalize this to handle other bit sizes
1118     if (LHSSB == 32 && RHSSB == 32 &&
1119         DAG.MaskedValueIsZero(N->getOperand(0), 0xFFFFFFFF00000000ULL) &&
1120         DAG.MaskedValueIsZero(N->getOperand(1), 0xFFFFFFFF00000000ULL)) {
1121       // The inputs are both zero-extended.
1122       if (HasUMUL_LOHI) {
1123         // We can emit a umul_lohi.
1124         Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1125         Hi = SDOperand(Lo.Val, 1);
1126         return;
1127       }
1128       if (HasMULHU) {
1129         // We can emit a mulhu+mul.
1130         Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1131         Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1132         return;
1133       }
1134     }
1135     if (LHSSB > BitSize && RHSSB > BitSize) {
1136       // The input values are both sign-extended.
1137       if (HasSMUL_LOHI) {
1138         // We can emit a smul_lohi.
1139         Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1140         Hi = SDOperand(Lo.Val, 1);
1141         return;
1142       }
1143       if (HasMULHS) {
1144         // We can emit a mulhs+mul.
1145         Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1146         Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
1147         return;
1148       }
1149     }
1150     if (HasUMUL_LOHI) {
1151       // Lo,Hi = umul LHS, RHS.
1152       SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
1153                                        DAG.getVTList(NVT, NVT), LL, RL);
1154       Lo = UMulLOHI;
1155       Hi = UMulLOHI.getValue(1);
1156       RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1157       LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1158       Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1159       Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1160       return;
1161     }
1162   }
1163   
1164   abort();
1165 #if 0 // FIXME!
1166   // If nothing else, we can make a libcall.
1167   Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), N,
1168                      false/*sign irrelevant*/, Hi);
1169 #endif
1170 }  
1171
1172
1173 void DAGTypeLegalizer::ExpandResult_Shift(SDNode *N,
1174                                           SDOperand &Lo, SDOperand &Hi) {
1175   MVT::ValueType VT = N->getValueType(0);
1176   
1177   // If we can emit an efficient shift operation, do so now.  Check to see if 
1178   // the RHS is a constant.
1179   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1180     return ExpandShiftByConstant(N, CN->getValue(), Lo, Hi);
1181
1182   // If we can determine that the high bit of the shift is zero or one, even if
1183   // the low bits are variable, emit this shift in an optimized form.
1184   if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1185     return;
1186   
1187   // If this target supports shift_PARTS, use it.  First, map to the _PARTS opc.
1188   unsigned PartsOpc;
1189   if (N->getOpcode() == ISD::SHL)
1190     PartsOpc = ISD::SHL_PARTS;
1191   else if (N->getOpcode() == ISD::SRL)
1192     PartsOpc = ISD::SRL_PARTS;
1193   else {
1194     assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1195     PartsOpc = ISD::SRA_PARTS;
1196   }
1197   
1198   // Next check to see if the target supports this SHL_PARTS operation or if it
1199   // will custom expand it.
1200   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1201   TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1202   if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1203       Action == TargetLowering::Custom) {
1204     // Expand the subcomponents.
1205     SDOperand LHSL, LHSH;
1206     GetExpandedOp(N->getOperand(0), LHSL, LHSH);
1207     
1208     SDOperand Ops[] = { LHSL, LHSH, N->getOperand(1) };
1209     MVT::ValueType VT = LHSL.getValueType();
1210     Lo = DAG.getNode(PartsOpc, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
1211     Hi = Lo.getValue(1);
1212     return;
1213   }
1214   
1215   abort();
1216 #if 0 // FIXME!
1217   // Otherwise, emit a libcall.
1218   unsigned RuntimeCode = ; // SRL -> SRL_I64 etc.
1219   bool Signed = ;
1220   Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), N,
1221                      false/*lshr is unsigned*/, Hi);
1222 #endif
1223 }  
1224
1225
1226 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
1227 /// and the shift amount is a constant 'Amt'.  Expand the operation.
1228 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt, 
1229                                              SDOperand &Lo, SDOperand &Hi) {
1230   // Expand the incoming operand to be shifted, so that we have its parts
1231   SDOperand InL, InH;
1232   GetExpandedOp(N->getOperand(0), InL, InH);
1233   
1234   MVT::ValueType NVT = InL.getValueType();
1235   unsigned VTBits = MVT::getSizeInBits(N->getValueType(0));
1236   unsigned NVTBits = MVT::getSizeInBits(NVT);
1237   MVT::ValueType ShTy = N->getOperand(1).getValueType();
1238
1239   if (N->getOpcode() == ISD::SHL) {
1240     if (Amt > VTBits) {
1241       Lo = Hi = DAG.getConstant(0, NVT);
1242     } else if (Amt > NVTBits) {
1243       Lo = DAG.getConstant(0, NVT);
1244       Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
1245     } else if (Amt == NVTBits) {
1246       Lo = DAG.getConstant(0, NVT);
1247       Hi = InL;
1248     } else {
1249       Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt, ShTy));
1250       Hi = DAG.getNode(ISD::OR, NVT,
1251                        DAG.getNode(ISD::SHL, NVT, InH,
1252                                    DAG.getConstant(Amt, ShTy)),
1253                        DAG.getNode(ISD::SRL, NVT, InL,
1254                                    DAG.getConstant(NVTBits-Amt, ShTy)));
1255     }
1256     return;
1257   }
1258   
1259   if (N->getOpcode() == ISD::SRL) {
1260     if (Amt > VTBits) {
1261       Lo = DAG.getConstant(0, NVT);
1262       Hi = DAG.getConstant(0, NVT);
1263     } else if (Amt > NVTBits) {
1264       Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1265       Hi = DAG.getConstant(0, NVT);
1266     } else if (Amt == NVTBits) {
1267       Lo = InH;
1268       Hi = DAG.getConstant(0, NVT);
1269     } else {
1270       Lo = DAG.getNode(ISD::OR, NVT,
1271                        DAG.getNode(ISD::SRL, NVT, InL,
1272                                    DAG.getConstant(Amt, ShTy)),
1273                        DAG.getNode(ISD::SHL, NVT, InH,
1274                                    DAG.getConstant(NVTBits-Amt, ShTy)));
1275       Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt, ShTy));
1276     }
1277     return;
1278   }
1279   
1280   assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1281   if (Amt > VTBits) {
1282     Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1283                           DAG.getConstant(NVTBits-1, ShTy));
1284   } else if (Amt > NVTBits) {
1285     Lo = DAG.getNode(ISD::SRA, NVT, InH,
1286                      DAG.getConstant(Amt-NVTBits, ShTy));
1287     Hi = DAG.getNode(ISD::SRA, NVT, InH,
1288                      DAG.getConstant(NVTBits-1, ShTy));
1289   } else if (Amt == NVTBits) {
1290     Lo = InH;
1291     Hi = DAG.getNode(ISD::SRA, NVT, InH,
1292                      DAG.getConstant(NVTBits-1, ShTy));
1293   } else {
1294     Lo = DAG.getNode(ISD::OR, NVT,
1295                      DAG.getNode(ISD::SRL, NVT, InL,
1296                                  DAG.getConstant(Amt, ShTy)),
1297                      DAG.getNode(ISD::SHL, NVT, InH,
1298                                  DAG.getConstant(NVTBits-Amt, ShTy)));
1299     Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Amt, ShTy));
1300   }
1301 }
1302
1303 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1304 /// this shift based on knowledge of the high bit of the shift amount.  If we
1305 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1306 /// shift amount.
1307 bool DAGTypeLegalizer::
1308 ExpandShiftWithKnownAmountBit(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
1309   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1310   unsigned NVTBits = MVT::getSizeInBits(NVT);
1311
1312   uint64_t HighBitMask = NVTBits, KnownZero, KnownOne;
1313   DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1314   
1315   // If we don't know anything about the high bit, exit.
1316   if (((KnownZero|KnownOne) & HighBitMask) == 0)
1317     return false;
1318
1319   // Get the incoming operand to be shifted.
1320   SDOperand InL, InH;
1321   GetExpandedOp(N->getOperand(0), InL, InH);
1322   SDOperand Amt = N->getOperand(1);
1323
1324   // If we know that the high bit of the shift amount is one, then we can do
1325   // this as a couple of simple shifts.
1326   if (KnownOne & HighBitMask) {
1327     // Mask out the high bit, which we know is set.
1328     Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
1329                       DAG.getConstant(NVTBits-1, Amt.getValueType()));
1330     
1331     switch (N->getOpcode()) {
1332     default: assert(0 && "Unknown shift");
1333     case ISD::SHL:
1334       Lo = DAG.getConstant(0, NVT);              // Low part is zero.
1335       Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
1336       return true;
1337     case ISD::SRL:
1338       Hi = DAG.getConstant(0, NVT);              // Hi part is zero.
1339       Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
1340       return true;
1341     case ISD::SRA:
1342       Hi = DAG.getNode(ISD::SRA, NVT, InH,       // Sign extend high part.
1343                        DAG.getConstant(NVTBits-1, Amt.getValueType()));
1344       Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
1345       return true;
1346     }
1347   }
1348   
1349   // If we know that the high bit of the shift amount is zero, then we can do
1350   // this as a couple of simple shifts.
1351   assert((KnownZero & HighBitMask) && "Bad mask computation above");
1352
1353   // Compute 32-amt.
1354   SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
1355                                DAG.getConstant(NVTBits, Amt.getValueType()),
1356                                Amt);
1357   unsigned Op1, Op2;
1358   switch (N->getOpcode()) {
1359   default: assert(0 && "Unknown shift");
1360   case ISD::SHL:  Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1361   case ISD::SRL:
1362   case ISD::SRA:  Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1363   }
1364     
1365   Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
1366   Hi = DAG.getNode(ISD::OR, NVT,
1367                    DAG.getNode(Op1, NVT, InH, Amt),
1368                    DAG.getNode(Op2, NVT, InL, Amt2));
1369   return true;
1370 }
1371
1372 //===----------------------------------------------------------------------===//
1373 //  Operand Promotion
1374 //===----------------------------------------------------------------------===//
1375
1376 /// PromoteOperand - This method is called when the specified operand of the
1377 /// specified node is found to need promotion.  At this point, all of the result
1378 /// types of the node are known to be legal, but other operands of the node may
1379 /// need promotion or expansion as well as the specified one.
1380 bool DAGTypeLegalizer::PromoteOperand(SDNode *N, unsigned OpNo) {
1381   DEBUG(cerr << "Promote node operand: "; N->dump(&DAG); cerr << "\n");
1382   SDOperand Res;
1383   switch (N->getOpcode()) {
1384     default:
1385 #ifndef NDEBUG
1386     cerr << "PromoteOperand Op #" << OpNo << ": ";
1387     N->dump(&DAG); cerr << "\n";
1388 #endif
1389     assert(0 && "Do not know how to promote this operator's operand!");
1390     abort();
1391     
1392   case ISD::ANY_EXTEND:  Res = PromoteOperand_ANY_EXTEND(N); break;
1393   case ISD::ZERO_EXTEND: Res = PromoteOperand_ZERO_EXTEND(N); break;
1394   case ISD::SIGN_EXTEND: Res = PromoteOperand_SIGN_EXTEND(N); break;
1395   case ISD::TRUNCATE:    Res = PromoteOperand_TRUNCATE(N); break;
1396   case ISD::FP_EXTEND:   Res = PromoteOperand_FP_EXTEND(N); break;
1397   case ISD::FP_ROUND:    Res = PromoteOperand_FP_ROUND(N); break;
1398   case ISD::SINT_TO_FP:
1399   case ISD::UINT_TO_FP:  Res = PromoteOperand_INT_TO_FP(N); break;
1400     
1401   case ISD::SELECT:      Res = PromoteOperand_SELECT(N, OpNo); break;
1402   case ISD::BRCOND:      Res = PromoteOperand_BRCOND(N, OpNo); break;
1403   case ISD::BR_CC:       Res = PromoteOperand_BR_CC(N, OpNo); break;
1404
1405   case ISD::STORE:       Res = PromoteOperand_STORE(cast<StoreSDNode>(N),
1406                                                     OpNo); break;
1407   case ISD::MEMSET:
1408   case ISD::MEMCPY:
1409   case ISD::MEMMOVE:     Res = HandleMemIntrinsic(N); break;
1410   }
1411   
1412   // If the result is null, the sub-method took care of registering results etc.
1413   if (!Res.Val) return false;
1414   // If the result is N, the sub-method updated N in place.
1415   if (Res.Val == N) {
1416     // Mark N as new and remark N and its operands.  This allows us to correctly
1417     // revisit N if it needs another step of promotion and allows us to visit
1418     // any new operands to N.
1419     N->setNodeId(NewNode);
1420     MarkNewNodes(N);
1421     return true;
1422   }
1423   
1424   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1425          "Invalid operand expansion");
1426   
1427   ReplaceLegalValueWith(SDOperand(N, 0), Res);
1428   return false;
1429 }
1430
1431 SDOperand DAGTypeLegalizer::PromoteOperand_ANY_EXTEND(SDNode *N) {
1432   SDOperand Op = GetPromotedOp(N->getOperand(0));
1433   return DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
1434 }
1435
1436 SDOperand DAGTypeLegalizer::PromoteOperand_ZERO_EXTEND(SDNode *N) {
1437   SDOperand Op = GetPromotedOp(N->getOperand(0));
1438   Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
1439   return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
1440 }
1441
1442 SDOperand DAGTypeLegalizer::PromoteOperand_SIGN_EXTEND(SDNode *N) {
1443   SDOperand Op = GetPromotedOp(N->getOperand(0));
1444   Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
1445   return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(),
1446                      Op, DAG.getValueType(N->getOperand(0).getValueType()));
1447 }
1448
1449 SDOperand DAGTypeLegalizer::PromoteOperand_TRUNCATE(SDNode *N) {
1450   SDOperand Op = GetPromotedOp(N->getOperand(0));
1451   return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), Op);
1452 }
1453
1454 SDOperand DAGTypeLegalizer::PromoteOperand_FP_EXTEND(SDNode *N) {
1455   SDOperand Op = GetPromotedOp(N->getOperand(0));
1456   return DAG.getNode(ISD::FP_EXTEND, N->getValueType(0), Op);
1457 }
1458
1459 SDOperand DAGTypeLegalizer::PromoteOperand_FP_ROUND(SDNode *N) {
1460   SDOperand Op = GetPromotedOp(N->getOperand(0));
1461   return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Op);
1462 }
1463
1464 SDOperand DAGTypeLegalizer::PromoteOperand_INT_TO_FP(SDNode *N) {
1465   SDOperand In = GetPromotedOp(N->getOperand(0));
1466   MVT::ValueType OpVT = N->getOperand(0).getValueType();
1467   if (N->getOpcode() == ISD::UINT_TO_FP)
1468     In = DAG.getZeroExtendInReg(In, OpVT);
1469   else
1470     In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(),
1471                      In, DAG.getValueType(OpVT));
1472   
1473   return DAG.UpdateNodeOperands(SDOperand(N, 0), In);
1474 }
1475
1476
1477 SDOperand DAGTypeLegalizer::PromoteOperand_SELECT(SDNode *N, unsigned OpNo) {
1478   assert(OpNo == 0 && "Only know how to promote condition");
1479   SDOperand Cond = GetPromotedOp(N->getOperand(0));  // Promote the condition.
1480
1481   // The top bits of the promoted condition are not necessarily zero, ensure
1482   // that the value is properly zero extended.
1483   if (!DAG.MaskedValueIsZero(Cond, 
1484                              MVT::getIntVTBitMask(Cond.getValueType())^1)) {
1485     Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
1486     MarkNewNodes(Cond.Val); 
1487   }
1488
1489   // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1490   return DAG.UpdateNodeOperands(SDOperand(N, 0), Cond, N->getOperand(1),
1491                                 N->getOperand(2));
1492 }
1493
1494
1495 SDOperand DAGTypeLegalizer::PromoteOperand_BRCOND(SDNode *N, unsigned OpNo) {
1496   assert(OpNo == 1 && "only know how to promote condition");
1497   SDOperand Cond = GetPromotedOp(N->getOperand(1));  // Promote the condition.
1498   
1499   // The top bits of the promoted condition are not necessarily zero, ensure
1500   // that the value is properly zero extended.
1501   if (!DAG.MaskedValueIsZero(Cond, 
1502                              MVT::getIntVTBitMask(Cond.getValueType())^1)) {
1503     Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
1504     MarkNewNodes(Cond.Val); 
1505   }
1506   
1507   // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1508   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0), Cond,
1509                                 N->getOperand(2));
1510 }
1511
1512 SDOperand DAGTypeLegalizer::PromoteOperand_BR_CC(SDNode *N, unsigned OpNo) {
1513   assert(OpNo == 2 && "Don't know how to promote this operand");
1514   
1515   SDOperand LHS = N->getOperand(2);
1516   SDOperand RHS = N->getOperand(3);
1517   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
1518   
1519   // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
1520   // legal types.
1521   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
1522                                 N->getOperand(1), LHS, RHS, N->getOperand(4));
1523 }
1524
1525 /// PromoteSetCCOperands - Promote the operands of a comparison.  This code is
1526 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1527 void DAGTypeLegalizer::PromoteSetCCOperands(SDOperand &NewLHS,SDOperand &NewRHS,
1528                                             ISD::CondCode CCCode) {
1529   MVT::ValueType VT = NewLHS.getValueType();
1530   
1531   // Get the promoted values.
1532   NewLHS = GetPromotedOp(NewLHS);
1533   NewRHS = GetPromotedOp(NewRHS);
1534   
1535   // If this is an FP compare, the operands have already been extended.
1536   if (!MVT::isInteger(NewLHS.getValueType()))
1537     return;
1538   
1539   // Otherwise, we have to insert explicit sign or zero extends.  Note
1540   // that we could insert sign extends for ALL conditions, but zero extend
1541   // is cheaper on many machines (an AND instead of two shifts), so prefer
1542   // it.
1543   switch (CCCode) {
1544   default: assert(0 && "Unknown integer comparison!");
1545   case ISD::SETEQ:
1546   case ISD::SETNE:
1547   case ISD::SETUGE:
1548   case ISD::SETUGT:
1549   case ISD::SETULE:
1550   case ISD::SETULT:
1551     // ALL of these operations will work if we either sign or zero extend
1552     // the operands (including the unsigned comparisons!).  Zero extend is
1553     // usually a simpler/cheaper operation, so prefer it.
1554     NewLHS = DAG.getZeroExtendInReg(NewLHS, VT);
1555     NewRHS = DAG.getZeroExtendInReg(NewRHS, VT);
1556     return;
1557   case ISD::SETGE:
1558   case ISD::SETGT:
1559   case ISD::SETLT:
1560   case ISD::SETLE:
1561     NewLHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewLHS.getValueType(), NewLHS,
1562                          DAG.getValueType(VT));
1563     NewRHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewRHS.getValueType(), NewRHS,
1564                          DAG.getValueType(VT));
1565     return;
1566   }
1567 }
1568   
1569
1570 SDOperand DAGTypeLegalizer::PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo){
1571   SDOperand Ch = N->getChain(), Ptr = N->getBasePtr();
1572   int SVOffset = N->getSrcValueOffset();
1573   unsigned Alignment = N->getAlignment();
1574   bool isVolatile = N->isVolatile();
1575   
1576   SDOperand Val = GetPromotedOp(N->getValue());  // Get promoted value.
1577
1578   assert(!N->isTruncatingStore() && "Cannot promote this store operand!");
1579   
1580   // Truncate the value and store the result.
1581   return DAG.getTruncStore(Ch, Val, Ptr, N->getSrcValue(),
1582                            SVOffset, N->getStoredVT(),
1583                            isVolatile, Alignment);
1584 }
1585
1586
1587 //===----------------------------------------------------------------------===//
1588 //  Operand Expansion
1589 //===----------------------------------------------------------------------===//
1590
1591 /// ExpandOperand - This method is called when the specified operand of the
1592 /// specified node is found to need expansion.  At this point, all of the result
1593 /// types of the node are known to be legal, but other operands of the node may
1594 /// need promotion or expansion as well as the specified one.
1595 bool DAGTypeLegalizer::ExpandOperand(SDNode *N, unsigned OpNo) {
1596   DEBUG(cerr << "Expand node operand: "; N->dump(&DAG); cerr << "\n");
1597   SDOperand Res;
1598   switch (N->getOpcode()) {
1599   default:
1600 #ifndef NDEBUG
1601     cerr << "ExpandOperand Op #" << OpNo << ": ";
1602     N->dump(&DAG); cerr << "\n";
1603 #endif
1604     assert(0 && "Do not know how to expand this operator's operand!");
1605     abort();
1606     
1607   case ISD::TRUNCATE:        Res = ExpandOperand_TRUNCATE(N); break;
1608   case ISD::BIT_CONVERT:     Res = ExpandOperand_BIT_CONVERT(N); break;
1609
1610   case ISD::SINT_TO_FP:
1611     Res = ExpandOperand_SINT_TO_FP(N->getOperand(0), N->getValueType(0));
1612     break;
1613   case ISD::UINT_TO_FP:
1614     Res = ExpandOperand_UINT_TO_FP(N->getOperand(0), N->getValueType(0)); 
1615     break;
1616   case ISD::EXTRACT_ELEMENT: Res = ExpandOperand_EXTRACT_ELEMENT(N); break;
1617   case ISD::SETCC:           Res = ExpandOperand_SETCC(N); break;
1618
1619   case ISD::STORE: Res = ExpandOperand_STORE(cast<StoreSDNode>(N), OpNo); break;
1620   case ISD::MEMSET:
1621   case ISD::MEMCPY:
1622   case ISD::MEMMOVE:     Res = HandleMemIntrinsic(N); break;
1623   }
1624   
1625   // If the result is null, the sub-method took care of registering results etc.
1626   if (!Res.Val) return false;
1627   // If the result is N, the sub-method updated N in place.  Check to see if any
1628   // operands are new, and if so, mark them.
1629   if (Res.Val == N) {
1630     // Mark N as new and remark N and its operands.  This allows us to correctly
1631     // revisit N if it needs another step of promotion and allows us to visit
1632     // any new operands to N.
1633     N->setNodeId(NewNode);
1634     MarkNewNodes(N);
1635     return true;
1636   }
1637
1638   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1639          "Invalid operand expansion");
1640   
1641   ReplaceLegalValueWith(SDOperand(N, 0), Res);
1642   return false;
1643 }
1644
1645 SDOperand DAGTypeLegalizer::ExpandOperand_TRUNCATE(SDNode *N) {
1646   SDOperand InL, InH;
1647   GetExpandedOp(N->getOperand(0), InL, InH);
1648   // Just truncate the low part of the source.
1649   return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
1650 }
1651
1652 SDOperand DAGTypeLegalizer::ExpandOperand_BIT_CONVERT(SDNode *N) {
1653   return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
1654 }
1655
1656 SDOperand DAGTypeLegalizer::ExpandOperand_SINT_TO_FP(SDOperand Source, 
1657                                                      MVT::ValueType DestTy) {
1658   // We know the destination is legal, but that the input needs to be expanded.
1659   assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1660   
1661   // Check to see if the target has a custom way to lower this.  If so, use it.
1662   switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
1663   default: assert(0 && "This action not implemented for this operation!");
1664   case TargetLowering::Legal:
1665   case TargetLowering::Expand:
1666     break;   // This case is handled below.
1667   case TargetLowering::Custom:
1668     SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
1669                                                   Source), DAG);
1670     if (NV.Val) return NV;
1671     break;   // The target lowered this.
1672   }
1673   
1674   RTLIB::Libcall LC;
1675   if (DestTy == MVT::f32)
1676     LC = RTLIB::SINTTOFP_I64_F32;
1677   else {
1678     assert(DestTy == MVT::f64 && "Unknown fp value type!");
1679     LC = RTLIB::SINTTOFP_I64_F64;
1680   }
1681   
1682   assert(0 && "FIXME: no libcalls yet!");
1683   abort();
1684 #if 0
1685   assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
1686   Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
1687   SDOperand UnusedHiPart;
1688   return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, true, UnusedHiPart);
1689 #endif
1690 }
1691
1692 SDOperand DAGTypeLegalizer::ExpandOperand_UINT_TO_FP(SDOperand Source, 
1693                                                      MVT::ValueType DestTy) {
1694   // We know the destination is legal, but that the input needs to be expanded.
1695   assert(getTypeAction(Source.getValueType()) == Expand &&
1696          "This is not an expansion!");
1697   assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1698   
1699   // If this is unsigned, and not supported, first perform the conversion to
1700   // signed, then adjust the result if the sign bit is set.
1701   SDOperand SignedConv = ExpandOperand_SINT_TO_FP(Source, DestTy);
1702
1703   // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
1704   // incoming integer is set.  To handle this, we dynamically test to see if
1705   // it is set, and, if so, add a fudge factor.
1706   SDOperand Lo, Hi;
1707   GetExpandedOp(Source, Lo, Hi);
1708   
1709   SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
1710                                    DAG.getConstant(0, Hi.getValueType()),
1711                                    ISD::SETLT);
1712   SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
1713   SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
1714                                     SignSet, Four, Zero);
1715   uint64_t FF = 0x5f800000ULL;
1716   if (TLI.isLittleEndian()) FF <<= 32;
1717   Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
1718   
1719   SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
1720   CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
1721   SDOperand FudgeInReg;
1722   if (DestTy == MVT::f32)
1723     FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
1724   else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
1725     // FIXME: Avoid the extend by construction the right constantpool?
1726     FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
1727                                 CPIdx, NULL, 0, MVT::f32);
1728   else 
1729     assert(0 && "Unexpected conversion");
1730   
1731   return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
1732 }
1733
1734
1735 SDOperand DAGTypeLegalizer::ExpandOperand_EXTRACT_ELEMENT(SDNode *N) {
1736   SDOperand Lo, Hi;
1737   GetExpandedOp(N->getOperand(0), Lo, Hi);
1738   return cast<ConstantSDNode>(N->getOperand(1))->getValue() ? Hi : Lo;
1739 }
1740
1741 SDOperand DAGTypeLegalizer::ExpandOperand_SETCC(SDNode *N) {
1742   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1743   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1744   ExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1745   
1746   // If ExpandSetCCOperands returned a scalar, use it.
1747   if (NewRHS.Val == 0) return NewLHS;
1748
1749   // Otherwise, update N to have the operands specified.
1750   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
1751                                 DAG.getCondCode(CCCode));
1752 }
1753
1754 /// ExpandSetCCOperands - Expand the operands of a comparison.  This code is
1755 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1756 void DAGTypeLegalizer::ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
1757                                            ISD::CondCode &CCCode) {
1758   SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1759   GetExpandedOp(NewLHS, LHSLo, LHSHi);
1760   GetExpandedOp(NewRHS, RHSLo, RHSHi);
1761   
1762   MVT::ValueType VT = NewLHS.getValueType();
1763   if (VT == MVT::f32 || VT == MVT::f64) {
1764     assert(0 && "FIXME: softfp not implemented yet! should be promote not exp");
1765   }
1766   
1767   if (VT == MVT::ppcf128) {
1768     // FIXME:  This generated code sucks.  We want to generate
1769     //         FCMP crN, hi1, hi2
1770     //         BNE crN, L:
1771     //         FCMP crN, lo1, lo2
1772     // The following can be improved, but not that much.
1773     SDOperand Tmp1, Tmp2, Tmp3;
1774     Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1775     Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
1776     Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1777     Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
1778     Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
1779     Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1780     NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
1781     NewRHS = SDOperand();   // LHS is the result, not a compare.
1782     return;
1783   }
1784   
1785   
1786   if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1787     if (RHSLo == RHSHi)
1788       if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1789         if (RHSCST->isAllOnesValue()) {
1790           // Equality comparison to -1.
1791           NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1792           NewRHS = RHSLo;
1793           return;
1794         }
1795           
1796     NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1797     NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1798     NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1799     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1800     return;
1801   }
1802   
1803   // If this is a comparison of the sign bit, just look at the top part.
1804   // X > -1,  x < 0
1805   if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1806     if ((CCCode == ISD::SETLT && CST->getValue() == 0) ||   // X < 0
1807         (CCCode == ISD::SETGT && CST->isAllOnesValue())) {  // X > -1
1808       NewLHS = LHSHi;
1809       NewRHS = RHSHi;
1810       return;
1811     }
1812       
1813   // FIXME: This generated code sucks.
1814   ISD::CondCode LowCC;
1815   switch (CCCode) {
1816   default: assert(0 && "Unknown integer setcc!");
1817   case ISD::SETLT:
1818   case ISD::SETULT: LowCC = ISD::SETULT; break;
1819   case ISD::SETGT:
1820   case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1821   case ISD::SETLE:
1822   case ISD::SETULE: LowCC = ISD::SETULE; break;
1823   case ISD::SETGE:
1824   case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1825   }
1826   
1827   // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison
1828   // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands
1829   // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1830   
1831   // NOTE: on targets without efficient SELECT of bools, we can always use
1832   // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
1833   TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
1834   SDOperand Tmp1, Tmp2;
1835   Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
1836                            false, DagCombineInfo);
1837   if (!Tmp1.Val)
1838     Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
1839   Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1840                            CCCode, false, DagCombineInfo);
1841   if (!Tmp2.Val)
1842     Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,
1843                        DAG.getCondCode(CCCode));
1844   
1845   ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
1846   ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
1847   if ((Tmp1C && Tmp1C->getValue() == 0) ||
1848       (Tmp2C && Tmp2C->getValue() == 0 &&
1849        (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
1850         CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
1851       (Tmp2C && Tmp2C->getValue() == 1 &&
1852        (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
1853         CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
1854     // low part is known false, returns high part.
1855     // For LE / GE, if high part is known false, ignore the low part.
1856     // For LT / GT, if high part is known true, ignore the low part.
1857     NewLHS = Tmp2;
1858     NewRHS = SDOperand();
1859     return;
1860   }
1861   
1862   NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1863                              ISD::SETEQ, false, DagCombineInfo);
1864   if (!NewLHS.Val)
1865     NewLHS = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1866   NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1867                        NewLHS, Tmp1, Tmp2);
1868   NewRHS = SDOperand();
1869 }
1870
1871
1872 SDOperand DAGTypeLegalizer::ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo) {
1873   assert(OpNo == 1 && "Can only expand the stored value so far");
1874   assert(!N->isTruncatingStore() && "Can't expand truncstore!");
1875
1876   unsigned IncrementSize = 0;
1877   SDOperand Lo, Hi;
1878   
1879   // If this is a vector type, then we have to calculate the increment as
1880   // the product of the element size in bytes, and the number of elements
1881   // in the high half of the vector.
1882   if (MVT::isVector(N->getValue().getValueType())) {
1883     assert(0 && "Vectors not supported yet");
1884 #if 0
1885     SDNode *InVal = ST->getValue().Val;
1886     unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1887     MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
1888     
1889     // Figure out if there is a simple type corresponding to this Vector
1890     // type.  If so, convert to the vector type.
1891     MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1892     if (TLI.isTypeLegal(TVT)) {
1893       // Turn this into a normal store of the vector type.
1894       Tmp3 = LegalizeOp(Node->getOperand(1));
1895       Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1896                             SVOffset, isVolatile, Alignment);
1897       Result = LegalizeOp(Result);
1898       break;
1899     } else if (NumElems == 1) {
1900       // Turn this into a normal store of the scalar type.
1901       Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
1902       Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1903                             SVOffset, isVolatile, Alignment);
1904       // The scalarized value type may not be legal, e.g. it might require
1905       // promotion or expansion.  Relegalize the scalar store.
1906       return LegalizeOp(Result);
1907     } else {
1908       SplitVectorOp(Node->getOperand(1), Lo, Hi);
1909       IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1910     }
1911 #endif
1912   } else {
1913     GetExpandedOp(N->getValue(), Lo, Hi);
1914     IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
1915     
1916     if (!TLI.isLittleEndian())
1917       std::swap(Lo, Hi);
1918   }
1919   
1920   SDOperand Chain    = N->getChain();
1921   SDOperand Ptr      = N->getBasePtr();
1922   int SVOffset       = N->getSrcValueOffset();
1923   unsigned Alignment = N->getAlignment();
1924   bool isVolatile    = N->isVolatile();
1925   
1926   Lo = DAG.getStore(Chain, Lo, Ptr, N->getSrcValue(),
1927                     SVOffset, isVolatile, Alignment);
1928   
1929   assert(Hi.Val && "FIXME: int <-> float should be handled with promote!");
1930 #if 0
1931   if (Hi.Val == NULL) {
1932     // Must be int <-> float one-to-one expansion.
1933     return Lo;
1934   }
1935 #endif
1936   
1937   Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1938                     getIntPtrConstant(IncrementSize));
1939   assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
1940   Hi = DAG.getStore(Chain, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
1941                     isVolatile, std::max(Alignment, IncrementSize));
1942   return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1943 }
1944
1945 //===----------------------------------------------------------------------===//
1946 //  Entry Point
1947 //===----------------------------------------------------------------------===//
1948
1949 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
1950 /// only uses types natively supported by the target.
1951 ///
1952 /// Note that this is an involved process that may invalidate pointers into
1953 /// the graph.
1954 void SelectionDAG::LegalizeTypes() {
1955   DAGTypeLegalizer(*this).run();
1956 }
1957