Anand Shukla | b85d265 | 2002-08-27 22:47:33 +0000 | [diff] [blame^] | 1 | //===- MappingInfo.cpp - create LLVM info and output to .s file ---------===// |
| 2 | // |
| 3 | // Create Map from LLVM BB and Instructions and Machine Instructions |
| 4 | // and output the information as .byte directives to the .s file |
| 5 | // Currently Sparc specific but will be extended for others later |
| 6 | // |
| 7 | //===--------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Reoptimizer/Mapping/MappingInfo.h" |
| 10 | #include "llvm/Pass.h" |
| 11 | #include "llvm/Module.h" |
| 12 | #include "llvm/CodeGen/MachineInstr.h" |
| 13 | #include "llvm/CodeGen/MachineCodeForBasicBlock.h" |
| 14 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
| 15 | #include <map> |
| 16 | using std::vector; |
| 17 | |
| 18 | |
| 19 | // MappingInfo - This method collects mapping info |
| 20 | // for the mapping from LLVM to machine code. |
| 21 | // |
| 22 | namespace { |
| 23 | class getMappingInfoForFunction : public FunctionPass { |
| 24 | std::ostream &Out; |
| 25 | public: |
| 26 | getMappingInfoForFunction(std::ostream &out) : Out(out){} |
| 27 | const char* getPassName() const{return "Sparc MappingInformation";} |
| 28 | bool runOnFunction(Function &FI); |
| 29 | private: |
| 30 | std::map<const Function*, unsigned> Fkey; //key of F to num |
| 31 | std::map<const MachineInstr*, unsigned> BBkey; //key BB to num |
| 32 | std::map<const MachineInstr*, unsigned> MIkey; //key MI to num |
| 33 | |
| 34 | bool doInitialization(Module &M); |
| 35 | void create_BB_to_MInumber_Key(Function &FI); |
| 36 | void create_MI_to_number_Key(Function &FI); |
| 37 | void writeBBToMImap(Function &FI); |
| 38 | void writeLLVMToMImap(Function &FI); |
| 39 | void getMappingInfoForFunction::writePrologue(const char * area, |
| 40 | const char *label, |
| 41 | unsigned FunctionNo); |
| 42 | void getMappingInfoForFunction::writeEpilogue(const char *area, |
| 43 | const char *label, |
| 44 | unsigned FunctionNo); |
| 45 | unsigned writeNumber(unsigned X); |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | //pass definition |
| 51 | Pass *MappingInfoForFunction(std::ostream &out){ |
| 52 | return (new getMappingInfoForFunction(out)); |
| 53 | } |
| 54 | |
| 55 | //function definitions : |
| 56 | //create and output maps to the .s file |
| 57 | bool getMappingInfoForFunction::runOnFunction(Function &FI) { |
| 58 | |
| 59 | |
| 60 | //first create reference maps |
| 61 | //createFunctionKey(M); |
| 62 | create_BB_to_MInumber_Key(FI); |
| 63 | create_MI_to_number_Key(FI); |
| 64 | unsigned FunctionNo = Fkey[&(FI)]; |
| 65 | |
| 66 | //now print out the maps |
| 67 | writePrologue("BB TO MI MAP", "BBMIMap", FunctionNo); |
| 68 | writeBBToMImap(FI); |
| 69 | writeEpilogue("BB TO MI MAP", "BBMIMap", FunctionNo); |
| 70 | |
| 71 | writePrologue("LLVM I TO MI MAP", "LMIMap", FunctionNo); |
| 72 | writeLLVMToMImap(FI); |
| 73 | writeEpilogue("LLVM I TO MI MAP", "LMIMap", FunctionNo); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | void getMappingInfoForFunction::writePrologue(const char *area, |
| 78 | const char *label, |
| 79 | unsigned FunctionNo){ |
| 80 | Out << "!" << area << "\n"; |
| 81 | Out << "\t.section \".rodata\"\n\t.align 8\n"; |
| 82 | Out << "\t.global " << label << FunctionNo << "\n"; |
| 83 | Out << "\t.type " << label << FunctionNo << ",#object\n"; |
| 84 | Out << label << FunctionNo << ":\n"; |
| 85 | Out << "\t.word .end_" << label << FunctionNo << "-" |
| 86 | << label << FunctionNo << "\n"; |
| 87 | } |
| 88 | |
| 89 | void getMappingInfoForFunction::writeEpilogue(const char *area, |
| 90 | const char *label, |
| 91 | unsigned FunctionNo){ |
| 92 | Out << ".end_" << label << FunctionNo << ":\n"; |
| 93 | Out << "\t.size " << label << FunctionNo << ", .end_" |
| 94 | << label << FunctionNo << "-" << label |
| 95 | << FunctionNo << "\n\n\n\n"; |
| 96 | } |
| 97 | |
| 98 | //write out information as .byte directives |
| 99 | unsigned getMappingInfoForFunction::writeNumber(unsigned X) { |
| 100 | unsigned i=0; |
| 101 | do { |
| 102 | unsigned tmp = X & 127; |
| 103 | X >>= 7; |
| 104 | if (X) tmp |= 128; |
| 105 | Out << "\t.byte " << tmp << "\n"; |
| 106 | ++i; |
| 107 | } while(X); |
| 108 | return i; |
| 109 | } |
| 110 | |
| 111 | //Assign a number to each Function |
| 112 | bool getMappingInfoForFunction::doInitialization(Module &M){ |
| 113 | unsigned i = 0; |
| 114 | for (Module::iterator FI = M.begin(), FE = M.end(); |
| 115 | FI != FE; ++FI){ |
| 116 | //dont count F with 0 BBs |
| 117 | if(FI->isExternal()) continue; |
| 118 | Fkey[FI] = i; |
| 119 | ++i; |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | //Assign a Number to each BB |
| 125 | void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI){ |
| 126 | unsigned i = 0; |
| 127 | for (Function::iterator BI = FI.begin(), BE = FI.end(); |
| 128 | BI != BE; ++BI){ |
| 129 | MachineCodeForBasicBlock &miBB = MachineCodeForBasicBlock::get(BI); |
| 130 | BBkey[miBB[0]] = i; |
| 131 | i = i+(miBB.size()); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | //Assign a number to each MI wrt beginning of the BB |
| 136 | void getMappingInfoForFunction::create_MI_to_number_Key(Function &FI){ |
| 137 | for (Function::iterator BI=FI.begin(), BE=FI.end(); |
| 138 | BI != BE; ++BI){ |
| 139 | MachineCodeForBasicBlock &miBB = MachineCodeForBasicBlock::get(BI); |
| 140 | unsigned j = 0; |
| 141 | for(MachineCodeForBasicBlock::iterator miI=miBB.begin(), miE=miBB.end(); |
| 142 | miI!=miE; ++miI, ++j){ |
| 143 | MIkey[*miI]=j; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | //BBtoMImap: contains F#, BB#, |
| 149 | // MI#[wrt beginning of F], #MI in BB |
| 150 | void getMappingInfoForFunction::writeBBToMImap(Function &FI){ |
| 151 | unsigned bb=0; |
| 152 | for (Function::iterator BI = FI.begin(), |
| 153 | BE = FI.end(); BI != BE; ++BI, ++bb){ |
| 154 | MachineCodeForBasicBlock &miBB = MachineCodeForBasicBlock::get(BI); |
| 155 | writeNumber(bb); |
| 156 | //Out << " BB: "<<(void *)BI<<"\n"; |
| 157 | //for(int i=0; i<miBB.size(); ++i) |
| 158 | //Out<<*miBB[i]<<"\n"; |
| 159 | writeNumber( BBkey[ miBB[0] ]); |
| 160 | writeNumber(miBB.size()); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | //LLVMtoMImap: contains F#, BB#, LLVM#, |
| 165 | // MIs[wrt to beginning of BB] |
| 166 | void getMappingInfoForFunction::writeLLVMToMImap(Function &FI){ |
| 167 | |
| 168 | unsigned bb =0; |
| 169 | for (Function::iterator BI = FI.begin(), BE = FI.end(); |
| 170 | BI != BE; ++BI, ++bb){ |
| 171 | unsigned li = 0; |
| 172 | writeNumber(bb); |
| 173 | //Out << "BB: "<<(void *)BI<<"\n"; |
| 174 | writeNumber(BI->size()); |
| 175 | for (BasicBlock::iterator II = BI->begin(), |
| 176 | IE = BI->end(); II != IE; ++II, ++li){ |
| 177 | //Out << "I: "<<*II<<"\n"; |
| 178 | MachineCodeForInstruction& miI = |
| 179 | MachineCodeForInstruction::get(II); |
| 180 | |
| 181 | //do for each corr. MI |
| 182 | writeNumber(li); |
| 183 | writeNumber(miI.size()); |
| 184 | for (MachineCodeForInstruction::iterator miII = miI.begin(), |
| 185 | miIE = miI.end(); miII != miIE; ++miII){ |
| 186 | //Out << "MI: "<<**miII<<"\n"; |
| 187 | writeNumber(MIkey[*miII]); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |