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