RenderMachineFunction.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. //===-- llvm/CodeGen/RenderMachineFunction.cpp - MF->HTML -----s-----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #define DEBUG_TYPE "rendermf"
  10. #include "RenderMachineFunction.h"
  11. #include "VirtRegMap.h"
  12. #include "llvm/Function.h"
  13. #include "llvm/Module.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineInstr.h"
  18. #include "llvm/CodeGen/MachineRegisterInfo.h"
  19. #include "llvm/Support/CommandLine.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include "llvm/Target/TargetMachine.h"
  23. #include <sstream>
  24. using namespace llvm;
  25. char RenderMachineFunction::ID = 0;
  26. INITIALIZE_PASS_BEGIN(RenderMachineFunction, "rendermf",
  27. "Render machine functions (and related info) to HTML pages",
  28. false, false)
  29. INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
  30. INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
  31. INITIALIZE_PASS_END(RenderMachineFunction, "rendermf",
  32. "Render machine functions (and related info) to HTML pages",
  33. false, false)
  34. static cl::opt<std::string>
  35. outputFileSuffix("rmf-file-suffix",
  36. cl::desc("Appended to function name to get output file name "
  37. "(default: \".html\")"),
  38. cl::init(".html"), cl::Hidden);
  39. static cl::opt<std::string>
  40. machineFuncsToRender("rmf-funcs",
  41. cl::desc("Coma seperated list of functions to render"
  42. ", or \"*\"."),
  43. cl::init(""), cl::Hidden);
  44. static cl::opt<std::string>
  45. pressureClasses("rmf-classes",
  46. cl::desc("Register classes to render pressure for."),
  47. cl::init(""), cl::Hidden);
  48. static cl::opt<std::string>
  49. showIntervals("rmf-intervals",
  50. cl::desc("Live intervals to show alongside code."),
  51. cl::init(""), cl::Hidden);
  52. static cl::opt<bool>
  53. filterEmpty("rmf-filter-empty-intervals",
  54. cl::desc("Don't display empty intervals."),
  55. cl::init(true), cl::Hidden);
  56. static cl::opt<bool>
  57. showEmptyIndexes("rmf-empty-indexes",
  58. cl::desc("Render indexes not associated with instructions or "
  59. "MBB starts."),
  60. cl::init(false), cl::Hidden);
  61. static cl::opt<bool>
  62. useFancyVerticals("rmf-fancy-verts",
  63. cl::desc("Use SVG for vertical text."),
  64. cl::init(true), cl::Hidden);
  65. static cl::opt<bool>
  66. prettyHTML("rmf-pretty-html",
  67. cl::desc("Pretty print HTML. For debugging the renderer only.."),
  68. cl::init(false), cl::Hidden);
  69. namespace llvm {
  70. bool MFRenderingOptions::renderingOptionsProcessed;
  71. std::set<std::string> MFRenderingOptions::mfNamesToRender;
  72. bool MFRenderingOptions::renderAllMFs = false;
  73. std::set<std::string> MFRenderingOptions::classNamesToRender;
  74. bool MFRenderingOptions::renderAllClasses = false;
  75. std::set<std::pair<unsigned, unsigned> >
  76. MFRenderingOptions::intervalNumsToRender;
  77. unsigned MFRenderingOptions::intervalTypesToRender = ExplicitOnly;
  78. template <typename OutputItr>
  79. void MFRenderingOptions::splitComaSeperatedList(const std::string &s,
  80. OutputItr outItr) {
  81. std::string::const_iterator curPos = s.begin();
  82. std::string::const_iterator nextComa = std::find(curPos, s.end(), ',');
  83. while (nextComa != s.end()) {
  84. std::string elem;
  85. std::copy(curPos, nextComa, std::back_inserter(elem));
  86. *outItr = elem;
  87. ++outItr;
  88. curPos = llvm::next(nextComa);
  89. nextComa = std::find(curPos, s.end(), ',');
  90. }
  91. if (curPos != s.end()) {
  92. std::string elem;
  93. std::copy(curPos, s.end(), std::back_inserter(elem));
  94. *outItr = elem;
  95. ++outItr;
  96. }
  97. }
  98. void MFRenderingOptions::processOptions() {
  99. if (!renderingOptionsProcessed) {
  100. processFuncNames();
  101. processRegClassNames();
  102. processIntervalNumbers();
  103. renderingOptionsProcessed = true;
  104. }
  105. }
  106. void MFRenderingOptions::processFuncNames() {
  107. if (machineFuncsToRender == "*") {
  108. renderAllMFs = true;
  109. } else {
  110. splitComaSeperatedList(machineFuncsToRender,
  111. std::inserter(mfNamesToRender,
  112. mfNamesToRender.begin()));
  113. }
  114. }
  115. void MFRenderingOptions::processRegClassNames() {
  116. if (pressureClasses == "*") {
  117. renderAllClasses = true;
  118. } else {
  119. splitComaSeperatedList(pressureClasses,
  120. std::inserter(classNamesToRender,
  121. classNamesToRender.begin()));
  122. }
  123. }
  124. void MFRenderingOptions::processIntervalNumbers() {
  125. std::set<std::string> intervalRanges;
  126. splitComaSeperatedList(showIntervals,
  127. std::inserter(intervalRanges,
  128. intervalRanges.begin()));
  129. std::for_each(intervalRanges.begin(), intervalRanges.end(),
  130. processIntervalRange);
  131. }
  132. void MFRenderingOptions::processIntervalRange(
  133. const std::string &intervalRangeStr) {
  134. if (intervalRangeStr == "*") {
  135. intervalTypesToRender |= All;
  136. } else if (intervalRangeStr == "virt-nospills*") {
  137. intervalTypesToRender |= VirtNoSpills;
  138. } else if (intervalRangeStr == "spills*") {
  139. intervalTypesToRender |= VirtSpills;
  140. } else if (intervalRangeStr == "virt*") {
  141. intervalTypesToRender |= AllVirt;
  142. } else if (intervalRangeStr == "phys*") {
  143. intervalTypesToRender |= AllPhys;
  144. } else {
  145. std::istringstream iss(intervalRangeStr);
  146. unsigned reg1, reg2;
  147. if ((iss >> reg1 >> std::ws)) {
  148. if (iss.eof()) {
  149. intervalNumsToRender.insert(std::make_pair(reg1, reg1 + 1));
  150. } else {
  151. char c;
  152. iss >> c;
  153. if (c == '-' && (iss >> reg2)) {
  154. intervalNumsToRender.insert(std::make_pair(reg1, reg2 + 1));
  155. } else {
  156. dbgs() << "Warning: Invalid interval range \""
  157. << intervalRangeStr << "\" in -rmf-intervals. Skipping.\n";
  158. }
  159. }
  160. } else {
  161. dbgs() << "Warning: Invalid interval number \""
  162. << intervalRangeStr << "\" in -rmf-intervals. Skipping.\n";
  163. }
  164. }
  165. }
  166. void MFRenderingOptions::setup(MachineFunction *mf,
  167. const TargetRegisterInfo *tri,
  168. LiveIntervals *lis,
  169. const RenderMachineFunction *rmf) {
  170. this->mf = mf;
  171. this->tri = tri;
  172. this->lis = lis;
  173. this->rmf = rmf;
  174. clear();
  175. }
  176. void MFRenderingOptions::clear() {
  177. regClassesTranslatedToCurrentFunction = false;
  178. regClassSet.clear();
  179. intervalsTranslatedToCurrentFunction = false;
  180. intervalSet.clear();
  181. }
  182. void MFRenderingOptions::resetRenderSpecificOptions() {
  183. intervalSet.clear();
  184. intervalsTranslatedToCurrentFunction = false;
  185. }
  186. bool MFRenderingOptions::shouldRenderCurrentMachineFunction() const {
  187. processOptions();
  188. return (renderAllMFs ||
  189. mfNamesToRender.find(mf->getFunction()->getName()) !=
  190. mfNamesToRender.end());
  191. }
  192. const MFRenderingOptions::RegClassSet& MFRenderingOptions::regClasses() const{
  193. translateRegClassNamesToCurrentFunction();
  194. return regClassSet;
  195. }
  196. const MFRenderingOptions::IntervalSet& MFRenderingOptions::intervals() const {
  197. translateIntervalNumbersToCurrentFunction();
  198. return intervalSet;
  199. }
  200. bool MFRenderingOptions::renderEmptyIndexes() const {
  201. return showEmptyIndexes;
  202. }
  203. bool MFRenderingOptions::fancyVerticals() const {
  204. return useFancyVerticals;
  205. }
  206. void MFRenderingOptions::translateRegClassNamesToCurrentFunction() const {
  207. if (!regClassesTranslatedToCurrentFunction) {
  208. processOptions();
  209. for (TargetRegisterInfo::regclass_iterator rcItr = tri->regclass_begin(),
  210. rcEnd = tri->regclass_end();
  211. rcItr != rcEnd; ++rcItr) {
  212. const TargetRegisterClass *trc = *rcItr;
  213. if (renderAllClasses ||
  214. classNamesToRender.find(trc->getName()) !=
  215. classNamesToRender.end()) {
  216. regClassSet.insert(trc);
  217. }
  218. }
  219. regClassesTranslatedToCurrentFunction = true;
  220. }
  221. }
  222. void MFRenderingOptions::translateIntervalNumbersToCurrentFunction() const {
  223. if (!intervalsTranslatedToCurrentFunction) {
  224. processOptions();
  225. // If we're not just doing explicit then do a copy over all matching
  226. // types.
  227. if (intervalTypesToRender != ExplicitOnly) {
  228. for (LiveIntervals::iterator liItr = lis->begin(), liEnd = lis->end();
  229. liItr != liEnd; ++liItr) {
  230. LiveInterval *li = liItr->second;
  231. if (filterEmpty && li->empty())
  232. continue;
  233. if ((TargetRegisterInfo::isPhysicalRegister(li->reg) &&
  234. (intervalTypesToRender & AllPhys))) {
  235. intervalSet.insert(li);
  236. } else if (TargetRegisterInfo::isVirtualRegister(li->reg)) {
  237. if (((intervalTypesToRender & VirtNoSpills) && !rmf->isSpill(li)) ||
  238. ((intervalTypesToRender & VirtSpills) && rmf->isSpill(li))) {
  239. intervalSet.insert(li);
  240. }
  241. }
  242. }
  243. }
  244. // If we need to process the explicit list...
  245. if (intervalTypesToRender != All) {
  246. for (std::set<std::pair<unsigned, unsigned> >::const_iterator
  247. regRangeItr = intervalNumsToRender.begin(),
  248. regRangeEnd = intervalNumsToRender.end();
  249. regRangeItr != regRangeEnd; ++regRangeItr) {
  250. const std::pair<unsigned, unsigned> &range = *regRangeItr;
  251. for (unsigned reg = range.first; reg != range.second; ++reg) {
  252. if (lis->hasInterval(reg)) {
  253. intervalSet.insert(&lis->getInterval(reg));
  254. }
  255. }
  256. }
  257. }
  258. intervalsTranslatedToCurrentFunction = true;
  259. }
  260. }
  261. // ---------- TargetRegisterExtraInformation implementation ----------
  262. TargetRegisterExtraInfo::TargetRegisterExtraInfo()
  263. : mapsPopulated(false) {
  264. }
  265. void TargetRegisterExtraInfo::setup(MachineFunction *mf,
  266. MachineRegisterInfo *mri,
  267. const TargetRegisterInfo *tri,
  268. LiveIntervals *lis) {
  269. this->mf = mf;
  270. this->mri = mri;
  271. this->tri = tri;
  272. this->lis = lis;
  273. }
  274. void TargetRegisterExtraInfo::reset() {
  275. if (!mapsPopulated) {
  276. initWorst();
  277. //initBounds();
  278. initCapacity();
  279. mapsPopulated = true;
  280. }
  281. resetPressureAndLiveStates();
  282. }
  283. void TargetRegisterExtraInfo::clear() {
  284. prWorst.clear();
  285. vrWorst.clear();
  286. capacityMap.clear();
  287. pressureMap.clear();
  288. //liveStatesMap.clear();
  289. mapsPopulated = false;
  290. }
  291. void TargetRegisterExtraInfo::initWorst() {
  292. assert(!mapsPopulated && prWorst.empty() && vrWorst.empty() &&
  293. "Worst map already initialised?");
  294. // Start with the physical registers.
  295. for (unsigned preg = 1; preg < tri->getNumRegs(); ++preg) {
  296. WorstMapLine &pregLine = prWorst[preg];
  297. for (TargetRegisterInfo::regclass_iterator rcItr = tri->regclass_begin(),
  298. rcEnd = tri->regclass_end();
  299. rcItr != rcEnd; ++rcItr) {
  300. const TargetRegisterClass *trc = *rcItr;
  301. unsigned numOverlaps = 0;
  302. for (TargetRegisterClass::iterator rItr = trc->begin(),
  303. rEnd = trc->end();
  304. rItr != rEnd; ++rItr) {
  305. unsigned trcPReg = *rItr;
  306. if (tri->regsOverlap(preg, trcPReg))
  307. ++numOverlaps;
  308. }
  309. pregLine[trc] = numOverlaps;
  310. }
  311. }
  312. // Now the register classes.
  313. for (TargetRegisterInfo::regclass_iterator rc1Itr = tri->regclass_begin(),
  314. rcEnd = tri->regclass_end();
  315. rc1Itr != rcEnd; ++rc1Itr) {
  316. const TargetRegisterClass *trc1 = *rc1Itr;
  317. WorstMapLine &classLine = vrWorst[trc1];
  318. for (TargetRegisterInfo::regclass_iterator rc2Itr = tri->regclass_begin();
  319. rc2Itr != rcEnd; ++rc2Itr) {
  320. const TargetRegisterClass *trc2 = *rc2Itr;
  321. unsigned worst = 0;
  322. for (TargetRegisterClass::iterator trc1Itr = trc1->begin(),
  323. trc1End = trc1->end();
  324. trc1Itr != trc1End; ++trc1Itr) {
  325. unsigned trc1Reg = *trc1Itr;
  326. unsigned trc1RegWorst = 0;
  327. for (TargetRegisterClass::iterator trc2Itr = trc2->begin(),
  328. trc2End = trc2->end();
  329. trc2Itr != trc2End; ++trc2Itr) {
  330. unsigned trc2Reg = *trc2Itr;
  331. if (tri->regsOverlap(trc1Reg, trc2Reg))
  332. ++trc1RegWorst;
  333. }
  334. if (trc1RegWorst > worst) {
  335. worst = trc1RegWorst;
  336. }
  337. }
  338. if (worst != 0) {
  339. classLine[trc2] = worst;
  340. }
  341. }
  342. }
  343. }
  344. unsigned TargetRegisterExtraInfo::getWorst(
  345. unsigned reg,
  346. const TargetRegisterClass *trc) const {
  347. const WorstMapLine *wml = 0;
  348. if (TargetRegisterInfo::isPhysicalRegister(reg)) {
  349. PRWorstMap::const_iterator prwItr = prWorst.find(reg);
  350. assert(prwItr != prWorst.end() && "Missing prWorst entry.");
  351. wml = &prwItr->second;
  352. } else {
  353. const TargetRegisterClass *regTRC = mri->getRegClass(reg);
  354. VRWorstMap::const_iterator vrwItr = vrWorst.find(regTRC);
  355. assert(vrwItr != vrWorst.end() && "Missing vrWorst entry.");
  356. wml = &vrwItr->second;
  357. }
  358. WorstMapLine::const_iterator wmlItr = wml->find(trc);
  359. if (wmlItr == wml->end())
  360. return 0;
  361. return wmlItr->second;
  362. }
  363. void TargetRegisterExtraInfo::initCapacity() {
  364. assert(!mapsPopulated && capacityMap.empty() &&
  365. "Capacity map already initialised?");
  366. for (TargetRegisterInfo::regclass_iterator rcItr = tri->regclass_begin(),
  367. rcEnd = tri->regclass_end();
  368. rcItr != rcEnd; ++rcItr) {
  369. const TargetRegisterClass *trc = *rcItr;
  370. unsigned capacity = std::distance(trc->allocation_order_begin(*mf),
  371. trc->allocation_order_end(*mf));
  372. if (capacity != 0)
  373. capacityMap[trc] = capacity;
  374. }
  375. }
  376. unsigned TargetRegisterExtraInfo::getCapacity(
  377. const TargetRegisterClass *trc) const {
  378. CapacityMap::const_iterator cmItr = capacityMap.find(trc);
  379. assert(cmItr != capacityMap.end() &&
  380. "vreg with unallocable register class");
  381. return cmItr->second;
  382. }
  383. void TargetRegisterExtraInfo::resetPressureAndLiveStates() {
  384. pressureMap.clear();
  385. //liveStatesMap.clear();
  386. // Iterate over all slots.
  387. // Iterate over all live intervals.
  388. for (LiveIntervals::iterator liItr = lis->begin(),
  389. liEnd = lis->end();
  390. liItr != liEnd; ++liItr) {
  391. LiveInterval *li = liItr->second;
  392. const TargetRegisterClass *liTRC;
  393. if (TargetRegisterInfo::isPhysicalRegister(li->reg))
  394. continue;
  395. liTRC = mri->getRegClass(li->reg);
  396. // For all ranges in the current interal.
  397. for (LiveInterval::iterator lrItr = li->begin(),
  398. lrEnd = li->end();
  399. lrItr != lrEnd; ++lrItr) {
  400. LiveRange *lr = &*lrItr;
  401. // For all slots in the current range.
  402. for (SlotIndex i = lr->start; i != lr->end; i = i.getNextSlot()) {
  403. // Record increased pressure at index for all overlapping classes.
  404. for (TargetRegisterInfo::regclass_iterator
  405. rcItr = tri->regclass_begin(),
  406. rcEnd = tri->regclass_end();
  407. rcItr != rcEnd; ++rcItr) {
  408. const TargetRegisterClass *trc = *rcItr;
  409. if (trc->allocation_order_begin(*mf) ==
  410. trc->allocation_order_end(*mf))
  411. continue;
  412. unsigned worstAtI = getWorst(li->reg, trc);
  413. if (worstAtI != 0) {
  414. pressureMap[i][trc] += worstAtI;
  415. }
  416. }
  417. }
  418. }
  419. }
  420. }
  421. unsigned TargetRegisterExtraInfo::getPressureAtSlot(
  422. const TargetRegisterClass *trc,
  423. SlotIndex i) const {
  424. PressureMap::const_iterator pmItr = pressureMap.find(i);
  425. if (pmItr == pressureMap.end())
  426. return 0;
  427. const PressureMapLine &pmLine = pmItr->second;
  428. PressureMapLine::const_iterator pmlItr = pmLine.find(trc);
  429. if (pmlItr == pmLine.end())
  430. return 0;
  431. return pmlItr->second;
  432. }
  433. bool TargetRegisterExtraInfo::classOverCapacityAtSlot(
  434. const TargetRegisterClass *trc,
  435. SlotIndex i) const {
  436. return (getPressureAtSlot(trc, i) > getCapacity(trc));
  437. }
  438. // ---------- MachineFunctionRenderer implementation ----------
  439. void RenderMachineFunction::Spacer::print(raw_ostream &os) const {
  440. if (!prettyHTML)
  441. return;
  442. for (unsigned i = 0; i < ns; ++i) {
  443. os << " ";
  444. }
  445. }
  446. RenderMachineFunction::Spacer RenderMachineFunction::s(unsigned ns) const {
  447. return Spacer(ns);
  448. }
  449. raw_ostream& operator<<(raw_ostream &os, const RenderMachineFunction::Spacer &s) {
  450. s.print(os);
  451. return os;
  452. }
  453. template <typename Iterator>
  454. std::string RenderMachineFunction::escapeChars(Iterator sBegin, Iterator sEnd) const {
  455. std::string r;
  456. for (Iterator sItr = sBegin; sItr != sEnd; ++sItr) {
  457. char c = *sItr;
  458. switch (c) {
  459. case '<': r.append("&lt;"); break;
  460. case '>': r.append("&gt;"); break;
  461. case '&': r.append("&amp;"); break;
  462. case ' ': r.append("&nbsp;"); break;
  463. case '\"': r.append("&quot;"); break;
  464. default: r.push_back(c); break;
  465. }
  466. }
  467. return r;
  468. }
  469. RenderMachineFunction::LiveState
  470. RenderMachineFunction::getLiveStateAt(const LiveInterval *li,
  471. SlotIndex i) const {
  472. const MachineInstr *mi = sis->getInstructionFromIndex(i);
  473. // For uses/defs recorded use/def indexes override current liveness and
  474. // instruction operands (Only for the interval which records the indexes).
  475. if (i.isUse() || i.isDef()) {
  476. UseDefs::const_iterator udItr = useDefs.find(li);
  477. if (udItr != useDefs.end()) {
  478. const SlotSet &slotSet = udItr->second;
  479. if (slotSet.count(i)) {
  480. if (i.isUse()) {
  481. return Used;
  482. }
  483. // else
  484. return Defined;
  485. }
  486. }
  487. }
  488. // If the slot is a load/store, or there's no info in the use/def set then
  489. // use liveness and instruction operand info.
  490. if (li->liveAt(i)) {
  491. if (mi == 0) {
  492. if (vrm == 0 ||
  493. (vrm->getStackSlot(li->reg) == VirtRegMap::NO_STACK_SLOT)) {
  494. return AliveReg;
  495. } else {
  496. return AliveStack;
  497. }
  498. } else {
  499. if (i.isDef() && mi->definesRegister(li->reg, tri)) {
  500. return Defined;
  501. } else if (i.isUse() && mi->readsRegister(li->reg)) {
  502. return Used;
  503. } else {
  504. if (vrm == 0 ||
  505. (vrm->getStackSlot(li->reg) == VirtRegMap::NO_STACK_SLOT)) {
  506. return AliveReg;
  507. } else {
  508. return AliveStack;
  509. }
  510. }
  511. }
  512. }
  513. return Dead;
  514. }
  515. RenderMachineFunction::PressureState
  516. RenderMachineFunction::getPressureStateAt(const TargetRegisterClass *trc,
  517. SlotIndex i) const {
  518. if (trei.getPressureAtSlot(trc, i) == 0) {
  519. return Zero;
  520. } else if (trei.classOverCapacityAtSlot(trc, i)){
  521. return High;
  522. }
  523. return Low;
  524. }
  525. /// \brief Render a machine instruction.
  526. void RenderMachineFunction::renderMachineInstr(raw_ostream &os,
  527. const MachineInstr *mi) const {
  528. std::string s;
  529. raw_string_ostream oss(s);
  530. oss << *mi;
  531. os << escapeChars(oss.str());
  532. }
  533. template <typename T>
  534. void RenderMachineFunction::renderVertical(const Spacer &indent,
  535. raw_ostream &os,
  536. const T &t) const {
  537. if (ro.fancyVerticals()) {
  538. os << indent << "<object\n"
  539. << indent + s(2) << "class=\"obj\"\n"
  540. << indent + s(2) << "type=\"image/svg+xml\"\n"
  541. << indent + s(2) << "width=\"14px\"\n"
  542. << indent + s(2) << "height=\"55px\"\n"
  543. << indent + s(2) << "data=\"data:image/svg+xml,\n"
  544. << indent + s(4) << "<svg xmlns='http://www.w3.org/2000/svg'>\n"
  545. << indent + s(6) << "<text x='-55' y='10' "
  546. "font-family='Courier' font-size='12' "
  547. "transform='rotate(-90)' "
  548. "text-rendering='optimizeSpeed' "
  549. "fill='#000'>" << t << "</text>\n"
  550. << indent + s(4) << "</svg>\">\n"
  551. << indent << "</object>\n";
  552. } else {
  553. std::ostringstream oss;
  554. oss << t;
  555. std::string tStr(oss.str());
  556. os << indent;
  557. for (std::string::iterator tStrItr = tStr.begin(), tStrEnd = tStr.end();
  558. tStrItr != tStrEnd; ++tStrItr) {
  559. os << *tStrItr << "<br/>";
  560. }
  561. os << "\n";
  562. }
  563. }
  564. void RenderMachineFunction::insertCSS(const Spacer &indent,
  565. raw_ostream &os) const {
  566. os << indent << "<style type=\"text/css\">\n"
  567. << indent + s(2) << "body { font-color: black; }\n"
  568. << indent + s(2) << "table.code td { font-family: monospace; "
  569. "border-width: 0px; border-style: solid; "
  570. "border-bottom: 1px solid #dddddd; white-space: nowrap; }\n"
  571. << indent + s(2) << "table.code td.p-z { background-color: #000000; }\n"
  572. << indent + s(2) << "table.code td.p-l { background-color: #00ff00; }\n"
  573. << indent + s(2) << "table.code td.p-h { background-color: #ff0000; }\n"
  574. << indent + s(2) << "table.code td.l-n { background-color: #ffffff; }\n"
  575. << indent + s(2) << "table.code td.l-d { background-color: #ff0000; }\n"
  576. << indent + s(2) << "table.code td.l-u { background-color: #ffff00; }\n"
  577. << indent + s(2) << "table.code td.l-r { background-color: #000000; }\n"
  578. << indent + s(2) << "table.code td.l-s { background-color: #770000; }\n"
  579. << indent + s(2) << "table.code th { border-width: 0px; "
  580. "border-style: solid; }\n"
  581. << indent << "</style>\n";
  582. }
  583. void RenderMachineFunction::renderFunctionSummary(
  584. const Spacer &indent, raw_ostream &os,
  585. const char * const renderContextStr) const {
  586. os << indent << "<h1>Function: " << mf->getFunction()->getName()
  587. << "</h1>\n"
  588. << indent << "<h2>Rendering context: " << renderContextStr << "</h2>\n";
  589. }
  590. void RenderMachineFunction::renderPressureTableLegend(
  591. const Spacer &indent,
  592. raw_ostream &os) const {
  593. os << indent << "<h2>Rendering Pressure Legend:</h2>\n"
  594. << indent << "<table class=\"code\">\n"
  595. << indent + s(2) << "<tr>\n"
  596. << indent + s(4) << "<th>Pressure</th><th>Description</th>"
  597. "<th>Appearance</th>\n"
  598. << indent + s(2) << "</tr>\n"
  599. << indent + s(2) << "<tr>\n"
  600. << indent + s(4) << "<td>No Pressure</td>"
  601. "<td>No physical registers of this class requested.</td>"
  602. "<td class=\"p-z\">&nbsp;&nbsp;</td>\n"
  603. << indent + s(2) << "</tr>\n"
  604. << indent + s(2) << "<tr>\n"
  605. << indent + s(4) << "<td>Low Pressure</td>"
  606. "<td>Sufficient physical registers to meet demand.</td>"
  607. "<td class=\"p-l\">&nbsp;&nbsp;</td>\n"
  608. << indent + s(2) << "</tr>\n"
  609. << indent + s(2) << "<tr>\n"
  610. << indent + s(4) << "<td>High Pressure</td>"
  611. "<td>Potentially insufficient physical registers to meet demand.</td>"
  612. "<td class=\"p-h\">&nbsp;&nbsp;</td>\n"
  613. << indent + s(2) << "</tr>\n"
  614. << indent << "</table>\n";
  615. }
  616. template <typename CellType>
  617. void RenderMachineFunction::renderCellsWithRLE(
  618. const Spacer &indent, raw_ostream &os,
  619. const std::pair<CellType, unsigned> &rleAccumulator,
  620. const std::map<CellType, std::string> &cellTypeStrs) const {
  621. if (rleAccumulator.second == 0)
  622. return;
  623. typename std::map<CellType, std::string>::const_iterator ctsItr =
  624. cellTypeStrs.find(rleAccumulator.first);
  625. assert(ctsItr != cellTypeStrs.end() && "No string for given cell type.");
  626. os << indent + s(4) << "<td class=\"" << ctsItr->second << "\"";
  627. if (rleAccumulator.second > 1)
  628. os << " colspan=" << rleAccumulator.second;
  629. os << "></td>\n";
  630. }
  631. void RenderMachineFunction::renderCodeTablePlusPI(const Spacer &indent,
  632. raw_ostream &os) const {
  633. std::map<LiveState, std::string> lsStrs;
  634. lsStrs[Dead] = "l-n";
  635. lsStrs[Defined] = "l-d";
  636. lsStrs[Used] = "l-u";
  637. lsStrs[AliveReg] = "l-r";
  638. lsStrs[AliveStack] = "l-s";
  639. std::map<PressureState, std::string> psStrs;
  640. psStrs[Zero] = "p-z";
  641. psStrs[Low] = "p-l";
  642. psStrs[High] = "p-h";
  643. // Open the table...
  644. os << indent << "<table cellpadding=0 cellspacing=0 class=\"code\">\n"
  645. << indent + s(2) << "<tr>\n";
  646. // Render the header row...
  647. os << indent + s(4) << "<th>index</th>\n"
  648. << indent + s(4) << "<th>instr</th>\n";
  649. // Render class names if necessary...
  650. if (!ro.regClasses().empty()) {
  651. for (MFRenderingOptions::RegClassSet::const_iterator
  652. rcItr = ro.regClasses().begin(),
  653. rcEnd = ro.regClasses().end();
  654. rcItr != rcEnd; ++rcItr) {
  655. const TargetRegisterClass *trc = *rcItr;
  656. os << indent + s(4) << "<th>\n";
  657. renderVertical(indent + s(6), os, trc->getName());
  658. os << indent + s(4) << "</th>\n";
  659. }
  660. }
  661. // FIXME: Is there a nicer way to insert space between columns in HTML?
  662. if (!ro.regClasses().empty() && !ro.intervals().empty())
  663. os << indent + s(4) << "<th>&nbsp;&nbsp;</th>\n";
  664. // Render interval numbers if necessary...
  665. if (!ro.intervals().empty()) {
  666. for (MFRenderingOptions::IntervalSet::const_iterator
  667. liItr = ro.intervals().begin(),
  668. liEnd = ro.intervals().end();
  669. liItr != liEnd; ++liItr) {
  670. const LiveInterval *li = *liItr;
  671. os << indent + s(4) << "<th>\n";
  672. renderVertical(indent + s(6), os, li->reg);
  673. os << indent + s(4) << "</th>\n";
  674. }
  675. }
  676. os << indent + s(2) << "</tr>\n";
  677. // End header row, start with the data rows...
  678. MachineInstr *mi = 0;
  679. // Data rows:
  680. for (SlotIndex i = sis->getZeroIndex(); i != sis->getLastIndex();
  681. i = i.getNextSlot()) {
  682. // Render the slot column.
  683. os << indent + s(2) << "<tr height=6ex>\n";
  684. // Render the code column.
  685. if (i.isLoad()) {
  686. MachineBasicBlock *mbb = sis->getMBBFromIndex(i);
  687. mi = sis->getInstructionFromIndex(i);
  688. if (i == sis->getMBBStartIdx(mbb) || mi != 0 ||
  689. ro.renderEmptyIndexes()) {
  690. os << indent + s(4) << "<td rowspan=4>" << i << "&nbsp;</td>\n"
  691. << indent + s(4) << "<td rowspan=4>\n";
  692. if (i == sis->getMBBStartIdx(mbb)) {
  693. os << indent + s(6) << "BB#" << mbb->getNumber() << ":&nbsp;\n";
  694. } else if (mi != 0) {
  695. os << indent + s(6) << "&nbsp;&nbsp;";
  696. renderMachineInstr(os, mi);
  697. } else {
  698. // Empty interval - leave blank.
  699. }
  700. os << indent + s(4) << "</td>\n";
  701. } else {
  702. i = i.getStoreIndex(); // <- Will be incremented to the next index.
  703. continue;
  704. }
  705. }
  706. // Render the class columns.
  707. if (!ro.regClasses().empty()) {
  708. std::pair<PressureState, unsigned> psRLEAccumulator(Zero, 0);
  709. for (MFRenderingOptions::RegClassSet::const_iterator
  710. rcItr = ro.regClasses().begin(),
  711. rcEnd = ro.regClasses().end();
  712. rcItr != rcEnd; ++rcItr) {
  713. const TargetRegisterClass *trc = *rcItr;
  714. PressureState newPressure = getPressureStateAt(trc, i);
  715. if (newPressure == psRLEAccumulator.first) {
  716. ++psRLEAccumulator.second;
  717. } else {
  718. renderCellsWithRLE(indent + s(4), os, psRLEAccumulator, psStrs);
  719. psRLEAccumulator.first = newPressure;
  720. psRLEAccumulator.second = 1;
  721. }
  722. }
  723. renderCellsWithRLE(indent + s(4), os, psRLEAccumulator, psStrs);
  724. }
  725. // FIXME: Is there a nicer way to insert space between columns in HTML?
  726. if (!ro.regClasses().empty() && !ro.intervals().empty())
  727. os << indent + s(4) << "<td width=2em></td>\n";
  728. if (!ro.intervals().empty()) {
  729. std::pair<LiveState, unsigned> lsRLEAccumulator(Dead, 0);
  730. for (MFRenderingOptions::IntervalSet::const_iterator
  731. liItr = ro.intervals().begin(),
  732. liEnd = ro.intervals().end();
  733. liItr != liEnd; ++liItr) {
  734. const LiveInterval *li = *liItr;
  735. LiveState newLiveness = getLiveStateAt(li, i);
  736. if (newLiveness == lsRLEAccumulator.first) {
  737. ++lsRLEAccumulator.second;
  738. } else {
  739. renderCellsWithRLE(indent + s(4), os, lsRLEAccumulator, lsStrs);
  740. lsRLEAccumulator.first = newLiveness;
  741. lsRLEAccumulator.second = 1;
  742. }
  743. }
  744. renderCellsWithRLE(indent + s(4), os, lsRLEAccumulator, lsStrs);
  745. }
  746. os << indent + s(2) << "</tr>\n";
  747. }
  748. os << indent << "</table>\n";
  749. if (!ro.regClasses().empty())
  750. renderPressureTableLegend(indent, os);
  751. }
  752. void RenderMachineFunction::renderFunctionPage(
  753. raw_ostream &os,
  754. const char * const renderContextStr) const {
  755. os << "<html>\n"
  756. << s(2) << "<head>\n"
  757. << s(4) << "<title>" << fqn << "</title>\n";
  758. insertCSS(s(4), os);
  759. os << s(2) << "<head>\n"
  760. << s(2) << "<body >\n";
  761. renderFunctionSummary(s(4), os, renderContextStr);
  762. os << s(4) << "<br/><br/><br/>\n";
  763. //renderLiveIntervalInfoTable(" ", os);
  764. os << s(4) << "<br/><br/><br/>\n";
  765. renderCodeTablePlusPI(s(4), os);
  766. os << s(2) << "</body>\n"
  767. << "</html>\n";
  768. }
  769. void RenderMachineFunction::getAnalysisUsage(AnalysisUsage &au) const {
  770. au.addRequired<SlotIndexes>();
  771. au.addRequired<LiveIntervals>();
  772. au.setPreservesAll();
  773. MachineFunctionPass::getAnalysisUsage(au);
  774. }
  775. bool RenderMachineFunction::runOnMachineFunction(MachineFunction &fn) {
  776. mf = &fn;
  777. mri = &mf->getRegInfo();
  778. tri = mf->getTarget().getRegisterInfo();
  779. lis = &getAnalysis<LiveIntervals>();
  780. sis = &getAnalysis<SlotIndexes>();
  781. trei.setup(mf, mri, tri, lis);
  782. ro.setup(mf, tri, lis, this);
  783. spillIntervals.clear();
  784. spillFor.clear();
  785. useDefs.clear();
  786. fqn = mf->getFunction()->getParent()->getModuleIdentifier() + "." +
  787. mf->getFunction()->getName().str();
  788. return false;
  789. }
  790. void RenderMachineFunction::releaseMemory() {
  791. trei.clear();
  792. ro.clear();
  793. spillIntervals.clear();
  794. spillFor.clear();
  795. useDefs.clear();
  796. }
  797. void RenderMachineFunction::rememberUseDefs(const LiveInterval *li) {
  798. if (!ro.shouldRenderCurrentMachineFunction())
  799. return;
  800. for (MachineRegisterInfo::reg_iterator rItr = mri->reg_begin(li->reg),
  801. rEnd = mri->reg_end();
  802. rItr != rEnd; ++rItr) {
  803. const MachineInstr *mi = &*rItr;
  804. if (mi->readsRegister(li->reg)) {
  805. useDefs[li].insert(lis->getInstructionIndex(mi).getUseIndex());
  806. }
  807. if (mi->definesRegister(li->reg)) {
  808. useDefs[li].insert(lis->getInstructionIndex(mi).getDefIndex());
  809. }
  810. }
  811. }
  812. void RenderMachineFunction::rememberSpills(
  813. const LiveInterval *li,
  814. const std::vector<LiveInterval*> &spills) {
  815. if (!ro.shouldRenderCurrentMachineFunction())
  816. return;
  817. for (std::vector<LiveInterval*>::const_iterator siItr = spills.begin(),
  818. siEnd = spills.end();
  819. siItr != siEnd; ++siItr) {
  820. const LiveInterval *spill = *siItr;
  821. spillIntervals[li].insert(spill);
  822. spillFor[spill] = li;
  823. }
  824. }
  825. bool RenderMachineFunction::isSpill(const LiveInterval *li) const {
  826. SpillForMap::const_iterator sfItr = spillFor.find(li);
  827. if (sfItr == spillFor.end())
  828. return false;
  829. return true;
  830. }
  831. void RenderMachineFunction::renderMachineFunction(
  832. const char *renderContextStr,
  833. const VirtRegMap *vrm,
  834. const char *renderSuffix) {
  835. if (!ro.shouldRenderCurrentMachineFunction())
  836. return;
  837. this->vrm = vrm;
  838. trei.reset();
  839. std::string rpFileName(mf->getFunction()->getName().str() +
  840. (renderSuffix ? renderSuffix : "") +
  841. outputFileSuffix);
  842. std::string errMsg;
  843. raw_fd_ostream outFile(rpFileName.c_str(), errMsg, raw_fd_ostream::F_Binary);
  844. renderFunctionPage(outFile, renderContextStr);
  845. ro.resetRenderSpecificOptions();
  846. }
  847. std::string RenderMachineFunction::escapeChars(const std::string &s) const {
  848. return escapeChars(s.begin(), s.end());
  849. }
  850. }