@@ -353,28 +353,14 @@ bool AvarBoundsInference::getReachableBoundKeys(const ProgramVarScope *DstScope,
353
353
void AvarBoundsInference::getRelevantBounds (BoundsKey BK,
354
354
BndsKindMap &ResBounds) {
355
355
// Try to get the bounds of all RBKeys.
356
- // If this pointer is used in pointer arithmetic then there
357
- // are no relevant bounds for this pointer.
358
- // if (!BI->hasPointerArithmetic(BK)) {
359
- if (CurrIterInferBounds.find (BK) != CurrIterInferBounds.end ()) {
360
- // get the bounds inferred from the current iteration
361
- ResBounds = CurrIterInferBounds[BK];
362
- } else if (ABounds *PrevBounds = BI->getBounds (BK)) {
363
- ResBounds[PrevBounds->getKind ()].insert (PrevBounds->getBKey ());
364
- }
365
- // }
356
+ if (CurrIterInferBounds.find (BK) != CurrIterInferBounds.end ()) {
357
+ // get the bounds inferred from the current iteration
358
+ ResBounds = CurrIterInferBounds[BK];
359
+ } else if (ABounds *PrevBounds = BI->getBounds (BK)) {
360
+ ResBounds[PrevBounds->getKind ()].insert (PrevBounds->getBKey ());
361
+ }
362
+
366
363
367
- // A pointer that is used in pointer arithmetic can have relevant bounds, but
368
- // it is not permitted to be a bound for another pointer. This avoids
369
- // inferring bounds such as
370
- // _Array_ptr<int> a : count(1) = 0;
371
- // _Array_ptr<int> b : bounds(a, a + 1) = a;
372
- // b++;
373
- // _Array_ptr<int> c : count(1) = b;
374
- // The bounds from `a` should not propagate through `b` to `c` because the
375
- // pointer arithmetic on `b` potentially invalidates the bound. For future
376
- // work, all bounds "down stream" of pointer arithmetic could also use range
377
- // bounds, i.e., `c` could have `bounds(a, a + 1)` as well.
378
364
// for (auto &E : ResBounds) {
379
365
// auto Iter = E.second.begin();
380
366
// while (Iter != E.second.end()) {
@@ -972,6 +958,18 @@ bool AVarBoundsInfo::handleAssignment(clang::Decl *L, CVarOption LCVars,
972
958
}
973
959
974
960
bool AVarBoundsInfo::addAssignment (BoundsKey L, BoundsKey R) {
961
+ // Before adding an edge from L to R or R to L, first verify that the source
962
+ // BoundsKey for the edge is not computed by pointer arithmetic. The pointer
963
+ // arithmetic invalidates the bounds on the pointer, so bounds should not
964
+ // propagate through it. The bounds are allowed to exist on the pointer
965
+ // because they will emitted as range bounds based on a different base
966
+ // pointer. For future work, all bounds "down stream" of pointer arithmetic
967
+ // could also use range bounds using the same base pointer.
968
+ auto AddEdgeUnlessPointerArithmetic = [this ](BoundsKey From, BoundsKey To) {
969
+ if (!hasPointerArithmetic (From))
970
+ ProgVarGraph.addUniqueEdge (From, To);
971
+ };
972
+
975
973
// If we are adding to function return, do not add bi-directional edges.
976
974
if (isFunctionReturn (L) || isFunctionReturn (R)) {
977
975
// Do not assign edge from return to itself.
@@ -980,14 +978,13 @@ bool AVarBoundsInfo::addAssignment(BoundsKey L, BoundsKey R) {
980
978
// So, if we create a edge from return to itself then we create a cyclic
981
979
// dependency and never will be able to find the bounds for the return
982
980
// value.
983
- if (L != R && ! hasPointerArithmetic (R) )
984
- ProgVarGraph. addUniqueEdge (R, L);
981
+ if (L != R)
982
+ AddEdgeUnlessPointerArithmetic (R, L);
985
983
} else {
986
- if (!hasPointerArithmetic (R))
987
- ProgVarGraph.addUniqueEdge (R, L);
984
+ AddEdgeUnlessPointerArithmetic (R, L);
988
985
ProgramVar *PV = getProgramVar (R);
989
- if (!(PV && PV->isNumConstant () && ! hasPointerArithmetic (L) ))
990
- ProgVarGraph. addUniqueEdge (L, R);
986
+ if (!(PV && PV->isNumConstant ()))
987
+ AddEdgeUnlessPointerArithmetic (L, R);
991
988
}
992
989
return true ;
993
990
}
0 commit comments