2550d64c1463ce3869fa60faeddad11391f1a556
[oota-llvm.git] / include / llvm / CodeGen / SelectionDAG.h
1 //===-- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
19 #include "llvm/ADT/ilist"
20 #include "llvm/CodeGen/SelectionDAGNodes.h"
21
22 #include <list>
23 #include <vector>
24 #include <map>
25 #include <set>
26 #include <string>
27
28 namespace llvm {
29   class AliasAnalysis;
30   class TargetLowering;
31   class TargetMachine;
32   class MachineModuleInfo;
33   class MachineFunction;
34   class MachineConstantPoolValue;
35
36 /// SelectionDAG class - This is used to represent a portion of an LLVM function
37 /// in a low-level Data Dependence DAG representation suitable for instruction
38 /// selection.  This DAG is constructed as the first step of instruction
39 /// selection in order to allow implementation of machine specific optimizations
40 /// and code simplifications.
41 ///
42 /// The representation used by the SelectionDAG is a target-independent
43 /// representation, which has some similarities to the GCC RTL representation,
44 /// but is significantly more simple, powerful, and is a graph form instead of a
45 /// linear form.
46 ///
47 class SelectionDAG {
48   TargetLowering &TLI;
49   MachineFunction &MF;
50   MachineModuleInfo *MMI;
51
52   /// Root - The root of the entire DAG.  EntryNode - The starting token.
53   SDOperand Root, EntryNode;
54
55   /// AllNodes - A linked list of nodes in the current DAG.
56   ilist<SDNode> AllNodes;
57
58   /// CSEMap - This structure is used to memoize nodes, automatically performing
59   /// CSE with existing nodes with a duplicate is requested.
60   FoldingSet<SDNode> CSEMap;
61
62 public:
63   SelectionDAG(TargetLowering &tli, MachineFunction &mf, MachineModuleInfo *mmi)
64   : TLI(tli), MF(mf), MMI(mmi) {
65     EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
66   }
67   ~SelectionDAG();
68
69   MachineFunction &getMachineFunction() const { return MF; }
70   const TargetMachine &getTarget() const;
71   TargetLowering &getTargetLoweringInfo() const { return TLI; }
72   MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
73
74   /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
75   ///
76   void viewGraph();
77   
78 #ifndef NDEBUG
79   std::map<const SDNode *, std::string> NodeGraphAttrs;
80 #endif
81
82   /// clearGraphAttrs - Clear all previously defined node graph attributes.
83   /// Intended to be used from a debugging tool (eg. gdb).
84   void clearGraphAttrs();
85   
86   /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
87   ///
88   void setGraphAttrs(const SDNode *N, const char *Attrs);
89   
90   /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
91   /// Used from getNodeAttributes.
92   const std::string getGraphAttrs(const SDNode *N) const;
93   
94   /// setGraphColor - Convenience for setting node color attribute.
95   ///
96   void setGraphColor(const SDNode *N, const char *Color);
97
98   typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
99   allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
100   allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
101   typedef ilist<SDNode>::iterator allnodes_iterator;
102   allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
103   allnodes_iterator allnodes_end() { return AllNodes.end(); }
104   
105   /// getRoot - Return the root tag of the SelectionDAG.
106   ///
107   const SDOperand &getRoot() const { return Root; }
108
109   /// getEntryNode - Return the token chain corresponding to the entry of the
110   /// function.
111   const SDOperand &getEntryNode() const { return EntryNode; }
112
113   /// setRoot - Set the current root tag of the SelectionDAG.
114   ///
115   const SDOperand &setRoot(SDOperand N) { return Root = N; }
116
117   /// Combine - This iterates over the nodes in the SelectionDAG, folding
118   /// certain types of nodes together, or eliminating superfluous nodes.  When
119   /// the AfterLegalize argument is set to 'true', Combine takes care not to
120   /// generate any nodes that will be illegal on the target.
121   void Combine(bool AfterLegalize, AliasAnalysis &AA);
122   
123   /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
124   /// only uses types natively supported by the target.
125   ///
126   /// Note that this is an involved process that may invalidate pointers into
127   /// the graph.
128   void LegalizeTypes();
129   
130   /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
131   /// compatible with the target instruction selector, as indicated by the
132   /// TargetLowering object.
133   ///
134   /// Note that this is an involved process that may invalidate pointers into
135   /// the graph.
136   void Legalize();
137
138   /// RemoveDeadNodes - This method deletes all unreachable nodes in the
139   /// SelectionDAG.
140   void RemoveDeadNodes();
141
142   /// DeleteNode - Remove the specified node from the system.  This node must
143   /// have no referrers.
144   void DeleteNode(SDNode *N);
145
146   /// getVTList - Return an SDVTList that represents the list of values
147   /// specified.
148   SDVTList getVTList(MVT::ValueType VT);
149   SDVTList getVTList(MVT::ValueType VT1, MVT::ValueType VT2);
150   SDVTList getVTList(MVT::ValueType VT1, MVT::ValueType VT2,MVT::ValueType VT3);
151   SDVTList getVTList(const MVT::ValueType *VTs, unsigned NumVTs);
152   
153   /// getNodeValueTypes - These are obsolete, use getVTList instead.
154   const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT) {
155     return getVTList(VT).VTs;
156   }
157   const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1, 
158                                           MVT::ValueType VT2) {
159     return getVTList(VT1, VT2).VTs;
160   }
161   const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1,MVT::ValueType VT2,
162                                           MVT::ValueType VT3) {
163     return getVTList(VT1, VT2, VT3).VTs;
164   }
165   const MVT::ValueType *getNodeValueTypes(std::vector<MVT::ValueType> &VTList) {
166     return getVTList(&VTList[0], VTList.size()).VTs;
167   }
168   
169   
170   //===--------------------------------------------------------------------===//
171   // Node creation methods.
172   //
173   SDOperand getString(const std::string &Val);
174   SDOperand getConstant(uint64_t Val, MVT::ValueType VT, bool isTarget = false);
175   SDOperand getIntPtrConstant(uint64_t Val, bool isTarget = false);
176   SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT) {
177     return getConstant(Val, VT, true);
178   }
179   SDOperand getConstantFP(double Val, MVT::ValueType VT, bool isTarget = false);
180   SDOperand getConstantFP(const APFloat& Val, MVT::ValueType VT, 
181                           bool isTarget = false);
182   SDOperand getTargetConstantFP(double Val, MVT::ValueType VT) {
183     return getConstantFP(Val, VT, true);
184   }
185   SDOperand getTargetConstantFP(const APFloat& Val, MVT::ValueType VT) {
186     return getConstantFP(Val, VT, true);
187   }
188   SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
189                              int offset = 0, bool isTargetGA = false);
190   SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
191                                    int offset = 0) {
192     return getGlobalAddress(GV, VT, offset, true);
193   }
194   SDOperand getFrameIndex(int FI, MVT::ValueType VT, bool isTarget = false);
195   SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT) {
196     return getFrameIndex(FI, VT, true);
197   }
198   SDOperand getJumpTable(int JTI, MVT::ValueType VT, bool isTarget = false);
199   SDOperand getTargetJumpTable(int JTI, MVT::ValueType VT) {
200     return getJumpTable(JTI, VT, true);
201   }
202   SDOperand getConstantPool(Constant *C, MVT::ValueType VT,
203                             unsigned Align = 0, int Offs = 0, bool isT=false);
204   SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT,
205                                   unsigned Align = 0, int Offset = 0) {
206     return getConstantPool(C, VT, Align, Offset, true);
207   }
208   SDOperand getConstantPool(MachineConstantPoolValue *C, MVT::ValueType VT,
209                             unsigned Align = 0, int Offs = 0, bool isT=false);
210   SDOperand getTargetConstantPool(MachineConstantPoolValue *C,
211                                   MVT::ValueType VT, unsigned Align = 0,
212                                   int Offset = 0) {
213     return getConstantPool(C, VT, Align, Offset, true);
214   }
215   SDOperand getBasicBlock(MachineBasicBlock *MBB);
216   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
217   SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
218   SDOperand getValueType(MVT::ValueType);
219   SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
220
221   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
222     return getNode(ISD::CopyToReg, MVT::Other, Chain,
223                    getRegister(Reg, N.getValueType()), N);
224   }
225
226   // This version of the getCopyToReg method takes an extra operand, which
227   // indicates that there is potentially an incoming flag value (if Flag is not
228   // null) and that there should be a flag result.
229   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
230                          SDOperand Flag) {
231     const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
232     SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
233     return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
234   }
235
236   // Similar to last getCopyToReg() except parameter Reg is a SDOperand
237   SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
238                          SDOperand Flag) {
239     const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
240     SDOperand Ops[] = { Chain, Reg, N, Flag };
241     return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
242   }
243   
244   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
245     const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
246     SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
247     return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
248   }
249   
250   // This version of the getCopyFromReg method takes an extra operand, which
251   // indicates that there is potentially an incoming flag value (if Flag is not
252   // null) and that there should be a flag result.
253   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
254                            SDOperand Flag) {
255     const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
256     SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
257     return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.Val ? 3 : 2);
258   }
259
260   SDOperand getCondCode(ISD::CondCode Cond);
261
262   /// getZeroExtendInReg - Return the expression required to zero extend the Op
263   /// value assuming it was the smaller SrcTy value.
264   SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
265   
266   /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
267   /// a flag result (to ensure it's not CSE'd).
268   SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
269     const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
270     SDOperand Ops[] = { Chain,  Op };
271     return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
272   }
273
274   /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
275   /// flag result (to ensure it's not CSE'd).
276   SDOperand getCALLSEQ_END(SDOperand Chain, SDOperand Op1, SDOperand Op2,
277                            SDOperand InFlag) {
278     SDVTList NodeTys = getVTList(MVT::Other, MVT::Flag);
279     SmallVector<SDOperand, 4> Ops;
280     Ops.push_back(Chain);
281     Ops.push_back(Op1);
282     Ops.push_back(Op2);
283     Ops.push_back(InFlag);
284     return getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0],
285                    Ops.size() - (InFlag.Val == 0 ? 1 : 0));
286   }
287
288   /// getNode - Gets or creates the specified node.
289   ///
290   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
291   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
292   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
293                     SDOperand N1, SDOperand N2);
294   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
295                     SDOperand N1, SDOperand N2, SDOperand N3);
296   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
297                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
298   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
299                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
300                     SDOperand N5);
301   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
302                     const SDOperand *Ops, unsigned NumOps);
303   SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
304                     const SDOperand *Ops, unsigned NumOps);
305   SDOperand getNode(unsigned Opcode, const MVT::ValueType *VTs, unsigned NumVTs,
306                     const SDOperand *Ops, unsigned NumOps);
307   SDOperand getNode(unsigned Opcode, SDVTList VTs);
308   SDOperand getNode(unsigned Opcode, SDVTList VTs, SDOperand N);
309   SDOperand getNode(unsigned Opcode, SDVTList VTs,
310                     SDOperand N1, SDOperand N2);
311   SDOperand getNode(unsigned Opcode, SDVTList VTs,
312                     SDOperand N1, SDOperand N2, SDOperand N3);
313   SDOperand getNode(unsigned Opcode, SDVTList VTs,
314                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
315   SDOperand getNode(unsigned Opcode, SDVTList VTs,
316                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
317                     SDOperand N5);
318   SDOperand getNode(unsigned Opcode, SDVTList VTs,
319                     const SDOperand *Ops, unsigned NumOps);
320
321   SDOperand getMemcpy(SDOperand Chain, SDOperand Dest, SDOperand Src,
322                       SDOperand Size, SDOperand Align,
323                       SDOperand AlwaysInline);
324
325   SDOperand getMemmove(SDOperand Chain, SDOperand Dest, SDOperand Src,
326                       SDOperand Size, SDOperand Align,
327                       SDOperand AlwaysInline);
328
329   SDOperand getMemset(SDOperand Chain, SDOperand Dest, SDOperand Src,
330                       SDOperand Size, SDOperand Align,
331                       SDOperand AlwaysInline);
332
333   /// getSetCC - Helper function to make it easier to build SetCC's if you just
334   /// have an ISD::CondCode instead of an SDOperand.
335   ///
336   SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
337                      ISD::CondCode Cond) {
338     return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
339   }
340
341   /// getSelectCC - Helper function to make it easier to build SelectCC's if you
342   /// just have an ISD::CondCode instead of an SDOperand.
343   ///
344   SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
345                         SDOperand True, SDOperand False, ISD::CondCode Cond) {
346     return getNode(ISD::SELECT_CC, True.getValueType(), LHS, RHS, True, False,
347                    getCondCode(Cond));
348   }
349   
350   /// getVAArg - VAArg produces a result and token chain, and takes a pointer
351   /// and a source value as input.
352   SDOperand getVAArg(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
353                      SDOperand SV);
354
355   /// getLoad - Loads are not normal binary operators: their result type is not
356   /// determined by their operands, and they produce a value AND a token chain.
357   ///
358   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
359                     const Value *SV, int SVOffset, bool isVolatile=false,
360                     unsigned Alignment=0);
361   SDOperand getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
362                        SDOperand Chain, SDOperand Ptr, const Value *SV,
363                        int SVOffset, MVT::ValueType EVT, bool isVolatile=false,
364                        unsigned Alignment=0);
365   SDOperand getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
366                            SDOperand Offset, ISD::MemIndexedMode AM);
367
368   /// getStore - Helper function to build ISD::STORE nodes.
369   ///
370   SDOperand getStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
371                      const Value *SV, int SVOffset, bool isVolatile=false,
372                      unsigned Alignment=0);
373   SDOperand getTruncStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
374                           const Value *SV, int SVOffset, MVT::ValueType TVT,
375                           bool isVolatile=false, unsigned Alignment=0);
376   SDOperand getIndexedStore(SDOperand OrigStoe, SDOperand Base,
377                            SDOperand Offset, ISD::MemIndexedMode AM);
378
379   // getSrcValue - construct a node to track a Value* through the backend
380   SDOperand getSrcValue(const Value* I, int offset = 0);
381
382   /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
383   /// specified operands.  If the resultant node already exists in the DAG,
384   /// this does not modify the specified node, instead it returns the node that
385   /// already exists.  If the resultant node does not exist in the DAG, the
386   /// input node is returned.  As a degenerate case, if you specify the same
387   /// input operands as the node already has, the input node is returned.
388   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
389   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
390   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
391                                SDOperand Op3);
392   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
393                                SDOperand Op3, SDOperand Op4);
394   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
395                                SDOperand Op3, SDOperand Op4, SDOperand Op5);
396   SDOperand UpdateNodeOperands(SDOperand N, SDOperand *Ops, unsigned NumOps);
397   
398   /// SelectNodeTo - These are used for target selectors to *mutate* the
399   /// specified node to have the specified return type, Target opcode, and
400   /// operands.  Note that target opcodes are stored as
401   /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.  The 0th value
402   /// of the resultant node is returned.
403   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
404   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
405                        SDOperand Op1);
406   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
407                        SDOperand Op1, SDOperand Op2);
408   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
409                        SDOperand Op1, SDOperand Op2, SDOperand Op3);
410   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
411                         const SDOperand *Ops, unsigned NumOps);
412   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 
413                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
414   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
415                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
416                        SDOperand Op3);
417
418
419   /// getTargetNode - These are used for target selectors to create a new node
420   /// with specified return type(s), target opcode, and operands.
421   ///
422   /// Note that getTargetNode returns the resultant node.  If there is already a
423   /// node of the specified opcode and operands, it returns that node instead of
424   /// the current one.
425   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT);
426   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
427                         SDOperand Op1);
428   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
429                         SDOperand Op1, SDOperand Op2);
430   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
431                         SDOperand Op1, SDOperand Op2, SDOperand Op3);
432   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
433                         const SDOperand *Ops, unsigned NumOps);
434   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
435                         MVT::ValueType VT2);
436   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
437                         MVT::ValueType VT2, SDOperand Op1);
438   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
439                         MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
440   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
441                         MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
442                         SDOperand Op3);
443   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
444                         MVT::ValueType VT2,
445                         const SDOperand *Ops, unsigned NumOps);
446   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
447                         MVT::ValueType VT2, MVT::ValueType VT3,
448                         SDOperand Op1, SDOperand Op2);
449   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
450                         MVT::ValueType VT2, MVT::ValueType VT3,
451                         SDOperand Op1, SDOperand Op2, SDOperand Op3);
452   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
453                         MVT::ValueType VT2, MVT::ValueType VT3,
454                         const SDOperand *Ops, unsigned NumOps);
455   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
456                         MVT::ValueType VT2, MVT::ValueType VT3,
457                         MVT::ValueType VT4,
458                         const SDOperand *Ops, unsigned NumOps);
459   SDNode *getTargetNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
460                         const SDOperand *Ops, unsigned NumOps);
461   
462   /// DAGUpdateListener - Clients of various APIs that cause global effects on
463   /// the DAG can optionally implement this interface.  This allows the clients
464   /// to handle the various sorts of updates that happen.
465   class DAGUpdateListener {
466   public:
467     virtual ~DAGUpdateListener();
468     virtual void NodeDeleted(SDNode *N) = 0;
469     virtual void NodeUpdated(SDNode *N) = 0;
470   };
471   
472   /// RemoveDeadNode - Remove the specified node from the system. If any of its
473   /// operands then becomes dead, remove them as well. Inform UpdateListener
474   /// for each node deleted.
475   void RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener = 0);
476   
477   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
478   /// This can cause recursive merging of nodes in the DAG.  Use the first
479   /// version if 'From' is known to have a single result, use the second
480   /// if you have two nodes with identical results, use the third otherwise.
481   ///
482   /// These methods all take an optional UpdateListener, which (if not null) is 
483   /// informed about nodes that are deleted and modified due to recursive
484   /// changes in the dag.
485   ///
486   void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
487                           DAGUpdateListener *UpdateListener = 0);
488   void ReplaceAllUsesWith(SDNode *From, SDNode *To,
489                           DAGUpdateListener *UpdateListener = 0);
490   void ReplaceAllUsesWith(SDNode *From, const SDOperand *To,
491                           DAGUpdateListener *UpdateListener = 0);
492
493   /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
494   /// uses of other values produced by From.Val alone.
495   void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
496                                  DAGUpdateListener *UpdateListener = 0);
497
498   /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
499   /// their allnodes order. It returns the maximum id.
500   unsigned AssignNodeIds();
501
502   /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
503   /// based on their topological order. It returns the maximum id and a vector
504   /// of the SDNodes* in assigned order by reference.
505   unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
506
507   /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
508   /// operation.
509   static bool isCommutativeBinOp(unsigned Opcode) {
510     // FIXME: This should get its info from the td file, so that we can include
511     // target info.
512     switch (Opcode) {
513     case ISD::ADD:
514     case ISD::MUL:
515     case ISD::MULHU:
516     case ISD::MULHS:
517     case ISD::SMUL_LOHI:
518     case ISD::UMUL_LOHI:
519     case ISD::FADD:
520     case ISD::FMUL:
521     case ISD::AND:
522     case ISD::OR:
523     case ISD::XOR:
524     case ISD::ADDC: 
525     case ISD::ADDE: return true;
526     default: return false;
527     }
528   }
529
530   void dump() const;
531
532   /// CreateStackTemporary - Create a stack temporary, suitable for holding the
533   /// specified value type.
534   SDOperand CreateStackTemporary(MVT::ValueType VT);
535   
536   /// FoldSetCC - Constant fold a setcc to true or false.
537   SDOperand FoldSetCC(MVT::ValueType VT, SDOperand N1,
538                       SDOperand N2, ISD::CondCode Cond);
539   
540   /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero.  We
541   /// use this predicate to simplify operations downstream.  Op and Mask are
542   /// known to be the same type.
543   bool MaskedValueIsZero(SDOperand Op, uint64_t Mask, unsigned Depth = 0)
544     const;
545   
546   /// ComputeMaskedBits - Determine which of the bits specified in Mask are
547   /// known to be either zero or one and return them in the KnownZero/KnownOne
548   /// bitsets.  This code only analyzes bits in Mask, in order to short-circuit
549   /// processing.  Targets can implement the computeMaskedBitsForTargetNode 
550   /// method in the TargetLowering class to allow target nodes to be understood.
551   void ComputeMaskedBits(SDOperand Op, uint64_t Mask, uint64_t &KnownZero,
552                          uint64_t &KnownOne, unsigned Depth = 0) const;
553     
554   /// ComputeNumSignBits - Return the number of times the sign bit of the
555   /// register is replicated into the other bits.  We know that at least 1 bit
556   /// is always equal to the sign bit (itself), but other cases can give us
557   /// information.  For example, immediately after an "SRA X, 2", we know that
558   /// the top 3 bits are all equal to each other, so we return 3.  Targets can
559   /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
560   /// class to allow target nodes to be understood.
561   unsigned ComputeNumSignBits(SDOperand Op, unsigned Depth = 0) const;
562
563   /// isVerifiedDebugInfoDesc - Returns true if the specified SDOperand has
564   /// been verified as a debug information descriptor.
565   bool isVerifiedDebugInfoDesc(SDOperand Op) const;
566   
567 private:
568   void RemoveNodeFromCSEMaps(SDNode *N);
569   SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
570   SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
571   SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
572                                void *&InsertPos);
573   SDNode *FindModifiedNodeSlot(SDNode *N, const SDOperand *Ops, unsigned NumOps,
574                                void *&InsertPos);
575
576   void DeleteNodeNotInCSEMaps(SDNode *N);
577   
578   // List of non-single value types.
579   std::list<std::vector<MVT::ValueType> > VTList;
580   
581   // Maps to auto-CSE operations.
582   std::vector<CondCodeSDNode*> CondCodeNodes;
583
584   std::vector<SDNode*> ValueTypeNodes;
585   std::map<MVT::ValueType, SDNode*> ExtendedValueTypeNodes;
586   std::map<std::string, SDNode*> ExternalSymbols;
587   std::map<std::string, SDNode*> TargetExternalSymbols;
588   std::map<std::string, StringSDNode*> StringNodes;
589 };
590
591 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
592   typedef SelectionDAG::allnodes_iterator nodes_iterator;
593   static nodes_iterator nodes_begin(SelectionDAG *G) {
594     return G->allnodes_begin();
595   }
596   static nodes_iterator nodes_end(SelectionDAG *G) {
597     return G->allnodes_end();
598   }
599 };
600
601 }  // end namespace llvm
602
603 #endif