1 //===- MappingInfo.cpp - create LLVM info and output to .s file -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains a FunctionPass called MappingInfoAsmPrinter,
11 // which creates a map between MachineBasicBlocks and
12 // MachineInstrs (the "BB TO MI MAP").
14 // As a side effect, it outputs this information as .byte directives to
15 // the assembly file. The output is designed to survive the SPARC assembler,
16 // in order that the Reoptimizer may read it in from memory later when the
17 // binary is loaded. Therefore, it may contain some hidden SPARC-architecture
18 // dependencies. Currently this question is purely theoretical as the
19 // Reoptimizer works only on the SPARC.
21 // The BB TO MI MAP consists of a three-element tuple for each
22 // MachineBasicBlock in a function, ordered from begin() to end() of
23 // its MachineFunction: first, the index of the MachineBasicBlock in the
24 // function; second, the number of the MachineBasicBlock in the function
25 // as computed by create_BB_to_MInumber_Key; and third, the number of
26 // MachineInstrs in the MachineBasicBlock.
28 //===--------------------------------------------------------------------===//
30 #include "MappingInfo.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Module.h"
33 #include "llvm/CodeGen/MachineFunction.h"
34 #include "llvm/ADT/StringExtras.h"
39 class MappingInfoAsmPrinter : public FunctionPass {
42 MappingInfoAsmPrinter(std::ostream &out) : Out(out){}
43 const char *getPassName () const { return "Instr. Mapping Info Collector"; }
44 bool runOnFunction(Function &FI);
45 typedef std::map<const MachineInstr*, unsigned> InstructionKey;
47 MappingInfo *currentOutputMap;
48 std::map<Function *, unsigned> Fkey; // Function # for all functions.
49 bool doInitialization(Module &M);
50 void create_BB_to_MInumber_Key(Function &FI, InstructionKey &key);
51 void buildBBMIMap (Function &FI, MappingInfo &Map);
52 void writeNumber(unsigned X);
53 void selectOutputMap (MappingInfo &m) { currentOutputMap = &m; }
54 void outByte (unsigned char b) { currentOutputMap->outByte (b); }
55 bool doFinalization (Module &M);
59 /// getMappingInfoAsmPrinterPass - Static factory method: returns a new
60 /// MappingInfoAsmPrinter Pass object, which uses OUT as its output
61 /// stream for assembly output.
63 ModulePass *getMappingInfoAsmPrinterPass(std::ostream &out){
64 return new MappingInfoAsmPrinter(out);
67 /// runOnFunction - Builds up the maps for the given function FI and then
68 /// writes them out as assembly code to the current output stream OUT.
69 /// This is an entry point to the pass, called by the PassManager.
71 bool MappingInfoAsmPrinter::runOnFunction(Function &FI) {
72 unsigned num = Fkey[&FI]; // Function number for the current function.
74 // Create an object to hold the map, then build the map.
75 MappingInfo BBMIMap ("BB TO MI MAP", "BBMIMap", num);
76 buildBBMIMap (FI, BBMIMap);
78 // Now, write out the maps.
79 BBMIMap.dumpAssembly (Out);
84 /// writeNumber - Write out the number X as a sequence of .byte
85 /// directives to the current output stream Out. This method performs a
86 /// run-length encoding of the unsigned integers X that are output.
88 void MappingInfoAsmPrinter::writeNumber(unsigned X) {
91 unsigned tmp = X & 127;
99 /// doInitialization - Assign a number to each Function, as follows:
100 /// Functions are numbered starting at 0 at the begin() of each Module.
101 /// Functions which are External (and thus have 0 basic blocks) are not
102 /// inserted into the maps, and are not assigned a number. The side-effect
103 /// of this method is to fill in Fkey to contain the mapping from Functions
104 /// to numbers. (This method is called automatically by the PassManager.)
106 bool MappingInfoAsmPrinter::doInitialization(Module &M) {
108 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
109 if (FI->isExternal()) continue;
113 return false; // Success.
116 /// create_BB_to_MInumber_Key -- Assign a number to each MachineBasicBlock
117 /// in the given Function, as follows: Numbering starts at zero in each
118 /// Function. MachineBasicBlocks are numbered from begin() to end()
119 /// in the Function's corresponding MachineFunction. Each successive
120 /// MachineBasicBlock increments the numbering by the number of instructions
121 /// it contains. The side-effect of this method is to fill in the parameter
122 /// KEY with the mapping of MachineBasicBlocks to numbers. KEY
123 /// is keyed on MachineInstrs, so each MachineBasicBlock is represented
124 /// therein by its first MachineInstr.
126 void MappingInfoAsmPrinter::create_BB_to_MInumber_Key(Function &FI,
127 InstructionKey &key) {
129 MachineFunction &MF = MachineFunction::get(&FI);
130 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
132 MachineBasicBlock &miBB = *BI;
133 key[&miBB.front()] = i;
138 /// buildBBMIMap - Build the BB TO MI MAP for the function FI,
139 /// and save it into the parameter MAP.
141 void MappingInfoAsmPrinter::buildBBMIMap(Function &FI, MappingInfo &Map) {
144 // First build temporary table used to write out the map.
145 InstructionKey BBkey;
146 create_BB_to_MInumber_Key(FI, BBkey);
148 selectOutputMap (Map);
149 MachineFunction &MF = MachineFunction::get(&FI);
150 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
151 BI != BE; ++BI, ++bb) {
152 MachineBasicBlock &miBB = *BI;
154 writeNumber(BBkey[&miBB.front()]);
155 writeNumber(miBB.size());
159 void MappingInfo::byteVector::dumpAssembly (std::ostream &Out) {
160 for (iterator i = begin (), e = end (); i != e; ++i)
161 Out << ".byte " << (int)*i << "\n";
164 static void writePrologue (std::ostream &Out, const std::string &comment,
165 const std::string &symName) {
167 // Output a comment describing the object.
168 Out << "!" << comment << "\n";
169 // Switch the current section to .rodata in the assembly output:
170 Out << "\t.section \".rodata\"\n\t.align 8\n";
171 // Output a global symbol naming the object:
172 Out << "\t.global " << symName << "\n";
173 Out << "\t.type " << symName << ",#object\n";
174 Out << symName << ":\n";
177 static void writeEpilogue (std::ostream &Out, const std::string &symName) {
179 // Output a local symbol marking the end of the object:
180 Out << ".end_" << symName << ":\n";
181 // Output size directive giving the size of the object:
182 Out << "\t.size " << symName << ", .end_" << symName << "-" << symName
186 void MappingInfo::dumpAssembly (std::ostream &Out) {
187 const std::string &name (symbolPrefix + utostr (functionNumber));
188 writePrologue (Out, comment, name);
189 // The LMIMap and BBMIMap are supposed to start with a length word:
190 Out << "\t.word .end_" << name << "-" << name << "\n";
191 bytes.dumpAssembly (Out);
192 writeEpilogue (Out, name);
195 /// doFinalization - This method writes out two tables, named
196 /// FunctionBB and FunctionLI, which map Function numbers (as in
197 /// doInitialization) to the BBMIMap and LMIMap tables. (This used to
198 /// be the "FunctionInfo" pass.)
200 bool MappingInfoAsmPrinter::doFinalization (Module &M) {
203 writePrologue(Out, "FUNCTION TO BB MAP", "FunctionBB");
205 for(Module::iterator FI = M.begin (), FE = M.end (); FE != FI; ++FI) {
206 if (FI->isExternal ())
208 Out << "\t.xword BBMIMap" << f << "\n";
211 writeEpilogue(Out, "FunctionBB");
216 } // End llvm namespace