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