1 //===- llvm/Analysis/InstForest.h - Partition Method into forest -*- C++ -*--=//
3 // This interface is used to partition a method into a forest of instruction
4 // trees, where the following invariants hold:
6 // 1. The instructions in a tree are all related to each other through use
8 // 2. All instructions in a tree are members of the same basic block
9 // 3. All instructions in a tree (with the exception of the root), may have only
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_INSTFOREST_H
15 #define LLVM_ANALYSIS_INSTFOREST_H
17 #include "llvm/Instruction.h"
18 #include "Support/Tree.h"
23 template<class Payload> class InstTreeNode;
24 template<class Payload> class InstForest;
27 //===----------------------------------------------------------------------===//
29 //===----------------------------------------------------------------------===//
31 // There is an instance of this class for each node in the instruction forest.
32 // There should be a node for every instruction in the tree, as well as
33 // Temporary nodes that correspond to other trees in the forest and to variables
34 // and global variables. Constants have their own special node.
36 template<class Payload>
38 public Tree<InstTreeNode<Payload>,
39 std::pair<std::pair<Value*, char>, Payload> > {
41 friend class InstForest<Payload>;
42 typedef Tree<InstTreeNode<Payload>,
43 std::pair<std::pair<Value*, char>, Payload> > super;
45 // Constants used for the node type value
47 ConstNode = Value::ConstantVal,
48 BasicBlockNode = Value::BasicBlockVal,
49 InstructionNode = Value::InstructionVal,
53 // Helper functions to make accessing our data nicer...
54 const Value *getValue() const { return getTreeData().first.first; }
55 Value *getValue() { return getTreeData().first.first; }
56 enum NodeTypeTy getNodeType() const {
57 return (enum NodeTypeTy)getTreeData().first.second;
60 InstTreeNode(const InstTreeNode &); // Do not implement
61 void operator=(const InstTreeNode &); // Do not implement
63 // Only creatable by InstForest
64 InstTreeNode(InstForest<Payload> &IF, Value *V, InstTreeNode *Parent);
65 bool CanMergeInstIntoTree(Instruction *Inst);
67 // Accessor functions...
68 inline Payload &getData() { return getTreeData().second; }
69 inline const Payload &getData() const { return getTreeData().second; }
71 // Type checking functions...
72 inline bool isConstant() const { return getNodeType() == ConstNode; }
73 inline bool isBasicBlock() const { return getNodeType() == BasicBlockNode; }
74 inline bool isInstruction() const { return getNodeType() == InstructionNode; }
75 inline bool isTemporary() const { return getNodeType() == TemporaryNode; }
77 // Accessors for different node types...
78 inline Constant *getConstant() {
79 return cast<Constant>(getValue());
81 inline const Constant *getConstant() const {
82 return cast<const Constant>(getValue());
84 inline BasicBlock *getBasicBlock() {
85 return cast<BasicBlock>(getValue());
87 inline const BasicBlock *getBasicBlock() const {
88 return cast<const BasicBlock>(getValue());
90 inline Instruction *getInstruction() {
91 assert(isInstruction() && "getInstruction() on non instruction node!");
92 return cast<Instruction>(getValue());
94 inline const Instruction *getInstruction() const {
95 assert(isInstruction() && "getInstruction() on non instruction node!");
96 return cast<Instruction>(getValue());
98 inline Instruction *getTemporary() {
99 assert(isTemporary() && "getTemporary() on non temporary node!");
100 return cast<Instruction>(getValue());
102 inline const Instruction *getTemporary() const {
103 assert(isTemporary() && "getTemporary() on non temporary node!");
104 return cast<Instruction>(getValue());
108 // print - Called by operator<< below...
109 void print(std::ostream &o, unsigned Indent) const {
110 o << std::string(Indent*2, ' ');
111 switch (getNodeType()) {
112 case ConstNode : o << "Constant : "; break;
113 case BasicBlockNode : o << "BasicBlock : " << getValue()->getName() << "\n";
115 case InstructionNode: o << "Instruction: "; break;
116 case TemporaryNode : o << "Temporary : "; break;
117 default: o << "UNKNOWN NODE TYPE: " << getNodeType() << "\n"; abort();
121 if (!isa<Instruction>(getValue())) o << "\n";
123 for (unsigned i = 0; i < getNumChildren(); ++i)
124 getChild(i)->print(o, Indent+1);
128 template<class Payload>
129 inline std::ostream &operator<<(std::ostream &o,
130 const InstTreeNode<Payload> *N) {
131 N->print(o, 0); return o;
134 //===----------------------------------------------------------------------===//
136 //===----------------------------------------------------------------------===//
138 // This class represents the instruction forest itself. It exposes iterators
139 // to an underlying vector of Instruction Trees. Each root of the tree is
140 // guaranteed to be an instruction node. The constructor builds the forest.
142 template<class Payload>
143 class InstForest : public std::vector<InstTreeNode<Payload> *> {
144 friend class InstTreeNode<Payload>;
146 // InstMap - Map contains entries for ALL instructions in the method and the
147 // InstTreeNode that they correspond to.
149 std::map<Instruction*, InstTreeNode<Payload> *> InstMap;
151 void addInstMapping(Instruction *I, InstTreeNode<Payload> *IN) {
152 InstMap.insert(std::make_pair(I, IN));
155 void removeInstFromRootList(Instruction *I) {
156 for (unsigned i = size(); i > 0; --i)
157 if (operator[](i-1)->getValue() == I) {
164 // ctor - Create an instruction forest for the specified method...
165 InstForest(Method *M) {
166 for (Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
168 Instruction *Inst = *I;
169 if (!getInstNode(Inst)) // Do we already have a tree for this inst?
170 push_back(new InstTreeNode<Payload>(*this, Inst, 0)); // No create one!
171 // InstTreeNode ctor automatically adds the created node into our InstMap
175 // dtor - Free the trees...
177 for (unsigned i = size(); i > 0; --i)
178 delete operator[](i-1);
181 // getInstNode - Return the instruction node that corresponds to the specified
182 // instruction... This node may be embeded in a larger tree, in which case
183 // the parent pointer can be used to find the root of the tree.
185 inline InstTreeNode<Payload> *getInstNode(Instruction *Inst) {
186 std::map<Instruction*, InstTreeNode<Payload> *>::iterator I =
188 if (I != InstMap.end()) return I->second;
191 inline const InstTreeNode<Payload> *getInstNode(const Instruction *Inst)const{
192 std::map<Instruction*, InstTreeNode<Payload>*>::const_iterator I =
194 if (I != InstMap.end()) return I->second;
198 // print - Called by operator<< below...
199 void print(std::ostream &out) const {
200 for (const_iterator I = begin(), E = end(); I != E; ++I)
205 template<class Payload>
206 inline std::ostream &operator<<(std::ostream &o, const InstForest<Payload> &IF){
207 IF.print(o); return o;
211 //===----------------------------------------------------------------------===//
212 // Method Implementations
213 //===----------------------------------------------------------------------===//
215 // CanMergeInstIntoTree - Return true if it is allowed to merge the specified
216 // instruction into 'this' instruction tree. This is allowed iff:
217 // 1. The instruction is in the same basic block as the current one
218 // 2. The instruction has only one use
220 template <class Payload>
221 bool InstTreeNode<Payload>::CanMergeInstIntoTree(Instruction *I) {
222 if (I->use_size() > 1) return false;
223 return I->getParent() == cast<Instruction>(getValue())->getParent();
227 // InstTreeNode ctor - This constructor creates the instruction tree for the
228 // specified value. If the value is an instruction, it recursively creates the
229 // internal/child nodes and adds them to the instruction forest.
231 template <class Payload>
232 InstTreeNode<Payload>::InstTreeNode(InstForest<Payload> &IF, Value *V,
233 InstTreeNode *Parent) : super(Parent) {
234 getTreeData().first.first = V; // Save tree node
236 if (!isa<Instruction>(V)) {
237 assert((isa<Constant>(V) || isa<BasicBlock>(V) ||
238 isa<MethodArgument>(V) || isa<GlobalValue>(V)) &&
239 "Unrecognized value type for InstForest Partition!");
240 if (isa<Constant>(V))
241 getTreeData().first.second = ConstNode;
242 else if (isa<BasicBlock>(V))
243 getTreeData().first.second = BasicBlockNode;
245 getTreeData().first.second = TemporaryNode;
250 // Must be an instruction then... see if we can include it in this tree!
251 Instruction *I = cast<Instruction>(V);
252 if (Parent && !Parent->CanMergeInstIntoTree(I)) {
253 // Not root node of tree, but mult uses?
254 getTreeData().first.second = TemporaryNode; // Must be a temporary!
258 // Otherwise, we are an internal instruction node. We must process our
259 // uses and add them as children of this node.
261 std::vector<InstTreeNode*> Children;
263 // Make sure that the forest knows about us!
264 IF.addInstMapping(I, this);
266 // Walk the operands of the instruction adding children for all of the uses
267 // of the instruction...
269 for (Instruction::op_iterator OI = I->op_begin(); OI != I->op_end(); ++OI) {
270 Value *Operand = *OI;
271 InstTreeNode<Payload> *IN = IF.getInstNode(dyn_cast<Instruction>(Operand));
272 if (IN && CanMergeInstIntoTree(cast<Instruction>(Operand))) {
273 Children.push_back(IN);
274 IF.removeInstFromRootList(cast<Instruction>(Operand));
276 // No node for this child yet... create one now!
277 Children.push_back(new InstTreeNode(IF, *OI, this));
281 setChildren(Children);
282 getTreeData().first.second = InstructionNode;
285 } // End namespace analysis