[PATCH] D38557: Convert an APInt to int64_t properly in TTI::getGEPCost().

Justin Lebar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 4 12:44:01 PDT 2017


jlebar created this revision.
Herald added subscribers: javed.absar, sanjoy.

If the pointer width is 32 bits and the calculated GEP offset is
negative, we call APInt::getLimitedValue(), which does a
*zero*-extension of the offset.  That's wrong -- we should do an sext.

Fixes a bug introduced in https://reviews.llvm.org/rL314362 and found by Evgeny Astigeevich.


https://reviews.llvm.org/D38557

Files:
  llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
  llvm/test/Analysis/CostModel/ARM/gep.ll


Index: llvm/test/Analysis/CostModel/ARM/gep.ll
===================================================================
--- llvm/test/Analysis/CostModel/ARM/gep.ll
+++ llvm/test/Analysis/CostModel/ARM/gep.ll
@@ -83,5 +83,8 @@
 ;CHECK: cost of 1 for instruction: {{.*}} getelementptr inbounds <4 x double>, <4 x double>*
   %c12 = getelementptr inbounds <4 x double>, <4 x double>* undef, i32 %i
 
+;CHECK: cost of 0 for instruction: {{.*}} getelementptr inbounds i8, i8*
+  %d0 = getelementptr inbounds i8, i8* undef, i32 -1
+
   ret void
 }
Index: llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
===================================================================
--- llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -718,10 +718,15 @@
     // Assumes the address space is 0 when Ptr is nullptr.
     unsigned AS =
         (Ptr == nullptr ? 0 : Ptr->getType()->getPointerAddressSpace());
+
+    // Convert BaseOffset to an int64_t, clamping to int64_t::max if necessary.
+    int64_t BaseOffsetInt64 =
+        BaseOffset.sgt(std::numeric_limits<int64_t>::max())
+            ? std::numeric_limits<int64_t>::max()
+            : BaseOffset.getSExtValue();
     if (static_cast<T *>(this)->isLegalAddressingMode(
-            TargetType, const_cast<GlobalValue *>(BaseGV),
-            static_cast<int64_t>(BaseOffset.getLimitedValue()), HasBaseReg,
-            Scale, AS))
+            TargetType, const_cast<GlobalValue *>(BaseGV), BaseOffsetInt64,
+            HasBaseReg, Scale, AS))
       return TTI::TCC_Free;
     return TTI::TCC_Basic;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38557.117712.patch
Type: text/x-patch
Size: 1616 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171004/bfc07e3e/attachment.bin>


More information about the llvm-commits mailing list