ModuleUtil.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "klee/Internal/Support/ModuleUtil.h"
00011
00012 #include "llvm/Function.h"
00013 #include "llvm/Instructions.h"
00014 #include "llvm/IntrinsicInst.h"
00015 #include "llvm/Linker.h"
00016 #include "llvm/Module.h"
00017 #include "llvm/Assembly/AsmAnnotationWriter.h"
00018 #include "llvm/Support/CFG.h"
00019 #include "llvm/Support/InstIterator.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 #include "llvm/Analysis/ValueTracking.h"
00022
00023 #include <map>
00024 #include <iostream>
00025 #include <fstream>
00026 #include <sstream>
00027 #include <string>
00028
00029 using namespace llvm;
00030 using namespace klee;
00031
00032 Module *klee::linkWithLibrary(Module *module,
00033 const std::string &libraryName) {
00034 Linker linker("klee", module, false);
00035
00036 llvm::sys::Path libraryPath(libraryName);
00037 bool native = false;
00038
00039 if (linker.LinkInFile(libraryPath, native)) {
00040 assert(0 && "linking in library failed!");
00041 }
00042
00043 return linker.releaseModule();
00044 }
00045
00046 Function *klee::getDirectCallTarget(const Instruction *i) {
00047 assert(isa<CallInst>(i) || isa<InvokeInst>(i));
00048
00049 Value *v = i->getOperand(0);
00050 if (Function *f = dyn_cast<Function>(v)) {
00051 return f;
00052 } else if (llvm::ConstantExpr *ce = dyn_cast<llvm::ConstantExpr>(v)) {
00053 if (ce->getOpcode()==Instruction::BitCast)
00054 if (Function *f = dyn_cast<Function>(ce->getOperand(0)))
00055 return f;
00056
00057
00058
00059 assert(0 && "FIXME: Unresolved direct target for a constant expression.");
00060 }
00061
00062 return 0;
00063 }
00064
00065 static bool valueIsOnlyCalled(const Value *v) {
00066 for (Value::use_const_iterator it = v->use_begin(), ie = v->use_end();
00067 it != ie; ++it) {
00068 if (const Instruction *instr = dyn_cast<Instruction>(*it)) {
00069 if (instr->getOpcode()==0) continue;
00070 if (!isa<CallInst>(instr) && !isa<InvokeInst>(instr)) return false;
00071
00072
00073
00074 for (unsigned i=1,e=instr->getNumOperands(); i!=e; ++i)
00075 if (instr->getOperand(i)==v)
00076 return false;
00077 } else if (const llvm::ConstantExpr *ce =
00078 dyn_cast<llvm::ConstantExpr>(*it)) {
00079 if (ce->getOpcode()==Instruction::BitCast)
00080 if (valueIsOnlyCalled(ce))
00081 continue;
00082 return false;
00083 } else if (const GlobalAlias *ga = dyn_cast<GlobalAlias>(*it)) {
00084
00085 if (v==ga->getAliasee() && !valueIsOnlyCalled(ga))
00086 return false;
00087 } else {
00088 return false;
00089 }
00090 }
00091
00092 return true;
00093 }
00094
00095 bool klee::functionEscapes(const Function *f) {
00096 return !valueIsOnlyCalled(f);
00097 }