1 //===- verify-uselistorder.cpp - The LLVM Modular Optimizer ---------------===//
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 // 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.
14 // The shuffles are deterministic, but guarantee that use-lists will change.
15 // The algorithm per iteration is as follows:
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.
20 // 2. Visit every Value in a deterministic order.
22 // 3. Assign a random number to each Use in the Value's use-list in order.
24 // 4. If the numbers are already in order, reassign numbers until they aren't.
26 // 5. Sort the use-list using Value::sortUseList(), which is a stable sort.
28 //===----------------------------------------------------------------------===//
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"
56 #define DEBUG_TYPE "uselistorder"
58 static cl::opt<std::string> InputFilename(cl::Positional,
59 cl::desc("<input bitcode file>"),
61 cl::value_desc("filename"));
63 static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temp files"),
66 static cl::opt<unsigned>
67 NumShuffles("num-shuffles",
68 cl::desc("Number of times to shuffle and verify use-lists"),
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;
84 DenseMap<const Value *, unsigned> IDs;
85 std::vector<const Value *> Values;
87 /// \brief Construct a value mapping for module.
89 /// Creates mapping from every value in \c M to an ID. This mapping includes
90 /// un-referencable values.
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.
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);
100 /// \brief Map a value.
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); }
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";
116 assert(!Vector.empty());
118 Filename.assign(Vector.data(), Vector.data() + Vector.size());
119 Remover.setFile(Filename, !SaveTemps);
121 outs() << " - filename = " << Filename << "\n";
125 bool TempFile::writeBitcode(const Module &M) const {
126 DEBUG(dbgs() << " - write bitcode\n");
128 raw_fd_ostream OS(Filename, EC, sys::fs::F_None);
130 errs() << "verify-uselistorder: error: " << EC.message() << "\n";
134 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
138 bool TempFile::writeAssembly(const Module &M) const {
139 DEBUG(dbgs() << " - write assembly\n");
141 raw_fd_ostream OS(Filename, EC, sys::fs::F_Text);
143 errs() << "verify-uselistorder: error: " << EC.message() << "\n";
147 M.print(OS, nullptr, /* ShouldPreserveUseListOrder */ true);
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);
156 errs() << "verify-uselistorder: error: " << BufferOr.getError().message()
161 MemoryBuffer *Buffer = BufferOr.get().get();
162 ErrorOr<std::unique_ptr<Module>> ModuleOr =
163 parseBitcodeFile(Buffer->getMemBufferRef(), Context);
165 errs() << "verify-uselistorder: error: " << ModuleOr.getError().message()
169 return std::move(ModuleOr.get());
172 std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
173 DEBUG(dbgs() << " - read assembly\n");
175 std::unique_ptr<Module> M = parseAssemblyFile(Filename, Err, Context);
177 Err.print("verify-uselistorder", errs());
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.
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.
190 for (const GlobalVariable &G : M.globals())
192 for (const GlobalAlias &A : M.aliases())
194 for (const Function &F : M)
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())
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());
213 for (const Function &F : M) {
214 for (const Argument &A : F.args())
216 for (const BasicBlock &BB : F)
218 for (const BasicBlock &BB : F)
219 for (const Instruction &I : BB)
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)) ||
232 void ValueMapping::map(const Value *V) {
236 if (auto *C = dyn_cast<Constant>(V))
237 if (!isa<GlobalValue>(C))
238 for (const Value *Op : C->operands())
242 IDs[V] = Values.size();
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();
254 static void debugValue(const ValueMapping &M, unsigned I, StringRef Desc) {
255 const Value *V = M.Values[I];
256 dbgs() << " - " << Desc << " value = ";
258 for (const Use &U : V->uses()) {
259 dbgs() << " => use: op = " << U.getOperandNo()
260 << ", user-id = " << M.IDs.lookup(U.getUser()) << ", user = ";
265 static void debugUserMismatch(const ValueMapping &L, const ValueMapping &R,
267 dbgs() << " - fail: user mismatch: ID = " << I << "\n";
268 debugValue(L, I, "LHS");
269 debugValue(R, I, "RHS");
277 static void debugSizeMismatch(const ValueMapping &L, const ValueMapping &R) {
278 dbgs() << " - fail: map size: " << L.Values.size()
279 << " != " << R.Values.size() << "\n";
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));
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()))
305 // Iterate through all values, and check that both mappings have the same
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);
317 DEBUG(debugUserMismatch(LM, RM, I));
320 if (LM.lookup(LU->getUser()) != RM.lookup(RU->getUser())) {
321 DEBUG(debugUserMismatch(LM, RM, I));
324 if (LU->getOperandNo() != RU->getOperandNo()) {
325 DEBUG(debugUserMismatch(LM, RM, I));
328 skipUnmappedUsers(++LU, LE, LM);
329 skipUnmappedUsers(++RU, RE, RM);
332 DEBUG(debugUserMismatch(LM, RM, I));
340 static void verifyAfterRoundTrip(const Module &M,
341 std::unique_ptr<Module> 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");
349 static void verifyBitcodeUseListOrder(const Module &M) {
352 report_fatal_error("failed to initialize bitcode file");
354 if (F.writeBitcode(M))
355 report_fatal_error("failed to write bitcode");
358 verifyAfterRoundTrip(M, F.readBitcode(Context));
361 static void verifyAssemblyUseListOrder(const Module &M) {
364 report_fatal_error("failed to initialize assembly file");
366 if (F.writeAssembly(M))
367 report_fatal_error("failed to write assembly");
370 verifyAfterRoundTrip(M, F.readAssembly(Context));
373 static void verifyUseListOrder(const Module &M) {
374 outs() << "verify bitcode\n";
375 verifyBitcodeUseListOrder(M);
376 outs() << "verify assembly\n";
377 verifyAssemblyUseListOrder(M);
380 static void shuffleValueUseLists(Value *V, std::minstd_rand0 &Gen,
381 DenseSet<Value *> &Seen) {
382 if (!Seen.insert(V).second)
385 if (auto *C = dyn_cast<Constant>(V))
386 if (!isa<GlobalValue>(C))
387 for (Value *Op : C->operands())
388 shuffleValueUseLists(Op, Gen, Seen);
390 if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
391 // Nothing to shuffle for 0 or 1 users.
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;
400 [&Order](const Use &L, const Use &R) { return Order[&L] < Order[&R]; };
402 for (const Use &U : V->uses()) {
405 DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo()
407 U.getUser()->dump());
409 } while (std::is_sorted(V->use_begin(), V->use_end(), compareUses));
411 DEBUG(dbgs() << " => shuffle\n");
412 V->sortUseList(compareUses);
415 for (const Use &U : V->uses()) {
416 dbgs() << " - order: " << Order.lookup(&U)
417 << ", op = " << U.getOperandNo() << ", U = ";
423 static void reverseValueUseLists(Value *V, DenseSet<Value *> &Seen) {
424 if (!Seen.insert(V).second)
427 if (auto *C = dyn_cast<Constant>(V))
428 if (!isa<GlobalValue>(C))
429 for (Value *Op : C->operands())
430 reverseValueUseLists(Op, Seen);
432 if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
433 // Nothing to shuffle for 0 or 1 users.
439 for (const Use &U : V->uses()) {
440 dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
443 dbgs() << " => reverse\n";
449 for (const Use &U : V->uses()) {
450 dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
456 template <class Changer>
457 static void changeUseLists(Module &M, Changer changeValueUseList) {
458 // Visit every value that would be serialized to an IR file.
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);
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());
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);
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)) ||
499 changeValueUseList(Op);
502 if (verifyModule(M, &errs()))
503 report_fatal_error("verification failed");
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");
513 static void reverseUseLists(Module &M) {
514 DenseSet<Value *> Seen;
515 changeUseLists(M, [&](Value *V) { reverseValueUseLists(V, Seen); });
516 DEBUG(dbgs() << "\n");
519 int main(int argc, char **argv) {
520 sys::PrintStackTraceOnErrorSignal();
521 llvm::PrettyStackTraceProgram X(argc, argv);
523 // Enable debug stream buffering.
524 EnableDebugBuffering = true;
526 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
527 LLVMContext &Context = getGlobalContext();
529 cl::ParseCommandLineOptions(argc, argv,
530 "llvm tool to verify use-list order\n");
534 // Load the input module...
535 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
538 Err.print(argv[0], errs());
541 if (verifyModule(*M, &errs())) {
542 errs() << argv[0] << ": " << InputFilename
543 << ": error: input module is broken!\n";
547 // Verify the use lists now and after reversing them.
548 outs() << "*** verify-uselistorder ***\n";
549 verifyUseListOrder(*M);
550 outs() << "reverse\n";
552 verifyUseListOrder(*M);
554 for (unsigned I = 0, E = NumShuffles; I != E; ++I) {
557 // Shuffle with a different (deterministic) seed each time.
558 outs() << "shuffle (" << I + 1 << " of " << E << ")\n";
559 shuffleUseLists(*M, I);
561 // Verify again before and after reversing.
562 verifyUseListOrder(*M);
563 outs() << "reverse\n";
565 verifyUseListOrder(*M);