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/SelectionDAGCSEMap.h"
19 #include "llvm/ADT/ilist"
31 class MachineDebugInfo;
32 class MachineFunction;
33 class MachineConstantPoolValue;
35 /// SelectionDAG class - This is used to represent a portion of an LLVM function
36 /// in a low-level Data Dependence DAG representation suitable for instruction
37 /// selection. This DAG is constructed as the first step of instruction
38 /// selection in order to allow implementation of machine specific optimizations
39 /// and code simplifications.
41 /// The representation used by the SelectionDAG is a target-independent
42 /// representation, which has some similarities to the GCC RTL representation,
43 /// but is significantly more simple, powerful, and is a graph form instead of a
51 /// Root - The root of the entire DAG. EntryNode - The starting token.
52 SDOperand Root, EntryNode;
54 /// AllNodes - A linked list of nodes in the current DAG.
55 ilist<SDNode> AllNodes;
57 /// CSEMap - This structure is used to memoize nodes, automatically performing
58 /// CSE with existing nodes with a duplicate is requested.
59 SelectionDAGCSEMap CSEMap;
62 SelectionDAG(TargetLowering &tli, MachineFunction &mf, MachineDebugInfo *di)
63 : TLI(tli), MF(mf), DI(di) {
64 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
68 MachineFunction &getMachineFunction() const { return MF; }
69 const TargetMachine &getTarget() const;
70 TargetLowering &getTargetLoweringInfo() const { return TLI; }
71 MachineDebugInfo *getMachineDebugInfo() const { return DI; }
73 /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
78 std::map<const SDNode *, std::string> NodeGraphAttrs;
81 /// clearGraphAttrs - Clear all previously defined node graph attributes.
82 /// Intended to be used from a debugging tool (eg. gdb).
83 void clearGraphAttrs();
85 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
87 void setGraphAttrs(const SDNode *N, const char *Attrs);
89 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
90 /// Used from getNodeAttributes.
91 const std::string getGraphAttrs(const SDNode *N) const;
93 /// setGraphColor - Convenience for setting node color attribute.
95 void setGraphColor(const SDNode *N, const char *Color);
97 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
98 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
99 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
100 typedef ilist<SDNode>::iterator allnodes_iterator;
101 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
102 allnodes_iterator allnodes_end() { return AllNodes.end(); }
104 /// getRoot - Return the root tag of the SelectionDAG.
106 const SDOperand &getRoot() const { return Root; }
108 /// getEntryNode - Return the token chain corresponding to the entry of the
110 const SDOperand &getEntryNode() const { return EntryNode; }
112 /// setRoot - Set the current root tag of the SelectionDAG.
114 const SDOperand &setRoot(SDOperand N) { return Root = N; }
116 /// Combine - This iterates over the nodes in the SelectionDAG, folding
117 /// certain types of nodes together, or eliminating superfluous nodes. When
118 /// the AfterLegalize argument is set to 'true', Combine takes care not to
119 /// generate any nodes that will be illegal on the target.
120 void Combine(bool AfterLegalize, AliasAnalysis &AA);
122 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
123 /// compatible with the target instruction selector, as indicated by the
124 /// TargetLowering object.
126 /// Note that this is an involved process that may invalidate pointers into
130 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
132 void RemoveDeadNodes();
134 /// RemoveDeadNode - Remove the specified node from the system. If any of its
135 /// operands then becomes dead, remove them as well. The vector Deleted is
136 /// populated with nodes that are deleted.
137 void RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted);
139 /// DeleteNode - Remove the specified node from the system. This node must
140 /// have no referrers.
141 void DeleteNode(SDNode *N);
143 /// getVTList - Return an SDVTList that represents the list of values
145 SDVTList getVTList(MVT::ValueType VT);
146 SDVTList getVTList(MVT::ValueType VT1, MVT::ValueType VT2);
147 SDVTList getVTList(MVT::ValueType VT1, MVT::ValueType VT2,MVT::ValueType VT3);
148 SDVTList getVTList(const MVT::ValueType *VTs, unsigned NumVTs);
150 /// getNodeValueTypes - These are obsolete, use getVTList instead.
151 const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT) {
152 return getVTList(VT).VTs;
154 const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1,
155 MVT::ValueType VT2) {
156 return getVTList(VT1, VT2).VTs;
158 const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1,MVT::ValueType VT2,
159 MVT::ValueType VT3) {
160 return getVTList(VT1, VT2, VT3).VTs;
162 const MVT::ValueType *getNodeValueTypes(std::vector<MVT::ValueType> &VTList) {
163 return getVTList(&VTList[0], VTList.size()).VTs;
167 //===--------------------------------------------------------------------===//
168 // Node creation methods.
170 SDOperand getString(const std::string &Val);
171 SDOperand getConstant(uint64_t Val, MVT::ValueType VT, bool isTarget = false);
172 SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT) {
173 return getConstant(Val, VT, true);
175 SDOperand getConstantFP(double Val, MVT::ValueType VT, bool isTarget = false);
176 SDOperand getTargetConstantFP(double Val, MVT::ValueType VT) {
177 return getConstantFP(Val, VT, true);
179 SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
180 int offset = 0, bool isTargetGA = false);
181 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
183 return getGlobalAddress(GV, VT, offset, true);
185 SDOperand getFrameIndex(int FI, MVT::ValueType VT, bool isTarget = false);
186 SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT) {
187 return getFrameIndex(FI, VT, true);
189 SDOperand getJumpTable(int JTI, MVT::ValueType VT, bool isTarget = false);
190 SDOperand getTargetJumpTable(int JTI, MVT::ValueType VT) {
191 return getJumpTable(JTI, VT, true);
193 SDOperand getConstantPool(Constant *C, MVT::ValueType VT,
194 unsigned Align = 0, int Offs = 0, bool isT=false);
195 SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT,
196 unsigned Align = 0, int Offset = 0) {
197 return getConstantPool(C, VT, Align, Offset, true);
199 SDOperand getConstantPool(MachineConstantPoolValue *C, MVT::ValueType VT,
200 unsigned Align = 0, int Offs = 0, bool isT=false);
201 SDOperand getTargetConstantPool(MachineConstantPoolValue *C,
202 MVT::ValueType VT, unsigned Align = 0,
204 return getConstantPool(C, VT, Align, Offset, true);
206 SDOperand getBasicBlock(MachineBasicBlock *MBB);
207 SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
208 SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
209 SDOperand getValueType(MVT::ValueType);
210 SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
212 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
213 return getNode(ISD::CopyToReg, MVT::Other, Chain,
214 getRegister(Reg, N.getValueType()), N);
217 // This version of the getCopyToReg method takes an extra operand, which
218 // indicates that there is potentially an incoming flag value (if Flag is not
219 // null) and that there should be a flag result.
220 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
222 const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
223 SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
224 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
227 // Similar to last getCopyToReg() except parameter Reg is a SDOperand
228 SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
230 const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
231 SDOperand Ops[] = { Chain, Reg, N, Flag };
232 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
235 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
236 const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
237 SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
238 return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
241 // This version of the getCopyFromReg method takes an extra operand, which
242 // indicates that there is potentially an incoming flag value (if Flag is not
243 // null) and that there should be a flag result.
244 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
246 const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
247 SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
248 return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.Val ? 3 : 2);
251 SDOperand getCondCode(ISD::CondCode Cond);
253 /// getZeroExtendInReg - Return the expression required to zero extend the Op
254 /// value assuming it was the smaller SrcTy value.
255 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
257 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
258 /// a flag result (to ensure it's not CSE'd).
259 SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
260 const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
261 SDOperand Ops[] = { Chain, Op };
262 return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
265 /// getNode - Gets or creates the specified node.
267 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
268 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
269 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
270 SDOperand N1, SDOperand N2);
271 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
272 SDOperand N1, SDOperand N2, SDOperand N3);
273 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
274 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
275 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
276 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
278 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
279 const SDOperand *Ops, unsigned NumOps);
280 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
281 const SDOperand *Ops, unsigned NumOps);
282 SDOperand getNode(unsigned Opcode, const MVT::ValueType *VTs, unsigned NumVTs,
283 const SDOperand *Ops, unsigned NumOps);
284 SDOperand getNode(unsigned Opcode, SDVTList VTs,
285 const SDOperand *Ops, unsigned NumOps);
287 /// getSetCC - Helper function to make it easier to build SetCC's if you just
288 /// have an ISD::CondCode instead of an SDOperand.
290 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
291 ISD::CondCode Cond) {
292 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
295 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
296 /// just have an ISD::CondCode instead of an SDOperand.
298 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
299 SDOperand True, SDOperand False, ISD::CondCode Cond) {
300 return getNode(ISD::SELECT_CC, True.getValueType(), LHS, RHS, True, False,
304 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
305 /// and a source value as input.
306 SDOperand getVAArg(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
309 /// getLoad - Loads are not normal binary operators: their result type is not
310 /// determined by their operands, and they produce a value AND a token chain.
312 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
313 const Value *SV, int SVOffset, bool isVolatile=false);
314 SDOperand getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
315 SDOperand Chain, SDOperand Ptr, const Value *SV,
316 int SVOffset, MVT::ValueType EVT, bool isVolatile=false);
317 SDOperand getPreIndexedLoad(SDOperand OrigLoad, SDOperand Base);
318 SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain,
319 SDOperand Ptr, SDOperand SV);
321 /// getStore - Helper function to build ISD::STORE nodes.
323 SDOperand getStore(SDOperand Chain, SDOperand Value, SDOperand Ptr,
324 const Value *SV, int SVOffset, bool isVolatile=false);
325 SDOperand getTruncStore(SDOperand Chain, SDOperand Value, SDOperand Ptr,
326 const Value *SV, int SVOffset, MVT::ValueType TVT,
327 bool isVolatile=false);
329 // getSrcValue - construct a node to track a Value* through the backend
330 SDOperand getSrcValue(const Value* I, int offset = 0);
332 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
333 /// specified operands. If the resultant node already exists in the DAG,
334 /// this does not modify the specified node, instead it returns the node that
335 /// already exists. If the resultant node does not exist in the DAG, the
336 /// input node is returned. As a degenerate case, if you specify the same
337 /// input operands as the node already has, the input node is returned.
338 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
339 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
340 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
342 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
343 SDOperand Op3, SDOperand Op4);
344 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
345 SDOperand Op3, SDOperand Op4, SDOperand Op5);
346 SDOperand UpdateNodeOperands(SDOperand N, SDOperand *Ops, unsigned NumOps);
348 /// SelectNodeTo - These are used for target selectors to *mutate* the
349 /// specified node to have the specified return type, Target opcode, and
350 /// operands. Note that target opcodes are stored as
351 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. The 0th value
352 /// of the resultant node is returned.
353 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
354 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
356 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
357 SDOperand Op1, SDOperand Op2);
358 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
359 SDOperand Op1, SDOperand Op2, SDOperand Op3);
360 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
361 const SDOperand *Ops, unsigned NumOps);
362 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
363 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
364 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
365 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
369 /// getTargetNode - These are used for target selectors to create a new node
370 /// with specified return type(s), target opcode, and operands.
372 /// Note that getTargetNode returns the resultant node. If there is already a
373 /// node of the specified opcode and operands, it returns that node instead of
375 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT);
376 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
378 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
379 SDOperand Op1, SDOperand Op2);
380 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
381 SDOperand Op1, SDOperand Op2, SDOperand Op3);
382 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
383 const SDOperand *Ops, unsigned NumOps);
384 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
385 MVT::ValueType VT2, SDOperand Op1);
386 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
387 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
388 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
389 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
391 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
393 const SDOperand *Ops, unsigned NumOps);
394 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
395 MVT::ValueType VT2, MVT::ValueType VT3,
396 SDOperand Op1, SDOperand Op2);
397 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
398 MVT::ValueType VT2, MVT::ValueType VT3,
399 const SDOperand *Ops, unsigned NumOps);
401 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
402 /// This can cause recursive merging of nodes in the DAG. Use the first
403 /// version if 'From' is known to have a single result, use the second
404 /// if you have two nodes with identical results, use the third otherwise.
406 /// These methods all take an optional vector, which (if not null) is
407 /// populated with any nodes that are deleted from the SelectionDAG, due to
408 /// new equivalences that are discovered.
410 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
411 std::vector<SDNode*> *Deleted = 0);
412 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
413 std::vector<SDNode*> *Deleted = 0);
414 void ReplaceAllUsesWith(SDNode *From, const SDOperand *To,
415 std::vector<SDNode*> *Deleted = 0);
417 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
418 /// uses of other values produced by From.Val alone. The Deleted vector is
419 /// handled the same was as for ReplaceAllUsesWith, but it is required for
421 void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
422 std::vector<SDNode*> &Deleted);
424 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
425 /// their allnodes order. It returns the maximum id.
426 unsigned AssignNodeIds();
428 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
429 /// based on their topological order. It returns the maximum id and a vector
430 /// of the SDNodes* in assigned order by reference.
431 unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
433 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
435 static bool isCommutativeBinOp(unsigned Opcode) {
447 case ISD::ADDE: return true;
448 default: return false;
454 /// FoldSetCC - Constant fold a setcc to true or false.
455 SDOperand FoldSetCC(MVT::ValueType VT, SDOperand N1,
456 SDOperand N2, ISD::CondCode Cond);
459 void RemoveNodeFromCSEMaps(SDNode *N);
460 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
461 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
462 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
464 SDNode *FindModifiedNodeSlot(SDNode *N, const SDOperand *Ops, unsigned NumOps,
467 void DeleteNodeNotInCSEMaps(SDNode *N);
469 // List of non-single value types.
470 std::list<std::vector<MVT::ValueType> > VTList;
472 // Maps to auto-CSE operations.
473 std::vector<CondCodeSDNode*> CondCodeNodes;
475 std::vector<SDNode*> ValueTypeNodes;
476 std::map<std::string, SDNode*> ExternalSymbols;
477 std::map<std::string, SDNode*> TargetExternalSymbols;
478 std::map<std::string, StringSDNode*> StringNodes;
481 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
482 typedef SelectionDAG::allnodes_iterator nodes_iterator;
483 static nodes_iterator nodes_begin(SelectionDAG *G) {
484 return G->allnodes_begin();
486 static nodes_iterator nodes_end(SelectionDAG *G) {
487 return G->allnodes_end();
491 } // end namespace llvm