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