@@ -193,67 +193,74 @@ static SmallVector<DecompEntry, 4>
193
193
decomposeGEP (GetElementPtrInst &GEP,
194
194
SmallVector<PreconditionTy, 4 > &Preconditions, bool IsSigned,
195
195
const DataLayout &DL) {
196
+ // Do not reason about pointers where the index size is larger than 64 bits,
197
+ // as the coefficients used to encode constraints are 64 bit integers.
198
+ unsigned AS =
199
+ cast<PointerType>(GEP.getPointerOperand ()->getType ())->getAddressSpace ();
200
+ if (DL.getIndexSizeInBits (AS) > 64 )
201
+ return {};
202
+
196
203
auto GTI = gep_type_begin (GEP);
197
204
if (GEP.getNumOperands () != 2 || !GEP.isInBounds () ||
198
205
isa<ScalableVectorType>(GTI.getIndexedType ()))
199
206
return {{0 , nullptr }, {1 , &GEP}};
200
207
201
208
int64_t Scale = static_cast <int64_t >(
202
209
DL.getTypeAllocSize (GTI.getIndexedType ()).getFixedSize ());
203
- int64_t MulRes;
204
210
// Handle the (gep (gep ....), C) case by incrementing the constant
205
211
// coefficient of the inner GEP, if C is a constant.
206
212
auto *InnerGEP = dyn_cast<GetElementPtrInst>(GEP.getPointerOperand ());
207
213
if (InnerGEP && InnerGEP->getNumOperands () == 2 &&
208
214
isa<ConstantInt>(GEP.getOperand (1 ))) {
209
215
APInt Offset = cast<ConstantInt>(GEP.getOperand (1 ))->getValue ();
210
216
auto Result = decompose (InnerGEP, Preconditions, IsSigned, DL);
211
- if (!MulOverflow (Scale, Offset.getSExtValue (), MulRes)) {
212
- Result[0 ].Coefficient += MulRes;
213
- if (Offset.isNegative ()) {
214
- // Add pre-condition ensuring the GEP is increasing monotonically and
215
- // can be de-composed.
216
- Preconditions.emplace_back (
217
- CmpInst::ICMP_SGE, InnerGEP->getOperand (1 ),
218
- ConstantInt::get (InnerGEP->getOperand (1 )->getType (),
219
- -1 * Offset.getSExtValue ()));
220
- }
221
- return Result;
217
+ Result[0 ].Coefficient += Scale * Offset.getSExtValue ();
218
+ if (Offset.isNegative ()) {
219
+ // Add pre-condition ensuring the GEP is increasing monotonically and
220
+ // can be de-composed.
221
+ Preconditions.emplace_back (
222
+ CmpInst::ICMP_SGE, InnerGEP->getOperand (1 ),
223
+ ConstantInt::get (InnerGEP->getOperand (1 )->getType (),
224
+ -1 * Offset.getSExtValue ()));
222
225
}
226
+ return Result;
223
227
}
224
228
225
229
Value *Op0, *Op1;
226
230
ConstantInt *CI;
227
231
// If the index is zero-extended, it is guaranteed to be positive.
228
232
if (match (GEP.getOperand (GEP.getNumOperands () - 1 ), m_ZExt (m_Value (Op0)))) {
229
- if (match (Op0, m_NUWShl (m_Value (Op1), m_ConstantInt (CI))) &&
230
- canUseSExt (CI) &&
231
- !MulOverflow (Scale, int64_t (std::pow (int64_t (2 ), CI->getSExtValue ())),
232
- MulRes))
233
- return {{0 , nullptr }, {1 , GEP.getPointerOperand ()}, {MulRes, Op1}};
233
+ if (match (Op0, m_NUWShl (m_Value (Op1), m_ConstantInt (CI))) && canUseSExt (CI))
234
+ return {{0 , nullptr },
235
+ {1 , GEP.getPointerOperand ()},
236
+ {Scale * int64_t (std::pow (int64_t (2 ), CI->getSExtValue ())), Op1}};
234
237
if (match (Op0, m_NSWAdd (m_Value (Op1), m_ConstantInt (CI))) &&
235
- canUseSExt (CI) && match (Op0, m_NUWAdd (m_Value (), m_Value ())) &&
236
- !MulOverflow (Scale, CI->getSExtValue (), MulRes))
237
- return {{MulRes, nullptr }, {1 , GEP.getPointerOperand ()}, {Scale, Op1}};
238
+ canUseSExt (CI) && match (Op0, m_NUWAdd (m_Value (), m_Value ())))
239
+ return {{Scale * CI->getSExtValue (), nullptr },
240
+ {1 , GEP.getPointerOperand ()},
241
+ {Scale, Op1}};
242
+
238
243
return {{0 , nullptr }, {1 , GEP.getPointerOperand ()}, {Scale, Op0, true }};
239
244
}
240
245
241
246
if (match (GEP.getOperand (GEP.getNumOperands () - 1 ), m_ConstantInt (CI)) &&
242
- !CI->isNegative () && canUseSExt (CI) &&
243
- ! MulOverflow ( Scale, CI->getSExtValue (), MulRes))
244
- return {{MulRes, nullptr }, {1 , GEP.getPointerOperand ()}};
247
+ !CI->isNegative () && canUseSExt (CI))
248
+ return {{ Scale * CI->getSExtValue (), nullptr },
249
+ {1 , GEP.getPointerOperand ()}};
245
250
246
251
SmallVector<DecompEntry, 4 > Result;
247
252
if (match (GEP.getOperand (GEP.getNumOperands () - 1 ),
248
253
m_NSWShl (m_Value (Op0), m_ConstantInt (CI))) &&
249
- canUseSExt (CI) &&
250
- ! MulOverflow (Scale, int64_t ( std::pow ( int64_t ( 2 ), CI-> getSExtValue ())) ,
251
- MulRes))
252
- Result = {{ 0 , nullptr }, { 1 , GEP. getPointerOperand ()}, {MulRes , Op0}};
254
+ canUseSExt (CI))
255
+ Result = {{ 0 , nullptr } ,
256
+ { 1 , GEP. getPointerOperand ()},
257
+ {Scale * int64_t ( std::pow ( int64_t ( 2 ), CI-> getSExtValue ())) , Op0}};
253
258
else if (match (GEP.getOperand (GEP.getNumOperands () - 1 ),
254
259
m_NSWAdd (m_Value (Op0), m_ConstantInt (CI))) &&
255
- canUseSExt (CI) && !MulOverflow (Scale, CI->getSExtValue (), MulRes))
256
- Result = {{MulRes, nullptr }, {1 , GEP.getPointerOperand ()}, {Scale, Op0}};
260
+ canUseSExt (CI))
261
+ Result = {{Scale * CI->getSExtValue (), nullptr },
262
+ {1 , GEP.getPointerOperand ()},
263
+ {Scale, Op0}};
257
264
else {
258
265
Op0 = GEP.getOperand (GEP.getNumOperands () - 1 );
259
266
Result = {{0 , nullptr }, {1 , GEP.getPointerOperand ()}, {Scale, Op0}};
0 commit comments