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/CodeGen/SelectionDAGCSEMap.h"
20 #include "llvm/ADT/ilist"
31 class MachineDebugInfo;
32 class MachineFunction;
34 /// SelectionDAG class - This is used to represent a portion of an LLVM function
35 /// in a low-level Data Dependence DAG representation suitable for instruction
36 /// selection. This DAG is constructed as the first step of instruction
37 /// selection in order to allow implementation of machine specific optimizations
38 /// and code simplifications.
40 /// The representation used by the SelectionDAG is a target-independent
41 /// representation, which has some similarities to the GCC RTL representation,
42 /// but is significantly more simple, powerful, and is a graph form instead of a
50 /// Root - The root of the entire DAG. EntryNode - The starting token.
51 SDOperand Root, EntryNode;
53 /// AllNodes - A linked list of nodes in the current DAG.
54 ilist<SDNode> AllNodes;
56 /// CSEMap - This structure is used to memoize nodes, automatically performing
57 /// CSE with existing nodes with a duplicate is requested.
58 SelectionDAGCSEMap CSEMap;
61 SelectionDAG(TargetLowering &tli, MachineFunction &mf, MachineDebugInfo *di)
62 : TLI(tli), MF(mf), DI(di) {
63 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
67 MachineFunction &getMachineFunction() const { return MF; }
68 const TargetMachine &getTarget() const;
69 TargetLowering &getTargetLoweringInfo() const { return TLI; }
70 MachineDebugInfo *getMachineDebugInfo() const { return DI; }
72 /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
77 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
78 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
79 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
80 typedef ilist<SDNode>::iterator allnodes_iterator;
81 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
82 allnodes_iterator allnodes_end() { return AllNodes.end(); }
84 /// getRoot - Return the root tag of the SelectionDAG.
86 const SDOperand &getRoot() const { return Root; }
88 /// getEntryNode - Return the token chain corresponding to the entry of the
90 const SDOperand &getEntryNode() const { return EntryNode; }
92 /// setRoot - Set the current root tag of the SelectionDAG.
94 const SDOperand &setRoot(SDOperand N) { return Root = N; }
96 /// Combine - This iterates over the nodes in the SelectionDAG, folding
97 /// certain types of nodes together, or eliminating superfluous nodes. When
98 /// the AfterLegalize argument is set to 'true', Combine takes care not to
99 /// generate any nodes that will be illegal on the target.
100 void Combine(bool AfterLegalize);
102 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
103 /// compatible with the target instruction selector, as indicated by the
104 /// TargetLowering object.
106 /// Note that this is an involved process that may invalidate pointers into
110 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
112 void RemoveDeadNodes();
114 /// getVTList - Return an SDVTList that represents the list of values
116 SDVTList getVTList(MVT::ValueType VT);
117 SDVTList getVTList(MVT::ValueType VT1, MVT::ValueType VT2);
118 SDVTList getVTList(MVT::ValueType VT1, MVT::ValueType VT2,MVT::ValueType VT3);
119 SDVTList getVTList(const MVT::ValueType *VTs, unsigned NumVTs);
121 /// getNodeValueTypes - These are obsolete, use getVTList instead.
122 const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT) {
123 return getVTList(VT).VTs;
125 const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1,
126 MVT::ValueType VT2) {
127 return getVTList(VT1, VT2).VTs;
129 const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1,MVT::ValueType VT2,
130 MVT::ValueType VT3) {
131 return getVTList(VT1, VT2, VT3).VTs;
133 const MVT::ValueType *getNodeValueTypes(std::vector<MVT::ValueType> &VTList) {
134 return getVTList(&VTList[0], VTList.size()).VTs;
138 //===--------------------------------------------------------------------===//
139 // Node creation methods.
141 SDOperand getString(const std::string &Val);
142 SDOperand getConstant(uint64_t Val, MVT::ValueType VT, bool isTarget = false);
143 SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT) {
144 return getConstant(Val, VT, true);
146 SDOperand getConstantFP(double Val, MVT::ValueType VT, bool isTarget = false);
147 SDOperand getTargetConstantFP(double Val, MVT::ValueType VT) {
148 return getConstantFP(Val, VT, true);
150 SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
151 int offset = 0, bool isTargetGA = false);
152 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
154 return getGlobalAddress(GV, VT, offset, true);
156 SDOperand getFrameIndex(int FI, MVT::ValueType VT, bool isTarget = false);
157 SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT) {
158 return getFrameIndex(FI, VT, true);
160 SDOperand getJumpTable(int JTI, MVT::ValueType VT, bool isTarget = false);
161 SDOperand getTargetJumpTable(int JTI, MVT::ValueType VT) {
162 return getJumpTable(JTI, VT, true);
164 SDOperand getConstantPool(Constant *C, MVT::ValueType VT,
165 unsigned Align = 0, int Offs = 0, bool isT=false);
166 SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT,
167 unsigned Align = 0, int Offset = 0) {
168 return getConstantPool(C, VT, Align, Offset, true);
170 SDOperand getBasicBlock(MachineBasicBlock *MBB);
171 SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
172 SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
173 SDOperand getValueType(MVT::ValueType);
174 SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
176 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
177 return getNode(ISD::CopyToReg, MVT::Other, Chain,
178 getRegister(Reg, N.getValueType()), N);
181 // This version of the getCopyToReg method takes an extra operand, which
182 // indicates that there is potentially an incoming flag value (if Flag is not
183 // null) and that there should be a flag result.
184 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
186 const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
187 SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
188 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
191 // Similar to last getCopyToReg() except parameter Reg is a SDOperand
192 SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
194 const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
195 SDOperand Ops[] = { Chain, Reg, N, Flag };
196 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
199 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
200 const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
201 SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
202 return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
205 // This version of the getCopyFromReg method takes an extra operand, which
206 // indicates that there is potentially an incoming flag value (if Flag is not
207 // null) and that there should be a flag result.
208 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
210 const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
211 SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
212 return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.Val ? 3 : 2);
215 SDOperand getCondCode(ISD::CondCode Cond);
217 /// getZeroExtendInReg - Return the expression required to zero extend the Op
218 /// value assuming it was the smaller SrcTy value.
219 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
221 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
222 /// a flag result (to ensure it's not CSE'd).
223 SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
224 const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
225 SDOperand Ops[] = { Chain, Op };
226 return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
229 /// getNode - Gets or creates the specified node.
231 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
232 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
233 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
234 SDOperand N1, SDOperand N2);
235 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
236 SDOperand N1, SDOperand N2, SDOperand N3);
237 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
238 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
239 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
240 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
242 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
243 const SDOperand *Ops, unsigned NumOps);
244 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
245 const SDOperand *Ops, unsigned NumOps);
246 SDOperand getNode(unsigned Opcode, const MVT::ValueType *VTs, unsigned NumVTs,
247 const SDOperand *Ops, unsigned NumOps);
248 SDOperand getNode(unsigned Opcode, SDVTList VTs,
249 const SDOperand *Ops, unsigned NumOps);
251 /// getSetCC - Helper function to make it easier to build SetCC's if you just
252 /// have an ISD::CondCode instead of an SDOperand.
254 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
255 ISD::CondCode Cond) {
256 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
259 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
260 /// just have an ISD::CondCode instead of an SDOperand.
262 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
263 SDOperand True, SDOperand False, ISD::CondCode Cond) {
264 return getNode(ISD::SELECT_CC, True.getValueType(), LHS, RHS, True, False,
268 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
269 /// and a source value as input.
270 SDOperand getVAArg(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
273 /// getLoad - Loads are not normal binary operators: their result type is not
274 /// determined by their operands, and they produce a value AND a token chain.
276 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
278 SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain,
279 SDOperand Ptr, SDOperand SV);
280 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
281 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
283 // getSrcValue - construct a node to track a Value* through the backend
284 SDOperand getSrcValue(const Value* I, int offset = 0);
286 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
287 /// specified operands. If the resultant node already exists in the DAG,
288 /// this does not modify the specified node, instead it returns the node that
289 /// already exists. If the resultant node does not exist in the DAG, the
290 /// input node is returned. As a degenerate case, if you specify the same
291 /// input operands as the node already has, the input node is returned.
292 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
293 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
294 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
296 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
297 SDOperand Op3, SDOperand Op4);
298 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
299 SDOperand Op3, SDOperand Op4, SDOperand Op5);
300 SDOperand UpdateNodeOperands(SDOperand N, SDOperand *Ops, unsigned NumOps);
302 /// SelectNodeTo - These are used for target selectors to *mutate* the
303 /// specified node to have the specified return type, Target opcode, and
304 /// operands. Note that target opcodes are stored as
305 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. The 0th value
306 /// of the resultant node is returned.
307 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
308 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
310 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
311 SDOperand Op1, SDOperand Op2);
312 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
313 SDOperand Op1, SDOperand Op2, SDOperand Op3);
314 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
315 const SDOperand *Ops, unsigned NumOps);
316 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
317 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
318 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
319 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
323 /// getTargetNode - These are used for target selectors to create a new node
324 /// with specified return type(s), target opcode, and operands.
326 /// Note that getTargetNode returns the resultant node. If there is already a
327 /// node of the specified opcode and operands, it returns that node instead of
329 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT);
330 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
332 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
333 SDOperand Op1, SDOperand Op2);
334 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
335 SDOperand Op1, SDOperand Op2, SDOperand Op3);
336 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
337 const SDOperand *Ops, unsigned NumOps);
338 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
339 MVT::ValueType VT2, SDOperand Op1);
340 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
341 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
342 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
343 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
345 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
347 const SDOperand *Ops, unsigned NumOps);
348 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
349 MVT::ValueType VT2, MVT::ValueType VT3,
350 SDOperand Op1, SDOperand Op2);
351 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
352 MVT::ValueType VT2, MVT::ValueType VT3,
353 const SDOperand *Ops, unsigned NumOps);
355 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
356 /// This can cause recursive merging of nodes in the DAG. Use the first
357 /// version if 'From' is known to have a single result, use the second
358 /// if you have two nodes with identical results, use the third otherwise.
360 /// These methods all take an optional vector, which (if not null) is
361 /// populated with any nodes that are deleted from the SelectionDAG, due to
362 /// new equivalences that are discovered.
364 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
365 std::vector<SDNode*> *Deleted = 0);
366 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
367 std::vector<SDNode*> *Deleted = 0);
368 void ReplaceAllUsesWith(SDNode *From, const SDOperand *To,
369 std::vector<SDNode*> *Deleted = 0);
371 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
372 /// uses of other values produced by From.Val alone. The Deleted vector is
373 /// handled the same was as for ReplaceAllUsesWith, but it is required for
375 void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
376 std::vector<SDNode*> &Deleted);
378 /// DeleteNode - Remove the specified node from the system. This node must
379 /// have no referrers.
380 void DeleteNode(SDNode *N);
382 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
383 /// their allnodes order. It returns the maximum id.
384 unsigned AssignNodeIds();
386 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
387 /// based on their topological order. It returns the maximum id and a vector
388 /// of the SDNodes* in assigned order by reference.
389 unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
391 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
393 static bool isCommutativeBinOp(unsigned Opcode) {
405 case ISD::ADDE: return true;
406 default: return false;
413 void RemoveNodeFromCSEMaps(SDNode *N);
414 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
415 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
416 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
418 SDNode *FindModifiedNodeSlot(SDNode *N, const SDOperand *Ops, unsigned NumOps,
421 void DeleteNodeNotInCSEMaps(SDNode *N);
423 /// SimplifySetCC - Try to simplify a setcc built with the specified operands
424 /// and cc. If unable to simplify it, return a null SDOperand.
425 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
426 SDOperand N2, ISD::CondCode Cond);
428 // List of non-single value types.
429 std::list<std::vector<MVT::ValueType> > VTList;
431 // Maps to auto-CSE operations.
432 std::vector<CondCodeSDNode*> CondCodeNodes;
434 std::vector<SDNode*> ValueTypeNodes;
435 std::map<std::string, SDNode*> ExternalSymbols;
436 std::map<std::string, SDNode*> TargetExternalSymbols;
437 std::map<std::string, StringSDNode*> StringNodes;
440 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
441 typedef SelectionDAG::allnodes_iterator nodes_iterator;
442 static nodes_iterator nodes_begin(SelectionDAG *G) {
443 return G->allnodes_begin();
445 static nodes_iterator nodes_end(SelectionDAG *G) {
446 return G->allnodes_end();
450 } // end namespace llvm