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