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"
19 #include "llvm/ADT/ilist"
23 #include <string> // FIXME remove eventually, turning map into const char* map.
28 class MachineFunction;
30 /// SelectionDAG class - This is used to represent a portion of an LLVM function
31 /// in a low-level Data Dependence DAG representation suitable for instruction
32 /// selection. This DAG is constructed as the first step of instruction
33 /// selection in order to allow implementation of machine specific optimizations
34 /// and code simplifications.
36 /// The representation used by the SelectionDAG is a target-independent
37 /// representation, which has some similarities to the GCC RTL representation,
38 /// but is significantly more simple, powerful, and is a graph form instead of a
45 // Root - The root of the entire DAG. EntryNode - The starting token.
46 SDOperand Root, EntryNode;
48 // AllNodes - A linked list of nodes in the current DAG.
49 ilist<SDNode> AllNodes;
51 // ValueNodes - track SrcValue nodes
52 std::map<std::pair<const Value*, int>, SDNode*> ValueNodes;
55 SelectionDAG(TargetLowering &tli, MachineFunction &mf) : TLI(tli), MF(mf) {
56 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
60 MachineFunction &getMachineFunction() const { return MF; }
61 const TargetMachine &getTarget() const;
62 TargetLowering &getTargetLoweringInfo() const { return TLI; }
64 /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
69 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
70 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
71 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
72 typedef ilist<SDNode>::iterator allnodes_iterator;
73 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
74 allnodes_iterator allnodes_end() { return AllNodes.end(); }
76 /// getRoot - Return the root tag of the SelectionDAG.
78 const SDOperand &getRoot() const { return Root; }
80 /// getEntryNode - Return the token chain corresponding to the entry of the
82 const SDOperand &getEntryNode() const { return EntryNode; }
84 /// setRoot - Set the current root tag of the SelectionDAG.
86 const SDOperand &setRoot(SDOperand N) { return Root = N; }
88 /// Combine - This iterates over the nodes in the SelectionDAG, folding
89 /// certain types of nodes together, or eliminating superfluous nodes. When
90 /// the AfterLegalize argument is set to 'true', Combine takes care not to
91 /// generate any nodes that will be illegal on the target.
92 void Combine(bool AfterLegalize);
94 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
95 /// compatible with the target instruction selector, as indicated by the
96 /// TargetLowering object.
98 /// Note that this is an involved process that may invalidate pointers into
102 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
103 /// SelectionDAG, including nodes (like loads) that have uses of their token
104 /// chain but no other uses and no side effect. If a node is passed in as an
105 /// argument, it is used as the seed for node deletion.
106 void RemoveDeadNodes(SDNode *N = 0);
108 SDOperand getString(const std::string &Val);
109 SDOperand getConstant(uint64_t Val, MVT::ValueType VT);
110 SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT);
111 SDOperand getConstantFP(double Val, MVT::ValueType VT);
112 SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
113 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
114 SDOperand getFrameIndex(int FI, MVT::ValueType VT);
115 SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT);
116 SDOperand getConstantPool(Constant *C, MVT::ValueType VT);
117 SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT);
118 SDOperand getBasicBlock(MachineBasicBlock *MBB);
119 SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
120 SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
121 SDOperand getValueType(MVT::ValueType);
122 SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
124 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
125 return getNode(ISD::CopyToReg, MVT::Other, Chain,
126 getRegister(Reg, N.getValueType()), N);
129 // This version of the getCopyToReg method takes an extra operand, which
130 // indicates that there is potentially an incoming flag value (if Flag is not
131 // null) and that there should be a flag result.
132 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
134 std::vector<MVT::ValueType> VTs;
135 VTs.push_back(MVT::Other);
136 VTs.push_back(MVT::Flag);
137 std::vector<SDOperand> Ops;
138 Ops.push_back(Chain);
139 Ops.push_back(getRegister(Reg, N.getValueType()));
141 if (Flag.Val) Ops.push_back(Flag);
142 return getNode(ISD::CopyToReg, VTs, Ops);
145 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
146 std::vector<MVT::ValueType> ResultTys;
147 ResultTys.push_back(VT);
148 ResultTys.push_back(MVT::Other);
149 std::vector<SDOperand> Ops;
150 Ops.push_back(Chain);
151 Ops.push_back(getRegister(Reg, VT));
152 return getNode(ISD::CopyFromReg, ResultTys, Ops);
155 // This version of the getCopyFromReg method takes an extra operand, which
156 // indicates that there is potentially an incoming flag value (if Flag is not
157 // null) and that there should be a flag result.
158 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
160 std::vector<MVT::ValueType> ResultTys;
161 ResultTys.push_back(VT);
162 ResultTys.push_back(MVT::Other);
163 ResultTys.push_back(MVT::Flag);
164 std::vector<SDOperand> Ops;
165 Ops.push_back(Chain);
166 Ops.push_back(getRegister(Reg, VT));
167 if (Flag.Val) Ops.push_back(Flag);
168 return getNode(ISD::CopyFromReg, ResultTys, Ops);
171 SDOperand getImplicitDef(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
172 return getNode(ISD::ImplicitDef, MVT::Other, Chain, getRegister(Reg, VT));
175 /// getCall - Note that this destroys the vector of RetVals passed in.
177 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
178 SDOperand Callee, bool isTailCall = false) {
179 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain,
181 setNodeValueTypes(NN, RetVals);
182 AllNodes.push_back(NN);
186 /// getCall - This is identical to the one above, and should be used for calls
187 /// where arguments are passed in physical registers. This destroys the
188 /// RetVals and ArgsInRegs vectors.
189 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
190 SDOperand Callee, std::vector<SDOperand> &ArgsInRegs,
191 bool isTailCall = false) {
192 ArgsInRegs.insert(ArgsInRegs.begin(), Callee);
193 ArgsInRegs.insert(ArgsInRegs.begin(), Chain);
194 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, ArgsInRegs);
195 setNodeValueTypes(NN, RetVals);
196 AllNodes.push_back(NN);
200 SDOperand getCondCode(ISD::CondCode Cond);
202 /// getZeroExtendInReg - Return the expression required to zero extend the Op
203 /// value assuming it was the smaller SrcTy value.
204 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
206 /// getNode - Gets or creates the specified node.
208 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
209 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
210 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
211 SDOperand N1, SDOperand N2);
212 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
213 SDOperand N1, SDOperand N2, SDOperand N3);
214 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
215 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
216 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
217 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
219 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
220 std::vector<SDOperand> &Children);
221 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
222 std::vector<SDOperand> &Ops);
224 /// getSetCC - Helper function to make it easier to build SetCC's if you just
225 /// have an ISD::CondCode instead of an SDOperand.
227 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
228 ISD::CondCode Cond) {
229 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
232 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
233 /// just have an ISD::CondCode instead of an SDOperand.
235 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
236 SDOperand True, SDOperand False, ISD::CondCode Cond) {
237 MVT::ValueType VT = True.getValueType();
238 return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
241 /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC
244 SDOperand getBR2Way_CC(SDOperand Chain, SDOperand CCNode, SDOperand LHS,
245 SDOperand RHS, SDOperand True, SDOperand False) {
246 std::vector<SDOperand> Ops;
247 Ops.push_back(Chain);
248 Ops.push_back(CCNode);
252 Ops.push_back(False);
253 return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
256 /// getLoad - Loads are not normal binary operators: their result type is not
257 /// determined by their operands, and they produce a value AND a token chain.
259 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
261 SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain,
262 SDOperand Ptr, SDOperand SV);
263 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
264 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
266 // getSrcValue - construct a node to track a Value* through the backend
267 SDOperand getSrcValue(const Value* I, int offset = 0);
270 /// SelectNodeTo - These are used for target selectors to *mutate* the
271 /// specified node to have the specified return type, Target opcode, and
272 /// operands. Note that target opcodes are stored as
273 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
274 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
275 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
277 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
278 SDOperand Op1, SDOperand Op2);
279 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
280 SDOperand Op1, SDOperand Op2, SDOperand Op3);
281 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
282 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4);
283 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
284 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4,
286 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
287 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
288 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
289 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
291 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
292 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
293 SDOperand Op3, SDOperand Op4);
294 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
295 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
296 SDOperand Op3, SDOperand Op4, SDOperand Op5);
298 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT) {
299 return getNode(ISD::BUILTIN_OP_END+Opcode, VT);
301 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
303 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1);
305 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
306 SDOperand Op1, SDOperand Op2) {
307 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2);
309 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
310 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
311 std::vector<MVT::ValueType> ResultTys;
312 ResultTys.push_back(VT1);
313 ResultTys.push_back(VT2);
314 std::vector<SDOperand> Ops;
317 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
319 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
320 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
321 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3);
323 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
324 SDOperand Op1, SDOperand Op2, SDOperand Op3,
326 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4);
328 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
329 SDOperand Op1, SDOperand Op2, SDOperand Op3,
330 SDOperand Op4, SDOperand Op5) {
331 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5);
333 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
334 std::vector<SDOperand> &Ops) {
335 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
337 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
338 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
339 std::vector<MVT::ValueType> ResultTys;
340 ResultTys.push_back(VT1);
341 ResultTys.push_back(VT2);
342 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
345 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
346 /// This can cause recursive merging of nodes in the DAG. Use the first
347 /// version if 'From' is known to have a single result, use the second
348 /// if you have two nodes with identical results, use the third otherwise.
350 /// These methods all take an optional vector, which (if not null) is
351 /// populated with any nodes that are deleted from the SelectionDAG, due to
352 /// new equivalences that are discovered.
354 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
355 std::vector<SDNode*> *Deleted = 0);
356 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
357 std::vector<SDNode*> *Deleted = 0);
358 void ReplaceAllUsesWith(SDNode *From, const std::vector<SDOperand> &To,
359 std::vector<SDNode*> *Deleted = 0);
362 /// DeleteNode - Remove the specified node from the system. This node must
363 /// have no referrers.
364 void DeleteNode(SDNode *N);
369 void RemoveNodeFromCSEMaps(SDNode *N);
370 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
371 void DestroyDeadNode(SDNode *N);
372 void DeleteNodeNotInCSEMaps(SDNode *N);
373 void setNodeValueTypes(SDNode *N, std::vector<MVT::ValueType> &RetVals);
374 void setNodeValueTypes(SDNode *N, MVT::ValueType VT1, MVT::ValueType VT2);
377 /// SimplifySetCC - Try to simplify a setcc built with the specified operands
378 /// and cc. If unable to simplify it, return a null SDOperand.
379 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
380 SDOperand N2, ISD::CondCode Cond);
382 // List of non-single value types.
383 std::list<std::vector<MVT::ValueType> > VTList;
385 // Maps to auto-CSE operations.
386 std::map<std::pair<unsigned, MVT::ValueType>, SDNode *> NullaryOps;
387 std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
389 std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
392 std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes;
393 std::vector<CondCodeSDNode*> CondCodeNodes;
395 std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
398 std::map<const GlobalValue*, SDNode*> GlobalValues;
399 std::map<const GlobalValue*, SDNode*> TargetGlobalValues;
400 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
401 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants;
402 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
403 std::map<int, SDNode*> FrameIndices, TargetFrameIndices;
404 std::map<Constant *, SDNode*> ConstantPoolIndices;
405 std::map<Constant *, SDNode*> TargetConstantPoolIndices;
406 std::map<MachineBasicBlock *, SDNode*> BBNodes;
407 std::vector<SDNode*> ValueTypeNodes;
408 std::map<std::string, SDNode*> ExternalSymbols;
409 std::map<std::string, SDNode*> TargetExternalSymbols;
410 std::map<std::string, StringSDNode*> StringNodes;
411 std::map<std::pair<unsigned,
412 std::pair<MVT::ValueType, std::vector<SDOperand> > >,
413 SDNode*> OneResultNodes;
414 std::map<std::pair<unsigned,
415 std::pair<std::vector<MVT::ValueType>,
416 std::vector<SDOperand> > >,
417 SDNode*> ArbitraryNodes;
420 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
421 typedef SelectionDAG::allnodes_iterator nodes_iterator;
422 static nodes_iterator nodes_begin(SelectionDAG *G) {
423 return G->allnodes_begin();
425 static nodes_iterator nodes_end(SelectionDAG *G) {
426 return G->allnodes_end();
430 } // end namespace llvm