klee
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"
13
14#include "llvm/CodeGen/TargetLowering.h"
15#include "llvm/CodeGen/TargetSubtargetInfo.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/IRBuilder.h"
18#include "llvm/IR/InlineAsm.h"
19#include "llvm/IR/Instructions.h"
20#include "llvm/IR/LLVMContext.h"
21#include "llvm/Support/Host.h"
22#include "llvm/Support/TargetRegistry.h"
23#include "llvm/Target/TargetMachine.h"
24
25
26using namespace llvm;
27using namespace klee;
28
29char RaiseAsmPass::ID = 0;
30
31Function *RaiseAsmPass::getIntrinsic(llvm::Module &M, unsigned IID, Type **Tys,
32 unsigned NumTys) {
33 return Intrinsic::getDeclaration(&M, (llvm::Intrinsic::ID) IID,
34 llvm::ArrayRef<llvm::Type*>(Tys, NumTys));
35}
36
37// FIXME: This should just be implemented as a patch to
38// X86TargetAsmInfo.cpp, then everyone will benefit.
39bool RaiseAsmPass::runOnInstruction(Module &M, Instruction *I) {
40 // We can just raise inline assembler using calls
41 CallInst *ci = dyn_cast<CallInst>(I);
42 if (!ci)
43 return false;
44
45#if LLVM_VERSION_CODE >= LLVM_VERSION(8, 0)
46 InlineAsm *ia = dyn_cast<InlineAsm>(ci->getCalledOperand());
47#else
48 InlineAsm *ia = dyn_cast<InlineAsm>(ci->getCalledValue());
49#endif
50 if (!ia)
51 return false;
52
53 // Try to use existing infrastructure
54 if (!TLI)
55 return false;
56
57 if (TLI->ExpandInlineAsm(ci))
58 return true;
59
60 if ((triple.getArch() == llvm::Triple::x86 ||
61 triple.getArch() == llvm::Triple::x86_64) &&
62 (triple.isOSLinux() || triple.isMacOSX() || triple.isOSFreeBSD())) {
63
64 if (ia->getAsmString() == "" && ia->hasSideEffects() &&
65 ia->getFunctionType()->getReturnType()->isVoidTy()) {
66 IRBuilder<> Builder(I);
67 Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent);
68 I->eraseFromParent();
69 return true;
70 }
71 }
72
73 return false;
74}
75
77 bool changed = false;
78 std::string Err;
79
80 // Use target triple from the module if possible.
81 std::string TargetTriple = M.getTargetTriple();
82 if (TargetTriple.empty())
83 TargetTriple = llvm::sys::getDefaultTargetTriple();
84 const Target *Target = TargetRegistry::lookupTarget(TargetTriple, Err);
85
86 TargetMachine * TM = 0;
87 if (Target == 0) {
88 klee_warning("Warning: unable to select target: %s", Err.c_str());
89 TLI = 0;
90 } else {
91 TM = Target->createTargetMachine(TargetTriple, "", "", TargetOptions(),
92 None);
93 TLI = TM->getSubtargetImpl(*(M.begin()))->getTargetLowering();
94
95 triple = llvm::Triple(TargetTriple);
96 }
97
98 for (Module::iterator fi = M.begin(), fe = M.end(); fi != fe; ++fi) {
99 for (Function::iterator bi = fi->begin(), be = fi->end(); bi != be; ++bi) {
100 for (BasicBlock::iterator ii = bi->begin(), ie = bi->end(); ii != ie;) {
101 Instruction *i = &*ii;
102 ++ii;
103 changed |= runOnInstruction(M, i);
104 }
105 }
106 }
107
108 delete TM;
109
110 return changed;
111}
llvm::Triple triple
Definition: Passes.h:40
bool runOnModule(llvm::Module &M) override
Definition: RaiseAsm.cpp:76
bool runOnInstruction(llvm::Module &M, llvm::Instruction *I)
Definition: RaiseAsm.cpp:39
const llvm::TargetLowering * TLI
Definition: Passes.h:38
Definition: main.cpp:291
void void void klee_warning(const char *msg,...) __attribute__((format(printf