Skip to content

Commit 6114c6b

Browse files
committed
Merge branch 'main' into subgroup_propagation
2 parents 0ce71f8 + cbbcc3d commit 6114c6b

File tree

5 files changed

+173
-6
lines changed

5 files changed

+173
-6
lines changed

clang/test/C/C2y/n3460.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: %clang_cc1 -verify -std=c2y -Wall %s
2+
3+
/* WG14 N3460: Clang 12
4+
* Complex operators
5+
*
6+
* This moves some Annex G requirements into the main body of the standard.
7+
*/
8+
9+
// CMPLX(0.0, inf) * 2.0, the result should be CMPLX(0.0, inf), not CMPLX(nan, inf)
10+
static_assert(__builtin_complex(0.0, __builtin_inf()) * 2.0 ==
11+
__builtin_complex(0.0, __builtin_inf()));
12+
13+
// CMPLX(0.0, 1.0) * -0.0 is CMPLX(-0.0, -0.0), not CMPLX(-0.0, +0.0)
14+
static_assert(__builtin_complex(0.0, 1.0) * -0.0 ==
15+
__builtin_complex(-0.0, -0.0));
16+
17+
// Testing for -0.0 is a pain because -0.0 == +0.0, so forcefully generate a
18+
// diagnostic and check the note.
19+
static_assert(__builtin_complex(0.0, 1.0) * -0.0 == 1); /* expected-error {{static assertion failed due to requirement '__builtin_complex(0., 1.) * -0. == 1'}} \
20+
expected-note {{expression evaluates to '(-0 + -0i) == 1'}}
21+
*/
22+
23+
// CMPLX(0.0, inf) / 2.0, the result should be CMPLX(0.0, inf),
24+
// not CMPLX(nan, inf)
25+
static_assert(__builtin_complex(0.0, __builtin_inf()) / 2.0 ==
26+
__builtin_complex(0.0, __builtin_inf()));
27+
28+
// CMPLX(2.0, 3.0) * 2.0, the result should be CMPLX(4.0, 6.0)
29+
static_assert(__builtin_complex(2.0, 3.0) * 2.0 ==
30+
__builtin_complex(4.0, 6.0));
31+
32+
// CMPLX(2.0, 4.0) / 2.0, the result should be CMPLX(1.0, 2.0)
33+
static_assert(__builtin_complex(2.0, 4.0) / 2.0 ==
34+
__builtin_complex(1.0, 2.0));
35+
36+
// CMPLX(2.0, 3.0) * CMPLX(4.0, 5.0), the result should be
37+
// CMPLX(8.0 - 15.0, 12.0 + 10.0)
38+
static_assert(__builtin_complex(2.0, 3.0) * __builtin_complex(4.0, 5.0) ==
39+
__builtin_complex(-7.0, 22.0));
40+
41+
// CMPLX(2.0, 3.0) / CMPLX(4.0, 5.0), the result should be
42+
// CMPLX((8.0 + 15.0)/(4.0^2 + 5.0^2), (12.0 - 10.0)/(4.0^2 + 5.0^2))
43+
static_assert(__builtin_complex(2.0, 3.0) / __builtin_complex(4.0, 5.0) ==
44+
__builtin_complex(23.0 / 41.0, 2.0 / 41.0));
45+
46+
47+
// 2.0 / CMPLX(2.0, 4.0), the result should be
48+
// CMPLX(4.0 /(2.0^2 + 4.0^2), -8.0/(2.0^2 + 4.0^2))
49+
static_assert(2.0 / __builtin_complex(2.0, 4.0) ==
50+
__builtin_complex(4.0 / 20.0, -8.0 / 20.0));
51+

