Support multiple ValueTypes per RegisterClass, needed for upcoming vector
[oota-llvm.git] / lib / Target / SparcV9 / MappingInfo.cpp
1 //===- MappingInfo.cpp - create LLVM info and output to .s file -----------===//
2 //
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 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a FunctionPass called MappingInfoAsmPrinter,
11 // which creates a map between MachineBasicBlocks and
12 // MachineInstrs (the "BB TO MI MAP").
13 //
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.
20 //
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.
27 //
28 //===--------------------------------------------------------------------===//
29
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"
35
36 namespace llvm {
37
38 namespace {
39   class MappingInfoAsmPrinter : public FunctionPass {
40     std::ostream &Out;
41   public:
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;
46   private:
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);
56   };
57 }
58
59 /// getMappingInfoAsmPrinterPass - Static factory method: returns a new
60 /// MappingInfoAsmPrinter Pass object, which uses OUT as its output
61 /// stream for assembly output.
62 ///
63 ModulePass *getMappingInfoAsmPrinterPass(std::ostream &out){
64   return new MappingInfoAsmPrinter(out);
65 }
66
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.
70 ///
71 bool MappingInfoAsmPrinter::runOnFunction(Function &FI) {
72   unsigned num = Fkey[&FI]; // Function number for the current function.
73
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);
77
78   // Now, write out the maps.
79   BBMIMap.dumpAssembly (Out);
80
81   return false;
82 }
83
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.
87 ///
88 void MappingInfoAsmPrinter::writeNumber(unsigned X) {
89   unsigned i=0;
90   do {
91     unsigned tmp = X & 127;
92     X >>= 7;
93     if (X) tmp |= 128;
94     outByte (tmp);
95     ++i;
96   } while(X);
97 }
98
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.)
105 ///
106 bool MappingInfoAsmPrinter::doInitialization(Module &M) {
107   unsigned i = 0;
108   for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
109     if (FI->isExternal()) continue;
110     Fkey[FI] = i;
111     ++i;
112   }
113   return false; // Success.
114 }
115
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.
125 ///
126 void MappingInfoAsmPrinter::create_BB_to_MInumber_Key(Function &FI,
127                                                       InstructionKey &key) {
128   unsigned i = 0;
129   MachineFunction &MF = MachineFunction::get(&FI);
130   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
131        BI != BE; ++BI) {
132     MachineBasicBlock &miBB = *BI;
133     key[&miBB.front()] = i;
134     i = i+(miBB.size());
135   }
136 }
137
138 /// buildBBMIMap - Build the BB TO MI MAP for the function FI,
139 /// and save it into the parameter MAP.
140 ///
141 void MappingInfoAsmPrinter::buildBBMIMap(Function &FI, MappingInfo &Map) {
142   unsigned bb = 0;
143
144   // First build temporary table used to write out the map.
145   InstructionKey BBkey;
146   create_BB_to_MInumber_Key(FI, BBkey);
147
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;
153     writeNumber(bb);
154     writeNumber(BBkey[&miBB.front()]);
155     writeNumber(miBB.size());
156   }
157 }
158
159 void MappingInfo::byteVector::dumpAssembly (std::ostream &Out) {
160   for (iterator i = begin (), e = end (); i != e; ++i)
161         Out << ".byte " << (int)*i << "\n";
162 }
163
164 static void writePrologue (std::ostream &Out, const std::string &comment,
165                            const std::string &symName) {
166   // Prologue:
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";
175 }
176
177 static void writeEpilogue (std::ostream &Out, const std::string &symName) {
178   // Epilogue:
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
183       << "\n";
184 }
185
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);
193 }
194
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.)
199 ///
200 bool MappingInfoAsmPrinter::doFinalization (Module &M) {
201   unsigned f;
202
203   writePrologue(Out, "FUNCTION TO BB MAP", "FunctionBB");
204   f=0;
205   for(Module::iterator FI = M.begin (), FE = M.end (); FE != FI; ++FI) {
206     if (FI->isExternal ())
207       continue;
208     Out << "\t.xword BBMIMap" << f << "\n";
209     ++f;
210   }
211   writeEpilogue(Out, "FunctionBB");
212
213   return false;
214 }
215
216 } // End llvm namespace