Skip to content

Commit e2c2124

Browse files
committed
Reapply [InstCombine] Extract bitcast -> gep transform
Relative to the original patch, an InstCombine test has been added to show a previously missed pattern, and the Coroutine test that resulted in the revert has been regenerated. ----- Move this into a separate function, to make sure that early returns do not accidentally skip other transforms. This previously happened for the isSized() check, which skipped folds like distributing a bitcast over a select.
1 parent 403792f commit e2c2124

File tree

3 files changed

+58
-54
lines changed

3 files changed

+58
-54
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,6 +2603,52 @@ Instruction *InstCombinerImpl::optimizeBitCastFromPhi(CastInst &CI,
26032603
return RetVal;
26042604
}
26052605

2606+
static Instruction *convertBitCastToGEP(BitCastInst &CI, IRBuilderBase &Builder,
2607+
const DataLayout &DL) {
2608+
Value *Src = CI.getOperand(0);
2609+
PointerType *SrcPTy = cast<PointerType>(Src->getType());
2610+
PointerType *DstPTy = cast<PointerType>(CI.getType());
2611+
Type *DstElTy = DstPTy->getElementType();
2612+
Type *SrcElTy = SrcPTy->getElementType();
2613+
2614+
// When the type pointed to is not sized the cast cannot be
2615+
// turned into a gep.
2616+
if (!SrcElTy->isSized())
2617+
return nullptr;
2618+
2619+
// If the source and destination are pointers, and this cast is equivalent
2620+
// to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
2621+
// This can enhance SROA and other transforms that want type-safe pointers.
2622+
unsigned NumZeros = 0;
2623+
while (SrcElTy && SrcElTy != DstElTy) {
2624+
SrcElTy = GetElementPtrInst::getTypeAtIndex(SrcElTy, (uint64_t)0);
2625+
++NumZeros;
2626+
}
2627+
2628+
// If we found a path from the src to dest, create the getelementptr now.
2629+
if (SrcElTy == DstElTy) {
2630+
SmallVector<Value *, 8> Idxs(NumZeros + 1, Builder.getInt32(0));
2631+
GetElementPtrInst *GEP =
2632+
GetElementPtrInst::Create(SrcPTy->getElementType(), Src, Idxs);
2633+
2634+
// If the source pointer is dereferenceable, then assume it points to an
2635+
// allocated object and apply "inbounds" to the GEP.
2636+
bool CanBeNull, CanBeFreed;
2637+
if (Src->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed)) {
2638+
// In a non-default address space (not 0), a null pointer can not be
2639+
// assumed inbounds, so ignore that case (dereferenceable_or_null).
2640+
// The reason is that 'null' is not treated differently in these address
2641+
// spaces, and we consequently ignore the 'gep inbounds' special case
2642+
// for 'null' which allows 'inbounds' on 'null' if the indices are
2643+
// zeros.
2644+
if (SrcPTy->getAddressSpace() == 0 || !CanBeNull)
2645+
GEP->setIsInBounds();
2646+
}
2647+
return GEP;
2648+
}
2649+
return nullptr;
2650+
}
2651+
26062652
Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
26072653
// If the operands are integer typed then apply the integer transforms,
26082654
// otherwise just apply the common ones.
@@ -2616,11 +2662,6 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
26162662
return replaceInstUsesWith(CI, Src);
26172663

26182664
if (isa<PointerType>(SrcTy) && isa<PointerType>(DestTy)) {
2619-
PointerType *SrcPTy = cast<PointerType>(SrcTy);
2620-
PointerType *DstPTy = cast<PointerType>(DestTy);
2621-
Type *DstElTy = DstPTy->getElementType();
2622-
Type *SrcElTy = SrcPTy->getElementType();
2623-
26242665
// If we are casting a alloca to a pointer to a type of the same
26252666
// size, rewrite the allocation instruction to allocate the "right" type.
26262667
// There is no need to modify malloc calls because it is their bitcast that
@@ -2629,43 +2670,8 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
26292670
if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
26302671
return V;
26312672

2632-
// When the type pointed to is not sized the cast cannot be
2633-
// turned into a gep.
2634-
Type *PointeeType =
2635-
cast<PointerType>(Src->getType()->getScalarType())->getElementType();
2636-
if (!PointeeType->isSized())
2637-
return nullptr;
2638-
2639-
// If the source and destination are pointers, and this cast is equivalent
2640-
// to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
2641-
// This can enhance SROA and other transforms that want type-safe pointers.
2642-
unsigned NumZeros = 0;
2643-
while (SrcElTy && SrcElTy != DstElTy) {
2644-
SrcElTy = GetElementPtrInst::getTypeAtIndex(SrcElTy, (uint64_t)0);
2645-
++NumZeros;
2646-
}
2647-
2648-
// If we found a path from the src to dest, create the getelementptr now.
2649-
if (SrcElTy == DstElTy) {
2650-
SmallVector<Value *, 8> Idxs(NumZeros + 1, Builder.getInt32(0));
2651-
GetElementPtrInst *GEP =
2652-
GetElementPtrInst::Create(SrcPTy->getElementType(), Src, Idxs);
2653-
2654-
// If the source pointer is dereferenceable, then assume it points to an
2655-
// allocated object and apply "inbounds" to the GEP.
2656-
bool CanBeNull, CanBeFreed;
2657-
if (Src->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed)) {
2658-
// In a non-default address space (not 0), a null pointer can not be
2659-
// assumed inbounds, so ignore that case (dereferenceable_or_null).
2660-
// The reason is that 'null' is not treated differently in these address
2661-
// spaces, and we consequently ignore the 'gep inbounds' special case
2662-
// for 'null' which allows 'inbounds' on 'null' if the indices are
2663-
// zeros.
2664-
if (SrcPTy->getAddressSpace() == 0 || !CanBeNull)
2665-
GEP->setIsInBounds();
2666-
}
2667-
return GEP;
2668-
}
2673+
if (Instruction *I = convertBitCastToGEP(CI, Builder, DL))
2674+
return I;
26692675
}
26702676

