Skip to content

Commit c3241ee

Browse files
Cleanup
1 parent 9636f01 commit c3241ee

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

clang/lib/3C/AVarBoundsInfo.cpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -353,28 +353,14 @@ bool AvarBoundsInference::getReachableBoundKeys(const ProgramVarScope *DstScope,
353353
void AvarBoundsInference::getRelevantBounds(BoundsKey BK,
354354
BndsKindMap &ResBounds) {
355355
// 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+
366363

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.
378364
//for (auto &E : ResBounds) {
379365
// auto Iter = E.second.begin();
380366
// while (Iter != E.second.end()) {
@@ -972,6 +958,18 @@ bool AVarBoundsInfo::handleAssignment(clang::Decl *L, CVarOption LCVars,
972958
}
973959

974960
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+
975973
// If we are adding to function return, do not add bi-directional edges.
976974
if (isFunctionReturn(L) || isFunctionReturn(R)) {
977975
// Do not assign edge from return to itself.
@@ -980,14 +978,13 @@ bool AVarBoundsInfo::addAssignment(BoundsKey L, BoundsKey R) {
980978
// So, if we create a edge from return to itself then we create a cyclic
981979
// dependency and never will be able to find the bounds for the return
982980
// value.
983-
if (L != R && !hasPointerArithmetic(R))
984-
ProgVarGraph.addUniqueEdge(R, L);
981+
if (L != R)
982+
AddEdgeUnlessPointerArithmetic(R, L);
985983
} else {
986-
if (!hasPointerArithmetic(R))
987-
ProgVarGraph.addUniqueEdge(R, L);
984+
AddEdgeUnlessPointerArithmetic(R, L);
988985
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);
991988
}
992989
return true;
993990
}

0 commit comments

Comments
 (0)