Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 1 | //===- MappingInfo.cpp - create LLVM info and output to .s file ---------===// |
| 2 | // |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 3 | // This file contains a FunctionPass called MappingInfo, |
| 4 | // which creates two maps: one between LLVM Instructions and MachineInstrs |
| 5 | // (the "LLVM I TO MI MAP"), and another between MachineBasicBlocks and |
| 6 | // MachineInstrs (the "BB TO MI MAP"). |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 7 | // |
| 8 | // As a side effect, it outputs this information as .byte directives to |
| 9 | // the assembly file. The output is designed to survive the SPARC assembler, |
| 10 | // in order that the Reoptimizer may read it in from memory later when the |
| 11 | // binary is loaded. Therefore, it may contain some hidden SPARC-architecture |
| 12 | // dependencies. Currently this question is purely theoretical as the |
| 13 | // Reoptimizer works only on the SPARC. |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 14 | // |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 15 | // The LLVM I TO MI MAP consists of a set of information for each |
| 16 | // BasicBlock in a Function, ordered from begin() to end(). The information |
| 17 | // for a BasicBlock consists of |
| 18 | // 1) its (0-based) index in the Function, |
| 19 | // 2) the number of LLVM Instructions it contains, and |
| 20 | // 3) information for each Instruction, in sequence from the begin() |
| 21 | // to the end() of the BasicBlock. The information for an Instruction |
| 22 | // consists of |
| 23 | // 1) its (0-based) index in the BasicBlock, |
| 24 | // 2) the number of MachineInstrs that correspond to that Instruction |
| 25 | // (as reported by MachineCodeForInstruction), and |
| 26 | // 3) the MachineInstr number calculated by create_MI_to_number_Key, |
| 27 | // for each of the MachineInstrs that correspond to that Instruction. |
| 28 | // |
| 29 | // The BB TO MI MAP consists of a three-element tuple for each |
| 30 | // MachineBasicBlock in a function, ordered from begin() to end() of |
| 31 | // its MachineFunction: first, the index of the MachineBasicBlock in the |
| 32 | // function; second, the number of the MachineBasicBlock in the function |
| 33 | // as computed by create_BB_to_MInumber_Key; and third, the number of |
| 34 | // MachineInstrs in the MachineBasicBlock. |
| 35 | // |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 36 | //===--------------------------------------------------------------------===// |
| 37 | |
| 38 | #include "llvm/Reoptimizer/Mapping/MappingInfo.h" |
| 39 | #include "llvm/Pass.h" |
| 40 | #include "llvm/Module.h" |
| 41 | #include "llvm/CodeGen/MachineInstr.h" |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/MachineFunction.h" |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 44 | |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 45 | namespace { |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 46 | class MappingInfoCollector : public FunctionPass { |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 47 | std::ostream &Out; |
| 48 | public: |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 49 | MappingInfoCollector(std::ostream &out) : Out(out){} |
| 50 | const char *getPassName () const { return "Instr. Mapping Info Collector"; } |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 51 | bool runOnFunction(Function &FI); |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 52 | typedef std::map<const MachineInstr*, unsigned> InstructionKey; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 53 | private: |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 54 | MappingInfo *currentOutputMap; |
| 55 | std::map<Function *, unsigned> Fkey; // Function # for all functions. |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 56 | bool doInitialization(Module &M); |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 57 | void create_BB_to_MInumber_Key(Function &FI, InstructionKey &key); |
| 58 | void create_MI_to_number_Key(Function &FI, InstructionKey &key); |
| 59 | void buildBBMIMap (Function &FI, MappingInfo &Map); |
| 60 | void buildLMIMap (Function &FI, MappingInfo &Map); |
| 61 | void writeNumber(unsigned X); |
| 62 | void selectOutputMap (MappingInfo &m) { currentOutputMap = &m; } |
| 63 | void outByte (unsigned char b) { currentOutputMap->outByte (b); } |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 64 | }; |
| 65 | } |
| 66 | |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 67 | /// getMappingInfoCollector -- Static factory method: returns a new |
| 68 | /// MappingInfoCollector Pass object, which uses OUT as its |
Brian Gaeke | fc97c8b | 2003-06-03 19:30:15 +0000 | [diff] [blame] | 69 | /// output stream for assembly output. |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 70 | Pass *getMappingInfoCollector(std::ostream &out){ |
| 71 | return (new MappingInfoCollector(out)); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Brian Gaeke | fc97c8b | 2003-06-03 19:30:15 +0000 | [diff] [blame] | 74 | /// runOnFunction -- Builds up the maps for the given function FI and then |
| 75 | /// writes them out as assembly code to the current output stream OUT. |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 76 | /// This is an entry point to the pass, called by the PassManager. |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 77 | bool MappingInfoCollector::runOnFunction(Function &FI) { |
Brian Gaeke | fc97c8b | 2003-06-03 19:30:15 +0000 | [diff] [blame] | 78 | unsigned num = Fkey[&FI]; // Function number for the current function. |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 79 | |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 80 | // Create objects to hold the maps. |
| 81 | MappingInfo LMIMap ("LLVM I TO MI MAP", "LMIMap", num); |
| 82 | MappingInfo BBMIMap ("BB TO MI MAP", "BBMIMap", num); |
| 83 | |
| 84 | // Now, build the maps. |
| 85 | buildLMIMap (FI, LMIMap); |
| 86 | buildBBMIMap (FI, BBMIMap); |
| 87 | |
Brian Gaeke | fc97c8b | 2003-06-03 19:30:15 +0000 | [diff] [blame] | 88 | // Now, write out the maps. |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 89 | LMIMap.dumpAssembly (Out); |
| 90 | BBMIMap.dumpAssembly (Out); |
Brian Gaeke | fc97c8b | 2003-06-03 19:30:15 +0000 | [diff] [blame] | 91 | |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 95 | /// writeNumber -- Write out the number X as a sequence of .byte |
Brian Gaeke | fc97c8b | 2003-06-03 19:30:15 +0000 | [diff] [blame] | 96 | /// directives to the current output stream Out. This method performs a |
| 97 | /// run-length encoding of the unsigned integers X that are output. |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 98 | void MappingInfoCollector::writeNumber(unsigned X) { |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 99 | unsigned i=0; |
| 100 | do { |
| 101 | unsigned tmp = X & 127; |
| 102 | X >>= 7; |
| 103 | if (X) tmp |= 128; |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 104 | outByte (tmp); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 105 | ++i; |
| 106 | } while(X); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 109 | /// doInitialization -- Assign a number to each Function, as follows: |
| 110 | /// Functions are numbered starting at 0 at the begin() of each Module. |
| 111 | /// Functions which are External (and thus have 0 basic blocks) are not |
| 112 | /// inserted into the maps, and are not assigned a number. The side-effect |
| 113 | /// of this method is to fill in Fkey to contain the mapping from Functions |
| 114 | /// to numbers. (This method is called automatically by the PassManager.) |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 115 | bool MappingInfoCollector::doInitialization(Module &M) { |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 116 | unsigned i = 0; |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 117 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { |
| 118 | if (FI->isExternal()) continue; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 119 | Fkey[FI] = i; |
| 120 | ++i; |
| 121 | } |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 122 | return false; // Success. |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 123 | } |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 124 | |
| 125 | /// create_BB_to_MInumber_Key -- Assign a number to each MachineBasicBlock |
| 126 | /// in the given Function, as follows: Numbering starts at zero in each |
| 127 | /// Function. MachineBasicBlocks are numbered from begin() to end() |
| 128 | /// in the Function's corresponding MachineFunction. Each successive |
| 129 | /// MachineBasicBlock increments the numbering by the number of instructions |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 130 | /// it contains. The side-effect of this method is to fill in the paramete |
| 131 | /// KEY with the mapping of MachineBasicBlocks to numbers. KEY |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 132 | /// is keyed on MachineInstrs, so each MachineBasicBlock is represented |
| 133 | /// therein by its first MachineInstr. |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 134 | void MappingInfoCollector::create_BB_to_MInumber_Key(Function &FI, |
| 135 | InstructionKey &key) { |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 136 | unsigned i = 0; |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 137 | MachineFunction &MF = MachineFunction::get(&FI); |
| 138 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); |
| 139 | BI != BE; ++BI) { |
| 140 | MachineBasicBlock &miBB = *BI; |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 141 | key[miBB[0]] = i; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 142 | i = i+(miBB.size()); |
| 143 | } |
| 144 | } |
| 145 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 146 | /// create_MI_to_number_Key -- Assign a number to each MachineInstr |
| 147 | /// in the given Function with respect to its enclosing MachineBasicBlock, as |
| 148 | /// follows: Numberings start at 0 in each MachineBasicBlock. MachineInstrs |
| 149 | /// are numbered from begin() to end() in their MachineBasicBlock. Each |
| 150 | /// MachineInstr is numbered, then the numbering is incremented by 1. The |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 151 | /// side-effect of this method is to fill in the parameter KEY |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 152 | /// with the mapping from MachineInstrs to numbers. |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 153 | void MappingInfoCollector::create_MI_to_number_Key(Function &FI, |
| 154 | InstructionKey &key) { |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 155 | MachineFunction &MF = MachineFunction::get(&FI); |
| 156 | for (MachineFunction::iterator BI=MF.begin(), BE=MF.end(); BI != BE; ++BI) { |
| 157 | MachineBasicBlock &miBB = *BI; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 158 | unsigned j = 0; |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 159 | for(MachineBasicBlock::iterator miI = miBB.begin(), miE = miBB.end(); |
| 160 | miI != miE; ++miI, ++j) { |
| 161 | key[*miI] = j; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 166 | /// buildBBMIMap -- Build the BB TO MI MAP for the function FI, |
| 167 | /// and save it into the parameter MAP. |
| 168 | void MappingInfoCollector::buildBBMIMap(Function &FI, MappingInfo &Map) { |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 169 | unsigned bb = 0; |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 170 | |
| 171 | // First build temporary table used to write out the map. |
| 172 | InstructionKey BBkey; |
| 173 | create_BB_to_MInumber_Key(FI, BBkey); |
| 174 | |
| 175 | selectOutputMap (Map); |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 176 | MachineFunction &MF = MachineFunction::get(&FI); |
| 177 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); |
| 178 | BI != BE; ++BI, ++bb) { |
| 179 | MachineBasicBlock &miBB = *BI; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 180 | writeNumber(bb); |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 181 | writeNumber(BBkey[miBB[0]]); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 182 | writeNumber(miBB.size()); |
| 183 | } |
| 184 | } |
| 185 | |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 186 | /// buildLMIMap -- Build the LLVM I TO MI MAP for the function FI, |
| 187 | /// and save it into the parameter MAP. |
| 188 | void MappingInfoCollector::buildLMIMap(Function &FI, MappingInfo &Map) { |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 189 | unsigned bb = 0; |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 190 | // First build temporary table used to write out the map. |
| 191 | InstructionKey MIkey; |
| 192 | create_MI_to_number_Key(FI, MIkey); |
| 193 | |
| 194 | selectOutputMap (Map); |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 195 | for (Function::iterator BI = FI.begin(), BE = FI.end(); |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 196 | BI != BE; ++BI, ++bb) { |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 197 | unsigned li = 0; |
| 198 | writeNumber(bb); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 199 | writeNumber(BI->size()); |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 200 | for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; |
| 201 | ++II, ++li) { |
| 202 | MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 203 | writeNumber(li); |
Anand Shukla | b379470 | 2002-09-17 20:24:46 +0000 | [diff] [blame] | 204 | writeNumber(miI.size()); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 205 | for (MachineCodeForInstruction::iterator miII = miI.begin(), |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame] | 206 | miIE = miI.end(); miII != miIE; ++miII) { |
| 207 | writeNumber(MIkey[*miII]); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
Brian Gaeke | aeab1e1 | 2003-06-04 22:07:12 +0000 | [diff] [blame^] | 211 | } |
| 212 | |
| 213 | void MappingInfo::byteVector::dumpAssembly (std::ostream &Out) { |
| 214 | for (iterator i = begin (), e = end (); i != e; ++i) |
| 215 | Out << ".byte " << (int)*i << "\n"; |
| 216 | } |
| 217 | |
| 218 | void MappingInfo::dumpAssembly (std::ostream &Out) { |
| 219 | // Prologue: |
| 220 | // Output a comment describing the map. |
| 221 | Out << "!" << comment << "\n"; |
| 222 | // Switch the current section to .rodata in the assembly output: |
| 223 | Out << "\t.section \".rodata\"\n\t.align 8\n"; |
| 224 | // Output a global symbol naming the map: |
| 225 | Out << "\t.global " << symbolPrefix << functionNumber << "\n"; |
| 226 | Out << "\t.type " << symbolPrefix << functionNumber << ",#object\n"; |
| 227 | Out << symbolPrefix << functionNumber << ":\n"; |
| 228 | // Output a word containing the length of the map: |
| 229 | Out << "\t.word .end_" << symbolPrefix << functionNumber << "-" |
| 230 | << symbolPrefix << functionNumber << "\n"; |
| 231 | |
| 232 | // Output the map data itself: |
| 233 | bytes.dumpAssembly (Out); |
| 234 | |
| 235 | // Epilogue: |
| 236 | // Output a local symbol marking the end of the map: |
| 237 | Out << ".end_" << symbolPrefix << functionNumber << ":\n"; |
| 238 | // Output size directive giving the size of the map: |
| 239 | Out << "\t.size " << symbolPrefix << functionNumber << ", .end_" |
| 240 | << symbolPrefix << functionNumber << "-" << symbolPrefix |
| 241 | << functionNumber << "\n\n"; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 242 | } |