blob: b932ccc7437c32d85eef7d6ba5afba4d9dc7e722 [file] [log] [blame]
John McCall73b21b72010-07-29 18:08:23 +00001//===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===//
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//
10// This file defines the command-line driver for the difference engine.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DifferenceEngine.h"
15
16#include "llvm/Instructions.h"
17#include "llvm/LLVMContext.h"
18#include "llvm/Module.h"
19#include "llvm/Type.h"
John McCall73b21b72010-07-29 18:08:23 +000020#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/ErrorHandling.h"
Dan Gohmanc32a2262010-09-13 18:02:47 +000025#include "llvm/Support/IRReader.h"
John McCall73b21b72010-07-29 18:08:23 +000026#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/Support/SourceMgr.h"
29
John McCall3dd706b2010-07-29 07:53:27 +000030#include <string>
31#include <utility>
32
John McCall3dd706b2010-07-29 07:53:27 +000033
34using namespace llvm;
35
Dan Gohmanc32a2262010-09-13 18:02:47 +000036/// Reads a module from a file. On error, messages are written to stderr
37/// and null is returned.
John McCall3dd706b2010-07-29 07:53:27 +000038static Module *ReadModule(LLVMContext &Context, StringRef Name) {
Dan Gohmanc32a2262010-09-13 18:02:47 +000039 SMDiagnostic Diag;
40 Module *M = ParseIRFile(Name, Diag, Context);
41 if (!M)
John McCall3dd706b2010-07-29 07:53:27 +000042 Diag.Print("llvmdiff", errs());
Dan Gohmanc32a2262010-09-13 18:02:47 +000043 return M;
John McCall3dd706b2010-07-29 07:53:27 +000044}
45
John McCall3dd706b2010-07-29 07:53:27 +000046namespace {
Chris Lattner53b1acd2010-09-05 21:25:43 +000047 struct DiffContext {
48 DiffContext(Value *L, Value *R)
49 : L(L), R(R), Differences(false), IsFunction(isa<Function>(L)) {}
50 Value *L;
51 Value *R;
52 bool Differences;
53 bool IsFunction;
54 DenseMap<Value*,unsigned> LNumbering;
55 DenseMap<Value*,unsigned> RNumbering;
56 };
57}
John McCall3dd706b2010-07-29 07:53:27 +000058
Chris Lattner53b1acd2010-09-05 21:25:43 +000059static void ComputeNumbering(Function *F, DenseMap<Value*,unsigned> &Numbering){
John McCall3dd706b2010-07-29 07:53:27 +000060 unsigned IN = 0;
61
62 // Arguments get the first numbers.
63 for (Function::arg_iterator
64 AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI)
65 if (!AI->hasName())
66 Numbering[&*AI] = IN++;
67
68 // Walk the basic blocks in order.
69 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
John McCall3dd706b2010-07-29 07:53:27 +000070 if (!FI->hasName())
John McCallb82b4332010-08-24 09:16:51 +000071 Numbering[&*FI] = IN++;
John McCall3dd706b2010-07-29 07:53:27 +000072
73 // Walk the instructions in order.
74 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
75 // void instructions don't get numbers.
76 if (!BI->hasName() && !BI->getType()->isVoidTy())
77 Numbering[&*BI] = IN++;
78 }
79
80 assert(!Numbering.empty() && "asked for numbering but numbering was no-op");
81}
82
Chris Lattner53b1acd2010-09-05 21:25:43 +000083namespace {
John McCall3dd706b2010-07-29 07:53:27 +000084class DiffConsumer : public DifferenceEngine::Consumer {
85private:
John McCall62dc1f32010-07-29 08:14:41 +000086 raw_ostream &out;
John McCall3dd706b2010-07-29 07:53:27 +000087 Module *LModule;
88 Module *RModule;
89 SmallVector<DiffContext, 5> contexts;
90 bool Differences;
91 unsigned Indent;
92
93 void printValue(Value *V, bool isL) {
94 if (V->hasName()) {
John McCall62dc1f32010-07-29 08:14:41 +000095 out << (isa<GlobalValue>(V) ? '@' : '%') << V->getName();
John McCall3dd706b2010-07-29 07:53:27 +000096 return;
97 }
98 if (V->getType()->isVoidTy()) {
99 if (isa<StoreInst>(V)) {
John McCall62dc1f32010-07-29 08:14:41 +0000100 out << "store to ";
John McCall3dd706b2010-07-29 07:53:27 +0000101 printValue(cast<StoreInst>(V)->getPointerOperand(), isL);
102 } else if (isa<CallInst>(V)) {
John McCall62dc1f32010-07-29 08:14:41 +0000103 out << "call to ";
John McCall3dd706b2010-07-29 07:53:27 +0000104 printValue(cast<CallInst>(V)->getCalledValue(), isL);
105 } else if (isa<InvokeInst>(V)) {
John McCall62dc1f32010-07-29 08:14:41 +0000106 out << "invoke to ";
John McCall3dd706b2010-07-29 07:53:27 +0000107 printValue(cast<InvokeInst>(V)->getCalledValue(), isL);
108 } else {
John McCall62dc1f32010-07-29 08:14:41 +0000109 out << *V;
John McCall3dd706b2010-07-29 07:53:27 +0000110 }
111 return;
112 }
113
114 unsigned N = contexts.size();
115 while (N > 0) {
116 --N;
117 DiffContext &ctxt = contexts[N];
118 if (!ctxt.IsFunction) continue;
119 if (isL) {
120 if (ctxt.LNumbering.empty())
121 ComputeNumbering(cast<Function>(ctxt.L), ctxt.LNumbering);
John McCall62dc1f32010-07-29 08:14:41 +0000122 out << '%' << ctxt.LNumbering[V];
John McCall3dd706b2010-07-29 07:53:27 +0000123 return;
124 } else {
125 if (ctxt.RNumbering.empty())
126 ComputeNumbering(cast<Function>(ctxt.R), ctxt.RNumbering);
John McCall62dc1f32010-07-29 08:14:41 +0000127 out << '%' << ctxt.RNumbering[V];
John McCall3dd706b2010-07-29 07:53:27 +0000128 return;
129 }
130 }
131
John McCall62dc1f32010-07-29 08:14:41 +0000132 out << "<anonymous>";
John McCall3dd706b2010-07-29 07:53:27 +0000133 }
134
135 void header() {
136 if (contexts.empty()) return;
137 for (SmallVectorImpl<DiffContext>::iterator
138 I = contexts.begin(), E = contexts.end(); I != E; ++I) {
139 if (I->Differences) continue;
140 if (isa<Function>(I->L)) {
141 // Extra newline between functions.
John McCall62dc1f32010-07-29 08:14:41 +0000142 if (Differences) out << "\n";
John McCall3dd706b2010-07-29 07:53:27 +0000143
144 Function *L = cast<Function>(I->L);
145 Function *R = cast<Function>(I->R);
146 if (L->getName() != R->getName())
John McCall44a98602010-07-29 18:20:13 +0000147 out << "in function " << L->getName()
148 << " / " << R->getName() << ":\n";
John McCall3dd706b2010-07-29 07:53:27 +0000149 else
John McCall62dc1f32010-07-29 08:14:41 +0000150 out << "in function " << L->getName() << ":\n";
John McCall3dd706b2010-07-29 07:53:27 +0000151 } else if (isa<BasicBlock>(I->L)) {
152 BasicBlock *L = cast<BasicBlock>(I->L);
153 BasicBlock *R = cast<BasicBlock>(I->R);
John McCall44a98602010-07-29 18:20:13 +0000154 if (L->hasName() && R->hasName() && L->getName() == R->getName())
155 out << " in block %" << L->getName() << ":\n";
156 else {
157 out << " in block ";
158 printValue(L, true);
159 out << " / ";
160 printValue(R, false);
161 out << ":\n";
162 }
John McCall3dd706b2010-07-29 07:53:27 +0000163 } else if (isa<Instruction>(I->L)) {
John McCall62dc1f32010-07-29 08:14:41 +0000164 out << " in instruction ";
John McCall3dd706b2010-07-29 07:53:27 +0000165 printValue(I->L, true);
John McCall62dc1f32010-07-29 08:14:41 +0000166 out << " / ";
John McCall3dd706b2010-07-29 07:53:27 +0000167 printValue(I->R, false);
John McCall62dc1f32010-07-29 08:14:41 +0000168 out << ":\n";
John McCall3dd706b2010-07-29 07:53:27 +0000169 }
170
171 I->Differences = true;
172 }
173 }
174
175 void indent() {
176 unsigned N = Indent;
John McCall62dc1f32010-07-29 08:14:41 +0000177 while (N--) out << ' ';
John McCall3dd706b2010-07-29 07:53:27 +0000178 }
179
180public:
181 DiffConsumer(Module *L, Module *R)
John McCall62dc1f32010-07-29 08:14:41 +0000182 : out(errs()), LModule(L), RModule(R), Differences(false), Indent(0) {}
John McCall3dd706b2010-07-29 07:53:27 +0000183
184 bool hadDifferences() const { return Differences; }
185
186 void enterContext(Value *L, Value *R) {
187 contexts.push_back(DiffContext(L, R));
188 Indent += 2;
189 }
190 void exitContext() {
191 Differences |= contexts.back().Differences;
192 contexts.pop_back();
193 Indent -= 2;
194 }
195
196 void log(StringRef text) {
197 header();
198 indent();
John McCall62dc1f32010-07-29 08:14:41 +0000199 out << text << '\n';
John McCall3dd706b2010-07-29 07:53:27 +0000200 }
201
202 void logf(const DifferenceEngine::LogBuilder &Log) {
203 header();
204 indent();
205
John McCall62dc1f32010-07-29 08:14:41 +0000206 unsigned arg = 0;
207
208 StringRef format = Log.getFormat();
209 while (true) {
210 size_t percent = format.find('%');
211 if (percent == StringRef::npos) {
212 out << format;
213 break;
214 }
215 assert(format[percent] == '%');
216
217 if (percent > 0) out << format.substr(0, percent);
218
219 switch (format[percent+1]) {
220 case '%': out << '%'; break;
221 case 'l': printValue(Log.getArgument(arg++), true); break;
222 case 'r': printValue(Log.getArgument(arg++), false); break;
223 default: llvm_unreachable("unknown format character");
224 }
225
226 format = format.substr(percent+2);
227 }
228
229 out << '\n';
John McCall3dd706b2010-07-29 07:53:27 +0000230 }
231
232 void logd(const DifferenceEngine::DiffLogBuilder &Log) {
233 header();
234
235 for (unsigned I = 0, E = Log.getNumLines(); I != E; ++I) {
236 indent();
237 switch (Log.getLineKind(I)) {
238 case DifferenceEngine::DC_match:
John McCall62dc1f32010-07-29 08:14:41 +0000239 out << " ";
John McCall3dd706b2010-07-29 07:53:27 +0000240 Log.getLeft(I)->dump();
241 //printValue(Log.getLeft(I), true);
242 break;
243 case DifferenceEngine::DC_left:
John McCall62dc1f32010-07-29 08:14:41 +0000244 out << "< ";
John McCall3dd706b2010-07-29 07:53:27 +0000245 Log.getLeft(I)->dump();
246 //printValue(Log.getLeft(I), true);
247 break;
248 case DifferenceEngine::DC_right:
John McCall62dc1f32010-07-29 08:14:41 +0000249 out << "> ";
John McCall3dd706b2010-07-29 07:53:27 +0000250 Log.getRight(I)->dump();
251 //printValue(Log.getRight(I), false);
252 break;
253 }
John McCall62dc1f32010-07-29 08:14:41 +0000254 //out << "\n";
John McCall3dd706b2010-07-29 07:53:27 +0000255 }
256 }
257
258};
Chris Lattner53b1acd2010-09-05 21:25:43 +0000259} // end anonymous namespace
John McCall3dd706b2010-07-29 07:53:27 +0000260
John McCalle5cbaf12010-07-29 17:55:00 +0000261static void diffGlobal(DifferenceEngine &Engine, Module *L, Module *R,
262 StringRef Name) {
263 // Drop leading sigils from the global name.
264 if (Name.startswith("@")) Name = Name.substr(1);
John McCall3dd706b2010-07-29 07:53:27 +0000265
John McCalle5cbaf12010-07-29 17:55:00 +0000266 Function *LFn = L->getFunction(Name);
267 Function *RFn = R->getFunction(Name);
268 if (LFn && RFn)
269 Engine.diff(LFn, RFn);
270 else if (!LFn && !RFn)
271 errs() << "No function named @" << Name << " in either module\n";
272 else if (!LFn)
273 errs() << "No function named @" << Name << " in left module\n";
274 else
275 errs() << "No function named @" << Name << " in right module\n";
276}
277
Chris Lattner53b1acd2010-09-05 21:25:43 +0000278static cl::opt<std::string> LeftFilename(cl::Positional,
279 cl::desc("<first file>"),
280 cl::Required);
281static cl::opt<std::string> RightFilename(cl::Positional,
282 cl::desc("<second file>"),
283 cl::Required);
284static cl::list<std::string> GlobalsToCompare(cl::Positional,
285 cl::desc("<globals to compare>"));
John McCalle5cbaf12010-07-29 17:55:00 +0000286
287int main(int argc, char **argv) {
288 cl::ParseCommandLineOptions(argc, argv);
John McCall3dd706b2010-07-29 07:53:27 +0000289
290 LLVMContext Context;
291
292 // Load both modules. Die if that fails.
John McCalle5cbaf12010-07-29 17:55:00 +0000293 Module *LModule = ReadModule(Context, LeftFilename);
294 Module *RModule = ReadModule(Context, RightFilename);
John McCall3dd706b2010-07-29 07:53:27 +0000295 if (!LModule || !RModule) return 1;
296
297 DiffConsumer Consumer(LModule, RModule);
298 DifferenceEngine Engine(Context, Consumer);
299
John McCalle5cbaf12010-07-29 17:55:00 +0000300 // If any global names were given, just diff those.
301 if (!GlobalsToCompare.empty()) {
302 for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)
303 diffGlobal(Engine, LModule, RModule, GlobalsToCompare[I]);
John McCall3dd706b2010-07-29 07:53:27 +0000304
John McCalle5cbaf12010-07-29 17:55:00 +0000305 // Otherwise, diff everything in the module.
John McCall3dd706b2010-07-29 07:53:27 +0000306 } else {
John McCall3dd706b2010-07-29 07:53:27 +0000307 Engine.diff(LModule, RModule);
308 }
309
310 delete LModule;
311 delete RModule;
312
313 return Consumer.hadDifferences();
314}