blob: c24c35b640b15fcf3870cce15e84c9bca75e5f0f [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- InstrForest.cpp - Build instruction forest for inst selection -----===//
2//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00003// The key goal is to group instructions into a single
4// tree if one or more of them might be potentially combined into a single
5// complex instruction in the target machine.
6// Since this grouping is completely machine-independent, we do it as
7// aggressive as possible to exploit any possible taret instructions.
8// In particular, we group two instructions O and I if:
9// (1) Instruction O computes an operand used by instruction I,
10// and (2) O and I are part of the same basic block,
11// and (3) O has only a single use, viz., I.
12//
Chris Lattner035dfbe2002-08-09 20:08:06 +000013//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000014
Chris Lattner942d99e2001-07-21 22:59:56 +000015#include "llvm/CodeGen/InstrForest.h"
Chris Lattnera8bbb6b2002-02-03 07:31:41 +000016#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000017#include "llvm/Function.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000020#include "llvm/Constant.h"
Chris Lattner86e91872002-04-29 18:48:55 +000021#include "llvm/Type.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000022#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Chris Lattner697954c2002-01-20 22:54:45 +000024using std::cerr;
25using std::vector;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000026
27//------------------------------------------------------------------------
28// class InstrTreeNode
29//------------------------------------------------------------------------
30
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000031void
32InstrTreeNode::dump(int dumpChildren, int indent) const
33{
Chris Lattnerd268ad62001-09-11 23:52:11 +000034 dumpNode(indent);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000035
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000036 if (dumpChildren)
37 {
38 if (LeftChild)
39 LeftChild->dump(dumpChildren, indent+1);
40 if (RightChild)
41 RightChild->dump(dumpChildren, indent+1);
42 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000043}
44
45
Chris Lattner4ddb4c82001-09-12 01:28:49 +000046InstructionNode::InstructionNode(Instruction* I)
Vikram S. Adved95919c2002-03-24 03:24:00 +000047 : InstrTreeNode(NTInstructionNode, I),
48 codeIsFoldedIntoParent(false)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000049{
Chris Lattner4ddb4c82001-09-12 01:28:49 +000050 opLabel = I->getOpcode();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000051
52 // Distinguish special cases of some instructions such as Ret and Br
53 //
Chris Lattnerb00c5822001-10-02 03:41:24 +000054 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000055 {
56 opLabel = RetValueOp; // ret(value) operation
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000057 }
Chris Lattnerb00c5822001-10-02 03:41:24 +000058 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000059 {
60 opLabel = BrCondOp; // br(cond) operation
61 }
62 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
63 {
64 opLabel = SetCCOp; // common label for all SetCC ops
65 }
66 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
67 {
68 opLabel = AllocaN; // Alloca(ptr, N) operation
69 }
70 else if ((opLabel == Instruction::Load ||
71 opLabel == Instruction::GetElementPtr) &&
Chris Lattner65ea1712001-11-14 11:27:58 +000072 cast<MemAccessInst>(I)->hasIndices())
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000073 {
74 opLabel = opLabel + 100; // load/getElem with index vector
75 }
Vikram S. Adve85af1312002-08-15 14:19:22 +000076 else if (opLabel == Instruction::Xor &&
77 BinaryOperator::isNot(I))
78 {
79 opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator
80 : BNotOp; // bitwise Not operator
81 }
Vikram S. Advebe495262001-11-08 04:47:06 +000082 else if (opLabel == Instruction::And ||
83 opLabel == Instruction::Or ||
Vikram S. Adve85af1312002-08-15 14:19:22 +000084 opLabel == Instruction::Xor)
Vikram S. Advebe495262001-11-08 04:47:06 +000085 {
86 // Distinguish bitwise operators from logical operators!
87 if (I->getType() != Type::BoolTy)
88 opLabel = opLabel + 100; // bitwise operator
89 }
Vikram S. Adve4c31fb52001-09-18 12:54:27 +000090 else if (opLabel == Instruction::Cast)
91 {
92 const Type *ITy = I->getType();
93 switch(ITy->getPrimitiveID())
94 {
95 case Type::BoolTyID: opLabel = ToBoolTy; break;
96 case Type::UByteTyID: opLabel = ToUByteTy; break;
97 case Type::SByteTyID: opLabel = ToSByteTy; break;
98 case Type::UShortTyID: opLabel = ToUShortTy; break;
99 case Type::ShortTyID: opLabel = ToShortTy; break;
100 case Type::UIntTyID: opLabel = ToUIntTy; break;
101 case Type::IntTyID: opLabel = ToIntTy; break;
102 case Type::ULongTyID: opLabel = ToULongTy; break;
103 case Type::LongTyID: opLabel = ToLongTy; break;
104 case Type::FloatTyID: opLabel = ToFloatTy; break;
105 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
106 case Type::ArrayTyID: opLabel = ToArrayTy; break;
107 case Type::PointerTyID: opLabel = ToPointerTy; break;
108 default:
109 // Just use `Cast' opcode otherwise. It's probably ignored.
110 break;
111 }
112 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000113}
114
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000115
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000116void
117InstructionNode::dumpNode(int indent) const
118{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000119 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000120 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000121
Chris Lattner697954c2002-01-20 22:54:45 +0000122 cerr << getInstruction()->getOpcodeName();
Chris Lattnera8bbb6b2002-02-03 07:31:41 +0000123 const MachineCodeForInstruction &mvec =
124 MachineCodeForInstruction::get(getInstruction());
125
Chris Lattner035dfbe2002-08-09 20:08:06 +0000126 if (!mvec.empty())
Chris Lattner697954c2002-01-20 22:54:45 +0000127 cerr << "\tMachine Instructions: ";
Chris Lattnera8bbb6b2002-02-03 07:31:41 +0000128
Chris Lattner035dfbe2002-08-09 20:08:06 +0000129 for (unsigned i = 0; i < mvec.size(); ++i) {
Vikram S. Advebf82a422002-07-08 23:01:46 +0000130 mvec[i]->dump();
Chris Lattnera8bbb6b2002-02-03 07:31:41 +0000131 if (i < mvec.size() - 1)
132 cerr << "; ";
133 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000134
Chris Lattner697954c2002-01-20 22:54:45 +0000135 cerr << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000136}
137
138
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000139void
140VRegListNode::dumpNode(int indent) const
141{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000142 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000143 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000144
Chris Lattner697954c2002-01-20 22:54:45 +0000145 cerr << "List" << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000146}
147
148
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000149void
150VRegNode::dumpNode(int indent) const
151{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000152 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000153 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000154
Chris Lattner697954c2002-01-20 22:54:45 +0000155 cerr << "VReg " << getValue() << "\t(type "
156 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000157}
158
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000159void
160ConstantNode::dumpNode(int indent) const
161{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000162 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000163 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000164
Chris Lattner697954c2002-01-20 22:54:45 +0000165 cerr << "Constant " << getValue() << "\t(type "
166 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000167}
168
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000169void
170LabelNode::dumpNode(int indent) const
171{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000172 for (int i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000173 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000174
Chris Lattner697954c2002-01-20 22:54:45 +0000175 cerr << "Label " << getValue() << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000176}
177
178//------------------------------------------------------------------------
179// class InstrForest
180//
181// A forest of instruction trees, usually for a single method.
182//------------------------------------------------------------------------
183
Chris Lattner79df7c02002-03-26 18:01:55 +0000184InstrForest::InstrForest(Function *F)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000185{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000186 for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
187 for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
188 buildTreeForInstruction(I);
Chris Lattner221d6882002-02-12 21:07:25 +0000189 }
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000190}
191
192InstrForest::~InstrForest()
193{
Chris Lattner7884cd12002-04-08 23:09:07 +0000194 for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000195}
196
197void
198InstrForest::dump() const
199{
Vikram S. Adved95919c2002-03-24 03:24:00 +0000200 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000201 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000202}
203
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000204inline void
Vikram S. Adved95919c2002-03-24 03:24:00 +0000205InstrForest::eraseRoot(InstructionNode* node)
206{
207 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
208 RI != RE; ++RI)
209 if (*RI == node)
210 treeRoots.erase(RI.base()-1);
211}
212
213inline void
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000214InstrForest::noteTreeNodeForInstr(Instruction *instr,
215 InstructionNode *treeNode)
216{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000217 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
218 (*this)[instr] = treeNode;
Vikram S. Adved95919c2002-03-24 03:24:00 +0000219 treeRoots.push_back(treeNode); // mark node as root of a new tree
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000220}
221
222
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000223inline void
Vikram S. Adved95919c2002-03-24 03:24:00 +0000224InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000225{
Vikram S. Adved95919c2002-03-24 03:24:00 +0000226 parent->LeftChild = child;
227 child->Parent = parent;
228 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
229 eraseRoot((InstructionNode*) child); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000230}
231
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000232inline void
Vikram S. Adved95919c2002-03-24 03:24:00 +0000233InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000234{
Vikram S. Adved95919c2002-03-24 03:24:00 +0000235 parent->RightChild = child;
236 child->Parent = parent;
237 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
238 eraseRoot((InstructionNode*) child); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000239}
240
241
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000242InstructionNode*
243InstrForest::buildTreeForInstruction(Instruction *instr)
244{
245 InstructionNode *treeNode = getTreeNodeForInstr(instr);
246 if (treeNode)
247 {
248 // treeNode has already been constructed for this instruction
249 assert(treeNode->getInstruction() == instr);
250 return treeNode;
251 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000252
253 // Otherwise, create a new tree node for this instruction.
254 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000255 treeNode = new InstructionNode(instr);
256 noteTreeNodeForInstr(instr, treeNode);
257
258 if (instr->getOpcode() == Instruction::Call)
259 { // Operands of call instruction
260 return treeNode;
261 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000262
263 // If the instruction has more than 2 instruction operands,
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000264 // then we need to create artificial list nodes to hold them.
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000265 // (Note that we only count operands that get tree nodes, and not
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000266 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000267 //
268 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000269 // for all appropriate operands and save them in an array. We then
270 // insert children at the end, creating list nodes where needed.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000271 // As a performance optimization, allocate a child array only
272 // if a fixed array is too small.
273 //
274 int numChildren = 0;
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000275 InstrTreeNode **childArray =
Chris Lattner7884cd12002-04-08 23:09:07 +0000276 (InstrTreeNode **)alloca(instr->getNumOperands()*sizeof(InstrTreeNode *));
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000277
278 //
279 // Walk the operands of the instruction
280 //
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000281 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
282 {
283 Value* operand = *O;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000284
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000285 // Check if the operand is a data value, not an branch label, type,
286 // method or module. If the operand is an address type (i.e., label
287 // or method) that is used in an non-branching operation, e.g., `add'.
288 // that should be considered a data value.
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000289
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000290 // Check latter condition here just to simplify the next IF.
291 bool includeAddressOperand =
Chris Lattner79df7c02002-03-26 18:01:55 +0000292 (isa<BasicBlock>(operand) || isa<Function>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000293 && !instr->isTerminator();
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000294
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000295 if (includeAddressOperand || isa<Instruction>(operand) ||
Chris Lattner73e21422002-04-09 19:48:49 +0000296 isa<Constant>(operand) || isa<Argument>(operand) ||
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000297 isa<GlobalVariable>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000298 {
299 // This operand is a data value
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000300
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000301 // An instruction that computes the incoming value is added as a
302 // child of the current instruction if:
303 // the value has only a single use
304 // AND both instructions are in the same basic block.
305 // AND the current instruction is not a PHI (because the incoming
306 // value is conceptually in a predecessor block,
307 // even though it may be in the same static block)
308 //
309 // (Note that if the value has only a single use (viz., `instr'),
310 // the def of the value can be safely moved just before instr
311 // and therefore it is safe to combine these two instructions.)
312 //
313 // In all other cases, the virtual register holding the value
314 // is used directly, i.e., made a child of the instruction node.
315 //
316 InstrTreeNode* opTreeNode;
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000317 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
318 cast<Instruction>(operand)->getParent() == instr->getParent() &&
Chris Lattner7884cd12002-04-08 23:09:07 +0000319 instr->getOpcode() != Instruction::PHINode &&
Vikram S. Adve64c2ced2001-09-30 23:45:08 +0000320 instr->getOpcode() != Instruction::Call)
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000321 {
322 // Recursively create a treeNode for it.
323 opTreeNode = buildTreeForInstruction((Instruction*)operand);
324 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000325 else if (Constant *CPV = dyn_cast<Constant>(operand))
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000326 {
327 // Create a leaf node for a constant
328 opTreeNode = new ConstantNode(CPV);
329 }
330 else
331 {
332 // Create a leaf node for the virtual register
333 opTreeNode = new VRegNode(operand);
334 }
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000335
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000336 childArray[numChildren++] = opTreeNode;
337 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000338 }
339
340 //--------------------------------------------------------------------
341 // Add any selected operands as children in the tree.
342 // Certain instructions can have more than 2 in some instances (viz.,
343 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
344 // array or struct). Make the operands of every such instruction into
345 // a right-leaning binary tree with the operand nodes at the leaves
346 // and VRegList nodes as internal nodes.
347 //--------------------------------------------------------------------
348
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000349 InstrTreeNode *parent = treeNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000350
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000351 if (numChildren > 2)
352 {
353 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
354 assert(instrOpcode == Instruction::PHINode ||
355 instrOpcode == Instruction::Call ||
356 instrOpcode == Instruction::Load ||
357 instrOpcode == Instruction::Store ||
358 instrOpcode == Instruction::GetElementPtr);
359 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000360
361 // Insert the first child as a direct child
362 if (numChildren >= 1)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000363 setLeftChild(parent, childArray[0]);
364
365 int n;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000366
367 // Create a list node for children 2 .. N-1, if any
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000368 for (n = numChildren-1; n >= 2; n--)
369 {
370 // We have more than two children
371 InstrTreeNode *listNode = new VRegListNode();
372 setRightChild(parent, listNode);
373 setLeftChild(listNode, childArray[numChildren - n]);
374 parent = listNode;
375 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000376
377 // Now insert the last remaining child (if any).
Vikram S. Adve4c31fb52001-09-18 12:54:27 +0000378 if (numChildren >= 2)
379 {
380 assert(n == 1);
381 setRightChild(parent, childArray[numChildren - 1]);
382 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000383
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000384 return treeNode;
385}