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 void 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 /// outByte -- NOT DONE YET.
108 void outByte (unsigned char b) {
109 //Out << "\t.byte " << tmp << "\n";
113 /// writeNumber -- Write out the number X as a sequence of .byte
114 /// directives to the current output stream Out. This method performs a
115 /// run-length encoding of the unsigned integers X that are output.
116 void getMappingInfoForFunction::writeNumber(unsigned X) {
119 unsigned tmp = X & 127;
127 /// 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.)
133 bool getMappingInfoForFunction::doInitialization(Module &M) {
135 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
136 if (FI->isExternal()) continue;
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.
152 void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI) {
154 MachineFunction &MF = MachineFunction::get(&FI);
155 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
157 MachineBasicBlock &miBB = *BI;
163 /// 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.
170 void 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;
175 for(MachineBasicBlock::iterator miI=miBB.begin(), miE=miBB.end();
176 miI!=miE; ++miI, ++j) {
182 /// 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.
189 void getMappingInfoForFunction::writeBBToMImap(Function &FI, unsigned num){
191 const std::string MapComment = "BB TO MI MAP";
192 const std::string MapSymbolPrefix = "BBMIMap";
193 writePrologue(MapComment, MapSymbolPrefix, num);
194 MachineFunction &MF = MachineFunction::get(&FI);
195 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
196 BI != BE; ++BI, ++bb) {
197 MachineBasicBlock &miBB = *BI;
199 writeNumber(BBkey[miBB[0]]);
200 writeNumber(miBB.size());
202 writeEpilogue(MapSymbolPrefix, num);
205 /// 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.
217 void getMappingInfoForFunction::writeLLVMToMImap(Function &FI, unsigned num) {
219 const std::string MapComment = "LLVM I TO MI MAP";
220 const std::string MapSymbolPrefix = "LMIMap";
221 writePrologue(MapComment, MapSymbolPrefix, num);
222 for (Function::iterator BI = FI.begin(), BE = FI.end();
223 BI != BE; ++BI, ++bb) {
226 writeNumber(BI->size());
227 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
229 MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II);
231 writeNumber(miI.size());
232 for (MachineCodeForInstruction::iterator miII = miI.begin(),
233 miIE = miI.end(); miII != miIE; ++miII) {
234 writeNumber(MIkey[*miII]);
238 writeEpilogue(MapSymbolPrefix, num);