=?utf-8?q?Ömer_Sinan_Ağacan?= <[email protected]> Message-ID: In-Reply-To: <llvm.org/llvm/llvm-project/pull/[email protected]>
llvmorg-github-actions[bot] wrote: <!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-llvm-transforms Author: Ömer Sinan Ağacan (osa1) <details> <summary>Changes</summary> This simplifies code and allows handling `GetElementPtrConstantExpr`. --- Full diff: https://github.com/llvm/llvm-project/pull/242819.diff 2 Files Affected: - (modified) llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp (+10-10) - (modified) llvm/test/Transforms/InstCombine/load-cmp.ll (+12) ``````````diff 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 267be58b62624..b5f750deb6774 100644 --- a/llvm/test/Transforms/InstCombine/load-cmp.ll +++ b/llvm/test/Transforms/InstCombine/load-cmp.ll @@ -808,3 +808,15 @@ 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: [[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)) + %cmp = icmp eq i16 %load, 85 + ret i1 %cmp +} `````````` </details> https://github.com/llvm/llvm-project/pull/242819 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
