llvm-project/clang/test/CodeGen/fp-reciprocal-pragma.cpp
Nikita Popov a105877646
[InstCombine] Remove some of the complexity-based canonicalization (#91185)
The idea behind this canonicalization is that it allows us to handle less
patterns, because we know that some will be canonicalized away. This is
indeed very useful to e.g. know that constants are always on the right.

However, this is only useful if the canonicalization is actually
reliable. This is the case for constants, but not for arguments: Moving
these to the right makes it look like the "more complex" expression is
guaranteed to be on the left, but this is not actually the case in
practice. It fails as soon as you replace the argument with another
instruction.

The end result is that it looks like things correctly work in tests,
while they actually don't. We use the "thwart complexity-based
canonicalization" trick to handle this in tests, but it's often a
challenge for new contributors to get this right, and based on the
regressions this PR originally exposed, we clearly don't get this right
in many cases.

For this reason, I think that it's better to remove this complexity
canonicalization. It will make it much easier to write tests for
commuted cases and make sure that they are handled.
2024-08-21 12:02:54 +02:00

131 lines
4.1 KiB
C++

// RUN: %clang_cc1 -O3 -triple %itanium_abi_triple -emit-llvm -o - %s | FileCheck -check-prefixes=CHECK,DEFAULT %s
// RUN: %clang_cc1 -O3 -triple %itanium_abi_triple -freciprocal-math -emit-llvm -o - %s | FileCheck -check-prefixes=CHECK,FLAG %s
float base(float a, float b, float c) {
// CHECK-LABEL: _Z4basefff
// FLAG: %[[A:.+]] = fdiv arcp float %b, %c
// FLAG: %[[M:.+]] = fdiv arcp float %[[A]], %b
// FLAG-NEXT: fadd arcp float %c, %[[M]]
// DEFAULT: %[[A:.+]] = fdiv float %b, %c
// DEFAULT: %[[M:.+]] = fdiv float %[[A]], %b
// DEFAULT-NEXT: fadd float %c, %[[M]]
a = b / c;
return a / b + c;
}
// Simple case
float fp_recip_simple(float a, float b, float c) {
// CHECK-LABEL: _Z15fp_recip_simplefff
// CHECK: %[[A:.+]] = fdiv arcp float %b, %c
// CHECK: %[[M:.+]] = fdiv arcp float %[[A]], %b
// CHECK-NEXT: fadd arcp float %c, %[[M]]
#pragma clang fp reciprocal(on)
a = b / c;
return a / b + c;
}
// Test interaction with -freciprocal-math
float fp_recip_disable(float a, float b, float c) {
// CHECK-LABEL: _Z16fp_recip_disablefff
// CHECK: %[[A:.+]] = fdiv float %b, %c
// CHECK: %[[M:.+]] = fdiv float %[[A]], %b
// CHECK-NEXT: fadd float %c, %[[M]]
#pragma clang fp reciprocal(off)
a = b / c;
return a / b + c;
}
float fp_recip_with_reassoc_simple(float a, float b, float c) {
// CHECK-LABEL: _Z28fp_recip_with_reassoc_simplefff
// CHECK: %[[A:.+]] = fmul reassoc arcp float %b, %c
// CHECK: %[[M:.+]] = fdiv reassoc arcp float %b, %[[A]]
// CHECK-NEXT: fadd reassoc arcp float %c, %[[M]]
#pragma clang fp reciprocal(on) reassociate(on)
a = b / c;
return a / b + c;
}
// arcp pragma should only apply to its scope
float fp_recip_scoped(float a, float b, float c) {
// CHECK-LABEL: _Z15fp_recip_scopedfff
// DEFAULT: %[[M:.+]] = fdiv float %a, %b
// DEFAULT-NEXT: fadd float %[[M]], %c
// FLAG: %[[M:.+]] = fdiv arcp float %a, %b
// FLAG-NEXT: fadd arcp float %[[M]], %c
{
#pragma clang fp reciprocal(on)
}
return a / b + c;
}
// arcp pragma should apply to templates as well
class Foo {};
Foo operator+(Foo, Foo);
template <typename T>
T template_recip(T a, T b, T c) {
#pragma clang fp reciprocal(on)
return ((a / b) - c) + c;
}
float fp_recip_template(float a, float b, float c) {
// CHECK-LABEL: _Z17fp_recip_templatefff
// CHECK: %[[A1:.+]] = fdiv arcp float %a, %b
// CHECK-NEXT: %[[A2:.+]] = fsub arcp float %[[A1]], %c
// CHECK-NEXT: fadd arcp float %c, %[[A2]]
return template_recip<float>(a, b, c);
}
// File Scoping should work across functions
#pragma clang fp reciprocal(on)
float fp_file_scope_on(float a, float b, float c) {
// CHECK-LABEL: _Z16fp_file_scope_onfff
// CHECK: %[[M1:.+]] = fdiv arcp float %a, %c
// CHECK-NEXT: %[[M2:.+]] = fdiv arcp float %b, %c
// CHECK-NEXT: fadd arcp float %[[M1]], %[[M2]]
return (a / c) + (b / c);
}
// Inner pragma has precedence
float fp_file_scope_stop(float a, float b, float c) {
// CHECK-LABEL: _Z18fp_file_scope_stopfff
// CHECK: %[[A:.+]] = fdiv arcp float %a, %a
// CHECK: %[[M1:.+]] = fdiv float %[[A]], %c
// CHECK-NEXT: %[[M2:.+]] = fdiv float %b, %c
// CHECK-NEXT: fsub float %[[M1]], %[[M2]]
a = a / a;
{
#pragma clang fp reciprocal(off)
return (a / c) - (b / c);
}
}
#pragma clang fp reciprocal(off)
float fp_recip_off(float a, float b, float c) {
// CHECK-LABEL: _Z12fp_recip_offfff
// CHECK: %[[D1:.+]] = fdiv float %a, %c
// CHECK-NEXT: %[[D2:.+]] = fdiv float %b, %c
// CHECK-NEXT: fadd float %[[D1]], %[[D2]]
return (a / c) + (b / c);
}
// Takes latest flag
float fp_recip_many(float a, float b, float c) {
// CHECK-LABEL: _Z13fp_recip_manyfff
// CHECK: %[[D1:.+]] = fdiv arcp float %a, %c
// CHECK-NEXT: %[[D2:.+]] = fdiv arcp float %b, %c
// CHECK-NEXT: fadd arcp float %[[D1]], %[[D2]]
#pragma clang fp reciprocal(off) reciprocal(on)
return (a / c) + (b / c);
}
// Pragma does not propagate through called functions
float helper_func(float a, float b, float c) { return a + b + c; }
float fp_recip_call_helper(float a, float b, float c) {
// CHECK-LABEL: _Z20fp_recip_call_helperfff
// CHECK: %[[S1:.+]] = fadd float %a, %b
// CHECK-NEXT: fadd float %[[S1]], %c
#pragma clang fp reciprocal(on)
return helper_func(a, b, c);
}