factor out a couple of llvm tooling functions
MODIFY
llvm_util/utils.cpp
llvm_util/utils.cpp
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/SourceMgr.h"
#include <unordered_map>
#include <utility>
#include <vector>
@@ -445,4 +449,33 @@ void reset_state(Function &f) {
current_fn = &f;
}
+static llvm::ExitOnError ExitOnErr;
+
+// adapted from llvm-dis.cpp
+std::unique_ptr<llvm::Module> openInputFile(llvm::LLVMContext &Context,
+ const string &InputFilename) {
+ auto MB =
+ ExitOnErr(errorOrToExpected(llvm::MemoryBuffer::getFile(InputFilename)));
+ llvm::SMDiagnostic Diag;
+ auto M = getLazyIRModule(std::move(MB), Diag, Context,
+ /*ShouldLazyLoadMetadata=*/true);
+ if (!M) {
+ Diag.print("", llvm::errs(), false);
+ return 0;
+ }
+ ExitOnErr(M->materializeAll());
+ return M;
+}
+
+llvm::Function *findFunction(llvm::Module &M, const string &FName) {
+ for (auto &F : M) {
+ if (F.isDeclaration())
+ continue;
+ if (FName.compare(F.getName()) != 0)
+ continue;
+ return &F;
+ }
+ return nullptr;
+}
+
}

MODIFY
llvm_util/utils.h
llvm_util/utils.h
@@ -15,6 +15,9 @@ class DataLayout;
class Instruction;
class Type;
class Value;
+class Module;
+class LLVMContext;
+class Function;
}
namespace IR {
@@ -55,4 +58,9 @@ void set_outs(std::ostream &os);
void reset_state();
void reset_state(IR::Function &f);
+
+std::unique_ptr<llvm::Module> openInputFile(llvm::LLVMContext &Context,
+ const std::string &InputFilename);
+llvm::Function *findFunction(llvm::Module &M, const std::string &FName);
+
}

MODIFY
tools/alive-exec.cpp
tools/alive-exec.cpp
#include "cache/cache.h"
#include "ir/type.h"
#include "llvm_util/llvm2alive.h"
+#include "llvm_util/utils.h"
#include "smt/expr.h"
#include "smt/smt.h"
#include "smt/solver.h"
@@ -46,25 +47,6 @@ llvm::cl::opt<string> opt_file(llvm::cl::Positional,
llvm::cl::desc("bitcode_file"), llvm::cl::Required,
llvm::cl::value_desc("filename"), llvm::cl::cat(alive_cmdargs));
-
-llvm::ExitOnError ExitOnErr;
-
-// adapted from llvm-dis.cpp
-std::unique_ptr<llvm::Module> openInputFile(llvm::LLVMContext &Context,
- const string &InputFilename) {
- auto MB =
- ExitOnErr(errorOrToExpected(llvm::MemoryBuffer::getFile(InputFilename)));
- llvm::SMDiagnostic Diag;
- auto M = getLazyIRModule(std::move(MB), Diag, Context,
- /*ShouldLazyLoadMetadata=*/true);
- if (!M) {
- Diag.print("", llvm::errs(), false);
- return 0;
- }
- ExitOnErr(M->materializeAll());
- return M;
-}
-
optional<smt::smt_initializer> smt_init;
unique_ptr<Cache> cache;

MODIFY
tools/alive-tv.cpp
tools/alive-tv.cpp
#include "llvm_util/compare.h"
#include "llvm_util/llvm2alive.h"
#include "llvm_util/llvm_optimizer.h"
+#include "llvm_util/utils.h"
#include "smt/smt.h"
#include "tools/transform.h"
#include "util/version.h"
@@ -70,34 +71,6 @@ llvm::cl::opt<string>
llvm::cl::cat(alive_cmdargs), llvm::cl::init("O2"));
-llvm::ExitOnError ExitOnErr;
-
-// adapted from llvm-dis.cpp
-std::unique_ptr<llvm::Module> openInputFile(llvm::LLVMContext &Context,
- const string &InputFilename) {
- auto MB =
- ExitOnErr(errorOrToExpected(llvm::MemoryBuffer::getFile(InputFilename)));
- llvm::SMDiagnostic Diag;
- auto M = getLazyIRModule(std::move(MB), Diag, Context,
- /*ShouldLazyLoadMetadata=*/true);
- if (!M) {
- Diag.print("", llvm::errs(), false);
- return 0;
- }
- ExitOnErr(M->materializeAll());
- return M;
-}
-
-llvm::Function *findFunction(llvm::Module &M, const string &FName) {
- for (auto &F : M) {
- if (F.isDeclaration())
- continue;
- if (FName.compare(F.getName()) != 0)
- continue;
- return &F;
- }
- return 0;
-}
}
unique_ptr<Cache> cache;

factor out a couple of llvm tooling functions (#876)