From ed9abe119b3cfa2b4019782dbe10180e1e71b8cf Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Wed, 22 Jul 2015 22:29:30 +0000 Subject: [PATCH] [ConstantFolding] Support folding loads from a GlobalAlias 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 llvm-svn: 242955 --- llvm/lib/Analysis/ConstantFolding.cpp | 4 ++++ llvm/test/Transforms/SCCP/global-alias-constprop.ll | 11 +++++++++++ 2 files changed, 15 insertions(+) create mode 100644 llvm/test/Transforms/SCCP/global-alias-constprop.ll diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index bd9439e..70c929b 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -532,6 +532,10 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, if (GV->isConstant() && GV->hasDefinitiveInitializer()) return GV->getInitializer(); + if (auto *GA = dyn_cast(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(C); if (!CE) diff --git a/llvm/test/Transforms/SCCP/global-alias-constprop.ll b/llvm/test/Transforms/SCCP/global-alias-constprop.ll new file mode 100644 index 0000000..453755b --- /dev/null +++ b/llvm/test/Transforms/SCCP/global-alias-constprop.ll @@ -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 +} -- 2.7.4