[llvm] 9af8cc8 - [SimplifyLibCalls] Remove unnecessary inbounds check

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 11 07:51:18 PDT 2022


Author: Nikita Popov
Date: 2022-04-11T16:51:09+02:00
New Revision: 9af8cc8d1790a17506049f39be3272ff16022d58

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

LOG: [SimplifyLibCalls] Remove unnecessary inbounds check

Even if the GEP is not inbounds, the GEP will have provenance of
the global, and accessing past the extent of the global would be
undefined behavior.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/test/Transforms/InstCombine/strlen-1.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index dc11f55f1728d..803ea6a2f8d26 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -677,7 +677,7 @@ Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilderBase &B,
       // Offset is outside that range. That is the case when GEP->getOperand(0)
       // is a pointer to an object whose memory extent is NullTermIdx+1.
       if ((Known.isNonNegative() && Known.getMaxValue().ule(NullTermIdx)) ||
-          (GEP->isInBounds() && isa<GlobalVariable>(GEP->getOperand(0)) &&
+          (isa<GlobalVariable>(GEP->getOperand(0)) &&
            NullTermIdx == ArrSize - 1)) {
         Offset = B.CreateSExtOrTrunc(Offset, CI->getType());
         return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx),

diff  --git a/llvm/test/Transforms/InstCombine/strlen-1.ll b/llvm/test/Transforms/InstCombine/strlen-1.ll
index 0cfdc75269e3c..4f52b73b960fd 100644
--- a/llvm/test/Transforms/InstCombine/strlen-1.ll
+++ b/llvm/test/Transforms/InstCombine/strlen-1.ll
@@ -125,9 +125,8 @@ define i32 @test_simplify10_inbounds(i32 %x) {
 
 define i32 @test_simplify10_no_inbounds(i32 %x) {
 ; CHECK-LABEL: @test_simplify10_no_inbounds(
-; CHECK-NEXT:    [[HELLO_P:%.*]] = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 [[X:%.*]]
-; CHECK-NEXT:    [[HELLO_L:%.*]] = call i32 @strlen(i8* noundef nonnull dereferenceable(1) [[HELLO_P]])
-; CHECK-NEXT:    ret i32 [[HELLO_L]]
+; CHECK-NEXT:    [[TMP1:%.*]] = sub i32 5, [[X:%.*]]
+; CHECK-NEXT:    ret i32 [[TMP1]]
 ;
   %hello_p = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 %x
   %hello_l = call i32 @strlen(i8* %hello_p)


        


More information about the llvm-commits mailing list