diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index ced281d182d3..69f37be2dfbe 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -305,7 +305,9 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal( DL.getTypeAllocSize(Init->getType()->getArrayElementType()); auto MaskIdx = [&](Value *Idx) { if (!GEP->isInBounds() && llvm::countr_zero(ElementSize) != 0) { - Value *Mask = ConstantInt::get(Idx->getType(), -1); + // EraVM local begin + Value *Mask = ConstantInt::get(Idx->getType(), -1, true); + // EraVM local end Mask = Builder.CreateLShr(Mask, llvm::countr_zero(ElementSize)); Idx = Builder.CreateAnd(Idx, Mask); } diff --git a/llvm/test/Transforms/InstCombine/load-cmp-bug.ll b/llvm/test/Transforms/InstCombine/load-cmp-bug.ll new file mode 100644 index 000000000000..3c481a1f3056 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/load-cmp-bug.ll @@ -0,0 +1,20 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2 +; RUN: opt -passes=instcombine -S < %s | FileCheck %s + +target datalayout = "E-p:256:256-i256:256:256-S32-a:256:256" + +@G16 = internal constant [10 x i256] [i256 35, i256 82, i256 69, i256 81, i256 85, + i256 73, i256 82, i256 69, i256 68, i256 0] + +define i1 @test_noinbounds(i256 %X) { +; CHECK-LABEL: define i1 @test_noinbounds +; CHECK-SAME: (i256 [[X:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = and i256 [[X]], 3618502788666131106986593281521497120414687020801267626233049500247285301247 +; CHECK-NEXT: [[R:%.*]] = icmp eq i256 [[TMP1]], 9 +; CHECK-NEXT: ret i1 [[R]] +; + %P = getelementptr [10 x i256], ptr @G16, i256 0, i256 %X + %Q = load i256, ptr %P + %R = icmp eq i256 %Q, 0 + ret i1 %R +}