[llvm] [InstCombine] Fold gep of exact unsigned division (PR #82334)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 20 02:13:13 PST 2024


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/82334

Extend the transform added in https://github.com/llvm/llvm-project/pull/76458 to also handle unsigned division. X exact/ Y * Y == X holds independently of whether the division is signed or unsigned.

Proofs: https://alive2.llvm.org/ce/z/wFd5Ec

>From 5e3b888ccc1a47f7c2e9d8536f65353515efe437 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 20 Feb 2024 11:04:44 +0100
Subject: [PATCH] [InstCombine] Fold gep of exact unsigned division

Extend the transform added in https://github.com/llvm/llvm-project/pull/76458
to also handle unsigned division. X exact/ Y * Y == X holds
independently of whether the division is signed or unsigned.

Proofs: https://alive2.llvm.org/ce/z/wFd5Ec
---
 llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 6 +++---
 llvm/test/Transforms/InstCombine/getelementptr.ll        | 6 ++----
 2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 7450f39c1e7641..4dbb79f7bef887 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2615,10 +2615,10 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
         Value *V;
         if ((has_single_bit(TyAllocSize) &&
              match(GEP.getOperand(1),
-                   m_Exact(m_AShr(m_Value(V),
-                                  m_SpecificInt(countr_zero(TyAllocSize)))))) ||
+                   m_Exact(m_Shr(m_Value(V),
+                                 m_SpecificInt(countr_zero(TyAllocSize)))))) ||
             match(GEP.getOperand(1),
-                  m_Exact(m_SDiv(m_Value(V), m_SpecificInt(TyAllocSize))))) {
+                  m_Exact(m_IDiv(m_Value(V), m_SpecificInt(TyAllocSize))))) {
           GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
               Builder.getInt8Ty(), GEP.getPointerOperand(), V);
           NewGEP->setIsInBounds(GEP.isInBounds());
diff --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index a9c619f3fddd05..04b0c196ab5108 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -1477,8 +1477,7 @@ define ptr @gep_sdiv(ptr %p, i64 %off) {
 
 define ptr @gep_udiv(ptr %p, i64 %off) {
 ; CHECK-LABEL: @gep_udiv(
-; CHECK-NEXT:    [[INDEX:%.*]] = udiv exact i64 [[OFF:%.*]], 7
-; CHECK-NEXT:    [[PTR:%.*]] = getelementptr [[STRUCT_C:%.*]], ptr [[P:%.*]], i64 [[INDEX]]
+; CHECK-NEXT:    [[PTR:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[OFF:%.*]]
 ; CHECK-NEXT:    ret ptr [[PTR]]
 ;
   %index = udiv exact i64 %off, 7
@@ -1518,8 +1517,7 @@ define ptr @gep_ashr(ptr %p, i64 %off) {
 
 define ptr @gep_lshr(ptr %p, i64 %off) {
 ; CHECK-LABEL: @gep_lshr(
-; CHECK-NEXT:    [[INDEX:%.*]] = lshr exact i64 [[OFF:%.*]], 2
-; CHECK-NEXT:    [[PTR:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
+; CHECK-NEXT:    [[PTR:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[OFF:%.*]]
 ; CHECK-NEXT:    ret ptr [[PTR]]
 ;
   %index = lshr exact i64 %off, 2



More information about the llvm-commits mailing list