mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-13 04:30:23 +00:00
verify-uselistorder: Move shuffleUseLists() out of lib/IR
`shuffleUseLists()` is only used in `verify-uselistorder`, so move it there to avoid bloating other executables. As a drive-by, update some of the header docs. This is part of PR5680. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214592 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
65225b64bb
commit
b2b0ad4c75
@ -1,4 +1,4 @@
|
||||
//===- llvm/IR/UseListOrder.h - LLVM Use List Order functions ---*- C++ -*-===//
|
||||
//===- llvm/IR/UseListOrder.h - LLVM Use List Order -------------*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
@ -7,8 +7,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file has functions to modify the use-list order and to verify that it
|
||||
// doesn't change after serialization.
|
||||
// This file has structures and command-line options for preserving use-list
|
||||
// order.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@ -111,11 +111,6 @@ typedef std::vector<UseListOrder> UseListOrderStack;
|
||||
bool shouldPreserveBitcodeUseListOrder();
|
||||
bool shouldPreserveAssemblyUseListOrder();
|
||||
|
||||
/// \brief Shuffle all use-lists in a module.
|
||||
///
|
||||
/// Adds \c SeedOffset to the default seed for the random number generator.
|
||||
void shuffleUseLists(Module &M, unsigned SeedOffset = 0);
|
||||
|
||||
} // end namespace llvm
|
||||
|
||||
#endif
|
||||
|
@ -1,4 +1,4 @@
|
||||
//===- UseListOrder.cpp - Implement Use List Order functions --------------===//
|
||||
//===- UseListOrder.cpp - Implement Use List Order ------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
@ -7,22 +7,12 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Implement use list order functions to modify use-list order and verify it
|
||||
// doesn't change after serialization.
|
||||
// Implement structures and command-line options for preserving use-list order.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/IR/UseListOrder.h"
|
||||
|
||||
#include "llvm/ADT/DenseSet.h"
|
||||
#include "llvm/IR/Module.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/Debug.h"
|
||||
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
#define DEBUG_TYPE "use-list-order"
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
@ -43,95 +33,3 @@ bool llvm::shouldPreserveBitcodeUseListOrder() {
|
||||
bool llvm::shouldPreserveAssemblyUseListOrder() {
|
||||
return PreserveAssemblyUseListOrder;
|
||||
}
|
||||
|
||||
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.
|
||||
DEBUG(dbgs() << "V = "; V->dump());
|
||||
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;
|
||||
DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo()
|
||||
<< ", U = ";
|
||||
U.getUser()->dump());
|
||||
}
|
||||
} while (std::is_sorted(V->use_begin(), V->use_end(), compareUses));
|
||||
|
||||
DEBUG(dbgs() << " => shuffle\n");
|
||||
V->sortUseList(compareUses);
|
||||
|
||||
DEBUG({
|
||||
for (const Use &U : V->uses()) {
|
||||
dbgs() << " - order: " << Order.lookup(&U)
|
||||
<< ", op = " << U.getOperandNo() << ", U = ";
|
||||
U.getUser()->dump();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void llvm::shuffleUseLists(Module &M, unsigned SeedOffset) {
|
||||
DEBUG(dbgs() << "*** shuffle-use-lists ***\n");
|
||||
std::minstd_rand0 Gen(std::minstd_rand0::default_seed + SeedOffset);
|
||||
DenseSet<Value *> Seen;
|
||||
|
||||
// Shuffle the use-list of each value that would be serialized to an IR file
|
||||
// (bitcode or assembly).
|
||||
auto shuffle = [&](Value *V) { shuffleValueUseLists(V, Gen, Seen); };
|
||||
|
||||
// Globals.
|
||||
for (GlobalVariable &G : M.globals())
|
||||
shuffle(&G);
|
||||
for (GlobalAlias &A : M.aliases())
|
||||
shuffle(&A);
|
||||
for (Function &F : M)
|
||||
shuffle(&F);
|
||||
|
||||
// Constants used by globals.
|
||||
for (GlobalVariable &G : M.globals())
|
||||
if (G.hasInitializer())
|
||||
shuffle(G.getInitializer());
|
||||
for (GlobalAlias &A : M.aliases())
|
||||
shuffle(A.getAliasee());
|
||||
for (Function &F : M)
|
||||
if (F.hasPrefixData())
|
||||
shuffle(F.getPrefixData());
|
||||
|
||||
// Function bodies.
|
||||
for (Function &F : M) {
|
||||
for (Argument &A : F.args())
|
||||
shuffle(&A);
|
||||
for (BasicBlock &BB : F)
|
||||
shuffle(&BB);
|
||||
for (BasicBlock &BB : F)
|
||||
for (Instruction &I : BB)
|
||||
shuffle(&I);
|
||||
|
||||
// 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))
|
||||
shuffle(Op);
|
||||
}
|
||||
|
||||
DEBUG(dbgs() << "\n");
|
||||
}
|
||||
|
@ -11,8 +11,8 @@
|
||||
// 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.
|
||||
//
|
||||
// The shuffles are deterministic and somewhat naive. On a given shuffle, some
|
||||
// use-lists will not change at all. The algorithm per iteration is as follows:
|
||||
// The shuffles are deterministic, but guarantee that use-lists will change.
|
||||
// The algorithm per iteration is as follows:
|
||||
//
|
||||
// 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.
|
||||
@ -21,14 +21,14 @@
|
||||
//
|
||||
// 3. Assign a random number to each Use in the Value's use-list in order.
|
||||
//
|
||||
// 4. Sort the use-list using Value::sortUseList(), which is a stable sort.
|
||||
// 4. If the numbers are already in order, reassign numbers until they aren't.
|
||||
//
|
||||
// Shuffling a larger number of times provides a better statistical guarantee
|
||||
// that each use-list has changed at least once.
|
||||
// 5. Sort the use-list using Value::sortUseList(), which is a stable sort.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/ADT/DenseMap.h"
|
||||
#include "llvm/ADT/DenseSet.h"
|
||||
#include "llvm/AsmParser/Parser.h"
|
||||
#include "llvm/Bitcode/ReaderWriter.h"
|
||||
#include "llvm/IR/LLVMContext.h"
|
||||
@ -46,6 +46,8 @@
|
||||
#include "llvm/Support/Signals.h"
|
||||
#include "llvm/Support/SourceMgr.h"
|
||||
#include "llvm/Support/SystemUtils.h"
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
@ -368,6 +370,99 @@ static void verifyUseListOrder(const Module &M) {
|
||||
report_fatal_error("assembly use-list order changed");
|
||||
}
|
||||
|
||||
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.
|
||||
DEBUG(dbgs() << "V = "; V->dump());
|
||||
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;
|
||||
DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo()
|
||||
<< ", U = ";
|
||||
U.getUser()->dump());
|
||||
}
|
||||
} while (std::is_sorted(V->use_begin(), V->use_end(), compareUses));
|
||||
|
||||
DEBUG(dbgs() << " => shuffle\n");
|
||||
V->sortUseList(compareUses);
|
||||
|
||||
DEBUG({
|
||||
for (const Use &U : V->uses()) {
|
||||
dbgs() << " - order: " << Order.lookup(&U)
|
||||
<< ", op = " << U.getOperandNo() << ", U = ";
|
||||
U.getUser()->dump();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Shuffle all use-lists in a module.
|
||||
void shuffleUseLists(Module &M, unsigned SeedOffset) {
|
||||
DEBUG(dbgs() << "*** shuffle-use-lists ***\n");
|
||||
std::minstd_rand0 Gen(std::minstd_rand0::default_seed + SeedOffset);
|
||||
DenseSet<Value *> Seen;
|
||||
|
||||
// Shuffle the use-list of each value that would be serialized to an IR file
|
||||
// (bitcode or assembly).
|
||||
auto shuffle = [&](Value *V) { shuffleValueUseLists(V, Gen, Seen); };
|
||||
|
||||
// Globals.
|
||||
for (GlobalVariable &G : M.globals())
|
||||
shuffle(&G);
|
||||
for (GlobalAlias &A : M.aliases())
|
||||
shuffle(&A);
|
||||
for (Function &F : M)
|
||||
shuffle(&F);
|
||||
|
||||
// Constants used by globals.
|
||||
for (GlobalVariable &G : M.globals())
|
||||
if (G.hasInitializer())
|
||||
shuffle(G.getInitializer());
|
||||
for (GlobalAlias &A : M.aliases())
|
||||
shuffle(A.getAliasee());
|
||||
for (Function &F : M)
|
||||
if (F.hasPrefixData())
|
||||
shuffle(F.getPrefixData());
|
||||
|
||||
// Function bodies.
|
||||
for (Function &F : M) {
|
||||
for (Argument &A : F.args())
|
||||
shuffle(&A);
|
||||
for (BasicBlock &BB : F)
|
||||
shuffle(&BB);
|
||||
for (BasicBlock &BB : F)
|
||||
for (Instruction &I : BB)
|
||||
shuffle(&I);
|
||||
|
||||
// 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))
|
||||
shuffle(Op);
|
||||
}
|
||||
|
||||
DEBUG(dbgs() << "\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
sys::PrintStackTraceOnErrorSignal();
|
||||
llvm::PrettyStackTraceProgram X(argc, argv);
|
||||
|
Loading…
Reference in New Issue
Block a user