From 39796e1ad02a45b09ac3ef9e3dc1906f28804a91 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 21 Jun 2021 20:35:30 +0200 Subject: [PATCH] Reapply [InstCombine] Don't try converting opaque pointer bitcast to GEP Reapplied without changes -- this was reverted together with an underlying patch. ----- 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. --- .../Transforms/InstCombine/InstCombineCasts.cpp | 5 +++ llvm/test/Transforms/InstCombine/opaque-ptr.ll | 43 ++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 llvm/test/Transforms/InstCombine/opaque-ptr.ll diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp index dcfff45..24187d5 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(Src->getType()); PointerType *DstPTy = cast(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 0000000..1bf205c --- /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 +;} -- 2.7.4