2002-08-28 00:47:33 +02:00
|
|
|
//===- MappingInfo.cpp - create LLVM info and output to .s file ---------===//
|
|
|
|
//
|
2003-06-03 09:56:05 +02:00
|
|
|
// This file contains a FunctionPass called getMappingInfoForFunction,
|
|
|
|
// which creates two maps: one between LLVM Instructions and MachineInstrs,
|
|
|
|
// and another between MachineBasicBlocks and MachineInstrs (the "BB TO
|
|
|
|
// MI MAP").
|
|
|
|
//
|
|
|
|
// 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
|
|
|
//
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Reoptimizer/Mapping/MappingInfo.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-10-28 21:00:31 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2002-08-28 00:47:33 +02:00
|
|
|
#include "llvm/CodeGen/MachineCodeForInstruction.h"
|
|
|
|
#include <map>
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class getMappingInfoForFunction : public FunctionPass {
|
|
|
|
std::ostream &Out;
|
|
|
|
public:
|
|
|
|
getMappingInfoForFunction(std::ostream &out) : Out(out){}
|
|
|
|
const char* getPassName() const{return "Sparc MappingInformation";}
|
|
|
|
bool runOnFunction(Function &FI);
|
|
|
|
private:
|
|
|
|
std::map<const Function*, unsigned> Fkey; //key of F to num
|
|
|
|
std::map<const MachineInstr*, unsigned> BBkey; //key BB to num
|
|
|
|
std::map<const MachineInstr*, unsigned> MIkey; //key MI to num
|
2003-06-03 21:30:15 +02:00
|
|
|
void writePrologue(const std::string &comment,
|
|
|
|
const std::string &symbolPrefix, unsigned num);
|
|
|
|
void writeEpilogue(const std::string &symbolPrefix, unsigned num);
|
|
|
|
|
2002-08-28 00:47:33 +02:00
|
|
|
bool doInitialization(Module &M);
|
|
|
|
void create_BB_to_MInumber_Key(Function &FI);
|
|
|
|
void create_MI_to_number_Key(Function &FI);
|
2003-06-03 21:30:15 +02:00
|
|
|
void writeBBToMImap(Function &FI, unsigned num);
|
|
|
|
void writeLLVMToMImap(Function &FI, unsigned num);
|
2003-06-04 20:17:22 +02:00
|
|
|
void writeNumber(unsigned X);
|
2002-08-28 00:47:33 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-06-03 09:56:05 +02:00
|
|
|
/// MappingInfoForFunction -- Static factory method: returns a new
|
2003-06-03 21:30:15 +02:00
|
|
|
/// getMappingInfoForFunction Pass object, which uses OUT as its
|
|
|
|
/// output stream for assembly output.
|
2002-08-28 00:47:33 +02:00
|
|
|
Pass *MappingInfoForFunction(std::ostream &out){
|
|
|
|
return (new getMappingInfoForFunction(out));
|
|
|
|
}
|
|
|
|
|
2003-06-03 21:30:15 +02:00
|
|
|
/// runOnFunction -- Builds up the maps for the given function FI and then
|
|
|
|
/// 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.
|
2002-08-28 00:47:33 +02:00
|
|
|
bool getMappingInfoForFunction::runOnFunction(Function &FI) {
|
2003-06-03 21:30:15 +02:00
|
|
|
// First we build temporary tables used to write out the maps.
|
2002-08-28 00:47:33 +02:00
|
|
|
create_BB_to_MInumber_Key(FI);
|
|
|
|
create_MI_to_number_Key(FI);
|
2003-06-03 21:30:15 +02:00
|
|
|
unsigned num = Fkey[&FI]; // Function number for the current function.
|
|
|
|
|
|
|
|
// Now, write out the maps.
|
|
|
|
writeBBToMImap(FI, num);
|
|
|
|
writeLLVMToMImap(FI, num);
|
2002-08-28 00:47:33 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-06-03 21:30:15 +02:00
|
|
|
/// writePrologue -- Output a COMMENT describing the map, then output a
|
|
|
|
/// global symbol to start the map named by concatenating SYMBOLPREFIX
|
|
|
|
/// and NUM, then output a word containing the length of the map, to the
|
|
|
|
/// current output stream Out. This also switches the current section to
|
|
|
|
/// .rodata in the assembly output.
|
|
|
|
void getMappingInfoForFunction::writePrologue(const std::string &comment,
|
|
|
|
const std::string &symbolPrefix,
|
|
|
|
unsigned num) {
|
|
|
|
// Comment:
|
|
|
|
Out << "!" << comment << "\n";
|
|
|
|
// Switch sections:
|
2002-08-28 00:47:33 +02:00
|
|
|
Out << "\t.section \".rodata\"\n\t.align 8\n";
|
2003-06-03 21:30:15 +02:00
|
|
|
// Global symbol naming the map:
|
|
|
|
Out << "\t.global " << symbolPrefix << num << "\n";
|
|
|
|
Out << "\t.type " << symbolPrefix << num << ",#object\n";
|
|
|
|
Out << symbolPrefix << num << ":\n";
|
|
|
|
// Length word:
|
|
|
|
Out << "\t.word .end_" << symbolPrefix << num << "-"
|
|
|
|
<< symbolPrefix << num << "\n";
|
2002-08-28 00:47:33 +02:00
|
|
|
}
|
|
|
|
|
2003-06-03 21:30:15 +02:00
|
|
|
/// writeEpilogue -- Outputs a local symbol to end the map named by
|
|
|
|
/// concatenating SYMBOLPREFIX and NUM, followed by a .size directive that
|
|
|
|
/// gives the size of the map, to the current output stream Out.
|
|
|
|
void getMappingInfoForFunction::writeEpilogue(const std::string &symbolPrefix,
|
|
|
|
unsigned num) {
|
|
|
|
// Local symbol ending the map:
|
|
|
|
Out << ".end_" << symbolPrefix << num << ":\n";
|
|
|
|
// Size directive:
|
|
|
|
Out << "\t.size " << symbolPrefix << num << ", .end_"
|
|
|
|
<< symbolPrefix << num << "-" << symbolPrefix
|
|
|
|
<< num << "\n\n\n\n";
|
2002-08-28 00:47:33 +02:00
|
|
|
}
|
|
|
|
|
2003-06-04 20:17:22 +02:00
|
|
|
/// outByte -- NOT DONE YET.
|
|
|
|
void outByte (unsigned char b) {
|
2003-06-04 22:53:46 +02:00
|
|
|
//Out << "\t.byte " << tmp << "\n";
|
2003-06-04 20:17:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-03 09:56:05 +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-06-04 20:17:22 +02:00
|
|
|
void getMappingInfoForFunction::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-04 20:17:22 +02:00
|
|
|
outByte (tmp);
|
2002-08-28 00:47:33 +02:00
|
|
|
++i;
|
|
|
|
} while(X);
|
|
|
|
}
|
|
|
|
|
2003-06-03 09:56:05 +02:00
|
|
|
/// doInitialization -- Assign a number to each Function, as follows:
|
|
|
|
/// 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.)
|
2002-10-28 21:00:31 +01:00
|
|
|
bool getMappingInfoForFunction::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;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
/// it contains. The side-effect of this method is to fill in the instance
|
|
|
|
/// variable BBkey with the mapping of MachineBasicBlocks to numbers. BBkey
|
|
|
|
/// is keyed on MachineInstrs, so each MachineBasicBlock is represented
|
|
|
|
/// therein by its first MachineInstr.
|
2002-10-28 21:00:31 +01:00
|
|
|
void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI) {
|
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;
|
2002-08-28 00:47:33 +02:00
|
|
|
BBkey[miBB[0]] = i;
|
|
|
|
i = i+(miBB.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-03 09:56:05 +02:00
|
|
|
/// create_MI_to_number_Key -- Assign a number to each MachineInstr
|
|
|
|
/// 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
|
|
|
|
/// side-effect of this method is to fill in the instance variable MIkey
|
|
|
|
/// with the mapping from MachineInstrs to numbers.
|
2002-10-28 21:00:31 +01:00
|
|
|
void getMappingInfoForFunction::create_MI_to_number_Key(Function &FI) {
|
|
|
|
MachineFunction &MF = MachineFunction::get(&FI);
|
|
|
|
for (MachineFunction::iterator BI=MF.begin(), BE=MF.end(); BI != BE; ++BI) {
|
|
|
|
MachineBasicBlock &miBB = *BI;
|
2002-08-28 00:47:33 +02:00
|
|
|
unsigned j = 0;
|
2002-10-28 02:41:47 +01:00
|
|
|
for(MachineBasicBlock::iterator miI=miBB.begin(), miE=miBB.end();
|
2002-10-28 21:00:31 +01:00
|
|
|
miI!=miE; ++miI, ++j) {
|
2002-08-28 00:47:33 +02:00
|
|
|
MIkey[*miI]=j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-03 09:56:05 +02:00
|
|
|
/// writeBBToMImap -- Output the BB TO MI MAP for the given function as
|
|
|
|
/// assembly code to the current output stream. The BB TO MI MAP consists
|
|
|
|
/// of a three-element tuple for each MachineBasicBlock in a function:
|
|
|
|
/// 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.
|
2003-06-03 21:30:15 +02:00
|
|
|
void getMappingInfoForFunction::writeBBToMImap(Function &FI, unsigned num){
|
2002-10-28 21:00:31 +01:00
|
|
|
unsigned bb = 0;
|
2003-06-03 21:30:15 +02:00
|
|
|
const std::string MapComment = "BB TO MI MAP";
|
|
|
|
const std::string MapSymbolPrefix = "BBMIMap";
|
|
|
|
writePrologue(MapComment, MapSymbolPrefix, num);
|
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, ++bb) {
|
|
|
|
MachineBasicBlock &miBB = *BI;
|
2002-08-28 00:47:33 +02:00
|
|
|
writeNumber(bb);
|
2003-06-03 09:56:05 +02:00
|
|
|
writeNumber(BBkey[miBB[0]]);
|
2002-08-28 00:47:33 +02:00
|
|
|
writeNumber(miBB.size());
|
|
|
|
}
|
2003-06-03 21:30:15 +02:00
|
|
|
writeEpilogue(MapSymbolPrefix, num);
|
2002-08-28 00:47:33 +02:00
|
|
|
}
|
|
|
|
|
2003-06-03 09:56:05 +02:00
|
|
|
/// writeLLVMToMImap -- Output the LLVM I TO MI MAP for the given function
|
|
|
|
/// as assembly code to the current output stream. 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.
|
2003-06-03 21:30:15 +02:00
|
|
|
void getMappingInfoForFunction::writeLLVMToMImap(Function &FI, unsigned num) {
|
2003-06-03 09:56:05 +02:00
|
|
|
unsigned bb = 0;
|
2003-06-03 21:30:15 +02:00
|
|
|
const std::string MapComment = "LLVM I TO MI MAP";
|
|
|
|
const std::string MapSymbolPrefix = "LMIMap";
|
|
|
|
writePrologue(MapComment, MapSymbolPrefix, num);
|
2003-06-03 09:56:05 +02:00
|
|
|
for (Function::iterator BI = FI.begin(), BE = FI.end();
|
2002-10-28 21:00:31 +01:00
|
|
|
BI != BE; ++BI, ++bb) {
|
2002-08-28 00:47:33 +02:00
|
|
|
unsigned li = 0;
|
|
|
|
writeNumber(bb);
|
|
|
|
writeNumber(BI->size());
|
2003-06-03 09:56:05 +02:00
|
|
|
for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
|
|
|
|
++II, ++li) {
|
|
|
|
MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II);
|
2002-08-28 00:47:33 +02:00
|
|
|
writeNumber(li);
|
2002-09-17 22:24:46 +02:00
|
|
|
writeNumber(miI.size());
|
2002-08-28 00:47:33 +02:00
|
|
|
for (MachineCodeForInstruction::iterator miII = miI.begin(),
|
2003-06-03 09:56:05 +02:00
|
|
|
miIE = miI.end(); miII != miIE; ++miII) {
|
|
|
|
writeNumber(MIkey[*miII]);
|
2002-08-28 00:47:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-03 21:30:15 +02:00
|
|
|
writeEpilogue(MapSymbolPrefix, num);
|
2002-08-28 00:47:33 +02:00
|
|
|
}
|