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