Skip to content

Commit 5780611

Browse files
committed
[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.
1 parent d9f5d7b commit 5780611

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,6 +2608,11 @@ static Instruction *convertBitCastToGEP(BitCastInst &CI, IRBuilderBase &Builder,
26082608
Value *Src = CI.getOperand(0);
26092609
PointerType *SrcPTy = cast<PointerType>(Src->getType());
26102610
PointerType *DstPTy = cast<PointerType>(CI.getType());
2611+
2612+
// Bitcasts involving opaque pointers cannot be converted into a GEP.
2613+
if (SrcPTy->isOpaque() || DstPTy->isOpaque())
2614+
return nullptr;
2615+
26112616
Type *DstElTy = DstPTy->getElementType();
26122617
Type *SrcElTy = SrcPTy->getElementType();
26132618

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -S -instcombine < %s | FileCheck %s
3+
4+
define ptr @bitcast_opaque_to_opaque(ptr %a) {
5+
; CHECK-LABEL: @bitcast_opaque_to_opaque(
6+
; CHECK-NEXT: ret ptr [[A:%.*]]
7+
;
8+
%b = bitcast ptr %a to ptr
9+
ret ptr %b
10+
}
11+
12+
define ptr @bitcast_typed_to_opaque(i8* %a) {
13+
; CHECK-LABEL: @bitcast_typed_to_opaque(
14+
; CHECK-NEXT: [[B:%.*]] = bitcast i8* [[A:%.*]] to ptr
15+
; CHECK-NEXT: ret ptr [[B]]
16+
;
17+
%b = bitcast i8* %a to ptr
18+
ret ptr %b
19+
}
20+
21+
define i8* @bitcast_opaque_to_typed(ptr %a) {
22+
; CHECK-LABEL: @bitcast_opaque_to_typed(
23+
; CHECK-NEXT: [[B:%.*]] = bitcast ptr [[A:%.*]] to i8*
24+
; CHECK-NEXT: ret i8* [[B]]
25+
;
26+
%b = bitcast ptr %a to i8*
27+
ret i8* %b
28+
}
29+
30+
;define ptr @addrspacecast_opaque_to_opaque(ptr addrspace(1) %a) {
31+
; %b = addrspacecast ptr addrspace(1) %a to ptr
32+
; ret ptr %b
33+
;}
34+
35+
;define ptr @addrspacecast_typed_to_opaque(i8 addrspace(1)* %a) {
36+
; %b = addrspacecast i8 addrspace(1)* %a to ptr
37+
; ret ptr %b
38+
;}
39+
40+
;define i8* @addrspacecast_opaque_to_typed(ptr addrspace(1) %a) {
41+
; %b = addrspacecast ptr addrspace(1) %a to i8*
42+
; ret i8* %b
43+
;}

0 commit comments

Comments
 (0)