blob: 6f808ab77804e7389af3e3afe25ae6b589d53ffd [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);
Brian Gaekee961d962003-06-04 18:17:22 +000046 void 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 Gaekee961d962003-06-04 18:17:22 +0000107/// outByte -- NOT DONE YET.
108void outByte (unsigned char b) {
Tanya Lattner758578e2003-06-04 20:53:46 +0000109 //Out << "\t.byte " << tmp << "\n";
Brian Gaekee961d962003-06-04 18:17:22 +0000110}
111
112
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000113/// writeNumber -- Write out the number X as a sequence of .byte
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000114/// directives to the current output stream Out. This method performs a
115/// run-length encoding of the unsigned integers X that are output.
Brian Gaekee961d962003-06-04 18:17:22 +0000116void getMappingInfoForFunction::writeNumber(unsigned X) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000117 unsigned i=0;
118 do {
119 unsigned tmp = X & 127;
120 X >>= 7;
121 if (X) tmp |= 128;
Brian Gaekee961d962003-06-04 18:17:22 +0000122 outByte (tmp);
Anand Shuklab85d2652002-08-27 22:47:33 +0000123 ++i;
124 } while(X);
Anand Shuklab85d2652002-08-27 22:47:33 +0000125}
126
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000127/// doInitialization -- Assign a number to each Function, as follows:
128/// Functions are numbered starting at 0 at the begin() of each Module.
129/// Functions which are External (and thus have 0 basic blocks) are not
130/// inserted into the maps, and are not assigned a number. The side-effect
131/// of this method is to fill in Fkey to contain the mapping from Functions
132/// to numbers. (This method is called automatically by the PassManager.)
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000133bool getMappingInfoForFunction::doInitialization(Module &M) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000134 unsigned i = 0;
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000135 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
136 if (FI->isExternal()) continue;
Anand Shuklab85d2652002-08-27 22:47:33 +0000137 Fkey[FI] = i;
138 ++i;
139 }
140 return false;
141}
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000142
143/// create_BB_to_MInumber_Key -- Assign a number to each MachineBasicBlock
144/// in the given Function, as follows: Numbering starts at zero in each
145/// Function. MachineBasicBlocks are numbered from begin() to end()
146/// in the Function's corresponding MachineFunction. Each successive
147/// MachineBasicBlock increments the numbering by the number of instructions
148/// it contains. The side-effect of this method is to fill in the instance
149/// variable BBkey with the mapping of MachineBasicBlocks to numbers. BBkey
150/// is keyed on MachineInstrs, so each MachineBasicBlock is represented
151/// therein by its first MachineInstr.
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000152void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000153 unsigned i = 0;
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000154 MachineFunction &MF = MachineFunction::get(&FI);
155 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
156 BI != BE; ++BI) {
157 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000158 BBkey[miBB[0]] = i;
159 i = i+(miBB.size());
160 }
161}
162
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000163/// create_MI_to_number_Key -- Assign a number to each MachineInstr
164/// in the given Function with respect to its enclosing MachineBasicBlock, as
165/// follows: Numberings start at 0 in each MachineBasicBlock. MachineInstrs
166/// are numbered from begin() to end() in their MachineBasicBlock. Each
167/// MachineInstr is numbered, then the numbering is incremented by 1. The
168/// side-effect of this method is to fill in the instance variable MIkey
169/// with the mapping from MachineInstrs to numbers.
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000170void getMappingInfoForFunction::create_MI_to_number_Key(Function &FI) {
171 MachineFunction &MF = MachineFunction::get(&FI);
172 for (MachineFunction::iterator BI=MF.begin(), BE=MF.end(); BI != BE; ++BI) {
173 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000174 unsigned j = 0;
Chris Lattner55291ea2002-10-28 01:41:47 +0000175 for(MachineBasicBlock::iterator miI=miBB.begin(), miE=miBB.end();
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000176 miI!=miE; ++miI, ++j) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000177 MIkey[*miI]=j;
178 }
179 }
180}
181
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000182/// writeBBToMImap -- Output the BB TO MI MAP for the given function as
183/// assembly code to the current output stream. The BB TO MI MAP consists
184/// of a three-element tuple for each MachineBasicBlock in a function:
185/// first, the index of the MachineBasicBlock in the function; second,
186/// the number of the MachineBasicBlock in the function as computed by
187/// create_BB_to_MInumber_Key; and third, the number of MachineInstrs in
188/// the MachineBasicBlock.
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000189void getMappingInfoForFunction::writeBBToMImap(Function &FI, unsigned num){
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000190 unsigned bb = 0;
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000191 const std::string MapComment = "BB TO MI MAP";
192 const std::string MapSymbolPrefix = "BBMIMap";
193 writePrologue(MapComment, MapSymbolPrefix, num);
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000194 MachineFunction &MF = MachineFunction::get(&FI);
195 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
196 BI != BE; ++BI, ++bb) {
197 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000198 writeNumber(bb);
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000199 writeNumber(BBkey[miBB[0]]);
Anand Shuklab85d2652002-08-27 22:47:33 +0000200 writeNumber(miBB.size());
201 }
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000202 writeEpilogue(MapSymbolPrefix, num);
Anand Shuklab85d2652002-08-27 22:47:33 +0000203}
204
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000205/// writeLLVMToMImap -- Output the LLVM I TO MI MAP for the given function
206/// as assembly code to the current output stream. The LLVM I TO MI MAP
207/// consists of a set of information for each BasicBlock in a Function,
208/// ordered from begin() to end(). The information for a BasicBlock consists
209/// of 1) its (0-based) index in the Function, 2) the number of LLVM
210/// Instructions it contains, and 3) information for each Instruction, in
211/// sequence from the begin() to the end() of the BasicBlock. The information
212/// for an Instruction consists of 1) its (0-based) index in the BasicBlock,
213/// 2) the number of MachineInstrs that correspond to that Instruction
214/// (as reported by MachineCodeForInstruction), and 3) the MachineInstr
215/// number calculated by create_MI_to_number_Key, for each of the
216/// MachineInstrs that correspond to that Instruction.
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000217void getMappingInfoForFunction::writeLLVMToMImap(Function &FI, unsigned num) {
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000218 unsigned bb = 0;
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000219 const std::string MapComment = "LLVM I TO MI MAP";
220 const std::string MapSymbolPrefix = "LMIMap";
221 writePrologue(MapComment, MapSymbolPrefix, num);
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000222 for (Function::iterator BI = FI.begin(), BE = FI.end();
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000223 BI != BE; ++BI, ++bb) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000224 unsigned li = 0;
225 writeNumber(bb);
Anand Shuklab85d2652002-08-27 22:47:33 +0000226 writeNumber(BI->size());
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000227 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
228 ++II, ++li) {
229 MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II);
Anand Shuklab85d2652002-08-27 22:47:33 +0000230 writeNumber(li);
Anand Shuklab3794702002-09-17 20:24:46 +0000231 writeNumber(miI.size());
Anand Shuklab85d2652002-08-27 22:47:33 +0000232 for (MachineCodeForInstruction::iterator miII = miI.begin(),
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000233 miIE = miI.end(); miII != miIE; ++miII) {
234 writeNumber(MIkey[*miII]);
Anand Shuklab85d2652002-08-27 22:47:33 +0000235 }
236 }
237 }
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000238 writeEpilogue(MapSymbolPrefix, num);
Anand Shuklab85d2652002-08-27 22:47:33 +0000239}