From 6982c38cb120ec7cd6a895b0363643678e362557 Mon Sep 17 00:00:00 2001 From: Serge Pavlov Date: Sat, 26 Feb 2022 23:39:12 +0700 Subject: [PATCH] [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 --- llvm/lib/Analysis/ConstantFolding.cpp | 9 ++++----- .../Transforms/InstSimplify/constfold-constrained.ll | 11 +++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 2a9ff10c5c27..d318b16d6776 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -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(Call); FCmpInst::Predicate Cond = FCmp->getPredicate(); - const APFloat &Op1 = cast(FCmp->getOperand(0))->getValueAPF(); - const APFloat &Op2 = cast(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(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(ConstrIntr), St)) diff --git a/llvm/test/Transforms/InstSimplify/constfold-constrained.ll b/llvm/test/Transforms/InstSimplify/constfold-constrained.ll index f659b82d9748..a9ef7f6a765d 100644 --- a/llvm/test/Transforms/InstSimplify/constfold-constrained.ll +++ b/llvm/test/Transforms/InstSimplify/constfold-constrained.ll @@ -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> +; +entry: + %result = call <2 x i1> @llvm.experimental.constrained.fcmp.v2f64(<2 x double> , <2 x double> , 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)