2 //***************************************************************************
7 // Machine-independent driver file for instruction selection.
8 // This file constructs a forest of BURG instruction trees and then
9 // uses the BURG-generated tree grammar (BURM) to find the optimal
10 // instruction sequences for a given machine.
13 // 7/02/01 - Vikram Adve - Created
14 //**************************************************************************/
17 #include "llvm/CodeGen/InstrSelection.h"
18 #include "llvm/CodeGen/InstrSelectionSupport.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Instruction.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/Method.h"
25 static bool SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
26 TargetMachine &target);
29 enum SelectDebugLevel_t {
31 Select_PrintMachineCode,
32 Select_DebugInstTrees,
33 Select_DebugBurgTrees,
36 // Enable Debug Options to be specified on the command line
37 cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags,
38 "enable instruction selection debugging information",
39 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
40 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
41 clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "),
42 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0);
46 //---------------------------------------------------------------------------
47 // Entry point for instruction selection using BURG.
48 // Returns true if instruction selection failed, false otherwise.
49 //---------------------------------------------------------------------------
52 SelectInstructionsForMethod(Method* method, TargetMachine &target)
57 // Build the instruction trees to be given as inputs to BURG.
59 InstrForest instrForest(method);
61 if (SelectDebugLevel >= Select_DebugInstTrees)
63 cout << "\n\n*** Instruction trees for method "
64 << (method->hasName()? method->getName() : "")
70 // Invoke BURG instruction selection for each tree
72 const hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet();
73 for (hash_set<InstructionNode*>::const_iterator
74 treeRootIter = treeRoots.begin(); treeRootIter != treeRoots.end();
77 InstrTreeNode* basicNode = *treeRootIter;
79 // Invoke BURM to label each tree node with a state
80 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 //*********************** Private Functions *****************************/
124 //---------------------------------------------------------------------------
125 // Function PostprocessMachineCodeForTree
127 // Apply any final cleanups to machine code for the root of a subtree
128 // after selection for all its children has been completed.
129 //---------------------------------------------------------------------------
132 PostprocessMachineCodeForTree(InstructionNode* instrNode,
135 TargetMachine &target)
137 // Fix up any constant operands in the machine instructions to either
138 // use an immediate field or to load the constant into a register
139 // Walk backwards and use direct indexes to allow insertion before current
141 Instruction* vmInstr = instrNode->getInstruction();
142 MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec();
143 for (int i = (int) mvec.size()-1; i >= 0; i--)
145 vector<MachineInstr*> loadConstVec =
146 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
148 if (loadConstVec.size() > 0)
149 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
153 //---------------------------------------------------------------------------
154 // Function SelectInstructionsForTree
156 // Recursively walk the tree to select instructions.
157 // Do this top-down so that child instructions can exploit decisions
158 // made at the child instructions.
160 // E.g., if br(setle(reg,const)) decides the constant is 0 and uses
161 // a branch-on-integer-register instruction, then the setle node
162 // can use that information to avoid generating the SUBcc instruction.
164 // Note that this cannot be done bottom-up because setle must do this
165 // only if it is a child of the branch (otherwise, the result of setle
166 // may be used by multiple instructions).
167 //---------------------------------------------------------------------------
170 SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
171 TargetMachine &target)
173 // Use a static vector to avoid allocating a new one per VM instruction
174 static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
176 // Get the rule that matches this node.
178 int ruleForNode = burm_rule(treeRoot->state, goalnt);
180 if (ruleForNode == 0)
182 cerr << "Could not match instruction tree for instr selection" << endl;
187 // Get this rule's non-terminals and the corresponding child nodes (if any)
189 short *nts = burm_nts[ruleForNode];
191 // First, select instructions for the current node and rule.
192 // (If this is a list node, not an instruction, then skip this step).
193 // This function is specific to the target architecture.
195 if (treeRoot->opLabel != VRegListOp)
197 InstructionNode* instrNode = (InstructionNode*)treeRoot;
198 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
200 unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, target,
202 assert(N <= MAX_INSTR_PER_VMINSTR);
203 for (unsigned i=0; i < N; i++)
205 assert(minstrVec[i] != NULL);
206 instrNode->getInstruction()->addMachineInstruction(minstrVec[i]);
210 // Then, recursively compile the child nodes, if any.
213 { // i.e., there is at least one kid
214 InstrTreeNode* kids[2];
215 int currentRule = ruleForNode;
216 burm_kids(treeRoot, currentRule, kids);
218 // First skip over any chain rules so that we don't visit
219 // the current node again.
221 while (ThisIsAChainRule(currentRule))
223 currentRule = burm_rule(treeRoot->state, nts[0]);
224 nts = burm_nts[currentRule];
225 burm_kids(treeRoot, currentRule, kids);
228 // Now we have the first non-chain rule so we have found
229 // the actual child nodes. Recursively compile them.
231 for (int i = 0; nts[i]; i++)
234 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
235 if (nodeType == InstrTreeNode::NTVRegListNode ||
236 nodeType == InstrTreeNode::NTInstructionNode)
238 if (SelectInstructionsForTree(kids[i], nts[i], target))
239 return true; // failure
244 // Finally, do any postprocessing on this node after its children
245 // have been translated
247 if (treeRoot->opLabel != VRegListOp)
249 InstructionNode* instrNode = (InstructionNode*)treeRoot;
250 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
253 return false; // success