[llvm] c41aa41 - [ConstFold] Add missing check for inbounds gep

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 6 00:59:49 PST 2022


Author: Nikita Popov
Date: 2022-01-06T09:59:40+01:00
New Revision: c41aa41957c102fdbe1e92c31fd1aec1c5fccbd5

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

LOG: [ConstFold] Add missing check for inbounds gep

If the gep is not inbounds, then the gep might compute a null
value even if the base pointer is non-null.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp
    llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 0d76dd732d61..9f1d76b0c768 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1537,7 +1537,7 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
         if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
           // If its not weak linkage, the GVal must have a non-zero address
           // so the result is greater-than
-          if (!GV->hasExternalWeakLinkage())
+          if (!GV->hasExternalWeakLinkage() && CE1GEP->isInBounds())
             return ICmpInst::ICMP_UGT;
         }
       } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {

diff  --git a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
index bac75d3788d5..67f007982a62 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
@@ -92,9 +92,10 @@ define i1 @global_sgt_null() {
   ret i1 %cmp
 }
 
+; Should not fold to true, as the gep computes a null value.
 define i1 @global_out_of_bounds_gep_ne_null() {
 ; CHECK-LABEL: @global_out_of_bounds_gep_ne_null(
-; CHECK-NEXT:    ret i1 true
+; CHECK-NEXT:    ret i1 icmp ne (i8* getelementptr (i8, i8* @g3, i64 sub (i64 0, i64 ptrtoint (i8* @g3 to i64))), i8* null)
 ;
   %cmp = icmp ne i8* getelementptr (i8, i8* @g3, i64 sub (i64 0, i64 ptrtoint (i8* @g3 to i64))), null
   ret i1 %cmp


        


More information about the llvm-commits mailing list