blob: 66f2aa5538fa8e761f72a88bafb52beb82890342 [file] [log] [blame]
Anand Shuklab85d2652002-08-27 22:47:33 +00001//===- MappingInfo.cpp - create LLVM info and output to .s file ---------===//
2//
Brian Gaekee14ccaf2003-06-03 07:56:05 +00003// 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 Shuklab85d2652002-08-27 22:47:33 +000014//
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 Brukmanb7551ef2002-10-28 20:00:31 +000021#include "llvm/CodeGen/MachineFunction.h"
Anand Shuklab85d2652002-08-27 22:47:33 +000022#include "llvm/CodeGen/MachineCodeForInstruction.h"
23#include <map>
24using std::vector;
25
Anand Shuklab85d2652002-08-27 22:47:33 +000026namespace {
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 Gaekee14ccaf2003-06-03 07:56:05 +000053/// MappingInfoForFunction -- Static factory method: returns a new
54/// getMappingInfoForFunction Pass object.
Anand Shuklab85d2652002-08-27 22:47:33 +000055Pass *MappingInfoForFunction(std::ostream &out){
56 return (new getMappingInfoForFunction(out));
57}
58
Brian Gaekee14ccaf2003-06-03 07:56:05 +000059/// 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 Shuklab85d2652002-08-27 22:47:33 +000062bool getMappingInfoForFunction::runOnFunction(Function &FI) {
Brian Gaekee14ccaf2003-06-03 07:56:05 +000063 // First we build up the maps.
Anand Shuklab85d2652002-08-27 22:47:33 +000064 create_BB_to_MInumber_Key(FI);
65 create_MI_to_number_Key(FI);
Brian Gaekee14ccaf2003-06-03 07:56:05 +000066 unsigned FunctionNo = Fkey[&FI];
Anand Shuklab85d2652002-08-27 22:47:33 +000067
Brian Gaekee14ccaf2003-06-03 07:56:05 +000068 // Now, print out the maps.
Anand Shuklab85d2652002-08-27 22:47:33 +000069 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
79void 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
91void 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 Gaekee14ccaf2003-06-03 07:56:05 +0000100/// writeNumber -- Write out the number X as a sequence of .byte
101/// directives to the current output stream Out.
Anand Shuklab85d2652002-08-27 22:47:33 +0000102unsigned 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 Gaekee14ccaf2003-06-03 07:56:05 +0000114/// 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 Brukmanb7551ef2002-10-28 20:00:31 +0000120bool getMappingInfoForFunction::doInitialization(Module &M) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000121 unsigned i = 0;
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000122 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
123 if (FI->isExternal()) continue;
Anand Shuklab85d2652002-08-27 22:47:33 +0000124 Fkey[FI] = i;
125 ++i;
126 }
127 return false;
128}
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000129
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 Brukmanb7551ef2002-10-28 20:00:31 +0000139void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000140 unsigned i = 0;
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000141 MachineFunction &MF = MachineFunction::get(&FI);
142 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
143 BI != BE; ++BI) {
144 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000145 BBkey[miBB[0]] = i;
146 i = i+(miBB.size());
147 }
148}
149
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000150/// 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 Brukmanb7551ef2002-10-28 20:00:31 +0000157void 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 Shuklab85d2652002-08-27 22:47:33 +0000161 unsigned j = 0;
Chris Lattner55291ea2002-10-28 01:41:47 +0000162 for(MachineBasicBlock::iterator miI=miBB.begin(), miE=miBB.end();
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000163 miI!=miE; ++miI, ++j) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000164 MIkey[*miI]=j;
165 }
166 }
167}
168
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000169/// 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 Shuklab85d2652002-08-27 22:47:33 +0000176void getMappingInfoForFunction::writeBBToMImap(Function &FI){
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000177 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 Shuklab85d2652002-08-27 22:47:33 +0000182 writeNumber(bb);
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000183 writeNumber(BBkey[miBB[0]]);
Anand Shuklab85d2652002-08-27 22:47:33 +0000184 writeNumber(miBB.size());
185 }
186}
187
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000188/// 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 Brukmanb7551ef2002-10-28 20:00:31 +0000200void getMappingInfoForFunction::writeLLVMToMImap(Function &FI) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000201
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000202 unsigned bb = 0;
203 for (Function::iterator BI = FI.begin(), BE = FI.end();
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000204 BI != BE; ++BI, ++bb) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000205 unsigned li = 0;
206 writeNumber(bb);
Anand Shuklab85d2652002-08-27 22:47:33 +0000207 writeNumber(BI->size());
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000208 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
209 ++II, ++li) {
210 MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II);
Anand Shuklab85d2652002-08-27 22:47:33 +0000211 writeNumber(li);
Anand Shuklab3794702002-09-17 20:24:46 +0000212 writeNumber(miI.size());
Anand Shuklab85d2652002-08-27 22:47:33 +0000213 for (MachineCodeForInstruction::iterator miII = miI.begin(),
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000214 miIE = miI.end(); miII != miIE; ++miII) {
215 writeNumber(MIkey[*miII]);
Anand Shuklab85d2652002-08-27 22:47:33 +0000216 }
217 }
218 }
219}