1 //===-- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the SelectionDAG class, and transitively defines the
11 // SDNode class and subclasses.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_SELECTIONDAG_H
16 #define LLVM_CODEGEN_SELECTIONDAG_H
18 #include "llvm/CodeGen/SelectionDAGNodes.h"
20 #include <string> // FIXME remove eventually, turning map into const char* map.
25 class MachineFunction;
27 /// SelectionDAG class - This is used to represent a portion of an LLVM function
28 /// in a low-level Data Dependence DAG representation suitable for instruction
29 /// selection. This DAG is constructed as the first step of instruction
30 /// selection in order to allow implementation of machine specific optimizations
31 /// and code simplifications.
33 /// The representation used by the SelectionDAG is a target-independent
34 /// representation, which has some similarities to the GCC RTL representation,
35 /// but is significantly more simple, powerful, and is a graph form instead of a
42 // Root - The root of the entire DAG. EntryNode - The starting token.
43 SDOperand Root, EntryNode;
45 // AllNodes - All of the nodes in the DAG
46 std::vector<SDNode*> AllNodes;
48 SelectionDAG(TargetLowering &tli, MachineFunction &mf) : TLI(tli), MF(mf) {
49 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
53 MachineFunction &getMachineFunction() const { return MF; }
54 const TargetMachine &getTarget() const;
55 TargetLowering &getTargetLoweringInfo() const { return TLI; }
57 /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
62 typedef std::vector<SDNode*>::const_iterator allnodes_iterator;
63 allnodes_iterator allnodes_begin() const { return AllNodes.begin(); }
64 allnodes_iterator allnodes_end() const { return AllNodes.end(); }
66 /// getRoot - Return the root tag of the SelectionDAG.
68 const SDOperand &getRoot() const { return Root; }
70 /// getEntryNode - Return the token chain corresponding to the entry of the
72 const SDOperand &getEntryNode() const { return EntryNode; }
74 /// setRoot - Set the current root tag of the SelectionDAG.
76 const SDOperand &setRoot(SDOperand N) { return Root = N; }
78 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
79 /// compatible with the target instruction selector, as indicated by the
80 /// TargetLowering object.
82 /// Note that this is an involved process that may invalidate pointers into
86 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
87 /// SelectionDAG, including nodes (like loads) that have uses of their token
88 /// chain but no other uses and no side effect. If a node is passed in as an
89 /// argument, it is used as the seed for node deletion.
90 void RemoveDeadNodes(SDNode *N = 0);
92 SDOperand getConstant(uint64_t Val, MVT::ValueType VT);
93 SDOperand getConstantFP(double Val, MVT::ValueType VT);
94 SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
95 SDOperand getFrameIndex(int FI, MVT::ValueType VT);
96 SDOperand getConstantPool(unsigned CPIdx, MVT::ValueType VT);
97 SDOperand getBasicBlock(MachineBasicBlock *MBB);
98 SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
100 SDOperand getCopyToReg(SDOperand Chain, SDOperand N, unsigned Reg) {
101 // Note: these are auto-CSE'd because the caller doesn't make requests that
102 // could cause duplicates to occur.
103 SDNode *NN = new RegSDNode(ISD::CopyToReg, Chain, N, Reg);
104 NN->setValueTypes(MVT::Other);
105 AllNodes.push_back(NN);
106 return SDOperand(NN, 0);
109 SDOperand getCopyFromReg(unsigned Reg, MVT::ValueType VT, SDOperand Chain) {
110 // Note: These nodes are auto-CSE'd by the caller of this method.
111 SDNode *NN = new RegSDNode(ISD::CopyFromReg, Chain, Reg);
112 NN->setValueTypes(VT, MVT::Other);
113 AllNodes.push_back(NN);
114 return SDOperand(NN, 0);
117 SDOperand getImplicitDef(SDOperand Chain, unsigned Reg) {
118 // Note: These nodes are auto-CSE'd by the caller of this method.
119 SDNode *NN = new RegSDNode(ISD::ImplicitDef, Chain, Reg);
120 NN->setValueTypes(MVT::Other);
121 AllNodes.push_back(NN);
122 return SDOperand(NN, 0);
125 /// getCall - Note that this destroys the vector of RetVals passed in.
127 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
129 SDNode *NN = new SDNode(ISD::CALL, Chain, Callee);
130 NN->setValueTypes(RetVals);
131 AllNodes.push_back(NN);
135 /// getCall - This is identical to the one above, and should be used for calls
136 /// where arguments are passed in physical registers. This destroys the
137 /// RetVals and ArgsInRegs vectors.
138 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
139 SDOperand Callee, std::vector<SDOperand> &ArgsInRegs) {
140 ArgsInRegs.insert(ArgsInRegs.begin(), Callee);
141 ArgsInRegs.insert(ArgsInRegs.begin(), Chain);
142 SDNode *NN = new SDNode(ISD::CALL, ArgsInRegs);
143 NN->setValueTypes(RetVals);
144 AllNodes.push_back(NN);
149 SDOperand getSetCC(ISD::CondCode, MVT::ValueType VT,
150 SDOperand LHS, SDOperand RHS);
152 /// getZeroExtendInReg - Return the expression required to zero extend the Op
153 /// value assuming it was the smaller SrcTy value.
154 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
156 /// getNode - Gets or creates the specified node.
158 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
159 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
160 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
161 SDOperand N1, SDOperand N2);
162 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
163 SDOperand N1, SDOperand N2, SDOperand N3);
164 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
165 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
166 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
167 std::vector<SDOperand> &Children);
169 // getNode - These versions take an extra value type for extending and
170 // truncating loads, stores, rounds, extends etc.
171 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
172 SDOperand N2, MVT::ValueType EVT);
173 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
174 SDOperand N, MVT::ValueType EVT);
175 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
176 SDOperand N2, SDOperand N3, MVT::ValueType EVT);
177 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
178 SDOperand N2, SDOperand N3, SDOperand N4, MVT::ValueType EVT);
180 /// getLoad - Loads are not normal binary operators: their result type is not
181 /// determined by their operands, and they produce a value AND a token chain.
183 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr, SDOperand SV);
185 // getSrcValue - construct a node to track a Value* through the backend
186 SDOperand getSrcValue(const Value* I);
188 void replaceAllUsesWith(SDOperand Old, SDOperand New) {
189 assert(Old != New && "RAUW self!");
190 assert(0 && "Unimplemented!");
196 void DeleteNodeIfDead(SDNode *N, void *NodeSet);
198 // Maps to auto-CSE operations.
199 std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
201 std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
204 std::map<std::pair<std::pair<SDOperand, SDOperand>,
205 std::pair<ISD::CondCode, MVT::ValueType> >,
206 SetCCSDNode*> SetCCs;
208 std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
211 std::map<const GlobalValue*, SDNode*> GlobalValues;
212 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
213 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
214 std::map<int, SDNode*> FrameIndices;
215 std::map<unsigned, SDNode*> ConstantPoolIndices;
216 std::map<MachineBasicBlock *, SDNode*> BBNodes;
217 std::map<std::string, SDNode*> ExternalSymbols;
220 MVT::ValueType VT, EVT;
221 std::vector<SDOperand> Ops;
222 bool operator<(const EVTStruct &RHS) const {
223 if (Opcode < RHS.Opcode) return true;
224 if (Opcode > RHS.Opcode) return false;
225 if (VT < RHS.VT) return true;
226 if (VT > RHS.VT) return false;
227 if (EVT < RHS.EVT) return true;
228 if (EVT > RHS.EVT) return false;
229 return Ops < RHS.Ops;
232 std::map<EVTStruct, SDNode*> MVTSDNodes;
235 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
236 typedef SelectionDAG::allnodes_iterator nodes_iterator;
237 static nodes_iterator nodes_begin(SelectionDAG *G) {
238 return G->allnodes_begin();
240 static nodes_iterator nodes_end(SelectionDAG *G) {
241 return G->allnodes_end();