@@ -502,7 +502,7 @@ static Value *evaluateGEPOffsetExpression(User *GEP, InstCombinerImpl &IC,
502
502
// / Returns true if we can rewrite Start as a GEP with pointer Base
503
503
// / and some integer offset. The nodes that need to be re-written
504
504
// / for this transformation will be added to Explored.
505
- static bool canRewriteGEPAsOffset (Value *Start, Value *Base,
505
+ static bool canRewriteGEPAsOffset (Type *ElemTy, Value *Start, Value *Base,
506
506
const DataLayout &DL,
507
507
SetVector<Value *> &Explored) {
508
508
SmallVector<Value *, 16 > WorkList (1 , Start);
@@ -550,7 +550,7 @@ static bool canRewriteGEPAsOffset(Value *Start, Value *Base,
550
550
// the original pointer type. We could handle more cases in the
551
551
// future.
552
552
if (GEP->getNumIndices () != 1 || !GEP->isInBounds () ||
553
- GEP->getType () != Start-> getType () )
553
+ GEP->getSourceElementType () != ElemTy )
554
554
return false ;
555
555
556
556
if (Explored.count (GEP->getOperand (0 )) == 0 )
@@ -626,7 +626,7 @@ static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
626
626
627
627
// / Returns a re-written value of Start as an indexed GEP using Base as a
628
628
// / pointer.
629
- static Value *rewriteGEPAsOffset (Value *Start, Value *Base,
629
+ static Value *rewriteGEPAsOffset (Type *ElemTy, Value *Start, Value *Base,
630
630
const DataLayout &DL,
631
631
SetVector<Value *> &Explored) {
632
632
// Perform all the substitutions. This is a bit tricky because we can
@@ -728,8 +728,7 @@ static Value *rewriteGEPAsOffset(Value *Start, Value *Base,
728
728
Start->getName () + " to.ptr" );
729
729
730
730
Value *GEP = Builder.CreateInBoundsGEP (
731
- Start->getType ()->getPointerElementType (), NewBase,
732
- makeArrayRef (NewInsts[Val]), Val->getName () + " .ptr" );
731
+ ElemTy, NewBase, makeArrayRef (NewInsts[Val]), Val->getName () + " .ptr" );
733
732
734
733
if (!Val->getType ()->isPointerTy ()) {
735
734
Value *Cast = Builder.CreatePointerCast (GEP, Val->getType (),
@@ -746,7 +745,7 @@ static Value *rewriteGEPAsOffset(Value *Start, Value *Base,
746
745
// / the input Value as a constant indexed GEP. Returns a pair containing
747
746
// / the GEPs Pointer and Index.
748
747
static std::pair<Value *, Value *>
749
- getAsConstantIndexedAddress (Value *V, const DataLayout &DL) {
748
+ getAsConstantIndexedAddress (Type *ElemTy, Value *V, const DataLayout &DL) {
750
749
Type *IndexType = IntegerType::get (V->getContext (),
751
750
DL.getIndexTypeSizeInBits (V->getType ()));
752
751
@@ -758,7 +757,7 @@ getAsConstantIndexedAddress(Value *V, const DataLayout &DL) {
758
757
if (!GEP->isInBounds ())
759
758
break ;
760
759
if (GEP->hasAllConstantIndices () && GEP->getNumIndices () == 1 &&
761
- GEP->getType () == V-> getType () ) {
760
+ GEP->getSourceElementType () == ElemTy ) {
762
761
V = GEP->getOperand (0 );
763
762
Constant *GEPIndex = static_cast <Constant *>(GEP->getOperand (1 ));
764
763
Index = ConstantExpr::getAdd (
@@ -797,17 +796,14 @@ static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,
797
796
if (!GEPLHS->hasAllConstantIndices ())
798
797
return nullptr ;
799
798
800
- // Make sure the pointers have the same type.
801
- if (GEPLHS->getType () != RHS->getType ())
802
- return nullptr ;
803
-
799
+ Type *ElemTy = GEPLHS->getSourceElementType ();
804
800
Value *PtrBase, *Index;
805
- std::tie (PtrBase, Index) = getAsConstantIndexedAddress (GEPLHS, DL);
801
+ std::tie (PtrBase, Index) = getAsConstantIndexedAddress (ElemTy, GEPLHS, DL);
806
802
807
803
// The set of nodes that will take part in this transformation.
808
804
SetVector<Value *> Nodes;
809
805
810
- if (!canRewriteGEPAsOffset (RHS, PtrBase, DL, Nodes))
806
+ if (!canRewriteGEPAsOffset (ElemTy, RHS, PtrBase, DL, Nodes))
811
807
return nullptr ;
812
808
813
809
// We know we can re-write this as
@@ -816,7 +812,7 @@ static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,
816
812
// can't have overflow on either side. We can therefore re-write
817
813
// this as:
818
814
// OFFSET1 cmp OFFSET2
819
- Value *NewRHS = rewriteGEPAsOffset (RHS, PtrBase, DL, Nodes);
815
+ Value *NewRHS = rewriteGEPAsOffset (ElemTy, RHS, PtrBase, DL, Nodes);
820
816
821
817
// RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
822
818
// GEP having PtrBase as the pointer base, and has returned in NewRHS the
0 commit comments