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" 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. 15 define void @test_GEP_CSE([240 x %struct]* %string, i32* %adj, i32 %lib, i64 %idxprom) { 16 %liberties = getelementptr [240 x %struct], [240 x %struct]* %string, i64 1, i64 %idxprom, i32 3 17 %1 = load i32, i32* %liberties, align 4 18 %cmp = icmp eq i32 %1, %lib 19 br i1 %cmp, label %if.then, label %if.end 20 21 if.then: ; preds = %entry 22 %origin = getelementptr [240 x %struct], [240 x %struct]* %string, i64 1, i64 %idxprom, i32 2 23 %2 = load i32, i32* %origin, align 4 24 store i32 %2, i32* %adj, align 4 25 br label %if.end 26 27 if.end: ; preds = %if.then, %entry 28 ret void 29 } 30 31 ; CHECK-UseAA-LABEL: @test_GEP_CSE( 32 ; CHECK-UseAA: [[PTR0:%[a-zA-Z0-9]+]] = bitcast [240 x %struct]* %string to i8* 33 ; CHECK-UseAA: [[IDX:%[a-zA-Z0-9]+]] = mul i64 %idxprom, 96 34 ; CHECK-UseAA: [[PTR1:%[a-zA-Z0-9]+]] = getelementptr i8, i8* [[PTR0]], i64 [[IDX]] 35 ; CHECK-UseAA: getelementptr i8, i8* [[PTR1]], i64 23052 36 ; CHECK-UseAA: bitcast 37 ; CHECK-UseAA: if.then: 38 ; CHECK-UseAA: getelementptr i8, i8* [[PTR1]], i64 23048 39 ; CHECK-UseAA: bitcast 40 41 %class.my = type { i32, [128 x i32], i32, [256 x %struct.pt]} 42 %struct.pt = type { %struct.point*, i32, i32 } 43 %struct.point = type { i32, i32 } 44 45 ; Check when a GEP is used across two basic block, LLVM can sink the address 46 ; calculation and code gen can generate a better addressing mode for the second 47 ; use. 48 define void @test_GEP_across_BB(%class.my* %this, i64 %idx) { 49 %1 = getelementptr %class.my, %class.my* %this, i64 0, i32 3, i64 %idx, i32 1 50 %2 = load i32, i32* %1, align 4 51 %3 = getelementptr %class.my, %class.my* %this, i64 0, i32 3, i64 %idx, i32 2 52 %4 = load i32, i32* %3, align 4 53 %5 = icmp eq i32 %2, %4 54 br i1 %5, label %if.true, label %exit 55 56 if.true: 57 %6 = shl i32 %4, 1 58 store i32 %6, i32* %3, align 4 59 br label %exit 60 61 exit: 62 %7 = add nsw i32 %4, 1 63 store i32 %7, i32* %1, align 4 64 ret void 65 } 66 ; CHECK-LABEL: test_GEP_across_BB: 67 ; CHECK-NOT: lwzu 68 ; CHECK: blr 69 70 ; CHECK-UseAA-LABEL: test_GEP_across_BB( 71 ; CHECK-UseAA: [[PTR0:%[a-zA-Z0-9]+]] = getelementptr 72 ; CHECK-UseAA: getelementptr i8, i8* [[PTR0]], i64 528 73 ; CHECK-UseAA: getelementptr i8, i8* [[PTR0]], i64 532 74 ; CHECK-UseAA: if.true: 75 ; CHECK-UseAA: {{%sunk[a-zA-Z0-9]+}} = getelementptr i8, i8* [[PTR0]], i64 532 76 ; CHECK-UseAA: exit: 77 ; CHECK-UseAA: {{%sunk[a-zA-Z0-9]+}} = getelementptr i8, i8* [[PTR0]], i64 528 78 79 %struct.S = type { float, double } 80 @struct_array = global [1024 x %struct.S] zeroinitializer, align 16 81 82 ; The following two test cases check we can extract constant from indices of 83 ; struct type. 84 ; The constant offsets are from indices "i64 %idxprom" and "i32 1". As the 85 ; alloca size of %struct.S is 16, and "i32 1" is the 2rd element whose field 86 ; offset is 8, the total constant offset is (5 * 16 + 8) = 88. 87 define double* @test-struct_1(i32 %i) { 88 entry: 89 %add = add nsw i32 %i, 5 90 %idxprom = sext i32 %add to i64 91 %p = getelementptr [1024 x %struct.S], [1024 x %struct.S]* @struct_array, i64 0, i64 %idxprom, i32 1 92 ret double* %p 93 } 94 ; CHECK-UseAA-LABEL: @test-struct_1( 95 ; CHECK-UseAA: getelementptr i8, i8* %{{[a-zA-Z0-9]+}}, i64 88 96 97 %struct3 = type { i64, i32 } 98 %struct2 = type { %struct3, i32 } 99 %struct1 = type { i64, %struct2 } 100 %struct0 = type { i32, i32, i64*, [100 x %struct1] } 101 102 ; The constant offsets are from indices "i32 3", "i64 %arrayidx" and "i32 1". 103 ; "i32 3" is the 4th element whose field offset is 16. The alloca size of 104 ; %struct1 is 32. "i32 1" is the 2rd element whose field offset is 8. So the 105 ; total constant offset is 16 + (-2 * 32) + 8 = -40 106 define %struct2* @test-struct_2(%struct0* %ptr, i64 %idx) { 107 entry: 108 %arrayidx = add nsw i64 %idx, -2 109 %ptr2 = getelementptr %struct0, %struct0* %ptr, i64 0, i32 3, i64 %arrayidx, i32 1 110 ret %struct2* %ptr2 111 } 112 ; CHECK-UseAA-LABEL: @test-struct_2( 113 ; CHECK-UseAA: getelementptr i8, i8* %{{[a-zA-Z0-9]+}}, i64 -40 114 115 ; Test that when a index is added from two constant, SeparateConstOffsetFromGEP 116 ; pass does not generate incorrect result. 117 define void @test_const_add([3 x i32]* %in) { 118 %inc = add nsw i32 2, 1 119 %idxprom = sext i32 %inc to i64 120 %arrayidx = getelementptr [3 x i32], [3 x i32]* %in, i64 %idxprom, i64 2 121 store i32 0, i32* %arrayidx, align 4 122 ret void 123 } 124 ; CHECK-LABEL: test_const_add: 125 ; CHECK: li [[REG:[0-9]+]], 0 126 ; CHECK: stw [[REG]], 44(3) 127 ; CHECK: blr 128 129