If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used,
has the same effect as specifying ``-ffinite-math-only``.
+.. _opt_fapprox-func:
+
+**-f[no-]approx-func**
+
+ Allow certain math function calls (such as ``log``, ``sqrt``, ``pow``, etc)
+ to be replaced with an approximately equivalent set of instructions
+ or alternative math function calls. For example, a ``pow(x, 0.25)``
+ may be replaced with ``sqrt(sqrt(x))``, despite being an inexact result
+ in cases where ``x`` is ``-0.0`` or ``-inf``.
+ Defaults to ``-fno-approx-func``.
+
.. _opt_fsigned-zeros:
**-f[no-]signed-zeros**
PosFlag<SetTrue, [CC1Option], "Allow division operations to be reassociated",
[menable_unsafe_fp_math.KeyPath]>,
NegFlag<SetFalse>>;
-def fapprox_func : Flag<["-"], "fapprox-func">, Group<f_Group>, Flags<[CC1Option, NoDriverOption]>,
- MarshallingInfoFlag<LangOpts<"ApproxFunc">>, ImpliedByAnyOf<[menable_unsafe_fp_math.KeyPath]>;
+defm approx_func : BoolFOption<"approx-func", LangOpts<"ApproxFunc">, DefaultFalse,
+ PosFlag<SetTrue, [CC1Option], "Allow certain math function calls to be replaced "
+ "with an approximately equivalent calculation",
+ [menable_unsafe_fp_math.KeyPath]>,
+ NegFlag<SetFalse>>;
defm finite_math_only : BoolFOption<"finite-math-only",
LangOpts<"FiniteMathOnly">, DefaultFalse,
PosFlag<SetTrue, [CC1Option], "", [cl_finite_math_only.KeyPath, ffast_math.KeyPath]>,
Options.NoNaNsFPMath = LangOpts.NoHonorNaNs;
Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
Options.UnsafeFPMath = LangOpts.UnsafeFPMath;
+ Options.ApproxFuncFPMath = LangOpts.ApproxFunc;
Options.BBSections =
llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.BBSections)
FuncAttrs.addAttribute("no-infs-fp-math", "true");
if (LangOpts.NoHonorNaNs)
FuncAttrs.addAttribute("no-nans-fp-math", "true");
+ if (LangOpts.ApproxFunc)
+ FuncAttrs.addAttribute("approx-func-fp-math", "true");
if (LangOpts.UnsafeFPMath)
FuncAttrs.addAttribute("unsafe-fp-math", "true");
if (CodeGenOpts.SoftFloat)
// LLVM flags based on the final state.
bool HonorINFs = true;
bool HonorNaNs = true;
+ bool ApproxFunc = false;
// -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
bool MathErrno = TC.IsMathErrnoDefault();
bool AssociativeMath = false;
case options::OPT_fno_honor_infinities: HonorINFs = false; break;
case options::OPT_fhonor_nans: HonorNaNs = true; break;
case options::OPT_fno_honor_nans: HonorNaNs = false; break;
+ case options::OPT_fapprox_func: ApproxFunc = true; break;
+ case options::OPT_fno_approx_func: ApproxFunc = false; break;
case options::OPT_fmath_errno: MathErrno = true; break;
case options::OPT_fno_math_errno: MathErrno = false; break;
case options::OPT_fassociative_math: AssociativeMath = true; break;
if (!HonorNaNs)
CmdArgs.push_back("-menable-no-nans");
+ if (ApproxFunc)
+ CmdArgs.push_back("-fapprox-func");
+
if (MathErrno)
CmdArgs.push_back("-fmath-errno");
--- /dev/null
+// RUN: %clang_cc1 -fapprox-func %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-AFN %s
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-NO-AFN %s
+
+extern double exp(double);
+double afn_option_test(double x) {
+ return exp(x);
+ // CHECK-LABEL: define{{.*}} double @afn_option_test(double %x) #0 {
+
+ // CHECK-AFN: %{{.*}} = call afn double @{{.*}}exp{{.*}}(double %{{.*}})
+ // CHECK-AFN: attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+
+ // CHECK-NO-AFN: %{{.*}} = call double @{{.*}}exp{{.*}}(double %{{.*}})
+ // CHECK-NO-AFN-NOT: attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+}
// CHECK-NO-NANS-NO-FAST-MATH: "-cc1"
// CHECK-NO-NANS-NO-FAST-MATH-NOT: "-menable-no-nans"
//
+// RUN: %clang -### -fapprox-func -c %s 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-APPROX-FUNC %s
+// CHECK-APPROX-FUNC: "-cc1"
+// CHECK-APPROX-FUNC: "-fapprox-func"
+//
// RUN: %clang -### -fmath-errno -c %s 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-MATH-ERRNO %s
// CHECK-MATH-ERRNO: "-cc1"
TargetOptions()
: UnsafeFPMath(false), NoInfsFPMath(false), NoNaNsFPMath(false),
NoTrappingFPMath(true), NoSignedZerosFPMath(false),
- EnableAIXExtendedAltivecABI(false),
+ ApproxFuncFPMath(false), EnableAIXExtendedAltivecABI(false),
HonorSignDependentRoundingFPMathOption(false), NoZerosInBSS(false),
GuaranteedTailCallOpt(false), StackSymbolOrdering(true),
EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false),
/// argument or result as insignificant.
unsigned NoSignedZerosFPMath : 1;
+ /// ApproxFuncFPMath - This flag is enabled when the
+ /// -enable-approx-func-fp-math is specified on the command line. This
+ /// specifies that optimizations are allowed to substitute math functions
+ /// with approximate calculations
+ unsigned ApproxFuncFPMath : 1;
+
/// EnableAIXExtendedAltivecABI - This flag returns true when -vec-extabi is
/// specified. The code generator is then able to use both volatile and
/// nonvolitle vector registers. When false, the code generator only uses