9c297698c1db16f941a6041606e0537e7f56bed4
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeTypes.cpp
1 //===-- LegalizeTypes.cpp - Common code for DAG type legalizer ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the SelectionDAG::LegalizeTypes method.  It transforms
11 // an arbitrary well-formed SelectionDAG to only consist of legal types.  This
12 // is common code shared among the LegalizeTypes*.cpp files.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "LegalizeTypes.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/IR/CallingConv.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
24
25 #define DEBUG_TYPE "legalize-types"
26
27 static cl::opt<bool>
28 EnableExpensiveChecks("enable-legalize-types-checking", cl::Hidden);
29
30 /// PerformExpensiveChecks - Do extensive, expensive, sanity checking.
31 void DAGTypeLegalizer::PerformExpensiveChecks() {
32   // If a node is not processed, then none of its values should be mapped by any
33   // of PromotedIntegers, ExpandedIntegers, ..., ReplacedValues.
34
35   // If a node is processed, then each value with an illegal type must be mapped
36   // by exactly one of PromotedIntegers, ExpandedIntegers, ..., ReplacedValues.
37   // Values with a legal type may be mapped by ReplacedValues, but not by any of
38   // the other maps.
39
40   // Note that these invariants may not hold momentarily when processing a node:
41   // the node being processed may be put in a map before being marked Processed.
42
43   // Note that it is possible to have nodes marked NewNode in the DAG.  This can
44   // occur in two ways.  Firstly, a node may be created during legalization but
45   // never passed to the legalization core.  This is usually due to the implicit
46   // folding that occurs when using the DAG.getNode operators.  Secondly, a new
47   // node may be passed to the legalization core, but when analyzed may morph
48   // into a different node, leaving the original node as a NewNode in the DAG.
49   // A node may morph if one of its operands changes during analysis.  Whether
50   // it actually morphs or not depends on whether, after updating its operands,
51   // it is equivalent to an existing node: if so, it morphs into that existing
52   // node (CSE).  An operand can change during analysis if the operand is a new
53   // node that morphs, or it is a processed value that was mapped to some other
54   // value (as recorded in ReplacedValues) in which case the operand is turned
55   // into that other value.  If a node morphs then the node it morphed into will
56   // be used instead of it for legalization, however the original node continues
57   // to live on in the DAG.
58   // The conclusion is that though there may be nodes marked NewNode in the DAG,
59   // all uses of such nodes are also marked NewNode: the result is a fungus of
60   // NewNodes growing on top of the useful nodes, and perhaps using them, but
61   // not used by them.
62
63   // If a value is mapped by ReplacedValues, then it must have no uses, except
64   // by nodes marked NewNode (see above).
65
66   // The final node obtained by mapping by ReplacedValues is not marked NewNode.
67   // Note that ReplacedValues should be applied iteratively.
68
69   // Note that the ReplacedValues map may also map deleted nodes (by iterating
70   // over the DAG we never dereference deleted nodes).  This means that it may
71   // also map nodes marked NewNode if the deallocated memory was reallocated as
72   // another node, and that new node was not seen by the LegalizeTypes machinery
73   // (for example because it was created but not used).  In general, we cannot
74   // distinguish between new nodes and deleted nodes.
75   SmallVector<SDNode*, 16> NewNodes;
76   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
77        E = DAG.allnodes_end(); I != E; ++I) {
78     // Remember nodes marked NewNode - they are subject to extra checking below.
79     if (I->getNodeId() == NewNode)
80       NewNodes.push_back(I);
81
82     for (unsigned i = 0, e = I->getNumValues(); i != e; ++i) {
83       SDValue Res(I, i);
84       bool Failed = false;
85
86       unsigned Mapped = 0;
87       if (ReplacedValues.find(Res) != ReplacedValues.end()) {
88         Mapped |= 1;
89         // Check that remapped values are only used by nodes marked NewNode.
90         for (SDNode::use_iterator UI = I->use_begin(), UE = I->use_end();
91              UI != UE; ++UI)
92           if (UI.getUse().getResNo() == i)
93             assert(UI->getNodeId() == NewNode &&
94                    "Remapped value has non-trivial use!");
95
96         // Check that the final result of applying ReplacedValues is not
97         // marked NewNode.
98         SDValue NewVal = ReplacedValues[Res];
99         DenseMap<SDValue, SDValue>::iterator I = ReplacedValues.find(NewVal);
100         while (I != ReplacedValues.end()) {
101           NewVal = I->second;
102           I = ReplacedValues.find(NewVal);
103         }
104         assert(NewVal.getNode()->getNodeId() != NewNode &&
105                "ReplacedValues maps to a new node!");
106       }
107       if (PromotedIntegers.find(Res) != PromotedIntegers.end())
108         Mapped |= 2;
109       if (SoftenedFloats.find(Res) != SoftenedFloats.end())
110         Mapped |= 4;
111       if (ScalarizedVectors.find(Res) != ScalarizedVectors.end())
112         Mapped |= 8;
113       if (ExpandedIntegers.find(Res) != ExpandedIntegers.end())
114         Mapped |= 16;
115       if (ExpandedFloats.find(Res) != ExpandedFloats.end())
116         Mapped |= 32;
117       if (SplitVectors.find(Res) != SplitVectors.end())
118         Mapped |= 64;
119       if (WidenedVectors.find(Res) != WidenedVectors.end())
120         Mapped |= 128;
121
122       if (I->getNodeId() != Processed) {
123         // Since we allow ReplacedValues to map deleted nodes, it may map nodes
124         // marked NewNode too, since a deleted node may have been reallocated as
125         // another node that has not been seen by the LegalizeTypes machinery.
126         if ((I->getNodeId() == NewNode && Mapped > 1) ||
127             (I->getNodeId() != NewNode && Mapped != 0)) {
128           dbgs() << "Unprocessed value in a map!";
129           Failed = true;
130         }
131       } else if (isTypeLegal(Res.getValueType()) || IgnoreNodeResults(I)) {
132         if (Mapped > 1) {
133           dbgs() << "Value with legal type was transformed!";
134           Failed = true;
135         }
136       } else {
137         if (Mapped == 0) {
138           dbgs() << "Processed value not in any map!";
139           Failed = true;
140         } else if (Mapped & (Mapped - 1)) {
141           dbgs() << "Value in multiple maps!";
142           Failed = true;
143         }
144       }
145
146       if (Failed) {
147         if (Mapped & 1)
148           dbgs() << " ReplacedValues";
149         if (Mapped & 2)
150           dbgs() << " PromotedIntegers";
151         if (Mapped & 4)
152           dbgs() << " SoftenedFloats";
153         if (Mapped & 8)
154           dbgs() << " ScalarizedVectors";
155         if (Mapped & 16)
156           dbgs() << " ExpandedIntegers";
157         if (Mapped & 32)
158           dbgs() << " ExpandedFloats";
159         if (Mapped & 64)
160           dbgs() << " SplitVectors";
161         if (Mapped & 128)
162           dbgs() << " WidenedVectors";
163         dbgs() << "\n";
164         llvm_unreachable(nullptr);
165       }
166     }
167   }
168
169   // Checked that NewNodes are only used by other NewNodes.
170   for (unsigned i = 0, e = NewNodes.size(); i != e; ++i) {
171     SDNode *N = NewNodes[i];
172     for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
173          UI != UE; ++UI)
174       assert(UI->getNodeId() == NewNode && "NewNode used by non-NewNode!");
175   }
176 }
177
178 /// run - This is the main entry point for the type legalizer.  This does a
179 /// top-down traversal of the dag, legalizing types as it goes.  Returns "true"
180 /// if it made any changes.
181 bool DAGTypeLegalizer::run() {
182   bool Changed = false;
183
184   // Create a dummy node (which is not added to allnodes), that adds a reference
185   // to the root node, preventing it from being deleted, and tracking any
186   // changes of the root.
187   HandleSDNode Dummy(DAG.getRoot());
188   Dummy.setNodeId(Unanalyzed);
189
190   // The root of the dag may dangle to deleted nodes until the type legalizer is
191   // done.  Set it to null to avoid confusion.
192   DAG.setRoot(SDValue());
193
194   // Walk all nodes in the graph, assigning them a NodeId of 'ReadyToProcess'
195   // (and remembering them) if they are leaves and assigning 'Unanalyzed' if
196   // non-leaves.
197   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
198        E = DAG.allnodes_end(); I != E; ++I) {
199     if (I->getNumOperands() == 0) {
200       I->setNodeId(ReadyToProcess);
201       Worklist.push_back(I);
202     } else {
203       I->setNodeId(Unanalyzed);
204     }
205   }
206
207   // Now that we have a set of nodes to process, handle them all.
208   while (!Worklist.empty()) {
209 #ifndef XDEBUG
210     if (EnableExpensiveChecks)
211 #endif
212       PerformExpensiveChecks();
213
214     SDNode *N = Worklist.back();
215     Worklist.pop_back();
216     assert(N->getNodeId() == ReadyToProcess &&
217            "Node should be ready if on worklist!");
218
219     if (IgnoreNodeResults(N))
220       goto ScanOperands;
221
222     // Scan the values produced by the node, checking to see if any result
223     // types are illegal.
224     for (unsigned i = 0, NumResults = N->getNumValues(); i < NumResults; ++i) {
225       EVT ResultVT = N->getValueType(i);
226       switch (getTypeAction(ResultVT)) {
227       case TargetLowering::TypeLegal:
228         break;
229       // The following calls must take care of *all* of the node's results,
230       // not just the illegal result they were passed (this includes results
231       // with a legal type).  Results can be remapped using ReplaceValueWith,
232       // or their promoted/expanded/etc values registered in PromotedIntegers,
233       // ExpandedIntegers etc.
234       case TargetLowering::TypePromoteInteger:
235         PromoteIntegerResult(N, i);
236         Changed = true;
237         goto NodeDone;
238       case TargetLowering::TypeExpandInteger:
239         ExpandIntegerResult(N, i);
240         Changed = true;
241         goto NodeDone;
242       case TargetLowering::TypeSoftenFloat:
243         SoftenFloatResult(N, i);
244         Changed = true;
245         goto NodeDone;
246       case TargetLowering::TypeExpandFloat:
247         ExpandFloatResult(N, i);
248         Changed = true;
249         goto NodeDone;
250       case TargetLowering::TypeScalarizeVector:
251         ScalarizeVectorResult(N, i);
252         Changed = true;
253         goto NodeDone;
254       case TargetLowering::TypeSplitVector:
255         SplitVectorResult(N, i);
256         Changed = true;
257         goto NodeDone;
258       case TargetLowering::TypeWidenVector:
259         WidenVectorResult(N, i);
260         Changed = true;
261         goto NodeDone;
262       case TargetLowering::TypePromoteFloat:
263         PromoteFloatResult(N, i);
264         Changed = true;
265         goto NodeDone;
266       }
267     }
268
269 ScanOperands:
270     // Scan the operand list for the node, handling any nodes with operands that
271     // are illegal.
272     {
273     unsigned NumOperands = N->getNumOperands();
274     bool NeedsReanalyzing = false;
275     unsigned i;
276     for (i = 0; i != NumOperands; ++i) {
277       if (IgnoreNodeResults(N->getOperand(i).getNode()))
278         continue;
279
280       EVT OpVT = N->getOperand(i).getValueType();
281       switch (getTypeAction(OpVT)) {
282       case TargetLowering::TypeLegal:
283         continue;
284       // The following calls must either replace all of the node's results
285       // using ReplaceValueWith, and return "false"; or update the node's
286       // operands in place, and return "true".
287       case TargetLowering::TypePromoteInteger:
288         NeedsReanalyzing = PromoteIntegerOperand(N, i);
289         Changed = true;
290         break;
291       case TargetLowering::TypeExpandInteger:
292         NeedsReanalyzing = ExpandIntegerOperand(N, i);
293         Changed = true;
294         break;
295       case TargetLowering::TypeSoftenFloat:
296         NeedsReanalyzing = SoftenFloatOperand(N, i);
297         Changed = true;
298         break;
299       case TargetLowering::TypeExpandFloat:
300         NeedsReanalyzing = ExpandFloatOperand(N, i);
301         Changed = true;
302         break;
303       case TargetLowering::TypeScalarizeVector:
304         NeedsReanalyzing = ScalarizeVectorOperand(N, i);
305         Changed = true;
306         break;
307       case TargetLowering::TypeSplitVector:
308         NeedsReanalyzing = SplitVectorOperand(N, i);
309         Changed = true;
310         break;
311       case TargetLowering::TypeWidenVector:
312         NeedsReanalyzing = WidenVectorOperand(N, i);
313         Changed = true;
314         break;
315       case TargetLowering::TypePromoteFloat:
316         NeedsReanalyzing = PromoteFloatOperand(N, i);
317         Changed = true;
318         break;
319       }
320       break;
321     }
322
323     // The sub-method updated N in place.  Check to see if any operands are new,
324     // and if so, mark them.  If the node needs revisiting, don't add all users
325     // to the worklist etc.
326     if (NeedsReanalyzing) {
327       assert(N->getNodeId() == ReadyToProcess && "Node ID recalculated?");
328       N->setNodeId(NewNode);
329       // Recompute the NodeId and correct processed operands, adding the node to
330       // the worklist if ready.
331       SDNode *M = AnalyzeNewNode(N);
332       if (M == N)
333         // The node didn't morph - nothing special to do, it will be revisited.
334         continue;
335
336       // The node morphed - this is equivalent to legalizing by replacing every
337       // value of N with the corresponding value of M.  So do that now.
338       assert(N->getNumValues() == M->getNumValues() &&
339              "Node morphing changed the number of results!");
340       for (unsigned i = 0, e = N->getNumValues(); i != e; ++i)
341         // Replacing the value takes care of remapping the new value.
342         ReplaceValueWith(SDValue(N, i), SDValue(M, i));
343       assert(N->getNodeId() == NewNode && "Unexpected node state!");
344       // The node continues to live on as part of the NewNode fungus that
345       // grows on top of the useful nodes.  Nothing more needs to be done
346       // with it - move on to the next node.
347       continue;
348     }
349
350     if (i == NumOperands) {
351       DEBUG(dbgs() << "Legally typed node: "; N->dump(&DAG); dbgs() << "\n");
352     }
353     }
354 NodeDone:
355
356     // If we reach here, the node was processed, potentially creating new nodes.
357     // Mark it as processed and add its users to the worklist as appropriate.
358     assert(N->getNodeId() == ReadyToProcess && "Node ID recalculated?");
359     N->setNodeId(Processed);
360
361     for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
362          UI != E; ++UI) {
363       SDNode *User = *UI;
364       int NodeId = User->getNodeId();
365
366       // This node has two options: it can either be a new node or its Node ID
367       // may be a count of the number of operands it has that are not ready.
368       if (NodeId > 0) {
369         User->setNodeId(NodeId-1);
370
371         // If this was the last use it was waiting on, add it to the ready list.
372         if (NodeId-1 == ReadyToProcess)
373           Worklist.push_back(User);
374         continue;
375       }
376
377       // If this is an unreachable new node, then ignore it.  If it ever becomes
378       // reachable by being used by a newly created node then it will be handled
379       // by AnalyzeNewNode.
380       if (NodeId == NewNode)
381         continue;
382
383       // Otherwise, this node is new: this is the first operand of it that
384       // became ready.  Its new NodeId is the number of operands it has minus 1
385       // (as this node is now processed).
386       assert(NodeId == Unanalyzed && "Unknown node ID!");
387       User->setNodeId(User->getNumOperands() - 1);
388
389       // If the node only has a single operand, it is now ready.
390       if (User->getNumOperands() == 1)
391         Worklist.push_back(User);
392     }
393   }
394
395 #ifndef XDEBUG
396   if (EnableExpensiveChecks)
397 #endif
398     PerformExpensiveChecks();
399
400   // If the root changed (e.g. it was a dead load) update the root.
401   DAG.setRoot(Dummy.getValue());
402
403   // Remove dead nodes.  This is important to do for cleanliness but also before
404   // the checking loop below.  Implicit folding by the DAG.getNode operators and
405   // node morphing can cause unreachable nodes to be around with their flags set
406   // to new.
407   DAG.RemoveDeadNodes();
408
409   // In a debug build, scan all the nodes to make sure we found them all.  This
410   // ensures that there are no cycles and that everything got processed.
411 #ifndef NDEBUG
412   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
413        E = DAG.allnodes_end(); I != E; ++I) {
414     bool Failed = false;
415
416     // Check that all result types are legal.
417     if (!IgnoreNodeResults(I))
418       for (unsigned i = 0, NumVals = I->getNumValues(); i < NumVals; ++i)
419         if (!isTypeLegal(I->getValueType(i))) {
420           dbgs() << "Result type " << i << " illegal!\n";
421           Failed = true;
422         }
423
424     // Check that all operand types are legal.
425     for (unsigned i = 0, NumOps = I->getNumOperands(); i < NumOps; ++i)
426       if (!IgnoreNodeResults(I->getOperand(i).getNode()) &&
427           !isTypeLegal(I->getOperand(i).getValueType())) {
428         dbgs() << "Operand type " << i << " illegal!\n";
429         Failed = true;
430       }
431
432     if (I->getNodeId() != Processed) {
433        if (I->getNodeId() == NewNode)
434          dbgs() << "New node not analyzed?\n";
435        else if (I->getNodeId() == Unanalyzed)
436          dbgs() << "Unanalyzed node not noticed?\n";
437        else if (I->getNodeId() > 0)
438          dbgs() << "Operand not processed?\n";
439        else if (I->getNodeId() == ReadyToProcess)
440          dbgs() << "Not added to worklist?\n";
441        Failed = true;
442     }
443
444     if (Failed) {
445       I->dump(&DAG); dbgs() << "\n";
446       llvm_unreachable(nullptr);
447     }
448   }
449 #endif
450
451   return Changed;
452 }
453
454 /// AnalyzeNewNode - The specified node is the root of a subtree of potentially
455 /// new nodes.  Correct any processed operands (this may change the node) and
456 /// calculate the NodeId.  If the node itself changes to a processed node, it
457 /// is not remapped - the caller needs to take care of this.
458 /// Returns the potentially changed node.
459 SDNode *DAGTypeLegalizer::AnalyzeNewNode(SDNode *N) {
460   // If this was an existing node that is already done, we're done.
461   if (N->getNodeId() != NewNode && N->getNodeId() != Unanalyzed)
462     return N;
463
464   // Remove any stale map entries.
465   ExpungeNode(N);
466
467   // Okay, we know that this node is new.  Recursively walk all of its operands
468   // to see if they are new also.  The depth of this walk is bounded by the size
469   // of the new tree that was constructed (usually 2-3 nodes), so we don't worry
470   // about revisiting of nodes.
471   //
472   // As we walk the operands, keep track of the number of nodes that are
473   // processed.  If non-zero, this will become the new nodeid of this node.
474   // Operands may morph when they are analyzed.  If so, the node will be
475   // updated after all operands have been analyzed.  Since this is rare,
476   // the code tries to minimize overhead in the non-morphing case.
477
478   SmallVector<SDValue, 8> NewOps;
479   unsigned NumProcessed = 0;
480   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
481     SDValue OrigOp = N->getOperand(i);
482     SDValue Op = OrigOp;
483
484     AnalyzeNewValue(Op); // Op may morph.
485
486     if (Op.getNode()->getNodeId() == Processed)
487       ++NumProcessed;
488
489     if (!NewOps.empty()) {
490       // Some previous operand changed.  Add this one to the list.
491       NewOps.push_back(Op);
492     } else if (Op != OrigOp) {
493       // This is the first operand to change - add all operands so far.
494       NewOps.append(N->op_begin(), N->op_begin() + i);
495       NewOps.push_back(Op);
496     }
497   }
498
499   // Some operands changed - update the node.
500   if (!NewOps.empty()) {
501     SDNode *M = DAG.UpdateNodeOperands(N, NewOps);
502     if (M != N) {
503       // The node morphed into a different node.  Normally for this to happen
504       // the original node would have to be marked NewNode.  However this can
505       // in theory momentarily not be the case while ReplaceValueWith is doing
506       // its stuff.  Mark the original node NewNode to help sanity checking.
507       N->setNodeId(NewNode);
508       if (M->getNodeId() != NewNode && M->getNodeId() != Unanalyzed)
509         // It morphed into a previously analyzed node - nothing more to do.
510         return M;
511
512       // It morphed into a different new node.  Do the equivalent of passing
513       // it to AnalyzeNewNode: expunge it and calculate the NodeId.  No need
514       // to remap the operands, since they are the same as the operands we
515       // remapped above.
516       N = M;
517       ExpungeNode(N);
518     }
519   }
520
521   // Calculate the NodeId.
522   N->setNodeId(N->getNumOperands() - NumProcessed);
523   if (N->getNodeId() == ReadyToProcess)
524     Worklist.push_back(N);
525
526   return N;
527 }
528
529 /// AnalyzeNewValue - Call AnalyzeNewNode, updating the node in Val if needed.
530 /// If the node changes to a processed node, then remap it.
531 void DAGTypeLegalizer::AnalyzeNewValue(SDValue &Val) {
532   Val.setNode(AnalyzeNewNode(Val.getNode()));
533   if (Val.getNode()->getNodeId() == Processed)
534     // We were passed a processed node, or it morphed into one - remap it.
535     RemapValue(Val);
536 }
537
538 /// ExpungeNode - If N has a bogus mapping in ReplacedValues, eliminate it.
539 /// This can occur when a node is deleted then reallocated as a new node -
540 /// the mapping in ReplacedValues applies to the deleted node, not the new
541 /// one.
542 /// The only map that can have a deleted node as a source is ReplacedValues.
543 /// Other maps can have deleted nodes as targets, but since their looked-up
544 /// values are always immediately remapped using RemapValue, resulting in a
545 /// not-deleted node, this is harmless as long as ReplacedValues/RemapValue
546 /// always performs correct mappings.  In order to keep the mapping correct,
547 /// ExpungeNode should be called on any new nodes *before* adding them as
548 /// either source or target to ReplacedValues (which typically means calling
549 /// Expunge when a new node is first seen, since it may no longer be marked
550 /// NewNode by the time it is added to ReplacedValues).
551 void DAGTypeLegalizer::ExpungeNode(SDNode *N) {
552   if (N->getNodeId() != NewNode)
553     return;
554
555   // If N is not remapped by ReplacedValues then there is nothing to do.
556   unsigned i, e;
557   for (i = 0, e = N->getNumValues(); i != e; ++i)
558     if (ReplacedValues.find(SDValue(N, i)) != ReplacedValues.end())
559       break;
560
561   if (i == e)
562     return;
563
564   // Remove N from all maps - this is expensive but rare.
565
566   for (DenseMap<SDValue, SDValue>::iterator I = PromotedIntegers.begin(),
567        E = PromotedIntegers.end(); I != E; ++I) {
568     assert(I->first.getNode() != N);
569     RemapValue(I->second);
570   }
571
572   for (DenseMap<SDValue, SDValue>::iterator I = SoftenedFloats.begin(),
573        E = SoftenedFloats.end(); I != E; ++I) {
574     assert(I->first.getNode() != N);
575     RemapValue(I->second);
576   }
577
578   for (DenseMap<SDValue, SDValue>::iterator I = ScalarizedVectors.begin(),
579        E = ScalarizedVectors.end(); I != E; ++I) {
580     assert(I->first.getNode() != N);
581     RemapValue(I->second);
582   }
583
584   for (DenseMap<SDValue, SDValue>::iterator I = WidenedVectors.begin(),
585        E = WidenedVectors.end(); I != E; ++I) {
586     assert(I->first.getNode() != N);
587     RemapValue(I->second);
588   }
589
590   for (DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator
591        I = ExpandedIntegers.begin(), E = ExpandedIntegers.end(); I != E; ++I){
592     assert(I->first.getNode() != N);
593     RemapValue(I->second.first);
594     RemapValue(I->second.second);
595   }
596
597   for (DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator
598        I = ExpandedFloats.begin(), E = ExpandedFloats.end(); I != E; ++I) {
599     assert(I->first.getNode() != N);
600     RemapValue(I->second.first);
601     RemapValue(I->second.second);
602   }
603
604   for (DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator
605        I = SplitVectors.begin(), E = SplitVectors.end(); I != E; ++I) {
606     assert(I->first.getNode() != N);
607     RemapValue(I->second.first);
608     RemapValue(I->second.second);
609   }
610
611   for (DenseMap<SDValue, SDValue>::iterator I = ReplacedValues.begin(),
612        E = ReplacedValues.end(); I != E; ++I)
613     RemapValue(I->second);
614
615   for (unsigned i = 0, e = N->getNumValues(); i != e; ++i)
616     ReplacedValues.erase(SDValue(N, i));
617 }
618
619 /// RemapValue - If the specified value was already legalized to another value,
620 /// replace it by that value.
621 void DAGTypeLegalizer::RemapValue(SDValue &N) {
622   DenseMap<SDValue, SDValue>::iterator I = ReplacedValues.find(N);
623   if (I != ReplacedValues.end()) {
624     // Use path compression to speed up future lookups if values get multiply
625     // replaced with other values.
626     RemapValue(I->second);
627     N = I->second;
628
629     // Note that it is possible to have N.getNode()->getNodeId() == NewNode at
630     // this point because it is possible for a node to be put in the map before
631     // being processed.
632   }
633 }
634
635 namespace {
636   /// NodeUpdateListener - This class is a DAGUpdateListener that listens for
637   /// updates to nodes and recomputes their ready state.
638   class NodeUpdateListener : public SelectionDAG::DAGUpdateListener {
639     DAGTypeLegalizer &DTL;
640     SmallSetVector<SDNode*, 16> &NodesToAnalyze;
641   public:
642     explicit NodeUpdateListener(DAGTypeLegalizer &dtl,
643                                 SmallSetVector<SDNode*, 16> &nta)
644       : SelectionDAG::DAGUpdateListener(dtl.getDAG()),
645         DTL(dtl), NodesToAnalyze(nta) {}
646
647     void NodeDeleted(SDNode *N, SDNode *E) override {
648       assert(N->getNodeId() != DAGTypeLegalizer::ReadyToProcess &&
649              N->getNodeId() != DAGTypeLegalizer::Processed &&
650              "Invalid node ID for RAUW deletion!");
651       // It is possible, though rare, for the deleted node N to occur as a
652       // target in a map, so note the replacement N -> E in ReplacedValues.
653       assert(E && "Node not replaced?");
654       DTL.NoteDeletion(N, E);
655
656       // In theory the deleted node could also have been scheduled for analysis.
657       // So remove it from the set of nodes which will be analyzed.
658       NodesToAnalyze.remove(N);
659
660       // In general nothing needs to be done for E, since it didn't change but
661       // only gained new uses.  However N -> E was just added to ReplacedValues,
662       // and the result of a ReplacedValues mapping is not allowed to be marked
663       // NewNode.  So if E is marked NewNode, then it needs to be analyzed.
664       if (E->getNodeId() == DAGTypeLegalizer::NewNode)
665         NodesToAnalyze.insert(E);
666     }
667
668     void NodeUpdated(SDNode *N) override {
669       // Node updates can mean pretty much anything.  It is possible that an
670       // operand was set to something already processed (f.e.) in which case
671       // this node could become ready.  Recompute its flags.
672       assert(N->getNodeId() != DAGTypeLegalizer::ReadyToProcess &&
673              N->getNodeId() != DAGTypeLegalizer::Processed &&
674              "Invalid node ID for RAUW deletion!");
675       N->setNodeId(DAGTypeLegalizer::NewNode);
676       NodesToAnalyze.insert(N);
677     }
678   };
679 }
680
681
682 /// ReplaceValueWith - The specified value was legalized to the specified other
683 /// value.  Update the DAG and NodeIds replacing any uses of From to use To
684 /// instead.
685 void DAGTypeLegalizer::ReplaceValueWith(SDValue From, SDValue To) {
686   assert(From.getNode() != To.getNode() && "Potential legalization loop!");
687
688   // If expansion produced new nodes, make sure they are properly marked.
689   ExpungeNode(From.getNode());
690   AnalyzeNewValue(To); // Expunges To.
691
692   // Anything that used the old node should now use the new one.  Note that this
693   // can potentially cause recursive merging.
694   SmallSetVector<SDNode*, 16> NodesToAnalyze;
695   NodeUpdateListener NUL(*this, NodesToAnalyze);
696   do {
697     DAG.ReplaceAllUsesOfValueWith(From, To);
698
699     // The old node may still be present in a map like ExpandedIntegers or
700     // PromotedIntegers.  Inform maps about the replacement.
701     ReplacedValues[From] = To;
702
703     // Process the list of nodes that need to be reanalyzed.
704     while (!NodesToAnalyze.empty()) {
705       SDNode *N = NodesToAnalyze.back();
706       NodesToAnalyze.pop_back();
707       if (N->getNodeId() != DAGTypeLegalizer::NewNode)
708         // The node was analyzed while reanalyzing an earlier node - it is safe
709         // to skip.  Note that this is not a morphing node - otherwise it would
710         // still be marked NewNode.
711         continue;
712
713       // Analyze the node's operands and recalculate the node ID.
714       SDNode *M = AnalyzeNewNode(N);
715       if (M != N) {
716         // The node morphed into a different node.  Make everyone use the new
717         // node instead.
718         assert(M->getNodeId() != NewNode && "Analysis resulted in NewNode!");
719         assert(N->getNumValues() == M->getNumValues() &&
720                "Node morphing changed the number of results!");
721         for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
722           SDValue OldVal(N, i);
723           SDValue NewVal(M, i);
724           if (M->getNodeId() == Processed)
725             RemapValue(NewVal);
726           DAG.ReplaceAllUsesOfValueWith(OldVal, NewVal);
727           // OldVal may be a target of the ReplacedValues map which was marked
728           // NewNode to force reanalysis because it was updated.  Ensure that
729           // anything that ReplacedValues mapped to OldVal will now be mapped
730           // all the way to NewVal.
731           ReplacedValues[OldVal] = NewVal;
732         }
733         // The original node continues to exist in the DAG, marked NewNode.
734       }
735     }
736     // When recursively update nodes with new nodes, it is possible to have
737     // new uses of From due to CSE. If this happens, replace the new uses of
738     // From with To.
739   } while (!From.use_empty());
740 }
741
742 void DAGTypeLegalizer::SetPromotedInteger(SDValue Op, SDValue Result) {
743   assert(Result.getValueType() ==
744          TLI.getTypeToTransformTo(*DAG.getContext(), Op.getValueType()) &&
745          "Invalid type for promoted integer");
746   AnalyzeNewValue(Result);
747
748   SDValue &OpEntry = PromotedIntegers[Op];
749   assert(!OpEntry.getNode() && "Node is already promoted!");
750   OpEntry = Result;
751 }
752
753 void DAGTypeLegalizer::SetSoftenedFloat(SDValue Op, SDValue Result) {
754   assert(Result.getValueType() ==
755          TLI.getTypeToTransformTo(*DAG.getContext(), Op.getValueType()) &&
756          "Invalid type for softened float");
757   AnalyzeNewValue(Result);
758
759   SDValue &OpEntry = SoftenedFloats[Op];
760   assert(!OpEntry.getNode() && "Node is already converted to integer!");
761   OpEntry = Result;
762 }
763
764 void DAGTypeLegalizer::SetPromotedFloat(SDValue Op, SDValue Result) {
765   assert(Result.getValueType() ==
766          TLI.getTypeToTransformTo(*DAG.getContext(), Op.getValueType()) &&
767          "Invalid type for promoted float");
768   AnalyzeNewValue(Result);
769
770   SDValue &OpEntry = PromotedFloats[Op];
771   assert(!OpEntry.getNode() && "Node is already promoted!");
772   OpEntry = Result;
773 }
774
775 void DAGTypeLegalizer::SetScalarizedVector(SDValue Op, SDValue Result) {
776   // Note that in some cases vector operation operands may be greater than
777   // the vector element type. For example BUILD_VECTOR of type <1 x i1> with
778   // a constant i8 operand.
779   assert(Result.getValueType().getSizeInBits() >=
780          Op.getValueType().getVectorElementType().getSizeInBits() &&
781          "Invalid type for scalarized vector");
782   AnalyzeNewValue(Result);
783
784   SDValue &OpEntry = ScalarizedVectors[Op];
785   assert(!OpEntry.getNode() && "Node is already scalarized!");
786   OpEntry = Result;
787 }
788
789 void DAGTypeLegalizer::GetExpandedInteger(SDValue Op, SDValue &Lo,
790                                           SDValue &Hi) {
791   std::pair<SDValue, SDValue> &Entry = ExpandedIntegers[Op];
792   RemapValue(Entry.first);
793   RemapValue(Entry.second);
794   assert(Entry.first.getNode() && "Operand isn't expanded");
795   Lo = Entry.first;
796   Hi = Entry.second;
797 }
798
799 void DAGTypeLegalizer::SetExpandedInteger(SDValue Op, SDValue Lo,
800                                           SDValue Hi) {
801   assert(Lo.getValueType() ==
802          TLI.getTypeToTransformTo(*DAG.getContext(), Op.getValueType()) &&
803          Hi.getValueType() == Lo.getValueType() &&
804          "Invalid type for expanded integer");
805   // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant.
806   AnalyzeNewValue(Lo);
807   AnalyzeNewValue(Hi);
808
809   // Remember that this is the result of the node.
810   std::pair<SDValue, SDValue> &Entry = ExpandedIntegers[Op];
811   assert(!Entry.first.getNode() && "Node already expanded");
812   Entry.first = Lo;
813   Entry.second = Hi;
814 }
815
816 void DAGTypeLegalizer::GetExpandedFloat(SDValue Op, SDValue &Lo,
817                                         SDValue &Hi) {
818   std::pair<SDValue, SDValue> &Entry = ExpandedFloats[Op];
819   RemapValue(Entry.first);
820   RemapValue(Entry.second);
821   assert(Entry.first.getNode() && "Operand isn't expanded");
822   Lo = Entry.first;
823   Hi = Entry.second;
824 }
825
826 void DAGTypeLegalizer::SetExpandedFloat(SDValue Op, SDValue Lo,
827                                         SDValue Hi) {
828   assert(Lo.getValueType() ==
829          TLI.getTypeToTransformTo(*DAG.getContext(), Op.getValueType()) &&
830          Hi.getValueType() == Lo.getValueType() &&
831          "Invalid type for expanded float");
832   // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant.
833   AnalyzeNewValue(Lo);
834   AnalyzeNewValue(Hi);
835
836   // Remember that this is the result of the node.
837   std::pair<SDValue, SDValue> &Entry = ExpandedFloats[Op];
838   assert(!Entry.first.getNode() && "Node already expanded");
839   Entry.first = Lo;
840   Entry.second = Hi;
841 }
842
843 void DAGTypeLegalizer::GetSplitVector(SDValue Op, SDValue &Lo,
844                                       SDValue &Hi) {
845   std::pair<SDValue, SDValue> &Entry = SplitVectors[Op];
846   RemapValue(Entry.first);
847   RemapValue(Entry.second);
848   assert(Entry.first.getNode() && "Operand isn't split");
849   Lo = Entry.first;
850   Hi = Entry.second;
851 }
852
853 void DAGTypeLegalizer::SetSplitVector(SDValue Op, SDValue Lo,
854                                       SDValue Hi) {
855   assert(Lo.getValueType().getVectorElementType() ==
856          Op.getValueType().getVectorElementType() &&
857          2*Lo.getValueType().getVectorNumElements() ==
858          Op.getValueType().getVectorNumElements() &&
859          Hi.getValueType() == Lo.getValueType() &&
860          "Invalid type for split vector");
861   // Lo/Hi may have been newly allocated, if so, add nodeid's as relevant.
862   AnalyzeNewValue(Lo);
863   AnalyzeNewValue(Hi);
864
865   // Remember that this is the result of the node.
866   std::pair<SDValue, SDValue> &Entry = SplitVectors[Op];
867   assert(!Entry.first.getNode() && "Node already split");
868   Entry.first = Lo;
869   Entry.second = Hi;
870 }
871
872 void DAGTypeLegalizer::SetWidenedVector(SDValue Op, SDValue Result) {
873   assert(Result.getValueType() ==
874          TLI.getTypeToTransformTo(*DAG.getContext(), Op.getValueType()) &&
875          "Invalid type for widened vector");
876   AnalyzeNewValue(Result);
877
878   SDValue &OpEntry = WidenedVectors[Op];
879   assert(!OpEntry.getNode() && "Node already widened!");
880   OpEntry = Result;
881 }
882
883
884 //===----------------------------------------------------------------------===//
885 // Utilities.
886 //===----------------------------------------------------------------------===//
887
888 /// BitConvertToInteger - Convert to an integer of the same size.
889 SDValue DAGTypeLegalizer::BitConvertToInteger(SDValue Op) {
890   unsigned BitWidth = Op.getValueType().getSizeInBits();
891   return DAG.getNode(ISD::BITCAST, SDLoc(Op),
892                      EVT::getIntegerVT(*DAG.getContext(), BitWidth), Op);
893 }
894
895 /// BitConvertVectorToIntegerVector - Convert to a vector of integers of the
896 /// same size.
897 SDValue DAGTypeLegalizer::BitConvertVectorToIntegerVector(SDValue Op) {
898   assert(Op.getValueType().isVector() && "Only applies to vectors!");
899   unsigned EltWidth = Op.getValueType().getVectorElementType().getSizeInBits();
900   EVT EltNVT = EVT::getIntegerVT(*DAG.getContext(), EltWidth);
901   unsigned NumElts = Op.getValueType().getVectorNumElements();
902   return DAG.getNode(ISD::BITCAST, SDLoc(Op),
903                      EVT::getVectorVT(*DAG.getContext(), EltNVT, NumElts), Op);
904 }
905
906 SDValue DAGTypeLegalizer::CreateStackStoreLoad(SDValue Op,
907                                                EVT DestVT) {
908   SDLoc dl(Op);
909   // Create the stack frame object.  Make sure it is aligned for both
910   // the source and destination types.
911   SDValue StackPtr = DAG.CreateStackTemporary(Op.getValueType(), DestVT);
912   // Emit a store to the stack slot.
913   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op, StackPtr,
914                                MachinePointerInfo(), false, false, 0);
915   // Result is a load from the stack slot.
916   return DAG.getLoad(DestVT, dl, Store, StackPtr, MachinePointerInfo(),
917                      false, false, false, 0);
918 }
919
920 /// CustomLowerNode - Replace the node's results with custom code provided
921 /// by the target and return "true", or do nothing and return "false".
922 /// The last parameter is FALSE if we are dealing with a node with legal
923 /// result types and illegal operand. The second parameter denotes the type of
924 /// illegal OperandNo in that case.
925 /// The last parameter being TRUE means we are dealing with a
926 /// node with illegal result types. The second parameter denotes the type of
927 /// illegal ResNo in that case.
928 bool DAGTypeLegalizer::CustomLowerNode(SDNode *N, EVT VT, bool LegalizeResult) {
929   // See if the target wants to custom lower this node.
930   if (TLI.getOperationAction(N->getOpcode(), VT) != TargetLowering::Custom)
931     return false;
932
933   SmallVector<SDValue, 8> Results;
934   if (LegalizeResult)
935     TLI.ReplaceNodeResults(N, Results, DAG);
936   else
937     TLI.LowerOperationWrapper(N, Results, DAG);
938
939   if (Results.empty())
940     // The target didn't want to custom lower it after all.
941     return false;
942
943   // When called from DAGTypeLegalizer::ExpandIntegerResult, we might need to
944   // provide the same kind of custom splitting behavior.
945   if (Results.size() == N->getNumValues() + 1 && LegalizeResult) {
946     // We've legalized a return type by splitting it. If there is a chain,
947     // replace that too.
948     SetExpandedInteger(SDValue(N, 0), Results[0], Results[1]);
949     if (N->getNumValues() > 1)
950       ReplaceValueWith(SDValue(N, 1), Results[2]);
951     return true;
952   }
953
954   // Make everything that once used N's values now use those in Results instead.
955   assert(Results.size() == N->getNumValues() &&
956          "Custom lowering returned the wrong number of results!");
957   for (unsigned i = 0, e = Results.size(); i != e; ++i) {
958     ReplaceValueWith(SDValue(N, i), Results[i]);
959   }
960   return true;
961 }
962
963
964 /// CustomWidenLowerNode - Widen the node's results with custom code provided
965 /// by the target and return "true", or do nothing and return "false".
966 bool DAGTypeLegalizer::CustomWidenLowerNode(SDNode *N, EVT VT) {
967   // See if the target wants to custom lower this node.
968   if (TLI.getOperationAction(N->getOpcode(), VT) != TargetLowering::Custom)
969     return false;
970
971   SmallVector<SDValue, 8> Results;
972   TLI.ReplaceNodeResults(N, Results, DAG);
973
974   if (Results.empty())
975     // The target didn't want to custom widen lower its result  after all.
976     return false;
977
978   // Update the widening map.
979   assert(Results.size() == N->getNumValues() &&
980          "Custom lowering returned the wrong number of results!");
981   for (unsigned i = 0, e = Results.size(); i != e; ++i)
982     SetWidenedVector(SDValue(N, i), Results[i]);
983   return true;
984 }
985
986 SDValue DAGTypeLegalizer::DisintegrateMERGE_VALUES(SDNode *N, unsigned ResNo) {
987   for (unsigned i = 0, e = N->getNumValues(); i != e; ++i)
988     if (i != ResNo)
989       ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
990   return SDValue(N->getOperand(ResNo));
991 }
992
993 /// GetPairElements - Use ISD::EXTRACT_ELEMENT nodes to extract the low and
994 /// high parts of the given value.
995 void DAGTypeLegalizer::GetPairElements(SDValue Pair,
996                                        SDValue &Lo, SDValue &Hi) {
997   SDLoc dl(Pair);
998   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), Pair.getValueType());
999   Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NVT, Pair,
1000                    DAG.getIntPtrConstant(0, dl));
1001   Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NVT, Pair,
1002                    DAG.getIntPtrConstant(1, dl));
1003 }
1004
1005 SDValue DAGTypeLegalizer::GetVectorElementPointer(SDValue VecPtr, EVT EltVT,
1006                                                   SDValue Index) {
1007   SDLoc dl(Index);
1008   // Make sure the index type is big enough to compute in.
1009   Index = DAG.getZExtOrTrunc(Index, dl, TLI.getPointerTy());
1010
1011   // Calculate the element offset and add it to the pointer.
1012   unsigned EltSize = EltVT.getSizeInBits() / 8; // FIXME: should be ABI size.
1013
1014   Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
1015                       DAG.getConstant(EltSize, dl, Index.getValueType()));
1016   return DAG.getNode(ISD::ADD, dl, Index.getValueType(), Index, VecPtr);
1017 }
1018
1019 /// JoinIntegers - Build an integer with low bits Lo and high bits Hi.
1020 SDValue DAGTypeLegalizer::JoinIntegers(SDValue Lo, SDValue Hi) {
1021   // Arbitrarily use dlHi for result SDLoc
1022   SDLoc dlHi(Hi);
1023   SDLoc dlLo(Lo);
1024   EVT LVT = Lo.getValueType();
1025   EVT HVT = Hi.getValueType();
1026   EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1027                               LVT.getSizeInBits() + HVT.getSizeInBits());
1028
1029   Lo = DAG.getNode(ISD::ZERO_EXTEND, dlLo, NVT, Lo);
1030   Hi = DAG.getNode(ISD::ANY_EXTEND, dlHi, NVT, Hi);
1031   Hi = DAG.getNode(ISD::SHL, dlHi, NVT, Hi,
1032                    DAG.getConstant(LVT.getSizeInBits(), dlHi,
1033                                    TLI.getPointerTy()));
1034   return DAG.getNode(ISD::OR, dlHi, NVT, Lo, Hi);
1035 }
1036
1037 /// LibCallify - Convert the node into a libcall with the same prototype.
1038 SDValue DAGTypeLegalizer::LibCallify(RTLIB::Libcall LC, SDNode *N,
1039                                      bool isSigned) {
1040   unsigned NumOps = N->getNumOperands();
1041   SDLoc dl(N);
1042   if (NumOps == 0) {
1043     return TLI.makeLibCall(DAG, LC, N->getValueType(0), nullptr, 0, isSigned,
1044                            dl).first;
1045   } else if (NumOps == 1) {
1046     SDValue Op = N->getOperand(0);
1047     return TLI.makeLibCall(DAG, LC, N->getValueType(0), &Op, 1, isSigned,
1048                            dl).first;
1049   } else if (NumOps == 2) {
1050     SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1051     return TLI.makeLibCall(DAG, LC, N->getValueType(0), Ops, 2, isSigned,
1052                            dl).first;
1053   }
1054   SmallVector<SDValue, 8> Ops(NumOps);
1055   for (unsigned i = 0; i < NumOps; ++i)
1056     Ops[i] = N->getOperand(i);
1057
1058   return TLI.makeLibCall(DAG, LC, N->getValueType(0),
1059                          &Ops[0], NumOps, isSigned, dl).first;
1060 }
1061
1062 // ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
1063 // ExpandLibCall except that the first operand is the in-chain.
1064 std::pair<SDValue, SDValue>
1065 DAGTypeLegalizer::ExpandChainLibCall(RTLIB::Libcall LC,
1066                                          SDNode *Node,
1067                                          bool isSigned) {
1068   SDValue InChain = Node->getOperand(0);
1069
1070   TargetLowering::ArgListTy Args;
1071   TargetLowering::ArgListEntry Entry;
1072   for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
1073     EVT ArgVT = Node->getOperand(i).getValueType();
1074     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1075     Entry.Node = Node->getOperand(i);
1076     Entry.Ty = ArgTy;
1077     Entry.isSExt = isSigned;
1078     Entry.isZExt = !isSigned;
1079     Args.push_back(Entry);
1080   }
1081   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1082                                          TLI.getPointerTy());
1083
1084   Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
1085
1086   TargetLowering::CallLoweringInfo CLI(DAG);
1087   CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
1088     .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
1089     .setSExtResult(isSigned).setZExtResult(!isSigned);
1090
1091   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
1092
1093   return CallInfo;
1094 }
1095
1096 /// PromoteTargetBoolean - Promote the given target boolean to a target boolean
1097 /// of the given type.  A target boolean is an integer value, not necessarily of
1098 /// type i1, the bits of which conform to getBooleanContents.
1099 ///
1100 /// ValVT is the type of values that produced the boolean.
1101 SDValue DAGTypeLegalizer::PromoteTargetBoolean(SDValue Bool, EVT ValVT) {
1102   SDLoc dl(Bool);
1103   EVT BoolVT = getSetCCResultType(ValVT);
1104   ISD::NodeType ExtendCode =
1105       TargetLowering::getExtendForContent(TLI.getBooleanContents(ValVT));
1106   return DAG.getNode(ExtendCode, dl, BoolVT, Bool);
1107 }
1108
1109 /// SplitInteger - Return the lower LoVT bits of Op in Lo and the upper HiVT
1110 /// bits in Hi.
1111 void DAGTypeLegalizer::SplitInteger(SDValue Op,
1112                                     EVT LoVT, EVT HiVT,
1113                                     SDValue &Lo, SDValue &Hi) {
1114   SDLoc dl(Op);
1115   assert(LoVT.getSizeInBits() + HiVT.getSizeInBits() ==
1116          Op.getValueType().getSizeInBits() && "Invalid integer splitting!");
1117   Lo = DAG.getNode(ISD::TRUNCATE, dl, LoVT, Op);
1118   Hi = DAG.getNode(ISD::SRL, dl, Op.getValueType(), Op,
1119                    DAG.getConstant(LoVT.getSizeInBits(), dl,
1120                                    TLI.getPointerTy()));
1121   Hi = DAG.getNode(ISD::TRUNCATE, dl, HiVT, Hi);
1122 }
1123
1124 /// SplitInteger - Return the lower and upper halves of Op's bits in a value
1125 /// type half the size of Op's.
1126 void DAGTypeLegalizer::SplitInteger(SDValue Op,
1127                                     SDValue &Lo, SDValue &Hi) {
1128   EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(),
1129                                  Op.getValueType().getSizeInBits()/2);
1130   SplitInteger(Op, HalfVT, HalfVT, Lo, Hi);
1131 }
1132
1133
1134 //===----------------------------------------------------------------------===//
1135 //  Entry Point
1136 //===----------------------------------------------------------------------===//
1137
1138 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
1139 /// only uses types natively supported by the target.  Returns "true" if it made
1140 /// any changes.
1141 ///
1142 /// Note that this is an involved process that may invalidate pointers into
1143 /// the graph.
1144 bool SelectionDAG::LegalizeTypes() {
1145   return DAGTypeLegalizer(*this).run();
1146 }