1 //===- MappingInfo.cpp - create LLVM info and output to .s file ---------===//
3 // 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
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.
15 //===--------------------------------------------------------------------===//
17 #include "llvm/Reoptimizer/Mapping/MappingInfo.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Module.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineCodeForInstruction.h"
27 class getMappingInfoForFunction : public FunctionPass {
30 getMappingInfoForFunction(std::ostream &out) : Out(out){}
31 const char* getPassName() const{return "Sparc MappingInformation";}
32 bool runOnFunction(Function &FI);
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 void writePrologue(const std::string &comment,
38 const std::string &symbolPrefix, unsigned num);
39 void writeEpilogue(const std::string &symbolPrefix, unsigned num);
41 bool doInitialization(Module &M);
42 void create_BB_to_MInumber_Key(Function &FI);
43 void create_MI_to_number_Key(Function &FI);
44 void writeBBToMImap(Function &FI, unsigned num);
45 void writeLLVMToMImap(Function &FI, unsigned num);
46 unsigned writeNumber(unsigned X);
50 /// MappingInfoForFunction -- Static factory method: returns a new
51 /// getMappingInfoForFunction Pass object, which uses OUT as its
52 /// output stream for assembly output.
53 Pass *MappingInfoForFunction(std::ostream &out){
54 return (new getMappingInfoForFunction(out));
57 /// 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.
59 /// This is an entry point to the pass, called by the PassManager.
60 bool getMappingInfoForFunction::runOnFunction(Function &FI) {
61 // First we build temporary tables used to write out the maps.
62 create_BB_to_MInumber_Key(FI);
63 create_MI_to_number_Key(FI);
64 unsigned num = Fkey[&FI]; // Function number for the current function.
66 // Now, write out the maps.
67 writeBBToMImap(FI, num);
68 writeLLVMToMImap(FI, num);
73 /// 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.
78 void getMappingInfoForFunction::writePrologue(const std::string &comment,
79 const std::string &symbolPrefix,
82 Out << "!" << comment << "\n";
84 Out << "\t.section \".rodata\"\n\t.align 8\n";
85 // 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";
90 Out << "\t.word .end_" << symbolPrefix << num << "-"
91 << symbolPrefix << num << "\n";
94 /// 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.
97 void getMappingInfoForFunction::writeEpilogue(const std::string &symbolPrefix,
99 // Local symbol ending the map:
100 Out << ".end_" << symbolPrefix << num << ":\n";
102 Out << "\t.size " << symbolPrefix << num << ", .end_"
103 << symbolPrefix << num << "-" << symbolPrefix
104 << num << "\n\n\n\n";
107 /// writeNumber -- Write out the number X as a sequence of .byte
108 /// directives to the current output stream Out. This method performs a
109 /// run-length encoding of the unsigned integers X that are output.
110 unsigned getMappingInfoForFunction::writeNumber(unsigned X) {
113 unsigned tmp = X & 127;
116 Out << "\t.byte " << tmp << "\n";
122 /// 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.)
128 bool getMappingInfoForFunction::doInitialization(Module &M) {
130 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
131 if (FI->isExternal()) continue;
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.
147 void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI) {
149 MachineFunction &MF = MachineFunction::get(&FI);
150 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
152 MachineBasicBlock &miBB = *BI;
158 /// 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.
165 void 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;
170 for(MachineBasicBlock::iterator miI=miBB.begin(), miE=miBB.end();
171 miI!=miE; ++miI, ++j) {
177 /// 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.
184 void getMappingInfoForFunction::writeBBToMImap(Function &FI, unsigned num){
186 const std::string MapComment = "BB TO MI MAP";
187 const std::string MapSymbolPrefix = "BBMIMap";
188 writePrologue(MapComment, MapSymbolPrefix, num);
189 MachineFunction &MF = MachineFunction::get(&FI);
190 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
191 BI != BE; ++BI, ++bb) {
192 MachineBasicBlock &miBB = *BI;
194 writeNumber(BBkey[miBB[0]]);
195 writeNumber(miBB.size());
197 writeEpilogue(MapSymbolPrefix, num);
200 /// 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.
212 void getMappingInfoForFunction::writeLLVMToMImap(Function &FI, unsigned num) {
214 const std::string MapComment = "LLVM I TO MI MAP";
215 const std::string MapSymbolPrefix = "LMIMap";
216 writePrologue(MapComment, MapSymbolPrefix, num);
217 for (Function::iterator BI = FI.begin(), BE = FI.end();
218 BI != BE; ++BI, ++bb) {
221 writeNumber(BI->size());
222 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
224 MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II);
226 writeNumber(miI.size());
227 for (MachineCodeForInstruction::iterator miII = miI.begin(),
228 miIE = miI.end(); miII != miIE; ++miII) {
229 writeNumber(MIkey[*miII]);
233 writeEpilogue(MapSymbolPrefix, num);