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"
38 //------------------------------------------------------------------------
39 // class InstrTreeNode
40 //------------------------------------------------------------------------
43 InstrTreeNode::dump(int dumpChildren, int indent) const
50 LeftChild->dump(dumpChildren, indent+1);
52 RightChild->dump(dumpChildren, indent+1);
57 InstructionNode::InstructionNode(Instruction* I)
58 : InstrTreeNode(NTInstructionNode, I)
60 opLabel = I->getOpcode();
62 // Distinguish special cases of some instructions such as Ret and Br
64 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
66 opLabel = RetValueOp; // ret(value) operation
68 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
70 opLabel = BrCondOp; // br(cond) operation
72 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
74 opLabel = SetCCOp; // common label for all SetCC ops
76 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
78 opLabel = AllocaN; // Alloca(ptr, N) operation
80 else if ((opLabel == Instruction::Load ||
81 opLabel == Instruction::GetElementPtr) &&
82 cast<MemAccessInst>(I)->hasIndices())
84 opLabel = opLabel + 100; // load/getElem with index vector
86 else if (opLabel == Instruction::And ||
87 opLabel == Instruction::Or ||
88 opLabel == Instruction::Xor ||
89 opLabel == Instruction::Not)
91 // Distinguish bitwise operators from logical operators!
92 if (I->getType() != Type::BoolTy)
93 opLabel = opLabel + 100; // bitwise operator
95 else if (opLabel == Instruction::Cast)
97 const Type *ITy = I->getType();
98 switch(ITy->getPrimitiveID())
100 case Type::BoolTyID: opLabel = ToBoolTy; break;
101 case Type::UByteTyID: opLabel = ToUByteTy; break;
102 case Type::SByteTyID: opLabel = ToSByteTy; break;
103 case Type::UShortTyID: opLabel = ToUShortTy; break;
104 case Type::ShortTyID: opLabel = ToShortTy; break;
105 case Type::UIntTyID: opLabel = ToUIntTy; break;
106 case Type::IntTyID: opLabel = ToIntTy; break;
107 case Type::ULongTyID: opLabel = ToULongTy; break;
108 case Type::LongTyID: opLabel = ToLongTy; break;
109 case Type::FloatTyID: opLabel = ToFloatTy; break;
110 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
111 case Type::ArrayTyID: opLabel = ToArrayTy; break;
112 case Type::PointerTyID: opLabel = ToPointerTy; break;
114 // Just use `Cast' opcode otherwise. It's probably ignored.
122 InstructionNode::dumpNode(int indent) const
124 for (int i=0; i < indent; i++)
127 cerr << getInstruction()->getOpcodeName();
129 const vector<MachineInstr*> &mvec = getInstruction()->getMachineInstrVec();
131 cerr << "\tMachine Instructions: ";
132 for (unsigned int i=0; i < mvec.size(); i++)
135 if (i < mvec.size() - 1)
144 VRegListNode::dumpNode(int indent) const
146 for (int i=0; i < indent; i++)
149 cerr << "List" << "\n";
154 VRegNode::dumpNode(int indent) const
156 for (int i=0; i < indent; i++)
159 cerr << "VReg " << getValue() << "\t(type "
160 << (int) getValue()->getValueType() << ")" << "\n";
164 ConstantNode::dumpNode(int indent) const
166 for (int i=0; i < indent; i++)
169 cerr << "Constant " << getValue() << "\t(type "
170 << (int) getValue()->getValueType() << ")" << "\n";
174 LabelNode::dumpNode(int indent) const
176 for (int i=0; i < indent; i++)
179 cerr << "Label " << getValue() << "\n";
182 //------------------------------------------------------------------------
185 // A forest of instruction trees, usually for a single method.
186 //------------------------------------------------------------------------
188 InstrForest::InstrForest(Method *M)
190 for (Method::inst_iterator I = M->inst_begin(); I != M->inst_end(); ++I)
191 this->buildTreeForInstruction(*I);
194 InstrForest::~InstrForest()
196 for (std::hash_map<const Instruction*,InstructionNode*>::iterator I = begin();
202 InstrForest::dump() const
204 for (std::hash_set<InstructionNode*>::const_iterator I = treeRoots.begin();
205 I != treeRoots.end(); ++I)
206 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
210 InstrForest::noteTreeNodeForInstr(Instruction *instr,
211 InstructionNode *treeNode)
213 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
214 (*this)[instr] = treeNode;
215 treeRoots.insert(treeNode); // mark node as root of a new tree
220 InstrForest::setLeftChild(InstrTreeNode *Par, InstrTreeNode *Chld)
222 Par->LeftChild = Chld;
224 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
225 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
229 InstrForest::setRightChild(InstrTreeNode *Par, InstrTreeNode *Chld)
231 Par->RightChild = Chld;
233 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
234 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
239 InstrForest::buildTreeForInstruction(Instruction *instr)
241 InstructionNode *treeNode = getTreeNodeForInstr(instr);
244 // treeNode has already been constructed for this instruction
245 assert(treeNode->getInstruction() == instr);
249 // Otherwise, create a new tree node for this instruction.
251 treeNode = new InstructionNode(instr);
252 noteTreeNodeForInstr(instr, treeNode);
254 if (instr->getOpcode() == Instruction::Call)
255 { // Operands of call instruction
259 // If the instruction has more than 2 instruction operands,
260 // then we need to create artificial list nodes to hold them.
261 // (Note that we only count operands that get tree nodes, and not
262 // others such as branch labels for a branch or switch instruction.)
264 // To do this efficiently, we'll walk all operands, build treeNodes
265 // for all appropriate operands and save them in an array. We then
266 // insert children at the end, creating list nodes where needed.
267 // As a performance optimization, allocate a child array only
268 // if a fixed array is too small.
271 const unsigned int MAX_CHILD = 8;
272 static InstrTreeNode *fixedChildArray[MAX_CHILD];
273 InstrTreeNode **childArray =
274 (instr->getNumOperands() > MAX_CHILD)
275 ? new (InstrTreeNode*)[instr->getNumOperands()] : fixedChildArray;
278 // Walk the operands of the instruction
280 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
284 // Check if the operand is a data value, not an branch label, type,
285 // method or module. If the operand is an address type (i.e., label
286 // or method) that is used in an non-branching operation, e.g., `add'.
287 // that should be considered a data value.
289 // Check latter condition here just to simplify the next IF.
290 bool includeAddressOperand =
291 (isa<BasicBlock>(operand) || isa<Method>(operand))
292 && !instr->isTerminator();
294 if (includeAddressOperand || isa<Instruction>(operand) ||
295 isa<Constant>(operand) || isa<MethodArgument>(operand) ||
296 isa<GlobalVariable>(operand))
298 // This operand is a data value
300 // An instruction that computes the incoming value is added as a
301 // child of the current instruction if:
302 // the value has only a single use
303 // AND both instructions are in the same basic block.
304 // AND the current instruction is not a PHI (because the incoming
305 // value is conceptually in a predecessor block,
306 // even though it may be in the same static block)
308 // (Note that if the value has only a single use (viz., `instr'),
309 // the def of the value can be safely moved just before instr
310 // and therefore it is safe to combine these two instructions.)
312 // In all other cases, the virtual register holding the value
313 // is used directly, i.e., made a child of the instruction node.
315 InstrTreeNode* opTreeNode;
316 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
317 cast<Instruction>(operand)->getParent() == instr->getParent() &&
318 !isa<PHINode>(instr) &&
319 instr->getOpcode() != Instruction::Call)
321 // Recursively create a treeNode for it.
322 opTreeNode = buildTreeForInstruction((Instruction*)operand);
324 else if (Constant *CPV = dyn_cast<Constant>(operand))
326 // Create a leaf node for a constant
327 opTreeNode = new ConstantNode(CPV);
331 // Create a leaf node for the virtual register
332 opTreeNode = new VRegNode(operand);
335 childArray[numChildren++] = opTreeNode;
339 //--------------------------------------------------------------------
340 // Add any selected operands as children in the tree.
341 // Certain instructions can have more than 2 in some instances (viz.,
342 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
343 // array or struct). Make the operands of every such instruction into
344 // a right-leaning binary tree with the operand nodes at the leaves
345 // and VRegList nodes as internal nodes.
346 //--------------------------------------------------------------------
348 InstrTreeNode *parent = treeNode;
352 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
353 assert(instrOpcode == Instruction::PHINode ||
354 instrOpcode == Instruction::Call ||
355 instrOpcode == Instruction::Load ||
356 instrOpcode == Instruction::Store ||
357 instrOpcode == Instruction::GetElementPtr);
360 // Insert the first child as a direct child
361 if (numChildren >= 1)
362 setLeftChild(parent, childArray[0]);
366 // Create a list node for children 2 .. N-1, if any
367 for (n = numChildren-1; n >= 2; n--)
369 // We have more than two children
370 InstrTreeNode *listNode = new VRegListNode();
371 setRightChild(parent, listNode);
372 setLeftChild(listNode, childArray[numChildren - n]);
376 // Now insert the last remaining child (if any).
377 if (numChildren >= 2)
380 setRightChild(parent, childArray[numChildren - 1]);
383 if (childArray != fixedChildArray)
384 delete [] childArray;