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/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"
25 #include "llvm/Support/IRReader.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/SourceMgr.h"
36 /// Reads a module from a file. On error, messages are written to stderr
37 /// and null is returned.
38 static Module *ReadModule(LLVMContext &Context, StringRef Name) {
40 Module *M = ParseIRFile(Name, Diag, Context);
42 Diag.Print("llvmdiff", errs());
48 DiffContext(Value *L, Value *R)
49 : L(L), R(R), Differences(false), IsFunction(isa<Function>(L)) {}
54 DenseMap<Value*,unsigned> LNumbering;
55 DenseMap<Value*,unsigned> RNumbering;
59 static void ComputeNumbering(Function *F, DenseMap<Value*,unsigned> &Numbering){
62 // Arguments get the first numbers.
63 for (Function::arg_iterator
64 AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI)
66 Numbering[&*AI] = IN++;
68 // Walk the basic blocks in order.
69 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
71 Numbering[&*FI] = IN++;
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++;
80 assert(!Numbering.empty() && "asked for numbering but numbering was no-op");
84 class DiffConsumer : public DifferenceEngine::Consumer {
89 SmallVector<DiffContext, 5> contexts;
93 void printValue(Value *V, bool isL) {
95 out << (isa<GlobalValue>(V) ? '@' : '%') << V->getName();
98 if (V->getType()->isVoidTy()) {
99 if (isa<StoreInst>(V)) {
101 printValue(cast<StoreInst>(V)->getPointerOperand(), isL);
102 } else if (isa<CallInst>(V)) {
104 printValue(cast<CallInst>(V)->getCalledValue(), isL);
105 } else if (isa<InvokeInst>(V)) {
107 printValue(cast<InvokeInst>(V)->getCalledValue(), isL);
114 unsigned N = contexts.size();
117 DiffContext &ctxt = contexts[N];
118 if (!ctxt.IsFunction) continue;
120 if (ctxt.LNumbering.empty())
121 ComputeNumbering(cast<Function>(ctxt.L), ctxt.LNumbering);
122 out << '%' << ctxt.LNumbering[V];
125 if (ctxt.RNumbering.empty())
126 ComputeNumbering(cast<Function>(ctxt.R), ctxt.RNumbering);
127 out << '%' << ctxt.RNumbering[V];
132 out << "<anonymous>";
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.
142 if (Differences) out << "\n";
144 Function *L = cast<Function>(I->L);
145 Function *R = cast<Function>(I->R);
146 if (L->getName() != R->getName())
147 out << "in function " << L->getName()
148 << " / " << R->getName() << ":\n";
150 out << "in function " << L->getName() << ":\n";
151 } else if (isa<BasicBlock>(I->L)) {
152 BasicBlock *L = cast<BasicBlock>(I->L);
153 BasicBlock *R = cast<BasicBlock>(I->R);
154 if (L->hasName() && R->hasName() && L->getName() == R->getName())
155 out << " in block %" << L->getName() << ":\n";
160 printValue(R, false);
163 } else if (isa<Instruction>(I->L)) {
164 out << " in instruction ";
165 printValue(I->L, true);
167 printValue(I->R, false);
171 I->Differences = true;
177 while (N--) out << ' ';
181 DiffConsumer(Module *L, Module *R)
182 : out(errs()), LModule(L), RModule(R), Differences(false), Indent(0) {}
184 bool hadDifferences() const { return Differences; }
186 void enterContext(Value *L, Value *R) {
187 contexts.push_back(DiffContext(L, R));
191 Differences |= contexts.back().Differences;
196 void log(StringRef text) {
202 void logf(const DifferenceEngine::LogBuilder &Log) {
208 StringRef format = Log.getFormat();
210 size_t percent = format.find('%');
211 if (percent == StringRef::npos) {
215 assert(format[percent] == '%');
217 if (percent > 0) out << format.substr(0, percent);
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");
226 format = format.substr(percent+2);
232 void logd(const DifferenceEngine::DiffLogBuilder &Log) {
235 for (unsigned I = 0, E = Log.getNumLines(); I != E; ++I) {
237 switch (Log.getLineKind(I)) {
238 case DifferenceEngine::DC_match:
240 Log.getLeft(I)->dump();
241 //printValue(Log.getLeft(I), true);
243 case DifferenceEngine::DC_left:
245 Log.getLeft(I)->dump();
246 //printValue(Log.getLeft(I), true);
248 case DifferenceEngine::DC_right:
250 Log.getRight(I)->dump();
251 //printValue(Log.getRight(I), false);
259 } // end anonymous namespace
261 static void diffGlobal(DifferenceEngine &Engine, Module *L, Module *R,
263 // Drop leading sigils from the global name.
264 if (Name.startswith("@")) Name = Name.substr(1);
266 Function *LFn = L->getFunction(Name);
267 Function *RFn = R->getFunction(Name);
269 Engine.diff(LFn, RFn);
270 else if (!LFn && !RFn)
271 errs() << "No function named @" << Name << " in either module\n";
273 errs() << "No function named @" << Name << " in left module\n";
275 errs() << "No function named @" << Name << " in right module\n";
278 static cl::opt<std::string> LeftFilename(cl::Positional,
279 cl::desc("<first file>"),
281 static cl::opt<std::string> RightFilename(cl::Positional,
282 cl::desc("<second file>"),
284 static cl::list<std::string> GlobalsToCompare(cl::Positional,
285 cl::desc("<globals to compare>"));
287 int main(int argc, char **argv) {
288 cl::ParseCommandLineOptions(argc, argv);
292 // Load both modules. Die if that fails.
293 Module *LModule = ReadModule(Context, LeftFilename);
294 Module *RModule = ReadModule(Context, RightFilename);
295 if (!LModule || !RModule) return 1;
297 DiffConsumer Consumer(LModule, RModule);
298 DifferenceEngine Engine(Context, Consumer);
300 // 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]);
305 // Otherwise, diff everything in the module.
307 Engine.diff(LModule, RModule);
313 return Consumer.hadDifferences();