clang/test/C/C2y/n3460_1.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang_cc1 -std=c2y -O0 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s
3+
// This tests the codegen for the same test cases as in n3460.c.
4+
5+
// CHECK-LABEL: define dso_local void @test(
6+
// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
7+
// CHECK-NEXT: [[ENTRY:.*]]:
8+
// CHECK-NEXT: [[A:%.*]] = alloca { double, double }, align 8
9+
// CHECK-NEXT: [[B:%.*]] = alloca { double, double }, align 8
10+
// CHECK-NEXT: [[C:%.*]] = alloca { double, double }, align 8
11+
// CHECK-NEXT: [[D:%.*]] = alloca { double, double }, align 8
12+
// CHECK-NEXT: [[E:%.*]] = alloca { double, double }, align 8
13+
// CHECK-NEXT: [[F:%.*]] = alloca { double, double }, align 8
14+
// CHECK-NEXT: [[G:%.*]] = alloca { double, double }, align 8
15+
// CHECK-NEXT: [[H:%.*]] = alloca { double, double }, align 8
16+
// CHECK-NEXT: [[A_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[A]], i32 0, i32 0
17+
// CHECK-NEXT: [[A_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[A]], i32 0, i32 1
18+
// CHECK-NEXT: store double 0.000000e+00, ptr [[A_REALP]], align 8
19+
// CHECK-NEXT: store double 0x7FF0000000000000, ptr [[A_IMAGP]], align 8
20+
// CHECK-NEXT: [[B_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[B]], i32 0, i32 0
21+
// CHECK-NEXT: [[B_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[B]], i32 0, i32 1
22+
// CHECK-NEXT: store double -0.000000e+00, ptr [[B_REALP]], align 8
23+
// CHECK-NEXT: store double -0.000000e+00, ptr [[B_IMAGP]], align 8
24+
// CHECK-NEXT: [[C_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 0
25+
// CHECK-NEXT: [[C_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 1
26+
// CHECK-NEXT: store double 0.000000e+00, ptr [[C_REALP]], align 8
27+
// CHECK-NEXT: store double 0x7FF0000000000000, ptr [[C_IMAGP]], align 8
28+
// CHECK-NEXT: [[D_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[D]], i32 0, i32 0
29+
// CHECK-NEXT: [[D_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[D]], i32 0, i32 1
30+
// CHECK-NEXT: store double 4.000000e+00, ptr [[D_REALP]], align 8
31+
// CHECK-NEXT: store double 6.000000e+00, ptr [[D_IMAGP]], align 8
32+
// CHECK-NEXT: [[E_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[E]], i32 0, i32 0
33+
// CHECK-NEXT: [[E_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[E]], i32 0, i32 1
34+
// CHECK-NEXT: store double 1.000000e+00, ptr [[E_REALP]], align 8
35+
// CHECK-NEXT: store double 2.000000e+00, ptr [[E_IMAGP]], align 8
36+
// CHECK-NEXT: br i1 false, label %[[COMPLEX_MUL_IMAG_NAN:.*]], label %[[COMPLEX_MUL_CONT:.*]], !prof [[PROF2:![0-9]+]]
37+
// CHECK: [[COMPLEX_MUL_IMAG_NAN]]:
38+
// CHECK-NEXT: br i1 false, label %[[COMPLEX_MUL_LIBCALL:.*]], label %[[COMPLEX_MUL_CONT]], !prof [[PROF2]]
39+
// CHECK: [[COMPLEX_MUL_LIBCALL]]:
40+
// CHECK-NEXT: [[CALL:%.*]] = call { double, double } @__muldc3(double noundef 2.000000e+00, double noundef 3.000000e+00, double noundef 4.000000e+00, double noundef 5.000000e+00) #[[ATTR1:[0-9]+]]
41+
// CHECK-NEXT: [[TMP0:%.*]] = extractvalue { double, double } [[CALL]], 0
42+
// CHECK-NEXT: [[TMP1:%.*]] = extractvalue { double, double } [[CALL]], 1
43+
// CHECK-NEXT: br label %[[COMPLEX_MUL_CONT]]
44+
// CHECK: [[COMPLEX_MUL_CONT]]:
45+
// CHECK-NEXT: [[REAL_MUL_PHI:%.*]] = phi double [ -7.000000e+00, %[[ENTRY]] ], [ -7.000000e+00, %[[COMPLEX_MUL_IMAG_NAN]] ], [ [[TMP0]], %[[COMPLEX_MUL_LIBCALL]] ]
46+
// CHECK-NEXT: [[IMAG_MUL_PHI:%.*]] = phi double [ 2.200000e+01, %[[ENTRY]] ], [ 2.200000e+01, %[[COMPLEX_MUL_IMAG_NAN]] ], [ [[TMP1]], %[[COMPLEX_MUL_LIBCALL]] ]
47+
// CHECK-NEXT: [[F_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[F]], i32 0, i32 0
48+
// CHECK-NEXT: [[F_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[F]], i32 0, i32 1
49+
// CHECK-NEXT: store double [[REAL_MUL_PHI]], ptr [[F_REALP]], align 8
50+
// CHECK-NEXT: store double [[IMAG_MUL_PHI]], ptr [[F_IMAGP]], align 8
51+
// CHECK-NEXT: [[CALL1:%.*]] = call { double, double } @__divdc3(double noundef 2.000000e+00, double noundef 3.000000e+00, double noundef 4.000000e+00, double noundef 5.000000e+00) #[[ATTR1]]
52+
// CHECK-NEXT: [[TMP2:%.*]] = extractvalue { double, double } [[CALL1]], 0
53+
// CHECK-NEXT: [[TMP3:%.*]] = extractvalue { double, double } [[CALL1]], 1
54+
// CHECK-NEXT: [[G_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[G]], i32 0, i32 0
55+
// CHECK-NEXT: [[G_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[G]], i32 0, i32 1
56+
// CHECK-NEXT: store double [[TMP2]], ptr [[G_REALP]], align 8
57+
// CHECK-NEXT: store double [[TMP3]], ptr [[G_IMAGP]], align 8
58+
// CHECK-NEXT: [[CALL2:%.*]] = call { double, double } @__divdc3(double noundef 2.000000e+00, double noundef 0.000000e+00, double noundef 2.000000e+00, double noundef 4.000000e+00) #[[ATTR1]]
59+
// CHECK-NEXT: [[TMP4:%.*]] = extractvalue { double, double } [[CALL2]], 0
60+
// CHECK-NEXT: [[TMP5:%.*]] = extractvalue { double, double } [[CALL2]], 1
61+
// CHECK-NEXT: [[H_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[H]], i32 0, i32 0
62+
// CHECK-NEXT: [[H_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[H]], i32 0, i32 1
63+
// CHECK-NEXT: store double [[TMP4]], ptr [[H_REALP]], align 8
64+
// CHECK-NEXT: store double [[TMP5]], ptr [[H_IMAGP]], align 8
65+
// CHECK-NEXT: ret void
66+
//
67+
void test() {
68+
_Complex double a = __builtin_complex(0.0, __builtin_inf()) * 2.0;
69+
_Complex double b = __builtin_complex(0.0, 1.0) * -0.0;
70+
_Complex double c = __builtin_complex(0.0, __builtin_inf()) / 2.0;
71+
_Complex double d = __builtin_complex(2.0, 3.0) * 2.0;
72+
_Complex double e = __builtin_complex(2.0, 4.0) / 2.0;
73+
_Complex double f = __builtin_complex(2.0, 3.0) * __builtin_complex(4.0, 5.0);
74+
_Complex double g = __builtin_complex(2.0, 3.0) / __builtin_complex(4.0, 5.0);
75+
_Complex double h = 2.0 / __builtin_complex(2.0, 4.0);
76+
}
77+
78+
//.
79+
// CHECK: [[PROF2]] = !{!"branch_weights", i32 1, i32 1048575}
80+
//.

