DiffConsumer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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(Function *F, DenseMap<Value*,unsigned> &Numbering){
  18. unsigned IN = 0;
  19. // Arguments get the first numbers.
  20. for (Function::arg_iterator
  21. AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI)
  22. if (!AI->hasName())
  23. Numbering[&*AI] = IN++;
  24. // Walk the basic blocks in order.
  25. for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
  26. if (!FI->hasName())
  27. Numbering[&*FI] = IN++;
  28. // Walk the instructions in order.
  29. for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
  30. // void instructions don't get numbers.
  31. if (!BI->hasName() && !BI->getType()->isVoidTy())
  32. Numbering[&*BI] = IN++;
  33. }
  34. assert(!Numbering.empty() && "asked for numbering but numbering was no-op");
  35. }
  36. void Consumer::anchor() { }
  37. void DiffConsumer::printValue(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. Function *L = cast<Function>(I->L);
  89. 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. BasicBlock *L = cast<BasicBlock>(I->L);
  97. 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. bool DiffConsumer::hadDifferences() const {
  122. return Differences;
  123. }
  124. void DiffConsumer::enterContext(Value *L, Value *R) {
  125. contexts.push_back(DiffContext(L, R));
  126. Indent += 2;
  127. }
  128. void DiffConsumer::exitContext() {
  129. Differences |= contexts.back().Differences;
  130. contexts.pop_back();
  131. Indent -= 2;
  132. }
  133. void DiffConsumer::log(StringRef text) {
  134. header();
  135. indent();
  136. out << text << '\n';
  137. }
  138. void DiffConsumer::logf(const LogBuilder &Log) {
  139. header();
  140. indent();
  141. unsigned arg = 0;
  142. StringRef format = Log.getFormat();
  143. while (true) {
  144. size_t percent = format.find('%');
  145. if (percent == StringRef::npos) {
  146. out << format;
  147. break;
  148. }
  149. assert(format[percent] == '%');
  150. if (percent > 0) out << format.substr(0, percent);
  151. switch (format[percent+1]) {
  152. case '%': out << '%'; break;
  153. case 'l': printValue(Log.getArgument(arg++), true); break;
  154. case 'r': printValue(Log.getArgument(arg++), false); break;
  155. default: llvm_unreachable("unknown format character");
  156. }
  157. format = format.substr(percent+2);
  158. }
  159. out << '\n';
  160. }
  161. void DiffConsumer::logd(const DiffLogBuilder &Log) {
  162. header();
  163. for (unsigned I = 0, E = Log.getNumLines(); I != E; ++I) {
  164. indent();
  165. switch (Log.getLineKind(I)) {
  166. case DC_match:
  167. out << " ";
  168. Log.getLeft(I)->print(dbgs()); dbgs() << '\n';
  169. //printValue(Log.getLeft(I), true);
  170. break;
  171. case DC_left:
  172. out << "< ";
  173. Log.getLeft(I)->print(dbgs()); dbgs() << '\n';
  174. //printValue(Log.getLeft(I), true);
  175. break;
  176. case DC_right:
  177. out << "> ";
  178. Log.getRight(I)->print(dbgs()); dbgs() << '\n';
  179. //printValue(Log.getRight(I), false);
  180. break;
  181. }
  182. //out << "\n";
  183. }
  184. }