[ConstantFolding] Fix folding of constrained compare intrinsics

The change fixes treatment of constrained compare intrinsics if
compared values are of vector type.

Differential revision: https://reviews.llvm.org/D110322
This commit is contained in:
Serge Pavlov 2022-02-26 23:39:12 +07:00
parent 20517719a9
commit 6982c38cb1
2 changed files with 15 additions and 5 deletions

View File

@ -2306,12 +2306,11 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
return nullptr;
}
static Constant *evaluateCompare(const ConstrainedFPIntrinsic *Call) {
static Constant *evaluateCompare(const APFloat &Op1, const APFloat &Op2,
const ConstrainedFPIntrinsic *Call) {
APFloat::opStatus St = APFloat::opOK;
auto *FCmp = cast<ConstrainedFPCmpIntrinsic>(Call);
FCmpInst::Predicate Cond = FCmp->getPredicate();
const APFloat &Op1 = cast<ConstantFP>(FCmp->getOperand(0))->getValueAPF();
const APFloat &Op2 = cast<ConstantFP>(FCmp->getOperand(1))->getValueAPF();
if (FCmp->isSignaling()) {
if (Op1.isNaN() || Op2.isNaN())
St = APFloat::opInvalidOp;
@ -2321,7 +2320,7 @@ static Constant *evaluateCompare(const ConstrainedFPIntrinsic *Call) {
}
bool Result = FCmpInst::compare(Op1, Op2, Cond);
if (mayFoldConstrained(const_cast<ConstrainedFPCmpIntrinsic *>(FCmp), St))
return ConstantInt::get(Call->getType(), Result);
return ConstantInt::get(Call->getType()->getScalarType(), Result);
return nullptr;
}
@ -2384,7 +2383,7 @@ static Constant *ConstantFoldScalarCall2(StringRef Name,
break;
case Intrinsic::experimental_constrained_fcmp:
case Intrinsic::experimental_constrained_fcmps:
return evaluateCompare(ConstrIntr);
return evaluateCompare(Op1V, Op2V, ConstrIntr);
}
if (mayFoldConstrained(const_cast<ConstrainedFPIntrinsic *>(ConstrIntr),
St))

View File

@ -438,6 +438,16 @@ entry:
ret i1 %result
}
define <2 x i1> @cmp_eq_02a() #0 {
; CHECK-LABEL: @cmp_eq_02a(
; CHECK-NEXT: entry:
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 false>
;
entry:
%result = call <2 x i1> @llvm.experimental.constrained.fcmp.v2f64(<2 x double> <double 2.0, double 3.0>, <2 x double> <double 2.0, double 2.0>, metadata !"oeq", metadata !"fpexcept.ignore") #0
ret <2 x i1> %result
}
define i1 @cmp_eq_03() #0 {
; CHECK-LABEL: @cmp_eq_03(
; CHECK-NEXT: entry:
@ -544,5 +554,6 @@ declare double @llvm.experimental.constrained.frem.f64(double, double, metadata,
declare double @llvm.experimental.constrained.fma.f64(double, double, double, metadata, metadata)
declare double @llvm.experimental.constrained.fmuladd.f64(double, double, double, metadata, metadata)
declare i1 @llvm.experimental.constrained.fcmp.f64(double, double, metadata, metadata)
declare <2 x i1> @llvm.experimental.constrained.fcmp.v2f64(<2 x double>, <2 x double>, metadata, metadata)
declare i1 @llvm.experimental.constrained.fcmps.f64(double, double, metadata, metadata)