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"
55 #define DEBUG_TYPE "use-list-order"
57 static cl::opt<std::string> InputFilename(cl::Positional,
58 cl::desc("<input bitcode file>"),
60 cl::value_desc("filename"));
62 static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temp files"),
65 static cl::opt<unsigned>
66 NumShuffles("num-shuffles",
67 cl::desc("Number of times to shuffle and verify use-lists"),
75 bool init(const std::string &Ext);
76 bool writeBitcode(const Module &M) const;
77 bool writeAssembly(const Module &M) const;
78 std::unique_ptr<Module> readBitcode(LLVMContext &Context) const;
79 std::unique_ptr<Module> readAssembly(LLVMContext &Context) const;
83 DenseMap<const Value *, unsigned> IDs;
84 std::vector<const Value *> Values;
86 /// \brief Construct a value mapping for module.
88 /// Creates mapping from every value in \c M to an ID. This mapping includes
89 /// un-referencable values.
91 /// Every \a Value that gets serialized in some way should be represented
92 /// here. The order needs to be deterministic, but it's unnecessary to match
93 /// the value-ids in the bitcode writer.
95 /// All constants that are referenced by other values are included in the
96 /// mapping, but others -- which wouldn't be serialized -- are not.
97 ValueMapping(const Module &M);
99 /// \brief Map a value.
101 /// Maps a value. If it's a constant, maps all of its operands first.
102 void map(const Value *V);
103 unsigned lookup(const Value *V) const { return IDs.lookup(V); }
108 bool TempFile::init(const std::string &Ext) {
109 SmallVector<char, 64> Vector;
110 DEBUG(dbgs() << " - create-temp-file\n");
111 if (auto EC = sys::fs::createTemporaryFile("use-list-order", Ext, Vector)) {
113 DEBUG(dbgs() << "error: " << EC.message() << "\n");
116 assert(!Vector.empty());
118 Filename.assign(Vector.data(), Vector.data() + Vector.size());
119 Remover.setFile(Filename, !SaveTemps);
120 DEBUG(dbgs() << " - filename = " << Filename << "\n");
124 bool TempFile::writeBitcode(const Module &M) const {
125 DEBUG(dbgs() << " - write bitcode\n");
127 raw_fd_ostream OS(Filename, EC, sys::fs::F_None);
129 DEBUG(dbgs() << "error: " << EC.message() << "\n");
133 WriteBitcodeToFile(&M, OS);
137 bool TempFile::writeAssembly(const Module &M) const {
138 DEBUG(dbgs() << " - write assembly\n");
140 raw_fd_ostream OS(Filename, EC, sys::fs::F_Text);
142 DEBUG(dbgs() << "error: " << EC.message() << "\n");
150 std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
151 DEBUG(dbgs() << " - read bitcode\n");
152 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOr =
153 MemoryBuffer::getFile(Filename);
155 DEBUG(dbgs() << "error: " << BufferOr.getError().message() << "\n");
159 MemoryBuffer *Buffer = BufferOr.get().get();
160 ErrorOr<Module *> ModuleOr =
161 parseBitcodeFile(Buffer->getMemBufferRef(), Context);
163 DEBUG(dbgs() << "error: " << ModuleOr.getError().message() << "\n");
166 return std::unique_ptr<Module>(ModuleOr.get());
169 std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
170 DEBUG(dbgs() << " - read assembly\n");
172 std::unique_ptr<Module> M = parseAssemblyFile(Filename, Err, Context);
174 DEBUG(dbgs() << "error: "; Err.print("verify-use-list-order", dbgs()));
178 ValueMapping::ValueMapping(const Module &M) {
179 // Every value should be mapped, including things like void instructions and
180 // basic blocks that are kept out of the ValueEnumerator.
182 // The current mapping order makes it easier to debug the tables. It happens
183 // to be similar to the ID mapping when writing ValueEnumerator, but they
184 // aren't (and needn't be) in sync.
187 for (const GlobalVariable &G : M.globals())
189 for (const GlobalAlias &A : M.aliases())
191 for (const Function &F : M)
194 // Constants used by globals.
195 for (const GlobalVariable &G : M.globals())
196 if (G.hasInitializer())
197 map(G.getInitializer());
198 for (const GlobalAlias &A : M.aliases())
200 for (const Function &F : M) {
201 if (F.hasPrefixData())
202 map(F.getPrefixData());
203 if (F.hasPrologueData())
204 map(F.getPrologueData());
208 for (const Function &F : M) {
209 for (const Argument &A : F.args())
211 for (const BasicBlock &BB : F)
213 for (const BasicBlock &BB : F)
214 for (const Instruction &I : BB)
217 // Constants used by instructions.
218 for (const BasicBlock &BB : F)
219 for (const Instruction &I : BB)
220 for (const Value *Op : I.operands())
221 if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
227 void ValueMapping::map(const Value *V) {
231 if (auto *C = dyn_cast<Constant>(V))
232 if (!isa<GlobalValue>(C))
233 for (const Value *Op : C->operands())
237 IDs[V] = Values.size();
241 static void dumpMapping(const ValueMapping &VM) {
242 dbgs() << "value-mapping (size = " << VM.Values.size() << "):\n";
243 for (unsigned I = 0, E = VM.Values.size(); I != E; ++I) {
244 dbgs() << " - id = " << I << ", value = ";
245 VM.Values[I]->dump();
249 static void debugValue(const ValueMapping &M, unsigned I, StringRef Desc) {
250 const Value *V = M.Values[I];
251 dbgs() << " - " << Desc << " value = ";
253 for (const Use &U : V->uses()) {
254 dbgs() << " => use: op = " << U.getOperandNo()
255 << ", user-id = " << M.IDs.lookup(U.getUser()) << ", user = ";
260 static void debugUserMismatch(const ValueMapping &L, const ValueMapping &R,
262 dbgs() << " - fail: user mismatch: ID = " << I << "\n";
263 debugValue(L, I, "LHS");
264 debugValue(R, I, "RHS");
272 static void debugSizeMismatch(const ValueMapping &L, const ValueMapping &R) {
273 dbgs() << " - fail: map size: " << L.Values.size()
274 << " != " << R.Values.size() << "\n";
282 static bool matches(const ValueMapping &LM, const ValueMapping &RM) {
283 DEBUG(dbgs() << "compare value maps\n");
284 if (LM.Values.size() != RM.Values.size()) {
285 DEBUG(debugSizeMismatch(LM, RM));
289 // This mapping doesn't include dangling constant users, since those don't
290 // get serialized. However, checking if users are constant and calling
291 // isConstantUsed() on every one is very expensive. Instead, just check if
292 // the user is mapped.
293 auto skipUnmappedUsers =
294 [&](Value::const_use_iterator &U, Value::const_use_iterator E,
295 const ValueMapping &M) {
296 while (U != E && !M.lookup(U->getUser()))
300 // Iterate through all values, and check that both mappings have the same
302 for (unsigned I = 0, E = LM.Values.size(); I != E; ++I) {
303 const Value *L = LM.Values[I];
304 const Value *R = RM.Values[I];
305 auto LU = L->use_begin(), LE = L->use_end();
306 auto RU = R->use_begin(), RE = R->use_end();
307 skipUnmappedUsers(LU, LE, LM);
308 skipUnmappedUsers(RU, RE, RM);
312 DEBUG(debugUserMismatch(LM, RM, I));
315 if (LM.lookup(LU->getUser()) != RM.lookup(RU->getUser())) {
316 DEBUG(debugUserMismatch(LM, RM, I));
319 if (LU->getOperandNo() != RU->getOperandNo()) {
320 DEBUG(debugUserMismatch(LM, RM, I));
323 skipUnmappedUsers(++LU, LE, LM);
324 skipUnmappedUsers(++RU, RE, RM);
327 DEBUG(debugUserMismatch(LM, RM, I));
335 static void verifyAfterRoundTrip(const Module &M,
336 std::unique_ptr<Module> OtherM) {
338 report_fatal_error("parsing failed");
339 if (verifyModule(*OtherM, &errs()))
340 report_fatal_error("verification failed");
341 if (!matches(ValueMapping(M), ValueMapping(*OtherM)))
342 report_fatal_error("use-list order changed");
344 static void verifyBitcodeUseListOrder(const Module &M) {
345 errs() << "*** verify-use-list-order: bitcode ***\n";
348 report_fatal_error("failed to initialize bitcode file");
350 if (F.writeBitcode(M))
351 report_fatal_error("failed to write bitcode");
354 verifyAfterRoundTrip(M, F.readBitcode(Context));
357 static void verifyAssemblyUseListOrder(const Module &M) {
358 errs() << "*** verify-use-list-order: assembly ***\n";
361 report_fatal_error("failed to initialize assembly file");
363 if (F.writeAssembly(M))
364 report_fatal_error("failed to write assembly");
367 verifyAfterRoundTrip(M, F.readAssembly(Context));
370 static void verifyUseListOrder(const Module &M) {
371 verifyBitcodeUseListOrder(M);
372 verifyAssemblyUseListOrder(M);
375 static void shuffleValueUseLists(Value *V, std::minstd_rand0 &Gen,
376 DenseSet<Value *> &Seen) {
377 if (!Seen.insert(V).second)
380 if (auto *C = dyn_cast<Constant>(V))
381 if (!isa<GlobalValue>(C))
382 for (Value *Op : C->operands())
383 shuffleValueUseLists(Op, Gen, Seen);
385 if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
386 // Nothing to shuffle for 0 or 1 users.
389 // Generate random numbers between 10 and 99, which will line up nicely in
390 // debug output. We're not worried about collisons here.
391 DEBUG(dbgs() << "V = "; V->dump());
392 std::uniform_int_distribution<short> Dist(10, 99);
393 SmallDenseMap<const Use *, short, 16> Order;
395 [&Order](const Use &L, const Use &R) { return Order[&L] < Order[&R]; };
397 for (const Use &U : V->uses()) {
400 DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo()
402 U.getUser()->dump());
404 } while (std::is_sorted(V->use_begin(), V->use_end(), compareUses));
406 DEBUG(dbgs() << " => shuffle\n");
407 V->sortUseList(compareUses);
410 for (const Use &U : V->uses()) {
411 dbgs() << " - order: " << Order.lookup(&U)
412 << ", op = " << U.getOperandNo() << ", U = ";
418 static void reverseValueUseLists(Value *V, DenseSet<Value *> &Seen) {
419 if (!Seen.insert(V).second)
422 if (auto *C = dyn_cast<Constant>(V))
423 if (!isa<GlobalValue>(C))
424 for (Value *Op : C->operands())
425 reverseValueUseLists(Op, Seen);
427 if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
428 // Nothing to shuffle for 0 or 1 users.
434 for (const Use &U : V->uses()) {
435 dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
438 dbgs() << " => reverse\n";
444 for (const Use &U : V->uses()) {
445 dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
451 template <class Changer>
452 static void changeUseLists(Module &M, Changer changeValueUseList) {
453 // Visit every value that would be serialized to an IR file.
456 for (GlobalVariable &G : M.globals())
457 changeValueUseList(&G);
458 for (GlobalAlias &A : M.aliases())
459 changeValueUseList(&A);
460 for (Function &F : M)
461 changeValueUseList(&F);
463 // Constants used by globals.
464 for (GlobalVariable &G : M.globals())
465 if (G.hasInitializer())
466 changeValueUseList(G.getInitializer());
467 for (GlobalAlias &A : M.aliases())
468 changeValueUseList(A.getAliasee());
469 for (Function &F : M) {
470 if (F.hasPrefixData())
471 changeValueUseList(F.getPrefixData());
472 if (F.hasPrologueData())
473 changeValueUseList(F.getPrologueData());
477 for (Function &F : M) {
478 for (Argument &A : F.args())
479 changeValueUseList(&A);
480 for (BasicBlock &BB : F)
481 changeValueUseList(&BB);
482 for (BasicBlock &BB : F)
483 for (Instruction &I : BB)
484 changeValueUseList(&I);
486 // Constants used by instructions.
487 for (BasicBlock &BB : F)
488 for (Instruction &I : BB)
489 for (Value *Op : I.operands())
490 if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
492 changeValueUseList(Op);
495 if (verifyModule(M, &errs()))
496 report_fatal_error("verification failed");
499 static void shuffleUseLists(Module &M, unsigned SeedOffset) {
500 errs() << "*** shuffle-use-lists ***\n";
501 std::minstd_rand0 Gen(std::minstd_rand0::default_seed + SeedOffset);
502 DenseSet<Value *> Seen;
503 changeUseLists(M, [&](Value *V) { shuffleValueUseLists(V, Gen, Seen); });
504 DEBUG(dbgs() << "\n");
507 static void reverseUseLists(Module &M) {
508 errs() << "*** reverse-use-lists ***\n";
509 DenseSet<Value *> Seen;
510 changeUseLists(M, [&](Value *V) { reverseValueUseLists(V, Seen); });
511 DEBUG(dbgs() << "\n");
514 int main(int argc, char **argv) {
515 sys::PrintStackTraceOnErrorSignal();
516 llvm::PrettyStackTraceProgram X(argc, argv);
518 // Enable debug stream buffering.
519 EnableDebugBuffering = true;
521 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
522 LLVMContext &Context = getGlobalContext();
524 cl::ParseCommandLineOptions(argc, argv,
525 "llvm tool to verify use-list order\n");
529 // Load the input module...
530 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
533 Err.print(argv[0], errs());
536 if (verifyModule(*M, &errs()))
537 report_fatal_error("verification failed");
539 errs() << "*** verify-use-list-order ***\n";
540 // Can't verify if order isn't preserved.
541 if (!shouldPreserveBitcodeUseListOrder()) {
542 errs() << "warning: forcing -preserve-bc-use-list-order\n";
543 setPreserveBitcodeUseListOrder(true);
545 if (!shouldPreserveAssemblyUseListOrder()) {
546 errs() << "warning: forcing -preserve-ll-use-list-order\n";
547 setPreserveAssemblyUseListOrder(true);
550 // Verify the use lists now and after reversing them.
551 verifyUseListOrder(*M);
553 verifyUseListOrder(*M);
555 for (unsigned I = 0, E = NumShuffles; I != E; ++I) {
556 errs() << "*** shuffle iteration: " << I + 1 << " of " << E << " ***\n";
558 // Shuffle with a different (deterministic) seed each time.
559 shuffleUseLists(*M, I);
561 // Verify again before and after reversing.
562 verifyUseListOrder(*M);
564 verifyUseListOrder(*M);