[flang] Fix crash in HLFIR generation (#118399)
[llvm-project.git] / clang / test / CodeGen / ubsan-shift-bitint.c
blob9e4ec15060b3fd6022b18f985e1e1132b1b8c6eb
1 // RUN: %clang_cc1 %s -O0 -fsanitize=shift-exponent -emit-llvm -std=c2x -triple=x86_64-unknown-linux -o - | FileCheck %s
3 // Checking that the code generation is using the unextended/untruncated
4 // exponent values and capping the values accordingly
6 // CHECK-LABEL: define{{.*}} i32 @test_left_variable
7 int test_left_variable(unsigned _BitInt(5) b, unsigned _BitInt(2) e) {
8 // CHECK: load i8
9 // CHECK: [[E_REG:%.+]] = trunc i8 {{.*}} to [[E_SIZE:i2]]
10 // CHECK: icmp ule [[E_SIZE]] [[E_REG]], -1,
11 return b << e;
14 // CHECK-LABEL: define{{.*}} i32 @test_right_variable
15 int test_right_variable(unsigned _BitInt(2) b, unsigned _BitInt(3) e) {
16 // CHECK: load i8
17 // CHECK: [[E_REG:%.+]] = trunc i8 {{.*}} to [[E_SIZE:i3]]
18 // CHECK: icmp ule [[E_SIZE]] [[E_REG]], 1,
19 return b >> e;
22 // Old code generation would give false positives on left shifts when:
23 // value(e) > (width(b) - 1 % 2 ** width(e))
24 // CHECK-LABEL: define{{.*}} i32 @test_left_literal
25 int test_left_literal(unsigned _BitInt(5) b) {
26 // CHECK-NOT: br i1 false, label %cont, label %handler.shift_out_of_bounds
27 // CHECK: br i1 true, label %cont, label %handler.shift_out_of_bounds
28 return b << 3uwb;
31 // Old code generation would give false positives on right shifts when:
32 // (value(e) % 2 ** width(b)) < width(b)
33 // CHECK-LABEL: define{{.*}} i32 @test_right_literal
34 int test_right_literal(unsigned _BitInt(2) b) {
35 // CHECK-NOT: br i1 true, label %cont, label %handler.shift_out_of_bounds
36 // CHECK: br i1 false, label %cont, label %handler.shift_out_of_bounds
37 return b >> 4uwb;
40 // CHECK-LABEL: define{{.*}} i32 @test_signed_left_variable
41 int test_signed_left_variable(unsigned _BitInt(15) b, _BitInt(2) e) {
42 // CHECK: load i8
43 // CHECK: [[E_REG:%.+]] = trunc i8 {{.*}} to [[E_SIZE:i2]]
44 // CHECK: icmp ule [[E_SIZE]] [[E_REG]], 1,
45 return b << e;
48 // CHECK-LABEL: define{{.*}} i32 @test_signed_right_variable
49 int test_signed_right_variable(unsigned _BitInt(32) b, _BitInt(4) e) {
50 // CHECK: load i8
51 // CHECK: [[E_REG:%.+]] = trunc i8 {{.*}} to [[E_SIZE:i4]]
52 // CHECK: icmp ule [[E_SIZE]] [[E_REG]], 7,
53 return b >> e;
56 // CHECK-LABEL: define{{.*}} i32 @test_signed_left_literal
57 int test_signed_left_literal(unsigned _BitInt(16) b) {
58 // CHECK-NOT: br i1 true, label %cont, label %handler.shift_out_of_bounds
59 // CHECK: br i1 false, label %cont, label %handler.shift_out_of_bounds
60 return b << (_BitInt(4))-2wb;
63 // CHECK-LABEL: define{{.*}} i32 @test_signed_right_literal
64 int test_signed_right_literal(unsigned _BitInt(16) b) {
65 // CHECK-NOT: br i1 true, label %cont, label %handler.shift_out_of_bounds
66 // CHECK: br i1 false, label %cont, label %handler.shift_out_of_bounds
67 return b >> (_BitInt(4))-8wb;