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 //*************************** User Include Files ***************************/
27 #include "llvm/CodeGen/InstrForest.h"
28 #include "llvm/Method.h"
29 #include "llvm/iTerminators.h"
30 #include "llvm/iMemory.h"
31 #include "llvm/ConstPoolVals.h"
32 #include "llvm/BasicBlock.h"
33 #include "llvm/CodeGen/MachineInstr.h"
36 //------------------------------------------------------------------------
37 // class InstrTreeNode
38 //------------------------------------------------------------------------
41 InstrTreeNode::InstrTreeNode(InstrTreeNodeType nodeType, Value* _val)
42 : treeNodeType(nodeType), val(_val) {
49 void InstrTreeNode::dump(int dumpChildren, int indent) const {
55 leftChild()->dump(dumpChildren, indent+1);
57 rightChild()->dump(dumpChildren, indent+1);
62 InstructionNode::InstructionNode(Instruction* _instr)
63 : InstrTreeNode(NTInstructionNode, _instr)
65 OpLabel opLabel = _instr->getOpcode();
67 // Distinguish special cases of some instructions such as Ret and Br
69 if (opLabel == Instruction::Ret && ((ReturnInst*) _instr)->getReturnValue())
71 opLabel = RetValueOp; // ret(value) operation
73 else if (opLabel == Instruction::Br && ! ((BranchInst*) _instr)->isUnconditional())
75 opLabel = BrCondOp; // br(cond) operation
77 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
79 opLabel = SetCCOp; // common label for all SetCC ops
81 else if (opLabel == Instruction::Alloca && _instr->getNumOperands() > 0)
83 opLabel = AllocaN; // Alloca(ptr, N) operation
85 else if ((opLabel == Instruction::Load ||
86 opLabel == Instruction::GetElementPtr)
87 && ((MemAccessInst*)_instr)->getFirstOffsetIdx() > 0)
89 opLabel = opLabel + 100; // load/getElem with index vector
91 else if (opLabel == Instruction::Cast)
93 const Type* instrValueType = _instr->getType();
94 switch(instrValueType->getPrimitiveID())
96 case Type::BoolTyID: opLabel = ToBoolTy; break;
97 case Type::UByteTyID: opLabel = ToUByteTy; break;
98 case Type::SByteTyID: opLabel = ToSByteTy; break;
99 case Type::UShortTyID: opLabel = ToUShortTy; break;
100 case Type::ShortTyID: opLabel = ToShortTy; break;
101 case Type::UIntTyID: opLabel = ToUIntTy; break;
102 case Type::IntTyID: opLabel = ToIntTy; break;
103 case Type::ULongTyID: opLabel = ToULongTy; break;
104 case Type::LongTyID: opLabel = ToLongTy; break;
105 case Type::FloatTyID: opLabel = ToFloatTy; break;
106 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
108 if (instrValueType->isArrayType())
110 else if (instrValueType->isPointerType())
111 opLabel = ToPointerTy;
113 ; // Just use `Cast' opcode otherwise. It's probably ignored.
118 this->opLabel = opLabel;
123 InstructionNode::dumpNode(int indent) const
125 for (int i=0; i < indent; i++)
128 cout << getInstruction()->getOpcodeName();
130 const vector<MachineInstr*>& mvec = getInstruction()->getMachineInstrVec();
132 cout << "\tMachine Instructions: ";
133 for (unsigned int i=0; i < mvec.size(); i++)
136 if (i < mvec.size() - 1)
144 VRegListNode::VRegListNode() : InstrTreeNode(NTVRegListNode, 0) {
145 opLabel = VRegListOp;
149 VRegListNode::dumpNode(int indent) const
151 for (int i=0; i < indent; i++)
154 cout << "List" << endl;
158 VRegNode::VRegNode(Value* _val) : InstrTreeNode(NTVRegNode, _val) {
159 opLabel = VRegNodeOp;
163 VRegNode::dumpNode(int indent) const
165 for (int i=0; i < indent; i++)
168 cout << "VReg " << getValue() << "\t(type "
169 << (int) getValue()->getValueType() << ")" << endl;
173 ConstantNode::ConstantNode(ConstPoolVal *constVal)
174 : InstrTreeNode(NTConstNode, constVal) {
175 opLabel = ConstantNodeOp;
179 ConstantNode::dumpNode(int indent) const
181 for (int i=0; i < indent; i++)
184 cout << "Constant " << getValue() << "\t(type "
185 << (int) getValue()->getValueType() << ")" << endl;
189 LabelNode::LabelNode(BasicBlock *BB) : InstrTreeNode(NTLabelNode, BB) {
190 opLabel = LabelNodeOp;
194 LabelNode::dumpNode(int indent) const
196 for (int i=0; i < indent; i++)
199 cout << "Label " << getValue() << endl;
202 //------------------------------------------------------------------------
205 // A forest of instruction trees, usually for a single method.
206 //------------------------------------------------------------------------
209 InstrForest::buildTreesForMethod(Method *method)
211 for (Method::inst_iterator instrIter = method->inst_begin();
212 instrIter != method->inst_end();
215 Instruction *instr = *instrIter;
216 (void) this->buildTreeForInstruction(instr);
222 InstrForest::dump() const
224 for (hash_set<InstructionNode*>::const_iterator
225 treeRootIter = treeRoots.begin();
226 treeRootIter != treeRoots.end();
229 (*treeRootIter)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
234 InstrForest::noteTreeNodeForInstr(Instruction* instr,
235 InstructionNode* treeNode)
237 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
238 (*this)[instr] = treeNode;
239 treeRoots.insert(treeNode); // mark node as root of a new tree
244 InstrForest::setLeftChild(InstrTreeNode* parent, InstrTreeNode* child) {
245 parent->LeftChild = child;
246 child->Parent = parent;
247 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
248 treeRoots.erase((InstructionNode*) child); // no longer a tree root
253 InstrForest::setRightChild(InstrTreeNode* parent, InstrTreeNode* child)
255 parent->RightChild = child;
256 child->Parent = parent;
257 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
258 treeRoots.erase((InstructionNode*) child); // no longer a tree root
263 InstrForest::buildTreeForInstruction(Instruction* instr)
265 InstructionNode* treeNode = this->getTreeNodeForInstr(instr);
266 if (treeNode != NULL)
267 {// treeNode has already been constructed for this instruction
268 assert(treeNode->getInstruction() == instr);
272 // Otherwise, create a new tree node for this instruction.
274 treeNode = new InstructionNode(instr);
275 this->noteTreeNodeForInstr(instr, treeNode);
277 // If the instruction has more than 2 instruction operands,
278 // then we need to create artificial list nodes to hold them.
279 // (Note that we only not count operands that get tree nodes, and not
280 // others such as branch labels for a branch or switch instruction.)
282 // To do this efficiently, we'll walk all operands, build treeNodes
283 // for all appropriate operands and save them in an array. We then
284 // insert children at the end, creating list nodes where needed.
285 // As a performance optimization, allocate a child array only
286 // if a fixed array is too small.
289 const unsigned int MAX_CHILD = 8;
290 static InstrTreeNode* fixedChildArray[MAX_CHILD];
291 InstrTreeNode** childArray =
292 (instr->getNumOperands() > MAX_CHILD)
293 ? new (InstrTreeNode*)[instr->getNumOperands()]
297 // Walk the operands of the instruction
299 for (Instruction::op_iterator O=instr->op_begin(); O != instr->op_end(); ++O)
303 // Check if the operand is a data value, not an branch label, type,
304 // method or module. If the operand is an address type (i.e., label
305 // or method) that is used in an non-branching operation, e.g., `add'.
306 // that should be considered a data value.
308 // Check latter condition here just to simplify the next IF.
309 bool includeAddressOperand =
310 ((operand->isBasicBlock() || operand->isMethod())
311 && !instr->isTerminator());
313 if (includeAddressOperand || operand->isInstruction() ||
314 operand->isConstant() || operand->isMethodArgument())
315 {// This operand is a data value
317 // An instruction that computes the incoming value is added as a
318 // child of the current instruction if:
319 // the value has only a single use
320 // AND both instructions are in the same basic block.
322 // (Note that if the value has only a single use (viz., `instr'),
323 // the def of the value can be safely moved just before instr
324 // and therefore it is safe to combine these two instructions.)
326 // In all other cases, the virtual register holding the value
327 // is used directly, i.e., made a child of the instruction node.
329 InstrTreeNode* opTreeNode;
330 if (operand->isInstruction() && operand->use_size() == 1 &&
331 ((Instruction*)operand)->getParent() == instr->getParent())
333 // Recursively create a treeNode for it.
334 opTreeNode =this->buildTreeForInstruction((Instruction*)operand);
336 else if (ConstPoolVal *CPV = operand->castConstant())
338 // Create a leaf node for a constant
339 opTreeNode = new ConstantNode(CPV);
343 // Create a leaf node for the virtual register
344 opTreeNode = new VRegNode(operand);
347 childArray[numChildren] = opTreeNode;
352 //--------------------------------------------------------------------
353 // Add any selected operands as children in the tree.
354 // Certain instructions can have more than 2 in some instances (viz.,
355 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
356 // array or struct). Make the operands of every such instruction into
357 // a right-leaning binary tree with the operand nodes at the leaves
358 // and VRegList nodes as internal nodes.
359 //--------------------------------------------------------------------
361 InstrTreeNode* parent = treeNode; // new VRegListNode();
366 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
367 assert(instrOpcode == Instruction::PHINode ||
368 instrOpcode == Instruction::Call ||
369 instrOpcode == Instruction::Load ||
370 instrOpcode == Instruction::Store ||
371 instrOpcode == Instruction::GetElementPtr);
374 // Insert the first child as a direct child
375 if (numChildren >= 1)
376 this->setLeftChild(parent, childArray[0]);
378 // Create a list node for children 2 .. N-1, if any
379 for (n = numChildren-1; n >= 2; n--)
380 { // We have more than two children
381 InstrTreeNode* listNode = new VRegListNode();
382 this->setRightChild(parent, listNode);
383 this->setLeftChild(listNode, childArray[numChildren - n]);
387 // Now insert the last remaining child (if any).
388 if (numChildren >= 2)
391 this->setRightChild(parent, childArray[numChildren - 1]);
394 if (childArray != fixedChildArray)