Skip to content

Commit d8f576a

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

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
@@ -4048,6 +4048,9 @@ static Value *foldFrexpOfSelect(ExtractValueInst &EV, IntrinsicInst *FrexpCall,
40484048
SelectInst *SelectInst,
40494049
InstCombiner::BuilderTy &Builder) {
40504050
// Helper to fold frexp of select to select of frexp.
4051+
4052+
if (!SelectInst->hasOneUse() || !FrexpCall->hasOneUse())
4053+
return nullptr;
40514054
Value *Cond = SelectInst->getCondition();
40524055
Value *TrueVal = SelectInst->getTrueValue();
40534056
Value *FalseVal = SelectInst->getFalseValue();
@@ -4077,22 +4080,11 @@ static Value *foldFrexpOfSelect(ExtractValueInst &EV, IntrinsicInst *FrexpCall,
40774080
int Exp;
40784081
APFloat Mantissa = frexp(*ConstVal, Exp, APFloat::rmNearestTiesToEven);
40794082

4080-
Constant *ConstantMantissa;
4081-
if (auto *VecTy = dyn_cast<VectorType>(TrueVal->getType())) {
4082-
SmallVector<Constant *, 4> Elems(
4083-
VecTy->getElementCount().getFixedValue(),
4084-
ConstantFP::get(VecTy->getElementType(), Mantissa));
4085-
ConstantMantissa = ConstantVector::get(Elems);
4086-
} else {
4087-
ConstantMantissa = ConstantFP::get(TrueVal->getType(), Mantissa);
4088-
}
4083+
Constant *ConstantMantissa = ConstantFP::get(TrueVal->getType(), Mantissa);
40894084

4090-
Value *NewSel = Builder.CreateSelect(
4085+
Value *NewSel = Builder.CreateSelectFMF(
40914086
Cond, ConstIsTrue ? ConstantMantissa : NewEV,
4092-
ConstIsTrue ? NewEV : ConstantMantissa, "select.frexp");
4093-
if (auto *NewSelInst = dyn_cast<Instruction>(NewSel))
4094-
NewSelInst->copyFastMathFlags(SelectInst);
4095-
4087+
ConstIsTrue ? NewEV : ConstantMantissa, SelectInst, "select.frexp");
40964088
return NewSel;
40974089
}
40984090
Instruction *InstCombinerImpl::visitExtractValueInst(ExtractValueInst &EV) {
@@ -4104,17 +4096,15 @@ Instruction *InstCombinerImpl::visitExtractValueInst(ExtractValueInst &EV) {
41044096
if (Value *V = simplifyExtractValueInst(Agg, EV.getIndices(),
41054097
SQ.getWithInstruction(&EV)))
41064098
return replaceInstUsesWith(EV, V);
4107-
if (EV.getNumIndices() == 1 && EV.getIndices()[0] == 0) {
4108-
if (auto *FrexpCall = dyn_cast<IntrinsicInst>(Agg)) {
4109-
if (FrexpCall->getIntrinsicID() == Intrinsic::frexp) {
4110-
if (auto *SelInst = dyn_cast<SelectInst>(FrexpCall->getArgOperand(0))) {
4111-
if (Value *Result =
4112-
foldFrexpOfSelect(EV, FrexpCall, SelInst, Builder)) {
4113-
return replaceInstUsesWith(EV, Result);
4114-
}
4115-
}
4116-
}
4117-
}
4099+
4100+
Value *Cond, *TrueVal, *FalseVal;
4101+
if (match(&EV, m_ExtractValue<0>(m_Intrinsic<Intrinsic::frexp>(m_Select(
4102+
m_Value(Cond), m_Value(TrueVal), m_Value(FalseVal)))))) {
4103+
auto *SelInst =
4104+
cast<SelectInst>(cast<IntrinsicInst>(Agg)->getArgOperand(0));
4105+
if (Value *Result =
4106+
foldFrexpOfSelect(EV, cast<IntrinsicInst>(Agg), SelInst, Builder))
4107+
return replaceInstUsesWith(EV, Result);
41184108
}
41194109
if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
41204110
// 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)