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/CommandLine.h>
17 #include <llvm/Support/raw_ostream.h>
18 #include <llvm/Support/ErrorHandling.h>
19 #include <llvm/LLVMContext.h>
20 #include <llvm/Module.h>
21 #include <llvm/Type.h>
22 #include <llvm/Instructions.h>
24 #include "DifferenceEngine.h"
28 /// Reads a module from a file. If the filename ends in .ll, it is
29 /// interpreted as an assembly file; otherwise, it is interpreted as
30 /// bitcode. On error, messages are written to stderr and null is
32 static Module *ReadModule(LLVMContext &Context, StringRef Name) {
33 // LLVM assembly path.
34 if (Name.endswith(".ll")) {
36 Module *M = ParseAssemblyFile(Name, Diag, Context);
39 Diag.Print("llvmdiff", errs());
44 MemoryBuffer *Buffer = MemoryBuffer::getFile(Name);
46 // ParseBitcodeFile takes ownership of the buffer if it succeeds.
48 Module *M = ParseBitcodeFile(Buffer, Context, &Error);
51 errs() << "error parsing " << Name << ": " << Error;
58 DiffContext(Value *L, Value *R)
59 : L(L), R(R), Differences(false), IsFunction(isa<Function>(L)) {}
64 DenseMap<Value*,unsigned> LNumbering;
65 DenseMap<Value*,unsigned> RNumbering;
68 void ComputeNumbering(Function *F, DenseMap<Value*,unsigned> &Numbering) {
72 // Arguments get the first numbers.
73 for (Function::arg_iterator
74 AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI)
76 Numbering[&*AI] = IN++;
78 // Walk the basic blocks in order.
79 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
80 // Basic blocks have their own 'namespace'.
82 Numbering[&*FI] = BBN++;
84 // Walk the instructions in order.
85 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
86 // void instructions don't get numbers.
87 if (!BI->hasName() && !BI->getType()->isVoidTy())
88 Numbering[&*BI] = IN++;
91 assert(!Numbering.empty() && "asked for numbering but numbering was no-op");
94 class DiffConsumer : public DifferenceEngine::Consumer {
99 SmallVector<DiffContext, 5> contexts;
103 void printValue(Value *V, bool isL) {
105 out << (isa<GlobalValue>(V) ? '@' : '%') << V->getName();
108 if (V->getType()->isVoidTy()) {
109 if (isa<StoreInst>(V)) {
111 printValue(cast<StoreInst>(V)->getPointerOperand(), isL);
112 } else if (isa<CallInst>(V)) {
114 printValue(cast<CallInst>(V)->getCalledValue(), isL);
115 } else if (isa<InvokeInst>(V)) {
117 printValue(cast<InvokeInst>(V)->getCalledValue(), isL);
124 unsigned N = contexts.size();
127 DiffContext &ctxt = contexts[N];
128 if (!ctxt.IsFunction) continue;
130 if (ctxt.LNumbering.empty())
131 ComputeNumbering(cast<Function>(ctxt.L), ctxt.LNumbering);
132 out << '%' << ctxt.LNumbering[V];
135 if (ctxt.RNumbering.empty())
136 ComputeNumbering(cast<Function>(ctxt.R), ctxt.RNumbering);
137 out << '%' << ctxt.RNumbering[V];
142 out << "<anonymous>";
146 if (contexts.empty()) return;
147 for (SmallVectorImpl<DiffContext>::iterator
148 I = contexts.begin(), E = contexts.end(); I != E; ++I) {
149 if (I->Differences) continue;
150 if (isa<Function>(I->L)) {
151 // Extra newline between functions.
152 if (Differences) out << "\n";
154 Function *L = cast<Function>(I->L);
155 Function *R = cast<Function>(I->R);
156 if (L->getName() != R->getName())
157 out << "in function " << L->getName() << " / " << R->getName() << ":\n";
159 out << "in function " << L->getName() << ":\n";
160 } else if (isa<BasicBlock>(I->L)) {
161 BasicBlock *L = cast<BasicBlock>(I->L);
162 BasicBlock *R = cast<BasicBlock>(I->R);
166 printValue(R, false);
168 } else if (isa<Instruction>(I->L)) {
169 out << " in instruction ";
170 printValue(I->L, true);
172 printValue(I->R, false);
176 I->Differences = true;
182 while (N--) out << ' ';
186 DiffConsumer(Module *L, Module *R)
187 : out(errs()), LModule(L), RModule(R), Differences(false), Indent(0) {}
189 bool hadDifferences() const { return Differences; }
191 void enterContext(Value *L, Value *R) {
192 contexts.push_back(DiffContext(L, R));
196 Differences |= contexts.back().Differences;
201 void log(StringRef text) {
207 void logf(const DifferenceEngine::LogBuilder &Log) {
213 StringRef format = Log.getFormat();
215 size_t percent = format.find('%');
216 if (percent == StringRef::npos) {
220 assert(format[percent] == '%');
222 if (percent > 0) out << format.substr(0, percent);
224 switch (format[percent+1]) {
225 case '%': out << '%'; break;
226 case 'l': printValue(Log.getArgument(arg++), true); break;
227 case 'r': printValue(Log.getArgument(arg++), false); break;
228 default: llvm_unreachable("unknown format character");
231 format = format.substr(percent+2);
237 void logd(const DifferenceEngine::DiffLogBuilder &Log) {
240 for (unsigned I = 0, E = Log.getNumLines(); I != E; ++I) {
242 switch (Log.getLineKind(I)) {
243 case DifferenceEngine::DC_match:
245 Log.getLeft(I)->dump();
246 //printValue(Log.getLeft(I), true);
248 case DifferenceEngine::DC_left:
250 Log.getLeft(I)->dump();
251 //printValue(Log.getLeft(I), true);
253 case DifferenceEngine::DC_right:
255 Log.getRight(I)->dump();
256 //printValue(Log.getRight(I), false);
266 static void diffGlobal(DifferenceEngine &Engine, Module *L, Module *R,
268 // Drop leading sigils from the global name.
269 if (Name.startswith("@")) Name = Name.substr(1);
271 Function *LFn = L->getFunction(Name);
272 Function *RFn = R->getFunction(Name);
274 Engine.diff(LFn, RFn);
275 else if (!LFn && !RFn)
276 errs() << "No function named @" << Name << " in either module\n";
278 errs() << "No function named @" << Name << " in left module\n";
280 errs() << "No function named @" << Name << " in right module\n";
283 cl::opt<std::string> LeftFilename(cl::Positional, cl::desc("<first file>"), cl::Required);
284 cl::opt<std::string> RightFilename(cl::Positional, cl::desc("<second file>"), cl::Required);
285 cl::list<std::string> GlobalsToCompare(cl::Positional, 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();