2002-08-27 22:47:33 +00:00
|
|
|
//===- MappingInfo.cpp - create LLVM info and output to .s file ---------===//
|
2003-10-20 19:43:21 +00: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-08-27 22:47:33 +00:00
|
|
|
//
|
2003-09-18 17:37:25 +00:00
|
|
|
// This file contains a FunctionPass called MappingInfoAsmPrinter,
|
2003-06-04 22:07:12 +00:00
|
|
|
// which creates two maps: one between LLVM Instructions and MachineInstrs
|
|
|
|
// (the "LLVM I TO MI MAP"), and another between MachineBasicBlocks and
|
|
|
|
// MachineInstrs (the "BB TO MI MAP").
|
2003-06-03 07:56:05 +00: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-27 22:47:33 +00:00
|
|
|
//
|
2003-06-04 22:07:12 +00:00
|
|
|
// The LLVM I TO MI MAP consists of a set of information for each
|
|
|
|
// BasicBlock in a Function, ordered from begin() to end(). The information
|
|
|
|
// for a BasicBlock consists of
|
|
|
|
// 1) its (0-based) index in the Function,
|
|
|
|
// 2) the number of LLVM Instructions it contains, and
|
|
|
|
// 3) information for each Instruction, in sequence from the begin()
|
|
|
|
// to the end() of the BasicBlock. The information for an Instruction
|
|
|
|
// consists of
|
|
|
|
// 1) its (0-based) index in the BasicBlock,
|
|
|
|
// 2) the number of MachineInstrs that correspond to that Instruction
|
|
|
|
// (as reported by MachineCodeForInstruction), and
|
|
|
|
// 3) the MachineInstr number calculated by create_MI_to_number_Key,
|
|
|
|
// for each of the MachineInstrs that correspond to that Instruction.
|
|
|
|
//
|
|
|
|
// 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-27 22:47:33 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2003-08-13 02:38:16 +00:00
|
|
|
#include "MappingInfo.h"
|
2002-08-27 22:47:33 +00:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Module.h"
|
2002-10-28 20:00:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2002-08-27 22:47:33 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeForInstruction.h"
|
2003-09-18 17:37:25 +00:00
|
|
|
#include "Support/StringExtras.h"
|
2002-08-27 22:47:33 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-08-27 22:47:33 +00:00
|
|
|
namespace {
|
2003-09-18 17:37:25 +00:00
|
|
|
class MappingInfoAsmPrinter : public FunctionPass {
|
2002-08-27 22:47:33 +00:00
|
|
|
std::ostream &Out;
|
|
|
|
public:
|
2003-09-18 17:37:25 +00:00
|
|
|
MappingInfoAsmPrinter(std::ostream &out) : Out(out){}
|
2003-06-04 22:07:12 +00:00
|
|
|
const char *getPassName () const { return "Instr. Mapping Info Collector"; }
|
2002-08-27 22:47:33 +00:00
|
|
|
bool runOnFunction(Function &FI);
|
2003-06-04 22:07:12 +00:00
|
|
|
typedef std::map<const MachineInstr*, unsigned> InstructionKey;
|
2002-08-27 22:47:33 +00:00
|
|
|
private:
|
2003-06-04 22:07:12 +00:00
|
|
|
MappingInfo *currentOutputMap;
|
|
|
|
std::map<Function *, unsigned> Fkey; // Function # for all functions.
|
2002-08-27 22:47:33 +00:00
|
|
|
bool doInitialization(Module &M);
|
2003-06-04 22:07:12 +00:00
|
|
|
void create_BB_to_MInumber_Key(Function &FI, InstructionKey &key);
|
|
|
|
void create_MI_to_number_Key(Function &FI, InstructionKey &key);
|
|
|
|
void buildBBMIMap (Function &FI, MappingInfo &Map);
|
|
|
|
void buildLMIMap (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 17:37:25 +00:00
|
|
|
bool doFinalization (Module &M);
|
2002-08-27 22:47:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-09-18 17:37:25 +00:00
|
|
|
/// getMappingInfoAsmPrinterPass - Static factory method: returns a new
|
|
|
|
/// MappingInfoAsmPrinter Pass object, which uses OUT as its output
|
|
|
|
/// stream for assembly output.
|
|
|
|
///
|
|
|
|
Pass *getMappingInfoAsmPrinterPass(std::ostream &out){
|
|
|
|
return (new MappingInfoAsmPrinter(out));
|
2002-08-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
2003-09-18 17:37:25 +00:00
|
|
|
/// runOnFunction - Builds up the maps for the given function FI and then
|
2003-06-03 19:30:15 +00:00
|
|
|
/// writes them out as assembly code to the current output stream OUT.
|
2003-06-03 07:56:05 +00:00
|
|
|
/// This is an entry point to the pass, called by the PassManager.
|
2003-09-18 17:37:25 +00:00
|
|
|
///
|
|
|
|
bool MappingInfoAsmPrinter::runOnFunction(Function &FI) {
|
2003-06-03 19:30:15 +00:00
|
|
|
unsigned num = Fkey[&FI]; // Function number for the current function.
|
|
|
|
|
2003-06-04 22:07:12 +00:00
|
|
|
// Create objects to hold the maps.
|
|
|
|
MappingInfo LMIMap ("LLVM I TO MI MAP", "LMIMap", num);
|
|
|
|
MappingInfo BBMIMap ("BB TO MI MAP", "BBMIMap", num);
|
|
|
|
|
|
|
|
// Now, build the maps.
|
|
|
|
buildLMIMap (FI, LMIMap);
|
|
|
|
buildBBMIMap (FI, BBMIMap);
|
|
|
|
|
2003-06-03 19:30:15 +00:00
|
|
|
// Now, write out the maps.
|
2003-06-04 22:07:12 +00:00
|
|
|
LMIMap.dumpAssembly (Out);
|
|
|
|
BBMIMap.dumpAssembly (Out);
|
2002-08-27 22:47:33 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-09-18 17:37:25 +00:00
|
|
|
/// writeNumber - Write out the number X as a sequence of .byte
|
2003-06-03 19:30:15 +00: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 17:37:25 +00:00
|
|
|
///
|
|
|
|
void MappingInfoAsmPrinter::writeNumber(unsigned X) {
|
2002-08-27 22:47:33 +00:00
|
|
|
unsigned i=0;
|
|
|
|
do {
|
|
|
|
unsigned tmp = X & 127;
|
|
|
|
X >>= 7;
|
|
|
|
if (X) tmp |= 128;
|
2003-06-04 22:07:12 +00:00
|
|
|
outByte (tmp);
|
2002-08-27 22:47:33 +00:00
|
|
|
++i;
|
|
|
|
} while(X);
|
|
|
|
}
|
|
|
|
|
2003-09-18 17:37:25 +00:00
|
|
|
/// doInitialization - Assign a number to each Function, as follows:
|
2003-06-03 07:56:05 +00: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 17:37:25 +00:00
|
|
|
///
|
|
|
|
bool MappingInfoAsmPrinter::doInitialization(Module &M) {
|
2002-08-27 22:47:33 +00:00
|
|
|
unsigned i = 0;
|
2003-06-03 07:56:05 +00:00
|
|
|
for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
|
|
|
|
if (FI->isExternal()) continue;
|
2002-08-27 22:47:33 +00:00
|
|
|
Fkey[FI] = i;
|
|
|
|
++i;
|
|
|
|
}
|
2003-06-04 22:07:12 +00:00
|
|
|
return false; // Success.
|
2002-08-27 22:47:33 +00:00
|
|
|
}
|
2003-06-03 07:56:05 +00: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 17:57:28 +00:00
|
|
|
/// it contains. The side-effect of this method is to fill in the parameter
|
2003-06-04 22:07:12 +00:00
|
|
|
/// KEY with the mapping of MachineBasicBlocks to numbers. KEY
|
2003-06-03 07:56:05 +00:00
|
|
|
/// is keyed on MachineInstrs, so each MachineBasicBlock is represented
|
|
|
|
/// therein by its first MachineInstr.
|
2003-09-18 17:37:25 +00:00
|
|
|
///
|
|
|
|
void MappingInfoAsmPrinter::create_BB_to_MInumber_Key(Function &FI,
|
2003-06-04 22:07:12 +00:00
|
|
|
InstructionKey &key) {
|
2002-08-27 22:47:33 +00:00
|
|
|
unsigned i = 0;
|
2002-10-28 20:00:31 +00:00
|
|
|
MachineFunction &MF = MachineFunction::get(&FI);
|
|
|
|
for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
MachineBasicBlock &miBB = *BI;
|
2004-02-12 02:27:10 +00:00
|
|
|
key[&miBB.front()] = i;
|
2002-08-27 22:47:33 +00:00
|
|
|
i = i+(miBB.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-18 17:37:25 +00:00
|
|
|
/// create_MI_to_number_Key - Assign a number to each MachineInstr
|
2003-06-03 07:56:05 +00:00
|
|
|
/// in the given Function with respect to its enclosing MachineBasicBlock, as
|
|
|
|
/// follows: Numberings start at 0 in each MachineBasicBlock. MachineInstrs
|
|
|
|
/// are numbered from begin() to end() in their MachineBasicBlock. Each
|
|
|
|
/// MachineInstr is numbered, then the numbering is incremented by 1. The
|
2003-06-04 22:07:12 +00:00
|
|
|
/// side-effect of this method is to fill in the parameter KEY
|
2003-06-03 07:56:05 +00:00
|
|
|
/// with the mapping from MachineInstrs to numbers.
|
2003-09-18 17:37:25 +00:00
|
|
|
///
|
|
|
|
void MappingInfoAsmPrinter::create_MI_to_number_Key(Function &FI,
|
2003-06-04 22:07:12 +00:00
|
|
|
InstructionKey &key) {
|
2002-10-28 20:00:31 +00:00
|
|
|
MachineFunction &MF = MachineFunction::get(&FI);
|
|
|
|
for (MachineFunction::iterator BI=MF.begin(), BE=MF.end(); BI != BE; ++BI) {
|
|
|
|
MachineBasicBlock &miBB = *BI;
|
2002-08-27 22:47:33 +00:00
|
|
|
unsigned j = 0;
|
2003-06-04 22:07:12 +00:00
|
|
|
for(MachineBasicBlock::iterator miI = miBB.begin(), miE = miBB.end();
|
|
|
|
miI != miE; ++miI, ++j) {
|
2004-02-12 02:27:10 +00:00
|
|
|
key[miI] = j;
|
2002-08-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-18 17:37:25 +00:00
|
|
|
/// buildBBMIMap - Build the BB TO MI MAP for the function FI,
|
2003-06-04 22:07:12 +00:00
|
|
|
/// and save it into the parameter MAP.
|
2003-09-18 17:37:25 +00:00
|
|
|
///
|
|
|
|
void MappingInfoAsmPrinter::buildBBMIMap(Function &FI, MappingInfo &Map) {
|
2002-10-28 20:00:31 +00:00
|
|
|
unsigned bb = 0;
|
2003-06-04 22:07:12 +00:00
|
|
|
|
|
|
|
// First build temporary table used to write out the map.
|
|
|
|
InstructionKey BBkey;
|
|
|
|
create_BB_to_MInumber_Key(FI, BBkey);
|
|
|
|
|
|
|
|
selectOutputMap (Map);
|
2002-10-28 20:00:31 +00:00
|
|
|
MachineFunction &MF = MachineFunction::get(&FI);
|
|
|
|
for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
|
|
|
|
BI != BE; ++BI, ++bb) {
|
|
|
|
MachineBasicBlock &miBB = *BI;
|
2002-08-27 22:47:33 +00:00
|
|
|
writeNumber(bb);
|
2004-02-12 02:27:10 +00:00
|
|
|
writeNumber(BBkey[&miBB.front()]);
|
2002-08-27 22:47:33 +00:00
|
|
|
writeNumber(miBB.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-18 17:37:25 +00:00
|
|
|
/// buildLMIMap - Build the LLVM I TO MI MAP for the function FI,
|
2003-06-04 22:07:12 +00:00
|
|
|
/// and save it into the parameter MAP.
|
2003-09-18 17:37:25 +00:00
|
|
|
///
|
|
|
|
void MappingInfoAsmPrinter::buildLMIMap(Function &FI, MappingInfo &Map) {
|
2003-06-03 07:56:05 +00:00
|
|
|
unsigned bb = 0;
|
2003-06-04 22:07:12 +00:00
|
|
|
// First build temporary table used to write out the map.
|
|
|
|
InstructionKey MIkey;
|
|
|
|
create_MI_to_number_Key(FI, MIkey);
|
|
|
|
|
|
|
|
selectOutputMap (Map);
|
2003-06-03 07:56:05 +00:00
|
|
|
for (Function::iterator BI = FI.begin(), BE = FI.end();
|
2002-10-28 20:00:31 +00:00
|
|
|
BI != BE; ++BI, ++bb) {
|
2002-08-27 22:47:33 +00:00
|
|
|
unsigned li = 0;
|
|
|
|
writeNumber(bb);
|
|
|
|
writeNumber(BI->size());
|
2003-06-03 07:56:05 +00:00
|
|
|
for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
|
|
|
|
++II, ++li) {
|
|
|
|
MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II);
|
2002-08-27 22:47:33 +00:00
|
|
|
writeNumber(li);
|
2002-09-17 20:24:46 +00:00
|
|
|
writeNumber(miI.size());
|
2002-08-27 22:47:33 +00:00
|
|
|
for (MachineCodeForInstruction::iterator miII = miI.begin(),
|
2003-06-03 07:56:05 +00:00
|
|
|
miIE = miI.end(); miII != miIE; ++miII) {
|
|
|
|
writeNumber(MIkey[*miII]);
|
2002-08-27 22:47:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-04 22:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappingInfo::byteVector::dumpAssembly (std::ostream &Out) {
|
|
|
|
for (iterator i = begin (), e = end (); i != e; ++i)
|
|
|
|
Out << ".byte " << (int)*i << "\n";
|
|
|
|
}
|
|
|
|
|
2003-09-18 17:37:25 +00:00
|
|
|
static void writePrologue (std::ostream &Out, const std::string &comment,
|
|
|
|
const std::string &symName) {
|
2003-06-04 22:07:12 +00:00
|
|
|
// Prologue:
|
2003-09-18 17:37:25 +00:00
|
|
|
// Output a comment describing the object.
|
2003-06-04 22:07:12 +00:00
|
|
|
Out << "!" << comment << "\n";
|
|
|
|
// Switch the current section to .rodata in the assembly output:
|
|
|
|
Out << "\t.section \".rodata\"\n\t.align 8\n";
|
2003-09-18 17:37:25 +00:00
|
|
|
// Output a global symbol naming the object:
|
|
|
|
Out << "\t.global " << symName << "\n";
|
|
|
|
Out << "\t.type " << symName << ",#object\n";
|
|
|
|
Out << symName << ":\n";
|
|
|
|
}
|
2003-06-04 22:07:12 +00:00
|
|
|
|
2003-09-18 17:37:25 +00:00
|
|
|
static void writeEpilogue (std::ostream &Out, const std::string &symName) {
|
2003-06-04 22:07:12 +00:00
|
|
|
// Epilogue:
|
2003-09-18 17:37:25 +00:00
|
|
|
// Output a local symbol marking the end of the object:
|
|
|
|
Out << ".end_" << symName << ":\n";
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
writePrologue(Out, "FUNCTION TO LI MAP", "FunctionLI");
|
|
|
|
f=0;
|
|
|
|
for(Module::iterator FI = M.begin (), FE = M.end (); FE != FI; ++FI) {
|
|
|
|
if (FI->isExternal ())
|
|
|
|
continue;
|
|
|
|
Out << "\t.xword LMIMap" << f << "\n";
|
|
|
|
++f;
|
|
|
|
}
|
|
|
|
writeEpilogue(Out, "FunctionLI");
|
|
|
|
|
|
|
|
return false;
|
2002-08-27 22:47:33 +00:00
|
|
|
}
|
2003-09-18 17:37:25 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|