start defining codes for instructions
[oota-llvm.git] / include / llvm / CodeGen / SelectionDAG.h
index f4ea64d46db637376f5935fc5f76e6adb9f78c03..9d8024b86d4f8b769f8f07582ef097b1fe86f95d 100644 (file)
@@ -15,9 +15,9 @@
 #ifndef LLVM_CODEGEN_SELECTIONDAG_H
 #define LLVM_CODEGEN_SELECTIONDAG_H
 
-#include "llvm/CodeGen/SelectionDAGNodes.h"
-#include "llvm/CodeGen/SelectionDAGCSEMap.h"
+#include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/ilist"
+#include "llvm/CodeGen/SelectionDAGNodes.h"
 
 #include <list>
 #include <vector>
 #include <string>
 
 namespace llvm {
+  class AliasAnalysis;
   class TargetLowering;
   class TargetMachine;
-  class MachineDebugInfo;
+  class MachineModuleInfo;
   class MachineFunction;
+  class MachineConstantPoolValue;
 
 /// SelectionDAG class - This is used to represent a portion of an LLVM function
 /// in a low-level Data Dependence DAG representation suitable for instruction
@@ -45,7 +47,7 @@ namespace llvm {
 class SelectionDAG {
   TargetLowering &TLI;
   MachineFunction &MF;
-  MachineDebugInfo *DI;
+  MachineModuleInfo *MMI;
 
   /// Root - The root of the entire DAG.  EntryNode - The starting token.
   SDOperand Root, EntryNode;
@@ -55,11 +57,11 @@ class SelectionDAG {
 
   /// CSEMap - This structure is used to memoize nodes, automatically performing
   /// CSE with existing nodes with a duplicate is requested.
-  SelectionDAGCSEMap CSEMap;
+  FoldingSet<SDNode> CSEMap;
 
 public:
-  SelectionDAG(TargetLowering &tli, MachineFunction &mf, MachineDebugInfo *di)
-  : TLI(tli), MF(mf), DI(di) {
+  SelectionDAG(TargetLowering &tli, MachineFunction &mf, MachineModuleInfo *mmi)
+  : TLI(tli), MF(mf), MMI(mmi) {
     EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
   }
   ~SelectionDAG();
@@ -67,12 +69,31 @@ public:
   MachineFunction &getMachineFunction() const { return MF; }
   const TargetMachine &getTarget() const;
   TargetLowering &getTargetLoweringInfo() const { return TLI; }
-  MachineDebugInfo *getMachineDebugInfo() const { return DI; }
+  MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
 
-  /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
+  /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
   ///
   void viewGraph();
+  
+#ifndef NDEBUG
+  std::map<const SDNode *, std::string> NodeGraphAttrs;
+#endif
 
+  /// clearGraphAttrs - Clear all previously defined node graph attributes.
+  /// Intended to be used from a debugging tool (eg. gdb).
+  void clearGraphAttrs();
+  
+  /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
+  ///
+  void setGraphAttrs(const SDNode *N, const char *Attrs);
+  
+  /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
+  /// Used from getNodeAttributes.
+  const std::string getGraphAttrs(const SDNode *N) const;
+  
+  /// setGraphColor - Convenience for setting node color attribute.
+  ///
+  void setGraphColor(const SDNode *N, const char *Color);
 
   typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
   allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
@@ -97,7 +118,7 @@ public:
   /// certain types of nodes together, or eliminating superfluous nodes.  When
   /// the AfterLegalize argument is set to 'true', Combine takes care not to
   /// generate any nodes that will be illegal on the target.
-  void Combine(bool AfterLegalize);
+  void Combine(bool AfterLegalize, AliasAnalysis &AA);
   
   /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
   /// compatible with the target instruction selector, as indicated by the
@@ -110,7 +131,16 @@ public:
   /// RemoveDeadNodes - This method deletes all unreachable nodes in the
   /// SelectionDAG.
   void RemoveDeadNodes();
+
+  /// RemoveDeadNode - Remove the specified node from the system. If any of its
+  /// operands then becomes dead, remove them as well. The vector Deleted is
+  /// populated with nodes that are deleted.
+  void RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted);
   
+  /// DeleteNode - Remove the specified node from the system.  This node must
+  /// have no referrers.
+  void DeleteNode(SDNode *N);
+
   /// getVTList - Return an SDVTList that represents the list of values
   /// specified.
   SDVTList getVTList(MVT::ValueType VT);
@@ -135,7 +165,7 @@ public:
   }
   
   
-  //===----------------------------------------------------------------------===//
+  //===--------------------------------------------------------------------===//
   // Node creation methods.
   //
   SDOperand getString(const std::string &Val);
@@ -167,6 +197,13 @@ public:
                                   unsigned Align = 0, int Offset = 0) {
     return getConstantPool(C, VT, Align, Offset, true);
   }
+  SDOperand getConstantPool(MachineConstantPoolValue *C, MVT::ValueType VT,
+                            unsigned Align = 0, int Offs = 0, bool isT=false);
+  SDOperand getTargetConstantPool(MachineConstantPoolValue *C,
+                                  MVT::ValueType VT, unsigned Align = 0,
+                                  int Offset = 0) {
+    return getConstantPool(C, VT, Align, Offset, true);
+  }
   SDOperand getBasicBlock(MachineBasicBlock *MBB);
   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
   SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
@@ -274,11 +311,27 @@ public:
   /// determined by their operands, and they produce a value AND a token chain.
   ///
   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
-                    SDOperand SV);
+                    const Value *SV, int SVOffset, bool isVolatile=false,
+                    unsigned Alignment=0);
+  SDOperand getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
+                       SDOperand Chain, SDOperand Ptr, const Value *SV,
+                       int SVOffset, MVT::ValueType EVT, bool isVolatile=false,
+                       unsigned Alignment=0);
+  SDOperand getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
+                           SDOperand Offset, ISD::MemIndexedMode AM);
   SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain, 
                        SDOperand Ptr, SDOperand SV);
