Skip to content

Commit ad6c501

Browse files
committed
[InstCombine] Refactor PatternMatch and add scalable Vector tests
1 parent 1e656c8 commit ad6c501

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4074,6 +4074,9 @@ static Value *foldFrexpOfSelect(ExtractValueInst &EV, IntrinsicInst *FrexpCall,
40744074
SelectInst *SelectInst,
40754075
InstCombiner::BuilderTy &Builder) {
40764076
// Helper to fold frexp of select to select of frexp.
4077+
4078+
if (!SelectInst->hasOneUse() || !FrexpCall->hasOneUse())
4079+
return nullptr;
40774080
Value *Cond = SelectInst->getCondition();
40784081
Value *TrueVal = SelectInst->getTrueValue();
40794082
Value *FalseVal = SelectInst->getFalseValue();
@@ -4103,22 +4106,11 @@ static Value *foldFrexpOfSelect(ExtractValueInst &EV, IntrinsicInst *FrexpCall,
41034106
int Exp;
41044107
APFloat Mantissa = frexp(*ConstVal, Exp, APFloat::rmNearestTiesToEven);
41054108

4106-
Constant *ConstantMantissa;
4107-
if (auto *VecTy = dyn_cast<VectorType>(TrueVal->getType())) {
4108-
SmallVector<Constant *, 4> Elems(
4109-
VecTy->getElementCount().getFixedValue(),
4110-
ConstantFP::get(VecTy->getElementType(), Mantissa));
4111-
ConstantMantissa = ConstantVector::get(Elems);
4112-
} else {
4113-
ConstantMantissa = ConstantFP::get(TrueVal->getType(), Mantissa);
4114-
}
4109+
Constant *ConstantMantissa = ConstantFP::get(TrueVal->getType(), Mantissa);
41154110

4116-
Value *NewSel = Builder.CreateSelect(
4111+
Value *NewSel = Builder.CreateSelectFMF(
41174112
Cond, ConstIsTrue ? ConstantMantissa : NewEV,
4118-
ConstIsTrue ? NewEV : ConstantMantissa, "select.frexp");
4119-
if (auto *NewSelInst = dyn_cast<Instruction>(NewSel))
4120-
NewSelInst->copyFastMathFlags(SelectInst);
4121-
4113+
ConstIsTrue ? NewEV : ConstantMantissa, SelectInst, "select.frexp");
41224114
return NewSel;
41234115
}
41244116
Instruction *InstCombinerImpl::visitExtractValueInst(ExtractValueInst &EV) {
@@ -4130,17 +4122,15 @@ Instruction *InstCombinerImpl::visitExtractValueInst(ExtractValueInst &EV) {
41304122
if (Value *V = simplifyExtractValueInst(Agg, EV.getIndices(),
41314123
SQ.getWithInstruction(&EV)))
41324124
return replaceInstUsesWith(EV, V);
4133-
if (EV.getNumIndices() == 1 && EV.getIndices()[0] == 0) {
4134-
if (auto *FrexpCall = dyn_cast<IntrinsicInst>(Agg)) {
4135-
if (FrexpCall->getIntrinsicID() == Intrinsic::frexp) {
4136-
if (auto *SelInst = dyn_cast<SelectInst>(FrexpCall->getArgOperand(0))) {
4137-
if (Value *Result =
4138-
foldFrexpOfSelect(EV, FrexpCall, SelInst, Builder)) {
4139-
return replaceInstUsesWith(EV, Result);
4140-
}
4141-
}
4142-
}
4143-
}
4125+
4126+
Value *Cond, *TrueVal, *FalseVal;
4127+
if (match(&EV, m_ExtractValue<0>(m_Intrinsic<Intrinsic::frexp>(m_Select(
4128+
m_Value(Cond), m_Value(TrueVal), m_Value(FalseVal)))))) {
4129+
auto *SelInst =
4130+
cast<SelectInst>(cast<IntrinsicInst>(Agg)->getArgOperand(0));
4131+
if (Value *Result =
4132+
foldFrexpOfSelect(EV, cast<IntrinsicInst>(Agg), SelInst, Builder))
4133+
return replaceInstUsesWith(EV, Result);
41444134
}
41454135
if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
41464136
// We're extracting from an insertvalue instruction, compare the indices

llvm/test/Transforms/InstCombine/select_frexp.ll

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ define float @test_select_frexp_multi_use(float %x, i1 %cond) {
4040
; CHECK-SAME: float [[X:%.*]], i1 [[COND:%.*]]) {
4141
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], float 1.000000e+00, float [[X]]
4242
; CHECK-NEXT: call void @use(float [[SEL]])
43-
; CHECK-NEXT: [[FREXP:%.*]] = call { float, i32 } @llvm.frexp.f32.i32(float [[X]])
43+
; CHECK-NEXT: [[FREXP:%.*]] = call { float, i32 } @llvm.frexp.f32.i32(float [[SEL]])
4444
; CHECK-NEXT: [[FREXP_0:%.*]] = extractvalue { float, i32 } [[FREXP]], 0
45-
; CHECK-NEXT: [[SELECT_FREXP:%.*]] = select i1 [[COND]], float 5.000000e-01, float [[FREXP_0]]
46-
; CHECK-NEXT: ret float [[SELECT_FREXP]]
45+
; CHECK-NEXT: ret float [[FREXP_0]]
4746
;
4847
%sel = select i1 %cond, float 1.000000e+00, float %x
4948
call void @use(float %sel)
@@ -158,4 +157,35 @@ define <2 x float> @test_select_frexp_vec_fast_math(<2 x float> %x, <2 x i1> %co
158157
ret <2 x float> %frexp.0
159158
}
160159

160+
; Test with scalable vectors with constant at True Position
161+
define <vscale x 2 x float> @test_select_frexp_scalable_vec0(<vscale x 2 x float> %x, <vscale x 2 x i1> %cond) {
162+
; CHECK-LABEL: define <vscale x 2 x float> @test_select_frexp_scalable_vec0(
163+
; CHECK-SAME: <vscale x 2 x float> [[X:%.*]], <vscale x 2 x i1> [[COND:%.*]]) {
164+
; CHECK-NEXT: [[FREXP1:%.*]] = call { <vscale x 2 x float>, <vscale x 2 x i32> } @llvm.frexp.nxv2f32.nxv2i32(<vscale x 2 x float> [[X]])
165+
; CHECK-NEXT: [[MANTISSA:%.*]] = extractvalue { <vscale x 2 x float>, <vscale x 2 x i32> } [[FREXP1]], 0
166+
; CHECK-NEXT: [[SELECT_FREXP:%.*]] = select <vscale x 2 x i1> [[COND]], <vscale x 2 x float> splat (float 5.000000e-01), <vscale x 2 x float> [[MANTISSA]]
167+
; CHECK-NEXT: ret <vscale x 2 x float> [[SELECT_FREXP]]
168+
;
169+
%sel = select <vscale x 2 x i1> %cond, <vscale x 2 x float> splat (float 1.000000e+00), <vscale x 2 x float> %x
170+
%frexp = call { <vscale x 2 x float>, <vscale x 2 x i32> } @llvm.frexp.nxv2f32.nxv2i32(<vscale x 2 x float> %sel)
171+
%frexp.0 = extractvalue { <vscale x 2 x float>, <vscale x 2 x i32> } %frexp, 0
172+
ret <vscale x 2 x float> %frexp.0
173+
}
174+
175+
; Test with scalable vectors with constant at False Position
176+
define <vscale x 2 x float> @test_select_frexp_scalable_vec1(<vscale x 2 x float> %x, <vscale x 2 x i1> %cond) {
177+
; CHECK-LABEL: define <vscale x 2 x float> @test_select_frexp_scalable_vec1(
178+
; CHECK-SAME: <vscale x 2 x float> [[X:%.*]], <vscale x 2 x i1> [[COND:%.*]]) {
179+
; CHECK-NEXT: [[FREXP1:%.*]] = call { <vscale x 2 x float>, <vscale x 2 x i32> } @llvm.frexp.nxv2f32.nxv2i32(<vscale x 2 x float> [[X]])
180+
; CHECK-NEXT: [[MANTISSA:%.*]] = extractvalue { <vscale x 2 x float>, <vscale x 2 x i32> } [[FREXP1]], 0
181+
; CHECK-NEXT: [[SELECT_FREXP:%.*]] = select <vscale x 2 x i1> [[COND]], <vscale x 2 x float> [[MANTISSA]], <vscale x 2 x float> splat (float 5.000000e-01)
182+
; CHECK-NEXT: ret <vscale x 2 x float> [[SELECT_FREXP]]
183+
;
184+
%sel = select <vscale x 2 x i1> %cond, <vscale x 2 x float> %x, <vscale x 2 x float> splat (float 1.000000e+00)
185+
%frexp = call { <vscale x 2 x float>, <vscale x 2 x i32> } @llvm.frexp.nxv2f32.nxv2i32(<vscale x 2 x float> %sel)
186+
%frexp.0 = extractvalue { <vscale x 2 x float>, <vscale x 2 x i32> } %frexp, 0
187+
ret <vscale x 2 x float> %frexp.0
188+
}
189+
161190
declare { <2 x float>, <2 x i32> } @llvm.frexp.v2f32.v2i32(<2 x float>)
191+
declare { <vscale x 2 x float>, <vscale x 2 x i32> } @llvm.frexp.nxv2f32.nxv2i32(<vscale x 2 x float>)

0 commit comments

Comments
 (0)