[llvm] c038845 - [InstCombine] Fold icmp (select c,const,arg), null if icmp arg, null can be simplified

Juneyoung Lee via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 21 01:39:18 PDT 2021


Author: Juneyoung Lee
Date: 2021-06-21T17:39:05+09:00
New Revision: c038845f58a84f03c88a03ab6a59014b3d6c180b

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

LOG: [InstCombine] Fold icmp (select c,const,arg), null if icmp arg, null can be simplified

This patch folds icmp (select c,const,arg), null if icmp arg, null can be simplified.

Resolves llvm.org/pr48975.

Reviewed By: nikic, xbolva00

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

Added: 
    llvm/test/Transforms/InstCombine/assume-icmp-null-select.ll

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 189b49e584c2..3e8526c92358 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -3290,14 +3290,24 @@ Instruction *InstCombinerImpl::foldICmpInstWithConstantNotInt(ICmpInst &I) {
     // constant folded and the select turned into a bitwise or.
     Value *Op1 = nullptr, *Op2 = nullptr;
     ConstantInt *CI = nullptr;
-    if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
-      Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
+
+    auto SimplifyOp = [&](Value *V) {
+      Value *Op = nullptr;
+      if (Constant *C = dyn_cast<Constant>(V)) {
+        Op = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
+      } else if (RHSC->isNullValue()) {
+        // If null is being compared, check if it can be further simplified.
+        Op = SimplifyICmpInst(I.getPredicate(), V, RHSC, SQ);
+      }
+      return Op;
+    };
+    Op1 = SimplifyOp(LHSI->getOperand(1));
+    if (Op1)
       CI = dyn_cast<ConstantInt>(Op1);
-    }
-    if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
-      Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
+
+    Op2 = SimplifyOp(LHSI->getOperand(2));
+    if (Op2)
       CI = dyn_cast<ConstantInt>(Op2);
-    }
 
     // We only want to perform this transformation if it will not lead to
     // additional code. This is true if either both sides of the select

diff  --git a/llvm/test/Transforms/InstCombine/assume-icmp-null-select.ll b/llvm/test/Transforms/InstCombine/assume-icmp-null-select.ll
new file mode 100644
index 000000000000..dd433f169ba0
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/assume-icmp-null-select.ll
@@ -0,0 +1,51 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instcombine -S | FileCheck %s
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i8* @example(i8* dereferenceable(24) %x) {
+; CHECK-LABEL: @example(
+; CHECK-NEXT:    [[X2:%.*]] = bitcast i8* [[X:%.*]] to {}**
+; CHECK-NEXT:    [[Y:%.*]] = load {}*, {}** [[X2]], align 8
+; CHECK-NEXT:    [[Y_IS_NULL:%.*]] = icmp ne {}* [[Y]], null
+; CHECK-NEXT:    call void @llvm.assume(i1 [[Y_IS_NULL]])
+; CHECK-NEXT:    ret i8* [[X]]
+;
+  %x2 = bitcast i8* %x to {}**
+  %y = load {}*, {}** %x2, align 8
+  %y_is_null = icmp eq {}* %y, null
+
+  %x0 = getelementptr inbounds i8, i8* %x, i64 0
+  %res = select i1 %y_is_null, i8* null, i8* %x0
+
+  %nonnull = icmp ne i8* %res, null
+  call void @llvm.assume(i1 %nonnull)
+
+  ret i8* %res
+}
+
+; TODO: this should be folded to `ret i8* %x` as well.
+define i8* @example2(i8* %x) {
+; CHECK-LABEL: @example2(
+; CHECK-NEXT:    [[X2:%.*]] = bitcast i8* [[X:%.*]] to {}**
+; CHECK-NEXT:    [[Y:%.*]] = load {}*, {}** [[X2]], align 8
+; CHECK-NEXT:    [[Y_IS_NULL:%.*]] = icmp eq {}* [[Y]], null
+; CHECK-NEXT:    [[RES:%.*]] = select i1 [[Y_IS_NULL]], i8* null, i8* [[X]]
+; CHECK-NEXT:    [[NONNULL:%.*]] = icmp ne i8* [[RES]], null
+; CHECK-NEXT:    call void @llvm.assume(i1 [[NONNULL]])
+; CHECK-NEXT:    ret i8* [[RES]]
+;
+  %x2 = bitcast i8* %x to {}**
+  %y = load {}*, {}** %x2, align 8
+  %y_is_null = icmp eq {}* %y, null
+
+  %x0 = getelementptr inbounds i8, i8* %x, i64 0
+  %res = select i1 %y_is_null, i8* null, i8* %x0
+
+  %nonnull = icmp ne i8* %res, null
+  call void @llvm.assume(i1 %nonnull)
+
+  ret i8* %res
+}
+
+declare void @llvm.assume(i1)


        


More information about the llvm-commits mailing list