@@ -1945,8 +1945,13 @@ static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds,
19451945
19461946 Type *CommonTy =
19471947 Type::getIntNTy (LastIdxTy->getContext (), CommonExtendedWidth);
1948- Idx0 = ConstantExpr::getSExtOrBitCast (Idx0, CommonTy);
1949- LastIdx = ConstantExpr::getSExtOrBitCast (LastIdx, CommonTy);
1948+ if (Idx0->getType () != CommonTy)
1949+ Idx0 = ConstantFoldCastInstruction (Instruction::SExt, Idx0, CommonTy);
1950+ if (LastIdx->getType () != CommonTy)
1951+ LastIdx =
1952+ ConstantFoldCastInstruction (Instruction::SExt, LastIdx, CommonTy);
1953+ if (!Idx0 || !LastIdx)
1954+ return nullptr ;
19501955 }
19511956
19521957 NewIndices.push_back (ConstantExpr::get (Instruction::Add, Idx0, LastIdx));
@@ -2164,11 +2169,13 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
21642169 : cast<FixedVectorType>(CurrIdx->getType ())->getNumElements ());
21652170
21662171 if (!PrevIdx->getType ()->isIntOrIntVectorTy (CommonExtendedWidth))
2167- PrevIdx = ConstantExpr::getSExt (PrevIdx, ExtendedTy);
2172+ PrevIdx =
2173+ ConstantFoldCastInstruction (Instruction::SExt, PrevIdx, ExtendedTy);
21682174
21692175 if (!Div->getType ()->isIntOrIntVectorTy (CommonExtendedWidth))
2170- Div = ConstantExpr::getSExt ( Div, ExtendedTy);
2176+ Div = ConstantFoldCastInstruction (Instruction::SExt, Div, ExtendedTy);
21712177
2178+ assert (PrevIdx && Div && " Should have folded" );
21722179 NewIdxs[i - 1 ] = ConstantExpr::getAdd (PrevIdx, Div);
21732180 }
21742181
0 commit comments