Skip to content

Commit 6922ab7

Browse files
committed
Revert "[InstCombine] Extract bitcast -> gep transform"
This reverts commit d9f5d7b. This reverts commit 5780611. This causes a failure in Coroutine tests.
1 parent 862313c commit 6922ab7

File tree

2 files changed

+42
-96
lines changed

2 files changed

+42
-96
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,57 +2603,6 @@ 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-
2612-
// Bitcasts involving opaque pointers cannot be converted into a GEP.
2613-
if (SrcPTy->isOpaque() || DstPTy->isOpaque())
2614-
return nullptr;
2615-
2616-
Type *DstElTy = DstPTy->getElementType();
2617-
Type *SrcElTy = SrcPTy->getElementType();
2618-
2619-
// When the type pointed to is not sized the cast cannot be
2620-
// turned into a gep.
2621-
if (!SrcElTy->isSized())
2622-
return nullptr;
2623-
2624-
// If the source and destination are pointers, and this cast is equivalent
2625-
// to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
2626-
// This can enhance SROA and other transforms that want type-safe pointers.
2627-
unsigned NumZeros = 0;
2628-
while (SrcElTy && SrcElTy != DstElTy) {
2629-
SrcElTy = GetElementPtrInst::getTypeAtIndex(SrcElTy, (uint64_t)0);
2630-
++NumZeros;
2631-
}
2632-
2633-
// If we found a path from the src to dest, create the getelementptr now.
2634-
if (SrcElTy == DstElTy) {
2635-
SmallVector<Value *, 8> Idxs(NumZeros + 1, Builder.getInt32(0));
2636-
GetElementPtrInst *GEP =
2637-
GetElementPtrInst::Create(SrcPTy->getElementType(), Src, Idxs);
2638-
2639-
// If the source pointer is dereferenceable, then assume it points to an
2640-
// allocated object and apply "inbounds" to the GEP.
2641-
bool CanBeNull, CanBeFreed;
2642-
if (Src->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed)) {
2643-
// In a non-default address space (not 0), a null pointer can not be
2644-
// assumed inbounds, so ignore that case (dereferenceable_or_null).
2645-
// The reason is that 'null' is not treated differently in these address
2646-
// spaces, and we consequently ignore the 'gep inbounds' special case
2647-
// for 'null' which allows 'inbounds' on 'null' if the indices are
2648-
// zeros.
2649-
if (SrcPTy->getAddressSpace() == 0 || !CanBeNull)
2650-
GEP->setIsInBounds();
2651-
}
2652-
return GEP;
2653-
}
2654-
return nullptr;
2655-
}
2656-
26572606
Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
26582607
// If the operands are integer typed then apply the integer transforms,
26592608
// otherwise just apply the common ones.
@@ -2667,6 +2616,11 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
26672616
return replaceInstUsesWith(CI, Src);
26682617

26692618
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+
26702624
// If we are casting a alloca to a pointer to a type of the same
26712625
// size, rewrite the allocation instruction to allocate the "right" type.
26722626
// There is no need to modify malloc calls because it is their bitcast that
@@ -2675,8 +2629,43 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
26752629
if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
26762630
return V;
26772631

2678-
if (Instruction *I = convertBitCastToGEP(CI, Builder, DL))
2679-
return I;
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+
}
26802669
}
26812670

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

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)