[llvm] r270826 - [ConstantFold] Fix incorrect index rewrites for GEPs

Adam Nemet via llvm-commits llvm-commits at lists.llvm.org
Thu May 26 00:08:05 PDT 2016


Author: anemet
Date: Thu May 26 02:08:05 2016
New Revision: 270826

URL: http://llvm.org/viewvc/llvm-project?rev=270826&view=rev
Log:
[ConstantFold] Fix incorrect index rewrites for GEPs

Summary:
If an index for a vector or array type is out-of-range GEP constant
folding tries to factor it into preceding dimensions.  The code however
does not consider addressing of structure field padding which should not
qualify as out-of-range index.

As demonstrated by the testcase, this can occur if the indexing
performed on a vector type and the preceding index is an array type.

SROA generates GEPs for example involving padding bytes as it slices an
alloca.

My fix disables this folding if the element type is a vector type.  I
believe that this is the only way we can end up with padding.  (We have
no access to DataLayout so I am not sure if there is actual robust way
of actually checking the presence of padding.)

Reviewers: majnemer

Subscribers: llvm-commits, Gerolf

Differential Revision: http://reviews.llvm.org/D20663

Added:
    llvm/trunk/test/Transforms/InstCombine/getelementptr-folding.ll
Modified:
    llvm/trunk/lib/IR/ConstantFold.cpp

Modified: llvm/trunk/lib/IR/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=270826&r1=270825&r2=270826&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Thu May 26 02:08:05 2016
@@ -2193,7 +2193,7 @@ static Constant *ConstantFoldGetElementP
   for (unsigned i = 1, e = Idxs.size(); i != e;
        Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
     if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
-      if (isa<ArrayType>(Ty) || isa<VectorType>(Ty))
+      if (isa<ArrayType>(Ty))
         if (CI->getSExtValue() > 0 &&
             !isIndexInRangeOfSequentialType(cast<SequentialType>(Ty), CI)) {
           if (isa<SequentialType>(Prev)) {

Added: llvm/trunk/test/Transforms/InstCombine/getelementptr-folding.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/getelementptr-folding.ll?rev=270826&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/getelementptr-folding.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/getelementptr-folding.ll Thu May 26 02:08:05 2016
@@ -0,0 +1,13 @@
+; RUN: opt -instcombine -S < %s | FileCheck %s
+
+%struct.matrix_float3x3 = type { [3 x <3 x float>] }
+
+; We used to fold this by rewriting the indices to 0, 0, 2, 0.  This is
+; invalid because there is a 4-byte padding after each <3 x float> field.
+
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.11.0"
+
+ at matrix_identity_float3x3 = external global %struct.matrix_float3x3, align 16
+ at bbb = global float* getelementptr inbounds (%struct.matrix_float3x3, %struct.matrix_float3x3* @matrix_identity_float3x3, i64 0, i32 0, i64 1, i64 3)
+; CHECK: @bbb = global float* getelementptr inbounds (%struct.matrix_float3x3, %struct.matrix_float3x3* @matrix_identity_float3x3, i64 0, i32 0, i64 1, i64 3)




More information about the llvm-commits mailing list