2 //***************************************************************************
9 // 7/02/01 - Vikram Adve - Created
10 //***************************************************************************
13 //************************** System Include Files **************************/
21 //*************************** User Include Files ***************************/
23 #include "llvm/Method.h"
24 #include "llvm/BasicBlock.h"
25 #include "llvm/Type.h"
26 #include "llvm/iMemory.h"
27 #include "llvm/Instruction.h"
28 #include "llvm/LLC/CompileContext.h"
29 #include "llvm/CodeGen/InstrForest.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/InstrSelection.h"
34 //************************* Forward Declarations ***************************/
36 static bool SelectInstructionsForTree (BasicTreeNode* treeRoot,
38 CompileContext& ccontext);
41 //******************* Externally Visible Functions *************************/
44 //---------------------------------------------------------------------------
45 // Entry point for instruction selection using BURG.
46 // Returns true if instruction selection failed, false otherwise.
47 //---------------------------------------------------------------------------
50 SelectInstructionsForMethod(Method* method,
51 CompileContext& ccontext)
55 InstrForest instrForest;
56 instrForest.buildTreesForMethod(method);
58 const hash_set<InstructionNode*, ptrHashFunc>&
59 treeRoots = instrForest.getRootSet();
62 // Invoke BURG instruction selection for each tree
64 for (hash_set<InstructionNode*, ptrHashFunc >::const_iterator
65 treeRootIter = treeRoots.begin();
66 treeRootIter != treeRoots.end();
69 BasicTreeNode* basicNode = (*treeRootIter)->getBasicNode();
71 // Invoke BURM to label each tree node with a state
72 (void) burm_label(basicNode);
74 if (ccontext.getOptions().IntOptionValue(DEBUG_INSTR_SELECT_OPT)
77 printcover(basicNode, 1, 0);
78 printf("\nCover cost == %d\n\n", treecost(basicNode, 1, 0));
79 printMatches(basicNode);
82 // Then recursively walk the tree to select instructions
83 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, ccontext))
92 if ( ccontext.getOptions().IntOptionValue(DEBUG_INSTR_SELECT_OPT)
95 cout << "\n\n*** Instruction trees for method "
96 << (method->hasName()? method->getName() : "")
101 if (ccontext.getOptions().IntOptionValue(DEBUG_INSTR_SELECT_OPT) > 0)
102 PrintMachineInstructions(method, ccontext);
109 //---------------------------------------------------------------------------
110 // Function: FoldGetElemChain
113 // Fold a chain of GetElementPtr instructions into an equivalent
114 // (Pointer, IndexVector) pair. Returns the pointer Value, and
115 // stores the resulting IndexVector in argument chainIdxVec.
116 //---------------------------------------------------------------------------
119 FoldGetElemChain(const InstructionNode* getElemInstrNode,
120 vector<ConstPoolVal*>& chainIdxVec)
122 MemAccessInst* getElemInst = (MemAccessInst*)
123 getElemInstrNode->getInstruction();
125 // Initialize return values from the incoming instruction
126 Value* ptrVal = getElemInst->getPtrOperand();
127 chainIdxVec = getElemInst->getIndexVec(); // copies index vector values
129 // Now chase the chain of getElementInstr instructions, if any
130 InstrTreeNode* ptrChild = getElemInstrNode->leftChild();
131 while (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
132 ptrChild->getOpLabel() == GetElemPtrIdx)
134 // Child is a GetElemPtr instruction
135 getElemInst = (MemAccessInst*)
136 ((InstructionNode*) ptrChild)->getInstruction();
137 const vector<ConstPoolVal*>& idxVec = getElemInst->getIndexVec();
139 // Get the pointer value out of ptrChild and *prepend* its index vector
140 ptrVal = getElemInst->getPtrOperand();
141 chainIdxVec.insert(chainIdxVec.begin(), idxVec.begin(), idxVec.end());
143 ptrChild = ptrChild->leftChild();
151 PrintMachineInstructions(Method* method,
152 CompileContext& ccontext)
154 cout << "\n" << method->getReturnType()
155 << " \"" << method->getName() << "\"" << endl;
157 for (Method::const_iterator bbIter = method->begin();
158 bbIter != method->end();
161 BasicBlock* bb = *bbIter;
163 << (bb->hasName()? bb->getName() : "Label")
164 << " (" << bb << ")" << ":"
167 for (BasicBlock::const_iterator instrIter = bb->begin();
168 instrIter != bb->end();
171 Instruction *instr = *instrIter;
172 const MachineCodeForVMInstr& minstrVec = instr->getMachineInstrVec();
173 for (unsigned i=0, N=minstrVec.size(); i < N; i++)
174 cout << "\t" << *minstrVec[i] << endl;
179 //*********************** Private Functions *****************************/
182 //---------------------------------------------------------------------------
183 // Function SelectInstructionsForTree
185 // Recursively walk the tree to select instructions.
186 // Do this top-down so that child instructions can exploit decisions
187 // made at the child instructions.
189 // E.g., if br(setle(reg,const)) decides the constant is 0 and uses
190 // a branch-on-integer-register instruction, then the setle node
191 // can use that information to avoid generating the SUBcc instruction.
193 // Note that this cannot be done bottom-up because setle must do this
194 // only if it is a child of the branch (otherwise, the result of setle
195 // may be used by multiple instructions).
196 //---------------------------------------------------------------------------
199 SelectInstructionsForTree(BasicTreeNode* treeRoot,
201 CompileContext& ccontext)
203 // Use a static vector to avoid allocating a new one per VM instruction
204 static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
206 // Get the rule that matches this node.
208 int ruleForNode = burm_rule(treeRoot->state, goalnt);
210 if (ruleForNode == 0)
212 cerr << "Could not match instruction tree for instr selection" << endl;
216 // Get this rule's non-terminals and the corresponding child nodes (if any)
218 short *nts = burm_nts[ruleForNode];
221 // First, select instructions for the current node and rule.
222 // (If this is a list node, not an instruction, then skip this step).
223 // This function is specific to the target architecture.
225 if (treeRoot->opLabel != VRegListOp)
227 InstructionNode* instrNode = (InstructionNode*) MainTreeNode(treeRoot);
228 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
230 unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, ccontext,
232 assert(N <= MAX_INSTR_PER_VMINSTR);
233 for (unsigned i=0; i < N; i++)
235 assert(minstrVec[i] != NULL);
236 instrNode->getInstruction()->addMachineInstruction(minstrVec[i]);
240 // Then, recursively compile the child nodes, if any.
243 { // i.e., there is at least one kid
245 BasicTreeNode* kids[2];
246 int currentRule = ruleForNode;
247 burm_kids(treeRoot, currentRule, kids);
249 // First skip over any chain rules so that we don't visit
250 // the current node again.
252 while (ThisIsAChainRule(currentRule))
254 currentRule = burm_rule(treeRoot->state, nts[0]);
255 nts = burm_nts[currentRule];
256 burm_kids(treeRoot, currentRule, kids);
259 // Now we have the first non-chain rule so we have found
260 // the actual child nodes. Recursively compile them.
262 for (int i = 0; nts[i]; i++)
265 InstrTreeNode::InstrTreeNodeType
266 nodeType = MainTreeNode(kids[i])->getNodeType();
267 if (nodeType == InstrTreeNode::NTVRegListNode ||
268 nodeType == InstrTreeNode::NTInstructionNode)
270 bool failed= SelectInstructionsForTree(kids[i], nts[i],ccontext);
272 return true; // failure
277 return false; // success