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/MachineInstr.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Instruction.h"
21 #include "llvm/BasicBlock.h"
22 #include "llvm/Method.h"
24 static bool SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
25 TargetMachine &Target);
28 enum SelectDebugLevel_t {
30 Select_PrintMachineCode,
31 Select_DebugInstTrees,
32 Select_DebugBurgTrees,
35 // Enable Debug Options to be specified on the command line
36 cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags,
37 "enable instruction selection debugging information",
38 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
39 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
40 clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "),
41 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0);
45 //---------------------------------------------------------------------------
46 // Entry point for instruction selection using BURG.
47 // Returns true if instruction selection failed, false otherwise.
48 //---------------------------------------------------------------------------
51 SelectInstructionsForMethod(Method* method, TargetMachine &Target)
56 // Build the instruction trees to be given as inputs to BURG.
58 InstrForest instrForest(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(); treeRootIter != treeRoots.end();
76 InstrTreeNode* basicNode = *treeRootIter;
78 // Invoke BURM to label each tree node with a state
79 burm_label(basicNode);
81 if (SelectDebugLevel >= Select_DebugBurgTrees)
83 printcover(basicNode, 1, 0);
84 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
85 printMatches(basicNode);
88 // Then recursively walk the tree to select instructions
89 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, Target))
97 // Record instructions in the vector for each basic block
99 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI)
101 MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec();
102 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
104 MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec();
105 for (unsigned i=0; i < mvec.size(); i++)
106 bbMvec.push_back(mvec[i]);
110 if (SelectDebugLevel >= Select_PrintMachineCode)
112 cout << endl << "*** Machine instructions after INSTRUCTION SELECTION" << endl;
113 PrintMachineInstructions(method);
120 //*********************** Private Functions *****************************/
123 //---------------------------------------------------------------------------
124 // Function SelectInstructionsForTree
126 // Recursively walk the tree to select instructions.
127 // Do this top-down so that child instructions can exploit decisions
128 // made at the child instructions.
130 // E.g., if br(setle(reg,const)) decides the constant is 0 and uses
131 // a branch-on-integer-register instruction, then the setle node
132 // can use that information to avoid generating the SUBcc instruction.
134 // Note that this cannot be done bottom-up because setle must do this
135 // only if it is a child of the branch (otherwise, the result of setle
136 // may be used by multiple instructions).
137 //---------------------------------------------------------------------------
140 SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
141 TargetMachine &Target)
143 // Use a static vector to avoid allocating a new one per VM instruction
144 static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
146 // Get the rule that matches this node.
148 int ruleForNode = burm_rule(treeRoot->state, goalnt);
150 if (ruleForNode == 0)
152 cerr << "Could not match instruction tree for instr selection" << endl;
157 // Get this rule's non-terminals and the corresponding child nodes (if any)
159 short *nts = burm_nts[ruleForNode];
162 // First, select instructions for the current node and rule.
163 // (If this is a list node, not an instruction, then skip this step).
164 // This function is specific to the target architecture.
166 if (treeRoot->opLabel != VRegListOp)
168 InstructionNode* instrNode = (InstructionNode*)treeRoot;
169 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
171 unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, Target,
173 assert(N <= MAX_INSTR_PER_VMINSTR);
174 for (unsigned i=0; i < N; i++)
176 assert(minstrVec[i] != NULL);
177 instrNode->getInstruction()->addMachineInstruction(minstrVec[i]);
181 // Then, recursively compile the child nodes, if any.
184 { // i.e., there is at least one kid
185 InstrTreeNode* kids[2];
186 int currentRule = ruleForNode;
187 burm_kids(treeRoot, currentRule, kids);
189 // First skip over any chain rules so that we don't visit
190 // the current node again.
192 while (ThisIsAChainRule(currentRule))
194 currentRule = burm_rule(treeRoot->state, nts[0]);
195 nts = burm_nts[currentRule];
196 burm_kids(treeRoot, currentRule, kids);
199 // Now we have the first non-chain rule so we have found
200 // the actual child nodes. Recursively compile them.
202 for (int i = 0; nts[i]; i++)
205 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
206 if (nodeType == InstrTreeNode::NTVRegListNode ||
207 nodeType == InstrTreeNode::NTInstructionNode)
209 if (SelectInstructionsForTree(kids[i], nts[i], Target))
210 return true; // failure
215 return false; // success