diff options
author | Martin Nowack <m.nowack@imperial.ac.uk> | 2019-03-16 23:27:20 +0000 |
---|---|---|
committer | Cristian Cadar <c.cadar@imperial.ac.uk> | 2019-03-18 20:29:35 +0000 |
commit | 842966d5f2921e807ddc348505ee710eba3ad86d (patch) | |
tree | 847f010a578f011e787def078dc33a6f42e48aad /lib/Module/OptNone.cpp | |
parent | 33394de6b4f14a7677e7c470be80929c1192b98e (diff) | |
download | klee-842966d5f2921e807ddc348505ee710eba3ad86d.tar.gz |
Disable optimisation for functions that contain KLEE calls
Compilers are allowed to hoist function calls and do GVE. This is currently done even without `--optimization` enabled. This is unfortunate in the context of KLEE function calls that might depend on specific code position without direct control flow dependencies. In such cases, function calls can be hoisted. To circumvent this, disallow to optimise functions that contain such calls by default. This might reduce optimisation for some functions containing such function calls but still allows it for all others. This patch adds an additional pass, that detects all functions starting with a prefix `klee_` and disable optimisations for functions containing such calls. This is enabled by default but can be disabled by `--klee-call-optimisation=false`.
Diffstat (limited to 'lib/Module/OptNone.cpp')
-rw-r--r-- | lib/Module/OptNone.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/Module/OptNone.cpp b/lib/Module/OptNone.cpp new file mode 100644 index 00000000..08837488 --- /dev/null +++ b/lib/Module/OptNone.cpp @@ -0,0 +1,59 @@ +//===-- OptNone.cpp -------------------------------------------------------===// +// +// The KLEE Symbolic Virtual Machine +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "Passes.h" + +#include "klee/Config/Version.h" + +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/Instruction.h" +#include "llvm/IR/Module.h" + +namespace klee { + +char OptNonePass::ID; + +bool OptNonePass::runOnModule(llvm::Module &M) { + // Find list of functions that start with `klee_` + // and mark all functions that contain such call or invoke as optnone + llvm::SmallPtrSet<llvm::Function *,16> CallingFunctions; + for (auto &F : M) { + if (!F.hasName() || !F.getName().startswith("klee_")) + continue; +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 5) + for (auto *U : F.users()) { + // skip non-calls and non-invokes + if (!llvm::isa<llvm::CallInst>(U) && !llvm::isa<llvm::InvokeInst>(U)) + continue; + auto *Inst = llvm::cast<llvm::Instruction>(U); + CallingFunctions.insert(Inst->getParent()->getParent()); + } +#else + for (auto i = F.use_begin(), e = F.use_end(); i != e; ++i) { + if (auto Inst = llvm::dyn_cast<llvm::Instruction>(*i)) { + CallingFunctions.insert(Inst->getParent()->getParent()); + } + } +#endif + } + + bool changed = false; + for (auto F : CallingFunctions) { + // Skip if already annotated + if (F->hasFnAttribute(llvm::Attribute::OptimizeNone)) + continue; + F->addFnAttr(llvm::Attribute::OptimizeNone); + F->addFnAttr(llvm::Attribute::NoInline); + changed = true; + } + + return changed; +} +} // namespace klee |