Home | History | Annotate | Download | only in CodeGenObjC
      1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-runtime-has-weak -O2 -disable-llvm-optzns -o - %s | FileCheck %s
      2 
      3 void test0(_Bool cond) {
      4   id test0_helper(void) __attribute__((ns_returns_retained));
      5 
      6   // CHECK:      define void @test0(
      7   // CHECK:      [[COND:%.*]] = alloca i8,
      8   // CHECK-NEXT: [[X:%.*]] = alloca i8*,
      9   // CHECK-NEXT: [[RELVAL:%.*]] = alloca i8*
     10   // CHECK-NEXT: [[RELCOND:%.*]] = alloca i1
     11   // CHECK-NEXT: zext
     12   // CHECK-NEXT: store
     13   // CHECK-NEXT: [[T0:%.*]] = load i8* [[COND]]
     14   // CHECK-NEXT: [[T1:%.*]] = trunc i8 [[T0]] to i1
     15   // CHECK-NEXT: store i1 false, i1* [[RELCOND]]
     16   // CHECK-NEXT: br i1 [[T1]],
     17   // CHECK:      br label
     18   // CHECK:      [[CALL:%.*]] = call i8* @test0_helper()
     19   // CHECK-NEXT: store i8* [[CALL]], i8** [[RELVAL]]
     20   // CHECK-NEXT: store i1 true, i1* [[RELCOND]]
     21   // CHECK-NEXT: br label
     22   // CHECK:      [[T0:%.*]] = phi i8* [ null, {{%.*}} ], [ [[CALL]], {{%.*}} ]
     23   // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retain(i8* [[T0]]) [[NUW:#[0-9]+]]
     24   // CHECK-NEXT: store i8* [[T1]], i8** [[X]],
     25   // CHECK-NEXT: [[REL:%.*]] = load i1* [[RELCOND]]
     26   // CHECK-NEXT: br i1 [[REL]],
     27   // CHECK:      [[T0:%.*]] = load i8** [[RELVAL]]
     28   // CHECK-NEXT: call void @objc_release(i8* [[T0]]) [[NUW]]
     29   // CHECK-NEXT: br label
     30   // CHECK:      [[T0:%.*]] = load i8** [[X]]
     31   // CHECK-NEXT: call void @objc_release(i8* [[T0]]) [[NUW]]
     32   // CHECK-NEXT: ret void
     33   id x = (cond ? 0 : test0_helper());
     34 }
     35 
     36 void test1(int cond) {
     37   __strong id strong;
     38   __weak id weak;
     39   extern void test1_sink(id *);
     40   test1_sink(cond ? &strong : 0);
     41   test1_sink(cond ? &weak : 0);
     42 
     43   // CHECK:    define void @test1(
     44   // CHECK:      [[COND:%.*]] = alloca i32
     45   // CHECK-NEXT: [[STRONG:%.*]] = alloca i8*
     46   // CHECK-NEXT: [[WEAK:%.*]] = alloca i8*
     47   // CHECK-NEXT: [[TEMP1:%.*]] = alloca i8*
     48   // CHECK-NEXT: [[TEMP2:%.*]] = alloca i8*
     49   // CHECK-NEXT: [[CONDCLEANUPSAVE:%.*]] = alloca i8*
     50   // CHECK-NEXT: [[CONDCLEANUP:%.*]] = alloca i1
     51   // CHECK-NEXT: store i32
     52   // CHECK-NEXT: store i8* null, i8** [[STRONG]]
     53   // CHECK-NEXT: call i8* @objc_initWeak(i8** [[WEAK]], i8* null)
     54 
     55   // CHECK-NEXT: [[T0:%.*]] = load i32* [[COND]]
     56   // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0
     57   // CHECK:      [[ARG:%.*]] = phi i8**
     58   // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
     59   // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP1]]
     60   // CHECK-NEXT: br i1 [[T0]],
     61   // CHECK:      [[T0:%.*]] = load i8** [[ARG]]
     62   // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP1]]
     63   // CHECK-NEXT: br label
     64   // CHECK:      call void @test1_sink(i8** [[T1]])
     65   // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
     66   // CHECK-NEXT: br i1 [[T0]],
     67   // CHECK:      [[T0:%.*]] = load i8** [[TEMP1]]
     68   // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retain(i8* [[T0]])
     69   // CHECK-NEXT: [[T2:%.*]] = load i8** [[ARG]]
     70   // CHECK-NEXT: store i8* [[T1]], i8** [[ARG]]
     71   // CHECK-NEXT: call void @objc_release(i8* [[T2]])
     72   // CHECK-NEXT: br label
     73 
     74   // CHECK:      [[T0:%.*]] = load i32* [[COND]]
     75   // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0
     76   // CHECK:      [[ARG:%.*]] = phi i8**
     77   // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
     78   // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP2]]
     79   // CHECK-NEXT: store i1 false, i1* [[CONDCLEANUP]]
     80   // CHECK-NEXT: br i1 [[T0]],
     81   // CHECK:      [[T0:%.*]] = call i8* @objc_loadWeakRetained(i8** [[ARG]])
     82   // CHECK-NEXT: store i8* [[T0]], i8** [[CONDCLEANUPSAVE]]
     83   // CHECK-NEXT: store i1 true, i1* [[CONDCLEANUP]]
     84   // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP2]]
     85   // CHECK-NEXT: br label
     86   // CHECK:      call void @test1_sink(i8** [[T1]])
     87   // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
     88   // CHECK-NEXT: br i1 [[T0]],
     89   // CHECK:      [[T0:%.*]] = load i8** [[TEMP2]]
     90   // CHECK-NEXT: call i8* @objc_storeWeak(i8** [[ARG]], i8* [[T0]])
     91   // CHECK-NEXT: br label
     92 
     93   // CHECK:      call void @objc_destroyWeak(i8** [[WEAK]])
     94   // CHECK:      ret void
     95 }
     96 
     97 // rdar://13113981
     98 // Test that, when emitting an expression at +1 that we can't peephole,
     99 // we emit the retain inside the full-expression.  If we ever peephole
    100 // +1s of conditional expressions (which we probably ought to), we'll
    101 // need to find another example of something we need to do this for.
    102 void test2(int cond) {
    103   extern id test2_producer(void);
    104   for (id obj in cond ? test2_producer() : (void*) 0) {
    105   }
    106 
    107   // CHECK:    define void @test2(
    108   // CHECK:      [[COND:%.*]] = alloca i32,
    109   // CHECK:      alloca i8*
    110   // CHECK:      [[CLEANUP_SAVE:%.*]] = alloca i8*
    111   // CHECK:      [[RUN_CLEANUP:%.*]] = alloca i1
    112   //   Evaluate condition; cleanup disabled by default.
    113   // CHECK:      [[T0:%.*]] = load i32* [[COND]],
    114   // CHECK-NEXT: icmp ne i32 [[T0]], 0
    115   // CHECK-NEXT: store i1 false, i1* [[RUN_CLEANUP]]
    116   // CHECK-NEXT: br i1
    117   //   Within true branch, cleanup enabled.
    118   // CHECK:      [[T0:%.*]] = call i8* @test2_producer()
    119   // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T0]])
    120   // CHECK-NEXT: store i8* [[T1]], i8** [[CLEANUP_SAVE]]
    121   // CHECK-NEXT: store i1 true, i1* [[RUN_CLEANUP]]
    122   // CHECK-NEXT: br label
    123   //   Join point for conditional operator; retain immediately.
    124   // CHECK:      [[T0:%.*]] = phi i8* [ [[T1]], {{%.*}} ], [ null, {{%.*}} ]
    125   // CHECK-NEXT: [[RESULT:%.*]] = call i8* @objc_retain(i8* [[T0]])
    126   //   Leaving full-expression; run conditional cleanup.
    127   // CHECK-NEXT: [[T0:%.*]] = load i1* [[RUN_CLEANUP]]
    128   // CHECK-NEXT: br i1 [[T0]]
    129   // CHECK:      [[T0:%.*]] = load i8** [[CLEANUP_SAVE]]
    130   // CHECK-NEXT: call void @objc_release(i8* [[T0]])
    131   // CHECK-NEXT: br label
    132   //   And way down at the end of the loop:
    133   // CHECK:      call void @objc_release(i8* [[RESULT]])
    134 }
    135 
    136 // CHECK: attributes [[NUW]] = { nounwind }
    137