From 0f2af7f93b8fe43178eea5e42006021121401921 Mon Sep 17 00:00:00 2001 From: Daniel Berlin Date: Mon, 8 May 2017 17:37:33 +0000 Subject: [PATCH] ConstantFold: Handle gep nonnull, undef as well llvm-svn: 302447 --- llvm/lib/IR/ConstantFold.cpp | 7 ++++--- .../Transforms/Inline/inline-constexpr-addrspacecast-argument.ll | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index c185933..a20f3f8 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -2041,9 +2041,6 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, Optional InRangeIndex, ArrayRef Idxs) { if (Idxs.empty()) return C; - Constant *Idx0 = cast(Idxs[0]); - if ((Idxs.size() == 1 && Idx0->isNullValue())) - return C; if (isa(C)) { Type *GEPTy = GetElementPtrInst::getGEPReturnType( @@ -2051,6 +2048,10 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, return UndefValue::get(GEPTy); } + Constant *Idx0 = cast(Idxs[0]); + if (Idxs.size() == 1 && (Idx0->isNullValue() || isa(Idx0))) + return C; + if (C->isNullValue()) { bool isNull = true; for (unsigned i = 0, e = Idxs.size(); i != e; ++i) diff --git a/llvm/test/Transforms/Inline/inline-constexpr-addrspacecast-argument.ll b/llvm/test/Transforms/Inline/inline-constexpr-addrspacecast-argument.ll index 1f2b143..b8d41ab 100644 --- a/llvm/test/Transforms/Inline/inline-constexpr-addrspacecast-argument.ll +++ b/llvm/test/Transforms/Inline/inline-constexpr-addrspacecast-argument.ll @@ -6,7 +6,7 @@ target datalayout = "e-p3:32:32-p4:64:64-n32" @lds = internal addrspace(3) global [64 x i64] zeroinitializer ; CHECK-LABEL: @constexpr_addrspacecast_ptr_size_change( -; CHECK: load i64, i64 addrspace(4)* getelementptr (i64, i64 addrspace(4)* addrspacecast (i64 addrspace(3)* getelementptr inbounds ([64 x i64], [64 x i64] addrspace(3)* @lds, i32 0, i32 0) to i64 addrspace(4)*), i64 undef) +; CHECK: load i64, i64 addrspace(4)* addrspacecast (i64 addrspace(3)* getelementptr inbounds ([64 x i64], [64 x i64] addrspace(3)* @lds, i32 0, i32 0) to i64 addrspace(4)*) ; CHECK-NEXT: br define void @constexpr_addrspacecast_ptr_size_change() #0 { %tmp0 = call i32 @foo(i64 addrspace(4)* addrspacecast (i64 addrspace(3)* getelementptr inbounds ([64 x i64], [64 x i64] addrspace(3)* @lds, i32 0, i32 0) to i64 addrspace(4)*)) #1 -- 2.7.4