blob: a95f1e377a0ce68e63bbb9d79a5698c2bb90d18e [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//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000020//---------------------------------------------------------------------------
21
Chris Lattner942d99e2001-07-21 22:59:56 +000022#include "llvm/CodeGen/InstrForest.h"
Chris Lattnera8bbb6b2002-02-03 07:31:41 +000023#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000024#include "llvm/Function.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000025#include "llvm/iTerminators.h"
26#include "llvm/iMemory.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000027#include "llvm/Constant.h"
Chris Lattner86e91872002-04-29 18:48:55 +000028#include "llvm/Type.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000029#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000030#include "Support/STLExtras.h"
Chris Lattner244212a2002-04-09 03:37:11 +000031#include <alloca.h>
Chris Lattner697954c2002-01-20 22:54:45 +000032#include <iostream>
33using std::cerr;
34using std::vector;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000035
36//------------------------------------------------------------------------
37// class InstrTreeNode
38//------------------------------------------------------------------------
39
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000040void
41InstrTreeNode::dump(int dumpChildren, int indent) const
42{
Chris Lattnerd268ad62001-09-11 23:52:11 +000043 dumpNode(indent);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000044
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000045 if (dumpChildren)
46 {
47 if (LeftChild)
48 LeftChild->dump(dumpChildren, indent+1);
49 if (RightChild)
50 RightChild->dump(dumpChildren, indent+1);
51 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000052}
53
54
Chris Lattner4ddb4c82001-09-12 01:28:49 +000055InstructionNode::InstructionNode(Instruction* I)
Vikram S. Adved95919c2002-03-24 03:24:00 +000056 : InstrTreeNode(NTInstructionNode, I),
57 codeIsFoldedIntoParent(false)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000058{
Chris Lattner4ddb4c82001-09-12 01:28:49 +000059 opLabel = I->getOpcode();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000060
61 // Distinguish special cases of some instructions such as Ret and Br
62 //
Chris Lattnerb00c5822001-10-02 03:41:24 +000063 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000064 {
65 opLabel = RetValueOp; // ret(value) operation
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000066 }
Chris Lattnerb00c5822001-10-02 03:41:24 +000067 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000068 {
69 opLabel = BrCondOp; // br(cond) operation
70 }
71 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
72 {
73 opLabel = SetCCOp; // common label for all SetCC ops
74 }
75 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
76 {
77 opLabel = AllocaN; // Alloca(ptr, N) operation
78 }
79 else if ((opLabel == Instruction::Load ||
80 opLabel == Instruction::GetElementPtr) &&
Chris Lattner65ea1712001-11-14 11:27:58 +000081 cast<MemAccessInst>(I)->hasIndices())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000082 {
83 opLabel = opLabel + 100; // load/getElem with index vector
84 }
Vikram S. Advebe495262001-11-08 04:47:06 +000085 else if (opLabel == Instruction::And ||
86 opLabel == Instruction::Or ||
87 opLabel == Instruction::Xor ||
88 opLabel == Instruction::Not)
89 {
90 // Distinguish bitwise operators from logical operators!
91 if (I->getType() != Type::BoolTy)
92 opLabel = opLabel + 100; // bitwise operator
93 }
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000094 else if (opLabel == Instruction::Cast)
95 {
96 const Type *ITy = I->getType();
97 switch(ITy->getPrimitiveID())
98 {
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;
112 default:
113 // Just use `Cast' opcode otherwise. It's probably ignored.
114 break;
115 }
116 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000117}
118
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000119
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000120void
121InstructionNode::dumpNode(int indent) const
122{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000123 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000124 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000125
Chris Lattner697954c2002-01-20 22:54:45 +0000126 cerr << getInstruction()->getOpcodeName();
Chris Lattnera8bbb6b2002-02-03 07:31:41 +0000127 const MachineCodeForInstruction &mvec =
128 MachineCodeForInstruction::get(getInstruction());
129
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000130 if (mvec.size() > 0)
Chris Lattner697954c2002-01-20 22:54:45 +0000131 cerr << "\tMachine Instructions: ";
Chris Lattnera8bbb6b2002-02-03 07:31:41 +0000132
133 for (unsigned int i=0; i < mvec.size(); ++i) {
134 mvec[i]->dump(0);
135 if (i < mvec.size() - 1)
136 cerr << "; ";
137 }
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
Chris Lattner79df7c02002-03-26 18:01:55 +0000188InstrForest::InstrForest(Function *F)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000189{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000190 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);
Chris Lattner221d6882002-02-12 21:07:25 +0000193 }
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000194}
195
196InstrForest::~InstrForest()
197{
Chris Lattner7884cd12002-04-08 23:09:07 +0000198 for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000199}
200
201void
202InstrForest::dump() const
203{
Vikram S. Adved95919c2002-03-24 03:24:00 +0000204 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000205 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000206}
207
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000208inline void
Vikram S. Adved95919c2002-03-24 03:24:00 +0000209InstrForest::eraseRoot(InstructionNode* node)
210{
211 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
212 RI != RE; ++RI)
213 if (*RI == node)
214 treeRoots.erase(RI.base()-1);
215}
216
217inline void
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000218InstrForest::noteTreeNodeForInstr(Instruction *instr,
219 InstructionNode *treeNode)
220{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000221 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
222 (*this)[instr] = treeNode;
Vikram S. Adved95919c2002-03-24 03:24:00 +0000223 treeRoots.push_back(treeNode); // mark node as root of a new tree
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000224}
225
226
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000227inline void
Vikram S. Adved95919c2002-03-24 03:24:00 +0000228InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000229{
Vikram S. Adved95919c2002-03-24 03:24:00 +0000230 parent->LeftChild = child;
231 child->Parent = parent;
232 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
233 eraseRoot((InstructionNode*) child); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000234}
235
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000236inline void
Vikram S. Adved95919c2002-03-24 03:24:00 +0000237InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000238{
Vikram S. Adved95919c2002-03-24 03:24:00 +0000239 parent->RightChild = child;
240 child->Parent = parent;
241 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
242 eraseRoot((InstructionNode*) child); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000243}
244
245
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000246InstructionNode*
247InstrForest::buildTreeForInstruction(Instruction *instr)
248{
249 InstructionNode *treeNode = getTreeNodeForInstr(instr);
250 if (treeNode)
251 {
252 // treeNode has already been constructed for this instruction
253 assert(treeNode->getInstruction() == instr);
254 return treeNode;
255 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000256
257 // Otherwise, create a new tree node for this instruction.
258 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000259 treeNode = new InstructionNode(instr);
260 noteTreeNodeForInstr(instr, treeNode);
261
262 if (instr->getOpcode() == Instruction::Call)
263 { // Operands of call instruction
264 return treeNode;
265 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000266
267 // If the instruction has more than 2 instruction operands,
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000268 // then we need to create artificial list nodes to hold them.
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000269 // (Note that we only count operands that get tree nodes, and not
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000270 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000271 //
272 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000273 // for all appropriate operands and save them in an array. We then
274 // insert children at the end, creating list nodes where needed.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000275 // As a performance optimization, allocate a child array only
276 // if a fixed array is too small.
277 //
278 int numChildren = 0;
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000279 InstrTreeNode **childArray =
Chris Lattner7884cd12002-04-08 23:09:07 +0000280 (InstrTreeNode **)alloca(instr->getNumOperands()*sizeof(InstrTreeNode *));
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000281
282 //
283 // Walk the operands of the instruction
284 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000285 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
286 {
287 Value* operand = *O;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000288
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000289 // 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.
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000293
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000294 // Check latter condition here just to simplify the next IF.
295 bool includeAddressOperand =
Chris Lattner79df7c02002-03-26 18:01:55 +0000296 (isa<BasicBlock>(operand) || isa<Function>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000297 && !instr->isTerminator();
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000298
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000299 if (includeAddressOperand || isa<Instruction>(operand) ||
Chris Lattner73e21422002-04-09 19:48:49 +0000300 isa<Constant>(operand) || isa<Argument>(operand) ||
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000301 isa<GlobalVariable>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000302 {
303 // This operand is a data value
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000304
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000305 // 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)
312 //
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.)
316 //
317 // In all other cases, the virtual register holding the value
318 // is used directly, i.e., made a child of the instruction node.
319 //
320 InstrTreeNode* opTreeNode;
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000321 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
322 cast<Instruction>(operand)->getParent() == instr->getParent() &&
Chris Lattner7884cd12002-04-08 23:09:07 +0000323 instr->getOpcode() != Instruction::PHINode &&
Vikram S. Adve64c2ced2001-09-30 23:45:08 +0000324 instr->getOpcode() != Instruction::Call)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000325 {
326 // Recursively create a treeNode for it.
327 opTreeNode = buildTreeForInstruction((Instruction*)operand);
328 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000329 else if (Constant *CPV = dyn_cast<Constant>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000330 {
331 // Create a leaf node for a constant
332 opTreeNode = new ConstantNode(CPV);
333 }
334 else
335 {
336 // Create a leaf node for the virtual register
337 opTreeNode = new VRegNode(operand);
338 }
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000339
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000340 childArray[numChildren++] = opTreeNode;
341 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000342 }
343
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 //--------------------------------------------------------------------
352
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000353 InstrTreeNode *parent = treeNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000354
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000355 if (numChildren > 2)
356 {
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);
363 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000364
365 // Insert the first child as a direct child
366 if (numChildren >= 1)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000367 setLeftChild(parent, childArray[0]);
368
369 int n;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000370
371 // Create a list node for children 2 .. N-1, if any
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000372 for (n = numChildren-1; n >= 2; n--)
373 {
374 // We have more than two children
375 InstrTreeNode *listNode = new VRegListNode();
376 setRightChild(parent, listNode);
377 setLeftChild(listNode, childArray[numChildren - n]);
378 parent = listNode;
379 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000380
381 // Now insert the last remaining child (if any).
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000382 if (numChildren >= 2)
383 {
384 assert(n == 1);
385 setRightChild(parent, childArray[numChildren - 1]);
386 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000387
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000388 return treeNode;
389}