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