1 ; RUN: opt -tbaa -basicaa -licm -S < %s | FileCheck %s
2 ; RUN: opt -aa-pipeline=type-based-aa,basic-aa -passes='require<aa>,require<targetir>,require<scalar-evolution>,require<opt-remark-emit>,loop(licm)' -S %s | FileCheck %s
4 ; If we can prove a local is thread local, we can insert stores during
5 ; promotion which wouldn't be legal otherwise.
7 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
8 target triple = "x86_64-linux-generic"
10 @p = external global i8*
12 declare i8* @malloc(i64)
14 ; Exercise the TLS case
16 define i32* @test(i32 %n) {
18 ;; ignore the required null check for simplicity
19 %mem = call dereferenceable(16) noalias i8* @malloc(i64 16)
20 %addr = bitcast i8* %mem to i32*
21 br label %for.body.lr.ph
23 for.body.lr.ph: ; preds = %entry
27 %i.02 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
28 %old = load i32, i32* %addr, align 4
29 ; deliberate impossible to analyze branch
30 %guard = load atomic i8*, i8** @p monotonic, align 8
31 %exitcmp = icmp eq i8* %guard, null
32 br i1 %exitcmp, label %for.body, label %early-exit
35 ; CHECK-LABEL: early-exit:
36 ; CHECK: store i32 %new1.lcssa, i32* %addr, align 1
40 %new = add i32 %old, 1
41 store i32 %new, i32* %addr, align 4
42 %inc = add nsw i32 %i.02, 1
43 %cmp = icmp slt i32 %inc, %n
44 br i1 %cmp, label %for.header, label %for.cond.for.end_crit_edge
46 for.cond.for.end_crit_edge: ; preds = %for.body
47 ; CHECK-LABEL: for.cond.for.end_crit_edge:
48 ; CHECK: store i32 %new.lcssa, i32* %addr, align 1
49 %split = phi i32* [ %addr, %for.body ]
53 ; Stack allocations can also be thread-local
55 define i32* @test2(i32 %n) {
57 %mem = alloca i8, i32 16
58 %addr = bitcast i8* %mem to i32*
59 br label %for.body.lr.ph
61 for.body.lr.ph: ; preds = %entry
65 %i.02 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
66 %old = load i32, i32* %addr, align 4
67 ; deliberate impossible to analyze branch
68 %guard = load atomic i8*, i8** @p monotonic, align 8
69 %exitcmp = icmp eq i8* %guard, null
70 br i1 %exitcmp, label %for.body, label %early-exit
73 ; CHECK-LABEL: early-exit:
74 ; CHECK: store i32 %new1.lcssa, i32* %addr, align 1
78 %new = add i32 %old, 1
79 store i32 %new, i32* %addr, align 4
80 %inc = add nsw i32 %i.02, 1
81 %cmp = icmp slt i32 %inc, %n
82 br i1 %cmp, label %for.header, label %for.cond.for.end_crit_edge
84 for.cond.for.end_crit_edge: ; preds = %for.body
85 ; CHECK-LABEL: for.cond.for.end_crit_edge:
86 ; CHECK: store i32 %new.lcssa, i32* %addr, align 1
87 %split = phi i32* [ %addr, %for.body ]
91 declare i8* @not_malloc(i64)
93 ; Negative test - not TLS
94 ; CHECK-LABEL: @test_neg
95 define i32* @test_neg(i32 %n) {
97 ;; ignore the required null check for simplicity
98 %mem = call dereferenceable(16) noalias i8* @not_malloc(i64 16)
99 %addr = bitcast i8* %mem to i32*
100 br label %for.body.lr.ph
102 for.body.lr.ph: ; preds = %entry
106 %i.02 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
107 %old = load i32, i32* %addr, align 4
108 ; deliberate impossible to analyze branch
109 %guard = load volatile i8*, i8** @p
110 %exitcmp = icmp eq i8* %guard, null
111 br i1 %exitcmp, label %for.body, label %early-exit
114 ; CHECK-LABEL: early-exit:
119 ; CHECK-LABEL: for.body:
120 ; CHECK: store i32 %new, i32* %addr, align 4
121 %new = add i32 %old, 1
122 store i32 %new, i32* %addr, align 4
123 %inc = add nsw i32 %i.02, 1
124 %cmp = icmp slt i32 %inc, %n
125 br i1 %cmp, label %for.header, label %for.cond.for.end_crit_edge
127 for.cond.for.end_crit_edge: ; preds = %for.body
128 ; CHECK-LABEL: for.cond.for.end_crit_edge:
130 %split = phi i32* [ %addr, %for.body ]
134 ; Negative test - can't speculate load since branch
135 ; may control alignment
136 ; CHECK-LABEL: @test_neg2
137 define i32* @test_neg2(i32 %n) {
139 ;; ignore the required null check for simplicity
140 %mem = call dereferenceable(16) noalias i8* @malloc(i64 16)
141 %addr = bitcast i8* %mem to i32*
142 br label %for.body.lr.ph
144 for.body.lr.ph: ; preds = %entry
148 %i.02 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
149 ; deliberate impossible to analyze branch
150 %guard = load volatile i8*, i8** @p
151 %exitcmp = icmp eq i8* %guard, null
152 br i1 %exitcmp, label %for.body, label %early-exit
155 ; CHECK-LABEL: early-exit:
160 ; CHECK-LABEL: for.body:
161 ; CHECK: store i32 %new, i32* %addr, align 4
162 %old = load i32, i32* %addr, align 4
163 %new = add i32 %old, 1
164 store i32 %new, i32* %addr, align 4
165 %inc = add nsw i32 %i.02, 1
166 %cmp = icmp slt i32 %inc, %n
167 br i1 %cmp, label %for.header, label %for.cond.for.end_crit_edge
169 for.cond.for.end_crit_edge: ; preds = %for.body
170 ; CHECK-LABEL: for.cond.for.end_crit_edge:
172 %split = phi i32* [ %addr, %for.body ]