Skip to content

Commit e780f40

Browse files
committed
Added some experimental optimizations to remove dead values from the
state. llvm-svn: 46106
1 parent 1aa1941 commit e780f40

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

clang/Analysis/GRConstants.cpp

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class VISIBILITY_HIDDEN DSPtr {
6565
inline bool operator!=(const DSPtr& X) const { return Raw != X.Raw; }
6666
inline bool operator<(const DSPtr& X) const {
6767
VariantKind k = getKind(), Xk = X.getKind();
68-
return k == Xk ? getPtr() < X.getPtr() : k < Xk;
68+
return k == Xk ? getPtr() < X.getPtr() : ((unsigned) k) < ((unsigned) Xk);
6969
}
7070
};
7171
} // end anonymous namespace
@@ -76,7 +76,7 @@ namespace llvm {
7676
return V.getKind() == DSPtr::IsValueDecl;
7777
}
7878
template<> inline bool isa<Stmt,DSPtr>(const DSPtr& V) {
79-
return ((unsigned) V.getKind()) > DSPtr::IsValueDecl;
79+
return ((unsigned) V.getKind()) != DSPtr::IsValueDecl;
8080
}
8181
template<> struct VISIBILITY_HIDDEN cast_retty_impl<ValueDecl,DSPtr> {
8282
typedef const ValueDecl* ret_type;
@@ -174,11 +174,12 @@ class VISIBILITY_HIDDEN GRConstants : public CFGStmtVisitor<GRConstants> {
174174
NodeSetTy* Nodes;
175175
NodeSetTy* OldNodes;
176176
StateTy CurrentState;
177+
NodeTy* InitialPred;
177178

178179
public:
179180
GRConstants() : Liveness(NULL), Builder(NULL), cfg(NULL),
180181
Nodes(&NodeSetA), OldNodes(&NodeSetB),
181-
CurrentState(StateMgr.GetEmptyMap()) {}
182+
CurrentState(StateMgr.GetEmptyMap()), InitialPred(NULL) {}
182183

183184
~GRConstants() { delete Liveness; }
184185

@@ -197,7 +198,9 @@ class VISIBILITY_HIDDEN GRConstants : public CFGStmtVisitor<GRConstants> {
197198
void ProcessStmt(Stmt* S, NodeBuilder& builder);
198199
void SwitchNodeSets();
199200
void DoStmt(Stmt* S);
200-
StateTy RemoveGrandchildrenMappings(Stmt* S, StateTy M);
201+
202+
StateTy RemoveDescendantMappings(Stmt* S, StateTy M, unsigned Levels=2);
203+
StateTy RemoveSubExprMappings(StateTy M);
201204

202205
void AddBinding(Expr* E, ExprVariantTy V, bool isBlkLvl = false);
203206
void AddBinding(ValueDecl* D, ExprVariantTy V);
@@ -224,9 +227,10 @@ void GRConstants::ProcessStmt(Stmt* S, NodeBuilder& builder) {
224227
Builder = &builder;
225228
Nodes->clear();
226229
OldNodes->clear();
227-
NodeTy* N = Builder->getLastNode();
228-
assert (N);
229-
OldNodes->push_back(N);
230+
InitialPred = Builder->getLastNode();
231+
assert (InitialPred);
232+
OldNodes->push_back(InitialPred);
233+
CurrentState = RemoveSubExprMappings(InitialPred->getState());
230234
BlockStmt_Visit(S);
231235
Builder = NULL;
232236
}
@@ -276,22 +280,51 @@ void GRConstants::SwitchNodeSets() {
276280
}
277281

278282
GRConstants::StateTy
279-
GRConstants::RemoveGrandchildrenMappings(Stmt* S, GRConstants::StateTy State) {
280-
283+
GRConstants::RemoveSubExprMappings(StateTy M) {
284+
#if 0
285+
return M;
286+
#else
287+
for (StateTy::iterator I = M.begin(), E = M.end();
288+
I!=E && I.getKey().getKind() == DSPtr::IsSubExp; ++I) {
289+
// Note: we can assign a new map to M since the iterators are
290+
// iterating over the tree of the original map (aren't immutable maps
291+
// nice?).
292+
M = StateMgr.Remove(M, I.getKey());
293+
}
294+
295+
return M;
296+
#endif
297+
}
298+
299+
300+
GRConstants::StateTy
301+
GRConstants::RemoveDescendantMappings(Stmt* S, GRConstants::StateTy State,
302+
unsigned Levels) {
303+
#if 1
304+
return State;
305+
#else
281306
typedef Stmt::child_iterator iterator;
282307
283308
for (iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
284-
if (Stmt* C = *I)
285-
for (iterator CI=C->child_begin(), CE=C->child_end(); CI!=CE; ++CI) {
309+
if (Stmt* C = *I) {
310+
if (Levels == 1) {
286311
// Observe that this will only remove mappings to non-block level
287312
// expressions. This is valid even if *CI is a block-level expression,
288313
// since it simply won't be in the map in the first place.
289314
// Note: This should also work if 'C' is a block-level expression,
290315
// although ideally we would want to skip processing C's children.
291-
State = StateMgr.Remove(State, DSPtr(*CI,false));
316+
State = StateMgr.Remove(State, DSPtr(C,false));
317+
}
318+
else {
319+
if (ParenExpr* P = dyn_cast<ParenExpr>(C))
320+
State = RemoveDescendantMappings(P, State, Levels);
321+
else
322+
State = RemoveDescendantMappings(C, State, Levels-1);
292323
}
324+
}
293325
294326
return State;
327+
#endif
295328
}
296329

297330
void GRConstants::DoStmt(Stmt* S) {
@@ -300,10 +333,14 @@ void GRConstants::DoStmt(Stmt* S) {
300333

301334
for (NodeSetTy::iterator I=OldNodes->begin(), E=OldNodes->end(); I!=E; ++I) {
302335
NodeTy* Pred = *I;
303-
CurrentState = Pred->getState();
336+
337+
if (Pred != InitialPred)
338+
CurrentState = Pred->getState();
304339

305340
StateTy OldState = CurrentState;
306-
CurrentState = RemoveGrandchildrenMappings(S, CurrentState);
341+
342+
if (Pred != InitialPred)
343+
CurrentState = RemoveDescendantMappings(S, CurrentState);
307344

308345
Visit(S);
309346

0 commit comments

Comments
 (0)