2014-07-31 01:11:27 +08:00
|
|
|
//===- verify-uselistorder.cpp - The LLVM Modular Optimizer ---------------===//
|
2014-07-25 22:49:26 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-07-25 22:49:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-07-31 00:59:19 +08:00
|
|
|
// Verify that use-list order can be serialized correctly. After reading the
|
|
|
|
// provided IR, this tool shuffles the use-lists and then writes and reads to a
|
|
|
|
// separate Module whose use-list orders are compared to the original.
|
2014-07-25 22:49:26 +08:00
|
|
|
//
|
2014-08-02 07:03:36 +08:00
|
|
|
// The shuffles are deterministic, but guarantee that use-lists will change.
|
|
|
|
// The algorithm per iteration is as follows:
|
2014-08-01 02:46:24 +08:00
|
|
|
//
|
|
|
|
// 1. Seed the random number generator. The seed is different for each
|
|
|
|
// shuffle. Shuffle 0 uses default+0, shuffle 1 uses default+1, and so on.
|
|
|
|
//
|
|
|
|
// 2. Visit every Value in a deterministic order.
|
|
|
|
//
|
|
|
|
// 3. Assign a random number to each Use in the Value's use-list in order.
|
|
|
|
//
|
2014-08-02 07:03:36 +08:00
|
|
|
// 4. If the numbers are already in order, reassign numbers until they aren't.
|
2014-08-01 02:46:24 +08:00
|
|
|
//
|
2014-08-02 07:03:36 +08:00
|
|
|
// 5. Sort the use-list using Value::sortUseList(), which is a stable sort.
|
2014-08-01 02:46:24 +08:00
|
|
|
//
|
2014-07-25 22:49:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-07-25 23:41:49 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2014-08-02 07:03:36 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2014-07-25 23:41:49 +08:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2014-07-25 23:41:49 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2014-07-25 22:49:26 +08:00
|
|
|
#include "llvm/IR/UseListOrder.h"
|
2014-08-19 07:44:14 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2014-07-26 01:13:03 +08:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-07-25 22:49:26 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-07-25 23:41:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2018-04-14 02:26:06 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2014-07-25 23:41:49 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2014-07-26 01:13:03 +08:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
2015-03-24 02:07:13 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-08-02 07:03:36 +08:00
|
|
|
#include <random>
|
|
|
|
#include <vector>
|
2014-07-25 22:49:26 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-04-15 02:19:27 +08:00
|
|
|
#define DEBUG_TYPE "uselistorder"
|
2014-07-25 22:49:26 +08:00
|
|
|
|
2014-07-26 01:13:03 +08:00
|
|
|
static cl::opt<std::string> InputFilename(cl::Positional,
|
|
|
|
cl::desc("<input bitcode file>"),
|
|
|
|
cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
2014-07-29 05:12:19 +08:00
|
|
|
static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temp files"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2014-07-29 07:25:21 +08:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
NumShuffles("num-shuffles",
|
|
|
|
cl::desc("Number of times to shuffle and verify use-lists"),
|
2014-08-02 07:49:41 +08:00
|
|
|
cl::init(1));
|
2014-07-29 07:25:21 +08:00
|
|
|
|
2014-07-25 23:41:49 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct TempFile {
|
|
|
|
std::string Filename;
|
|
|
|
FileRemover Remover;
|
|
|
|
bool init(const std::string &Ext);
|
|
|
|
bool writeBitcode(const Module &M) const;
|
|
|
|
bool writeAssembly(const Module &M) const;
|
|
|
|
std::unique_ptr<Module> readBitcode(LLVMContext &Context) const;
|
|
|
|
std::unique_ptr<Module> readAssembly(LLVMContext &Context) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ValueMapping {
|
|
|
|
DenseMap<const Value *, unsigned> IDs;
|
|
|
|
std::vector<const Value *> Values;
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Construct a value mapping for module.
|
2014-07-25 23:41:49 +08:00
|
|
|
///
|
|
|
|
/// Creates mapping from every value in \c M to an ID. This mapping includes
|
|
|
|
/// un-referencable values.
|
|
|
|
///
|
|
|
|
/// Every \a Value that gets serialized in some way should be represented
|
|
|
|
/// here. The order needs to be deterministic, but it's unnecessary to match
|
|
|
|
/// the value-ids in the bitcode writer.
|
|
|
|
///
|
|
|
|
/// All constants that are referenced by other values are included in the
|
|
|
|
/// mapping, but others -- which wouldn't be serialized -- are not.
|
|
|
|
ValueMapping(const Module &M);
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Map a value.
|
2014-07-25 23:41:49 +08:00
|
|
|
///
|
|
|
|
/// Maps a value. If it's a constant, maps all of its operands first.
|
|
|
|
void map(const Value *V);
|
|
|
|
unsigned lookup(const Value *V) const { return IDs.lookup(V); }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
bool TempFile::init(const std::string &Ext) {
|
|
|
|
SmallVector<char, 64> Vector;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " - create-temp-file\n");
|
2015-04-15 02:19:27 +08:00
|
|
|
if (auto EC = sys::fs::createTemporaryFile("uselistorder", Ext, Vector)) {
|
2015-04-15 03:04:03 +08:00
|
|
|
errs() << "verify-uselistorder: error: " << EC.message() << "\n";
|
2014-07-25 23:41:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
assert(!Vector.empty());
|
|
|
|
|
|
|
|
Filename.assign(Vector.data(), Vector.data() + Vector.size());
|
2014-07-29 05:12:19 +08:00
|
|
|
Remover.setFile(Filename, !SaveTemps);
|
2015-04-15 03:04:03 +08:00
|
|
|
if (SaveTemps)
|
|
|
|
outs() << " - filename = " << Filename << "\n";
|
2014-07-25 23:41:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TempFile::writeBitcode(const Module &M) const {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " - write bitcode\n");
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Filename, EC, sys::fs::F_None);
|
|
|
|
if (EC) {
|
2015-04-15 03:04:03 +08:00
|
|
|
errs() << "verify-uselistorder: error: " << EC.message() << "\n";
|
2014-07-25 23:41:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-15 03:11:32 +08:00
|
|
|
WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
|
2014-07-25 23:41:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TempFile::writeAssembly(const Module &M) const {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " - write assembly\n");
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Filename, EC, sys::fs::F_Text);
|
|
|
|
if (EC) {
|
2015-04-15 03:04:03 +08:00
|
|
|
errs() << "verify-uselistorder: error: " << EC.message() << "\n";
|
2014-07-25 23:41:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-15 10:12:41 +08:00
|
|
|
M.print(OS, nullptr, /* ShouldPreserveUseListOrder */ true);
|
2014-07-25 23:41:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " - read bitcode\n");
|
2014-07-25 23:41:49 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOr =
|
|
|
|
MemoryBuffer::getFile(Filename);
|
|
|
|
if (!BufferOr) {
|
2015-04-15 03:04:03 +08:00
|
|
|
errs() << "verify-uselistorder: error: " << BufferOr.getError().message()
|
|
|
|
<< "\n";
|
2014-07-25 23:41:49 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-01 22:31:55 +08:00
|
|
|
MemoryBuffer *Buffer = BufferOr.get().get();
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<Module>> ModuleOr =
|
2014-08-27 05:49:01 +08:00
|
|
|
parseBitcodeFile(Buffer->getMemBufferRef(), Context);
|
2014-07-25 23:41:49 +08:00
|
|
|
if (!ModuleOr) {
|
2016-11-13 15:00:17 +08:00
|
|
|
logAllUnhandledErrors(ModuleOr.takeError(), errs(),
|
|
|
|
"verify-uselistorder: error: ");
|
2014-07-25 23:41:49 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-17 06:27:55 +08:00
|
|
|
return std::move(ModuleOr.get());
|
2014-07-25 23:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " - read assembly\n");
|
2014-07-25 23:41:49 +08:00
|
|
|
SMDiagnostic Err;
|
2014-08-20 00:58:54 +08:00
|
|
|
std::unique_ptr<Module> M = parseAssemblyFile(Filename, Err, Context);
|
2014-07-25 23:41:49 +08:00
|
|
|
if (!M.get())
|
2015-04-15 03:04:03 +08:00
|
|
|
Err.print("verify-uselistorder", errs());
|
2014-07-25 23:41:49 +08:00
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueMapping::ValueMapping(const Module &M) {
|
|
|
|
// Every value should be mapped, including things like void instructions and
|
|
|
|
// basic blocks that are kept out of the ValueEnumerator.
|
|
|
|
//
|
|
|
|
// The current mapping order makes it easier to debug the tables. It happens
|
|
|
|
// to be similar to the ID mapping when writing ValueEnumerator, but they
|
|
|
|
// aren't (and needn't be) in sync.
|
|
|
|
|
|
|
|
// Globals.
|
|
|
|
for (const GlobalVariable &G : M.globals())
|
|
|
|
map(&G);
|
|
|
|
for (const GlobalAlias &A : M.aliases())
|
|
|
|
map(&A);
|
2016-05-04 08:20:48 +08:00
|
|
|
for (const GlobalIFunc &IF : M.ifuncs())
|
|
|
|
map(&IF);
|
2014-07-25 23:41:49 +08:00
|
|
|
for (const Function &F : M)
|
|
|
|
map(&F);
|
|
|
|
|
|
|
|
// Constants used by globals.
|
|
|
|
for (const GlobalVariable &G : M.globals())
|
|
|
|
if (G.hasInitializer())
|
|
|
|
map(G.getInitializer());
|
|
|
|
for (const GlobalAlias &A : M.aliases())
|
|
|
|
map(A.getAliasee());
|
2016-05-04 08:20:48 +08:00
|
|
|
for (const GlobalIFunc &IF : M.ifuncs())
|
|
|
|
map(IF.getResolver());
|
2014-12-03 10:08:38 +08:00
|
|
|
for (const Function &F : M) {
|
2014-07-25 23:41:49 +08:00
|
|
|
if (F.hasPrefixData())
|
|
|
|
map(F.getPrefixData());
|
2014-12-03 10:08:38 +08:00
|
|
|
if (F.hasPrologueData())
|
|
|
|
map(F.getPrologueData());
|
2015-06-18 04:52:32 +08:00
|
|
|
if (F.hasPersonalityFn())
|
|
|
|
map(F.getPersonalityFn());
|
2014-12-03 10:08:38 +08:00
|
|
|
}
|
2014-07-25 23:41:49 +08:00
|
|
|
|
|
|
|
// Function bodies.
|
|
|
|
for (const Function &F : M) {
|
|
|
|
for (const Argument &A : F.args())
|
|
|
|
map(&A);
|
|
|
|
for (const BasicBlock &BB : F)
|
|
|
|
map(&BB);
|
|
|
|
for (const BasicBlock &BB : F)
|
|
|
|
for (const Instruction &I : BB)
|
|
|
|
map(&I);
|
|
|
|
|
|
|
|
// Constants used by instructions.
|
|
|
|
for (const BasicBlock &BB : F)
|
|
|
|
for (const Instruction &I : BB)
|
|
|
|
for (const Value *Op : I.operands())
|
|
|
|
if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
|
|
|
|
isa<InlineAsm>(Op))
|
|
|
|
map(Op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValueMapping::map(const Value *V) {
|
|
|
|
if (IDs.lookup(V))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (auto *C = dyn_cast<Constant>(V))
|
|
|
|
if (!isa<GlobalValue>(C))
|
|
|
|
for (const Value *Op : C->operands())
|
|
|
|
map(Op);
|
|
|
|
|
|
|
|
Values.push_back(V);
|
|
|
|
IDs[V] = Values.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
static void dumpMapping(const ValueMapping &VM) {
|
|
|
|
dbgs() << "value-mapping (size = " << VM.Values.size() << "):\n";
|
|
|
|
for (unsigned I = 0, E = VM.Values.size(); I != E; ++I) {
|
|
|
|
dbgs() << " - id = " << I << ", value = ";
|
|
|
|
VM.Values[I]->dump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void debugValue(const ValueMapping &M, unsigned I, StringRef Desc) {
|
|
|
|
const Value *V = M.Values[I];
|
|
|
|
dbgs() << " - " << Desc << " value = ";
|
|
|
|
V->dump();
|
|
|
|
for (const Use &U : V->uses()) {
|
|
|
|
dbgs() << " => use: op = " << U.getOperandNo()
|
|
|
|
<< ", user-id = " << M.IDs.lookup(U.getUser()) << ", user = ";
|
|
|
|
U.getUser()->dump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void debugUserMismatch(const ValueMapping &L, const ValueMapping &R,
|
|
|
|
unsigned I) {
|
|
|
|
dbgs() << " - fail: user mismatch: ID = " << I << "\n";
|
|
|
|
debugValue(L, I, "LHS");
|
|
|
|
debugValue(R, I, "RHS");
|
|
|
|
|
|
|
|
dbgs() << "\nlhs-";
|
|
|
|
dumpMapping(L);
|
|
|
|
dbgs() << "\nrhs-";
|
|
|
|
dumpMapping(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void debugSizeMismatch(const ValueMapping &L, const ValueMapping &R) {
|
|
|
|
dbgs() << " - fail: map size: " << L.Values.size()
|
|
|
|
<< " != " << R.Values.size() << "\n";
|
|
|
|
dbgs() << "\nlhs-";
|
|
|
|
dumpMapping(L);
|
|
|
|
dbgs() << "\nrhs-";
|
|
|
|
dumpMapping(R);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool matches(const ValueMapping &LM, const ValueMapping &RM) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "compare value maps\n");
|
2014-07-25 23:41:49 +08:00
|
|
|
if (LM.Values.size() != RM.Values.size()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(debugSizeMismatch(LM, RM));
|
2014-07-25 23:41:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This mapping doesn't include dangling constant users, since those don't
|
|
|
|
// get serialized. However, checking if users are constant and calling
|
|
|
|
// isConstantUsed() on every one is very expensive. Instead, just check if
|
|
|
|
// the user is mapped.
|
|
|
|
auto skipUnmappedUsers =
|
|
|
|
[&](Value::const_use_iterator &U, Value::const_use_iterator E,
|
|
|
|
const ValueMapping &M) {
|
|
|
|
while (U != E && !M.lookup(U->getUser()))
|
|
|
|
++U;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Iterate through all values, and check that both mappings have the same
|
|
|
|
// users.
|
|
|
|
for (unsigned I = 0, E = LM.Values.size(); I != E; ++I) {
|
|
|
|
const Value *L = LM.Values[I];
|
|
|
|
const Value *R = RM.Values[I];
|
|
|
|
auto LU = L->use_begin(), LE = L->use_end();
|
|
|
|
auto RU = R->use_begin(), RE = R->use_end();
|
|
|
|
skipUnmappedUsers(LU, LE, LM);
|
|
|
|
skipUnmappedUsers(RU, RE, RM);
|
|
|
|
|
|
|
|
while (LU != LE) {
|
|
|
|
if (RU == RE) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(debugUserMismatch(LM, RM, I));
|
2014-07-25 23:41:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (LM.lookup(LU->getUser()) != RM.lookup(RU->getUser())) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(debugUserMismatch(LM, RM, I));
|
2014-07-25 23:41:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (LU->getOperandNo() != RU->getOperandNo()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(debugUserMismatch(LM, RM, I));
|
2014-07-25 23:41:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
skipUnmappedUsers(++LU, LE, LM);
|
|
|
|
skipUnmappedUsers(++RU, RE, RM);
|
|
|
|
}
|
|
|
|
if (RU != RE) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(debugUserMismatch(LM, RM, I));
|
2014-07-25 23:41:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:44:14 +08:00
|
|
|
static void verifyAfterRoundTrip(const Module &M,
|
|
|
|
std::unique_ptr<Module> OtherM) {
|
|
|
|
if (!OtherM)
|
|
|
|
report_fatal_error("parsing failed");
|
|
|
|
if (verifyModule(*OtherM, &errs()))
|
|
|
|
report_fatal_error("verification failed");
|
|
|
|
if (!matches(ValueMapping(M), ValueMapping(*OtherM)))
|
|
|
|
report_fatal_error("use-list order changed");
|
|
|
|
}
|
2015-10-14 05:51:02 +08:00
|
|
|
|
2014-08-19 07:44:14 +08:00
|
|
|
static void verifyBitcodeUseListOrder(const Module &M) {
|
2014-07-25 23:41:49 +08:00
|
|
|
TempFile F;
|
|
|
|
if (F.init("bc"))
|
2014-08-19 07:44:14 +08:00
|
|
|
report_fatal_error("failed to initialize bitcode file");
|
2014-07-25 23:41:49 +08:00
|
|
|
|
|
|
|
if (F.writeBitcode(M))
|
2014-08-19 07:44:14 +08:00
|
|
|
report_fatal_error("failed to write bitcode");
|
2014-07-25 23:41:49 +08:00
|
|
|
|
|
|
|
LLVMContext Context;
|
2014-08-19 07:44:14 +08:00
|
|
|
verifyAfterRoundTrip(M, F.readBitcode(Context));
|
2014-07-25 23:41:49 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:44:14 +08:00
|
|
|
static void verifyAssemblyUseListOrder(const Module &M) {
|
2014-07-25 23:41:49 +08:00
|
|
|
TempFile F;
|
|
|
|
if (F.init("ll"))
|
2014-08-19 07:44:14 +08:00
|
|
|
report_fatal_error("failed to initialize assembly file");
|
2014-07-25 23:41:49 +08:00
|
|
|
|
|
|
|
if (F.writeAssembly(M))
|
2014-08-19 07:44:14 +08:00
|
|
|
report_fatal_error("failed to write assembly");
|
2014-07-25 23:41:49 +08:00
|
|
|
|
|
|
|
LLVMContext Context;
|
2014-08-19 07:44:14 +08:00
|
|
|
verifyAfterRoundTrip(M, F.readAssembly(Context));
|
2014-07-25 23:41:49 +08:00
|
|
|
}
|
|
|
|
|
2014-08-02 06:52:06 +08:00
|
|
|
static void verifyUseListOrder(const Module &M) {
|
2015-04-15 03:04:03 +08:00
|
|
|
outs() << "verify bitcode\n";
|
2014-08-19 07:44:14 +08:00
|
|
|
verifyBitcodeUseListOrder(M);
|
2015-04-15 03:04:03 +08:00
|
|
|
outs() << "verify assembly\n";
|
2014-08-20 05:30:15 +08:00
|
|
|
verifyAssemblyUseListOrder(M);
|
2014-08-02 06:52:06 +08:00
|
|
|
}
|
|
|
|
|
2014-08-02 07:03:36 +08:00
|
|
|
static void shuffleValueUseLists(Value *V, std::minstd_rand0 &Gen,
|
|
|
|
DenseSet<Value *> &Seen) {
|
|
|
|
if (!Seen.insert(V).second)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (auto *C = dyn_cast<Constant>(V))
|
|
|
|
if (!isa<GlobalValue>(C))
|
|
|
|
for (Value *Op : C->operands())
|
|
|
|
shuffleValueUseLists(Op, Gen, Seen);
|
|
|
|
|
|
|
|
if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
|
|
|
|
// Nothing to shuffle for 0 or 1 users.
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Generate random numbers between 10 and 99, which will line up nicely in
|
|
|
|
// debug output. We're not worried about collisons here.
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "V = "; V->dump());
|
2014-08-02 07:03:36 +08:00
|
|
|
std::uniform_int_distribution<short> Dist(10, 99);
|
|
|
|
SmallDenseMap<const Use *, short, 16> Order;
|
|
|
|
auto compareUses =
|
|
|
|
[&Order](const Use &L, const Use &R) { return Order[&L] < Order[&R]; };
|
|
|
|
do {
|
|
|
|
for (const Use &U : V->uses()) {
|
|
|
|
auto I = Dist(Gen);
|
|
|
|
Order[&U] = I;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo()
|
|
|
|
<< ", U = ";
|
|
|
|
U.getUser()->dump());
|
2014-08-02 07:03:36 +08:00
|
|
|
}
|
|
|
|
} while (std::is_sorted(V->use_begin(), V->use_end(), compareUses));
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " => shuffle\n");
|
2014-08-02 07:03:36 +08:00
|
|
|
V->sortUseList(compareUses);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2014-08-02 07:03:36 +08:00
|
|
|
for (const Use &U : V->uses()) {
|
|
|
|
dbgs() << " - order: " << Order.lookup(&U)
|
|
|
|
<< ", op = " << U.getOperandNo() << ", U = ";
|
|
|
|
U.getUser()->dump();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-08-02 07:49:41 +08:00
|
|
|
static void reverseValueUseLists(Value *V, DenseSet<Value *> &Seen) {
|
|
|
|
if (!Seen.insert(V).second)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (auto *C = dyn_cast<Constant>(V))
|
|
|
|
if (!isa<GlobalValue>(C))
|
|
|
|
for (Value *Op : C->operands())
|
|
|
|
reverseValueUseLists(Op, Seen);
|
|
|
|
|
|
|
|
if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
|
|
|
|
// Nothing to shuffle for 0 or 1 users.
|
|
|
|
return;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2014-08-02 07:49:41 +08:00
|
|
|
dbgs() << "V = ";
|
|
|
|
V->dump();
|
|
|
|
for (const Use &U : V->uses()) {
|
|
|
|
dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
|
|
|
|
U.getUser()->dump();
|
|
|
|
}
|
|
|
|
dbgs() << " => reverse\n";
|
|
|
|
});
|
2014-08-02 07:03:36 +08:00
|
|
|
|
2014-08-02 07:49:41 +08:00
|
|
|
V->reverseUseList();
|
2014-08-02 07:03:36 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2014-08-02 07:49:41 +08:00
|
|
|
for (const Use &U : V->uses()) {
|
|
|
|
dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
|
|
|
|
U.getUser()->dump();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Changer>
|
|
|
|
static void changeUseLists(Module &M, Changer changeValueUseList) {
|
|
|
|
// Visit every value that would be serialized to an IR file.
|
|
|
|
//
|
2014-08-02 07:03:36 +08:00
|
|
|
// Globals.
|
|
|
|
for (GlobalVariable &G : M.globals())
|
2014-08-02 07:49:41 +08:00
|
|
|
changeValueUseList(&G);
|
2014-08-02 07:03:36 +08:00
|
|
|
for (GlobalAlias &A : M.aliases())
|
2014-08-02 07:49:41 +08:00
|
|
|
changeValueUseList(&A);
|
2016-05-04 08:20:48 +08:00
|
|
|
for (GlobalIFunc &IF : M.ifuncs())
|
|
|
|
changeValueUseList(&IF);
|
2014-08-02 07:03:36 +08:00
|
|
|
for (Function &F : M)
|
2014-08-02 07:49:41 +08:00
|
|
|
changeValueUseList(&F);
|
2014-08-02 07:03:36 +08:00
|
|
|
|
|
|
|
// Constants used by globals.
|
|
|
|
for (GlobalVariable &G : M.globals())
|
|
|
|
if (G.hasInitializer())
|
2014-08-02 07:49:41 +08:00
|
|
|
changeValueUseList(G.getInitializer());
|
2014-08-02 07:03:36 +08:00
|
|
|
for (GlobalAlias &A : M.aliases())
|
2014-08-02 07:49:41 +08:00
|
|
|
changeValueUseList(A.getAliasee());
|
2016-05-04 08:20:48 +08:00
|
|
|
for (GlobalIFunc &IF : M.ifuncs())
|
|
|
|
changeValueUseList(IF.getResolver());
|
2014-12-03 10:08:38 +08:00
|
|
|
for (Function &F : M) {
|
2014-08-02 07:03:36 +08:00
|
|
|
if (F.hasPrefixData())
|
2014-08-02 07:49:41 +08:00
|
|
|
changeValueUseList(F.getPrefixData());
|
2014-12-03 10:08:38 +08:00
|
|
|
if (F.hasPrologueData())
|
|
|
|
changeValueUseList(F.getPrologueData());
|
2015-06-18 04:52:32 +08:00
|
|
|
if (F.hasPersonalityFn())
|
|
|
|
changeValueUseList(F.getPersonalityFn());
|
2014-12-03 10:08:38 +08:00
|
|
|
}
|
2014-08-02 07:03:36 +08:00
|
|
|
|
|
|
|
// Function bodies.
|
|
|
|
for (Function &F : M) {
|
|
|
|
for (Argument &A : F.args())
|
2014-08-02 07:49:41 +08:00
|
|
|
changeValueUseList(&A);
|
2014-08-02 07:03:36 +08:00
|
|
|
for (BasicBlock &BB : F)
|
2014-08-02 07:49:41 +08:00
|
|
|
changeValueUseList(&BB);
|
2014-08-02 07:03:36 +08:00
|
|
|
for (BasicBlock &BB : F)
|
|
|
|
for (Instruction &I : BB)
|
2014-08-02 07:49:41 +08:00
|
|
|
changeValueUseList(&I);
|
2014-08-02 07:03:36 +08:00
|
|
|
|
|
|
|
// Constants used by instructions.
|
|
|
|
for (BasicBlock &BB : F)
|
|
|
|
for (Instruction &I : BB)
|
|
|
|
for (Value *Op : I.operands())
|
|
|
|
if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
|
|
|
|
isa<InlineAsm>(Op))
|
2014-08-02 07:49:41 +08:00
|
|
|
changeValueUseList(Op);
|
2014-08-02 07:03:36 +08:00
|
|
|
}
|
2014-08-19 07:44:14 +08:00
|
|
|
|
|
|
|
if (verifyModule(M, &errs()))
|
|
|
|
report_fatal_error("verification failed");
|
2014-08-02 07:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void shuffleUseLists(Module &M, unsigned SeedOffset) {
|
|
|
|
std::minstd_rand0 Gen(std::minstd_rand0::default_seed + SeedOffset);
|
|
|
|
DenseSet<Value *> Seen;
|
|
|
|
changeUseLists(M, [&](Value *V) { shuffleValueUseLists(V, Gen, Seen); });
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n");
|
2014-08-02 07:49:41 +08:00
|
|
|
}
|
2014-08-02 07:03:36 +08:00
|
|
|
|
2014-08-02 07:49:41 +08:00
|
|
|
static void reverseUseLists(Module &M) {
|
|
|
|
DenseSet<Value *> Seen;
|
|
|
|
changeUseLists(M, [&](Value *V) { reverseValueUseLists(V, Seen); });
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n");
|
2014-08-02 07:03:36 +08:00
|
|
|
}
|
|
|
|
|
2014-07-26 01:13:03 +08:00
|
|
|
int main(int argc, char **argv) {
|
2018-04-14 02:26:06 +08:00
|
|
|
InitLLVM X(argc, argv);
|
2014-07-25 22:49:26 +08:00
|
|
|
|
2014-07-26 01:13:03 +08:00
|
|
|
// Enable debug stream buffering.
|
|
|
|
EnableDebugBuffering = true;
|
|
|
|
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext Context;
|
2014-07-26 01:13:03 +08:00
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
"llvm tool to verify use-list order\n");
|
|
|
|
|
|
|
|
SMDiagnostic Err;
|
|
|
|
|
|
|
|
// Load the input module...
|
2014-08-27 01:29:46 +08:00
|
|
|
std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
|
2014-07-26 01:13:03 +08:00
|
|
|
|
|
|
|
if (!M.get()) {
|
|
|
|
Err.print(argv[0], errs());
|
|
|
|
return 1;
|
|
|
|
}
|
2015-03-31 11:07:23 +08:00
|
|
|
if (verifyModule(*M, &errs())) {
|
|
|
|
errs() << argv[0] << ": " << InputFilename
|
|
|
|
<< ": error: input module is broken!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2014-07-25 22:49:26 +08:00
|
|
|
|
2014-08-02 07:49:41 +08:00
|
|
|
// Verify the use lists now and after reversing them.
|
2015-04-15 10:12:41 +08:00
|
|
|
outs() << "*** verify-uselistorder ***\n";
|
2014-08-02 07:49:41 +08:00
|
|
|
verifyUseListOrder(*M);
|
2015-04-15 03:04:03 +08:00
|
|
|
outs() << "reverse\n";
|
2014-08-02 07:49:41 +08:00
|
|
|
reverseUseLists(*M);
|
|
|
|
verifyUseListOrder(*M);
|
|
|
|
|
2014-07-29 07:25:21 +08:00
|
|
|
for (unsigned I = 0, E = NumShuffles; I != E; ++I) {
|
2015-04-15 03:04:03 +08:00
|
|
|
outs() << "\n";
|
2014-07-29 07:25:21 +08:00
|
|
|
|
2014-08-02 07:49:41 +08:00
|
|
|
// Shuffle with a different (deterministic) seed each time.
|
2015-04-15 03:04:03 +08:00
|
|
|
outs() << "shuffle (" << I + 1 << " of " << E << ")\n";
|
2014-07-29 07:25:21 +08:00
|
|
|
shuffleUseLists(*M, I);
|
2014-08-02 07:49:41 +08:00
|
|
|
|
|
|
|
// Verify again before and after reversing.
|
|
|
|
verifyUseListOrder(*M);
|
2015-04-15 03:04:03 +08:00
|
|
|
outs() << "reverse\n";
|
2014-08-02 07:49:41 +08:00
|
|
|
reverseUseLists(*M);
|
2014-08-02 06:52:06 +08:00
|
|
|
verifyUseListOrder(*M);
|
2014-07-29 07:25:21 +08:00
|
|
|
}
|
2014-07-25 23:41:49 +08:00
|
|
|
|
2014-07-26 01:13:03 +08:00
|
|
|
return 0;
|
2014-07-25 22:49:26 +08:00
|
|
|
}
|