@@ -1945,8 +1945,13 @@ static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds,
1945
1945
1946
1946
Type *CommonTy =
1947
1947
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 ;
1950
1955
}
1951
1956
1952
1957
NewIndices.push_back (ConstantExpr::get (Instruction::Add, Idx0, LastIdx));
@@ -2164,11 +2169,13 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
2164
2169
: cast<FixedVectorType>(CurrIdx->getType ())->getNumElements ());
2165
2170
2166
2171
if (!PrevIdx->getType ()->isIntOrIntVectorTy (CommonExtendedWidth))
2167
- PrevIdx = ConstantExpr::getSExt (PrevIdx, ExtendedTy);
2172
+ PrevIdx =
2173
+ ConstantFoldCastInstruction (Instruction::SExt, PrevIdx, ExtendedTy);
2168
2174
2169
2175
if (!Div->getType ()->isIntOrIntVectorTy (CommonExtendedWidth))
2170
- Div = ConstantExpr::getSExt ( Div, ExtendedTy);
2176
+ Div = ConstantFoldCastInstruction (Instruction::SExt, Div, ExtendedTy);
2171
2177
2178
+ assert (PrevIdx && Div && " Should have folded" );
2172
2179
NewIdxs[i - 1 ] = ConstantExpr::getAdd (PrevIdx, Div);
2173
2180
}
2174
2181
0 commit comments