-  SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
-                       SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
+
+  /// getStore - Helper function to build ISD::STORE nodes.
+  ///
+  SDOperand getStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
+                     const Value *SV, int SVOffset, bool isVolatile=false,
+                     unsigned Alignment=0);
+  SDOperand getTruncStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
+                          const Value *SV, int SVOffset, MVT::ValueType TVT,
+                          bool isVolatile=false, unsigned Alignment=0);
+  SDOperand getIndexedStore(SDOperand OrigStoe, SDOperand Base,
+                           SDOperand Offset, ISD::MemIndexedMode AM);
 
   // getSrcValue - construct a node to track a Value* through the backend
   SDOperand getSrcValue(const Value* I, int offset = 0);
@@ -311,34 +364,14 @@ public:
                        SDOperand Op1, SDOperand Op2);
   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
                        SDOperand Op1, SDOperand Op2, SDOperand Op3);
-  SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
-                       SDOperand Op1, SDOperand Op2, SDOperand Op3, 
-                       SDOperand Op4);
-  SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
-                       SDOperand Op1, SDOperand Op2, SDOperand Op3,
-                       SDOperand Op4, SDOperand Op5);
-  SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
-                       SDOperand Op1, SDOperand Op2, SDOperand Op3, 
-                       SDOperand Op4, SDOperand Op5, SDOperand Op6);
-  SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
-                       SDOperand Op1, SDOperand Op2, SDOperand Op3,
-                       SDOperand Op4, SDOperand Op5, SDOperand Op6,
-                       SDOperand Op7);
-  SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
-                       SDOperand Op1, SDOperand Op2, SDOperand Op3,
-                       SDOperand Op4, SDOperand Op5, SDOperand Op6,
-                       SDOperand Op7, SDOperand Op8);
+  SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
+                        const SDOperand *Ops, unsigned NumOps);
   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 
                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
                        SDOperand Op3);
-  SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
-                       MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
-                       SDOperand Op3, SDOperand Op4);
-  SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
-                       MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
-                       SDOperand Op3, SDOperand Op4, SDOperand Op5);
+
 
   /// getTargetNode - These are used for target selectors to create a new node
   /// with specified return type(s), target opcode, and operands.
@@ -353,23 +386,6 @@ public:
                         SDOperand Op1, SDOperand Op2);
   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
                         SDOperand Op1, SDOperand Op2, SDOperand Op3);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
