2002-03-26 17:49:55 +00:00
|
|
|
//===- llvm/Analysis/InstForest.h - Partition Func into forest ---*- C++ -*--=//
|
2001-09-14 01:42:26 +00:00
|
|
|
//
|
|
|
|
// This interface is used to partition a method into a forest of instruction
|
|
|
|
// trees, where the following invariants hold:
|
|
|
|
//
|
|
|
|
// 1. The instructions in a tree are all related to each other through use
|
|
|
|
// relationships.
|
|
|
|
// 2. All instructions in a tree are members of the same basic block
|
|
|
|
// 3. All instructions in a tree (with the exception of the root), may have only
|
|
|
|
// a single user.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ANALYSIS_INSTFOREST_H
|
|
|
|
#define LLVM_ANALYSIS_INSTFOREST_H
|
|
|
|
|
|
|
|
#include "llvm/Instruction.h"
|
2002-02-12 21:04:35 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
2002-04-29 18:12:52 +00:00
|
|
|
#include "llvm/Function.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/Tree.h"
|
2001-09-14 01:42:26 +00:00
|
|
|
#include <map>
|
|
|
|
|
2001-10-01 18:26:53 +00:00
|
|
|
template<class Payload> class InstTreeNode;
|
|
|
|
template<class Payload> class InstForest;
|
2001-09-14 01:42:26 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Class InstTreeNode
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// There is an instance of this class for each node in the instruction forest.
|
|
|
|
// There should be a node for every instruction in the tree, as well as
|
|
|
|
// Temporary nodes that correspond to other trees in the forest and to variables
|
|
|
|
// and global variables. Constants have their own special node.
|
|
|
|
//
|
|
|
|
template<class Payload>
|
|
|
|
class InstTreeNode :
|
2002-01-20 22:54:45 +00:00
|
|
|
public Tree<InstTreeNode<Payload>,
|
|
|
|
std::pair<std::pair<Value*, char>, Payload> > {
|
2001-09-14 01:42:26 +00:00
|
|
|
|
|
|
|
friend class InstForest<Payload>;
|
2002-01-20 22:54:45 +00:00
|
|
|
typedef Tree<InstTreeNode<Payload>,
|
|
|
|
std::pair<std::pair<Value*, char>, Payload> > super;
|
2001-09-14 01:42:26 +00:00
|
|
|
|
|
|
|
// Constants used for the node type value
|
|
|
|
enum NodeTypeTy {
|
|
|
|
ConstNode = Value::ConstantVal,
|
|
|
|
BasicBlockNode = Value::BasicBlockVal,
|
|
|
|
InstructionNode = Value::InstructionVal,
|
|
|
|
TemporaryNode = -1
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper functions to make accessing our data nicer...
|
|
|
|
const Value *getValue() const { return getTreeData().first.first; }
|
|
|
|
Value *getValue() { return getTreeData().first.first; }
|
|
|
|
enum NodeTypeTy getNodeType() const {
|
|
|
|
return (enum NodeTypeTy)getTreeData().first.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstTreeNode(const InstTreeNode &); // Do not implement
|
|
|
|
void operator=(const InstTreeNode &); // Do not implement
|
|
|
|
|
|
|
|
// Only creatable by InstForest
|
|
|
|
InstTreeNode(InstForest<Payload> &IF, Value *V, InstTreeNode *Parent);
|
|
|
|
bool CanMergeInstIntoTree(Instruction *Inst);
|
|
|
|
public:
|
|
|
|
// Accessor functions...
|
|
|
|
inline Payload &getData() { return getTreeData().second; }
|
|
|
|
inline const Payload &getData() const { return getTreeData().second; }
|
|
|
|
|
|
|
|
// Type checking functions...
|
|
|
|
inline bool isConstant() const { return getNodeType() == ConstNode; }
|
|
|
|
inline bool isBasicBlock() const { return getNodeType() == BasicBlockNode; }
|
|
|
|
inline bool isInstruction() const { return getNodeType() == InstructionNode; }
|
|
|
|
inline bool isTemporary() const { return getNodeType() == TemporaryNode; }
|
|
|
|
|
|
|
|
// Accessors for different node types...
|
2001-12-03 22:26:30 +00:00
|
|
|
inline Constant *getConstant() {
|
|
|
|
return cast<Constant>(getValue());
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
inline const Constant *getConstant() const {
|
|
|
|
return cast<const Constant>(getValue());
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
inline BasicBlock *getBasicBlock() {
|
2001-10-01 16:18:37 +00:00
|
|
|
return cast<BasicBlock>(getValue());
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
inline const BasicBlock *getBasicBlock() const {
|
2001-10-01 16:18:37 +00:00
|
|
|
return cast<const BasicBlock>(getValue());
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
inline Instruction *getInstruction() {
|
|
|
|
assert(isInstruction() && "getInstruction() on non instruction node!");
|
2001-10-01 16:18:37 +00:00
|
|
|
return cast<Instruction>(getValue());
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
inline const Instruction *getInstruction() const {
|
|
|
|
assert(isInstruction() && "getInstruction() on non instruction node!");
|
2001-10-01 16:18:37 +00:00
|
|
|
return cast<Instruction>(getValue());
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
inline Instruction *getTemporary() {
|
|
|
|
assert(isTemporary() && "getTemporary() on non temporary node!");
|
2001-10-01 16:18:37 +00:00
|
|
|
return cast<Instruction>(getValue());
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
inline const Instruction *getTemporary() const {
|
|
|
|
assert(isTemporary() && "getTemporary() on non temporary node!");
|
2001-10-01 16:18:37 +00:00
|
|
|
return cast<Instruction>(getValue());
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
// print - Called by operator<< below...
|
2002-01-20 22:54:45 +00:00
|
|
|
void print(std::ostream &o, unsigned Indent) const {
|
|
|
|
o << std::string(Indent*2, ' ');
|
2001-09-14 01:42:26 +00:00
|
|
|
switch (getNodeType()) {
|
|
|
|
case ConstNode : o << "Constant : "; break;
|
2002-01-20 22:54:45 +00:00
|
|
|
case BasicBlockNode : o << "BasicBlock : " << getValue()->getName() << "\n";
|
2001-09-14 01:42:26 +00:00
|
|
|
return;
|
|
|
|
case InstructionNode: o << "Instruction: "; break;
|
|
|
|
case TemporaryNode : o << "Temporary : "; break;
|
2002-01-20 22:54:45 +00:00
|
|
|
default: o << "UNKNOWN NODE TYPE: " << getNodeType() << "\n"; abort();
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o << getValue();
|
2001-10-01 20:11:19 +00:00
|
|
|
if (!isa<Instruction>(getValue())) o << "\n";
|
2001-09-14 01:42:26 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < getNumChildren(); ++i)
|
|
|
|
getChild(i)->print(o, Indent+1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Payload>
|
2002-01-20 22:54:45 +00:00
|
|
|
inline std::ostream &operator<<(std::ostream &o,
|
|
|
|
const InstTreeNode<Payload> *N) {
|
2001-09-14 01:42:26 +00:00
|
|
|
N->print(o, 0); return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Class InstForest
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class represents the instruction forest itself. It exposes iterators
|
|
|
|
// to an underlying vector of Instruction Trees. Each root of the tree is
|
|
|
|
// guaranteed to be an instruction node. The constructor builds the forest.
|
|
|
|
//
|
|
|
|
template<class Payload>
|
2002-01-20 22:54:45 +00:00
|
|
|
class InstForest : public std::vector<InstTreeNode<Payload> *> {
|
2001-09-14 01:42:26 +00:00
|
|
|
friend class InstTreeNode<Payload>;
|
|
|
|
|
2002-07-25 17:58:58 +00:00
|
|
|
typedef std::vector<InstTreeNode<Payload> *>::const_iterator const_iterator;
|
|
|
|
|
2001-09-14 01:42:26 +00:00
|
|
|
// InstMap - Map contains entries for ALL instructions in the method and the
|
|
|
|
// InstTreeNode that they correspond to.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::map<Instruction*, InstTreeNode<Payload> *> InstMap;
|
2001-09-14 01:42:26 +00:00
|
|
|
|
|
|
|
void addInstMapping(Instruction *I, InstTreeNode<Payload> *IN) {
|
2002-01-20 22:54:45 +00:00
|
|
|
InstMap.insert(std::make_pair(I, IN));
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void removeInstFromRootList(Instruction *I) {
|
|
|
|
for (unsigned i = size(); i > 0; --i)
|
|
|
|
if (operator[](i-1)->getValue() == I) {
|
|
|
|
erase(begin()+i-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
// ctor - Create an instruction forest for the specified method...
|
2002-03-26 17:49:55 +00:00
|
|
|
InstForest(Function *F) {
|
2002-06-25 16:13:24 +00:00
|
|
|
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?
|
2002-02-12 21:04:35 +00:00
|
|
|
// No, create one! InstTreeNode ctor automatically adds the
|
|
|
|
// created node into our InstMap
|
2002-06-25 16:13:24 +00:00
|
|
|
push_back(new InstTreeNode<Payload>(*this, I, 0));
|
2002-02-12 21:04:35 +00:00
|
|
|
}
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dtor - Free the trees...
|
|
|
|
~InstForest() {
|
2002-06-25 16:13:24 +00:00
|
|
|
for (unsigned i = size(); i != 0; --i)
|
2001-09-14 01:42:26 +00:00
|
|
|
delete operator[](i-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// getInstNode - Return the instruction node that corresponds to the specified
|
|
|
|
// instruction... This node may be embeded in a larger tree, in which case
|
|
|
|
// the parent pointer can be used to find the root of the tree.
|
|
|
|
//
|
|
|
|
inline InstTreeNode<Payload> *getInstNode(Instruction *Inst) {
|
2002-07-25 17:55:37 +00:00
|
|
|
typename std::map<Instruction*, InstTreeNode<Payload> *>::iterator I =
|
2002-01-20 22:54:45 +00:00
|
|
|
InstMap.find(Inst);
|
2001-09-14 01:42:26 +00:00
|
|
|
if (I != InstMap.end()) return I->second;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
inline const InstTreeNode<Payload> *getInstNode(const Instruction *Inst)const{
|
2002-07-25 17:55:37 +00:00
|
|
|
typename std::map<Instruction*, InstTreeNode<Payload>*>::const_iterator I =
|
2001-09-14 01:42:26 +00:00
|
|
|
InstMap.find(Inst);
|
|
|
|
if (I != InstMap.end()) return I->second;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// print - Called by operator<< below...
|
2002-01-20 22:54:45 +00:00
|
|
|
void print(std::ostream &out) const {
|
2002-07-25 17:58:58 +00:00
|
|
|
for (const_iterator I = begin(), E = end(); I != E; ++I)
|
2001-09-14 01:42:26 +00:00
|
|
|
out << *I;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Payload>
|
2002-01-20 22:54:45 +00:00
|
|
|
inline std::ostream &operator<<(std::ostream &o, const InstForest<Payload> &IF){
|
2001-09-14 01:42:26 +00:00
|
|
|
IF.print(o); return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Method Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// CanMergeInstIntoTree - Return true if it is allowed to merge the specified
|
|
|
|
// instruction into 'this' instruction tree. This is allowed iff:
|
|
|
|
// 1. The instruction is in the same basic block as the current one
|
|
|
|
// 2. The instruction has only one use
|
|
|
|
//
|
|
|
|
template <class Payload>
|
|
|
|
bool InstTreeNode<Payload>::CanMergeInstIntoTree(Instruction *I) {
|
|
|
|
if (I->use_size() > 1) return false;
|
2001-10-01 16:18:37 +00:00
|
|
|
return I->getParent() == cast<Instruction>(getValue())->getParent();
|
2001-09-14 01:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// InstTreeNode ctor - This constructor creates the instruction tree for the
|
|
|
|
// specified value. If the value is an instruction, it recursively creates the
|
|
|
|
// internal/child nodes and adds them to the instruction forest.
|
|
|
|
//
|
|
|
|
template <class Payload>
|
|
|
|
InstTreeNode<Payload>::InstTreeNode(InstForest<Payload> &IF, Value *V,
|
|
|
|
InstTreeNode *Parent) : super(Parent) {
|
|
|
|
getTreeData().first.first = V; // Save tree node
|
|
|
|
|
2001-10-01 20:11:19 +00:00
|
|
|
if (!isa<Instruction>(V)) {
|
2001-12-03 22:26:30 +00:00
|
|
|
assert((isa<Constant>(V) || isa<BasicBlock>(V) ||
|
2002-04-09 19:59:31 +00:00
|
|
|
isa<Argument>(V) || isa<GlobalValue>(V)) &&
|
2001-09-14 01:42:26 +00:00
|
|
|
"Unrecognized value type for InstForest Partition!");
|
2001-12-03 22:26:30 +00:00
|
|
|
if (isa<Constant>(V))
|
2001-09-14 01:42:26 +00:00
|
|
|
getTreeData().first.second = ConstNode;
|
2001-10-01 18:26:53 +00:00
|
|
|
else if (isa<BasicBlock>(V))
|
2001-09-14 01:42:26 +00:00
|
|
|
getTreeData().first.second = BasicBlockNode;
|
|
|
|
else
|
|
|
|
getTreeData().first.second = TemporaryNode;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be an instruction then... see if we can include it in this tree!
|
2001-10-01 16:18:37 +00:00
|
|
|
Instruction *I = cast<Instruction>(V);
|
2001-09-14 01:42:26 +00:00
|
|
|
if (Parent && !Parent->CanMergeInstIntoTree(I)) {
|
|
|
|
// Not root node of tree, but mult uses?
|
|
|
|
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.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<InstTreeNode*> Children;
|
2001-09-14 01:42:26 +00:00
|
|
|
|
|
|
|
// Make sure that the forest knows about us!
|
|
|
|
IF.addInstMapping(I, this);
|
|
|
|
|
|
|
|
// Walk the operands of the instruction adding children for all of the uses
|
|
|
|
// of the instruction...
|
|
|
|
//
|
|
|
|
for (Instruction::op_iterator OI = I->op_begin(); OI != I->op_end(); ++OI) {
|
|
|
|
Value *Operand = *OI;
|
2001-10-01 16:18:37 +00:00
|
|
|
InstTreeNode<Payload> *IN = IF.getInstNode(dyn_cast<Instruction>(Operand));
|
|
|
|
if (IN && CanMergeInstIntoTree(cast<Instruction>(Operand))) {
|
2001-09-14 01:42:26 +00:00
|
|
|
Children.push_back(IN);
|
2001-10-01 16:18:37 +00:00
|
|
|
IF.removeInstFromRootList(cast<Instruction>(Operand));
|
2001-09-14 01:42:26 +00:00
|
|
|
} else {
|
|
|
|
// No node for this child yet... create one now!
|
|
|
|
Children.push_back(new InstTreeNode(IF, *OI, this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setChildren(Children);
|
|
|
|
getTreeData().first.second = InstructionNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|