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