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