[llvm] 5780611 - [InstCombine] Don't try converting opaque pointer bitcast to GEP

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 21 12:25:03 PDT 2021


Author: Nikita Popov
Date: 2021-06-21T21:24:50+02:00
New Revision: 5780611d7e044ef56c4214df2c236ef5e15545ab

URL: https://github.com/llvm/llvm-project/commit/5780611d7e044ef56c4214df2c236ef5e15545ab
DIFF: https://github.com/llvm/llvm-project/commit/5780611d7e044ef56c4214df2c236ef5e15545ab.diff

LOG: [InstCombine] Don't try converting opaque pointer bitcast to GEP

Bitcasts having opaque pointer source or result type cannot be
converted into a zero-index GEP, GEP source and result types
always have the same opaque-ness.

Added: 
    llvm/test/Transforms/InstCombine/opaque-ptr.ll

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index dcfff4552f11..24187d50a484 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -2608,6 +2608,11 @@ static Instruction *convertBitCastToGEP(BitCastInst &CI, IRBuilderBase &Builder,
   Value *Src = CI.getOperand(0);
   PointerType *SrcPTy = cast<PointerType>(Src->getType());
   PointerType *DstPTy = cast<PointerType>(CI.getType());
+
+  // Bitcasts involving opaque pointers cannot be converted into a GEP.
+  if (SrcPTy->isOpaque() || DstPTy->isOpaque())
+    return nullptr;
+
   Type *DstElTy = DstPTy->getElementType();
   Type *SrcElTy = SrcPTy->getElementType();
 

diff  --git a/llvm/test/Transforms/InstCombine/opaque-ptr.ll b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
new file mode 100644
index 000000000000..1bf205ca9141
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
@@ -0,0 +1,43 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -instcombine < %s | FileCheck %s
+
+define ptr @bitcast_opaque_to_opaque(ptr %a) {
+; CHECK-LABEL: @bitcast_opaque_to_opaque(
+; CHECK-NEXT:    ret ptr [[A:%.*]]
+;
+  %b = bitcast ptr %a to ptr
+  ret ptr %b
+}
+
+define ptr @bitcast_typed_to_opaque(i8* %a) {
+; CHECK-LABEL: @bitcast_typed_to_opaque(
+; CHECK-NEXT:    [[B:%.*]] = bitcast i8* [[A:%.*]] to ptr
+; CHECK-NEXT:    ret ptr [[B]]
+;
+  %b = bitcast i8* %a to ptr
+  ret ptr %b
+}
+
+define i8* @bitcast_opaque_to_typed(ptr %a) {
+; CHECK-LABEL: @bitcast_opaque_to_typed(
+; CHECK-NEXT:    [[B:%.*]] = bitcast ptr [[A:%.*]] to i8*
+; CHECK-NEXT:    ret i8* [[B]]
+;
+  %b = bitcast ptr %a to i8*
+  ret i8* %b
+}
+
+;define ptr @addrspacecast_opaque_to_opaque(ptr addrspace(1) %a) {
+;  %b = addrspacecast ptr addrspace(1) %a to ptr
+;  ret ptr %b
+;}
+
+;define ptr @addrspacecast_typed_to_opaque(i8 addrspace(1)* %a) {
+;  %b = addrspacecast i8 addrspace(1)* %a to ptr
+;  ret ptr %b
+;}
+
+;define i8* @addrspacecast_opaque_to_typed(ptr addrspace(1) %a) {
+;  %b = addrspacecast ptr addrspace(1) %a to i8*
+;  ret i8* %b
+;}


        


More information about the llvm-commits mailing list