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/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"
19 // MappingInfo - This method collects mapping info
20 // for the mapping from LLVM to machine code.
23 class getMappingInfoForFunction : public FunctionPass {
26 getMappingInfoForFunction(std::ostream &out) : Out(out){}
27 const char* getPassName() const{return "Sparc MappingInformation";}
28 bool runOnFunction(Function &FI);
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
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,
42 void getMappingInfoForFunction::writeEpilogue(const char *area,
45 unsigned writeNumber(unsigned X);
51 Pass *MappingInfoForFunction(std::ostream &out){
52 return (new getMappingInfoForFunction(out));
55 //function definitions :
56 //create and output maps to the .s file
57 bool getMappingInfoForFunction::runOnFunction(Function &FI) {
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)];
66 //now print out the maps
67 writePrologue("BB TO MI MAP", "BBMIMap", FunctionNo);
69 writeEpilogue("BB TO MI MAP", "BBMIMap", FunctionNo);
71 writePrologue("LLVM I TO MI MAP", "LMIMap", FunctionNo);
73 writeEpilogue("LLVM I TO MI MAP", "LMIMap", FunctionNo);
77 void getMappingInfoForFunction::writePrologue(const char *area,
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";
89 void getMappingInfoForFunction::writeEpilogue(const char *area,
92 Out << ".end_" << label << FunctionNo << ":\n";
93 Out << "\t.size " << label << FunctionNo << ", .end_"
94 << label << FunctionNo << "-" << label
95 << FunctionNo << "\n\n\n\n";
98 //write out information as .byte directives
99 unsigned getMappingInfoForFunction::writeNumber(unsigned X) {
102 unsigned tmp = X & 127;
105 Out << "\t.byte " << tmp << "\n";
111 //Assign a number to each Function
112 bool getMappingInfoForFunction::doInitialization(Module &M){
114 for (Module::iterator FI = M.begin(), FE = M.end();
116 //dont count F with 0 BBs
117 if(FI->isExternal()) continue;
124 //Assign a Number to each BB
125 void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI){
127 for (Function::iterator BI = FI.begin(), BE = FI.end();
129 MachineCodeForBasicBlock &miBB = MachineCodeForBasicBlock::get(BI);
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();
139 MachineCodeForBasicBlock &miBB = MachineCodeForBasicBlock::get(BI);
141 for(MachineCodeForBasicBlock::iterator miI=miBB.begin(), miE=miBB.end();
142 miI!=miE; ++miI, ++j){
148 //BBtoMImap: contains F#, BB#,
149 // MI#[wrt beginning of F], #MI in BB
150 void getMappingInfoForFunction::writeBBToMImap(Function &FI){
152 for (Function::iterator BI = FI.begin(),
153 BE = FI.end(); BI != BE; ++BI, ++bb){
154 MachineCodeForBasicBlock &miBB = MachineCodeForBasicBlock::get(BI);
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());
164 //LLVMtoMImap: contains F#, BB#, LLVM#,
165 // MIs[wrt to beginning of BB]
166 void getMappingInfoForFunction::writeLLVMToMImap(Function &FI){
169 for (Function::iterator BI = FI.begin(), BE = FI.end();
170 BI != BE; ++BI, ++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);
181 //do for each corr. MI
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]);