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 target 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"
24 #include "Config/alloca.h"
26 //------------------------------------------------------------------------
27 // class InstrTreeNode
28 //------------------------------------------------------------------------
31 InstrTreeNode::dump(int dumpChildren, int indent) const
38 LeftChild->dump(dumpChildren, indent+1);
40 RightChild->dump(dumpChildren, indent+1);
45 InstructionNode::InstructionNode(Instruction* I)
46 : InstrTreeNode(NTInstructionNode, I),
47 codeIsFoldedIntoParent(false)
49 opLabel = I->getOpcode();
51 // Distinguish special cases of some instructions such as Ret and Br
53 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
55 opLabel = RetValueOp; // ret(value) operation
57 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
59 opLabel = BrCondOp; // br(cond) operation
61 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
63 opLabel = SetCCOp; // common label for all SetCC ops
65 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
67 opLabel = AllocaN; // Alloca(ptr, N) operation
69 else if (opLabel == Instruction::GetElementPtr &&
70 cast<GetElementPtrInst>(I)->hasIndices())
72 opLabel = opLabel + 100; // getElem with index vector
74 else if (opLabel == Instruction::Xor &&
75 BinaryOperator::isNot(I))
77 opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator
78 : BNotOp; // bitwise Not operator
80 else if (opLabel == Instruction::And ||
81 opLabel == Instruction::Or ||
82 opLabel == Instruction::Xor)
84 // Distinguish bitwise operators from logical operators!
85 if (I->getType() != Type::BoolTy)
86 opLabel = opLabel + 100; // bitwise operator
88 else if (opLabel == Instruction::Cast)
90 const Type *ITy = I->getType();
91 switch(ITy->getPrimitiveID())
93 case Type::BoolTyID: opLabel = ToBoolTy; break;
94 case Type::UByteTyID: opLabel = ToUByteTy; break;
95 case Type::SByteTyID: opLabel = ToSByteTy; break;
96 case Type::UShortTyID: opLabel = ToUShortTy; break;
97 case Type::ShortTyID: opLabel = ToShortTy; break;
98 case Type::UIntTyID: opLabel = ToUIntTy; break;
99 case Type::IntTyID: opLabel = ToIntTy; break;
100 case Type::ULongTyID: opLabel = ToULongTy; break;
101 case Type::LongTyID: opLabel = ToLongTy; break;
102 case Type::FloatTyID: opLabel = ToFloatTy; break;
103 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
104 case Type::ArrayTyID: opLabel = ToArrayTy; break;
105 case Type::PointerTyID: opLabel = ToPointerTy; break;
107 // Just use `Cast' opcode otherwise. It's probably ignored.
115 InstructionNode::dumpNode(int indent) const
117 for (int i=0; i < indent; i++)
119 std::cerr << getInstruction()->getOpcodeName()
120 << " [label " << getOpLabel() << "]" << "\n";
125 VRegListNode::dumpNode(int indent) const
127 for (int i=0; i < indent; i++)
130 std::cerr << "List" << "\n";
135 VRegNode::dumpNode(int indent) const
137 for (int i=0; i < indent; i++)
140 std::cerr << "VReg " << getValue() << "\t(type "
141 << (int) getValue()->getValueType() << ")" << "\n";
145 ConstantNode::dumpNode(int indent) const
147 for (int i=0; i < indent; i++)
150 std::cerr << "Constant " << getValue() << "\t(type "
151 << (int) getValue()->getValueType() << ")" << "\n";
155 LabelNode::dumpNode(int indent) const
157 for (int i=0; i < indent; i++)
160 std::cerr << "Label " << getValue() << "\n";
163 //------------------------------------------------------------------------
166 // A forest of instruction trees, usually for a single method.
167 //------------------------------------------------------------------------
169 InstrForest::InstrForest(Function *F)
171 for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
172 for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
173 buildTreeForInstruction(I);
177 InstrForest::~InstrForest()
179 for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
183 InstrForest::dump() const
185 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
186 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
190 InstrForest::eraseRoot(InstructionNode* node)
192 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
195 treeRoots.erase(RI.base()-1);
199 InstrForest::noteTreeNodeForInstr(Instruction *instr,
200 InstructionNode *treeNode)
202 (*this)[instr] = treeNode;
203 treeRoots.push_back(treeNode); // mark node as root of a new tree
208 InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
210 parent->LeftChild = child;
211 child->Parent = parent;
212 if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
213 eraseRoot(instrNode); // no longer a tree root
217 InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
219 parent->RightChild = child;
220 child->Parent = parent;
221 if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
222 eraseRoot(instrNode); // no longer a tree root
227 InstrForest::buildTreeForInstruction(Instruction *instr)
229 InstructionNode *treeNode = getTreeNodeForInstr(instr);
232 // treeNode has already been constructed for this instruction
233 assert(treeNode->getInstruction() == instr);
237 // Otherwise, create a new tree node for this instruction.
239 treeNode = new InstructionNode(instr);
240 noteTreeNodeForInstr(instr, treeNode);
242 if (instr->getOpcode() == Instruction::Call)
243 { // Operands of call instruction
247 // If the instruction has more than 2 instruction operands,
248 // then we need to create artificial list nodes to hold them.
249 // (Note that we only count operands that get tree nodes, and not
250 // others such as branch labels for a branch or switch instruction.)
252 // To do this efficiently, we'll walk all operands, build treeNodes
253 // for all appropriate operands and save them in an array. We then
254 // insert children at the end, creating list nodes where needed.
255 // As a performance optimization, allocate a child array only
256 // if a fixed array is too small.
259 InstrTreeNode** childArray = new InstrTreeNode*[instr->getNumOperands()];
262 // Walk the operands of the instruction
264 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
268 // Check if the operand is a data value, not an branch label, type,
269 // method or module. If the operand is an address type (i.e., label
270 // or method) that is used in an non-branching operation, e.g., `add'.
271 // that should be considered a data value.
273 // Check latter condition here just to simplify the next IF.
274 bool includeAddressOperand =
275 (isa<BasicBlock>(operand) || isa<Function>(operand))
276 && !instr->isTerminator();
278 if (includeAddressOperand || isa<Instruction>(operand) ||
279 isa<Constant>(operand) || isa<Argument>(operand) ||
280 isa<GlobalVariable>(operand))
282 // This operand is a data value
284 // An instruction that computes the incoming value is added as a
285 // child of the current instruction if:
286 // the value has only a single use
287 // AND both instructions are in the same basic block.
288 // AND the current instruction is not a PHI (because the incoming
289 // value is conceptually in a predecessor block,
290 // even though it may be in the same static block)
292 // (Note that if the value has only a single use (viz., `instr'),
293 // the def of the value can be safely moved just before instr
294 // and therefore it is safe to combine these two instructions.)
296 // In all other cases, the virtual register holding the value
297 // is used directly, i.e., made a child of the instruction node.
299 InstrTreeNode* opTreeNode;
300 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
301 cast<Instruction>(operand)->getParent() == instr->getParent() &&
302 instr->getOpcode() != Instruction::PHINode &&
303 instr->getOpcode() != Instruction::Call)
305 // Recursively create a treeNode for it.
306 opTreeNode = buildTreeForInstruction((Instruction*)operand);
308 else if (Constant *CPV = dyn_cast<Constant>(operand))
310 // Create a leaf node for a constant
311 opTreeNode = new ConstantNode(CPV);
315 // Create a leaf node for the virtual register
316 opTreeNode = new VRegNode(operand);
319 childArray[numChildren++] = opTreeNode;
323 //--------------------------------------------------------------------
324 // Add any selected operands as children in the tree.
325 // Certain instructions can have more than 2 in some instances (viz.,
326 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
327 // array or struct). Make the operands of every such instruction into
328 // a right-leaning binary tree with the operand nodes at the leaves
329 // and VRegList nodes as internal nodes.
330 //--------------------------------------------------------------------
332 InstrTreeNode *parent = treeNode;
336 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
337 assert(instrOpcode == Instruction::PHINode ||
338 instrOpcode == Instruction::Call ||
339 instrOpcode == Instruction::Load ||
340 instrOpcode == Instruction::Store ||
341 instrOpcode == Instruction::GetElementPtr);
344 // Insert the first child as a direct child
345 if (numChildren >= 1)
346 setLeftChild(parent, childArray[0]);
350 // Create a list node for children 2 .. N-1, if any
351 for (n = numChildren-1; n >= 2; n--)
353 // We have more than two children
354 InstrTreeNode *listNode = new VRegListNode();
355 setRightChild(parent, listNode);
356 setLeftChild(listNode, childArray[numChildren - n]);
360 // Now insert the last remaining child (if any).
361 if (numChildren >= 2)
364 setRightChild(parent, childArray[numChildren - 1]);
367 delete [] childArray;