Skip to content

Commit 4e25e29

Browse files
Move lookups of VarData by Decl to a separate routine in TBR
1 parent 46e5272 commit 4e25e29

2 files changed

Lines changed: 20 additions & 34 deletions

File tree

lib/Differentiator/TBRAnalyzer.cpp

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,7 @@ TBRAnalyzer::VarData* TBRAnalyzer::getExprVarData(const clang::Expr* E) {
157157
// ``this`` does not have a declaration so it is represented with nullptr.
158158
if (const auto* DRE = dyn_cast<clang::DeclRefExpr>(E))
159159
VD = dyn_cast<clang::VarDecl>(DRE->getDecl());
160-
auto* branch = &getCurBlockVarsData();
161-
while (branch) {
162-
auto it = branch->find(VD);
163-
if (it != branch->end()) {
164-
EData = &it->second;
165-
break;
166-
}
167-
branch = branch->m_Prev;
168-
}
160+
EData = getVarDataFromDecl(VD);
169161
}
170162
if (const auto* ME = dyn_cast<clang::MemberExpr>(E))
171163
EData = getMemberVarData(ME);
@@ -234,17 +226,7 @@ void TBRAnalyzer::overlay(const clang::Expr* E) {
234226
} else if (const auto* DRE = dyn_cast<clang::DeclRefExpr>(E)) {
235227
const auto* VD = cast<VarDecl>(DRE->getDecl());
236228
if (VD->getType()->isReferenceType()) {
237-
// FIXME: Handle this in a separate functions
238-
VarData* refData = nullptr;
239-
auto* branch = &getCurBlockVarsData();
240-
while (branch) {
241-
auto it = branch->find(VD);
242-
if (it != branch->end()) {
243-
refData = &it->second;
244-
break;
245-
}
246-
branch = branch->m_Prev;
247-
}
229+
VarData* refData = getVarDataFromDecl(VD);
248230
E = refData->m_Val.m_RefData;
249231
continue;
250232
}
@@ -290,20 +272,10 @@ void TBRAnalyzer::setIsRequired(const clang::Expr* E, bool isReq) {
290272
const auto* VD = cast<VarDecl>(DRE->getDecl());
291273
auto& curBranch = getCurBlockVarsData();
292274
if (curBranch.find(VD) == curBranch.end()) {
293-
// Visit all predecessors one by one until the variable VD is found.
294-
auto* pred = curBranch.m_Prev;
295-
bool found = false;
296-
while (pred) {
297-
auto it = pred->find(VD);
298-
if (it != pred->end()) {
299-
curBranch[VD] = copy(it->second);
300-
found = true;
301-
break;
302-
}
303-
pred = pred->m_Prev;
304-
}
305-
// If this variable was not found in predecessors, add it.
306-
if (!found)
275+
if (VarData* data = getVarDataFromDecl(VD))
276+
curBranch[VD] = copy(*data);
277+
else
278+
// If this variable was not found in predecessors, add it.
307279
addVar(VD);
308280
}
309281
}
@@ -321,6 +293,18 @@ void TBRAnalyzer::setIsRequired(const clang::Expr* E, bool isReq) {
321293
}
322294
}
323295

296+
TBRAnalyzer::VarData*
297+
TBRAnalyzer::getVarDataFromDecl(const clang::VarDecl* VD) {
298+
auto* branch = &getCurBlockVarsData();
299+
while (branch) {
300+
auto it = branch->find(VD);
301+
if (it != branch->end())
302+
return &it->second;
303+
branch = branch->m_Prev;
304+
}
305+
return nullptr;
306+
}
307+
324308
void TBRAnalyzer::Analyze(const FunctionDecl* FD) {
325309
// Build the CFG (control-flow graph) of FD.
326310
clang::CFG::BuildOptions Options;

lib/Differentiator/TBRAnalyzer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ class TBRAnalyzer : public clang::RecursiveASTVisitor<TBRAnalyzer> {
143143
VarData* getArrSubVarData(const clang::ArraySubscriptExpr* ASE);
144144
/// Given an Expr* returns its corresponding VarData.
145145
VarData* getExprVarData(const clang::Expr* E);
146+
/// Finds VD in the most recent block.
147+
VarData* getVarDataFromDecl(const clang::VarDecl* VD);
146148

147149
/// Whenever an array element with a non-constant index is set to required
148150
/// this function is used to set to required all the array elements that

0 commit comments

Comments
 (0)