Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 1 | //===- InstrSelection.cpp - Machine Independant Inst Selection Driver -----===// |
| 2 | // |
| 3 | // Machine-independent driver file for instruction selection. This file |
| 4 | // constructs a forest of BURG instruction trees and then uses the |
| 5 | // BURG-generated tree grammar (BURM) to find the optimal instruction sequences |
| 6 | // for a given machine. |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 9 | |
Chris Lattner | feb6059 | 2001-09-07 17:15:18 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/InstrSelection.h" |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 11 | #include "llvm/CodeGen/InstrSelectionSupport.h" |
Chris Lattner | 06cb1b7 | 2002-02-03 07:33:46 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/InstrForest.h" |
| 13 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | d0f166a | 2002-12-29 03:13:05 +0000 | [diff] [blame^] | 15 | #include "llvm/Target/TargetRegInfo.h" |
Chris Lattner | 06cb1b7 | 2002-02-03 07:33:46 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 18 | #include "llvm/iPHINode.h" |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 20 | #include "Support/CommandLine.h" |
Chris Lattner | a175ed4 | 2002-09-08 21:08:43 +0000 | [diff] [blame] | 21 | #include "Support/LeakDetector.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 22 | using std::cerr; |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 23 | using std::vector; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | //===--------------------------------------------------------------------===// |
| 27 | // SelectDebugLevel - Allow command line control over debugging. |
| 28 | // |
| 29 | enum SelectDebugLevel_t { |
| 30 | Select_NoDebugInfo, |
| 31 | Select_PrintMachineCode, |
| 32 | Select_DebugInstTrees, |
| 33 | Select_DebugBurgTrees, |
| 34 | }; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 36 | // Enable Debug Options to be specified on the command line |
| 37 | cl::opt<SelectDebugLevel_t> |
| 38 | SelectDebugLevel("dselect", cl::Hidden, |
| 39 | cl::desc("enable instruction selection debug information"), |
| 40 | cl::values( |
| 41 | clEnumValN(Select_NoDebugInfo, "n", "disable debug output"), |
| 42 | clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"), |
| 43 | clEnumValN(Select_DebugInstTrees, "i", |
| 44 | "print debugging info for instruction selection"), |
| 45 | clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), |
| 46 | 0)); |
| 47 | |
| 48 | |
| 49 | //===--------------------------------------------------------------------===// |
| 50 | // InstructionSelection Pass |
| 51 | // |
| 52 | // This is the actual pass object that drives the instruction selection |
| 53 | // process. |
| 54 | // |
| 55 | class InstructionSelection : public FunctionPass { |
| 56 | TargetMachine &Target; |
| 57 | void InsertCodeForPhis(Function &F); |
| 58 | void InsertPhiElimInstructions(BasicBlock *BB, |
Chris Lattner | b91b31c | 2002-08-09 20:05:34 +0000 | [diff] [blame] | 59 | const vector<MachineInstr*>& CpVec); |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 60 | void SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt); |
| 61 | void PostprocessMachineCodeForTree(InstructionNode* instrNode, |
| 62 | int ruleForNode, short* nts); |
| 63 | public: |
| 64 | InstructionSelection(TargetMachine &T) : Target(T) {} |
Chris Lattner | a087772 | 2002-10-23 03:30:47 +0000 | [diff] [blame] | 65 | |
| 66 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 67 | AU.setPreservesCFG(); |
| 68 | } |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 69 | |
| 70 | bool runOnFunction(Function &F); |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | // Register the pass... |
| 75 | static RegisterLLC<InstructionSelection> |
| 76 | X("instselect", "Instruction Selection", createInstructionSelectionPass); |
| 77 | |
Chris Lattner | a175ed4 | 2002-09-08 21:08:43 +0000 | [diff] [blame] | 78 | TmpInstruction::TmpInstruction(Value *s1, Value *s2, const std::string &name) |
| 79 | : Instruction(s1->getType(), Instruction::UserOp1, name) { |
| 80 | Operands.push_back(Use(s1, this)); // s1 must be nonnull |
| 81 | if (s2) { |
| 82 | Operands.push_back(Use(s2, this)); |
| 83 | } |
| 84 | |
| 85 | // TmpInstructions should not be garbage checked. |
| 86 | LeakDetector::removeGarbageObject(this); |
| 87 | } |
| 88 | |
| 89 | // Constructor that requires the type of the temporary to be specified. |
| 90 | // Both S1 and S2 may be NULL.( |
| 91 | TmpInstruction::TmpInstruction(const Type *Ty, Value *s1, Value* s2, |
| 92 | const std::string &name) |
| 93 | : Instruction(Ty, Instruction::UserOp1, name) { |
| 94 | if (s1) { Operands.push_back(Use(s1, this)); } |
| 95 | if (s2) { Operands.push_back(Use(s2, this)); } |
| 96 | |
| 97 | // TmpInstructions should not be garbage checked. |
| 98 | LeakDetector::removeGarbageObject(this); |
| 99 | } |
| 100 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 101 | |
| 102 | bool InstructionSelection::runOnFunction(Function &F) |
| 103 | { |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 104 | // |
| 105 | // Build the instruction trees to be given as inputs to BURG. |
| 106 | // |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 107 | InstrForest instrForest(&F); |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 108 | |
| 109 | if (SelectDebugLevel >= Select_DebugInstTrees) |
| 110 | { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 111 | cerr << "\n\n*** Input to instruction selection for function " |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 112 | << F.getName() << "\n\n" << F |
| 113 | << "\n\n*** Instruction trees for function " |
| 114 | << F.getName() << "\n\n"; |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 115 | instrForest.dump(); |
| 116 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 117 | |
| 118 | // |
| 119 | // Invoke BURG instruction selection for each tree |
| 120 | // |
Vikram S. Adve | 4e7bc49 | 2002-03-24 03:36:52 +0000 | [diff] [blame] | 121 | for (InstrForest::const_root_iterator RI = instrForest.roots_begin(); |
| 122 | RI != instrForest.roots_end(); ++RI) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 123 | { |
Vikram S. Adve | 4e7bc49 | 2002-03-24 03:36:52 +0000 | [diff] [blame] | 124 | InstructionNode* basicNode = *RI; |
| 125 | assert(basicNode->parent() == NULL && "A `root' node has a parent?"); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 126 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 127 | // Invoke BURM to label each tree node with a state |
| 128 | burm_label(basicNode); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 129 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 130 | if (SelectDebugLevel >= Select_DebugBurgTrees) |
| 131 | { |
| 132 | printcover(basicNode, 1, 0); |
| 133 | cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n"; |
| 134 | printMatches(basicNode); |
| 135 | } |
| 136 | |
| 137 | // Then recursively walk the tree to select instructions |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 138 | SelectInstructionsForTree(basicNode, /*goalnt*/1); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 141 | // |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 142 | // Create the MachineBasicBlock records and add all of the MachineInstrs |
| 143 | // defined in the MachineCodeForInstruction objects to also live in the |
| 144 | // MachineBasicBlock objects. |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 145 | // |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 146 | MachineFunction &MF = MachineFunction::get(&F); |
| 147 | for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) { |
| 148 | MachineBasicBlock *MCBB = new MachineBasicBlock(BI); |
| 149 | MF.getBasicBlockList().push_back(MCBB); |
| 150 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 151 | for (BasicBlock::iterator II = BI->begin(); II != BI->end(); ++II) { |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 152 | MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(II); |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 153 | MCBB->insert(MCBB->end(), mvec.begin(), mvec.end()); |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 155 | } |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 157 | // Insert phi elimination code |
| 158 | InsertCodeForPhis(F); |
Vikram S. Adve | 76d3520 | 2001-07-30 18:48:43 +0000 | [diff] [blame] | 159 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 160 | if (SelectDebugLevel >= Select_PrintMachineCode) |
| 161 | { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 162 | cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n"; |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 163 | MachineFunction::get(&F).dump(); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 164 | } |
Vikram S. Adve | 89df1ae | 2001-08-28 23:04:38 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 166 | return true; |
Ruchira Sasanka | b2490fc | 2001-11-12 14:44:50 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 169 | |
| 170 | //------------------------------------------------------------------------- |
| 171 | // This method inserts phi elimination code for all BBs in a method |
| 172 | //------------------------------------------------------------------------- |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 173 | |
Vikram S. Adve | 1ed009f | 2002-03-18 03:31:54 +0000 | [diff] [blame] | 174 | void |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 175 | InstructionSelection::InsertCodeForPhis(Function &F) |
Vikram S. Adve | 1ed009f | 2002-03-18 03:31:54 +0000 | [diff] [blame] | 176 | { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 177 | // for all basic blocks in function |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 178 | // |
Chris Lattner | fcffe86 | 2002-10-28 19:01:16 +0000 | [diff] [blame] | 179 | MachineFunction &MF = MachineFunction::get(&F); |
| 180 | for (MachineFunction::iterator BB = MF.begin(); BB != MF.end(); ++BB) { |
| 181 | for (BasicBlock::iterator IIt = BB->getBasicBlock()->begin(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 182 | PHINode *PN = dyn_cast<PHINode>(&*IIt); ++IIt) { |
| 183 | // FIXME: This is probably wrong... |
| 184 | Value *PhiCpRes = new PHINode(PN->getType(), "PhiCp:"); |
Chris Lattner | 823c4ab | 2002-09-08 21:19:29 +0000 | [diff] [blame] | 185 | |
| 186 | // The leak detector shouldn't track these nodes. They are not garbage, |
| 187 | // even though their parent field is never filled in. |
| 188 | // |
| 189 | LeakDetector::removeGarbageObject(PhiCpRes); |
| 190 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 191 | // for each incoming value of the phi, insert phi elimination |
| 192 | // |
| 193 | for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) { |
| 194 | // insert the copy instruction to the predecessor BB |
| 195 | vector<MachineInstr*> mvec, CpVec; |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 196 | Target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes, |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 197 | mvec); |
| 198 | for (vector<MachineInstr*>::iterator MI=mvec.begin(); |
| 199 | MI != mvec.end(); ++MI) { |
| 200 | vector<MachineInstr*> CpVec2 = |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 201 | FixConstantOperandsForInstr(PN, *MI, Target); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 202 | CpVec2.push_back(*MI); |
| 203 | CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end()); |
| 204 | } |
Vikram S. Adve | 1ed009f | 2002-03-18 03:31:54 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 206 | InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec); |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 207 | } |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 209 | vector<MachineInstr*> mvec; |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 210 | Target.getRegInfo().cpValue2Value(PhiCpRes, PN, mvec); |
Chris Lattner | fcffe86 | 2002-10-28 19:01:16 +0000 | [diff] [blame] | 211 | BB->insert(BB->begin(), mvec.begin(), mvec.end()); |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 212 | } // for each Phi Instr in BB |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 213 | } // for all BBs in function |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 216 | //------------------------------------------------------------------------- |
| 217 | // Thid method inserts a copy instruction to a predecessor BB as a result |
| 218 | // of phi elimination. |
| 219 | //------------------------------------------------------------------------- |
Ruchira Sasanka | 20ac79e | 2001-11-15 00:27:14 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 221 | void |
| 222 | InstructionSelection::InsertPhiElimInstructions(BasicBlock *BB, |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 223 | const vector<MachineInstr*>& CpVec) |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 224 | { |
| 225 | Instruction *TermInst = (Instruction*)BB->getTerminator(); |
| 226 | MachineCodeForInstruction &MC4Term = MachineCodeForInstruction::get(TermInst); |
| 227 | MachineInstr *FirstMIOfTerm = MC4Term.front(); |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 228 | assert (FirstMIOfTerm && "No Machine Instrs for terminator"); |
Chris Lattner | fcffe86 | 2002-10-28 19:01:16 +0000 | [diff] [blame] | 229 | |
| 230 | MachineFunction &MF = MachineFunction::get(BB->getParent()); |
Chris Lattner | fcffe86 | 2002-10-28 19:01:16 +0000 | [diff] [blame] | 231 | |
| 232 | // FIXME: if PHI instructions existed in the machine code, this would be |
| 233 | // unnecesary. |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 234 | MachineBasicBlock *MBB = 0; |
Chris Lattner | fcffe86 | 2002-10-28 19:01:16 +0000 | [diff] [blame] | 235 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) |
| 236 | if (I->getBasicBlock() == BB) { |
| 237 | MBB = I; |
| 238 | break; |
| 239 | } |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 241 | // find the position of first machine instruction generated by the |
| 242 | // terminator of this BB |
Chris Lattner | 55291ea | 2002-10-28 01:41:47 +0000 | [diff] [blame] | 243 | MachineBasicBlock::iterator MCIt = |
Chris Lattner | fcffe86 | 2002-10-28 19:01:16 +0000 | [diff] [blame] | 244 | std::find(MBB->begin(), MBB->end(), FirstMIOfTerm); |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 245 | |
Chris Lattner | fcffe86 | 2002-10-28 19:01:16 +0000 | [diff] [blame] | 246 | assert(MCIt != MBB->end() && "Start inst of terminator not found"); |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 247 | |
| 248 | // insert the copy instructions just before the first machine instruction |
| 249 | // generated for the terminator |
Chris Lattner | fcffe86 | 2002-10-28 19:01:16 +0000 | [diff] [blame] | 250 | MBB->insert(MCIt, CpVec.begin(), CpVec.end()); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 253 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 254 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 255 | // Function SelectInstructionsForTree |
| 256 | // |
| 257 | // Recursively walk the tree to select instructions. |
| 258 | // Do this top-down so that child instructions can exploit decisions |
| 259 | // made at the child instructions. |
| 260 | // |
| 261 | // E.g., if br(setle(reg,const)) decides the constant is 0 and uses |
| 262 | // a branch-on-integer-register instruction, then the setle node |
| 263 | // can use that information to avoid generating the SUBcc instruction. |
| 264 | // |
| 265 | // Note that this cannot be done bottom-up because setle must do this |
| 266 | // only if it is a child of the branch (otherwise, the result of setle |
| 267 | // may be used by multiple instructions). |
| 268 | //--------------------------------------------------------------------------- |
| 269 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 270 | void |
| 271 | InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot, |
| 272 | int goalnt) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 273 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 274 | // Get the rule that matches this node. |
| 275 | // |
| 276 | int ruleForNode = burm_rule(treeRoot->state, goalnt); |
| 277 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 278 | if (ruleForNode == 0) { |
| 279 | cerr << "Could not match instruction tree for instr selection\n"; |
| 280 | abort(); |
| 281 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 282 | |
| 283 | // Get this rule's non-terminals and the corresponding child nodes (if any) |
| 284 | // |
| 285 | short *nts = burm_nts[ruleForNode]; |
| 286 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 287 | // First, select instructions for the current node and rule. |
| 288 | // (If this is a list node, not an instruction, then skip this step). |
| 289 | // This function is specific to the target architecture. |
| 290 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 291 | if (treeRoot->opLabel != VRegListOp) |
| 292 | { |
Chris Lattner | b91b31c | 2002-08-09 20:05:34 +0000 | [diff] [blame] | 293 | vector<MachineInstr*> minstrVec; |
Vikram S. Adve | 1ed009f | 2002-03-18 03:31:54 +0000 | [diff] [blame] | 294 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 295 | InstructionNode* instrNode = (InstructionNode*)treeRoot; |
| 296 | assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode); |
Vikram S. Adve | 7ad1046 | 2001-10-22 13:51:09 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 298 | GetInstructionsByRule(instrNode, ruleForNode, nts, Target, minstrVec); |
Vikram S. Adve | 1ed009f | 2002-03-18 03:31:54 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 06cb1b7 | 2002-02-03 07:33:46 +0000 | [diff] [blame] | 300 | MachineCodeForInstruction &mvec = |
| 301 | MachineCodeForInstruction::get(instrNode->getInstruction()); |
Vikram S. Adve | 1ed009f | 2002-03-18 03:31:54 +0000 | [diff] [blame] | 302 | mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end()); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // Then, recursively compile the child nodes, if any. |
| 306 | // |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 307 | if (nts[0]) |
| 308 | { // i.e., there is at least one kid |
| 309 | InstrTreeNode* kids[2]; |
| 310 | int currentRule = ruleForNode; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 311 | burm_kids(treeRoot, currentRule, kids); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 312 | |
| 313 | // First skip over any chain rules so that we don't visit |
| 314 | // the current node again. |
| 315 | // |
| 316 | while (ThisIsAChainRule(currentRule)) |
| 317 | { |
| 318 | currentRule = burm_rule(treeRoot->state, nts[0]); |
| 319 | nts = burm_nts[currentRule]; |
| 320 | burm_kids(treeRoot, currentRule, kids); |
| 321 | } |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 322 | |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 323 | // Now we have the first non-chain rule so we have found |
| 324 | // the actual child nodes. Recursively compile them. |
| 325 | // |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 326 | for (unsigned i = 0; nts[i]; i++) |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 327 | { |
| 328 | assert(i < 2); |
| 329 | InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType(); |
| 330 | if (nodeType == InstrTreeNode::NTVRegListNode || |
| 331 | nodeType == InstrTreeNode::NTInstructionNode) |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 332 | SelectInstructionsForTree(kids[i], nts[i]); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 333 | } |
Chris Lattner | 0e6530e | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 334 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 335 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 336 | // Finally, do any postprocessing on this node after its children |
| 337 | // have been translated |
| 338 | // |
| 339 | if (treeRoot->opLabel != VRegListOp) |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 340 | PostprocessMachineCodeForTree((InstructionNode*)treeRoot, ruleForNode, nts); |
| 341 | } |
| 342 | |
| 343 | //--------------------------------------------------------------------------- |
| 344 | // Function PostprocessMachineCodeForTree |
| 345 | // |
| 346 | // Apply any final cleanups to machine code for the root of a subtree |
| 347 | // after selection for all its children has been completed. |
| 348 | // |
| 349 | void |
| 350 | InstructionSelection::PostprocessMachineCodeForTree(InstructionNode* instrNode, |
| 351 | int ruleForNode, |
| 352 | short* nts) |
| 353 | { |
| 354 | // Fix up any constant operands in the machine instructions to either |
| 355 | // use an immediate field or to load the constant into a register |
| 356 | // Walk backwards and use direct indexes to allow insertion before current |
| 357 | // |
| 358 | Instruction* vmInstr = instrNode->getInstruction(); |
| 359 | MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr); |
Chris Lattner | b91b31c | 2002-08-09 20:05:34 +0000 | [diff] [blame] | 360 | for (unsigned i = mvec.size(); i != 0; --i) |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 361 | { |
Chris Lattner | b91b31c | 2002-08-09 20:05:34 +0000 | [diff] [blame] | 362 | vector<MachineInstr*> loadConstVec = |
| 363 | FixConstantOperandsForInstr(vmInstr, mvec[i-1], Target); |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 364 | |
Chris Lattner | b91b31c | 2002-08-09 20:05:34 +0000 | [diff] [blame] | 365 | mvec.insert(mvec.begin()+i-1, loadConstVec.begin(), loadConstVec.end()); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 366 | } |
Chris Lattner | 2e1749b | 2002-07-30 03:57:36 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | |
| 370 | |
| 371 | //===----------------------------------------------------------------------===// |
| 372 | // createInstructionSelectionPass - Public entrypoint for instruction selection |
| 373 | // and this file as a whole... |
| 374 | // |
| 375 | Pass *createInstructionSelectionPass(TargetMachine &T) { |
| 376 | return new InstructionSelection(T); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 377 | } |
| 378 | |