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