1 //===- MappingInfo.cpp - create LLVM info and output to .s file ---------===//
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
7 //===--------------------------------------------------------------------===//
9 #include "llvm/CodeGen/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"
20 // MappingInfo - This method collects mapping info
21 // for the mapping from LLVM to machine code.
24 class getMappingInfoForFunction : public Pass {
27 std::map<const Function*, int> Fkey; //key of function to num
28 std::map<const MachineInstr*, int> BBkey; //key basic block to num
29 std::map<const MachineInstr*, int> MIkey; //key machine instruction to num
30 vector<vector<int> > BBmap;
31 vector<vector<int> > MImap;
33 void createFunctionKey(Module &M);
34 void createBasicBlockKey(Module &M);
35 void createMachineInstructionKey(Module &M);
36 void createBBToMImap(Module &M);
37 void createLLVMToMImap(Module &M);
38 void writeNumber(int X);
41 getMappingInfoForFunction(std::ostream &out) : Out(out){}
43 const char* getPassName() const {
44 return "Sparc CollectMappingInfoForInstruction";
53 Pass *MappingInfoForFunction(std::ostream &out){
54 return (new getMappingInfoForFunction(out));
57 //function definitions :
58 //create and output maps to the .s file
59 bool getMappingInfoForFunction::run(Module &M) {
62 //map for Function to Function number
65 //map for BB to LLVM instruction number
66 createBasicBlockKey(M);
68 //map from Machine Instruction to Machine Instruction number
69 createMachineInstructionKey(M);
71 //map of Basic Block to first Machine Instruction and number
72 // of instructions go thro each function
75 //map of LLVM Instruction to Machine Instruction
79 // Write map to the sparc assembly stream
80 // Start by writing out the basic block to first and last
81 // machine instruction map to the .s file
82 Out << "\n\n!BB TO MI MAP\n";
83 Out << "\t.section \".data\"\n\t.align 8\n";
84 Out << "\t.global BBMIMap\n";
86 //add stream object here that will contain info about the map
87 //add object to write this out to the .s file
88 for (vector<vector<int> >::iterator BBmapI =
89 BBmap.begin(), BBmapE = BBmap.end(); BBmapI != BBmapE;
91 writeNumber((*BBmapI)[0]);
92 writeNumber((*BBmapI)[1]);
93 writeNumber((*BBmapI)[2]);
94 writeNumber((*BBmapI)[3]);
97 Out << "\t.type BBMIMap,#object\n";
98 Out << "\t.size BBMIMap,"<<BBmap.size() << "\n";
101 Out <<"\n\n!LLVM BB MAP Length\n\t.section \".bbdata";
102 Out << "\",#alloc,#write\n\t.global BBMIMap_length\n\t.align 4\n\t.type BBMIMap_length,";
103 Out <<"#object\n\t.size BBMIMap_length,4\nBBMIMap_length:\n\t.word "
104 << ((BBmap.size())*4)<<"\n\n\n\n";
107 //Now write out the LLVM instruction to the corresponding
108 //machine instruction map
109 Out << "!LLVM I TO MI MAP\n";
110 Out << "\t.section\".data\"\n\t.align 8\n";
111 Out << "\t.global LMIMap\n";
113 //add stream object here that will contain info about the map
114 //add object to write this out to the .s file
115 for (vector<vector<int> >::iterator MImapI =
116 MImap.begin(), MImapE = MImap.end(); MImapI != MImapE;
118 writeNumber((*MImapI)[0]);
119 writeNumber((*MImapI)[1]);
120 writeNumber((*MImapI)[2]);
121 writeNumber((*MImapI)[3]);
123 Out << "\t.type LMIMap,#object\n";
124 Out << "\t.size LMIMap,"<<MImap.size() << "\n";
126 Out <<"\n\n!LLVM MI MAP Length\n\t.section\".llvmdata";
127 Out << "\",#alloc,#write\n\t.global LMIMap_length\n\t.align 4\n\t.type LMIMap_length,";
128 Out <<"#object\n\t.size LMIMap_length,4\nLMIMap_length:\n\t.word "
129 << ((MImap.size())*4)<<"\n\n\n\n";
134 //write out information as .byte directives
135 void getMappingInfoForFunction::writeNumber(int X) {
140 Out << "\t.byte " << tmp << "\n";
144 //Assign a number to each Function
145 void getMappingInfoForFunction::createFunctionKey(Module &M){
148 for (Module::iterator FI = M.begin(), FE = M.end();
150 //dont count F with 0 BBs
151 if(FI->isExternal()) continue;
157 //Assign a Number to each BB
158 void getMappingInfoForFunction::createBasicBlockKey(Module &M){
160 for (Module::iterator FI = M.begin(), FE = M.end();
163 if(FI->isExternal()) continue;
164 for (Function::iterator BI = FI->begin(), BE = FI->end();
166 MachineCodeForBasicBlock &miBB = MachineCodeForBasicBlock::get(BI);
173 //Assign a number to each MI wrt beginning of the BB
174 void getMappingInfoForFunction::createMachineInstructionKey(Module &M){
175 for (Module::iterator FI = M.begin(), FE = M.end();
177 if(FI->isExternal()) continue;
178 for (Function::iterator BI=FI->begin(), BE=FI->end();
180 MachineCodeForBasicBlock &miBB = MachineCodeForBasicBlock::get(BI);
182 for (MachineCodeForBasicBlock::iterator miI = miBB.begin(),
183 miE = miBB.end(); miI != miE; ++miI, ++j){
190 //BBtoMImap: contains F#, BB#,
191 // MI#[wrt beginning of F], #MI in BB
192 void getMappingInfoForFunction::createBBToMImap(Module &M){
194 for (Module::iterator FI = M.begin(), FE = M.end();
196 if(FI->isExternal())continue;
198 for (Function::iterator BI = FI->begin(),
199 BE = FI->end(); BI != BE; ++BI){
200 MachineCodeForBasicBlock &miBB = MachineCodeForBasicBlock::get(BI);
201 //add record into the map
202 BBmap.push_back(vector<int>());
203 vector<int> &oneBB = BBmap.back();
207 oneBB.push_back(Fkey[FI]);
209 oneBB.push_back( i );
210 //add the MI#[wrt the beginning of F]
211 oneBB.push_back( BBkey[ miBB[0] ]);
213 oneBB.push_back(miBB.size());
220 //LLVMtoMImap: contains F#, BB#, LLVM#,
221 // MIs[wrt to beginning of BB]
222 void getMappingInfoForFunction::createLLVMToMImap(Module &M){
224 for (Module::iterator FI = M.begin(), FE = M.end();
226 if(FI->isExternal()) continue;
228 for (Function::iterator BI = FI->begin(), BE = FI->end();
229 BI != BE; ++BI, ++i){
231 for (BasicBlock::iterator II = BI->begin(),
232 IE = BI->end(); II != IE; ++II, ++j){
233 MachineCodeForInstruction& miI =
234 MachineCodeForInstruction::get(II);
235 //do for each corr. MI
236 for (MachineCodeForInstruction::iterator miII = miI.begin(),
237 miIE = miI.end(); miII != miIE; ++miII){
239 MImap.push_back(vector<int>());
240 vector<int> &oneMI = MImap.back();
244 oneMI.push_back(Fkey[FI]);
249 //add MI#[wrt to beginning of BB]
250 oneMI.push_back(MIkey[*miII]);