Eliminate the ISel priority queue, which used the topological order for a
[oota-llvm.git] / include / llvm / CodeGen / DAGISelHeader.h
1 //==-llvm/CodeGen/DAGISelHeader.h - Common DAG ISel definitions  -*- C++ -*-==//
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 provides definitions of the common, target-independent methods and 
11 // data, which is used by SelectionDAG-based instruction selectors.
12 //
13 // *** NOTE: This file is #included into the middle of the target
14 // instruction selector class.  These functions are really methods.
15 // This is a little awkward, but it allows this code to be shared
16 // by all the targets while still being able to call into
17 // target-specific code without using a virtual function call.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_CODEGEN_DAGISEL_HEADER_H
22 #define LLVM_CODEGEN_DAGISEL_HEADER_H
23
24 /// ISelPosition - Node iterator marking the current position of
25 /// instruction selection as it procedes through the topologically-sorted
26 /// node list.
27 SelectionDAG::allnodes_iterator ISelPosition;
28
29 /// IsChainCompatible - Returns true if Chain is Op or Chain does
30 /// not reach Op.
31 static bool IsChainCompatible(SDNode *Chain, SDNode *Op) {
32   if (Chain->getOpcode() == ISD::EntryToken)
33     return true;
34   else if (Chain->getOpcode() == ISD::TokenFactor)
35     return false;
36   else if (Chain->getNumOperands() > 0) {
37     SDValue C0 = Chain->getOperand(0);
38     if (C0.getValueType() == MVT::Other)
39       return C0.getNode() != Op && IsChainCompatible(C0.getNode(), Op);
40   }
41   return true;
42 }
43
44 /// ISelUpdater - helper class to handle updates of the 
45 /// instruciton selection graph.
46 class VISIBILITY_HIDDEN ISelUpdater : public SelectionDAG::DAGUpdateListener {
47   SelectionDAG::allnodes_iterator &ISelPosition;
48   bool HadDelete; // Indicate if any deletions were done.
49 public:
50   explicit ISelUpdater(SelectionDAG::allnodes_iterator &isp)
51     : ISelPosition(isp) {}
52   
53   /// NodeDeleted - remove node from the selection queue.
54   virtual void NodeDeleted(SDNode *N, SDNode *E) {
55     if (ISelPosition == SelectionDAG::allnodes_iterator(N))
56       ++ISelPosition;
57   }
58
59   /// NodeUpdated - Ignore updates for now.
60   virtual void NodeUpdated(SDNode *N) {}
61 };
62
63 /// ReplaceUses - replace all uses of the old node F with the use
64 /// of the new node T.
65 void ReplaceUses(SDValue F, SDValue T) DISABLE_INLINE {
66   ISelUpdater ISU(ISelPosition);
67   CurDAG->ReplaceAllUsesOfValueWith(F, T, &ISU);
68 }
69
70 /// ReplaceUses - replace all uses of the old nodes F with the use
71 /// of the new nodes T.
72 void ReplaceUses(const SDValue *F, const SDValue *T,
73                  unsigned Num) DISABLE_INLINE {
74   ISelUpdater ISU(ISelPosition);
75   CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num, &ISU);
76 }
77
78 /// ReplaceUses - replace all uses of the old node F with the use
79 /// of the new node T.
80 void ReplaceUses(SDNode *F, SDNode *T) DISABLE_INLINE {
81   unsigned FNumVals = F->getNumValues();
82   unsigned TNumVals = T->getNumValues();
83   ISelUpdater ISU(ISelPosition);
84   if (FNumVals != TNumVals) {
85     for (unsigned i = 0, e = std::min(FNumVals, TNumVals); i < e; ++i)
86      CurDAG->ReplaceAllUsesOfValueWith(SDValue(F, i), SDValue(T, i), &ISU);
87   } else {
88     CurDAG->ReplaceAllUsesWith(F, T, &ISU);
89   }
90 }
91
92 /// SelectRoot - Top level entry to DAG instruction selector.
93 /// Selects instructions starting at the root of the current DAG.
94 void SelectRoot(SelectionDAG &DAG) {
95   SelectRootInit();
96
97   // Create a dummy node (which is not added to allnodes), that adds
98   // a reference to the root node, preventing it from being deleted,
99   // and tracking any changes of the root.
100   HandleSDNode Dummy(CurDAG->getRoot());
101   ISelPosition = next(SelectionDAG::allnodes_iterator(CurDAG->getRoot().getNode()));
102
103   // Select pending nodes from the instruction selection queue
104   // until no more nodes are left for selection.
105   while (ISelPosition != CurDAG->allnodes_begin()) {
106     SDNode *Node = --ISelPosition;
107 #if 0
108     DAG.setSubgraphColor(Node, "red");
109 #endif
110     SDNode *ResNode = Select(SDValue(Node, 0));
111     // If node should not be replaced, 
112     // continue with the next one.
113     if (ResNode == Node)
114       continue;
115     // Replace node.
116     if (ResNode) {
117 #if 0
118       DAG.setSubgraphColor(ResNode, "yellow");
119       DAG.setSubgraphColor(ResNode, "black");
120 #endif
121       ReplaceUses(Node, ResNode);
122     }
123     // If after the replacement this node is not used any more,
124     // remove this dead node.
125     if (Node->use_empty()) { // Don't delete EntryToken, etc.
126       ISelUpdater ISU(ISelPosition);
127       CurDAG->RemoveDeadNode(Node, &ISU);
128     }
129   }
130
131   CurDAG->setRoot(Dummy.getValue());
132 }
133
134 #endif /* LLVM_CODEGEN_DAGISEL_HEADER_H */