@@ -31,6 +31,40 @@ static bool isAligned(const Value *Base, Align Alignment,
31
31
return Base->getPointerAlignment (DL) >= Alignment;
32
32
}
33
33
34
+ static bool isDereferenceableAndAlignedPointerViaAssumption (
35
+ const Value *Ptr, Align Alignment,
36
+ function_ref<bool (const RetainedKnowledge &RK)> CheckSize,
37
+ const DataLayout &DL, const Instruction *CtxI, AssumptionCache *AC,
38
+ const DominatorTree *DT) {
39
+ // Dereferenceable information from assumptions is only valid if the value
40
+ // cannot be freed between the assumption and use. For now just use the
41
+ // information for values that cannot be freed in the function.
42
+ // TODO: More precisely check if the pointer can be freed between assumption
43
+ // and use.
44
+ if (!CtxI || Ptr->canBeFreed ())
45
+ return false ;
46
+ // / Look through assumes to see if both dereferencability and alignment can
47
+ // / be proven by an assume if needed.
48
+ RetainedKnowledge AlignRK;
49
+ RetainedKnowledge DerefRK;
50
+ bool IsAligned = Ptr->getPointerAlignment (DL) >= Alignment;
51
+ return getKnowledgeForValue (
52
+ Ptr, {Attribute::Dereferenceable, Attribute::Alignment}, *AC,
53
+ [&](RetainedKnowledge RK, Instruction *Assume, auto ) {
54
+ if (!isValidAssumeForContext (Assume, CtxI, DT))
55
+ return false ;
56
+ if (RK.AttrKind == Attribute::Alignment)
57
+ AlignRK = std::max (AlignRK, RK);
58
+ if (RK.AttrKind == Attribute::Dereferenceable)
59
+ DerefRK = std::max (DerefRK, RK);
60
+ IsAligned |= AlignRK && AlignRK.ArgValue >= Alignment.value ();
61
+ if (IsAligned && DerefRK && CheckSize (DerefRK))
62
+ return true ; // We have found what we needed so we stop looking
63
+ return false ; // Other assumes may have better information. so
64
+ // keep looking
65
+ });
66
+ }
67
+
34
68
// / Test if V is always a pointer to allocated and suitably aligned memory for
35
69
// / a simple load or store.
36
70
static bool isDereferenceableAndAlignedPointer (
@@ -169,38 +203,12 @@ static bool isDereferenceableAndAlignedPointer(
169
203
Size, DL, CtxI, AC, DT, TLI,
170
204
Visited, MaxDepth);
171
205
172
- // Dereferenceable information from assumptions is only valid if the value
173
- // cannot be freed between the assumption and use. For now just use the
174
- // information for values that cannot be freed in the function.
175
- // TODO: More precisely check if the pointer can be freed between assumption
176
- // and use.
177
- if (CtxI && AC && !V->canBeFreed ()) {
178
- // / Look through assumes to see if both dereferencability and alignment can
179
- // / be proven by an assume if needed.
180
- RetainedKnowledge AlignRK;
181
- RetainedKnowledge DerefRK;
182
- bool IsAligned = V->getPointerAlignment (DL) >= Alignment;
183
- if (getKnowledgeForValue (
184
- V, {Attribute::Dereferenceable, Attribute::Alignment}, *AC,
185
- [&](RetainedKnowledge RK, Instruction *Assume, auto ) {
186
- if (!isValidAssumeForContext (Assume, CtxI, DT))
187
- return false ;
188
- if (RK.AttrKind == Attribute::Alignment)
189
- AlignRK = std::max (AlignRK, RK);
190
- if (RK.AttrKind == Attribute::Dereferenceable)
191
- DerefRK = std::max (DerefRK, RK);
192
- IsAligned |= AlignRK && AlignRK.ArgValue >= Alignment.value ();
193
- if (IsAligned && DerefRK &&
194
- DerefRK.ArgValue >= Size.getZExtValue ())
195
- return true ; // We have found what we needed so we stop looking
196
- return false ; // Other assumes may have better information. so
197
- // keep looking
198
- }))
199
- return true ;
200
- }
201
-
202
- // If we don't know, assume the worst.
203
- return false ;
206
+ return AC && isDereferenceableAndAlignedPointerViaAssumption (
207
+ V, Alignment,
208
+ [Size](const RetainedKnowledge &RK) {
209
+ return RK.ArgValue >= Size.getZExtValue ();
210
+ },
211
+ DL, CtxI, AC, DT);
204
212
}
205
213
206
214
bool llvm::isDereferenceableAndAlignedPointer (
@@ -317,8 +325,8 @@ bool llvm::isDereferenceableAndAlignedInLoop(
317
325
return false ;
318
326
319
327
const SCEV *MaxBECount =
320
- Predicates ? SE.getPredicatedConstantMaxBackedgeTakenCount (L, *Predicates)
321
- : SE.getConstantMaxBackedgeTakenCount (L);
328
+ Predicates ? SE.getPredicatedSymbolicMaxBackedgeTakenCount (L, *Predicates)
329
+ : SE.getSymbolicMaxBackedgeTakenCount (L);
322
330
const SCEV *BECount = Predicates
323
331
? SE.getPredicatedBackedgeTakenCount (L, *Predicates)
324
332
: SE.getBackedgeTakenCount (L);
@@ -339,9 +347,11 @@ bool llvm::isDereferenceableAndAlignedInLoop(
339
347
340
348
Value *Base = nullptr ;
341
349
APInt AccessSize;
350
+ const SCEV *AccessSizeSCEV = nullptr ;
342
351
if (const SCEVUnknown *NewBase = dyn_cast<SCEVUnknown>(AccessStart)) {
343
352
Base = NewBase->getValue ();
344
353
AccessSize = MaxPtrDiff;
354
+ AccessSizeSCEV = PtrDiff;
345
355
} else if (auto *MinAdd = dyn_cast<SCEVAddExpr>(AccessStart)) {
346
356
if (MinAdd->getNumOperands () != 2 )
347
357
return false ;
@@ -365,12 +375,20 @@ bool llvm::isDereferenceableAndAlignedInLoop(
365
375
return false ;
366
376
367
377
AccessSize = MaxPtrDiff + Offset->getAPInt ();
378
+ AccessSizeSCEV = SE.getAddExpr (PtrDiff, Offset);
368
379
Base = NewBase->getValue ();
369
380
} else
370
381
return false ;
371
382
372
383
Instruction *HeaderFirstNonPHI = &*L->getHeader ()->getFirstNonPHIIt ();
373
- return isDereferenceableAndAlignedPointer (Base, Alignment, AccessSize, DL,
384
+ return isDereferenceableAndAlignedPointerViaAssumption (
385
+ Base, Alignment,
386
+ [&SE, AccessSizeSCEV](const RetainedKnowledge &RK) {
387
+ return SE.isKnownPredicate (CmpInst::ICMP_ULE, AccessSizeSCEV,
388
+ SE.getSCEV (RK.IRArgValue ));
389
+ },
390
+ DL, HeaderFirstNonPHI, AC, &DT) ||
391
+ isDereferenceableAndAlignedPointer (Base, Alignment, AccessSize, DL,
374
392
HeaderFirstNonPHI, AC, &DT);
375
393
}
376
394
0 commit comments