-//===-- llvm-stress.cpp - Print the size of each object section ------------===//
+//===-- llvm-stress.cpp - Generate random LL files to stress-test LLVM ----===//
//
// The LLVM Compiler Infrastructure
//
// different components in LLVM.
//
//===----------------------------------------------------------------------===//
-#include "llvm/LLVMContext.h"
-#include "llvm/Module.h"
+
+#include "llvm/Analysis/CallGraphSCCPass.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/IRPrintingPasses.h"
+#include "llvm/IR/Instruction.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/LegacyPassNameParser.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Verifier.h"
#include "llvm/PassManager.h"
-#include "llvm/Constants.h"
-#include "llvm/Instruction.h"
-#include "llvm/CallGraphSCCPass.h"
-#include "llvm/Assembly/PrintModulePass.h"
-#include "llvm/Analysis/Verifier.h"
-#include "llvm/Support/PassNameParser.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/ToolOutputFile.h"
-#include <memory>
-#include <sstream>
+#include <algorithm>
#include <set>
+#include <sstream>
#include <vector>
-#include <algorithm>
using namespace llvm;
static cl::opt<unsigned> SeedCL("seed",
OutputFilename("o", cl::desc("Override output filename"),
cl::value_desc("filename"));
+static cl::opt<bool> GenHalfFloat("generate-half-float",
+ cl::desc("Generate half-length floating-point values"), cl::init(false));
+static cl::opt<bool> GenX86FP80("generate-x86-fp80",
+ cl::desc("Generate 80-bit X86 floating-point values"), cl::init(false));
+static cl::opt<bool> GenFP128("generate-fp128",
+ cl::desc("Generate 128-bit floating-point values"), cl::init(false));
+static cl::opt<bool> GenPPCFP128("generate-ppc-fp128",
+ cl::desc("Generate 128-bit PPC floating-point values"), cl::init(false));
+static cl::opt<bool> GenX86MMX("generate-x86-mmx",
+ cl::desc("Generate X86 MMX floating-point values"), cl::init(false));
+
+namespace {
/// 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
public:
/// C'tor
Random(unsigned _seed):Seed(_seed) {}
- /// Return the next random value.
- unsigned Rand() {
- unsigned Val = Seed + 0x000b07a1;
+
+ /// Return a random integer, up to a
+ /// maximum of 2**19 - 1.
+ uint32_t Rand() {
+ uint32_t Val = Seed + 0x000b07a1;
Seed = (Val * 0x3c7c0ac1);
// Only lowest 19 bits are random-ish.
return Seed & 0x7ffff;
}
+ /// Return a random 32 bit integer.
+ uint32_t Rand32() {
+ uint32_t Val = Rand();
+ Val &= 0xffff;
+ return Val | (Rand() << 16);
+ }
+
+ /// Return a random 64 bit integer.
+ uint64_t Rand64() {
+ uint64_t Val = Rand32();
+ return Val | (uint64_t(Rand32()) << 32);
+ }
+
+ /// Rand operator for STL algorithms.
+ ptrdiff_t operator()(ptrdiff_t y) {
+ return Rand64() % y;
+ }
+
private:
unsigned Seed;
};
public:
/// C'tor
- Modifier(BasicBlock *_BB, PieceTable *PT, Random *R):
- BB(_BB),PT(PT),Ran(R),Context(BB->getContext()) {};
+ Modifier(BasicBlock *Block, PieceTable *PT, Random *R):
+ BB(Block),PT(PT),Ran(R),Context(BB->getContext()) {}
+
+ /// virtual D'tor to silence warnings.
+ virtual ~Modifier() {}
+
/// Add a new instruction.
virtual void Act() = 0;
/// Add N new instructions,
return PT->at(Ran->Rand() % PT->size());
}
+ Constant *getRandomConstant(Type *Tp) {
+ if (Tp->isIntegerTy()) {
+ if (Ran->Rand() & 1)
+ return ConstantInt::getAllOnesValue(Tp);
+ return ConstantInt::getNullValue(Tp);
+ } else if (Tp->isFloatingPointTy()) {
+ if (Ran->Rand() & 1)
+ return ConstantFP::getAllOnesValue(Tp);
+ return ConstantFP::getNullValue(Tp);
+ }
+ return UndefValue::get(Tp);
+ }
+
/// Return a random value with a known type.
Value *getRandomValue(Type *Tp) {
unsigned index = Ran->Rand();
if (Ran->Rand() & 1)
return ConstantFP::getAllOnesValue(Tp);
return ConstantFP::getNullValue(Tp);
+ } 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);
}
- // TODO: return values for vector types.
return UndefValue::get(Tp);
}
/// Pick a random vector type.
Type *pickVectorType(unsigned len = (unsigned)-1) {
- Type *Ty = pickScalarType();
// 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.
unsigned width = 1<<((Ran->Rand() % 3) + (Ran->Rand() % 3));
+ Type *Ty;
+
+ // Vectors of x86mmx are illegal; keep trying till we get something else.
+ do {
+ Ty = pickScalarType();
+ } while (Ty->isX86_MMXTy());
+
if (len != (unsigned)-1)
width = len;
return VectorType::get(Ty, width);
/// Pick a random scalar type.
Type *pickScalarType() {
- switch (Ran->Rand() % 15) {
- case 0: return Type::getInt1Ty(Context);
- case 1: return Type::getInt8Ty(Context);
- case 2: return Type::getInt16Ty(Context);
- case 3: case 4:
- case 5: return Type::getFloatTy(Context);
- case 6: case 7:
- case 8: return Type::getDoubleTy(Context);
- case 9: case 10:
- case 11: return Type::getInt32Ty(Context);
- case 12: case 13:
- case 14: return Type::getInt64Ty(Context);
- }
- llvm_unreachable("Invalid scalar value");
+ Type *t = 0;
+ do {
+ switch (Ran->Rand() % 30) {
+ case 0: t = Type::getInt1Ty(Context); break;
+ case 1: t = Type::getInt8Ty(Context); break;
+ case 2: t = Type::getInt16Ty(Context); break;
+ case 3: case 4:
+ case 5: t = Type::getFloatTy(Context); break;
+ case 6: case 7:
+ case 8: t = Type::getDoubleTy(Context); break;
+ case 9: case 10:
+ case 11: t = Type::getInt32Ty(Context); break;
+ case 12: case 13:
+ case 14: t = Type::getInt64Ty(Context); break;
+ case 15: case 16:
+ case 17: if (GenHalfFloat) t = Type::getHalfTy(Context); break;
+ case 18: case 19:
+ case 20: if (GenX86FP80) t = Type::getX86_FP80Ty(Context); break;
+ case 21: case 22:
+ case 23: if (GenFP128) t = Type::getFP128Ty(Context); break;
+ case 24: case 25:
+ case 26: if (GenPPCFP128) t = Type::getPPC_FP128Ty(Context); break;
+ case 27: case 28:
+ case 29: if (GenX86MMX) t = Type::getX86_MMXTy(Context); break;
+ default: llvm_unreachable("Invalid scalar value");
+ }
+ } while (t == 0);
+
+ return t;
}
/// Basic block to populate
};
struct LoadModifier: public Modifier {
- LoadModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {};
+ LoadModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
virtual void Act() {
- // Try to use predefined pointers. If non exist, use undef pointer value;
+ // Try to use predefined pointers. If non-exist, use undef pointer value;
Value *Ptr = getRandomPointerValue();
Value *V = new LoadInst(Ptr, "L", BB->getTerminator());
PT->push_back(V);
struct StoreModifier: public Modifier {
StoreModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
virtual void Act() {
- // Try to use predefined pointers. If non exist, use undef pointer value;
+ // Try to use predefined pointers. If non-exist, use undef pointer value;
Value *Ptr = getRandomPointerValue();
Type *Tp = Ptr->getType();
Value *Val = getRandomValue(Tp->getContainedType(0));
+ Type *ValTy = Val->getType();
// Do not store vectors of i1s because they are unsupported
- //by the codegen.
- if (Tp->isVectorTy() && Tp->getScalarSizeInBits() == 1)
+ // by the codegen.
+ if (ValTy->isVectorTy() && ValTy->getScalarSizeInBits() == 1)
return;
new StoreInst(Val, Ptr, BB->getTerminator());
}
if (Ty->isFloatingPointTy()) {
+ // 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));
+ APFloat RandomFloat(Ty->getFltSemantics(), RandomInt);
+
if (Ran->Rand() & 1)
return PT->push_back(ConstantFP::getNullValue(Ty));
- return PT->push_back(ConstantFP::get(Ty,
- static_cast<double>(1)/Ran->Rand()));
+ return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
}
if (Ty->isIntegerTy()) {
Value *Val0 = getRandomVectorValue();
Value *V = ExtractElementInst::Create(Val0,
ConstantInt::get(Type::getInt32Ty(BB->getContext()),
- Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
+ Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
"E", BB->getTerminator());
return PT->push_back(V);
}
DestTy = pickVectorType(VecTy->getNumElements());
}
- // no need to casr.
+ // no need to cast.
if (VTy == DestTy) return;
// Pointers:
new BitCastInst(V, DestTy, "PC", BB->getTerminator()));
}
+ unsigned VSize = VTy->getScalarType()->getPrimitiveSizeInBits();
+ unsigned DestSize = DestTy->getScalarType()->getPrimitiveSizeInBits();
+
// Generate lots of bitcasts.
- if ((Ran->Rand() & 1) &&
- VTy->getPrimitiveSizeInBits() == DestTy->getPrimitiveSizeInBits()) {
+ if ((Ran->Rand() & 1) && VSize == DestSize) {
return PT->push_back(
new BitCastInst(V, DestTy, "BC", BB->getTerminator()));
}
// Both types are integers:
if (VTy->getScalarType()->isIntegerTy() &&
DestTy->getScalarType()->isIntegerTy()) {
- if (VTy->getScalarType()->getPrimitiveSizeInBits() >
- DestTy->getScalarType()->getPrimitiveSizeInBits()) {
+ if (VSize > DestSize) {
return PT->push_back(
new TruncInst(V, DestTy, "Tr", BB->getTerminator()));
} else {
+ assert(VSize < DestSize && "Different int types with the same size?");
if (Ran->Rand() & 1)
return PT->push_back(
new ZExtInst(V, DestTy, "ZE", BB->getTerminator()));
// Both floats.
if (VTy->getScalarType()->isFloatingPointTy() &&
DestTy->getScalarType()->isFloatingPointTy()) {
- if (VTy->getScalarType()->getPrimitiveSizeInBits() >
- DestTy->getScalarType()->getPrimitiveSizeInBits()) {
+ if (VSize > DestSize) {
return PT->push_back(
new FPTruncInst(V, DestTy, "Tr", BB->getTerminator()));
- } else {
+ } else if (VSize < DestSize) {
return PT->push_back(
new FPExtInst(V, DestTy, "ZE", BB->getTerminator()));
}
+ // If VSize == DestSize, then the two types must be fp128 and ppc_fp128,
+ // for which there is no defined conversion. So do nothing.
}
}
}
};
-void FillFunction(Function *F) {
+} // end anonymous namespace
+
+static void FillFunction(Function *F, Random &R) {
// 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;
- // Pick an initial seed value
- Random R(SeedCL);
// Consider arguments as legal values.
for (Function::arg_iterator it = F->arg_begin(), e = F->arg_end();
// List of modifiers which add new random instructions.
std::vector<Modifier*> Modifiers;
- std::auto_ptr<Modifier> LM(new LoadModifier(BB, &PT, &R));
- std::auto_ptr<Modifier> SM(new StoreModifier(BB, &PT, &R));
- std::auto_ptr<Modifier> EE(new ExtractElementModifier(BB, &PT, &R));
- std::auto_ptr<Modifier> SHM(new ShuffModifier(BB, &PT, &R));
- std::auto_ptr<Modifier> IE(new InsertElementModifier(BB, &PT, &R));
- std::auto_ptr<Modifier> BM(new BinModifier(BB, &PT, &R));
- std::auto_ptr<Modifier> CM(new CastModifier(BB, &PT, &R));
- std::auto_ptr<Modifier> SLM(new SelectModifier(BB, &PT, &R));
- std::auto_ptr<Modifier> PM(new CmpModifier(BB, &PT, &R));
+ std::unique_ptr<Modifier> LM(new LoadModifier(BB, &PT, &R));
+ std::unique_ptr<Modifier> SM(new StoreModifier(BB, &PT, &R));
+ std::unique_ptr<Modifier> EE(new ExtractElementModifier(BB, &PT, &R));
+ std::unique_ptr<Modifier> SHM(new ShuffModifier(BB, &PT, &R));
+ std::unique_ptr<Modifier> IE(new InsertElementModifier(BB, &PT, &R));
+ std::unique_ptr<Modifier> BM(new BinModifier(BB, &PT, &R));
+ std::unique_ptr<Modifier> CM(new CastModifier(BB, &PT, &R));
+ std::unique_ptr<Modifier> SLM(new SelectModifier(BB, &PT, &R));
+ std::unique_ptr<Modifier> PM(new CmpModifier(BB, &PT, &R));
Modifiers.push_back(LM.get());
Modifiers.push_back(SM.get());
Modifiers.push_back(EE.get());
SM->ActN(5); // Throw in a few stores.
}
-void IntroduceControlFlow(Function *F) {
- std::set<Instruction*> BoolInst;
+static void IntroduceControlFlow(Function *F, Random &R) {
+ std::vector<Instruction*> BoolInst;
for (BasicBlock::iterator it = F->begin()->begin(),
e = F->begin()->end(); it != e; ++it) {
if (it->getType() == IntegerType::getInt1Ty(F->getContext()))
- BoolInst.insert(it);
+ BoolInst.push_back(it);
}
- for (std::set<Instruction*>::iterator it = BoolInst.begin(),
+ std::random_shuffle(BoolInst.begin(), BoolInst.end(), R);
+
+ for (std::vector<Instruction*>::iterator it = BoolInst.begin(),
e = BoolInst.end(); it != e; ++it) {
Instruction *Instr = *it;
BasicBlock *Curr = Instr->getParent();
cl::ParseCommandLineOptions(argc, argv, "llvm codegen stress-tester\n");
llvm_shutdown_obj Y;
- std::auto_ptr<Module> M(new Module("/tmp/autogen.bc", getGlobalContext()));
+ std::unique_ptr<Module> M(new Module("/tmp/autogen.bc", getGlobalContext()));
Function *F = GenEmptyFunction(M.get());
- FillFunction(F);
- IntroduceControlFlow(F);
+
+ // 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);
// Figure out what stream we are supposed to write to...
- OwningPtr<tool_output_file> Out;
+ std::unique_ptr<tool_output_file> Out;
// Default to standard output.
if (OutputFilename.empty())
OutputFilename = "-";
std::string ErrorInfo;
Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
- raw_fd_ostream::F_Binary));
+ sys::fs::F_None));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
return 1;
PassManager Passes;
Passes.add(createVerifierPass());
- Passes.add(createPrintModulePass(&Out->os()));
+ Passes.add(createPrintModulePass(Out->os()));
Passes.run(*M.get());
Out->keep();