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/iPHINode.h"
30 #include "llvm/ConstantVals.h"
31 #include "llvm/BasicBlock.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "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 cast<MemAccessInst>(I)->hasIndices())
81 opLabel = opLabel + 100; // load/getElem with index vector
83 else if (opLabel == Instruction::And ||
84 opLabel == Instruction::Or ||
85 opLabel == Instruction::Xor ||
86 opLabel == Instruction::Not)
88 // Distinguish bitwise operators from logical operators!
89 if (I->getType() != Type::BoolTy)
90 opLabel = opLabel + 100; // bitwise operator
92 else if (opLabel == Instruction::Cast)
94 const Type *ITy = I->getType();
95 switch(ITy->getPrimitiveID())
97 case Type::BoolTyID: opLabel = ToBoolTy; break;
98 case Type::UByteTyID: opLabel = ToUByteTy; break;
99 case Type::SByteTyID: opLabel = ToSByteTy; break;
100 case Type::UShortTyID: opLabel = ToUShortTy; break;
101 case Type::ShortTyID: opLabel = ToShortTy; break;
102 case Type::UIntTyID: opLabel = ToUIntTy; break;
103 case Type::IntTyID: opLabel = ToIntTy; break;
104 case Type::ULongTyID: opLabel = ToULongTy; break;
105 case Type::LongTyID: opLabel = ToLongTy; break;
106 case Type::FloatTyID: opLabel = ToFloatTy; break;
107 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
108 case Type::ArrayTyID: opLabel = ToArrayTy; break;
109 case Type::PointerTyID: opLabel = ToPointerTy; break;
111 // Just use `Cast' opcode otherwise. It's probably ignored.
119 InstructionNode::dumpNode(int indent) const
121 for (int i=0; i < indent; i++)
124 cout << getInstruction()->getOpcodeName();
126 const vector<MachineInstr*> &mvec = getInstruction()->getMachineInstrVec();
128 cout << "\tMachine Instructions: ";
129 for (unsigned int i=0; i < mvec.size(); i++)
132 if (i < mvec.size() - 1)
141 VRegListNode::dumpNode(int indent) const
143 for (int i=0; i < indent; i++)
146 cout << "List" << endl;
151 VRegNode::dumpNode(int indent) const
153 for (int i=0; i < indent; i++)
156 cout << "VReg " << getValue() << "\t(type "
157 << (int) getValue()->getValueType() << ")" << endl;
161 ConstantNode::dumpNode(int indent) const
163 for (int i=0; i < indent; i++)
166 cout << "Constant " << getValue() << "\t(type "
167 << (int) getValue()->getValueType() << ")" << endl;
171 LabelNode::dumpNode(int indent) const
173 for (int i=0; i < indent; i++)
176 cout << "Label " << getValue() << endl;
179 //------------------------------------------------------------------------
182 // A forest of instruction trees, usually for a single method.
183 //------------------------------------------------------------------------
185 InstrForest::InstrForest(Method *M)
187 for (Method::inst_iterator I = M->inst_begin(); I != M->inst_end(); ++I)
188 this->buildTreeForInstruction(*I);
191 InstrForest::~InstrForest()
193 for (hash_map<const Instruction*, InstructionNode*>:: iterator I = begin();
199 InstrForest::dump() const
201 for (hash_set<InstructionNode*>::const_iterator I = treeRoots.begin();
202 I != treeRoots.end(); ++I)
203 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
207 InstrForest::noteTreeNodeForInstr(Instruction *instr,
208 InstructionNode *treeNode)
210 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
211 (*this)[instr] = treeNode;
212 treeRoots.insert(treeNode); // mark node as root of a new tree
217 InstrForest::setLeftChild(InstrTreeNode *Par, InstrTreeNode *Chld)
219 Par->LeftChild = Chld;
221 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
222 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
226 InstrForest::setRightChild(InstrTreeNode *Par, InstrTreeNode *Chld)
228 Par->RightChild = Chld;
230 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
231 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
236 InstrForest::buildTreeForInstruction(Instruction *instr)
238 InstructionNode *treeNode = getTreeNodeForInstr(instr);
241 // treeNode has already been constructed for this instruction
242 assert(treeNode->getInstruction() == instr);
246 // Otherwise, create a new tree node for this instruction.
248 treeNode = new InstructionNode(instr);
249 noteTreeNodeForInstr(instr, treeNode);
251 if (instr->getOpcode() == Instruction::Call)
252 { // Operands of call instruction
256 // If the instruction has more than 2 instruction operands,
257 // then we need to create artificial list nodes to hold them.
258 // (Note that we only count operands that get tree nodes, and not
259 // others such as branch labels for a branch or switch instruction.)
261 // To do this efficiently, we'll walk all operands, build treeNodes
262 // for all appropriate operands and save them in an array. We then
263 // insert children at the end, creating list nodes where needed.
264 // As a performance optimization, allocate a child array only
265 // if a fixed array is too small.
268 const unsigned int MAX_CHILD = 8;
269 static InstrTreeNode *fixedChildArray[MAX_CHILD];
270 InstrTreeNode **childArray =
271 (instr->getNumOperands() > MAX_CHILD)
272 ? new (InstrTreeNode*)[instr->getNumOperands()] : fixedChildArray;
275 // Walk the operands of the instruction
277 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
281 // Check if the operand is a data value, not an branch label, type,
282 // method or module. If the operand is an address type (i.e., label
283 // or method) that is used in an non-branching operation, e.g., `add'.
284 // that should be considered a data value.
286 // Check latter condition here just to simplify the next IF.
287 bool includeAddressOperand =
288 (isa<BasicBlock>(operand) || isa<Method>(operand))
289 && !instr->isTerminator();
291 if (includeAddressOperand || isa<Instruction>(operand) ||
292 isa<Constant>(operand) || isa<MethodArgument>(operand) ||
293 isa<GlobalVariable>(operand))
295 // This operand is a data value
297 // An instruction that computes the incoming value is added as a
298 // child of the current instruction if:
299 // the value has only a single use
300 // AND both instructions are in the same basic block.
301 // AND the current instruction is not a PHI (because the incoming
302 // value is conceptually in a predecessor block,
303 // even though it may be in the same static block)
305 // (Note that if the value has only a single use (viz., `instr'),
306 // the def of the value can be safely moved just before instr
307 // and therefore it is safe to combine these two instructions.)
309 // In all other cases, the virtual register holding the value
310 // is used directly, i.e., made a child of the instruction node.
312 InstrTreeNode* opTreeNode;
313 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
314 cast<Instruction>(operand)->getParent() == instr->getParent() &&
315 !isa<PHINode>(instr) &&
316 instr->getOpcode() != Instruction::Call)
318 // Recursively create a treeNode for it.
319 opTreeNode = buildTreeForInstruction((Instruction*)operand);
321 else if (Constant *CPV = dyn_cast<Constant>(operand))
323 // Create a leaf node for a constant
324 opTreeNode = new ConstantNode(CPV);
328 // Create a leaf node for the virtual register
329 opTreeNode = new VRegNode(operand);
332 childArray[numChildren++] = opTreeNode;
336 //--------------------------------------------------------------------
337 // Add any selected operands as children in the tree.
338 // Certain instructions can have more than 2 in some instances (viz.,
339 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
340 // array or struct). Make the operands of every such instruction into
341 // a right-leaning binary tree with the operand nodes at the leaves
342 // and VRegList nodes as internal nodes.
343 //--------------------------------------------------------------------
345 InstrTreeNode *parent = treeNode;
349 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
350 assert(instrOpcode == Instruction::PHINode ||
351 instrOpcode == Instruction::Call ||
352 instrOpcode == Instruction::Load ||
353 instrOpcode == Instruction::Store ||
354 instrOpcode == Instruction::GetElementPtr);
357 // Insert the first child as a direct child
358 if (numChildren >= 1)
359 setLeftChild(parent, childArray[0]);
363 // Create a list node for children 2 .. N-1, if any
364 for (n = numChildren-1; n >= 2; n--)
366 // We have more than two children
367 InstrTreeNode *listNode = new VRegListNode();
368 setRightChild(parent, listNode);
369 setLeftChild(listNode, childArray[numChildren - n]);
373 // Now insert the last remaining child (if any).
374 if (numChildren >= 2)
377 setRightChild(parent, childArray[numChildren - 1]);
380 if (childArray != fixedChildArray)
381 delete [] childArray;