blob: 9c51bcf9dbaa9ca5f4efd6f7b913dd2fa112bdfc [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
Brian Gaekefc97c8b2003-06-03 19:30:15 +000037 void writePrologue(const std::string &comment,
38 const std::string &symbolPrefix, unsigned num);
39 void writeEpilogue(const std::string &symbolPrefix, unsigned num);
40
Anand Shuklab85d2652002-08-27 22:47:33 +000041 bool doInitialization(Module &M);
42 void create_BB_to_MInumber_Key(Function &FI);
43 void create_MI_to_number_Key(Function &FI);
Brian Gaekefc97c8b2003-06-03 19:30:15 +000044 void writeBBToMImap(Function &FI, unsigned num);
45 void writeLLVMToMImap(Function &FI, unsigned num);
Chris Lattnere5d42932003-06-04 21:01:12 +000046 unsigned writeNumber(unsigned X);
Anand Shuklab85d2652002-08-27 22:47:33 +000047 };
48}
49
Brian Gaekee14ccaf2003-06-03 07:56:05 +000050/// MappingInfoForFunction -- Static factory method: returns a new
Brian Gaekefc97c8b2003-06-03 19:30:15 +000051/// getMappingInfoForFunction Pass object, which uses OUT as its
52/// output stream for assembly output.
Anand Shuklab85d2652002-08-27 22:47:33 +000053Pass *MappingInfoForFunction(std::ostream &out){
54 return (new getMappingInfoForFunction(out));
55}
56
Brian Gaekefc97c8b2003-06-03 19:30:15 +000057/// runOnFunction -- Builds up the maps for the given function FI and then
58/// writes them out as assembly code to the current output stream OUT.
Brian Gaekee14ccaf2003-06-03 07:56:05 +000059/// This is an entry point to the pass, called by the PassManager.
Anand Shuklab85d2652002-08-27 22:47:33 +000060bool getMappingInfoForFunction::runOnFunction(Function &FI) {
Brian Gaekefc97c8b2003-06-03 19:30:15 +000061 // First we build temporary tables used to write out the maps.
Anand Shuklab85d2652002-08-27 22:47:33 +000062 create_BB_to_MInumber_Key(FI);
63 create_MI_to_number_Key(FI);
Brian Gaekefc97c8b2003-06-03 19:30:15 +000064 unsigned num = Fkey[&FI]; // Function number for the current function.
Anand Shuklab85d2652002-08-27 22:47:33 +000065
Brian Gaekefc97c8b2003-06-03 19:30:15 +000066 // Now, write out the maps.
67 writeBBToMImap(FI, num);
68 writeLLVMToMImap(FI, num);
69
Anand Shuklab85d2652002-08-27 22:47:33 +000070 return false;
71}
72
Brian Gaekefc97c8b2003-06-03 19:30:15 +000073/// writePrologue -- Output a COMMENT describing the map, then output a
74/// global symbol to start the map named by concatenating SYMBOLPREFIX
75/// and NUM, then output a word containing the length of the map, to the
76/// current output stream Out. This also switches the current section to
77/// .rodata in the assembly output.
78void getMappingInfoForFunction::writePrologue(const std::string &comment,
79 const std::string &symbolPrefix,
80 unsigned num) {
81 // Comment:
82 Out << "!" << comment << "\n";
83 // Switch sections:
Anand Shuklab85d2652002-08-27 22:47:33 +000084 Out << "\t.section \".rodata\"\n\t.align 8\n";
Brian Gaekefc97c8b2003-06-03 19:30:15 +000085 // Global symbol naming the map:
86 Out << "\t.global " << symbolPrefix << num << "\n";
87 Out << "\t.type " << symbolPrefix << num << ",#object\n";
88 Out << symbolPrefix << num << ":\n";
89 // Length word:
90 Out << "\t.word .end_" << symbolPrefix << num << "-"
91 << symbolPrefix << num << "\n";
Anand Shuklab85d2652002-08-27 22:47:33 +000092}
93
Brian Gaekefc97c8b2003-06-03 19:30:15 +000094/// writeEpilogue -- Outputs a local symbol to end the map named by
95/// concatenating SYMBOLPREFIX and NUM, followed by a .size directive that
96/// gives the size of the map, to the current output stream Out.
97void getMappingInfoForFunction::writeEpilogue(const std::string &symbolPrefix,
98 unsigned num) {
99 // Local symbol ending the map:
100 Out << ".end_" << symbolPrefix << num << ":\n";
101 // Size directive:
102 Out << "\t.size " << symbolPrefix << num << ", .end_"
103 << symbolPrefix << num << "-" << symbolPrefix
104 << num << "\n\n\n\n";
Anand Shuklab85d2652002-08-27 22:47:33 +0000105}
106
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000107/// writeNumber -- Write out the number X as a sequence of .byte
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000108/// directives to the current output stream Out. This method performs a
109/// run-length encoding of the unsigned integers X that are output.
Chris Lattnere5d42932003-06-04 21:01:12 +0000110unsigned getMappingInfoForFunction::writeNumber(unsigned X) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000111 unsigned i=0;
112 do {
113 unsigned tmp = X & 127;
114 X >>= 7;
115 if (X) tmp |= 128;
Chris Lattnere5d42932003-06-04 21:01:12 +0000116 Out << "\t.byte " << tmp << "\n";
Anand Shuklab85d2652002-08-27 22:47:33 +0000117 ++i;
118 } while(X);
Chris Lattnere5d42932003-06-04 21:01:12 +0000119 return i;
Anand Shuklab85d2652002-08-27 22:47:33 +0000120}
121
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000122/// doInitialization -- Assign a number to each Function, as follows:
123/// Functions are numbered starting at 0 at the begin() of each Module.
124/// Functions which are External (and thus have 0 basic blocks) are not
125/// inserted into the maps, and are not assigned a number. The side-effect
126/// of this method is to fill in Fkey to contain the mapping from Functions
127/// to numbers. (This method is called automatically by the PassManager.)
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000128bool getMappingInfoForFunction::doInitialization(Module &M) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000129 unsigned i = 0;
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000130 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
131 if (FI->isExternal()) continue;
Anand Shuklab85d2652002-08-27 22:47:33 +0000132 Fkey[FI] = i;
133 ++i;
134 }
135 return false;
136}
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000137
138/// create_BB_to_MInumber_Key -- Assign a number to each MachineBasicBlock
139/// in the given Function, as follows: Numbering starts at zero in each
140/// Function. MachineBasicBlocks are numbered from begin() to end()
141/// in the Function's corresponding MachineFunction. Each successive
142/// MachineBasicBlock increments the numbering by the number of instructions
143/// it contains. The side-effect of this method is to fill in the instance
144/// variable BBkey with the mapping of MachineBasicBlocks to numbers. BBkey
145/// is keyed on MachineInstrs, so each MachineBasicBlock is represented
146/// therein by its first MachineInstr.
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000147void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000148 unsigned i = 0;
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000149 MachineFunction &MF = MachineFunction::get(&FI);
150 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
151 BI != BE; ++BI) {
152 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000153 BBkey[miBB[0]] = i;
154 i = i+(miBB.size());
155 }
156}
157
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000158/// create_MI_to_number_Key -- Assign a number to each MachineInstr
159/// in the given Function with respect to its enclosing MachineBasicBlock, as
160/// follows: Numberings start at 0 in each MachineBasicBlock. MachineInstrs
161/// are numbered from begin() to end() in their MachineBasicBlock. Each
162/// MachineInstr is numbered, then the numbering is incremented by 1. The
163/// side-effect of this method is to fill in the instance variable MIkey
164/// with the mapping from MachineInstrs to numbers.
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000165void getMappingInfoForFunction::create_MI_to_number_Key(Function &FI) {
166 MachineFunction &MF = MachineFunction::get(&FI);
167 for (MachineFunction::iterator BI=MF.begin(), BE=MF.end(); BI != BE; ++BI) {
168 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000169 unsigned j = 0;
Chris Lattner55291ea2002-10-28 01:41:47 +0000170 for(MachineBasicBlock::iterator miI=miBB.begin(), miE=miBB.end();
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000171 miI!=miE; ++miI, ++j) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000172 MIkey[*miI]=j;
173 }
174 }
175}
176
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000177/// writeBBToMImap -- Output the BB TO MI MAP for the given function as
178/// assembly code to the current output stream. The BB TO MI MAP consists
179/// of a three-element tuple for each MachineBasicBlock in a function:
180/// first, the index of the MachineBasicBlock in the function; second,
181/// the number of the MachineBasicBlock in the function as computed by
182/// create_BB_to_MInumber_Key; and third, the number of MachineInstrs in
183/// the MachineBasicBlock.
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000184void getMappingInfoForFunction::writeBBToMImap(Function &FI, unsigned num){
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000185 unsigned bb = 0;
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000186 const std::string MapComment = "BB TO MI MAP";
187 const std::string MapSymbolPrefix = "BBMIMap";
188 writePrologue(MapComment, MapSymbolPrefix, num);
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000189 MachineFunction &MF = MachineFunction::get(&FI);
190 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
191 BI != BE; ++BI, ++bb) {
192 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000193 writeNumber(bb);
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000194 writeNumber(BBkey[miBB[0]]);
Anand Shuklab85d2652002-08-27 22:47:33 +0000195 writeNumber(miBB.size());
196 }
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000197 writeEpilogue(MapSymbolPrefix, num);
Anand Shuklab85d2652002-08-27 22:47:33 +0000198}
199
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000200/// writeLLVMToMImap -- Output the LLVM I TO MI MAP for the given function
201/// as assembly code to the current output stream. The LLVM I TO MI MAP
202/// consists of a set of information for each BasicBlock in a Function,
203/// ordered from begin() to end(). The information for a BasicBlock consists
204/// of 1) its (0-based) index in the Function, 2) the number of LLVM
205/// Instructions it contains, and 3) information for each Instruction, in
206/// sequence from the begin() to the end() of the BasicBlock. The information
207/// for an Instruction consists of 1) its (0-based) index in the BasicBlock,
208/// 2) the number of MachineInstrs that correspond to that Instruction
209/// (as reported by MachineCodeForInstruction), and 3) the MachineInstr
210/// number calculated by create_MI_to_number_Key, for each of the
211/// MachineInstrs that correspond to that Instruction.
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000212void getMappingInfoForFunction::writeLLVMToMImap(Function &FI, unsigned num) {
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000213 unsigned bb = 0;
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000214 const std::string MapComment = "LLVM I TO MI MAP";
215 const std::string MapSymbolPrefix = "LMIMap";
216 writePrologue(MapComment, MapSymbolPrefix, num);
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000217 for (Function::iterator BI = FI.begin(), BE = FI.end();
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000218 BI != BE; ++BI, ++bb) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000219 unsigned li = 0;
220 writeNumber(bb);
Anand Shuklab85d2652002-08-27 22:47:33 +0000221 writeNumber(BI->size());
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000222 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
223 ++II, ++li) {
224 MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II);
Anand Shuklab85d2652002-08-27 22:47:33 +0000225 writeNumber(li);
Anand Shuklab3794702002-09-17 20:24:46 +0000226 writeNumber(miI.size());
Anand Shuklab85d2652002-08-27 22:47:33 +0000227 for (MachineCodeForInstruction::iterator miII = miI.begin(),
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000228 miIE = miI.end(); miII != miIE; ++miII) {
229 writeNumber(MIkey[*miII]);
Anand Shuklab85d2652002-08-27 22:47:33 +0000230 }
231 }
232 }
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000233 writeEpilogue(MapSymbolPrefix, num);
Anand Shuklab85d2652002-08-27 22:47:33 +0000234}