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