-                        SDOperand Op1, SDOperand Op2, SDOperand Op3,
-                        SDOperand Op4);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
-                        SDOperand Op1, SDOperand Op2, SDOperand Op3,
-                        SDOperand Op4, SDOperand Op5);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
-                        SDOperand Op1, SDOperand Op2, SDOperand Op3,
-                        SDOperand Op4, SDOperand Op5, SDOperand Op6);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
-                        SDOperand Op1, SDOperand Op2, SDOperand Op3,
-                        SDOperand Op4, SDOperand Op5, SDOperand Op6,
-                        SDOperand Op7);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
-                        SDOperand Op1, SDOperand Op2, SDOperand Op3,
-                        SDOperand Op4, SDOperand Op5, SDOperand Op6,
-                        SDOperand Op7, SDOperand Op8);
   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
                         const SDOperand *Ops, unsigned NumOps);
   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
@@ -379,39 +395,17 @@ public:
   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
                         MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
                         SDOperand Op3);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
-                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
-                        SDOperand Op3, SDOperand Op4);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
-                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
-                        SDOperand Op3, SDOperand Op4, SDOperand Op5);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
-                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
-                        SDOperand Op3, SDOperand Op4, SDOperand Op5,
-                        SDOperand Op6);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
-                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
-                        SDOperand Op3, SDOperand Op4, SDOperand Op5,
-                        SDOperand Op6, SDOperand Op7);
+  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
+                        MVT::ValueType VT2,
+                        const SDOperand *Ops, unsigned NumOps);
   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
                         MVT::ValueType VT2, MVT::ValueType VT3,
                         SDOperand Op1, SDOperand Op2);
   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
                         MVT::ValueType VT2, MVT::ValueType VT3,
-                        SDOperand Op1, SDOperand Op2,
-                        SDOperand Op3, SDOperand Op4, SDOperand Op5);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
-                        MVT::ValueType VT2, MVT::ValueType VT3,
-                        SDOperand Op1, SDOperand Op2,
-                        SDOperand Op3, SDOperand Op4, SDOperand Op5,
-                        SDOperand Op6);
-  SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
-                        MVT::ValueType VT2, MVT::ValueType VT3,
-                        SDOperand Op1, SDOperand Op2,
-                        SDOperand Op3, SDOperand Op4, SDOperand Op5,
-                        SDOperand Op6, SDOperand Op7);
+                        SDOperand Op1, SDOperand Op2, SDOperand Op3);
   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
-                        MVT::ValueType VT2,
+                        MVT::ValueType VT2, MVT::ValueType VT3,
                         const SDOperand *Ops, unsigned NumOps);
   
   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
@@ -437,10 +431,6 @@ public:
   void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
                                  std::vector<SDNode*> &Deleted);
 
-  /// DeleteNode - Remove the specified node from the system.  This node must
-  /// have no referrers.
-  void DeleteNode(SDNode *N);
-
   /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
   /// their allnodes order. It returns the maximum id.
   unsigned AssignNodeIds();
@@ -450,8 +440,31 @@ public:
   /// of the SDNodes* in assigned order by reference.
   unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
 
+  /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
+  /// operation.
+  static bool isCommutativeBinOp(unsigned Opcode) {
+    switch (Opcode) {
+    case ISD::ADD:
+    case ISD::MUL:
+    case ISD::MULHU:
+    case ISD::MULHS:
+    case ISD::FADD:
+    case ISD::FMUL:
+    case ISD::AND:
+    case ISD::OR:
+    case ISD::XOR:
+    case ISD::ADDC: 
+    case ISD::ADDE: return true;
+    default: return false;
+    }
+  }
+
   void dump() const;
 
+  /// FoldSetCC - Constant fold a setcc to true or false.
+  SDOperand FoldSetCC(MVT::ValueType VT, SDOperand N1,
+                      SDOperand N2, ISD::CondCode Cond);
+  
 private:
   void RemoveNodeFromCSEMaps(SDNode *N);
   SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
@@ -463,11 +476,6 @@ private:
 
   void DeleteNodeNotInCSEMaps(SDNode *N);
   
-  /// SimplifySetCC - Try to simplify a setcc built with the specified operands 
-  /// and cc.  If unable to simplify it, return a null SDOperand.
-  SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
-                          SDOperand N2, ISD::CondCode Cond);
-  
   // List of non-single value types.
   std::list<std::vector<MVT::ValueType> > VTList;