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