blob: 3aa23592888d22a05daa27c00a12cc24f53f5ad1 [file] [log] [blame]
Brian Gaeke3ca4fcc2004-04-25 07:04:49 +00001//===- MappingInfo.cpp - create LLVM info and output to .s file -----------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Anand Shuklab85d2652002-08-27 22:47:33 +00009//
Brian Gaeke866dc1d2003-09-18 17:37:25 +000010// This file contains a FunctionPass called MappingInfoAsmPrinter,
Brian Gaekeaeab1e12003-06-04 22:07:12 +000011// which creates two maps: one between LLVM Instructions and MachineInstrs
12// (the "LLVM I TO MI MAP"), and another between MachineBasicBlocks and
13// MachineInstrs (the "BB TO MI MAP").
Brian Gaekee14ccaf2003-06-03 07:56:05 +000014//
15// As a side effect, it outputs this information as .byte directives to
16// the assembly file. The output is designed to survive the SPARC assembler,
17// in order that the Reoptimizer may read it in from memory later when the
18// binary is loaded. Therefore, it may contain some hidden SPARC-architecture
19// dependencies. Currently this question is purely theoretical as the
20// Reoptimizer works only on the SPARC.
Anand Shuklab85d2652002-08-27 22:47:33 +000021//
Brian Gaekeaeab1e12003-06-04 22:07:12 +000022// The LLVM I TO MI MAP consists of a set of information for each
23// BasicBlock in a Function, ordered from begin() to end(). The information
24// for a BasicBlock consists of
25// 1) its (0-based) index in the Function,
26// 2) the number of LLVM Instructions it contains, and
27// 3) information for each Instruction, in sequence from the begin()
28// to the end() of the BasicBlock. The information for an Instruction
29// consists of
30// 1) its (0-based) index in the BasicBlock,
31// 2) the number of MachineInstrs that correspond to that Instruction
32// (as reported by MachineCodeForInstruction), and
33// 3) the MachineInstr number calculated by create_MI_to_number_Key,
34// for each of the MachineInstrs that correspond to that Instruction.
35//
36// The BB TO MI MAP consists of a three-element tuple for each
37// MachineBasicBlock in a function, ordered from begin() to end() of
38// its MachineFunction: first, the index of the MachineBasicBlock in the
39// function; second, the number of the MachineBasicBlock in the function
40// as computed by create_BB_to_MInumber_Key; and third, the number of
41// MachineInstrs in the MachineBasicBlock.
42//
Anand Shuklab85d2652002-08-27 22:47:33 +000043//===--------------------------------------------------------------------===//
44
Chris Lattner48e60792003-08-13 02:38:16 +000045#include "MappingInfo.h"
Anand Shuklab85d2652002-08-27 22:47:33 +000046#include "llvm/Pass.h"
47#include "llvm/Module.h"
Misha Brukmanb7551ef2002-10-28 20:00:31 +000048#include "llvm/CodeGen/MachineFunction.h"
Anand Shuklab85d2652002-08-27 22:47:33 +000049#include "llvm/CodeGen/MachineCodeForInstruction.h"
Brian Gaeke866dc1d2003-09-18 17:37:25 +000050#include "Support/StringExtras.h"
Anand Shuklab85d2652002-08-27 22:47:33 +000051
Brian Gaeked0fde302003-11-11 22:41:34 +000052namespace llvm {
53
Anand Shuklab85d2652002-08-27 22:47:33 +000054namespace {
Brian Gaeke866dc1d2003-09-18 17:37:25 +000055 class MappingInfoAsmPrinter : public FunctionPass {
Anand Shuklab85d2652002-08-27 22:47:33 +000056 std::ostream &Out;
57 public:
Brian Gaeke866dc1d2003-09-18 17:37:25 +000058 MappingInfoAsmPrinter(std::ostream &out) : Out(out){}
Brian Gaekeaeab1e12003-06-04 22:07:12 +000059 const char *getPassName () const { return "Instr. Mapping Info Collector"; }
Anand Shuklab85d2652002-08-27 22:47:33 +000060 bool runOnFunction(Function &FI);
Brian Gaekeaeab1e12003-06-04 22:07:12 +000061 typedef std::map<const MachineInstr*, unsigned> InstructionKey;
Anand Shuklab85d2652002-08-27 22:47:33 +000062 private:
Brian Gaekeaeab1e12003-06-04 22:07:12 +000063 MappingInfo *currentOutputMap;
64 std::map<Function *, unsigned> Fkey; // Function # for all functions.
Anand Shuklab85d2652002-08-27 22:47:33 +000065 bool doInitialization(Module &M);
Brian Gaekeaeab1e12003-06-04 22:07:12 +000066 void create_BB_to_MInumber_Key(Function &FI, InstructionKey &key);
67 void create_MI_to_number_Key(Function &FI, InstructionKey &key);
68 void buildBBMIMap (Function &FI, MappingInfo &Map);
69 void buildLMIMap (Function &FI, MappingInfo &Map);
70 void writeNumber(unsigned X);
71 void selectOutputMap (MappingInfo &m) { currentOutputMap = &m; }
72 void outByte (unsigned char b) { currentOutputMap->outByte (b); }
Brian Gaeke866dc1d2003-09-18 17:37:25 +000073 bool doFinalization (Module &M);
Anand Shuklab85d2652002-08-27 22:47:33 +000074 };
75}
76
Brian Gaeke866dc1d2003-09-18 17:37:25 +000077/// getMappingInfoAsmPrinterPass - Static factory method: returns a new
78/// MappingInfoAsmPrinter Pass object, which uses OUT as its output
79/// stream for assembly output.
80///
81Pass *getMappingInfoAsmPrinterPass(std::ostream &out){
82 return (new MappingInfoAsmPrinter(out));
Anand Shuklab85d2652002-08-27 22:47:33 +000083}
84
Brian Gaeke866dc1d2003-09-18 17:37:25 +000085/// runOnFunction - Builds up the maps for the given function FI and then
Brian Gaekefc97c8b2003-06-03 19:30:15 +000086/// writes them out as assembly code to the current output stream OUT.
Brian Gaekee14ccaf2003-06-03 07:56:05 +000087/// This is an entry point to the pass, called by the PassManager.
Brian Gaeke866dc1d2003-09-18 17:37:25 +000088///
89bool MappingInfoAsmPrinter::runOnFunction(Function &FI) {
Brian Gaekefc97c8b2003-06-03 19:30:15 +000090 unsigned num = Fkey[&FI]; // Function number for the current function.
Anand Shuklab85d2652002-08-27 22:47:33 +000091
Brian Gaekeaeab1e12003-06-04 22:07:12 +000092 // Create objects to hold the maps.
93 MappingInfo LMIMap ("LLVM I TO MI MAP", "LMIMap", num);
94 MappingInfo BBMIMap ("BB TO MI MAP", "BBMIMap", num);
95
96 // Now, build the maps.
97 buildLMIMap (FI, LMIMap);
98 buildBBMIMap (FI, BBMIMap);
99
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000100 // Now, write out the maps.
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000101 LMIMap.dumpAssembly (Out);
102 BBMIMap.dumpAssembly (Out);
Brian Gaekefc97c8b2003-06-03 19:30:15 +0000103
Anand Shuklab85d2652002-08-27 22:47:33 +0000104 return false;
105}
106
Brian Gaeke866dc1d2003-09-18 17:37:25 +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.
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000110///
111void MappingInfoAsmPrinter::writeNumber(unsigned X) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000112 unsigned i=0;
113 do {
114 unsigned tmp = X & 127;
115 X >>= 7;
116 if (X) tmp |= 128;
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000117 outByte (tmp);
Anand Shuklab85d2652002-08-27 22:47:33 +0000118 ++i;
119 } while(X);
Anand Shuklab85d2652002-08-27 22:47:33 +0000120}
121
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000122/// doInitialization - Assign a number to each Function, as follows:
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000123/// 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.)
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000128///
129bool MappingInfoAsmPrinter::doInitialization(Module &M) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000130 unsigned i = 0;
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000131 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
132 if (FI->isExternal()) continue;
Anand Shuklab85d2652002-08-27 22:47:33 +0000133 Fkey[FI] = i;
134 ++i;
135 }
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000136 return false; // Success.
Anand Shuklab85d2652002-08-27 22:47:33 +0000137}
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000138
139/// create_BB_to_MInumber_Key -- Assign a number to each MachineBasicBlock
140/// in the given Function, as follows: Numbering starts at zero in each
141/// Function. MachineBasicBlocks are numbered from begin() to end()
142/// in the Function's corresponding MachineFunction. Each successive
143/// MachineBasicBlock increments the numbering by the number of instructions
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000144/// it contains. The side-effect of this method is to fill in the parameter
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000145/// KEY with the mapping of MachineBasicBlocks to numbers. KEY
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000146/// is keyed on MachineInstrs, so each MachineBasicBlock is represented
147/// therein by its first MachineInstr.
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000148///
149void MappingInfoAsmPrinter::create_BB_to_MInumber_Key(Function &FI,
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000150 InstructionKey &key) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000151 unsigned i = 0;
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000152 MachineFunction &MF = MachineFunction::get(&FI);
153 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
154 BI != BE; ++BI) {
155 MachineBasicBlock &miBB = *BI;
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000156 key[&miBB.front()] = i;
Anand Shuklab85d2652002-08-27 22:47:33 +0000157 i = i+(miBB.size());
158 }
159}
160
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000161/// create_MI_to_number_Key - Assign a number to each MachineInstr
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000162/// in the given Function with respect to its enclosing MachineBasicBlock, as
163/// follows: Numberings start at 0 in each MachineBasicBlock. MachineInstrs
164/// are numbered from begin() to end() in their MachineBasicBlock. Each
165/// MachineInstr is numbered, then the numbering is incremented by 1. The
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000166/// side-effect of this method is to fill in the parameter KEY
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000167/// with the mapping from MachineInstrs to numbers.
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000168///
169void MappingInfoAsmPrinter::create_MI_to_number_Key(Function &FI,
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000170 InstructionKey &key) {
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000171 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;
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000175 for(MachineBasicBlock::iterator miI = miBB.begin(), miE = miBB.end();
176 miI != miE; ++miI, ++j) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000177 key[miI] = j;
Anand Shuklab85d2652002-08-27 22:47:33 +0000178 }
179 }
180}
181
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000182/// buildBBMIMap - Build the BB TO MI MAP for the function FI,
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000183/// and save it into the parameter MAP.
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000184///
185void MappingInfoAsmPrinter::buildBBMIMap(Function &FI, MappingInfo &Map) {
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000186 unsigned bb = 0;
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000187
188 // First build temporary table used to write out the map.
189 InstructionKey BBkey;
190 create_BB_to_MInumber_Key(FI, BBkey);
191
192 selectOutputMap (Map);
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000193 MachineFunction &MF = MachineFunction::get(&FI);
194 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
195 BI != BE; ++BI, ++bb) {
196 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000197 writeNumber(bb);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000198 writeNumber(BBkey[&miBB.front()]);
Anand Shuklab85d2652002-08-27 22:47:33 +0000199 writeNumber(miBB.size());
200 }
201}
202
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000203/// buildLMIMap - Build the LLVM I TO MI MAP for the function FI,
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000204/// and save it into the parameter MAP.
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000205///
206void MappingInfoAsmPrinter::buildLMIMap(Function &FI, MappingInfo &Map) {
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000207 unsigned bb = 0;
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000208 // First build temporary table used to write out the map.
209 InstructionKey MIkey;
210 create_MI_to_number_Key(FI, MIkey);
211
212 selectOutputMap (Map);
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000213 for (Function::iterator BI = FI.begin(), BE = FI.end();
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000214 BI != BE; ++BI, ++bb) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000215 unsigned li = 0;
216 writeNumber(bb);
Anand Shuklab85d2652002-08-27 22:47:33 +0000217 writeNumber(BI->size());
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000218 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
219 ++II, ++li) {
220 MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II);
Anand Shuklab85d2652002-08-27 22:47:33 +0000221 writeNumber(li);
Anand Shuklab3794702002-09-17 20:24:46 +0000222 writeNumber(miI.size());
Anand Shuklab85d2652002-08-27 22:47:33 +0000223 for (MachineCodeForInstruction::iterator miII = miI.begin(),
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000224 miIE = miI.end(); miII != miIE; ++miII) {
225 writeNumber(MIkey[*miII]);
Anand Shuklab85d2652002-08-27 22:47:33 +0000226 }
227 }
228 }
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000229}
230
231void MappingInfo::byteVector::dumpAssembly (std::ostream &Out) {
232 for (iterator i = begin (), e = end (); i != e; ++i)
233 Out << ".byte " << (int)*i << "\n";
234}
235
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000236static void writePrologue (std::ostream &Out, const std::string &comment,
237 const std::string &symName) {
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000238 // Prologue:
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000239 // Output a comment describing the object.
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000240 Out << "!" << comment << "\n";
241 // Switch the current section to .rodata in the assembly output:
242 Out << "\t.section \".rodata\"\n\t.align 8\n";
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000243 // Output a global symbol naming the object:
244 Out << "\t.global " << symName << "\n";
245 Out << "\t.type " << symName << ",#object\n";
246 Out << symName << ":\n";
Anand Shuklab85d2652002-08-27 22:47:33 +0000247}
Brian Gaeke866dc1d2003-09-18 17:37:25 +0000248
249static void writeEpilogue (std::ostream &Out, const std::string &symName) {
250 // Epilogue:
251 // Output a local symbol marking the end of the object:
252 Out << ".end_" << symName << ":\n";
253 // Output size directive giving the size of the object:
254 Out << "\t.size " << symName << ", .end_" << symName << "-" << symName
255 << "\n";
256}
257
258void MappingInfo::dumpAssembly (std::ostream &Out) {
259 const std::string &name (symbolPrefix + utostr (functionNumber));
260 writePrologue (Out, comment, name);
261 // The LMIMap and BBMIMap are supposed to start with a length word:
262 Out << "\t.word .end_" << name << "-" << name << "\n";
263 bytes.dumpAssembly (Out);
264 writeEpilogue (Out, name);
265}
266
267/// doFinalization - This method writes out two tables, named
268/// FunctionBB and FunctionLI, which map Function numbers (as in
269/// doInitialization) to the BBMIMap and LMIMap tables. (This used to
270/// be the "FunctionInfo" pass.)
271///
272bool MappingInfoAsmPrinter::doFinalization (Module &M) {
273 unsigned f;
274
275 writePrologue(Out, "FUNCTION TO BB MAP", "FunctionBB");
276 f=0;
277 for(Module::iterator FI = M.begin (), FE = M.end (); FE != FI; ++FI) {
278 if (FI->isExternal ())
279 continue;
280 Out << "\t.xword BBMIMap" << f << "\n";
281 ++f;
282 }
283 writeEpilogue(Out, "FunctionBB");
284
285 writePrologue(Out, "FUNCTION TO LI MAP", "FunctionLI");
286 f=0;
287 for(Module::iterator FI = M.begin (), FE = M.end (); FE != FI; ++FI) {
288 if (FI->isExternal ())
289 continue;
290 Out << "\t.xword LMIMap" << f << "\n";
291 ++f;
292 }
293 writeEpilogue(Out, "FunctionLI");
294
295 return false;
296}
297
Brian Gaeked0fde302003-11-11 22:41:34 +0000298} // End llvm namespace
299