DiffConsumer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //===-- DiffConsumer.cpp - Difference Consumer ------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This files implements the LLVM difference Consumer
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "DiffConsumer.h"
  13. #include "llvm/IR/Instructions.h"
  14. #include "llvm/Support/Debug.h"
  15. #include "llvm/Support/ErrorHandling.h"
  16. using namespace llvm;
  17. static void ComputeNumbering(const Function *F,
  18. DenseMap<const Value *, unsigned> &Numbering) {
  19. unsigned IN = 0;
  20. // Arguments get the first numbers.
  21. for (const auto &Arg : F->args())
  22. if (!Arg.hasName())
  23. Numbering[&Arg] = IN++;
  24. // Walk the basic blocks in order.
  25. for (const auto &Func : *F) {
  26. if (!Func.hasName())
  27. Numbering[&Func] = IN++;
  28. // Walk the instructions in order.
  29. for (const auto &BB : Func)
  30. // void instructions don't get numbers.
  31. if (!BB.hasName() && !BB.getType()->isVoidTy())
  32. Numbering[&BB] = IN++;
  33. }
  34. assert(!Numbering.empty() && "asked for numbering but numbering was no-op");
  35. }
  36. void Consumer::anchor() { }
  37. void DiffConsumer::printValue(const Value *V, bool isL) {
  38. if (V->hasName()) {
  39. out << (isa<GlobalValue>(V) ? '@' : '%') << V->getName();
  40. return;
  41. }
  42. if (V->getType()->isVoidTy()) {
  43. if (auto *SI = dyn_cast<StoreInst>(V)) {
  44. out << "store to ";
  45. printValue(SI->getPointerOperand(), isL);
  46. } else if (auto *CI = dyn_cast<CallInst>(V)) {
  47. out << "call to ";
  48. printValue(CI->getCalledOperand(), isL);
  49. } else if (auto *II = dyn_cast<InvokeInst>(V)) {
  50. out << "invoke to ";
  51. printValue(II->getCalledOperand(), isL);
  52. } else {
  53. out << *V;
  54. }
  55. return;
  56. }
  57. if (isa<Constant>(V)) {
  58. out << *V;
  59. return;
  60. }
  61. unsigned N = contexts.size();
  62. while (N > 0) {
  63. --N;
  64. DiffContext &ctxt = contexts[N];
  65. if (!ctxt.IsFunction) continue;
  66. if (isL) {
  67. if (ctxt.LNumbering.empty())
  68. ComputeNumbering(cast<Function>(ctxt.L), ctxt.LNumbering);
  69. out << '%' << ctxt.LNumbering[V];
  70. return;
  71. } else {
  72. if (ctxt.RNumbering.empty())
  73. ComputeNumbering(cast<Function>(ctxt.R), ctxt.RNumbering);
  74. out << '%' << ctxt.RNumbering[V];
  75. return;
  76. }
  77. }
  78. out << "<anonymous>";
  79. }
  80. void DiffConsumer::header() {
  81. if (contexts.empty()) return;
  82. for (SmallVectorImpl<DiffContext>::iterator
  83. I = contexts.begin(), E = contexts.end(); I != E; ++I) {
  84. if (I->Differences) continue;
  85. if (isa<Function>(I->L)) {
  86. // Extra newline between functions.
  87. if (Differences) out << "\n";
  88. const Function *L = cast<Function>(I->L);
  89. const Function *R = cast<Function>(I->R);
  90. if (L->getName() != R->getName())
  91. out << "in function " << L->getName()
  92. << " / " << R->getName() << ":\n";
  93. else
  94. out << "in function " << L->getName() << ":\n";
  95. } else if (isa<BasicBlock>(I->L)) {
  96. const BasicBlock *L = cast<BasicBlock>(I->L);
  97. const BasicBlock *R = cast<BasicBlock>(I->R);
  98. if (L->hasName() && R->hasName() && L->getName() == R->getName())
  99. out << " in block %" << L->getName() << ":\n";
  100. else {
  101. out << " in block ";
  102. printValue(L, true);
  103. out << " / ";
  104. printValue(R, false);
  105. out << ":\n";
  106. }
  107. } else if (isa<Instruction>(I->L)) {
  108. out << " in instruction ";
  109. printValue(I->L, true);
  110. out << " / ";
  111. printValue(I->R, false);
  112. out << ":\n";
  113. }
  114. I->Differences = true;
  115. }
  116. }
  117. void DiffConsumer::indent() {
  118. unsigned N = Indent;
  119. while (N--) out << ' ';
  120. }
  121. void DiffConsumer::reset() {
  122. contexts.clear();
  123. Differences = false;
  124. Indent = 0;
  125. }
  126. bool DiffConsumer::hadDifferences() const {
  127. return Differences;
  128. }
  129. void DiffConsumer::enterContext(const Value *L, const Value *R) {
  130. contexts.push_back(DiffContext(L, R));
  131. Indent += 2;
  132. }
  133. void DiffConsumer::exitContext() {
  134. Differences |= contexts.back().Differences;
  135. contexts.pop_back();
  136. Indent -= 2;
  137. }
  138. void DiffConsumer::log(StringRef text) {
  139. header();
  140. indent();
  141. out << text << '\n';
  142. }
  143. void DiffConsumer::logf(const LogBuilder &Log) {
  144. header();
  145. indent();
  146. unsigned arg = 0;
  147. StringRef format = Log.getFormat();
  148. while (true) {
  149. size_t percent = format.find('%');
  150. if (percent == StringRef::npos) {
  151. out << format;
  152. break;
  153. }
  154. assert(format[percent] == '%');
  155. if (percent > 0) out << format.substr(0, percent);
  156. switch (format[percent+1]) {
  157. case '%': out << '%'; break;
  158. case 'l': printValue(Log.getArgument(arg++), true); break;
  159. case 'r': printValue(Log.getArgument(arg++), false); break;
  160. default: llvm_unreachable("unknown format character");
  161. }
  162. format = format.substr(percent+2);
  163. }
  164. out << '\n';
  165. }
  166. void DiffConsumer::logd(const DiffLogBuilder &Log) {
  167. header();
  168. for (unsigned I = 0, E = Log.getNumLines(); I != E; ++I) {
  169. indent();
  170. switch (Log.getLineKind(I)) {
  171. case DC_match:
  172. out << " ";
  173. Log.getLeft(I)->print(dbgs()); dbgs() << '\n';
  174. //printValue(Log.getLeft(I), true);
  175. break;
  176. case DC_left:
  177. out << "< ";
  178. Log.getLeft(I)->print(dbgs()); dbgs() << '\n';
  179. //printValue(Log.getLeft(I), true);
  180. break;
  181. case DC_right:
  182. out << "> ";
  183. Log.getRight(I)->print(dbgs()); dbgs() << '\n';
  184. //printValue(Log.getRight(I), false);
  185. break;
  186. }
  187. //out << "\n";
  188. }
  189. }