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