blob: 660f3112d72a68f396c4151065d1a3cc15b338e0 [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"
Chris Lattnera8bbb6b2002-02-03 07:31:41 +000026#include "llvm/CodeGen/MachineCodeForInstruction.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000027#include "llvm/Method.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000028#include "llvm/iTerminators.h"
29#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000030#include "llvm/iPHINode.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000031#include "llvm/ConstantVals.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000032#include "llvm/BasicBlock.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000033#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000034#include "Support/STLExtras.h"
Chris Lattner697954c2002-01-20 22:54:45 +000035#include <iostream>
36using std::cerr;
37using std::vector;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000038
39//------------------------------------------------------------------------
40// class InstrTreeNode
41//------------------------------------------------------------------------
42
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000043void
44InstrTreeNode::dump(int dumpChildren, int indent) const
45{
Chris Lattnerd268ad62001-09-11 23:52:11 +000046 dumpNode(indent);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000047
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000048 if (dumpChildren)
49 {
50 if (LeftChild)
51 LeftChild->dump(dumpChildren, indent+1);
52 if (RightChild)
53 RightChild->dump(dumpChildren, indent+1);
54 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000055}
56
57
Chris Lattner4ddb4c82001-09-12 01:28:49 +000058InstructionNode::InstructionNode(Instruction* I)
Vikram S. Adved95919c2002-03-24 03:24:00 +000059 : InstrTreeNode(NTInstructionNode, I),
60 codeIsFoldedIntoParent(false)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000061{
Chris Lattner4ddb4c82001-09-12 01:28:49 +000062 opLabel = I->getOpcode();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000063
64 // Distinguish special cases of some instructions such as Ret and Br
65 //
Chris Lattnerb00c5822001-10-02 03:41:24 +000066 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000067 {
68 opLabel = RetValueOp; // ret(value) operation
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000069 }
Chris Lattnerb00c5822001-10-02 03:41:24 +000070 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000071 {
72 opLabel = BrCondOp; // br(cond) operation
73 }
74 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
75 {
76 opLabel = SetCCOp; // common label for all SetCC ops
77 }
78 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
79 {
80 opLabel = AllocaN; // Alloca(ptr, N) operation
81 }
82 else if ((opLabel == Instruction::Load ||
83 opLabel == Instruction::GetElementPtr) &&
Chris Lattner65ea1712001-11-14 11:27:58 +000084 cast<MemAccessInst>(I)->hasIndices())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000085 {
86 opLabel = opLabel + 100; // load/getElem with index vector
87 }
Vikram S. Advebe495262001-11-08 04:47:06 +000088 else if (opLabel == Instruction::And ||
89 opLabel == Instruction::Or ||
90 opLabel == Instruction::Xor ||
91 opLabel == Instruction::Not)
92 {
93 // Distinguish bitwise operators from logical operators!
94 if (I->getType() != Type::BoolTy)
95 opLabel = opLabel + 100; // bitwise operator
96 }
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000097 else if (opLabel == Instruction::Cast)
98 {
99 const Type *ITy = I->getType();
100 switch(ITy->getPrimitiveID())
101 {
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;
115 default:
116 // Just use `Cast' opcode otherwise. It's probably ignored.
117 break;
118 }
119 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000120}
121
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000122
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000123void
124InstructionNode::dumpNode(int indent) const
125{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000126 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000127 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000128
Chris Lattner697954c2002-01-20 22:54:45 +0000129 cerr << getInstruction()->getOpcodeName();
Chris Lattnera8bbb6b2002-02-03 07:31:41 +0000130 const MachineCodeForInstruction &mvec =
131 MachineCodeForInstruction::get(getInstruction());
132
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000133 if (mvec.size() > 0)
Chris Lattner697954c2002-01-20 22:54:45 +0000134 cerr << "\tMachine Instructions: ";
Chris Lattnera8bbb6b2002-02-03 07:31:41 +0000135
136 for (unsigned int i=0; i < mvec.size(); ++i) {
137 mvec[i]->dump(0);
138 if (i < mvec.size() - 1)
139 cerr << "; ";
140 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000141
Chris Lattner697954c2002-01-20 22:54:45 +0000142 cerr << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000143}
144
145
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000146void
147VRegListNode::dumpNode(int indent) const
148{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000149 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000150 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000151
Chris Lattner697954c2002-01-20 22:54:45 +0000152 cerr << "List" << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000153}
154
155
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000156void
157VRegNode::dumpNode(int indent) const
158{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000159 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000160 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000161
Chris Lattner697954c2002-01-20 22:54:45 +0000162 cerr << "VReg " << getValue() << "\t(type "
163 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000164}
165
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000166void
167ConstantNode::dumpNode(int indent) const
168{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000169 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000170 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000171
Chris Lattner697954c2002-01-20 22:54:45 +0000172 cerr << "Constant " << getValue() << "\t(type "
173 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000174}
175
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000176void
177LabelNode::dumpNode(int indent) const
178{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000179 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000180 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000181
Chris Lattner697954c2002-01-20 22:54:45 +0000182 cerr << "Label " << getValue() << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000183}
184
185//------------------------------------------------------------------------
186// class InstrForest
187//
188// A forest of instruction trees, usually for a single method.
189//------------------------------------------------------------------------
190
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000191InstrForest::InstrForest(Method *M)
192{
Chris Lattner221d6882002-02-12 21:07:25 +0000193 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
194 BasicBlock *BB = *MI;
195 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
196 buildTreeForInstruction(*I);
197 }
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000198}
199
200InstrForest::~InstrForest()
201{
Vikram S. Adved95919c2002-03-24 03:24:00 +0000202 for (std::hash_map<const Instruction*,InstructionNode*>::iterator I=begin();
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000203 I != end(); ++I)
Chris Lattner221d6882002-02-12 21:07:25 +0000204 delete I->second;
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000205}
206
207void
208InstrForest::dump() const
209{
Vikram S. Adved95919c2002-03-24 03:24:00 +0000210 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000211 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000212}
213
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000214inline void
Vikram S. Adved95919c2002-03-24 03:24:00 +0000215InstrForest::eraseRoot(InstructionNode* node)
216{
217 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
218 RI != RE; ++RI)
219 if (*RI == node)
220 treeRoots.erase(RI.base()-1);
221}
222
223inline void
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000224InstrForest::noteTreeNodeForInstr(Instruction *instr,
225 InstructionNode *treeNode)
226{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000227 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
228 (*this)[instr] = treeNode;
Vikram S. Adved95919c2002-03-24 03:24:00 +0000229 treeRoots.push_back(treeNode); // mark node as root of a new tree
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000230}
231
232
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000233inline void
Vikram S. Adved95919c2002-03-24 03:24:00 +0000234InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000235{
Vikram S. Adved95919c2002-03-24 03:24:00 +0000236 parent->LeftChild = child;
237 child->Parent = parent;
238 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
239 eraseRoot((InstructionNode*) child); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000240}
241
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000242inline void
Vikram S. Adved95919c2002-03-24 03:24:00 +0000243InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000244{
Vikram S. Adved95919c2002-03-24 03:24:00 +0000245 parent->RightChild = child;
246 child->Parent = parent;
247 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
248 eraseRoot((InstructionNode*) child); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000249}
250
251
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000252InstructionNode*
253InstrForest::buildTreeForInstruction(Instruction *instr)
254{
255 InstructionNode *treeNode = getTreeNodeForInstr(instr);
256 if (treeNode)
257 {
258 // treeNode has already been constructed for this instruction
259 assert(treeNode->getInstruction() == instr);
260 return treeNode;
261 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000262
263 // Otherwise, create a new tree node for this instruction.
264 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000265 treeNode = new InstructionNode(instr);
266 noteTreeNodeForInstr(instr, treeNode);
267
268 if (instr->getOpcode() == Instruction::Call)
269 { // Operands of call instruction
270 return treeNode;
271 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000272
273 // If the instruction has more than 2 instruction operands,
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000274 // then we need to create artificial list nodes to hold them.
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000275 // (Note that we only count operands that get tree nodes, and not
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000276 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000277 //
278 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000279 // for all appropriate operands and save them in an array. We then
280 // insert children at the end, creating list nodes where needed.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000281 // As a performance optimization, allocate a child array only
282 // if a fixed array is too small.
283 //
284 int numChildren = 0;
285 const unsigned int MAX_CHILD = 8;
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000286 static InstrTreeNode *fixedChildArray[MAX_CHILD];
287 InstrTreeNode **childArray =
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000288 (instr->getNumOperands() > MAX_CHILD)
289 ? new (InstrTreeNode*)[instr->getNumOperands()] : fixedChildArray;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000290
291 //
292 // Walk the operands of the instruction
293 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000294 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
295 {
296 Value* operand = *O;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000297
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000298 // 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.
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000302
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000303 // Check latter condition here just to simplify the next IF.
304 bool includeAddressOperand =
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000305 (isa<BasicBlock>(operand) || isa<Method>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000306 && !instr->isTerminator();
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000307
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000308 if (includeAddressOperand || isa<Instruction>(operand) ||
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000309 isa<Constant>(operand) || isa<MethodArgument>(operand) ||
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000310 isa<GlobalVariable>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000311 {
312 // This operand is a data value
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000313
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000314 // 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)
321 //
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.)
325 //
326 // In all other cases, the virtual register holding the value
327 // is used directly, i.e., made a child of the instruction node.
328 //
329 InstrTreeNode* opTreeNode;
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000330 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
331 cast<Instruction>(operand)->getParent() == instr->getParent() &&
Chris Lattnerb00c5822001-10-02 03:41:24 +0000332 !isa<PHINode>(instr) &&
Vikram S. Adve64c2ced2001-09-30 23:45:08 +0000333 instr->getOpcode() != Instruction::Call)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000334 {
335 // Recursively create a treeNode for it.
336 opTreeNode = buildTreeForInstruction((Instruction*)operand);
337 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000338 else if (Constant *CPV = dyn_cast<Constant>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000339 {
340 // Create a leaf node for a constant
341 opTreeNode = new ConstantNode(CPV);
342 }
343 else
344 {
345 // Create a leaf node for the virtual register
346 opTreeNode = new VRegNode(operand);
347 }
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000348
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000349 childArray[numChildren++] = opTreeNode;
350 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000351 }
352
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 //--------------------------------------------------------------------
361
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000362 InstrTreeNode *parent = treeNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000363
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000364 if (numChildren > 2)
365 {
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);
372 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000373
374 // Insert the first child as a direct child
375 if (numChildren >= 1)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000376 setLeftChild(parent, childArray[0]);
377
378 int n;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000379
380 // Create a list node for children 2 .. N-1, if any
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000381 for (n = numChildren-1; n >= 2; n--)
382 {
383 // We have more than two children
384 InstrTreeNode *listNode = new VRegListNode();
385 setRightChild(parent, listNode);
386 setLeftChild(listNode, childArray[numChildren - n]);
387 parent = listNode;
388 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000389
390 // Now insert the last remaining child (if any).
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000391 if (numChildren >= 2)
392 {
393 assert(n == 1);
394 setRightChild(parent, childArray[numChildren - 1]);
395 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000396
397 if (childArray != fixedChildArray)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000398 delete [] childArray;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000399
400 return treeNode;
401}
402