xref: /llvm-project/llvm/test/Transforms/GVN/PRE/load-pre-nonlocal.ll (revision 23abf931386002fb9d2c11d026846475c224c641)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S -o - -passes=gvn %s | FileCheck %s
3
4target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
5
6%struct.S1 = type { i32, i32 }
7
8@a2 = common global ptr null, align 8
9@a = common global ptr null, align 8
10@s1 = common global %struct.S1 zeroinitializer, align 8
11
12; Check that GVN doesn't determine %2 is partially redundant.
13
14define i32 @volatile_load(i32 %n) {
15; CHECK-LABEL: @volatile_load(
16; CHECK-NEXT:  entry:
17; CHECK-NEXT:    [[CMP6:%.*]] = icmp sgt i32 [[N:%.*]], 0
18; CHECK-NEXT:    br i1 [[CMP6]], label [[FOR_BODY_LR_PH:%.*]], label [[FOR_END:%.*]]
19; CHECK:       for.body.lr.ph:
20; CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr @a2, align 8, !tbaa [[TBAA5:![0-9]+]]
21; CHECK-NEXT:    [[TMP1:%.*]] = load ptr, ptr @a, align 8, !tbaa [[TBAA5]]
22; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
23; CHECK:       for.body:
24; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_BODY_LR_PH]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ]
25; CHECK-NEXT:    [[S_09:%.*]] = phi i32 [ 0, [[FOR_BODY_LR_PH]] ], [ [[ADD:%.*]], [[FOR_BODY]] ]
26; CHECK-NEXT:    [[P_08:%.*]] = phi ptr [ [[TMP0]], [[FOR_BODY_LR_PH]] ], [ [[INCDEC_PTR:%.*]], [[FOR_BODY]] ]
27; CHECK-NEXT:    [[TMP2:%.*]] = load i32, ptr [[P_08]], align 4, !tbaa [[TBAA9:![0-9]+]]
28; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[TMP1]], i64 [[INDVARS_IV]]
29; CHECK-NEXT:    store i32 [[TMP2]], ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA9]]
30; CHECK-NEXT:    [[TMP3:%.*]] = load volatile i32, ptr [[P_08]], align 4, !tbaa [[TBAA9]]
31; CHECK-NEXT:    [[ADD]] = add nsw i32 [[TMP3]], [[S_09]]
32; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
33; CHECK-NEXT:    [[INCDEC_PTR]] = getelementptr inbounds i32, ptr [[P_08]], i64 1
34; CHECK-NEXT:    [[LFTR_WIDEIV:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
35; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp ne i32 [[LFTR_WIDEIV]], [[N]]
36; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_BODY]], label [[FOR_COND_FOR_END_CRIT_EDGE:%.*]]
37; CHECK:       for.cond.for.end_crit_edge:
38; CHECK-NEXT:    br label [[FOR_END]]
39; CHECK:       for.end:
40; CHECK-NEXT:    [[S_0_LCSSA:%.*]] = phi i32 [ [[ADD]], [[FOR_COND_FOR_END_CRIT_EDGE]] ], [ 0, [[ENTRY:%.*]] ]
41; CHECK-NEXT:    ret i32 [[S_0_LCSSA]]
42;
43entry:
44  %cmp6 = icmp sgt i32 %n, 0
45  br i1 %cmp6, label %for.body.lr.ph, label %for.end
46
47for.body.lr.ph:
48  %0 = load ptr, ptr @a2, align 8, !tbaa !1
49  %1 = load ptr, ptr @a, align 8, !tbaa !1
50  br label %for.body
51
52for.body:
53  %indvars.iv = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next, %for.body ]
54  %s.09 = phi i32 [ 0, %for.body.lr.ph ], [ %add, %for.body ]
55  %p.08 = phi ptr [ %0, %for.body.lr.ph ], [ %incdec.ptr, %for.body ]
56  %2 = load i32, ptr %p.08, align 4, !tbaa !5
57  %arrayidx = getelementptr inbounds i32, ptr %1, i64 %indvars.iv
58  store i32 %2, ptr %arrayidx, align 4, !tbaa !5
59  %3 = load volatile i32, ptr %p.08, align 4, !tbaa !5
60  %add = add nsw i32 %3, %s.09
61  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
62  %incdec.ptr = getelementptr inbounds i32, ptr %p.08, i64 1
63  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
64  %exitcond = icmp ne i32 %lftr.wideiv, %n
65  br i1 %exitcond, label %for.body, label %for.cond.for.end_crit_edge
66
67for.cond.for.end_crit_edge:
68  %add.lcssa = phi i32 [ %add, %for.body ]
69  br label %for.end
70
71for.end:
72  %s.0.lcssa = phi i32 [ %add.lcssa, %for.cond.for.end_crit_edge ], [ 0, %entry ]
73  ret i32 %s.0.lcssa
74}
75
76; %1 is partially redundant if %0 can be widened to a 64-bit load.
77; But we should not widen %0 to 64-bit load.
78
79define i32 @overaligned_load(i32 %a, ptr nocapture %b) !dbg !13 {
80; CHECK-LABEL: @overaligned_load(
81; CHECK-NEXT:  entry:
82; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[A:%.*]], 0, !dbg [[DBG14:![0-9]+]]
83; CHECK-NEXT:    br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]], !dbg [[DBG14]]
84; CHECK:       if.then:
85; CHECK-NEXT:    [[TMP0:%.*]] = load i32, ptr @s1, align 8, !dbg [[DBG15:![0-9]+]], !tbaa [[TBAA9]]
86; CHECK-NEXT:    br label [[IF_END:%.*]], !dbg [[DBG15]]
87; CHECK:       if.else:
88; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[B:%.*]], i64 2, !dbg [[DBG16:![0-9]+]]
89; CHECK-NEXT:    store i32 10, ptr [[ARRAYIDX]], align 4, !dbg [[DBG16]], !tbaa [[TBAA9]]
90; CHECK-NEXT:    br label [[IF_END]], !dbg [[DBG16]]
91; CHECK:       if.end:
92; CHECK-NEXT:    [[I_0:%.*]] = phi i32 [ [[TMP0]], [[IF_THEN]] ], [ 0, [[IF_ELSE]] ]
93; CHECK-NEXT:    [[P_0:%.*]] = phi ptr [ @s1, [[IF_THEN]] ], [ [[B]], [[IF_ELSE]] ]
94; CHECK-NEXT:    [[ADD_PTR:%.*]] = getelementptr inbounds i32, ptr [[P_0]], i64 1, !dbg [[DBG17:![0-9]+]]
95; CHECK-NEXT:    [[TMP1:%.*]] = load i32, ptr [[ADD_PTR]], align 4, !dbg [[DBG17]], !tbaa [[TBAA9]]
96; CHECK-NEXT:    [[ADD1:%.*]] = add nsw i32 [[TMP1]], [[I_0]], !dbg [[DBG17]]
97; CHECK-NEXT:    ret i32 [[ADD1]], !dbg [[DBG17]]
98;
99entry:
100  %cmp = icmp sgt i32 %a, 0, !dbg !14
101  br i1 %cmp, label %if.then, label %if.else, !dbg !14
102
103if.then:
104  %0 = load i32, ptr @s1, align 8, !tbaa !5, !dbg !15
105  br label %if.end, !dbg !15
106
107if.else:
108  %arrayidx = getelementptr inbounds i32, ptr %b, i64 2, !dbg !16
109  store i32 10, ptr %arrayidx, align 4, !tbaa !5, !dbg !16
110  br label %if.end, !dbg !16
111
112if.end:
113  %i.0 = phi i32 [ %0, %if.then ], [ 0, %if.else ]
114  %p.0 = phi ptr [ @s1, %if.then ], [ %b, %if.else ]
115  %add.ptr = getelementptr inbounds i32, ptr %p.0, i64 1, !dbg !17
116  %1 = load i32, ptr %add.ptr, align 4, !tbaa !5, !dbg !17
117  %add1 = add nsw i32 %1, %i.0, !dbg !17
118  ret i32 %add1, !dbg !17
119}
120
121!1 = !{!2, !2, i64 0}
122!2 = !{!"any pointer", !3, i64 0}
123!3 = !{!"omnipotent char", !4, i64 0}
124!4 = !{!"Simple C/C++ TBAA"}
125!5 = !{!6, !6, i64 0}
126!6 = !{!"int", !3, i64 0}
127
128!llvm.module.flags = !{!7, !8, !9}
129!llvm.dbg.cu = !{!18}
130!7 = !{i32 2, !"Dwarf Version", i32 4}
131!8 = !{i32 2, !"Debug Info Version", i32 3}
132!9 = !{i32 1, !"PIC Level", i32 2}
133
134!10 = !{}
135!11 = !DISubroutineType(types: !10)
136!12 = !DIFile(filename: "test.cpp", directory: "/tmp")
137!13 = distinct !DISubprogram(name: "test", scope: !12, file: !12, line: 99, type: !11, isLocal: false, isDefinition: true, scopeLine: 100, flags: DIFlagPrototyped, isOptimized: false, unit: !18, retainedNodes: !10)
138!14 = !DILocation(line: 100, column: 1, scope: !13)
139!15 = !DILocation(line: 101, column: 1, scope: !13)
140!16 = !DILocation(line: 102, column: 1, scope: !13)
141!17 = !DILocation(line: 103, column: 1, scope: !13)
142!18 = distinct !DICompileUnit(language: DW_LANG_C99, producer: "clang",
143  file: !12,
144  isOptimized: true, flags: "-O2",
145  splitDebugFilename: "abc.debug", emissionKind: 2)
146