xref: /llvm-project/llvm/test/Transforms/GlobalOpt/tls.ll (revision dede19caf0da31d9b8b69d014602cde41ab9a96e)
1; RUN: opt < %s -passes=globalopt -S | FileCheck %s
2; RUN: opt -emulated-tls < %s -passes=globalopt -S | FileCheck %s
3
4declare void @wait()
5declare void @signal()
6declare void @start_thread(ptr)
7
8@x = internal thread_local global [100 x i32] zeroinitializer, align 16
9@ip = internal global ptr null, align 8
10
11; PR14309: GlobalOpt would think that the value of @ip is always the address of
12; x[1]. However, that address is different for different threads so @ip cannot
13; be replaced with a constant.
14
15define i32 @f() {
16entry:
17  ; Set @ip to point to x[1] for thread 1.
18  %p = call ptr @llvm.threadlocal.address(ptr @x)
19  %addr = getelementptr inbounds [100 x i32], ptr %p, i64 0, i64 1
20  store ptr %addr, ptr @ip, align 8
21
22  ; Run g on a new thread.
23  tail call void @start_thread(ptr @g) nounwind
24  tail call void @wait() nounwind
25
26  ; Reset x[1] for thread 1.
27  store i32 0, ptr %addr, align 4
28
29  ; Read the value of @ip, which now points at x[1] for thread 2.
30  %0 = load ptr, ptr @ip, align 8
31
32  %1 = load i32, ptr %0, align 4
33  ret i32 %1
34
35; CHECK-LABEL: @f(
36; Make sure that the load from @ip hasn't been removed.
37; CHECK: load ptr, ptr @ip
38; CHECK: ret
39}
40
41define internal void @g() nounwind uwtable {
42entry:
43  ; Set @ip to point to x[1] for thread 2.
44  %p = call ptr @llvm.threadlocal.address(ptr @x)
45  %addr = getelementptr inbounds [100 x i32], ptr %p, i64 0, i64 1
46  store ptr %addr, ptr @ip, align 8
47
48  ; Store 50 in x[1] for thread 2.
49  store i32 50, ptr %addr, align 4
50
51  tail call void @signal() nounwind
52  ret void
53
54; CHECK-LABEL: @g(
55; Make sure that the store to @ip hasn't been removed.
56; CHECK: store {{.*}} @ip
57; CHECK: ret
58}
59