blob: e11bd42ccaa644e7cbabb260896455da04250f92 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner84e66db2007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the SelectionDAG class, and transitively defines the
11// SDNode class and subclasses.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_SELECTIONDAG_H
16#define LLVM_CODEGEN_SELECTIONDAG_H
17
18#include "llvm/ADT/FoldingSet.h"
Anton Korobeynikova9db7a22008-05-29 17:41:17 +000019#include "llvm/ADT/ilist.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CodeGen/SelectionDAGNodes.h"
21
22#include <list>
23#include <vector>
24#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include <string>
26
27namespace llvm {
28 class AliasAnalysis;
29 class TargetLowering;
30 class TargetMachine;
31 class MachineModuleInfo;
32 class MachineFunction;
33 class MachineConstantPoolValue;
34
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.
40///
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
44/// linear form.
45///
46class SelectionDAG {
47 TargetLowering &TLI;
48 MachineFunction &MF;
49 MachineModuleInfo *MMI;
50
51 /// Root - The root of the entire DAG. EntryNode - The starting token.
52 SDOperand Root, EntryNode;
53
54 /// AllNodes - A linked list of nodes in the current DAG.
55 ilist<SDNode> AllNodes;
56
57 /// CSEMap - This structure is used to memoize nodes, automatically performing
58 /// CSE with existing nodes with a duplicate is requested.
59 FoldingSet<SDNode> CSEMap;
60
61public:
62 SelectionDAG(TargetLowering &tli, MachineFunction &mf, MachineModuleInfo *mmi)
63 : TLI(tli), MF(mf), MMI(mmi) {
64 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
65 }
66 ~SelectionDAG();
67
68 MachineFunction &getMachineFunction() const { return MF; }
69 const TargetMachine &getTarget() const;
70 TargetLowering &getTargetLoweringInfo() const { return TLI; }
71 MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
72
73 /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
74 ///
75 void viewGraph();
76
77#ifndef NDEBUG
78 std::map<const SDNode *, std::string> NodeGraphAttrs;
79#endif
80
81 /// clearGraphAttrs - Clear all previously defined node graph attributes.
82 /// Intended to be used from a debugging tool (eg. gdb).
83 void clearGraphAttrs();
84
85 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
86 ///
87 void setGraphAttrs(const SDNode *N, const char *Attrs);
88
89 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
90 /// Used from getNodeAttributes.
91 const std::string getGraphAttrs(const SDNode *N) const;
92
93 /// setGraphColor - Convenience for setting node color attribute.
94 ///
95 void setGraphColor(const SDNode *N, const char *Color);
96
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(); }
103
104 /// getRoot - Return the root tag of the SelectionDAG.
105 ///
106 const SDOperand &getRoot() const { return Root; }
107
108 /// getEntryNode - Return the token chain corresponding to the entry of the
109 /// function.
110 const SDOperand &getEntryNode() const { return EntryNode; }
111
112 /// setRoot - Set the current root tag of the SelectionDAG.
113 ///
114 const SDOperand &setRoot(SDOperand N) { return Root = N; }
115
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);
121
Chris Lattner8a258202007-10-15 06:10:22 +0000122 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
123 /// only uses types natively supported by the target.
124 ///
125 /// Note that this is an involved process that may invalidate pointers into
126 /// the graph.
127 void LegalizeTypes();
128
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
130 /// compatible with the target instruction selector, as indicated by the
131 /// TargetLowering object.
132 ///
133 /// Note that this is an involved process that may invalidate pointers into
134 /// the graph.
135 void Legalize();
136
137 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
138 /// SelectionDAG.
139 void RemoveDeadNodes();
140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 /// DeleteNode - Remove the specified node from the system. This node must
142 /// have no referrers.
143 void DeleteNode(SDNode *N);
144
145 /// getVTList - Return an SDVTList that represents the list of values
146 /// specified.
Duncan Sands92c43912008-06-06 12:08:01 +0000147 SDVTList getVTList(MVT VT);
148 SDVTList getVTList(MVT VT1, MVT VT2);
149 SDVTList getVTList(MVT VT1, MVT VT2, MVT VT3);
150 SDVTList getVTList(const MVT *VTs, unsigned NumVTs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151
152 /// getNodeValueTypes - These are obsolete, use getVTList instead.
Duncan Sands92c43912008-06-06 12:08:01 +0000153 const MVT *getNodeValueTypes(MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 return getVTList(VT).VTs;
155 }
Duncan Sands92c43912008-06-06 12:08:01 +0000156 const MVT *getNodeValueTypes(MVT VT1, MVT VT2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 return getVTList(VT1, VT2).VTs;
158 }
Duncan Sands92c43912008-06-06 12:08:01 +0000159 const MVT *getNodeValueTypes(MVT VT1, MVT VT2, MVT VT3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 return getVTList(VT1, VT2, VT3).VTs;
161 }
Duncan Sands92c43912008-06-06 12:08:01 +0000162 const MVT *getNodeValueTypes(std::vector<MVT> &vtList) {
Bill Wendling51bacec2008-05-19 20:15:12 +0000163 return getVTList(&vtList[0], (unsigned)vtList.size()).VTs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 }
165
166
167 //===--------------------------------------------------------------------===//
168 // Node creation methods.
169 //
170 SDOperand getString(const std::string &Val);
Duncan Sands92c43912008-06-06 12:08:01 +0000171 SDOperand getConstant(uint64_t Val, MVT VT, bool isTarget = false);
172 SDOperand getConstant(const APInt &Val, MVT VT, bool isTarget = false);
Chris Lattner5872a362008-01-17 07:00:52 +0000173 SDOperand getIntPtrConstant(uint64_t Val, bool isTarget = false);
Duncan Sands92c43912008-06-06 12:08:01 +0000174 SDOperand getTargetConstant(uint64_t Val, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 return getConstant(Val, VT, true);
176 }
Duncan Sands92c43912008-06-06 12:08:01 +0000177 SDOperand getTargetConstant(const APInt &Val, MVT VT) {
Dan Gohmandc458cf2008-02-08 22:59:30 +0000178 return getConstant(Val, VT, true);
179 }
Duncan Sands92c43912008-06-06 12:08:01 +0000180 SDOperand getConstantFP(double Val, MVT VT, bool isTarget = false);
181 SDOperand getConstantFP(const APFloat& Val, MVT VT, bool isTarget = false);
182 SDOperand getTargetConstantFP(double Val, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 return getConstantFP(Val, VT, true);
184 }
Duncan Sands92c43912008-06-06 12:08:01 +0000185 SDOperand getTargetConstantFP(const APFloat& Val, MVT VT) {
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000186 return getConstantFP(Val, VT, true);
187 }
Duncan Sands92c43912008-06-06 12:08:01 +0000188 SDOperand getGlobalAddress(const GlobalValue *GV, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 int offset = 0, bool isTargetGA = false);
Duncan Sands92c43912008-06-06 12:08:01 +0000190 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 int offset = 0) {
192 return getGlobalAddress(GV, VT, offset, true);
193 }
Duncan Sands92c43912008-06-06 12:08:01 +0000194 SDOperand getFrameIndex(int FI, MVT VT, bool isTarget = false);
195 SDOperand getTargetFrameIndex(int FI, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 return getFrameIndex(FI, VT, true);
197 }
Duncan Sands92c43912008-06-06 12:08:01 +0000198 SDOperand getJumpTable(int JTI, MVT VT, bool isTarget = false);
199 SDOperand getTargetJumpTable(int JTI, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 return getJumpTable(JTI, VT, true);
201 }
Duncan Sands92c43912008-06-06 12:08:01 +0000202 SDOperand getConstantPool(Constant *C, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 unsigned Align = 0, int Offs = 0, bool isT=false);
Duncan Sands92c43912008-06-06 12:08:01 +0000204 SDOperand getTargetConstantPool(Constant *C, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 unsigned Align = 0, int Offset = 0) {
206 return getConstantPool(C, VT, Align, Offset, true);
207 }
Duncan Sands92c43912008-06-06 12:08:01 +0000208 SDOperand getConstantPool(MachineConstantPoolValue *C, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 unsigned Align = 0, int Offs = 0, bool isT=false);
210 SDOperand getTargetConstantPool(MachineConstantPoolValue *C,
Duncan Sands92c43912008-06-06 12:08:01 +0000211 MVT VT, unsigned Align = 0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 int Offset = 0) {
213 return getConstantPool(C, VT, Align, Offset, true);
214 }
215 SDOperand getBasicBlock(MachineBasicBlock *MBB);
Duncan Sands92c43912008-06-06 12:08:01 +0000216 SDOperand getExternalSymbol(const char *Sym, MVT VT);
217 SDOperand getTargetExternalSymbol(const char *Sym, MVT VT);
Duncan Sandsc93fae32008-03-21 09:14:45 +0000218 SDOperand getArgFlags(ISD::ArgFlagsTy Flags);
Duncan Sands92c43912008-06-06 12:08:01 +0000219 SDOperand getValueType(MVT);
220 SDOperand getRegister(unsigned Reg, MVT VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221
222 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
223 return getNode(ISD::CopyToReg, MVT::Other, Chain,
224 getRegister(Reg, N.getValueType()), N);
225 }
226
227 // This version of the getCopyToReg method takes an extra operand, which
228 // indicates that there is potentially an incoming flag value (if Flag is not
229 // null) and that there should be a flag result.
230 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
231 SDOperand Flag) {
Duncan Sands92c43912008-06-06 12:08:01 +0000232 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
234 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
235 }
236
237 // Similar to last getCopyToReg() except parameter Reg is a SDOperand
238 SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
239 SDOperand Flag) {
Duncan Sands92c43912008-06-06 12:08:01 +0000240 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 SDOperand Ops[] = { Chain, Reg, N, Flag };
242 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
243 }
244
Duncan Sands92c43912008-06-06 12:08:01 +0000245 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT VT) {
246 const MVT *VTs = getNodeValueTypes(VT, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
248 return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
249 }
250
251 // This version of the getCopyFromReg method takes an extra operand, which
252 // indicates that there is potentially an incoming flag value (if Flag is not
253 // null) and that there should be a flag result.
Duncan Sands92c43912008-06-06 12:08:01 +0000254 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 SDOperand Flag) {
Duncan Sands92c43912008-06-06 12:08:01 +0000256 const MVT *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
258 return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.Val ? 3 : 2);
259 }
260
261 SDOperand getCondCode(ISD::CondCode Cond);
262
263 /// getZeroExtendInReg - Return the expression required to zero extend the Op
264 /// value assuming it was the smaller SrcTy value.
Duncan Sands92c43912008-06-06 12:08:01 +0000265 SDOperand getZeroExtendInReg(SDOperand Op, MVT SrcTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266
267 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
268 /// a flag result (to ensure it's not CSE'd).
269 SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000270 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 SDOperand Ops[] = { Chain, Op };
272 return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
273 }
274
Bill Wendling22f8deb2007-11-13 00:44:25 +0000275 /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
276 /// flag result (to ensure it's not CSE'd).
277 SDOperand getCALLSEQ_END(SDOperand Chain, SDOperand Op1, SDOperand Op2,
278 SDOperand InFlag) {
279 SDVTList NodeTys = getVTList(MVT::Other, MVT::Flag);
280 SmallVector<SDOperand, 4> Ops;
281 Ops.push_back(Chain);
282 Ops.push_back(Op1);
283 Ops.push_back(Op2);
284 Ops.push_back(InFlag);
285 return getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0],
Evan Cheng591bfc82008-05-05 18:30:58 +0000286 (unsigned)Ops.size() - (InFlag.Val == 0 ? 1 : 0));
Bill Wendling22f8deb2007-11-13 00:44:25 +0000287 }
288
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 /// getNode - Gets or creates the specified node.
290 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000291 SDOperand getNode(unsigned Opcode, MVT VT);
292 SDOperand getNode(unsigned Opcode, MVT VT, SDOperand N);
293 SDOperand getNode(unsigned Opcode, MVT VT, SDOperand N1, SDOperand N2);
294 SDOperand getNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 SDOperand N1, SDOperand N2, SDOperand N3);
Duncan Sands92c43912008-06-06 12:08:01 +0000296 SDOperand getNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
Duncan Sands92c43912008-06-06 12:08:01 +0000298 SDOperand getNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
300 SDOperand N5);
Duncan Sands92c43912008-06-06 12:08:01 +0000301 SDOperand getNode(unsigned Opcode, MVT VT, SDOperandPtr Ops, unsigned NumOps);
302 SDOperand getNode(unsigned Opcode, std::vector<MVT> &ResultTys,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000303 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000304 SDOperand getNode(unsigned Opcode, const MVT *VTs, unsigned NumVTs,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000305 SDOperandPtr Ops, unsigned NumOps);
Dan Gohman798d1272007-10-08 15:49:58 +0000306 SDOperand getNode(unsigned Opcode, SDVTList VTs);
307 SDOperand getNode(unsigned Opcode, SDVTList VTs, SDOperand N);
Duncan Sands92c43912008-06-06 12:08:01 +0000308 SDOperand getNode(unsigned Opcode, SDVTList VTs, SDOperand N1, SDOperand N2);
Dan Gohman798d1272007-10-08 15:49:58 +0000309 SDOperand getNode(unsigned Opcode, SDVTList VTs,
310 SDOperand N1, SDOperand N2, SDOperand N3);
311 SDOperand getNode(unsigned Opcode, SDVTList VTs,
312 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
313 SDOperand getNode(unsigned Opcode, SDVTList VTs,
314 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
315 SDOperand N5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 SDOperand getNode(unsigned Opcode, SDVTList VTs,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000317 SDOperandPtr Ops, unsigned NumOps);
Rafael Espindola80825902007-10-19 10:41:11 +0000318
Dan Gohmane8b391e2008-04-12 04:36:06 +0000319 SDOperand getMemcpy(SDOperand Chain, SDOperand Dst, SDOperand Src,
320 SDOperand Size, unsigned Align,
321 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +0000322 const Value *DstSV, uint64_t DstSVOff,
323 const Value *SrcSV, uint64_t SrcSVOff);
Rafael Espindola80825902007-10-19 10:41:11 +0000324
Dan Gohmane8b391e2008-04-12 04:36:06 +0000325 SDOperand getMemmove(SDOperand Chain, SDOperand Dst, SDOperand Src,
Dan Gohman1457fa82008-04-23 17:50:15 +0000326 SDOperand Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +0000327 const Value *DstSV, uint64_t DstOSVff,
328 const Value *SrcSV, uint64_t SrcSVOff);
Rafael Espindola80825902007-10-19 10:41:11 +0000329
Dan Gohmane8b391e2008-04-12 04:36:06 +0000330 SDOperand getMemset(SDOperand Chain, SDOperand Dst, SDOperand Src,
331 SDOperand Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +0000332 const Value *DstSV, uint64_t DstSVOff);
Rafael Espindola80825902007-10-19 10:41:11 +0000333
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 /// getSetCC - Helper function to make it easier to build SetCC's if you just
335 /// have an ISD::CondCode instead of an SDOperand.
336 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000337 SDOperand getSetCC(MVT VT, SDOperand LHS, SDOperand RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 ISD::CondCode Cond) {
339 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
340 }
341
Nate Begeman9a1ce152008-05-12 19:40:03 +0000342 /// getVSetCC - Helper function to make it easier to build VSetCC's nodes
343 /// if you just have an ISD::CondCode instead of an SDOperand.
344 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000345 SDOperand getVSetCC(MVT VT, SDOperand LHS, SDOperand RHS,
Nate Begeman9a1ce152008-05-12 19:40:03 +0000346 ISD::CondCode Cond) {
347 return getNode(ISD::VSETCC, VT, LHS, RHS, getCondCode(Cond));
348 }
349
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
351 /// just have an ISD::CondCode instead of an SDOperand.
352 ///
353 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
354 SDOperand True, SDOperand False, ISD::CondCode Cond) {
355 return getNode(ISD::SELECT_CC, True.getValueType(), LHS, RHS, True, False,
356 getCondCode(Cond));
357 }
358
359 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
360 /// and a source value as input.
Duncan Sands92c43912008-06-06 12:08:01 +0000361 SDOperand getVAArg(MVT VT, SDOperand Chain, SDOperand Ptr,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 SDOperand SV);
363
Andrew Lenharthe44f3902008-02-21 06:45:13 +0000364 /// getAtomic - Gets a node for an atomic op, produces result and chain, takes
365 // 3 operands
366 SDOperand getAtomic(unsigned Opcode, SDOperand Chain, SDOperand Ptr,
Duncan Sands92c43912008-06-06 12:08:01 +0000367 SDOperand Cmp, SDOperand Swp, MVT VT);
Andrew Lenharthe44f3902008-02-21 06:45:13 +0000368
369 /// getAtomic - Gets a node for an atomic op, produces result and chain, takes
370 // 2 operands
371 SDOperand getAtomic(unsigned Opcode, SDOperand Chain, SDOperand Ptr,
Duncan Sands92c43912008-06-06 12:08:01 +0000372 SDOperand Val, MVT VT);
Andrew Lenharthe44f3902008-02-21 06:45:13 +0000373
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 /// getLoad - Loads are not normal binary operators: their result type is not
375 /// determined by their operands, and they produce a value AND a token chain.
376 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000377 SDOperand getLoad(MVT VT, SDOperand Chain, SDOperand Ptr,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 const Value *SV, int SVOffset, bool isVolatile=false,
379 unsigned Alignment=0);
Duncan Sands92c43912008-06-06 12:08:01 +0000380 SDOperand getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 SDOperand Chain, SDOperand Ptr, const Value *SV,
Duncan Sands92c43912008-06-06 12:08:01 +0000382 int SVOffset, MVT EVT, bool isVolatile=false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 unsigned Alignment=0);
384 SDOperand getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
385 SDOperand Offset, ISD::MemIndexedMode AM);
Duncan Sandsb2589712008-03-28 09:45:24 +0000386 SDOperand getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands92c43912008-06-06 12:08:01 +0000387 MVT VT, SDOperand Chain,
Duncan Sandsb2589712008-03-28 09:45:24 +0000388 SDOperand Ptr, SDOperand Offset,
Duncan Sands92c43912008-06-06 12:08:01 +0000389 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandsb2589712008-03-28 09:45:24 +0000390 bool isVolatile=false, unsigned Alignment=0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391
392 /// getStore - Helper function to build ISD::STORE nodes.
393 ///
394 SDOperand getStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
395 const Value *SV, int SVOffset, bool isVolatile=false,
396 unsigned Alignment=0);
397 SDOperand getTruncStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
Duncan Sands92c43912008-06-06 12:08:01 +0000398 const Value *SV, int SVOffset, MVT TVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 bool isVolatile=false, unsigned Alignment=0);
400 SDOperand getIndexedStore(SDOperand OrigStoe, SDOperand Base,
401 SDOperand Offset, ISD::MemIndexedMode AM);
402
Dan Gohman12a9c082008-02-06 22:27:42 +0000403 // getSrcValue - Construct a node to track a Value* through the backend.
404 SDOperand getSrcValue(const Value *v);
405
406 // getMemOperand - Construct a node to track a memory reference
407 // through the backend.
Dan Gohman1fad9e62008-04-07 19:35:22 +0000408 SDOperand getMemOperand(const MachineMemOperand &MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409
410 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
411 /// specified operands. If the resultant node already exists in the DAG,
412 /// this does not modify the specified node, instead it returns the node that
413 /// already exists. If the resultant node does not exist in the DAG, the
414 /// input node is returned. As a degenerate case, if you specify the same
415 /// input operands as the node already has, the input node is returned.
416 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
417 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
418 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
419 SDOperand Op3);
420 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
421 SDOperand Op3, SDOperand Op4);
422 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
423 SDOperand Op3, SDOperand Op4, SDOperand Op5);
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000424 SDOperand UpdateNodeOperands(SDOperand N, SDOperandPtr Ops, unsigned NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425
426 /// SelectNodeTo - These are used for target selectors to *mutate* the
427 /// specified node to have the specified return type, Target opcode, and
428 /// operands. Note that target opcodes are stored as
429 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. The 0th value
430 /// of the resultant node is returned.
Duncan Sands92c43912008-06-06 12:08:01 +0000431 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT);
432 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT, SDOperand Op1);
433 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 SDOperand Op1, SDOperand Op2);
Duncan Sands92c43912008-06-06 12:08:01 +0000435 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 SDOperand Op1, SDOperand Op2, SDOperand Op3);
Duncan Sands92c43912008-06-06 12:08:01 +0000437 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000438 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000439 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
440 MVT VT2, SDOperand Op1, SDOperand Op2);
441 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
442 MVT VT2, SDOperand Op1, SDOperand Op2, SDOperand Op3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443
444
445 /// getTargetNode - These are used for target selectors to create a new node
446 /// with specified return type(s), target opcode, and operands.
447 ///
448 /// Note that getTargetNode returns the resultant node. If there is already a
449 /// node of the specified opcode and operands, it returns that node instead of
450 /// the current one.
Duncan Sands92c43912008-06-06 12:08:01 +0000451 SDNode *getTargetNode(unsigned Opcode, MVT VT);
452 SDNode *getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1);
453 SDNode *getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1, SDOperand Op2);
454 SDNode *getTargetNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 SDOperand Op1, SDOperand Op2, SDOperand Op3);
Duncan Sands92c43912008-06-06 12:08:01 +0000456 SDNode *getTargetNode(unsigned Opcode, MVT VT,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000457 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000458 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2);
459 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, SDOperand Op1);
460 SDNode *getTargetNode(unsigned Opcode, MVT VT1,
461 MVT VT2, SDOperand Op1, SDOperand Op2);
462 SDNode *getTargetNode(unsigned Opcode, MVT VT1,
463 MVT VT2, SDOperand Op1, SDOperand Op2, SDOperand Op3);
464 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000465 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000466 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 SDOperand Op1, SDOperand Op2);
Duncan Sands92c43912008-06-06 12:08:01 +0000468 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 SDOperand Op1, SDOperand Op2, SDOperand Op3);
Duncan Sands92c43912008-06-06 12:08:01 +0000470 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000471 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000472 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3, MVT VT4,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000473 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000474 SDNode *getTargetNode(unsigned Opcode, std::vector<MVT> &ResultTys,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000475 SDOperandPtr Ops, unsigned NumOps);
Evan Chengd1113582008-03-22 01:55:50 +0000476
477 /// getNodeIfExists - Get the specified node if it's already available, or
478 /// else return NULL.
479 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000480 SDOperandPtr Ops, unsigned NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000482 /// DAGUpdateListener - Clients of various APIs that cause global effects on
483 /// the DAG can optionally implement this interface. This allows the clients
484 /// to handle the various sorts of updates that happen.
485 class DAGUpdateListener {
486 public:
487 virtual ~DAGUpdateListener();
488 virtual void NodeDeleted(SDNode *N) = 0;
489 virtual void NodeUpdated(SDNode *N) = 0;
490 };
491
492 /// RemoveDeadNode - Remove the specified node from the system. If any of its
493 /// operands then becomes dead, remove them as well. Inform UpdateListener
494 /// for each node deleted.
495 void RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener = 0);
496
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
498 /// This can cause recursive merging of nodes in the DAG. Use the first
499 /// version if 'From' is known to have a single result, use the second
500 /// if you have two nodes with identical results, use the third otherwise.
501 ///
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000502 /// These methods all take an optional UpdateListener, which (if not null) is
503 /// informed about nodes that are deleted and modified due to recursive
504 /// changes in the dag.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 ///
506 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000507 DAGUpdateListener *UpdateListener = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000509 DAGUpdateListener *UpdateListener = 0);
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000510 void ReplaceAllUsesWith(SDNode *From, SDOperandPtr To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000511 DAGUpdateListener *UpdateListener = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512
513 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000514 /// uses of other values produced by From.Val alone.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000516 DAGUpdateListener *UpdateListener = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517
518 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
519 /// their allnodes order. It returns the maximum id.
520 unsigned AssignNodeIds();
521
522 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
523 /// based on their topological order. It returns the maximum id and a vector
524 /// of the SDNodes* in assigned order by reference.
525 unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
526
527 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
528 /// operation.
529 static bool isCommutativeBinOp(unsigned Opcode) {
Chris Lattner94a4bcd2008-01-25 06:20:20 +0000530 // FIXME: This should get its info from the td file, so that we can include
531 // target info.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 switch (Opcode) {
533 case ISD::ADD:
534 case ISD::MUL:
535 case ISD::MULHU:
536 case ISD::MULHS:
Dan Gohmanad24f692007-10-05 14:09:33 +0000537 case ISD::SMUL_LOHI:
538 case ISD::UMUL_LOHI:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 case ISD::FADD:
540 case ISD::FMUL:
541 case ISD::AND:
542 case ISD::OR:
543 case ISD::XOR:
544 case ISD::ADDC:
545 case ISD::ADDE: return true;
546 default: return false;
547 }
548 }
549
550 void dump() const;
551
Chris Lattner53f5aee2007-10-15 17:47:20 +0000552 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
553 /// specified value type.
Duncan Sands92c43912008-06-06 12:08:01 +0000554 SDOperand CreateStackTemporary(MVT VT);
Chris Lattner53f5aee2007-10-15 17:47:20 +0000555
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 /// FoldSetCC - Constant fold a setcc to true or false.
Duncan Sands92c43912008-06-06 12:08:01 +0000557 SDOperand FoldSetCC(MVT VT, SDOperand N1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 SDOperand N2, ISD::CondCode Cond);
559
Dan Gohman07961cd2008-02-25 21:11:39 +0000560 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
561 /// use this predicate to simplify operations downstream.
562 bool SignBitIsZero(SDOperand Op, unsigned Depth = 0) const;
563
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We
565 /// use this predicate to simplify operations downstream. Op and Mask are
566 /// known to be the same type.
Dan Gohman07961cd2008-02-25 21:11:39 +0000567 bool MaskedValueIsZero(SDOperand Op, const APInt &Mask, unsigned Depth = 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 const;
569
570 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
571 /// known to be either zero or one and return them in the KnownZero/KnownOne
572 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
573 /// processing. Targets can implement the computeMaskedBitsForTargetNode
574 /// method in the TargetLowering class to allow target nodes to be understood.
Dan Gohmand0dfc772008-02-13 22:28:48 +0000575 void ComputeMaskedBits(SDOperand Op, const APInt &Mask, APInt &KnownZero,
Dan Gohman229fa052008-02-13 00:35:47 +0000576 APInt &KnownOne, unsigned Depth = 0) const;
577
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 /// ComputeNumSignBits - Return the number of times the sign bit of the
579 /// register is replicated into the other bits. We know that at least 1 bit
580 /// is always equal to the sign bit (itself), but other cases can give us
581 /// information. For example, immediately after an "SRA X, 2", we know that
582 /// the top 3 bits are all equal to each other, so we return 3. Targets can
583 /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
584 /// class to allow target nodes to be understood.
585 unsigned ComputeNumSignBits(SDOperand Op, unsigned Depth = 0) const;
Evan Cheng2e28d622008-02-02 04:07:54 +0000586
587 /// isVerifiedDebugInfoDesc - Returns true if the specified SDOperand has
588 /// been verified as a debug information descriptor.
589 bool isVerifiedDebugInfoDesc(SDOperand Op) const;
Evan Cheng411fc172008-05-13 08:35:03 +0000590
591 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
592 /// element of the result of the vector shuffle.
593 SDOperand getShuffleScalarElt(const SDNode *N, unsigned Idx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594
595private:
596 void RemoveNodeFromCSEMaps(SDNode *N);
597 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
598 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
599 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
600 void *&InsertPos);
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000601 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 void *&InsertPos);
603
604 void DeleteNodeNotInCSEMaps(SDNode *N);
605
606 // List of non-single value types.
Duncan Sands92c43912008-06-06 12:08:01 +0000607 std::list<std::vector<MVT> > VTList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608
609 // Maps to auto-CSE operations.
610 std::vector<CondCodeSDNode*> CondCodeNodes;
611
612 std::vector<SDNode*> ValueTypeNodes;
Duncan Sands92c43912008-06-06 12:08:01 +0000613 std::map<MVT, SDNode*> ExtendedValueTypeNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 std::map<std::string, SDNode*> ExternalSymbols;
615 std::map<std::string, SDNode*> TargetExternalSymbols;
616 std::map<std::string, StringSDNode*> StringNodes;
617};
618
619template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
620 typedef SelectionDAG::allnodes_iterator nodes_iterator;
621 static nodes_iterator nodes_begin(SelectionDAG *G) {
622 return G->allnodes_begin();
623 }
624 static nodes_iterator nodes_end(SelectionDAG *G) {
625 return G->allnodes_end();
626 }
627};
628
629} // end namespace llvm
630
631#endif