[llvm] d34537a - [CVP] Handle use-site conditions in urem folds
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 13 06:45:44 PST 2023
Author: Nikita Popov
Date: 2023-01-13T15:45:34+01:00
New Revision: d34537a732989fe605a9ba127903a263f0e134dd
URL: https://github.com/llvm/llvm-project/commit/d34537a732989fe605a9ba127903a263f0e134dd
DIFF: https://github.com/llvm/llvm-project/commit/d34537a732989fe605a9ba127903a263f0e134dd.diff
LOG: [CVP] Handle use-site conditions in urem folds
Added:
Modified:
llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
llvm/test/Transforms/CorrelatedValuePropagation/cond-at-use.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 77570221ddd3..c0d109854b27 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -773,11 +773,11 @@ static bool processURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
assert(Instr->getOpcode() == Instruction::URem);
assert(!Instr->getType()->isVectorTy());
- Value *X = Instr->getOperand(0);
- Value *Y = Instr->getOperand(1);
+ const Use &X = Instr->getOperandUse(0);
+ const Use &Y = Instr->getOperandUse(1);
- ConstantRange XCR = LVI->getConstantRange(X, Instr);
- ConstantRange YCR = LVI->getConstantRange(Y, Instr);
+ ConstantRange XCR = LVI->getConstantRangeAtUse(X);
+ ConstantRange YCR = LVI->getConstantRangeAtUse(Y);
// X u% Y -> X iff X u< Y
if (XCR.icmp(ICmpInst::ICMP_ULT, YCR)) {
@@ -819,10 +819,11 @@ static bool processURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
IRBuilder<> B(Instr);
// NOTE: this transformation introduces two uses of X,
// but it may be undef so we must freeze it first.
- X = B.CreateFreeze(X, X->getName() + ".frozen");
- auto *AdjX = B.CreateNUWSub(X, Y, Instr->getName() + ".urem");
- auto *Cmp = B.CreateICmp(ICmpInst::ICMP_ULT, X, Y, Instr->getName() + ".cmp");
- auto *ExpandedURem = B.CreateSelect(Cmp, X, AdjX);
+ Value *FrozenX = B.CreateFreeze(X, X->getName() + ".frozen");
+ auto *AdjX = B.CreateNUWSub(FrozenX, Y, Instr->getName() + ".urem");
+ auto *Cmp =
+ B.CreateICmp(ICmpInst::ICMP_ULT, FrozenX, Y, Instr->getName() + ".cmp");
+ auto *ExpandedURem = B.CreateSelect(Cmp, FrozenX, AdjX);
ExpandedURem->takeName(Instr);
Instr->replaceAllUsesWith(ExpandedURem);
Instr->eraseFromParent();
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/cond-at-use.ll b/llvm/test/Transforms/CorrelatedValuePropagation/cond-at-use.ll
index 534413ec4d64..ffa09abf8756 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/cond-at-use.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/cond-at-use.ll
@@ -301,9 +301,8 @@ exit:
define i16 @urem_elide(i16 %x) {
; CHECK-LABEL: @urem_elide(
-; CHECK-NEXT: [[UREM:%.*]] = urem i16 [[X:%.*]], 42
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i16 [[X]], 42
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i16 [[UREM]], i16 24
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i16 [[X:%.*]], 42
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i16 [[X]], i16 24
; CHECK-NEXT: ret i16 [[SEL]]
;
%urem = urem i16 %x, 42
@@ -314,7 +313,10 @@ define i16 @urem_elide(i16 %x) {
define i16 @urem_expand(i16 %x) {
; CHECK-LABEL: @urem_expand(
-; CHECK-NEXT: [[UREM:%.*]] = urem i16 [[X:%.*]], 42
+; CHECK-NEXT: [[X_FROZEN:%.*]] = freeze i16 [[X:%.*]]
+; CHECK-NEXT: [[UREM_UREM:%.*]] = sub nuw i16 [[X_FROZEN]], 42
+; CHECK-NEXT: [[UREM_CMP:%.*]] = icmp ult i16 [[X_FROZEN]], 42
+; CHECK-NEXT: [[UREM:%.*]] = select i1 [[UREM_CMP]], i16 [[X_FROZEN]], i16 [[UREM_UREM]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i16 [[X]], 84
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i16 [[UREM]], i16 24
; CHECK-NEXT: ret i16 [[SEL]]
More information about the llvm-commits
mailing list