[ConstantFolding] Support folding loads from a GlobalAlias
authorDavid Majnemer <david.majnemer@gmail.com>
Wed, 22 Jul 2015 22:29:30 +0000 (22:29 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Wed, 22 Jul 2015 22:29:30 +0000 (22:29 +0000)
The MSVC ABI requires that we generate an alias for the vtable which
means looking through a GlobalAlias which cannot be overridden improves
our ability to devirtualize.

Found while investigating PR20801.

Patch by Andrew Zhogin!

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

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

lib/Analysis/ConstantFolding.cpp
test/Transforms/SCCP/global-alias-constprop.ll [new file with mode: 0644]

index bd9439ec4d631a659572424847edc6322c6e029e..70c929b53a3f91881bd70e92b971019a0c76a9f4 100644 (file)
@@ -532,6 +532,10 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C,
     if (GV->isConstant() && GV->hasDefinitiveInitializer())
       return GV->getInitializer();
 
+  if (auto *GA = dyn_cast<GlobalAlias>(C))
+    if (GA->getAliasee() && !GA->mayBeOverridden())
+      return ConstantFoldLoadFromConstPtr(GA->getAliasee(), DL);
+
   // If the loaded value isn't a constant expr, we can't handle it.
   ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
   if (!CE)
diff --git a/test/Transforms/SCCP/global-alias-constprop.ll b/test/Transforms/SCCP/global-alias-constprop.ll
new file mode 100644 (file)
index 0000000..453755b
--- /dev/null
@@ -0,0 +1,11 @@
+; RUN: opt < %s -sccp -S | FileCheck %s
+
+@0 = private unnamed_addr constant [2 x i32] [i32 -1, i32 1]
+@"\01??_7A@@6B@" = unnamed_addr alias getelementptr inbounds ([2 x i32], [2 x i32]* @0, i32 0, i32 1)
+
+; CHECK: ret i32 1
+
+define i32 @main() {
+  %a = load i32, i32* @"\01??_7A@@6B@"
+  ret i32 %a
+}