Skip to content

Commit e4be53a

Browse files
committed
Revert "Merge pull request #282 from correctcomputation/iss281"
This reverts commit c554433, reversing changes made to 0f8d6dc.
1 parent c554433 commit e4be53a

15 files changed

+321
-718
lines changed

clang/include/clang/CConv/ABounds.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ class ABounds {
4444
virtual ~ABounds() { }
4545

4646
virtual std::string mkString(AVarBoundsInfo *) = 0;
47-
virtual bool areSame(ABounds *, AVarBoundsInfo *) = 0;
47+
virtual bool areSame(ABounds *) = 0;
4848
virtual BoundsKey getBKey() = 0;
4949
virtual ABounds* makeCopy(BoundsKey NK) = 0;
5050

5151
// Set that maintains all the bound keys that are used inin
52-
// TODO: Is this still needed?
5352
static std::set<BoundsKey> KeysUsedInBounds;
53+
5454
static bool isKeyUsedInBounds(BoundsKey ToCheck);
5555

5656
static ABounds *getBoundsInfo(AVarBoundsInfo *AVBInfo,
@@ -66,7 +66,7 @@ class CountBound : public ABounds {
6666
virtual ~CountBound() { }
6767

6868
std::string mkString(AVarBoundsInfo *ABI) override ;
69-
bool areSame(ABounds *O, AVarBoundsInfo *ABI) override;
69+
bool areSame(ABounds *O) override;
7070
BoundsKey getBKey() override;
7171
ABounds* makeCopy(BoundsKey NK) override;
7272

@@ -88,7 +88,7 @@ class ByteBound : public ABounds {
8888
virtual ~ByteBound() { }
8989

9090
std::string mkString(AVarBoundsInfo *ABI) override ;
91-
bool areSame(ABounds *O, AVarBoundsInfo *ABI) override;
91+
bool areSame(ABounds *O) override;
9292
BoundsKey getBKey() override;
9393
ABounds* makeCopy(BoundsKey NK) override;
9494

@@ -111,7 +111,7 @@ class RangeBound : public ABounds {
111111
virtual ~RangeBound() { }
112112

113113
std::string mkString(AVarBoundsInfo *ABI) override ;
114-
bool areSame(ABounds *O, AVarBoundsInfo *ABI) override;
114+
bool areSame(ABounds *O) override;
115115

116116
BoundsKey getBKey() override {
117117
assert (false && "Not implemented.");

clang/include/clang/CConv/AVarBoundsInfo.h

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -89,69 +89,40 @@ enum BoundsPriority {
8989
};
9090

9191
class AVarBoundsInfo;
92-
typedef std::map<ABounds::BoundsKind, std::set<BoundsKey>> BndsKindMap;
92+
9393
// The main class that handles figuring out bounds of arr variables.
9494
class AvarBoundsInference {
9595
public:
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-
}
96+
AvarBoundsInference(AVarBoundsInfo *BoundsInfo) : BI(BoundsInfo) { }
10597

10698
// Infer bounds for the given key from the set of given ARR atoms.
10799
// The flag FromPB requests the inference to use potential length variables.
108100
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();
113101
private:
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);
102+
bool inferPossibleBounds(BoundsKey K, ABounds *SB,
103+
AVarGraph &BKGraph,
104+
std::set<ABounds *> &EB);
121105

122106
bool intersectBounds(std::set<ProgramVar *> &ProgVars,
123107
ABounds::BoundsKind BK,
124108
std::set<ABounds *> &CurrB);
125109

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);
110+
bool getRelevantBounds(std::set<BoundsKey> &RBKeys,
111+
std::set<ABounds *> &ResBounds);
130112

131-
// Get all the bounds of the given array i.e., BK
132-
bool getRelevantBounds(BoundsKey BK,
133-
BndsKindMap &ResBounds);
113+
bool predictBounds(BoundsKey K, std::set<BoundsKey> &Neighbours,
114+
AVarGraph &BKGraph,
115+
ABounds **KB);
134116

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);
139117

140-
141-
void mergeReachableProgramVars(std::set<BoundsKey> &AllVars);
118+
void mergeReachableProgramVars(std::set<ProgramVar *> &AllVars);
142119

143120
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;
149121
};
150122

151123
class AVarBoundsInfo {
152124
public:
153-
AVarBoundsInfo() : ProgVarGraph(this), CtxSensProgVarGraph(this),
154-
RevCtxSensProgVarGraph(this) {
125+
AVarBoundsInfo() : ProgVarGraph(this), CtxSensProgVarGraph(this) {
155126
BCount = 1;
156127
PVarInfo.clear();
157128
InProgramArrPtrBoundsKeys.clear();
@@ -192,7 +163,6 @@ class AVarBoundsInfo {
192163
BoundsKey getVariable(clang::FieldDecl *FD);
193164
BoundsKey getVariable(clang::FunctionDecl *FD);
194165
BoundsKey getConstKey(uint64_t value);
195-
bool fetchAllConstKeys(uint64_t value, std::set<BoundsKey> &AllKeys);
196166

197167
// Generate a random bounds key to be used for inference.
198168
BoundsKey getRandomBKey();
@@ -257,8 +227,6 @@ class AVarBoundsInfo {
257227
const CVarSet &SrcCVarSet,
258228
bool JsonFormat = false) const;
259229

260-
bool areSameProgramVar(BoundsKey B1, BoundsKey B2);
261-
262230
private:
263231
friend class AvarBoundsInference;
264232

@@ -270,8 +238,8 @@ class AVarBoundsInfo {
270238
BoundsKey BCount;
271239
// Map of VarKeys and corresponding program variables.
272240
std::map<BoundsKey, ProgramVar *> PVarInfo;
273-
// Map of APSInt (constants) and set of BoundKeys that correspond to it.
274-
std::map<uint64_t, std::set<BoundsKey>> ConstVarKeys;
241+
// Map of APSInt (constants) and corresponding VarKeys.
242+
std::map<uint64_t, BoundsKey> ConstVarKeys;
275243
// Map of BoundsKey and corresponding prioritized bounds information.
276244
// Note that although each PSL could have multiple ConstraintKeys Ex: **p.
277245
// Only the outer most pointer can have bounds.
@@ -304,11 +272,9 @@ class AVarBoundsInfo {
304272

305273
// Graph of all program variables.
306274
AVarGraph ProgVarGraph;
307-
// Graph that contains only edges from normal BoundsKey to
308-
// context-sensitive BoundsKey.
275+
// Graph that contains only edges between context-sensitive
276+
// BoundsKey and corresponding original BoundsKey.
309277
AVarGraph CtxSensProgVarGraph;
310-
// Same as above but in the reverse direction.
311-
AVarGraph RevCtxSensProgVarGraph;
312278
// Stats on techniques used to find length for various variables.
313279
AVarBoundsStats BoundsInferStats;
314280
// This is the map of pointer variable bounds key and set of bounds key
@@ -339,22 +305,19 @@ class AVarBoundsInfo {
339305
// Check if the provided bounds key corresponds to function return.
340306
bool isFunctionReturn(BoundsKey BK);
341307

342-
// Of all the pointer bounds key, find arr pointers.
308+
// Of all teh pointer bounds key, find arr pointers.
343309
void computerArrPointers(ProgramInfo *PI, std::set<BoundsKey> &Ret);
344310

345-
// Get all the array pointers that need bounds.
346-
void getBoundsNeededArrPointers(const std::set<BoundsKey> &ArrPtrs,
347-
std::set<BoundsKey> &AB);
348-
349311
// Keep only highest priority bounds for all the provided BoundsKeys
350312
// returns true if any thing changed, else false.
351313
bool keepHighestPriorityBounds(std::set<BoundsKey> &ArrPtrs);
352314

353315
// Perform worklist based inference on the requested array variables using
354-
// the provided graph and potential length variables.
355-
bool performWorkListInference(const std::set<BoundsKey> &ArrNeededBounds,
316+
// the provided graph.
317+
// The flag FromPB requests the algorithm to use potential length variables.
318+
bool performWorkListInference(std::set<BoundsKey> &ArrNeededBounds,
356319
AVarGraph &BKGraph,
357-
AvarBoundsInference &BI);
320+
bool FromPB = false);
358321

359322
void insertParamKey(ParamDeclType ParamDecl, BoundsKey NK);
360323
};

clang/include/clang/CConv/ArrayBoundsInferenceConsumer.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@
2222
class LocalVarABVisitor;
2323
class ConstraintResolver;
2424

25-
class AllocBasedBoundsInference : public ASTConsumer {
26-
public:
27-
explicit AllocBasedBoundsInference(ProgramInfo &I, clang::ASTContext *C) : Info(I) { }
28-
virtual void HandleTranslationUnit(ASTContext &Context);
29-
30-
private:
31-
ProgramInfo &Info;
32-
};
33-
3425
// This class handles determining bounds of global array variables.
3526
// i.e., function parameters, structure fields and global variables.
3627
class GlobalABVisitor: public clang::RecursiveASTVisitor<GlobalABVisitor> {
@@ -105,8 +96,7 @@ class LengthVarInference : public StmtVisitor<LengthVarInference> {
10596
std::unique_ptr<CFG> Cfg;
10697
};
10798

108-
void HandleArrayVariablesBoundsDetection(ASTContext *C, ProgramInfo &I,
109-
bool UseHeuristics = true);
99+
void HandleArrayVariablesBoundsDetection(ASTContext *C, ProgramInfo &I);
110100

111101
// Add constraints based on heuristics to the parameters of the
112102
// provided function.

clang/include/clang/CConv/ConstraintsGraph.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ class DataGraph :
114114
N = nullptr;
115115
}
116116
this->Nodes.clear();
117-
invalidateBFSCache();
118117
}
119118

120119
void removeEdge(Data Src, Data Dst) {
@@ -127,14 +126,12 @@ class DataGraph :
127126
(*NDst)->removeEdge(*E);
128127
delete E;
129128
}
130-
invalidateBFSCache();
131129
}
132130

133131
void addEdge(Data L, Data R) {
134132
NodeType *BL = this->findOrCreateNode(L);
135133
NodeType *BR = this->findOrCreateNode(R);
136134
BL->connectTo(*BR);
137-
invalidateBFSCache();
138135
}
139136

140137
bool getNeighbors(Data D, std::set<Data> &DataSet, bool Succ){
@@ -164,16 +161,8 @@ class DataGraph :
164161
auto *N = this->findNode(NodeType(Start));
165162
if (N == this->end())
166163
return;
167-
// Insert into BFS cache.
168-
if (BFSCache.find(Start) == BFSCache.end()) {
169-
std::set<Data> ReachableNodes;
170-
for (auto TNode : llvm::breadth_first(*N)) {
171-
ReachableNodes.insert(TNode->getData());
172-
}
173-
BFSCache[Start] = ReachableNodes;
174-
}
175-
for (auto SN : BFSCache[Start])
176-
Fn(SN);
164+
for (auto TNode : llvm::breadth_first(*N))
165+
Fn(TNode->getData());
177166
}
178167

179168
protected:
@@ -194,11 +183,6 @@ class DataGraph :
194183
template <typename G>
195184
friend struct llvm::GraphTraits;
196185
friend class GraphVizOutputGraph;
197-
std::map<Data, std::set<Data>> BFSCache;
198-
199-
void invalidateBFSCache() {
200-
BFSCache.clear();
201-
}
202186
};
203187

204188
// Specialize the graph for the checked and pointer type constraint graphs. This

clang/include/clang/CConv/ProgramVar.h

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include "clang/AST/ASTContext.h"
2020
#include "clang/CConv/PersistentSourceLoc.h"
2121

22-
// Unique ID for a program variable or constant literal, both of
23-
// which could serve as bounds
2422
typedef uint32_t BoundsKey;
2523

2624
// Class representing scope of a program variable.
@@ -29,9 +27,9 @@ class ProgramVarScope {
2927
enum ScopeKind {
3028
// Function scope.
3129
FunctionScopeKind,
32-
// Parameters of a particular function.
30+
// Function parameter scope.
3331
FunctionParamScopeKind,
34-
// All arguments to a particular call
32+
// Context sensitive argument scope.
3533
CtxFunctionArgScopeKind,
3634
// Struct scope.
3735
StructScopeKind,
@@ -217,17 +215,6 @@ class CtxFunctionArgScope : public FunctionParamScope {
217215
const PersistentSourceLoc &CtxPSL) :
218216
FunctionParamScope(FN, IsSt) {
219217
PSL = CtxPSL;
220-
std::string FileName = PSL.getFileName();
221-
CtxIDStr = "";
222-
if (!FileName.empty()) {
223-
llvm::sys::fs::UniqueID UId;
224-
if (llvm::sys::fs::getUniqueID(FileName, UId)) {
225-
CtxIDStr = std::to_string(UId.getDevice()) + ":" +
226-
std::to_string(UId.getFile()) + ":";
227-
}
228-
}
229-
CtxIDStr += std::to_string(PSL.getLineNo()) + ":" +
230-
std::to_string(PSL.getColSNo());
231218
this->Kind = CtxFunctionArgScopeKind;
232219
}
233220

@@ -274,16 +261,16 @@ class CtxFunctionArgScope : public FunctionParamScope {
274261
}
275262

276263
std::string getStr() const {
277-
return FName + "_Ctx_" + CtxIDStr;
264+
return "CtxFuncArg_" + FName;
278265
}
279266

280267
static const CtxFunctionArgScope *
281268
getCtxFunctionParamScope(const FunctionParamScope *FPS,
282269
const PersistentSourceLoc &PSL);
283270

284271
private:
285-
PersistentSourceLoc PSL; // source code location of this function call
286-
std::string CtxIDStr;
272+
PersistentSourceLoc PSL;
273+
287274
static std::set<CtxFunctionArgScope, PVSComp> AllCtxFnArgScopes;
288275
};
289276

@@ -367,7 +354,7 @@ class ProgramVar {
367354
BoundsKey K;
368355
std::string VarName;
369356
const ProgramVarScope *VScope;
370-
bool IsConstant; // is a literal integer, not a variable
357+
bool IsConstant;
371358
// TODO: All the ProgramVars may not be used. We should try to figure out
372359
// a way to free unused program vars.
373360
static std::set<ProgramVar *> AllProgramVars;

clang/include/clang/CConv/Utils.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ bool isFunctionAllocator(std::string FuncName);
124124
// Is the given variable built in type?
125125
bool isPointerType(clang::ValueDecl *VD);
126126

127-
// Is this a pointer or array type?
128-
bool isPtrOrArrayType(const clang::QualType &QT);
129-
130127
// Check if provided type is a var arg type?
131128
bool isVarArgType(const std::string &TypeName);
132129

clang/lib/CConv/ABounds.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ std::string CountBound::mkString(AVarBoundsInfo *ABI) {
6363
return "count(" + PV->mkString() + ")";
6464
}
6565

66-
bool CountBound::areSame(ABounds *O, AVarBoundsInfo *ABI) {
66+
bool CountBound::areSame(ABounds *O) {
6767
if (O != nullptr) {
68-
if (CountBound *OT = dyn_cast<CountBound>(O))
69-
return ABI->areSameProgramVar(this->CountVar, OT->CountVar);
68+
if (CountBound *OT = dyn_cast<CountBound>(O)) {
69+
return OT->CountVar == CountVar;
70+
}
7071
}
7172
return false;
7273
}
@@ -85,10 +86,10 @@ std::string ByteBound::mkString(AVarBoundsInfo *ABI) {
8586
return "byte_count(" + PV->mkString() + ")";
8687
}
8788

88-
bool ByteBound::areSame(ABounds *O, AVarBoundsInfo *ABI) {
89+
bool ByteBound::areSame(ABounds *O) {
8990
if (O != nullptr) {
9091
if (ByteBound *BB = dyn_cast<ByteBound>(O)) {
91-
return ABI->areSameProgramVar(this->ByteVar, BB->ByteVar);
92+
return BB->ByteVar == ByteVar;
9293
}
9394
}
9495
return false;
@@ -110,11 +111,10 @@ std::string RangeBound::mkString(AVarBoundsInfo *ABI) {
110111
return "bounds(" + LBVar->mkString() + ", " + UBVar->mkString() + ")";
111112
}
112113

113-
bool RangeBound::areSame(ABounds *O, AVarBoundsInfo *ABI) {
114+
bool RangeBound::areSame(ABounds *O) {
114115
if (O != nullptr) {
115116
if (RangeBound *RB = dyn_cast<RangeBound>(O)) {
116-
return ABI->areSameProgramVar(this->LB, RB->LB) &&
117-
ABI->areSameProgramVar(this->UB, RB->UB);
117+
return RB->LB == LB && RB->UB == UB;
118118
}
119119
}
120120
return false;

0 commit comments

Comments
 (0)