Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 1 | // $Id$ -*-c++-*- |
| 2 | //*************************************************************************** |
| 3 | // File: |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 4 | // InstrSelection.cpp |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 5 | // |
| 6 | // Purpose: |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 7 | // Machine-independent driver file for instruction selection. |
| 8 | // This file constructs a forest of BURG instruction trees and then |
Vikram S. Adve | 9aba1d3 | 2001-10-10 20:49:07 +0000 | [diff] [blame] | 9 | // uses the BURG-generated tree grammar (BURM) to find the optimal |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 10 | // instruction sequences for a given machine. |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 11 | // |
| 12 | // History: |
| 13 | // 7/02/01 - Vikram Adve - Created |
Vikram S. Adve | 960066a | 2001-07-31 21:53:25 +0000 | [diff] [blame] | 14 | //**************************************************************************/ |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 15 | |
| 16 | |
Chris Lattner | feb6059 | 2001-09-07 17:15:18 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/InstrSelection.h" |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame^] | 18 | #include "llvm/CodeGen/InstrSelectionSupport.h" |
Chris Lattner | d268ad6 | 2001-09-11 23:52:11 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstr.h" |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 21 | #include "llvm/Instruction.h" |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 22 | #include "llvm/BasicBlock.h" |
| 23 | #include "llvm/Method.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 24 | |
Chris Lattner | d268ad6 | 2001-09-11 23:52:11 +0000 | [diff] [blame] | 25 | static bool SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt, |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame^] | 26 | TargetMachine &target); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 27 | |
| 28 | |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 29 | enum SelectDebugLevel_t { |
| 30 | Select_NoDebugInfo, |
| 31 | Select_PrintMachineCode, |
| 32 | Select_DebugInstTrees, |
| 33 | Select_DebugBurgTrees, |
| 34 | }; |
| 35 | |
| 36 | // Enable Debug Options to be specified on the command line |
Chris Lattner | 5f6baf7 | 2001-09-12 16:34:03 +0000 | [diff] [blame] | 37 | cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags, |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 38 | "enable instruction selection debugging information", |
| 39 | clEnumValN(Select_NoDebugInfo, "n", "disable debug output"), |
| 40 | clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"), |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 41 | clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "), |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 42 | clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0); |
| 43 | |
| 44 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 45 | |
| 46 | //--------------------------------------------------------------------------- |
| 47 | // Entry point for instruction selection using BURG. |
| 48 | // Returns true if instruction selection failed, false otherwise. |
| 49 | //--------------------------------------------------------------------------- |
| 50 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 51 | bool |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame^] | 52 | SelectInstructionsForMethod(Method* method, TargetMachine &target) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 53 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 54 | bool failed = false; |
| 55 | |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 56 | // |
| 57 | // Build the instruction trees to be given as inputs to BURG. |
| 58 | // |
Chris Lattner | 5f6baf7 | 2001-09-12 16:34:03 +0000 | [diff] [blame] | 59 | InstrForest instrForest(method); |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 60 | |
| 61 | if (SelectDebugLevel >= Select_DebugInstTrees) |
| 62 | { |
| 63 | cout << "\n\n*** Instruction trees for method " |
| 64 | << (method->hasName()? method->getName() : "") |
| 65 | << endl << endl; |
| 66 | instrForest.dump(); |
| 67 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 68 | |
| 69 | // |
| 70 | // Invoke BURG instruction selection for each tree |
| 71 | // |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 72 | const hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet(); |
Chris Lattner | 75279cc | 2001-07-23 03:50:57 +0000 | [diff] [blame] | 73 | for (hash_set<InstructionNode*>::const_iterator |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 74 | treeRootIter = treeRoots.begin(); treeRootIter != treeRoots.end(); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 75 | ++treeRootIter) |
| 76 | { |
| 77 | InstrTreeNode* basicNode = *treeRootIter; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 78 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 79 | // Invoke BURM to label each tree node with a state |
| 80 | burm_label(basicNode); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 81 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 82 | if (SelectDebugLevel >= Select_DebugBurgTrees) |
| 83 | { |
| 84 | printcover(basicNode, 1, 0); |
| 85 | cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n"; |
| 86 | printMatches(basicNode); |
| 87 | } |
| 88 | |
| 89 | // Then recursively walk the tree to select instructions |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame^] | 90 | if (SelectInstructionsForTree(basicNode, /*goalnt*/1, target)) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 91 | { |
| 92 | failed = true; |
| 93 | break; |
| 94 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 97 | // |
| 98 | // Record instructions in the vector for each basic block |
| 99 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 100 | for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) |
| 101 | { |
| 102 | MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec(); |
| 103 | for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II) |
| 104 | { |
| 105 | MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec(); |
| 106 | for (unsigned i=0; i < mvec.size(); i++) |
| 107 | bbMvec.push_back(mvec[i]); |
| 108 | } |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 111 | if (SelectDebugLevel >= Select_PrintMachineCode) |
| 112 | { |
| 113 | cout << endl << "*** Machine instructions after INSTRUCTION SELECTION" << endl; |
| 114 | PrintMachineInstructions(method); |
| 115 | } |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 116 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
| 120 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 121 | //*********************** Private Functions *****************************/ |
| 122 | |
| 123 | |
| 124 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame^] | 125 | // Function PostprocessMachineCodeForTree |
| 126 | // |
| 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 | //--------------------------------------------------------------------------- |
| 130 | |
| 131 | void |
| 132 | PostprocessMachineCodeForTree(InstructionNode* instrNode, |
| 133 | int ruleForNode, |
| 134 | short* nts, |
| 135 | TargetMachine &target) |
| 136 | { |
| 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 |
| 140 | // |
| 141 | Instruction* vmInstr = instrNode->getInstruction(); |
| 142 | MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec(); |
| 143 | for (int i = (int) mvec.size()-1; i >= 0; i--) |
| 144 | { |
| 145 | vector<MachineInstr*> loadConstVec = |
| 146 | FixConstantOperandsForInstr(vmInstr, mvec[i], target); |
| 147 | |
| 148 | if (loadConstVec.size() > 0) |
| 149 | mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end()); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 154 | // Function SelectInstructionsForTree |
| 155 | // |
| 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. |
| 159 | // |
| 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. |
| 163 | // |
| 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 | //--------------------------------------------------------------------------- |
| 168 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 169 | bool |
| 170 | SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt, |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame^] | 171 | TargetMachine &target) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 172 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 173 | // Use a static vector to avoid allocating a new one per VM instruction |
| 174 | static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR]; |
| 175 | |
| 176 | // Get the rule that matches this node. |
| 177 | // |
| 178 | int ruleForNode = burm_rule(treeRoot->state, goalnt); |
| 179 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 180 | if (ruleForNode == 0) |
| 181 | { |
| 182 | cerr << "Could not match instruction tree for instr selection" << endl; |
| 183 | assert(0); |
| 184 | return true; |
| 185 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 186 | |
| 187 | // Get this rule's non-terminals and the corresponding child nodes (if any) |
| 188 | // |
| 189 | short *nts = burm_nts[ruleForNode]; |
| 190 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 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. |
| 194 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 195 | if (treeRoot->opLabel != VRegListOp) |
| 196 | { |
| 197 | InstructionNode* instrNode = (InstructionNode*)treeRoot; |
| 198 | assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode); |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 199 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame^] | 200 | unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, target, |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 201 | minstrVec); |
| 202 | assert(N <= MAX_INSTR_PER_VMINSTR); |
| 203 | for (unsigned i=0; i < N; i++) |
| 204 | { |
| 205 | assert(minstrVec[i] != NULL); |
| 206 | instrNode->getInstruction()->addMachineInstruction(minstrVec[i]); |
| 207 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Then, recursively compile the child nodes, if any. |
| 211 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 212 | if (nts[0]) |
| 213 | { // i.e., there is at least one kid |
| 214 | InstrTreeNode* kids[2]; |
| 215 | int currentRule = ruleForNode; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 216 | burm_kids(treeRoot, currentRule, kids); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 217 | |
| 218 | // First skip over any chain rules so that we don't visit |
| 219 | // the current node again. |
| 220 | // |
| 221 | while (ThisIsAChainRule(currentRule)) |
| 222 | { |
| 223 | currentRule = burm_rule(treeRoot->state, nts[0]); |
| 224 | nts = burm_nts[currentRule]; |
| 225 | burm_kids(treeRoot, currentRule, kids); |
| 226 | } |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 227 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 228 | // Now we have the first non-chain rule so we have found |
| 229 | // the actual child nodes. Recursively compile them. |
| 230 | // |
| 231 | for (int i = 0; nts[i]; i++) |
| 232 | { |
| 233 | assert(i < 2); |
| 234 | InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType(); |
| 235 | if (nodeType == InstrTreeNode::NTVRegListNode || |
| 236 | nodeType == InstrTreeNode::NTInstructionNode) |
| 237 | { |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame^] | 238 | if (SelectInstructionsForTree(kids[i], nts[i], target)) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 239 | return true; // failure |
| 240 | } |
| 241 | } |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 242 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 243 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame^] | 244 | // Finally, do any postprocessing on this node after its children |
| 245 | // have been translated |
| 246 | // |
| 247 | if (treeRoot->opLabel != VRegListOp) |
| 248 | { |
| 249 | InstructionNode* instrNode = (InstructionNode*)treeRoot; |
| 250 | PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target); |
| 251 | } |
| 252 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 253 | return false; // success |
| 254 | } |
| 255 | |