[PATCH] D38934: Move folding of icmp with zero after checking for min/max idioms.
Artur Gainullin via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 15 03:46:43 PDT 2017
ArturGainullin updated this revision to Diff 119069.
ArturGainullin added a comment.
Fixed comment.
https://reviews.llvm.org/D38934
Files:
lib/Transforms/InstCombine/InstCombineCompares.cpp
lib/Transforms/InstCombine/InstCombineInternal.h
test/Transforms/InstCombine/minmax-fold.ll
Index: test/Transforms/InstCombine/minmax-fold.ll
===================================================================
--- test/Transforms/InstCombine/minmax-fold.ll
+++ test/Transforms/InstCombine/minmax-fold.ll
@@ -507,7 +507,7 @@
; CHECK-LABEL: @clamp_check_for_no_infinite_loop1(
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[I:%.*]], 255
; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[CMP1]], i32 [[I]], i32 255
-; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[I]], 0
+; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[SEL1]], 0
; CHECK-NEXT: [[RES:%.*]] = select i1 [[TMP1]], i32 [[SEL1]], i32 0
; CHECK-NEXT: ret i32 [[RES]]
;
Index: lib/Transforms/InstCombine/InstCombineInternal.h
===================================================================
--- lib/Transforms/InstCombine/InstCombineInternal.h
+++ lib/Transforms/InstCombine/InstCombineInternal.h
@@ -700,6 +700,7 @@
Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp);
Instruction *foldICmpBinOp(ICmpInst &Cmp);
Instruction *foldICmpEquality(ICmpInst &Cmp);
+ Instruction *foldICmpWithZero(ICmpInst &Cmp);
Instruction *foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select,
ConstantInt *C);
Index: lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1318,6 +1318,24 @@
return ExtractValueInst::Create(Call, 1, "sadd.overflow");
}
+// Handle (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
+Instruction *InstCombiner::foldICmpWithZero(ICmpInst &Cmp) {
+ CmpInst::Predicate Pred = Cmp.getPredicate();
+ Value *X = Cmp.getOperand(0);
+
+ if (match(Cmp.getOperand(1), m_Zero()) && Pred == ICmpInst::ICMP_SGT) {
+ Value *A, *B;
+ SelectPatternResult SPR = matchSelectPattern(X, A, B);
+ if (SPR.Flavor == SPF_SMIN) {
+ if (isKnownPositive(A, DL, 0, &AC, &Cmp, &DT))
+ return new ICmpInst(Pred, B, Cmp.getOperand(1));
+ if (isKnownPositive(B, DL, 0, &AC, &Cmp, &DT))
+ return new ICmpInst(Pred, A, Cmp.getOperand(1));
+ }
+ }
+ return nullptr;
+}
+
// Fold icmp Pred X, C.
Instruction *InstCombiner::foldICmpWithConstant(ICmpInst &Cmp) {
CmpInst::Predicate Pred = Cmp.getPredicate();
@@ -1349,17 +1367,6 @@
return Res;
}
- // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
- if (C->isNullValue() && Pred == ICmpInst::ICMP_SGT) {
- SelectPatternResult SPR = matchSelectPattern(X, A, B);
- if (SPR.Flavor == SPF_SMIN) {
- if (isKnownPositive(A, DL, 0, &AC, &Cmp, &DT))
- return new ICmpInst(Pred, B, Cmp.getOperand(1));
- if (isKnownPositive(B, DL, 0, &AC, &Cmp, &DT))
- return new ICmpInst(Pred, A, Cmp.getOperand(1));
- }
- }
-
// FIXME: Use m_APInt to allow folds for splat constants.
ConstantInt *CI = dyn_cast<ConstantInt>(Cmp.getOperand(1));
if (!CI)
@@ -4462,6 +4469,10 @@
(SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
return nullptr;
+ // Do this after checking for min/max to prevent infinite looping.
+ if (Instruction *Res = foldICmpWithZero(I))
+ return Res;
+
// FIXME: We only do this after checking for min/max to prevent infinite
// looping caused by a reverse canonicalization of these patterns for min/max.
// FIXME: The organization of folds is a mess. These would naturally go into
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38934.119069.patch
Type: text/x-patch
Size: 3464 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171015/20535c7f/attachment.bin>
More information about the llvm-commits
mailing list