efa4bcbe1aaa21827fd1fe6aa8931548492807df
[oota-llvm.git] / tools / verify-uselistorder / verify-uselistorder.cpp
1 //===- verify-uselistorder.cpp - The LLVM Modular Optimizer ---------------===//
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 // Verify that use-list order can be serialized correctly.  After reading the
11 // provided IR, this tool shuffles the use-lists and then writes and reads to a
12 // separate Module whose use-list orders are compared to the original.
13 //
14 // The shuffles are deterministic, but guarantee that use-lists will change.
15 // The algorithm per iteration is as follows:
16 //
17 //  1. Seed the random number generator.  The seed is different for each
18 //     shuffle.  Shuffle 0 uses default+0, shuffle 1 uses default+1, and so on.
19 //
20 //  2. Visit every Value in a deterministic order.
21 //
22 //  3. Assign a random number to each Use in the Value's use-list in order.
23 //
24 //  4. If the numbers are already in order, reassign numbers until they aren't.
25 //
26 //  5. Sort the use-list using Value::sortUseList(), which is a stable sort.
27 //
28 //===----------------------------------------------------------------------===//
29
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/DenseSet.h"
32 #include "llvm/AsmParser/Parser.h"
33 #include "llvm/Bitcode/ReaderWriter.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/UseListOrder.h"
37 #include "llvm/IR/Verifier.h"
38 #include "llvm/IRReader/IRReader.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/FileSystem.h"
43 #include "llvm/Support/FileUtilities.h"
44 #include "llvm/Support/ManagedStatic.h"
45 #include "llvm/Support/MemoryBuffer.h"
46 #include "llvm/Support/PrettyStackTrace.h"
47 #include "llvm/Support/Signals.h"
48 #include "llvm/Support/SourceMgr.h"
49 #include "llvm/Support/SystemUtils.h"
50 #include "llvm/Support/raw_ostream.h"
51 #include <random>
52 #include <vector>
53
54 using namespace llvm;
55
56 #define DEBUG_TYPE "uselistorder"
57
58 static cl::opt<std::string> InputFilename(cl::Positional,
59                                           cl::desc("<input bitcode file>"),
60                                           cl::init("-"),
61                                           cl::value_desc("filename"));
62
63 static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temp files"),
64                                cl::init(false));
65
66 static cl::opt<unsigned>
67     NumShuffles("num-shuffles",
68                 cl::desc("Number of times to shuffle and verify use-lists"),
69                 cl::init(1));
70
71 namespace {
72
73 struct TempFile {
74   std::string Filename;
75   FileRemover Remover;
76   bool init(const std::string &Ext);
77   bool writeBitcode(const Module &M) const;
78   bool writeAssembly(const Module &M) const;
79   std::unique_ptr<Module> readBitcode(LLVMContext &Context) const;
80   std::unique_ptr<Module> readAssembly(LLVMContext &Context) const;
81 };
82
83 struct ValueMapping {
84   DenseMap<const Value *, unsigned> IDs;
85   std::vector<const Value *> Values;
86
87   /// \brief Construct a value mapping for module.
88   ///
89   /// Creates mapping from every value in \c M to an ID.  This mapping includes
90   /// un-referencable values.
91   ///
92   /// Every \a Value that gets serialized in some way should be represented
93   /// here.  The order needs to be deterministic, but it's unnecessary to match
94   /// the value-ids in the bitcode writer.
95   ///
96   /// All constants that are referenced by other values are included in the
97   /// mapping, but others -- which wouldn't be serialized -- are not.
98   ValueMapping(const Module &M);
99
100   /// \brief Map a value.
101   ///
102   /// Maps a value.  If it's a constant, maps all of its operands first.
103   void map(const Value *V);
104   unsigned lookup(const Value *V) const { return IDs.lookup(V); }
105 };
106
107 } // end namespace
108
109 bool TempFile::init(const std::string &Ext) {
110   SmallVector<char, 64> Vector;
111   DEBUG(dbgs() << " - create-temp-file\n");
112   if (auto EC = sys::fs::createTemporaryFile("uselistorder", Ext, Vector)) {
113     errs() << "verify-uselistorder: error: " << EC.message() << "\n";
114     return true;
115   }
116   assert(!Vector.empty());
117
118   Filename.assign(Vector.data(), Vector.data() + Vector.size());
119   Remover.setFile(Filename, !SaveTemps);
120   if (SaveTemps)
121     outs() << " - filename = " << Filename << "\n";
122   return false;
123 }
124
125 bool TempFile::writeBitcode(const Module &M) const {
126   DEBUG(dbgs() << " - write bitcode\n");
127   std::error_code EC;
128   raw_fd_ostream OS(Filename, EC, sys::fs::F_None);
129   if (EC) {
130     errs() << "verify-uselistorder: error: " << EC.message() << "\n";
131     return true;
132   }
133
134   WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
135   return false;
136 }
137
138 bool TempFile::writeAssembly(const Module &M) const {
139   DEBUG(dbgs() << " - write assembly\n");
140   std::error_code EC;
141   raw_fd_ostream OS(Filename, EC, sys::fs::F_Text);
142   if (EC) {
143     errs() << "verify-uselistorder: error: " << EC.message() << "\n";
144     return true;
145   }
146
147   M.print(OS, nullptr, /* ShouldPreserveUseListOrder */ true);
148   return false;
149 }
150
151 std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
152   DEBUG(dbgs() << " - read bitcode\n");
153   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOr =
154       MemoryBuffer::getFile(Filename);
155   if (!BufferOr) {
156     errs() << "verify-uselistorder: error: " << BufferOr.getError().message()
157            << "\n";
158     return nullptr;
159   }
160
161   MemoryBuffer *Buffer = BufferOr.get().get();
162   ErrorOr<std::unique_ptr<Module>> ModuleOr =
163       parseBitcodeFile(Buffer->getMemBufferRef(), Context);
164   if (!ModuleOr) {
165     errs() << "verify-uselistorder: error: " << ModuleOr.getError().message()
166            << "\n";
167     return nullptr;
168   }
169   return std::move(ModuleOr.get());
170 }
171
172 std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
173   DEBUG(dbgs() << " - read assembly\n");
174   SMDiagnostic Err;
175   std::unique_ptr<Module> M = parseAssemblyFile(Filename, Err, Context);
176   if (!M.get())
177     Err.print("verify-uselistorder", errs());
178   return M;
179 }
180
181 ValueMapping::ValueMapping(const Module &M) {
182   // Every value should be mapped, including things like void instructions and
183   // basic blocks that are kept out of the ValueEnumerator.
184   //
185   // The current mapping order makes it easier to debug the tables.  It happens
186   // to be similar to the ID mapping when writing ValueEnumerator, but they
187   // aren't (and needn't be) in sync.
188
189   // Globals.
190   for (const GlobalVariable &G : M.globals())
191     map(&G);
192   for (const GlobalAlias &A : M.aliases())
193     map(&A);
194   for (const Function &F : M)
195     map(&F);
196
197   // Constants used by globals.
198   for (const GlobalVariable &G : M.globals())
199     if (G.hasInitializer())
200       map(G.getInitializer());
201   for (const GlobalAlias &A : M.aliases())
202     map(A.getAliasee());
203   for (const Function &F : M) {
204     if (F.hasPrefixData())
205       map(F.getPrefixData());
206     if (F.hasPrologueData())
207       map(F.getPrologueData());
208     if (F.hasPersonalityFn())
209       map(F.getPersonalityFn());
210   }
211
212   // Function bodies.
213   for (const Function &F : M) {
214     for (const Argument &A : F.args())
215       map(&A);
216     for (const BasicBlock &BB : F)
217       map(&BB);
218     for (const BasicBlock &BB : F)
219       for (const Instruction &I : BB)
220         map(&I);
221
222     // Constants used by instructions.
223     for (const BasicBlock &BB : F)
224       for (const Instruction &I : BB)
225         for (const Value *Op : I.operands())
226           if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
227               isa<InlineAsm>(Op))
228             map(Op);
229   }
230 }
231
232 void ValueMapping::map(const Value *V) {
233   if (IDs.lookup(V))
234     return;
235
236   if (auto *C = dyn_cast<Constant>(V))
237     if (!isa<GlobalValue>(C))
238       for (const Value *Op : C->operands())
239         map(Op);
240
241   Values.push_back(V);
242   IDs[V] = Values.size();
243 }
244
245 #ifndef NDEBUG
246 static void dumpMapping(const ValueMapping &VM) {
247   dbgs() << "value-mapping (size = " << VM.Values.size() << "):\n";
248   for (unsigned I = 0, E = VM.Values.size(); I != E; ++I) {
249     dbgs() << " - id = " << I << ", value = ";
250     VM.Values[I]->dump();
251   }
252 }
253
254 static void debugValue(const ValueMapping &M, unsigned I, StringRef Desc) {
255   const Value *V = M.Values[I];
256   dbgs() << " - " << Desc << " value = ";
257   V->dump();
258   for (const Use &U : V->uses()) {
259     dbgs() << "   => use: op = " << U.getOperandNo()
260            << ", user-id = " << M.IDs.lookup(U.getUser()) << ", user = ";
261     U.getUser()->dump();
262   }
263 }
264
265 static void debugUserMismatch(const ValueMapping &L, const ValueMapping &R,
266                               unsigned I) {
267   dbgs() << " - fail: user mismatch: ID = " << I << "\n";
268   debugValue(L, I, "LHS");
269   debugValue(R, I, "RHS");
270
271   dbgs() << "\nlhs-";
272   dumpMapping(L);
273   dbgs() << "\nrhs-";
274   dumpMapping(R);
275 }
276
277 static void debugSizeMismatch(const ValueMapping &L, const ValueMapping &R) {
278   dbgs() << " - fail: map size: " << L.Values.size()
279          << " != " << R.Values.size() << "\n";
280   dbgs() << "\nlhs-";
281   dumpMapping(L);
282   dbgs() << "\nrhs-";
283   dumpMapping(R);
284 }
285 #endif
286
287 static bool matches(const ValueMapping &LM, const ValueMapping &RM) {
288   DEBUG(dbgs() << "compare value maps\n");
289   if (LM.Values.size() != RM.Values.size()) {
290     DEBUG(debugSizeMismatch(LM, RM));
291     return false;
292   }
293
294   // This mapping doesn't include dangling constant users, since those don't
295   // get serialized.  However, checking if users are constant and calling
296   // isConstantUsed() on every one is very expensive.  Instead, just check if
297   // the user is mapped.
298   auto skipUnmappedUsers =
299       [&](Value::const_use_iterator &U, Value::const_use_iterator E,
300           const ValueMapping &M) {
301     while (U != E && !M.lookup(U->getUser()))
302       ++U;
303   };
304
305   // Iterate through all values, and check that both mappings have the same
306   // users.
307   for (unsigned I = 0, E = LM.Values.size(); I != E; ++I) {
308     const Value *L = LM.Values[I];
309     const Value *R = RM.Values[I];
310     auto LU = L->use_begin(), LE = L->use_end();
311     auto RU = R->use_begin(), RE = R->use_end();
312     skipUnmappedUsers(LU, LE, LM);
313     skipUnmappedUsers(RU, RE, RM);
314
315     while (LU != LE) {
316       if (RU == RE) {
317         DEBUG(debugUserMismatch(LM, RM, I));
318         return false;
319       }
320       if (LM.lookup(LU->getUser()) != RM.lookup(RU->getUser())) {
321         DEBUG(debugUserMismatch(LM, RM, I));
322         return false;
323       }
324       if (LU->getOperandNo() != RU->getOperandNo()) {
325         DEBUG(debugUserMismatch(LM, RM, I));
326         return false;
327       }
328       skipUnmappedUsers(++LU, LE, LM);
329       skipUnmappedUsers(++RU, RE, RM);
330     }
331     if (RU != RE) {
332       DEBUG(debugUserMismatch(LM, RM, I));
333       return false;
334     }
335   }
336
337   return true;
338 }
339
340 static void verifyAfterRoundTrip(const Module &M,
341                                  std::unique_ptr<Module> OtherM) {
342   if (!OtherM)
343     report_fatal_error("parsing failed");
344   if (verifyModule(*OtherM, &errs()))
345     report_fatal_error("verification failed");
346   if (!matches(ValueMapping(M), ValueMapping(*OtherM)))
347     report_fatal_error("use-list order changed");
348 }
349 static void verifyBitcodeUseListOrder(const Module &M) {
350   TempFile F;
351   if (F.init("bc"))
352     report_fatal_error("failed to initialize bitcode file");
353
354   if (F.writeBitcode(M))
355     report_fatal_error("failed to write bitcode");
356
357   LLVMContext Context;
358   verifyAfterRoundTrip(M, F.readBitcode(Context));
359 }
360
361 static void verifyAssemblyUseListOrder(const Module &M) {
362   TempFile F;
363   if (F.init("ll"))
364     report_fatal_error("failed to initialize assembly file");
365
366   if (F.writeAssembly(M))
367     report_fatal_error("failed to write assembly");
368
369   LLVMContext Context;
370   verifyAfterRoundTrip(M, F.readAssembly(Context));
371 }
372
373 static void verifyUseListOrder(const Module &M) {
374   outs() << "verify bitcode\n";
375   verifyBitcodeUseListOrder(M);
376   outs() << "verify assembly\n";
377   verifyAssemblyUseListOrder(M);
378 }
379
380 static void shuffleValueUseLists(Value *V, std::minstd_rand0 &Gen,
381                                  DenseSet<Value *> &Seen) {
382   if (!Seen.insert(V).second)
383     return;
384
385   if (auto *C = dyn_cast<Constant>(V))
386     if (!isa<GlobalValue>(C))
387       for (Value *Op : C->operands())
388         shuffleValueUseLists(Op, Gen, Seen);
389
390   if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
391     // Nothing to shuffle for 0 or 1 users.
392     return;
393
394   // Generate random numbers between 10 and 99, which will line up nicely in
395   // debug output.  We're not worried about collisons here.
396   DEBUG(dbgs() << "V = "; V->dump());
397   std::uniform_int_distribution<short> Dist(10, 99);
398   SmallDenseMap<const Use *, short, 16> Order;
399   auto compareUses =
400       [&Order](const Use &L, const Use &R) { return Order[&L] < Order[&R]; };
401   do {
402     for (const Use &U : V->uses()) {
403       auto I = Dist(Gen);
404       Order[&U] = I;
405       DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo()
406                    << ", U = ";
407             U.getUser()->dump());
408     }
409   } while (std::is_sorted(V->use_begin(), V->use_end(), compareUses));
410
411   DEBUG(dbgs() << " => shuffle\n");
412   V->sortUseList(compareUses);
413
414   DEBUG({
415     for (const Use &U : V->uses()) {
416       dbgs() << " - order: " << Order.lookup(&U)
417              << ", op = " << U.getOperandNo() << ", U = ";
418       U.getUser()->dump();
419     }
420   });
421 }
422
423 static void reverseValueUseLists(Value *V, DenseSet<Value *> &Seen) {
424   if (!Seen.insert(V).second)
425     return;
426
427   if (auto *C = dyn_cast<Constant>(V))
428     if (!isa<GlobalValue>(C))
429       for (Value *Op : C->operands())
430         reverseValueUseLists(Op, Seen);
431
432   if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
433     // Nothing to shuffle for 0 or 1 users.
434     return;
435
436   DEBUG({
437     dbgs() << "V = ";
438     V->dump();
439     for (const Use &U : V->uses()) {
440       dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
441       U.getUser()->dump();
442     }
443     dbgs() << " => reverse\n";
444   });
445
446   V->reverseUseList();
447
448   DEBUG({
449     for (const Use &U : V->uses()) {
450       dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
451       U.getUser()->dump();
452     }
453   });
454 }
455
456 template <class Changer>
457 static void changeUseLists(Module &M, Changer changeValueUseList) {
458   // Visit every value that would be serialized to an IR file.
459   //
460   // Globals.
461   for (GlobalVariable &G : M.globals())
462     changeValueUseList(&G);
463   for (GlobalAlias &A : M.aliases())
464     changeValueUseList(&A);
465   for (Function &F : M)
466     changeValueUseList(&F);
467
468   // Constants used by globals.
469   for (GlobalVariable &G : M.globals())
470     if (G.hasInitializer())
471       changeValueUseList(G.getInitializer());
472   for (GlobalAlias &A : M.aliases())
473     changeValueUseList(A.getAliasee());
474   for (Function &F : M) {
475     if (F.hasPrefixData())
476       changeValueUseList(F.getPrefixData());
477     if (F.hasPrologueData())
478       changeValueUseList(F.getPrologueData());
479     if (F.hasPersonalityFn())
480       changeValueUseList(F.getPersonalityFn());
481   }
482
483   // Function bodies.
484   for (Function &F : M) {
485     for (Argument &A : F.args())
486       changeValueUseList(&A);
487     for (BasicBlock &BB : F)
488       changeValueUseList(&BB);
489     for (BasicBlock &BB : F)
490       for (Instruction &I : BB)
491         changeValueUseList(&I);
492
493     // Constants used by instructions.
494     for (BasicBlock &BB : F)
495       for (Instruction &I : BB)
496         for (Value *Op : I.operands())
497           if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
498               isa<InlineAsm>(Op))
499             changeValueUseList(Op);
500   }
501
502   if (verifyModule(M, &errs()))
503     report_fatal_error("verification failed");
504 }
505
506 static void shuffleUseLists(Module &M, unsigned SeedOffset) {
507   std::minstd_rand0 Gen(std::minstd_rand0::default_seed + SeedOffset);
508   DenseSet<Value *> Seen;
509   changeUseLists(M, [&](Value *V) { shuffleValueUseLists(V, Gen, Seen); });
510   DEBUG(dbgs() << "\n");
511 }
512
513 static void reverseUseLists(Module &M) {
514   DenseSet<Value *> Seen;
515   changeUseLists(M, [&](Value *V) { reverseValueUseLists(V, Seen); });
516   DEBUG(dbgs() << "\n");
517 }
518
519 int main(int argc, char **argv) {
520   sys::PrintStackTraceOnErrorSignal();
521   llvm::PrettyStackTraceProgram X(argc, argv);
522
523   // Enable debug stream buffering.
524   EnableDebugBuffering = true;
525
526   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
527   LLVMContext &Context = getGlobalContext();
528
529   cl::ParseCommandLineOptions(argc, argv,
530                               "llvm tool to verify use-list order\n");
531
532   SMDiagnostic Err;
533
534   // Load the input module...
535   std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
536
537   if (!M.get()) {
538     Err.print(argv[0], errs());
539     return 1;
540   }
541   if (verifyModule(*M, &errs())) {
542     errs() << argv[0] << ": " << InputFilename
543            << ": error: input module is broken!\n";
544     return 1;
545   }
546
547   // Verify the use lists now and after reversing them.
548   outs() << "*** verify-uselistorder ***\n";
549   verifyUseListOrder(*M);
550   outs() << "reverse\n";
551   reverseUseLists(*M);
552   verifyUseListOrder(*M);
553
554   for (unsigned I = 0, E = NumShuffles; I != E; ++I) {
555     outs() << "\n";
556
557     // Shuffle with a different (deterministic) seed each time.
558     outs() << "shuffle (" << I + 1 << " of " << E << ")\n";
559     shuffleUseLists(*M, I);
560
561     // Verify again before and after reversing.
562     verifyUseListOrder(*M);
563     outs() << "reverse\n";
564     reverseUseLists(*M);
565     verifyUseListOrder(*M);
566   }
567
568   return 0;
569 }