|
| 1 | +//===-- LocalVarDeclarationFinderPass.cpp -----------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// The KLEEF 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 | +#include "Passes.h" |
| 10 | + |
| 11 | +#include "llvm/ADT/SmallVector.h" |
| 12 | +#include "llvm/IR/Attributes.h" |
| 13 | +#include "llvm/IR/BasicBlock.h" |
| 14 | +#include "llvm/IR/DerivedTypes.h" |
| 15 | +#include "llvm/IR/Function.h" |
| 16 | +#include "llvm/IR/IRBuilder.h" |
| 17 | +#include "llvm/IR/Instruction.h" |
| 18 | +#include "llvm/IR/IntrinsicInst.h" |
| 19 | +#include "llvm/IR/Value.h" |
| 20 | +#include "llvm/IR/Verifier.h" |
| 21 | +#include "llvm/Support/Casting.h" |
| 22 | +#include "llvm/Support/raw_ostream.h" |
| 23 | +#include <cassert> |
| 24 | + |
| 25 | +#include <string> |
| 26 | +#include <unordered_map> |
| 27 | + |
| 28 | +using namespace klee; |
| 29 | + |
| 30 | +char DbgIntrinsicWrapperPass::ID = 0; |
| 31 | + |
| 32 | +std::string DbgIntrinsicWrapperPass::generateNewWrapperName() { |
| 33 | + return "_dbg_intrinsic_wrapper_#" + std::to_string(wrappedCounter++); |
| 34 | +} |
| 35 | + |
| 36 | +llvm::Function & |
| 37 | +DbgIntrinsicWrapperPass::wrapInFunction(llvm::DbgInfoIntrinsic &intrinsic) { |
| 38 | + std::string wrapperName = generateNewWrapperName(); |
| 39 | + |
| 40 | + auto &ctx = intrinsic.getContext(); |
| 41 | + auto &module = *intrinsic.getModule(); |
| 42 | + |
| 43 | + /* Generate function */ |
| 44 | + auto wrapperFunctionReturnType = |
| 45 | + llvm::FunctionType::get(llvm::Type::getVoidTy(ctx), {}, false); |
| 46 | + auto wrapperFunction = llvm::Function::Create(wrapperFunctionReturnType, |
| 47 | + llvm::Function::InternalLinkage, |
| 48 | + wrapperName, &module); |
| 49 | + |
| 50 | + wrapperFunction->addFnAttr(llvm::Attribute::OptimizeNone); |
| 51 | + wrapperFunction->addFnAttr(llvm::Attribute::NoInline); |
| 52 | + |
| 53 | + /* Generate function body */ |
| 54 | + auto wrapperBlock = llvm::BasicBlock::Create(ctx, "", wrapperFunction); |
| 55 | + |
| 56 | + llvm::IRBuilder<> builder(ctx); |
| 57 | + builder.SetInsertPoint(wrapperBlock); |
| 58 | + |
| 59 | + auto operands = intrinsic.getOperandList(); |
| 60 | + auto functionCallee = module.getOrInsertFunction( |
| 61 | + intrinsic.getCalledFunction()->getName(), |
| 62 | + intrinsic.getCalledFunction()->getFunctionType()); |
| 63 | + |
| 64 | + auto callToDbgInst = builder.CreateCall(functionCallee, operands->get()); |
| 65 | + callToDbgInst->setDebugLoc(intrinsic.getDebugLoc()); |
| 66 | + |
| 67 | + builder.CreateRetVoid(); |
| 68 | + |
| 69 | + assert(!llvm::verifyFunction(*wrapperFunction)); |
| 70 | + return *wrapperFunction; |
| 71 | +} |
| 72 | + |
| 73 | +bool DbgIntrinsicWrapperPass::runOnBasicBlock(llvm::BasicBlock &block) { |
| 74 | + bool isModified = false; |
| 75 | + |
| 76 | + auto &module = *block.getModule(); |
| 77 | + auto &ctx = block.getContext(); |
| 78 | + |
| 79 | + std::unordered_map<llvm::DbgLabelInst *, llvm::Function *> wrappers; |
| 80 | + |
| 81 | + for (auto &instruction : block) { |
| 82 | + if (auto intrinsic = |
| 83 | + llvm::dyn_cast_or_null<llvm::DbgLabelInst>(&instruction)) { |
| 84 | + |
| 85 | + isModified = true; |
| 86 | + auto &function = wrapInFunction(*intrinsic); |
| 87 | + wrappers.emplace(intrinsic, &function); |
| 88 | + addedWrappers.emplace(&function); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + llvm::IRBuilder<> builder(ctx); |
| 93 | + for (auto &[intrinsic, function] : wrappers) { |
| 94 | + auto iteratorAfterIntrinsic = intrinsic->eraseFromParent(); |
| 95 | + |
| 96 | + auto functionCallee = module.getOrInsertFunction(function->getName(), |
| 97 | + function->getReturnType()); |
| 98 | + |
| 99 | + auto wrapperCall = builder.CreateCall(functionCallee); |
| 100 | + wrapperCall->insertBefore(&*iteratorAfterIntrinsic); |
| 101 | + } |
| 102 | + |
| 103 | + return isModified; |
| 104 | +} |
| 105 | + |
| 106 | +bool DbgIntrinsicWrapperPass::runOnModule(llvm::Module &module) { |
| 107 | + bool isModified = false; |
| 108 | + |
| 109 | + for (auto &function : module) { |
| 110 | + if (addedWrappers.count(&function) != 0) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + for (auto &block : function) { |
| 115 | + isModified |= runOnBasicBlock(block); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return isModified; |
| 120 | +} |
0 commit comments