blob: dee6bc746e7ea3e1c884ee230ed61167c92e67b6 [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"
Dan Gohmanc96298c2008-06-23 21:08:32 +000019#include "llvm/ADT/StringMap.h"
Anton Korobeynikova9db7a22008-05-29 17:41:17 +000020#include "llvm/ADT/ilist.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/SelectionDAGNodes.h"
22
23#include <list>
24#include <vector>
25#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include <string>
27
28namespace llvm {
29 class AliasAnalysis;
30 class TargetLowering;
31 class TargetMachine;
32 class MachineModuleInfo;
33 class MachineFunction;
34 class MachineConstantPoolValue;
Chris Lattner68068cc2008-06-17 06:09:18 +000035 class FunctionLoweringInfo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036
37/// SelectionDAG class - This is used to represent a portion of an LLVM function
38/// in a low-level Data Dependence DAG representation suitable for instruction
39/// selection. This DAG is constructed as the first step of instruction
40/// selection in order to allow implementation of machine specific optimizations
41/// and code simplifications.
42///
43/// The representation used by the SelectionDAG is a target-independent
44/// representation, which has some similarities to the GCC RTL representation,
45/// but is significantly more simple, powerful, and is a graph form instead of a
46/// linear form.
47///
48class SelectionDAG {
49 TargetLowering &TLI;
50 MachineFunction &MF;
Chris Lattner68068cc2008-06-17 06:09:18 +000051 FunctionLoweringInfo &FLI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 MachineModuleInfo *MMI;
53
54 /// Root - The root of the entire DAG. EntryNode - The starting token.
55 SDOperand Root, EntryNode;
56
57 /// AllNodes - A linked list of nodes in the current DAG.
58 ilist<SDNode> AllNodes;
59
60 /// CSEMap - This structure is used to memoize nodes, automatically performing
61 /// CSE with existing nodes with a duplicate is requested.
62 FoldingSet<SDNode> CSEMap;
63
64public:
Chris Lattner68068cc2008-06-17 06:09:18 +000065 SelectionDAG(TargetLowering &tli, MachineFunction &mf,
66 FunctionLoweringInfo &fli, MachineModuleInfo *mmi)
67 : TLI(tli), MF(mf), FLI(fli), MMI(mmi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
69 }
70 ~SelectionDAG();
71
72 MachineFunction &getMachineFunction() const { return MF; }
73 const TargetMachine &getTarget() const;
74 TargetLowering &getTargetLoweringInfo() const { return TLI; }
Chris Lattner68068cc2008-06-17 06:09:18 +000075 FunctionLoweringInfo &getFunctionLoweringInfo() const { return FLI; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
77
78 /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
79 ///
80 void viewGraph();
81
82#ifndef NDEBUG
83 std::map<const SDNode *, std::string> NodeGraphAttrs;
84#endif
85
86 /// clearGraphAttrs - Clear all previously defined node graph attributes.
87 /// Intended to be used from a debugging tool (eg. gdb).
88 void clearGraphAttrs();
89
90 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
91 ///
92 void setGraphAttrs(const SDNode *N, const char *Attrs);
93
94 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
95 /// Used from getNodeAttributes.
96 const std::string getGraphAttrs(const SDNode *N) const;
97
98 /// setGraphColor - Convenience for setting node color attribute.
99 ///
100 void setGraphColor(const SDNode *N, const char *Color);
101
102 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
103 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
104 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
105 typedef ilist<SDNode>::iterator allnodes_iterator;
106 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
107 allnodes_iterator allnodes_end() { return AllNodes.end(); }
Dan Gohman4c5e2d72008-06-20 16:03:16 +0000108 ilist<SDNode>::size_type allnodes_size() const { return AllNodes.size(); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110 /// getRoot - Return the root tag of the SelectionDAG.
111 ///
112 const SDOperand &getRoot() const { return Root; }
113
114 /// getEntryNode - Return the token chain corresponding to the entry of the
115 /// function.
116 const SDOperand &getEntryNode() const { return EntryNode; }
117
118 /// setRoot - Set the current root tag of the SelectionDAG.
119 ///
120 const SDOperand &setRoot(SDOperand N) { return Root = N; }
121
122 /// Combine - This iterates over the nodes in the SelectionDAG, folding
123 /// certain types of nodes together, or eliminating superfluous nodes. When
124 /// the AfterLegalize argument is set to 'true', Combine takes care not to
125 /// generate any nodes that will be illegal on the target.
126 void Combine(bool AfterLegalize, AliasAnalysis &AA);
127
Chris Lattner8a258202007-10-15 06:10:22 +0000128 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
129 /// only uses types natively supported by the target.
130 ///
131 /// Note that this is an involved process that may invalidate pointers into
132 /// the graph.
133 void LegalizeTypes();
134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
136 /// compatible with the target instruction selector, as indicated by the
137 /// TargetLowering object.
138 ///
139 /// Note that this is an involved process that may invalidate pointers into
140 /// the graph.
141 void Legalize();
142
143 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
144 /// SelectionDAG.
145 void RemoveDeadNodes();
146
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 /// DeleteNode - Remove the specified node from the system. This node must
148 /// have no referrers.
149 void DeleteNode(SDNode *N);
150
151 /// getVTList - Return an SDVTList that represents the list of values
152 /// specified.
Duncan Sands92c43912008-06-06 12:08:01 +0000153 SDVTList getVTList(MVT VT);
154 SDVTList getVTList(MVT VT1, MVT VT2);
155 SDVTList getVTList(MVT VT1, MVT VT2, MVT VT3);
156 SDVTList getVTList(const MVT *VTs, unsigned NumVTs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
158 /// getNodeValueTypes - These are obsolete, use getVTList instead.
Duncan Sands92c43912008-06-06 12:08:01 +0000159 const MVT *getNodeValueTypes(MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 return getVTList(VT).VTs;
161 }
Duncan Sands92c43912008-06-06 12:08:01 +0000162 const MVT *getNodeValueTypes(MVT VT1, MVT VT2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 return getVTList(VT1, VT2).VTs;
164 }
Duncan Sands92c43912008-06-06 12:08:01 +0000165 const MVT *getNodeValueTypes(MVT VT1, MVT VT2, MVT VT3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 return getVTList(VT1, VT2, VT3).VTs;
167 }
Duncan Sands92c43912008-06-06 12:08:01 +0000168 const MVT *getNodeValueTypes(std::vector<MVT> &vtList) {
Bill Wendling51bacec2008-05-19 20:15:12 +0000169 return getVTList(&vtList[0], (unsigned)vtList.size()).VTs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 }
171
172
173 //===--------------------------------------------------------------------===//
174 // Node creation methods.
175 //
176 SDOperand getString(const std::string &Val);
Duncan Sands92c43912008-06-06 12:08:01 +0000177 SDOperand getConstant(uint64_t Val, MVT VT, bool isTarget = false);
178 SDOperand getConstant(const APInt &Val, MVT VT, bool isTarget = false);
Chris Lattner5872a362008-01-17 07:00:52 +0000179 SDOperand getIntPtrConstant(uint64_t Val, bool isTarget = false);
Duncan Sands92c43912008-06-06 12:08:01 +0000180 SDOperand getTargetConstant(uint64_t Val, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 return getConstant(Val, VT, true);
182 }
Duncan Sands92c43912008-06-06 12:08:01 +0000183 SDOperand getTargetConstant(const APInt &Val, MVT VT) {
Dan Gohmandc458cf2008-02-08 22:59:30 +0000184 return getConstant(Val, VT, true);
185 }
Duncan Sands92c43912008-06-06 12:08:01 +0000186 SDOperand getConstantFP(double Val, MVT VT, bool isTarget = false);
187 SDOperand getConstantFP(const APFloat& Val, MVT VT, bool isTarget = false);
188 SDOperand getTargetConstantFP(double Val, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 return getConstantFP(Val, VT, true);
190 }
Duncan Sands92c43912008-06-06 12:08:01 +0000191 SDOperand getTargetConstantFP(const APFloat& Val, MVT VT) {
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000192 return getConstantFP(Val, VT, true);
193 }
Duncan Sands92c43912008-06-06 12:08:01 +0000194 SDOperand getGlobalAddress(const GlobalValue *GV, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 int offset = 0, bool isTargetGA = false);
Duncan Sands92c43912008-06-06 12:08:01 +0000196 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 int offset = 0) {
198 return getGlobalAddress(GV, VT, offset, true);
199 }
Duncan Sands92c43912008-06-06 12:08:01 +0000200 SDOperand getFrameIndex(int FI, MVT VT, bool isTarget = false);
201 SDOperand getTargetFrameIndex(int FI, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 return getFrameIndex(FI, VT, true);
203 }
Duncan Sands92c43912008-06-06 12:08:01 +0000204 SDOperand getJumpTable(int JTI, MVT VT, bool isTarget = false);
205 SDOperand getTargetJumpTable(int JTI, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 return getJumpTable(JTI, VT, true);
207 }
Duncan Sands92c43912008-06-06 12:08:01 +0000208 SDOperand getConstantPool(Constant *C, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 unsigned Align = 0, int Offs = 0, bool isT=false);
Duncan Sands92c43912008-06-06 12:08:01 +0000210 SDOperand getTargetConstantPool(Constant *C, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 unsigned Align = 0, int Offset = 0) {
212 return getConstantPool(C, VT, Align, Offset, true);
213 }
Duncan Sands92c43912008-06-06 12:08:01 +0000214 SDOperand getConstantPool(MachineConstantPoolValue *C, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 unsigned Align = 0, int Offs = 0, bool isT=false);
216 SDOperand getTargetConstantPool(MachineConstantPoolValue *C,
Duncan Sands92c43912008-06-06 12:08:01 +0000217 MVT VT, unsigned Align = 0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 int Offset = 0) {
219 return getConstantPool(C, VT, Align, Offset, true);
220 }
221 SDOperand getBasicBlock(MachineBasicBlock *MBB);
Duncan Sands92c43912008-06-06 12:08:01 +0000222 SDOperand getExternalSymbol(const char *Sym, MVT VT);
223 SDOperand getTargetExternalSymbol(const char *Sym, MVT VT);
Duncan Sandsc93fae32008-03-21 09:14:45 +0000224 SDOperand getArgFlags(ISD::ArgFlagsTy Flags);
Duncan Sands92c43912008-06-06 12:08:01 +0000225 SDOperand getValueType(MVT);
226 SDOperand getRegister(unsigned Reg, MVT VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227
228 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
229 return getNode(ISD::CopyToReg, MVT::Other, Chain,
230 getRegister(Reg, N.getValueType()), N);
231 }
232
233 // This version of the getCopyToReg method takes an extra operand, which
234 // indicates that there is potentially an incoming flag value (if Flag is not
235 // null) and that there should be a flag result.
236 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
237 SDOperand Flag) {
Duncan Sands92c43912008-06-06 12:08:01 +0000238 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
240 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
241 }
242
243 // Similar to last getCopyToReg() except parameter Reg is a SDOperand
244 SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
245 SDOperand Flag) {
Duncan Sands92c43912008-06-06 12:08:01 +0000246 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 SDOperand Ops[] = { Chain, Reg, N, Flag };
248 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
249 }
250
Duncan Sands92c43912008-06-06 12:08:01 +0000251 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT VT) {
252 const MVT *VTs = getNodeValueTypes(VT, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
254 return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
255 }
256
257 // This version of the getCopyFromReg method takes an extra operand, which
258 // indicates that there is potentially an incoming flag value (if Flag is not
259 // null) and that there should be a flag result.
Duncan Sands92c43912008-06-06 12:08:01 +0000260 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 SDOperand Flag) {
Duncan Sands92c43912008-06-06 12:08:01 +0000262 const MVT *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
264 return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.Val ? 3 : 2);
265 }
266
267 SDOperand getCondCode(ISD::CondCode Cond);
268
269 /// getZeroExtendInReg - Return the expression required to zero extend the Op
270 /// value assuming it was the smaller SrcTy value.
Duncan Sands92c43912008-06-06 12:08:01 +0000271 SDOperand getZeroExtendInReg(SDOperand Op, MVT SrcTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272
273 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
274 /// a flag result (to ensure it's not CSE'd).
275 SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000276 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 SDOperand Ops[] = { Chain, Op };
278 return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
279 }
280
Bill Wendling22f8deb2007-11-13 00:44:25 +0000281 /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
282 /// flag result (to ensure it's not CSE'd).
283 SDOperand getCALLSEQ_END(SDOperand Chain, SDOperand Op1, SDOperand Op2,
284 SDOperand InFlag) {
285 SDVTList NodeTys = getVTList(MVT::Other, MVT::Flag);
286 SmallVector<SDOperand, 4> Ops;
287 Ops.push_back(Chain);
288 Ops.push_back(Op1);
289 Ops.push_back(Op2);
290 Ops.push_back(InFlag);
291 return getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0],
Evan Cheng591bfc82008-05-05 18:30:58 +0000292 (unsigned)Ops.size() - (InFlag.Val == 0 ? 1 : 0));
Bill Wendling22f8deb2007-11-13 00:44:25 +0000293 }
294
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 /// getNode - Gets or creates the specified node.
296 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000297 SDOperand getNode(unsigned Opcode, MVT VT);
298 SDOperand getNode(unsigned Opcode, MVT VT, SDOperand N);
299 SDOperand getNode(unsigned Opcode, MVT VT, SDOperand N1, SDOperand N2);
300 SDOperand getNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 SDOperand N1, SDOperand N2, SDOperand N3);
Duncan Sands92c43912008-06-06 12:08:01 +0000302 SDOperand getNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
Duncan Sands92c43912008-06-06 12:08:01 +0000304 SDOperand getNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
306 SDOperand N5);
Duncan Sands92c43912008-06-06 12:08:01 +0000307 SDOperand getNode(unsigned Opcode, MVT VT, SDOperandPtr Ops, unsigned NumOps);
308 SDOperand getNode(unsigned Opcode, std::vector<MVT> &ResultTys,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000309 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000310 SDOperand getNode(unsigned Opcode, const MVT *VTs, unsigned NumVTs,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000311 SDOperandPtr Ops, unsigned NumOps);
Dan Gohman798d1272007-10-08 15:49:58 +0000312 SDOperand getNode(unsigned Opcode, SDVTList VTs);
313 SDOperand getNode(unsigned Opcode, SDVTList VTs, SDOperand N);
Duncan Sands92c43912008-06-06 12:08:01 +0000314 SDOperand getNode(unsigned Opcode, SDVTList VTs, SDOperand N1, SDOperand N2);
Dan Gohman798d1272007-10-08 15:49:58 +0000315 SDOperand getNode(unsigned Opcode, SDVTList VTs,
316 SDOperand N1, SDOperand N2, SDOperand N3);
317 SDOperand getNode(unsigned Opcode, SDVTList VTs,
318 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
319 SDOperand getNode(unsigned Opcode, SDVTList VTs,
320 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
321 SDOperand N5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 SDOperand getNode(unsigned Opcode, SDVTList VTs,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000323 SDOperandPtr Ops, unsigned NumOps);
Rafael Espindola80825902007-10-19 10:41:11 +0000324
Dan Gohmane8b391e2008-04-12 04:36:06 +0000325 SDOperand getMemcpy(SDOperand Chain, SDOperand Dst, SDOperand Src,
326 SDOperand Size, unsigned Align,
327 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +0000328 const Value *DstSV, uint64_t DstSVOff,
329 const Value *SrcSV, uint64_t SrcSVOff);
Rafael Espindola80825902007-10-19 10:41:11 +0000330
Dan Gohmane8b391e2008-04-12 04:36:06 +0000331 SDOperand getMemmove(SDOperand Chain, SDOperand Dst, SDOperand Src,
Dan Gohman1457fa82008-04-23 17:50:15 +0000332 SDOperand Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +0000333 const Value *DstSV, uint64_t DstOSVff,
334 const Value *SrcSV, uint64_t SrcSVOff);
Rafael Espindola80825902007-10-19 10:41:11 +0000335
Dan Gohmane8b391e2008-04-12 04:36:06 +0000336 SDOperand getMemset(SDOperand Chain, SDOperand Dst, SDOperand Src,
337 SDOperand Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +0000338 const Value *DstSV, uint64_t DstSVOff);
Rafael Espindola80825902007-10-19 10:41:11 +0000339
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 /// getSetCC - Helper function to make it easier to build SetCC's if you just
341 /// have an ISD::CondCode instead of an SDOperand.
342 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000343 SDOperand getSetCC(MVT VT, SDOperand LHS, SDOperand RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 ISD::CondCode Cond) {
345 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
346 }
347
Nate Begeman9a1ce152008-05-12 19:40:03 +0000348 /// getVSetCC - Helper function to make it easier to build VSetCC's nodes
349 /// if you just have an ISD::CondCode instead of an SDOperand.
350 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000351 SDOperand getVSetCC(MVT VT, SDOperand LHS, SDOperand RHS,
Nate Begeman9a1ce152008-05-12 19:40:03 +0000352 ISD::CondCode Cond) {
353 return getNode(ISD::VSETCC, VT, LHS, RHS, getCondCode(Cond));
354 }
355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
357 /// just have an ISD::CondCode instead of an SDOperand.
358 ///
359 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
360 SDOperand True, SDOperand False, ISD::CondCode Cond) {
361 return getNode(ISD::SELECT_CC, True.getValueType(), LHS, RHS, True, False,
362 getCondCode(Cond));
363 }
364
365 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
366 /// and a source value as input.
Duncan Sands92c43912008-06-06 12:08:01 +0000367 SDOperand getVAArg(MVT VT, SDOperand Chain, SDOperand Ptr,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 SDOperand SV);
369
Andrew Lenharthe44f3902008-02-21 06:45:13 +0000370 /// getAtomic - Gets a node for an atomic op, produces result and chain, takes
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000371 /// 3 operands
Andrew Lenharthe44f3902008-02-21 06:45:13 +0000372 SDOperand getAtomic(unsigned Opcode, SDOperand Chain, SDOperand Ptr,
Dan Gohmanc70fa752008-06-25 16:07:49 +0000373 SDOperand Cmp, SDOperand Swp, const Value* PtrVal,
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000374 unsigned Alignment=0);
Andrew Lenharthe44f3902008-02-21 06:45:13 +0000375
376 /// getAtomic - Gets a node for an atomic op, produces result and chain, takes
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000377 /// 2 operands
Andrew Lenharthe44f3902008-02-21 06:45:13 +0000378 SDOperand getAtomic(unsigned Opcode, SDOperand Chain, SDOperand Ptr,
Dan Gohmanc70fa752008-06-25 16:07:49 +0000379 SDOperand Val, const Value* PtrVal,
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000380 unsigned Alignment = 0);
Andrew Lenharthe44f3902008-02-21 06:45:13 +0000381
Duncan Sandsf19591c2008-06-30 10:19:09 +0000382 /// getMergeValues - Create a MERGE_VALUES node from the given types and ops.
383 /// Allowed to return something different (and simpler) if Simplify is true.
384 SDOperand getMergeValues(SDVTList VTs, SDOperandPtr Ops, unsigned NumOps,
385 bool Simplify = true) {
386 if (Simplify && NumOps == 1)
387 return Ops[0];
388 return getNode(ISD::MERGE_VALUES, VTs, Ops, NumOps);
389 }
390
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 /// getLoad - Loads are not normal binary operators: their result type is not
392 /// determined by their operands, and they produce a value AND a token chain.
393 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000394 SDOperand getLoad(MVT VT, SDOperand Chain, SDOperand Ptr,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 const Value *SV, int SVOffset, bool isVolatile=false,
396 unsigned Alignment=0);
Duncan Sands92c43912008-06-06 12:08:01 +0000397 SDOperand getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 SDOperand Chain, SDOperand Ptr, const Value *SV,
Duncan Sands92c43912008-06-06 12:08:01 +0000399 int SVOffset, MVT EVT, bool isVolatile=false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 unsigned Alignment=0);
401 SDOperand getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
402 SDOperand Offset, ISD::MemIndexedMode AM);
Duncan Sandsb2589712008-03-28 09:45:24 +0000403 SDOperand getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands92c43912008-06-06 12:08:01 +0000404 MVT VT, SDOperand Chain,
Duncan Sandsb2589712008-03-28 09:45:24 +0000405 SDOperand Ptr, SDOperand Offset,
Duncan Sands92c43912008-06-06 12:08:01 +0000406 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandsb2589712008-03-28 09:45:24 +0000407 bool isVolatile=false, unsigned Alignment=0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408
409 /// getStore - Helper function to build ISD::STORE nodes.
410 ///
411 SDOperand getStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
412 const Value *SV, int SVOffset, bool isVolatile=false,
413 unsigned Alignment=0);
414 SDOperand getTruncStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
Duncan Sands92c43912008-06-06 12:08:01 +0000415 const Value *SV, int SVOffset, MVT TVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 bool isVolatile=false, unsigned Alignment=0);
417 SDOperand getIndexedStore(SDOperand OrigStoe, SDOperand Base,
418 SDOperand Offset, ISD::MemIndexedMode AM);
419
Dan Gohman12a9c082008-02-06 22:27:42 +0000420 // getSrcValue - Construct a node to track a Value* through the backend.
421 SDOperand getSrcValue(const Value *v);
422
423 // getMemOperand - Construct a node to track a memory reference
424 // through the backend.
Dan Gohman1fad9e62008-04-07 19:35:22 +0000425 SDOperand getMemOperand(const MachineMemOperand &MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426
427 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
428 /// specified operands. If the resultant node already exists in the DAG,
429 /// this does not modify the specified node, instead it returns the node that
430 /// already exists. If the resultant node does not exist in the DAG, the
431 /// input node is returned. As a degenerate case, if you specify the same
432 /// input operands as the node already has, the input node is returned.
433 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
434 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
435 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
436 SDOperand Op3);
437 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
438 SDOperand Op3, SDOperand Op4);
439 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
440 SDOperand Op3, SDOperand Op4, SDOperand Op5);
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000441 SDOperand UpdateNodeOperands(SDOperand N, SDOperandPtr Ops, unsigned NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442
443 /// SelectNodeTo - These are used for target selectors to *mutate* the
444 /// specified node to have the specified return type, Target opcode, and
445 /// operands. Note that target opcodes are stored as
446 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. The 0th value
447 /// of the resultant node is returned.
Duncan Sands92c43912008-06-06 12:08:01 +0000448 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT);
449 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT, SDOperand Op1);
450 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 SDOperand Op1, SDOperand Op2);
Duncan Sands92c43912008-06-06 12:08:01 +0000452 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 SDOperand Op1, SDOperand Op2, SDOperand Op3);
Duncan Sands92c43912008-06-06 12:08:01 +0000454 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000455 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000456 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
457 MVT VT2, SDOperand Op1, SDOperand Op2);
458 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
459 MVT VT2, SDOperand Op1, SDOperand Op2, SDOperand Op3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460
461
462 /// getTargetNode - These are used for target selectors to create a new node
463 /// with specified return type(s), target opcode, and operands.
464 ///
465 /// Note that getTargetNode returns the resultant node. If there is already a
466 /// node of the specified opcode and operands, it returns that node instead of
467 /// the current one.
Duncan Sands92c43912008-06-06 12:08:01 +0000468 SDNode *getTargetNode(unsigned Opcode, MVT VT);
469 SDNode *getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1);
470 SDNode *getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1, SDOperand Op2);
471 SDNode *getTargetNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 SDOperand Op1, SDOperand Op2, SDOperand Op3);
Duncan Sands92c43912008-06-06 12:08:01 +0000473 SDNode *getTargetNode(unsigned Opcode, MVT VT,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000474 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000475 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2);
476 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, SDOperand Op1);
477 SDNode *getTargetNode(unsigned Opcode, MVT VT1,
478 MVT VT2, SDOperand Op1, SDOperand Op2);
479 SDNode *getTargetNode(unsigned Opcode, MVT VT1,
480 MVT VT2, SDOperand Op1, SDOperand Op2, SDOperand Op3);
481 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000482 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000483 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 SDOperand Op1, SDOperand Op2);
Duncan Sands92c43912008-06-06 12:08:01 +0000485 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 SDOperand Op1, SDOperand Op2, SDOperand Op3);
Duncan Sands92c43912008-06-06 12:08:01 +0000487 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000488 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000489 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3, MVT VT4,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000490 SDOperandPtr Ops, unsigned NumOps);
Duncan Sands92c43912008-06-06 12:08:01 +0000491 SDNode *getTargetNode(unsigned Opcode, std::vector<MVT> &ResultTys,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000492 SDOperandPtr Ops, unsigned NumOps);
Evan Chengd1113582008-03-22 01:55:50 +0000493
494 /// getNodeIfExists - Get the specified node if it's already available, or
495 /// else return NULL.
496 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000497 SDOperandPtr Ops, unsigned NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000499 /// DAGUpdateListener - Clients of various APIs that cause global effects on
500 /// the DAG can optionally implement this interface. This allows the clients
501 /// to handle the various sorts of updates that happen.
502 class DAGUpdateListener {
503 public:
504 virtual ~DAGUpdateListener();
Duncan Sands3866b1c2008-06-11 11:42:12 +0000505
506 /// NodeDeleted - The node N that was deleted and, if E is not null, an
507 /// equivalent node E that replaced it.
508 virtual void NodeDeleted(SDNode *N, SDNode *E) = 0;
509
510 /// NodeUpdated - The node N that was updated.
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000511 virtual void NodeUpdated(SDNode *N) = 0;
512 };
513
514 /// RemoveDeadNode - Remove the specified node from the system. If any of its
515 /// operands then becomes dead, remove them as well. Inform UpdateListener
516 /// for each node deleted.
517 void RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener = 0);
518
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
520 /// This can cause recursive merging of nodes in the DAG. Use the first
521 /// version if 'From' is known to have a single result, use the second
522 /// if you have two nodes with identical results, use the third otherwise.
523 ///
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000524 /// These methods all take an optional UpdateListener, which (if not null) is
525 /// informed about nodes that are deleted and modified due to recursive
526 /// changes in the dag.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 ///
528 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000529 DAGUpdateListener *UpdateListener = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000531 DAGUpdateListener *UpdateListener = 0);
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000532 void ReplaceAllUsesWith(SDNode *From, SDOperandPtr To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000533 DAGUpdateListener *UpdateListener = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534
535 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000536 /// uses of other values produced by From.Val alone.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000538 DAGUpdateListener *UpdateListener = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539
540 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
541 /// their allnodes order. It returns the maximum id.
542 unsigned AssignNodeIds();
543
544 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
545 /// based on their topological order. It returns the maximum id and a vector
546 /// of the SDNodes* in assigned order by reference.
547 unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
548
549 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
550 /// operation.
551 static bool isCommutativeBinOp(unsigned Opcode) {
Chris Lattner94a4bcd2008-01-25 06:20:20 +0000552 // FIXME: This should get its info from the td file, so that we can include
553 // target info.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 switch (Opcode) {
555 case ISD::ADD:
556 case ISD::MUL:
557 case ISD::MULHU:
558 case ISD::MULHS:
Dan Gohmanad24f692007-10-05 14:09:33 +0000559 case ISD::SMUL_LOHI:
560 case ISD::UMUL_LOHI:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 case ISD::FADD:
562 case ISD::FMUL:
563 case ISD::AND:
564 case ISD::OR:
565 case ISD::XOR:
566 case ISD::ADDC:
567 case ISD::ADDE: return true;
568 default: return false;
569 }
570 }
571
572 void dump() const;
573
Chris Lattner53f5aee2007-10-15 17:47:20 +0000574 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
575 /// specified value type.
Duncan Sands92c43912008-06-06 12:08:01 +0000576 SDOperand CreateStackTemporary(MVT VT);
Chris Lattner53f5aee2007-10-15 17:47:20 +0000577
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 /// FoldSetCC - Constant fold a setcc to true or false.
Duncan Sands92c43912008-06-06 12:08:01 +0000579 SDOperand FoldSetCC(MVT VT, SDOperand N1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 SDOperand N2, ISD::CondCode Cond);
581
Dan Gohman07961cd2008-02-25 21:11:39 +0000582 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
583 /// use this predicate to simplify operations downstream.
584 bool SignBitIsZero(SDOperand Op, unsigned Depth = 0) const;
585
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We
587 /// use this predicate to simplify operations downstream. Op and Mask are
588 /// known to be the same type.
Dan Gohman07961cd2008-02-25 21:11:39 +0000589 bool MaskedValueIsZero(SDOperand Op, const APInt &Mask, unsigned Depth = 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 const;
591
592 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
593 /// known to be either zero or one and return them in the KnownZero/KnownOne
594 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
595 /// processing. Targets can implement the computeMaskedBitsForTargetNode
596 /// method in the TargetLowering class to allow target nodes to be understood.
Dan Gohmand0dfc772008-02-13 22:28:48 +0000597 void ComputeMaskedBits(SDOperand Op, const APInt &Mask, APInt &KnownZero,
Dan Gohman229fa052008-02-13 00:35:47 +0000598 APInt &KnownOne, unsigned Depth = 0) const;
599
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 /// ComputeNumSignBits - Return the number of times the sign bit of the
601 /// register is replicated into the other bits. We know that at least 1 bit
602 /// is always equal to the sign bit (itself), but other cases can give us
603 /// information. For example, immediately after an "SRA X, 2", we know that
604 /// the top 3 bits are all equal to each other, so we return 3. Targets can
605 /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
606 /// class to allow target nodes to be understood.
607 unsigned ComputeNumSignBits(SDOperand Op, unsigned Depth = 0) const;
Evan Cheng2e28d622008-02-02 04:07:54 +0000608
609 /// isVerifiedDebugInfoDesc - Returns true if the specified SDOperand has
610 /// been verified as a debug information descriptor.
611 bool isVerifiedDebugInfoDesc(SDOperand Op) const;
Evan Cheng411fc172008-05-13 08:35:03 +0000612
613 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
614 /// element of the result of the vector shuffle.
615 SDOperand getShuffleScalarElt(const SDNode *N, unsigned Idx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616
617private:
618 void RemoveNodeFromCSEMaps(SDNode *N);
619 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
620 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
621 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
622 void *&InsertPos);
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000623 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 void *&InsertPos);
625
626 void DeleteNodeNotInCSEMaps(SDNode *N);
627
628 // List of non-single value types.
Duncan Sands92c43912008-06-06 12:08:01 +0000629 std::list<std::vector<MVT> > VTList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630
631 // Maps to auto-CSE operations.
632 std::vector<CondCodeSDNode*> CondCodeNodes;
633
634 std::vector<SDNode*> ValueTypeNodes;
Duncan Sandsec142ee2008-06-08 20:54:56 +0000635 std::map<MVT, SDNode*, MVT::compareRawBits> ExtendedValueTypeNodes;
Dan Gohmanc96298c2008-06-23 21:08:32 +0000636 StringMap<SDNode*> ExternalSymbols;
637 StringMap<SDNode*> TargetExternalSymbols;
638 StringMap<StringSDNode*> StringNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639};
640
641template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
642 typedef SelectionDAG::allnodes_iterator nodes_iterator;
643 static nodes_iterator nodes_begin(SelectionDAG *G) {
644 return G->allnodes_begin();
645 }
646 static nodes_iterator nodes_end(SelectionDAG *G) {
647 return G->allnodes_end();
648 }
649};
650
651} // end namespace llvm
652
653#endif