[llvm] 87bdde4 - [ConstantFold] Skip bitcast -> GEP transform for opaque pointers

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 22 06:51:40 PDT 2021


Author: Nikita Popov
Date: 2021-06-22T15:50:55+02:00
New Revision: 87bdde4962ea926e81a3534119ac8f02901b75b9

URL: https://github.com/llvm/llvm-project/commit/87bdde4962ea926e81a3534119ac8f02901b75b9
DIFF: https://github.com/llvm/llvm-project/commit/87bdde4962ea926e81a3534119ac8f02901b75b9.diff

LOG: [ConstantFold] Skip bitcast -> GEP transform for opaque pointers

Same as with the InstCombine transform, this is not possible for
bitcasts involving opaque pointers, as GEP preserves opaqueness.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp
    llvm/test/Transforms/InstCombine/opaque-ptr.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index ecd05bea5c8e8..652ccd3efb312 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -117,8 +117,9 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
   // the first element.  If so, return the appropriate GEP instruction.
   if (PointerType *PTy = dyn_cast<PointerType>(V->getType()))
     if (PointerType *DPTy = dyn_cast<PointerType>(DestTy))
-      if (PTy->getAddressSpace() == DPTy->getAddressSpace()
-          && PTy->getElementType()->isSized()) {
+      if (PTy->getAddressSpace() == DPTy->getAddressSpace() &&
+          !PTy->isOpaque() && !DPTy->isOpaque() &&
+          PTy->getElementType()->isSized()) {
         SmallVector<Value*, 8> IdxList;
         Value *Zero =
           Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));

diff  --git a/llvm/test/Transforms/InstCombine/opaque-ptr.ll b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
index 1bf205ca9141b..cd8e3c273456c 100644
--- a/llvm/test/Transforms/InstCombine/opaque-ptr.ll
+++ b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
@@ -27,6 +27,14 @@ define i8* @bitcast_opaque_to_typed(ptr %a) {
   ret i8* %b
 }
 
+ at g = global i8 0
+define ptr @bitcast_typed_to_opaque_constexpr() {
+; CHECK-LABEL: @bitcast_typed_to_opaque_constexpr(
+; CHECK-NEXT:    ret ptr bitcast (i8* @g to ptr)
+;
+  ret ptr bitcast (i8* @g to ptr)
+}
+
 ;define ptr @addrspacecast_opaque_to_opaque(ptr addrspace(1) %a) {
 ;  %b = addrspacecast ptr addrspace(1) %a to ptr
 ;  ret ptr %b


        


More information about the llvm-commits mailing list