LLVM API Documentation

SelectionDAG.h
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ---------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the SelectionDAG class, and transitively defines the
00011 // SDNode class and subclasses.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CODEGEN_SELECTIONDAG_H
00016 #define LLVM_CODEGEN_SELECTIONDAG_H
00017 
00018 #include "llvm/ADT/DenseSet.h"
00019 #include "llvm/ADT/SetVector.h"
00020 #include "llvm/ADT/StringMap.h"
00021 #include "llvm/ADT/ilist.h"
00022 #include "llvm/CodeGen/DAGCombine.h"
00023 #include "llvm/CodeGen/MachineFunction.h"
00024 #include "llvm/CodeGen/SelectionDAGNodes.h"
00025 #include "llvm/Support/RecyclingAllocator.h"
00026 #include "llvm/Target/TargetMachine.h"
00027 #include <cassert>
00028 #include <map>
00029 #include <string>
00030 #include <vector>
00031 
00032 namespace llvm {
00033 
00034 class AliasAnalysis;
00035 class MachineConstantPoolValue;
00036 class MachineFunction;
00037 class MDNode;
00038 class SDDbgValue;
00039 class TargetLowering;
00040 class TargetSelectionDAGInfo;
00041 
00042 class SDVTListNode : public FoldingSetNode {
00043   friend struct FoldingSetTrait<SDVTListNode>;
00044   /// FastID - A reference to an Interned FoldingSetNodeID for this node.
00045   /// The Allocator in SelectionDAG holds the data.
00046   /// SDVTList contains all types which are frequently accessed in SelectionDAG.
00047   /// The size of this list is not expected big so it won't introduce memory penalty.
00048   FoldingSetNodeIDRef FastID;
00049   const EVT *VTs;
00050   unsigned int NumVTs;
00051   /// The hash value for SDVTList is fixed so cache it to avoid hash calculation
00052   unsigned HashValue;
00053 public:
00054   SDVTListNode(const FoldingSetNodeIDRef ID, const EVT *VT, unsigned int Num) :
00055       FastID(ID), VTs(VT), NumVTs(Num) {
00056     HashValue = ID.ComputeHash();
00057   }
00058   SDVTList getSDVTList() {
00059     SDVTList result = {VTs, NumVTs};
00060     return result;
00061   }
00062 };
00063 
00064 // Specialize FoldingSetTrait for SDVTListNode
00065 // To avoid computing temp FoldingSetNodeID and hash value.
00066 template<> struct FoldingSetTrait<SDVTListNode> : DefaultFoldingSetTrait<SDVTListNode> {
00067   static void Profile(const SDVTListNode &X, FoldingSetNodeID& ID) {
00068     ID = X.FastID;
00069   }
00070   static bool Equals(const SDVTListNode &X, const FoldingSetNodeID &ID,
00071                      unsigned IDHash, FoldingSetNodeID &TempID) {
00072     if (X.HashValue != IDHash)
00073       return false;
00074     return ID == X.FastID;
00075   }
00076   static unsigned ComputeHash(const SDVTListNode &X, FoldingSetNodeID &TempID) {
00077     return X.HashValue;
00078   }
00079 };
00080 
00081 template<> struct ilist_traits<SDNode> : public ilist_default_traits<SDNode> {
00082 private:
00083   mutable ilist_half_node<SDNode> Sentinel;
00084 public:
00085   SDNode *createSentinel() const {
00086     return static_cast<SDNode*>(&Sentinel);
00087   }
00088   static void destroySentinel(SDNode *) {}
00089 
00090   SDNode *provideInitialHead() const { return createSentinel(); }
00091   SDNode *ensureHead(SDNode*) const { return createSentinel(); }
00092   static void noteHead(SDNode*, SDNode*) {}
00093 
00094   static void deleteNode(SDNode *) {
00095     llvm_unreachable("ilist_traits<SDNode> shouldn't see a deleteNode call!");
00096   }
00097 private:
00098   static void createNode(const SDNode &);
00099 };
00100 
00101 /// SDDbgInfo - Keeps track of dbg_value information through SDISel.  We do
00102 /// not build SDNodes for these so as not to perturb the generated code;
00103 /// instead the info is kept off to the side in this structure. Each SDNode may
00104 /// have one or more associated dbg_value entries. This information is kept in
00105 /// DbgValMap.
00106 /// Byval parameters are handled separately because they don't use alloca's,
00107 /// which busts the normal mechanism.  There is good reason for handling all
00108 /// parameters separately:  they may not have code generated for them, they
00109 /// should always go at the beginning of the function regardless of other code
00110 /// motion, and debug info for them is potentially useful even if the parameter
00111 /// is unused.  Right now only byval parameters are handled separately.
00112 class SDDbgInfo {
00113   SmallVector<SDDbgValue*, 32> DbgValues;
00114   SmallVector<SDDbgValue*, 32> ByvalParmDbgValues;
00115   typedef DenseMap<const SDNode*, SmallVector<SDDbgValue*, 2> > DbgValMapType;
00116   DbgValMapType DbgValMap;
00117 
00118   void operator=(const SDDbgInfo&) LLVM_DELETED_FUNCTION;
00119   SDDbgInfo(const SDDbgInfo&) LLVM_DELETED_FUNCTION;
00120 public:
00121   SDDbgInfo() {}
00122 
00123   void add(SDDbgValue *V, const SDNode *Node, bool isParameter) {
00124     if (isParameter) {
00125       ByvalParmDbgValues.push_back(V);
00126     } else     DbgValues.push_back(V);
00127     if (Node)
00128       DbgValMap[Node].push_back(V);
00129   }
00130 
00131   void clear() {
00132     DbgValMap.clear();
00133     DbgValues.clear();
00134     ByvalParmDbgValues.clear();
00135   }
00136 
00137   bool empty() const {
00138     return DbgValues.empty() && ByvalParmDbgValues.empty();
00139   }
00140 
00141   ArrayRef<SDDbgValue*> getSDDbgValues(const SDNode *Node) {
00142     DbgValMapType::iterator I = DbgValMap.find(Node);
00143     if (I != DbgValMap.end())
00144       return I->second;
00145     return ArrayRef<SDDbgValue*>();
00146   }
00147 
00148   typedef SmallVectorImpl<SDDbgValue*>::iterator DbgIterator;
00149   DbgIterator DbgBegin() { return DbgValues.begin(); }
00150   DbgIterator DbgEnd()   { return DbgValues.end(); }
00151   DbgIterator ByvalParmDbgBegin() { return ByvalParmDbgValues.begin(); }
00152   DbgIterator ByvalParmDbgEnd()   { return ByvalParmDbgValues.end(); }
00153 };
00154 
00155 class SelectionDAG;
00156 void checkForCycles(const SelectionDAG *DAG, bool force = false);
00157 
00158 /// SelectionDAG class - This is used to represent a portion of an LLVM function
00159 /// in a low-level Data Dependence DAG representation suitable for instruction
00160 /// selection.  This DAG is constructed as the first step of instruction
00161 /// selection in order to allow implementation of machine specific optimizations
00162 /// and code simplifications.
00163 ///
00164 /// The representation used by the SelectionDAG is a target-independent
00165 /// representation, which has some similarities to the GCC RTL representation,
00166 /// but is significantly more simple, powerful, and is a graph form instead of a
00167 /// linear form.
00168 ///
00169 class SelectionDAG {
00170   const TargetMachine &TM;
00171   const TargetSelectionDAGInfo *TSI;
00172   const TargetLowering *TLI;
00173   MachineFunction *MF;
00174   LLVMContext *Context;
00175   CodeGenOpt::Level OptLevel;
00176 
00177   /// EntryNode - The starting token.
00178   SDNode EntryNode;
00179 
00180   /// Root - The root of the entire DAG.
00181   SDValue Root;
00182 
00183   /// AllNodes - A linked list of nodes in the current DAG.
00184   ilist<SDNode> AllNodes;
00185 
00186   /// NodeAllocatorType - The AllocatorType for allocating SDNodes. We use
00187   /// pool allocation with recycling.
00188   typedef RecyclingAllocator<BumpPtrAllocator, SDNode, sizeof(LargestSDNode),
00189                              AlignOf<MostAlignedSDNode>::Alignment>
00190     NodeAllocatorType;
00191 
00192   /// NodeAllocator - Pool allocation for nodes.
00193   NodeAllocatorType NodeAllocator;
00194 
00195   /// CSEMap - This structure is used to memoize nodes, automatically performing
00196   /// CSE with existing nodes when a duplicate is requested.
00197   FoldingSet<SDNode> CSEMap;
00198 
00199   /// OperandAllocator - Pool allocation for machine-opcode SDNode operands.
00200   BumpPtrAllocator OperandAllocator;
00201 
00202   /// Allocator - Pool allocation for misc. objects that are created once per
00203   /// SelectionDAG.
00204   BumpPtrAllocator Allocator;
00205 
00206   /// DbgInfo - Tracks dbg_value information through SDISel.
00207   SDDbgInfo *DbgInfo;
00208 
00209 public:
00210   /// DAGUpdateListener - Clients of various APIs that cause global effects on
00211   /// the DAG can optionally implement this interface.  This allows the clients
00212   /// to handle the various sorts of updates that happen.
00213   ///
00214   /// A DAGUpdateListener automatically registers itself with DAG when it is
00215   /// constructed, and removes itself when destroyed in RAII fashion.
00216   struct DAGUpdateListener {
00217     DAGUpdateListener *const Next;
00218     SelectionDAG &DAG;
00219 
00220     explicit DAGUpdateListener(SelectionDAG &D)
00221       : Next(D.UpdateListeners), DAG(D) {
00222       DAG.UpdateListeners = this;
00223     }
00224 
00225     virtual ~DAGUpdateListener() {
00226       assert(DAG.UpdateListeners == this &&
00227              "DAGUpdateListeners must be destroyed in LIFO order");
00228       DAG.UpdateListeners = Next;
00229     }
00230 
00231     /// NodeDeleted - The node N that was deleted and, if E is not null, an
00232     /// equivalent node E that replaced it.
00233     virtual void NodeDeleted(SDNode *N, SDNode *E);
00234 
00235     /// NodeUpdated - The node N that was updated.
00236     virtual void NodeUpdated(SDNode *N);
00237   };
00238 
00239   /// NewNodesMustHaveLegalTypes - When true, additional steps are taken to
00240   /// ensure that getConstant() and similar functions return DAG nodes that
00241   /// have legal types. This is important after type legalization since
00242   /// any illegally typed nodes generated after this point will not experience
00243   /// type legalization.
00244   bool NewNodesMustHaveLegalTypes;
00245 
00246 private:
00247   /// DAGUpdateListener is a friend so it can manipulate the listener stack.
00248   friend struct DAGUpdateListener;
00249 
00250   /// UpdateListeners - Linked list of registered DAGUpdateListener instances.
00251   /// This stack is maintained by DAGUpdateListener RAII.
00252   DAGUpdateListener *UpdateListeners;
00253 
00254   /// setGraphColorHelper - Implementation of setSubgraphColor.
00255   /// Return whether we had to truncate the search.
00256   ///
00257   bool setSubgraphColorHelper(SDNode *N, const char *Color,
00258                               DenseSet<SDNode *> &visited,
00259                               int level, bool &printed);
00260 
00261   void operator=(const SelectionDAG&) LLVM_DELETED_FUNCTION;
00262   SelectionDAG(const SelectionDAG&) LLVM_DELETED_FUNCTION;
00263 
00264 public:
00265   explicit SelectionDAG(const TargetMachine &TM, llvm::CodeGenOpt::Level);
00266   ~SelectionDAG();
00267 
00268   /// init - Prepare this SelectionDAG to process code in the given
00269   /// MachineFunction.
00270   ///
00271   void init(MachineFunction &mf, const TargetLowering *TLI);
00272 
00273   /// clear - Clear state and free memory necessary to make this
00274   /// SelectionDAG ready to process a new block.
00275   ///
00276   void clear();
00277 
00278   MachineFunction &getMachineFunction() const { return *MF; }
00279   const TargetMachine &getTarget() const { return TM; }
00280   const TargetSubtargetInfo &getSubtarget() const { return MF->getSubtarget(); }
00281   const TargetLowering &getTargetLoweringInfo() const { return *TLI; }
00282   const TargetSelectionDAGInfo &getSelectionDAGInfo() const { return *TSI; }
00283   LLVMContext *getContext() const {return Context; }
00284 
00285   /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
00286   ///
00287   void viewGraph(const std::string &Title);
00288   void viewGraph();
00289 
00290 #ifndef NDEBUG
00291   std::map<const SDNode *, std::string> NodeGraphAttrs;
00292 #endif
00293 
00294   /// clearGraphAttrs - Clear all previously defined node graph attributes.
00295   /// Intended to be used from a debugging tool (eg. gdb).
00296   void clearGraphAttrs();
00297 
00298   /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
00299   ///
00300   void setGraphAttrs(const SDNode *N, const char *Attrs);
00301 
00302   /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
00303   /// Used from getNodeAttributes.
00304   const std::string getGraphAttrs(const SDNode *N) const;
00305 
00306   /// setGraphColor - Convenience for setting node color attribute.
00307   ///
00308   void setGraphColor(const SDNode *N, const char *Color);
00309 
00310   /// setGraphColor - Convenience for setting subgraph color attribute.
00311   ///
00312   void setSubgraphColor(SDNode *N, const char *Color);
00313 
00314   typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
00315   allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
00316   allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
00317   typedef ilist<SDNode>::iterator allnodes_iterator;
00318   allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
00319   allnodes_iterator allnodes_end() { return AllNodes.end(); }
00320   ilist<SDNode>::size_type allnodes_size() const {
00321     return AllNodes.size();
00322   }
00323 
00324   /// getRoot - Return the root tag of the SelectionDAG.
00325   ///
00326   const SDValue &getRoot() const { return Root; }
00327 
00328   /// getEntryNode - Return the token chain corresponding to the entry of the
00329   /// function.
00330   SDValue getEntryNode() const {
00331     return SDValue(const_cast<SDNode *>(&EntryNode), 0);
00332   }
00333 
00334   /// setRoot - Set the current root tag of the SelectionDAG.
00335   ///
00336   const SDValue &setRoot(SDValue N) {
00337     assert((!N.getNode() || N.getValueType() == MVT::Other) &&
00338            "DAG root value is not a chain!");
00339     if (N.getNode())
00340       checkForCycles(N.getNode(), this);
00341     Root = N;
00342     if (N.getNode())
00343       checkForCycles(this);
00344     return Root;
00345   }
00346 
00347   /// Combine - This iterates over the nodes in the SelectionDAG, folding
00348   /// certain types of nodes together, or eliminating superfluous nodes.  The
00349   /// Level argument controls whether Combine is allowed to produce nodes and
00350   /// types that are illegal on the target.
00351   void Combine(CombineLevel Level, AliasAnalysis &AA,
00352                CodeGenOpt::Level OptLevel);
00353 
00354   /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
00355   /// only uses types natively supported by the target.  Returns "true" if it
00356   /// made any changes.
00357   ///
00358   /// Note that this is an involved process that may invalidate pointers into
00359   /// the graph.
00360   bool LegalizeTypes();
00361 
00362   /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
00363   /// compatible with the target instruction selector, as indicated by the
00364   /// TargetLowering object.
00365   ///
00366   /// Note that this is an involved process that may invalidate pointers into
00367   /// the graph.
00368   void Legalize();
00369 
00370   /// \brief Transforms a SelectionDAG node and any operands to it into a node
00371   /// that is compatible with the target instruction selector, as indicated by
00372   /// the TargetLowering object.
00373   ///
00374   /// \returns true if \c N is a valid, legal node after calling this.
00375   ///
00376   /// This essentially runs a single recursive walk of the \c Legalize process
00377   /// over the given node (and its operands). This can be used to incrementally
00378   /// legalize the DAG. All of the nodes which are directly replaced,
00379   /// potentially including N, are added to the output parameter \c
00380   /// UpdatedNodes so that the delta to the DAG can be understood by the
00381   /// caller.
00382   ///
00383   /// When this returns false, N has been legalized in a way that make the
00384   /// pointer passed in no longer valid. It may have even been deleted from the
00385   /// DAG, and so it shouldn't be used further. When this returns true, the
00386   /// N passed in is a legal node, and can be immediately processed as such.
00387   /// This may still have done some work on the DAG, and will still populate
00388   /// UpdatedNodes with any new nodes replacing those originally in the DAG.
00389   bool LegalizeOp(SDNode *N, SmallSetVector<SDNode *, 16> &UpdatedNodes);
00390 
00391   /// LegalizeVectors - This transforms the SelectionDAG into a SelectionDAG
00392   /// that only uses vector math operations supported by the target.  This is
00393   /// necessary as a separate step from Legalize because unrolling a vector
00394   /// operation can introduce illegal types, which requires running
00395   /// LegalizeTypes again.
00396   ///
00397   /// This returns true if it made any changes; in that case, LegalizeTypes
00398   /// is called again before Legalize.
00399   ///
00400   /// Note that this is an involved process that may invalidate pointers into
00401   /// the graph.
00402   bool LegalizeVectors();
00403 
00404   /// RemoveDeadNodes - This method deletes all unreachable nodes in the
00405   /// SelectionDAG.
00406   void RemoveDeadNodes();
00407 
00408   /// DeleteNode - Remove the specified node from the system.  This node must
00409   /// have no referrers.
00410   void DeleteNode(SDNode *N);
00411 
00412   /// getVTList - Return an SDVTList that represents the list of values
00413   /// specified.
00414   SDVTList getVTList(EVT VT);
00415   SDVTList getVTList(EVT VT1, EVT VT2);
00416   SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3);
00417   SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4);
00418   SDVTList getVTList(ArrayRef<EVT> VTs);
00419 
00420   //===--------------------------------------------------------------------===//
00421   // Node creation methods.
00422   //
00423   SDValue getConstant(uint64_t Val, EVT VT, bool isTarget = false,
00424                       bool isOpaque = false);
00425   SDValue getConstant(const APInt &Val, EVT VT, bool isTarget = false,
00426                       bool isOpaque = false);
00427   SDValue getConstant(const ConstantInt &Val, EVT VT, bool isTarget = false,
00428                       bool isOpaque = false);
00429   SDValue getIntPtrConstant(uint64_t Val, bool isTarget = false);
00430   SDValue getTargetConstant(uint64_t Val, EVT VT, bool isOpaque = false) {
00431     return getConstant(Val, VT, true, isOpaque);
00432   }
00433   SDValue getTargetConstant(const APInt &Val, EVT VT, bool isOpaque = false) {
00434     return getConstant(Val, VT, true, isOpaque);
00435   }
00436   SDValue getTargetConstant(const ConstantInt &Val, EVT VT,
00437                             bool isOpaque = false) {
00438     return getConstant(Val, VT, true, isOpaque);
00439   }
00440   // The forms below that take a double should only be used for simple
00441   // constants that can be exactly represented in VT.  No checks are made.
00442   SDValue getConstantFP(double Val, EVT VT, bool isTarget = false);
00443   SDValue getConstantFP(const APFloat& Val, EVT VT, bool isTarget = false);
00444   SDValue getConstantFP(const ConstantFP &CF, EVT VT, bool isTarget = false);
00445   SDValue getTargetConstantFP(double Val, EVT VT) {
00446     return getConstantFP(Val, VT, true);
00447   }
00448   SDValue getTargetConstantFP(const APFloat& Val, EVT VT) {
00449     return getConstantFP(Val, VT, true);
00450   }
00451   SDValue getTargetConstantFP(const ConstantFP &Val, EVT VT) {
00452     return getConstantFP(Val, VT, true);
00453   }
00454   SDValue getGlobalAddress(const GlobalValue *GV, SDLoc DL, EVT VT,
00455                            int64_t offset = 0, bool isTargetGA = false,
00456                            unsigned char TargetFlags = 0);
00457   SDValue getTargetGlobalAddress(const GlobalValue *GV, SDLoc DL, EVT VT,
00458                                  int64_t offset = 0,
00459                                  unsigned char TargetFlags = 0) {
00460     return getGlobalAddress(GV, DL, VT, offset, true, TargetFlags);
00461   }
00462   SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
00463   SDValue getTargetFrameIndex(int FI, EVT VT) {
00464     return getFrameIndex(FI, VT, true);
00465   }
00466   SDValue getJumpTable(int JTI, EVT VT, bool isTarget = false,
00467                        unsigned char TargetFlags = 0);
00468   SDValue getTargetJumpTable(int JTI, EVT VT, unsigned char TargetFlags = 0) {
00469     return getJumpTable(JTI, VT, true, TargetFlags);
00470   }
00471   SDValue getConstantPool(const Constant *C, EVT VT,
00472                           unsigned Align = 0, int Offs = 0, bool isT=false,
00473                           unsigned char TargetFlags = 0);
00474   SDValue getTargetConstantPool(const Constant *C, EVT VT,
00475                                 unsigned Align = 0, int Offset = 0,
00476                                 unsigned char TargetFlags = 0) {
00477     return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
00478   }
00479   SDValue getConstantPool(MachineConstantPoolValue *C, EVT VT,
00480                           unsigned Align = 0, int Offs = 0, bool isT=false,
00481                           unsigned char TargetFlags = 0);
00482   SDValue getTargetConstantPool(MachineConstantPoolValue *C,
00483                                   EVT VT, unsigned Align = 0,
00484                                   int Offset = 0, unsigned char TargetFlags=0) {
00485     return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
00486   }
00487   SDValue getTargetIndex(int Index, EVT VT, int64_t Offset = 0,
00488                          unsigned char TargetFlags = 0);
00489   // When generating a branch to a BB, we don't in general know enough
00490   // to provide debug info for the BB at that time, so keep this one around.
00491   SDValue getBasicBlock(MachineBasicBlock *MBB);
00492   SDValue getBasicBlock(MachineBasicBlock *MBB, SDLoc dl);
00493   SDValue getExternalSymbol(const char *Sym, EVT VT);
00494   SDValue getExternalSymbol(const char *Sym, SDLoc dl, EVT VT);
00495   SDValue getTargetExternalSymbol(const char *Sym, EVT VT,
00496                                   unsigned char TargetFlags = 0);
00497   SDValue getValueType(EVT);
00498   SDValue getRegister(unsigned Reg, EVT VT);
00499   SDValue getRegisterMask(const uint32_t *RegMask);
00500   SDValue getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label);
00501   SDValue getBlockAddress(const BlockAddress *BA, EVT VT,
00502                           int64_t Offset = 0, bool isTarget = false,
00503                           unsigned char TargetFlags = 0);
00504   SDValue getTargetBlockAddress(const BlockAddress *BA, EVT VT,
00505                                 int64_t Offset = 0,
00506                                 unsigned char TargetFlags = 0) {
00507     return getBlockAddress(BA, VT, Offset, true, TargetFlags);
00508   }
00509 
00510   SDValue getCopyToReg(SDValue Chain, SDLoc dl, unsigned Reg, SDValue N) {
00511     return getNode(ISD::CopyToReg, dl, MVT::Other, Chain,
00512                    getRegister(Reg, N.getValueType()), N);
00513   }
00514 
00515   // This version of the getCopyToReg method takes an extra operand, which
00516   // indicates that there is potentially an incoming glue value (if Glue is not
00517   // null) and that there should be a glue result.
00518   SDValue getCopyToReg(SDValue Chain, SDLoc dl, unsigned Reg, SDValue N,
00519                        SDValue Glue) {
00520     SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
00521     SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Glue };
00522     return getNode(ISD::CopyToReg, dl, VTs,
00523                    ArrayRef<SDValue>(Ops, Glue.getNode() ? 4 : 3));
00524   }
00525 
00526   // Similar to last getCopyToReg() except parameter Reg is a SDValue
00527   SDValue getCopyToReg(SDValue Chain, SDLoc dl, SDValue Reg, SDValue N,
00528                          SDValue Glue) {
00529     SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
00530     SDValue Ops[] = { Chain, Reg, N, Glue };
00531     return getNode(ISD::CopyToReg, dl, VTs,
00532                    ArrayRef<SDValue>(Ops, Glue.getNode() ? 4 : 3));
00533   }
00534 
00535   SDValue getCopyFromReg(SDValue Chain, SDLoc dl, unsigned Reg, EVT VT) {
00536     SDVTList VTs = getVTList(VT, MVT::Other);
00537     SDValue Ops[] = { Chain, getRegister(Reg, VT) };
00538     return getNode(ISD::CopyFromReg, dl, VTs, Ops);
00539   }
00540 
00541   // This version of the getCopyFromReg method takes an extra operand, which
00542   // indicates that there is potentially an incoming glue value (if Glue is not
00543   // null) and that there should be a glue result.
00544   SDValue getCopyFromReg(SDValue Chain, SDLoc dl, unsigned Reg, EVT VT,
00545                            SDValue Glue) {
00546     SDVTList VTs = getVTList(VT, MVT::Other, MVT::Glue);
00547     SDValue Ops[] = { Chain, getRegister(Reg, VT), Glue };
00548     return getNode(ISD::CopyFromReg, dl, VTs,
00549                    ArrayRef<SDValue>(Ops, Glue.getNode() ? 3 : 2));
00550   }
00551 
00552   SDValue getCondCode(ISD::CondCode Cond);
00553 
00554   /// Returns the ConvertRndSat Note: Avoid using this node because it may
00555   /// disappear in the future and most targets don't support it.
00556   SDValue getConvertRndSat(EVT VT, SDLoc dl, SDValue Val, SDValue DTy,
00557                            SDValue STy,
00558                            SDValue Rnd, SDValue Sat, ISD::CvtCode Code);
00559 
00560   /// getVectorShuffle - Return an ISD::VECTOR_SHUFFLE node.  The number of
00561   /// elements in VT, which must be a vector type, must match the number of
00562   /// mask elements NumElts.  A integer mask element equal to -1 is treated as
00563   /// undefined.
00564   SDValue getVectorShuffle(EVT VT, SDLoc dl, SDValue N1, SDValue N2,
00565                            const int *MaskElts);
00566   SDValue getVectorShuffle(EVT VT, SDLoc dl, SDValue N1, SDValue N2,
00567                            ArrayRef<int> MaskElts) {
00568     assert(VT.getVectorNumElements() == MaskElts.size() &&
00569            "Must have the same number of vector elements as mask elements!");
00570     return getVectorShuffle(VT, dl, N1, N2, MaskElts.data());
00571   }
00572 
00573   /// \brief Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to
00574   /// the shuffle node in input but with swapped operands.
00575   ///
00576   /// Example: shuffle A, B, <0,5,2,7> -> shuffle B, A, <4,1,6,3>
00577   SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV);
00578 
00579   /// getAnyExtOrTrunc - Convert Op, which must be of integer type, to the
00580   /// integer type VT, by either any-extending or truncating it.
00581   SDValue getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT);
00582 
00583   /// getSExtOrTrunc - Convert Op, which must be of integer type, to the
00584   /// integer type VT, by either sign-extending or truncating it.
00585   SDValue getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT);
00586 
00587   /// getZExtOrTrunc - Convert Op, which must be of integer type, to the
00588   /// integer type VT, by either zero-extending or truncating it.
00589   SDValue getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT);
00590 
00591   /// getZeroExtendInReg - Return the expression required to zero extend the Op
00592   /// value assuming it was the smaller SrcTy value.
00593   SDValue getZeroExtendInReg(SDValue Op, SDLoc DL, EVT SrcTy);
00594 
00595   /// getAnyExtendVectorInReg - Return an operation which will any-extend the
00596   /// low lanes of the operand into the specified vector type. For example,
00597   /// this can convert a v16i8 into a v4i32 by any-extending the low four
00598   /// lanes of the operand from i8 to i32.
00599   SDValue getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT);
00600 
00601   /// getSignExtendVectorInReg - Return an operation which will sign extend the
00602   /// low lanes of the operand into the specified vector type. For example,
00603   /// this can convert a v16i8 into a v4i32 by sign extending the low four
00604   /// lanes of the operand from i8 to i32.
00605   SDValue getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT);
00606 
00607   /// getZeroExtendVectorInReg - Return an operation which will zero extend the
00608   /// low lanes of the operand into the specified vector type. For example,
00609   /// this can convert a v16i8 into a v4i32 by zero extending the low four
00610   /// lanes of the operand from i8 to i32.
00611   SDValue getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT);
00612 
00613   /// getBoolExtOrTrunc - Convert Op, which must be of integer type, to the
00614   /// integer type VT, by using an extension appropriate for the target's
00615   /// BooleanContent for type OpVT or truncating it.
00616   SDValue getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT, EVT OpVT);
00617 
00618   /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
00619   SDValue getNOT(SDLoc DL, SDValue Val, EVT VT);
00620 
00621   /// \brief Create a logical NOT operation as (XOR Val, BooleanOne).
00622   SDValue getLogicalNOT(SDLoc DL, SDValue Val, EVT VT);
00623 
00624   /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
00625   /// a glue result (to ensure it's not CSE'd).  CALLSEQ_START does not have a
00626   /// useful SDLoc.
00627   SDValue getCALLSEQ_START(SDValue Chain, SDValue Op, SDLoc DL) {
00628     SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
00629     SDValue Ops[] = { Chain,  Op };
00630     return getNode(ISD::CALLSEQ_START, DL, VTs, Ops);
00631   }
00632 
00633   /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
00634   /// glue result (to ensure it's not CSE'd).  CALLSEQ_END does not have
00635   /// a useful SDLoc.
00636   SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
00637                            SDValue InGlue, SDLoc DL) {
00638     SDVTList NodeTys = getVTList(MVT::Other, MVT::Glue);
00639     SmallVector<SDValue, 4> Ops;
00640     Ops.push_back(Chain);
00641     Ops.push_back(Op1);
00642     Ops.push_back(Op2);
00643     if (InGlue.getNode())
00644       Ops.push_back(InGlue);
00645     return getNode(ISD::CALLSEQ_END, DL, NodeTys, Ops);
00646   }
00647 
00648   /// getUNDEF - Return an UNDEF node.  UNDEF does not have a useful SDLoc.
00649   SDValue getUNDEF(EVT VT) {
00650     return getNode(ISD::UNDEF, SDLoc(), VT);
00651   }
00652 
00653   /// getGLOBAL_OFFSET_TABLE - Return a GLOBAL_OFFSET_TABLE node.  This does
00654   /// not have a useful SDLoc.
00655   SDValue getGLOBAL_OFFSET_TABLE(EVT VT) {
00656     return getNode(ISD::GLOBAL_OFFSET_TABLE, SDLoc(), VT);
00657   }
00658 
00659   /// getNode - Gets or creates the specified node.
00660   ///
00661   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT);
00662   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N);
00663   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1, SDValue N2,
00664                   bool nuw = false, bool nsw = false, bool exact = false);
00665   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1, SDValue N2,
00666                   SDValue N3);
00667   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1, SDValue N2,
00668                   SDValue N3, SDValue N4);
00669   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1, SDValue N2,
00670                   SDValue N3, SDValue N4, SDValue N5);
00671   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT, ArrayRef<SDUse> Ops);
00672   SDValue getNode(unsigned Opcode, SDLoc DL, EVT VT,
00673                   ArrayRef<SDValue> Ops);
00674   SDValue getNode(unsigned Opcode, SDLoc DL,
00675                   ArrayRef<EVT> ResultTys,
00676                   ArrayRef<SDValue> Ops);
00677   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
00678                   ArrayRef<SDValue> Ops);
00679   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs);
00680   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs, SDValue N);
00681   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
00682                   SDValue N1, SDValue N2);
00683   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
00684                   SDValue N1, SDValue N2, SDValue N3);
00685   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
00686                   SDValue N1, SDValue N2, SDValue N3, SDValue N4);
00687   SDValue getNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
00688                   SDValue N1, SDValue N2, SDValue N3, SDValue N4,
00689                   SDValue N5);
00690 
00691   /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
00692   /// the incoming stack arguments to be loaded from the stack. This is
00693   /// used in tail call lowering to protect stack arguments from being
00694   /// clobbered.
00695   SDValue getStackArgumentTokenFactor(SDValue Chain);
00696 
00697   SDValue getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst, SDValue Src,
00698                     SDValue Size, unsigned Align, bool isVol, bool AlwaysInline,
00699                     MachinePointerInfo DstPtrInfo,
00700                     MachinePointerInfo SrcPtrInfo);
00701 
00702   SDValue getMemmove(SDValue Chain, SDLoc dl, SDValue Dst, SDValue Src,
00703                      SDValue Size, unsigned Align, bool isVol,
00704                      MachinePointerInfo DstPtrInfo,
00705                      MachinePointerInfo SrcPtrInfo);
00706 
00707   SDValue getMemset(SDValue Chain, SDLoc dl, SDValue Dst, SDValue Src,
00708                     SDValue Size, unsigned Align, bool isVol,
00709                     MachinePointerInfo DstPtrInfo);
00710 
00711   /// getSetCC - Helper function to make it easier to build SetCC's if you just
00712   /// have an ISD::CondCode instead of an SDValue.
00713   ///
00714   SDValue getSetCC(SDLoc DL, EVT VT, SDValue LHS, SDValue RHS,
00715                    ISD::CondCode Cond) {
00716     assert(LHS.getValueType().isVector() == RHS.getValueType().isVector() &&
00717       "Cannot compare scalars to vectors");
00718     assert(LHS.getValueType().isVector() == VT.isVector() &&
00719       "Cannot compare scalars to vectors");
00720     assert(Cond != ISD::SETCC_INVALID &&
00721         "Cannot create a setCC of an invalid node.");
00722     return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
00723   }
00724 
00725   // getSelect - Helper function to make it easier to build Select's if you just
00726   // have operands and don't want to check for vector.
00727   SDValue getSelect(SDLoc DL, EVT VT, SDValue Cond,
00728                     SDValue LHS, SDValue RHS) {
00729     assert(LHS.getValueType() == RHS.getValueType() &&
00730            "Cannot use select on differing types");
00731     assert(VT.isVector() == LHS.getValueType().isVector() &&
00732            "Cannot mix vectors and scalars");
00733     return getNode(Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT, DL, VT,
00734                    Cond, LHS, RHS);
00735   }
00736 
00737   /// getSelectCC - Helper function to make it easier to build SelectCC's if you
00738   /// just have an ISD::CondCode instead of an SDValue.
00739   ///
00740   SDValue getSelectCC(SDLoc DL, SDValue LHS, SDValue RHS,
00741                       SDValue True, SDValue False, ISD::CondCode Cond) {
00742     return getNode(ISD::SELECT_CC, DL, True.getValueType(),
00743                    LHS, RHS, True, False, getCondCode(Cond));
00744   }
00745 
00746   /// getVAArg - VAArg produces a result and token chain, and takes a pointer
00747   /// and a source value as input.
00748   SDValue getVAArg(EVT VT, SDLoc dl, SDValue Chain, SDValue Ptr,
00749                    SDValue SV, unsigned Align);
00750 
00751   /// getAtomicCmpSwap - Gets a node for an atomic cmpxchg op. There are two
00752   /// valid Opcodes. ISD::ATOMIC_CMO_SWAP produces the value loaded and a
00753   /// chain result. ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS produces the value loaded,
00754   /// a success flag (initially i1), and a chain.
00755   SDValue getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs,
00756                            SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp,
00757                            MachinePointerInfo PtrInfo, unsigned Alignment,
00758                            AtomicOrdering SuccessOrdering,
00759                            AtomicOrdering FailureOrdering,
00760                            SynchronizationScope SynchScope);
00761   SDValue getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs,
00762                            SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp,
00763                            MachineMemOperand *MMO,
00764                            AtomicOrdering SuccessOrdering,
00765                            AtomicOrdering FailureOrdering,
00766                            SynchronizationScope SynchScope);
00767 
00768   /// getAtomic - Gets a node for an atomic op, produces result (if relevant)
00769   /// and chain and takes 2 operands.
00770   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, SDValue Chain,
00771                     SDValue Ptr, SDValue Val, const Value *PtrVal,
00772                     unsigned Alignment, AtomicOrdering Ordering,
00773                     SynchronizationScope SynchScope);
00774   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, SDValue Chain,
00775                     SDValue Ptr, SDValue Val, MachineMemOperand *MMO,
00776                     AtomicOrdering Ordering,
00777                     SynchronizationScope SynchScope);
00778 
00779   /// getAtomic - Gets a node for an atomic op, produces result and chain and
00780   /// takes 1 operand.
00781   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, EVT VT,
00782                     SDValue Chain, SDValue Ptr, MachineMemOperand *MMO,
00783                     AtomicOrdering Ordering,
00784                     SynchronizationScope SynchScope);
00785 
00786   /// getAtomic - Gets a node for an atomic op, produces result and chain and
00787   /// takes N operands.
00788   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTList,
00789                     ArrayRef<SDValue> Ops, MachineMemOperand *MMO,
00790                     AtomicOrdering SuccessOrdering,
00791                     AtomicOrdering FailureOrdering,
00792                     SynchronizationScope SynchScope);
00793   SDValue getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTList,
00794                     ArrayRef<SDValue> Ops, MachineMemOperand *MMO,
00795                     AtomicOrdering Ordering, SynchronizationScope SynchScope);
00796 
00797   /// getMemIntrinsicNode - Creates a MemIntrinsicNode that may produce a
00798   /// result and takes a list of operands. Opcode may be INTRINSIC_VOID,
00799   /// INTRINSIC_W_CHAIN, or a target-specific opcode with a value not
00800   /// less than FIRST_TARGET_MEMORY_OPCODE.
00801   SDValue getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
00802                               ArrayRef<SDValue> Ops,
00803                               EVT MemVT, MachinePointerInfo PtrInfo,
00804                               unsigned Align = 0, bool Vol = false,
00805                               bool ReadMem = true, bool WriteMem = true,
00806                               unsigned Size = 0);
00807 
00808   SDValue getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
00809                               ArrayRef<SDValue> Ops,
00810                               EVT MemVT, MachineMemOperand *MMO);
00811 
00812   /// getMergeValues - Create a MERGE_VALUES node from the given operands.
00813   SDValue getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl);
00814 
00815   /// getLoad - Loads are not normal binary operators: their result type is not
00816   /// determined by their operands, and they produce a value AND a token chain.
00817   ///
00818   SDValue getLoad(EVT VT, SDLoc dl, SDValue Chain, SDValue Ptr,
00819                   MachinePointerInfo PtrInfo, bool isVolatile,
00820                   bool isNonTemporal, bool isInvariant, unsigned Alignment,
00821                   const AAMDNodes &AAInfo = AAMDNodes(),
00822                   const MDNode *Ranges = nullptr);
00823   SDValue getLoad(EVT VT, SDLoc dl, SDValue Chain, SDValue Ptr,
00824                   MachineMemOperand *MMO);
00825   SDValue getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
00826                      SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo,
00827                      EVT MemVT, bool isVolatile,
00828                      bool isNonTemporal, bool isInvariant, unsigned Alignment,
00829                      const AAMDNodes &AAInfo = AAMDNodes());
00830   SDValue getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
00831                      SDValue Chain, SDValue Ptr, EVT MemVT,
00832                      MachineMemOperand *MMO);
00833   SDValue getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
00834                          SDValue Offset, ISD::MemIndexedMode AM);
00835   SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
00836                   EVT VT, SDLoc dl,
00837                   SDValue Chain, SDValue Ptr, SDValue Offset,
00838                   MachinePointerInfo PtrInfo, EVT MemVT,
00839                   bool isVolatile, bool isNonTemporal, bool isInvariant,
00840                   unsigned Alignment, const AAMDNodes &AAInfo = AAMDNodes(),
00841                   const MDNode *Ranges = nullptr);
00842   SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
00843                   EVT VT, SDLoc dl,
00844                   SDValue Chain, SDValue Ptr, SDValue Offset,
00845                   EVT MemVT, MachineMemOperand *MMO);
00846 
00847   /// getStore - Helper function to build ISD::STORE nodes.
00848   ///
00849   SDValue getStore(SDValue Chain, SDLoc dl, SDValue Val, SDValue Ptr,
00850                    MachinePointerInfo PtrInfo, bool isVolatile,
00851                    bool isNonTemporal, unsigned Alignment,
00852                    const AAMDNodes &AAInfo = AAMDNodes());
00853   SDValue getStore(SDValue Chain, SDLoc dl, SDValue Val, SDValue Ptr,
00854                    MachineMemOperand *MMO);
00855   SDValue getTruncStore(SDValue Chain, SDLoc dl, SDValue Val, SDValue Ptr,
00856                         MachinePointerInfo PtrInfo, EVT TVT,
00857                         bool isNonTemporal, bool isVolatile,
00858                         unsigned Alignment,
00859                         const AAMDNodes &AAInfo = AAMDNodes());
00860   SDValue getTruncStore(SDValue Chain, SDLoc dl, SDValue Val, SDValue Ptr,
00861                         EVT TVT, MachineMemOperand *MMO);
00862   SDValue getIndexedStore(SDValue OrigStoe, SDLoc dl, SDValue Base,
00863                            SDValue Offset, ISD::MemIndexedMode AM);
00864 
00865   /// getSrcValue - Construct a node to track a Value* through the backend.
00866   SDValue getSrcValue(const Value *v);
00867 
00868   /// getMDNode - Return an MDNodeSDNode which holds an MDNode.
00869   SDValue getMDNode(const MDNode *MD);
00870 
00871   /// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
00872   SDValue getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
00873                            unsigned SrcAS, unsigned DestAS);
00874 
00875   /// getShiftAmountOperand - Return the specified value casted to
00876   /// the target's desired shift amount type.
00877   SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op);
00878 
00879   /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
00880   /// specified operands.  If the resultant node already exists in the DAG,
00881   /// this does not modify the specified node, instead it returns the node that
00882   /// already exists.  If the resultant node does not exist in the DAG, the
00883   /// input node is returned.  As a degenerate case, if you specify the same
00884   /// input operands as the node already has, the input node is returned.
00885   SDNode *UpdateNodeOperands(SDNode *N, SDValue Op);
00886   SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2);
00887   SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
00888                                SDValue Op3);
00889   SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
00890                                SDValue Op3, SDValue Op4);
00891   SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
00892                                SDValue Op3, SDValue Op4, SDValue Op5);
00893   SDNode *UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops);
00894 
00895   /// SelectNodeTo - These are used for target selectors to *mutate* the
00896   /// specified node to have the specified return type, Target opcode, and
00897   /// operands.  Note that target opcodes are stored as
00898   /// ~TargetOpcode in the node opcode field.  The resultant node is returned.
00899   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT);
00900   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT, SDValue Op1);
00901   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
00902                        SDValue Op1, SDValue Op2);
00903   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
00904                        SDValue Op1, SDValue Op2, SDValue Op3);
00905   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
00906                        ArrayRef<SDValue> Ops);
00907   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1, EVT VT2);
00908   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
00909                        EVT VT2, ArrayRef<SDValue> Ops);
00910   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
00911                        EVT VT2, EVT VT3, ArrayRef<SDValue> Ops);
00912   SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
00913                        EVT VT2, EVT VT3, EVT VT4, ArrayRef<SDValue> Ops);
00914   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
00915                        EVT VT2, SDValue Op1);
00916   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
00917                        EVT VT2, SDValue Op1, SDValue Op2);
00918   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
00919                        EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
00920   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
00921                        EVT VT2, EVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
00922   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, SDVTList VTs,
00923                        ArrayRef<SDValue> Ops);
00924 
00925   /// MorphNodeTo - This *mutates* the specified node to have the specified
00926   /// return type, opcode, and operands.
00927   SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
00928                       ArrayRef<SDValue> Ops);
00929 
00930   /// getMachineNode - These are used for target selectors to create a new node
00931   /// with specified return type(s), MachineInstr opcode, and operands.
00932   ///
00933   /// Note that getMachineNode returns the resultant node.  If there is already
00934   /// a node of the specified opcode and operands, it returns that node instead
00935   /// of the current one.
00936   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT);
00937   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
00938                                 SDValue Op1);
00939   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
00940                                 SDValue Op1, SDValue Op2);
00941   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
00942                                 SDValue Op1, SDValue Op2, SDValue Op3);
00943   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
00944                                 ArrayRef<SDValue> Ops);
00945   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2);
00946   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
00947                                 SDValue Op1);
00948   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
00949                                 SDValue Op1, SDValue Op2);
00950   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
00951                                 SDValue Op1, SDValue Op2, SDValue Op3);
00952   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
00953                                 ArrayRef<SDValue> Ops);
00954   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
00955                                 EVT VT3, SDValue Op1, SDValue Op2);
00956   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
00957                                 EVT VT3, SDValue Op1, SDValue Op2,
00958                                 SDValue Op3);
00959   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
00960                                 EVT VT3, ArrayRef<SDValue> Ops);
00961   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2,
00962                                 EVT VT3, EVT VT4, ArrayRef<SDValue> Ops);
00963   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl,
00964                                 ArrayRef<EVT> ResultTys,
00965                                 ArrayRef<SDValue> Ops);
00966   MachineSDNode *getMachineNode(unsigned Opcode, SDLoc dl, SDVTList VTs,
00967                                 ArrayRef<SDValue> Ops);
00968 
00969   /// getTargetExtractSubreg - A convenience function for creating
00970   /// TargetInstrInfo::EXTRACT_SUBREG nodes.
00971   SDValue getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
00972                                  SDValue Operand);
00973 
00974   /// getTargetInsertSubreg - A convenience function for creating
00975   /// TargetInstrInfo::INSERT_SUBREG nodes.
00976   SDValue getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
00977                                 SDValue Operand, SDValue Subreg);
00978 
00979   /// getNodeIfExists - Get the specified node if it's already available, or
00980   /// else return NULL.
00981   SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs, ArrayRef<SDValue> Ops,
00982                           bool nuw = false, bool nsw = false,
00983                           bool exact = false);
00984 
00985   /// getDbgValue - Creates a SDDbgValue node.
00986   ///
00987   SDDbgValue *getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R,
00988         bool IsIndirect, uint64_t Off,
00989                           DebugLoc DL, unsigned O);
00990   /// Constant.
00991   SDDbgValue *getConstantDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
00992           DebugLoc DL, unsigned O);
00993   /// Frame index.
00994   SDDbgValue *getFrameIndexDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
00995             DebugLoc DL, unsigned O);
00996 
00997   /// RemoveDeadNode - Remove the specified node from the system. If any of its
00998   /// operands then becomes dead, remove them as well. Inform UpdateListener
00999   /// for each node deleted.
01000   void RemoveDeadNode(SDNode *N);
01001 
01002   /// RemoveDeadNodes - This method deletes the unreachable nodes in the
01003   /// given list, and any nodes that become unreachable as a result.
01004   void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes);
01005 
01006   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
01007   /// This can cause recursive merging of nodes in the DAG.  Use the first
01008   /// version if 'From' is known to have a single result, use the second
01009   /// if you have two nodes with identical results (or if 'To' has a superset
01010   /// of the results of 'From'), use the third otherwise.
01011   ///
01012   /// These methods all take an optional UpdateListener, which (if not null) is
01013   /// informed about nodes that are deleted and modified due to recursive
01014   /// changes in the dag.
01015   ///
01016   /// These functions only replace all existing uses. It's possible that as
01017   /// these replacements are being performed, CSE may cause the From node
01018   /// to be given new uses. These new uses of From are left in place, and
01019   /// not automatically transferred to To.
01020   ///
01021   void ReplaceAllUsesWith(SDValue From, SDValue Op);
01022   void ReplaceAllUsesWith(SDNode *From, SDNode *To);
01023   void ReplaceAllUsesWith(SDNode *From, const SDValue *To);
01024 
01025   /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
01026   /// uses of other values produced by From.Val alone.
01027   void ReplaceAllUsesOfValueWith(SDValue From, SDValue To);
01028 
01029   /// ReplaceAllUsesOfValuesWith - Like ReplaceAllUsesOfValueWith, but
01030   /// for multiple values at once. This correctly handles the case where
01031   /// there is an overlap between the From values and the To values.
01032   void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
01033                                   unsigned Num);
01034 
01035   /// AssignTopologicalOrder - Topological-sort the AllNodes list and a
01036   /// assign a unique node id for each node in the DAG based on their
01037   /// topological order. Returns the number of nodes.
01038   unsigned AssignTopologicalOrder();
01039 
01040   /// RepositionNode - Move node N in the AllNodes list to be immediately
01041   /// before the given iterator Position. This may be used to update the
01042   /// topological ordering when the list of nodes is modified.
01043   void RepositionNode(allnodes_iterator Position, SDNode *N) {
01044     AllNodes.insert(Position, AllNodes.remove(N));
01045   }
01046 
01047   /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
01048   /// operation.
01049   static bool isCommutativeBinOp(unsigned Opcode) {
01050     // FIXME: This should get its info from the td file, so that we can include
01051     // target info.
01052     switch (Opcode) {
01053     case ISD::ADD:
01054     case ISD::MUL:
01055     case ISD::MULHU:
01056     case ISD::MULHS:
01057     case ISD::SMUL_LOHI:
01058     case ISD::UMUL_LOHI:
01059     case ISD::FADD:
01060     case ISD::FMUL:
01061     case ISD::AND:
01062     case ISD::OR:
01063     case ISD::XOR:
01064     case ISD::SADDO:
01065     case ISD::UADDO:
01066     case ISD::ADDC:
01067     case ISD::ADDE: return true;
01068     default: return false;
01069     }
01070   }
01071 
01072   /// Returns an APFloat semantics tag appropriate for the given type. If VT is
01073   /// a vector type, the element semantics are returned.
01074   static const fltSemantics &EVTToAPFloatSemantics(EVT VT) {
01075     switch (VT.getScalarType().getSimpleVT().SimpleTy) {
01076     default: llvm_unreachable("Unknown FP format");
01077     case MVT::f16:     return APFloat::IEEEhalf;
01078     case MVT::f32:     return APFloat::IEEEsingle;
01079     case MVT::f64:     return APFloat::IEEEdouble;
01080     case MVT::f80:     return APFloat::x87DoubleExtended;
01081     case MVT::f128:    return APFloat::IEEEquad;
01082     case MVT::ppcf128: return APFloat::PPCDoubleDouble;
01083     }
01084   }
01085 
01086   /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
01087   /// value is produced by SD.
01088   void AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter);
01089 
01090   /// GetDbgValues - Get the debug values which reference the given SDNode.
01091   ArrayRef<SDDbgValue*> GetDbgValues(const SDNode* SD) {
01092     return DbgInfo->getSDDbgValues(SD);
01093   }
01094 
01095   /// TransferDbgValues - Transfer SDDbgValues.
01096   void TransferDbgValues(SDValue From, SDValue To);
01097 
01098   /// hasDebugValues - Return true if there are any SDDbgValue nodes associated
01099   /// with this SelectionDAG.
01100   bool hasDebugValues() const { return !DbgInfo->empty(); }
01101 
01102   SDDbgInfo::DbgIterator DbgBegin() { return DbgInfo->DbgBegin(); }
01103   SDDbgInfo::DbgIterator DbgEnd()   { return DbgInfo->DbgEnd(); }
01104   SDDbgInfo::DbgIterator ByvalParmDbgBegin() {
01105     return DbgInfo->ByvalParmDbgBegin();
01106   }
01107   SDDbgInfo::DbgIterator ByvalParmDbgEnd()   {
01108     return DbgInfo->ByvalParmDbgEnd();
01109   }
01110 
01111   void dump() const;
01112 
01113   /// CreateStackTemporary - Create a stack temporary, suitable for holding the
01114   /// specified value type.  If minAlign is specified, the slot size will have
01115   /// at least that alignment.
01116   SDValue CreateStackTemporary(EVT VT, unsigned minAlign = 1);
01117 
01118   /// CreateStackTemporary - Create a stack temporary suitable for holding
01119   /// either of the specified value types.
01120   SDValue CreateStackTemporary(EVT VT1, EVT VT2);
01121 
01122   /// FoldConstantArithmetic -
01123   SDValue FoldConstantArithmetic(unsigned Opcode, EVT VT,
01124                                  SDNode *Cst1, SDNode *Cst2);
01125 
01126   /// FoldSetCC - Constant fold a setcc to true or false.
01127   SDValue FoldSetCC(EVT VT, SDValue N1,
01128                     SDValue N2, ISD::CondCode Cond, SDLoc dl);
01129 
01130   /// SignBitIsZero - Return true if the sign bit of Op is known to be zero.  We
01131   /// use this predicate to simplify operations downstream.
01132   bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;
01133 
01134   /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero.  We
01135   /// use this predicate to simplify operations downstream.  Op and Mask are
01136   /// known to be the same type.
01137   bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth = 0)
01138     const;
01139 
01140   /// Determine which bits of Op are known to be either zero or one and return
01141   /// them in the KnownZero/KnownOne bitsets.  Targets can implement the
01142   /// computeKnownBitsForTargetNode method in the TargetLowering class to allow
01143   /// target nodes to be understood.
01144   void computeKnownBits(SDValue Op, APInt &KnownZero, APInt &KnownOne,
01145                         unsigned Depth = 0) const;
01146 
01147   /// ComputeNumSignBits - Return the number of times the sign bit of the
01148   /// register is replicated into the other bits.  We know that at least 1 bit
01149   /// is always equal to the sign bit (itself), but other cases can give us
01150   /// information.  For example, immediately after an "SRA X, 2", we know that
01151   /// the top 3 bits are all equal to each other, so we return 3.  Targets can
01152   /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
01153   /// class to allow target nodes to be understood.
01154   unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const;
01155 
01156   /// isBaseWithConstantOffset - Return true if the specified operand is an
01157   /// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
01158   /// ISD::OR with a ConstantSDNode that is guaranteed to have the same
01159   /// semantics as an ADD.  This handles the equivalence:
01160   ///     X|Cst == X+Cst iff X&Cst = 0.
01161   bool isBaseWithConstantOffset(SDValue Op) const;
01162 
01163   /// isKnownNeverNan - Test whether the given SDValue is known to never be NaN.
01164   bool isKnownNeverNaN(SDValue Op) const;
01165 
01166   /// isKnownNeverZero - Test whether the given SDValue is known to never be
01167   /// positive or negative Zero.
01168   bool isKnownNeverZero(SDValue Op) const;
01169 
01170   /// isEqualTo - Test whether two SDValues are known to compare equal. This
01171   /// is true if they are the same value, or if one is negative zero and the
01172   /// other positive zero.
01173   bool isEqualTo(SDValue A, SDValue B) const;
01174 
01175   /// UnrollVectorOp - Utility function used by legalize and lowering to
01176   /// "unroll" a vector operation by splitting out the scalars and operating
01177   /// on each element individually.  If the ResNE is 0, fully unroll the vector
01178   /// op. If ResNE is less than the width of the vector op, unroll up to ResNE.
01179   /// If the  ResNE is greater than the width of the vector op, unroll the
01180   /// vector op and fill the end of the resulting vector with UNDEFS.
01181   SDValue UnrollVectorOp(SDNode *N, unsigned ResNE = 0);
01182 
01183   /// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
01184   /// location that is 'Dist' units away from the location that the 'Base' load
01185   /// is loading from.
01186   bool isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
01187                          unsigned Bytes, int Dist) const;
01188 
01189   /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
01190   /// it cannot be inferred.
01191   unsigned InferPtrAlignment(SDValue Ptr) const;
01192 
01193   /// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
01194   /// which is split (or expanded) into two not necessarily identical pieces.
01195   std::pair<EVT, EVT> GetSplitDestVTs(const EVT &VT) const;
01196 
01197   /// SplitVector - Split the vector with EXTRACT_SUBVECTOR using the provides
01198   /// VTs and return the low/high part.
01199   std::pair<SDValue, SDValue> SplitVector(const SDValue &N, const SDLoc &DL,
01200                                           const EVT &LoVT, const EVT &HiVT);
01201 
01202   /// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
01203   /// low/high part.
01204   std::pair<SDValue, SDValue> SplitVector(const SDValue &N, const SDLoc &DL) {
01205     EVT LoVT, HiVT;
01206     std::tie(LoVT, HiVT) = GetSplitDestVTs(N.getValueType());
01207     return SplitVector(N, DL, LoVT, HiVT);
01208   }
01209 
01210   /// SplitVectorOperand - Split the node's operand with EXTRACT_SUBVECTOR and
01211   /// return the low/high part.
01212   std::pair<SDValue, SDValue> SplitVectorOperand(const SDNode *N, unsigned OpNo)
01213   {
01214     return SplitVector(N->getOperand(OpNo), SDLoc(N));
01215   }
01216 
01217   /// ExtractVectorElements - Append the extracted elements from Start to Count
01218   /// out of the vector Op in Args. If Count is 0, all of the elements will be
01219   /// extracted.
01220   void ExtractVectorElements(SDValue Op, SmallVectorImpl<SDValue> &Args,
01221                              unsigned Start = 0, unsigned Count = 0);
01222 
01223   unsigned getEVTAlignment(EVT MemoryVT) const;
01224 
01225 private:
01226   void InsertNode(SDNode *N);
01227   bool RemoveNodeFromCSEMaps(SDNode *N);
01228   void AddModifiedNodeToCSEMaps(SDNode *N);
01229   SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
01230   SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
01231                                void *&InsertPos);
01232   SDNode *FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
01233                                void *&InsertPos);
01234   SDNode *UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc loc);
01235 
01236   void DeleteNodeNotInCSEMaps(SDNode *N);
01237   void DeallocateNode(SDNode *N);
01238 
01239   void allnodes_clear();
01240 
01241   BinarySDNode *GetBinarySDNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
01242                                 SDValue N1, SDValue N2, bool nuw, bool nsw,
01243                                 bool exact);
01244 
01245   /// VTList - List of non-single value types.
01246   FoldingSet<SDVTListNode> VTListMap;
01247 
01248   /// CondCodeNodes - Maps to auto-CSE operations.
01249   std::vector<CondCodeSDNode*> CondCodeNodes;
01250 
01251   std::vector<SDNode*> ValueTypeNodes;
01252   std::map<EVT, SDNode*, EVT::compareRawBits> ExtendedValueTypeNodes;
01253   StringMap<SDNode*> ExternalSymbols;
01254 
01255   std::map<std::pair<std::string, unsigned char>,SDNode*> TargetExternalSymbols;
01256 };
01257 
01258 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
01259   typedef SelectionDAG::allnodes_iterator nodes_iterator;
01260   static nodes_iterator nodes_begin(SelectionDAG *G) {
01261     return G->allnodes_begin();
01262   }
01263   static nodes_iterator nodes_end(SelectionDAG *G) {
01264     return G->allnodes_end();
01265   }
01266 };
01267 
01268 }  // end namespace llvm
01269 
01270 #endif