Skip to content

Commit 771b154

Browse files
committed
[InstCombine] Fix a cycle when folding fneg(select) with scalable vector types
The two folding operations are causing a cycle for the following case with scalable vector types: define <vscale x 2 x double> @test_fneg_select_abs(<vscale x 2 x i1> %cond, <vscale x 2 x double> %b) { %1 = select <vscale x 2 x i1> %cond, <vscale x 2 x double> zeroinitializer, <vscale x 2 x double> %b %2 = fneg fast <vscale x 2 x double> %1 ret <vscale x 2 x double> %2 } 1) fold fneg: -(Cond ? C : Y) -> Cond ? -C : -Y 2) fold select: (Cond ? -X : -Y) -> -(Cond ? X : Y) 1) results in the following since '<vscale x 2 x double> zeroinitializer' passes the check for the immediate constant: %.neg = fneg fast <vscale x 2 x double> zeroinitializer %b.neg = fneg fast <vscale x 2 x double> %b %1 = select fast <vscale x 2 x i1> %cond, <vscale x 2 x double> %.neg, <vscale x 2 x double> %b.neg and so we end up going back and forth between 1) and 2). Don't perform 1) if we are dealing with scalable vector constants as there is no immediate representation for a negative scalable zeroinitializer.
1 parent 6c64c8a commit 771b154

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2878,7 +2878,8 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
28782878

28792879
// -(Cond ? X : C) --> Cond ? -X : -C
28802880
// -(Cond ? C : Y) --> Cond ? -C : -Y
2881-
if (match(X, m_ImmConstant()) || match(Y, m_ImmConstant())) {
2881+
if ((match(X, m_ImmConstant()) && !isa<ScalableVectorType>(X->getType())) ||
2882+
(match(Y, m_ImmConstant()) && !isa<ScalableVectorType>(Y->getType()))) {
28822883
Value *NegX = Builder.CreateFNegFMF(X, &I, X->getName() + ".neg");
28832884
Value *NegY = Builder.CreateFNegFMF(Y, &I, Y->getName() + ".neg");
28842885
SelectInst *NewSel = SelectInst::Create(Cond, NegX, NegY);

llvm/test/Transforms/InstCombine/fneg.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,4 +1109,16 @@ define float @test_fneg_select_maxnum(float %x) {
11091109
ret float %neg
11101110
}
11111111

1112+
; Check that there's no infinite loop.
1113+
define <vscale x 2 x double> @test_fneg_select_svec(<vscale x 2 x i1> %cond, <vscale x 2 x double> %b) {
1114+
; CHECK-LABEL: @test_fneg_select_svec(
1115+
; CHECK-NEXT: [[TMP1:%.*]] = select <vscale x 2 x i1> [[COND:%.*]], <vscale x 2 x double> zeroinitializer, <vscale x 2 x double> [[B:%.*]]
1116+
; CHECK-NEXT: [[TMP2:%.*]] = fneg fast <vscale x 2 x double> [[TMP1]]
1117+
; CHECK-NEXT: ret <vscale x 2 x double> [[TMP2]]
1118+
;
1119+
%1 = select <vscale x 2 x i1> %cond, <vscale x 2 x double> zeroinitializer, <vscale x 2 x double> %b
1120+
%2 = fneg fast <vscale x 2 x double> %1
1121+
ret <vscale x 2 x double> %2
1122+
}
1123+
11121124
!0 = !{}

0 commit comments

Comments
 (0)