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

Thomas Jablin tjablin at gmail.com
Fri Aug 22 16:01:02 PDT 2014


Fix a related bug in ConstantFoldLoadThroughBitcast and add an additional test case.

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
@@ -512,7 +512,10 @@
     // a different type, so we can try to walk down through the initial
     // elements of an aggregate to see if some part of th e aggregate is
     // castable to implement the "load" semantic model.
-    C = C->getAggregateElement(0u);
+    Constant *D = C->getAggregateElement(0U);
+    for (unsigned i = 0; D && D->getType()->isEmptyTy(); ++i)
+      D = C->getAggregateElement(i);
+    C = D;
   } while (C);
 
   return nullptr;
@@ -1206,6 +1209,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 +1229,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,29 @@
+; RUN: opt < %s -S -globalopt -instcombine | 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
+
+%struct.gorp = type { [0 x i8*], i8* }
+
+ at gorp = internal unnamed_addr constant %struct.gorp { [0 x i8*] zeroinitializer, i8* inttoptr (i64 1 to i8*) }, 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)
+
+  %1 = load i64* bitcast (%struct.gorp* @gorp to i64*), align 1
+  call fastcc void @blarg(i64 %1)
+; CHECK-NOT: @blarg(i64 0)
+
+  ret void
+}
+
+declare void @bar(double)
+declare void @blarg(i64)
\ No newline at end of file
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D5032.12863.patch
Type: text/x-patch
Size: 2360 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140822/dad06b9e/attachment.bin>


More information about the llvm-commits mailing list