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