blob: b681bfca4e25ef2749e798f2ddb1729d28315243 [file] [log] [blame]
Anand Shuklab85d2652002-08-27 22:47:33 +00001//===- MappingInfo.cpp - create LLVM info and output to .s file ---------===//
2//
Brian Gaekeaeab1e12003-06-04 22:07:12 +00003// This file contains a FunctionPass called MappingInfo,
4// which creates two maps: one between LLVM Instructions and MachineInstrs
5// (the "LLVM I TO MI MAP"), and another between MachineBasicBlocks and
6// MachineInstrs (the "BB TO MI MAP").
Brian Gaekee14ccaf2003-06-03 07:56:05 +00007//
8// As a side effect, it outputs this information as .byte directives to
9// the assembly file. The output is designed to survive the SPARC assembler,
10// in order that the Reoptimizer may read it in from memory later when the
11// binary is loaded. Therefore, it may contain some hidden SPARC-architecture
12// dependencies. Currently this question is purely theoretical as the
13// Reoptimizer works only on the SPARC.
Anand Shuklab85d2652002-08-27 22:47:33 +000014//
Brian Gaekeaeab1e12003-06-04 22:07:12 +000015// The LLVM I TO MI MAP consists of a set of information for each
16// BasicBlock in a Function, ordered from begin() to end(). The information
17// for a BasicBlock consists of
18// 1) its (0-based) index in the Function,
19// 2) the number of LLVM Instructions it contains, and
20// 3) information for each Instruction, in sequence from the begin()
21// to the end() of the BasicBlock. The information for an Instruction
22// consists of
23// 1) its (0-based) index in the BasicBlock,
24// 2) the number of MachineInstrs that correspond to that Instruction
25// (as reported by MachineCodeForInstruction), and
26// 3) the MachineInstr number calculated by create_MI_to_number_Key,
27// for each of the MachineInstrs that correspond to that Instruction.
28//
29// The BB TO MI MAP consists of a three-element tuple for each
30// MachineBasicBlock in a function, ordered from begin() to end() of
31// its MachineFunction: first, the index of the MachineBasicBlock in the
32// function; second, the number of the MachineBasicBlock in the function
33// as computed by create_BB_to_MInumber_Key; and third, the number of
34// MachineInstrs in the MachineBasicBlock.
35//
Anand Shuklab85d2652002-08-27 22:47:33 +000036//===--------------------------------------------------------------------===//
37
Chris Lattner48e60792003-08-13 02:38:16 +000038#include "MappingInfo.h"
Anand Shuklab85d2652002-08-27 22:47:33 +000039#include "llvm/Pass.h"
40#include "llvm/Module.h"
Misha Brukmanb7551ef2002-10-28 20:00:31 +000041#include "llvm/CodeGen/MachineFunction.h"
Anand Shuklab85d2652002-08-27 22:47:33 +000042#include "llvm/CodeGen/MachineCodeForInstruction.h"
Anand Shuklab85d2652002-08-27 22:47:33 +000043
Anand Shuklab85d2652002-08-27 22:47:33 +000044namespace {
Brian Gaekeaeab1e12003-06-04 22:07:12 +000045 class MappingInfoCollector : public FunctionPass {
Anand Shuklab85d2652002-08-27 22:47:33 +000046 std::ostream &Out;
47 public:
Brian Gaekeaeab1e12003-06-04 22:07:12 +000048 MappingInfoCollector(std::ostream &out) : Out(out){}
49 const char *getPassName () const { return "Instr. Mapping Info Collector"; }
Anand Shuklab85d2652002-08-27 22:47:33 +000050 bool runOnFunction(Function &FI);
Brian Gaekeaeab1e12003-06-04 22:07:12 +000051 typedef std::map<const MachineInstr*, unsigned> InstructionKey;
Anand Shuklab85d2652002-08-27 22:47:33 +000052 private:
Brian Gaekeaeab1e12003-06-04 22:07:12 +000053 MappingInfo *currentOutputMap;
54 std::map<Function *, unsigned> Fkey; // Function # for all functions.
Anand Shuklab85d2652002-08-27 22:47:33 +000055 bool doInitialization(Module &M);
Brian Gaekeaeab1e12003-06-04 22:07:12 +000056 void create_BB_to_MInumber_Key(Function &FI, InstructionKey &key);
57 void create_MI_to_number_Key(Function &FI, InstructionKey &key);
58 void buildBBMIMap (Function &FI, MappingInfo &Map);
59 void buildLMIMap (Function &FI, MappingInfo &Map);
60 void writeNumber(unsigned X);
61 void selectOutputMap (MappingInfo &m) { currentOutputMap = &m; }
62 void outByte (unsigned char b) { currentOutputMap->outByte (b); }
Anand Shuklab85d2652002-08-27 22:47:33 +000063 };
64}
65
Brian Gaekeaeab1e12003-06-04 22:07:12 +000066/// getMappingInfoCollector -- Static factory method: returns a new
67/// MappingInfoCollector Pass object, which uses OUT as its
Brian Gaekefc97c8b2003-06-03 19:30:15 +000068/// output stream for assembly output.
Brian Gaekeaeab1e12003-06-04 22:07:12 +000069Pass *getMappingInfoCollector(std::ostream &out){
70 return (new MappingInfoCollector(out));
Anand Shuklab85d2652002-08-27 22:47:33 +000071}
72
Brian Gaekefc97c8b2003-06-03 19:30:15 +000073/// runOnFunction -- Builds up the maps for the given function FI and then
74/// writes them out as assembly code to the current output stream OUT.
Brian Gaekee14ccaf2003-06-03 07:56:05 +000075/// This is an entry point to the pass, called by the PassManager.
Brian Gaekeaeab1e12003-06-04 22:07:12 +000076bool MappingInfoCollector::runOnFunction(Function &FI) {
Brian Gaekefc97c8b2003-06-03 19:30:15 +000077 unsigned num = Fkey[&FI]; // Function number for the current function.
Anand Shuklab85d2652002-08-27 22:47:33 +000078
Brian Gaekeaeab1e12003-06-04 22:07:12 +000079 // Create objects to hold the maps.
80 MappingInfo LMIMap ("LLVM I TO MI MAP", "LMIMap", num);
81 MappingInfo BBMIMap ("BB TO MI MAP", "BBMIMap", num);
82
83 // Now, build the maps.
84 buildLMIMap (FI, LMIMap);
85 buildBBMIMap (FI, BBMIMap);
86
Brian Gaekefc97c8b2003-06-03 19:30:15 +000087 // Now, write out the maps.
Brian Gaekeaeab1e12003-06-04 22:07:12 +000088 LMIMap.dumpAssembly (Out);
89 BBMIMap.dumpAssembly (Out);
Brian Gaekefc97c8b2003-06-03 19:30:15 +000090
Anand Shuklab85d2652002-08-27 22:47:33 +000091 return false;
92}
93
Brian Gaekee14ccaf2003-06-03 07:56:05 +000094/// writeNumber -- Write out the number X as a sequence of .byte
Brian Gaekefc97c8b2003-06-03 19:30:15 +000095/// directives to the current output stream Out. This method performs a
96/// run-length encoding of the unsigned integers X that are output.
Brian Gaekeaeab1e12003-06-04 22:07:12 +000097void MappingInfoCollector::writeNumber(unsigned X) {
Anand Shuklab85d2652002-08-27 22:47:33 +000098 unsigned i=0;
99 do {
100 unsigned tmp = X & 127;
101 X >>= 7;
102 if (X) tmp |= 128;
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000103 outByte (tmp);
Anand Shuklab85d2652002-08-27 22:47:33 +0000104 ++i;
105 } while(X);
Anand Shuklab85d2652002-08-27 22:47:33 +0000106}
107
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000108/// doInitialization -- Assign a number to each Function, as follows:
109/// Functions are numbered starting at 0 at the begin() of each Module.
110/// Functions which are External (and thus have 0 basic blocks) are not
111/// inserted into the maps, and are not assigned a number. The side-effect
112/// of this method is to fill in Fkey to contain the mapping from Functions
113/// to numbers. (This method is called automatically by the PassManager.)
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000114bool MappingInfoCollector::doInitialization(Module &M) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000115 unsigned i = 0;
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000116 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
117 if (FI->isExternal()) continue;
Anand Shuklab85d2652002-08-27 22:47:33 +0000118 Fkey[FI] = i;
119 ++i;
120 }
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000121 return false; // Success.
Anand Shuklab85d2652002-08-27 22:47:33 +0000122}
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000123
124/// create_BB_to_MInumber_Key -- Assign a number to each MachineBasicBlock
125/// in the given Function, as follows: Numbering starts at zero in each
126/// Function. MachineBasicBlocks are numbered from begin() to end()
127/// in the Function's corresponding MachineFunction. Each successive
128/// MachineBasicBlock increments the numbering by the number of instructions
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000129/// it contains. The side-effect of this method is to fill in the paramete
130/// KEY with the mapping of MachineBasicBlocks to numbers. KEY
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000131/// is keyed on MachineInstrs, so each MachineBasicBlock is represented
132/// therein by its first MachineInstr.
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000133void MappingInfoCollector::create_BB_to_MInumber_Key(Function &FI,
134 InstructionKey &key) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000135 unsigned i = 0;
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000136 MachineFunction &MF = MachineFunction::get(&FI);
137 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
138 BI != BE; ++BI) {
139 MachineBasicBlock &miBB = *BI;
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000140 key[miBB[0]] = i;
Anand Shuklab85d2652002-08-27 22:47:33 +0000141 i = i+(miBB.size());
142 }
143}
144
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000145/// create_MI_to_number_Key -- Assign a number to each MachineInstr
146/// in the given Function with respect to its enclosing MachineBasicBlock, as
147/// follows: Numberings start at 0 in each MachineBasicBlock. MachineInstrs
148/// are numbered from begin() to end() in their MachineBasicBlock. Each
149/// MachineInstr is numbered, then the numbering is incremented by 1. The
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000150/// side-effect of this method is to fill in the parameter KEY
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000151/// with the mapping from MachineInstrs to numbers.
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000152void MappingInfoCollector::create_MI_to_number_Key(Function &FI,
153 InstructionKey &key) {
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000154 MachineFunction &MF = MachineFunction::get(&FI);
155 for (MachineFunction::iterator BI=MF.begin(), BE=MF.end(); BI != BE; ++BI) {
156 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000157 unsigned j = 0;
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000158 for(MachineBasicBlock::iterator miI = miBB.begin(), miE = miBB.end();
159 miI != miE; ++miI, ++j) {
160 key[*miI] = j;
Anand Shuklab85d2652002-08-27 22:47:33 +0000161 }
162 }
163}
164
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000165/// buildBBMIMap -- Build the BB TO MI MAP for the function FI,
166/// and save it into the parameter MAP.
167void MappingInfoCollector::buildBBMIMap(Function &FI, MappingInfo &Map) {
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000168 unsigned bb = 0;
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000169
170 // First build temporary table used to write out the map.
171 InstructionKey BBkey;
172 create_BB_to_MInumber_Key(FI, BBkey);
173
174 selectOutputMap (Map);
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000175 MachineFunction &MF = MachineFunction::get(&FI);
176 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
177 BI != BE; ++BI, ++bb) {
178 MachineBasicBlock &miBB = *BI;
Anand Shuklab85d2652002-08-27 22:47:33 +0000179 writeNumber(bb);
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000180 writeNumber(BBkey[miBB[0]]);
Anand Shuklab85d2652002-08-27 22:47:33 +0000181 writeNumber(miBB.size());
182 }
183}
184
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000185/// buildLMIMap -- Build the LLVM I TO MI MAP for the function FI,
186/// and save it into the parameter MAP.
187void MappingInfoCollector::buildLMIMap(Function &FI, MappingInfo &Map) {
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000188 unsigned bb = 0;
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000189 // First build temporary table used to write out the map.
190 InstructionKey MIkey;
191 create_MI_to_number_Key(FI, MIkey);
192
193 selectOutputMap (Map);
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000194 for (Function::iterator BI = FI.begin(), BE = FI.end();
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000195 BI != BE; ++BI, ++bb) {
Anand Shuklab85d2652002-08-27 22:47:33 +0000196 unsigned li = 0;
197 writeNumber(bb);
Anand Shuklab85d2652002-08-27 22:47:33 +0000198 writeNumber(BI->size());
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000199 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
200 ++II, ++li) {
201 MachineCodeForInstruction& miI = MachineCodeForInstruction::get(II);
Anand Shuklab85d2652002-08-27 22:47:33 +0000202 writeNumber(li);
Anand Shuklab3794702002-09-17 20:24:46 +0000203 writeNumber(miI.size());
Anand Shuklab85d2652002-08-27 22:47:33 +0000204 for (MachineCodeForInstruction::iterator miII = miI.begin(),
Brian Gaekee14ccaf2003-06-03 07:56:05 +0000205 miIE = miI.end(); miII != miIE; ++miII) {
206 writeNumber(MIkey[*miII]);
Anand Shuklab85d2652002-08-27 22:47:33 +0000207 }
208 }
209 }
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000210}
211
212void MappingInfo::byteVector::dumpAssembly (std::ostream &Out) {
213 for (iterator i = begin (), e = end (); i != e; ++i)
214 Out << ".byte " << (int)*i << "\n";
215}
216
217void MappingInfo::dumpAssembly (std::ostream &Out) {
218 // Prologue:
219 // Output a comment describing the map.
220 Out << "!" << comment << "\n";
221 // Switch the current section to .rodata in the assembly output:
222 Out << "\t.section \".rodata\"\n\t.align 8\n";
223 // Output a global symbol naming the map:
224 Out << "\t.global " << symbolPrefix << functionNumber << "\n";
225 Out << "\t.type " << symbolPrefix << functionNumber << ",#object\n";
226 Out << symbolPrefix << functionNumber << ":\n";
227 // Output a word containing the length of the map:
228 Out << "\t.word .end_" << symbolPrefix << functionNumber << "-"
229 << symbolPrefix << functionNumber << "\n";
230
231 // Output the map data itself:
232 bytes.dumpAssembly (Out);
233
234 // Epilogue:
235 // Output a local symbol marking the end of the map:
236 Out << ".end_" << symbolPrefix << functionNumber << ":\n";
237 // Output size directive giving the size of the map:
238 Out << "\t.size " << symbolPrefix << functionNumber << ", .end_"
239 << symbolPrefix << functionNumber << "-" << symbolPrefix
240 << functionNumber << "\n\n";
Anand Shuklab85d2652002-08-27 22:47:33 +0000241}