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