// 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",
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
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;
};
/// C'tor
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,
struct LoadModifier: public Modifier {
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));
RandomBits[i] = Ran->Rand64();
APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits));
-
- bool isIEEE = !Ty->isX86_FP80Ty() && !Ty->isPPC_FP128Ty();
- APFloat RandomFloat(RandomInt, isIEEE);
+ APFloat RandomFloat(Ty->getFltSemantics(), RandomInt);
if (Ran->Rand() & 1)
return PT->push_back(ConstantFP::getNullValue(Ty));
}
};
-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();