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 |
| 37 | |
| 38 | bool doInitialization(Module &M); |
| 39 | void create_BB_to_MInumber_Key(Function &FI); |
| 40 | void create_MI_to_number_Key(Function &FI); |
| 41 | void writeBBToMImap(Function &FI); |
| 42 | void writeLLVMToMImap(Function &FI); |
| 43 | void getMappingInfoForFunction::writePrologue(const char * area, |
| 44 | const char *label, |
| 45 | unsigned FunctionNo); |
| 46 | void getMappingInfoForFunction::writeEpilogue(const char *area, |
| 47 | const char *label, |
| 48 | unsigned FunctionNo); |
| 49 | unsigned writeNumber(unsigned X); |
| 50 | }; |
| 51 | } |
| 52 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 53 | /// MappingInfoForFunction -- Static factory method: returns a new |
| 54 | /// getMappingInfoForFunction Pass object. |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 55 | Pass *MappingInfoForFunction(std::ostream &out){ |
| 56 | return (new getMappingInfoForFunction(out)); |
| 57 | } |
| 58 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 59 | /// runOnFunction -- Builds up the maps for the given function and then |
| 60 | /// writes them out as assembly code to the current output stream Out. |
| 61 | /// This is an entry point to the pass, called by the PassManager. |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 62 | bool getMappingInfoForFunction::runOnFunction(Function &FI) { |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 63 | // First we build up the maps. |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 64 | create_BB_to_MInumber_Key(FI); |
| 65 | create_MI_to_number_Key(FI); |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 66 | unsigned FunctionNo = Fkey[&FI]; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 67 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 68 | // Now, print out the maps. |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 69 | writePrologue("BB TO MI MAP", "BBMIMap", FunctionNo); |
| 70 | writeBBToMImap(FI); |
| 71 | writeEpilogue("BB TO MI MAP", "BBMIMap", FunctionNo); |
| 72 | |
| 73 | writePrologue("LLVM I TO MI MAP", "LMIMap", FunctionNo); |
| 74 | writeLLVMToMImap(FI); |
| 75 | writeEpilogue("LLVM I TO MI MAP", "LMIMap", FunctionNo); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | void getMappingInfoForFunction::writePrologue(const char *area, |
| 80 | const char *label, |
| 81 | unsigned FunctionNo){ |
| 82 | Out << "!" << area << "\n"; |
| 83 | Out << "\t.section \".rodata\"\n\t.align 8\n"; |
| 84 | Out << "\t.global " << label << FunctionNo << "\n"; |
| 85 | Out << "\t.type " << label << FunctionNo << ",#object\n"; |
| 86 | Out << label << FunctionNo << ":\n"; |
| 87 | Out << "\t.word .end_" << label << FunctionNo << "-" |
| 88 | << label << FunctionNo << "\n"; |
| 89 | } |
| 90 | |
| 91 | void getMappingInfoForFunction::writeEpilogue(const char *area, |
| 92 | const char *label, |
| 93 | unsigned FunctionNo){ |
| 94 | Out << ".end_" << label << FunctionNo << ":\n"; |
| 95 | Out << "\t.size " << label << FunctionNo << ", .end_" |
| 96 | << label << FunctionNo << "-" << label |
| 97 | << FunctionNo << "\n\n\n\n"; |
| 98 | } |
| 99 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 100 | /// writeNumber -- Write out the number X as a sequence of .byte |
| 101 | /// directives to the current output stream Out. |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 102 | unsigned getMappingInfoForFunction::writeNumber(unsigned X) { |
| 103 | unsigned i=0; |
| 104 | do { |
| 105 | unsigned tmp = X & 127; |
| 106 | X >>= 7; |
| 107 | if (X) tmp |= 128; |
| 108 | Out << "\t.byte " << tmp << "\n"; |
| 109 | ++i; |
| 110 | } while(X); |
| 111 | return i; |
| 112 | } |
| 113 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 114 | /// doInitialization -- Assign a number to each Function, as follows: |
| 115 | /// Functions are numbered starting at 0 at the begin() of each Module. |
| 116 | /// Functions which are External (and thus have 0 basic blocks) are not |
| 117 | /// inserted into the maps, and are not assigned a number. The side-effect |
| 118 | /// of this method is to fill in Fkey to contain the mapping from Functions |
| 119 | /// to numbers. (This method is called automatically by the PassManager.) |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 120 | bool getMappingInfoForFunction::doInitialization(Module &M) { |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 121 | unsigned i = 0; |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 122 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { |
| 123 | if (FI->isExternal()) continue; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 124 | Fkey[FI] = i; |
| 125 | ++i; |
| 126 | } |
| 127 | return false; |
| 128 | } |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 129 | |
| 130 | /// create_BB_to_MInumber_Key -- Assign a number to each MachineBasicBlock |
| 131 | /// in the given Function, as follows: Numbering starts at zero in each |
| 132 | /// Function. MachineBasicBlocks are numbered from begin() to end() |
| 133 | /// in the Function's corresponding MachineFunction. Each successive |
| 134 | /// MachineBasicBlock increments the numbering by the number of instructions |
| 135 | /// it contains. The side-effect of this method is to fill in the instance |
| 136 | /// variable BBkey with the mapping of MachineBasicBlocks to numbers. BBkey |
| 137 | /// is keyed on MachineInstrs, so each MachineBasicBlock is represented |
| 138 | /// therein by its first MachineInstr. |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 139 | void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI) { |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 140 | unsigned i = 0; |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 141 | MachineFunction &MF = MachineFunction::get(&FI); |
| 142 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); |
| 143 | BI != BE; ++BI) { |
| 144 | MachineBasicBlock &miBB = *BI; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 145 | BBkey[miBB[0]] = i; |
| 146 | i = i+(miBB.size()); |
| 147 | } |
| 148 | } |
| 149 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 150 | /// create_MI_to_number_Key -- Assign a number to each MachineInstr |
| 151 | /// in the given Function with respect to its enclosing MachineBasicBlock, as |
| 152 | /// follows: Numberings start at 0 in each MachineBasicBlock. MachineInstrs |
| 153 | /// are numbered from begin() to end() in their MachineBasicBlock. Each |
| 154 | /// MachineInstr is numbered, then the numbering is incremented by 1. The |
| 155 | /// side-effect of this method is to fill in the instance variable MIkey |
| 156 | /// with the mapping from MachineInstrs to numbers. |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 157 | void getMappingInfoForFunction::create_MI_to_number_Key(Function &FI) { |
| 158 | MachineFunction &MF = MachineFunction::get(&FI); |
| 159 | for (MachineFunction::iterator BI=MF.begin(), BE=MF.end(); BI != BE; ++BI) { |
| 160 | MachineBasicBlock &miBB = *BI; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 161 | unsigned j = 0; |
Chris Lattner | 55291ea | 2002-10-28 01:41:47 +0000 | [diff] [blame] | 162 | for(MachineBasicBlock::iterator miI=miBB.begin(), miE=miBB.end(); |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 163 | miI!=miE; ++miI, ++j) { |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 164 | MIkey[*miI]=j; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 169 | /// writeBBToMImap -- Output the BB TO MI MAP for the given function as |
| 170 | /// assembly code to the current output stream. The BB TO MI MAP consists |
| 171 | /// of a three-element tuple for each MachineBasicBlock in a function: |
| 172 | /// first, the index of the MachineBasicBlock in the function; second, |
| 173 | /// the number of the MachineBasicBlock in the function as computed by |
| 174 | /// create_BB_to_MInumber_Key; and third, the number of MachineInstrs in |
| 175 | /// the MachineBasicBlock. |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 176 | void getMappingInfoForFunction::writeBBToMImap(Function &FI){ |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 177 | unsigned bb = 0; |
| 178 | MachineFunction &MF = MachineFunction::get(&FI); |
| 179 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); |
| 180 | BI != BE; ++BI, ++bb) { |
| 181 | MachineBasicBlock &miBB = *BI; |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 182 | writeNumber(bb); |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 183 | writeNumber(BBkey[miBB[0]]); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 184 | writeNumber(miBB.size()); |
| 185 | } |
| 186 | } |
| 187 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 188 | /// writeLLVMToMImap -- Output the LLVM I TO MI MAP for the given function |
| 189 | /// as assembly code to the current output stream. The LLVM I TO MI MAP |
| 190 | /// consists of a set of information for each BasicBlock in a Function, |
| 191 | /// ordered from begin() to end(). The information for a BasicBlock consists |
| 192 | /// of 1) its (0-based) index in the Function, 2) the number of LLVM |
| 193 | /// Instructions it contains, and 3) information for each Instruction, in |
| 194 | /// sequence from the begin() to the end() of the BasicBlock. The information |
| 195 | /// for an Instruction consists of 1) its (0-based) index in the BasicBlock, |
| 196 | /// 2) the number of MachineInstrs that correspond to that Instruction |
| 197 | /// (as reported by MachineCodeForInstruction), and 3) the MachineInstr |
| 198 | /// number calculated by create_MI_to_number_Key, for each of the |
| 199 | /// MachineInstrs that correspond to that Instruction. |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 200 | void getMappingInfoForFunction::writeLLVMToMImap(Function &FI) { |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 201 | |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 202 | unsigned bb = 0; |
| 203 | for (Function::iterator BI = FI.begin(), BE = FI.end(); |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 204 | BI != BE; ++BI, ++bb) { |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 205 | unsigned li = 0; |
| 206 | writeNumber(bb); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 207 | writeNumber(BI->size()); |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 208 | for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; |
| 209 | ++II, ++li) { |
| 210 | MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 211 | writeNumber(li); |
Anand Shukla | b379470 | 2002-09-17 20:24:46 +0000 | [diff] [blame] | 212 | writeNumber(miI.size()); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 213 | for (MachineCodeForInstruction::iterator miII = miI.begin(), |
Brian Gaeke | e14ccaf | 2003-06-03 07:56:05 +0000 | [diff] [blame^] | 214 | miIE = miI.end(); miII != miIE; ++miII) { |
| 215 | writeNumber(MIkey[*miII]); |
Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |