blob: fd5056d22d7bd6a0fdc94300c620a713690ceceb [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
Misha Brukmanc7b1bce2003-10-23 17:39:37 +000022#include "llvm/Constant.h"
Chris Lattner57698e22002-03-26 18:01:55 +000023#include "llvm/Function.h"
Vikram S. Adveab9e5572001-07-21 12:41:50 +000024#include "llvm/iTerminators.h"
25#include "llvm/iMemory.h"
Chris Lattner70411b02002-04-29 18:48:55 +000026#include "llvm/Type.h"
Misha Brukmanc7b1bce2003-10-23 17:39:37 +000027#include "llvm/CodeGen/InstrForest.h"
28#include "llvm/CodeGen/MachineCodeForInstruction.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
Brian Gaeke960707c2003-11-11 22:41:34 +000033namespace llvm {
34
Vikram S. Adveab9e5572001-07-21 12:41:50 +000035//------------------------------------------------------------------------
36// class InstrTreeNode
37//------------------------------------------------------------------------
38
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000039void
Misha Brukmanc7b1bce2003-10-23 17:39:37 +000040InstrTreeNode::dump(int dumpChildren, int indent) const {
Chris Lattnercc38da72001-09-11 23:52:11 +000041 dumpNode(indent);
Vikram S. Adveab9e5572001-07-21 12:41:50 +000042
Misha Brukmanc7b1bce2003-10-23 17:39:37 +000043 if (dumpChildren) {
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)
Misha Brukmanc7b1bce2003-10-23 17:39:37 +000053 : InstrTreeNode(NTInstructionNode, I), codeIsFoldedIntoParent(false)
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000054{
Chris Lattner16dd09a2001-09-12 01:28:49 +000055 opLabel = I->getOpcode();
Vikram S. Adveab9e5572001-07-21 12:41:50 +000056
57 // Distinguish special cases of some instructions such as Ret and Br
58 //
Misha Brukmanc7b1bce2003-10-23 17:39:37 +000059 if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue()) {
60 opLabel = RetValueOp; // ret(value) operation
61 }
Chris Lattnerda558102001-10-02 03:41:24 +000062 else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
Misha Brukmanc7b1bce2003-10-23 17:39:37 +000063 {
64 opLabel = BrCondOp; // br(cond) operation
65 } else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT) {
66 opLabel = SetCCOp; // common label for all SetCC ops
67 } else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0) {
68 opLabel = AllocaN; // Alloca(ptr, N) operation
69 } else if (opLabel == Instruction::GetElementPtr &&
70 cast<GetElementPtrInst>(I)->hasIndices()) {
71 opLabel = opLabel + 100; // getElem with index vector
72 } else if (opLabel == Instruction::Xor &&
73 BinaryOperator::isNot(I)) {
74 opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator
75 : BNotOp; // bitwise Not operator
76 } else if (opLabel == Instruction::And || opLabel == Instruction::Or ||
77 opLabel == Instruction::Xor) {
78 // Distinguish bitwise operators from logical operators!
79 if (I->getType() != Type::BoolTy)
80 opLabel = opLabel + 100; // bitwise operator
81 } else if (opLabel == Instruction::Cast) {
82 const Type *ITy = I->getType();
83 switch(ITy->getPrimitiveID())
Vikram S. Adve0c51cf02001-09-18 12:54:27 +000084 {
Misha Brukmanc7b1bce2003-10-23 17:39:37 +000085 case Type::BoolTyID: opLabel = ToBoolTy; break;
86 case Type::UByteTyID: opLabel = ToUByteTy; break;
87 case Type::SByteTyID: opLabel = ToSByteTy; break;
88 case Type::UShortTyID: opLabel = ToUShortTy; break;
89 case Type::ShortTyID: opLabel = ToShortTy; break;
90 case Type::UIntTyID: opLabel = ToUIntTy; break;
91 case Type::IntTyID: opLabel = ToIntTy; break;
92 case Type::ULongTyID: opLabel = ToULongTy; break;
93 case Type::LongTyID: opLabel = ToLongTy; break;
94 case Type::FloatTyID: opLabel = ToFloatTy; break;
95 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
96 case Type::ArrayTyID: opLabel = ToArrayTy; break;
97 case Type::PointerTyID: opLabel = ToPointerTy; break;
98 default:
99 // Just use `Cast' opcode otherwise. It's probably ignored.
100 break;
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000101 }
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000102 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000103}
104
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000105
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000106void
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000107InstructionNode::dumpNode(int indent) const {
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000108 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000109 std::cerr << " ";
110 std::cerr << getInstruction()->getOpcodeName()
111 << " [label " << getOpLabel() << "]" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000112}
113
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000114void
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000115VRegListNode::dumpNode(int indent) const {
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 << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000118
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000119 std::cerr << "List" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000120}
121
122
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000123void
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000124VRegNode::dumpNode(int indent) const {
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000125 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000126 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000127
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000128 std::cerr << "VReg " << getValue() << "\t(type "
129 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000130}
131
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000132void
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000133ConstantNode::dumpNode(int indent) const {
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 << "Constant " << getValue() << "\t(type "
138 << (int) getValue()->getValueType() << ")" << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000139}
140
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000141void LabelNode::dumpNode(int indent) const {
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000142 for (int i=0; i < indent; i++)
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000143 std::cerr << " ";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000144
Chris Lattner5b7e3ca2003-06-16 22:18:28 +0000145 std::cerr << "Label " << getValue() << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000146}
147
148//------------------------------------------------------------------------
149// class InstrForest
150//
151// A forest of instruction trees, usually for a single method.
152//------------------------------------------------------------------------
153
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000154InstrForest::InstrForest(Function *F) {
Chris Lattner7076ff22002-06-25 16:13:21 +0000155 for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
156 for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
157 buildTreeForInstruction(I);
Chris Lattner60a65912002-02-12 21:07:25 +0000158 }
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000159}
160
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000161InstrForest::~InstrForest() {
Chris Lattner8690ac12002-04-08 23:09:07 +0000162 for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000163}
164
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000165void InstrForest::dump() const {
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000166 for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
Chris Lattner16dd09a2001-09-12 01:28:49 +0000167 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000168}
169
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000170inline void InstrForest::eraseRoot(InstructionNode* node) {
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000171 for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
172 RI != RE; ++RI)
173 if (*RI == node)
174 treeRoots.erase(RI.base()-1);
175}
176
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000177inline void InstrForest::noteTreeNodeForInstr(Instruction *instr,
178 InstructionNode *treeNode) {
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000179 (*this)[instr] = treeNode;
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000180 treeRoots.push_back(treeNode); // mark node as root of a new tree
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000181}
182
183
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000184inline void InstrForest::setLeftChild(InstrTreeNode *parent,
185 InstrTreeNode *child) {
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000186 parent->LeftChild = child;
187 child->Parent = parent;
Vikram S. Adve872c7f92002-08-24 21:02:09 +0000188 if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
189 eraseRoot(instrNode); // no longer a tree root
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000190}
191
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000192inline void InstrForest::setRightChild(InstrTreeNode *parent,
193 InstrTreeNode *child) {
Vikram S. Adve3228f9c2002-03-24 03:24:00 +0000194 parent->RightChild = child;
195 child->Parent = parent;
Vikram S. Adve872c7f92002-08-24 21:02:09 +0000196 if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
197 eraseRoot(instrNode); // no longer a tree root
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000198}
199
200
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000201InstructionNode* InstrForest::buildTreeForInstruction(Instruction *instr) {
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000202 InstructionNode *treeNode = getTreeNodeForInstr(instr);
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000203 if (treeNode) {
204 // treeNode has already been constructed for this instruction
205 assert(treeNode->getInstruction() == instr);
206 return treeNode;
207 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000208
209 // Otherwise, create a new tree node for this instruction.
210 //
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000211 treeNode = new InstructionNode(instr);
212 noteTreeNodeForInstr(instr, treeNode);
213
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000214 if (instr->getOpcode() == Instruction::Call) {
215 // Operands of call instruction
216 return treeNode;
217 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000218
219 // If the instruction has more than 2 instruction operands,
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000220 // then we need to create artificial list nodes to hold them.
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000221 // (Note that we only count operands that get tree nodes, and not
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000222 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000223 //
224 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Adve1c73bc12001-07-31 21:49:53 +0000225 // for all appropriate operands and save them in an array. We then
226 // insert children at the end, creating list nodes where needed.
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000227 // As a performance optimization, allocate a child array only
228 // if a fixed array is too small.
229 //
230 int numChildren = 0;
Chris Lattner55491772003-06-16 22:29:09 +0000231 InstrTreeNode** childArray = new InstrTreeNode*[instr->getNumOperands()];
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000232
233 //
234 // Walk the operands of the instruction
235 //
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000236 for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
237 {
238 Value* operand = *O;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000239
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000240 // Check if the operand is a data value, not an branch label, type,
241 // method or module. If the operand is an address type (i.e., label
242 // or method) that is used in an non-branching operation, e.g., `add'.
243 // that should be considered a data value.
Chris Lattner16dd09a2001-09-12 01:28:49 +0000244
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000245 // Check latter condition here just to simplify the next IF.
246 bool includeAddressOperand =
Chris Lattner57698e22002-03-26 18:01:55 +0000247 (isa<BasicBlock>(operand) || isa<Function>(operand))
Vikram S. Adve0c51cf02001-09-18 12:54:27 +0000248 && !instr->isTerminator();
Chris Lattner16dd09a2001-09-12 01:28:49 +0000249
Chris Lattner38569342001-10-01 20:11:19 +0000250 if (includeAddressOperand || isa<Instruction>(operand) ||
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000251 isa<Constant>(operand) || isa<Argument>(operand) ||
Chris Lattner38569342001-10-01 20:11:19 +0000252 isa<GlobalVariable>(operand))
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000253 {
254 // This operand is a data value
Chris Lattner16dd09a2001-09-12 01:28:49 +0000255
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000256 // An instruction that computes the incoming value is added as a
257 // child of the current instruction if:
258 // the value has only a single use
259 // AND both instructions are in the same basic block.
260 // AND the current instruction is not a PHI (because the incoming
261 // value is conceptually in a predecessor block,
262 // even though it may be in the same static block)
263 //
264 // (Note that if the value has only a single use (viz., `instr'),
265 // the def of the value can be safely moved just before instr
266 // and therefore it is safe to combine these two instructions.)
267 //
268 // In all other cases, the virtual register holding the value
269 // is used directly, i.e., made a child of the instruction node.
270 //
271 InstrTreeNode* opTreeNode;
272 if (isa<Instruction>(operand) && operand->hasOneUse() &&
273 cast<Instruction>(operand)->getParent() == instr->getParent() &&
274 instr->getOpcode() != Instruction::PHI &&
275 instr->getOpcode() != Instruction::Call)
276 {
277 // Recursively create a treeNode for it.
278 opTreeNode = buildTreeForInstruction((Instruction*)operand);
279 } else if (Constant *CPV = dyn_cast<Constant>(operand)) {
280 // Create a leaf node for a constant
281 opTreeNode = new ConstantNode(CPV);
282 } else {
283 // Create a leaf node for the virtual register
284 opTreeNode = new VRegNode(operand);
285 }
Chris Lattner16dd09a2001-09-12 01:28:49 +0000286
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000287 childArray[numChildren++] = opTreeNode;
288 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000289 }
290
291 //--------------------------------------------------------------------
292 // Add any selected operands as children in the tree.
293 // Certain instructions can have more than 2 in some instances (viz.,
294 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
295 // array or struct). Make the operands of every such instruction into
296 // a right-leaning binary tree with the operand nodes at the leaves
297 // and VRegList nodes as internal nodes.
298 //--------------------------------------------------------------------
299
Chris Lattner16dd09a2001-09-12 01:28:49 +0000300 InstrTreeNode *parent = treeNode;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000301
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000302 if (numChildren > 2) {
303 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
304 assert(instrOpcode == Instruction::PHI ||
305 instrOpcode == Instruction::Call ||
306 instrOpcode == Instruction::Load ||
307 instrOpcode == Instruction::Store ||
308 instrOpcode == Instruction::GetElementPtr);
309 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000310
311 // Insert the first child as a direct child
312 if (numChildren >= 1)
Chris Lattner16dd09a2001-09-12 01:28:49 +0000313 setLeftChild(parent, childArray[0]);
314
315 int n;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000316
317 // Create a list node for children 2 .. N-1, if any
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000318 for (n = numChildren-1; n >= 2; n--) {
319 // We have more than two children
320 InstrTreeNode *listNode = new VRegListNode();
321 setRightChild(parent, listNode);
322 setLeftChild(listNode, childArray[numChildren - n]);
323 parent = listNode;
324 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000325
326 // Now insert the last remaining child (if any).
Misha Brukmanc7b1bce2003-10-23 17:39:37 +0000327 if (numChildren >= 2) {
328 assert(n == 1);
329 setRightChild(parent, childArray[numChildren - 1]);
330 }
Chris Lattner55491772003-06-16 22:29:09 +0000331
332 delete [] childArray;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000333 return treeNode;
334}
Brian Gaeke960707c2003-11-11 22:41:34 +0000335
336} // End llvm namespace