2 //***************************************************************************
9 // 7/02/01 - Vikram Adve - Created
10 //**************************************************************************/
13 #include "llvm/CodeGen/InstrSelection.h"
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Type.h"
17 #include "llvm/iMemory.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/Method.h"
22 static bool SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
23 TargetMachine &Target);
26 enum SelectDebugLevel_t {
28 Select_PrintMachineCode,
29 Select_DebugInstTrees,
30 Select_DebugBurgTrees,
33 // Enable Debug Options to be specified on the command line
34 cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags, // cl::Hidden
35 "enable instruction selection debugging information",
36 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
37 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
38 clEnumValN(Select_DebugInstTrees, "i", "print instr. selection debugging info"),
39 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0);
43 //---------------------------------------------------------------------------
44 // Entry point for instruction selection using BURG.
45 // Returns true if instruction selection failed, false otherwise.
46 //---------------------------------------------------------------------------
49 SelectInstructionsForMethod(Method* method,
50 TargetMachine &Target)
55 // Build the instruction trees to be given as inputs to BURG.
57 InstrForest instrForest;
58 instrForest.buildTreesForMethod(method);
60 if (SelectDebugLevel >= Select_DebugInstTrees)
62 cout << "\n\n*** Instruction trees for method "
63 << (method->hasName()? method->getName() : "")
69 // Invoke BURG instruction selection for each tree
71 const hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet();
72 for (hash_set<InstructionNode*>::const_iterator
73 treeRootIter = treeRoots.begin();
74 treeRootIter != treeRoots.end();
77 InstrTreeNode* basicNode = *treeRootIter;
79 // Invoke BURM to label each tree node with a state
80 (void) burm_label(basicNode);
82 if (SelectDebugLevel >= Select_DebugBurgTrees)
84 printcover(basicNode, 1, 0);
85 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
86 printMatches(basicNode);
89 // Then recursively walk the tree to select instructions
90 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, Target))
98 // Record instructions in the vector for each basic block
100 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI)
102 MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec();
103 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
105 MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec();
106 for (unsigned i=0; i < mvec.size(); i++)
107 bbMvec.push_back(mvec[i]);
111 if (SelectDebugLevel >= Select_PrintMachineCode)
113 cout << endl << "*** Machine instructions after INSTRUCTION SELECTION" << endl;
114 PrintMachineInstructions(method);
121 //---------------------------------------------------------------------------
122 // Function: FoldGetElemChain
125 // Fold a chain of GetElementPtr instructions into an equivalent
126 // (Pointer, IndexVector) pair. Returns the pointer Value, and
127 // stores the resulting IndexVector in argument chainIdxVec.
128 //---------------------------------------------------------------------------
131 FoldGetElemChain(const InstructionNode* getElemInstrNode,
132 vector<ConstPoolVal*>& chainIdxVec)
134 MemAccessInst* getElemInst = (MemAccessInst*)
135 getElemInstrNode->getInstruction();
137 // Initialize return values from the incoming instruction
138 Value* ptrVal = getElemInst->getPtrOperand();
139 chainIdxVec = getElemInst->getIndexVec(); // copies index vector values
141 // Now chase the chain of getElementInstr instructions, if any
142 InstrTreeNode* ptrChild = getElemInstrNode->leftChild();
143 while (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
144 ptrChild->getOpLabel() == GetElemPtrIdx)
146 // Child is a GetElemPtr instruction
147 getElemInst = (MemAccessInst*)
148 ((InstructionNode*) ptrChild)->getInstruction();
149 const vector<ConstPoolVal*>& idxVec = getElemInst->getIndexVec();
151 // Get the pointer value out of ptrChild and *prepend* its index vector
152 ptrVal = getElemInst->getPtrOperand();
153 chainIdxVec.insert(chainIdxVec.begin(), idxVec.begin(), idxVec.end());
155 ptrChild = ptrChild->leftChild();
162 //*********************** Private Functions *****************************/
165 //---------------------------------------------------------------------------
166 // Function SelectInstructionsForTree
168 // Recursively walk the tree to select instructions.
169 // Do this top-down so that child instructions can exploit decisions
170 // made at the child instructions.
172 // E.g., if br(setle(reg,const)) decides the constant is 0 and uses
173 // a branch-on-integer-register instruction, then the setle node
174 // can use that information to avoid generating the SUBcc instruction.
176 // Note that this cannot be done bottom-up because setle must do this
177 // only if it is a child of the branch (otherwise, the result of setle
178 // may be used by multiple instructions).
179 //---------------------------------------------------------------------------
182 SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
183 TargetMachine &Target)
185 // Use a static vector to avoid allocating a new one per VM instruction
186 static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
188 // Get the rule that matches this node.
190 int ruleForNode = burm_rule(treeRoot->state, goalnt);
192 if (ruleForNode == 0)
194 cerr << "Could not match instruction tree for instr selection" << endl;
198 // Get this rule's non-terminals and the corresponding child nodes (if any)
200 short *nts = burm_nts[ruleForNode];
203 // First, select instructions for the current node and rule.
204 // (If this is a list node, not an instruction, then skip this step).
205 // This function is specific to the target architecture.
207 if (treeRoot->opLabel != VRegListOp)
209 InstructionNode* instrNode = (InstructionNode*)treeRoot;
210 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
212 unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, Target,
214 assert(N <= MAX_INSTR_PER_VMINSTR);
215 for (unsigned i=0; i < N; i++)
217 assert(minstrVec[i] != NULL);
218 instrNode->getInstruction()->addMachineInstruction(minstrVec[i]);
222 // Then, recursively compile the child nodes, if any.
225 { // i.e., there is at least one kid
227 InstrTreeNode* kids[2];
228 int currentRule = ruleForNode;
229 burm_kids(treeRoot, currentRule, kids);
231 // First skip over any chain rules so that we don't visit
232 // the current node again.
234 while (ThisIsAChainRule(currentRule))
236 currentRule = burm_rule(treeRoot->state, nts[0]);
237 nts = burm_nts[currentRule];
238 burm_kids(treeRoot, currentRule, kids);
241 // Now we have the first non-chain rule so we have found
242 // the actual child nodes. Recursively compile them.
244 for (int i = 0; nts[i]; i++)
247 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
248 if (nodeType == InstrTreeNode::NTVRegListNode ||
249 nodeType == InstrTreeNode::NTInstructionNode)
251 if (SelectInstructionsForTree(kids[i], nts[i], Target))
252 return true; // failure
257 return false; // success