From 628c200526b6d72ef592b920e196220af1082c83 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. --- .../InstCombine/InstCombineCasts.cpp | 5 +++ test/Transforms/InstCombine/opaque-ptr.ll | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 test/Transforms/InstCombine/opaque-ptr.ll diff --git a/lib/Transforms/InstCombine/InstCombineCasts.cpp b/lib/Transforms/InstCombine/InstCombineCasts.cpp index dcfff4552f1..24187d50a48 100644 --- a/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/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/test/Transforms/InstCombine/opaque-ptr.ll b/test/Transforms/InstCombine/opaque-ptr.ll new file mode 100644 index 00000000000..1bf205ca914 --- /dev/null +++ b/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 +;}