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.
20 //---------------------------------------------------------------------------
22 #include "llvm/CodeGen/InstrForest.h"
23 #include "llvm/CodeGen/MachineCodeForInstruction.h"
24 #include "llvm/Function.h"
25 #include "llvm/iTerminators.h"
26 #include "llvm/iMemory.h"
27 #include "llvm/Constant.h"
28 #include "llvm/BasicBlock.h"
29 #include "llvm/Type.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "Support/STLExtras.h"
37 //------------------------------------------------------------------------
38 // class InstrTreeNode
39 //------------------------------------------------------------------------
42 InstrTreeNode::dump(int dumpChildren, int indent) const
49 LeftChild->dump(dumpChildren, indent+1);
51 RightChild->dump(dumpChildren, indent+1);
56 InstructionNode::InstructionNode(Instruction* I)
57 : InstrTreeNode(NTInstructionNode, I),
58 codeIsFoldedIntoParent(false)
60 opLabel = I->getOpcode();
62 // Distinguish special cases of some instructions such as Ret and Br
64 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
66 opLabel = RetValueOp; // ret(value) operation
68 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
70 opLabel = BrCondOp; // br(cond) operation
72 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
74 opLabel = SetCCOp; // common label for all SetCC ops
76 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
78 opLabel = AllocaN; // Alloca(ptr, N) operation
80 else if ((opLabel == Instruction::Load ||
81 opLabel == Instruction::GetElementPtr) &&
82 cast<MemAccessInst>(I)->hasIndices())
84 opLabel = opLabel + 100; // load/getElem with index vector
86 else if (opLabel == Instruction::And ||
87 opLabel == Instruction::Or ||
88 opLabel == Instruction::Xor ||
89 opLabel == Instruction::Not)
91 // Distinguish bitwise operators from logical operators!
92 if (I->getType() != Type::BoolTy)
93 opLabel = opLabel + 100; // bitwise operator
95 else if (opLabel == Instruction::Cast)
97 const Type *ITy = I->getType();
98 switch(ITy->getPrimitiveID())
100 case Type::BoolTyID: opLabel = ToBoolTy; break;
101 case Type::UByteTyID: opLabel = ToUByteTy; break;
102 case Type::SByteTyID: opLabel = ToSByteTy; break;
103 case Type::UShortTyID: opLabel = ToUShortTy; break;
104 case Type::ShortTyID: opLabel = ToShortTy; break;
105 case Type::UIntTyID: opLabel = ToUIntTy; break;
106 case Type::IntTyID: opLabel = ToIntTy; break;
107 case Type::ULongTyID: opLabel = ToULongTy; break;
108 case Type::LongTyID: opLabel = ToLongTy; break;
109 case Type::FloatTyID: opLabel = ToFloatTy; break;
110 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
111 case Type::ArrayTyID: opLabel = ToArrayTy; break;
112 case Type::PointerTyID: opLabel = ToPointerTy; break;
114 // Just use `Cast' opcode otherwise. It's probably ignored.
122 InstructionNode::dumpNode(int indent) const
124 for (int i=0; i < indent; i++)
127 cerr << getInstruction()->getOpcodeName();
128 const MachineCodeForInstruction &mvec =
129 MachineCodeForInstruction::get(getInstruction());
132 cerr << "\tMachine Instructions: ";
134 for (unsigned int i=0; i < mvec.size(); ++i) {
136 if (i < mvec.size() - 1)
145 VRegListNode::dumpNode(int indent) const
147 for (int i=0; i < indent; i++)
150 cerr << "List" << "\n";
155 VRegNode::dumpNode(int indent) const
157 for (int i=0; i < indent; i++)
160 cerr << "VReg " << getValue() << "\t(type "
161 << (int) getValue()->getValueType() << ")" << "\n";
165 ConstantNode::dumpNode(int indent) const
167 for (int i=0; i < indent; i++)
170 cerr << "Constant " << getValue() << "\t(type "
171 << (int) getValue()->getValueType() << ")" << "\n";
175 LabelNode::dumpNode(int indent) const
177 for (int i=0; i < indent; i++)
180 cerr << "Label " << getValue() << "\n";
183 //------------------------------------------------------------------------
186 // A forest of instruction trees, usually for a single method.
187 //------------------------------------------------------------------------
189 InstrForest::InstrForest(Function *F)
191 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
192 BasicBlock *BB = *FI;
193 for_each(BB->begin(), BB->end(),
194 bind_obj(this, &InstrForest::buildTreeForInstruction));
198 InstrForest::~InstrForest()
200 for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
204 InstrForest::dump() const
206 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
207 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
211 InstrForest::eraseRoot(InstructionNode* node)
213 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
216 treeRoots.erase(RI.base()-1);
220 InstrForest::noteTreeNodeForInstr(Instruction *instr,
221 InstructionNode *treeNode)
223 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
224 (*this)[instr] = treeNode;
225 treeRoots.push_back(treeNode); // mark node as root of a new tree
230 InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
232 parent->LeftChild = child;
233 child->Parent = parent;
234 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
235 eraseRoot((InstructionNode*) child); // no longer a tree root
239 InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
241 parent->RightChild = child;
242 child->Parent = parent;
243 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
244 eraseRoot((InstructionNode*) child); // no longer a tree root
249 InstrForest::buildTreeForInstruction(Instruction *instr)
251 InstructionNode *treeNode = getTreeNodeForInstr(instr);
254 // treeNode has already been constructed for this instruction
255 assert(treeNode->getInstruction() == instr);
259 // Otherwise, create a new tree node for this instruction.
261 treeNode = new InstructionNode(instr);
262 noteTreeNodeForInstr(instr, treeNode);
264 if (instr->getOpcode() == Instruction::Call)
265 { // Operands of call instruction
269 // If the instruction has more than 2 instruction operands,
270 // then we need to create artificial list nodes to hold them.
271 // (Note that we only count operands that get tree nodes, and not
272 // others such as branch labels for a branch or switch instruction.)
274 // To do this efficiently, we'll walk all operands, build treeNodes
275 // for all appropriate operands and save them in an array. We then
276 // insert children at the end, creating list nodes where needed.
277 // As a performance optimization, allocate a child array only
278 // if a fixed array is too small.
281 InstrTreeNode **childArray =
282 (InstrTreeNode **)alloca(instr->getNumOperands()*sizeof(InstrTreeNode *));
285 // Walk the operands of the instruction
287 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
291 // Check if the operand is a data value, not an branch label, type,
292 // method or module. If the operand is an address type (i.e., label
293 // or method) that is used in an non-branching operation, e.g., `add'.
294 // that should be considered a data value.
296 // Check latter condition here just to simplify the next IF.
297 bool includeAddressOperand =
298 (isa<BasicBlock>(operand) || isa<Function>(operand))
299 && !instr->isTerminator();
301 if (includeAddressOperand || isa<Instruction>(operand) ||
302 isa<Constant>(operand) || isa<Argument>(operand) ||
303 isa<GlobalVariable>(operand))
305 // This operand is a data value
307 // An instruction that computes the incoming value is added as a
308 // child of the current instruction if:
309 // the value has only a single use
310 // AND both instructions are in the same basic block.
311 // AND the current instruction is not a PHI (because the incoming
312 // value is conceptually in a predecessor block,
313 // even though it may be in the same static block)
315 // (Note that if the value has only a single use (viz., `instr'),
316 // the def of the value can be safely moved just before instr
317 // and therefore it is safe to combine these two instructions.)
319 // In all other cases, the virtual register holding the value
320 // is used directly, i.e., made a child of the instruction node.
322 InstrTreeNode* opTreeNode;
323 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
324 cast<Instruction>(operand)->getParent() == instr->getParent() &&
325 instr->getOpcode() != Instruction::PHINode &&
326 instr->getOpcode() != Instruction::Call)
328 // Recursively create a treeNode for it.
329 opTreeNode = buildTreeForInstruction((Instruction*)operand);
331 else if (Constant *CPV = dyn_cast<Constant>(operand))
333 // Create a leaf node for a constant
334 opTreeNode = new ConstantNode(CPV);
338 // Create a leaf node for the virtual register
339 opTreeNode = new VRegNode(operand);
342 childArray[numChildren++] = opTreeNode;
346 //--------------------------------------------------------------------
347 // Add any selected operands as children in the tree.
348 // Certain instructions can have more than 2 in some instances (viz.,
349 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
350 // array or struct). Make the operands of every such instruction into
351 // a right-leaning binary tree with the operand nodes at the leaves
352 // and VRegList nodes as internal nodes.
353 //--------------------------------------------------------------------
355 InstrTreeNode *parent = treeNode;
359 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
360 assert(instrOpcode == Instruction::PHINode ||
361 instrOpcode == Instruction::Call ||
362 instrOpcode == Instruction::Load ||
363 instrOpcode == Instruction::Store ||
364 instrOpcode == Instruction::GetElementPtr);
367 // Insert the first child as a direct child
368 if (numChildren >= 1)
369 setLeftChild(parent, childArray[0]);
373 // Create a list node for children 2 .. N-1, if any
374 for (n = numChildren-1; n >= 2; n--)
376 // We have more than two children
377 InstrTreeNode *listNode = new VRegListNode();
378 setRightChild(parent, listNode);
379 setLeftChild(listNode, childArray[numChildren - n]);
383 // Now insert the last remaining child (if any).
384 if (numChildren >= 2)
387 setRightChild(parent, childArray[numChildren - 1]);