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/Type.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "Support/STLExtras.h"
36 //------------------------------------------------------------------------
37 // class InstrTreeNode
38 //------------------------------------------------------------------------
41 InstrTreeNode::dump(int dumpChildren, int indent) const
48 LeftChild->dump(dumpChildren, indent+1);
50 RightChild->dump(dumpChildren, indent+1);
55 InstructionNode::InstructionNode(Instruction* I)
56 : InstrTreeNode(NTInstructionNode, I),
57 codeIsFoldedIntoParent(false)
59 opLabel = I->getOpcode();
61 // Distinguish special cases of some instructions such as Ret and Br
63 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
65 opLabel = RetValueOp; // ret(value) operation
67 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
69 opLabel = BrCondOp; // br(cond) operation
71 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
73 opLabel = SetCCOp; // common label for all SetCC ops
75 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
77 opLabel = AllocaN; // Alloca(ptr, N) operation
79 else if ((opLabel == Instruction::Load ||
80 opLabel == Instruction::GetElementPtr) &&
81 cast<MemAccessInst>(I)->hasIndices())
83 opLabel = opLabel + 100; // load/getElem with index vector
85 else if (opLabel == Instruction::And ||
86 opLabel == Instruction::Or ||
87 opLabel == Instruction::Xor ||
88 opLabel == Instruction::Not)
90 // Distinguish bitwise operators from logical operators!
91 if (I->getType() != Type::BoolTy)
92 opLabel = opLabel + 100; // bitwise operator
94 else if (opLabel == Instruction::Cast)
96 const Type *ITy = I->getType();
97 switch(ITy->getPrimitiveID())
99 case Type::BoolTyID: opLabel = ToBoolTy; break;
100 case Type::UByteTyID: opLabel = ToUByteTy; break;
101 case Type::SByteTyID: opLabel = ToSByteTy; break;
102 case Type::UShortTyID: opLabel = ToUShortTy; break;
103 case Type::ShortTyID: opLabel = ToShortTy; break;
104 case Type::UIntTyID: opLabel = ToUIntTy; break;
105 case Type::IntTyID: opLabel = ToIntTy; break;
106 case Type::ULongTyID: opLabel = ToULongTy; break;
107 case Type::LongTyID: opLabel = ToLongTy; break;
108 case Type::FloatTyID: opLabel = ToFloatTy; break;
109 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
110 case Type::ArrayTyID: opLabel = ToArrayTy; break;
111 case Type::PointerTyID: opLabel = ToPointerTy; break;
113 // Just use `Cast' opcode otherwise. It's probably ignored.
121 InstructionNode::dumpNode(int indent) const
123 for (int i=0; i < indent; i++)
126 cerr << getInstruction()->getOpcodeName();
127 const MachineCodeForInstruction &mvec =
128 MachineCodeForInstruction::get(getInstruction());
131 cerr << "\tMachine Instructions: ";
133 for (unsigned int i=0; i < mvec.size(); ++i) {
135 if (i < mvec.size() - 1)
144 VRegListNode::dumpNode(int indent) const
146 for (int i=0; i < indent; i++)
149 cerr << "List" << "\n";
154 VRegNode::dumpNode(int indent) const
156 for (int i=0; i < indent; i++)
159 cerr << "VReg " << getValue() << "\t(type "
160 << (int) getValue()->getValueType() << ")" << "\n";
164 ConstantNode::dumpNode(int indent) const
166 for (int i=0; i < indent; i++)
169 cerr << "Constant " << getValue() << "\t(type "
170 << (int) getValue()->getValueType() << ")" << "\n";
174 LabelNode::dumpNode(int indent) const
176 for (int i=0; i < indent; i++)
179 cerr << "Label " << getValue() << "\n";
182 //------------------------------------------------------------------------
185 // A forest of instruction trees, usually for a single method.
186 //------------------------------------------------------------------------
188 InstrForest::InstrForest(Function *F)
190 for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
191 for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
192 buildTreeForInstruction(I);
196 InstrForest::~InstrForest()
198 for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
202 InstrForest::dump() const
204 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
205 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
209 InstrForest::eraseRoot(InstructionNode* node)
211 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
214 treeRoots.erase(RI.base()-1);
218 InstrForest::noteTreeNodeForInstr(Instruction *instr,
219 InstructionNode *treeNode)
221 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
222 (*this)[instr] = treeNode;
223 treeRoots.push_back(treeNode); // mark node as root of a new tree
228 InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
230 parent->LeftChild = child;
231 child->Parent = parent;
232 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
233 eraseRoot((InstructionNode*) child); // no longer a tree root
237 InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
239 parent->RightChild = child;
240 child->Parent = parent;
241 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
242 eraseRoot((InstructionNode*) child); // no longer a tree root
247 InstrForest::buildTreeForInstruction(Instruction *instr)
249 InstructionNode *treeNode = getTreeNodeForInstr(instr);
252 // treeNode has already been constructed for this instruction
253 assert(treeNode->getInstruction() == instr);
257 // Otherwise, create a new tree node for this instruction.
259 treeNode = new InstructionNode(instr);
260 noteTreeNodeForInstr(instr, treeNode);
262 if (instr->getOpcode() == Instruction::Call)
263 { // Operands of call instruction
267 // If the instruction has more than 2 instruction operands,
268 // then we need to create artificial list nodes to hold them.
269 // (Note that we only count operands that get tree nodes, and not
270 // others such as branch labels for a branch or switch instruction.)
272 // To do this efficiently, we'll walk all operands, build treeNodes
273 // for all appropriate operands and save them in an array. We then
274 // insert children at the end, creating list nodes where needed.
275 // As a performance optimization, allocate a child array only
276 // if a fixed array is too small.
279 InstrTreeNode **childArray =
280 (InstrTreeNode **)alloca(instr->getNumOperands()*sizeof(InstrTreeNode *));
283 // Walk the operands of the instruction
285 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
289 // Check if the operand is a data value, not an branch label, type,
290 // method or module. If the operand is an address type (i.e., label
291 // or method) that is used in an non-branching operation, e.g., `add'.
292 // that should be considered a data value.
294 // Check latter condition here just to simplify the next IF.
295 bool includeAddressOperand =
296 (isa<BasicBlock>(operand) || isa<Function>(operand))
297 && !instr->isTerminator();
299 if (includeAddressOperand || isa<Instruction>(operand) ||
300 isa<Constant>(operand) || isa<Argument>(operand) ||
301 isa<GlobalVariable>(operand))
303 // This operand is a data value
305 // An instruction that computes the incoming value is added as a
306 // child of the current instruction if:
307 // the value has only a single use
308 // AND both instructions are in the same basic block.
309 // AND the current instruction is not a PHI (because the incoming
310 // value is conceptually in a predecessor block,
311 // even though it may be in the same static block)
313 // (Note that if the value has only a single use (viz., `instr'),
314 // the def of the value can be safely moved just before instr
315 // and therefore it is safe to combine these two instructions.)
317 // In all other cases, the virtual register holding the value
318 // is used directly, i.e., made a child of the instruction node.
320 InstrTreeNode* opTreeNode;
321 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
322 cast<Instruction>(operand)->getParent() == instr->getParent() &&
323 instr->getOpcode() != Instruction::PHINode &&
324 instr->getOpcode() != Instruction::Call)
326 // Recursively create a treeNode for it.
327 opTreeNode = buildTreeForInstruction((Instruction*)operand);
329 else if (Constant *CPV = dyn_cast<Constant>(operand))
331 // Create a leaf node for a constant
332 opTreeNode = new ConstantNode(CPV);
336 // Create a leaf node for the virtual register
337 opTreeNode = new VRegNode(operand);
340 childArray[numChildren++] = opTreeNode;
344 //--------------------------------------------------------------------
345 // Add any selected operands as children in the tree.
346 // Certain instructions can have more than 2 in some instances (viz.,
347 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
348 // array or struct). Make the operands of every such instruction into
349 // a right-leaning binary tree with the operand nodes at the leaves
350 // and VRegList nodes as internal nodes.
351 //--------------------------------------------------------------------
353 InstrTreeNode *parent = treeNode;
357 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
358 assert(instrOpcode == Instruction::PHINode ||
359 instrOpcode == Instruction::Call ||
360 instrOpcode == Instruction::Load ||
361 instrOpcode == Instruction::Store ||
362 instrOpcode == Instruction::GetElementPtr);
365 // Insert the first child as a direct child
366 if (numChildren >= 1)
367 setLeftChild(parent, childArray[0]);
371 // Create a list node for children 2 .. N-1, if any
372 for (n = numChildren-1; n >= 2; n--)
374 // We have more than two children
375 InstrTreeNode *listNode = new VRegListNode();
376 setRightChild(parent, listNode);
377 setLeftChild(listNode, childArray[numChildren - n]);
381 // Now insert the last remaining child (if any).
382 if (numChildren >= 2)
385 setRightChild(parent, childArray[numChildren - 1]);