From 3c9e97ad45cb250cec6eb70dc54968f41d58557f Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Fri, 5 Aug 2016 19:23:29 +0000 Subject: [PATCH] [ConstantFolding] Don't create illegal (non-integral) inttoptrs Reviewers: majnemer, arsenm Subscribers: mcrosier, llvm-commits Differential Revision: https://reviews.llvm.org/D23182 llvm-svn: 277854 --- lib/Analysis/ConstantFolding.cpp | 7 ++++--- .../InstCombine/non-integral-pointers.ll | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 test/Transforms/InstCombine/non-integral-pointers.ll diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index 39e422f164c..a7fb10b2d2b 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -828,7 +828,9 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP, } } - if (Ptr->isNullValue() || BasePtr != 0) { + auto *PTy = cast(Ptr->getType()); + if ((Ptr->isNullValue() || BasePtr != 0) && + !DL.isNonIntegralPointerType(PTy)) { Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr); return ConstantExpr::getIntToPtr(C, ResTy); } @@ -837,8 +839,7 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP, // we eliminate over-indexing of the notional static type array bounds. // This makes it easy to determine if the getelementptr is "inbounds". // Also, this helps GlobalOpt do SROA on GlobalVariables. - Type *Ty = Ptr->getType(); - assert(Ty->isPointerTy() && "Forming regular GEP of non-pointer type"); + Type *Ty = PTy; SmallVector NewIdxs; do { diff --git a/test/Transforms/InstCombine/non-integral-pointers.ll b/test/Transforms/InstCombine/non-integral-pointers.ll new file mode 100644 index 00000000000..d5e2569179b --- /dev/null +++ b/test/Transforms/InstCombine/non-integral-pointers.ll @@ -0,0 +1,20 @@ +; RUN: opt -instcombine -S < %s | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-ni:4" +target triple = "x86_64-unknown-linux-gnu" + +define i8 addrspace(4)* @f_0() { +; CHECK-LABEL: @f_0( +; CHECK: ret i8 addrspace(4)* getelementptr (i8, i8 addrspace(4)* null, i64 50) + %result = getelementptr i8, i8 addrspace(4)* null, i64 50 + ret i8 addrspace(4)* %result +} + +define i8 addrspace(3)* @f_1() { +; inttoptr is fine here since addrspace(3) is integral. + +; CHECK-LABEL: @f_1( +; CHECK: ret i8 addrspace(3)* inttoptr (i64 50 to i8 addrspace(3)*) + %result = getelementptr i8, i8 addrspace(3)* null, i64 50 + ret i8 addrspace(3)* %result +}