Skip to content

Commit

Permalink
Make KLEE compile against LLVM 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Trembecký committed May 4, 2016
1 parent 51e37df commit 7c16c63
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 28 deletions.
3 changes: 1 addition & 2 deletions include/klee/CommandLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ extern llvm::cl::opt<klee::MetaSMTBackendType> MetaSMTBackend;

//A bit of ugliness so we can use cl::list<> like cl::bits<>, see queryLoggingOptions
template <typename T>
static bool optionIsSet(llvm::cl::list<T> list, T option)
{
static bool optionIsSet(llvm::cl::list<T> &list, T option) {
return std::find(list.begin(), list.end(), option) != list.end();
}

Expand Down
7 changes: 7 additions & 0 deletions include/klee/Internal/Support/FloatEvaluation.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,15 @@ inline uint64_t mod(uint64_t l, uint64_t r, unsigned inWidth) {
// determine if l represents NaN
inline bool isNaN(uint64_t l, unsigned inWidth) {
switch( inWidth ) {
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
case FLT_BITS:
return std::isnan(UInt64AsFloat(l));
case DBL_BITS:
return std::isnan(UInt64AsDouble(l));
#else
case FLT_BITS: return llvm::IsNAN( UInt64AsFloat(l) );
case DBL_BITS: return llvm::IsNAN( UInt64AsDouble(l) );
#endif
default: llvm::report_fatal_error("unsupported floating point width");
}
}
Expand Down
15 changes: 10 additions & 5 deletions lib/Core/ExternalDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,16 @@ Function *ExternalDispatcher::createDispatcher(Function *target, Instruction *in
// functions.
LLVM_TYPE_Q Type *argTy = (i < FTy->getNumParams() ? FTy->getParamType(i) :
(*ai)->getType());
Instruction *argI64p =
GetElementPtrInst::Create(argI64s,
ConstantInt::get(Type::getInt32Ty(getGlobalContext()),
idx),
"", dBB);
Instruction *argI64p =
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
GetElementPtrInst::Create(
nullptr, argI64s,
#else
GetElementPtrInst::Create(
argI64s,
#endif
ConstantInt::get(Type::getInt32Ty(getGlobalContext()), idx), "",
dBB);

Instruction *argp = new BitCastInst(argI64p, PointerType::getUnqual(argTy),
"", dBB);
Expand Down
3 changes: 2 additions & 1 deletion lib/Core/StatsTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ static bool instructionIsCoverable(Instruction *i) {
} else {
Instruction *prev = --it;
if (isa<CallInst>(prev) || isa<InvokeInst>(prev)) {
Function *target = getDirectCallTarget(prev);
CallSite cs(prev);
Function *target = getDirectCallTarget(cs);
if (target && target->doesNotReturn())
return false;
}
Expand Down
8 changes: 7 additions & 1 deletion lib/Module/InstructionInfoTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static void buildInstructionToLineMap(Module *m,
}
}

static std::string getDSPIPath(DILocation Loc) {
static std::string getDSPIPath(const DILocation &Loc) {
std::string dir = Loc.getDirectory();
std::string file = Loc.getFilename();
if (dir.empty() || file[0] == '/') {
Expand All @@ -103,9 +103,15 @@ bool InstructionInfoTable::getInstructionDebugInfo(const llvm::Instruction *I,
const std::string *&File,
unsigned &Line) {
if (MDNode *N = I->getMetadata("dbg")) {
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
DILocation *Loc = cast<DILocation>(N);
File = internString(getDSPIPath(*Loc));
Line = Loc->getLine();
#else
DILocation Loc(N);
File = internString(getDSPIPath(Loc));
Line = Loc.getLineNumber();
#endif
return true;
}

Expand Down
14 changes: 14 additions & 0 deletions lib/Module/IntrinsicCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,25 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
Value *pSrc = CastInst::CreatePointerCast(src, i64p, "vacopy.cast.src", ii);
Value *val = new LoadInst(pSrc, std::string(), ii); new StoreInst(val, pDst, ii);
Value *off = ConstantInt::get(Type::getInt64Ty(getGlobalContext()), 1);
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
pDst =
GetElementPtrInst::Create(nullptr, pDst, off, std::string(), ii);
pSrc =
GetElementPtrInst::Create(nullptr, pSrc, off, std::string(), ii);
#else
pDst = GetElementPtrInst::Create(pDst, off, std::string(), ii);
pSrc = GetElementPtrInst::Create(pSrc, off, std::string(), ii);
#endif
val = new LoadInst(pSrc, std::string(), ii); new StoreInst(val, pDst, ii);
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
pDst =
GetElementPtrInst::Create(nullptr, pDst, off, std::string(), ii);
pSrc =
GetElementPtrInst::Create(nullptr, pSrc, off, std::string(), ii);
#else
pDst = GetElementPtrInst::Create(pDst, off, std::string(), ii);
pSrc = GetElementPtrInst::Create(pSrc, off, std::string(), ii);
#endif
val = new LoadInst(pSrc, std::string(), ii); new StoreInst(val, pDst, ii);
}
ii->removeFromParent();
Expand Down
13 changes: 12 additions & 1 deletion lib/Module/KModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@
#else
#include "llvm/IR/CallSite.h"
#endif

#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
#include "llvm/IR/LegacyPassManager.h"
#else
#include "llvm/PassManager.h"
#endif
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/raw_os_ostream.h"
Expand Down Expand Up @@ -301,7 +304,11 @@ void KModule::prepare(const Interpreter::ModuleOptions &opts,
// invariant transformations that we will end up doing later so that
// optimize is seeing what is as close as possible to the final
// module.
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
legacy::PassManager pm;
#else
PassManager pm;
#endif
pm.add(new RaiseAsmPass());
if (opts.CheckDivZero) pm.add(new DivCheckPass());
if (opts.CheckOvershift) pm.add(new OvershiftCheckPass());
Expand Down Expand Up @@ -369,7 +376,11 @@ void KModule::prepare(const Interpreter::ModuleOptions &opts,
// linked in something with intrinsics but any external calls are
// going to be unresolved. We really need to handle the intrinsics
// directly I think?
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
legacy::PassManager pm3;
#else
PassManager pm3;
#endif
pm3.add(createCFGSimplificationPass());
switch(SwitchType) {
case eSwitchTypeInternal: break;
Expand Down
24 changes: 21 additions & 3 deletions lib/Module/ModuleUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,22 @@ static bool linkBCA(object::Archive* archive, Module* composite, std::string& er
// FIXME: Maybe load bitcode file lazily? Then if we need to link, materialise
// the module
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 5)
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
ErrorOr<std::unique_ptr<Module> > Result_error =
#else
ErrorOr<Module *> Result_error =
#endif
parseBitcodeFile(buff.get(), getGlobalContext());
ec = Result_error.getError();
if (ec)
errorMessage = ec.message();
else
Result = Result_error.get();
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
Result = Result_error->release();
#else
Result = Result_error.get();
#endif
#else // LLVM 3.3, 3.4
Result =
ParseBitcodeFile(buff.get(), getGlobalContext(), &errorMessage);
#endif
Expand Down Expand Up @@ -416,8 +424,15 @@ Module *klee::linkWithLibrary(Module *module,

if (magic == sys::fs::file_magic::bitcode) {

#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
ErrorOr<std::unique_ptr<Module> > Result = parseBitcodeFile(buff, Context);

if ((ec = Buffer.getError()) || Linker::LinkModules(module, Result->get()))
klee_error("Link with library %s failed: %s", libraryName.c_str(),
ec.message().c_str());
#else // LLVM 3.5, 3.6
ErrorOr<Module *> Result = parseBitcodeFile(buff, Context);
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 6)
#if LLVM_VERSION_CODE == LLVM_VERSION(3, 6)
if ((ec = Buffer.getError()) || Linker::LinkModules(module, Result.get()))
#else // LLVM 3.5
if ((ec = Buffer.getError()) ||
Expand All @@ -426,8 +441,11 @@ Module *klee::linkWithLibrary(Module *module,
#endif
klee_error("Link with library %s failed: %s", libraryName.c_str(),
ec.message().c_str());

// unique_ptr owns the Module, we don't have to delete it
#if LLVM_VERSION_CODE < LLVM_VERSION(3, 7)
delete Result.get();
#endif
#endif

} else if (magic == sys::fs::file_magic::archive) {
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 6)
Expand Down
39 changes: 27 additions & 12 deletions lib/Module/Optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
//===----------------------------------------------------------------------===//

#include "klee/Config/Version.h"
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
#include "llvm/IR/LegacyPassManager.h"
#else
#include "llvm/PassManager.h"
#endif
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Support/CommandLine.h"
Expand Down Expand Up @@ -80,7 +84,11 @@ static cl::alias A1("S", cl::desc("Alias for --strip-debug"),

// A utility function that adds a pass to the pass manager but will also add
// a verifier pass after if we're supposed to verify.
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
static inline void addPass(legacy::PassManager &PM, Pass *P) {
#else
static inline void addPass(PassManager &PM, Pass *P) {
#endif
// Add the pass to the pass manager...
PM.add(P);

Expand All @@ -91,8 +99,11 @@ static inline void addPass(PassManager &PM, Pass *P) {

namespace llvm {


#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
static void AddStandardCompilePasses(legacy::PassManager &PM) {
#else
static void AddStandardCompilePasses(PassManager &PM) {
#endif
PM.add(createVerifierPass()); // Verify that input is correct

#if LLVM_VERSION_CODE < LLVM_VERSION(3, 0)
Expand Down Expand Up @@ -166,27 +177,31 @@ static void AddStandardCompilePasses(PassManager &PM) {
void Optimize(Module* M) {

// Instantiate the pass manager to organize the passes.
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
legacy::PassManager Passes;
#else
PassManager Passes;
#endif

// If we're verifying, start off with a verification pass.
if (VerifyEach)
Passes.add(createVerifierPass());

#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 6)
#if LLVM_VERSION_CODE <= LLVM_VERSION(3, 1)
// Add an appropriate TargetData instance for this module...
addPass(Passes, new TargetData(M));
#elif LLVM_VERSION_CODE <= LLVM_VERSION(3, 4)
// Add an appropriate DataLayout instance for this module...
DataLayoutPass *dlpass = new DataLayoutPass();
dlpass->doInitialization(*M);
addPass(Passes, dlpass);
#elif LLVM_VERSION_CODE >= LLVM_VERSION(3, 5)
addPass(Passes, new DataLayout(M));
#elif LLVM_VERSION_CODE == LLVM_VERSION(3, 5)
// Add an appropriate DataLayout instance for this module...
addPass(Passes, new DataLayoutPass(M));
#elif LLVM_VERSION_CODE > LLVM_VERSION(3, 1)
#elif LLVM_VERSION_CODE == LLVM_VERSION(3, 6)
// Add an appropriate DataLayout instance for this module...
addPass(Passes, new DataLayout(M));
#else
// Add an appropriate TargetData instance for this module...
addPass(Passes, new TargetData(M));
#endif
DataLayoutPass *dlpass = new DataLayoutPass();
dlpass->doInitialization(*M);
addPass(Passes, dlpass);
#endif // LLVM 3.7+ doesn't have DataLayoutPass anymore.

// DWD - Run the opt standard pass list as well.
AddStandardCompilePasses(Passes);
Expand Down
6 changes: 4 additions & 2 deletions lib/Module/RaiseAsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ bool RaiseAsmPass::runOnModule(Module &M) {
llvm::errs() << "Warning: unable to select native target: " << Err << "\n";
TLI = 0;
} else {

#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 6)
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
TM = NativeTarget->createTargetMachine(HostTriple, "", "", TargetOptions());
TLI = TM->getSubtargetImpl(*(M.begin()))->getTargetLowering();
#elif LLVM_VERSION_CODE >= LLVM_VERSION(3, 6)
TM = NativeTarget->createTargetMachine(HostTriple, "", "", TargetOptions());
TLI = TM->getSubtargetImpl()->getTargetLowering();
#elif LLVM_VERSION_CODE >= LLVM_VERSION(3, 1)
Expand Down
9 changes: 8 additions & 1 deletion tools/klee/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
#include "llvm/Support/system_error.h"
#endif

#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
#include "llvm/Support/Path.h"
#endif

#include <dirent.h>
#include <signal.h>
#include <unistd.h>
Expand Down Expand Up @@ -1282,8 +1286,11 @@ int main(int argc, char **argv, char **envp) {
// from the std::unique_ptr
Buffer->release();
}

#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 7)
mainModule = mainModuleOrError->release();
#else
mainModule = *mainModuleOrError;
#endif
if (auto ec = mainModule->materializeAllPermanently()) {
klee_error("error loading program '%s': %s", InputFile.c_str(),
ec.message().c_str());
Expand Down

0 comments on commit 7c16c63

Please sign in to comment.