1 //===-- llvm/CodeGen/RenderMachineFunction.h - MF->HTML -*- C++ -*---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_CODEGEN_RENDERMACHINEFUNCTION_H
13 #define LLVM_CODEGEN_RENDERMACHINEFUNCTION_H
15 #include "llvm/CodeGen/LiveInterval.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/SlotIndexes.h"
18 #include "llvm/Target/TargetRegisterInfo.h"
30 class MachineRegisterInfo;
31 class TargetRegisterClass;
32 class TargetRegisterInfo;
36 /// \brief Provide extra information about the physical and virtual registers
37 /// in the function being compiled.
38 class TargetRegisterExtraInfo {
40 TargetRegisterExtraInfo();
42 /// \brief Set up TargetRegisterExtraInfo with pointers to necessary
43 /// sources of information.
44 void setup(MachineFunction *mf, MachineRegisterInfo *mri,
45 const TargetRegisterInfo *tri, LiveIntervals *lis);
47 /// \brief Recompute tables for changed function.
50 /// \brief Free all tables in TargetRegisterExtraInfo.
53 /// \brief Maximum number of registers from trc which alias reg.
54 unsigned getWorst(unsigned reg, const TargetRegisterClass *trc) const;
56 /// \brief Returns the number of allocable registers in trc.
57 unsigned getCapacity(const TargetRegisterClass *trc) const;
59 /// \brief Return the number of registers of class trc that may be
61 unsigned getPressureAtSlot(const TargetRegisterClass *trc,
64 /// \brief Return true if the number of registers of type trc that may be
65 /// needed at slot i is greater than the capacity of trc.
66 bool classOverCapacityAtSlot(const TargetRegisterClass *trc,
72 MachineRegisterInfo *mri;
73 const TargetRegisterInfo *tri;
76 typedef std::map<const TargetRegisterClass*, unsigned> WorstMapLine;
77 typedef std::map<const TargetRegisterClass*, WorstMapLine> VRWorstMap;
80 typedef std::map<unsigned, WorstMapLine> PRWorstMap;
83 typedef std::map<const TargetRegisterClass*, unsigned> CapacityMap;
84 CapacityMap capacityMap;
86 typedef std::map<const TargetRegisterClass*, unsigned> PressureMapLine;
87 typedef std::map<SlotIndex, PressureMapLine> PressureMap;
88 PressureMap pressureMap;
92 /// \brief Initialise the 'worst' table.
95 /// \brief Initialise the 'capacity' table.
98 /// \brief Initialise/Reset the 'pressure' and live states tables.
99 void resetPressureAndLiveStates();
102 /// \brief Helper class to process rendering options. Tries to be as lazy as
104 class MFRenderingOptions {
107 struct RegClassComp {
108 bool operator()(const TargetRegisterClass *trc1,
109 const TargetRegisterClass *trc2) const {
110 std::string trc1Name(trc1->getName()), trc2Name(trc2->getName());
111 return std::lexicographical_compare(trc1Name.begin(), trc1Name.end(),
112 trc2Name.begin(), trc2Name.end());
116 typedef std::set<const TargetRegisterClass*, RegClassComp> RegClassSet;
118 struct IntervalComp {
119 bool operator()(const LiveInterval *li1, const LiveInterval *li2) const {
120 return li1->reg < li2->reg;
124 typedef std::set<const LiveInterval*, IntervalComp> IntervalSet;
126 /// Initialise the rendering options.
127 void setup(MachineFunction *mf, const TargetRegisterInfo *tri,
130 /// Clear translations of options to the current function.
133 /// Reset any options computed for this specific rendering.
134 void resetRenderSpecificOptions();
136 /// Should we render the current function.
137 bool shouldRenderCurrentMachineFunction() const;
139 /// Return the set of register classes to render pressure for.
140 const RegClassSet& regClasses() const;
142 /// Return the set of live intervals to render liveness for.
143 const IntervalSet& intervals() const;
145 /// Render indexes which are not associated with instructions / MBB starts.
146 bool renderEmptyIndexes() const;
148 /// Return whether or not to render using SVG for fancy vertical text.
149 bool fancyVerticals() const;
153 static bool renderingOptionsProcessed;
154 static std::set<std::string> mfNamesToRender;
155 static bool renderAllMFs;
157 static std::set<std::string> classNamesToRender;
158 static bool renderAllClasses;
161 static std::set<std::pair<unsigned, unsigned> > intervalNumsToRender;
162 typedef enum { ExplicitOnly = 0,
163 VirtPlusExplicit = 1,
164 PhysPlusExplicit = 2,
166 IntervalTypesToRender;
167 static unsigned intervalTypesToRender;
169 template <typename OutputItr>
170 static void splitComaSeperatedList(const std::string &s, OutputItr outItr);
172 static void processOptions();
174 static void processFuncNames();
175 static void processRegClassNames();
176 static void processIntervalNumbers();
178 static void processIntervalRange(const std::string &intervalRangeStr);
181 const TargetRegisterInfo *tri;
184 mutable bool regClassesTranslatedToCurrentFunction;
185 mutable RegClassSet regClassSet;
187 mutable bool intervalsTranslatedToCurrentFunction;
188 mutable IntervalSet intervalSet;
190 void translateRegClassNamesToCurrentFunction() const;
192 void translateIntervalNumbersToCurrentFunction() const;
195 /// \brief Render MachineFunction objects and related information to a HTML
197 class RenderMachineFunction : public MachineFunctionPass {
201 RenderMachineFunction() : MachineFunctionPass(ID) {}
203 virtual void getAnalysisUsage(AnalysisUsage &au) const;
205 virtual bool runOnMachineFunction(MachineFunction &fn);
207 virtual void releaseMemory();
209 /// \brief Render this machine function to HTML.
211 /// @param renderContextStr This parameter will be included in the top of
212 /// the html file to explain where (in the
213 /// codegen pipeline) this function was rendered
214 /// from. Set it to something like
215 /// "Pre-register-allocation".
216 /// @param vrm If non-null the VRM will be queried to determine
217 /// whether a virtual register was allocated to a
218 /// physical register or spilled.
219 /// @param renderFilePrefix This string will be appended to the function
220 /// name (before the output file suffix) to enable
221 /// multiple renderings from the same function.
222 void renderMachineFunction(const char *renderContextStr,
223 const VirtRegMap *vrm = 0,
224 const char *renderSuffix = 0);
229 friend raw_ostream& operator<<(raw_ostream &os, const Spacer &s);
235 MachineRegisterInfo *mri;
236 const TargetRegisterInfo *tri;
239 const VirtRegMap *vrm;
241 TargetRegisterExtraInfo trei;
242 MFRenderingOptions ro;
245 typedef enum { Dead, Defined, Used, AliveReg, AliveStack } LiveState;
246 LiveState getLiveStateAt(const LiveInterval *li, SlotIndex i) const;
248 typedef enum { Zero, Low, High } PressureState;
249 PressureState getPressureStateAt(const TargetRegisterClass *trc,
252 // ---------- Rendering methods ----------
254 /// For inserting spaces when pretty printing.
257 explicit Spacer(unsigned numSpaces) : ns(numSpaces) {}
258 Spacer operator+(const Spacer &o) const { return Spacer(ns + o.ns); }
259 void print(raw_ostream &os) const;
264 Spacer s(unsigned ns) const;
266 template <typename Iterator>
267 std::string escapeChars(Iterator sBegin, Iterator sEnd) const;
269 /// \brief Render a machine instruction.
270 void renderMachineInstr(raw_ostream &os,
271 const MachineInstr *mi) const;
273 /// \brief Render vertical text.
274 template <typename T>
275 void renderVertical(const Spacer &indent,
279 /// \brief Insert CSS layout info.
280 void insertCSS(const Spacer &indent,
281 raw_ostream &os) const;
283 /// \brief Render a brief summary of the function (including rendering
285 void renderFunctionSummary(const Spacer &indent,
287 const char * const renderContextStr) const;
289 /// \brief Render a legend for the pressure table.
290 void renderPressureTableLegend(const Spacer &indent,
291 raw_ostream &os) const;
293 /// \brief Render a consecutive set of HTML cells of the same class using
294 /// the colspan attribute for run-length encoding.
295 template <typename CellType>
296 void renderCellsWithRLE(
297 const Spacer &indent, raw_ostream &os,
298 const std::pair<CellType, unsigned> &rleAccumulator,
299 const std::map<CellType, std::string> &cellTypeStrs) const;
301 /// \brief Render code listing, potentially with register pressure
302 /// and live intervals shown alongside.
303 void renderCodeTablePlusPI(const Spacer &indent,
304 raw_ostream &os) const;
306 /// \brief Render the HTML page representing the MachineFunction.
307 void renderFunctionPage(raw_ostream &os,
308 const char * const renderContextStr) const;
310 std::string escapeChars(const std::string &s) const;
314 #endif /* LLVM_CODEGEN_RENDERMACHINEFUNCTION_H */