@@ -89,40 +89,69 @@ enum BoundsPriority {
8989};
9090
9191class AVarBoundsInfo ;
92-
92+ typedef std::map<ABounds::BoundsKind, std::set<BoundsKey>> BndsKindMap;
9393// The main class that handles figuring out bounds of arr variables.
9494class AvarBoundsInference {
9595public:
96- AvarBoundsInference (AVarBoundsInfo *BoundsInfo) : BI(BoundsInfo) { }
96+ AvarBoundsInference (AVarBoundsInfo *BoundsInfo) : BI(BoundsInfo) {
97+ clearInferredBounds ();
98+ }
99+
100+ // Clear all possible inferred bounds for all the BoundsKeys
101+ void clearInferredBounds () {
102+ CurrIterInferBounds.clear ();
103+ BKsFailedFlowInference.clear ();
104+ }
97105
98106 // Infer bounds for the given key from the set of given ARR atoms.
99107 // The flag FromPB requests the inference to use potential length variables.
100108 bool inferBounds (BoundsKey K, AVarGraph &BKGraph, bool FromPB = false );
109+
110+ // Get a consistent bound for all the arrays whose bounds have been
111+ // inferred.
112+ bool convergeInferredBounds ();
101113private:
102- bool inferPossibleBounds (BoundsKey K, ABounds *SB,
103- AVarGraph &BKGraph,
104- std::set<ABounds *> &EB);
114+ // Find all the reachable variables form FromVarK that are visible
115+ // in DstScope
116+ bool getReachableBoundKeys (const ProgramVarScope *DstScope,
117+ BoundsKey FromVarK,
118+ std::set<BoundsKey> &PotK,
119+ AVarGraph &BKGraph,
120+ bool CheckImmediate = false );
105121
106122 bool intersectBounds (std::set<ProgramVar *> &ProgVars,
107123 ABounds::BoundsKind BK,
108124 std::set<ABounds *> &CurrB);
109125
110- bool getRelevantBounds (std::set<BoundsKey> &RBKeys,
111- std::set<ABounds *> &ResBounds);
126+ // Check if bounds specified by Bnds are declared bounds of K.
127+ bool areDeclaredBounds (BoundsKey K,
128+ const std::pair<ABounds::BoundsKind,
129+ std::set<BoundsKey>> &Bnds);
112130
113- bool predictBounds (BoundsKey K, std::set<BoundsKey> &Neighbours,
114- AVarGraph &BKGraph ,
115- ABounds **KB );
131+ // Get all the bounds of the given array i.e., BK
132+ bool getRelevantBounds (BoundsKey BK ,
133+ BndsKindMap &ResBounds );
116134
135+ // Predict possible bounds for DstArrK from the bounds of Neighbours.
136+ // Return true if there is any change in the captured bounds information.
137+ bool predictBounds (BoundsKey DstArrK, std::set<BoundsKey> &Neighbours,
138+ AVarGraph &BKGraph);
117139
118- void mergeReachableProgramVars (std::set<ProgramVar *> &AllVars);
140+
141+ void mergeReachableProgramVars (std::set<BoundsKey> &AllVars);
119142
120143 AVarBoundsInfo *BI;
144+
145+ // Potential Bounds for each bounds key inferred for the current iteration.
146+ std::map<BoundsKey, BndsKindMap> CurrIterInferBounds;
147+ // BoundsKey that failed the flow inference.
148+ std::set<BoundsKey> BKsFailedFlowInference;
121149};
122150
123151class AVarBoundsInfo {
124152public:
125- AVarBoundsInfo () : ProgVarGraph(this ), CtxSensProgVarGraph(this ) {
153+ AVarBoundsInfo () : ProgVarGraph(this ), CtxSensProgVarGraph(this ),
154+ RevCtxSensProgVarGraph (this ) {
126155 BCount = 1 ;
127156 PVarInfo.clear ();
128157 InProgramArrPtrBoundsKeys.clear ();
@@ -163,6 +192,7 @@ class AVarBoundsInfo {
163192 BoundsKey getVariable (clang::FieldDecl *FD);
164193 BoundsKey getVariable (clang::FunctionDecl *FD);
165194 BoundsKey getConstKey (uint64_t value);
195+ bool fetchAllConstKeys (uint64_t value, std::set<BoundsKey> &AllKeys);
166196
167197 // Generate a random bounds key to be used for inference.
168198 BoundsKey getRandomBKey ();
@@ -227,6 +257,8 @@ class AVarBoundsInfo {
227257 const CVarSet &SrcCVarSet,
228258 bool JsonFormat = false ) const ;
229259
260+ bool areSameProgramVar (BoundsKey B1, BoundsKey B2);
261+
230262private:
231263 friend class AvarBoundsInference ;
232264
@@ -238,8 +270,8 @@ class AVarBoundsInfo {
238270 BoundsKey BCount;
239271 // Map of VarKeys and corresponding program variables.
240272 std::map<BoundsKey, ProgramVar *> PVarInfo;
241- // Map of APSInt (constants) and corresponding VarKeys .
242- std::map<uint64_t , BoundsKey> ConstVarKeys;
273+ // Map of APSInt (constants) and set of BoundKeys that correspond to it .
274+ std::map<uint64_t , std::set< BoundsKey> > ConstVarKeys;
243275 // Map of BoundsKey and corresponding prioritized bounds information.
244276 // Note that although each PSL could have multiple ConstraintKeys Ex: **p.
245277 // Only the outer most pointer can have bounds.
@@ -272,9 +304,11 @@ class AVarBoundsInfo {
272304
273305 // Graph of all program variables.
274306 AVarGraph ProgVarGraph;
275- // Graph that contains only edges between context-sensitive
276- // BoundsKey and corresponding original BoundsKey.
307+ // Graph that contains only edges from normal BoundsKey to
308+ // context-sensitive BoundsKey.
277309 AVarGraph CtxSensProgVarGraph;
310+ // Same as above but in the reverse direction.
311+ AVarGraph RevCtxSensProgVarGraph;
278312 // Stats on techniques used to find length for various variables.
279313 AVarBoundsStats BoundsInferStats;
280314 // This is the map of pointer variable bounds key and set of bounds key
@@ -305,19 +339,22 @@ class AVarBoundsInfo {
305339 // Check if the provided bounds key corresponds to function return.
306340 bool isFunctionReturn (BoundsKey BK);
307341
308- // Of all teh pointer bounds key, find arr pointers.
342+ // Of all the pointer bounds key, find arr pointers.
309343 void computerArrPointers (ProgramInfo *PI, std::set<BoundsKey> &Ret);
310344
345+ // Get all the array pointers that need bounds.
346+ void getBoundsNeededArrPointers (const std::set<BoundsKey> &ArrPtrs,
347+ std::set<BoundsKey> &AB);
348+
311349 // Keep only highest priority bounds for all the provided BoundsKeys
312350 // returns true if any thing changed, else false.
313351 bool keepHighestPriorityBounds (std::set<BoundsKey> &ArrPtrs);
314352
315353 // Perform worklist based inference on the requested array variables using
316- // the provided graph.
317- // The flag FromPB requests the algorithm to use potential length variables.
318- bool performWorkListInference (std::set<BoundsKey> &ArrNeededBounds,
354+ // the provided graph and potential length variables.
355+ bool performWorkListInference (const std::set<BoundsKey> &ArrNeededBounds,
319356 AVarGraph &BKGraph,
320- bool FromPB = false );
357+ AvarBoundsInference &BI );
321358
322359 void insertParamKey (ParamDeclType ParamDecl, BoundsKey NK);
323360};
0 commit comments