| 1 | // Test instrumentation of general constructs in objective C. |
| 2 | |
| 3 | // RUN: %clang_cc1 -triple x86_64-apple-macosx10.9 -main-file-name objc-general.m %s -o - -emit-llvm -fblocks -fprofile-instrument=clang | FileCheck -check-prefix=PGOGEN %s |
| 4 | |
| 5 | // RUN: llvm-profdata merge %S/Inputs/objc-general.proftext -o %t.profdata |
| 6 | // RUN: %clang_cc1 -triple x86_64-apple-macosx10.9 -main-file-name objc-general.m %s -o - -emit-llvm -fblocks -fprofile-instrument-use-path=%t.profdata 2>&1 | FileCheck -check-prefix=PGOUSE %s |
| 7 | |
| 8 | // PGOUSE-NOT: warning: profile data may be out of date |
| 9 | |
| 10 | #ifdef HAVE_FOUNDATION |
| 11 | |
| 12 | // Use this to build an instrumented version to regenerate the input file. |
| 13 | #import <Foundation/Foundation.h> |
| 14 | |
| 15 | #else |
| 16 | |
| 17 | // Minimal definitions to get this to compile without Foundation.h. |
| 18 | |
| 19 | @protocol NSObject |
| 20 | @end |
| 21 | |
| 22 | @interface NSObject <NSObject> |
| 23 | - (id)init; |
| 24 | + (id)alloc; |
| 25 | @end |
| 26 | |
| 27 | struct NSFastEnumerationState; |
| 28 | @interface NSArray : NSObject |
| 29 | - (unsigned long) countByEnumeratingWithState: (struct NSFastEnumerationState*) state |
| 30 | objects: (id*) buffer |
| 31 | count: (unsigned long) bufferSize; |
| 32 | +(NSArray*) arrayWithObjects: (id) first, ...; |
| 33 | @end; |
| 34 | #endif |
| 35 | |
| 36 | // PGOGEN: @[[FRC:"__profc_objc_general.m_\+\[A foreach_\]"]] = private global [2 x i64] zeroinitializer |
| 37 | // PGOGEN: @[[BLC:"__profc_objc_general.m___13\+\[A foreach_\]_block_invoke"]] = private global [2 x i64] zeroinitializer |
| 38 | // PGOGEN: @[[MAC:__profc_main]] = private global [1 x i64] zeroinitializer |
| 39 | |
| 40 | @interface A : NSObject |
| 41 | + (void)foreach: (NSArray *)array; |
| 42 | @end |
| 43 | |
| 44 | @implementation A |
| 45 | // PGOGEN: define {{.*}}+[A foreach:] |
| 46 | // PGOUSE: define {{.*}}+[A foreach:] |
| 47 | // PGOGEN: store {{.*}} @[[FRC]], i64 0, i64 0 |
| 48 | + (void)foreach: (NSArray *)array |
| 49 | { |
| 50 | __block id result; |
| 51 | // PGOGEN: store {{.*}} @[[FRC]], i64 0, i64 1 |
| 52 | // PGOUSE: br {{.*}} !prof ![[FR1:[0-9]+]] |
| 53 | // PGOUSE: br {{.*}} !prof ![[FR2:[0-9]+]] |
| 54 | for (id x in array) { |
| 55 | // PGOGEN: define {{.*}}_block_invoke |
| 56 | // PGOUSE: define {{.*}}_block_invoke |
| 57 | // PGOGEN: store {{.*}} @[[BLC]], i64 0, i64 0 |
| 58 | ^{ static int init = 0; |
| 59 | // PGOGEN: store {{.*}} @[[BLC]], i64 0, i64 1 |
| 60 | // PGOUSE: br {{.*}} !prof ![[BL1:[0-9]+]] |
| 61 | if (init) |
| 62 | result = x; |
| 63 | init = 1; }(); |
| 64 | } |
| 65 | } |
| 66 | @end |
| 67 | |
| 68 | void nested_objc_for_ranges(NSArray *arr) { |
| 69 | int x = 0; |
| 70 | for (id a in arr) |
| 71 | for (id b in arr) |
| 72 | ++x; |
| 73 | } |
| 74 | |
| 75 | void consecutive_objc_for_ranges(NSArray *arr) { |
| 76 | int x = 0; |
| 77 | for (id a in arr) {} |
| 78 | for (id b in arr) |
| 79 | ++x; |
| 80 | } |
| 81 | |
| 82 | // PGOUSE-DAG: ![[FR1]] = !{!"branch_weights", i32 2, i32 3} |
| 83 | // PGOUSE-DAG: ![[FR2]] = !{!"branch_weights", i32 3, i32 2} |
| 84 | // PGOUSE-DAG: ![[BL1]] = !{!"branch_weights", i32 2, i32 2} |
| 85 | |
| 86 | int main(int argc, const char *argv[]) { |
| 87 | A *a = [[A alloc] init]; |
| 88 | NSArray *array = [NSArray arrayWithObjects: @"0", @"1", (void*)0]; |
| 89 | [A foreach: array]; |
| 90 | return 0; |
| 91 | } |
| 92 | |