Commit e125d84c authored by Adéla Štěpková's avatar Adéla Štěpková
Browse files

use domination instead of reachability to find out if a value is needed

parent aba53008
Loading
Loading
Loading
Loading
+22 −1
Original line number Diff line number Diff line
@@ -5,11 +5,31 @@
#include "blockReachabilityInfo.hpp"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Dominators.h"
#include <unordered_map>
#include <vector>
#include <optional>

namespace llvm {

struct DomTreeWrapper {
    std::optional<DominatorTree> DT;

    DomTreeWrapper(Function& F) : DT()
    {
        if (!F.isDeclaration()) {
            DT = DominatorTree(F);
        }
    }

    bool dominates(BasicBlock* BB1, BasicBlock* BB2) {
        if (DT.has_value()) {
            return DT.value().dominates(BB1, BB2);
        }
        return false;
    }
};

struct FunctionBundle {
    Function* origFunction;
    Function* revFunction;
@@ -21,7 +41,8 @@ struct FunctionBundle {
    DenseMap<Instruction*, Instruction*> directAsserts; // calls to assert in the original function
    DenseMap<Instruction*, Instruction*> calledAsserts; // calls to functions with assert inside
    DenseMap<BasicBlock*, bool> BBscontainAssert;       // BBs from orig program
    BlockReachabilityInfo reachableBlocks; 
    // BlockReachabilityInfo reachableBlocks; 
    DomTreeWrapper origDT;

    FunctionBundle(Function* origFunction, Function* reversedFunction, Module& origModule,
                   HelperFunctionsBuilder& functionsBuilder);
+2 −4
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ FunctionBundle::FunctionBundle(Function* origFunction, Function* reversedFunctio
    : origFunction(origFunction),
      revFunction(reversedFunction),
      instBuilder(functionsBuilder, *this, origFunction->size() + 2),
      reachableBlocks(origFunction)
      origDT(*origFunction)
{
    for (auto& BB : *origFunction) {
        BBscontainAssert[&BB] = false;
@@ -30,7 +30,7 @@ FunctionBundle::FunctionBundle(FunctionBundle&& other) noexcept
      directAsserts(std::move(other.directAsserts)),
      calledAsserts(std::move(other.calledAsserts)),
      BBscontainAssert(std::move(other.BBscontainAssert)),
      reachableBlocks(other.origFunction)
      origDT(std::move(other.origDT))
{
    other.origFunction = nullptr;
    other.revFunction = nullptr;
@@ -101,7 +101,5 @@ FunctionBundle FunctionBundle::duplicate()

    copyFBundle.BBscontainAssert = BBscontainAssert;

    copyFBundle.reachableBlocks = reachableBlocks;

    return copyFBundle;
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -633,7 +633,7 @@ bool ValueBuilder::isValueNeededInRevBlock(Value* origVal, BasicBlock* origBB, B
    }

    if (auto* I = dyn_cast<Instruction>(origVal)) {
        return _fBundle.reachableBlocks.isReachable(I->getParent(), origBB);
        return I->getParent() == origBB || _fBundle.origDT.dominates(I->getParent(), origBB);
    }

    return false;