xref: /llvm-project/llvm/test/ThinLTO/X86/index-const-prop2.ll (revision d5d3eb16b7ab72529c83dacb2889811491e48909)
1bf46e741SEugene Leviant; Check constant propagation in thinlto combined summary. This allows us to do 2 things:
2bf46e741SEugene Leviant;  1. Internalize global definition which is not used externally if all accesses to it are read-only
3bf46e741SEugene Leviant;  2. Make a local copy of internal definition if all accesses to it are readonly. This allows constant
4bf46e741SEugene Leviant;     folding it during optimziation phase.
5bf46e741SEugene Leviant; RUN: opt -module-summary %s -o %t1.bc
6bf46e741SEugene Leviant; RUN: opt -module-summary %p/Inputs/index-const-prop.ll -o %t2.bc
7bf46e741SEugene Leviant; RUN: llvm-lto2 run %t1.bc %t2.bc -save-temps \
8bf46e741SEugene Leviant; RUN:  -r=%t2.bc,foo,pl \
9bf46e741SEugene Leviant; RUN:  -r=%t2.bc,bar,pl \
10bf46e741SEugene Leviant; RUN:  -r=%t2.bc,baz,pl \
11bf46e741SEugene Leviant; RUN:  -r=%t2.bc,rand, \
12bf46e741SEugene Leviant; RUN:  -r=%t2.bc,gBar,pl \
13bf46e741SEugene Leviant; RUN:  -r=%t1.bc,main,plx \
143aef3528SEugene Leviant; RUN:  -r=%t1.bc,main2,pl \
15bf46e741SEugene Leviant; RUN:  -r=%t1.bc,foo, \
16bf46e741SEugene Leviant; RUN:  -r=%t1.bc,bar, \
173aef3528SEugene Leviant; RUN:  -r=%t1.bc,baz, \
18bf46e741SEugene Leviant; RUN:  -r=%t1.bc,gBar, \
19bf46e741SEugene Leviant; RUN:  -o %t3
20bf46e741SEugene Leviant; RUN: llvm-dis %t3.1.3.import.bc -o - | FileCheck %s --check-prefix=IMPORT
21bf46e741SEugene Leviant; RUN: llvm-dis %t3.1.5.precodegen.bc -o - | FileCheck %s --check-prefix=CODEGEN
22bf46e741SEugene Leviant
23bf46e741SEugene Leviant; Now check that we won't internalize global (gBar) if it's externally referenced
24bf46e741SEugene Leviant; RUN: llvm-lto2 run %t1.bc %t2.bc -save-temps \
25bf46e741SEugene Leviant; RUN:  -r=%t2.bc,foo,pl \
26bf46e741SEugene Leviant; RUN:  -r=%t2.bc,bar,pl \
27bf46e741SEugene Leviant; RUN:  -r=%t2.bc,baz,pl \
28bf46e741SEugene Leviant; RUN:  -r=%t2.bc,rand, \
29bf46e741SEugene Leviant; RUN:  -r=%t2.bc,gBar,plx \
30bf46e741SEugene Leviant; RUN:  -r=%t1.bc,main,plx \
313aef3528SEugene Leviant; RUN:  -r=%t1.bc,main2,pl \
32bf46e741SEugene Leviant; RUN:  -r=%t1.bc,foo, \
33bf46e741SEugene Leviant; RUN:  -r=%t1.bc,bar, \
343aef3528SEugene Leviant; RUN:  -r=%t1.bc,baz, \
35bf46e741SEugene Leviant; RUN:  -r=%t1.bc,gBar, \
363aef3528SEugene Leviant; RUN:  -o %t4
373aef3528SEugene Leviant; RUN: llvm-dis %t4.1.3.import.bc -o - | FileCheck %s --check-prefix=IMPORT2
383aef3528SEugene Leviant
39e420c0c7STeresa Johnson; Run again but with main2 exported instead of main to check that write only
40e420c0c7STeresa Johnson; variables are optimized out.
413aef3528SEugene Leviant; RUN: llvm-lto2 run %t1.bc %t2.bc -save-temps \
423aef3528SEugene Leviant; RUN:  -r=%t2.bc,foo,pl \
433aef3528SEugene Leviant; RUN:  -r=%t2.bc,bar,pl \
443aef3528SEugene Leviant; RUN:  -r=%t2.bc,baz,pl \
453aef3528SEugene Leviant; RUN:  -r=%t2.bc,rand, \
463aef3528SEugene Leviant; RUN:  -r=%t2.bc,gBar,pl \
473aef3528SEugene Leviant; RUN:  -r=%t1.bc,main,pl \
483aef3528SEugene Leviant; RUN:  -r=%t1.bc,main2,plx \
493aef3528SEugene Leviant; RUN:  -r=%t1.bc,foo, \
503aef3528SEugene Leviant; RUN:  -r=%t1.bc,bar, \
513aef3528SEugene Leviant; RUN:  -r=%t1.bc,baz, \
523aef3528SEugene Leviant; RUN:  -r=%t1.bc,gBar, \
533aef3528SEugene Leviant; RUN:  -o %t5
54e420c0c7STeresa Johnson; RUN: llvm-dis %t5.1.3.import.bc -o - | FileCheck %s --check-prefix=IMPORT-WRITEONLY
553aef3528SEugene Leviant; RUN: llvm-dis %t5.1.5.precodegen.bc -o - | FileCheck %s --check-prefix=CODEGEN2
563aef3528SEugene Leviant; Check that gFoo and gBar were eliminated from source module together
573aef3528SEugene Leviant; with corresponsing stores
583aef3528SEugene Leviant; RUN: llvm-dis %t5.2.5.precodegen.bc -o - | FileCheck %s --check-prefix=CODEGEN2-SRC
59bf46e741SEugene Leviant
60cedfa38fSJin Lin; IMPORT:       @gBar = internal local_unnamed_addr global i32 2, align 4
61cedfa38fSJin Lin; IMPORT-NEXT:  @gFoo.llvm.0 = internal unnamed_addr global i32 1, align 4
6201f1d9b5SDavid Blaikie; IMPORT:       !DICompileUnit({{.*}})
63bf46e741SEugene Leviant
64e420c0c7STeresa Johnson; Write only variables are imported with a zero initializer.
65e420c0c7STeresa Johnson; IMPORT-WRITEONLY:  @gBar = internal local_unnamed_addr global i32 0
66cedfa38fSJin Lin; IMPORT-WRITEONLY:  @gFoo.llvm.0 = internal unnamed_addr global i32 0
67e420c0c7STeresa Johnson
68bf46e741SEugene Leviant; CODEGEN:        i32 @main()
69bf46e741SEugene Leviant; CODEGEN-NEXT:     ret i32 3
70bf46e741SEugene Leviant
71d2ef8c1fSFangrui Song; IMPORT2: @gBar = available_externally local_unnamed_addr global i32 2, align 4
72bf46e741SEugene Leviant
733aef3528SEugene Leviant; CODEGEN2:      i32 @main2
743aef3528SEugene Leviant; CODEGEN2-NEXT:   %1 = tail call i32 @rand()
753aef3528SEugene Leviant; CODEGEN2-NEXT:   %2 = tail call i32 @rand()
763aef3528SEugene Leviant; CODEGEN2-NEXT:   ret i32 0
773aef3528SEugene Leviant
783aef3528SEugene Leviant; CODEGEN2-SRC:       void @baz()
793aef3528SEugene Leviant; CODEGEN2-SRC-NEXT:    %1 = tail call i32 @rand()
803aef3528SEugene Leviant; CODEGEN2-SRC-NEXT:    %2 = tail call i32 @rand()
813aef3528SEugene Leviant; CODEGEN2-SRC-NEXT:    ret void
823aef3528SEugene Leviant
837b1d7937SAmy Huangtarget datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
84bf46e741SEugene Levianttarget triple = "x86_64-pc-linux-gnu"
85bf46e741SEugene Leviant
86bf46e741SEugene Leviant; We should be able to link external definition of gBar to its declaration
87bf46e741SEugene Leviant@gBar = external global i32
88bf46e741SEugene Leviant
89bf46e741SEugene Leviantdefine i32 @main() local_unnamed_addr {
90*d5d3eb16SFangrui Song  %call = tail call i32 @foo()
91*d5d3eb16SFangrui Song  %call1 = tail call i32 @bar()
92bf46e741SEugene Leviant  %add = add nsw i32 %call1, %call
93bf46e741SEugene Leviant  ret i32 %add
94bf46e741SEugene Leviant}
95bf46e741SEugene Leviant
963aef3528SEugene Leviantdefine i32 @main2() local_unnamed_addr {
973aef3528SEugene Leviant  tail call void @baz()
983aef3528SEugene Leviant  ret i32 0
993aef3528SEugene Leviant}
1003aef3528SEugene Leviant
101bf46e741SEugene Leviantdeclare i32 @foo(...) local_unnamed_addr
102bf46e741SEugene Leviant
103bf46e741SEugene Leviantdeclare i32 @bar(...) local_unnamed_addr
1043aef3528SEugene Leviant
1053aef3528SEugene Leviantdeclare void @baz() local_unnamed_addr
106