26712677
if (FixedVectorType *DestVTy = dyn_cast<FixedVectorType>(DestTy)) {

llvm/test/Transforms/Coroutines/coro-retcon-once-value.ll

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ define {i8*, i32} @f(i8* %buffer, i32* %array) {
1212
; CHECK-NEXT: store i32* [[ARRAY:%.*]], i32** [[ARRAY_SPILL_ADDR]], align 8
1313
; CHECK-NEXT: [[LOAD:%.*]] = load i32, i32* [[ARRAY]], align 4
1414
; CHECK-NEXT: [[LOAD_POS:%.*]] = icmp sgt i32 [[LOAD]], 0
15-
; CHECK-NEXT: [[TMP0:%.*]] = select i1 [[LOAD_POS]], void (i8*, i1)* @f.resume.0, void (i8*, i1)* @f.resume.1
16-
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[LOAD_POS]], i32 [[LOAD]], i32 0
17-
; CHECK-NEXT: [[TMP2:%.*]] = bitcast void (i8*, i1)* [[TMP0]] to i8*
18-
; CHECK-NEXT: [[TMP3:%.*]] = insertvalue { i8*, i32 } undef, i8* [[TMP2]], 0
19-
; CHECK-NEXT: [[TMP4:%.*]] = insertvalue { i8*, i32 } [[TMP3]], i32 [[TMP1]], 1
20-
; CHECK-NEXT: ret { i8*, i32 } [[TMP4]]
15+
; CHECK-NEXT: [[TMP0:%.*]] = select i1 [[LOAD_POS]], i32 [[LOAD]], i32 0
16+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[LOAD_POS]], i8* bitcast (void (i8*, i1)* @f.resume.0 to i8*), i8* bitcast (void (i8*, i1)* @f.resume.1 to i8*)
17+
; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { i8*, i32 } undef, i8* [[TMP1]], 0
18+
; CHECK-NEXT: [[TMP3:%.*]] = insertvalue { i8*, i32 } [[TMP2]], i32 [[TMP0]], 1
19+
; CHECK-NEXT: ret { i8*, i32 } [[TMP3]]
2120
;
2221
entry:
2322
%id = call token @llvm.coro.id.retcon.once(i32 8, i32 8, i8* %buffer, i8* bitcast (void (i8*, i1)* @prototype to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
@@ -58,10 +57,10 @@ define void @test(i32* %array) {
5857
; CHECK-NEXT: store i32* [[ARRAY:%.*]], i32** [[TMP0]], align 8
5958
; CHECK-NEXT: [[LOAD_I:%.*]] = load i32, i32* [[ARRAY]], align 4
6059
; CHECK-NEXT: [[LOAD_POS_I:%.*]] = icmp sgt i32 [[LOAD_I]], 0
61-
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[LOAD_POS_I]], void (i8*, i1)* @f.resume.0, void (i8*, i1)* @f.resume.1
62-
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[LOAD_POS_I]], i32 [[LOAD_I]], i32 0
63-
; CHECK-NEXT: call void @print(i32 [[TMP2]])
64-
; CHECK-NEXT: call void [[TMP1]](i8* nonnull [[DOTSUB]], i1 zeroext false)
60+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[LOAD_POS_I]], i32 [[LOAD_I]], i32 0
61+
; CHECK-NEXT: call void @print(i32 [[TMP1]])
62+
; CHECK-NEXT: [[CONT_CAST:%.*]] = select i1 [[LOAD_POS_I]], void (i8*, i1)* @f.resume.0, void (i8*, i1)* @f.resume.1
63+
; CHECK-NEXT: call void [[CONT_CAST]](i8* nonnull [[DOTSUB]], i1 zeroext false)
6564
; CHECK-NEXT: ret void
6665
;
6766
entry:

llvm/test/Transforms/InstCombine/bitcast.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,7 @@ declare void @f1()
584584
declare void @f2()
585585
define i8* @select_bitcast_unsized_pointer(i1 %c) {
586586
; CHECK-LABEL: @select_bitcast_unsized_pointer(
587-
; CHECK-NEXT: [[S:%.*]] = select i1 [[C:%.*]], void ()* @f1, void ()* @f2
588-
; CHECK-NEXT: [[B:%.*]] = bitcast void ()* [[S]] to i8*
587+
; CHECK-NEXT: [[B:%.*]] = select i1 [[C:%.*]], i8* bitcast (void ()* @f1 to i8*), i8* bitcast (void ()* @f2 to i8*)
589588
; CHECK-NEXT: ret i8* [[B]]
590589
;
591590
%s = select i1 %c, void ()* @f1, void ()* @f2

0 commit comments

Comments
 (0)