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