clang/www/c_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ <h2 id="c2y">C2y implementation status</h2>
318318
<tr>
319319
<td>Complex operators</td>
320320
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3460.pdf">N3460</a></td>
321-
<td class="unknown" align="center">Unknown</td>
321+
<td class="full" align="center">Clang 12</td>
322322
</tr>
323323
</table>
324324
</details>

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ static Instruction *foldSelectBinOpIdentity(SelectInst &Sel,
120120
/// With some variations depending if FC is larger than TC, or the shift
121121
/// isn't needed, or the bit widths don't match.
122122
static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
123-
InstCombiner::BuilderTy &Builder) {
123+
InstCombiner::BuilderTy &Builder,
124+
const SimplifyQuery &SQ) {
124125
const APInt *SelTC, *SelFC;
125126
if (!match(Sel.getTrueValue(), m_APInt(SelTC)) ||
126127
!match(Sel.getFalseValue(), m_APInt(SelFC)))
@@ -148,11 +149,14 @@ static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
148149
} else if (auto Res = decomposeBitTestICmp(Cmp->getOperand(0),
149150
Cmp->getOperand(1), Pred)) {
150151
assert(ICmpInst::isEquality(Res->Pred) && "Not equality test?");
151-
if (!Res->Mask.isPowerOf2())
152+
AndMask = Res->Mask;
153+
V = Res->X;
154+
KnownBits Known =
155+
computeKnownBits(V, /*Depth=*/0, SQ.getWithInstruction(&Sel));
156+
AndMask &= Known.getMaxValue();
157+
if (!AndMask.isPowerOf2())
152158
return nullptr;
153159

154-
V = Res->X;
155-
AndMask = Res->Mask;
156160
Pred = Res->Pred;
157161
CreateAnd = true;
158162
} else {
@@ -1957,7 +1961,7 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
19571961
tryToReuseConstantFromSelectInComparison(SI, *ICI, *this))
19581962
return NewSel;
19591963

