blob: 0ecf1c2fe99d2b8cdc10bf8f6e405d0f67529c37 [file] [log] [blame]
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00001// $Id$
2//---------------------------------------------------------------------------
3// File:
4// InstrForest.cpp
5//
6// Purpose:
7// Convert SSA graph to instruction trees for instruction selection.
8//
9// Strategy:
10// 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
14// aggressive as possible to exploit any possible taret instructions.
15// 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//
20// History:
21// 6/28/01 - Vikram Adve - Created
22//
23//---------------------------------------------------------------------------
24
Chris Lattner942d99e2001-07-21 22:59:56 +000025#include "llvm/CodeGen/InstrForest.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000026#include "llvm/Method.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000027#include "llvm/iTerminators.h"
28#include "llvm/iMemory.h"
29#include "llvm/ConstPoolVals.h"
30#include "llvm/BasicBlock.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000031#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner4ddb4c82001-09-12 01:28:49 +000032#include "llvm/Support/STLExtras.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000033
34//------------------------------------------------------------------------
35// class InstrTreeNode
36//------------------------------------------------------------------------
37
Chris Lattnerd268ad62001-09-11 23:52:11 +000038void InstrTreeNode::dump(int dumpChildren, int indent) const {
39 dumpNode(indent);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000040
Chris Lattner4ddb4c82001-09-12 01:28:49 +000041 if (dumpChildren) {
42 if (leftChild())
43 leftChild()->dump(dumpChildren, indent+1);
44 if (rightChild())
45 rightChild()->dump(dumpChildren, indent+1);
46 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000047}
48
49
Chris Lattner4ddb4c82001-09-12 01:28:49 +000050InstructionNode::InstructionNode(Instruction* I)
51 : InstrTreeNode(NTInstructionNode, I) {
52 opLabel = I->getOpcode();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000053
54 // Distinguish special cases of some instructions such as Ret and Br
55 //
Chris Lattner4ddb4c82001-09-12 01:28:49 +000056 if (opLabel == Instruction::Ret && ((ReturnInst*)I)->getReturnValue()) {
57 opLabel = RetValueOp; // ret(value) operation
58 } else if (opLabel == Instruction::Br &&
59 !((BranchInst*)I)->isUnconditional()) {
60 opLabel = BrCondOp; // br(cond) operation
61 } else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT) {
62 opLabel = SetCCOp; // common label for all SetCC ops
63 } else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0) {
64 opLabel = AllocaN; // Alloca(ptr, N) operation
65 } else if ((opLabel == Instruction::Load ||
66 opLabel == Instruction::GetElementPtr) &&
67 ((MemAccessInst*)I)->getFirstOffsetIdx() > 0) {
68 opLabel = opLabel + 100; // load/getElem with index vector
69 } else if (opLabel == Instruction::Cast) {
70 const Type *ITy = I->getType();
71 switch(ITy->getPrimitiveID()) {
72 case Type::BoolTyID: opLabel = ToBoolTy; break;
73 case Type::UByteTyID: opLabel = ToUByteTy; break;
74 case Type::SByteTyID: opLabel = ToSByteTy; break;
75 case Type::UShortTyID: opLabel = ToUShortTy; break;
76 case Type::ShortTyID: opLabel = ToShortTy; break;
77 case Type::UIntTyID: opLabel = ToUIntTy; break;
78 case Type::IntTyID: opLabel = ToIntTy; break;
79 case Type::ULongTyID: opLabel = ToULongTy; break;
80 case Type::LongTyID: opLabel = ToLongTy; break;
81 case Type::FloatTyID: opLabel = ToFloatTy; break;
82 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
83 case Type::ArrayTyID: opLabel = ToArrayTy; break;
84 case Type::PointerTyID: opLabel = ToPointerTy; break;
85 default:
86 // Just use `Cast' opcode otherwise. It's probably ignored.
87 break;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000088 }
Chris Lattner4ddb4c82001-09-12 01:28:49 +000089 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000090}
91
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000092
Chris Lattner4ddb4c82001-09-12 01:28:49 +000093void InstructionNode::dumpNode(int indent) const {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000094 for (int i=0; i < indent; i++)
95 cout << " ";
96
97 cout << getInstruction()->getOpcodeName();
98
Chris Lattner4ddb4c82001-09-12 01:28:49 +000099 const vector<MachineInstr*> &mvec = getInstruction()->getMachineInstrVec();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000100 if (mvec.size() > 0)
101 cout << "\tMachine Instructions: ";
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000102 for (unsigned int i=0; i < mvec.size(); i++) {
103 mvec[i]->dump(0);
104 if (i < mvec.size() - 1)
105 cout << "; ";
106 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000107
108 cout << endl;
109}
110
111
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000112void VRegListNode::dumpNode(int indent) const {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000113 for (int i=0; i < indent; i++)
114 cout << " ";
115
116 cout << "List" << endl;
117}
118
119
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000120void VRegNode::dumpNode(int indent) const {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000121 for (int i=0; i < indent; i++)
122 cout << " ";
123
124 cout << "VReg " << getValue() << "\t(type "
125 << (int) getValue()->getValueType() << ")" << endl;
126}
127
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000128void ConstantNode::dumpNode(int indent) const {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000129 for (int i=0; i < indent; i++)
130 cout << " ";
131
132 cout << "Constant " << getValue() << "\t(type "
133 << (int) getValue()->getValueType() << ")" << endl;
134}
135
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000136void LabelNode::dumpNode(int indent) const {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000137 for (int i=0; i < indent; i++)
138 cout << " ";
139
140 cout << "Label " << getValue() << endl;
141}
142
143//------------------------------------------------------------------------
144// class InstrForest
145//
146// A forest of instruction trees, usually for a single method.
147//------------------------------------------------------------------------
148
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000149void InstrForest::dump() const {
150 for (hash_set<InstructionNode*>::const_iterator I = treeRoots.begin();
151 I != treeRoots.end(); ++I)
152 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000153}
154
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000155inline void InstrForest::noteTreeNodeForInstr(Instruction *instr,
156 InstructionNode *treeNode) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000157 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
158 (*this)[instr] = treeNode;
159 treeRoots.insert(treeNode); // mark node as root of a new tree
160}
161
162
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000163inline void InstrForest::setLeftChild(InstrTreeNode *Par, InstrTreeNode *Chld) {
164 Par->LeftChild = Chld;
165 Chld->Parent = Par;
166 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
167 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000168}
169
170
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000171inline void InstrForest::setRightChild(InstrTreeNode *Par, InstrTreeNode *Chld){
172 Par->RightChild = Chld;
173 Chld->Parent = Par;
174 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
175 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000176}
177
178
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000179InstructionNode *InstrForest::buildTreeForInstruction(Instruction *Inst) {
180 InstructionNode *treeNode = getTreeNodeForInstr(Inst);
181 if (treeNode) {
182 // treeNode has already been constructed for this instruction
183 assert(treeNode->getInstruction() == Inst);
184 return treeNode;
185 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000186
187 // Otherwise, create a new tree node for this instruction.
188 //
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000189 treeNode = new InstructionNode(Inst);
190 noteTreeNodeForInstr(Inst, treeNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000191
192 // If the instruction has more than 2 instruction operands,
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000193 // then we need to create artificial list nodes to hold them.
194 // (Note that we only not count operands that get tree nodes, and not
195 // others such as branch labels for a branch or switch instruction.)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000196 //
197 // To do this efficiently, we'll walk all operands, build treeNodes
Vikram S. Advee4e77f92001-07-31 21:49:53 +0000198 // for all appropriate operands and save them in an array. We then
199 // insert children at the end, creating list nodes where needed.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000200 // As a performance optimization, allocate a child array only
201 // if a fixed array is too small.
202 //
203 int numChildren = 0;
204 const unsigned int MAX_CHILD = 8;
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000205 static InstrTreeNode *fixedChildArray[MAX_CHILD];
206 InstrTreeNode **childArray =
207 (Inst->getNumOperands() > MAX_CHILD)
Chris Lattner5f6baf72001-09-12 16:34:03 +0000208 ? new (InstrTreeNode*)[Inst->getNumOperands()] : fixedChildArray;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000209
210 //
211 // Walk the operands of the instruction
212 //
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000213 for (Instruction::op_iterator O = Inst->op_begin(); O != Inst->op_end(); ++O){
214 Value* operand = *O;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000215
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000216 // Check if the operand is a data value, not an branch label, type,
217 // method or module. If the operand is an address type (i.e., label
218 // or method) that is used in an non-branching operation, e.g., `add'.
219 // that should be considered a data value.
220
221 // Check latter condition here just to simplify the next IF.
222 bool includeAddressOperand =
223 (operand->isBasicBlock() || operand->isMethod())
224 && !Inst->isTerminator();
225
226 if (includeAddressOperand || operand->isInstruction() ||
227 operand->isConstant() || operand->isMethodArgument()) {
228 // This operand is a data value
229
230 // An instruction that computes the incoming value is added as a
231 // child of the current instruction if:
232 // the value has only a single use
233 // AND both instructions are in the same basic block.
234 //
235 // (Note that if the value has only a single use (viz., `instr'),
236 // the def of the value can be safely moved just before instr
237 // and therefore it is safe to combine these two instructions.)
238 //
239 // In all other cases, the virtual register holding the value
240 // is used directly, i.e., made a child of the instruction node.
241 //
242 InstrTreeNode* opTreeNode;
243 if (operand->isInstruction() && operand->use_size() == 1 &&
244 ((Instruction*)operand)->getParent() == Inst->getParent()) {
245 // Recursively create a treeNode for it.
246 opTreeNode = buildTreeForInstruction((Instruction*)operand);
247 } else if (ConstPoolVal *CPV = operand->castConstant()) {
248 // Create a leaf node for a constant
249 opTreeNode = new ConstantNode(CPV);
250 } else {
251 // Create a leaf node for the virtual register
252 opTreeNode = new VRegNode(operand);
253 }
254
255 childArray[numChildren++] = opTreeNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000256 }
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000257 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000258
259 //--------------------------------------------------------------------
260 // Add any selected operands as children in the tree.
261 // Certain instructions can have more than 2 in some instances (viz.,
262 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
263 // array or struct). Make the operands of every such instruction into
264 // a right-leaning binary tree with the operand nodes at the leaves
265 // and VRegList nodes as internal nodes.
266 //--------------------------------------------------------------------
267
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000268 InstrTreeNode *parent = treeNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000269
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000270 if (numChildren > 2) {
271 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
272 assert(instrOpcode == Instruction::PHINode ||
273 instrOpcode == Instruction::Call ||
274 instrOpcode == Instruction::Load ||
275 instrOpcode == Instruction::Store ||
276 instrOpcode == Instruction::GetElementPtr);
277 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000278
279 // Insert the first child as a direct child
280 if (numChildren >= 1)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000281 setLeftChild(parent, childArray[0]);
282
283 int n;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000284
285 // Create a list node for children 2 .. N-1, if any
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000286 for (n = numChildren-1; n >= 2; n--) {
287 // We have more than two children
288 InstrTreeNode *listNode = new VRegListNode();
289 setRightChild(parent, listNode);
290 setLeftChild(listNode, childArray[numChildren - n]);
291 parent = listNode;
292 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000293
294 // Now insert the last remaining child (if any).
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000295 if (numChildren >= 2) {
296 assert(n == 1);
297 setRightChild(parent, childArray[numChildren - 1]);
298 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000299
300 if (childArray != fixedChildArray)
Chris Lattner4ddb4c82001-09-12 01:28:49 +0000301 delete [] childArray;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000302
303 return treeNode;
304}
305
Chris Lattner5f6baf72001-09-12 16:34:03 +0000306
307InstrForest::InstrForest(Method *M) {
308 for_each(M->inst_begin(), M->inst_end(),
309 bind_obj(this, &InstrForest::buildTreeForInstruction));
310}