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 /// getNode - Gets or creates the specified node.
154 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
155 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
156 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
157 SDOperand N1, SDOperand N2);
158 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
159 SDOperand N1, SDOperand N2, SDOperand N3);
160 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
161 std::vector<SDOperand> &Children);
163 // getNode - These versions take an extra value type for extending and
164 // truncating loads, stores, rounds, extends etc.
165 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
166 SDOperand N2, MVT::ValueType EVT);
167 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
168 SDOperand N, MVT::ValueType EVT);
169 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
170 SDOperand N2, SDOperand N3, MVT::ValueType EVT);
172 /// getLoad - Loads are not normal binary operators: their result type is not
173 /// determined by their operands, and they produce a value AND a token chain.
175 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr);
177 void replaceAllUsesWith(SDOperand Old, SDOperand New) {
178 assert(Old != New && "RAUW self!");
179 assert(0 && "Unimplemented!");
185 void DeleteNodeIfDead(SDNode *N, void *NodeSet);
187 // Maps to auto-CSE operations.
188 std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
190 std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
193 std::map<std::pair<std::pair<SDOperand, SDOperand>,
194 std::pair<ISD::CondCode, MVT::ValueType> >,
195 SetCCSDNode*> SetCCs;
197 std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
200 std::map<const GlobalValue*, SDNode*> GlobalValues;
201 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
202 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
203 std::map<int, SDNode*> FrameIndices;
204 std::map<unsigned, SDNode*> ConstantPoolIndices;
205 std::map<MachineBasicBlock *, SDNode*> BBNodes;
206 std::map<std::string, SDNode*> ExternalSymbols;
209 MVT::ValueType VT, EVT;
210 std::vector<SDOperand> Ops;
211 bool operator<(const EVTStruct &RHS) const {
212 if (Opcode < RHS.Opcode) return true;
213 if (Opcode > RHS.Opcode) return false;
214 if (VT < RHS.VT) return true;
215 if (VT > RHS.VT) return false;
216 if (EVT < RHS.EVT) return true;
217 if (EVT > RHS.EVT) return false;
218 return Ops < RHS.Ops;
221 std::map<EVTStruct, SDNode*> MVTSDNodes;
224 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
225 typedef SelectionDAG::allnodes_iterator nodes_iterator;
226 static nodes_iterator nodes_begin(SelectionDAG *G) {
227 return G->allnodes_begin();
229 static nodes_iterator nodes_end(SelectionDAG *G) {
230 return G->allnodes_end();