[llvm] 99d8bc9 - [InstCombine] Preserve all gep nowrap flags in ptradd canonicalization

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 1 07:46:10 PDT 2024


Author: Nikita Popov
Date: 2024-07-01T16:46:00+02:00
New Revision: 99d8bc9e7686994015fe744af3d11cd9c2050b8c

URL: https://github.com/llvm/llvm-project/commit/99d8bc9e7686994015fe744af3d11cd9c2050b8c
DIFF: https://github.com/llvm/llvm-project/commit/99d8bc9e7686994015fe744af3d11cd9c2050b8c.diff

LOG: [InstCombine] Preserve all gep nowrap flags in ptradd canonicalization

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/test/Transforms/InstCombine/canonicalize-gep-mul.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 82765b4a62bef..abcd94bea93e3 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2799,7 +2799,7 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
                                   m_Shl(m_Value(), m_ConstantInt())))))) {
     Value *Offset = EmitGEPOffset(cast<GEPOperator>(&GEP));
     return replaceInstUsesWith(
-        GEP, Builder.CreatePtrAdd(PtrOp, Offset, "", GEP.isInBounds()));
+        GEP, Builder.CreatePtrAdd(PtrOp, Offset, "", GEP.getNoWrapFlags()));
   }
 
   // Check to see if the inputs to the PHI node are getelementptr instructions.

diff  --git a/llvm/test/Transforms/InstCombine/canonicalize-gep-mul.ll b/llvm/test/Transforms/InstCombine/canonicalize-gep-mul.ll
index 7d917aac0aa4f..1b33e0c2087f1 100644
--- a/llvm/test/Transforms/InstCombine/canonicalize-gep-mul.ll
+++ b/llvm/test/Transforms/InstCombine/canonicalize-gep-mul.ll
@@ -15,6 +15,20 @@ entry:
   ret ptr %gep
 }
 
+define ptr @mul4_nuw(ptr %p, i64 %x) {
+; CHECK-LABEL: define ptr @mul4_nuw(
+; CHECK-SAME: ptr [[P:%.*]], i64 [[X:%.*]]) {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[GEP_IDX:%.*]] = shl i64 [[X]], 4
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr nuw i8, ptr [[P]], i64 [[GEP_IDX]]
+; CHECK-NEXT:    ret ptr [[GEP]]
+;
+entry:
+  %mul = mul i64 %x, 4
+  %gep = getelementptr nuw i32, ptr %p, i64 %mul
+  ret ptr %gep
+}
+
 define ptr @mul5(ptr %p, i64 %x) {
 ; CHECK-LABEL: define ptr @mul5(
 ; CHECK-SAME: ptr [[P:%.*]], i64 [[X:%.*]]) {


        


More information about the llvm-commits mailing list