[llvm] 9d96d01 - [IR] Add offset stripping test with mixed const/variable offsets (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 12 06:12:24 PDT 2025


Author: Nikita Popov
Date: 2025-08-12T15:12:16+02:00
New Revision: 9d96d01b42b9b11b375dca3927586f8962404e47

URL: https://github.com/llvm/llvm-project/commit/9d96d01b42b9b11b375dca3927586f8962404e47
DIFF: https://github.com/llvm/llvm-project/commit/9d96d01b42b9b11b375dca3927586f8962404e47.diff

LOG: [IR] Add offset stripping test with mixed const/variable offsets (NFC)

Regression test for:
https://github.com/llvm/llvm-project/commit/a7edc95c799c46665ecf4465a4dc7ff4bee3ced0#commitcomment-163691175

Added: 
    

Modified: 
    llvm/unittests/IR/InstructionsTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/IR/InstructionsTest.cpp b/llvm/unittests/IR/InstructionsTest.cpp
index fd8838e06f34c..21d45960dce7c 100644
--- a/llvm/unittests/IR/InstructionsTest.cpp
+++ b/llvm/unittests/IR/InstructionsTest.cpp
@@ -1935,5 +1935,22 @@ TEST(InstructionsTest, CmpPredicate) {
   EXPECT_EQ(P2, R2);
 }
 
+TEST(InstructionsTest, StripAndAccumulateConstantOffset) {
+  LLVMContext C;
+  DataLayout DL;
+  std::unique_ptr<Module> M = parseIR(C, R"(
+  define void @foo(ptr %ptr, i64 %offset) {
+    %gep = getelementptr inbounds [1 x i8], ptr %ptr, i64 4, i64 %offset
+    ret void
+  })");
+  ASSERT_TRUE(M);
+  Value *GEP = &M->getFunction("foo")->getEntryBlock().front();
+  APInt Offset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
+  Value *Stripped = GEP->stripAndAccumulateConstantOffsets(
+      DL, Offset, /*AllowNonInBounds=*/true);
+  EXPECT_EQ(Stripped, GEP);
+  EXPECT_TRUE(Offset.isZero());
+}
+
 } // end anonymous namespace
 } // end namespace llvm


        


More information about the llvm-commits mailing list