[PATCH] llvm.noalias - InstCombiner::OptimizePointerDifference should look through them

hfinkel at anl.gov hfinkel at anl.gov
Thu Apr 30 09:07:05 PDT 2015


Hi chandlerc, reames,

This is part of the series started by D9375, and teaches InstCombiner::OptimizePointerDifference to look through calls to llvm.noalias.

http://reviews.llvm.org/D9393

Files:
  lib/Transforms/InstCombine/InstCombineAddSub.cpp
  test/Transforms/InstCombine/sub.ll

Index: lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1445,6 +1445,10 @@
   bool Swapped = false;
   GEPOperator *GEP1 = nullptr, *GEP2 = nullptr;
 
+  // This is primarially to deal with leading noalias intrinsics.
+  LHS = LHS->stripPointerCasts(/*LookThroughNoAlias*/true);
+  RHS = RHS->stripPointerCasts(/*LookThroughNoAlias*/true);
+
   // For now we require one side to be the base pointer "A" or a constant
   // GEP derived from it.
   if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
@@ -1454,8 +1458,8 @@
       Swapped = false;
     } else if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
       // (gep X, ...) - (gep X, ...)
-      if (LHSGEP->getOperand(0)->stripPointerCasts() ==
-            RHSGEP->getOperand(0)->stripPointerCasts()) {
+      if (LHSGEP->getOperand(0)->stripPointerCasts(true) ==
+            RHSGEP->getOperand(0)->stripPointerCasts(true)) {
         GEP2 = RHSGEP;
         GEP1 = LHSGEP;
         Swapped = false;
@@ -1470,8 +1474,8 @@
       Swapped = true;
     } else if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
       // (gep X, ...) - (gep X, ...)
-      if (RHSGEP->getOperand(0)->stripPointerCasts() ==
-            LHSGEP->getOperand(0)->stripPointerCasts()) {
+      if (RHSGEP->getOperand(0)->stripPointerCasts(true) ==
+            LHSGEP->getOperand(0)->stripPointerCasts(true)) {
         GEP2 = LHSGEP;
         GEP1 = RHSGEP;
         Swapped = true;
Index: test/Transforms/InstCombine/sub.ll
===================================================================
--- test/Transforms/InstCombine/sub.ll
+++ test/Transforms/InstCombine/sub.ll
@@ -308,6 +308,18 @@
 ; CHECK-NEXT: ret i64
 }
 
+define i64 @test25a(i8* %P, i64 %A){
+  %B = getelementptr inbounds [42 x i16], [42 x i16]* @Arr, i64 0, i64 %A
+  %BA = call i16* @llvm.noalias.p0i16(i16* %B, metadata !1)
+  %C = ptrtoint i16* %BA to i64
+  %G = sub i64 %C, ptrtoint (i16* getelementptr ([42 x i16], [42 x i16]* @Arr, i64 1, i64 0) to i64)
+  ret i64 %G
+; CHECK-LABEL: @test25a(
+; CHECK-NEXT: shl nuw i64 %A, 1
+; CHECK-NEXT: add i64 {{.*}}, -84
+; CHECK-NEXT: ret i64
+}
+
 @Arr_as1 = external addrspace(1) global [42 x i16]
 
 define i16 @test25_as1(i8 addrspace(1)* %P, i64 %A) {
@@ -550,3 +562,9 @@
 ; CHECK-NEXT: %sub = and i32 %y, %x.not
 ; CHECK: ret i32 %sub
 }
+
+declare i16* @llvm.noalias.p0i16(i16*, metadata) nounwind
+
+!0 = !{!0, !"some domain"}
+!1 = !{!1, !0, !"some scope"}
+

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9393.24731.patch
Type: text/x-patch
Size: 2613 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150430/9cadd193/attachment.bin>


More information about the llvm-commits mailing list