Simplify pointer comparisons involving memory allocation functions
[oota-llvm.git] / test / Transforms / InstSimplify / noalias-ptr.ll
1 ; RUN: opt -instsimplify -S < %s | FileCheck %s
2 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3 target triple = "x86_64-unknown-linux-gnu"
4
5 @g1 = global i32 0, align 4
6
7 ; Make sure we can simplify away a pointer comparison between
8 ; dynamically-allocated memory and a local stack allocation.
9 ;   void p()
10 ;   {
11 ;     int *mData;
12 ;     int mStackData[10];
13 ;     mData = new int[12];
14 ;     if (mData != mStackData) {
15 ;       delete[] mData;
16 ;     }
17 ;   }
18
19 define void @_Z2p1v() #0 {
20   %mStackData = alloca [10 x i32], align 16
21   %1 = bitcast [10 x i32]* %mStackData to i8*
22   %2 = tail call noalias i8* @_Znam(i64 48) #4
23   %3 = bitcast i8* %2 to i32*
24   %4 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0
25   %5 = icmp eq i32* %3, %4
26   br i1 %5, label %7, label %6
27
28 ; CHECK-LABEL: @_Z2p1v
29 ; CHECK-NOT: icmp
30 ; CHECK: ret void
31
32 ; <label>:6                                       ; preds = %0
33   call void @_ZdaPv(i8* %2) #5
34   br label %7
35
36 ; <label>:7                                       ; preds = %0, %6
37   ret void
38 }
39
40 ; Also check a more-complicated case with multiple underlying objects.
41
42 define void @_Z2p2bb(i1 zeroext %b1, i1 zeroext %b2) #0 {
43   %mStackData = alloca [10 x i32], align 16
44   %1 = bitcast [10 x i32]* %mStackData to i8*
45   %2 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0
46   %3 = select i1 %b1, i32* %2, i32* @g1
47   %4 = tail call noalias i8* @_Znam(i64 48) #4
48   %5 = tail call noalias i8* @_Znam(i64 48) #4
49   %.v = select i1 %b2, i8* %4, i8* %5
50   %6 = bitcast i8* %.v to i32*
51   %7 = icmp eq i32* %6, %3
52   br i1 %7, label %9, label %8
53
54 ; CHECK-LABEL: @_Z2p2bb
55 ; CHECK-NOT: icmp
56 ; CHECK: ret void
57
58 ; <label>:8                                       ; preds = %0
59   call void @_ZdaPv(i8* %4) #5
60   call void @_ZdaPv(i8* %5) #5
61   br label %9
62
63 ; <label>:9                                       ; preds = %0, %8
64   ret void
65 }
66
67 ; Here's another case involving multiple underlying objects, but this time we
68 ; must keep the comparison (it might involve a regular pointer-typed function
69 ; argument).
70
71 define void @_Z4nopebbPi(i1 zeroext %b1, i1 zeroext %b2, i32* readnone %q) #0 {
72   %mStackData = alloca [10 x i32], align 16
73   %1 = bitcast [10 x i32]* %mStackData to i8*
74   %2 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0
75   %3 = select i1 %b1, i32* %2, i32* %q
76   %4 = tail call noalias i8* @_Znam(i64 48) #4
77   %5 = tail call noalias i8* @_Znam(i64 48) #4
78   %.v = select i1 %b2, i8* %4, i8* %5
79   %6 = bitcast i8* %.v to i32*
80   %7 = icmp eq i32* %6, %3
81   br i1 %7, label %9, label %8
82
83 ; CHECK-LABEL: @_Z4nopebbPi
84 ; CHECK: icmp
85 ; CHECK: ret void
86
87 ; <label>:8                                       ; preds = %0
88   call void @_ZdaPv(i8* %4) #5
89   call void @_ZdaPv(i8* %5) #5
90   br label %9
91
92 ; <label>:9                                       ; preds = %0, %8
93   ret void
94 }
95
96 ; Function Attrs: nobuiltin
97 declare noalias i8* @_Znam(i64) #2
98
99 ; Function Attrs: nobuiltin nounwind
100 declare void @_ZdaPv(i8*) #3
101
102 attributes #0 = { uwtable }
103 attributes #1 = { nounwind }
104 attributes #2 = { nobuiltin }
105 attributes #3 = { nobuiltin nounwind }
106 attributes #4 = { builtin }
107 attributes #5 = { builtin nounwind }
108