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 |
| 9 | // use the BURG-generated tree grammar (BURM) to find the optimal |
| 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" |
Chris Lattner | d268ad6 | 2001-09-11 23:52:11 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstr.h" |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 20 | #include "llvm/Type.h" |
| 21 | #include "llvm/iMemory.h" |
| 22 | #include "llvm/Instruction.h" |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 23 | #include "llvm/BasicBlock.h" |
| 24 | #include "llvm/Method.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 25 | |
Chris Lattner | d268ad6 | 2001-09-11 23:52:11 +0000 | [diff] [blame] | 26 | static bool SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt, |
Chris Lattner | 6c5a32d | 2001-07-23 03:09:03 +0000 | [diff] [blame] | 27 | TargetMachine &Target); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 28 | |
| 29 | |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 30 | enum SelectDebugLevel_t { |
| 31 | Select_NoDebugInfo, |
| 32 | Select_PrintMachineCode, |
| 33 | Select_DebugInstTrees, |
| 34 | Select_DebugBurgTrees, |
| 35 | }; |
| 36 | |
| 37 | // Enable Debug Options to be specified on the command line |
Chris Lattner | 5f6baf7 | 2001-09-12 16:34:03 +0000 | [diff] [blame] | 38 | cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags, |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 39 | "enable instruction selection debugging information", |
| 40 | clEnumValN(Select_NoDebugInfo, "n", "disable debug output"), |
| 41 | clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"), |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 42 | clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "), |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 43 | clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0); |
| 44 | |
| 45 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 46 | |
| 47 | //--------------------------------------------------------------------------- |
| 48 | // Entry point for instruction selection using BURG. |
| 49 | // Returns true if instruction selection failed, false otherwise. |
| 50 | //--------------------------------------------------------------------------- |
| 51 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 52 | bool |
| 53 | SelectInstructionsForMethod(Method* method, TargetMachine &Target) |
| 54 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 55 | bool failed = false; |
| 56 | |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 57 | // |
| 58 | // Build the instruction trees to be given as inputs to BURG. |
| 59 | // |
Chris Lattner | 5f6baf7 | 2001-09-12 16:34:03 +0000 | [diff] [blame] | 60 | InstrForest instrForest(method); |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 61 | |
| 62 | if (SelectDebugLevel >= Select_DebugInstTrees) |
| 63 | { |
| 64 | cout << "\n\n*** Instruction trees for method " |
| 65 | << (method->hasName()? method->getName() : "") |
| 66 | << endl << endl; |
| 67 | instrForest.dump(); |
| 68 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 69 | |
| 70 | // |
| 71 | // Invoke BURG instruction selection for each tree |
| 72 | // |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 73 | const hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet(); |
Chris Lattner | 75279cc | 2001-07-23 03:50:57 +0000 | [diff] [blame] | 74 | for (hash_set<InstructionNode*>::const_iterator |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 75 | treeRootIter = treeRoots.begin(); treeRootIter != treeRoots.end(); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 76 | ++treeRootIter) |
| 77 | { |
| 78 | InstrTreeNode* basicNode = *treeRootIter; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 79 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 80 | // Invoke BURM to label each tree node with a state |
| 81 | burm_label(basicNode); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 82 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 83 | if (SelectDebugLevel >= Select_DebugBurgTrees) |
| 84 | { |
| 85 | printcover(basicNode, 1, 0); |
| 86 | cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n"; |
| 87 | printMatches(basicNode); |
| 88 | } |
| 89 | |
| 90 | // Then recursively walk the tree to select instructions |
| 91 | if (SelectInstructionsForTree(basicNode, /*goalnt*/1, Target)) |
| 92 | { |
| 93 | failed = true; |
| 94 | break; |
| 95 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 98 | // |
| 99 | // Record instructions in the vector for each basic block |
| 100 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 101 | for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) |
| 102 | { |
| 103 | MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec(); |
| 104 | for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II) |
| 105 | { |
| 106 | MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec(); |
| 107 | for (unsigned i=0; i < mvec.size(); i++) |
| 108 | bbMvec.push_back(mvec[i]); |
| 109 | } |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 112 | if (SelectDebugLevel >= Select_PrintMachineCode) |
| 113 | { |
| 114 | cout << endl << "*** Machine instructions after INSTRUCTION SELECTION" << endl; |
| 115 | PrintMachineInstructions(method); |
| 116 | } |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 117 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | //--------------------------------------------------------------------------- |
| 123 | // Function: FoldGetElemChain |
| 124 | // |
| 125 | // Purpose: |
| 126 | // Fold a chain of GetElementPtr instructions into an equivalent |
| 127 | // (Pointer, IndexVector) pair. Returns the pointer Value, and |
| 128 | // stores the resulting IndexVector in argument chainIdxVec. |
| 129 | //--------------------------------------------------------------------------- |
| 130 | |
| 131 | Value* |
| 132 | FoldGetElemChain(const InstructionNode* getElemInstrNode, |
| 133 | vector<ConstPoolVal*>& chainIdxVec) |
| 134 | { |
| 135 | MemAccessInst* getElemInst = (MemAccessInst*) |
| 136 | getElemInstrNode->getInstruction(); |
| 137 | |
| 138 | // Initialize return values from the incoming instruction |
| 139 | Value* ptrVal = getElemInst->getPtrOperand(); |
| 140 | chainIdxVec = getElemInst->getIndexVec(); // copies index vector values |
| 141 | |
| 142 | // Now chase the chain of getElementInstr instructions, if any |
| 143 | InstrTreeNode* ptrChild = getElemInstrNode->leftChild(); |
| 144 | while (ptrChild->getOpLabel() == Instruction::GetElementPtr || |
| 145 | ptrChild->getOpLabel() == GetElemPtrIdx) |
| 146 | { |
| 147 | // Child is a GetElemPtr instruction |
| 148 | getElemInst = (MemAccessInst*) |
| 149 | ((InstructionNode*) ptrChild)->getInstruction(); |
| 150 | const vector<ConstPoolVal*>& idxVec = getElemInst->getIndexVec(); |
| 151 | |
| 152 | // Get the pointer value out of ptrChild and *prepend* its index vector |
| 153 | ptrVal = getElemInst->getPtrOperand(); |
| 154 | chainIdxVec.insert(chainIdxVec.begin(), idxVec.begin(), idxVec.end()); |
| 155 | |
| 156 | ptrChild = ptrChild->leftChild(); |
| 157 | } |
| 158 | |
| 159 | return ptrVal; |
| 160 | } |
| 161 | |
| 162 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 163 | //*********************** Private Functions *****************************/ |
| 164 | |
| 165 | |
| 166 | //--------------------------------------------------------------------------- |
| 167 | // Function SelectInstructionsForTree |
| 168 | // |
| 169 | // Recursively walk the tree to select instructions. |
| 170 | // Do this top-down so that child instructions can exploit decisions |
| 171 | // made at the child instructions. |
| 172 | // |
| 173 | // E.g., if br(setle(reg,const)) decides the constant is 0 and uses |
| 174 | // a branch-on-integer-register instruction, then the setle node |
| 175 | // can use that information to avoid generating the SUBcc instruction. |
| 176 | // |
| 177 | // Note that this cannot be done bottom-up because setle must do this |
| 178 | // only if it is a child of the branch (otherwise, the result of setle |
| 179 | // may be used by multiple instructions). |
| 180 | //--------------------------------------------------------------------------- |
| 181 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 182 | bool |
| 183 | SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt, |
| 184 | TargetMachine &Target) |
| 185 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 186 | // Use a static vector to avoid allocating a new one per VM instruction |
| 187 | static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR]; |
| 188 | |
| 189 | // Get the rule that matches this node. |
| 190 | // |
| 191 | int ruleForNode = burm_rule(treeRoot->state, goalnt); |
| 192 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 193 | if (ruleForNode == 0) |
| 194 | { |
| 195 | cerr << "Could not match instruction tree for instr selection" << endl; |
| 196 | assert(0); |
| 197 | return true; |
| 198 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 199 | |
| 200 | // Get this rule's non-terminals and the corresponding child nodes (if any) |
| 201 | // |
| 202 | short *nts = burm_nts[ruleForNode]; |
| 203 | |
| 204 | |
| 205 | // First, select instructions for the current node and rule. |
| 206 | // (If this is a list node, not an instruction, then skip this step). |
| 207 | // This function is specific to the target architecture. |
| 208 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 209 | if (treeRoot->opLabel != VRegListOp) |
| 210 | { |
| 211 | InstructionNode* instrNode = (InstructionNode*)treeRoot; |
| 212 | assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode); |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 213 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 214 | unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, Target, |
| 215 | minstrVec); |
| 216 | assert(N <= MAX_INSTR_PER_VMINSTR); |
| 217 | for (unsigned i=0; i < N; i++) |
| 218 | { |
| 219 | assert(minstrVec[i] != NULL); |
| 220 | instrNode->getInstruction()->addMachineInstruction(minstrVec[i]); |
| 221 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // Then, recursively compile the child nodes, if any. |
| 225 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 226 | if (nts[0]) |
| 227 | { // i.e., there is at least one kid |
| 228 | InstrTreeNode* kids[2]; |
| 229 | int currentRule = ruleForNode; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 230 | burm_kids(treeRoot, currentRule, kids); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 231 | |
| 232 | // First skip over any chain rules so that we don't visit |
| 233 | // the current node again. |
| 234 | // |
| 235 | while (ThisIsAChainRule(currentRule)) |
| 236 | { |
| 237 | currentRule = burm_rule(treeRoot->state, nts[0]); |
| 238 | nts = burm_nts[currentRule]; |
| 239 | burm_kids(treeRoot, currentRule, kids); |
| 240 | } |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 241 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame^] | 242 | // Now we have the first non-chain rule so we have found |
| 243 | // the actual child nodes. Recursively compile them. |
| 244 | // |
| 245 | for (int i = 0; nts[i]; i++) |
| 246 | { |
| 247 | assert(i < 2); |
| 248 | InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType(); |
| 249 | if (nodeType == InstrTreeNode::NTVRegListNode || |
| 250 | nodeType == InstrTreeNode::NTInstructionNode) |
| 251 | { |
| 252 | if (SelectInstructionsForTree(kids[i], nts[i], Target)) |
| 253 | return true; // failure |
| 254 | } |
| 255 | } |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 256 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 257 | |
| 258 | return false; // success |
| 259 | } |
| 260 | |