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