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 | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 20 | #include "llvm/Instruction.h" |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 21 | #include "llvm/BasicBlock.h" |
| 22 | #include "llvm/Method.h" |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 23 | #include "llvm/iOther.h" |
| 24 | #include "llvm/Target/MachineRegInfo.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 25 | #include "Support/CommandLine.h" |
Ruchira Sasanka | 07c7086 | 2001-11-15 20:46:40 +0000 | [diff] [blame] | 26 | #include <string.h> |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 27 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 28 | |
Vikram S. Adve | 7ad1046 | 2001-10-22 13:51:09 +0000 | [diff] [blame] | 29 | //******************** Internal Data Declarations ************************/ |
| 30 | |
| 31 | // Use a static vector to avoid allocating a new one per VM instruction |
| 32 | static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR]; |
| 33 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 34 | |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 35 | enum SelectDebugLevel_t { |
| 36 | Select_NoDebugInfo, |
| 37 | Select_PrintMachineCode, |
| 38 | Select_DebugInstTrees, |
| 39 | Select_DebugBurgTrees, |
| 40 | }; |
| 41 | |
| 42 | // Enable Debug Options to be specified on the command line |
Chris Lattner | 5f6baf7 | 2001-09-12 16:34:03 +0000 | [diff] [blame] | 43 | cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags, |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 44 | "enable instruction selection debugging information", |
| 45 | clEnumValN(Select_NoDebugInfo, "n", "disable debug output"), |
| 46 | clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"), |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 47 | clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "), |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 48 | clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0); |
| 49 | |
| 50 | |
Vikram S. Adve | 7ad1046 | 2001-10-22 13:51:09 +0000 | [diff] [blame] | 51 | //******************** Forward Function Declarations ***********************/ |
| 52 | |
| 53 | |
| 54 | static bool SelectInstructionsForTree (InstrTreeNode* treeRoot, |
| 55 | int goalnt, |
| 56 | TargetMachine &target); |
| 57 | |
| 58 | static void PostprocessMachineCodeForTree(InstructionNode* instrNode, |
| 59 | int ruleForNode, |
| 60 | short* nts, |
| 61 | TargetMachine &target); |
| 62 | |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 63 | static void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target); |
| 64 | |
| 65 | |
Vikram S. Adve | 7ad1046 | 2001-10-22 13:51:09 +0000 | [diff] [blame] | 66 | |
| 67 | //******************* Externally Visible Functions *************************/ |
| 68 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 69 | |
| 70 | //--------------------------------------------------------------------------- |
| 71 | // Entry point for instruction selection using BURG. |
| 72 | // Returns true if instruction selection failed, false otherwise. |
| 73 | //--------------------------------------------------------------------------- |
| 74 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 75 | bool |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 76 | SelectInstructionsForMethod(Method* method, TargetMachine &target) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 77 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 78 | bool failed = false; |
| 79 | |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 80 | // |
| 81 | // Build the instruction trees to be given as inputs to BURG. |
| 82 | // |
Chris Lattner | 5f6baf7 | 2001-09-12 16:34:03 +0000 | [diff] [blame] | 83 | InstrForest instrForest(method); |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 84 | |
| 85 | if (SelectDebugLevel >= Select_DebugInstTrees) |
| 86 | { |
| 87 | cout << "\n\n*** Instruction trees for method " |
| 88 | << (method->hasName()? method->getName() : "") |
| 89 | << endl << endl; |
| 90 | instrForest.dump(); |
| 91 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 92 | |
| 93 | // |
| 94 | // Invoke BURG instruction selection for each tree |
| 95 | // |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 96 | const hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet(); |
Chris Lattner | 75279cc | 2001-07-23 03:50:57 +0000 | [diff] [blame] | 97 | for (hash_set<InstructionNode*>::const_iterator |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 98 | treeRootIter = treeRoots.begin(); treeRootIter != treeRoots.end(); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 99 | ++treeRootIter) |
| 100 | { |
| 101 | InstrTreeNode* basicNode = *treeRootIter; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 102 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 103 | // Invoke BURM to label each tree node with a state |
| 104 | burm_label(basicNode); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 105 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 106 | if (SelectDebugLevel >= Select_DebugBurgTrees) |
| 107 | { |
| 108 | printcover(basicNode, 1, 0); |
| 109 | cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n"; |
| 110 | printMatches(basicNode); |
| 111 | } |
| 112 | |
| 113 | // Then recursively walk the tree to select instructions |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 114 | if (SelectInstructionsForTree(basicNode, /*goalnt*/1, target)) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 115 | { |
| 116 | failed = true; |
| 117 | break; |
| 118 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 121 | // |
| 122 | // Record instructions in the vector for each basic block |
| 123 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 124 | for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) |
| 125 | { |
| 126 | MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec(); |
| 127 | for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II) |
| 128 | { |
| 129 | MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec(); |
| 130 | for (unsigned i=0; i < mvec.size(); i++) |
| 131 | bbMvec.push_back(mvec[i]); |
| 132 | } |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 133 | } |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 134 | |
| 135 | // Insert phi elimination code -- added by Ruchira |
| 136 | InsertCode4AllPhisInMeth(method, target); |
| 137 | |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 138 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 139 | if (SelectDebugLevel >= Select_PrintMachineCode) |
| 140 | { |
Vikram S. Adve | 7ad1046 | 2001-10-22 13:51:09 +0000 | [diff] [blame] | 141 | cout << endl |
| 142 | << "*** Machine instructions after INSTRUCTION SELECTION" << endl; |
Vikram S. Adve | be49526 | 2001-11-08 04:47:06 +0000 | [diff] [blame] | 143 | MachineCodeForMethod::get(method).dump(); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 144 | } |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 145 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | |
| 149 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 150 | //*********************** Private Functions *****************************/ |
| 151 | |
| 152 | |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 153 | //------------------------------------------------------------------------- |
| 154 | // Thid method inserts a copy instruction to a predecessor BB as a result |
| 155 | // of phi elimination. |
| 156 | //------------------------------------------------------------------------- |
| 157 | |
Ruchira Sasanka | 7130938 | 2001-11-12 19:42:27 +0000 | [diff] [blame] | 158 | void InsertPhiElimInst(BasicBlock *BB, MachineInstr *CpMI) { |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 159 | |
| 160 | TerminatorInst *TermInst = BB->getTerminator(); |
| 161 | MachineCodeForVMInstr &MC4Term = TermInst->getMachineInstrVec(); |
| 162 | MachineInstr *FirstMIOfTerm = *( MC4Term.begin() ); |
| 163 | |
| 164 | assert( FirstMIOfTerm && "No Machine Instrs for terminator" ); |
| 165 | |
| 166 | // get an iterator to machine instructions in the BB |
| 167 | MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec(); |
| 168 | MachineCodeForBasicBlock::iterator MCIt = bbMvec.begin(); |
| 169 | |
| 170 | // find the position of first machine instruction generated by the |
| 171 | // terminator of this BB |
| 172 | for( ; (MCIt != bbMvec.end()) && (*MCIt != FirstMIOfTerm) ; ++MCIt ) ; |
| 173 | |
| 174 | assert( MCIt != bbMvec.end() && "Start inst of terminator not found"); |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 175 | |
| 176 | // insert the copy instruction just before the first machine instruction |
| 177 | // generated for the terminator |
Ruchira Sasanka | 7130938 | 2001-11-12 19:42:27 +0000 | [diff] [blame] | 178 | bbMvec.insert( MCIt , CpMI ); |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 179 | |
Ruchira Sasanka | 7130938 | 2001-11-12 19:42:27 +0000 | [diff] [blame] | 180 | //cerr << "\nPhiElimination copy inst: " << *CopyInstVec[0]; |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 181 | |
| 182 | } |
| 183 | |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 184 | #if 0 |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 185 | //------------------------------------------------------------------------- |
| 186 | // This method inserts phi elimination code for all BBs in a method |
| 187 | //------------------------------------------------------------------------- |
| 188 | void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target) { |
| 189 | |
| 190 | |
| 191 | // for all basic blocks in method |
| 192 | // |
| 193 | for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) { |
| 194 | |
| 195 | BasicBlock *BB = *BI; |
| 196 | const BasicBlock::InstListType &InstList = BB->getInstList(); |
| 197 | BasicBlock::InstListType::const_iterator IIt = InstList.begin(); |
| 198 | |
| 199 | // for all instructions in the basic block |
| 200 | // |
| 201 | for( ; IIt != InstList.end(); ++IIt ) { |
| 202 | |
| 203 | if( (*IIt)->getOpcode() == Instruction::PHINode ) { |
| 204 | |
| 205 | PHINode *PN = (PHINode *) (*IIt); |
| 206 | |
| 207 | // for each incoming value of the phi, insert phi elimination |
| 208 | // |
| 209 | for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) { |
| 210 | |
| 211 | // insert the copy instruction to the predecessor BB |
| 212 | |
| 213 | vector<MachineInstr*> CopyInstVec; |
| 214 | |
Ruchira Sasanka | 7130938 | 2001-11-12 19:42:27 +0000 | [diff] [blame] | 215 | MachineInstr *CpMI = |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 216 | target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PN); |
| 217 | |
Ruchira Sasanka | 7130938 | 2001-11-12 19:42:27 +0000 | [diff] [blame] | 218 | InsertPhiElimInst( PN->getIncomingBlock(i), CpMI); |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | else break; // since PHI nodes can only be at the top |
| 222 | |
| 223 | } // for each Phi Instr in BB |
| 224 | |
| 225 | } // for all BBs in method |
| 226 | |
| 227 | } |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 228 | #endif |
| 229 | |
| 230 | |
| 231 | //------------------------------------------------------------------------- |
| 232 | // This method inserts phi elimination code for all BBs in a method |
| 233 | //------------------------------------------------------------------------- |
| 234 | void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target) { |
| 235 | |
| 236 | |
| 237 | // for all basic blocks in method |
| 238 | // |
| 239 | for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) { |
| 240 | |
| 241 | BasicBlock *BB = *BI; |
| 242 | const BasicBlock::InstListType &InstList = BB->getInstList(); |
| 243 | BasicBlock::InstListType::const_iterator IIt = InstList.begin(); |
| 244 | |
| 245 | // for all instructions in the basic block |
| 246 | // |
| 247 | for( ; IIt != InstList.end(); ++IIt ) { |
| 248 | |
| 249 | if( (*IIt)->getOpcode() == Instruction::PHINode ) { |
| 250 | |
| 251 | PHINode *PN = (PHINode *) (*IIt); |
| 252 | |
| 253 | Value *PhiCpRes = |
| 254 | new Value(PN->getType(), PN->getValueType() ); |
| 255 | |
Ruchira Sasanka | 07c7086 | 2001-11-15 20:46:40 +0000 | [diff] [blame] | 256 | string *Name = new string("PhiCp:"); |
| 257 | (*Name) += (int) PhiCpRes; |
| 258 | PhiCpRes->setName( *Name ); |
| 259 | |
| 260 | |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 261 | // for each incoming value of the phi, insert phi elimination |
| 262 | // |
| 263 | for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) { |
| 264 | |
| 265 | // insert the copy instruction to the predecessor BB |
| 266 | |
| 267 | MachineInstr *CpMI = |
| 268 | target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), |
| 269 | PhiCpRes); |
| 270 | |
| 271 | InsertPhiElimInst(PN->getIncomingBlock(i), CpMI); |
| 272 | |
| 273 | } |
| 274 | |
| 275 | |
| 276 | MachineInstr *CpMI2 = |
| 277 | target.getRegInfo().cpValue2Value(PhiCpRes, PN); |
| 278 | |
| 279 | // get an iterator to machine instructions in the BB |
| 280 | MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec(); |
| 281 | |
| 282 | bbMvec.insert( bbMvec.begin(), CpMI2); |
| 283 | |
| 284 | |
| 285 | } |
| 286 | else break; // since PHI nodes can only be at the top |
| 287 | |
| 288 | } // for each Phi Instr in BB |
| 289 | |
| 290 | } // for all BBs in method |
| 291 | |
| 292 | } |
| 293 | |
| 294 | |
| 295 | |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 302 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 7ad1046 | 2001-10-22 13:51:09 +0000 | [diff] [blame] | 303 | // Function AppendMachineCodeForVMInstr |
| 304 | // |
| 305 | // Append machine instr sequence to the machine code vec for a VM instr |
| 306 | //--------------------------------------------------------------------------- |
| 307 | |
| 308 | inline void |
| 309 | AppendMachineCodeForVMInstr(MachineInstr** minstrVec, |
| 310 | unsigned int N, |
| 311 | Instruction* vmInstr) |
| 312 | { |
| 313 | if (N == 0) |
| 314 | return; |
| 315 | MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec(); |
| 316 | mvec.insert(mvec.end(), minstrVec, minstrVec+N); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | |
| 321 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 322 | // Function PostprocessMachineCodeForTree |
| 323 | // |
| 324 | // Apply any final cleanups to machine code for the root of a subtree |
| 325 | // after selection for all its children has been completed. |
| 326 | //--------------------------------------------------------------------------- |
| 327 | |
Vikram S. Adve | 7ad1046 | 2001-10-22 13:51:09 +0000 | [diff] [blame] | 328 | static void |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 329 | PostprocessMachineCodeForTree(InstructionNode* instrNode, |
| 330 | int ruleForNode, |
| 331 | short* nts, |
| 332 | TargetMachine &target) |
| 333 | { |
| 334 | // Fix up any constant operands in the machine instructions to either |
| 335 | // use an immediate field or to load the constant into a register |
| 336 | // Walk backwards and use direct indexes to allow insertion before current |
| 337 | // |
| 338 | Instruction* vmInstr = instrNode->getInstruction(); |
| 339 | MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec(); |
| 340 | for (int i = (int) mvec.size()-1; i >= 0; i--) |
| 341 | { |
| 342 | vector<MachineInstr*> loadConstVec = |
| 343 | FixConstantOperandsForInstr(vmInstr, mvec[i], target); |
| 344 | |
| 345 | if (loadConstVec.size() > 0) |
| 346 | mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end()); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 351 | // Function SelectInstructionsForTree |
| 352 | // |
| 353 | // Recursively walk the tree to select instructions. |
| 354 | // Do this top-down so that child instructions can exploit decisions |
| 355 | // made at the child instructions. |
| 356 | // |
| 357 | // E.g., if br(setle(reg,const)) decides the constant is 0 and uses |
| 358 | // a branch-on-integer-register instruction, then the setle node |
| 359 | // can use that information to avoid generating the SUBcc instruction. |
| 360 | // |
| 361 | // Note that this cannot be done bottom-up because setle must do this |
| 362 | // only if it is a child of the branch (otherwise, the result of setle |
| 363 | // may be used by multiple instructions). |
| 364 | //--------------------------------------------------------------------------- |
| 365 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 366 | bool |
| 367 | SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt, |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 368 | TargetMachine &target) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 369 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 370 | // Get the rule that matches this node. |
| 371 | // |
| 372 | int ruleForNode = burm_rule(treeRoot->state, goalnt); |
| 373 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 374 | if (ruleForNode == 0) |
| 375 | { |
| 376 | cerr << "Could not match instruction tree for instr selection" << endl; |
| 377 | assert(0); |
| 378 | return true; |
| 379 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 380 | |
| 381 | // Get this rule's non-terminals and the corresponding child nodes (if any) |
| 382 | // |
| 383 | short *nts = burm_nts[ruleForNode]; |
| 384 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 385 | // First, select instructions for the current node and rule. |
| 386 | // (If this is a list node, not an instruction, then skip this step). |
| 387 | // This function is specific to the target architecture. |
| 388 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 389 | if (treeRoot->opLabel != VRegListOp) |
| 390 | { |
| 391 | InstructionNode* instrNode = (InstructionNode*)treeRoot; |
| 392 | assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode); |
Vikram S. Adve | 7ad1046 | 2001-10-22 13:51:09 +0000 | [diff] [blame] | 393 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 394 | unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, target, |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 395 | minstrVec); |
Vikram S. Adve | 7ad1046 | 2001-10-22 13:51:09 +0000 | [diff] [blame] | 396 | if (N > 0) |
| 397 | { |
| 398 | assert(N <= MAX_INSTR_PER_VMINSTR); |
| 399 | AppendMachineCodeForVMInstr(minstrVec,N,instrNode->getInstruction()); |
| 400 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // Then, recursively compile the child nodes, if any. |
| 404 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 405 | if (nts[0]) |
| 406 | { // i.e., there is at least one kid |
| 407 | InstrTreeNode* kids[2]; |
| 408 | int currentRule = ruleForNode; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 409 | burm_kids(treeRoot, currentRule, kids); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 410 | |
| 411 | // First skip over any chain rules so that we don't visit |
| 412 | // the current node again. |
| 413 | // |
| 414 | while (ThisIsAChainRule(currentRule)) |
| 415 | { |
| 416 | currentRule = burm_rule(treeRoot->state, nts[0]); |
| 417 | nts = burm_nts[currentRule]; |
| 418 | burm_kids(treeRoot, currentRule, kids); |
| 419 | } |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 420 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 421 | // Now we have the first non-chain rule so we have found |
| 422 | // the actual child nodes. Recursively compile them. |
| 423 | // |
| 424 | for (int i = 0; nts[i]; i++) |
| 425 | { |
| 426 | assert(i < 2); |
| 427 | InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType(); |
| 428 | if (nodeType == InstrTreeNode::NTVRegListNode || |
| 429 | nodeType == InstrTreeNode::NTInstructionNode) |
| 430 | { |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 431 | if (SelectInstructionsForTree(kids[i], nts[i], target)) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 432 | return true; // failure |
| 433 | } |
| 434 | } |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 435 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 436 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 437 | // Finally, do any postprocessing on this node after its children |
| 438 | // have been translated |
| 439 | // |
| 440 | if (treeRoot->opLabel != VRegListOp) |
| 441 | { |
| 442 | InstructionNode* instrNode = (InstructionNode*)treeRoot; |
| 443 | PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target); |
| 444 | } |
| 445 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 446 | return false; // success |
| 447 | } |
| 448 | |