John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 1 | //===-- 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 McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 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" |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 25 | #include "llvm/Support/IRReader.h" |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include "llvm/Support/SourceMgr.h" |
| 29 | |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <utility> |
| 32 | |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
| 35 | |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 36 | /// Reads a module from a file. On error, messages are written to stderr |
| 37 | /// and null is returned. |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 38 | static Module *ReadModule(LLVMContext &Context, StringRef Name) { |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 39 | SMDiagnostic Diag; |
| 40 | Module *M = ParseIRFile(Name, Diag, Context); |
| 41 | if (!M) |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 42 | Diag.Print("llvmdiff", errs()); |
Dan Gohman | c32a226 | 2010-09-13 18:02:47 +0000 | [diff] [blame] | 43 | return M; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 44 | } |
| 45 | |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 46 | namespace { |
Chris Lattner | 53b1acd | 2010-09-05 21:25:43 +0000 | [diff] [blame] | 47 | 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 McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 53b1acd | 2010-09-05 21:25:43 +0000 | [diff] [blame] | 59 | static void ComputeNumbering(Function *F, DenseMap<Value*,unsigned> &Numbering){ |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 60 | 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 McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 70 | if (!FI->hasName()) |
John McCall | b82b433 | 2010-08-24 09:16:51 +0000 | [diff] [blame] | 71 | Numbering[&*FI] = IN++; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 72 | |
| 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 Lattner | 53b1acd | 2010-09-05 21:25:43 +0000 | [diff] [blame] | 83 | namespace { |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 84 | class DiffConsumer : public DifferenceEngine::Consumer { |
| 85 | private: |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 86 | raw_ostream &out; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 87 | 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 McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 95 | out << (isa<GlobalValue>(V) ? '@' : '%') << V->getName(); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | if (V->getType()->isVoidTy()) { |
| 99 | if (isa<StoreInst>(V)) { |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 100 | out << "store to "; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 101 | printValue(cast<StoreInst>(V)->getPointerOperand(), isL); |
| 102 | } else if (isa<CallInst>(V)) { |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 103 | out << "call to "; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 104 | printValue(cast<CallInst>(V)->getCalledValue(), isL); |
| 105 | } else if (isa<InvokeInst>(V)) { |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 106 | out << "invoke to "; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 107 | printValue(cast<InvokeInst>(V)->getCalledValue(), isL); |
| 108 | } else { |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 109 | out << *V; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 110 | } |
| 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 McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 122 | out << '%' << ctxt.LNumbering[V]; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 123 | return; |
| 124 | } else { |
| 125 | if (ctxt.RNumbering.empty()) |
| 126 | ComputeNumbering(cast<Function>(ctxt.R), ctxt.RNumbering); |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 127 | out << '%' << ctxt.RNumbering[V]; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | } |
| 131 | |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 132 | out << "<anonymous>"; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 133 | } |
| 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 McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 142 | if (Differences) out << "\n"; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 143 | |
| 144 | Function *L = cast<Function>(I->L); |
| 145 | Function *R = cast<Function>(I->R); |
| 146 | if (L->getName() != R->getName()) |
John McCall | 44a9860 | 2010-07-29 18:20:13 +0000 | [diff] [blame] | 147 | out << "in function " << L->getName() |
| 148 | << " / " << R->getName() << ":\n"; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 149 | else |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 150 | out << "in function " << L->getName() << ":\n"; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 151 | } else if (isa<BasicBlock>(I->L)) { |
| 152 | BasicBlock *L = cast<BasicBlock>(I->L); |
| 153 | BasicBlock *R = cast<BasicBlock>(I->R); |
John McCall | 44a9860 | 2010-07-29 18:20:13 +0000 | [diff] [blame] | 154 | 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 McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 163 | } else if (isa<Instruction>(I->L)) { |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 164 | out << " in instruction "; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 165 | printValue(I->L, true); |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 166 | out << " / "; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 167 | printValue(I->R, false); |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 168 | out << ":\n"; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | I->Differences = true; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void indent() { |
| 176 | unsigned N = Indent; |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 177 | while (N--) out << ' '; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | public: |
| 181 | DiffConsumer(Module *L, Module *R) |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 182 | : out(errs()), LModule(L), RModule(R), Differences(false), Indent(0) {} |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 183 | |
| 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 McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 199 | out << text << '\n'; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void logf(const DifferenceEngine::LogBuilder &Log) { |
| 203 | header(); |
| 204 | indent(); |
| 205 | |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 206 | 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 McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 230 | } |
| 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 McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 239 | out << " "; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 240 | Log.getLeft(I)->dump(); |
| 241 | //printValue(Log.getLeft(I), true); |
| 242 | break; |
| 243 | case DifferenceEngine::DC_left: |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 244 | out << "< "; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 245 | Log.getLeft(I)->dump(); |
| 246 | //printValue(Log.getLeft(I), true); |
| 247 | break; |
| 248 | case DifferenceEngine::DC_right: |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 249 | out << "> "; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 250 | Log.getRight(I)->dump(); |
| 251 | //printValue(Log.getRight(I), false); |
| 252 | break; |
| 253 | } |
John McCall | 62dc1f3 | 2010-07-29 08:14:41 +0000 | [diff] [blame] | 254 | //out << "\n"; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | }; |
Chris Lattner | 53b1acd | 2010-09-05 21:25:43 +0000 | [diff] [blame] | 259 | } // end anonymous namespace |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 260 | |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 261 | static 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 McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 265 | |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 266 | 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 Lattner | 53b1acd | 2010-09-05 21:25:43 +0000 | [diff] [blame] | 278 | static cl::opt<std::string> LeftFilename(cl::Positional, |
| 279 | cl::desc("<first file>"), |
| 280 | cl::Required); |
| 281 | static cl::opt<std::string> RightFilename(cl::Positional, |
| 282 | cl::desc("<second file>"), |
| 283 | cl::Required); |
| 284 | static cl::list<std::string> GlobalsToCompare(cl::Positional, |
| 285 | cl::desc("<globals to compare>")); |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 286 | |
| 287 | int main(int argc, char **argv) { |
| 288 | cl::ParseCommandLineOptions(argc, argv); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 289 | |
| 290 | LLVMContext Context; |
| 291 | |
| 292 | // Load both modules. Die if that fails. |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 293 | Module *LModule = ReadModule(Context, LeftFilename); |
| 294 | Module *RModule = ReadModule(Context, RightFilename); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 295 | if (!LModule || !RModule) return 1; |
| 296 | |
| 297 | DiffConsumer Consumer(LModule, RModule); |
| 298 | DifferenceEngine Engine(Context, Consumer); |
| 299 | |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 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]); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 304 | |
John McCall | e5cbaf1 | 2010-07-29 17:55:00 +0000 | [diff] [blame] | 305 | // Otherwise, diff everything in the module. |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 306 | } else { |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 307 | Engine.diff(LModule, RModule); |
| 308 | } |
| 309 | |
| 310 | delete LModule; |
| 311 | delete RModule; |
| 312 | |
| 313 | return Consumer.hadDifferences(); |
| 314 | } |