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
3 target datalayout = "E-m:e-i64:64-n32:64"
4 target triple = "powerpc64-unknown-linux-gnu"
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.
11 %struct = type { i32, i32, i32, i32, [20 x i32] }
13 ; Check that when two complex GEPs are used in two basic blocks, LLVM can
14 ; elimilate the common subexpression for the second use.
15 define 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
21 if.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
27 if.end: ; preds = %if.then, %entry
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
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 }
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
45 define 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
55 store i32 %6, ptr %3, align 4
59 %7 = add nsw i32 %4, 1
60 store i32 %7, ptr %1, align 4
63 ; CHECK-LABEL: test_GEP_across_BB:
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
74 ; CHECK-UseAA: {{%sunk[a-zA-Z0-9]+}} = getelementptr i8, ptr [[PTR0]], i64 528
76 %struct.S = type { float, double }
77 @struct_array = global [1024 x %struct.S] zeroinitializer, align 16
79 ; The following two test cases check we can extract constant from indices of
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.
84 define ptr @test-struct_1(i32 %i) {
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
91 ; CHECK-UseAA-LABEL: @test-struct_1(
92 ; CHECK-UseAA: getelementptr i8, ptr %{{[a-zA-Z0-9]+}}, i64 88
94 %struct3 = type { i64, i32 }
95 %struct2 = type { %struct3, i32 }
96 %struct1 = type { i64, %struct2 }
97 %struct0 = type { i32, i32, ptr, [100 x %struct1] }
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
103 define ptr @test-struct_2(ptr %ptr, i64 %idx) {
105 %arrayidx = add nsw i64 %idx, -2
106 %ptr2 = getelementptr %struct0, ptr %ptr, i64 0, i32 3, i64 %arrayidx, i32 1
109 ; CHECK-UseAA-LABEL: @test-struct_2(
110 ; CHECK-UseAA: getelementptr i8, ptr %{{[a-zA-Z0-9]+}}, i64 -40
112 ; Test that when a index is added from two constant, SeparateConstOffsetFromGEP
113 ; pass does not generate incorrect result.
114 define 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
121 ; CHECK-LABEL: test_const_add:
122 ; CHECK: li [[REG:[0-9]+]], 0
123 ; CHECK: stw [[REG]], 44(3)