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 #include "llvm/CodeGen/InstrForest.h"
26 #include "llvm/CodeGen/MachineCodeForInstruction.h"
27 #include "llvm/Function.h"
28 #include "llvm/iTerminators.h"
29 #include "llvm/iMemory.h"
30 #include "llvm/iPHINode.h"
31 #include "llvm/ConstantVals.h"
32 #include "llvm/BasicBlock.h"
33 #include "llvm/CodeGen/MachineInstr.h"
34 #include "Support/STLExtras.h"
39 //------------------------------------------------------------------------
40 // class InstrTreeNode
41 //------------------------------------------------------------------------
44 InstrTreeNode::dump(int dumpChildren, int indent) const
51 LeftChild->dump(dumpChildren, indent+1);
53 RightChild->dump(dumpChildren, indent+1);
58 InstructionNode::InstructionNode(Instruction* I)
59 : InstrTreeNode(NTInstructionNode, I),
60 codeIsFoldedIntoParent(false)
62 opLabel = I->getOpcode();
64 // Distinguish special cases of some instructions such as Ret and Br
66 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
68 opLabel = RetValueOp; // ret(value) operation
70 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
72 opLabel = BrCondOp; // br(cond) operation
74 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
76 opLabel = SetCCOp; // common label for all SetCC ops
78 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
80 opLabel = AllocaN; // Alloca(ptr, N) operation
82 else if ((opLabel == Instruction::Load ||
83 opLabel == Instruction::GetElementPtr) &&
84 cast<MemAccessInst>(I)->hasIndices())
86 opLabel = opLabel + 100; // load/getElem with index vector
88 else if (opLabel == Instruction::And ||
89 opLabel == Instruction::Or ||
90 opLabel == Instruction::Xor ||
91 opLabel == Instruction::Not)
93 // Distinguish bitwise operators from logical operators!
94 if (I->getType() != Type::BoolTy)
95 opLabel = opLabel + 100; // bitwise operator
97 else if (opLabel == Instruction::Cast)
99 const Type *ITy = I->getType();
100 switch(ITy->getPrimitiveID())
102 case Type::BoolTyID: opLabel = ToBoolTy; break;
103 case Type::UByteTyID: opLabel = ToUByteTy; break;
104 case Type::SByteTyID: opLabel = ToSByteTy; break;
105 case Type::UShortTyID: opLabel = ToUShortTy; break;
106 case Type::ShortTyID: opLabel = ToShortTy; break;
107 case Type::UIntTyID: opLabel = ToUIntTy; break;
108 case Type::IntTyID: opLabel = ToIntTy; break;
109 case Type::ULongTyID: opLabel = ToULongTy; break;
110 case Type::LongTyID: opLabel = ToLongTy; break;
111 case Type::FloatTyID: opLabel = ToFloatTy; break;
112 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
113 case Type::ArrayTyID: opLabel = ToArrayTy; break;
114 case Type::PointerTyID: opLabel = ToPointerTy; break;
116 // Just use `Cast' opcode otherwise. It's probably ignored.
124 InstructionNode::dumpNode(int indent) const
126 for (int i=0; i < indent; i++)
129 cerr << getInstruction()->getOpcodeName();
130 const MachineCodeForInstruction &mvec =
131 MachineCodeForInstruction::get(getInstruction());
134 cerr << "\tMachine Instructions: ";
136 for (unsigned int i=0; i < mvec.size(); ++i) {
138 if (i < mvec.size() - 1)
147 VRegListNode::dumpNode(int indent) const
149 for (int i=0; i < indent; i++)
152 cerr << "List" << "\n";
157 VRegNode::dumpNode(int indent) const
159 for (int i=0; i < indent; i++)
162 cerr << "VReg " << getValue() << "\t(type "
163 << (int) getValue()->getValueType() << ")" << "\n";
167 ConstantNode::dumpNode(int indent) const
169 for (int i=0; i < indent; i++)
172 cerr << "Constant " << getValue() << "\t(type "
173 << (int) getValue()->getValueType() << ")" << "\n";
177 LabelNode::dumpNode(int indent) const
179 for (int i=0; i < indent; i++)
182 cerr << "Label " << getValue() << "\n";
185 //------------------------------------------------------------------------
188 // A forest of instruction trees, usually for a single method.
189 //------------------------------------------------------------------------
191 InstrForest::InstrForest(Function *F)
193 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
194 BasicBlock *BB = *FI;
195 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
196 buildTreeForInstruction(*I);
200 InstrForest::~InstrForest()
202 for (std::hash_map<const Instruction*,InstructionNode*>::iterator I=begin();
208 InstrForest::dump() const
210 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
211 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
215 InstrForest::eraseRoot(InstructionNode* node)
217 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
220 treeRoots.erase(RI.base()-1);
224 InstrForest::noteTreeNodeForInstr(Instruction *instr,
225 InstructionNode *treeNode)
227 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
228 (*this)[instr] = treeNode;
229 treeRoots.push_back(treeNode); // mark node as root of a new tree
234 InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
236 parent->LeftChild = child;
237 child->Parent = parent;
238 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
239 eraseRoot((InstructionNode*) child); // no longer a tree root
243 InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
245 parent->RightChild = child;
246 child->Parent = parent;
247 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
248 eraseRoot((InstructionNode*) child); // no longer a tree root
253 InstrForest::buildTreeForInstruction(Instruction *instr)
255 InstructionNode *treeNode = getTreeNodeForInstr(instr);
258 // treeNode has already been constructed for this instruction
259 assert(treeNode->getInstruction() == instr);
263 // Otherwise, create a new tree node for this instruction.
265 treeNode = new InstructionNode(instr);
266 noteTreeNodeForInstr(instr, treeNode);
268 if (instr->getOpcode() == Instruction::Call)
269 { // Operands of call instruction
273 // If the instruction has more than 2 instruction operands,
274 // then we need to create artificial list nodes to hold them.
275 // (Note that we only count operands that get tree nodes, and not
276 // others such as branch labels for a branch or switch instruction.)
278 // To do this efficiently, we'll walk all operands, build treeNodes
279 // for all appropriate operands and save them in an array. We then
280 // insert children at the end, creating list nodes where needed.
281 // As a performance optimization, allocate a child array only
282 // if a fixed array is too small.
285 const unsigned int MAX_CHILD = 8;
286 static InstrTreeNode *fixedChildArray[MAX_CHILD];
287 InstrTreeNode **childArray =
288 (instr->getNumOperands() > MAX_CHILD)
289 ? new (InstrTreeNode*)[instr->getNumOperands()] : fixedChildArray;
292 // Walk the operands of the instruction
294 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
298 // Check if the operand is a data value, not an branch label, type,
299 // method or module. If the operand is an address type (i.e., label
300 // or method) that is used in an non-branching operation, e.g., `add'.
301 // that should be considered a data value.
303 // Check latter condition here just to simplify the next IF.
304 bool includeAddressOperand =
305 (isa<BasicBlock>(operand) || isa<Function>(operand))
306 && !instr->isTerminator();
308 if (includeAddressOperand || isa<Instruction>(operand) ||
309 isa<Constant>(operand) || isa<FunctionArgument>(operand) ||
310 isa<GlobalVariable>(operand))
312 // This operand is a data value
314 // An instruction that computes the incoming value is added as a
315 // child of the current instruction if:
316 // the value has only a single use
317 // AND both instructions are in the same basic block.
318 // AND the current instruction is not a PHI (because the incoming
319 // value is conceptually in a predecessor block,
320 // even though it may be in the same static block)
322 // (Note that if the value has only a single use (viz., `instr'),
323 // the def of the value can be safely moved just before instr
324 // and therefore it is safe to combine these two instructions.)
326 // In all other cases, the virtual register holding the value
327 // is used directly, i.e., made a child of the instruction node.
329 InstrTreeNode* opTreeNode;
330 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
331 cast<Instruction>(operand)->getParent() == instr->getParent() &&
332 !isa<PHINode>(instr) &&
333 instr->getOpcode() != Instruction::Call)
335 // Recursively create a treeNode for it.
336 opTreeNode = buildTreeForInstruction((Instruction*)operand);
338 else if (Constant *CPV = dyn_cast<Constant>(operand))
340 // Create a leaf node for a constant
341 opTreeNode = new ConstantNode(CPV);
345 // Create a leaf node for the virtual register
346 opTreeNode = new VRegNode(operand);
349 childArray[numChildren++] = opTreeNode;
353 //--------------------------------------------------------------------
354 // Add any selected operands as children in the tree.
355 // Certain instructions can have more than 2 in some instances (viz.,
356 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
357 // array or struct). Make the operands of every such instruction into
358 // a right-leaning binary tree with the operand nodes at the leaves
359 // and VRegList nodes as internal nodes.
360 //--------------------------------------------------------------------
362 InstrTreeNode *parent = treeNode;
366 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
367 assert(instrOpcode == Instruction::PHINode ||
368 instrOpcode == Instruction::Call ||
369 instrOpcode == Instruction::Load ||
370 instrOpcode == Instruction::Store ||
371 instrOpcode == Instruction::GetElementPtr);
374 // Insert the first child as a direct child
375 if (numChildren >= 1)
376 setLeftChild(parent, childArray[0]);
380 // Create a list node for children 2 .. N-1, if any
381 for (n = numChildren-1; n >= 2; n--)
383 // We have more than two children
384 InstrTreeNode *listNode = new VRegListNode();
385 setRightChild(parent, listNode);
386 setLeftChild(listNode, childArray[numChildren - n]);
390 // Now insert the last remaining child (if any).
391 if (numChildren >= 2)
394 setRightChild(parent, childArray[numChildren - 1]);
397 if (childArray != fixedChildArray)
398 delete [] childArray;