From 2d50a58da7f1d7b70f39069de6a8a9c29033c26a Mon Sep 17 00:00:00 2001 From: guruhegde Date: Mon, 7 Oct 2019 10:10:18 +0200 Subject: [PATCH] Add helper methods --- src/Util.cpp | 32 ++++++++++++++++++++++++++++++++ src/Util.h | 9 +++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Util.cpp b/src/Util.cpp index dfa5306..72abf56 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -421,4 +421,36 @@ pair unifyFunctionExitNodes(Function &F) { return make_pair(NewRetBlock, UnreachableBlock); } +template +auto findFirstUseOfValueInInstType(Value * V) { + auto FU = find_if(V->users(), + [&] (const auto * U) { return isa(U)?true:false; }); + return cast(FU); +} + +template +auto findLastUseOfValueInInstType(Value * V) { + InstType * LU = nullptr; + for_each(V->users(), + [&] (const auto * U) { if (isa(U)) LU = cast(U); }); + return LU; +} + +AllocaInst * getLoopIndexVar(Loop * L) { + AllocaInst * Index = nullptr; + auto LatchBB = L->getLoopLatch(); + assert(LatchBB && "Latch block can't be null!"); + + // Find index increment instruction. + // XXX Assume latch block contains only one add instruction, verify it. + auto It = find_if(*LatchBB, + [&] (const Instruction & I) { + return (isa(I) && + I.getOpcode() == Instruction::Add && + cast(I.getOperand(1))->equalsInt(1)) ? true : false; }); + + Index = cast(cast(It->getOperand(0))->getOperand(0)); + return Index; +} + } // namespace tas diff --git a/src/Util.h b/src/Util.h index 8f0308f..2d153ec 100644 --- a/src/Util.h +++ b/src/Util.h @@ -78,6 +78,15 @@ llvm::Value * addIncrementIndexOp(llvm::AllocaInst * IdxPtr, void printRegeionInfo(llvm::Function * F); std::pair unifyFunctionExitNodes(llvm::Function & F); + +template +auto findFirstUseOfValueInInstType(llvm::Value * V); + +template +auto findLastUseOfValueInInstType(llvm::Value * V); + +llvm::AllocaInst * getLoopIndexVar(llvm::Loop * L); + } // namespace tas #endif -- GitLab