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