[llvm] 8a469e2 - [InstSimplify] Fold inbounds GEP to poison if base is undef.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 11 08:23:16 PST 2022


Author: Florian Hahn
Date: 2022-01-11T16:11:22Z
New Revision: 8a469e20505fd9b8daddd88227ff33888d6be821

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

LOG: [InstSimplify] Fold inbounds GEP to poison if base is undef.

D92270 updated constant expression folding to fold inbounds GEP to
poison if the base is undef. Apply the same logic to SimplifyGEPInst.

The justification is that we can choose an out-of-bounds pointer as base
pointer.

Reviewed By: nikic, lebedev.ri

Differential Revision: https://reviews.llvm.org/D117015

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/gep.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 8bdcb70a41708..1ec5c4e9f6b54 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4457,7 +4457,8 @@ static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, bool InBounds,
     return PoisonValue::get(GEPTy);
 
   if (Q.isUndefValue(Ops[0]))
-    return UndefValue::get(GEPTy);
+    // If inbounds, we can choose an out-of-bounds pointer as a base pointer.
+    return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy);
 
   bool IsScalableVec =
       isa<ScalableVectorType>(SrcTy) || any_of(Ops, [](const Value *V) {

diff  --git a/llvm/test/Transforms/InstSimplify/gep.ll b/llvm/test/Transforms/InstSimplify/gep.ll
index 4814291ddd672..66bcbd9be8667 100644
--- a/llvm/test/Transforms/InstSimplify/gep.ll
+++ b/llvm/test/Transforms/InstSimplify/gep.ll
@@ -160,7 +160,7 @@ define i8* @test7(i8* %b, i8** %e) {
 
 define i64* @undef_inbounds_var_idx(i64 %idx) {
 ; CHECK-LABEL: @undef_inbounds_var_idx(
-; CHECK-NEXT:    ret i64* undef
+; CHECK-NEXT:    ret i64* poison
 ;
   %el = getelementptr inbounds i64, i64* undef, i64 %idx
   ret i64* %el
@@ -176,7 +176,7 @@ define i64* @undef_no_inbounds_var_idx(i64 %idx) {
 
 define <8 x i64*> @undef_vec1() {
 ; CHECK-LABEL: @undef_vec1(
-; CHECK-NEXT:    ret <8 x i64*> undef
+; CHECK-NEXT:    ret <8 x i64*> poison
 ;
   %el = getelementptr inbounds i64, i64* undef, <8 x i64> undef
   ret <8 x i64*> %el


        


More information about the llvm-commits mailing list