1960-
if (Value *V = foldSelectICmpAnd(SI, ICI, Builder))
1964+
if (Value *V = foldSelectICmpAnd(SI, ICI, Builder, SQ))
19611965
return replaceInstUsesWith(SI, V);
19621966

19631967
// NOTE: if we wanted to, this is where to detect integer MIN/MAX

llvm/test/Transforms/InstCombine/select-icmp-and.ll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,35 @@ define i16 @select_trunc_nuw_bittest_or(i8 %x) {
912912
%res = or i16 4, %select
913913
ret i16 %res
914914
}
915+
916+
define i16 @select_icmp_bittest_range(i16 range (i16 0, 512) %a) {
917+
; CHECK-LABEL: @select_icmp_bittest_range(
918+
; CHECK-NEXT: [[RES:%.*]] = and i16 [[A:%.*]], 256
919+
; CHECK-NEXT: ret i16 [[RES]]
920+
;
921+
%cmp = icmp ult i16 %a, 256
922+
%res = select i1 %cmp, i16 0, i16 256
923+
ret i16 %res
924+
}
925+
926+
define i16 @select_icmp_bittest_range_negative_test(i16 range (i16 0, 513) %a) {
927+
; CHECK-LABEL: @select_icmp_bittest_range_negative_test(
928+
; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ult i16 [[A:%.*]], 256
929+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP]], i16 0, i16 256
930+
; CHECK-NEXT: ret i16 [[RES]]
931+
;
932+
%cmp = icmp ult i16 %a, 256
933+
%res = select i1 %cmp, i16 0, i16 256
934+
ret i16 %res
935+
}
936+
937+
define i16 @select_icmp_bittest_range_negative_test2(i16 range (i16 0, 512) %a) {
938+
; CHECK-LABEL: @select_icmp_bittest_range_negative_test2(
939+
; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ult i16 [[A:%.*]], 255
940+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP]], i16 0, i16 255
941+
; CHECK-NEXT: ret i16 [[RES]]
942+
;
943+
%cmp = icmp ult i16 %a, 255
944+
%res = select i1 %cmp, i16 0, i16 255
945+
ret i16 %res
946+
}

0 commit comments

Comments
 (0)