blob: 7ae237376452dfc9c4986fdbeb2ce0dea3563898 [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
Misha Brukman02fe6b72003-09-17 21:34:23 +00007// aggressive as possible to exploit any possible target instructions.
Vikram S. Adveab9e5572001-07-21 12:41:50 +00008// 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"
John Criswell3ef61af2003-06-30 21:59:07 +000024#include "Config/alloca.h"
Vikram S. Adveab9e5572001-07-21 12:41:50 +000025
26//------------------------------------------------------------------------
27// class InstrTreeNode
28//------------------------------------------------------------------------
29
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000030void
31InstrTreeNode::dump(int dumpChildren, int indent) const
32{
Chris Lattnercc38da72001-09-11 23:52:11 +000033 dumpNode(indent);
Vikram S. Adveab9e5572001-07-21 12:41:50 +000034
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000035 if (dumpChildren)
36 {
37 if (LeftChild)
38 LeftChild->dump(dumpChildren, indent+1);
39 if (RightChild)
40 RightChild->dump(dumpChildren, indent+1);
41 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +000042}
43
44
Chris Lattner16dd09a2001-09-12 01:28:49 +000045InstructionNode::InstructionNode(Instruction* I)
Vikram S. Adve3228f9c2002-03-24 03:24:00 +000046 : InstrTreeNode(NTInstructionNode, I),
47 codeIsFoldedIntoParent(false)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000048{
Chris Lattner16dd09a2001-09-12 01:28:49 +000049 opLabel = I->getOpcode();
Vikram S. Adveab9e5572001-07-21 12:41:50 +000050
51 // Distinguish special cases of some instructions such as Ret and Br
52 //
Chris Lattnerda558102001-10-02 03:41:24 +000053 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000054 {
55 opLabel = RetValueOp; // ret(value) operation
Vikram S. Adveab9e5572001-07-21 12:41:50 +000056 }
Chris Lattnerda558102001-10-02 03:41:24 +000057 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000058 {
59 opLabel = BrCondOp; // br(cond) operation
60 }
61 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
62 {
63 opLabel = SetCCOp; // common label for all SetCC ops
64 }
65 else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
66 {
67 opLabel = AllocaN; // Alloca(ptr, N) operation
68 }
Chris Lattnerdfb3a2c2002-08-22 23:37:20 +000069 else if (opLabel == Instruction::GetElementPtr &&
70 cast<GetElementPtrInst>(I)->hasIndices())
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000071 {
Chris Lattnerdfb3a2c2002-08-22 23:37:20 +000072 opLabel = opLabel + 100; // getElem with index vector
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000073 }
Vikram S. Adve3db97eb2002-08-15 14:19:22 +000074 else if (opLabel == Instruction::Xor &&
75 BinaryOperator::isNot(I))
76 {
77 opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator
78 : BNotOp; // bitwise Not operator
79 }
Vikram S. Advec5303722001-11-08 04:47:06 +000080 else if (opLabel == Instruction::And ||
81 opLabel == Instruction::Or ||
Vikram S. Adve3db97eb2002-08-15 14:19:22 +000082 opLabel == Instruction::Xor)
Vikram S. Advec5303722001-11-08 04:47:06 +000083 {
84 // Distinguish bitwise operators from logical operators!
85 if (I->getType() != Type::BoolTy)
86 opLabel = opLabel + 100; // bitwise operator
87 }
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000088 else if (opLabel == Instruction::Cast)
89 {
90 const Type *ITy = I->getType();
91 switch(ITy->getPrimitiveID())
92 {
93 case Type::BoolTyID: opLabel = ToBoolTy; break;
94 case Type::UByteTyID: opLabel = ToUByteTy; break;
95 case Type::SByteTyID: opLabel = ToSByteTy; break;
96 case Type::UShortTyID: opLabel = ToUShortTy; break;
97 case Type::ShortTyID: opLabel = ToShortTy; break;
98 case Type::UIntTyID: opLabel = ToUIntTy; break;
99 case Type::IntTyID: opLabel = ToIntTy; break;
100 case Type::ULongTyID: opLabel = ToULongTy; break;
101 case Type::LongTyID: opLabel = ToLongTy; break;
102 case Type::FloatTyID: opLabel = ToFloatTy; break;
103 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
104 case Type::ArrayTyID: opLabel = ToArrayTy; break;
105 case Type::PointerTyID: opLabel = ToPointerTy; break;
106 default:
107 // Just use `Cast' opcode otherwise. It's probably ignored.
108 break;
109 }
110 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000111}
112
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000113
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000114void
115InstructionNode::dumpNode(int indent) const
116{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000117 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000118 std::cerr << " ";
119 std::cerr << getInstruction()->getOpcodeName()
120 << " [label " << getOpLabel() << "]" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000121}
122
123
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000124void
125VRegListNode::dumpNode(int indent) const
126{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000127 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000128 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000129
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000130 std::cerr << "List" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000131}
132
133
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000134void
135VRegNode::dumpNode(int indent) const
136{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000137 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000138 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000139
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000140 std::cerr << "VReg " << getValue() << "\t(type "
141 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000142}
143
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000144void
145ConstantNode::dumpNode(int indent) const
146{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000147 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000148 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000149
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000150 std::cerr << "Constant " << getValue() << "\t(type "
151 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000152}
153
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000154void
155LabelNode::dumpNode(int indent) const
156{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000157 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000158 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000159
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000160 std::cerr << "Label " << getValue() << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000161}
162
163//------------------------------------------------------------------------
164// class InstrForest
165//
166// A forest of instruction trees, usually for a single method.
167//------------------------------------------------------------------------
168
Chris Lattner57698e22002-03-26 18:01:55 +0000169InstrForest::InstrForest(Function *F)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000170{
Chris Lattner7076ff22002-06-25 16:13:21 +0000171 for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
172 for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
173 buildTreeForInstruction(I);
Chris Lattner60a65912002-02-12 21:07:25 +0000174 }
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000175}
176
177InstrForest::~InstrForest()
178{
Chris Lattner8690ac12002-04-08 23:09:07 +0000179 for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000180}
181
182void
183InstrForest::dump() const
184{
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000185 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
Chris Lattner16dd09a2001-09-12 01:28:49 +0000186 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000187}
188
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000189inline void
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000190InstrForest::eraseRoot(InstructionNode* node)
191{
192 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
193 RI != RE; ++RI)
194 if (*RI == node)
195 treeRoots.erase(RI.base()-1);
196}
197
198inline void
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000199InstrForest::noteTreeNodeForInstr(Instruction *instr,
200 InstructionNode *treeNode)
201{
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000202 (*this)[instr] = treeNode;
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000203 treeRoots.push_back(treeNode); // mark node as root of a new tree
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000204}
205
206
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000207inline void
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000208InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000209{
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000210 parent->LeftChild = child;
211 child->Parent = parent;
Vikram S. Adve872c7f92002-08-24 21:02:09 +0000212 if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
213 eraseRoot(instrNode); // no longer a tree root
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000214}
215
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000216inline void
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000217InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000218{
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000219 parent->RightChild = child;
220 child->Parent = parent;
Vikram S. Adve872c7f92002-08-24 21:02:09 +0000221 if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
222 eraseRoot(instrNode); // no longer a tree root
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000223}
224
225
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000226InstructionNode*
227InstrForest::buildTreeForInstruction(Instruction *instr)
228{
229 InstructionNode *treeNode = getTreeNodeForInstr(instr);
230 if (treeNode)
231 {
232 // treeNode has already been constructed for this instruction
233 assert(treeNode->getInstruction() == instr);
234 return treeNode;
235 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000236
237 // Otherwise, create a new tree node for this instruction.
238 //
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000239 treeNode = new InstructionNode(instr);
240 noteTreeNodeForInstr(instr, treeNode);
241
242 if (instr->getOpcode() == Instruction::Call)
243 { // Operands of call instruction
244 return treeNode;
245 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000246
247 // If the instruction has more than 2 instruction operands,
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000248 // then we need to create artificial list nodes to hold them.
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000249 // (Note that we only count operands that get tree nodes, and not
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000250 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000251 //
252 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000253 // for all appropriate operands and save them in an array. We then
254 // insert children at the end, creating list nodes where needed.
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000255 // As a performance optimization, allocate a child array only
256 // if a fixed array is too small.
257 //
258 int numChildren = 0;
Chris Lattner55491772003-06-16 22:29:09 +0000259 InstrTreeNode** childArray = new InstrTreeNode*[instr->getNumOperands()];
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000260
261 //
262 // Walk the operands of the instruction
263 //
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000264 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
265 {
266 Value* operand = *O;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000267
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000268 // Check if the operand is a data value, not an branch label, type,
269 // method or module. If the operand is an address type (i.e., label
270 // or method) that is used in an non-branching operation, e.g., `add'.
271 // that should be considered a data value.
Chris Lattner16dd09a2001-09-12 01:28:49 +0000272
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000273 // Check latter condition here just to simplify the next IF.
274 bool includeAddressOperand =
Chris Lattner57698e22002-03-26 18:01:55 +0000275 (isa<BasicBlock>(operand) || isa<Function>(operand))
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000276 && !instr->isTerminator();
Chris Lattner16dd09a2001-09-12 01:28:49 +0000277
Chris Lattner38569342001-10-01 20:11:19 +0000278 if (includeAddressOperand || isa<Instruction>(operand) ||
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000279 isa<Constant>(operand) || isa<Argument>(operand) ||
Chris Lattner38569342001-10-01 20:11:19 +0000280 isa<GlobalVariable>(operand))
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000281 {
282 // This operand is a data value
Chris Lattner16dd09a2001-09-12 01:28:49 +0000283
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000284 // An instruction that computes the incoming value is added as a
285 // child of the current instruction if:
286 // the value has only a single use
287 // AND both instructions are in the same basic block.
288 // AND the current instruction is not a PHI (because the incoming
289 // value is conceptually in a predecessor block,
290 // even though it may be in the same static block)
291 //
292 // (Note that if the value has only a single use (viz., `instr'),
293 // the def of the value can be safely moved just before instr
294 // and therefore it is safe to combine these two instructions.)
295 //
296 // In all other cases, the virtual register holding the value
297 // is used directly, i.e., made a child of the instruction node.
298 //
299 InstrTreeNode* opTreeNode;
Chris Lattnerf95d9b92003-10-15 16:48:29 +0000300 if (isa<Instruction>(operand) && operand->hasOneUse() &&
Chris Lattner38569342001-10-01 20:11:19 +0000301 cast<Instruction>(operand)->getParent() == instr->getParent() &&
Chris Lattnerb94550e2003-10-19 21:34:28 +0000302 instr->getOpcode() != Instruction::PHI &&
Vikram S. Adve813ffcc2001-09-30 23:45:08 +0000303 instr->getOpcode() != Instruction::Call)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000304 {
305 // Recursively create a treeNode for it.
306 opTreeNode = buildTreeForInstruction((Instruction*)operand);
307 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000308 else if (Constant *CPV = dyn_cast<Constant>(operand))
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000309 {
310 // Create a leaf node for a constant
311 opTreeNode = new ConstantNode(CPV);
312 }
313 else
314 {
315 // Create a leaf node for the virtual register
316 opTreeNode = new VRegNode(operand);
317 }
Chris Lattner16dd09a2001-09-12 01:28:49 +0000318
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000319 childArray[numChildren++] = opTreeNode;
320 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000321 }
322
323 //--------------------------------------------------------------------
324 // Add any selected operands as children in the tree.
325 // Certain instructions can have more than 2 in some instances (viz.,
326 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
327 // array or struct). Make the operands of every such instruction into
328 // a right-leaning binary tree with the operand nodes at the leaves
329 // and VRegList nodes as internal nodes.
330 //--------------------------------------------------------------------
331
Chris Lattner16dd09a2001-09-12 01:28:49 +0000332 InstrTreeNode *parent = treeNode;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000333
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000334 if (numChildren > 2)
335 {
336 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
Chris Lattnerb94550e2003-10-19 21:34:28 +0000337 assert(instrOpcode == Instruction::PHI ||
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000338 instrOpcode == Instruction::Call ||
339 instrOpcode == Instruction::Load ||
340 instrOpcode == Instruction::Store ||
341 instrOpcode == Instruction::GetElementPtr);
342 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000343
344 // Insert the first child as a direct child
345 if (numChildren >= 1)
Chris Lattner16dd09a2001-09-12 01:28:49 +0000346 setLeftChild(parent, childArray[0]);
347
348 int n;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000349
350 // Create a list node for children 2 .. N-1, if any
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000351 for (n = numChildren-1; n >= 2; n--)
352 {
353 // We have more than two children
354 InstrTreeNode *listNode = new VRegListNode();
355 setRightChild(parent, listNode);
356 setLeftChild(listNode, childArray[numChildren - n]);
357 parent = listNode;
358 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000359
360 // Now insert the last remaining child (if any).
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000361 if (numChildren >= 2)
362 {
363 assert(n == 1);
364 setRightChild(parent, childArray[numChildren - 1]);
365 }
Chris Lattner55491772003-06-16 22:29:09 +0000366
367 delete [] childArray;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000368 return treeNode;
369}