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