2017-09-01 05:56:16 +08:00
|
|
|
//===- llvm-stress.cpp - Generate random LL files to stress-test LLVM -----===//
|
2012-02-26 16:35:53 +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
|
2012-02-26 16:35:53 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This program is a utility that generates random .ll files to stress-test
|
|
|
|
// different components in LLVM.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2014-03-06 13:51:42 +08:00
|
|
|
|
2017-09-01 05:56:16 +08:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
|
|
|
#include "llvm/ADT/APInt.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/CallingConv.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2017-09-01 05:56:16 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalValue.h"
|
2014-01-12 19:10:32 +08:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2017-09-01 05:56:16 +08:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instruction.h"
|
2017-09-01 05:56:16 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2014-01-12 19:10:32 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-04-18 17:17:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2017-09-01 05:56:16 +08:00
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2017-09-01 05:56:16 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-04-30 07:26:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-02-26 16:35:53 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2017-09-01 05:56:16 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include <algorithm>
|
2017-09-01 05:56:16 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
2012-02-26 16:35:53 +08:00
|
|
|
#include <vector>
|
2015-07-10 18:01:47 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2012-02-26 16:35:53 +08:00
|
|
|
|
|
|
|
static cl::opt<unsigned> SeedCL("seed",
|
|
|
|
cl::desc("Seed used for randomness"), cl::init(0));
|
2017-09-01 05:56:16 +08:00
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
static cl::opt<unsigned> SizeCL("size",
|
|
|
|
cl::desc("The estimated size of the generated function (# of instrs)"),
|
|
|
|
cl::init(100));
|
2017-09-01 05:56:16 +08:00
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
2016-04-15 05:59:01 +08:00
|
|
|
static LLVMContext Context;
|
|
|
|
|
2015-07-10 18:01:47 +08:00
|
|
|
namespace cl {
|
2017-09-01 05:56:16 +08:00
|
|
|
|
2015-07-10 18:01:47 +08:00
|
|
|
template <> class parser<Type*> final : public basic_parser<Type*> {
|
|
|
|
public:
|
|
|
|
parser(Option &O) : basic_parser(O) {}
|
|
|
|
|
|
|
|
// Parse options as IR types. Return true on error.
|
|
|
|
bool parse(Option &O, StringRef, StringRef Arg, Type *&Value) {
|
|
|
|
if (Arg == "half") Value = Type::getHalfTy(Context);
|
|
|
|
else if (Arg == "fp128") Value = Type::getFP128Ty(Context);
|
|
|
|
else if (Arg == "x86_fp80") Value = Type::getX86_FP80Ty(Context);
|
|
|
|
else if (Arg == "ppc_fp128") Value = Type::getPPC_FP128Ty(Context);
|
|
|
|
else if (Arg == "x86_mmx") Value = Type::getX86_MMXTy(Context);
|
|
|
|
else if (Arg.startswith("i")) {
|
|
|
|
unsigned N = 0;
|
|
|
|
Arg.drop_front().getAsInteger(10, N);
|
|
|
|
if (N > 0)
|
|
|
|
Value = Type::getIntNTy(Context, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Value)
|
|
|
|
return O.error("Invalid IR scalar type: '" + Arg + "'!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-01 11:43:20 +08:00
|
|
|
StringRef getValueName() const override { return "IR scalar type"; }
|
2015-07-10 18:01:47 +08:00
|
|
|
};
|
|
|
|
|
2017-09-01 05:56:16 +08:00
|
|
|
} // end namespace cl
|
2015-07-10 18:01:47 +08:00
|
|
|
|
|
|
|
static cl::list<Type*> AdditionalScalarTypes("types", cl::CommaSeparated,
|
|
|
|
cl::desc("Additional IR scalar types "
|
|
|
|
"(always includes i1, i8, i16, i32, i64, float and double)"));
|
2012-02-28 07:59:33 +08:00
|
|
|
|
2013-11-19 11:08:35 +08:00
|
|
|
namespace {
|
2017-09-01 05:56:16 +08:00
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
/// A utility class to provide a pseudo-random number generator which is
|
|
|
|
/// the same across all platforms. This is somewhat close to the libc
|
|
|
|
/// implementation. Note: This is not a cryptographically secure pseudorandom
|
|
|
|
/// number generator.
|
|
|
|
class Random {
|
|
|
|
public:
|
|
|
|
/// C'tor
|
|
|
|
Random(unsigned _seed):Seed(_seed) {}
|
2012-04-11 06:44:51 +08:00
|
|
|
|
|
|
|
/// Return a random integer, up to a
|
|
|
|
/// maximum of 2**19 - 1.
|
|
|
|
uint32_t Rand() {
|
|
|
|
uint32_t Val = Seed + 0x000b07a1;
|
2012-02-26 16:35:53 +08:00
|
|
|
Seed = (Val * 0x3c7c0ac1);
|
|
|
|
// Only lowest 19 bits are random-ish.
|
|
|
|
return Seed & 0x7ffff;
|
|
|
|
}
|
|
|
|
|
2012-04-11 06:44:51 +08:00
|
|
|
/// Return a random 64 bit integer.
|
|
|
|
uint64_t Rand64() {
|
2017-06-26 21:17:36 +08:00
|
|
|
uint64_t Val = Rand() & 0xffff;
|
|
|
|
Val |= uint64_t(Rand() & 0xffff) << 16;
|
|
|
|
Val |= uint64_t(Rand() & 0xffff) << 32;
|
|
|
|
Val |= uint64_t(Rand() & 0xffff) << 48;
|
|
|
|
return Val;
|
2012-04-11 06:44:51 +08:00
|
|
|
}
|
2012-06-21 16:58:15 +08:00
|
|
|
|
|
|
|
/// Rand operator for STL algorithms.
|
|
|
|
ptrdiff_t operator()(ptrdiff_t y) {
|
|
|
|
return Rand64() % y;
|
|
|
|
}
|
|
|
|
|
2017-02-16 22:37:03 +08:00
|
|
|
/// Make this like a C++11 random device
|
2017-09-01 05:56:16 +08:00
|
|
|
using result_type = uint32_t ;
|
|
|
|
|
2017-02-16 22:37:03 +08:00
|
|
|
static constexpr result_type min() { return 0; }
|
|
|
|
static constexpr result_type max() { return 0x7ffff; }
|
2017-09-01 05:56:16 +08:00
|
|
|
|
2017-06-26 18:16:34 +08:00
|
|
|
uint32_t operator()() {
|
|
|
|
uint32_t Val = Rand();
|
|
|
|
assert(Val <= max() && "Random value out of range");
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
private:
|
|
|
|
unsigned Seed;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Generate an empty function with a default argument list.
|
|
|
|
Function *GenEmptyFunction(Module *M) {
|
|
|
|
// Define a few arguments
|
|
|
|
LLVMContext &Context = M->getContext();
|
2015-06-24 19:49:44 +08:00
|
|
|
Type* ArgsTy[] = {
|
|
|
|
Type::getInt8PtrTy(Context),
|
|
|
|
Type::getInt32PtrTy(Context),
|
|
|
|
Type::getInt64PtrTy(Context),
|
|
|
|
Type::getInt32Ty(Context),
|
|
|
|
Type::getInt64Ty(Context),
|
|
|
|
Type::getInt8Ty(Context)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto *FuncTy = FunctionType::get(Type::getVoidTy(Context), ArgsTy, false);
|
2012-02-26 16:35:53 +08:00
|
|
|
// Pick a unique name to describe the input parameters
|
2015-06-24 19:49:44 +08:00
|
|
|
Twine Name = "autogen_SD" + Twine{SeedCL};
|
|
|
|
auto *Func = Function::Create(FuncTy, GlobalValue::ExternalLinkage, Name, M);
|
2012-02-26 16:35:53 +08:00
|
|
|
Func->setCallingConv(CallingConv::C);
|
|
|
|
return Func;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A base class, implementing utilities needed for
|
|
|
|
/// modifying and adding new random instructions.
|
|
|
|
struct Modifier {
|
|
|
|
/// Used to store the randomly generated values.
|
2017-09-01 05:56:16 +08:00
|
|
|
using PieceTable = std::vector<Value *>;
|
2012-02-26 16:35:53 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// C'tor
|
2017-09-01 05:56:16 +08:00
|
|
|
Modifier(BasicBlock *Block, PieceTable *PT, Random *R)
|
|
|
|
: BB(Block), PT(PT), Ran(R), Context(BB->getContext()) {}
|
2012-09-19 13:08:30 +08:00
|
|
|
|
|
|
|
/// virtual D'tor to silence warnings.
|
2017-09-01 05:56:16 +08:00
|
|
|
virtual ~Modifier() = default;
|
2012-09-19 13:08:30 +08:00
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
/// Add a new instruction.
|
|
|
|
virtual void Act() = 0;
|
2017-09-01 05:56:16 +08:00
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
/// Add N new instructions,
|
|
|
|
virtual void ActN(unsigned n) {
|
|
|
|
for (unsigned i=0; i<n; ++i)
|
|
|
|
Act();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-06-26 23:41:36 +08:00
|
|
|
/// Return a random integer.
|
|
|
|
uint32_t getRandom() {
|
|
|
|
return Ran->Rand();
|
|
|
|
}
|
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
/// Return a random value from the list of known values.
|
|
|
|
Value *getRandomVal() {
|
|
|
|
assert(PT->size());
|
2017-06-26 23:41:36 +08:00
|
|
|
return PT->at(getRandom() % PT->size());
|
2012-02-26 16:35:53 +08:00
|
|
|
}
|
|
|
|
|
2012-02-26 21:56:18 +08:00
|
|
|
Constant *getRandomConstant(Type *Tp) {
|
|
|
|
if (Tp->isIntegerTy()) {
|
2017-06-26 23:41:36 +08:00
|
|
|
if (getRandom() & 1)
|
2012-02-26 21:56:18 +08:00
|
|
|
return ConstantInt::getAllOnesValue(Tp);
|
|
|
|
return ConstantInt::getNullValue(Tp);
|
|
|
|
} else if (Tp->isFloatingPointTy()) {
|
2017-06-26 23:41:36 +08:00
|
|
|
if (getRandom() & 1)
|
2012-02-26 21:56:18 +08:00
|
|
|
return ConstantFP::getAllOnesValue(Tp);
|
|
|
|
return ConstantFP::getNullValue(Tp);
|
|
|
|
}
|
|
|
|
return UndefValue::get(Tp);
|
|
|
|
}
|
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
/// Return a random value with a known type.
|
|
|
|
Value *getRandomValue(Type *Tp) {
|
2017-06-26 23:41:36 +08:00
|
|
|
unsigned index = getRandom();
|
2012-02-26 16:35:53 +08:00
|
|
|
for (unsigned i=0; i<PT->size(); ++i) {
|
|
|
|
Value *V = PT->at((index + i) % PT->size());
|
|
|
|
if (V->getType() == Tp)
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the requested type was not found, generate a constant value.
|
|
|
|
if (Tp->isIntegerTy()) {
|
2017-06-26 23:41:36 +08:00
|
|
|
if (getRandom() & 1)
|
2012-02-26 16:35:53 +08:00
|
|
|
return ConstantInt::getAllOnesValue(Tp);
|
|
|
|
return ConstantInt::getNullValue(Tp);
|
|
|
|
} else if (Tp->isFloatingPointTy()) {
|
2017-06-26 23:41:36 +08:00
|
|
|
if (getRandom() & 1)
|
2012-02-26 16:35:53 +08:00
|
|
|
return ConstantFP::getAllOnesValue(Tp);
|
|
|
|
return ConstantFP::getNullValue(Tp);
|
2012-02-26 21:56:18 +08:00
|
|
|
} else if (Tp->isVectorTy()) {
|
|
|
|
VectorType *VTp = cast<VectorType>(Tp);
|
|
|
|
|
|
|
|
std::vector<Constant*> TempValues;
|
|
|
|
TempValues.reserve(VTp->getNumElements());
|
|
|
|
for (unsigned i = 0; i < VTp->getNumElements(); ++i)
|
|
|
|
TempValues.push_back(getRandomConstant(VTp->getScalarType()));
|
|
|
|
|
|
|
|
ArrayRef<Constant*> VectorValue(TempValues);
|
|
|
|
return ConstantVector::get(VectorValue);
|
2012-02-26 16:35:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return UndefValue::get(Tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a random value of any pointer type.
|
|
|
|
Value *getRandomPointerValue() {
|
2017-06-26 23:41:36 +08:00
|
|
|
unsigned index = getRandom();
|
2012-02-26 16:35:53 +08:00
|
|
|
for (unsigned i=0; i<PT->size(); ++i) {
|
|
|
|
Value *V = PT->at((index + i) % PT->size());
|
|
|
|
if (V->getType()->isPointerTy())
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
return UndefValue::get(pickPointerType());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a random value of any vector type.
|
|
|
|
Value *getRandomVectorValue() {
|
2017-06-26 23:41:36 +08:00
|
|
|
unsigned index = getRandom();
|
2012-02-26 16:35:53 +08:00
|
|
|
for (unsigned i=0; i<PT->size(); ++i) {
|
|
|
|
Value *V = PT->at((index + i) % PT->size());
|
|
|
|
if (V->getType()->isVectorTy())
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
return UndefValue::get(pickVectorType());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pick a random type.
|
|
|
|
Type *pickType() {
|
2019-05-03 21:50:38 +08:00
|
|
|
return (getRandom() & 1) ? pickVectorType() : pickScalarType();
|
2012-02-26 16:35:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Pick a random pointer type.
|
|
|
|
Type *pickPointerType() {
|
|
|
|
Type *Ty = pickType();
|
|
|
|
return PointerType::get(Ty, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pick a random vector type.
|
|
|
|
Type *pickVectorType(unsigned len = (unsigned)-1) {
|
|
|
|
// Pick a random vector width in the range 2**0 to 2**4.
|
|
|
|
// by adding two randoms we are generating a normal-like distribution
|
|
|
|
// around 2**3.
|
2017-06-26 23:41:36 +08:00
|
|
|
unsigned width = 1<<((getRandom() % 3) + (getRandom() % 3));
|
2012-04-11 06:44:49 +08:00
|
|
|
Type *Ty;
|
|
|
|
|
|
|
|
// Vectors of x86mmx are illegal; keep trying till we get something else.
|
|
|
|
do {
|
|
|
|
Ty = pickScalarType();
|
|
|
|
} while (Ty->isX86_MMXTy());
|
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
if (len != (unsigned)-1)
|
|
|
|
width = len;
|
|
|
|
return VectorType::get(Ty, width);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pick a random scalar type.
|
|
|
|
Type *pickScalarType() {
|
2015-07-10 18:01:47 +08:00
|
|
|
static std::vector<Type*> ScalarTypes;
|
|
|
|
if (ScalarTypes.empty()) {
|
|
|
|
ScalarTypes.assign({
|
|
|
|
Type::getInt1Ty(Context),
|
|
|
|
Type::getInt8Ty(Context),
|
|
|
|
Type::getInt16Ty(Context),
|
|
|
|
Type::getInt32Ty(Context),
|
|
|
|
Type::getInt64Ty(Context),
|
|
|
|
Type::getFloatTy(Context),
|
|
|
|
Type::getDoubleTy(Context)
|
|
|
|
});
|
|
|
|
ScalarTypes.insert(ScalarTypes.end(),
|
|
|
|
AdditionalScalarTypes.begin(), AdditionalScalarTypes.end());
|
|
|
|
}
|
2012-02-28 07:59:33 +08:00
|
|
|
|
2017-06-26 23:41:36 +08:00
|
|
|
return ScalarTypes[getRandom() % ScalarTypes.size()];
|
2012-02-26 16:35:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Basic block to populate
|
|
|
|
BasicBlock *BB;
|
2017-09-01 05:56:16 +08:00
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
/// Value table
|
|
|
|
PieceTable *PT;
|
2017-09-01 05:56:16 +08:00
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
/// Random number generator
|
|
|
|
Random *Ran;
|
2017-09-01 05:56:16 +08:00
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
/// Context
|
|
|
|
LLVMContext &Context;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LoadModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
LoadModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
void Act() override {
|
2013-12-05 13:44:44 +08:00
|
|
|
// Try to use predefined pointers. If non-exist, use undef pointer value;
|
2012-02-26 16:35:53 +08:00
|
|
|
Value *Ptr = getRandomPointerValue();
|
|
|
|
Value *V = new LoadInst(Ptr, "L", BB->getTerminator());
|
|
|
|
PT->push_back(V);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct StoreModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
StoreModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
void Act() override {
|
2013-12-05 13:44:44 +08:00
|
|
|
// Try to use predefined pointers. If non-exist, use undef pointer value;
|
2012-02-26 16:35:53 +08:00
|
|
|
Value *Ptr = getRandomPointerValue();
|
2019-01-11 00:43:26 +08:00
|
|
|
PointerType *Tp = cast<PointerType>(Ptr->getType());
|
2019-01-11 00:07:20 +08:00
|
|
|
Value *Val = getRandomValue(Tp->getElementType());
|
2012-02-26 20:00:22 +08:00
|
|
|
Type *ValTy = Val->getType();
|
2012-02-26 16:35:53 +08:00
|
|
|
|
|
|
|
// Do not store vectors of i1s because they are unsupported
|
2012-02-26 20:34:17 +08:00
|
|
|
// by the codegen.
|
|
|
|
if (ValTy->isVectorTy() && ValTy->getScalarSizeInBits() == 1)
|
2012-02-26 16:35:53 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
new StoreInst(Val, Ptr, BB->getTerminator());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BinModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
BinModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
void Act() override {
|
2012-02-26 16:35:53 +08:00
|
|
|
Value *Val0 = getRandomVal();
|
|
|
|
Value *Val1 = getRandomValue(Val0->getType());
|
|
|
|
|
|
|
|
// Don't handle pointer types.
|
|
|
|
if (Val0->getType()->isPointerTy() ||
|
|
|
|
Val1->getType()->isPointerTy())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't handle i1 types.
|
|
|
|
if (Val0->getType()->getScalarSizeInBits() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool isFloat = Val0->getType()->getScalarType()->isFloatingPointTy();
|
|
|
|
Instruction* Term = BB->getTerminator();
|
2017-06-26 23:41:36 +08:00
|
|
|
unsigned R = getRandom() % (isFloat ? 7 : 13);
|
2012-02-26 16:35:53 +08:00
|
|
|
Instruction::BinaryOps Op;
|
|
|
|
|
|
|
|
switch (R) {
|
|
|
|
default: llvm_unreachable("Invalid BinOp");
|
|
|
|
case 0:{Op = (isFloat?Instruction::FAdd : Instruction::Add); break; }
|
|
|
|
case 1:{Op = (isFloat?Instruction::FSub : Instruction::Sub); break; }
|
|
|
|
case 2:{Op = (isFloat?Instruction::FMul : Instruction::Mul); break; }
|
|
|
|
case 3:{Op = (isFloat?Instruction::FDiv : Instruction::SDiv); break; }
|
|
|
|
case 4:{Op = (isFloat?Instruction::FDiv : Instruction::UDiv); break; }
|
|
|
|
case 5:{Op = (isFloat?Instruction::FRem : Instruction::SRem); break; }
|
|
|
|
case 6:{Op = (isFloat?Instruction::FRem : Instruction::URem); break; }
|
|
|
|
case 7: {Op = Instruction::Shl; break; }
|
|
|
|
case 8: {Op = Instruction::LShr; break; }
|
|
|
|
case 9: {Op = Instruction::AShr; break; }
|
|
|
|
case 10:{Op = Instruction::And; break; }
|
|
|
|
case 11:{Op = Instruction::Or; break; }
|
|
|
|
case 12:{Op = Instruction::Xor; break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
PT->push_back(BinaryOperator::Create(Op, Val0, Val1, "B", Term));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Generate constant values.
|
|
|
|
struct ConstModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
ConstModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
void Act() override {
|
2012-02-26 16:35:53 +08:00
|
|
|
Type *Ty = pickType();
|
|
|
|
|
|
|
|
if (Ty->isVectorTy()) {
|
2017-06-26 23:41:36 +08:00
|
|
|
switch (getRandom() % 2) {
|
2017-07-09 15:04:00 +08:00
|
|
|
case 0: if (Ty->isIntOrIntVectorTy())
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(ConstantVector::getAllOnesValue(Ty));
|
2017-06-11 02:26:19 +08:00
|
|
|
break;
|
2017-07-09 15:04:00 +08:00
|
|
|
case 1: if (Ty->isIntOrIntVectorTy())
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(ConstantVector::getNullValue(Ty));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ty->isFloatingPointTy()) {
|
2012-04-11 06:44:51 +08:00
|
|
|
// Generate 128 random bits, the size of the (currently)
|
|
|
|
// largest floating-point types.
|
|
|
|
uint64_t RandomBits[2];
|
|
|
|
for (unsigned i = 0; i < 2; ++i)
|
|
|
|
RandomBits[i] = Ran->Rand64();
|
|
|
|
|
|
|
|
APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits));
|
2013-01-22 18:18:26 +08:00
|
|
|
APFloat RandomFloat(Ty->getFltSemantics(), RandomInt);
|
2012-04-11 06:44:51 +08:00
|
|
|
|
2017-06-26 23:41:36 +08:00
|
|
|
if (getRandom() & 1)
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(ConstantFP::getNullValue(Ty));
|
2012-04-11 06:44:51 +08:00
|
|
|
return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
|
2012-02-26 16:35:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Ty->isIntegerTy()) {
|
2017-06-26 23:41:36 +08:00
|
|
|
switch (getRandom() % 7) {
|
2017-06-13 04:09:53 +08:00
|
|
|
case 0:
|
|
|
|
return PT->push_back(ConstantInt::get(
|
|
|
|
Ty, APInt::getAllOnesValue(Ty->getPrimitiveSizeInBits())));
|
|
|
|
case 1:
|
|
|
|
return PT->push_back(ConstantInt::get(
|
|
|
|
Ty, APInt::getNullValue(Ty->getPrimitiveSizeInBits())));
|
2017-06-21 23:20:46 +08:00
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 5:
|
2017-06-13 04:09:53 +08:00
|
|
|
case 6:
|
2017-06-26 23:41:36 +08:00
|
|
|
PT->push_back(ConstantInt::get(Ty, getRandom()));
|
2012-02-26 16:35:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AllocaModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
AllocaModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
void Act() override {
|
2012-02-26 16:35:53 +08:00
|
|
|
Type *Tp = pickType();
|
2017-04-11 06:27:50 +08:00
|
|
|
const DataLayout &DL = BB->getModule()->getDataLayout();
|
|
|
|
PT->push_back(new AllocaInst(Tp, DL.getAllocaAddrSpace(),
|
|
|
|
"A", BB->getFirstNonPHI()));
|
2012-02-26 16:35:53 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExtractElementModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
ExtractElementModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
void Act() override {
|
2012-02-26 16:35:53 +08:00
|
|
|
Value *Val0 = getRandomVectorValue();
|
|
|
|
Value *V = ExtractElementInst::Create(Val0,
|
|
|
|
ConstantInt::get(Type::getInt32Ty(BB->getContext()),
|
2017-06-26 23:41:36 +08:00
|
|
|
getRandom() % cast<VectorType>(Val0->getType())->getNumElements()),
|
2012-02-26 16:35:53 +08:00
|
|
|
"E", BB->getTerminator());
|
|
|
|
return PT->push_back(V);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ShuffModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
ShuffModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2017-09-01 05:56:16 +08:00
|
|
|
void Act() override {
|
2012-02-26 16:35:53 +08:00
|
|
|
Value *Val0 = getRandomVectorValue();
|
|
|
|
Value *Val1 = getRandomValue(Val0->getType());
|
|
|
|
|
|
|
|
unsigned Width = cast<VectorType>(Val0->getType())->getNumElements();
|
|
|
|
std::vector<Constant*> Idxs;
|
|
|
|
|
|
|
|
Type *I32 = Type::getInt32Ty(BB->getContext());
|
|
|
|
for (unsigned i=0; i<Width; ++i) {
|
2017-06-26 23:41:36 +08:00
|
|
|
Constant *CI = ConstantInt::get(I32, getRandom() % (Width*2));
|
2012-02-26 16:35:53 +08:00
|
|
|
// Pick some undef values.
|
2017-06-26 23:41:36 +08:00
|
|
|
if (!(getRandom() % 5))
|
2012-02-26 16:35:53 +08:00
|
|
|
CI = UndefValue::get(I32);
|
|
|
|
Idxs.push_back(CI);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant *Mask = ConstantVector::get(Idxs);
|
|
|
|
|
|
|
|
Value *V = new ShuffleVectorInst(Val0, Val1, Mask, "Shuff",
|
|
|
|
BB->getTerminator());
|
|
|
|
PT->push_back(V);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InsertElementModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
InsertElementModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
void Act() override {
|
2012-02-26 16:35:53 +08:00
|
|
|
Value *Val0 = getRandomVectorValue();
|
|
|
|
Value *Val1 = getRandomValue(Val0->getType()->getScalarType());
|
|
|
|
|
|
|
|
Value *V = InsertElementInst::Create(Val0, Val1,
|
|
|
|
ConstantInt::get(Type::getInt32Ty(BB->getContext()),
|
2017-06-26 23:41:36 +08:00
|
|
|
getRandom() % cast<VectorType>(Val0->getType())->getNumElements()),
|
2012-02-26 16:35:53 +08:00
|
|
|
"I", BB->getTerminator());
|
|
|
|
return PT->push_back(V);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CastModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
CastModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2017-09-01 05:56:16 +08:00
|
|
|
void Act() override {
|
2012-02-26 16:35:53 +08:00
|
|
|
Value *V = getRandomVal();
|
|
|
|
Type *VTy = V->getType();
|
|
|
|
Type *DestTy = pickScalarType();
|
|
|
|
|
|
|
|
// Handle vector casts vectors.
|
|
|
|
if (VTy->isVectorTy()) {
|
|
|
|
VectorType *VecTy = cast<VectorType>(VTy);
|
|
|
|
DestTy = pickVectorType(VecTy->getNumElements());
|
|
|
|
}
|
|
|
|
|
2012-04-16 04:17:14 +08:00
|
|
|
// no need to cast.
|
2012-02-26 16:35:53 +08:00
|
|
|
if (VTy == DestTy) return;
|
|
|
|
|
|
|
|
// Pointers:
|
|
|
|
if (VTy->isPointerTy()) {
|
|
|
|
if (!DestTy->isPointerTy())
|
|
|
|
DestTy = PointerType::get(DestTy, 0);
|
|
|
|
return PT->push_back(
|
|
|
|
new BitCastInst(V, DestTy, "PC", BB->getTerminator()));
|
|
|
|
}
|
|
|
|
|
2012-04-16 04:17:14 +08:00
|
|
|
unsigned VSize = VTy->getScalarType()->getPrimitiveSizeInBits();
|
|
|
|
unsigned DestSize = DestTy->getScalarType()->getPrimitiveSizeInBits();
|
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
// Generate lots of bitcasts.
|
2017-06-26 23:41:36 +08:00
|
|
|
if ((getRandom() & 1) && VSize == DestSize) {
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(
|
|
|
|
new BitCastInst(V, DestTy, "BC", BB->getTerminator()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both types are integers:
|
2017-07-09 15:04:00 +08:00
|
|
|
if (VTy->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy()) {
|
2012-04-16 04:17:14 +08:00
|
|
|
if (VSize > DestSize) {
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(
|
|
|
|
new TruncInst(V, DestTy, "Tr", BB->getTerminator()));
|
|
|
|
} else {
|
2012-04-16 04:17:14 +08:00
|
|
|
assert(VSize < DestSize && "Different int types with the same size?");
|
2017-06-26 23:41:36 +08:00
|
|
|
if (getRandom() & 1)
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(
|
|
|
|
new ZExtInst(V, DestTy, "ZE", BB->getTerminator()));
|
|
|
|
return PT->push_back(new SExtInst(V, DestTy, "Se", BB->getTerminator()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fp to int.
|
2017-07-09 15:04:00 +08:00
|
|
|
if (VTy->isFPOrFPVectorTy() && DestTy->isIntOrIntVectorTy()) {
|
2017-06-26 23:41:36 +08:00
|
|
|
if (getRandom() & 1)
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(
|
|
|
|
new FPToSIInst(V, DestTy, "FC", BB->getTerminator()));
|
|
|
|
return PT->push_back(new FPToUIInst(V, DestTy, "FC", BB->getTerminator()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int to fp.
|
2017-07-09 15:04:00 +08:00
|
|
|
if (VTy->isIntOrIntVectorTy() && DestTy->isFPOrFPVectorTy()) {
|
2017-06-26 23:41:36 +08:00
|
|
|
if (getRandom() & 1)
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(
|
|
|
|
new SIToFPInst(V, DestTy, "FC", BB->getTerminator()));
|
|
|
|
return PT->push_back(new UIToFPInst(V, DestTy, "FC", BB->getTerminator()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both floats.
|
2017-07-09 15:04:00 +08:00
|
|
|
if (VTy->isFPOrFPVectorTy() && DestTy->isFPOrFPVectorTy()) {
|
2012-04-16 04:17:14 +08:00
|
|
|
if (VSize > DestSize) {
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(
|
|
|
|
new FPTruncInst(V, DestTy, "Tr", BB->getTerminator()));
|
2012-04-16 04:17:14 +08:00
|
|
|
} else if (VSize < DestSize) {
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(
|
|
|
|
new FPExtInst(V, DestTy, "ZE", BB->getTerminator()));
|
|
|
|
}
|
2012-04-16 04:17:14 +08:00
|
|
|
// If VSize == DestSize, then the two types must be fp128 and ppc_fp128,
|
|
|
|
// for which there is no defined conversion. So do nothing.
|
2012-02-26 16:35:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SelectModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
SelectModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
void Act() override {
|
2012-02-26 16:35:53 +08:00
|
|
|
// Try a bunch of different select configuration until a valid one is found.
|
2017-09-01 05:56:16 +08:00
|
|
|
Value *Val0 = getRandomVal();
|
|
|
|
Value *Val1 = getRandomValue(Val0->getType());
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2017-09-01 05:56:16 +08:00
|
|
|
Type *CondTy = Type::getInt1Ty(Context);
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2017-09-01 05:56:16 +08:00
|
|
|
// If the value type is a vector, and we allow vector select, then in 50%
|
|
|
|
// of the cases generate a vector select.
|
|
|
|
if (Val0->getType()->isVectorTy() && (getRandom() % 1)) {
|
|
|
|
unsigned NumElem = cast<VectorType>(Val0->getType())->getNumElements();
|
|
|
|
CondTy = VectorType::get(CondTy, NumElem);
|
|
|
|
}
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2017-09-01 05:56:16 +08:00
|
|
|
Value *Cond = getRandomValue(CondTy);
|
|
|
|
Value *V = SelectInst::Create(Cond, Val0, Val1, "Sl", BB->getTerminator());
|
|
|
|
return PT->push_back(V);
|
2012-02-26 16:35:53 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CmpModifier: public Modifier {
|
2017-09-01 05:56:16 +08:00
|
|
|
CmpModifier(BasicBlock *BB, PieceTable *PT, Random *R)
|
|
|
|
: Modifier(BB, PT, R) {}
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2017-09-01 05:56:16 +08:00
|
|
|
void Act() override {
|
2012-02-26 16:35:53 +08:00
|
|
|
Value *Val0 = getRandomVal();
|
|
|
|
Value *Val1 = getRandomValue(Val0->getType());
|
|
|
|
|
|
|
|
if (Val0->getType()->isPointerTy()) return;
|
|
|
|
bool fp = Val0->getType()->getScalarType()->isFloatingPointTy();
|
|
|
|
|
|
|
|
int op;
|
|
|
|
if (fp) {
|
2017-06-26 23:41:36 +08:00
|
|
|
op = getRandom() %
|
2012-02-26 16:35:53 +08:00
|
|
|
(CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE) +
|
|
|
|
CmpInst::FIRST_FCMP_PREDICATE;
|
|
|
|
} else {
|
2017-06-26 23:41:36 +08:00
|
|
|
op = getRandom() %
|
2012-02-26 16:35:53 +08:00
|
|
|
(CmpInst::LAST_ICMP_PREDICATE - CmpInst::FIRST_ICMP_PREDICATE) +
|
|
|
|
CmpInst::FIRST_ICMP_PREDICATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *V = CmpInst::Create(fp ? Instruction::FCmp : Instruction::ICmp,
|
2015-12-15 14:11:33 +08:00
|
|
|
(CmpInst::Predicate)op, Val0, Val1, "Cmp",
|
|
|
|
BB->getTerminator());
|
2012-02-26 16:35:53 +08:00
|
|
|
return PT->push_back(V);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-19 11:08:35 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static void FillFunction(Function *F, Random &R) {
|
2012-02-26 16:35:53 +08:00
|
|
|
// Create a legal entry block.
|
|
|
|
BasicBlock *BB = BasicBlock::Create(F->getContext(), "BB", F);
|
|
|
|
ReturnInst::Create(F->getContext(), BB);
|
|
|
|
|
|
|
|
// Create the value table.
|
|
|
|
Modifier::PieceTable PT;
|
|
|
|
|
|
|
|
// Consider arguments as legal values.
|
2015-06-24 19:49:44 +08:00
|
|
|
for (auto &arg : F->args())
|
|
|
|
PT.push_back(&arg);
|
2012-02-26 16:35:53 +08:00
|
|
|
|
|
|
|
// List of modifiers which add new random instructions.
|
2015-06-24 19:49:44 +08:00
|
|
|
std::vector<std::unique_ptr<Modifier>> Modifiers;
|
|
|
|
Modifiers.emplace_back(new LoadModifier(BB, &PT, &R));
|
|
|
|
Modifiers.emplace_back(new StoreModifier(BB, &PT, &R));
|
|
|
|
auto SM = Modifiers.back().get();
|
|
|
|
Modifiers.emplace_back(new ExtractElementModifier(BB, &PT, &R));
|
|
|
|
Modifiers.emplace_back(new ShuffModifier(BB, &PT, &R));
|
|
|
|
Modifiers.emplace_back(new InsertElementModifier(BB, &PT, &R));
|
|
|
|
Modifiers.emplace_back(new BinModifier(BB, &PT, &R));
|
|
|
|
Modifiers.emplace_back(new CastModifier(BB, &PT, &R));
|
|
|
|
Modifiers.emplace_back(new SelectModifier(BB, &PT, &R));
|
|
|
|
Modifiers.emplace_back(new CmpModifier(BB, &PT, &R));
|
2012-02-26 16:35:53 +08:00
|
|
|
|
|
|
|
// Generate the random instructions
|
2015-06-24 19:49:44 +08:00
|
|
|
AllocaModifier{BB, &PT, &R}.ActN(5); // Throw in a few allocas
|
|
|
|
ConstModifier{BB, &PT, &R}.ActN(40); // Throw in a few constants
|
2012-02-26 16:35:53 +08:00
|
|
|
|
2015-06-24 19:49:44 +08:00
|
|
|
for (unsigned i = 0; i < SizeCL / Modifiers.size(); ++i)
|
|
|
|
for (auto &Mod : Modifiers)
|
|
|
|
Mod->Act();
|
2012-02-26 16:35:53 +08:00
|
|
|
|
|
|
|
SM->ActN(5); // Throw in a few stores.
|
|
|
|
}
|
|
|
|
|
2013-11-19 11:08:35 +08:00
|
|
|
static void IntroduceControlFlow(Function *F, Random &R) {
|
2012-06-21 16:58:15 +08:00
|
|
|
std::vector<Instruction*> BoolInst;
|
2015-06-24 19:49:44 +08:00
|
|
|
for (auto &Instr : F->front()) {
|
|
|
|
if (Instr.getType() == IntegerType::getInt1Ty(F->getContext()))
|
|
|
|
BoolInst.push_back(&Instr);
|
2012-02-26 16:35:53 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 22:37:03 +08:00
|
|
|
std::shuffle(BoolInst.begin(), BoolInst.end(), R);
|
2012-06-21 16:58:15 +08:00
|
|
|
|
2015-06-24 19:49:44 +08:00
|
|
|
for (auto *Instr : BoolInst) {
|
2012-02-26 16:35:53 +08:00
|
|
|
BasicBlock *Curr = Instr->getParent();
|
2015-11-07 08:01:16 +08:00
|
|
|
BasicBlock::iterator Loc = Instr->getIterator();
|
2012-02-26 16:35:53 +08:00
|
|
|
BasicBlock *Next = Curr->splitBasicBlock(Loc, "CF");
|
|
|
|
Instr->moveBefore(Curr->getTerminator());
|
|
|
|
if (Curr != &F->getEntryBlock()) {
|
|
|
|
BranchInst::Create(Curr, Next, Instr, Curr->getTerminator());
|
|
|
|
Curr->getTerminator()->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 05:56:16 +08:00
|
|
|
} // end namespace llvm
|
2015-07-10 18:01:47 +08:00
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
int main(int argc, char **argv) {
|
2015-07-10 18:01:47 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2012-02-26 16:35:53 +08:00
|
|
|
// Init LLVM, call llvm_shutdown() on exit, parse args, etc.
|
2015-07-13 19:25:56 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2012-02-26 16:35:53 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm codegen stress-tester\n");
|
|
|
|
llvm_shutdown_obj Y;
|
|
|
|
|
2019-08-15 23:54:37 +08:00
|
|
|
auto M = std::make_unique<Module>("/tmp/autogen.bc", Context);
|
2012-02-26 16:35:53 +08:00
|
|
|
Function *F = GenEmptyFunction(M.get());
|
2012-06-21 16:58:15 +08:00
|
|
|
|
|
|
|
// Pick an initial seed value
|
|
|
|
Random R(SeedCL);
|
|
|
|
// Generate lots of random instructions inside a single basic block.
|
|
|
|
FillFunction(F, R);
|
|
|
|
// Break the basic block into many loops.
|
|
|
|
IntroduceControlFlow(F, R);
|
2012-02-26 16:35:53 +08:00
|
|
|
|
|
|
|
// Figure out what stream we are supposed to write to...
|
2017-09-23 09:03:17 +08:00
|
|
|
std::unique_ptr<ToolOutputFile> Out;
|
2012-02-26 16:35:53 +08:00
|
|
|
// Default to standard output.
|
|
|
|
if (OutputFilename.empty())
|
|
|
|
OutputFilename = "-";
|
|
|
|
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
2019-08-05 13:43:48 +08:00
|
|
|
Out.reset(new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
|
2014-08-26 02:16:47 +08:00
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
2012-02-26 16:35:53 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-13 18:01:29 +08:00
|
|
|
legacy::PassManager Passes;
|
2012-02-26 16:35:53 +08:00
|
|
|
Passes.add(createVerifierPass());
|
2014-01-12 19:39:04 +08:00
|
|
|
Passes.add(createPrintModulePass(Out->os()));
|
2012-02-26 16:35:53 +08:00
|
|
|
Passes.run(*M.get());
|
|
|
|
Out->keep();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|