blob: ea2ccadab945684e954bd652223758c790289a7c [file] [log] [blame]
Chris Lattner959a5fb2002-08-09 20:08:06 +00001//===-- InstrForest.cpp - Build instruction forest for inst selection -----===//
2//
Vikram S. Adveab9e5572001-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 Lattner959a5fb2002-08-09 20:08:06 +000013//===----------------------------------------------------------------------===//
Vikram S. Adveab9e5572001-07-21 12:41:50 +000014
Chris Lattner77699702001-07-21 22:59:56 +000015#include "llvm/CodeGen/InstrForest.h"
Chris Lattner0068ea22002-02-03 07:31:41 +000016#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner57698e22002-03-26 18:01:55 +000017#include "llvm/Function.h"
Vikram S. Adveab9e5572001-07-21 12:41:50 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattnerca142372002-04-28 19:55:58 +000020#include "llvm/Constant.h"
Chris Lattner70411b02002-04-29 18:48:55 +000021#include "llvm/Type.h"
Chris Lattnerdd511762001-07-21 20:58:30 +000022#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner5de22042001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Vikram S. Adveab9e5572001-07-21 12:41:50 +000024
25//------------------------------------------------------------------------
26// class InstrTreeNode
27//------------------------------------------------------------------------
28
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000029void
30InstrTreeNode::dump(int dumpChildren, int indent) const
31{
Chris Lattnercc38da72001-09-11 23:52:11 +000032 dumpNode(indent);
Vikram S. Adveab9e5572001-07-21 12:41:50 +000033
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000034 if (dumpChildren)
35 {
36 if (LeftChild)
37 LeftChild->dump(dumpChildren, indent+1);
38 if (RightChild)
39 RightChild->dump(dumpChildren, indent+1);
40 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +000041}
42
43
Chris Lattner16dd09a2001-09-12 01:28:49 +000044InstructionNode::InstructionNode(Instruction* I)
Vikram S. Adve3228f9c2002-03-24 03:24:00 +000045 : InstrTreeNode(NTInstructionNode, I),
46 codeIsFoldedIntoParent(false)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000047{
Chris Lattner16dd09a2001-09-12 01:28:49 +000048 opLabel = I->getOpcode();
Vikram S. Adveab9e5572001-07-21 12:41:50 +000049
50 // Distinguish special cases of some instructions such as Ret and Br
51 //
Chris Lattnerda558102001-10-02 03:41:24 +000052 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000053 {
54 opLabel = RetValueOp; // ret(value) operation
Vikram S. Adveab9e5572001-07-21 12:41:50 +000055 }
Chris Lattnerda558102001-10-02 03:41:24 +000056 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000057 {
58 opLabel = BrCondOp; // br(cond) operation
59 }
60 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
61 {
62 opLabel = SetCCOp; // common label for all SetCC ops
63 }
64 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
65 {
66 opLabel = AllocaN; // Alloca(ptr, N) operation
67 }
Chris Lattnerdfb3a2c2002-08-22 23:37:20 +000068 else if (opLabel == Instruction::GetElementPtr &&
69 cast<GetElementPtrInst>(I)->hasIndices())
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000070 {
Chris Lattnerdfb3a2c2002-08-22 23:37:20 +000071 opLabel = opLabel + 100; // getElem with index vector
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000072 }
Vikram S. Adve3db97eb2002-08-15 14:19:22 +000073 else if (opLabel == Instruction::Xor &&
74 BinaryOperator::isNot(I))
75 {
76 opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator
77 : BNotOp; // bitwise Not operator
78 }
Vikram S. Advec5303722001-11-08 04:47:06 +000079 else if (opLabel == Instruction::And ||
80 opLabel == Instruction::Or ||
Vikram S. Adve3db97eb2002-08-15 14:19:22 +000081 opLabel == Instruction::Xor)
Vikram S. Advec5303722001-11-08 04:47:06 +000082 {
83 // Distinguish bitwise operators from logical operators!
84 if (I->getType() != Type::BoolTy)
85 opLabel = opLabel + 100; // bitwise operator
86 }
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000087 else if (opLabel == Instruction::Cast)
88 {
89 const Type *ITy = I->getType();
90 switch(ITy->getPrimitiveID())
91 {
92 case Type::BoolTyID: opLabel = ToBoolTy; break;
93 case Type::UByteTyID: opLabel = ToUByteTy; break;
94 case Type::SByteTyID: opLabel = ToSByteTy; break;
95 case Type::UShortTyID: opLabel = ToUShortTy; break;
96 case Type::ShortTyID: opLabel = ToShortTy; break;
97 case Type::UIntTyID: opLabel = ToUIntTy; break;
98 case Type::IntTyID: opLabel = ToIntTy; break;
99 case Type::ULongTyID: opLabel = ToULongTy; break;
100 case Type::LongTyID: opLabel = ToLongTy; break;
101 case Type::FloatTyID: opLabel = ToFloatTy; break;
102 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
103 case Type::ArrayTyID: opLabel = ToArrayTy; break;
104 case Type::PointerTyID: opLabel = ToPointerTy; break;
105 default:
106 // Just use `Cast' opcode otherwise. It's probably ignored.
107 break;
108 }
109 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000110}
111
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000112
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000113void
114InstructionNode::dumpNode(int indent) const
115{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000116 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000117 std::cerr << " ";
118 std::cerr << getInstruction()->getOpcodeName()
119 << " [label " << getOpLabel() << "]" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000120}
121
122
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000123void
124VRegListNode::dumpNode(int indent) const
125{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000126 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000127 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000128
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000129 std::cerr << "List" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000130}
131
132
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000133void
134VRegNode::dumpNode(int indent) const
135{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000136 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000137 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000138
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000139 std::cerr << "VReg " << getValue() << "\t(type "
140 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000141}
142
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000143void
144ConstantNode::dumpNode(int indent) const
145{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000146 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000147 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000148
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000149 std::cerr << "Constant " << getValue() << "\t(type "
150 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000151}
152
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000153void
154LabelNode::dumpNode(int indent) const
155{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000156 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000157 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000158
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000159 std::cerr << "Label " << getValue() << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000160}
161
162//------------------------------------------------------------------------
163// class InstrForest
164//
165// A forest of instruction trees, usually for a single method.
166//------------------------------------------------------------------------
167
Chris Lattner57698e22002-03-26 18:01:55 +0000168InstrForest::InstrForest(Function *F)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000169{
Chris Lattner7076ff22002-06-25 16:13:21 +0000170 for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
171 for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
172 buildTreeForInstruction(I);
Chris Lattner60a65912002-02-12 21:07:25 +0000173 }
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000174}
175
176InstrForest::~InstrForest()
177{
Chris Lattner8690ac12002-04-08 23:09:07 +0000178 for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000179}
180
181void
182InstrForest::dump() const
183{
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000184 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
Chris Lattner16dd09a2001-09-12 01:28:49 +0000185 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000186}
187
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000188inline void
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000189InstrForest::eraseRoot(InstructionNode* node)
190{
191 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
192 RI != RE; ++RI)
193 if (*RI == node)
194 treeRoots.erase(RI.base()-1);
195}
196
197inline void
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000198InstrForest::noteTreeNodeForInstr(Instruction *instr,
199 InstructionNode *treeNode)
200{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000201 (*this)[instr] = treeNode;
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000202 treeRoots.push_back(treeNode); // mark node as root of a new tree
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000203}
204
205
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000206inline void
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000207InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000208{
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000209 parent->LeftChild = child;
210 child->Parent = parent;
Vikram S. Adve872c7f92002-08-24 21:02:09 +0000211 if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
212 eraseRoot(instrNode); // no longer a tree root
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000213}
214
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000215inline void
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000216InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000217{
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000218 parent->RightChild = child;
219 child->Parent = parent;
Vikram S. Adve872c7f92002-08-24 21:02:09 +0000220 if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
221 eraseRoot(instrNode); // no longer a tree root
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000222}
223
224
Vikram S. Adve0c51cf02001-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. Adveab9e5572001-07-21 12:41:50 +0000235
236 // Otherwise, create a new tree node for this instruction.
237 //
Vikram S. Adve0c51cf02001-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. Adveab9e5572001-07-21 12:41:50 +0000245
246 // If the instruction has more than 2 instruction operands,
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000247 // then we need to create artificial list nodes to hold them.
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000248 // (Note that we only count operands that get tree nodes, and not
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000249 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000250 //
251 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Adve1c73bc12001-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. Adveab9e5572001-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;
Chris Lattner55491772003-06-16 22:29:09 +0000258 InstrTreeNode** childArray = new InstrTreeNode*[instr->getNumOperands()];
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000259
260 //
261 // Walk the operands of the instruction
262 //
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000263 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
264 {
265 Value* operand = *O;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000266
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000267 // Check if the operand is a data value, not an branch label, type,
268 // method or module. If the operand is an address type (i.e., label
269 // or method) that is used in an non-branching operation, e.g., `add'.
270 // that should be considered a data value.
Chris Lattner16dd09a2001-09-12 01:28:49 +0000271
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000272 // Check latter condition here just to simplify the next IF.
273 bool includeAddressOperand =
Chris Lattner57698e22002-03-26 18:01:55 +0000274 (isa<BasicBlock>(operand) || isa<Function>(operand))
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000275 && !instr->isTerminator();
Chris Lattner16dd09a2001-09-12 01:28:49 +0000276
Chris Lattner38569342001-10-01 20:11:19 +0000277 if (includeAddressOperand || isa<Instruction>(operand) ||
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000278 isa<Constant>(operand) || isa<Argument>(operand) ||
Chris Lattner38569342001-10-01 20:11:19 +0000279 isa<GlobalVariable>(operand))
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000280 {
281 // This operand is a data value
Chris Lattner16dd09a2001-09-12 01:28:49 +0000282
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000283 // An instruction that computes the incoming value is added as a
284 // child of the current instruction if:
285 // the value has only a single use
286 // AND both instructions are in the same basic block.
287 // AND the current instruction is not a PHI (because the incoming
288 // value is conceptually in a predecessor block,
289 // even though it may be in the same static block)
290 //
291 // (Note that if the value has only a single use (viz., `instr'),
292 // the def of the value can be safely moved just before instr
293 // and therefore it is safe to combine these two instructions.)
294 //
295 // In all other cases, the virtual register holding the value
296 // is used directly, i.e., made a child of the instruction node.
297 //
298 InstrTreeNode* opTreeNode;
Chris Lattner38569342001-10-01 20:11:19 +0000299 if (isa<Instruction>(operand) && operand->use_size() == 1 &&
300 cast<Instruction>(operand)->getParent() == instr->getParent() &&
Chris Lattner8690ac12002-04-08 23:09:07 +0000301 instr->getOpcode() != Instruction::PHINode &&
Vikram S. Adve813ffcc2001-09-30 23:45:08 +0000302 instr->getOpcode() != Instruction::Call)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000303 {
304 // Recursively create a treeNode for it.
305 opTreeNode = buildTreeForInstruction((Instruction*)operand);
306 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000307 else if (Constant *CPV = dyn_cast<Constant>(operand))
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000308 {
309 // Create a leaf node for a constant
310 opTreeNode = new ConstantNode(CPV);
311 }
312 else
313 {
314 // Create a leaf node for the virtual register
315 opTreeNode = new VRegNode(operand);
316 }
Chris Lattner16dd09a2001-09-12 01:28:49 +0000317
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000318 childArray[numChildren++] = opTreeNode;
319 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000320 }
321
322 //--------------------------------------------------------------------
323 // Add any selected operands as children in the tree.
324 // Certain instructions can have more than 2 in some instances (viz.,
325 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
326 // array or struct). Make the operands of every such instruction into
327 // a right-leaning binary tree with the operand nodes at the leaves
328 // and VRegList nodes as internal nodes.
329 //--------------------------------------------------------------------
330
Chris Lattner16dd09a2001-09-12 01:28:49 +0000331 InstrTreeNode *parent = treeNode;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000332
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000333 if (numChildren > 2)
334 {
335 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
336 assert(instrOpcode == Instruction::PHINode ||
337 instrOpcode == Instruction::Call ||
338 instrOpcode == Instruction::Load ||
339 instrOpcode == Instruction::Store ||
340 instrOpcode == Instruction::GetElementPtr);
341 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000342
343 // Insert the first child as a direct child
344 if (numChildren >= 1)
Chris Lattner16dd09a2001-09-12 01:28:49 +0000345 setLeftChild(parent, childArray[0]);
346
347 int n;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000348
349 // Create a list node for children 2 .. N-1, if any
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000350 for (n = numChildren-1; n >= 2; n--)
351 {
352 // We have more than two children
353 InstrTreeNode *listNode = new VRegListNode();
354 setRightChild(parent, listNode);
355 setLeftChild(listNode, childArray[numChildren - n]);
356 parent = listNode;
357 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000358
359 // Now insert the last remaining child (if any).
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000360 if (numChildren >= 2)
361 {
362 assert(n == 1);
363 setRightChild(parent, childArray[numChildren - 1]);
364 }
Chris Lattner55491772003-06-16 22:29:09 +0000365
366 delete [] childArray;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000367 return treeNode;
368}