blob: 757247874b4e201c17e482f62e312cadc189a619 [file] [log] [blame]
Anand Shuklab85d2652002-08-27 22:47:33 +00001//===- 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"
Misha Brukmanb7551ef2002-10-28 20:00:31 +000013#include "llvm/CodeGen/MachineFunction.h"
Anand Shuklab85d2652002-08-27 22:47:33 +000014#include "llvm/CodeGen/MachineCodeForInstruction.h"
15#include <map>
16using std::vector;
17
18
19// MappingInfo - This method collects mapping info
20// for the mapping from LLVM to machine code.
21//
22namespace {
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
51Pass *MappingInfoForFunction(std::ostream &out){
52 return (new getMappingInfoForFunction(out));
53}
54
55//function definitions :
56//create and output maps to the .s file
57bool 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
77void 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
89void 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
99unsigned 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
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000112bool getMappingInfoForFunction::doInitialization(Module &M) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000113 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
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000125void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000126 unsigned i = 0;
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000127 MachineFunction &MF = MachineFunction::get(&FI);
128 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
129 BI != BE; ++BI) {
130 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000131 BBkey[miBB[0]] = i;
132 i = i+(miBB.size());
133 }
134}
135
136//Assign a number to each MI wrt beginning of the BB
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000137void getMappingInfoForFunction::create_MI_to_number_Key(Function &FI) {
138 MachineFunction &MF = MachineFunction::get(&FI);
139 for (MachineFunction::iterator BI=MF.begin(), BE=MF.end(); BI != BE; ++BI) {
140 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000141 unsigned j = 0;
Chris Lattner55291ea2002-10-28 01:41:47 +0000142 for(MachineBasicBlock::iterator miI=miBB.begin(), miE=miBB.end();
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000143 miI!=miE; ++miI, ++j) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000144 MIkey[*miI]=j;
145 }
146 }
147}
148
149//BBtoMImap: contains F#, BB#,
150// MI#[wrt beginning of F], #MI in BB
151void getMappingInfoForFunction::writeBBToMImap(Function &FI){
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000152 unsigned bb = 0;
153 MachineFunction &MF = MachineFunction::get(&FI);
154 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
155 BI != BE; ++BI, ++bb) {
156 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000157 writeNumber(bb);
158 //Out << " BB: "<<(void *)BI<<"\n";
159 //for(int i=0; i<miBB.size(); ++i)
160 //Out<<*miBB[i]<<"\n";
161 writeNumber( BBkey[ miBB[0] ]);
162 writeNumber(miBB.size());
163 }
164}
165
166//LLVMtoMImap: contains F#, BB#, LLVM#,
167// MIs[wrt to beginning of BB]
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000168void getMappingInfoForFunction::writeLLVMToMImap(Function &FI) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000169
170 unsigned bb =0;
171 for (Function::iterator BI = FI.begin(), BE = FI.end();
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000172 BI != BE; ++BI, ++bb) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000173 unsigned li = 0;
174 writeNumber(bb);
Anand Shuklab3794702002-09-17 20:24:46 +0000175 //std::cerr<<"BasicBlockNumber= "<<bb<<"\n";
176
Anand Shuklab85d2652002-08-27 22:47:33 +0000177 //Out << "BB: "<<(void *)BI<<"\n";
178 writeNumber(BI->size());
Anand Shuklab3794702002-09-17 20:24:46 +0000179 //std::cerr<<"BasicBlockSize = "<<BI->size()<<"\n";
180
Anand Shuklab85d2652002-08-27 22:47:33 +0000181 for (BasicBlock::iterator II = BI->begin(),
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000182 IE = BI->end(); II != IE; ++II, ++li) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000183 //Out << "I: "<<*II<<"\n";
184 MachineCodeForInstruction& miI =
185 MachineCodeForInstruction::get(II);
186
187 //do for each corr. MI
188 writeNumber(li);
Anand Shuklab3794702002-09-17 20:24:46 +0000189 //std::cerr<<"InstructionNumber= "<<li<<"\n";
190
191 writeNumber(miI.size());
192 //std::cerr<<"InstructionSize = "<<miI.size()<<"\n";
193
Anand Shuklab85d2652002-08-27 22:47:33 +0000194 for (MachineCodeForInstruction::iterator miII = miI.begin(),
195 miIE = miI.end(); miII != miIE; ++miII){
196 //Out << "MI: "<<**miII<<"\n";
197 writeNumber(MIkey[*miII]);
Anand Shuklab3794702002-09-17 20:24:46 +0000198 //std::cerr<<"MachineInstruction= "<<MIkey[*miII]<<"\n";
Anand Shuklab85d2652002-08-27 22:47:33 +0000199 }
200 }
201 }
202}