2004-04-25 09:04:49 +02:00
|
|
|
//===- MappingInfo.cpp - create LLVM info and output to .s file -----------===//
|
2005-04-22 01:30:14 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 01:30:14 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-08-28 00:47:33 +02:00
|
|
|
//
|
2003-09-18 19:37:25 +02:00
|
|
|
// This file contains a FunctionPass called MappingInfoAsmPrinter,
|
2004-10-19 07:15:21 +02:00
|
|
|
// which creates a map between MachineBasicBlocks and
|
2003-06-05 00:07:12 +02:00
|
|
|
// MachineInstrs (the "BB TO MI MAP").
|
2003-06-03 09:56:05 +02:00
|
|
|
//
|
|
|
|
// As a side effect, it outputs this information as .byte directives to
|
|
|
|
// the assembly file. The output is designed to survive the SPARC assembler,
|
|
|
|
// in order that the Reoptimizer may read it in from memory later when the
|
|
|
|
// binary is loaded. Therefore, it may contain some hidden SPARC-architecture
|
|
|
|
// dependencies. Currently this question is purely theoretical as the
|
|
|
|
// Reoptimizer works only on the SPARC.
|
2002-08-28 00:47:33 +02:00
|
|
|
//
|
2003-06-05 00:07:12 +02:00
|
|
|
// The BB TO MI MAP consists of a three-element tuple for each
|
|
|
|
// MachineBasicBlock in a function, ordered from begin() to end() of
|
|
|
|
// its MachineFunction: first, the index of the MachineBasicBlock in the
|
|
|
|
// function; second, the number of the MachineBasicBlock in the function
|
|
|
|
// as computed by create_BB_to_MInumber_Key; and third, the number of
|
|
|
|
// MachineInstrs in the MachineBasicBlock.
|
|
|
|
//
|
2002-08-28 00:47:33 +02:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2003-08-13 04:38:16 +02:00
|
|
|
#include "MappingInfo.h"
|
2002-08-28 00:47:33 +02:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Module.h"
|
2002-10-28 21:00:31 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2002-08-28 00:47:33 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-08-28 00:47:33 +02:00
|
|
|
namespace {
|
2005-04-22 01:30:14 +02:00
|
|
|
class MappingInfoAsmPrinter : public FunctionPass {
|
2002-08-28 00:47:33 +02:00
|
|
|
std::ostream &Out;
|
|
|
|
public:
|
2003-09-18 19:37:25 +02:00
|
|
|
MappingInfoAsmPrinter(std::ostream &out) : Out(out){}
|
2003-06-05 00:07:12 +02:00
|
|
|
const char *getPassName () const { return "Instr. Mapping Info Collector"; }
|
2002-08-28 00:47:33 +02:00
|
|
|
bool runOnFunction(Function &FI);
|
2003-06-05 00:07:12 +02:00
|
|
|
typedef std::map<const MachineInstr*, unsigned> InstructionKey;
|
2002-08-28 00:47:33 +02:00
|
|
|
private:
|
2003-06-05 00:07:12 +02:00
|
|
|
MappingInfo *currentOutputMap;
|
|
|
|
std::map<Function *, unsigned> Fkey; // Function # for all functions.
|
2002-08-28 00:47:33 +02:00
|
|
|
bool doInitialization(Module &M);
|
2003-06-05 00:07:12 +02:00
|
|
|
void create_BB_to_MInumber_Key(Function &FI, InstructionKey &key);
|
|
|
|
void buildBBMIMap (Function &FI, MappingInfo &Map);
|
|
|
|
void writeNumber(unsigned X);
|
|
|
|
void selectOutputMap (MappingInfo &m) { currentOutputMap = &m; }
|
|
|
|
void outByte (unsigned char b) { currentOutputMap->outByte (b); }
|
2003-09-18 19:37:25 +02:00
|
|
|
bool doFinalization (Module &M);
|
2002-08-28 00:47:33 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-09-18 19:37:25 +02:00
|
|
|
/// getMappingInfoAsmPrinterPass - Static factory method: returns a new
|
|
|
|
/// MappingInfoAsmPrinter Pass object, which uses OUT as its output
|
|
|
|
/// stream for assembly output.
|
|
|
|
///
|
2004-09-20 06:48:05 +02:00
|
|
|
ModulePass *getMappingInfoAsmPrinterPass(std::ostream &out){
|
|
|
|
return new MappingInfoAsmPrinter(out);
|
2002-08-28 00:47:33 +02:00
|
|
|
}
|
|
|
|
|
2003-09-18 19:37:25 +02:00
|
|
|
/// runOnFunction - Builds up the maps for the given function FI and then
|
2003-06-03 21:30:15 +02:00
|
|
|
/// writes them out as assembly code to the current output stream OUT.
|
2003-06-03 09:56:05 +02:00
|
|
|
/// This is an entry point to the pass, called by the PassManager.
|
2003-09-18 19:37:25 +02:00
|
|
|
///
|
|
|
|
bool MappingInfoAsmPrinter::runOnFunction(Function &FI) {
|
2003-06-03 21:30:15 +02:00
|
|
|
unsigned num = Fkey[&FI]; // Function number for the current function.
|
|
|
|
|
2004-10-19 07:15:21 +02:00
|
|
|
// Create an object to hold the map, then build the map.
|
2003-06-05 00:07:12 +02:00
|
|
|
MappingInfo BBMIMap ("BB TO MI MAP", "BBMIMap", num);
|
|
|
|
buildBBMIMap (FI, BBMIMap);
|
|
|
|
|
2003-06-03 21:30:15 +02:00
|
|
|
// Now, write out the maps.
|
2003-06-05 00:07:12 +02:00
|
|
|
BBMIMap.dumpAssembly (Out);
|
2002-08-28 00:47:33 +02:00
|
|
|
|
2005-04-22 01:30:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
2002-08-28 00:47:33 +02:00
|
|
|
|
2003-09-18 19:37:25 +02:00
|
|
|
/// writeNumber - Write out the number X as a sequence of .byte
|
2003-06-03 21:30:15 +02:00
|
|
|
/// directives to the current output stream Out. This method performs a
|
|
|
|
/// run-length encoding of the unsigned integers X that are output.
|
2003-09-18 19:37:25 +02:00
|
|
|
///
|
|
|
|
void MappingInfoAsmPrinter::writeNumber(unsigned X) {
|
2002-08-28 00:47:33 +02:00
|
|
|
unsigned i=0;
|
|
|
|
do {
|
|
|
|
unsigned tmp = X & 127;
|
|
|
|
X >>= 7;
|
|
|
|
if (X) tmp |= 128;
|
2003-06-05 00:07:12 +02:00
|
|
|
outByte (tmp);
|
2002-08-28 00:47:33 +02:00
|
|
|
++i;
|
|
|
|
} while(X);
|
|
|
|
}
|
|
|
|
|
2003-09-18 19:37:25 +02:00
|
|
|
/// doInitialization - Assign a number to each Function, as follows:
|
2003-06-03 09:56:05 +02:00
|
|
|
/// Functions are numbered starting at 0 at the begin() of each Module.
|
|
|
|
/// Functions which are External (and thus have 0 basic blocks) are not
|
|
|
|
/// inserted into the maps, and are not assigned a number. The side-effect
|
|
|
|
/// of this method is to fill in Fkey to contain the mapping from Functions
|
|
|
|
/// to numbers. (This method is called automatically by the PassManager.)
|
2003-09-18 19:37:25 +02:00
|
|
|
///
|
|
|
|
bool MappingInfoAsmPrinter::doInitialization(Module &M) {
|
2002-08-28 00:47:33 +02:00
|
|
|
unsigned i = 0;
|
2003-06-03 09:56:05 +02:00
|
|
|
for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
|
|
|
|
if (FI->isExternal()) continue;
|
2002-08-28 00:47:33 +02:00
|
|
|
Fkey[FI] = i;
|
|
|
|
++i;
|
|
|
|
}
|
2003-06-05 00:07:12 +02:00
|
|
|
return false; // Success.
|
2002-08-28 00:47:33 +02:00
|
|
|
}
|
2003-06-03 09:56:05 +02:00
|
|
|
|
|
|
|
/// create_BB_to_MInumber_Key -- Assign a number to each MachineBasicBlock
|
|
|
|
/// in the given Function, as follows: Numbering starts at zero in each
|
|
|
|
/// Function. MachineBasicBlocks are numbered from begin() to end()
|
|
|
|
/// in the Function's corresponding MachineFunction. Each successive
|
|
|
|
/// MachineBasicBlock increments the numbering by the number of instructions
|
2003-10-10 19:57:28 +02:00
|
|
|
/// it contains. The side-effect of this method is to fill in the parameter
|
2003-06-05 00:07:12 +02:00
|
|
|
/// KEY with the mapping of MachineBasicBlocks to numbers. KEY
|
2003-06-03 09:56:05 +02:00
|
|
|
/// is keyed on MachineInstrs, so each MachineBasicBlock is represented
|
|
|
|
/// therein by its first MachineInstr.
|
2003-09-18 19:37:25 +02:00
|
|
|
///
|
|
|
|
void MappingInfoAsmPrinter::create_BB_to_MInumber_Key(Function &FI,
|
2004-10-19 07:15:21 +02:00
|
|
|
InstructionKey &key) {
|
2002-08-28 00:47:33 +02:00
|
|
|
unsigned i = 0;
|
2002-10-28 21:00:31 +01:00
|
|
|
MachineFunction &MF = MachineFunction::get(&FI);
|
|
|
|
for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
MachineBasicBlock &miBB = *BI;
|
2004-02-12 03:27:10 +01:00
|
|
|
key[&miBB.front()] = i;
|
2002-08-28 00:47:33 +02:00
|
|
|
i = i+(miBB.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-18 19:37:25 +02:00
|
|
|
/// buildBBMIMap - Build the BB TO MI MAP for the function FI,
|
2003-06-05 00:07:12 +02:00
|
|
|
/// and save it into the parameter MAP.
|
2003-09-18 19:37:25 +02:00
|
|
|
///
|
|
|
|
void MappingInfoAsmPrinter::buildBBMIMap(Function &FI, MappingInfo &Map) {
|
2002-10-28 21:00:31 +01:00
|
|
|
unsigned bb = 0;
|
2003-06-05 00:07:12 +02:00
|
|
|
|
|
|
|
// First build temporary table used to write out the map.
|
|
|
|
InstructionKey BBkey;
|
|
|
|
create_BB_to_MInumber_Key(FI, BBkey);
|
|
|
|
|
|
|
|
selectOutputMap (Map);
|
2005-04-22 01:30:14 +02:00
|
|
|
MachineFunction &MF = MachineFunction::get(&FI);
|
2002-10-28 21:00:31 +01:00
|
|
|
for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
|
|
|
|
BI != BE; ++BI, ++bb) {
|
|
|
|
MachineBasicBlock &miBB = *BI;
|
2002-08-28 00:47:33 +02:00
|
|
|
writeNumber(bb);
|
2004-02-12 03:27:10 +01:00
|
|
|
writeNumber(BBkey[&miBB.front()]);
|
2002-08-28 00:47:33 +02:00
|
|
|
writeNumber(miBB.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-05 00:07:12 +02:00
|
|
|
void MappingInfo::byteVector::dumpAssembly (std::ostream &Out) {
|
|
|
|
for (iterator i = begin (), e = end (); i != e; ++i)
|
2005-07-27 07:53:44 +02:00
|
|
|
Out << ".byte " << (int)*i << "\n";
|
2003-06-05 00:07:12 +02:00
|
|
|
}
|
|
|
|
|
2003-09-18 19:37:25 +02:00
|
|
|
static void writePrologue (std::ostream &Out, const std::string &comment,
|
2005-07-27 07:53:44 +02:00
|
|
|
const std::string &symName) {
|
2003-06-05 00:07:12 +02:00
|
|
|
// Prologue:
|
2003-09-18 19:37:25 +02:00
|
|
|
// Output a comment describing the object.
|
2005-04-22 01:30:14 +02:00
|
|
|
Out << "!" << comment << "\n";
|
2003-06-05 00:07:12 +02:00
|
|
|
// Switch the current section to .rodata in the assembly output:
|
2005-04-22 01:30:14 +02:00
|
|
|
Out << "\t.section \".rodata\"\n\t.align 8\n";
|
2003-09-18 19:37:25 +02:00
|
|
|
// Output a global symbol naming the object:
|
2005-04-22 01:30:14 +02:00
|
|
|
Out << "\t.global " << symName << "\n";
|
|
|
|
Out << "\t.type " << symName << ",#object\n";
|
|
|
|
Out << symName << ":\n";
|
2003-09-18 19:37:25 +02:00
|
|
|
}
|
2003-06-05 00:07:12 +02:00
|
|
|
|
2003-09-18 19:37:25 +02:00
|
|
|
static void writeEpilogue (std::ostream &Out, const std::string &symName) {
|
2003-06-05 00:07:12 +02:00
|
|
|
// Epilogue:
|
2003-09-18 19:37:25 +02:00
|
|
|
// Output a local symbol marking the end of the object:
|
2005-04-22 01:30:14 +02:00
|
|
|
Out << ".end_" << symName << ":\n";
|
2003-09-18 19:37:25 +02:00
|
|
|
// Output size directive giving the size of the object:
|
|
|
|
Out << "\t.size " << symName << ", .end_" << symName << "-" << symName
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingInfo::dumpAssembly (std::ostream &Out) {
|
|
|
|
const std::string &name (symbolPrefix + utostr (functionNumber));
|
|
|
|
writePrologue (Out, comment, name);
|
|
|
|
// The LMIMap and BBMIMap are supposed to start with a length word:
|
|
|
|
Out << "\t.word .end_" << name << "-" << name << "\n";
|
|
|
|
bytes.dumpAssembly (Out);
|
|
|
|
writeEpilogue (Out, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// doFinalization - This method writes out two tables, named
|
|
|
|
/// FunctionBB and FunctionLI, which map Function numbers (as in
|
|
|
|
/// doInitialization) to the BBMIMap and LMIMap tables. (This used to
|
|
|
|
/// be the "FunctionInfo" pass.)
|
|
|
|
///
|
|
|
|
bool MappingInfoAsmPrinter::doFinalization (Module &M) {
|
|
|
|
unsigned f;
|
2005-04-22 01:30:14 +02:00
|
|
|
|
2003-09-18 19:37:25 +02:00
|
|
|
writePrologue(Out, "FUNCTION TO BB MAP", "FunctionBB");
|
|
|
|
f=0;
|
|
|
|
for(Module::iterator FI = M.begin (), FE = M.end (); FE != FI; ++FI) {
|
|
|
|
if (FI->isExternal ())
|
|
|
|
continue;
|
|
|
|
Out << "\t.xword BBMIMap" << f << "\n";
|
|
|
|
++f;
|
|
|
|
}
|
|
|
|
writeEpilogue(Out, "FunctionBB");
|
2005-04-22 01:30:14 +02:00
|
|
|
|
2003-09-18 19:37:25 +02:00
|
|
|
return false;
|
2002-08-28 00:47:33 +02:00
|
|
|
}
|
2003-09-18 19:37:25 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|