Adjust files for move of mapping info stuff into the Sparc directory
[oota-llvm.git] / lib / Target / SparcV9 / MappingInfo.cpp
1 //===- MappingInfo.cpp - create LLVM info and output to .s file ---------===//
2 //
3 // This file contains a FunctionPass called MappingInfo,
4 // which creates two maps: one between LLVM Instructions and MachineInstrs
5 // (the "LLVM I TO MI MAP"), and another between MachineBasicBlocks and
6 // MachineInstrs (the "BB TO 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.
14 //
15 // The LLVM I TO MI MAP consists of a set of information for each
16 // BasicBlock in a Function, ordered from begin() to end(). The information
17 // for a BasicBlock consists of
18 //  1) its (0-based) index in the Function,
19 //  2) the number of LLVM Instructions it contains, and
20 //  3) information for each Instruction, in sequence from the begin()
21 //     to the end() of the BasicBlock. The information for an Instruction
22 //     consists of
23 //     1) its (0-based) index in the BasicBlock,
24 //     2) the number of MachineInstrs that correspond to that Instruction
25 //        (as reported by MachineCodeForInstruction), and
26 //     3) the MachineInstr number calculated by create_MI_to_number_Key,
27 //        for each of the MachineInstrs that correspond to that Instruction.
28 //
29 // The BB TO MI MAP consists of a three-element tuple for each
30 // MachineBasicBlock in a function, ordered from begin() to end() of
31 // its MachineFunction: first, the index of the MachineBasicBlock in the
32 // function; second, the number of the MachineBasicBlock in the function
33 // as computed by create_BB_to_MInumber_Key; and third, the number of
34 // MachineInstrs in the MachineBasicBlock.
35 //
36 //===--------------------------------------------------------------------===//
37
38 #include "MappingInfo.h"
39 #include "llvm/Pass.h"
40 #include "llvm/Module.h"
41 #include "llvm/CodeGen/MachineFunction.h"
42 #include "llvm/CodeGen/MachineCodeForInstruction.h"
43
44 namespace {
45   class MappingInfoCollector : public FunctionPass { 
46     std::ostream &Out;
47   public:
48     MappingInfoCollector(std::ostream &out) : Out(out){}
49     const char *getPassName () const { return "Instr. Mapping Info Collector"; }
50     bool runOnFunction(Function &FI);
51     typedef std::map<const MachineInstr*, unsigned> InstructionKey;
52   private:
53     MappingInfo *currentOutputMap;
54     std::map<Function *, unsigned> Fkey; // Function # for all functions.
55     bool doInitialization(Module &M);
56     void create_BB_to_MInumber_Key(Function &FI, InstructionKey &key);
57     void create_MI_to_number_Key(Function &FI, InstructionKey &key);
58     void buildBBMIMap (Function &FI, MappingInfo &Map);
59     void buildLMIMap (Function &FI, MappingInfo &Map);
60     void writeNumber(unsigned X);
61     void selectOutputMap (MappingInfo &m) { currentOutputMap = &m; }
62     void outByte (unsigned char b) { currentOutputMap->outByte (b); }
63   };
64 }
65
66 /// getMappingInfoCollector -- Static factory method: returns a new
67 /// MappingInfoCollector Pass object, which uses OUT as its
68 /// output stream for assembly output. 
69 Pass *getMappingInfoCollector(std::ostream &out){
70   return (new MappingInfoCollector(out));
71 }
72
73 /// runOnFunction -- Builds up the maps for the given function FI and then
74 /// writes them out as assembly code to the current output stream OUT.
75 /// This is an entry point to the pass, called by the PassManager.
76 bool MappingInfoCollector::runOnFunction(Function &FI) {
77   unsigned num = Fkey[&FI]; // Function number for the current function.
78
79   // Create objects to hold the maps.
80   MappingInfo LMIMap ("LLVM I TO MI MAP", "LMIMap", num);
81   MappingInfo BBMIMap ("BB TO MI MAP", "BBMIMap", num);
82
83   // Now, build the maps.
84   buildLMIMap (FI, LMIMap);
85   buildBBMIMap (FI, BBMIMap);
86
87   // Now, write out the maps.
88   LMIMap.dumpAssembly (Out);
89   BBMIMap.dumpAssembly (Out);
90
91   return false; 
92 }  
93
94 /// writeNumber -- Write out the number X as a sequence of .byte
95 /// directives to the current output stream Out. This method performs a
96 /// run-length encoding of the unsigned integers X that are output.
97 void MappingInfoCollector::writeNumber(unsigned X) {
98   unsigned i=0;
99   do {
100     unsigned tmp = X & 127;
101     X >>= 7;
102     if (X) tmp |= 128;
103     outByte (tmp);
104     ++i;
105   } while(X);
106 }
107
108 /// doInitialization -- Assign a number to each Function, as follows:
109 /// Functions are numbered starting at 0 at the begin() of each Module.
110 /// Functions which are External (and thus have 0 basic blocks) are not
111 /// inserted into the maps, and are not assigned a number.  The side-effect
112 /// of this method is to fill in Fkey to contain the mapping from Functions
113 /// to numbers. (This method is called automatically by the PassManager.)
114 bool MappingInfoCollector::doInitialization(Module &M) {
115   unsigned i = 0;
116   for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
117     if (FI->isExternal()) continue;
118     Fkey[FI] = i;
119     ++i;
120   }
121   return false; // Success.
122 }
123
124 /// create_BB_to_MInumber_Key -- Assign a number to each MachineBasicBlock
125 /// in the given Function, as follows: Numbering starts at zero in each
126 /// Function. MachineBasicBlocks are numbered from begin() to end()
127 /// in the Function's corresponding MachineFunction. Each successive
128 /// MachineBasicBlock increments the numbering by the number of instructions
129 /// it contains. The side-effect of this method is to fill in the paramete
130 /// KEY with the mapping of MachineBasicBlocks to numbers. KEY
131 /// is keyed on MachineInstrs, so each MachineBasicBlock is represented
132 /// therein by its first MachineInstr.
133 void MappingInfoCollector::create_BB_to_MInumber_Key(Function &FI,
134                                                      InstructionKey &key) {
135   unsigned i = 0;
136   MachineFunction &MF = MachineFunction::get(&FI);
137   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
138        BI != BE; ++BI) {
139     MachineBasicBlock &miBB = *BI;
140     key[miBB[0]] = i;
141     i = i+(miBB.size());
142   }
143 }
144
145 /// create_MI_to_number_Key -- Assign a number to each MachineInstr
146 /// in the given Function with respect to its enclosing MachineBasicBlock, as
147 /// follows: Numberings start at 0 in each MachineBasicBlock. MachineInstrs
148 /// are numbered from begin() to end() in their MachineBasicBlock. Each
149 /// MachineInstr is numbered, then the numbering is incremented by 1. The
150 /// side-effect of this method is to fill in the parameter KEY
151 /// with the mapping from MachineInstrs to numbers.
152 void MappingInfoCollector::create_MI_to_number_Key(Function &FI,
153                                                    InstructionKey &key) {
154   MachineFunction &MF = MachineFunction::get(&FI);
155   for (MachineFunction::iterator BI=MF.begin(), BE=MF.end(); BI != BE; ++BI) {
156     MachineBasicBlock &miBB = *BI;
157     unsigned j = 0;
158     for(MachineBasicBlock::iterator miI = miBB.begin(), miE = miBB.end();
159         miI != miE; ++miI, ++j) {
160       key[*miI] = j;
161     }
162   }
163 }
164
165 /// buildBBMIMap -- Build the BB TO MI MAP for the function FI,
166 /// and save it into the parameter MAP.
167 void MappingInfoCollector::buildBBMIMap(Function &FI, MappingInfo &Map) {
168   unsigned bb = 0;
169
170   // First build temporary table used to write out the map.
171   InstructionKey BBkey;
172   create_BB_to_MInumber_Key(FI, BBkey);
173
174   selectOutputMap (Map);
175   MachineFunction &MF = MachineFunction::get(&FI);  
176   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
177        BI != BE; ++BI, ++bb) {
178     MachineBasicBlock &miBB = *BI;
179     writeNumber(bb);
180     writeNumber(BBkey[miBB[0]]);
181     writeNumber(miBB.size());
182   }
183 }
184
185 /// buildLMIMap -- Build the LLVM I TO MI MAP for the function FI,
186 /// and save it into the parameter MAP.
187 void MappingInfoCollector::buildLMIMap(Function &FI, MappingInfo &Map) {
188   unsigned bb = 0;
189   // First build temporary table used to write out the map.
190   InstructionKey MIkey;
191   create_MI_to_number_Key(FI, MIkey);
192
193   selectOutputMap (Map);
194   for (Function::iterator BI = FI.begin(), BE = FI.end(); 
195        BI != BE; ++BI, ++bb) {
196     unsigned li = 0;
197     writeNumber(bb);
198     writeNumber(BI->size());
199     for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
200          ++II, ++li) {
201       MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II);
202       writeNumber(li);
203       writeNumber(miI.size());
204       for (MachineCodeForInstruction::iterator miII = miI.begin(), 
205            miIE = miI.end(); miII != miIE; ++miII) {
206              writeNumber(MIkey[*miII]);
207       }
208     }
209   } 
210 }
211
212 void MappingInfo::byteVector::dumpAssembly (std::ostream &Out) {
213   for (iterator i = begin (), e = end (); i != e; ++i)
214         Out << ".byte " << (int)*i << "\n";
215 }
216
217 void MappingInfo::dumpAssembly (std::ostream &Out) {
218   // Prologue:
219   // Output a comment describing the map.
220   Out << "!" << comment << "\n";   
221   // Switch the current section to .rodata in the assembly output:
222   Out << "\t.section \".rodata\"\n\t.align 8\n";  
223   // Output a global symbol naming the map:
224   Out << "\t.global " << symbolPrefix << functionNumber << "\n";    
225   Out << "\t.type " << symbolPrefix << functionNumber << ",#object\n"; 
226   Out << symbolPrefix << functionNumber << ":\n"; 
227   // Output a word containing the length of the map:
228   Out << "\t.word .end_" << symbolPrefix << functionNumber << "-"
229       << symbolPrefix << functionNumber << "\n";
230
231   // Output the map data itself:
232   bytes.dumpAssembly (Out);
233
234   // Epilogue:
235   // Output a local symbol marking the end of the map:
236   Out << ".end_" << symbolPrefix << functionNumber << ":\n";    
237   // Output size directive giving the size of the map:
238   Out << "\t.size " << symbolPrefix << functionNumber << ", .end_" 
239       << symbolPrefix << functionNumber << "-" << symbolPrefix 
240       << functionNumber << "\n\n";
241 }