diff --git a/src/Util.cpp b/src/Util.cpp index dfa5306914fac5a17e3296ff55ca27fea43256e4..72abf56da0c67c34240a7d5b31f96c868443f4ce 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 8f0308f716df35855bad4754196fdc4661643504..2d153ec5f68ad7365633187fe776ace5c0a9dfd6 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