Skip to content

Commit 140d6a9

Browse files
author
dbakunevich
committed
[SCEV][NFC] Introduce utility function to get power of 2
The new function has been added to SCEV that allows to raise the number 2 to the desired power. Authored-by: Dmitry Bakunevich Differential Revision: https://reviews.llvm.org/D144381
1 parent 4f9a544 commit 140d6a9

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,12 @@ class ScalarEvolution {
655655
/// Return a SCEV for the constant 1 of a specific type.
656656
const SCEV *getOne(Type *Ty) { return getConstant(Ty, 1); }
657657

658+
/// Return a SCEV for the constant \p Power of two.
659+
const SCEV *getPowerOfTwo(Type *Ty, unsigned Power) {
660+
assert(Power < getTypeSizeInBits(Ty) && "Power out of range");
661+
return getConstant(APInt::getOneBitSet(getTypeSizeInBits(Ty), Power));
662+
}
663+
658664
/// Return a SCEV for the constant -1 of a specific type.
659665
const SCEV *getMinusOne(Type *Ty) {
660666
return getConstant(Ty, -1, /*isSigned=*/true);

llvm/unittests/Analysis/ScalarEvolutionTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,4 +1744,20 @@ TEST_F(ScalarEvolutionsTest, ComputeMaxTripCountFromMultiDemArray) {
17441744
});
17451745
}
17461746

1747+
TEST_F(ScalarEvolutionsTest, CheckGetPowerOfTwo) {
1748+
Module M("CheckGetPowerOfTwo", Context);
1749+
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), {}, false);
1750+
Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M);
1751+
BasicBlock *Entry = BasicBlock::Create(Context, "entry", F);
1752+
IRBuilder<> Builder(Entry);
1753+
Builder.CreateRetVoid();
1754+
ScalarEvolution SE = buildSE(*F);
1755+
1756+
for (unsigned short i = 0; i < 64; ++i)
1757+
EXPECT_TRUE(
1758+
dyn_cast<SCEVConstant>(SE.getPowerOfTwo(Type::getInt64Ty(Context), i))
1759+
->getValue()
1760+
->equalsInt(1ULL << i));
1761+
}
1762+
17471763
} // end namespace llvm

0 commit comments

Comments
 (0)