Skip to content

Commit 99777ef

Browse files
RKSimontru
authored andcommitted
[X86] Ensure the _mm_test_all_ones macro does not reuse argument (PR60006)
The macro _mm_test_all_ones(V) was defined as _mm_testc_si128((V), _mm_cmpeq_epi32((V), (V))) - which could cause side effects depending on the source of the V value. The _mm_cmpeq_epi32((V), (V)) trick was just to materialize an all-ones value, which can be more safely generated with _mm_set1_epi32(-1) . Fixes #60006 Differential Revision: https://reviews.llvm.org/D142477 (cherry picked from commit c9b2823)
1 parent 59325c9 commit 99777ef

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

clang/lib/Headers/smmintrin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ static __inline__ int __DEFAULT_FN_ATTRS _mm_testnzc_si128(__m128i __M,
11451145
/// A 128-bit integer vector containing the bits to be tested.
11461146
/// \returns TRUE if the bits specified in the operand are all set to 1; FALSE
11471147
/// otherwise.
1148-
#define _mm_test_all_ones(V) _mm_testc_si128((V), _mm_cmpeq_epi32((V), (V)))
1148+
#define _mm_test_all_ones(V) _mm_testc_si128((V), _mm_set1_epi32(-1))
11491149

11501150
/// Tests whether the specified bits in a 128-bit integer vector are
11511151
/// neither all zeros nor all ones.

clang/test/CodeGen/X86/sse41-builtins.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,13 @@ float pr51324(__m128 a) {
401401
// CHECK: extractelement <4 x float> %{{.*}}, i32 0
402402
return _mm_round_ps(a, 0)[0];
403403
}
404+
405+
// Ensure _mm_test_all_ones macro doesn't reuse argument
406+
__m128i expensive_call();
407+
int pr60006() {
408+
// CHECK-LABEL: pr60006
409+
// CHECK: call {{.*}} @expensive_call
410+
// CHECK-NOT: call {{.*}} @expensive_call
411+
// CHECK: call i32 @llvm.x86.sse41.ptestc(<2 x i64> %{{.*}}, <2 x i64> %{{.*}})
412+
return _mm_test_all_ones(expensive_call());
413+
}

0 commit comments

Comments
 (0)