1*f4a2713aSLionel Sambuc // Test frontend handling of __sync builtins. 2*f4a2713aSLionel Sambuc // Modified from a gcc testcase. 3*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm %s -o - | FileCheck %s 4*f4a2713aSLionel Sambuc 5*f4a2713aSLionel Sambuc signed char sc; 6*f4a2713aSLionel Sambuc unsigned char uc; 7*f4a2713aSLionel Sambuc signed short ss; 8*f4a2713aSLionel Sambuc unsigned short us; 9*f4a2713aSLionel Sambuc signed int si; 10*f4a2713aSLionel Sambuc unsigned int ui; 11*f4a2713aSLionel Sambuc signed long long sll; 12*f4a2713aSLionel Sambuc unsigned long long ull; 13*f4a2713aSLionel Sambuc 14*f4a2713aSLionel Sambuc void test_op_ignore (void) // CHECK-LABEL: define void @test_op_ignore 15*f4a2713aSLionel Sambuc { 16*f4a2713aSLionel Sambuc (void) __sync_fetch_and_add (&sc, 1); // CHECK: atomicrmw add i8 17*f4a2713aSLionel Sambuc (void) __sync_fetch_and_add (&uc, 1); // CHECK: atomicrmw add i8 18*f4a2713aSLionel Sambuc (void) __sync_fetch_and_add (&ss, 1); // CHECK: atomicrmw add i16 19*f4a2713aSLionel Sambuc (void) __sync_fetch_and_add (&us, 1); // CHECK: atomicrmw add i16 20*f4a2713aSLionel Sambuc (void) __sync_fetch_and_add (&si, 1); // CHECK: atomicrmw add i32 21*f4a2713aSLionel Sambuc (void) __sync_fetch_and_add (&ui, 1); // CHECK: atomicrmw add i32 22*f4a2713aSLionel Sambuc (void) __sync_fetch_and_add (&sll, 1); // CHECK: atomicrmw add i64 23*f4a2713aSLionel Sambuc (void) __sync_fetch_and_add (&ull, 1); // CHECK: atomicrmw add i64 24*f4a2713aSLionel Sambuc 25*f4a2713aSLionel Sambuc (void) __sync_fetch_and_sub (&sc, 1); // CHECK: atomicrmw sub i8 26*f4a2713aSLionel Sambuc (void) __sync_fetch_and_sub (&uc, 1); // CHECK: atomicrmw sub i8 27*f4a2713aSLionel Sambuc (void) __sync_fetch_and_sub (&ss, 1); // CHECK: atomicrmw sub i16 28*f4a2713aSLionel Sambuc (void) __sync_fetch_and_sub (&us, 1); // CHECK: atomicrmw sub i16 29*f4a2713aSLionel Sambuc (void) __sync_fetch_and_sub (&si, 1); // CHECK: atomicrmw sub i32 30*f4a2713aSLionel Sambuc (void) __sync_fetch_and_sub (&ui, 1); // CHECK: atomicrmw sub i32 31*f4a2713aSLionel Sambuc (void) __sync_fetch_and_sub (&sll, 1); // CHECK: atomicrmw sub i64 32*f4a2713aSLionel Sambuc (void) __sync_fetch_and_sub (&ull, 1); // CHECK: atomicrmw sub i64 33*f4a2713aSLionel Sambuc 34*f4a2713aSLionel Sambuc (void) __sync_fetch_and_or (&sc, 1); // CHECK: atomicrmw or i8 35*f4a2713aSLionel Sambuc (void) __sync_fetch_and_or (&uc, 1); // CHECK: atomicrmw or i8 36*f4a2713aSLionel Sambuc (void) __sync_fetch_and_or (&ss, 1); // CHECK: atomicrmw or i16 37*f4a2713aSLionel Sambuc (void) __sync_fetch_and_or (&us, 1); // CHECK: atomicrmw or i16 38*f4a2713aSLionel Sambuc (void) __sync_fetch_and_or (&si, 1); // CHECK: atomicrmw or i32 39*f4a2713aSLionel Sambuc (void) __sync_fetch_and_or (&ui, 1); // CHECK: atomicrmw or i32 40*f4a2713aSLionel Sambuc (void) __sync_fetch_and_or (&sll, 1); // CHECK: atomicrmw or i64 41*f4a2713aSLionel Sambuc (void) __sync_fetch_and_or (&ull, 1); // CHECK: atomicrmw or i64 42*f4a2713aSLionel Sambuc 43*f4a2713aSLionel Sambuc (void) __sync_fetch_and_xor (&sc, 1); // CHECK: atomicrmw xor i8 44*f4a2713aSLionel Sambuc (void) __sync_fetch_and_xor (&uc, 1); // CHECK: atomicrmw xor i8 45*f4a2713aSLionel Sambuc (void) __sync_fetch_and_xor (&ss, 1); // CHECK: atomicrmw xor i16 46*f4a2713aSLionel Sambuc (void) __sync_fetch_and_xor (&us, 1); // CHECK: atomicrmw xor i16 47*f4a2713aSLionel Sambuc (void) __sync_fetch_and_xor (&si, 1); // CHECK: atomicrmw xor i32 48*f4a2713aSLionel Sambuc (void) __sync_fetch_and_xor (&ui, 1); // CHECK: atomicrmw xor i32 49*f4a2713aSLionel Sambuc (void) __sync_fetch_and_xor (&sll, 1); // CHECK: atomicrmw xor i64 50*f4a2713aSLionel Sambuc (void) __sync_fetch_and_xor (&ull, 1); // CHECK: atomicrmw xor i64 51*f4a2713aSLionel Sambuc 52*f4a2713aSLionel Sambuc (void) __sync_fetch_and_and (&sc, 1); // CHECK: atomicrmw and i8 53*f4a2713aSLionel Sambuc (void) __sync_fetch_and_and (&uc, 1); // CHECK: atomicrmw and i8 54*f4a2713aSLionel Sambuc (void) __sync_fetch_and_and (&ss, 1); // CHECK: atomicrmw and i16 55*f4a2713aSLionel Sambuc (void) __sync_fetch_and_and (&us, 1); // CHECK: atomicrmw and i16 56*f4a2713aSLionel Sambuc (void) __sync_fetch_and_and (&si, 1); // CHECK: atomicrmw and i32 57*f4a2713aSLionel Sambuc (void) __sync_fetch_and_and (&ui, 1); // CHECK: atomicrmw and i32 58*f4a2713aSLionel Sambuc (void) __sync_fetch_and_and (&sll, 1); // CHECK: atomicrmw and i64 59*f4a2713aSLionel Sambuc (void) __sync_fetch_and_and (&ull, 1); // CHECK: atomicrmw and i64 60*f4a2713aSLionel Sambuc 61*f4a2713aSLionel Sambuc } 62*f4a2713aSLionel Sambuc 63*f4a2713aSLionel Sambuc void test_fetch_and_op (void) // CHECK-LABEL: define void @test_fetch_and_op 64*f4a2713aSLionel Sambuc { 65*f4a2713aSLionel Sambuc sc = __sync_fetch_and_add (&sc, 11); // CHECK: atomicrmw add 66*f4a2713aSLionel Sambuc uc = __sync_fetch_and_add (&uc, 11); // CHECK: atomicrmw add 67*f4a2713aSLionel Sambuc ss = __sync_fetch_and_add (&ss, 11); // CHECK: atomicrmw add 68*f4a2713aSLionel Sambuc us = __sync_fetch_and_add (&us, 11); // CHECK: atomicrmw add 69*f4a2713aSLionel Sambuc si = __sync_fetch_and_add (&si, 11); // CHECK: atomicrmw add 70*f4a2713aSLionel Sambuc ui = __sync_fetch_and_add (&ui, 11); // CHECK: atomicrmw add 71*f4a2713aSLionel Sambuc sll = __sync_fetch_and_add (&sll, 11); // CHECK: atomicrmw add 72*f4a2713aSLionel Sambuc ull = __sync_fetch_and_add (&ull, 11); // CHECK: atomicrmw add 73*f4a2713aSLionel Sambuc 74*f4a2713aSLionel Sambuc sc = __sync_fetch_and_sub (&sc, 11); // CHECK: atomicrmw sub 75*f4a2713aSLionel Sambuc uc = __sync_fetch_and_sub (&uc, 11); // CHECK: atomicrmw sub 76*f4a2713aSLionel Sambuc ss = __sync_fetch_and_sub (&ss, 11); // CHECK: atomicrmw sub 77*f4a2713aSLionel Sambuc us = __sync_fetch_and_sub (&us, 11); // CHECK: atomicrmw sub 78*f4a2713aSLionel Sambuc si = __sync_fetch_and_sub (&si, 11); // CHECK: atomicrmw sub 79*f4a2713aSLionel Sambuc ui = __sync_fetch_and_sub (&ui, 11); // CHECK: atomicrmw sub 80*f4a2713aSLionel Sambuc sll = __sync_fetch_and_sub (&sll, 11); // CHECK: atomicrmw sub 81*f4a2713aSLionel Sambuc ull = __sync_fetch_and_sub (&ull, 11); // CHECK: atomicrmw sub 82*f4a2713aSLionel Sambuc 83*f4a2713aSLionel Sambuc sc = __sync_fetch_and_or (&sc, 11); // CHECK: atomicrmw or 84*f4a2713aSLionel Sambuc uc = __sync_fetch_and_or (&uc, 11); // CHECK: atomicrmw or 85*f4a2713aSLionel Sambuc ss = __sync_fetch_and_or (&ss, 11); // CHECK: atomicrmw or 86*f4a2713aSLionel Sambuc us = __sync_fetch_and_or (&us, 11); // CHECK: atomicrmw or 87*f4a2713aSLionel Sambuc si = __sync_fetch_and_or (&si, 11); // CHECK: atomicrmw or 88*f4a2713aSLionel Sambuc ui = __sync_fetch_and_or (&ui, 11); // CHECK: atomicrmw or 89*f4a2713aSLionel Sambuc sll = __sync_fetch_and_or (&sll, 11); // CHECK: atomicrmw or 90*f4a2713aSLionel Sambuc ull = __sync_fetch_and_or (&ull, 11); // CHECK: atomicrmw or 91*f4a2713aSLionel Sambuc 92*f4a2713aSLionel Sambuc sc = __sync_fetch_and_xor (&sc, 11); // CHECK: atomicrmw xor 93*f4a2713aSLionel Sambuc uc = __sync_fetch_and_xor (&uc, 11); // CHECK: atomicrmw xor 94*f4a2713aSLionel Sambuc ss = __sync_fetch_and_xor (&ss, 11); // CHECK: atomicrmw xor 95*f4a2713aSLionel Sambuc us = __sync_fetch_and_xor (&us, 11); // CHECK: atomicrmw xor 96*f4a2713aSLionel Sambuc si = __sync_fetch_and_xor (&si, 11); // CHECK: atomicrmw xor 97*f4a2713aSLionel Sambuc ui = __sync_fetch_and_xor (&ui, 11); // CHECK: atomicrmw xor 98*f4a2713aSLionel Sambuc sll = __sync_fetch_and_xor (&sll, 11); // CHECK: atomicrmw xor 99*f4a2713aSLionel Sambuc ull = __sync_fetch_and_xor (&ull, 11); // CHECK: atomicrmw xor 100*f4a2713aSLionel Sambuc 101*f4a2713aSLionel Sambuc sc = __sync_fetch_and_and (&sc, 11); // CHECK: atomicrmw and 102*f4a2713aSLionel Sambuc uc = __sync_fetch_and_and (&uc, 11); // CHECK: atomicrmw and 103*f4a2713aSLionel Sambuc ss = __sync_fetch_and_and (&ss, 11); // CHECK: atomicrmw and 104*f4a2713aSLionel Sambuc us = __sync_fetch_and_and (&us, 11); // CHECK: atomicrmw and 105*f4a2713aSLionel Sambuc si = __sync_fetch_and_and (&si, 11); // CHECK: atomicrmw and 106*f4a2713aSLionel Sambuc ui = __sync_fetch_and_and (&ui, 11); // CHECK: atomicrmw and 107*f4a2713aSLionel Sambuc sll = __sync_fetch_and_and (&sll, 11); // CHECK: atomicrmw and 108*f4a2713aSLionel Sambuc ull = __sync_fetch_and_and (&ull, 11); // CHECK: atomicrmw and 109*f4a2713aSLionel Sambuc 110*f4a2713aSLionel Sambuc } 111*f4a2713aSLionel Sambuc 112*f4a2713aSLionel Sambuc void test_op_and_fetch (void) 113*f4a2713aSLionel Sambuc { 114*f4a2713aSLionel Sambuc sc = __sync_add_and_fetch (&sc, uc); // CHECK: atomicrmw add 115*f4a2713aSLionel Sambuc uc = __sync_add_and_fetch (&uc, uc); // CHECK: atomicrmw add 116*f4a2713aSLionel Sambuc ss = __sync_add_and_fetch (&ss, uc); // CHECK: atomicrmw add 117*f4a2713aSLionel Sambuc us = __sync_add_and_fetch (&us, uc); // CHECK: atomicrmw add 118*f4a2713aSLionel Sambuc si = __sync_add_and_fetch (&si, uc); // CHECK: atomicrmw add 119*f4a2713aSLionel Sambuc ui = __sync_add_and_fetch (&ui, uc); // CHECK: atomicrmw add 120*f4a2713aSLionel Sambuc sll = __sync_add_and_fetch (&sll, uc); // CHECK: atomicrmw add 121*f4a2713aSLionel Sambuc ull = __sync_add_and_fetch (&ull, uc); // CHECK: atomicrmw add 122*f4a2713aSLionel Sambuc 123*f4a2713aSLionel Sambuc sc = __sync_sub_and_fetch (&sc, uc); // CHECK: atomicrmw sub 124*f4a2713aSLionel Sambuc uc = __sync_sub_and_fetch (&uc, uc); // CHECK: atomicrmw sub 125*f4a2713aSLionel Sambuc ss = __sync_sub_and_fetch (&ss, uc); // CHECK: atomicrmw sub 126*f4a2713aSLionel Sambuc us = __sync_sub_and_fetch (&us, uc); // CHECK: atomicrmw sub 127*f4a2713aSLionel Sambuc si = __sync_sub_and_fetch (&si, uc); // CHECK: atomicrmw sub 128*f4a2713aSLionel Sambuc ui = __sync_sub_and_fetch (&ui, uc); // CHECK: atomicrmw sub 129*f4a2713aSLionel Sambuc sll = __sync_sub_and_fetch (&sll, uc); // CHECK: atomicrmw sub 130*f4a2713aSLionel Sambuc ull = __sync_sub_and_fetch (&ull, uc); // CHECK: atomicrmw sub 131*f4a2713aSLionel Sambuc 132*f4a2713aSLionel Sambuc sc = __sync_or_and_fetch (&sc, uc); // CHECK: atomicrmw or 133*f4a2713aSLionel Sambuc uc = __sync_or_and_fetch (&uc, uc); // CHECK: atomicrmw or 134*f4a2713aSLionel Sambuc ss = __sync_or_and_fetch (&ss, uc); // CHECK: atomicrmw or 135*f4a2713aSLionel Sambuc us = __sync_or_and_fetch (&us, uc); // CHECK: atomicrmw or 136*f4a2713aSLionel Sambuc si = __sync_or_and_fetch (&si, uc); // CHECK: atomicrmw or 137*f4a2713aSLionel Sambuc ui = __sync_or_and_fetch (&ui, uc); // CHECK: atomicrmw or 138*f4a2713aSLionel Sambuc sll = __sync_or_and_fetch (&sll, uc); // CHECK: atomicrmw or 139*f4a2713aSLionel Sambuc ull = __sync_or_and_fetch (&ull, uc); // CHECK: atomicrmw or 140*f4a2713aSLionel Sambuc 141*f4a2713aSLionel Sambuc sc = __sync_xor_and_fetch (&sc, uc); // CHECK: atomicrmw xor 142*f4a2713aSLionel Sambuc uc = __sync_xor_and_fetch (&uc, uc); // CHECK: atomicrmw xor 143*f4a2713aSLionel Sambuc ss = __sync_xor_and_fetch (&ss, uc); // CHECK: atomicrmw xor 144*f4a2713aSLionel Sambuc us = __sync_xor_and_fetch (&us, uc); // CHECK: atomicrmw xor 145*f4a2713aSLionel Sambuc si = __sync_xor_and_fetch (&si, uc); // CHECK: atomicrmw xor 146*f4a2713aSLionel Sambuc ui = __sync_xor_and_fetch (&ui, uc); // CHECK: atomicrmw xor 147*f4a2713aSLionel Sambuc sll = __sync_xor_and_fetch (&sll, uc); // CHECK: atomicrmw xor 148*f4a2713aSLionel Sambuc ull = __sync_xor_and_fetch (&ull, uc); // CHECK: atomicrmw xor 149*f4a2713aSLionel Sambuc 150*f4a2713aSLionel Sambuc sc = __sync_and_and_fetch (&sc, uc); // CHECK: atomicrmw and 151*f4a2713aSLionel Sambuc uc = __sync_and_and_fetch (&uc, uc); // CHECK: atomicrmw and 152*f4a2713aSLionel Sambuc ss = __sync_and_and_fetch (&ss, uc); // CHECK: atomicrmw and 153*f4a2713aSLionel Sambuc us = __sync_and_and_fetch (&us, uc); // CHECK: atomicrmw and 154*f4a2713aSLionel Sambuc si = __sync_and_and_fetch (&si, uc); // CHECK: atomicrmw and 155*f4a2713aSLionel Sambuc ui = __sync_and_and_fetch (&ui, uc); // CHECK: atomicrmw and 156*f4a2713aSLionel Sambuc sll = __sync_and_and_fetch (&sll, uc); // CHECK: atomicrmw and 157*f4a2713aSLionel Sambuc ull = __sync_and_and_fetch (&ull, uc); // CHECK: atomicrmw and 158*f4a2713aSLionel Sambuc 159*f4a2713aSLionel Sambuc } 160*f4a2713aSLionel Sambuc 161*f4a2713aSLionel Sambuc void test_compare_and_swap (void) 162*f4a2713aSLionel Sambuc { 163*f4a2713aSLionel Sambuc sc = __sync_val_compare_and_swap (&sc, uc, sc); // CHECK: cmpxchg i8 164*f4a2713aSLionel Sambuc uc = __sync_val_compare_and_swap (&uc, uc, sc); // CHECK: cmpxchg i8 165*f4a2713aSLionel Sambuc ss = __sync_val_compare_and_swap (&ss, uc, sc); // CHECK: cmpxchg i16 166*f4a2713aSLionel Sambuc us = __sync_val_compare_and_swap (&us, uc, sc); // CHECK: cmpxchg i16 167*f4a2713aSLionel Sambuc si = __sync_val_compare_and_swap (&si, uc, sc); // CHECK: cmpxchg i32 168*f4a2713aSLionel Sambuc ui = __sync_val_compare_and_swap (&ui, uc, sc); // CHECK: cmpxchg i32 169*f4a2713aSLionel Sambuc sll = __sync_val_compare_and_swap (&sll, uc, sc); // CHECK: cmpxchg i64 170*f4a2713aSLionel Sambuc ull = __sync_val_compare_and_swap (&ull, uc, sc); // CHECK: cmpxchg i64 171*f4a2713aSLionel Sambuc 172*f4a2713aSLionel Sambuc ui = __sync_bool_compare_and_swap (&sc, uc, sc); // CHECK: cmpxchg 173*f4a2713aSLionel Sambuc ui = __sync_bool_compare_and_swap (&uc, uc, sc); // CHECK: cmpxchg 174*f4a2713aSLionel Sambuc ui = __sync_bool_compare_and_swap (&ss, uc, sc); // CHECK: cmpxchg 175*f4a2713aSLionel Sambuc ui = __sync_bool_compare_and_swap (&us, uc, sc); // CHECK: cmpxchg 176*f4a2713aSLionel Sambuc ui = __sync_bool_compare_and_swap (&si, uc, sc); // CHECK: cmpxchg 177*f4a2713aSLionel Sambuc ui = __sync_bool_compare_and_swap (&ui, uc, sc); // CHECK: cmpxchg 178*f4a2713aSLionel Sambuc ui = __sync_bool_compare_and_swap (&sll, uc, sc); // CHECK: cmpxchg 179*f4a2713aSLionel Sambuc ui = __sync_bool_compare_and_swap (&ull, uc, sc); // CHECK: cmpxchg 180*f4a2713aSLionel Sambuc } 181*f4a2713aSLionel Sambuc 182*f4a2713aSLionel Sambuc void test_lock (void) 183*f4a2713aSLionel Sambuc { 184*f4a2713aSLionel Sambuc sc = __sync_lock_test_and_set (&sc, 1); // CHECK: atomicrmw xchg i8 185*f4a2713aSLionel Sambuc uc = __sync_lock_test_and_set (&uc, 1); // CHECK: atomicrmw xchg i8 186*f4a2713aSLionel Sambuc ss = __sync_lock_test_and_set (&ss, 1); // CHECK: atomicrmw xchg i16 187*f4a2713aSLionel Sambuc us = __sync_lock_test_and_set (&us, 1); // CHECK: atomicrmw xchg i16 188*f4a2713aSLionel Sambuc si = __sync_lock_test_and_set (&si, 1); // CHECK: atomicrmw xchg i32 189*f4a2713aSLionel Sambuc ui = __sync_lock_test_and_set (&ui, 1); // CHECK: atomicrmw xchg i32 190*f4a2713aSLionel Sambuc sll = __sync_lock_test_and_set (&sll, 1); // CHECK: atomicrmw xchg i64 191*f4a2713aSLionel Sambuc ull = __sync_lock_test_and_set (&ull, 1); // CHECK: atomicrmw xchg i64 192*f4a2713aSLionel Sambuc 193*f4a2713aSLionel Sambuc __sync_synchronize (); // CHECK: fence seq_cst 194*f4a2713aSLionel Sambuc 195*f4a2713aSLionel Sambuc __sync_lock_release (&sc); // CHECK: store atomic {{.*}} release, align 1 196*f4a2713aSLionel Sambuc __sync_lock_release (&uc); // CHECK: store atomic {{.*}} release, align 1 197*f4a2713aSLionel Sambuc __sync_lock_release (&ss); // CHECK: store atomic {{.*}} release, align 2 198*f4a2713aSLionel Sambuc __sync_lock_release (&us); /// CHECK: store atomic {{.*}} release, align 2 199*f4a2713aSLionel Sambuc __sync_lock_release (&si); // CHECK: store atomic {{.*}} release, align 4 200*f4a2713aSLionel Sambuc __sync_lock_release (&ui); // CHECK: store atomic {{.*}} release, align 4 201*f4a2713aSLionel Sambuc __sync_lock_release (&sll); // CHECK: store atomic {{.*}} release, align 8 202*f4a2713aSLionel Sambuc __sync_lock_release (&ull); // CHECK: store atomic {{.*}} release, align 8 203*f4a2713aSLionel Sambuc } 204