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,
43 : treeNodeType(nodeType),
46 basicNode.leftChild = NULL;
47 basicNode.rightChild = NULL;
48 basicNode.parent = NULL;
49 basicNode.opLabel = InvalidOp;
50 basicNode.treeNodePtr = this;
54 InstrTreeNode::dump(int dumpChildren,
57 this->dumpNode(indent);
62 leftChild()->dump(dumpChildren, indent+1);
64 rightChild()->dump(dumpChildren, indent+1);
69 InstructionNode::InstructionNode(Instruction* _instr)
70 : InstrTreeNode(NTInstructionNode, _instr)
72 OpLabel opLabel = _instr->getOpcode();
74 // Distinguish special cases of some instructions such as Ret and Br
76 if (opLabel == Instruction::Ret && ((ReturnInst*) _instr)->getReturnValue())
78 opLabel = RetValueOp; // ret(value) operation
80 else if (opLabel == Instruction::Br && ! ((BranchInst*) _instr)->isUnconditional())
82 opLabel = BrCondOp; // br(cond) operation
84 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
86 opLabel = SetCCOp; // common label for all SetCC ops
88 else if (opLabel == Instruction::Alloca && _instr->getNumOperands() > 0)
90 opLabel = AllocaN; // Alloca(ptr, N) operation
92 else if ((opLabel == Instruction::Load ||
93 opLabel == Instruction::GetElementPtr)
94 && ((MemAccessInst*)_instr)->getFirstOffsetIdx() > 0)
96 opLabel = opLabel + 100; // load/getElem with index vector
98 else if (opLabel == Instruction::Cast)
100 const Type* instrValueType = _instr->getType();
101 switch(instrValueType->getPrimitiveID())
103 case Type::BoolTyID: opLabel = ToBoolTy; break;
104 case Type::UByteTyID: opLabel = ToUByteTy; break;
105 case Type::SByteTyID: opLabel = ToSByteTy; break;
106 case Type::UShortTyID: opLabel = ToUShortTy; break;
107 case Type::ShortTyID: opLabel = ToShortTy; break;
108 case Type::UIntTyID: opLabel = ToUIntTy; break;
109 case Type::IntTyID: opLabel = ToIntTy; break;
110 case Type::ULongTyID: opLabel = ToULongTy; break;
111 case Type::LongTyID: opLabel = ToLongTy; break;
112 case Type::FloatTyID: opLabel = ToFloatTy; break;
113 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
115 if (instrValueType->isArrayType())
117 else if (instrValueType->isPointerType())
118 opLabel = ToPointerTy;
120 ; // Just use `Cast' opcode otherwise. It's probably ignored.
125 basicNode.opLabel = opLabel;
130 InstructionNode::dumpNode(int indent) const
132 for (int i=0; i < indent; i++)
135 cout << getInstruction()->getOpcodeName();
137 const vector<MachineInstr*>& mvec = getInstruction()->getMachineInstrVec();
139 cout << "\tMachine Instructions: ";
140 for (unsigned int i=0; i < mvec.size(); i++)
143 if (i < mvec.size() - 1)
151 VRegListNode::VRegListNode()
152 : InstrTreeNode(NTVRegListNode, NULL)
154 basicNode.opLabel = VRegListOp;
158 VRegListNode::dumpNode(int indent) const
160 for (int i=0; i < indent; i++)
163 cout << "List" << endl;
167 VRegNode::VRegNode(Value* _val)
168 : InstrTreeNode(NTVRegNode, _val)
170 basicNode.opLabel = VRegNodeOp;
174 VRegNode::dumpNode(int indent) const
176 for (int i=0; i < indent; i++)
179 cout << "VReg " << getValue() << "\t(type "
180 << (int) getValue()->getValueType() << ")" << endl;
184 ConstantNode::ConstantNode(ConstPoolVal* constVal)
185 : InstrTreeNode(NTConstNode, constVal)
187 basicNode.opLabel = ConstantNodeOp;
191 ConstantNode::dumpNode(int indent) const
193 for (int i=0; i < indent; i++)
196 cout << "Constant " << getValue() << "\t(type "
197 << (int) getValue()->getValueType() << ")" << endl;
201 LabelNode::LabelNode(BasicBlock* _bblock)
202 : InstrTreeNode(NTLabelNode, _bblock)
204 basicNode.opLabel = LabelNodeOp;
208 LabelNode::dumpNode(int indent) const
210 for (int i=0; i < indent; i++)
213 cout << "Label " << getValue() << endl;
216 //------------------------------------------------------------------------
219 // A forest of instruction trees, usually for a single method.
220 //------------------------------------------------------------------------
223 InstrForest::buildTreesForMethod(Method *method)
225 for (Method::inst_iterator instrIter = method->inst_begin();
226 instrIter != method->inst_end();
229 Instruction *instr = *instrIter;
230 (void) this->buildTreeForInstruction(instr);
236 InstrForest::dump() const
238 for (hash_set<InstructionNode*>::const_iterator
239 treeRootIter = treeRoots.begin();
240 treeRootIter != treeRoots.end();
243 (*treeRootIter)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
248 InstrForest::noteTreeNodeForInstr(Instruction* instr,
249 InstructionNode* treeNode)
251 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
252 (*this)[instr] = treeNode;
253 treeRoots.insert(treeNode); // mark node as root of a new tree
258 InstrForest::setLeftChild(InstrTreeNode* parent, InstrTreeNode* child)
260 parent->basicNode.leftChild = & child->basicNode;
261 child->basicNode.parent = & parent->basicNode;
262 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
263 treeRoots.erase((InstructionNode*) child); // no longer a tree root
268 InstrForest::setRightChild(InstrTreeNode* parent, InstrTreeNode* child)
270 parent->basicNode.rightChild = & child->basicNode;
271 child->basicNode.parent = & parent->basicNode;
272 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
273 treeRoots.erase((InstructionNode*) child); // no longer a tree root
278 InstrForest::buildTreeForInstruction(Instruction* instr)
280 InstructionNode* treeNode = this->getTreeNodeForInstr(instr);
281 if (treeNode != NULL)
282 {// treeNode has already been constructed for this instruction
283 assert(treeNode->getInstruction() == instr);
287 // Otherwise, create a new tree node for this instruction.
289 treeNode = new InstructionNode(instr);
290 this->noteTreeNodeForInstr(instr, treeNode);
292 // If the instruction has more than 2 instruction operands,
293 // then we need to create artificial list nodes to hold them.
294 // (Note that we only not count operands that get tree nodes, and not
295 // others such as branch labels for a branch or switch instruction.)
297 // To do this efficiently, we'll walk all operands, build treeNodes
298 // for all appropriate operands and save them in an array. We then
299 // insert children at the end, creating list nodes where needed.
300 // As a performance optimization, allocate a child array only
301 // if a fixed array is too small.
304 const unsigned int MAX_CHILD = 8;
305 static InstrTreeNode* fixedChildArray[MAX_CHILD];
306 InstrTreeNode** childArray =
307 (instr->getNumOperands() > MAX_CHILD)
308 ? new (InstrTreeNode*)[instr->getNumOperands()]
312 // Walk the operands of the instruction
314 for (Instruction::op_iterator O=instr->op_begin(); O != instr->op_end(); ++O)
318 // Check if the operand is a data value, not an branch label, type,
319 // method or module. If the operand is an address type (i.e., label
320 // or method) that is used in an non-branching operation, e.g., `add'.
321 // that should be considered a data value.
323 // Check latter condition here just to simplify the next IF.
324 bool includeAddressOperand =
325 ((operand->isBasicBlock() || operand->isMethod())
326 && !instr->isTerminator());
328 if (includeAddressOperand || operand->isInstruction() ||
329 operand->isConstant() || operand->isMethodArgument())
330 {// This operand is a data value
332 // An instruction that computes the incoming value is added as a
333 // child of the current instruction if:
334 // the value has only a single use
335 // AND both instructions are in the same basic block.
337 // (Note that if the value has only a single use (viz., `instr'),
338 // the def of the value can be safely moved just before instr
339 // and therefore it is safe to combine these two instructions.)
341 // In all other cases, the virtual register holding the value
342 // is used directly, i.e., made a child of the instruction node.
344 InstrTreeNode* opTreeNode;
345 if (operand->isInstruction() && operand->use_size() == 1 &&
346 ((Instruction*)operand)->getParent() == instr->getParent())
348 // Recursively create a treeNode for it.
349 opTreeNode =this->buildTreeForInstruction((Instruction*)operand);
351 else if (ConstPoolVal *CPV = operand->castConstant())
353 // Create a leaf node for a constant
354 opTreeNode = new ConstantNode(CPV);
358 // Create a leaf node for the virtual register
359 opTreeNode = new VRegNode(operand);
362 childArray[numChildren] = opTreeNode;
367 //--------------------------------------------------------------------
368 // Add any selected operands as children in the tree.
369 // Certain instructions can have more than 2 in some instances (viz.,
370 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
371 // array or struct). Make the operands of every such instruction into
372 // a right-leaning binary tree with the operand nodes at the leaves
373 // and VRegList nodes as internal nodes.
374 //--------------------------------------------------------------------
376 InstrTreeNode* parent = treeNode; // new VRegListNode();
381 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
382 assert(instrOpcode == Instruction::PHINode ||
383 instrOpcode == Instruction::Call ||
384 instrOpcode == Instruction::Load ||
385 instrOpcode == Instruction::Store ||
386 instrOpcode == Instruction::GetElementPtr);
389 // Insert the first child as a direct child
390 if (numChildren >= 1)
391 this->setLeftChild(parent, childArray[0]);
393 // Create a list node for children 2 .. N-1, if any
394 for (n = numChildren-1; n >= 2; n--)
395 { // We have more than two children
396 InstrTreeNode* listNode = new VRegListNode();
397 this->setRightChild(parent, listNode);
398 this->setLeftChild(listNode, childArray[numChildren - n]);
402 // Now insert the last remaining child (if any).
403 if (numChildren >= 2)
406 this->setRightChild(parent, childArray[numChildren - 1]);
409 if (childArray != fixedChildArray)