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