2 //---------------------------------------------------------------------------
7 // Convert SSA graph to instruction trees for instruction selection.
10 // The key goal is to group instructions into a single
11 // tree if one or more of them might be potentially combined into a single
12 // complex instruction in the target machine.
13 // Since this grouping is completely machine-independent, we do it as
14 // aggressive as possible to exploit any possible taret instructions.
15 // In particular, we group two instructions O and I if:
16 // (1) Instruction O computes an operand used by instruction I,
17 // and (2) O and I are part of the same basic block,
18 // and (3) O has only a single use, viz., I.
21 // 6/28/01 - Vikram Adve - Created
23 //---------------------------------------------------------------------------
25 #include "llvm/CodeGen/InstrForest.h"
26 #include "llvm/Method.h"
27 #include "llvm/iTerminators.h"
28 #include "llvm/iMemory.h"
29 #include "llvm/iOther.h"
30 #include "llvm/ConstPoolVals.h"
31 #include "llvm/BasicBlock.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "llvm/Support/STLExtras.h"
35 //------------------------------------------------------------------------
36 // class InstrTreeNode
37 //------------------------------------------------------------------------
40 InstrTreeNode::dump(int dumpChildren, int indent) const
47 LeftChild->dump(dumpChildren, indent+1);
49 RightChild->dump(dumpChildren, indent+1);
54 InstructionNode::InstructionNode(Instruction* I)
55 : InstrTreeNode(NTInstructionNode, I)
57 opLabel = I->getOpcode();
59 // Distinguish special cases of some instructions such as Ret and Br
61 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
63 opLabel = RetValueOp; // ret(value) operation
65 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
67 opLabel = BrCondOp; // br(cond) operation
69 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
71 opLabel = SetCCOp; // common label for all SetCC ops
73 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
75 opLabel = AllocaN; // Alloca(ptr, N) operation
77 else if ((opLabel == Instruction::Load ||
78 opLabel == Instruction::GetElementPtr) &&
79 ((MemAccessInst*)I)->getFirstOffsetIdx() > 0)
81 opLabel = opLabel + 100; // load/getElem with index vector
83 else if (opLabel == Instruction::Cast)
85 const Type *ITy = I->getType();
86 switch(ITy->getPrimitiveID())
88 case Type::BoolTyID: opLabel = ToBoolTy; break;
89 case Type::UByteTyID: opLabel = ToUByteTy; break;
90 case Type::SByteTyID: opLabel = ToSByteTy; break;
91 case Type::UShortTyID: opLabel = ToUShortTy; break;
92 case Type::ShortTyID: opLabel = ToShortTy; break;
93 case Type::UIntTyID: opLabel = ToUIntTy; break;
94 case Type::IntTyID: opLabel = ToIntTy; break;
95 case Type::ULongTyID: opLabel = ToULongTy; break;
96 case Type::LongTyID: opLabel = ToLongTy; break;
97 case Type::FloatTyID: opLabel = ToFloatTy; break;
98 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
99 case Type::ArrayTyID: opLabel = ToArrayTy; break;
100 case Type::PointerTyID: opLabel = ToPointerTy; break;
102 // Just use `Cast' opcode otherwise. It's probably ignored.
110 InstructionNode::dumpNode(int indent) const
112 for (int i=0; i < indent; i++)
115 cout << getInstruction()->getOpcodeName();
117 const vector<MachineInstr*> &mvec = getInstruction()->getMachineInstrVec();
119 cout << "\tMachine Instructions: ";
120 for (unsigned int i=0; i < mvec.size(); i++)
123 if (i < mvec.size() - 1)
132 VRegListNode::dumpNode(int indent) const
134 for (int i=0; i < indent; i++)
137 cout << "List" << endl;
142 VRegNode::dumpNode(int indent) const
144 for (int i=0; i < indent; i++)
147 cout << "VReg " << getValue() << "\t(type "
148 << (int) getValue()->getValueType() << ")" << endl;
152 ConstantNode::dumpNode(int indent) const
154 for (int i=0; i < indent; i++)
157 cout << "Constant " << getValue() << "\t(type "
158 << (int) getValue()->getValueType() << ")" << endl;
162 LabelNode::dumpNode(int indent) const
164 for (int i=0; i < indent; i++)
167 cout << "Label " << getValue() << endl;
170 //------------------------------------------------------------------------
173 // A forest of instruction trees, usually for a single method.
174 //------------------------------------------------------------------------
176 InstrForest::InstrForest(Method *M)
178 for (Method::inst_iterator I = M->inst_begin(); I != M->inst_end(); ++I)
179 this->buildTreeForInstruction(*I);
182 InstrForest::~InstrForest()
184 for (hash_map<const Instruction*, InstructionNode*>:: iterator I = begin();
190 InstrForest::dump() const
192 for (hash_set<InstructionNode*>::const_iterator I = treeRoots.begin();
193 I != treeRoots.end(); ++I)
194 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
198 InstrForest::noteTreeNodeForInstr(Instruction *instr,
199 InstructionNode *treeNode)
201 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
202 (*this)[instr] = treeNode;
203 treeRoots.insert(treeNode); // mark node as root of a new tree
208 InstrForest::setLeftChild(InstrTreeNode *Par, InstrTreeNode *Chld)
210 Par->LeftChild = Chld;
212 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
213 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
217 InstrForest::setRightChild(InstrTreeNode *Par, InstrTreeNode *Chld)
219 Par->RightChild = Chld;
221 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
222 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
227 InstrForest::buildTreeForInstruction(Instruction *instr)
229 InstructionNode *treeNode = getTreeNodeForInstr(instr);
232 // treeNode has already been constructed for this instruction
233 assert(treeNode->getInstruction() == instr);
237 // Otherwise, create a new tree node for this instruction.
239 treeNode = new InstructionNode(instr);
240 noteTreeNodeForInstr(instr, treeNode);
242 if (instr->getOpcode() == Instruction::Call)
243 { // Operands of call instruction
247 // If the instruction has more than 2 instruction operands,
248 // then we need to create artificial list nodes to hold them.
249 // (Note that we only count operands that get tree nodes, and not
250 // others such as branch labels for a branch or switch instruction.)
252 // To do this efficiently, we'll walk all operands, build treeNodes
253 // for all appropriate operands and save them in an array. We then
254 // insert children at the end, creating list nodes where needed.
255 // As a performance optimization, allocate a child array only
256 // if a fixed array is too small.
259 const unsigned int MAX_CHILD = 8;
260 static InstrTreeNode *fixedChildArray[MAX_CHILD];
261 InstrTreeNode **childArray =
262 (instr->getNumOperands() > MAX_CHILD)
263 ? new (InstrTreeNode*)[instr->getNumOperands()] : fixedChildArray;
266 // Walk the operands of the instruction
268 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
272 // Check if the operand is a data value, not an branch label, type,
273 // method or module. If the operand is an address type (i.e., label
274 // or method) that is used in an non-branching operation, e.g., `add'.
275 // that should be considered a data value.
277 // Check latter condition here just to simplify the next IF.
278 bool includeAddressOperand =
279 (isa<BasicBlock>(operand) || isa<Method>(operand))
280 && !instr->isTerminator();
282 if (includeAddressOperand || isa<Instruction>(operand) ||
283 isa<ConstPoolVal>(operand) || isa<MethodArgument>(operand) ||
284 isa<GlobalVariable>(operand))
286 // This operand is a data value
288 // An instruction that computes the incoming value is added as a
289 // child of the current instruction if:
290 // the value has only a single use
291 // AND both instructions are in the same basic block.
292 // AND the current instruction is not a PHI (because the incoming
293 // value is conceptually in a predecessor block,
294 // even though it may be in the same static block)
296 // (Note that if the value has only a single use (viz., `instr'),
297 // the def of the value can be safely moved just before instr
298 // and therefore it is safe to combine these two instructions.)
300 // In all other cases, the virtual register holding the value
301 // is used directly, i.e., made a child of the instruction node.
303 InstrTreeNode* opTreeNode;
304 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
305 cast<Instruction>(operand)->getParent() == instr->getParent() &&
306 !isa<PHINode>(instr) &&
307 instr->getOpcode() != Instruction::Call)
309 // Recursively create a treeNode for it.
310 opTreeNode = buildTreeForInstruction((Instruction*)operand);
312 else if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(operand))
314 // Create a leaf node for a constant
315 opTreeNode = new ConstantNode(CPV);
319 // Create a leaf node for the virtual register
320 opTreeNode = new VRegNode(operand);
323 childArray[numChildren++] = opTreeNode;
327 //--------------------------------------------------------------------
328 // Add any selected operands as children in the tree.
329 // Certain instructions can have more than 2 in some instances (viz.,
330 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
331 // array or struct). Make the operands of every such instruction into
332 // a right-leaning binary tree with the operand nodes at the leaves
333 // and VRegList nodes as internal nodes.
334 //--------------------------------------------------------------------
336 InstrTreeNode *parent = treeNode;
340 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
341 assert(instrOpcode == Instruction::PHINode ||
342 instrOpcode == Instruction::Call ||
343 instrOpcode == Instruction::Load ||
344 instrOpcode == Instruction::Store ||
345 instrOpcode == Instruction::GetElementPtr);
348 // Insert the first child as a direct child
349 if (numChildren >= 1)
350 setLeftChild(parent, childArray[0]);
354 // Create a list node for children 2 .. N-1, if any
355 for (n = numChildren-1; n >= 2; n--)
357 // We have more than two children
358 InstrTreeNode *listNode = new VRegListNode();
359 setRightChild(parent, listNode);
360 setLeftChild(listNode, childArray[numChildren - n]);
364 // Now insert the last remaining child (if any).
365 if (numChildren >= 2)
368 setRightChild(parent, childArray[numChildren - 1]);
371 if (childArray != fixedChildArray)
372 delete [] childArray;