LLVM API Documentation

MappingInfo.cpp

Go to the documentation of this file.
00001 //===- MappingInfo.cpp - create LLVM info and output to .s file -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains a FunctionPass called MappingInfoAsmPrinter,
00011 // which creates a map between MachineBasicBlocks and
00012 // MachineInstrs (the "BB TO MI MAP").
00013 //
00014 // As a side effect, it outputs this information as .byte directives to
00015 // the assembly file. The output is designed to survive the SPARC assembler,
00016 // in order that the Reoptimizer may read it in from memory later when the
00017 // binary is loaded. Therefore, it may contain some hidden SPARC-architecture
00018 // dependencies. Currently this question is purely theoretical as the
00019 // Reoptimizer works only on the SPARC.
00020 //
00021 // The BB TO MI MAP consists of a three-element tuple for each
00022 // MachineBasicBlock in a function, ordered from begin() to end() of
00023 // its MachineFunction: first, the index of the MachineBasicBlock in the
00024 // function; second, the number of the MachineBasicBlock in the function
00025 // as computed by create_BB_to_MInumber_Key; and third, the number of
00026 // MachineInstrs in the MachineBasicBlock.
00027 //
00028 //===--------------------------------------------------------------------===//
00029 
00030 #include "MappingInfo.h"
00031 #include "llvm/Pass.h"
00032 #include "llvm/Module.h"
00033 #include "llvm/CodeGen/MachineFunction.h"
00034 #include "llvm/ADT/StringExtras.h"
00035 
00036 namespace llvm {
00037 
00038 namespace {
00039   class MappingInfoAsmPrinter : public FunctionPass {
00040     std::ostream &Out;
00041   public:
00042     MappingInfoAsmPrinter(std::ostream &out) : Out(out){}
00043     const char *getPassName () const { return "Instr. Mapping Info Collector"; }
00044     bool runOnFunction(Function &FI);
00045     typedef std::map<const MachineInstr*, unsigned> InstructionKey;
00046   private:
00047     MappingInfo *currentOutputMap;
00048     std::map<Function *, unsigned> Fkey; // Function # for all functions.
00049     bool doInitialization(Module &M);
00050     void create_BB_to_MInumber_Key(Function &FI, InstructionKey &key);
00051     void buildBBMIMap (Function &FI, MappingInfo &Map);
00052     void writeNumber(unsigned X);
00053     void selectOutputMap (MappingInfo &m) { currentOutputMap = &m; }
00054     void outByte (unsigned char b) { currentOutputMap->outByte (b); }
00055     bool doFinalization (Module &M);
00056   };
00057 }
00058 
00059 /// getMappingInfoAsmPrinterPass - Static factory method: returns a new
00060 /// MappingInfoAsmPrinter Pass object, which uses OUT as its output
00061 /// stream for assembly output.
00062 ///
00063 ModulePass *getMappingInfoAsmPrinterPass(std::ostream &out){
00064   return new MappingInfoAsmPrinter(out);
00065 }
00066 
00067 /// runOnFunction - Builds up the maps for the given function FI and then
00068 /// writes them out as assembly code to the current output stream OUT.
00069 /// This is an entry point to the pass, called by the PassManager.
00070 ///
00071 bool MappingInfoAsmPrinter::runOnFunction(Function &FI) {
00072   unsigned num = Fkey[&FI]; // Function number for the current function.
00073 
00074   // Create an object to hold the map, then build the map.
00075   MappingInfo BBMIMap ("BB TO MI MAP", "BBMIMap", num);
00076   buildBBMIMap (FI, BBMIMap);
00077 
00078   // Now, write out the maps.
00079   BBMIMap.dumpAssembly (Out);
00080 
00081   return false;
00082 }
00083 
00084 /// writeNumber - Write out the number X as a sequence of .byte
00085 /// directives to the current output stream Out. This method performs a
00086 /// run-length encoding of the unsigned integers X that are output.
00087 ///
00088 void MappingInfoAsmPrinter::writeNumber(unsigned X) {
00089   unsigned i=0;
00090   do {
00091     unsigned tmp = X & 127;
00092     X >>= 7;
00093     if (X) tmp |= 128;
00094     outByte (tmp);
00095     ++i;
00096   } while(X);
00097 }
00098 
00099 /// doInitialization - Assign a number to each Function, as follows:
00100 /// Functions are numbered starting at 0 at the begin() of each Module.
00101 /// Functions which are External (and thus have 0 basic blocks) are not
00102 /// inserted into the maps, and are not assigned a number.  The side-effect
00103 /// of this method is to fill in Fkey to contain the mapping from Functions
00104 /// to numbers. (This method is called automatically by the PassManager.)
00105 ///
00106 bool MappingInfoAsmPrinter::doInitialization(Module &M) {
00107   unsigned i = 0;
00108   for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
00109     if (FI->isExternal()) continue;
00110     Fkey[FI] = i;
00111     ++i;
00112   }
00113   return false; // Success.
00114 }
00115 
00116 /// create_BB_to_MInumber_Key -- Assign a number to each MachineBasicBlock
00117 /// in the given Function, as follows: Numbering starts at zero in each
00118 /// Function. MachineBasicBlocks are numbered from begin() to end()
00119 /// in the Function's corresponding MachineFunction. Each successive
00120 /// MachineBasicBlock increments the numbering by the number of instructions
00121 /// it contains. The side-effect of this method is to fill in the parameter
00122 /// KEY with the mapping of MachineBasicBlocks to numbers. KEY
00123 /// is keyed on MachineInstrs, so each MachineBasicBlock is represented
00124 /// therein by its first MachineInstr.
00125 ///
00126 void MappingInfoAsmPrinter::create_BB_to_MInumber_Key(Function &FI,
00127                                                       InstructionKey &key) {
00128   unsigned i = 0;
00129   MachineFunction &MF = MachineFunction::get(&FI);
00130   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
00131        BI != BE; ++BI) {
00132     MachineBasicBlock &miBB = *BI;
00133     key[&miBB.front()] = i;
00134     i = i+(miBB.size());
00135   }
00136 }
00137 
00138 /// buildBBMIMap - Build the BB TO MI MAP for the function FI,
00139 /// and save it into the parameter MAP.
00140 ///
00141 void MappingInfoAsmPrinter::buildBBMIMap(Function &FI, MappingInfo &Map) {
00142   unsigned bb = 0;
00143 
00144   // First build temporary table used to write out the map.
00145   InstructionKey BBkey;
00146   create_BB_to_MInumber_Key(FI, BBkey);
00147 
00148   selectOutputMap (Map);
00149   MachineFunction &MF = MachineFunction::get(&FI);
00150   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
00151        BI != BE; ++BI, ++bb) {
00152     MachineBasicBlock &miBB = *BI;
00153     writeNumber(bb);
00154     writeNumber(BBkey[&miBB.front()]);
00155     writeNumber(miBB.size());
00156   }
00157 }
00158 
00159 void MappingInfo::byteVector::dumpAssembly (std::ostream &Out) {
00160   for (iterator i = begin (), e = end (); i != e; ++i)
00161         Out << ".byte " << (int)*i << "\n";
00162 }
00163 
00164 static void writePrologue (std::ostream &Out, const std::string &comment,
00165                            const std::string &symName) {
00166   // Prologue:
00167   // Output a comment describing the object.
00168   Out << "!" << comment << "\n";
00169   // Switch the current section to .rodata in the assembly output:
00170   Out << "\t.section \".rodata\"\n\t.align 8\n";
00171   // Output a global symbol naming the object:
00172   Out << "\t.global " << symName << "\n";
00173   Out << "\t.type " << symName << ",#object\n";
00174   Out << symName << ":\n";
00175 }
00176 
00177 static void writeEpilogue (std::ostream &Out, const std::string &symName) {
00178   // Epilogue:
00179   // Output a local symbol marking the end of the object:
00180   Out << ".end_" << symName << ":\n";
00181   // Output size directive giving the size of the object:
00182   Out << "\t.size " << symName << ", .end_" << symName << "-" << symName
00183       << "\n";
00184 }
00185 
00186 void MappingInfo::dumpAssembly (std::ostream &Out) {
00187   const std::string &name (symbolPrefix + utostr (functionNumber));
00188   writePrologue (Out, comment, name);
00189   // The LMIMap and BBMIMap are supposed to start with a length word:
00190   Out << "\t.word .end_" << name << "-" << name << "\n";
00191   bytes.dumpAssembly (Out);
00192   writeEpilogue (Out, name);
00193 }
00194 
00195 /// doFinalization - This method writes out two tables, named
00196 /// FunctionBB and FunctionLI, which map Function numbers (as in
00197 /// doInitialization) to the BBMIMap and LMIMap tables. (This used to
00198 /// be the "FunctionInfo" pass.)
00199 ///
00200 bool MappingInfoAsmPrinter::doFinalization (Module &M) {
00201   unsigned f;
00202 
00203   writePrologue(Out, "FUNCTION TO BB MAP", "FunctionBB");
00204   f=0;
00205   for(Module::iterator FI = M.begin (), FE = M.end (); FE != FI; ++FI) {
00206     if (FI->isExternal ())
00207       continue;
00208     Out << "\t.xword BBMIMap" << f << "\n";
00209     ++f;
00210   }
00211   writeEpilogue(Out, "FunctionBB");
00212 
00213   return false;
00214 }
00215 
00216 } // End llvm namespace