76ed7f6f661434b33257dfd75068ca5598143dc9
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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   /// RemoveDeadNode - Remove the specified node from the system. If any of its
143   /// operands then becomes dead, remove them as well. The vector Deleted is
144   /// populated with nodes that are deleted.
145   void RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted);
146   
147   /// 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.
153   SDVTList getVTList(MVT::ValueType VT);
154   SDVTList getVTList(MVT::ValueType VT1, MVT::ValueType VT2);
155   SDVTList getVTList(MVT::ValueType VT1, MVT::ValueType VT2,MVT::ValueType VT3);
156   SDVTList getVTList(const MVT::ValueType *VTs, unsigned NumVTs);
157   
158   /// getNodeValueTypes - These are obsolete, use getVTList instead.
159   const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT) {
160     return getVTList(VT).VTs;
161   }
162   const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1, 
163                                           MVT::ValueType VT2) {
164     return getVTList(VT1, VT2).VTs;
165   }
166   const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1,MVT::ValueType VT2,
167                                           MVT::ValueType VT3) {
168     return getVTList(VT1, VT2, VT3).VTs;
169   }
170   const MVT::ValueType *getNodeValueTypes(std::vector<MVT::ValueType> &VTList) {
171     return getVTList(&VTList[0], VTList.size()).VTs;
172   }
173   
174   
175   //===--------------------------------------------------------------------===//
176   // Node creation methods.
177   //
178   SDOperand getString(const std::string &Val);
179   SDOperand getConstant(uint64_t Val, MVT::ValueType VT, bool isTarget = false);
180   SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT) {
181     return getConstant(Val, VT, true);
182   }
183   SDOperand getConstantFP(double Val, MVT::ValueType VT, bool isTarget = false);
184   SDOperand getConstantFP(const APFloat& Val, MVT::ValueType VT, 
185                           bool isTarget = false);
186   SDOperand getTargetConstantFP(double Val, MVT::ValueType VT) {
187     return getConstantFP(Val, VT, true);
188   }
189   SDOperand getTargetConstantFP(const APFloat& Val, MVT::ValueType VT) {
190     return getConstantFP(Val, VT, true);
191   }
192   SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
193                              int offset = 0, bool isTargetGA = false);
194   SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
195                                    int offset = 0) {
196     return getGlobalAddress(GV, VT, offset, true);
197   }
198   SDOperand getFrameIndex(int FI, MVT::ValueType VT, bool isTarget = false);
199   SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT) {
200     return getFrameIndex(FI, VT, true);
201   }
202   SDOperand getJumpTable(int JTI, MVT::ValueType VT, bool isTarget = false);
203   SDOperand getTargetJumpTable(int JTI, MVT::ValueType VT) {
204     return getJumpTable(JTI, VT, true);
205   }
206   SDOperand getConstantPool(Constant *C, MVT::ValueType VT,
207                             unsigned Align = 0, int Offs = 0, bool isT=false);
208   SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT,
209                                   unsigned Align = 0, int Offset = 0) {
210     return getConstantPool(C, VT, Align, Offset, true);
211   }
212   SDOperand getConstantPool(MachineConstantPoolValue *C, MVT::ValueType VT,
213                             unsigned Align = 0, int Offs = 0, bool isT=false);
214   SDOperand getTargetConstantPool(MachineConstantPoolValue *C,
215                                   MVT::ValueType VT, unsigned Align = 0,
216                                   int Offset = 0) {
217     return getConstantPool(C, VT, Align, Offset, true);
218   }
219   SDOperand getBasicBlock(MachineBasicBlock *MBB);
220   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
221   SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
222   SDOperand getValueType(MVT::ValueType);
223   SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
224
225   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
226     return getNode(ISD::CopyToReg, MVT::Other, Chain,
227                    getRegister(Reg, N.getValueType()), N);
228   }
229
230   // This version of the getCopyToReg method takes an extra operand, which
231   // indicates that there is potentially an incoming flag value (if Flag is not
232   // null) and that there should be a flag result.
233   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
234                          SDOperand Flag) {
235     const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
236     SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
237     return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
238   }
239
240   // Similar to last getCopyToReg() except parameter Reg is a SDOperand
241   SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
242                          SDOperand Flag) {
243     const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
244     SDOperand Ops[] = { Chain, Reg, N, Flag };
245     return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
246   }
247   
248   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
249     const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
250     SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
251     return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
252   }
253   
254   // This version of the getCopyFromReg method takes an extra operand, which
255   // indicates that there is potentially an incoming flag value (if Flag is not
256   // null) and that there should be a flag result.
257   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
258                            SDOperand Flag) {
259     const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
260     SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
261     return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.Val ? 3 : 2);
262   }
263
264   SDOperand getCondCode(ISD::CondCode Cond);
265
266   /// getZeroExtendInReg - Return the expression required to zero extend the Op
267   /// value assuming it was the smaller SrcTy value.
268   SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
269   
270   /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
271   /// a flag result (to ensure it's not CSE'd).
272   SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
273     const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
274     SDOperand Ops[] = { Chain,  Op };
275     return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
276   }
277
278   /// getNode - Gets or creates the specified node.
279   ///
280   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
281   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
282   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
283                     SDOperand N1, SDOperand N2);
284   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
285                     SDOperand N1, SDOperand N2, SDOperand N3);
286   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
287                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
288   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
289                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
290                     SDOperand N5);
291   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
292                     const SDOperand *Ops, unsigned NumOps);
293   SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
294                     const SDOperand *Ops, unsigned NumOps);
295   SDOperand getNode(unsigned Opcode, const MVT::ValueType *VTs, unsigned NumVTs,
296                     const SDOperand *Ops, unsigned NumOps);
297   SDOperand getNode(unsigned Opcode, SDVTList VTs);
298   SDOperand getNode(unsigned Opcode, SDVTList VTs, SDOperand N);
299   SDOperand getNode(unsigned Opcode, SDVTList VTs,
300                     SDOperand N1, SDOperand N2);
301   SDOperand getNode(unsigned Opcode, SDVTList VTs,
302                     SDOperand N1, SDOperand N2, SDOperand N3);
303   SDOperand getNode(unsigned Opcode, SDVTList VTs,
304                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
305   SDOperand getNode(unsigned Opcode, SDVTList VTs,
306                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
307                     SDOperand N5);
308   SDOperand getNode(unsigned Opcode, SDVTList VTs,
309                     const SDOperand *Ops, unsigned NumOps);
310   
311   /// getSetCC - Helper function to make it easier to build SetCC's if you just
312   /// have an ISD::CondCode instead of an SDOperand.
313   ///
314   SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
315                      ISD::CondCode Cond) {
316     return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
317   }
318
319   /// getSelectCC - Helper function to make it easier to build SelectCC's if you
320   /// just have an ISD::CondCode instead of an SDOperand.
321   ///
322   SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
323                         SDOperand True, SDOperand False, ISD::CondCode Cond) {
324     return getNode(ISD::SELECT_CC, True.getValueType(), LHS, RHS, True, False,
325                    getCondCode(Cond));
326   }
327   
328   /// getVAArg - VAArg produces a result and token chain, and takes a pointer
329   /// and a source value as input.
330   SDOperand getVAArg(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
331                      SDOperand SV);
332
333   /// getLoad - Loads are not normal binary operators: their result type is not
334   /// determined by their operands, and they produce a value AND a token chain.
335   ///
336   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
337                     const Value *SV, int SVOffset, bool isVolatile=false,
338                     unsigned Alignment=0);
339   SDOperand getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
340                        SDOperand Chain, SDOperand Ptr, const Value *SV,
341                        int SVOffset, MVT::ValueType EVT, bool isVolatile=false,
342                        unsigned Alignment=0);
343   SDOperand getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
344                            SDOperand Offset, ISD::MemIndexedMode AM);
345
346   /// getStore - Helper function to build ISD::STORE nodes.
347   ///
348   SDOperand getStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
349                      const Value *SV, int SVOffset, bool isVolatile=false,
350                      unsigned Alignment=0);
351   SDOperand getTruncStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
352                           const Value *SV, int SVOffset, MVT::ValueType TVT,
353                           bool isVolatile=false, unsigned Alignment=0);
354   SDOperand getIndexedStore(SDOperand OrigStoe, SDOperand Base,
355                            SDOperand Offset, ISD::MemIndexedMode AM);
356
357   // getSrcValue - construct a node to track a Value* through the backend
358   SDOperand getSrcValue(const Value* I, int offset = 0);
359
360   /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
361   /// specified operands.  If the resultant node already exists in the DAG,
362   /// this does not modify the specified node, instead it returns the node that
363   /// already exists.  If the resultant node does not exist in the DAG, the
364   /// input node is returned.  As a degenerate case, if you specify the same
365   /// input operands as the node already has, the input node is returned.
366   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
367   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
368   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
369                                SDOperand Op3);
370   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
371                                SDOperand Op3, SDOperand Op4);
372   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
373                                SDOperand Op3, SDOperand Op4, SDOperand Op5);
374   SDOperand UpdateNodeOperands(SDOperand N, SDOperand *Ops, unsigned NumOps);
375   
376   /// SelectNodeTo - These are used for target selectors to *mutate* the
377   /// specified node to have the specified return type, Target opcode, and
378   /// operands.  Note that target opcodes are stored as
379   /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.  The 0th value
380   /// of the resultant node is returned.
381   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
382   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
383                        SDOperand Op1);
384   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
385                        SDOperand Op1, SDOperand Op2);
386   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
387                        SDOperand Op1, SDOperand Op2, SDOperand Op3);
388   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
389                         const SDOperand *Ops, unsigned NumOps);
390   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 
391                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
392   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
393                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
394                        SDOperand Op3);
395
396
397   /// getTargetNode - These are used for target selectors to create a new node
398   /// with specified return type(s), target opcode, and operands.
399   ///
400   /// Note that getTargetNode returns the resultant node.  If there is already a
401   /// node of the specified opcode and operands, it returns that node instead of
402   /// the current one.
403   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT);
404   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
405                         SDOperand Op1);
406   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
407                         SDOperand Op1, SDOperand Op2);
408   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
409                         SDOperand Op1, SDOperand Op2, SDOperand Op3);
410   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
411                         const SDOperand *Ops, unsigned NumOps);
412   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
413                         MVT::ValueType VT2);
414   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
415                         MVT::ValueType VT2, SDOperand Op1);
416   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
417                         MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
418   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
419                         MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
420                         SDOperand Op3);
421   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
422                         MVT::ValueType VT2,
423                         const SDOperand *Ops, unsigned NumOps);
424   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
425                         MVT::ValueType VT2, MVT::ValueType VT3,
426                         SDOperand Op1, SDOperand Op2);
427   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
428                         MVT::ValueType VT2, MVT::ValueType VT3,
429                         SDOperand Op1, SDOperand Op2, SDOperand Op3);
430   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
431                         MVT::ValueType VT2, MVT::ValueType VT3,
432                         const SDOperand *Ops, unsigned NumOps);
433   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
434                         MVT::ValueType VT2, MVT::ValueType VT3,
435                         MVT::ValueType VT4,
436                         const SDOperand *Ops, unsigned NumOps);
437   SDNode *getTargetNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
438                         const SDOperand *Ops, unsigned NumOps);
439   
440   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
441   /// This can cause recursive merging of nodes in the DAG.  Use the first
442   /// version if 'From' is known to have a single result, use the second
443   /// if you have two nodes with identical results, use the third otherwise.
444   ///
445   /// These methods all take an optional vector, which (if not null) is 
446   /// populated with any nodes that are deleted from the SelectionDAG, due to
447   /// new equivalences that are discovered.
448   ///
449   void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
450                           std::vector<SDNode*> *Deleted = 0);
451   void ReplaceAllUsesWith(SDNode *From, SDNode *To,
452                           std::vector<SDNode*> *Deleted = 0);
453   void ReplaceAllUsesWith(SDNode *From, const SDOperand *To,
454                           std::vector<SDNode*> *Deleted = 0);
455
456   /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
457   /// uses of other values produced by From.Val alone.  The Deleted vector is
458   /// handled the same was as for ReplaceAllUsesWith, but it is required for
459   /// this method.
460   void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
461                                  std::vector<SDNode*> *Deleted = 0);
462
463   /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
464   /// their allnodes order. It returns the maximum id.
465   unsigned AssignNodeIds();
466
467   /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
468   /// based on their topological order. It returns the maximum id and a vector
469   /// of the SDNodes* in assigned order by reference.
470   unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
471
472   /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
473   /// operation.
474   static bool isCommutativeBinOp(unsigned Opcode) {
475     switch (Opcode) {
476     case ISD::ADD:
477     case ISD::MUL:
478     case ISD::MULHU:
479     case ISD::MULHS:
480     case ISD::SMUL_LOHI:
481     case ISD::UMUL_LOHI:
482     case ISD::FADD:
483     case ISD::FMUL:
484     case ISD::AND:
485     case ISD::OR:
486     case ISD::XOR:
487     case ISD::ADDC: 
488     case ISD::ADDE: return true;
489     default: return false;
490     }
491   }
492
493   void dump() const;
494
495   /// CreateStackTemporary - Create a stack temporary, suitable for holding the
496   /// specified value type.
497   SDOperand CreateStackTemporary(MVT::ValueType VT);
498   
499   /// FoldSetCC - Constant fold a setcc to true or false.
500   SDOperand FoldSetCC(MVT::ValueType VT, SDOperand N1,
501                       SDOperand N2, ISD::CondCode Cond);
502   
503   /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero.  We
504   /// use this predicate to simplify operations downstream.  Op and Mask are
505   /// known to be the same type.
506   bool MaskedValueIsZero(SDOperand Op, uint64_t Mask, unsigned Depth = 0)
507     const;
508   
509   /// ComputeMaskedBits - Determine which of the bits specified in Mask are
510   /// known to be either zero or one and return them in the KnownZero/KnownOne
511   /// bitsets.  This code only analyzes bits in Mask, in order to short-circuit
512   /// processing.  Targets can implement the computeMaskedBitsForTargetNode 
513   /// method in the TargetLowering class to allow target nodes to be understood.
514   void ComputeMaskedBits(SDOperand Op, uint64_t Mask, uint64_t &KnownZero,
515                          uint64_t &KnownOne, unsigned Depth = 0) const;
516     
517   /// ComputeNumSignBits - Return the number of times the sign bit of the
518   /// register is replicated into the other bits.  We know that at least 1 bit
519   /// is always equal to the sign bit (itself), but other cases can give us
520   /// information.  For example, immediately after an "SRA X, 2", we know that
521   /// the top 3 bits are all equal to each other, so we return 3.  Targets can
522   /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
523   /// class to allow target nodes to be understood.
524   unsigned ComputeNumSignBits(SDOperand Op, unsigned Depth = 0) const;
525   
526 private:
527   void RemoveNodeFromCSEMaps(SDNode *N);
528   SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
529   SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
530   SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
531                                void *&InsertPos);
532   SDNode *FindModifiedNodeSlot(SDNode *N, const SDOperand *Ops, unsigned NumOps,
533                                void *&InsertPos);
534
535   void DeleteNodeNotInCSEMaps(SDNode *N);
536   
537   // List of non-single value types.
538   std::list<std::vector<MVT::ValueType> > VTList;
539   
540   // Maps to auto-CSE operations.
541   std::vector<CondCodeSDNode*> CondCodeNodes;
542
543   std::vector<SDNode*> ValueTypeNodes;
544   std::map<MVT::ValueType, SDNode*> ExtendedValueTypeNodes;
545   std::map<std::string, SDNode*> ExternalSymbols;
546   std::map<std::string, SDNode*> TargetExternalSymbols;
547   std::map<std::string, StringSDNode*> StringNodes;
548 };
549
550 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
551   typedef SelectionDAG::allnodes_iterator nodes_iterator;
552   static nodes_iterator nodes_begin(SelectionDAG *G) {
553     return G->allnodes_begin();
554   }
555   static nodes_iterator nodes_end(SelectionDAG *G) {
556     return G->allnodes_end();
557   }
558 };
559
560 }  // end namespace llvm
561
562 #endif