blob: 90993b7a8ddb7d80690eab11d97b053bb49a42a9 [file] [log] [blame]
Chris Lattner959a5fb2002-08-09 20:08:06 +00001//===-- InstrForest.cpp - Build instruction forest for inst selection -----===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner959a5fb2002-08-09 20:08:06 +00009//
Vikram S. Adveab9e5572001-07-21 12:41:50 +000010// 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
Misha Brukman02fe6b72003-09-17 21:34:23 +000014// aggressive as possible to exploit any possible target instructions.
Vikram S. Adveab9e5572001-07-21 12:41:50 +000015// 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//
Chris Lattner959a5fb2002-08-09 20:08:06 +000020//===----------------------------------------------------------------------===//
Vikram S. Adveab9e5572001-07-21 12:41:50 +000021
Chris Lattner77699702001-07-21 22:59:56 +000022#include "llvm/CodeGen/InstrForest.h"
Chris Lattner0068ea22002-02-03 07:31:41 +000023#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner57698e22002-03-26 18:01:55 +000024#include "llvm/Function.h"
Vikram S. Adveab9e5572001-07-21 12:41:50 +000025#include "llvm/iTerminators.h"
26#include "llvm/iMemory.h"
Chris Lattnerca142372002-04-28 19:55:58 +000027#include "llvm/Constant.h"
Chris Lattner70411b02002-04-29 18:48:55 +000028#include "llvm/Type.h"
Chris Lattnerdd511762001-07-21 20:58:30 +000029#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner5de22042001-11-27 00:03:19 +000030#include "Support/STLExtras.h"
John Criswell3ef61af2003-06-30 21:59:07 +000031#include "Config/alloca.h"
Vikram S. Adveab9e5572001-07-21 12:41:50 +000032
33//------------------------------------------------------------------------
34// class InstrTreeNode
35//------------------------------------------------------------------------
36
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000037void
38InstrTreeNode::dump(int dumpChildren, int indent) const
39{
Chris Lattnercc38da72001-09-11 23:52:11 +000040 dumpNode(indent);
Vikram S. Adveab9e5572001-07-21 12:41:50 +000041
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000042 if (dumpChildren)
43 {
44 if (LeftChild)
45 LeftChild->dump(dumpChildren, indent+1);
46 if (RightChild)
47 RightChild->dump(dumpChildren, indent+1);
48 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +000049}
50
51
Chris Lattner16dd09a2001-09-12 01:28:49 +000052InstructionNode::InstructionNode(Instruction* I)
Vikram S. Adve3228f9c2002-03-24 03:24:00 +000053 : InstrTreeNode(NTInstructionNode, I),
54 codeIsFoldedIntoParent(false)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000055{
Chris Lattner16dd09a2001-09-12 01:28:49 +000056 opLabel = I->getOpcode();
Vikram S. Adveab9e5572001-07-21 12:41:50 +000057
58 // Distinguish special cases of some instructions such as Ret and Br
59 //
Chris Lattnerda558102001-10-02 03:41:24 +000060 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000061 {
62 opLabel = RetValueOp; // ret(value) operation
Vikram S. Adveab9e5572001-07-21 12:41:50 +000063 }
Chris Lattnerda558102001-10-02 03:41:24 +000064 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000065 {
66 opLabel = BrCondOp; // br(cond) operation
67 }
68 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
69 {
70 opLabel = SetCCOp; // common label for all SetCC ops
71 }
72 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
73 {
74 opLabel = AllocaN; // Alloca(ptr, N) operation
75 }
Chris Lattnerdfb3a2c2002-08-22 23:37:20 +000076 else if (opLabel == Instruction::GetElementPtr &&
77 cast<GetElementPtrInst>(I)->hasIndices())
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000078 {
Chris Lattnerdfb3a2c2002-08-22 23:37:20 +000079 opLabel = opLabel + 100; // getElem with index vector
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000080 }
Vikram S. Adve3db97eb2002-08-15 14:19:22 +000081 else if (opLabel == Instruction::Xor &&
82 BinaryOperator::isNot(I))
83 {
84 opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator
85 : BNotOp; // bitwise Not operator
86 }
Vikram S. Advec5303722001-11-08 04:47:06 +000087 else if (opLabel == Instruction::And ||
88 opLabel == Instruction::Or ||
Vikram S. Adve3db97eb2002-08-15 14:19:22 +000089 opLabel == Instruction::Xor)
Vikram S. Advec5303722001-11-08 04:47:06 +000090 {
91 // Distinguish bitwise operators from logical operators!
92 if (I->getType() != Type::BoolTy)
93 opLabel = opLabel + 100; // bitwise operator
94 }
Vikram S. Adve0c51cf02001-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. Adveab9e5572001-07-21 12:41:50 +0000118}
119
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000120
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000121void
122InstructionNode::dumpNode(int indent) const
123{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000124 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000125 std::cerr << " ";
126 std::cerr << getInstruction()->getOpcodeName()
127 << " [label " << getOpLabel() << "]" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000128}
129
130
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000131void
132VRegListNode::dumpNode(int indent) const
133{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000134 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000135 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000136
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000137 std::cerr << "List" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000138}
139
140
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000141void
142VRegNode::dumpNode(int indent) const
143{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000144 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000145 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000146
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000147 std::cerr << "VReg " << getValue() << "\t(type "
148 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000149}
150
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000151void
152ConstantNode::dumpNode(int indent) const
153{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000154 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000155 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000156
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000157 std::cerr << "Constant " << getValue() << "\t(type "
158 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000159}
160
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000161void
162LabelNode::dumpNode(int indent) const
163{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000164 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000165 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000166
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000167 std::cerr << "Label " << getValue() << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000168}
169
170//------------------------------------------------------------------------
171// class InstrForest
172//
173// A forest of instruction trees, usually for a single method.
174//------------------------------------------------------------------------
175
Chris Lattner57698e22002-03-26 18:01:55 +0000176InstrForest::InstrForest(Function *F)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000177{
Chris Lattner7076ff22002-06-25 16:13:21 +0000178 for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
179 for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
180 buildTreeForInstruction(I);
Chris Lattner60a65912002-02-12 21:07:25 +0000181 }
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000182}
183
184InstrForest::~InstrForest()
185{
Chris Lattner8690ac12002-04-08 23:09:07 +0000186 for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000187}
188
189void
190InstrForest::dump() const
191{
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000192 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
Chris Lattner16dd09a2001-09-12 01:28:49 +0000193 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000194}
195
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000196inline void
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000197InstrForest::eraseRoot(InstructionNode* node)
198{
199 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
200 RI != RE; ++RI)
201 if (*RI == node)
202 treeRoots.erase(RI.base()-1);
203}
204
205inline void
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000206InstrForest::noteTreeNodeForInstr(Instruction *instr,
207 InstructionNode *treeNode)
208{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000209 (*this)[instr] = treeNode;
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000210 treeRoots.push_back(treeNode); // mark node as root of a new tree
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000211}
212
213
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000214inline void
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000215InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000216{
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000217 parent->LeftChild = child;
218 child->Parent = parent;
Vikram S. Adve872c7f92002-08-24 21:02:09 +0000219 if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
220 eraseRoot(instrNode); // no longer a tree root
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000221}
222
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000223inline void
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000224InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000225{
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000226 parent->RightChild = child;
227 child->Parent = parent;
Vikram S. Adve872c7f92002-08-24 21:02:09 +0000228 if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
229 eraseRoot(instrNode); // no longer a tree root
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000230}
231
232
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000233InstructionNode*
234InstrForest::buildTreeForInstruction(Instruction *instr)
235{
236 InstructionNode *treeNode = getTreeNodeForInstr(instr);
237 if (treeNode)
238 {
239 // treeNode has already been constructed for this instruction
240 assert(treeNode->getInstruction() == instr);
241 return treeNode;
242 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000243
244 // Otherwise, create a new tree node for this instruction.
245 //
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000246 treeNode = new InstructionNode(instr);
247 noteTreeNodeForInstr(instr, treeNode);
248
249 if (instr->getOpcode() == Instruction::Call)
250 { // Operands of call instruction
251 return treeNode;
252 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000253
254 // If the instruction has more than 2 instruction operands,
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000255 // then we need to create artificial list nodes to hold them.
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000256 // (Note that we only count operands that get tree nodes, and not
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000257 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000258 //
259 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000260 // for all appropriate operands and save them in an array. We then
261 // insert children at the end, creating list nodes where needed.
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000262 // As a performance optimization, allocate a child array only
263 // if a fixed array is too small.
264 //
265 int numChildren = 0;
Chris Lattner55491772003-06-16 22:29:09 +0000266 InstrTreeNode** childArray = new InstrTreeNode*[instr->getNumOperands()];
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000267
268 //
269 // Walk the operands of the instruction
270 //
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000271 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
272 {
273 Value* operand = *O;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000274
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000275 // Check if the operand is a data value, not an branch label, type,
276 // method or module. If the operand is an address type (i.e., label
277 // or method) that is used in an non-branching operation, e.g., `add'.
278 // that should be considered a data value.
Chris Lattner16dd09a2001-09-12 01:28:49 +0000279
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000280 // Check latter condition here just to simplify the next IF.
281 bool includeAddressOperand =
Chris Lattner57698e22002-03-26 18:01:55 +0000282 (isa<BasicBlock>(operand) || isa<Function>(operand))
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000283 && !instr->isTerminator();
Chris Lattner16dd09a2001-09-12 01:28:49 +0000284
Chris Lattner38569342001-10-01 20:11:19 +0000285 if (includeAddressOperand || isa<Instruction>(operand) ||
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000286 isa<Constant>(operand) || isa<Argument>(operand) ||
Chris Lattner38569342001-10-01 20:11:19 +0000287 isa<GlobalVariable>(operand))
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000288 {
289 // This operand is a data value
Chris Lattner16dd09a2001-09-12 01:28:49 +0000290
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000291 // An instruction that computes the incoming value is added as a
292 // child of the current instruction if:
293 // the value has only a single use
294 // AND both instructions are in the same basic block.
295 // AND the current instruction is not a PHI (because the incoming
296 // value is conceptually in a predecessor block,
297 // even though it may be in the same static block)
298 //
299 // (Note that if the value has only a single use (viz., `instr'),
300 // the def of the value can be safely moved just before instr
301 // and therefore it is safe to combine these two instructions.)
302 //
303 // In all other cases, the virtual register holding the value
304 // is used directly, i.e., made a child of the instruction node.
305 //
306 InstrTreeNode* opTreeNode;
Chris Lattnerf95d9b92003-10-15 16:48:29 +0000307 if (isa<Instruction>(operand) && operand->hasOneUse() &&
Chris Lattner38569342001-10-01 20:11:19 +0000308 cast<Instruction>(operand)->getParent() == instr->getParent() &&
Chris Lattnerb94550e2003-10-19 21:34:28 +0000309 instr->getOpcode() != Instruction::PHI &&
Vikram S. Adve813ffcc2001-09-30 23:45:08 +0000310 instr->getOpcode() != Instruction::Call)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000311 {
312 // Recursively create a treeNode for it.
313 opTreeNode = buildTreeForInstruction((Instruction*)operand);
314 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000315 else if (Constant *CPV = dyn_cast<Constant>(operand))
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000316 {
317 // Create a leaf node for a constant
318 opTreeNode = new ConstantNode(CPV);
319 }
320 else
321 {
322 // Create a leaf node for the virtual register
323 opTreeNode = new VRegNode(operand);
324 }
Chris Lattner16dd09a2001-09-12 01:28:49 +0000325
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000326 childArray[numChildren++] = opTreeNode;
327 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000328 }
329
330 //--------------------------------------------------------------------
331 // Add any selected operands as children in the tree.
332 // Certain instructions can have more than 2 in some instances (viz.,
333 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
334 // array or struct). Make the operands of every such instruction into
335 // a right-leaning binary tree with the operand nodes at the leaves
336 // and VRegList nodes as internal nodes.
337 //--------------------------------------------------------------------
338
Chris Lattner16dd09a2001-09-12 01:28:49 +0000339 InstrTreeNode *parent = treeNode;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000340
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000341 if (numChildren > 2)
342 {
343 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
Chris Lattnerb94550e2003-10-19 21:34:28 +0000344 assert(instrOpcode == Instruction::PHI ||
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000345 instrOpcode == Instruction::Call ||
346 instrOpcode == Instruction::Load ||
347 instrOpcode == Instruction::Store ||
348 instrOpcode == Instruction::GetElementPtr);
349 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000350
351 // Insert the first child as a direct child
352 if (numChildren >= 1)
Chris Lattner16dd09a2001-09-12 01:28:49 +0000353 setLeftChild(parent, childArray[0]);
354
355 int n;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000356
357 // Create a list node for children 2 .. N-1, if any
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000358 for (n = numChildren-1; n >= 2; n--)
359 {
360 // We have more than two children
361 InstrTreeNode *listNode = new VRegListNode();
362 setRightChild(parent, listNode);
363 setLeftChild(listNode, childArray[numChildren - n]);
364 parent = listNode;
365 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000366
367 // Now insert the last remaining child (if any).
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000368 if (numChildren >= 2)
369 {
370 assert(n == 1);
371 setRightChild(parent, childArray[numChildren - 1]);
372 }
Chris Lattner55491772003-06-16 22:29:09 +0000373
374 delete [] childArray;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000375 return treeNode;
376}