146 lines · plain
1; RUN: opt < %s -passes=nary-reassociate,early-cse -earlycse-debug-hash -S | FileCheck %s2; RUN: opt < %s -passes='nary-reassociate' -S | opt -passes=early-cse -S | FileCheck %s3 4target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"5target triple = "nvptx64-unknown-unknown"6 7declare void @foo(ptr)8 9; foo(&a[i]);10; foo(&a[i + j]);11; =>12; t = &a[i];13; foo(t);14; foo(t + j);15define void @reassociate_gep(ptr %a, i64 %i, i64 %j) {16; CHECK-LABEL: @reassociate_gep(17 %1 = add i64 %i, %j18 %2 = getelementptr float, ptr %a, i64 %i19; CHECK: [[t1:[^ ]+]] = getelementptr float, ptr %a, i64 %i20 call void @foo(ptr %2)21; CHECK: call void @foo(ptr [[t1]])22 %3 = getelementptr float, ptr %a, i64 %123; CHECK: [[t2:[^ ]+]] = getelementptr float, ptr [[t1]], i64 %j24 call void @foo(ptr %3)25; CHECK: call void @foo(ptr [[t2]])26 ret void27}28 29; foo(&a[sext(j)]);30; foo(&a[sext(i +nsw j)]);31; foo(&a[sext((i +nsw j) +nsw i)]);32; =>33; t1 = &a[sext(j)];34; foo(t1);35; t2 = t1 + sext(i);36; foo(t2);37; t3 = t2 + sext(i); // sext(i) should be GVN'ed.38; foo(t3);39define void @reassociate_gep_nsw(ptr %a, i32 %i, i32 %j) {40; CHECK-LABEL: @reassociate_gep_nsw(41 %idxprom.j = sext i32 %j to i6442 %1 = getelementptr float, ptr %a, i64 %idxprom.j43; CHECK: [[t1:[^ ]+]] = getelementptr float, ptr %a, i64 %idxprom.j44 call void @foo(ptr %1)45; CHECK: call void @foo(ptr [[t1]])46 47 %2 = add nsw i32 %i, %j48 %idxprom.2 = sext i32 %2 to i6449 %3 = getelementptr float, ptr %a, i64 %idxprom.250; CHECK: [[sexti:[^ ]+]] = sext i32 %i to i6451; CHECK: [[t2:[^ ]+]] = getelementptr float, ptr [[t1]], i64 [[sexti]]52 call void @foo(ptr %3)53; CHECK: call void @foo(ptr [[t2]])54 55 %4 = add nsw i32 %2, %i56 %idxprom.4 = sext i32 %4 to i6457 %5 = getelementptr float, ptr %a, i64 %idxprom.458; CHECK: [[t3:[^ ]+]] = getelementptr float, ptr [[t2]], i64 [[sexti]]59 call void @foo(ptr %5)60; CHECK: call void @foo(ptr [[t3]])61 62 ret void63}64 65; assume(j >= 0);66; foo(&a[zext(j)]);67; assume(i + j >= 0);68; foo(&a[zext(i + j)]);69; =>70; t1 = &a[zext(j)];71; foo(t1);72; t2 = t1 + sext(i);73; foo(t2);74define void @reassociate_gep_assume(ptr %a, i32 %i, i32 %j) {75; CHECK-LABEL: @reassociate_gep_assume(76 ; assume(j >= 0)77 %cmp = icmp sgt i32 %j, -178 call void @llvm.assume(i1 %cmp)79 %1 = add i32 %i, %j80 %cmp2 = icmp sgt i32 %1, -181 call void @llvm.assume(i1 %cmp2)82 83 %idxprom.j = zext i32 %j to i6484 %2 = getelementptr float, ptr %a, i64 %idxprom.j85; CHECK: [[t1:[^ ]+]] = getelementptr float, ptr %a, i64 %idxprom.j86 call void @foo(ptr %2)87; CHECK: call void @foo(ptr [[t1]])88 89 %idxprom.1 = zext i32 %1 to i6490 %3 = getelementptr float, ptr %a, i64 %idxprom.191; CHECK: [[sexti:[^ ]+]] = sext i32 %i to i6492; CHECK: [[t2:[^ ]+]] = getelementptr float, ptr [[t1]], i64 [[sexti]]93 call void @foo(ptr %3)94; CHECK: call void @foo(ptr [[t2]])95 96 ret void97}98 99; Do not split the second GEP because sext(i + j) != sext(i) + sext(j).100define void @reassociate_gep_no_nsw(ptr %a, i32 %i, i32 %j) {101; CHECK-LABEL: @reassociate_gep_no_nsw(102 %1 = add i32 %i, %j103 %2 = getelementptr float, ptr %a, i32 %j104; CHECK: getelementptr float, ptr %a, i32 %j105 call void @foo(ptr %2)106 %3 = getelementptr float, ptr %a, i32 %1107; CHECK: getelementptr float, ptr %a, i32 %1108 call void @foo(ptr %3)109 ret void110}111 112define void @reassociate_gep_128(ptr %a, i128 %i, i128 %j) {113; CHECK-LABEL: @reassociate_gep_128(114 %1 = add i128 %i, %j115 %2 = getelementptr float, ptr %a, i128 %i116; CHECK: [[t1:[^ ]+]] = getelementptr float, ptr %a, i128 %i117 call void @foo(ptr %2)118; CHECK: call void @foo(ptr [[t1]])119 %3 = getelementptr float, ptr %a, i128 %1120; CHECK: [[truncj:[^ ]+]] = trunc i128 %j to i64121; CHECK: [[t2:[^ ]+]] = getelementptr float, ptr [[t1]], i64 [[truncj]]122 call void @foo(ptr %3)123; CHECK: call void @foo(ptr [[t2]])124 ret void125}126 127%struct.complex = type { float, float }128 129declare void @bar(ptr)130 131define void @different_types(ptr %input, i64 %i) {132; CHECK-LABEL: @different_types(133; CHECK-NEXT: %t1 = getelementptr %struct.complex, ptr %input, i64 %i134; CHECK-NEXT: call void @bar(ptr %t1)135; CHECK-NEXT: %t2 = getelementptr float, ptr %t1, i64 10136; CHECK-NEXT: call void @foo(ptr %t2)137 %t1 = getelementptr %struct.complex, ptr %input, i64 %i138 call void @bar(ptr %t1)139 %j = add i64 %i, 5140 %t2 = getelementptr %struct.complex, ptr %input, i64 %j, i32 0141 call void @foo(ptr %t2)142 ret void143}144 145declare void @llvm.assume(i1)146