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"
26 //******************** Internal Data Declarations ************************/
28 // Use a static vector to avoid allocating a new one per VM instruction
29 static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
32 enum SelectDebugLevel_t {
34 Select_PrintMachineCode,
35 Select_DebugInstTrees,
36 Select_DebugBurgTrees,
39 // Enable Debug Options to be specified on the command line
40 cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags,
41 "enable instruction selection debugging information",
42 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
43 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
44 clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "),
45 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0);
48 //******************** Forward Function Declarations ***********************/
51 static bool SelectInstructionsForTree (InstrTreeNode* treeRoot,
53 TargetMachine &target);
55 static void PostprocessMachineCodeForTree(InstructionNode* instrNode,
58 TargetMachine &target);
61 //******************* Externally Visible Functions *************************/
64 //---------------------------------------------------------------------------
65 // Entry point for instruction selection using BURG.
66 // Returns true if instruction selection failed, false otherwise.
67 //---------------------------------------------------------------------------
70 SelectInstructionsForMethod(Method* method, TargetMachine &target)
75 // Build the instruction trees to be given as inputs to BURG.
77 InstrForest instrForest(method);
79 if (SelectDebugLevel >= Select_DebugInstTrees)
81 cout << "\n\n*** Instruction trees for method "
82 << (method->hasName()? method->getName() : "")
88 // Invoke BURG instruction selection for each tree
90 const hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet();
91 for (hash_set<InstructionNode*>::const_iterator
92 treeRootIter = treeRoots.begin(); treeRootIter != treeRoots.end();
95 InstrTreeNode* basicNode = *treeRootIter;
97 // Invoke BURM to label each tree node with a state
98 burm_label(basicNode);
100 if (SelectDebugLevel >= Select_DebugBurgTrees)
102 printcover(basicNode, 1, 0);
103 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
104 printMatches(basicNode);
107 // Then recursively walk the tree to select instructions
108 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, target))
116 // Record instructions in the vector for each basic block
118 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI)
120 MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec();
121 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
123 MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec();
124 for (unsigned i=0; i < mvec.size(); i++)
125 bbMvec.push_back(mvec[i]);
129 if (SelectDebugLevel >= Select_PrintMachineCode)
132 << "*** Machine instructions after INSTRUCTION SELECTION" << endl;
133 method->getMachineCode().dump();
140 //*********************** Private Functions *****************************/
143 //---------------------------------------------------------------------------
144 // Function AppendMachineCodeForVMInstr
146 // Append machine instr sequence to the machine code vec for a VM instr
147 //---------------------------------------------------------------------------
150 AppendMachineCodeForVMInstr(MachineInstr** minstrVec,
152 Instruction* vmInstr)
156 MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec();
157 mvec.insert(mvec.end(), minstrVec, minstrVec+N);
162 //---------------------------------------------------------------------------
163 // Function PostprocessMachineCodeForTree
165 // Apply any final cleanups to machine code for the root of a subtree
166 // after selection for all its children has been completed.
167 //---------------------------------------------------------------------------
170 PostprocessMachineCodeForTree(InstructionNode* instrNode,
173 TargetMachine &target)
175 // Fix up any constant operands in the machine instructions to either
176 // use an immediate field or to load the constant into a register
177 // Walk backwards and use direct indexes to allow insertion before current
179 Instruction* vmInstr = instrNode->getInstruction();
180 MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec();
181 for (int i = (int) mvec.size()-1; i >= 0; i--)
183 vector<MachineInstr*> loadConstVec =
184 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
186 if (loadConstVec.size() > 0)
187 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
191 //---------------------------------------------------------------------------
192 // Function SelectInstructionsForTree
194 // Recursively walk the tree to select instructions.
195 // Do this top-down so that child instructions can exploit decisions
196 // made at the child instructions.
198 // E.g., if br(setle(reg,const)) decides the constant is 0 and uses
199 // a branch-on-integer-register instruction, then the setle node
200 // can use that information to avoid generating the SUBcc instruction.
202 // Note that this cannot be done bottom-up because setle must do this
203 // only if it is a child of the branch (otherwise, the result of setle
204 // may be used by multiple instructions).
205 //---------------------------------------------------------------------------
208 SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
209 TargetMachine &target)
211 // Get the rule that matches this node.
213 int ruleForNode = burm_rule(treeRoot->state, goalnt);
215 if (ruleForNode == 0)
217 cerr << "Could not match instruction tree for instr selection" << endl;
222 // Get this rule's non-terminals and the corresponding child nodes (if any)
224 short *nts = burm_nts[ruleForNode];
226 // First, select instructions for the current node and rule.
227 // (If this is a list node, not an instruction, then skip this step).
228 // This function is specific to the target architecture.
230 if (treeRoot->opLabel != VRegListOp)
232 InstructionNode* instrNode = (InstructionNode*)treeRoot;
233 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
235 unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, target,
239 assert(N <= MAX_INSTR_PER_VMINSTR);
240 AppendMachineCodeForVMInstr(minstrVec,N,instrNode->getInstruction());
244 // Then, recursively compile the child nodes, if any.
247 { // i.e., there is at least one kid
248 InstrTreeNode* kids[2];
249 int currentRule = ruleForNode;
250 burm_kids(treeRoot, currentRule, kids);
252 // First skip over any chain rules so that we don't visit
253 // the current node again.
255 while (ThisIsAChainRule(currentRule))
257 currentRule = burm_rule(treeRoot->state, nts[0]);
258 nts = burm_nts[currentRule];
259 burm_kids(treeRoot, currentRule, kids);
262 // Now we have the first non-chain rule so we have found
263 // the actual child nodes. Recursively compile them.
265 for (int i = 0; nts[i]; i++)
268 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
269 if (nodeType == InstrTreeNode::NTVRegListNode ||
270 nodeType == InstrTreeNode::NTInstructionNode)
272 if (SelectInstructionsForTree(kids[i], nts[i], target))
273 return true; // failure
278 // Finally, do any postprocessing on this node after its children
279 // have been translated
281 if (treeRoot->opLabel != VRegListOp)
283 InstructionNode* instrNode = (InstructionNode*)treeRoot;
284 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
287 return false; // success