4 #include <llvm/ADT/StringRef.h>
5 #include <llvm/ADT/SmallVector.h>
6 #include <llvm/ADT/DenseMap.h>
8 // Required to parse .ll files.
9 #include <llvm/Support/SourceMgr.h>
10 #include <llvm/Assembly/Parser.h>
12 // Required to parse .bc files.
13 #include <llvm/Support/MemoryBuffer.h>
14 #include <llvm/Bitcode/ReaderWriter.h>
16 #include <llvm/Support/raw_ostream.h>
17 #include <llvm/Support/ErrorHandling.h>
18 #include <llvm/LLVMContext.h>
19 #include <llvm/Module.h>
20 #include <llvm/Type.h>
21 #include <llvm/Instructions.h>
23 #include "DifferenceEngine.h"
27 /// Reads a module from a file. If the filename ends in .ll, it is
28 /// interpreted as an assembly file; otherwise, it is interpreted as
29 /// bitcode. On error, messages are written to stderr and null is
31 static Module *ReadModule(LLVMContext &Context, StringRef Name) {
32 // LLVM assembly path.
33 if (Name.endswith(".ll")) {
35 Module *M = ParseAssemblyFile(Name, Diag, Context);
38 Diag.Print("llvmdiff", errs());
43 MemoryBuffer *Buffer = MemoryBuffer::getFile(Name);
45 // ParseBitcodeFile takes ownership of the buffer if it succeeds.
47 Module *M = ParseBitcodeFile(Buffer, Context, &Error);
50 errs() << "error parsing " << Name << ": " << Error;
56 errs() << "expected usage:\n";
57 errs() << " llvm-diff oldmodule.ll newmodule.ll [function list]\n";
58 errs() << "Assembly or bitcode modules may be used interchangeably.\n";
59 errs() << "If no functions are provided, all functions will be compared.\n";
65 DiffContext(Value *L, Value *R)
66 : L(L), R(R), Differences(false), IsFunction(isa<Function>(L)) {}
71 DenseMap<Value*,unsigned> LNumbering;
72 DenseMap<Value*,unsigned> RNumbering;
75 void ComputeNumbering(Function *F, DenseMap<Value*,unsigned> &Numbering) {
79 // Arguments get the first numbers.
80 for (Function::arg_iterator
81 AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI)
83 Numbering[&*AI] = IN++;
85 // Walk the basic blocks in order.
86 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
87 // Basic blocks have their own 'namespace'.
89 Numbering[&*FI] = BBN++;
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() << " / " << R->getName() << ":\n";
166 out << "in function " << L->getName() << ":\n";
167 } else if (isa<BasicBlock>(I->L)) {
168 BasicBlock *L = cast<BasicBlock>(I->L);
169 BasicBlock *R = cast<BasicBlock>(I->R);
173 printValue(R, false);
175 } else if (isa<Instruction>(I->L)) {
176 out << " in instruction ";
177 printValue(I->L, true);
179 printValue(I->R, false);
183 I->Differences = true;
189 while (N--) out << ' ';
193 DiffConsumer(Module *L, Module *R)
194 : out(errs()), LModule(L), RModule(R), Differences(false), Indent(0) {}
196 bool hadDifferences() const { return Differences; }
198 void enterContext(Value *L, Value *R) {
199 contexts.push_back(DiffContext(L, R));
203 Differences |= contexts.back().Differences;
208 void log(StringRef text) {
214 void logf(const DifferenceEngine::LogBuilder &Log) {
220 StringRef format = Log.getFormat();
222 size_t percent = format.find('%');
223 if (percent == StringRef::npos) {
227 assert(format[percent] == '%');
229 if (percent > 0) out << format.substr(0, percent);
231 switch (format[percent+1]) {
232 case '%': out << '%'; break;
233 case 'l': printValue(Log.getArgument(arg++), true); break;
234 case 'r': printValue(Log.getArgument(arg++), false); break;
235 default: llvm_unreachable("unknown format character");
238 format = format.substr(percent+2);
244 void logd(const DifferenceEngine::DiffLogBuilder &Log) {
247 for (unsigned I = 0, E = Log.getNumLines(); I != E; ++I) {
249 switch (Log.getLineKind(I)) {
250 case DifferenceEngine::DC_match:
252 Log.getLeft(I)->dump();
253 //printValue(Log.getLeft(I), true);
255 case DifferenceEngine::DC_left:
257 Log.getLeft(I)->dump();
258 //printValue(Log.getLeft(I), true);
260 case DifferenceEngine::DC_right:
262 Log.getRight(I)->dump();
263 //printValue(Log.getRight(I), false);
273 int main(int argc, const char **argv) {
274 if (argc < 3) return usage();
276 // Don't make StringRef locals like this at home.
277 StringRef LModuleFile = argv[1];
278 StringRef RModuleFile = argv[2];
282 // Load both modules. Die if that fails.
283 Module *LModule = ReadModule(Context, LModuleFile);
284 Module *RModule = ReadModule(Context, RModuleFile);
285 if (!LModule || !RModule) return 1;
287 DiffConsumer Consumer(LModule, RModule);
288 DifferenceEngine Engine(Context, Consumer);
290 // If any function names were given, just diff those.
291 const char **FnNames = argv + 3;
292 unsigned NumFnNames = argc - 3;
294 for (unsigned I = 0; I != NumFnNames; ++I) {
295 StringRef FnName = FnNames[I];
297 // Drop leading sigils from the function name.
298 if (FnName.startswith("@")) FnName = FnName.substr(1);
300 Function *LFn = LModule->getFunction(FnName);
301 Function *RFn = RModule->getFunction(FnName);
303 Engine.diff(LFn, RFn);
306 errs() << "No function named @" << FnName << " in either module\n";
308 errs() << "No function named @" << FnName << " in left module\n";
310 errs() << "No function named @" << FnName << " in right module\n";
314 // Otherwise, diff all functions in the modules.
315 Engine.diff(LModule, RModule);
321 return Consumer.hadDifferences();