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