klee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RaiseAsm.cpp
Go to the documentation of this file.
1 //===-- RaiseAsm.cpp ------------------------------------------------------===//
2 //
3 // The KLEE Symbolic Virtual Machine
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Passes.h"
11 #include "klee/Config/Version.h"
12 #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3)
13 #include "llvm/IR/InlineAsm.h"
14 #include "llvm/IR/LLVMContext.h"
15 #else
16 #include "llvm/InlineAsm.h"
17 #include "llvm/LLVMContext.h"
18 #endif
19 
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/Host.h"
22 #include "llvm/Target/TargetLowering.h"
23 #if LLVM_VERSION_CODE < LLVM_VERSION(3, 0)
24 #include "llvm/Target/TargetRegistry.h"
25 #else
26 #include "llvm/Support/TargetRegistry.h"
27 #endif
28 
29 using namespace llvm;
30 using namespace klee;
31 
32 char RaiseAsmPass::ID = 0;
33 
34 Function *RaiseAsmPass::getIntrinsic(llvm::Module &M,
35  unsigned IID,
36  LLVM_TYPE_Q Type **Tys,
37  unsigned NumTys) {
38 #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 0)
39  return Intrinsic::getDeclaration(&M, (llvm::Intrinsic::ID) IID,
40  llvm::ArrayRef<llvm::Type*>(Tys, NumTys));
41 #else
42  return Intrinsic::getDeclaration(&M, (llvm::Intrinsic::ID) IID, Tys, NumTys);
43 #endif
44 }
45 
46 // FIXME: This should just be implemented as a patch to
47 // X86TargetAsmInfo.cpp, then everyone will benefit.
48 bool RaiseAsmPass::runOnInstruction(Module &M, Instruction *I) {
49  if (CallInst *ci = dyn_cast<CallInst>(I)) {
50  if (InlineAsm *ia = dyn_cast<InlineAsm>(ci->getCalledValue())) {
51  (void) ia;
52  return TLI && TLI->ExpandInlineAsm(ci);
53  }
54  }
55 
56  return false;
57 }
58 
59 bool RaiseAsmPass::runOnModule(Module &M) {
60  bool changed = false;
61 
62  std::string Err;
63 #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 1)
64  std::string HostTriple = llvm::sys::getDefaultTargetTriple();
65 #else
66  std::string HostTriple = llvm::sys::getHostTriple();
67 #endif
68  const Target *NativeTarget = TargetRegistry::lookupTarget(HostTriple, Err);
69  if (NativeTarget == 0) {
70  llvm::errs() << "Warning: unable to select native target: " << Err << "\n";
71  TLI = 0;
72  } else {
73 #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 1)
74  TargetMachine *TM = NativeTarget->createTargetMachine(HostTriple, "", "",
75  TargetOptions());
76 #elif LLVM_VERSION_CODE >= LLVM_VERSION(3, 0)
77  TargetMachine *TM = NativeTarget->createTargetMachine(HostTriple, "", "");
78 #else
79  TargetMachine *TM = NativeTarget->createTargetMachine(HostTriple, "");
80 #endif
81  TLI = TM->getTargetLowering();
82  }
83 
84  for (Module::iterator fi = M.begin(), fe = M.end(); fi != fe; ++fi) {
85  for (Function::iterator bi = fi->begin(), be = fi->end(); bi != be; ++bi) {
86  for (BasicBlock::iterator ii = bi->begin(), ie = bi->end(); ii != ie;) {
87  Instruction *i = ii;
88  ++ii;
89  changed |= runOnInstruction(M, i);
90  }
91  }
92  }
93 
94  return changed;
95 }
#define LLVM_TYPE_Q
Definition: Version.h:21