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