Fwd: [PATCH] Don't constant fold through zero-length fields

Thomas Jablin tjablin at gmail.com
Fri Aug 22 13:48:40 PDT 2014


Hi All,

Please find enclosed a patch to avoid constant folding through empty type
fields. Previously, ConstantFoldLoadThroughGEPIndices and
ConstantFoldLoadThroughGEPConstantExpr would return zeroinitializer when a
constant GEP expression indexed to a zero-length field. The patch adds
checks to these two methods based on isEmptyTy to avoid these scenarios.
The patch also includes a simple testcase. I have verified that the
testcase fails with r216246 and passes after applying the patch.

Tom

http://reviews.llvm.org/D5032

Files:
  lib/Analysis/ConstantFolding.cpp
  test/Transforms/GlobalOpt/empty-type-field.ll

Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -1206,6 +1206,10 @@
     C = C->getAggregateElement(CE->getOperand(i));
     if (!C)
       return nullptr;
+
+    // Don't step through empty types. The aliasing is too hard to figure
out.
+    if (C->getType()->isEmptyTy())
+      return nullptr;
   }
   return C;
 }
@@ -1222,6 +1226,10 @@
     C = C->getAggregateElement(Indices[i]);
     if (!C)
       return nullptr;
+
+    // Don't step through empty types. The aliasing is too hard to figure
out.
+    if (C->getType()->isEmptyTy())
+      return nullptr;
   }
   return C;
 }
Index: test/Transforms/GlobalOpt/empty-type-field.ll
===================================================================
--- test/Transforms/GlobalOpt/empty-type-field.ll
+++ test/Transforms/GlobalOpt/empty-type-field.ll
@@ -0,0 +1,19 @@
+; RUN: opt < %s -S -globalopt | FileCheck %s
+
+; ModuleID = 'bugpoint-passinput.bc'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+%struct.qux = type { [0 x double], double }
+
+ at qux = internal global %struct.qux { [0 x double] zeroinitializer, double
4.053854e+04 }, align 8
+
+define void @foo() {
+entry:
+  %0 = load double* getelementptr inbounds (%struct.qux* @qux, i32 0, i32
0, i32 0), align 1
+  call void @bar(double %0)
+; CHECK-NOT: @bar(double 0.000000e+00)
+  ret void
+}
+
+declare void @bar(double)
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140822/b1ccd25a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D5032.12855.patch
Type: text/x-patch
Size: 1565 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140822/b1ccd25a/attachment.bin>


More information about the llvm-commits mailing list