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;
53 InstrTreeNode::~InstrTreeNode()
58 InstrTreeNode::dump(int dumpChildren,
61 this->dumpNode(indent);
66 leftChild()->dump(dumpChildren, indent+1);
68 rightChild()->dump(dumpChildren, indent+1);
73 InstructionNode::InstructionNode(Instruction* _instr)
74 : InstrTreeNode(NTInstructionNode, _instr)
76 OpLabel opLabel = _instr->getOpcode();
78 // Distinguish special cases of some instructions such as Ret and Br
80 if (opLabel == Instruction::Ret && ((ReturnInst*) _instr)->getReturnValue())
82 opLabel = RetValueOp; // ret(value) operation
84 else if (opLabel == Instruction::Br && ! ((BranchInst*) _instr)->isUnconditional())
86 opLabel = BrCondOp; // br(cond) operation
88 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
90 opLabel = SetCCOp; // common label for all SetCC ops
92 else if (opLabel == Instruction::Alloca && _instr->getNumOperands() > 0)
94 opLabel = AllocaN; // Alloca(ptr, N) operation
96 else if ((opLabel == Instruction::Load ||
97 opLabel == Instruction::GetElementPtr)
98 && ((MemAccessInst*)_instr)->getFirstOffsetIdx() > 0)
100 opLabel = opLabel + 100; // load/getElem with index vector
102 else if (opLabel == Instruction::Cast)
104 const Type* instrValueType = _instr->getType();
105 switch(instrValueType->getPrimitiveID())
107 case Type::BoolTyID: opLabel = ToBoolTy; break;
108 case Type::UByteTyID: opLabel = ToUByteTy; break;
109 case Type::SByteTyID: opLabel = ToSByteTy; break;
110 case Type::UShortTyID: opLabel = ToUShortTy; break;
111 case Type::ShortTyID: opLabel = ToShortTy; break;
112 case Type::UIntTyID: opLabel = ToUIntTy; break;
113 case Type::IntTyID: opLabel = ToIntTy; break;
114 case Type::ULongTyID: opLabel = ToULongTy; break;
115 case Type::LongTyID: opLabel = ToLongTy; break;
116 case Type::FloatTyID: opLabel = ToFloatTy; break;
117 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
119 if (instrValueType->isArrayType())
121 else if (instrValueType->isPointerType())
122 opLabel = ToPointerTy;
124 ; // Just use `Cast' opcode otherwise. It's probably ignored.
129 basicNode.opLabel = opLabel;
133 InstructionNode::reverseBinaryArgumentOrder()
135 assert(getInstruction()->isBinaryOp());
137 // switch arguments for the instruction
138 ((BinaryOperator*) getInstruction())->swapOperands();
140 // switch arguments for this tree node itself
141 BasicTreeNode* leftCopy = basicNode.leftChild;
142 basicNode.leftChild = basicNode.rightChild;
143 basicNode.rightChild = leftCopy;
147 InstructionNode::dumpNode(int indent) const
149 for (int i=0; i < indent; i++)
152 cout << getInstruction()->getOpcodeName();
154 const vector<MachineInstr*>& mvec = getInstruction()->getMachineInstrVec();
156 cout << "\tMachine Instructions: ";
157 for (unsigned int i=0; i < mvec.size(); i++)
160 if (i < mvec.size() - 1)
168 VRegListNode::VRegListNode()
169 : InstrTreeNode(NTVRegListNode, NULL)
171 basicNode.opLabel = VRegListOp;
175 VRegListNode::dumpNode(int indent) const
177 for (int i=0; i < indent; i++)
180 cout << "List" << endl;
184 VRegNode::VRegNode(Value* _val)
185 : InstrTreeNode(NTVRegNode, _val)
187 basicNode.opLabel = VRegNodeOp;
191 VRegNode::dumpNode(int indent) const
193 for (int i=0; i < indent; i++)
196 cout << "VReg " << getValue() << "\t(type "
197 << (int) getValue()->getValueType() << ")" << endl;
201 ConstantNode::ConstantNode(ConstPoolVal* constVal)
202 : InstrTreeNode(NTConstNode, constVal)
204 basicNode.opLabel = ConstantNodeOp;
208 ConstantNode::dumpNode(int indent) const
210 for (int i=0; i < indent; i++)
213 cout << "Constant " << getValue() << "\t(type "
214 << (int) getValue()->getValueType() << ")" << endl;
218 LabelNode::LabelNode(BasicBlock* _bblock)
219 : InstrTreeNode(NTLabelNode, _bblock)
221 basicNode.opLabel = LabelNodeOp;
225 LabelNode::dumpNode(int indent) const
227 for (int i=0; i < indent; i++)
230 cout << "Label " << getValue() << endl;
233 //------------------------------------------------------------------------
236 // A forest of instruction trees, usually for a single method.
237 //------------------------------------------------------------------------
240 InstrForest::buildTreesForMethod(Method *method)
242 for (Method::inst_iterator instrIter = method->inst_begin();
243 instrIter != method->inst_end();
246 Instruction *instr = *instrIter;
247 if (! instr->isPHINode())
248 (void) this->buildTreeForInstruction(instr);
254 InstrForest::dump() const
256 for (hash_set<InstructionNode*>::const_iterator
257 treeRootIter = treeRoots.begin();
258 treeRootIter != treeRoots.end();
261 (*treeRootIter)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
266 InstrForest::noteTreeNodeForInstr(Instruction* instr,
267 InstructionNode* treeNode)
269 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
270 (*this)[instr] = treeNode;
271 treeRoots.insert(treeNode); // mark node as root of a new tree
276 InstrForest::setLeftChild(InstrTreeNode* parent, InstrTreeNode* child)
278 parent->basicNode.leftChild = & child->basicNode;
279 child->basicNode.parent = & parent->basicNode;
280 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
281 treeRoots.erase((InstructionNode*) child); // no longer a tree root
286 InstrForest::setRightChild(InstrTreeNode* parent, InstrTreeNode* child)
288 parent->basicNode.rightChild = & child->basicNode;
289 child->basicNode.parent = & parent->basicNode;
290 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
291 treeRoots.erase((InstructionNode*) child); // no longer a tree root
296 InstrForest::buildTreeForInstruction(Instruction* instr)
298 InstructionNode* treeNode = this->getTreeNodeForInstr(instr);
299 if (treeNode != NULL)
300 {// treeNode has already been constructed for this instruction
301 assert(treeNode->getInstruction() == instr);
305 // Otherwise, create a new tree node for this instruction.
307 treeNode = new InstructionNode(instr);
308 this->noteTreeNodeForInstr(instr, treeNode);
310 // If the instruction has more than 2 instruction operands,
311 // then we will not add any children. This assumes that instructions
312 // like 'call' that have more than 2 instruction operands do not
313 // ever get combined with the instructions that compute the operands.
314 // Note that we only count operands of type instruction and not other
315 // values such as branch labels for a branch or switch instruction.
317 // To do this efficiently, we'll walk all operands, build treeNodes
318 // for all instruction operands and save them in an array, and then
319 // insert children at the end if there are not more than 2.
320 // As a performance optimization, allocate a child array only
321 // if a fixed array is too small.
324 const unsigned int MAX_CHILD = 8;
325 static InstrTreeNode* fixedChildArray[MAX_CHILD];
326 InstrTreeNode** childArray =
327 (instr->getNumOperands() > MAX_CHILD)
328 ? new (InstrTreeNode*)[instr->getNumOperands()]
332 // Walk the operands of the instruction
334 for (Instruction::op_iterator opIter = instr->op_begin();
335 opIter != instr->op_end();
338 Value* operand = *opIter;
340 // Check if the operand is a data value, not an branch label, type,
341 // method or module. If the operand is an address type (i.e., label
342 // or method) that is used in an non-branching operation, e.g., `add'.
343 // that should be considered a data value.
345 // Check latter condition here just to simplify the next IF.
346 bool includeAddressOperand =
347 ((operand->getValueType() == Value::BasicBlockVal
348 || operand->getValueType() == Value::MethodVal)
349 && ! instr->isTerminator());
351 if (/* (*opIter) != NULL
352 &&*/ includeAddressOperand
353 || operand->getValueType() == Value::InstructionVal
354 || operand->getValueType() == Value::ConstantVal
355 || operand->getValueType() == Value::MethodArgumentVal)
356 {// This operand is a data value
358 // An instruction that computes the incoming value is added as a
359 // child of the current instruction if:
360 // the value has only a single use
361 // AND both instructions are in the same basic block
362 // AND the instruction is not a PHI
364 // (Note that if the value has only a single use (viz., `instr'),
365 // the def of the value can be safely moved just before instr
366 // and therefore it is safe to combine these two instructions.)
368 // In all other cases, the virtual register holding the value
369 // is used directly, i.e., made a child of the instruction node.
371 InstrTreeNode* opTreeNode;
372 if (operand->getValueType() == Value::InstructionVal
373 && operand->use_size() == 1
374 && ((Instruction*)operand)->getParent() == instr->getParent()
375 && ! ((Instruction*)operand)->isPHINode())
377 // Recursively create a treeNode for it.
378 opTreeNode =this->buildTreeForInstruction((Instruction*)operand);
380 else if (operand->getValueType() == Value::ConstantVal)
382 // Create a leaf node for a constant
383 opTreeNode = new ConstantNode((ConstPoolVal*) operand);
387 // Create a leaf node for the virtual register
388 opTreeNode = new VRegNode(operand);
391 childArray[numChildren] = opTreeNode;
396 //--------------------------------------------------------------------
397 // Add any selected operands as children in the tree.
398 // Certain instructions can have more than 2 in some instances (viz.,
399 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
400 // array or struct). Make the operands of every such instruction into
401 // a right-leaning binary tree with the operand nodes at the leaves
402 // and VRegList nodes as internal nodes.
403 //--------------------------------------------------------------------
405 InstrTreeNode* parent = treeNode; // new VRegListNode();
410 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
411 assert(instrOpcode == Instruction::Call ||
412 instrOpcode == Instruction::Load ||
413 instrOpcode == Instruction::Store ||
414 instrOpcode == Instruction::GetElementPtr);
417 // Insert the first child as a direct child
418 if (numChildren >= 1)
419 this->setLeftChild(parent, childArray[0]);
421 // Create a list node for children 2 .. N-1, if any
422 for (n = numChildren-1; n >= 2; n--)
423 { // We have more than two children
424 InstrTreeNode* listNode = new VRegListNode();
425 this->setRightChild(parent, listNode);
426 this->setLeftChild(listNode, childArray[numChildren - n]);
430 // Now insert the last remaining child (if any).
431 if (numChildren >= 2)
434 this->setRightChild(parent, childArray[numChildren - 1]);
437 if (childArray != fixedChildArray)