[Alignment][NFC] Convert StoreInst to MaybeAlign
[llvm-complete.git] / tools / verify-uselistorder / verify-uselistorder.cpp
blobf61d23940fb00eafc5966ce5361bd871e7a2b8e5
1 //===- verify-uselistorder.cpp - The LLVM Modular Optimizer ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Verify that use-list order can be serialized correctly. After reading the
10 // provided IR, this tool shuffles the use-lists and then writes and reads to a
11 // separate Module whose use-list orders are compared to the original.
13 // The shuffles are deterministic, but guarantee that use-lists will change.
14 // The algorithm per iteration is as follows:
16 // 1. Seed the random number generator. The seed is different for each
17 // shuffle. Shuffle 0 uses default+0, shuffle 1 uses default+1, and so on.
19 // 2. Visit every Value in a deterministic order.
21 // 3. Assign a random number to each Use in the Value's use-list in order.
23 // 4. If the numbers are already in order, reassign numbers until they aren't.
25 // 5. Sort the use-list using Value::sortUseList(), which is a stable sort.
27 //===----------------------------------------------------------------------===//
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/DenseSet.h"
31 #include "llvm/AsmParser/Parser.h"
32 #include "llvm/Bitcode/BitcodeReader.h"
33 #include "llvm/Bitcode/BitcodeWriter.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/InitLLVM.h"
45 #include "llvm/Support/MemoryBuffer.h"
46 #include "llvm/Support/SourceMgr.h"
47 #include "llvm/Support/SystemUtils.h"
48 #include "llvm/Support/raw_ostream.h"
49 #include <random>
50 #include <vector>
52 using namespace llvm;
54 #define DEBUG_TYPE "uselistorder"
56 static cl::opt<std::string> InputFilename(cl::Positional,
57 cl::desc("<input bitcode file>"),
58 cl::init("-"),
59 cl::value_desc("filename"));
61 static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temp files"),
62 cl::init(false));
64 static cl::opt<unsigned>
65 NumShuffles("num-shuffles",
66 cl::desc("Number of times to shuffle and verify use-lists"),
67 cl::init(1));
69 namespace {
71 struct TempFile {
72 std::string Filename;
73 FileRemover Remover;
74 bool init(const std::string &Ext);
75 bool writeBitcode(const Module &M) const;
76 bool writeAssembly(const Module &M) const;
77 std::unique_ptr<Module> readBitcode(LLVMContext &Context) const;
78 std::unique_ptr<Module> readAssembly(LLVMContext &Context) const;
81 struct ValueMapping {
82 DenseMap<const Value *, unsigned> IDs;
83 std::vector<const Value *> Values;
85 /// Construct a value mapping for module.
86 ///
87 /// Creates mapping from every value in \c M to an ID. This mapping includes
88 /// un-referencable values.
89 ///
90 /// Every \a Value that gets serialized in some way should be represented
91 /// here. The order needs to be deterministic, but it's unnecessary to match
92 /// the value-ids in the bitcode writer.
93 ///
94 /// All constants that are referenced by other values are included in the
95 /// mapping, but others -- which wouldn't be serialized -- are not.
96 ValueMapping(const Module &M);
98 /// Map a value.
99 ///
100 /// Maps a value. If it's a constant, maps all of its operands first.
101 void map(const Value *V);
102 unsigned lookup(const Value *V) const { return IDs.lookup(V); }
105 } // end namespace
107 bool TempFile::init(const std::string &Ext) {
108 SmallVector<char, 64> Vector;
109 LLVM_DEBUG(dbgs() << " - create-temp-file\n");
110 if (auto EC = sys::fs::createTemporaryFile("uselistorder", Ext, Vector)) {
111 errs() << "verify-uselistorder: error: " << EC.message() << "\n";
112 return true;
114 assert(!Vector.empty());
116 Filename.assign(Vector.data(), Vector.data() + Vector.size());
117 Remover.setFile(Filename, !SaveTemps);
118 if (SaveTemps)
119 outs() << " - filename = " << Filename << "\n";
120 return false;
123 bool TempFile::writeBitcode(const Module &M) const {
124 LLVM_DEBUG(dbgs() << " - write bitcode\n");
125 std::error_code EC;
126 raw_fd_ostream OS(Filename, EC, sys::fs::OF_None);
127 if (EC) {
128 errs() << "verify-uselistorder: error: " << EC.message() << "\n";
129 return true;
132 WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
133 return false;
136 bool TempFile::writeAssembly(const Module &M) const {
137 LLVM_DEBUG(dbgs() << " - write assembly\n");
138 std::error_code EC;
139 raw_fd_ostream OS(Filename, EC, sys::fs::OF_Text);
140 if (EC) {
141 errs() << "verify-uselistorder: error: " << EC.message() << "\n";
142 return true;
145 M.print(OS, nullptr, /* ShouldPreserveUseListOrder */ true);
146 return false;
149 std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
150 LLVM_DEBUG(dbgs() << " - read bitcode\n");
151 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOr =
152 MemoryBuffer::getFile(Filename);
153 if (!BufferOr) {
154 errs() << "verify-uselistorder: error: " << BufferOr.getError().message()
155 << "\n";
156 return nullptr;
159 MemoryBuffer *Buffer = BufferOr.get().get();
160 Expected<std::unique_ptr<Module>> ModuleOr =
161 parseBitcodeFile(Buffer->getMemBufferRef(), Context);
162 if (!ModuleOr) {
163 logAllUnhandledErrors(ModuleOr.takeError(), errs(),
164 "verify-uselistorder: error: ");
165 return nullptr;
167 return std::move(ModuleOr.get());
170 std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
171 LLVM_DEBUG(dbgs() << " - read assembly\n");
172 SMDiagnostic Err;
173 std::unique_ptr<Module> M = parseAssemblyFile(Filename, Err, Context);
174 if (!M.get())
175 Err.print("verify-uselistorder", errs());
176 return M;
179 ValueMapping::ValueMapping(const Module &M) {
180 // Every value should be mapped, including things like void instructions and
181 // basic blocks that are kept out of the ValueEnumerator.
183 // The current mapping order makes it easier to debug the tables. It happens
184 // to be similar to the ID mapping when writing ValueEnumerator, but they
185 // aren't (and needn't be) in sync.
187 // Globals.
188 for (const GlobalVariable &G : M.globals())
189 map(&G);
190 for (const GlobalAlias &A : M.aliases())
191 map(&A);
192 for (const GlobalIFunc &IF : M.ifuncs())
193 map(&IF);
194 for (const Function &F : M)
195 map(&F);
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 GlobalIFunc &IF : M.ifuncs())
204 map(IF.getResolver());
205 for (const Function &F : M) {
206 if (F.hasPrefixData())
207 map(F.getPrefixData());
208 if (F.hasPrologueData())
209 map(F.getPrologueData());
210 if (F.hasPersonalityFn())
211 map(F.getPersonalityFn());
214 // Function bodies.
215 for (const Function &F : M) {
216 for (const Argument &A : F.args())
217 map(&A);
218 for (const BasicBlock &BB : F)
219 map(&BB);
220 for (const BasicBlock &BB : F)
221 for (const Instruction &I : BB)
222 map(&I);
224 // Constants used by instructions.
225 for (const BasicBlock &BB : F)
226 for (const Instruction &I : BB)
227 for (const Value *Op : I.operands())
228 if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
229 isa<InlineAsm>(Op))
230 map(Op);
234 void ValueMapping::map(const Value *V) {
235 if (IDs.lookup(V))
236 return;
238 if (auto *C = dyn_cast<Constant>(V))
239 if (!isa<GlobalValue>(C))
240 for (const Value *Op : C->operands())
241 map(Op);
243 Values.push_back(V);
244 IDs[V] = Values.size();
247 #ifndef NDEBUG
248 static void dumpMapping(const ValueMapping &VM) {
249 dbgs() << "value-mapping (size = " << VM.Values.size() << "):\n";
250 for (unsigned I = 0, E = VM.Values.size(); I != E; ++I) {
251 dbgs() << " - id = " << I << ", value = ";
252 VM.Values[I]->dump();
256 static void debugValue(const ValueMapping &M, unsigned I, StringRef Desc) {
257 const Value *V = M.Values[I];
258 dbgs() << " - " << Desc << " value = ";
259 V->dump();
260 for (const Use &U : V->uses()) {
261 dbgs() << " => use: op = " << U.getOperandNo()
262 << ", user-id = " << M.IDs.lookup(U.getUser()) << ", user = ";
263 U.getUser()->dump();
267 static void debugUserMismatch(const ValueMapping &L, const ValueMapping &R,
268 unsigned I) {
269 dbgs() << " - fail: user mismatch: ID = " << I << "\n";
270 debugValue(L, I, "LHS");
271 debugValue(R, I, "RHS");
273 dbgs() << "\nlhs-";
274 dumpMapping(L);
275 dbgs() << "\nrhs-";
276 dumpMapping(R);
279 static void debugSizeMismatch(const ValueMapping &L, const ValueMapping &R) {
280 dbgs() << " - fail: map size: " << L.Values.size()
281 << " != " << R.Values.size() << "\n";
282 dbgs() << "\nlhs-";
283 dumpMapping(L);
284 dbgs() << "\nrhs-";
285 dumpMapping(R);
287 #endif
289 static bool matches(const ValueMapping &LM, const ValueMapping &RM) {
290 LLVM_DEBUG(dbgs() << "compare value maps\n");
291 if (LM.Values.size() != RM.Values.size()) {
292 LLVM_DEBUG(debugSizeMismatch(LM, RM));
293 return false;
296 // This mapping doesn't include dangling constant users, since those don't
297 // get serialized. However, checking if users are constant and calling
298 // isConstantUsed() on every one is very expensive. Instead, just check if
299 // the user is mapped.
300 auto skipUnmappedUsers =
301 [&](Value::const_use_iterator &U, Value::const_use_iterator E,
302 const ValueMapping &M) {
303 while (U != E && !M.lookup(U->getUser()))
304 ++U;
307 // Iterate through all values, and check that both mappings have the same
308 // users.
309 for (unsigned I = 0, E = LM.Values.size(); I != E; ++I) {
310 const Value *L = LM.Values[I];
311 const Value *R = RM.Values[I];
312 auto LU = L->use_begin(), LE = L->use_end();
313 auto RU = R->use_begin(), RE = R->use_end();
314 skipUnmappedUsers(LU, LE, LM);
315 skipUnmappedUsers(RU, RE, RM);
317 while (LU != LE) {
318 if (RU == RE) {
319 LLVM_DEBUG(debugUserMismatch(LM, RM, I));
320 return false;
322 if (LM.lookup(LU->getUser()) != RM.lookup(RU->getUser())) {
323 LLVM_DEBUG(debugUserMismatch(LM, RM, I));
324 return false;
326 if (LU->getOperandNo() != RU->getOperandNo()) {
327 LLVM_DEBUG(debugUserMismatch(LM, RM, I));
328 return false;
330 skipUnmappedUsers(++LU, LE, LM);
331 skipUnmappedUsers(++RU, RE, RM);
333 if (RU != RE) {
334 LLVM_DEBUG(debugUserMismatch(LM, RM, I));
335 return false;
339 return true;
342 static void verifyAfterRoundTrip(const Module &M,
343 std::unique_ptr<Module> OtherM) {
344 if (!OtherM)
345 report_fatal_error("parsing failed");
346 if (verifyModule(*OtherM, &errs()))
347 report_fatal_error("verification failed");
348 if (!matches(ValueMapping(M), ValueMapping(*OtherM)))
349 report_fatal_error("use-list order changed");
352 static void verifyBitcodeUseListOrder(const Module &M) {
353 TempFile F;
354 if (F.init("bc"))
355 report_fatal_error("failed to initialize bitcode file");
357 if (F.writeBitcode(M))
358 report_fatal_error("failed to write bitcode");
360 LLVMContext Context;
361 verifyAfterRoundTrip(M, F.readBitcode(Context));
364 static void verifyAssemblyUseListOrder(const Module &M) {
365 TempFile F;
366 if (F.init("ll"))
367 report_fatal_error("failed to initialize assembly file");
369 if (F.writeAssembly(M))
370 report_fatal_error("failed to write assembly");
372 LLVMContext Context;
373 verifyAfterRoundTrip(M, F.readAssembly(Context));
376 static void verifyUseListOrder(const Module &M) {
377 outs() << "verify bitcode\n";
378 verifyBitcodeUseListOrder(M);
379 outs() << "verify assembly\n";
380 verifyAssemblyUseListOrder(M);
383 static void shuffleValueUseLists(Value *V, std::minstd_rand0 &Gen,
384 DenseSet<Value *> &Seen) {
385 if (!Seen.insert(V).second)
386 return;
388 if (auto *C = dyn_cast<Constant>(V))
389 if (!isa<GlobalValue>(C))
390 for (Value *Op : C->operands())
391 shuffleValueUseLists(Op, Gen, Seen);
393 if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
394 // Nothing to shuffle for 0 or 1 users.
395 return;
397 // Generate random numbers between 10 and 99, which will line up nicely in
398 // debug output. We're not worried about collisons here.
399 LLVM_DEBUG(dbgs() << "V = "; V->dump());
400 std::uniform_int_distribution<short> Dist(10, 99);
401 SmallDenseMap<const Use *, short, 16> Order;
402 auto compareUses =
403 [&Order](const Use &L, const Use &R) { return Order[&L] < Order[&R]; };
404 do {
405 for (const Use &U : V->uses()) {
406 auto I = Dist(Gen);
407 Order[&U] = I;
408 LLVM_DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo()
409 << ", U = ";
410 U.getUser()->dump());
412 } while (std::is_sorted(V->use_begin(), V->use_end(), compareUses));
414 LLVM_DEBUG(dbgs() << " => shuffle\n");
415 V->sortUseList(compareUses);
417 LLVM_DEBUG({
418 for (const Use &U : V->uses()) {
419 dbgs() << " - order: " << Order.lookup(&U)
420 << ", op = " << U.getOperandNo() << ", U = ";
421 U.getUser()->dump();
426 static void reverseValueUseLists(Value *V, DenseSet<Value *> &Seen) {
427 if (!Seen.insert(V).second)
428 return;
430 if (auto *C = dyn_cast<Constant>(V))
431 if (!isa<GlobalValue>(C))
432 for (Value *Op : C->operands())
433 reverseValueUseLists(Op, Seen);
435 if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
436 // Nothing to shuffle for 0 or 1 users.
437 return;
439 LLVM_DEBUG({
440 dbgs() << "V = ";
441 V->dump();
442 for (const Use &U : V->uses()) {
443 dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
444 U.getUser()->dump();
446 dbgs() << " => reverse\n";
449 V->reverseUseList();
451 LLVM_DEBUG({
452 for (const Use &U : V->uses()) {
453 dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
454 U.getUser()->dump();
459 template <class Changer>
460 static void changeUseLists(Module &M, Changer changeValueUseList) {
461 // Visit every value that would be serialized to an IR file.
463 // Globals.
464 for (GlobalVariable &G : M.globals())
465 changeValueUseList(&G);
466 for (GlobalAlias &A : M.aliases())
467 changeValueUseList(&A);
468 for (GlobalIFunc &IF : M.ifuncs())
469 changeValueUseList(&IF);
470 for (Function &F : M)
471 changeValueUseList(&F);
473 // Constants used by globals.
474 for (GlobalVariable &G : M.globals())
475 if (G.hasInitializer())
476 changeValueUseList(G.getInitializer());
477 for (GlobalAlias &A : M.aliases())
478 changeValueUseList(A.getAliasee());
479 for (GlobalIFunc &IF : M.ifuncs())
480 changeValueUseList(IF.getResolver());
481 for (Function &F : M) {
482 if (F.hasPrefixData())
483 changeValueUseList(F.getPrefixData());
484 if (F.hasPrologueData())
485 changeValueUseList(F.getPrologueData());
486 if (F.hasPersonalityFn())
487 changeValueUseList(F.getPersonalityFn());
490 // Function bodies.
491 for (Function &F : M) {
492 for (Argument &A : F.args())
493 changeValueUseList(&A);
494 for (BasicBlock &BB : F)
495 changeValueUseList(&BB);
496 for (BasicBlock &BB : F)
497 for (Instruction &I : BB)
498 changeValueUseList(&I);
500 // Constants used by instructions.
501 for (BasicBlock &BB : F)
502 for (Instruction &I : BB)
503 for (Value *Op : I.operands())
504 if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
505 isa<InlineAsm>(Op))
506 changeValueUseList(Op);
509 if (verifyModule(M, &errs()))
510 report_fatal_error("verification failed");
513 static void shuffleUseLists(Module &M, unsigned SeedOffset) {
514 std::minstd_rand0 Gen(std::minstd_rand0::default_seed + SeedOffset);
515 DenseSet<Value *> Seen;
516 changeUseLists(M, [&](Value *V) { shuffleValueUseLists(V, Gen, Seen); });
517 LLVM_DEBUG(dbgs() << "\n");
520 static void reverseUseLists(Module &M) {
521 DenseSet<Value *> Seen;
522 changeUseLists(M, [&](Value *V) { reverseValueUseLists(V, Seen); });
523 LLVM_DEBUG(dbgs() << "\n");
526 int main(int argc, char **argv) {
527 InitLLVM X(argc, argv);
529 // Enable debug stream buffering.
530 EnableDebugBuffering = true;
532 LLVMContext Context;
534 cl::ParseCommandLineOptions(argc, argv,
535 "llvm tool to verify use-list order\n");
537 SMDiagnostic Err;
539 // Load the input module...
540 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
542 if (!M.get()) {
543 Err.print(argv[0], errs());
544 return 1;
546 if (verifyModule(*M, &errs())) {
547 errs() << argv[0] << ": " << InputFilename
548 << ": error: input module is broken!\n";
549 return 1;
552 // Verify the use lists now and after reversing them.
553 outs() << "*** verify-uselistorder ***\n";
554 verifyUseListOrder(*M);
555 outs() << "reverse\n";
556 reverseUseLists(*M);
557 verifyUseListOrder(*M);
559 for (unsigned I = 0, E = NumShuffles; I != E; ++I) {
560 outs() << "\n";
562 // Shuffle with a different (deterministic) seed each time.
563 outs() << "shuffle (" << I + 1 << " of " << E << ")\n";
564 shuffleUseLists(*M, I);
566 // Verify again before and after reversing.
567 verifyUseListOrder(*M);
568 outs() << "reverse\n";
569 reverseUseLists(*M);
570 verifyUseListOrder(*M);
573 return 0;