[PATCH] D21499: Load Combine : Combine Loads formed from GEPS with negative indexes

River Riddle via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 18 18:15:34 PDT 2016


rriddle updated this revision to Diff 61183.
rriddle added a comment.

Fixed the size type change mistake. Changed the iteration variable to be a separate value.


http://reviews.llvm.org/D21499

Files:
  /Users/rriddle/Desktop/llvm/llvm/lib/Transforms/Scalar/LoadCombine.cpp
  /Users/rriddle/Desktop/llvm/llvm/test/Transforms/LoadCombine/load-combine-negativegep.ll

Index: /Users/rriddle/Desktop/llvm/llvm/test/Transforms/LoadCombine/load-combine-negativegep.ll
===================================================================
--- /Users/rriddle/Desktop/llvm/llvm/test/Transforms/LoadCombine/load-combine-negativegep.ll
+++ /Users/rriddle/Desktop/llvm/llvm/test/Transforms/LoadCombine/load-combine-negativegep.ll
@@ -0,0 +1,16 @@
+; RUN: opt -basicaa -load-combine -S < %s | FileCheck %s
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @Load_NegGep(i32* %i){
+  %1 = getelementptr inbounds i32, i32* %i, i64 -1
+  %2 = load i32, i32* %1, align 4
+  %3 = load i32, i32* %i, align 4
+  %4 = add nsw i32 %3, %2
+  ret i32 %4
+; CHECK-LABEL: @Load_NegGep(
+; CHECK: load i64, i64* %{{.*}}, align 4
+; CHECK-NOT: load
+}
+
+
Index: /Users/rriddle/Desktop/llvm/llvm/lib/Transforms/Scalar/LoadCombine.cpp
===================================================================
--- /Users/rriddle/Desktop/llvm/llvm/lib/Transforms/Scalar/LoadCombine.cpp
+++ /Users/rriddle/Desktop/llvm/llvm/lib/Transforms/Scalar/LoadCombine.cpp
@@ -40,7 +40,7 @@
 namespace {
 struct PointerOffsetPair {
   Value *Pointer;
-  uint64_t Offset;
+  int64_t Offset;
 };
 
 struct LoadPOPPair {
@@ -102,7 +102,7 @@
       unsigned BitWidth = DL.getPointerTypeSizeInBits(GEP->getType());
       APInt Offset(BitWidth, 0);
       if (GEP->accumulateConstantOffset(DL, Offset))
-        POP.Offset += Offset.getZExtValue();
+        POP.Offset += Offset.getSExtValue();
       else
         // Can't handle GEPs with variable indices.
         return POP;
@@ -138,15 +138,17 @@
   LoadInst *BaseLoad = nullptr;
   SmallVector<LoadPOPPair, 8> AggregateLoads;
   bool Combined = false;
-  uint64_t PrevOffset = -1ull;
+  bool ValidPrevOffset = false;
+  int64_t PrevOffset = 0;
   uint64_t PrevSize = 0;
   for (auto &L : Loads) {
-    if (PrevOffset == -1ull) {
+    if (ValidPrevOffset == false) {
       BaseLoad = L.Load;
       PrevOffset = L.POP.Offset;
       PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize(
           L.Load->getType());
       AggregateLoads.push_back(L);
+      ValidPrevOffset = true;
       continue;
     }
     if (L.Load->getAlignment() > BaseLoad->getAlignment())
@@ -156,7 +158,7 @@
       if (combineLoads(AggregateLoads))
         Combined = true;
       AggregateLoads.clear();
-      PrevOffset = -1;
+      ValidPrevOffset = false;
       continue;
     }
     if (L.POP.Offset != PrevOffset + PrevSize)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21499.61183.patch
Type: text/x-patch
Size: 2530 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160619/acb6096f/attachment.bin>


More information about the llvm-commits mailing list