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