Index: llvm/test/tools/llvm-reduce/remove-operands-fp.ll =================================================================== --- /dev/null +++ llvm/test/tools/llvm-reduce/remove-operands-fp.ll @@ -0,0 +1,63 @@ +; Test that llvm-reduce can reduce floating point operands +; +; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-undef --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t +; RUN: FileCheck --check-prefixes=CHECK,UNDEF %s < %t + +; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-one --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t +; RUN: FileCheck --check-prefixes=CHECK,ONE %s < %t + +; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-zero --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t +; RUN: FileCheck --check-prefixes=CHECK,ZERO %s < %t + +; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t +; RUN: FileCheck --check-prefixes=CHECK,ZERO %s < %t + +; CHECK-INTERESTINGNESS: = fadd float % +; CHECK-INTERESTINGNESS: = fadd float + +; CHECK-INTERESTINGNESS: = fadd <2 x float> +; CHECK-INTERESTINGNESS: = fadd <2 x float> +; CHECK-INTERESTINGNESS: = fadd <2 x float> + +; CHECK-LABEL: define void @foo( + +; UNDEF: %fadd0 = fadd float %arg0, undef +; UNDEF: %fadd1 = fadd float undef, undef +; UNDEF: %fadd2 = fadd <2 x float> undef, undef +; UNDEF: %fadd3 = fadd <2 x float> undef, undef +; UNDEF: %fadd4 = fadd <2 x float> undef, + +; ZERO: %fadd0 = fadd float %arg0, 0.000000e+00 +; ZERO: %fadd1 = fadd float 0.000000e+00, 0.000000e+00 +; ZERO: %fadd2 = fadd <2 x float> zeroinitializer, zeroinitializer +; ZERO: %fadd3 = fadd <2 x float> zeroinitializer, zeroinitializer +; ZERO: %fadd4 = fadd <2 x float> zeroinitializer, zeroinitializer + +; ONE: %fadd0 = fadd float %arg0, 1.000000e+00 +; ONE: %fadd1 = fadd float 1.000000e+00, 1.000000e+00 +; ONE: %fadd2 = fadd <2 x float> , +; ONE: %fadd3 = fadd <2 x float> , +; ONE: %fadd4 = fadd <2 x float> , + +declare float @llvm.fma.f32(float, float, float) +declare <2 x float> @llvm.fma.v2f32(<2 x float>, <2 x float>, <2 x float>) + +define void @foo(float %arg0, float %arg1, <2 x float> %arg2, <2 x float> %arg3) { +bb0: + %fadd0 = fadd float %arg0, %arg1 + store float %fadd0, float* undef + + %fadd1 = fadd float %arg0, %arg1 + store float %fadd1, float* undef + + %fadd2 = fadd <2 x float> %arg2, %arg3 + store <2 x float> %fadd2, <2 x float>* undef + + %fadd3 = fadd <2 x float> %arg2, %arg3 + store <2 x float> %fadd3, <2 x float>* undef + + %fadd4 = fadd <2 x float> %arg2, + store <2 x float> %fadd4, <2 x float>* undef + + ret void +} Index: llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp =================================================================== --- llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp +++ llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp @@ -11,9 +11,11 @@ #include "llvm/IR/InstIterator.h" #include "llvm/IR/InstrTypes.h" #include "llvm/IR/Operator.h" +#include "llvm/IR/PatternMatch.h" #include "llvm/IR/Type.h" using namespace llvm; +using namespace PatternMatch; static void extractOperandsFromModule(Oracle &O, Module &Program, @@ -39,6 +41,12 @@ return C && C->isNullValue(); } +static bool isZeroOrOneFP(Value *Op) { + const APFloat *C; + return match(Op, m_APFloat(C)) && + ((C->isZero() && !C->isNegative()) || C->isExactlyValue(1.0)); +} + static bool shouldReduceOperand(Use &Op) { Type *Ty = Op->getType(); if (Ty->isLabelTy() || Ty->isMetadataTy()) @@ -70,14 +78,27 @@ void llvm::reduceOperandsOneDeltaPass(TestRunner &Test) { errs() << "*** Reducing Operands to one...\n"; auto ReduceValue = [](Use &Op) -> Value * { - // TODO: support floats if (!shouldReduceOperand(Op)) return nullptr; - auto *Ty = dyn_cast(Op->getType()); - if (!Ty) - return nullptr; - // Don't replace existing ones and zeroes. - return (isOne(Op) || isZero(Op)) ? nullptr : ConstantInt::get(Ty, 1); + + Type *Ty = Op->getType(); + if (auto *IntTy = dyn_cast(Ty)) { + // Don't replace existing ones and zeroes. + return (isOne(Op) || isZero(Op)) ? nullptr : ConstantInt::get(IntTy, 1); + } + + if (Ty->isFloatingPointTy()) + return isZeroOrOneFP(Op) ? nullptr : ConstantFP::get(Ty, 1.0); + + if (VectorType *VT = dyn_cast(Ty)) { + if (isZeroOrOneFP(Op)) + return nullptr; + + return ConstantVector::getSplat( + VT->getElementCount(), ConstantFP::get(VT->getElementType(), 1.0)); + } + + return nullptr; }; runDeltaPass(Test, [ReduceValue](Oracle &O, Module &Program) { extractOperandsFromModule(O, Program, ReduceValue);