@@ -2603,57 +2603,6 @@ Instruction *InstCombinerImpl::optimizeBitCastFromPhi(CastInst &CI,
2603
2603
return RetVal;
2604
2604
}
2605
2605
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
-
2657
2606
Instruction *InstCombinerImpl::visitBitCast (BitCastInst &CI) {
2658
2607
// If the operands are integer typed then apply the integer transforms,
2659
2608
// otherwise just apply the common ones.
@@ -2667,6 +2616,11 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
2667
2616
return replaceInstUsesWith (CI, Src);
2668
2617
2669
2618
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
+
2670
2624
// If we are casting a alloca to a pointer to a type of the same
2671
2625
// size, rewrite the allocation instruction to allocate the "right" type.
2672
2626
// There is no need to modify malloc calls because it is their bitcast that
@@ -2675,8 +2629,43 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
2675
2629
if (Instruction *V = PromoteCastOfAllocation (CI, *AI))
2676
2630
return V;
2677
2631
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
+ }
2680
2669
}
2681
2670
2682
2671
if (FixedVectorType *DestVTy = dyn_cast<FixedVectorType>(DestTy)) {
0 commit comments