blob: 20cbe8d71bfa7ba086aedcdc32b0deee27f88797 [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 Lattner7061dc52001-12-03 18:02:31 +000029#include "llvm/iPHINode.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000030#include "llvm/ConstantVals.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000031#include "llvm/BasicBlock.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000032#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000033#include "Support/STLExtras.h"
Chris Lattner697954c2002-01-20 22:54:45 +000034#include <iostream>
35using std::cerr;
36using std::vector;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000037
38//------------------------------------------------------------------------
39// class InstrTreeNode
40//------------------------------------------------------------------------
41
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000042void
43InstrTreeNode::dump(int dumpChildren, int indent) const
44{
Chris Lattnerd268ad62001-09-11 23:52:11 +000045 dumpNode(indent);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000046
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000047 if (dumpChildren)
48 {
49 if (LeftChild)
50 LeftChild->dump(dumpChildren, indent+1);
51 if (RightChild)
52 RightChild->dump(dumpChildren, indent+1);
53 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000054}
55
56
Chris Lattner4ddb4c82001-09-12 01:28:49 +000057InstructionNode::InstructionNode(Instruction* I)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000058 : InstrTreeNode(NTInstructionNode, I)
59{
Chris Lattner4ddb4c82001-09-12 01:28:49 +000060 opLabel = I->getOpcode();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000061
62 // Distinguish special cases of some instructions such as Ret and Br
63 //
Chris Lattnerb00c5822001-10-02 03:41:24 +000064 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000065 {
66 opLabel = RetValueOp; // ret(value) operation
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000067 }
Chris Lattnerb00c5822001-10-02 03:41:24 +000068 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000069 {
70 opLabel = BrCondOp; // br(cond) operation
71 }
72 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
73 {
74 opLabel = SetCCOp; // common label for all SetCC ops
75 }
76 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
77 {
78 opLabel = AllocaN; // Alloca(ptr, N) operation
79 }
80 else if ((opLabel == Instruction::Load ||
81 opLabel == Instruction::GetElementPtr) &&
Chris Lattner65ea1712001-11-14 11:27:58 +000082 cast<MemAccessInst>(I)->hasIndices())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000083 {
84 opLabel = opLabel + 100; // load/getElem with index vector
85 }
Vikram S. Advebe495262001-11-08 04:47:06 +000086 else if (opLabel == Instruction::And ||
87 opLabel == Instruction::Or ||
88 opLabel == Instruction::Xor ||
89 opLabel == Instruction::Not)
90 {
91 // Distinguish bitwise operators from logical operators!
92 if (I->getType() != Type::BoolTy)
93 opLabel = opLabel + 100; // bitwise operator
94 }
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000095 else if (opLabel == Instruction::Cast)
96 {
97 const Type *ITy = I->getType();
98 switch(ITy->getPrimitiveID())
99 {
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;
113 default:
114 // Just use `Cast' opcode otherwise. It's probably ignored.
115 break;
116 }
117 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000118}
119
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000120
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000121void
122InstructionNode::dumpNode(int indent) const
123{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000124 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000125 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000126
Chris Lattner697954c2002-01-20 22:54:45 +0000127 cerr << getInstruction()->getOpcodeName();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000128
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000129 const vector<MachineInstr*> &mvec = getInstruction()->getMachineInstrVec();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000130 if (mvec.size() > 0)
Chris Lattner697954c2002-01-20 22:54:45 +0000131 cerr << "\tMachine Instructions: ";
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000132 for (unsigned int i=0; i < mvec.size(); i++)
133 {
134 mvec[i]->dump(0);
135 if (i < mvec.size() - 1)
Chris Lattner697954c2002-01-20 22:54:45 +0000136 cerr << "; ";
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000137 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000138
Chris Lattner697954c2002-01-20 22:54:45 +0000139 cerr << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000140}
141
142
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000143void
144VRegListNode::dumpNode(int indent) const
145{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000146 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000147 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000148
Chris Lattner697954c2002-01-20 22:54:45 +0000149 cerr << "List" << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000150}
151
152
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000153void
154VRegNode::dumpNode(int indent) const
155{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000156 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000157 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000158
Chris Lattner697954c2002-01-20 22:54:45 +0000159 cerr << "VReg " << getValue() << "\t(type "
160 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000161}
162
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000163void
164ConstantNode::dumpNode(int indent) const
165{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000166 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000167 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000168
Chris Lattner697954c2002-01-20 22:54:45 +0000169 cerr << "Constant " << getValue() << "\t(type "
170 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000171}
172
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000173void
174LabelNode::dumpNode(int indent) const
175{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000176 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000177 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000178
Chris Lattner697954c2002-01-20 22:54:45 +0000179 cerr << "Label " << getValue() << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000180}
181
182//------------------------------------------------------------------------
183// class InstrForest
184//
185// A forest of instruction trees, usually for a single method.
186//------------------------------------------------------------------------
187
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000188InstrForest::InstrForest(Method *M)
189{
190 for (Method::inst_iterator I = M->inst_begin(); I != M->inst_end(); ++I)
191 this->buildTreeForInstruction(*I);
192}
193
194InstrForest::~InstrForest()
195{
Chris Lattner697954c2002-01-20 22:54:45 +0000196 for (std::hash_map<const Instruction*,InstructionNode*>::iterator I = begin();
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000197 I != end(); ++I)
Chris Lattner921b5e12001-09-18 17:02:42 +0000198 delete (*I).second;
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000199}
200
201void
202InstrForest::dump() const
203{
Chris Lattner697954c2002-01-20 22:54:45 +0000204 for (std::hash_set<InstructionNode*>::const_iterator I = treeRoots.begin();
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000205 I != treeRoots.end(); ++I)
206 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000207}
208
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000209inline void
210InstrForest::noteTreeNodeForInstr(Instruction *instr,
211 InstructionNode *treeNode)
212{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000213 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
214 (*this)[instr] = treeNode;
215 treeRoots.insert(treeNode); // mark node as root of a new tree
216}
217
218
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000219inline void
220InstrForest::setLeftChild(InstrTreeNode *Par, InstrTreeNode *Chld)
221{
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000222 Par->LeftChild = Chld;
223 Chld->Parent = Par;
224 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
225 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000226}
227
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000228inline void
229InstrForest::setRightChild(InstrTreeNode *Par, InstrTreeNode *Chld)
230{
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000231 Par->RightChild = Chld;
232 Chld->Parent = Par;
233 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
234 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000235}
236
237
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000238InstructionNode*
239InstrForest::buildTreeForInstruction(Instruction *instr)
240{
241 InstructionNode *treeNode = getTreeNodeForInstr(instr);
242 if (treeNode)
243 {
244 // treeNode has already been constructed for this instruction
245 assert(treeNode->getInstruction() == instr);
246 return treeNode;
247 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000248
249 // Otherwise, create a new tree node for this instruction.
250 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000251 treeNode = new InstructionNode(instr);
252 noteTreeNodeForInstr(instr, treeNode);
253
254 if (instr->getOpcode() == Instruction::Call)
255 { // Operands of call instruction
256 return treeNode;
257 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000258
259 // If the instruction has more than 2 instruction operands,
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000260 // then we need to create artificial list nodes to hold them.
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000261 // (Note that we only count operands that get tree nodes, and not
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000262 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000263 //
264 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000265 // for all appropriate operands and save them in an array. We then
266 // insert children at the end, creating list nodes where needed.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000267 // As a performance optimization, allocate a child array only
268 // if a fixed array is too small.
269 //
270 int numChildren = 0;
271 const unsigned int MAX_CHILD = 8;
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000272 static InstrTreeNode *fixedChildArray[MAX_CHILD];
273 InstrTreeNode **childArray =
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000274 (instr->getNumOperands() > MAX_CHILD)
275 ? new (InstrTreeNode*)[instr->getNumOperands()] : fixedChildArray;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000276
277 //
278 // Walk the operands of the instruction
279 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000280 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
281 {
282 Value* operand = *O;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000283
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000284 // Check if the operand is a data value, not an branch label, type,
285 // method or module. If the operand is an address type (i.e., label
286 // or method) that is used in an non-branching operation, e.g., `add'.
287 // that should be considered a data value.
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000288
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000289 // Check latter condition here just to simplify the next IF.
290 bool includeAddressOperand =
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000291 (isa<BasicBlock>(operand) || isa<Method>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000292 && !instr->isTerminator();
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000293
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000294 if (includeAddressOperand || isa<Instruction>(operand) ||
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000295 isa<Constant>(operand) || isa<MethodArgument>(operand) ||
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000296 isa<GlobalVariable>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000297 {
298 // This operand is a data value
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000299
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000300 // An instruction that computes the incoming value is added as a
301 // child of the current instruction if:
302 // the value has only a single use
303 // AND both instructions are in the same basic block.
304 // AND the current instruction is not a PHI (because the incoming
305 // value is conceptually in a predecessor block,
306 // even though it may be in the same static block)
307 //
308 // (Note that if the value has only a single use (viz., `instr'),
309 // the def of the value can be safely moved just before instr
310 // and therefore it is safe to combine these two instructions.)
311 //
312 // In all other cases, the virtual register holding the value
313 // is used directly, i.e., made a child of the instruction node.
314 //
315 InstrTreeNode* opTreeNode;
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000316 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
317 cast<Instruction>(operand)->getParent() == instr->getParent() &&
Chris Lattnerb00c5822001-10-02 03:41:24 +0000318 !isa<PHINode>(instr) &&
Vikram S. Adve64c2ced2001-09-30 23:45:08 +0000319 instr->getOpcode() != Instruction::Call)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000320 {
321 // Recursively create a treeNode for it.
322 opTreeNode = buildTreeForInstruction((Instruction*)operand);
323 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000324 else if (Constant *CPV = dyn_cast<Constant>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000325 {
326 // Create a leaf node for a constant
327 opTreeNode = new ConstantNode(CPV);
328 }
329 else
330 {
331 // Create a leaf node for the virtual register
332 opTreeNode = new VRegNode(operand);
333 }
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000334
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000335 childArray[numChildren++] = opTreeNode;
336 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000337 }
338
339 //--------------------------------------------------------------------
340 // Add any selected operands as children in the tree.
341 // Certain instructions can have more than 2 in some instances (viz.,
342 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
343 // array or struct). Make the operands of every such instruction into
344 // a right-leaning binary tree with the operand nodes at the leaves
345 // and VRegList nodes as internal nodes.
346 //--------------------------------------------------------------------
347
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000348 InstrTreeNode *parent = treeNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000349
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000350 if (numChildren > 2)
351 {
352 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
353 assert(instrOpcode == Instruction::PHINode ||
354 instrOpcode == Instruction::Call ||
355 instrOpcode == Instruction::Load ||
356 instrOpcode == Instruction::Store ||
357 instrOpcode == Instruction::GetElementPtr);
358 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000359
360 // Insert the first child as a direct child
361 if (numChildren >= 1)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000362 setLeftChild(parent, childArray[0]);
363
364 int n;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000365
366 // Create a list node for children 2 .. N-1, if any
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000367 for (n = numChildren-1; n >= 2; n--)
368 {
369 // We have more than two children
370 InstrTreeNode *listNode = new VRegListNode();
371 setRightChild(parent, listNode);
372 setLeftChild(listNode, childArray[numChildren - n]);
373 parent = listNode;
374 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000375
376 // Now insert the last remaining child (if any).
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000377 if (numChildren >= 2)
378 {
379 assert(n == 1);
380 setRightChild(parent, childArray[numChildren - 1]);
381 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000382
383 if (childArray != fixedChildArray)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000384 delete [] childArray;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000385
386 return treeNode;
387}
388