487ab28ccfd150aeb10b139cef21a6a222465425
[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/DenseSet.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/ilist.h"
21 #include "llvm/CodeGen/DAGCombine.h"
22 #include "llvm/CodeGen/SelectionDAGNodes.h"
23 #include "llvm/Support/RecyclingAllocator.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include <cassert>
26 #include <map>
27 #include <string>
28 #include <vector>
29
30 namespace llvm {
31
32 class AliasAnalysis;
33 class MachineConstantPoolValue;
34 class MachineFunction;
35 class MDNode;
36 class SDDbgValue;
37 class TargetLowering;
38 class TargetSelectionDAGInfo;
39 class TargetTransformInfo;
40
41 template<> struct ilist_traits<SDNode> : public ilist_default_traits<SDNode> {
42 private:
43   mutable ilist_half_node<SDNode> Sentinel;
44 public:
45   SDNode *createSentinel() const {
46     return static_cast<SDNode*>(&Sentinel);
47   }
48   static void destroySentinel(SDNode *) {}
49
50   SDNode *provideInitialHead() const { return createSentinel(); }
51   SDNode *ensureHead(SDNode*) const { return createSentinel(); }
52   static void noteHead(SDNode*, SDNode*) {}
53
54   static void deleteNode(SDNode *) {
55     llvm_unreachable("ilist_traits<SDNode> shouldn't see a deleteNode call!");
56   }
57 private:
58   static void createNode(const SDNode &);
59 };
60
61 /// SDDbgInfo - Keeps track of dbg_value information through SDISel.  We do
62 /// not build SDNodes for these so as not to perturb the generated code;
63 /// instead the info is kept off to the side in this structure. Each SDNode may
64 /// have one or more associated dbg_value entries. This information is kept in
65 /// DbgValMap.
66 /// Byval parameters are handled separately because they don't use alloca's,
67 /// which busts the normal mechanism.  There is good reason for handling all
68 /// parameters separately:  they may not have code generated for them, they
69 /// should always go at the beginning of the function regardless of other code
70 /// motion, and debug info for them is potentially useful even if the parameter
71 /// is unused.  Right now only byval parameters are handled separately.
72 class SDDbgInfo {
73   SmallVector<SDDbgValue*, 32> DbgValues;
74   SmallVector<SDDbgValue*, 32> ByvalParmDbgValues;
75   DenseMap<const SDNode*, SmallVector<SDDbgValue*, 2> > DbgValMap;
76
77   void operator=(const SDDbgInfo&) LLVM_DELETED_FUNCTION;
78   SDDbgInfo(const SDDbgInfo&) LLVM_DELETED_FUNCTION;
79 public:
80   SDDbgInfo() {}
81
82   void add(SDDbgValue *V, const SDNode *Node, bool isParameter) {
83     if (isParameter) {
84       ByvalParmDbgValues.push_back(V);
85     } else     DbgValues.push_back(V);
86     if (Node)
87       DbgValMap[Node].push_back(V);
88   }
89
90   void clear() {
91     DbgValMap.clear();
92     DbgValues.clear();
93     ByvalParmDbgValues.clear();
94   }
95
96   bool empty() const {
97     return DbgValues.empty() && ByvalParmDbgValues.empty();
98   }
99
100   ArrayRef<SDDbgValue*> getSDDbgValues(const SDNode *Node) {
101     DenseMap<const SDNode*, SmallVector<SDDbgValue*, 2> >::iterator I =
102       DbgValMap.find(Node);
103     if (I != DbgValMap.end())
104       return I->second;
105     return ArrayRef<SDDbgValue*>();
106   }
107
108   typedef SmallVector<SDDbgValue*,32>::iterator DbgIterator;
109   DbgIterator DbgBegin() { return DbgValues.begin(); }
110   DbgIterator DbgEnd()   { return DbgValues.end(); }
111   DbgIterator ByvalParmDbgBegin() { return ByvalParmDbgValues.begin(); }
112   DbgIterator ByvalParmDbgEnd()   { return ByvalParmDbgValues.end(); }
113 };
114
115 class SelectionDAG;
116 void checkForCycles(const SDNode *N);
117 void checkForCycles(const SelectionDAG *DAG);
118
119 /// SelectionDAG class - This is used to represent a portion of an LLVM function
120 /// in a low-level Data Dependence DAG representation suitable for instruction
121 /// selection.  This DAG is constructed as the first step of instruction
122 /// selection in order to allow implementation of machine specific optimizations
123 /// and code simplifications.
124 ///
125 /// The representation used by the SelectionDAG is a target-independent
126 /// representation, which has some similarities to the GCC RTL representation,
127 /// but is significantly more simple, powerful, and is a graph form instead of a
128 /// linear form.
129 ///
130 class SelectionDAG {
131   const TargetMachine &TM;
132   const TargetLowering &TLI;
133   const TargetSelectionDAGInfo &TSI;
134   const TargetTransformInfo *TTI;
135   MachineFunction *MF;
136   LLVMContext *Context;
137   CodeGenOpt::Level OptLevel;
138
139   /// EntryNode - The starting token.
140   SDNode EntryNode;
141
142   /// Root - The root of the entire DAG.
143   SDValue Root;
144
145   /// AllNodes - A linked list of nodes in the current DAG.
146   ilist<SDNode> AllNodes;
147
148   /// NodeAllocatorType - The AllocatorType for allocating SDNodes. We use
149   /// pool allocation with recycling.
150   typedef RecyclingAllocator<BumpPtrAllocator, SDNode, sizeof(LargestSDNode),
151                              AlignOf<MostAlignedSDNode>::Alignment>
152     NodeAllocatorType;
153
154   /// NodeAllocator - Pool allocation for nodes.
155   NodeAllocatorType NodeAllocator;
156
157   /// CSEMap - This structure is used to memoize nodes, automatically performing
158   /// CSE with existing nodes when a duplicate is requested.
159   FoldingSet<SDNode> CSEMap;
160
161   /// OperandAllocator - Pool allocation for machine-opcode SDNode operands.
162   BumpPtrAllocator OperandAllocator;
163
164   /// Allocator - Pool allocation for misc. objects that are created once per
165   /// SelectionDAG.
166   BumpPtrAllocator Allocator;
167
168   /// DbgInfo - Tracks dbg_value information through SDISel.
169   SDDbgInfo *DbgInfo;
170
171 public:
172   /// DAGUpdateListener - Clients of various APIs that cause global effects on
173   /// the DAG can optionally implement this interface.  This allows the clients
174   /// to handle the various sorts of updates that happen.
175   ///
176   /// A DAGUpdateListener automatically registers itself with DAG when it is
177   /// constructed, and removes itself when destroyed in RAII fashion.
178   struct DAGUpdateListener {
179     DAGUpdateListener *const Next;
180     SelectionDAG &DAG;
181
182     explicit DAGUpdateListener(SelectionDAG &D)
183       : Next(D.UpdateListeners), DAG(D) {
184       DAG.UpdateListeners = this;
185     }
186
187     virtual ~DAGUpdateListener() {
188       assert(DAG.UpdateListeners == this &&
189              "DAGUpdateListeners must be destroyed in LIFO order");
190       DAG.UpdateListeners = Next;
191     }
192
193     /// NodeDeleted - The node N that was deleted and, if E is not null, an
194     /// equivalent node E that replaced it.
195     virtual void NodeDeleted(SDNode *N, SDNode *E);
196
197     /// NodeUpdated - The node N that was updated.
198     virtual void NodeUpdated(SDNode *N);
199   };
200
201 private:
202   /// DAGUpdateListener is a friend so it can manipulate the listener stack.
203   friend struct DAGUpdateListener;
204
205   /// UpdateListeners - Linked list of registered DAGUpdateListener instances.
206   /// This stack is maintained by DAGUpdateListener RAII.
207   DAGUpdateListener *UpdateListeners;
208
209   /// setGraphColorHelper - Implementation of setSubgraphColor.
210   /// Return whether we had to truncate the search.
211   ///
212   bool setSubgraphColorHelper(SDNode *N, const char *Color,
213                               DenseSet<SDNode *> &visited,
214                               int level, bool &printed);
215
216   void operator=(const SelectionDAG&) LLVM_DELETED_FUNCTION;
217   SelectionDAG(const SelectionDAG&) LLVM_DELETED_FUNCTION;
218
219 public:
220   explicit SelectionDAG(const TargetMachine &TM, llvm::CodeGenOpt::Level);
221   ~SelectionDAG();
222
223   /// init - Prepare this SelectionDAG to process code in the given
224   /// MachineFunction.
225   ///
226   void init(MachineFunction &mf, const TargetTransformInfo *TTI);
227
228   /// clear - Clear state and free memory necessary to make this
229   /// SelectionDAG ready to process a new block.
230   ///
231   void clear();
232
233   MachineFunction &getMachineFunction() const { return *MF; }
234   const TargetMachine &getTarget() const { return TM; }
235   const TargetLowering &getTargetLoweringInfo() const { return TLI; }
236   const TargetSelectionDAGInfo &getSelectionDAGInfo() const { return TSI; }
237   const TargetTransformInfo *getTargetTransformInfo() const { return TTI; }
238   LLVMContext *getContext() const {return Context; }
239
240   /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
241   ///
242   void viewGraph(const std::string &Title);
243   void viewGraph();
244
245 #ifndef NDEBUG
246   std::map<const SDNode *, std::string> NodeGraphAttrs;
247 #endif
248
249   /// clearGraphAttrs - Clear all previously defined node graph attributes.
250   /// Intended to be used from a debugging tool (eg. gdb).
251   void clearGraphAttrs();
252
253   /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
254   ///
255   void setGraphAttrs(const SDNode *N, const char *Attrs);
256
257   /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
258   /// Used from getNodeAttributes.
259   const std::string getGraphAttrs(const SDNode *N) const;
260
261   /// setGraphColor - Convenience for setting node color attribute.
262   ///
263   void setGraphColor(const SDNode *N, const char *Color);
264
265   /// setGraphColor - Convenience for setting subgraph color attribute.
266   ///
267   void setSubgraphColor(SDNode *N, const char *Color);
268
269   typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
270   allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
271   allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
272   typedef ilist<SDNode>::iterator allnodes_iterator;
273   allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
274   allnodes_iterator allnodes_end() { return AllNodes.end(); }
275   ilist<SDNode>::size_type allnodes_size() const {
276     return AllNodes.size();
277   }
278
279   /// getRoot - Return the root tag of the SelectionDAG.
280   ///
281   const SDValue &getRoot() const { return Root; }
282
283   /// getEntryNode - Return the token chain corresponding to the entry of the
284   /// function.
285   SDValue getEntryNode() const {
286     return SDValue(const_cast<SDNode *>(&EntryNode), 0);
287   }
288
289   /// setRoot - Set the current root tag of the SelectionDAG.
290   ///
291   const SDValue &setRoot(SDValue N) {
292     assert((!N.getNode() || N.getValueType() == MVT::Other) &&
293            "DAG root value is not a chain!");
294     if (N.getNode())
295       checkForCycles(N.getNode());
296     Root = N;
297     if (N.getNode())
298       checkForCycles(this);
299     return Root;
300   }
301
302   /// Combine - This iterates over the nodes in the SelectionDAG, folding
303   /// certain types of nodes together, or eliminating superfluous nodes.  The
304   /// Level argument controls whether Combine is allowed to produce nodes and
305   /// types that are illegal on the target.
306   void Combine(CombineLevel Level, AliasAnalysis &AA,
307                CodeGenOpt::Level OptLevel);
308
309   /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
310   /// only uses types natively supported by the target.  Returns "true" if it
311   /// made any changes.
312   ///
313   /// Note that this is an involved process that may invalidate pointers into
314   /// the graph.
315   bool LegalizeTypes();
316
317   /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
318   /// compatible with the target instruction selector, as indicated by the
319   /// TargetLowering object.
320   ///
321   /// Note that this is an involved process that may invalidate pointers into
322   /// the graph.
323   void Legalize();
324
325   /// LegalizeVectors - This transforms the SelectionDAG into a SelectionDAG
326   /// that only uses vector math operations supported by the target.  This is
327   /// necessary as a separate step from Legalize because unrolling a vector
328   /// operation can introduce illegal types, which requires running
329   /// LegalizeTypes again.
330   ///
331   /// This returns true if it made any changes; in that case, LegalizeTypes
332   /// is called again before Legalize.
333   ///
334   /// Note that this is an involved process that may invalidate pointers into
335   /// the graph.
336   bool LegalizeVectors();
337
338   /// RemoveDeadNodes - This method deletes all unreachable nodes in the
339   /// SelectionDAG.
340   void RemoveDeadNodes();
341
342   /// DeleteNode - Remove the specified node from the system.  This node must
343   /// have no referrers.
344   void DeleteNode(SDNode *N);
345
346   /// getVTList - Return an SDVTList that represents the list of values
347   /// specified.
348   SDVTList getVTList(EVT VT);
349   SDVTList getVTList(EVT VT1, EVT VT2);
350   SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3);
351   SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4);
352   SDVTList getVTList(const EVT *VTs, unsigned NumVTs);
353
354   //===--------------------------------------------------------------------===//
355   // Node creation methods.
356   //
357   SDValue getConstant(uint64_t Val, EVT VT, bool isTarget = false);
358   SDValue getConstant(const APInt &Val, EVT VT, bool isTarget = false);
359   SDValue getConstant(const ConstantInt &Val, EVT VT, bool isTarget = false);
360   SDValue getIntPtrConstant(uint64_t Val, bool isTarget = false);
361   SDValue getTargetConstant(uint64_t Val, EVT VT) {
362     return getConstant(Val, VT, true);
363   }
364   SDValue getTargetConstant(const APInt &Val, EVT VT) {
365     return getConstant(Val, VT, true);
366   }
367   SDValue getTargetConstant(const ConstantInt &Val, EVT VT) {
368     return getConstant(Val, VT, true);
369   }
370   // The forms below that take a double should only be used for simple
371   // constants that can be exactly represented in VT.  No checks are made.
372   SDValue getConstantFP(double Val, EVT VT, bool isTarget = false);
373   SDValue getConstantFP(const APFloat& Val, EVT VT, bool isTarget = false);
374   SDValue getConstantFP(const ConstantFP &CF, EVT VT, bool isTarget = false);
375   SDValue getTargetConstantFP(double Val, EVT VT) {
376     return getConstantFP(Val, VT, true);
377   }
378   SDValue getTargetConstantFP(const APFloat& Val, EVT VT) {
379     return getConstantFP(Val, VT, true);
380   }
381   SDValue getTargetConstantFP(const ConstantFP &Val, EVT VT) {
382     return getConstantFP(Val, VT, true);
383   }
384   SDValue getGlobalAddress(const GlobalValue *GV, SDLoc DL, EVT VT,
385                            int64_t offset = 0, bool isTargetGA = false,
386                            unsigned char TargetFlags = 0);
387   SDValue getTargetGlobalAddress(const GlobalValue *GV, SDLoc DL, EVT VT,
388                                  int64_t offset = 0,
389                                  unsigned char TargetFlags = 0) {
390     return getGlobalAddress(GV, DL, VT, offset, true, TargetFlags);
391   }
392   SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
393   SDValue getTargetFrameIndex(int FI, EVT VT) {
394     return getFrameIndex(FI, VT, true);
395   }
396   SDValue getJumpTable(int JTI, EVT VT, bool isTarget = false,
397                        unsigned char TargetFlags = 0);
398   SDValue getTargetJumpTable(int JTI, EVT VT, unsigned char TargetFlags = 0) {
399     return getJumpTable(JTI, VT, true, TargetFlags);
400   }
401   SDValue getConstantPool(const Constant *C, EVT VT,
402                           unsigned Align = 0, int Offs = 0, bool isT=false,
403                           unsigned char TargetFlags = 0);
404   SDValue getTargetConstantPool(const Constant *C, EVT VT,
405                                 unsigned Align = 0, int Offset = 0,
406                                 unsigned char TargetFlags = 0) {
407     return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
408   }
409   SDValue getConstantPool(MachineConstantPoolValue *C, EVT VT,
410                           unsigned Align = 0, int Offs = 0, bool isT=false,
411                           unsigned char TargetFlags = 0);
412   SDValue getTargetConstantPool(MachineConstantPoolValue *C,
413                                   EVT VT, unsigned Align = 0,
414                                   int Offset = 0, unsigned char TargetFlags=0) {
415     return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
416   }
417   SDValue getTargetIndex(int Index, EVT VT, int64_t Offset = 0,
418                          unsigned char TargetFlags = 0);
419   // When generating a branch to a BB, we don't in general know enough
420   // to provide debug info for the BB at that time, so keep this one around.
421   SDValue getBasicBlock(MachineBasicBlock *MBB);
422   SDValue getBasicBlock(MachineBasicBlock *MBB, SDLoc dl);
423   SDValue getExternalSymbol(const char *Sym, EVT VT);
424   SDValue getExternalSymbol(const char *Sym, SDLoc dl, EVT VT);
425   SDValue getTargetExternalSymbol(const char *Sym, EVT VT,
426                                   unsigned char TargetFlags = 0);
427   SDValue getValueType(EVT);
428   SDValue getRegister(unsigned Reg, EVT VT);
429   SDValue getRegisterMask(const uint32_t *RegMask);
430   SDValue getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label);
431   SDValue getBlockAddress(const BlockAddress *BA, EVT VT,
432                           int64_t Offset = 0, bool isTarget = false,
433                           unsigned char TargetFlags = 0);
434   SDValue getTargetBlockAddress(const BlockAddress *BA, EVT VT,
435                                 int64_t Offset = 0,
436                                 unsigned char TargetFlags = 0) {
437     return getBlockAddress(BA, VT, Offset, true, TargetFlags);
438   }
439
440   SDValue getCopyToReg(SDValue Chain, SDLoc dl, unsigned Reg, SDValue N) {
441     return getNode(ISD::CopyToReg, dl, MVT::Other, Chain,
442                    getRegister(Reg, N.getValueType()), N);
443   }
444
445   // This version of the getCopyToReg method takes an extra operand, which
446   // indicates that there is potentially an incoming glue value (if Glue is not
447   // null) and that there should be a glue result.
448   SDValue getCopyToReg(SDValue Chain, SDLoc dl, unsigned Reg, SDValue N,
449                        SDValue Glue) {
450     SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
451     SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Glue };
452     return getNode(ISD::CopyToReg, dl, VTs, Ops, Glue.getNode() ? 4 : 3);
453   }
454
455   // Similar to last getCopyToReg() except parameter Reg is a SDValue
456   SDValue getCopyToReg(SDValue Chain, SDLoc dl, SDValue Reg, SDValue N,
457                          SDValue Glue) {
458     SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
459     SDValue Ops[] = { Chain, Reg, N, Glue };
460     return getNode(ISD::CopyToReg, dl, VTs, Ops, Glue.getNode() ? 4 : 3);
461   }
462
463   SDValue getCopyFromReg(SDValue Chain, SDLoc dl, unsigned Reg, EVT VT) {
464     SDVTList VTs = getVTList(VT, MVT::Other);
465     SDValue Ops[] = { Chain, getRegister(Reg, VT) };
466     return getNode(ISD::CopyFromReg, dl, VTs, Ops, 2);
467   }
468
469   // This version of the getCopyFromReg method takes an extra operand, which
470   // indicates that there is potentially an incoming glue value (if Glue is not
471   // null) and that there should be a glue result.
472   SDValue getCopyFromReg(SDValue Chain, SDLoc dl, unsigned Reg, EVT VT,
473                            SDValue Glue) {
474     SDVTList VTs = getVTList(VT, MVT::Other, MVT::Glue);
475     SDValue Ops[] = { Chain, getRegister(Reg, VT), Glue };
476     return getNode(ISD::CopyFromReg, dl, VTs, Ops, Glue.getNode() ? 3 : 2);
477   }
478
479   SDValue getCondCode(ISD::CondCode Cond);
480
481   /// Returns the ConvertRndSat Note: Avoid using this node because it may
482   /// disappear in the future and most targets don't support it.
483   SDValue getConvertRndSat(EVT VT, SDLoc dl, SDValue Val, SDValue DTy,
484                            SDValue STy,
485                            SDValue Rnd, SDValue Sat, ISD::CvtCode Code);
486
487   /// getVectorShuffle - Return an ISD::VECTOR_SHUFFLE node.  The number of
488   /// elements in VT, which must be a vector type, must match the number of
489   /// mask elements NumElts.  A integer mask element equal to -1 is treated as
490   /// undefined.
491   SDValue getVectorShuffle(EVT VT, SDLoc dl, SDValue N1, SDValue N2,
492                            const int *MaskElts);
493
494   /// getAnyExtOrTrunc - Convert Op, which must be of integer type, to the
495   /// integer type VT, by either any-extending or truncating it.
496   SDValue getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT);
497
498   /// getSExtOrTrunc - Convert Op, which must be of integer type, to the
499   /// integer type VT, by either sign-extending or truncating it.
500   SDValue getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT);
501
502   /// getZExtOrTrunc - Convert Op, which must be of integer type, to the
503   /// integer type VT, by either zero-extending or truncating it.
504   SDValue getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT);
505
506   /// getZeroExtendInReg - Return the expression required to zero extend the Op
507   /// value assuming it was the smaller SrcTy value.
508   SDValue getZeroExtendInReg(SDValue Op, SDLoc DL, EVT SrcTy);
509
510   /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
511   SDValue getNOT(SDLoc DL, SDValue Val, EVT VT);
512
513   /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
514   /// a glue result (to ensure it's not CSE'd).  CALLSEQ_START does not have a
515   /// useful SDLoc.
516   SDValue getCALLSEQ_START(SDValue Chain, SDValue Op, SDLoc DL) {
517     SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
518     SDValue Ops[] = { Chain,  Op };
519     return getNode(ISD::CALLSEQ_START, DL, VTs, Ops, 2);
520   }
521
522   /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
523   /// glue result (to ensure it's not CSE'd).  CALLSEQ_END does not have
524   /// a useful SDLoc.
525   SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
526                            SDValue InGlue, SDLoc DL) {
527     SDVTList NodeTys = getVTList(MVT::Other, MVT::Glue);
528     SmallVector<SDValue, 4> Ops;
529     Ops.push_back(Chain);
530     Ops.push_back(Op1);
531     Ops.push_back(Op2);
532     Ops.push_back(InGlue);
533     return getNode(ISD::CALLSEQ_END, DL, NodeTys, &Ops[0],
534                    (unsigned)Ops.size() - (InGlue.getNode() == 0 ? 1 : 0));
535   }
536
537   /// getUNDEF - Return an UNDEF node.  UNDEF does not have a useful SDLoc.
538   SDValue getUNDEF(EVT VT) {
539     return getNode(ISD::UNDEF, SDLoc(), VT);
540   }
541
542   /// getGLOBAL_OFFSET_TABLE - Return a GLOBAL_OFFSET_TABLE node.  This does
543   /// not have a useful SDLoc.
544   SDValue getGLOBAL_OFFSET_TABLE(EVT VT) {
545     return getNode(ISD::GLOBAL_OFFSET_TABLE, SDLoc(), VT);
546   }
547
548   /// getNode - Gets or creates the specified node.
549   ///
550   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT);
551   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N);
552   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1, SDValue N2);
553   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT,
554                   SDValue N1, SDValue N2, SDValue N3);
555   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT,
556                   SDValue N1, SDValue N2, SDValue N3, SDValue N4);
557   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT,
558                   SDValue N1, SDValue N2, SDValue N3, SDValue N4,
559                   SDValue N5);
560   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT,
561                   const SDUse *Ops, unsigned NumOps);
562   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT,
563                   const SDValue *Ops, unsigned NumOps);
564   SDValue getNode(unsigned Opcode, SDLoc DL,
565                   ArrayRef<EVT> ResultTys,
566                   const SDValue *Ops, unsigned NumOps);
567   SDValue getNode(unsigned Opcode, SDLoc DL, const EVT *VTs, unsigned NumVTs,
568                   const SDValue *Ops, unsigned NumOps);
569   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
570                   const SDValue *Ops, unsigned NumOps);
571   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs);
572   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs, SDValue N);
573   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
574                   SDValue N1, SDValue N2);
575   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
576                   SDValue N1, SDValue N2, SDValue N3);
577   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
578                   SDValue N1, SDValue N2, SDValue N3, SDValue N4);
579   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
580                   SDValue N1, SDValue N2, SDValue N3, SDValue N4,
581                   SDValue N5);
582
583   /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
584   /// the incoming stack arguments to be loaded from the stack. This is
585   /// used in tail call lowering to protect stack arguments from being
586   /// clobbered.
587   SDValue getStackArgumentTokenFactor(SDValue Chain);
588
589   SDValue getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst, SDValue Src,
590                     SDValue Size, unsigned Align, bool isVol, bool AlwaysInline,
591                     MachinePointerInfo DstPtrInfo,
592                     MachinePointerInfo SrcPtrInfo);
593
594   SDValue getMemmove(SDValue Chain, SDLoc dl, SDValue Dst, SDValue Src,
595                      SDValue Size, unsigned Align, bool isVol,
596                      MachinePointerInfo DstPtrInfo,
597                      MachinePointerInfo SrcPtrInfo);
598
599   SDValue getMemset(SDValue Chain, SDLoc dl, SDValue Dst, SDValue Src,
600                     SDValue Size, unsigned Align, bool isVol,
601                     MachinePointerInfo DstPtrInfo);
602
603   /// getSetCC - Helper function to make it easier to build SetCC's if you just
604   /// have an ISD::CondCode instead of an SDValue.
605   ///
606   SDValue getSetCC(SDLoc DL, EVT VT, SDValue LHS, SDValue RHS,
607                    ISD::CondCode Cond) {
608     assert(LHS.getValueType().isVector() == RHS.getValueType().isVector() &&
609       "Cannot compare scalars to vectors");
610     assert(LHS.getValueType().isVector() == VT.isVector() &&
611       "Cannot compare scalars to vectors");
612     return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
613   }
614
615   /// getSelectCC - Helper function to make it easier to build SelectCC's if you
616   /// just have an ISD::CondCode instead of an SDValue.
617   ///
618   SDValue getSelectCC(SDLoc DL, SDValue LHS, SDValue RHS,
619                       SDValue True, SDValue False, ISD::CondCode Cond) {
620     return getNode(ISD::SELECT_CC, DL, True.getValueType(),
621                    LHS, RHS, True, False, getCondCode(Cond));
622   }
623
624   /// getVAArg - VAArg produces a result and token chain, and takes a pointer
625   /// and a source value as input.
626   SDValue getVAArg(EVT VT, SDLoc dl, SDValue Chain, SDValue Ptr,
627                    SDValue SV, unsigned Align);
628
629   /// getAtomic - Gets a node for an atomic op, produces result and chain and
630   /// takes 3 operands
631   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, SDValue Chain,
632                     SDValue Ptr, SDValue Cmp, SDValue Swp,
633                     MachinePointerInfo PtrInfo, unsigned Alignment,
634                     AtomicOrdering Ordering,
635                     SynchronizationScope SynchScope);
636   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, SDValue Chain,
637                     SDValue Ptr, SDValue Cmp, SDValue Swp,
638                     MachineMemOperand *MMO,
639                     AtomicOrdering Ordering,
640                     SynchronizationScope SynchScope);
641
642   /// getAtomic - Gets a node for an atomic op, produces result (if relevant)
643   /// and chain and takes 2 operands.
644   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, SDValue Chain,
645                     SDValue Ptr, SDValue Val, const Value* PtrVal,
646                     unsigned Alignment, AtomicOrdering Ordering,
647                     SynchronizationScope SynchScope);
648   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, SDValue Chain,
649                     SDValue Ptr, SDValue Val, MachineMemOperand *MMO,
650                     AtomicOrdering Ordering,
651                     SynchronizationScope SynchScope);
652
653   /// getAtomic - Gets a node for an atomic op, produces result and chain and
654   /// takes 1 operand.
655   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, EVT VT,
656                     SDValue Chain, SDValue Ptr, const Value* PtrVal,
657                     unsigned Alignment,
658                     AtomicOrdering Ordering,
659                     SynchronizationScope SynchScope);
660   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, EVT VT,
661                     SDValue Chain, SDValue Ptr, MachineMemOperand *MMO,
662                     AtomicOrdering Ordering,
663                     SynchronizationScope SynchScope);
664
665   /// getMemIntrinsicNode - Creates a MemIntrinsicNode that may produce a
666   /// result and takes a list of operands. Opcode may be INTRINSIC_VOID,
667   /// INTRINSIC_W_CHAIN, or a target-specific opcode with a value not
668   /// less than FIRST_TARGET_MEMORY_OPCODE.
669   SDValue getMemIntrinsicNode(unsigned Opcode, SDLoc dl,
670                               const EVT *VTs, unsigned NumVTs,
671                               const SDValue *Ops, unsigned NumOps,
672                               EVT MemVT, MachinePointerInfo PtrInfo,
673                               unsigned Align = 0, bool Vol = false,
674                               bool ReadMem = true, bool WriteMem = true);
675
676   SDValue getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
677                               const SDValue *Ops, unsigned NumOps,
678                               EVT MemVT, MachinePointerInfo PtrInfo,
679                               unsigned Align = 0, bool Vol = false,
680                               bool ReadMem = true, bool WriteMem = true);
681
682   SDValue getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
683                               const SDValue *Ops, unsigned NumOps,
684                               EVT MemVT, MachineMemOperand *MMO);
685
686   /// getMergeValues - Create a MERGE_VALUES node from the given operands.
687   SDValue getMergeValues(const SDValue *Ops, unsigned NumOps, SDLoc dl);
688
689   /// getLoad - Loads are not normal binary operators: their result type is not
690   /// determined by their operands, and they produce a value AND a token chain.
691   ///
692   SDValue getLoad(EVT VT, SDLoc dl, SDValue Chain, SDValue Ptr,
693                   MachinePointerInfo PtrInfo, bool isVolatile,
694                   bool isNonTemporal, bool isInvariant, unsigned Alignment,
695                   const MDNode *TBAAInfo = 0, const MDNode *Ranges = 0);
696   SDValue getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
697                      SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo,
698                      EVT MemVT, bool isVolatile,
699                      bool isNonTemporal, unsigned Alignment,
700                      const MDNode *TBAAInfo = 0);
701   SDValue getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
702                          SDValue Offset, ISD::MemIndexedMode AM);
703   SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
704                   EVT VT, SDLoc dl,
705                   SDValue Chain, SDValue Ptr, SDValue Offset,
706                   MachinePointerInfo PtrInfo, EVT MemVT,
707                   bool isVolatile, bool isNonTemporal, bool isInvariant,
708                   unsigned Alignment, const MDNode *TBAAInfo = 0,
709                   const MDNode *Ranges = 0);
710   SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
711                   EVT VT, SDLoc dl,
712                   SDValue Chain, SDValue Ptr, SDValue Offset,
713                   EVT MemVT, MachineMemOperand *MMO);
714
715   /// getStore - Helper function to build ISD::STORE nodes.
716   ///
717   SDValue getStore(SDValue Chain, SDLoc dl, SDValue Val, SDValue Ptr,
718                    MachinePointerInfo PtrInfo, bool isVolatile,
719                    bool isNonTemporal, unsigned Alignment,
720                    const MDNode *TBAAInfo = 0);
721   SDValue getStore(SDValue Chain, SDLoc dl, SDValue Val, SDValue Ptr,
722                    MachineMemOperand *MMO);
723   SDValue getTruncStore(SDValue Chain, SDLoc dl, SDValue Val, SDValue Ptr,
724                         MachinePointerInfo PtrInfo, EVT TVT,
725                         bool isNonTemporal, bool isVolatile,
726                         unsigned Alignment,
727                         const MDNode *TBAAInfo = 0);
728   SDValue getTruncStore(SDValue Chain, SDLoc dl, SDValue Val, SDValue Ptr,
729                         EVT TVT, MachineMemOperand *MMO);
730   SDValue getIndexedStore(SDValue OrigStoe, SDLoc dl, SDValue Base,
731                            SDValue Offset, ISD::MemIndexedMode AM);
732
733   /// getSrcValue - Construct a node to track a Value* through the backend.
734   SDValue getSrcValue(const Value *v);
735
736   /// getMDNode - Return an MDNodeSDNode which holds an MDNode.
737   SDValue getMDNode(const MDNode *MD);
738
739   /// getShiftAmountOperand - Return the specified value casted to
740   /// the target's desired shift amount type.
741   SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op);
742
743   /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
744   /// specified operands.  If the resultant node already exists in the DAG,
745   /// this does not modify the specified node, instead it returns the node that
746   /// already exists.  If the resultant node does not exist in the DAG, the
747   /// input node is returned.  As a degenerate case, if you specify the same
748   /// input operands as the node already has, the input node is returned.
749   SDNode *UpdateNodeOperands(SDNode *N, SDValue Op);
750   SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2);
751   SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
752                                SDValue Op3);
753   SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
754                                SDValue Op3, SDValue Op4);
755   SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
756                                SDValue Op3, SDValue Op4, SDValue Op5);
757   SDNode *UpdateNodeOperands(SDNode *N,
758                                const SDValue *Ops, unsigned NumOps);
759
760   /// SelectNodeTo - These are used for target selectors to *mutate* the
761   /// specified node to have the specified return type, Target opcode, and
762   /// operands.  Note that target opcodes are stored as
763   /// ~TargetOpcode in the node opcode field.  The resultant node is returned.
764   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT);
765   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT, SDValue Op1);
766   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
767                        SDValue Op1, SDValue Op2);
768   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
769                        SDValue Op1, SDValue Op2, SDValue Op3);
770   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
771                        const SDValue *Ops, unsigned NumOps);
772   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1, EVT VT2);
773   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
774                        EVT VT2, const SDValue *Ops, unsigned NumOps);
775   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
776                        EVT VT2, EVT VT3, const SDValue *Ops, unsigned NumOps);
777   SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
778                        EVT VT2, EVT VT3, EVT VT4, const SDValue *Ops,
779                        unsigned NumOps);
780   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
781                        EVT VT2, SDValue Op1);
782   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
783                        EVT VT2, SDValue Op1, SDValue Op2);
784   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
785                        EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
786   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
787                        EVT VT2, EVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
788   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, SDVTList VTs,
789                        const SDValue *Ops, unsigned NumOps);
790
791   /// MorphNodeTo - This *mutates* the specified node to have the specified
792   /// return type, opcode, and operands.
793   SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
794                       const SDValue *Ops, unsigned NumOps);
795
796   /// getMachineNode - These are used for target selectors to create a new node
797   /// with specified return type(s), MachineInstr opcode, and operands.
798   ///
799   /// Note that getMachineNode returns the resultant node.  If there is already
800   /// a node of the specified opcode and operands, it returns that node instead
801   /// of the current one.
802   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT);
803   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
804                                 SDValue Op1);
805   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
806                                 SDValue Op1, SDValue Op2);
807   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
808                                 SDValue Op1, SDValue Op2, SDValue Op3);
809   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
810                                 ArrayRef<SDValue> Ops);
811   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2);
812   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
813                                 SDValue Op1);
814   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
815                                 SDValue Op1, SDValue Op2);
816   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
817                                 SDValue Op1, SDValue Op2, SDValue Op3);
818   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
819                                 ArrayRef<SDValue> Ops);
820   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
821                                 EVT VT3, SDValue Op1, SDValue Op2);
822   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
823                                 EVT VT3, SDValue Op1, SDValue Op2,
824                                 SDValue Op3);
825   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
826                                 EVT VT3, ArrayRef<SDValue> Ops);
827   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
828                                 EVT VT3, EVT VT4, ArrayRef<SDValue> Ops);
829   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl,
830                                 ArrayRef<EVT> ResultTys,
831                                 ArrayRef<SDValue> Ops);
832   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, SDVTList VTs,
833                                 ArrayRef<SDValue> Ops);
834
835   /// getTargetExtractSubreg - A convenience function for creating
836   /// TargetInstrInfo::EXTRACT_SUBREG nodes.
837   SDValue getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
838                                  SDValue Operand);
839
840   /// getTargetInsertSubreg - A convenience function for creating
841   /// TargetInstrInfo::INSERT_SUBREG nodes.
842   SDValue getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
843                                 SDValue Operand, SDValue Subreg);
844
845   /// getNodeIfExists - Get the specified node if it's already available, or
846   /// else return NULL.
847   SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs,
848                           const SDValue *Ops, unsigned NumOps);
849
850   /// getDbgValue - Creates a SDDbgValue node.
851   ///
852   SDDbgValue *getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
853                           DebugLoc DL, unsigned O);
854   SDDbgValue *getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
855                           DebugLoc DL, unsigned O);
856   SDDbgValue *getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
857                           DebugLoc DL, unsigned O);
858
859   /// RemoveDeadNode - Remove the specified node from the system. If any of its
860   /// operands then becomes dead, remove them as well. Inform UpdateListener
861   /// for each node deleted.
862   void RemoveDeadNode(SDNode *N);
863
864   /// RemoveDeadNodes - This method deletes the unreachable nodes in the
865   /// given list, and any nodes that become unreachable as a result.
866   void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes);
867
868   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
869   /// This can cause recursive merging of nodes in the DAG.  Use the first
870   /// version if 'From' is known to have a single result, use the second
871   /// if you have two nodes with identical results (or if 'To' has a superset
872   /// of the results of 'From'), use the third otherwise.
873   ///
874   /// These methods all take an optional UpdateListener, which (if not null) is
875   /// informed about nodes that are deleted and modified due to recursive
876   /// changes in the dag.
877   ///
878   /// These functions only replace all existing uses. It's possible that as
879   /// these replacements are being performed, CSE may cause the From node
880   /// to be given new uses. These new uses of From are left in place, and
881   /// not automatically transferred to To.
882   ///
883   void ReplaceAllUsesWith(SDValue From, SDValue Op);
884   void ReplaceAllUsesWith(SDNode *From, SDNode *To);
885   void ReplaceAllUsesWith(SDNode *From, const SDValue *To);
886
887   /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
888   /// uses of other values produced by From.Val alone.
889   void ReplaceAllUsesOfValueWith(SDValue From, SDValue To);
890
891   /// ReplaceAllUsesOfValuesWith - Like ReplaceAllUsesOfValueWith, but
892   /// for multiple values at once. This correctly handles the case where
893   /// there is an overlap between the From values and the To values.
894   void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
895                                   unsigned Num);
896
897   /// AssignTopologicalOrder - Topological-sort the AllNodes list and a
898   /// assign a unique node id for each node in the DAG based on their
899   /// topological order. Returns the number of nodes.
900   unsigned AssignTopologicalOrder();
901
902   /// RepositionNode - Move node N in the AllNodes list to be immediately
903   /// before the given iterator Position. This may be used to update the
904   /// topological ordering when the list of nodes is modified.
905   void RepositionNode(allnodes_iterator Position, SDNode *N) {
906     AllNodes.insert(Position, AllNodes.remove(N));
907   }
908
909   /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
910   /// operation.
911   static bool isCommutativeBinOp(unsigned Opcode) {
912     // FIXME: This should get its info from the td file, so that we can include
913     // target info.
914     switch (Opcode) {
915     case ISD::ADD:
916     case ISD::MUL:
917     case ISD::MULHU:
918     case ISD::MULHS:
919     case ISD::SMUL_LOHI:
920     case ISD::UMUL_LOHI:
921     case ISD::FADD:
922     case ISD::FMUL:
923     case ISD::AND:
924     case ISD::OR:
925     case ISD::XOR:
926     case ISD::SADDO:
927     case ISD::UADDO:
928     case ISD::ADDC:
929     case ISD::ADDE: return true;
930     default: return false;
931     }
932   }
933
934   /// Returns an APFloat semantics tag appropriate for the given type. If VT is
935   /// a vector type, the element semantics are returned.
936   static const fltSemantics &EVTToAPFloatSemantics(EVT VT) {
937     switch (VT.getScalarType().getSimpleVT().SimpleTy) {
938     default: llvm_unreachable("Unknown FP format");
939     case MVT::f16:     return APFloat::IEEEhalf;
940     case MVT::f32:     return APFloat::IEEEsingle;
941     case MVT::f64:     return APFloat::IEEEdouble;
942     case MVT::f80:     return APFloat::x87DoubleExtended;
943     case MVT::f128:    return APFloat::IEEEquad;
944     case MVT::ppcf128: return APFloat::PPCDoubleDouble;
945     }
946   }
947
948   /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
949   /// value is produced by SD.
950   void AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter);
951
952   /// GetDbgValues - Get the debug values which reference the given SDNode.
953   ArrayRef<SDDbgValue*> GetDbgValues(const SDNode* SD) {
954     return DbgInfo->getSDDbgValues(SD);
955   }
956
957   /// TransferDbgValues - Transfer SDDbgValues.
958   void TransferDbgValues(SDValue From, SDValue To);
959
960   /// hasDebugValues - Return true if there are any SDDbgValue nodes associated
961   /// with this SelectionDAG.
962   bool hasDebugValues() const { return !DbgInfo->empty(); }
963
964   SDDbgInfo::DbgIterator DbgBegin() { return DbgInfo->DbgBegin(); }
965   SDDbgInfo::DbgIterator DbgEnd()   { return DbgInfo->DbgEnd(); }
966   SDDbgInfo::DbgIterator ByvalParmDbgBegin() {
967     return DbgInfo->ByvalParmDbgBegin();
968   }
969   SDDbgInfo::DbgIterator ByvalParmDbgEnd()   {
970     return DbgInfo->ByvalParmDbgEnd();
971   }
972
973   void dump() const;
974
975   /// CreateStackTemporary - Create a stack temporary, suitable for holding the
976   /// specified value type.  If minAlign is specified, the slot size will have
977   /// at least that alignment.
978   SDValue CreateStackTemporary(EVT VT, unsigned minAlign = 1);
979
980   /// CreateStackTemporary - Create a stack temporary suitable for holding
981   /// either of the specified value types.
982   SDValue CreateStackTemporary(EVT VT1, EVT VT2);
983
984   /// FoldConstantArithmetic -
985   SDValue FoldConstantArithmetic(unsigned Opcode, EVT VT,
986                                  SDNode *Cst1, SDNode *Cst2);
987
988   /// FoldSetCC - Constant fold a setcc to true or false.
989   SDValue FoldSetCC(EVT VT, SDValue N1,
990                     SDValue N2, ISD::CondCode Cond, SDLoc dl);
991
992   /// SignBitIsZero - Return true if the sign bit of Op is known to be zero.  We
993   /// use this predicate to simplify operations downstream.
994   bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;
995
996   /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero.  We
997   /// use this predicate to simplify operations downstream.  Op and Mask are
998   /// known to be the same type.
999   bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth = 0)
1000     const;
1001
1002   /// ComputeMaskedBits - Determine which of the bits specified in Mask are
1003   /// known to be either zero or one and return them in the KnownZero/KnownOne
1004   /// bitsets.  This code only analyzes bits in Mask, in order to short-circuit
1005   /// processing.  Targets can implement the computeMaskedBitsForTargetNode
1006   /// method in the TargetLowering class to allow target nodes to be understood.
1007   void ComputeMaskedBits(SDValue Op, APInt &KnownZero, APInt &KnownOne,
1008                          unsigned Depth = 0) const;
1009
1010   /// ComputeNumSignBits - Return the number of times the sign bit of the
1011   /// register is replicated into the other bits.  We know that at least 1 bit
1012   /// is always equal to the sign bit (itself), but other cases can give us
1013   /// information.  For example, immediately after an "SRA X, 2", we know that
1014   /// the top 3 bits are all equal to each other, so we return 3.  Targets can
1015   /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
1016   /// class to allow target nodes to be understood.
1017   unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const;
1018
1019   /// isBaseWithConstantOffset - Return true if the specified operand is an
1020   /// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
1021   /// ISD::OR with a ConstantSDNode that is guaranteed to have the same
1022   /// semantics as an ADD.  This handles the equivalence:
1023   ///     X|Cst == X+Cst iff X&Cst = 0.
1024   bool isBaseWithConstantOffset(SDValue Op) const;
1025
1026   /// isKnownNeverNan - Test whether the given SDValue is known to never be NaN.
1027   bool isKnownNeverNaN(SDValue Op) const;
1028
1029   /// isKnownNeverZero - Test whether the given SDValue is known to never be
1030   /// positive or negative Zero.
1031   bool isKnownNeverZero(SDValue Op) const;
1032
1033   /// isEqualTo - Test whether two SDValues are known to compare equal. This
1034   /// is true if they are the same value, or if one is negative zero and the
1035   /// other positive zero.
1036   bool isEqualTo(SDValue A, SDValue B) const;
1037
1038   /// UnrollVectorOp - Utility function used by legalize and lowering to
1039   /// "unroll" a vector operation by splitting out the scalars and operating
1040   /// on each element individually.  If the ResNE is 0, fully unroll the vector
1041   /// op. If ResNE is less than the width of the vector op, unroll up to ResNE.
1042   /// If the  ResNE is greater than the width of the vector op, unroll the
1043   /// vector op and fill the end of the resulting vector with UNDEFS.
1044   SDValue UnrollVectorOp(SDNode *N, unsigned ResNE = 0);
1045
1046   /// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
1047   /// location that is 'Dist' units away from the location that the 'Base' load
1048   /// is loading from.
1049   bool isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
1050                          unsigned Bytes, int Dist) const;
1051
1052   /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
1053   /// it cannot be inferred.
1054   unsigned InferPtrAlignment(SDValue Ptr) const;
1055
1056 private:
1057   bool RemoveNodeFromCSEMaps(SDNode *N);
1058   void AddModifiedNodeToCSEMaps(SDNode *N);
1059   SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
1060   SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
1061                                void *&InsertPos);
1062   SDNode *FindModifiedNodeSlot(SDNode *N, const SDValue *Ops, unsigned NumOps,
1063                                void *&InsertPos);
1064   SDNode *UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc loc);
1065
1066   void DeleteNodeNotInCSEMaps(SDNode *N);
1067   void DeallocateNode(SDNode *N);
1068
1069   unsigned getEVTAlignment(EVT MemoryVT) const;
1070
1071   void allnodes_clear();
1072
1073   /// VTList - List of non-single value types.
1074   std::vector<SDVTList> VTList;
1075
1076   /// CondCodeNodes - Maps to auto-CSE operations.
1077   std::vector<CondCodeSDNode*> CondCodeNodes;
1078
1079   std::vector<SDNode*> ValueTypeNodes;
1080   std::map<EVT, SDNode*, EVT::compareRawBits> ExtendedValueTypeNodes;
1081   StringMap<SDNode*> ExternalSymbols;
1082
1083   std::map<std::pair<std::string, unsigned char>,SDNode*> TargetExternalSymbols;
1084 };
1085
1086 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
1087   typedef SelectionDAG::allnodes_iterator nodes_iterator;
1088   static nodes_iterator nodes_begin(SelectionDAG *G) {
1089     return G->allnodes_begin();
1090   }
1091   static nodes_iterator nodes_end(SelectionDAG *G) {
1092     return G->allnodes_end();
1093   }
1094 };
1095
1096 }  // end namespace llvm
1097
1098 #endif