blob: 199ed6569be2e51af9ac6d46bda2f72978886d15 [file] [log] [blame]
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00001// $Id$
2//---------------------------------------------------------------------------
3// File:
4// InstrForest.cpp
5//
6// Purpose:
7// Convert SSA graph to instruction trees for instruction selection.
8//
9// Strategy:
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.
19//
20// History:
21// 6/28/01 - Vikram Adve - Created
22//
23//---------------------------------------------------------------------------
24
Chris Lattner942d99e2001-07-21 22:59:56 +000025#include "llvm/CodeGen/InstrForest.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000026#include "llvm/Method.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000027#include "llvm/iTerminators.h"
28#include "llvm/iMemory.h"
Chris Lattnerb00c5822001-10-02 03:41:24 +000029#include "llvm/iOther.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000030#include "llvm/ConstPoolVals.h"
31#include "llvm/BasicBlock.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000032#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner4ddb4c82001-09-12 01:28:49 +000033#include "llvm/Support/STLExtras.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000034
35//------------------------------------------------------------------------
36// class InstrTreeNode
37//------------------------------------------------------------------------
38
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000039void
40InstrTreeNode::dump(int dumpChildren, int indent) const
41{
Chris Lattnerd268ad62001-09-11 23:52:11 +000042 dumpNode(indent);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000043
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000044 if (dumpChildren)
45 {
46 if (LeftChild)
47 LeftChild->dump(dumpChildren, indent+1);
48 if (RightChild)
49 RightChild->dump(dumpChildren, indent+1);
50 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000051}
52
53
Chris Lattner4ddb4c82001-09-12 01:28:49 +000054InstructionNode::InstructionNode(Instruction* I)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000055 : InstrTreeNode(NTInstructionNode, I)
56{
Chris Lattner4ddb4c82001-09-12 01:28:49 +000057 opLabel = I->getOpcode();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000058
59 // Distinguish special cases of some instructions such as Ret and Br
60 //
Chris Lattnerb00c5822001-10-02 03:41:24 +000061 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000062 {
63 opLabel = RetValueOp; // ret(value) operation
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000064 }
Chris Lattnerb00c5822001-10-02 03:41:24 +000065 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000066 {
67 opLabel = BrCondOp; // br(cond) operation
68 }
69 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
70 {
71 opLabel = SetCCOp; // common label for all SetCC ops
72 }
73 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
74 {
75 opLabel = AllocaN; // Alloca(ptr, N) operation
76 }
77 else if ((opLabel == Instruction::Load ||
78 opLabel == Instruction::GetElementPtr) &&
79 ((MemAccessInst*)I)->getFirstOffsetIdx() > 0)
80 {
81 opLabel = opLabel + 100; // load/getElem with index vector
82 }
83 else if (opLabel == Instruction::Cast)
84 {
85 const Type *ITy = I->getType();
86 switch(ITy->getPrimitiveID())
87 {
88 case Type::BoolTyID: opLabel = ToBoolTy; break;
89 case Type::UByteTyID: opLabel = ToUByteTy; break;
90 case Type::SByteTyID: opLabel = ToSByteTy; break;
91 case Type::UShortTyID: opLabel = ToUShortTy; break;
92 case Type::ShortTyID: opLabel = ToShortTy; break;
93 case Type::UIntTyID: opLabel = ToUIntTy; break;
94 case Type::IntTyID: opLabel = ToIntTy; break;
95 case Type::ULongTyID: opLabel = ToULongTy; break;
96 case Type::LongTyID: opLabel = ToLongTy; break;
97 case Type::FloatTyID: opLabel = ToFloatTy; break;
98 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
99 case Type::ArrayTyID: opLabel = ToArrayTy; break;
100 case Type::PointerTyID: opLabel = ToPointerTy; break;
101 default:
102 // Just use `Cast' opcode otherwise. It's probably ignored.
103 break;
104 }
105 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000106}
107
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000108
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000109void
110InstructionNode::dumpNode(int indent) const
111{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000112 for (int i=0; i < indent; i++)
113 cout << " ";
114
115 cout << getInstruction()->getOpcodeName();
116
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000117 const vector<MachineInstr*> &mvec = getInstruction()->getMachineInstrVec();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000118 if (mvec.size() > 0)
119 cout << "\tMachine Instructions: ";
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000120 for (unsigned int i=0; i < mvec.size(); i++)
121 {
122 mvec[i]->dump(0);
123 if (i < mvec.size() - 1)
124 cout << "; ";
125 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000126
127 cout << endl;
128}
129
130
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000131void
132VRegListNode::dumpNode(int indent) const
133{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000134 for (int i=0; i < indent; i++)
135 cout << " ";
136
137 cout << "List" << endl;
138}
139
140
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000141void
142VRegNode::dumpNode(int indent) const
143{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000144 for (int i=0; i < indent; i++)
145 cout << " ";
146
147 cout << "VReg " << getValue() << "\t(type "
148 << (int) getValue()->getValueType() << ")" << endl;
149}
150
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000151void
152ConstantNode::dumpNode(int indent) const
153{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000154 for (int i=0; i < indent; i++)
155 cout << " ";
156
157 cout << "Constant " << getValue() << "\t(type "
158 << (int) getValue()->getValueType() << ")" << endl;
159}
160
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000161void
162LabelNode::dumpNode(int indent) const
163{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000164 for (int i=0; i < indent; i++)
165 cout << " ";
166
167 cout << "Label " << getValue() << endl;
168}
169
170//------------------------------------------------------------------------
171// class InstrForest
172//
173// A forest of instruction trees, usually for a single method.
174//------------------------------------------------------------------------
175
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000176InstrForest::InstrForest(Method *M)
177{
178 for (Method::inst_iterator I = M->inst_begin(); I != M->inst_end(); ++I)
179 this->buildTreeForInstruction(*I);
180}
181
182InstrForest::~InstrForest()
183{
184 for (hash_map<const Instruction*, InstructionNode*>:: iterator I = begin();
185 I != end(); ++I)
Chris Lattner921b5e12001-09-18 17:02:42 +0000186 delete (*I).second;
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000187}
188
189void
190InstrForest::dump() const
191{
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000192 for (hash_set<InstructionNode*>::const_iterator I = treeRoots.begin();
193 I != treeRoots.end(); ++I)
194 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000195}
196
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000197inline void
198InstrForest::noteTreeNodeForInstr(Instruction *instr,
199 InstructionNode *treeNode)
200{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000201 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
202 (*this)[instr] = treeNode;
203 treeRoots.insert(treeNode); // mark node as root of a new tree
204}
205
206
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000207inline void
208InstrForest::setLeftChild(InstrTreeNode *Par, InstrTreeNode *Chld)
209{
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000210 Par->LeftChild = Chld;
211 Chld->Parent = Par;
212 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
213 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000214}
215
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000216inline void
217InstrForest::setRightChild(InstrTreeNode *Par, InstrTreeNode *Chld)
218{
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000219 Par->RightChild = Chld;
220 Chld->Parent = Par;
221 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
222 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000223}
224
225
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000226InstructionNode*
227InstrForest::buildTreeForInstruction(Instruction *instr)
228{
229 InstructionNode *treeNode = getTreeNodeForInstr(instr);
230 if (treeNode)
231 {
232 // treeNode has already been constructed for this instruction
233 assert(treeNode->getInstruction() == instr);
234 return treeNode;
235 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000236
237 // Otherwise, create a new tree node for this instruction.
238 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000239 treeNode = new InstructionNode(instr);
240 noteTreeNodeForInstr(instr, treeNode);
241
242 if (instr->getOpcode() == Instruction::Call)
243 { // Operands of call instruction
244 return treeNode;
245 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000246
247 // If the instruction has more than 2 instruction operands,
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000248 // then we need to create artificial list nodes to hold them.
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000249 // (Note that we only count operands that get tree nodes, and not
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000250 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000251 //
252 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000253 // for all appropriate operands and save them in an array. We then
254 // insert children at the end, creating list nodes where needed.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000255 // As a performance optimization, allocate a child array only
256 // if a fixed array is too small.
257 //
258 int numChildren = 0;
259 const unsigned int MAX_CHILD = 8;
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000260 static InstrTreeNode *fixedChildArray[MAX_CHILD];
261 InstrTreeNode **childArray =
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000262 (instr->getNumOperands() > MAX_CHILD)
263 ? new (InstrTreeNode*)[instr->getNumOperands()] : fixedChildArray;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000264
265 //
266 // Walk the operands of the instruction
267 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000268 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
269 {
270 Value* operand = *O;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000271
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000272 // 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.
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000276
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000277 // Check latter condition here just to simplify the next IF.
278 bool includeAddressOperand =
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000279 (isa<BasicBlock>(operand) || isa<Method>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000280 && !instr->isTerminator();
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000281
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000282 if (includeAddressOperand || isa<Instruction>(operand) ||
283 isa<ConstPoolVal>(operand) || isa<MethodArgument>(operand) ||
284 isa<GlobalVariable>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000285 {
286 // This operand is a data value
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000287
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000288 // 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)
295 //
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.)
299 //
300 // In all other cases, the virtual register holding the value
301 // is used directly, i.e., made a child of the instruction node.
302 //
303 InstrTreeNode* opTreeNode;
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000304 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
305 cast<Instruction>(operand)->getParent() == instr->getParent() &&
Chris Lattnerb00c5822001-10-02 03:41:24 +0000306 !isa<PHINode>(instr) &&
Vikram S. Adve64c2ced2001-09-30 23:45:08 +0000307 instr->getOpcode() != Instruction::Call)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000308 {
309 // Recursively create a treeNode for it.
310 opTreeNode = buildTreeForInstruction((Instruction*)operand);
311 }
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000312 else if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000313 {
314 // Create a leaf node for a constant
315 opTreeNode = new ConstantNode(CPV);
316 }
317 else
318 {
319 // Create a leaf node for the virtual register
320 opTreeNode = new VRegNode(operand);
321 }
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000322
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000323 childArray[numChildren++] = opTreeNode;
324 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000325 }
326
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 //--------------------------------------------------------------------
335
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000336 InstrTreeNode *parent = treeNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000337
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000338 if (numChildren > 2)
339 {
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);
346 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000347
348 // Insert the first child as a direct child
349 if (numChildren >= 1)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000350 setLeftChild(parent, childArray[0]);
351
352 int n;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000353
354 // Create a list node for children 2 .. N-1, if any
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000355 for (n = numChildren-1; n >= 2; n--)
356 {
357 // We have more than two children
358 InstrTreeNode *listNode = new VRegListNode();
359 setRightChild(parent, listNode);
360 setLeftChild(listNode, childArray[numChildren - n]);
361 parent = listNode;
362 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000363
364 // Now insert the last remaining child (if any).
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000365 if (numChildren >= 2)
366 {
367 assert(n == 1);
368 setRightChild(parent, childArray[numChildren - 1]);
369 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000370
371 if (childArray != fixedChildArray)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000372 delete [] childArray;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000373
374 return treeNode;
375}
376