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 and somewhat naive. On a given shuffle, some
15 // use-lists will not change at all. 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. Sort the use-list using Value::sortUseList(), which is a stable sort.
26 // Shuffling a larger number of times provides a better statistical guarantee
27 // that each use-list has changed at least once.
29 //===----------------------------------------------------------------------===//
31 #include "llvm/ADT/DenseMap.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/IRReader/IRReader.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/FileSystem.h"
42 #include "llvm/Support/FileUtilities.h"
43 #include "llvm/Support/ManagedStatic.h"
44 #include "llvm/Support/MemoryBuffer.h"
45 #include "llvm/Support/PrettyStackTrace.h"
46 #include "llvm/Support/Signals.h"
47 #include "llvm/Support/SourceMgr.h"
48 #include "llvm/Support/SystemUtils.h"
52 #define DEBUG_TYPE "use-list-order"
54 static cl::opt<std::string> InputFilename(cl::Positional,
55 cl::desc("<input bitcode file>"),
57 cl::value_desc("filename"));
59 static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temp files"),
62 static cl::opt<unsigned>
63 NumShuffles("num-shuffles",
64 cl::desc("Number of times to shuffle and verify use-lists"),
72 bool init(const std::string &Ext);
73 bool writeBitcode(const Module &M) const;
74 bool writeAssembly(const Module &M) const;
75 std::unique_ptr<Module> readBitcode(LLVMContext &Context) const;
76 std::unique_ptr<Module> readAssembly(LLVMContext &Context) const;
80 DenseMap<const Value *, unsigned> IDs;
81 std::vector<const Value *> Values;
83 /// \brief Construct a value mapping for module.
85 /// Creates mapping from every value in \c M to an ID. This mapping includes
86 /// un-referencable values.
88 /// Every \a Value that gets serialized in some way should be represented
89 /// here. The order needs to be deterministic, but it's unnecessary to match
90 /// the value-ids in the bitcode writer.
92 /// All constants that are referenced by other values are included in the
93 /// mapping, but others -- which wouldn't be serialized -- are not.
94 ValueMapping(const Module &M);
96 /// \brief Map a value.
98 /// Maps a value. If it's a constant, maps all of its operands first.
99 void map(const Value *V);
100 unsigned lookup(const Value *V) const { return IDs.lookup(V); }
105 bool TempFile::init(const std::string &Ext) {
106 SmallVector<char, 64> Vector;
107 DEBUG(dbgs() << " - create-temp-file\n");
108 if (auto EC = sys::fs::createTemporaryFile("use-list-order", Ext, Vector)) {
110 DEBUG(dbgs() << "error: " << EC.message() << "\n");
113 assert(!Vector.empty());
115 Filename.assign(Vector.data(), Vector.data() + Vector.size());
116 Remover.setFile(Filename, !SaveTemps);
117 DEBUG(dbgs() << " - filename = " << Filename << "\n");
121 bool TempFile::writeBitcode(const Module &M) const {
122 DEBUG(dbgs() << " - write bitcode\n");
123 std::string ErrorInfo;
124 raw_fd_ostream OS(Filename.c_str(), ErrorInfo, sys::fs::F_None);
125 if (!ErrorInfo.empty()) {
126 DEBUG(dbgs() << "error: " << ErrorInfo << "\n");
130 WriteBitcodeToFile(&M, OS);
134 bool TempFile::writeAssembly(const Module &M) const {
135 DEBUG(dbgs() << " - write assembly\n");
136 std::string ErrorInfo;
137 raw_fd_ostream OS(Filename.c_str(), ErrorInfo, sys::fs::F_Text);
138 if (!ErrorInfo.empty()) {
139 DEBUG(dbgs() << "error: " << ErrorInfo << "\n");
147 std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
148 DEBUG(dbgs() << " - read bitcode\n");
149 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOr =
150 MemoryBuffer::getFile(Filename);
152 DEBUG(dbgs() << "error: " << BufferOr.getError().message() << "\n");
156 MemoryBuffer *Buffer = BufferOr.get().get();
157 ErrorOr<Module *> ModuleOr = parseBitcodeFile(Buffer, Context);
159 DEBUG(dbgs() << "error: " << ModuleOr.getError().message() << "\n");
162 return std::unique_ptr<Module>(ModuleOr.get());
165 std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
166 DEBUG(dbgs() << " - read assembly\n");
168 std::unique_ptr<Module> M(ParseAssemblyFile(Filename, Err, Context));
170 DEBUG(dbgs() << "error: "; Err.print("verify-use-list-order", dbgs()));
174 ValueMapping::ValueMapping(const Module &M) {
175 // Every value should be mapped, including things like void instructions and
176 // basic blocks that are kept out of the ValueEnumerator.
178 // The current mapping order makes it easier to debug the tables. It happens
179 // to be similar to the ID mapping when writing ValueEnumerator, but they
180 // aren't (and needn't be) in sync.
183 for (const GlobalVariable &G : M.globals())
185 for (const GlobalAlias &A : M.aliases())
187 for (const Function &F : M)
190 // Constants used by globals.
191 for (const GlobalVariable &G : M.globals())
192 if (G.hasInitializer())
193 map(G.getInitializer());
194 for (const GlobalAlias &A : M.aliases())
196 for (const Function &F : M)
197 if (F.hasPrefixData())
198 map(F.getPrefixData());
201 for (const Function &F : M) {
202 for (const Argument &A : F.args())
204 for (const BasicBlock &BB : F)
206 for (const BasicBlock &BB : F)
207 for (const Instruction &I : BB)
210 // Constants used by instructions.
211 for (const BasicBlock &BB : F)
212 for (const Instruction &I : BB)
213 for (const Value *Op : I.operands())
214 if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
220 void ValueMapping::map(const Value *V) {
224 if (auto *C = dyn_cast<Constant>(V))
225 if (!isa<GlobalValue>(C))
226 for (const Value *Op : C->operands())
230 IDs[V] = Values.size();
234 static void dumpMapping(const ValueMapping &VM) {
235 dbgs() << "value-mapping (size = " << VM.Values.size() << "):\n";
236 for (unsigned I = 0, E = VM.Values.size(); I != E; ++I) {
237 dbgs() << " - id = " << I << ", value = ";
238 VM.Values[I]->dump();
242 static void debugValue(const ValueMapping &M, unsigned I, StringRef Desc) {
243 const Value *V = M.Values[I];
244 dbgs() << " - " << Desc << " value = ";
246 for (const Use &U : V->uses()) {
247 dbgs() << " => use: op = " << U.getOperandNo()
248 << ", user-id = " << M.IDs.lookup(U.getUser()) << ", user = ";
253 static void debugUserMismatch(const ValueMapping &L, const ValueMapping &R,
255 dbgs() << " - fail: user mismatch: ID = " << I << "\n";
256 debugValue(L, I, "LHS");
257 debugValue(R, I, "RHS");
265 static void debugSizeMismatch(const ValueMapping &L, const ValueMapping &R) {
266 dbgs() << " - fail: map size: " << L.Values.size()
267 << " != " << R.Values.size() << "\n";
275 static bool matches(const ValueMapping &LM, const ValueMapping &RM) {
276 DEBUG(dbgs() << "compare value maps\n");
277 if (LM.Values.size() != RM.Values.size()) {
278 DEBUG(debugSizeMismatch(LM, RM));
282 // This mapping doesn't include dangling constant users, since those don't
283 // get serialized. However, checking if users are constant and calling
284 // isConstantUsed() on every one is very expensive. Instead, just check if
285 // the user is mapped.
286 auto skipUnmappedUsers =
287 [&](Value::const_use_iterator &U, Value::const_use_iterator E,
288 const ValueMapping &M) {
289 while (U != E && !M.lookup(U->getUser()))
293 // Iterate through all values, and check that both mappings have the same
295 for (unsigned I = 0, E = LM.Values.size(); I != E; ++I) {
296 const Value *L = LM.Values[I];
297 const Value *R = RM.Values[I];
298 auto LU = L->use_begin(), LE = L->use_end();
299 auto RU = R->use_begin(), RE = R->use_end();
300 skipUnmappedUsers(LU, LE, LM);
301 skipUnmappedUsers(RU, RE, RM);
305 DEBUG(debugUserMismatch(LM, RM, I));
308 if (LM.lookup(LU->getUser()) != RM.lookup(RU->getUser())) {
309 DEBUG(debugUserMismatch(LM, RM, I));
312 if (LU->getOperandNo() != RU->getOperandNo()) {
313 DEBUG(debugUserMismatch(LM, RM, I));
316 skipUnmappedUsers(++LU, LE, LM);
317 skipUnmappedUsers(++RU, RE, RM);
320 DEBUG(debugUserMismatch(LM, RM, I));
328 static bool verifyBitcodeUseListOrder(const Module &M) {
329 DEBUG(dbgs() << "*** verify-use-list-order: bitcode ***\n");
334 if (F.writeBitcode(M))
338 std::unique_ptr<Module> OtherM = F.readBitcode(Context);
342 return matches(ValueMapping(M), ValueMapping(*OtherM));
345 static bool verifyAssemblyUseListOrder(const Module &M) {
346 DEBUG(dbgs() << "*** verify-use-list-order: assembly ***\n");
351 if (F.writeAssembly(M))
355 std::unique_ptr<Module> OtherM = F.readAssembly(Context);
359 return matches(ValueMapping(M), ValueMapping(*OtherM));
362 static void verifyUseListOrder(const Module &M) {
363 if (!verifyBitcodeUseListOrder(M))
364 report_fatal_error("bitcode use-list order changed");
366 if (shouldPreserveAssemblyUseListOrder())
367 if (!verifyAssemblyUseListOrder(M))
368 report_fatal_error("assembly use-list order changed");
371 int main(int argc, char **argv) {
372 sys::PrintStackTraceOnErrorSignal();
373 llvm::PrettyStackTraceProgram X(argc, argv);
375 // Enable debug stream buffering.
376 EnableDebugBuffering = true;
378 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
379 LLVMContext &Context = getGlobalContext();
381 cl::ParseCommandLineOptions(argc, argv,
382 "llvm tool to verify use-list order\n");
386 // Load the input module...
387 std::unique_ptr<Module> M;
388 M.reset(ParseIRFile(InputFilename, Err, Context));
391 Err.print(argv[0], errs());
395 DEBUG(dbgs() << "*** verify-use-list-order ***\n");
396 if (!shouldPreserveBitcodeUseListOrder()) {
397 // Can't verify if order isn't preserved.
398 DEBUG(dbgs() << "warning: cannot verify bitcode; "
399 "try -preserve-bc-use-list-order\n");
403 for (unsigned I = 0, E = NumShuffles; I != E; ++I) {
404 DEBUG(dbgs() << "*** iteration: " << I << " ***\n");
406 // Shuffle with a different seed each time so that use-lists that aren't
407 // modified the first time are likely to be modified the next time.
408 shuffleUseLists(*M, I);
409 verifyUseListOrder(*M);