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