Because I like being able to instantiate the cfgprinter from external projects,
[oota-llvm.git] / include / llvm / Analysis / InstForest.h
index 967ed45ec967022374778e3c89980051a3c4a4b1..e41bf8c3a9325d5c013a57100a096b4a35a9aa54 100644 (file)
@@ -1,4 +1,11 @@
-//===- llvm/Analysis/InstForest.h - Partition Method into forest -*- C++ -*--=//
+//===- llvm/Analysis/InstForest.h - Partition Func into forest --*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This interface is used to partition a method into a forest of instruction
 // trees, where the following invariants hold:
 #ifndef LLVM_ANALYSIS_INSTFOREST_H
 #define LLVM_ANALYSIS_INSTFOREST_H
 
-#include "llvm/Support/Tree.h"
-#include "llvm/Instruction.h"
+#include "llvm/Function.h"
+#include "Support/Tree.h"
 #include <map>
 
-namespace analysis {
+namespace llvm {
 
 template<class Payload> class InstTreeNode;
 template<class Payload> class InstForest;
 
-
 //===----------------------------------------------------------------------===//
 //  Class InstTreeNode
 //===----------------------------------------------------------------------===//
@@ -35,10 +41,12 @@ template<class Payload> class InstForest;
 //
 template<class Payload>
 class InstTreeNode : 
-    public Tree<InstTreeNode<Payload>, pair<pair<Value*, char>, Payload> > {
+    public Tree<InstTreeNode<Payload>, 
+                std::pair<std::pair<Value*, char>, Payload> > {
 
   friend class InstForest<Payload>;
-  typedef Tree<InstTreeNode<Payload>, pair<pair<Value*, char>, Payload> > super;
+  typedef Tree<InstTreeNode<Payload>,
+               std::pair<std::pair<Value*, char>, Payload> > super;
 
   // Constants used for the node type value
   enum NodeTypeTy {
@@ -49,10 +57,10 @@ class InstTreeNode :
   };
 
   // Helper functions to make accessing our data nicer...
-  const Value *getValue() const { return getTreeData().first.first; }
-        Value *getValue()       { return getTreeData().first.first; }
+  const Value *getValue() const { return this->getTreeData().first.first; }
+        Value *getValue()       { return this->getTreeData().first.first; }
   enum NodeTypeTy getNodeType() const {
-    return (enum NodeTypeTy)getTreeData().first.second;
+    return (enum NodeTypeTy)this->getTreeData().first.second;
   }
 
   InstTreeNode(const InstTreeNode &);     // Do not implement
@@ -63,8 +71,8 @@ class InstTreeNode :
   bool CanMergeInstIntoTree(Instruction *Inst);
 public:
   // Accessor functions...
-  inline       Payload &getData()       { return getTreeData().second; }
-  inline const Payload &getData() const { return getTreeData().second; }
+  inline       Payload &getData()       { return this->getTreeData().second; }
+  inline const Payload &getData() const { return this->getTreeData().second; }
 
   // Type checking functions...
   inline bool isConstant()    const { return getNodeType() == ConstNode; }
@@ -73,17 +81,17 @@ public:
   inline bool isTemporary()   const { return getNodeType() == TemporaryNode; }
 
   // Accessors for different node types...
-  inline ConstPoolVal *getConstant() {
-    return cast<ConstPoolVal>(getValue());
+  inline Constant *getConstant() {
+    return cast<Constant>(getValue());
   }
-  inline const ConstPoolVal *getConstant() const {
-    return cast<const ConstPoolVal>(getValue());
+  inline const Constant *getConstant() const {
+    return cast<Constant>(getValue());
   }
   inline BasicBlock *getBasicBlock() {
     return cast<BasicBlock>(getValue());
   }
   inline const BasicBlock *getBasicBlock() const {
-    return cast<const BasicBlock>(getValue());
+    return cast<BasicBlock>(getValue());
   }
   inline Instruction *getInstruction() {
     assert(isInstruction() && "getInstruction() on non instruction node!");
@@ -104,27 +112,28 @@ public:
 
 public:
   // print - Called by operator<< below...
-  void print(ostream &o, unsigned Indent) const {
-    o << string(Indent*2, ' ');
+  void print(std::ostream &o, unsigned Indent) const {
+    o << std::string(Indent*2, ' ');
     switch (getNodeType()) {
     case ConstNode      : o << "Constant   : "; break;
-    case BasicBlockNode : o << "BasicBlock : " << getValue()->getName() << endl;
+    case BasicBlockNode : o << "BasicBlock : " << getValue()->getName() << "\n";
       return;
     case InstructionNode: o << "Instruction: "; break;
     case TemporaryNode  : o << "Temporary  : "; break;
-    default: o << "UNKNOWN NODE TYPE: " << getNodeType() << endl; abort();
+    default: o << "UNKNOWN NODE TYPE: " << getNodeType() << "\n"; abort();
     }
 
     o << getValue();
     if (!isa<Instruction>(getValue())) o << "\n";
 
-    for (unsigned i = 0; i < getNumChildren(); ++i)
-      getChild(i)->print(o, Indent+1);
+    for (unsigned i = 0; i < this->getNumChildren(); ++i)
+      this->getChild(i)->print(o, Indent+1);
   }
 };
 
 template<class Payload>
-inline ostream &operator<<(ostream &o, const InstTreeNode<Payload> *N) {
+inline std::ostream &operator<<(std::ostream &o,
+                                const InstTreeNode<Payload> *N) {
   N->print(o, 0); return o;
 }
 
@@ -137,42 +146,44 @@ inline ostream &operator<<(ostream &o, const InstTreeNode<Payload> *N) {
 // guaranteed to be an instruction node.  The constructor builds the forest.
 //
 template<class Payload>
-class InstForest : public vector<InstTreeNode<Payload> *> {
+class InstForest : public std::vector<InstTreeNode<Payload> *> {
   friend class InstTreeNode<Payload>;
 
+  typedef typename std::vector<InstTreeNode<Payload> *>::const_iterator const_iterator;
+
   // InstMap - Map contains entries for ALL instructions in the method and the
   // InstTreeNode that they correspond to.
   //
-  map<Instruction*, InstTreeNode<Payload> *> InstMap;
+  std::map<Instruction*, InstTreeNode<Payload> *> InstMap;
 
   void addInstMapping(Instruction *I, InstTreeNode<Payload> *IN) {
-    InstMap.insert(make_pair(I, IN));
+    InstMap.insert(std::make_pair(I, IN));
   }
 
   void removeInstFromRootList(Instruction *I) {
-    for (unsigned i = size(); i > 0; --i)
-      if (operator[](i-1)->getValue() == I) {
-       erase(begin()+i-1);
+    for (unsigned i = this->size(); i > 0; --i)
+      if ((*this)[i-1]->getValue() == I) {
+       this->erase(this->begin()+i-1);
        return;
       }
   }
 
 public:
   // ctor - Create an instruction forest for the specified method...
-  InstForest(Method *M) {
-    for (Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
-        I != E; ++I) {
-      Instruction *Inst = *I;
-      if (!getInstNode(Inst))   // Do we already have a tree for this inst?
-       push_back(new InstTreeNode<Payload>(*this, Inst, 0));  // No create one!
-      // InstTreeNode ctor automatically adds the created node into our InstMap
-    }
+  InstForest(Function *F) {
+    for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
+      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
+        if (!getInstNode(I)) {  // Do we already have a tree for this inst?
+          // No, create one!  InstTreeNode ctor automatically adds the
+          // created node into our InstMap
+          push_back(new InstTreeNode<Payload>(*this, I, 0));
+        }
   }
 
   // dtor - Free the trees...
   ~InstForest() {
-    for (unsigned i = size(); i > 0; --i)
-      delete operator[](i-1);
+    for (unsigned i = this->size(); i != 0; --i)
+      delete (*this)[i-1];
   }
 
   // getInstNode - Return the instruction node that corresponds to the specified
@@ -180,26 +191,27 @@ public:
   // the parent pointer can be used to find the root of the tree.
   //
   inline InstTreeNode<Payload> *getInstNode(Instruction *Inst) {
-    map<Instruction*, InstTreeNode<Payload> *>::iterator I = InstMap.find(Inst);
+    typename std::map<Instruction*, InstTreeNode<Payload> *>::iterator I =
+      InstMap.find(Inst);
     if (I != InstMap.end()) return I->second;
     return 0;
   }
   inline const InstTreeNode<Payload> *getInstNode(const Instruction *Inst)const{
-    map<Instruction*, InstTreeNode<Payload>*>::const_iterator I = 
+    typename std::map<Instruction*, InstTreeNode<Payload>*>::const_iterator I = 
       InstMap.find(Inst);
     if (I != InstMap.end()) return I->second;
     return 0;
   }
 
   // print - Called by operator<< below...
-  void print(ostream &out) const {
-    for (const_iterator I = begin(), E = end(); I != E; ++I)
+  void print(std::ostream &out) const {
+    for (const_iterator I = this->begin(), E = this->end(); I != E; ++I)
       out << *I;
   }
 };
 
 template<class Payload>
-inline ostream &operator<<(ostream &o, const InstForest<Payload> &IF) {
+inline std::ostream &operator<<(std::ostream &o, const InstForest<Payload> &IF){
   IF.print(o); return o;
 }
 
@@ -215,7 +227,7 @@ inline ostream &operator<<(ostream &o, const InstForest<Payload> &IF) {
 //
 template <class Payload>
 bool InstTreeNode<Payload>::CanMergeInstIntoTree(Instruction *I) {
-  if (I->use_size() > 1) return false;
+  if (!I->use_empty() && !I->hasOneUse()) return false;
   return I->getParent() == cast<Instruction>(getValue())->getParent();
 }
 
@@ -227,18 +239,18 @@ bool InstTreeNode<Payload>::CanMergeInstIntoTree(Instruction *I) {
 template <class Payload>
 InstTreeNode<Payload>::InstTreeNode(InstForest<Payload> &IF, Value *V,
                                    InstTreeNode *Parent) : super(Parent) {
-  getTreeData().first.first = V;   // Save tree node
+  this->getTreeData().first.first = V;   // Save tree node
  
   if (!isa<Instruction>(V)) {
-    assert((isa<ConstPoolVal>(V) || isa<BasicBlock>(V) ||
-           isa<MethodArgument>(V) || isa<GlobalVariable>(V)) &&
+    assert((isa<Constant>(V) || isa<BasicBlock>(V) ||
+           isa<Argument>(V) || isa<GlobalValue>(V)) &&
           "Unrecognized value type for InstForest Partition!");
-    if (isa<ConstPoolVal>(V))
-      getTreeData().first.second = ConstNode;
+    if (isa<Constant>(V))
+      this->getTreeData().first.second = ConstNode;
     else if (isa<BasicBlock>(V))
-      getTreeData().first.second = BasicBlockNode;
+      this->getTreeData().first.second = BasicBlockNode;
     else 
-      getTreeData().first.second = TemporaryNode;
+      this->getTreeData().first.second = TemporaryNode;
       
     return;
   }
@@ -247,14 +259,14 @@ InstTreeNode<Payload>::InstTreeNode(InstForest<Payload> &IF, Value *V,
   Instruction *I = cast<Instruction>(V);
   if (Parent && !Parent->CanMergeInstIntoTree(I)) {
     // Not root node of tree, but mult uses?
-    getTreeData().first.second = TemporaryNode;   // Must be a temporary!
+    this->getTreeData().first.second = TemporaryNode;   // Must be a temporary!
     return;
   }
 
   // Otherwise, we are an internal instruction node.  We must process our
   // uses and add them as children of this node.
   //
-  vector<InstTreeNode*> Children;
+  std::vector<InstTreeNode*> Children;
 
   // Make sure that the forest knows about us!
   IF.addInstMapping(I, this);
@@ -275,11 +287,10 @@ InstTreeNode<Payload>::InstTreeNode(InstForest<Payload> &IF, Value *V,
   }
 
   setChildren(Children);
-  getTreeData().first.second = InstructionNode;
+  this->getTreeData().first.second = InstructionNode;
 }
 
-}  // End namespace analysis
-
+} // End llvm namespace
 
 #endif