Change references to the Method class to be references to the Function
[oota-llvm.git] / lib / Target / SparcV9 / InstrSelection / InstrForest.cpp
1 // $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
25 #include "llvm/CodeGen/InstrForest.h"
26 #include "llvm/CodeGen/MachineCodeForInstruction.h"
27 #include "llvm/Function.h"
28 #include "llvm/iTerminators.h"
29 #include "llvm/iMemory.h"
30 #include "llvm/iPHINode.h"
31 #include "llvm/ConstantVals.h"
32 #include "llvm/BasicBlock.h"
33 #include "llvm/CodeGen/MachineInstr.h"
34 #include "Support/STLExtras.h"
35 #include <iostream>
36 using std::cerr;
37 using std::vector;
38
39 //------------------------------------------------------------------------ 
40 // class InstrTreeNode
41 //------------------------------------------------------------------------ 
42
43 void
44 InstrTreeNode::dump(int dumpChildren, int indent) const
45 {
46   dumpNode(indent);
47   
48   if (dumpChildren)
49     {
50       if (LeftChild)
51         LeftChild->dump(dumpChildren, indent+1);
52       if (RightChild)
53         RightChild->dump(dumpChildren, indent+1);
54     }
55 }
56
57
58 InstructionNode::InstructionNode(Instruction* I)
59   : InstrTreeNode(NTInstructionNode, I),
60     codeIsFoldedIntoParent(false)
61 {
62   opLabel = I->getOpcode();
63
64   // Distinguish special cases of some instructions such as Ret and Br
65   // 
66   if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue())
67     {
68       opLabel = RetValueOp;                      // ret(value) operation
69     }
70   else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
71     {
72       opLabel = BrCondOp;               // br(cond) operation
73     }
74   else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
75     {
76       opLabel = SetCCOp;                // common label for all SetCC ops
77     }
78   else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
79     {
80       opLabel = AllocaN;                 // Alloca(ptr, N) operation
81     }
82   else if ((opLabel == Instruction::Load ||
83             opLabel == Instruction::GetElementPtr) &&
84            cast<MemAccessInst>(I)->hasIndices())
85     {
86       opLabel = opLabel + 100;           // load/getElem with index vector
87     }
88   else if (opLabel == Instruction::And ||
89            opLabel == Instruction::Or ||
90            opLabel == Instruction::Xor ||
91            opLabel == Instruction::Not)
92     {
93       // Distinguish bitwise operators from logical operators!
94       if (I->getType() != Type::BoolTy)
95         opLabel = opLabel + 100;         // bitwise operator
96     }
97   else if (opLabel == Instruction::Cast)
98     {
99       const Type *ITy = I->getType();
100       switch(ITy->getPrimitiveID())
101         {
102         case Type::BoolTyID:    opLabel = ToBoolTy;    break;
103         case Type::UByteTyID:   opLabel = ToUByteTy;   break;
104         case Type::SByteTyID:   opLabel = ToSByteTy;   break;
105         case Type::UShortTyID:  opLabel = ToUShortTy;  break;
106         case Type::ShortTyID:   opLabel = ToShortTy;   break;
107         case Type::UIntTyID:    opLabel = ToUIntTy;    break;
108         case Type::IntTyID:     opLabel = ToIntTy;     break;
109         case Type::ULongTyID:   opLabel = ToULongTy;   break;
110         case Type::LongTyID:    opLabel = ToLongTy;    break;
111         case Type::FloatTyID:   opLabel = ToFloatTy;   break;
112         case Type::DoubleTyID:  opLabel = ToDoubleTy;  break;
113         case Type::ArrayTyID:   opLabel = ToArrayTy;   break;
114         case Type::PointerTyID: opLabel = ToPointerTy; break;
115         default:
116           // Just use `Cast' opcode otherwise. It's probably ignored.
117           break;
118         }
119     }
120 }
121
122
123 void
124 InstructionNode::dumpNode(int indent) const
125 {
126   for (int i=0; i < indent; i++)
127     cerr << "    ";
128   
129   cerr << getInstruction()->getOpcodeName();
130   const MachineCodeForInstruction &mvec =
131     MachineCodeForInstruction::get(getInstruction());
132
133   if (mvec.size() > 0)
134     cerr << "\tMachine Instructions:  ";
135
136   for (unsigned int i=0; i < mvec.size(); ++i) {
137     mvec[i]->dump(0);
138     if (i < mvec.size() - 1)
139       cerr << ";  ";
140   }
141   
142   cerr << "\n";
143 }
144
145
146 void
147 VRegListNode::dumpNode(int indent) const
148 {
149   for (int i=0; i < indent; i++)
150     cerr << "    ";
151   
152   cerr << "List" << "\n";
153 }
154
155
156 void
157 VRegNode::dumpNode(int indent) const
158 {
159   for (int i=0; i < indent; i++)
160     cerr << "    ";
161   
162   cerr << "VReg " << getValue() << "\t(type "
163        << (int) getValue()->getValueType() << ")" << "\n";
164 }
165
166 void
167 ConstantNode::dumpNode(int indent) const
168 {
169   for (int i=0; i < indent; i++)
170     cerr << "    ";
171   
172   cerr << "Constant " << getValue() << "\t(type "
173        << (int) getValue()->getValueType() << ")" << "\n";
174 }
175
176 void
177 LabelNode::dumpNode(int indent) const
178 {
179   for (int i=0; i < indent; i++)
180     cerr << "    ";
181   
182   cerr << "Label " << getValue() << "\n";
183 }
184
185 //------------------------------------------------------------------------
186 // class InstrForest
187 // 
188 // A forest of instruction trees, usually for a single method.
189 //------------------------------------------------------------------------ 
190
191 InstrForest::InstrForest(Function *F)
192 {
193   for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
194     BasicBlock *BB = *FI;
195     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
196       buildTreeForInstruction(*I);
197   }
198 }
199
200 InstrForest::~InstrForest()
201 {
202   for (std::hash_map<const Instruction*,InstructionNode*>::iterator I=begin();
203        I != end(); ++I)
204       delete I->second;
205 }
206
207 void
208 InstrForest::dump() const
209 {
210   for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
211     (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
212 }
213
214 inline void
215 InstrForest::eraseRoot(InstructionNode* node)
216 {
217   for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
218        RI != RE; ++RI)
219     if (*RI == node)
220       treeRoots.erase(RI.base()-1);
221 }
222
223 inline void
224 InstrForest::noteTreeNodeForInstr(Instruction *instr,
225                                   InstructionNode *treeNode)
226 {
227   assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
228   (*this)[instr] = treeNode;
229   treeRoots.push_back(treeNode);        // mark node as root of a new tree
230 }
231
232
233 inline void
234 InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
235 {
236   parent->LeftChild = child;
237   child->Parent = parent;
238   if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
239     eraseRoot((InstructionNode*) child); // no longer a tree root
240 }
241
242 inline void
243 InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
244 {
245   parent->RightChild = child;
246   child->Parent = parent;
247   if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
248     eraseRoot((InstructionNode*) child); // no longer a tree root
249 }
250
251
252 InstructionNode*
253 InstrForest::buildTreeForInstruction(Instruction *instr)
254 {
255   InstructionNode *treeNode = getTreeNodeForInstr(instr);
256   if (treeNode)
257     {
258       // treeNode has already been constructed for this instruction
259       assert(treeNode->getInstruction() == instr);
260       return treeNode;
261     }
262   
263   // Otherwise, create a new tree node for this instruction.
264   // 
265   treeNode = new InstructionNode(instr);
266   noteTreeNodeForInstr(instr, treeNode);
267   
268   if (instr->getOpcode() == Instruction::Call)
269     { // Operands of call instruction
270       return treeNode;
271     }
272   
273   // If the instruction has more than 2 instruction operands,
274   // then we need to create artificial list nodes to hold them.
275   // (Note that we only count operands that get tree nodes, and not
276   // others such as branch labels for a branch or switch instruction.)
277   //
278   // To do this efficiently, we'll walk all operands, build treeNodes
279   // for all appropriate operands and save them in an array.  We then
280   // insert children at the end, creating list nodes where needed.
281   // As a performance optimization, allocate a child array only
282   // if a fixed array is too small.
283   // 
284   int numChildren = 0;
285   const unsigned int MAX_CHILD = 8;
286   static InstrTreeNode *fixedChildArray[MAX_CHILD];
287   InstrTreeNode **childArray =
288     (instr->getNumOperands() > MAX_CHILD)
289     ? new (InstrTreeNode*)[instr->getNumOperands()] : fixedChildArray;
290   
291   //
292   // Walk the operands of the instruction
293   // 
294   for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O)
295     {
296       Value* operand = *O;
297       
298       // Check if the operand is a data value, not an branch label, type,
299       // method or module.  If the operand is an address type (i.e., label
300       // or method) that is used in an non-branching operation, e.g., `add'.
301       // that should be considered a data value.
302     
303       // Check latter condition here just to simplify the next IF.
304       bool includeAddressOperand =
305         (isa<BasicBlock>(operand) || isa<Function>(operand))
306         && !instr->isTerminator();
307     
308       if (includeAddressOperand || isa<Instruction>(operand) ||
309           isa<Constant>(operand) || isa<FunctionArgument>(operand) ||
310           isa<GlobalVariable>(operand))
311         {
312           // This operand is a data value
313         
314           // An instruction that computes the incoming value is added as a
315           // child of the current instruction if:
316           //   the value has only a single use
317           //   AND both instructions are in the same basic block.
318           //   AND the current instruction is not a PHI (because the incoming
319           //            value is conceptually in a predecessor block,
320           //            even though it may be in the same static block)
321           // 
322           // (Note that if the value has only a single use (viz., `instr'),
323           //  the def of the value can be safely moved just before instr
324           //  and therefore it is safe to combine these two instructions.)
325           // 
326           // In all other cases, the virtual register holding the value
327           // is used directly, i.e., made a child of the instruction node.
328           // 
329           InstrTreeNode* opTreeNode;
330           if (isa<Instruction>(operand) && operand->use_size() == 1 &&
331               cast<Instruction>(operand)->getParent() == instr->getParent() &&
332               !isa<PHINode>(instr) &&
333               instr->getOpcode() != Instruction::Call)
334             {
335               // Recursively create a treeNode for it.
336               opTreeNode = buildTreeForInstruction((Instruction*)operand);
337             }
338           else if (Constant *CPV = dyn_cast<Constant>(operand))
339             {
340               // Create a leaf node for a constant
341               opTreeNode = new ConstantNode(CPV);
342             }
343           else
344             {
345               // Create a leaf node for the virtual register
346               opTreeNode = new VRegNode(operand);
347             }
348
349           childArray[numChildren++] = opTreeNode;
350         }
351     }
352   
353   //-------------------------------------------------------------------- 
354   // Add any selected operands as children in the tree.
355   // Certain instructions can have more than 2 in some instances (viz.,
356   // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
357   // array or struct). Make the operands of every such instruction into
358   // a right-leaning binary tree with the operand nodes at the leaves
359   // and VRegList nodes as internal nodes.
360   //-------------------------------------------------------------------- 
361   
362   InstrTreeNode *parent = treeNode;
363   
364   if (numChildren > 2)
365     {
366       unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
367       assert(instrOpcode == Instruction::PHINode ||
368              instrOpcode == Instruction::Call ||
369              instrOpcode == Instruction::Load ||
370              instrOpcode == Instruction::Store ||
371              instrOpcode == Instruction::GetElementPtr);
372     }
373   
374   // Insert the first child as a direct child
375   if (numChildren >= 1)
376     setLeftChild(parent, childArray[0]);
377
378   int n;
379   
380   // Create a list node for children 2 .. N-1, if any
381   for (n = numChildren-1; n >= 2; n--)
382     {
383       // We have more than two children
384       InstrTreeNode *listNode = new VRegListNode();
385       setRightChild(parent, listNode);
386       setLeftChild(listNode, childArray[numChildren - n]);
387       parent = listNode;
388     }
389   
390   // Now insert the last remaining child (if any).
391   if (numChildren >= 2)
392     {
393       assert(n == 1);
394       setRightChild(parent, childArray[numChildren - 1]);
395     }
396   
397   if (childArray != fixedChildArray)
398     delete [] childArray; 
399   
400   return treeNode;
401 }
402