blob: ddac9fec92a01455492e29d526b8c1696043296a [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
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000025//*************************** User Include Files ***************************/
26
Chris Lattner942d99e2001-07-21 22:59:56 +000027#include "llvm/CodeGen/InstrForest.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000028#include "llvm/Method.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000029#include "llvm/iTerminators.h"
30#include "llvm/iMemory.h"
31#include "llvm/ConstPoolVals.h"
32#include "llvm/BasicBlock.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000033#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000034
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000035
36//------------------------------------------------------------------------
37// class InstrTreeNode
38//------------------------------------------------------------------------
39
40
41InstrTreeNode::InstrTreeNode(InstrTreeNodeType nodeType,
42 Value* _val)
43 : treeNodeType(nodeType),
44 val(_val)
45{
46 basicNode.leftChild = NULL;
47 basicNode.rightChild = NULL;
48 basicNode.parent = NULL;
49 basicNode.opLabel = InvalidOp;
50 basicNode.treeNodePtr = this;
51}
52
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000053void
54InstrTreeNode::dump(int dumpChildren,
55 int indent) const
56{
57 this->dumpNode(indent);
58
59 if (dumpChildren)
60 {
61 if (leftChild())
62 leftChild()->dump(dumpChildren, indent+1);
63 if (rightChild())
64 rightChild()->dump(dumpChildren, indent+1);
65 }
66}
67
68
69InstructionNode::InstructionNode(Instruction* _instr)
70 : InstrTreeNode(NTInstructionNode, _instr)
71{
72 OpLabel opLabel = _instr->getOpcode();
73
74 // Distinguish special cases of some instructions such as Ret and Br
75 //
76 if (opLabel == Instruction::Ret && ((ReturnInst*) _instr)->getReturnValue())
77 {
78 opLabel = RetValueOp; // ret(value) operation
79 }
80 else if (opLabel == Instruction::Br && ! ((BranchInst*) _instr)->isUnconditional())
81 {
82 opLabel = BrCondOp; // br(cond) operation
83 }
84 else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
85 {
86 opLabel = SetCCOp; // common label for all SetCC ops
87 }
88 else if (opLabel == Instruction::Alloca && _instr->getNumOperands() > 0)
89 {
90 opLabel = AllocaN; // Alloca(ptr, N) operation
91 }
92 else if ((opLabel == Instruction::Load ||
93 opLabel == Instruction::GetElementPtr)
94 && ((MemAccessInst*)_instr)->getFirstOffsetIdx() > 0)
95 {
96 opLabel = opLabel + 100; // load/getElem with index vector
97 }
98 else if (opLabel == Instruction::Cast)
99 {
100 const Type* instrValueType = _instr->getType();
101 switch(instrValueType->getPrimitiveID())
102 {
103 case Type::BoolTyID: opLabel = ToBoolTy; break;
104 case Type::UByteTyID: opLabel = ToUByteTy; break;
105 case Type::SByteTyID: opLabel = ToSByteTy; break;
106 case Type::UShortTyID: opLabel = ToUShortTy; break;
107 case Type::ShortTyID: opLabel = ToShortTy; break;
108 case Type::UIntTyID: opLabel = ToUIntTy; break;
109 case Type::IntTyID: opLabel = ToIntTy; break;
110 case Type::ULongTyID: opLabel = ToULongTy; break;
111 case Type::LongTyID: opLabel = ToLongTy; break;
112 case Type::FloatTyID: opLabel = ToFloatTy; break;
113 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
114 default:
115 if (instrValueType->isArrayType())
116 opLabel = ToArrayTy;
117 else if (instrValueType->isPointerType())
118 opLabel = ToPointerTy;
119 else
120 ; // Just use `Cast' opcode otherwise. It's probably ignored.
121 break;
122 }
123 }
124
125 basicNode.opLabel = opLabel;
126}
127
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000128
129void
130InstructionNode::dumpNode(int indent) const
131{
132 for (int i=0; i < indent; i++)
133 cout << " ";
134
135 cout << getInstruction()->getOpcodeName();
136
137 const vector<MachineInstr*>& mvec = getInstruction()->getMachineInstrVec();
138 if (mvec.size() > 0)
139 cout << "\tMachine Instructions: ";
140 for (unsigned int i=0; i < mvec.size(); i++)
141 {
142 mvec[i]->dump(0);
143 if (i < mvec.size() - 1)
144 cout << "; ";
145 }
146
147 cout << endl;
148}
149
150
151VRegListNode::VRegListNode()
152 : InstrTreeNode(NTVRegListNode, NULL)
153{
154 basicNode.opLabel = VRegListOp;
155}
156
157void
158VRegListNode::dumpNode(int indent) const
159{
160 for (int i=0; i < indent; i++)
161 cout << " ";
162
163 cout << "List" << endl;
164}
165
166
167VRegNode::VRegNode(Value* _val)
168 : InstrTreeNode(NTVRegNode, _val)
169{
170 basicNode.opLabel = VRegNodeOp;
171}
172
173void
174VRegNode::dumpNode(int indent) const
175{
176 for (int i=0; i < indent; i++)
177 cout << " ";
178
179 cout << "VReg " << getValue() << "\t(type "
180 << (int) getValue()->getValueType() << ")" << endl;
181}
182
183
184ConstantNode::ConstantNode(ConstPoolVal* constVal)
185 : InstrTreeNode(NTConstNode, constVal)
186{
187 basicNode.opLabel = ConstantNodeOp;
188}
189
190void
191ConstantNode::dumpNode(int indent) const
192{
193 for (int i=0; i < indent; i++)
194 cout << " ";
195
196 cout << "Constant " << getValue() << "\t(type "
197 << (int) getValue()->getValueType() << ")" << endl;
198}
199
200
201LabelNode::LabelNode(BasicBlock* _bblock)
202 : InstrTreeNode(NTLabelNode, _bblock)
203{
204 basicNode.opLabel = LabelNodeOp;
205}
206
207void
208LabelNode::dumpNode(int indent) const
209{
210 for (int i=0; i < indent; i++)
211 cout << " ";
212
213 cout << "Label " << getValue() << endl;
214}
215
216//------------------------------------------------------------------------
217// class InstrForest
218//
219// A forest of instruction trees, usually for a single method.
220//------------------------------------------------------------------------
221
222void
223InstrForest::buildTreesForMethod(Method *method)
224{
225 for (Method::inst_iterator instrIter = method->inst_begin();
226 instrIter != method->inst_end();
227 ++instrIter)
228 {
229 Instruction *instr = *instrIter;
230 if (! instr->isPHINode())
231 (void) this->buildTreeForInstruction(instr);
232 }
233}
234
235
236void
237InstrForest::dump() const
238{
Chris Lattner75279cc2001-07-23 03:50:57 +0000239 for (hash_set<InstructionNode*>::const_iterator
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000240 treeRootIter = treeRoots.begin();
241 treeRootIter != treeRoots.end();
242 ++treeRootIter)
243 {
244 (*treeRootIter)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
245 }
246}
247
248inline void
249InstrForest::noteTreeNodeForInstr(Instruction* instr,
250 InstructionNode* treeNode)
251{
252 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
253 (*this)[instr] = treeNode;
254 treeRoots.insert(treeNode); // mark node as root of a new tree
255}
256
257
258inline void
259InstrForest::setLeftChild(InstrTreeNode* parent, InstrTreeNode* child)
260{
261 parent->basicNode.leftChild = & child->basicNode;
262 child->basicNode.parent = & parent->basicNode;
263 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
264 treeRoots.erase((InstructionNode*) child); // no longer a tree root
265}
266
267
268inline void
269InstrForest::setRightChild(InstrTreeNode* parent, InstrTreeNode* child)
270{
271 parent->basicNode.rightChild = & child->basicNode;
272 child->basicNode.parent = & parent->basicNode;
273 if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
274 treeRoots.erase((InstructionNode*) child); // no longer a tree root
275}
276
277
278InstructionNode*
279InstrForest::buildTreeForInstruction(Instruction* instr)
280{
281 InstructionNode* treeNode = this->getTreeNodeForInstr(instr);
282 if (treeNode != NULL)
283 {// treeNode has already been constructed for this instruction
284 assert(treeNode->getInstruction() == instr);
285 return treeNode;
286 }
287
288 // Otherwise, create a new tree node for this instruction.
289 //
290 treeNode = new InstructionNode(instr);
291 this->noteTreeNodeForInstr(instr, treeNode);
292
293 // If the instruction has more than 2 instruction operands,
294 // then we will not add any children. This assumes that instructions
295 // like 'call' that have more than 2 instruction operands do not
296 // ever get combined with the instructions that compute the operands.
297 // Note that we only count operands of type instruction and not other
298 // values such as branch labels for a branch or switch instruction.
299 //
300 // To do this efficiently, we'll walk all operands, build treeNodes
301 // for all instruction operands and save them in an array, and then
302 // insert children at the end if there are not more than 2.
303 // As a performance optimization, allocate a child array only
304 // if a fixed array is too small.
305 //
306 int numChildren = 0;
307 const unsigned int MAX_CHILD = 8;
308 static InstrTreeNode* fixedChildArray[MAX_CHILD];
309 InstrTreeNode** childArray =
310 (instr->getNumOperands() > MAX_CHILD)
311 ? new (InstrTreeNode*)[instr->getNumOperands()]
312 : fixedChildArray;
313
314 //
315 // Walk the operands of the instruction
316 //
317 for (Instruction::op_iterator opIter = instr->op_begin();
318 opIter != instr->op_end();
319 ++opIter)
320 {
321 Value* operand = *opIter;
322
323 // Check if the operand is a data value, not an branch label, type,
324 // method or module. If the operand is an address type (i.e., label
325 // or method) that is used in an non-branching operation, e.g., `add'.
326 // that should be considered a data value.
327
328 // Check latter condition here just to simplify the next IF.
329 bool includeAddressOperand =
330 ((operand->getValueType() == Value::BasicBlockVal
331 || operand->getValueType() == Value::MethodVal)
332 && ! instr->isTerminator());
333
334 if (/* (*opIter) != NULL
335 &&*/ includeAddressOperand
336 || operand->getValueType() == Value::InstructionVal
337 || operand->getValueType() == Value::ConstantVal
338 || operand->getValueType() == Value::MethodArgumentVal)
339 {// This operand is a data value
340
341 // An instruction that computes the incoming value is added as a
342 // child of the current instruction if:
343 // the value has only a single use
344 // AND both instructions are in the same basic block
345 // AND the instruction is not a PHI
346 //
347 // (Note that if the value has only a single use (viz., `instr'),
348 // the def of the value can be safely moved just before instr
349 // and therefore it is safe to combine these two instructions.)
350 //
351 // In all other cases, the virtual register holding the value
352 // is used directly, i.e., made a child of the instruction node.
353 //
354 InstrTreeNode* opTreeNode;
355 if (operand->getValueType() == Value::InstructionVal
356 && operand->use_size() == 1
357 && ((Instruction*)operand)->getParent() == instr->getParent()
358 && ! ((Instruction*)operand)->isPHINode())
359 {
360 // Recursively create a treeNode for it.
361 opTreeNode =this->buildTreeForInstruction((Instruction*)operand);
362 }
363 else if (operand->getValueType() == Value::ConstantVal)
364 {
365 // Create a leaf node for a constant
366 opTreeNode = new ConstantNode((ConstPoolVal*) operand);
367 }
368 else
369 {
370 // Create a leaf node for the virtual register
371 opTreeNode = new VRegNode(operand);
372 }
373
374 childArray[numChildren] = opTreeNode;
375 numChildren++;
376 }
377 }
378
379 //--------------------------------------------------------------------
380 // Add any selected operands as children in the tree.
381 // Certain instructions can have more than 2 in some instances (viz.,
382 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
383 // array or struct). Make the operands of every such instruction into
384 // a right-leaning binary tree with the operand nodes at the leaves
385 // and VRegList nodes as internal nodes.
386 //--------------------------------------------------------------------
387
388 InstrTreeNode* parent = treeNode; // new VRegListNode();
389 int n;
390
391 if (numChildren > 2)
392 {
393 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
394 assert(instrOpcode == Instruction::Call ||
395 instrOpcode == Instruction::Load ||
396 instrOpcode == Instruction::Store ||
397 instrOpcode == Instruction::GetElementPtr);
398 }
399
400 // Insert the first child as a direct child
401 if (numChildren >= 1)
402 this->setLeftChild(parent, childArray[0]);
403
404 // Create a list node for children 2 .. N-1, if any
405 for (n = numChildren-1; n >= 2; n--)
406 { // We have more than two children
407 InstrTreeNode* listNode = new VRegListNode();
408 this->setRightChild(parent, listNode);
409 this->setLeftChild(listNode, childArray[numChildren - n]);
410 parent = listNode;
411 }
412
413 // Now insert the last remaining child (if any).
414 if (numChildren >= 2)
415 {
416 assert(n == 1);
417 this->setRightChild(parent, childArray[numChildren - 1]);
418 }
419
420 if (childArray != fixedChildArray)
421 {
422 delete[] childArray;
423 }
424
425 return treeNode;
426}
427