1 //===-- InstrForest.cpp - Build instruction forest for inst selection -----===//
3 // The key goal is to group instructions into a single
4 // tree if one or more of them might be potentially combined into a single
5 // complex instruction in the target machine.
6 // Since this grouping is completely machine-independent, we do it as
7 // aggressive as possible to exploit any possible taret instructions.
8 // In particular, we group two instructions O and I if:
9 // (1) Instruction O computes an operand used by instruction I,
10 // and (2) O and I are part of the same basic block,
11 // and (3) O has only a single use, viz., I.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/InstrForest.h"
16 #include "llvm/CodeGen/MachineCodeForInstruction.h"
17 #include "llvm/Function.h"
18 #include "llvm/iTerminators.h"
19 #include "llvm/iMemory.h"
20 #include "llvm/Constant.h"
21 #include "llvm/Type.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "Support/STLExtras.h"
27 //------------------------------------------------------------------------
28 // class InstrTreeNode
29 //------------------------------------------------------------------------
32 InstrTreeNode::dump(int dumpChildren, int indent) const
39 LeftChild->dump(dumpChildren, indent+1);
41 RightChild->dump(dumpChildren, indent+1);
46 InstructionNode::InstructionNode(Instruction* I)
47 : InstrTreeNode(NTInstructionNode, I),
48 codeIsFoldedIntoParent(false)
50 opLabel = I->getOpcode();
52 // Distinguish special cases of some instructions such as Ret and Br
54 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
56 opLabel = RetValueOp; // ret(value) operation
58 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
60 opLabel = BrCondOp; // br(cond) operation
62 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
64 opLabel = SetCCOp; // common label for all SetCC ops
66 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
68 opLabel = AllocaN; // Alloca(ptr, N) operation
70 else if ((opLabel == Instruction::Load ||
71 opLabel == Instruction::GetElementPtr) &&
72 cast<MemAccessInst>(I)->hasIndices())
74 opLabel = opLabel + 100; // load/getElem with index vector
76 else if (opLabel == Instruction::Xor &&
77 BinaryOperator::isNot(I))
79 opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator
80 : BNotOp; // bitwise Not operator
82 else if (opLabel == Instruction::And ||
83 opLabel == Instruction::Or ||
84 opLabel == Instruction::Xor)
86 // Distinguish bitwise operators from logical operators!
87 if (I->getType() != Type::BoolTy)
88 opLabel = opLabel + 100; // bitwise operator
90 else if (opLabel == Instruction::Cast)
92 const Type *ITy = I->getType();
93 switch(ITy->getPrimitiveID())
95 case Type::BoolTyID: opLabel = ToBoolTy; break;
96 case Type::UByteTyID: opLabel = ToUByteTy; break;
97 case Type::SByteTyID: opLabel = ToSByteTy; break;
98 case Type::UShortTyID: opLabel = ToUShortTy; break;
99 case Type::ShortTyID: opLabel = ToShortTy; break;
100 case Type::UIntTyID: opLabel = ToUIntTy; break;
101 case Type::IntTyID: opLabel = ToIntTy; break;
102 case Type::ULongTyID: opLabel = ToULongTy; break;
103 case Type::LongTyID: opLabel = ToLongTy; break;
104 case Type::FloatTyID: opLabel = ToFloatTy; break;
105 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
106 case Type::ArrayTyID: opLabel = ToArrayTy; break;
107 case Type::PointerTyID: opLabel = ToPointerTy; break;
109 // Just use `Cast' opcode otherwise. It's probably ignored.
117 InstructionNode::dumpNode(int indent) const
119 for (int i=0; i < indent; i++)
121 cerr << getInstruction()->getOpcodeName()
122 << " [label " << getOpLabel() << "]" << "\n";
127 VRegListNode::dumpNode(int indent) const
129 for (int i=0; i < indent; i++)
132 cerr << "List" << "\n";
137 VRegNode::dumpNode(int indent) const
139 for (int i=0; i < indent; i++)
142 cerr << "VReg " << getValue() << "\t(type "
143 << (int) getValue()->getValueType() << ")" << "\n";
147 ConstantNode::dumpNode(int indent) const
149 for (int i=0; i < indent; i++)
152 cerr << "Constant " << getValue() << "\t(type "
153 << (int) getValue()->getValueType() << ")" << "\n";
157 LabelNode::dumpNode(int indent) const
159 for (int i=0; i < indent; i++)
162 cerr << "Label " << getValue() << "\n";
165 //------------------------------------------------------------------------
168 // A forest of instruction trees, usually for a single method.
169 //------------------------------------------------------------------------
171 InstrForest::InstrForest(Function *F)
173 for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
174 for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
175 buildTreeForInstruction(I);
179 InstrForest::~InstrForest()
181 for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
185 InstrForest::dump() const
187 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
188 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
192 InstrForest::eraseRoot(InstructionNode* node)
194 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
197 treeRoots.erase(RI.base()-1);
201 InstrForest::noteTreeNodeForInstr(Instruction *instr,
202 InstructionNode *treeNode)
204 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
205 (*this)[instr] = treeNode;
206 treeRoots.push_back(treeNode); // mark node as root of a new tree
211 InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
213 parent->LeftChild = child;
214 child->Parent = parent;
215 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
216 eraseRoot((InstructionNode*) child); // no longer a tree root
220 InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
222 parent->RightChild = child;
223 child->Parent = parent;
224 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
225 eraseRoot((InstructionNode*) child); // no longer a tree root
230 InstrForest::buildTreeForInstruction(Instruction *instr)
232 InstructionNode *treeNode = getTreeNodeForInstr(instr);
235 // treeNode has already been constructed for this instruction
236 assert(treeNode->getInstruction() == instr);
240 // Otherwise, create a new tree node for this instruction.
242 treeNode = new InstructionNode(instr);
243 noteTreeNodeForInstr(instr, treeNode);
245 if (instr->getOpcode() == Instruction::Call)
246 { // Operands of call instruction
250 // If the instruction has more than 2 instruction operands,
251 // then we need to create artificial list nodes to hold them.
252 // (Note that we only count operands that get tree nodes, and not
253 // others such as branch labels for a branch or switch instruction.)
255 // To do this efficiently, we'll walk all operands, build treeNodes
256 // for all appropriate operands and save them in an array. We then
257 // insert children at the end, creating list nodes where needed.
258 // As a performance optimization, allocate a child array only
259 // if a fixed array is too small.
262 InstrTreeNode **childArray =
263 (InstrTreeNode **)alloca(instr->getNumOperands()*sizeof(InstrTreeNode *));
266 // Walk the operands of the instruction
268 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
272 // Check if the operand is a data value, not an branch label, type,
273 // method or module. If the operand is an address type (i.e., label
274 // or method) that is used in an non-branching operation, e.g., `add'.
275 // that should be considered a data value.
277 // Check latter condition here just to simplify the next IF.
278 bool includeAddressOperand =
279 (isa<BasicBlock>(operand) || isa<Function>(operand))
280 && !instr->isTerminator();
282 if (includeAddressOperand || isa<Instruction>(operand) ||
283 isa<Constant>(operand) || isa<Argument>(operand) ||
284 isa<GlobalVariable>(operand))
286 // This operand is a data value
288 // An instruction that computes the incoming value is added as a
289 // child of the current instruction if:
290 // the value has only a single use
291 // AND both instructions are in the same basic block.
292 // AND the current instruction is not a PHI (because the incoming
293 // value is conceptually in a predecessor block,
294 // even though it may be in the same static block)
296 // (Note that if the value has only a single use (viz., `instr'),
297 // the def of the value can be safely moved just before instr
298 // and therefore it is safe to combine these two instructions.)
300 // In all other cases, the virtual register holding the value
301 // is used directly, i.e., made a child of the instruction node.
303 InstrTreeNode* opTreeNode;
304 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
305 cast<Instruction>(operand)->getParent() == instr->getParent() &&
306 instr->getOpcode() != Instruction::PHINode &&
307 instr->getOpcode() != Instruction::Call)
309 // Recursively create a treeNode for it.
310 opTreeNode = buildTreeForInstruction((Instruction*)operand);
312 else if (Constant *CPV = dyn_cast<Constant>(operand))
314 // Create a leaf node for a constant
315 opTreeNode = new ConstantNode(CPV);
319 // Create a leaf node for the virtual register
320 opTreeNode = new VRegNode(operand);
323 childArray[numChildren++] = opTreeNode;
327 //--------------------------------------------------------------------
328 // Add any selected operands as children in the tree.
329 // Certain instructions can have more than 2 in some instances (viz.,
330 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
331 // array or struct). Make the operands of every such instruction into
332 // a right-leaning binary tree with the operand nodes at the leaves
333 // and VRegList nodes as internal nodes.
334 //--------------------------------------------------------------------
336 InstrTreeNode *parent = treeNode;
340 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
341 assert(instrOpcode == Instruction::PHINode ||
342 instrOpcode == Instruction::Call ||
343 instrOpcode == Instruction::Load ||
344 instrOpcode == Instruction::Store ||
345 instrOpcode == Instruction::GetElementPtr);
348 // Insert the first child as a direct child
349 if (numChildren >= 1)
350 setLeftChild(parent, childArray[0]);
354 // Create a list node for children 2 .. N-1, if any
355 for (n = numChildren-1; n >= 2; n--)
357 // We have more than two children
358 InstrTreeNode *listNode = new VRegListNode();
359 setRightChild(parent, listNode);
360 setLeftChild(listNode, childArray[numChildren - n]);
364 // Now insert the last remaining child (if any).
365 if (numChildren >= 2)
368 setRightChild(parent, childArray[numChildren - 1]);