1 //===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the command-line driver for the difference engine.
12 //===----------------------------------------------------------------------===//
14 #include "DifferenceEngine.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/LLVMContext.h"
18 #include "llvm/Module.h"
19 #include "llvm/Type.h"
20 #include "llvm/Assembly/Parser.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/SourceMgr.h"
37 /// Reads a module from a file. If the filename ends in .ll, it is
38 /// interpreted as an assembly file; otherwise, it is interpreted as
39 /// bitcode. On error, messages are written to stderr and null is
41 static Module *ReadModule(LLVMContext &Context, StringRef Name) {
42 // LLVM assembly path.
43 if (Name.endswith(".ll")) {
45 Module *M = ParseAssemblyFile(Name, Diag, Context);
48 Diag.Print("llvmdiff", errs());
53 MemoryBuffer *Buffer = MemoryBuffer::getFile(Name);
55 // ParseBitcodeFile takes ownership of the buffer if it succeeds.
57 Module *M = ParseBitcodeFile(Buffer, Context, &Error);
60 errs() << "error parsing " << Name << ": " << Error;
67 DiffContext(Value *L, Value *R)
68 : L(L), R(R), Differences(false), IsFunction(isa<Function>(L)) {}
73 DenseMap<Value*,unsigned> LNumbering;
74 DenseMap<Value*,unsigned> RNumbering;
77 void ComputeNumbering(Function *F, DenseMap<Value*,unsigned> &Numbering) {
80 // Arguments get the first numbers.
81 for (Function::arg_iterator
82 AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI)
84 Numbering[&*AI] = IN++;
86 // Walk the basic blocks in order.
87 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
89 Numbering[&*FI] = IN++;
91 // Walk the instructions in order.
92 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
93 // void instructions don't get numbers.
94 if (!BI->hasName() && !BI->getType()->isVoidTy())
95 Numbering[&*BI] = IN++;
98 assert(!Numbering.empty() && "asked for numbering but numbering was no-op");
101 class DiffConsumer : public DifferenceEngine::Consumer {
106 SmallVector<DiffContext, 5> contexts;
110 void printValue(Value *V, bool isL) {
112 out << (isa<GlobalValue>(V) ? '@' : '%') << V->getName();
115 if (V->getType()->isVoidTy()) {
116 if (isa<StoreInst>(V)) {
118 printValue(cast<StoreInst>(V)->getPointerOperand(), isL);
119 } else if (isa<CallInst>(V)) {
121 printValue(cast<CallInst>(V)->getCalledValue(), isL);
122 } else if (isa<InvokeInst>(V)) {
124 printValue(cast<InvokeInst>(V)->getCalledValue(), isL);
131 unsigned N = contexts.size();
134 DiffContext &ctxt = contexts[N];
135 if (!ctxt.IsFunction) continue;
137 if (ctxt.LNumbering.empty())
138 ComputeNumbering(cast<Function>(ctxt.L), ctxt.LNumbering);
139 out << '%' << ctxt.LNumbering[V];
142 if (ctxt.RNumbering.empty())
143 ComputeNumbering(cast<Function>(ctxt.R), ctxt.RNumbering);
144 out << '%' << ctxt.RNumbering[V];
149 out << "<anonymous>";
153 if (contexts.empty()) return;
154 for (SmallVectorImpl<DiffContext>::iterator
155 I = contexts.begin(), E = contexts.end(); I != E; ++I) {
156 if (I->Differences) continue;
157 if (isa<Function>(I->L)) {
158 // Extra newline between functions.
159 if (Differences) out << "\n";
161 Function *L = cast<Function>(I->L);
162 Function *R = cast<Function>(I->R);
163 if (L->getName() != R->getName())
164 out << "in function " << L->getName()
165 << " / " << R->getName() << ":\n";
167 out << "in function " << L->getName() << ":\n";
168 } else if (isa<BasicBlock>(I->L)) {
169 BasicBlock *L = cast<BasicBlock>(I->L);
170 BasicBlock *R = cast<BasicBlock>(I->R);
171 if (L->hasName() && R->hasName() && L->getName() == R->getName())
172 out << " in block %" << L->getName() << ":\n";
177 printValue(R, false);
180 } else if (isa<Instruction>(I->L)) {
181 out << " in instruction ";
182 printValue(I->L, true);
184 printValue(I->R, false);
188 I->Differences = true;
194 while (N--) out << ' ';
198 DiffConsumer(Module *L, Module *R)
199 : out(errs()), LModule(L), RModule(R), Differences(false), Indent(0) {}
201 bool hadDifferences() const { return Differences; }
203 void enterContext(Value *L, Value *R) {
204 contexts.push_back(DiffContext(L, R));
208 Differences |= contexts.back().Differences;
213 void log(StringRef text) {
219 void logf(const DifferenceEngine::LogBuilder &Log) {
225 StringRef format = Log.getFormat();
227 size_t percent = format.find('%');
228 if (percent == StringRef::npos) {
232 assert(format[percent] == '%');
234 if (percent > 0) out << format.substr(0, percent);
236 switch (format[percent+1]) {
237 case '%': out << '%'; break;
238 case 'l': printValue(Log.getArgument(arg++), true); break;
239 case 'r': printValue(Log.getArgument(arg++), false); break;
240 default: llvm_unreachable("unknown format character");
243 format = format.substr(percent+2);
249 void logd(const DifferenceEngine::DiffLogBuilder &Log) {
252 for (unsigned I = 0, E = Log.getNumLines(); I != E; ++I) {
254 switch (Log.getLineKind(I)) {
255 case DifferenceEngine::DC_match:
257 Log.getLeft(I)->dump();
258 //printValue(Log.getLeft(I), true);
260 case DifferenceEngine::DC_left:
262 Log.getLeft(I)->dump();
263 //printValue(Log.getLeft(I), true);
265 case DifferenceEngine::DC_right:
267 Log.getRight(I)->dump();
268 //printValue(Log.getRight(I), false);
278 static void diffGlobal(DifferenceEngine &Engine, Module *L, Module *R,
280 // Drop leading sigils from the global name.
281 if (Name.startswith("@")) Name = Name.substr(1);
283 Function *LFn = L->getFunction(Name);
284 Function *RFn = R->getFunction(Name);
286 Engine.diff(LFn, RFn);
287 else if (!LFn && !RFn)
288 errs() << "No function named @" << Name << " in either module\n";
290 errs() << "No function named @" << Name << " in left module\n";
292 errs() << "No function named @" << Name << " in right module\n";
295 cl::opt<std::string> LeftFilename(cl::Positional,
296 cl::desc("<first file>"),
298 cl::opt<std::string> RightFilename(cl::Positional,
299 cl::desc("<second file>"),
301 cl::list<std::string> GlobalsToCompare(cl::Positional,
302 cl::desc("<globals to compare>"));
304 int main(int argc, char **argv) {
305 cl::ParseCommandLineOptions(argc, argv);
309 // Load both modules. Die if that fails.
310 Module *LModule = ReadModule(Context, LeftFilename);
311 Module *RModule = ReadModule(Context, RightFilename);
312 if (!LModule || !RModule) return 1;
314 DiffConsumer Consumer(LModule, RModule);
315 DifferenceEngine Engine(Context, Consumer);
317 // If any global names were given, just diff those.
318 if (!GlobalsToCompare.empty()) {
319 for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)
320 diffGlobal(Engine, LModule, RModule, GlobalsToCompare[I]);
322 // Otherwise, diff everything in the module.
324 Engine.diff(LModule, RModule);
330 return Consumer.hadDifferences();