Fix assertion failure in global-merge with unused ConstantExpr
authorOliver Stannard <oliver.stannard@arm.com>
Mon, 8 Jun 2015 16:55:31 +0000 (16:55 +0000)
committerOliver Stannard <oliver.stannard@arm.com>
Mon, 8 Jun 2015 16:55:31 +0000 (16:55 +0000)
The global-merge pass was crashing because it assumes that all ConstantExprs
(reached via the global variables that they use) have at least one user.

I haven't worked out a way to test this, as an unused ConstantExpr cannot be
represented by serialised IR, and global-merge can only be run in llc, which
does not run any passes which can make a ConstantExpr dead.

This (reduced to the point of silliness) C code triggers this bug when compiled
for arm-none-eabi at -O1:

  static a = 7;
  static volatile b[10] = {&a};

  c;
  main() {
    c = 0;
    for (; c < 10;)
      printf(b[c]);
  }

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239308 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/GlobalMerge.cpp

index df54a9c6916216b3b5bd357a3c1cb821d9c93a21..37b3bf17ed1f30ab6f2d5edb0fbc8e9ed8c71161 100644 (file)
@@ -280,6 +280,8 @@ bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
       // users, so look through ConstantExpr...
       Use *UI, *UE;
       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
+        if (CE->use_empty())
+          continue;
         UI = &*CE->use_begin();
         UE = nullptr;
       } else if (isa<Instruction>(U.getUser())) {