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