about summary refs log tree commit diff homepage
path: root/lib/Module/OptNone.cpp
blob: 285fac5cf13ee3426470049ffae6fddc2e0f3653 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//===-- 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;
    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());
    }
  }

  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