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 // ValueNodes - track SrcValue nodes
49 std::map<std::pair<const Value*, int>, SDNode*> ValueNodes;
52 SelectionDAG(TargetLowering &tli, MachineFunction &mf) : TLI(tli), MF(mf) {
53 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
57 MachineFunction &getMachineFunction() const { return MF; }
58 const TargetMachine &getTarget() const;
59 TargetLowering &getTargetLoweringInfo() const { return TLI; }
61 /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
66 typedef std::vector<SDNode*>::const_iterator allnodes_iterator;
67 allnodes_iterator allnodes_begin() const { return AllNodes.begin(); }
68 allnodes_iterator allnodes_end() const { return AllNodes.end(); }
70 /// getRoot - Return the root tag of the SelectionDAG.
72 const SDOperand &getRoot() const { return Root; }
74 /// getEntryNode - Return the token chain corresponding to the entry of the
76 const SDOperand &getEntryNode() const { return EntryNode; }
78 /// setRoot - Set the current root tag of the SelectionDAG.
80 const SDOperand &setRoot(SDOperand N) { return Root = N; }
82 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
83 /// compatible with the target instruction selector, as indicated by the
84 /// TargetLowering object.
86 /// Note that this is an involved process that may invalidate pointers into
90 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
91 /// SelectionDAG, including nodes (like loads) that have uses of their token
92 /// chain but no other uses and no side effect. If a node is passed in as an
93 /// argument, it is used as the seed for node deletion.
94 void RemoveDeadNodes(SDNode *N = 0);
96 SDOperand getConstant(uint64_t Val, MVT::ValueType VT);
97 SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT);
98 SDOperand getConstantFP(double Val, MVT::ValueType VT);
99 SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
100 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
101 SDOperand getFrameIndex(int FI, MVT::ValueType VT);
102 SDOperand getConstantPool(unsigned CPIdx, MVT::ValueType VT);
103 SDOperand getBasicBlock(MachineBasicBlock *MBB);
104 SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
105 SDOperand getValueType(MVT::ValueType);
106 SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
108 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
109 return getNode(ISD::CopyToReg, MVT::Other, Chain,
110 getRegister(Reg, N.getValueType()), N);
113 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
114 std::vector<MVT::ValueType> ResultTys;
115 ResultTys.push_back(VT);
116 ResultTys.push_back(MVT::Other);
117 std::vector<SDOperand> Ops;
118 Ops.push_back(Chain);
119 Ops.push_back(getRegister(Reg, VT));
120 return getNode(ISD::CopyFromReg, ResultTys, Ops);
123 SDOperand getImplicitDef(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
124 return getNode(ISD::ImplicitDef, MVT::Other, Chain, getRegister(Reg, VT));
127 /// getCall - Note that this destroys the vector of RetVals passed in.
129 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
130 SDOperand Callee, bool isTailCall = false) {
131 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain,
133 NN->setValueTypes(RetVals);
134 AllNodes.push_back(NN);
138 /// getCall - This is identical to the one above, and should be used for calls
139 /// where arguments are passed in physical registers. This destroys the
140 /// RetVals and ArgsInRegs vectors.
141 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
142 SDOperand Callee, std::vector<SDOperand> &ArgsInRegs,
143 bool isTailCall = false) {
144 ArgsInRegs.insert(ArgsInRegs.begin(), Callee);
145 ArgsInRegs.insert(ArgsInRegs.begin(), Chain);
146 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, ArgsInRegs);
147 NN->setValueTypes(RetVals);
148 AllNodes.push_back(NN);
152 SDOperand getCondCode(ISD::CondCode Cond);
154 /// getZeroExtendInReg - Return the expression required to zero extend the Op
155 /// value assuming it was the smaller SrcTy value.
156 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
158 /// getNode - Gets or creates the specified node.
160 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
161 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
162 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
163 SDOperand N1, SDOperand N2);
164 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
165 SDOperand N1, SDOperand N2, SDOperand N3);
166 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
167 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
168 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
169 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
171 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
172 std::vector<SDOperand> &Children);
173 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
174 std::vector<SDOperand> &Ops);
176 /// getSetCC - Helper function to make it easier to build SetCC's if you just
177 /// have an ISD::CondCode instead of an SDOperand.
179 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
180 ISD::CondCode Cond) {
181 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
184 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
185 /// just have an ISD::CondCode instead of an SDOperand.
187 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
188 SDOperand True, SDOperand False, ISD::CondCode Cond) {
189 MVT::ValueType VT = True.getValueType();
190 return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
193 /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC
196 SDOperand getBR2Way_CC(SDOperand Chain, SDOperand CCNode, SDOperand LHS,
197 SDOperand RHS, SDOperand True, SDOperand False) {
198 std::vector<SDOperand> Ops;
199 Ops.push_back(Chain);
200 Ops.push_back(CCNode);
204 Ops.push_back(False);
205 return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
208 /// getLoad - Loads are not normal binary operators: their result type is not
209 /// determined by their operands, and they produce a value AND a token chain.
211 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
213 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
214 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
216 // getSrcValue - construct a node to track a Value* through the backend
217 SDOperand getSrcValue(const Value* I, int offset = 0);
220 /// SelectNodeTo - These are used for target selectors to *mutate* the
221 /// specified node to have the specified return type, Target opcode, and
222 /// operands. Note that target opcodes are stored as
223 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
224 void SelectNodeTo(SDNode *N, MVT::ValueType VT, unsigned TargetOpc,
226 void SelectNodeTo(SDNode *N, MVT::ValueType VT, unsigned TargetOpc,
227 SDOperand Op1, SDOperand Op2);
228 void SelectNodeTo(SDNode *N, MVT::ValueType VT, unsigned TargetOpc,
229 SDOperand Op1, SDOperand Op2, SDOperand Op3);
230 void SelectNodeTo(SDNode *N, MVT::ValueType VT, unsigned TargetOpc,
231 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4);
232 void SelectNodeTo(SDNode *N, MVT::ValueType VT, unsigned TargetOpc,
233 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4,
235 void SelectNodeTo(SDNode *N, MVT::ValueType VT1, MVT::ValueType VT2,
236 unsigned TargetOpc, SDOperand Op1, SDOperand Op2);
238 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
240 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1);
242 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
243 SDOperand Op1, SDOperand Op2) {
244 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2);
246 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
247 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
248 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3);
250 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
251 SDOperand Op1, SDOperand Op2, SDOperand Op3,
253 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4);
255 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
256 SDOperand Op1, SDOperand Op2, SDOperand Op3,
257 SDOperand Op4, SDOperand Op5) {
258 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5);
261 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
262 /// This can cause recursive merging of nodes in the DAG.
264 void ReplaceAllUsesWith(SDNode *From, SDNode *To);
269 void RemoveNodeFromCSEMaps(SDNode *N);
270 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
271 void DeleteNodeIfDead(SDNode *N, void *NodeSet);
273 /// SimplifySetCC - Try to simplify a setcc built with the specified operands
274 /// and cc. If unable to simplify it, return a null SDOperand.
275 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
276 SDOperand N2, ISD::CondCode Cond);
278 /// SimplifySelectCC - Try to simplify a select_cc built with the specified
279 /// operands and cc. This can be used to simplify both the select_cc node,
280 /// and a select node whose first operand is a setcc.
281 SDOperand SimplifySelectCC(SDOperand N1, SDOperand N2, SDOperand N3,
282 SDOperand N4, ISD::CondCode CC);
284 // Maps to auto-CSE operations.
285 std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
287 std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
290 std::vector<RegisterSDNode*> RegNodes;
291 std::vector<CondCodeSDNode*> CondCodeNodes;
293 std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
296 std::map<const GlobalValue*, SDNode*> GlobalValues;
297 std::map<const GlobalValue*, SDNode*> TargetGlobalValues;
298 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
299 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants;
300 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
301 std::map<int, SDNode*> FrameIndices;
302 std::map<unsigned, SDNode*> ConstantPoolIndices;
303 std::map<MachineBasicBlock *, SDNode*> BBNodes;
304 std::vector<SDNode*> ValueTypeNodes;
305 std::map<std::string, SDNode*> ExternalSymbols;
306 std::map<std::pair<unsigned,
307 std::pair<MVT::ValueType, std::vector<SDOperand> > >,
308 SDNode*> OneResultNodes;
309 std::map<std::pair<unsigned,
310 std::pair<std::vector<MVT::ValueType>,
311 std::vector<SDOperand> > >,
312 SDNode*> ArbitraryNodes;
315 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
316 typedef SelectionDAG::allnodes_iterator nodes_iterator;
317 static nodes_iterator nodes_begin(SelectionDAG *G) {
318 return G->allnodes_begin();
320 static nodes_iterator nodes_end(SelectionDAG *G) {
321 return G->allnodes_end();