Sanjoy Das | a085cfc | 2016-04-21 19:26:45 +0000 | [diff] [blame] | 1 | ; RUN: opt -instcombine -S < %s | FileCheck %s |
| 2 | |
| 3 | @gp = global i32* null, align 8 |
| 4 | |
| 5 | declare i8* @malloc(i64) #1 |
| 6 | |
| 7 | define i1 @compare_global_trivialeq() { |
| 8 | %m = call i8* @malloc(i64 4) |
| 9 | %bc = bitcast i8* %m to i32* |
| 10 | %lgp = load i32*, i32** @gp, align 8 |
| 11 | %cmp = icmp eq i32* %bc, %lgp |
| 12 | ret i1 %cmp |
| 13 | ; CHECK-LABEL: compare_global_trivialeq |
| 14 | ; CHECK: ret i1 false |
| 15 | } |
| 16 | |
| 17 | define i1 @compare_global_trivialne() { |
| 18 | %m = call i8* @malloc(i64 4) |
| 19 | %bc = bitcast i8* %m to i32* |
| 20 | %lgp = load i32*, i32** @gp, align 8 |
| 21 | %cmp = icmp ne i32* %bc, %lgp |
| 22 | ret i1 %cmp |
| 23 | ; CHECK-LABEL: compare_global_trivialne |
| 24 | ; CHECK: ret i1 true |
| 25 | } |
| 26 | |
| 27 | |
| 28 | ; Although the %m is marked nocapture in the deopt operand in call to function f, |
| 29 | ; we cannot remove the alloc site: call to malloc |
| 30 | ; FIXME: The comparison should fold to false irrespective of whether the call to malloc can be elided or not |
| 31 | declare void @f() |
| 32 | define i32 @compare_and_call_with_deopt() { |
| 33 | ; CHECK-LABEL: compare_and_call_with_deopt |
| 34 | %m = call i8* @malloc(i64 24) |
| 35 | %bc = bitcast i8* %m to i32* |
| 36 | %lgp = load i32*, i32** @gp, align 8 |
| 37 | %cmp = icmp eq i32* %bc, %lgp |
| 38 | %rt = zext i1 %cmp to i32 |
| 39 | tail call void @f() [ "deopt"(i8* %m) ] |
| 40 | ret i32 %rt |
| 41 | ; CHECK: ret i32 %rt |
| 42 | } |
Sanjoy Das | f97229d | 2016-04-22 20:52:25 +0000 | [diff] [blame^] | 43 | |
| 44 | define i1 @compare_distinct_mallocs() { |
| 45 | %m = call i8* @malloc(i64 4) |
| 46 | %n = call i8* @malloc(i64 4) |
| 47 | %cmp = icmp eq i8* %m, %n |
| 48 | ret i1 %cmp |
| 49 | ; CHECK-LABEL: compare_distinct_mallocs |
| 50 | ; CHECK: ret i1 false |
| 51 | } |
| 52 | |
| 53 | ; the compare is folded to true since the folding compare looks through bitcasts. |
| 54 | ; call to malloc and the bitcast instructions are elided after that since there are no uses of the malloc |
| 55 | define i1 @compare_samepointer_under_bitcast() { |
| 56 | %m = call i8* @malloc(i64 4) |
| 57 | %bc = bitcast i8* %m to i32* |
| 58 | %bcback = bitcast i32* %bc to i8* |
| 59 | %cmp = icmp eq i8* %m, %bcback |
| 60 | ret i1 %cmp |
| 61 | ; CHECK-LABEL: compare_samepointer_under_bitcast |
| 62 | ; CHECK: ret i1 true |
| 63 | } |
| 64 | |
| 65 | ; the compare is folded to true since the folding compare looks through bitcasts. |
| 66 | ; call to malloc and the bitcast instructions are elided after that since there are no uses of the malloc |
| 67 | define i1 @compare_samepointer_escaped() { |
| 68 | %m = call i8* @malloc(i64 4) |
| 69 | %bc = bitcast i8* %m to i32* |
| 70 | %bcback = bitcast i32* %bc to i8* |
| 71 | %cmp = icmp eq i8* %m, %bcback |
| 72 | call void @f() [ "deopt"(i8* %m) ] |
| 73 | ret i1 %cmp |
| 74 | ; CHECK-LABEL: compare_samepointer_escaped |
| 75 | ; CHECK-NEXT: %m = call i8* @malloc(i64 4) |
| 76 | ; CHECK-NEXT: call void @f() [ "deopt"(i8* %m) ] |
| 77 | ; CHECK: ret i1 true |
| 78 | } |
| 79 | |
| 80 | ; The malloc call for %m cannot be elided since it is used in the call to function f. |
| 81 | ; However, the cmp can be folded to true as %n doesnt escape and %m, %n are distinct allocations |
| 82 | define i1 @compare_distinct_pointer_escape() { |
| 83 | %m = call i8* @malloc(i64 4) |
| 84 | %n = call i8* @malloc(i64 4) |
| 85 | tail call void @f() [ "deopt"(i8* %m) ] |
| 86 | %cmp = icmp ne i8* %m, %n |
| 87 | ret i1 %cmp |
| 88 | ; CHECK-LABEL: compare_distinct_pointer_escape |
| 89 | ; CHECK-NEXT: %m = call i8* @malloc(i64 4) |
| 90 | ; CHECK-NEXT: tail call void @f() [ "deopt"(i8* %m) ] |
| 91 | ; CHECK-NEXT: ret i1 true |
| 92 | } |