xref: /llvm-project/llvm/test/CodeGen/PowerPC/ppc64-gep-opt.ll (revision f4fbcd62af31ac8921c2c2216bdbed1b49a0dff1)
1; RUN: llc -verify-machineinstrs -O3 -mcpu=pwr7 < %s | FileCheck %s
2; RUN: llc -verify-machineinstrs -O3 -print-after=codegenprepare -mcpu=pwr7  < %s >%t 2>&1 && FileCheck --check-prefix=CHECK-UseAA <%t %s
3target datalayout = "E-m:e-i64:64-n32:64"
4target triple = "powerpc64-unknown-linux-gnu"
5
6; Following test cases test enabling SeparateConstOffsetFromGEP pass in the PPC
7; backend. If useAA() returns true, it will lower a GEP with multiple indices
8; into GEPs with a single index, otherwise it will lower it into a
9; "ptrtoint+arithmetics+inttoptr" form.
10
11%struct = type { i32, i32, i32, i32, [20 x i32] }
12
13; Check that when two complex GEPs are used in two basic blocks, LLVM can
14; elimilate the common subexpression for the second use.
15define void @test_GEP_CSE(ptr %string, ptr %adj, i32 %lib, i64 %idxprom) {
16  %liberties = getelementptr [240 x %struct], ptr %string, i64 1, i64 %idxprom, i32 3
17  %1 = load i32, ptr %liberties, align 4
18  %cmp = icmp eq i32 %1, %lib
19  br i1 %cmp, label %if.then, label %if.end
20
21if.then:                                          ; preds = %entry
22  %origin = getelementptr [240 x %struct], ptr %string, i64 1, i64 %idxprom, i32 2
23  %2 = load i32, ptr %origin, align 4
24  store i32 %2, ptr %adj, align 4
25  br label %if.end
26
27if.end:                                           ; preds = %if.then, %entry
28  ret void
29}
30
31; CHECK-UseAA-LABEL: @test_GEP_CSE(
32; CHECK-UseAA: [[IDX:%[a-zA-Z0-9]+]] = mul i64 %idxprom, 96
33; CHECK-UseAA: [[PTR1:%[a-zA-Z0-9]+]] = getelementptr i8, ptr %string, i64 [[IDX]]
34; CHECK-UseAA: getelementptr i8, ptr [[PTR1]], i64 23052
35; CHECK-UseAA: if.then:
36; CHECK-UseAA: getelementptr i8, ptr [[PTR1]], i64 23048
37
38%class.my = type { i32, [128 x i32], i32, [256 x %struct.pt]}
39%struct.pt = type { ptr, i32, i32 }
40%struct.point = type { i32, i32 }
41
42; Check when a GEP is used across two basic block, LLVM can sink the address
43; calculation and code gen can generate a better addressing mode for the second
44; use.
45define void @test_GEP_across_BB(ptr %this, i64 %idx) {
46  %1 = getelementptr %class.my, ptr %this, i64 0, i32 3, i64 %idx, i32 1
47  %2 = load i32, ptr %1, align 4
48  %3 = getelementptr %class.my, ptr %this, i64 0, i32 3, i64 %idx, i32 2
49  %4 = load i32, ptr %3, align 4
50  %5 = icmp eq i32 %2, %4
51  br i1 %5, label %if.true, label %exit
52
53if.true:
54  %6 = shl i32 %4, 1
55  store i32 %6, ptr %3, align 4
56  br label %exit
57
58exit:
59  %7 = add nsw i32 %4, 1
60  store i32 %7, ptr %1, align 4
61  ret void
62}
63; CHECK-LABEL: test_GEP_across_BB:
64; CHECK-NOT: lwzu
65; CHECK: blr
66
67; CHECK-UseAA-LABEL: test_GEP_across_BB(
68; CHECK-UseAA: [[PTR0:%[a-zA-Z0-9]+]] = getelementptr
69; CHECK-UseAA: getelementptr i8, ptr [[PTR0]], i64 528
70; CHECK-UseAA: getelementptr i8, ptr [[PTR0]], i64 532
71; CHECK-UseAA: if.true:
72; CHECK-UseAA: {{%sunk[a-zA-Z0-9]+}} = getelementptr i8, ptr [[PTR0]], i64 532
73; CHECK-UseAA: exit:
74; CHECK-UseAA: {{%sunk[a-zA-Z0-9]+}} = getelementptr i8, ptr [[PTR0]], i64 528
75
76%struct.S = type { float, double }
77@struct_array = global [1024 x %struct.S] zeroinitializer, align 16
78
79; The following two test cases check we can extract constant from indices of
80; struct type.
81; The constant offsets are from indices "i64 %idxprom" and "i32 1". As the
82; alloca size of %struct.S is 16, and "i32 1" is the 2rd element whose field
83; offset is 8, the total constant offset is (5 * 16 + 8) = 88.
84define ptr @test-struct_1(i32 %i) {
85entry:
86  %add = add nsw i32 %i, 5
87  %idxprom = sext i32 %add to i64
88  %p = getelementptr [1024 x %struct.S], ptr @struct_array, i64 0, i64 %idxprom, i32 1
89  ret ptr %p
90}
91; CHECK-UseAA-LABEL: @test-struct_1(
92; CHECK-UseAA: getelementptr i8, ptr %{{[a-zA-Z0-9]+}}, i64 88
93
94%struct3 = type { i64, i32 }
95%struct2 = type { %struct3, i32 }
96%struct1 = type { i64, %struct2 }
97%struct0 = type { i32, i32, ptr, [100 x %struct1] }
98
99; The constant offsets are from indices "i32 3", "i64 %arrayidx" and "i32 1".
100; "i32 3" is the 4th element whose field offset is 16. The alloca size of
101; %struct1 is 32. "i32 1" is the 2rd element whose field offset is 8. So the
102; total constant offset is 16 + (-2 * 32) + 8 = -40
103define ptr @test-struct_2(ptr %ptr, i64 %idx) {
104entry:
105  %arrayidx = add nsw i64 %idx, -2
106  %ptr2 = getelementptr %struct0, ptr %ptr, i64 0, i32 3, i64 %arrayidx, i32 1
107  ret ptr %ptr2
108}
109; CHECK-UseAA-LABEL: @test-struct_2(
110; CHECK-UseAA: getelementptr i8, ptr %{{[a-zA-Z0-9]+}}, i64 -40
111
112; Test that when a index is added from two constant, SeparateConstOffsetFromGEP
113; pass does not generate incorrect result.
114define void @test_const_add(ptr %in) {
115  %inc = add nsw i32 2, 1
116  %idxprom = sext i32 %inc to i64
117  %arrayidx = getelementptr [3 x i32], ptr %in, i64 %idxprom, i64 2
118  store i32 0, ptr %arrayidx, align 4
119  ret void
120}
121; CHECK-LABEL: test_const_add:
122; CHECK: li [[REG:[0-9]+]], 0
123; CHECK: stw [[REG]], 44(3)
124; CHECK: blr
125
126