[PATCH] D144381: [SCEV] Added a util function to SCEV

Dmitry Bakunevich via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 20 04:26:35 PST 2023


dbakunevich created this revision.
dbakunevich added a reviewer: mkazantsev.
Herald added subscribers: StephenFan, javed.absar.
Herald added a project: All.
dbakunevich requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The new function has been added to SCEV that allows
to raise the number 2 to the desired power.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144381

Files:
  llvm/include/llvm/Analysis/ScalarEvolution.h
  llvm/unittests/Analysis/ScalarEvolutionTest.cpp


Index: llvm/unittests/Analysis/ScalarEvolutionTest.cpp
===================================================================
--- llvm/unittests/Analysis/ScalarEvolutionTest.cpp
+++ llvm/unittests/Analysis/ScalarEvolutionTest.cpp
@@ -1744,4 +1744,27 @@
   });
 }
 
+TEST_F(ScalarEvolutionsTest, CheckGetPowerOfTwo) {
+  LLVMContext C;
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M = parseAssemblyString("define void @foo(i32 %x) { "
+                                                  "  ret void "
+                                                  "} ",
+                                                  Err, C);
+
+  ASSERT_TRUE(M && "Could not parse module?");
+  ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
+
+  runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
+    auto *X = SE.getSCEV(getArgByName(F, "x"));
+    Type *Ty = X->getType();
+    const SCEV *TwoPowerThree = SE.getPowerOfTwo(Ty, 3);
+    const SCEV *TwoPowerFour = SE.getPowerOfTwo(Ty, 4);
+    auto *Sum = SE.getAddExpr(TwoPowerThree, TwoPowerThree, SCEV::FlagNUW);
+    EXPECT_TRUE(
+        SE.isKnownPredicate(ICmpInst::ICMP_ULT, TwoPowerThree, TwoPowerFour));
+    EXPECT_TRUE(SE.isKnownPredicate(ICmpInst::ICMP_EQ, Sum, TwoPowerFour));
+  });
+}
+
 }  // end namespace llvm
Index: llvm/include/llvm/Analysis/ScalarEvolution.h
===================================================================
--- llvm/include/llvm/Analysis/ScalarEvolution.h
+++ llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -655,6 +655,11 @@
   /// Return a SCEV for the constant 1 of a specific type.
   const SCEV *getOne(Type *Ty) { return getConstant(Ty, 1); }
 
+  /// Return a SCEV for the constant 2 with \p Degree power of a specific type.
+  const SCEV *getPowerOfTwo(Type *Ty, long long Degree) {
+    return getConstant(Ty, 1 << Degree);
+  }
+
   /// Return a SCEV for the constant -1 of a specific type.
   const SCEV *getMinusOne(Type *Ty) {
     return getConstant(Ty, -1, /*isSigned=*/true);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144381.498798.patch
Type: text/x-patch
Size: 2002 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230220/39b6fd0f/attachment.bin>


More information about the llvm-commits mailing list