https://github.com/osa1 updated https://github.com/llvm/llvm-project/pull/242819
From dcac8d9bdb03fda76075dc32061d99afac82772e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= <[email protected]> Date: Sun, 15 Nov 2026 10:36:57 +0000 Subject: [PATCH 1/2] Add test --- llvm/test/Transforms/InstCombine/load-cmp.ll | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/llvm/test/Transforms/InstCombine/load-cmp.ll b/llvm/test/Transforms/InstCombine/load-cmp.ll index 267be58b62624..42c3884800575 100644 --- a/llvm/test/Transforms/InstCombine/load-cmp.ll +++ b/llvm/test/Transforms/InstCombine/load-cmp.ll @@ -808,3 +808,16 @@ define i1 @cmp_load_shifted_index_non_const(i32 %X, i32 %sh) { %cmp = icmp eq i16 %load, 68 ret i1 %cmp } + +@unknown_idx = external global i8 + +define i1 @cmp_load_constexpr_gep() { +; CHECK-LABEL: @cmp_load_constexpr_gep( +; CHECK-NEXT: [[LOAD:%.*]] = load i16, ptr getelementptr inbounds (i16, ptr @G16, i32 ptrtoint (ptr @unknown_idx to i32)), align 2 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[LOAD]], 85 +; CHECK-NEXT: ret i1 [[CMP]] +; + %load = load i16, ptr getelementptr inbounds (i16, ptr @G16, i32 ptrtoint (ptr @unknown_idx to i32)) + %cmp = icmp eq i16 %load, 85 + ret i1 %cmp +} From ae9dda6ba7dea5ff4f1a6dfece6a4e7344361b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= <[email protected]> Date: Sun, 15 Nov 2026 10:18:43 +0000 Subject: [PATCH 2/2] [InstCombine] Relax pointer operand type check in foldCmpLoadFromIndexedGlobal This simplifies code and allows handling `GetElementPtrConstantExpr`. --- .../InstCombine/InstCombineCompares.cpp | 20 +++++++++---------- llvm/test/Transforms/InstCombine/load-cmp.ll | 3 +-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index ba29b86b65ab9..183206beb2dc3 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -106,10 +106,13 @@ static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) { } /// Try to fold -/// cmp pred (load (gep GV, ...)), cmpcst -/// where GV is a global variable with a constant initializer. Try to simplify -/// this into some simple computation that does not need the load. For example -/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3". +/// cmp pred (load ...), cmpcst +/// where the load instruction loads from a global variable with a constant +/// initializer. +/// +/// Try to simplify this into some simple computation that does not need the +/// load. For example we can optimize +/// "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3". /// /// If AndCst is non-null, then the loaded value is masked with that constant /// before doing the comparison. This handles cases like "A[i]&4 == 0". @@ -121,11 +124,8 @@ static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) { Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal( LoadInst *LI, CmpPredicate Pred, Constant *RHSC, Instruction &CtxI, ConstantInt *AndCst) { - auto *GEP = dyn_cast<GetElementPtrInst>(LI->getPointerOperand()); - if (!GEP) - return nullptr; - - auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(GEP)); + Value *Ptr = LI->getPointerOperand(); + auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(Ptr)); if (LI->isVolatile() || !GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) return nullptr; @@ -135,7 +135,7 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal( if (EltSize.isScalable()) return nullptr; - LinearExpression Expr = decomposeLinearExpression(DL, GEP); + LinearExpression Expr = decomposeLinearExpression(DL, Ptr); if (!Expr.Index || Expr.BasePtr != GV || Expr.Offset.getBitWidth() > 64) return nullptr; diff --git a/llvm/test/Transforms/InstCombine/load-cmp.ll b/llvm/test/Transforms/InstCombine/load-cmp.ll index 42c3884800575..b5f750deb6774 100644 --- a/llvm/test/Transforms/InstCombine/load-cmp.ll +++ b/llvm/test/Transforms/InstCombine/load-cmp.ll @@ -813,8 +813,7 @@ define i1 @cmp_load_shifted_index_non_const(i32 %X, i32 %sh) { define i1 @cmp_load_constexpr_gep() { ; CHECK-LABEL: @cmp_load_constexpr_gep( -; CHECK-NEXT: [[LOAD:%.*]] = load i16, ptr getelementptr inbounds (i16, ptr @G16, i32 ptrtoint (ptr @unknown_idx to i32)), align 2 -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[LOAD]], 85 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 ptrtoint (ptr @unknown_idx to i32), 4 ; CHECK-NEXT: ret i1 [[CMP]] ; %load = load i16, ptr getelementptr inbounds (i16, ptr @G16, i32 ptrtoint (ptr @unknown_idx to i32)) _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
