1 ; RUN: opt -S -globalopt < %s | FileCheck %s
3 ; CHECK: @llvm.global_ctors = appending global [1 x {{.*}}@_GLOBAL__I_c
4 @llvm.global_ctors = appending global [3 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__I_a, i8* null }, { i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__I_b, i8* null }, { i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__I_c, i8* null }]
6 ; CHECK: @tmp = local_unnamed_addr global i32 42
7 ; CHECK: @tmp2 = local_unnamed_addr global i32 42
8 ; CHECK: @tmp3 = global i32 42
12 @ptrToTmp3 = global i32* null
14 define i32 @TheAnswerToLifeTheUniverseAndEverything() {
18 define void @_GLOBAL__I_a() {
20 call void @_optimizable()
21 call void @_not_optimizable()
25 define void @_optimizable() {
29 %val = call i32 @TheAnswerToLifeTheUniverseAndEverything()
30 store i32 %val, i32* @tmp
31 store i32 %val, i32* %valptr
33 %0 = bitcast i32* %valptr to i8*
34 %barr = call i8* @llvm.launder.invariant.group(i8* %0)
35 %1 = getelementptr i8, i8* %barr, i32 0
36 %2 = bitcast i8* %1 to i32*
38 %val2 = load i32, i32* %2
39 store i32 %val2, i32* @tmp2
43 ; We can't step through launder.invariant.group here, because that would change
44 ; this load in @usage_of_globals()
45 ; %val = load i32, i32* %ptrVal, !invariant.group !0
47 ; %val = load i32, i32* @tmp3, !invariant.group !0
48 ; and then we could assume that %val and %val2 to be the same, which coud be
49 ; false, because @changeTmp3ValAndCallBarrierInside() may change the value
51 define void @_not_optimizable() {
53 store i32 13, i32* @tmp3, !invariant.group !0
55 %0 = bitcast i32* @tmp3 to i8*
56 %barr = call i8* @llvm.launder.invariant.group(i8* %0)
57 %1 = bitcast i8* %barr to i32*
59 store i32* %1, i32** @ptrToTmp3
60 store i32 42, i32* %1, !invariant.group !0
65 define void @usage_of_globals() {
67 %ptrVal = load i32*, i32** @ptrToTmp3
68 %val = load i32, i32* %ptrVal, !invariant.group !0
70 call void @changeTmp3ValAndCallBarrierInside()
71 %val2 = load i32, i32* @tmp3, !invariant.group !0
77 define void @_GLOBAL__I_b() {
79 %val = call i32 @TheAnswerToLifeTheUniverseAndEverything()
80 %p1 = bitcast i32* @tmp4 to i8*
81 %p2 = call i8* @llvm.strip.invariant.group.p0i8(i8* %p1)
82 %p3 = bitcast i8* %p2 to i32*
83 store i32 %val, i32* %p3
88 @tmp6 = global i32* null
89 ; CHECK: @tmp6 = local_unnamed_addr global i32* null
91 define i32* @_dont_return_param(i32* %p) {
92 %p1 = bitcast i32* %p to i8*
93 %p2 = call i8* @llvm.launder.invariant.group(i8* %p1)
94 %p3 = bitcast i8* %p2 to i32*
98 ; We should bail out if we return any pointers derived via invariant.group intrinsics at any point.
99 define void @_GLOBAL__I_c() {
101 %tmp5 = call i32* @_dont_return_param(i32* @tmp5)
102 store i32* %tmp5, i32** @tmp6
107 declare void @changeTmp3ValAndCallBarrierInside()
109 declare i8* @llvm.launder.invariant.group(i8*)
110 declare i8* @llvm.strip.invariant.group.p0i8(i8*)