blob: f7091a29c61bb4a12f0c0c96ebafca5b9fb8bf53 [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"
29#include "llvm/ConstPoolVals.h"
30#include "llvm/BasicBlock.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000031#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner4ddb4c82001-09-12 01:28:49 +000032#include "llvm/Support/STLExtras.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000033
34//------------------------------------------------------------------------
35// class InstrTreeNode
36//------------------------------------------------------------------------
37
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000038void
39InstrTreeNode::dump(int dumpChildren, int indent) const
40{
Chris Lattnerd268ad62001-09-11 23:52:11 +000041 dumpNode(indent);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000042
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000043 if (dumpChildren)
44 {
45 if (LeftChild)
46 LeftChild->dump(dumpChildren, indent+1);
47 if (RightChild)
48 RightChild->dump(dumpChildren, indent+1);
49 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000050}
51
52
Chris Lattner4ddb4c82001-09-12 01:28:49 +000053InstructionNode::InstructionNode(Instruction* I)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000054 : InstrTreeNode(NTInstructionNode, I)
55{
Chris Lattner4ddb4c82001-09-12 01:28:49 +000056 opLabel = I->getOpcode();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000057
58 // Distinguish special cases of some instructions such as Ret and Br
59 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000060 if (opLabel == Instruction::Ret && ((ReturnInst*)I)->getReturnValue())
61 {
62 opLabel = RetValueOp; // ret(value) operation
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000063 }
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000064 else if (opLabel == Instruction::Br && ! ((BranchInst*)I)->isUnconditional())
65 {
66 opLabel = BrCondOp; // br(cond) operation
67 }
68 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
69 {
70 opLabel = SetCCOp; // common label for all SetCC ops
71 }
72 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
73 {
74 opLabel = AllocaN; // Alloca(ptr, N) operation
75 }
76 else if ((opLabel == Instruction::Load ||
77 opLabel == Instruction::GetElementPtr) &&
78 ((MemAccessInst*)I)->getFirstOffsetIdx() > 0)
79 {
80 opLabel = opLabel + 100; // load/getElem with index vector
81 }
82 else if (opLabel == Instruction::Cast)
83 {
84 const Type *ITy = I->getType();
85 switch(ITy->getPrimitiveID())
86 {
87 case Type::BoolTyID: opLabel = ToBoolTy; break;
88 case Type::UByteTyID: opLabel = ToUByteTy; break;
89 case Type::SByteTyID: opLabel = ToSByteTy; break;
90 case Type::UShortTyID: opLabel = ToUShortTy; break;
91 case Type::ShortTyID: opLabel = ToShortTy; break;
92 case Type::UIntTyID: opLabel = ToUIntTy; break;
93 case Type::IntTyID: opLabel = ToIntTy; break;
94 case Type::ULongTyID: opLabel = ToULongTy; break;
95 case Type::LongTyID: opLabel = ToLongTy; break;
96 case Type::FloatTyID: opLabel = ToFloatTy; break;
97 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
98 case Type::ArrayTyID: opLabel = ToArrayTy; break;
99 case Type::PointerTyID: opLabel = ToPointerTy; break;
100 default:
101 // Just use `Cast' opcode otherwise. It's probably ignored.
102 break;
103 }
104 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000105}
106
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000107
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000108void
109InstructionNode::dumpNode(int indent) const
110{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000111 for (int i=0; i < indent; i++)
112 cout << " ";
113
114 cout << getInstruction()->getOpcodeName();
115
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000116 const vector<MachineInstr*> &mvec = getInstruction()->getMachineInstrVec();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000117 if (mvec.size() > 0)
118 cout << "\tMachine Instructions: ";
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000119 for (unsigned int i=0; i < mvec.size(); i++)
120 {
121 mvec[i]->dump(0);
122 if (i < mvec.size() - 1)
123 cout << "; ";
124 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000125
126 cout << endl;
127}
128
129
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000130void
131VRegListNode::dumpNode(int indent) const
132{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000133 for (int i=0; i < indent; i++)
134 cout << " ";
135
136 cout << "List" << endl;
137}
138
139
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000140void
141VRegNode::dumpNode(int indent) const
142{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000143 for (int i=0; i < indent; i++)
144 cout << " ";
145
146 cout << "VReg " << getValue() << "\t(type "
147 << (int) getValue()->getValueType() << ")" << endl;
148}
149
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000150void
151ConstantNode::dumpNode(int indent) const
152{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000153 for (int i=0; i < indent; i++)
154 cout << " ";
155
156 cout << "Constant " << getValue() << "\t(type "
157 << (int) getValue()->getValueType() << ")" << endl;
158}
159
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000160void
161LabelNode::dumpNode(int indent) const
162{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000163 for (int i=0; i < indent; i++)
164 cout << " ";
165
166 cout << "Label " << getValue() << endl;
167}
168
169//------------------------------------------------------------------------
170// class InstrForest
171//
172// A forest of instruction trees, usually for a single method.
173//------------------------------------------------------------------------
174
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000175InstrForest::InstrForest(Method *M)
176{
177 for (Method::inst_iterator I = M->inst_begin(); I != M->inst_end(); ++I)
178 this->buildTreeForInstruction(*I);
179}
180
181InstrForest::~InstrForest()
182{
183 for (hash_map<const Instruction*, InstructionNode*>:: iterator I = begin();
184 I != end(); ++I)
Chris Lattner921b5e12001-09-18 17:02:42 +0000185 delete (*I).second;
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000186}
187
188void
189InstrForest::dump() const
190{
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000191 for (hash_set<InstructionNode*>::const_iterator I = treeRoots.begin();
192 I != treeRoots.end(); ++I)
193 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000194}
195
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000196inline void
197InstrForest::noteTreeNodeForInstr(Instruction *instr,
198 InstructionNode *treeNode)
199{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000200 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
201 (*this)[instr] = treeNode;
202 treeRoots.insert(treeNode); // mark node as root of a new tree
203}
204
205
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000206inline void
207InstrForest::setLeftChild(InstrTreeNode *Par, InstrTreeNode *Chld)
208{
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000209 Par->LeftChild = Chld;
210 Chld->Parent = Par;
211 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
212 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000213}
214
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000215inline void
216InstrForest::setRightChild(InstrTreeNode *Par, InstrTreeNode *Chld)
217{
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000218 Par->RightChild = Chld;
219 Chld->Parent = Par;
220 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
221 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000222}
223
224
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000225InstructionNode*
226InstrForest::buildTreeForInstruction(Instruction *instr)
227{
228 InstructionNode *treeNode = getTreeNodeForInstr(instr);
229 if (treeNode)
230 {
231 // treeNode has already been constructed for this instruction
232 assert(treeNode->getInstruction() == instr);
233 return treeNode;
234 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000235
236 // Otherwise, create a new tree node for this instruction.
237 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000238 treeNode = new InstructionNode(instr);
239 noteTreeNodeForInstr(instr, treeNode);
240
241 if (instr->getOpcode() == Instruction::Call)
242 { // Operands of call instruction
243 return treeNode;
244 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000245
246 // If the instruction has more than 2 instruction operands,
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000247 // then we need to create artificial list nodes to hold them.
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000248 // (Note that we only count operands that get tree nodes, and not
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000249 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000250 //
251 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000252 // for all appropriate operands and save them in an array. We then
253 // insert children at the end, creating list nodes where needed.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000254 // As a performance optimization, allocate a child array only
255 // if a fixed array is too small.
256 //
257 int numChildren = 0;
258 const unsigned int MAX_CHILD = 8;
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000259 static InstrTreeNode *fixedChildArray[MAX_CHILD];
260 InstrTreeNode **childArray =
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000261 (instr->getNumOperands() > MAX_CHILD)
262 ? new (InstrTreeNode*)[instr->getNumOperands()] : fixedChildArray;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000263
264 //
265 // Walk the operands of the instruction
266 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000267 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
268 {
269 Value* operand = *O;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000270
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000271 // Check if the operand is a data value, not an branch label, type,
272 // method or module. If the operand is an address type (i.e., label
273 // or method) that is used in an non-branching operation, e.g., `add'.
274 // that should be considered a data value.
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000275
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000276 // Check latter condition here just to simplify the next IF.
277 bool includeAddressOperand =
278 (operand->isBasicBlock() || operand->isMethod())
279 && !instr->isTerminator();
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000280
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000281 if (includeAddressOperand || operand->isInstruction() ||
282 operand->isConstant() || operand->isMethodArgument() ||
283 operand->isGlobal())
284 {
285 // This operand is a data value
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000286
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000287 // An instruction that computes the incoming value is added as a
288 // child of the current instruction if:
289 // the value has only a single use
290 // AND both instructions are in the same basic block.
291 // AND the current instruction is not a PHI (because the incoming
292 // value is conceptually in a predecessor block,
293 // even though it may be in the same static block)
294 //
295 // (Note that if the value has only a single use (viz., `instr'),
296 // the def of the value can be safely moved just before instr
297 // and therefore it is safe to combine these two instructions.)
298 //
299 // In all other cases, the virtual register holding the value
300 // is used directly, i.e., made a child of the instruction node.
301 //
302 InstrTreeNode* opTreeNode;
303 if (operand->isInstruction() && operand->use_size() == 1 &&
304 ((Instruction*)operand)->getParent() == instr->getParent() &&
305 ! instr->isPHINode() &&
306 ! instr->getOpcode() == Instruction::Call)
307 {
308 // Recursively create a treeNode for it.
309 opTreeNode = buildTreeForInstruction((Instruction*)operand);
310 }
311 else if (ConstPoolVal *CPV = operand->castConstant())
312 {
313 // Create a leaf node for a constant
314 opTreeNode = new ConstantNode(CPV);
315 }
316 else
317 {
318 // Create a leaf node for the virtual register
319 opTreeNode = new VRegNode(operand);
320 }
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000321
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000322 childArray[numChildren++] = opTreeNode;
323 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000324 }
325
326 //--------------------------------------------------------------------
327 // Add any selected operands as children in the tree.
328 // Certain instructions can have more than 2 in some instances (viz.,
329 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
330 // array or struct). Make the operands of every such instruction into
331 // a right-leaning binary tree with the operand nodes at the leaves
332 // and VRegList nodes as internal nodes.
333 //--------------------------------------------------------------------
334
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000335 InstrTreeNode *parent = treeNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000336
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000337 if (numChildren > 2)
338 {
339 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
340 assert(instrOpcode == Instruction::PHINode ||
341 instrOpcode == Instruction::Call ||
342 instrOpcode == Instruction::Load ||
343 instrOpcode == Instruction::Store ||
344 instrOpcode == Instruction::GetElementPtr);
345 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000346
347 // Insert the first child as a direct child
348 if (numChildren >= 1)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000349 setLeftChild(parent, childArray[0]);
350
351 int n;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000352
353 // Create a list node for children 2 .. N-1, if any
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000354 for (n = numChildren-1; n >= 2; n--)
355 {
356 // We have more than two children
357 InstrTreeNode *listNode = new VRegListNode();
358 setRightChild(parent, listNode);
359 setLeftChild(listNode, childArray[numChildren - n]);
360 parent = listNode;
361 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000362
363 // Now insert the last remaining child (if any).
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000364 if (numChildren >= 2)
365 {
366 assert(n == 1);
367 setRightChild(parent, childArray[numChildren - 1]);
368 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000369
370 if (childArray != fixedChildArray)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000371 delete [] childArray;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000372
373 return treeNode;
374}
375