ModuleUtil.cpp

Go to the documentation of this file.
00001 //===-- ModuleUtil.cpp ----------------------------------------------------===//
00002 //
00003 //                     The KLEE Symbolic Virtual Machine
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
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     // NOTE: This assert may fire, it isn't necessarily a problem and
00058     // can be disabled, I just wanted to know when and if it happened.
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; // XXX function numbering inst
00070       if (!isa<CallInst>(instr) && !isa<InvokeInst>(instr)) return false;
00071       
00072       // Make sure that the value is only the target of this call and
00073       // not an argument.
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       // XXX what about v is bitcast of aliasee?
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 }

Generated on Fri Jun 5 03:31:32 2009 for klee by  doxygen 1.5.8