Skip to content

Commit 01d438b

Browse files
Remove addNonConstIdx arg from utility functions in TBR.
``getExprVarData`` has a parameter ``addNonConstIdx``, which was probably meant for efficiency when building ``VarData``. The difference, however, is likely negligible, as the function will return earlier only under quite specific circumstances. There's no need to overcomplicate the analysis with this.
1 parent e7e0c33 commit 01d438b

2 files changed

Lines changed: 14 additions & 33 deletions

File tree

lib/Differentiator/TBRAnalyzer.cpp

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,50 +98,36 @@ void TBRAnalyzer::overlay(VarData& targetData,
9898
overlay((*targetData.m_Val.m_ArrData)[curID], IDSequence, i);
9999
}
100100

101-
TBRAnalyzer::VarData* TBRAnalyzer::getMemberVarData(const clang::MemberExpr* ME,
102-
bool addNonConstIdx) {
101+
TBRAnalyzer::VarData*
102+
TBRAnalyzer::getMemberVarData(const clang::MemberExpr* ME) {
103103
if (const auto* FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
104104
const auto* base = ME->getBase();
105105
VarData* baseData = getExprVarData(base);
106106

107107
if (!baseData)
108108
return nullptr;
109109

110-
// if non-const index was found and it is not supposed to be added just
111-
// return the current VarData*.
112-
if (m_NonConstIndexFound && !addNonConstIdx)
113-
return baseData;
114-
115110
return &(*baseData->m_Val.m_ArrData)[getProfileID(FD)];
116111
}
117112
return nullptr;
118113
}
119114

120115
TBRAnalyzer::VarData*
121-
TBRAnalyzer::getArrSubVarData(const clang::ArraySubscriptExpr* ASE,
122-
bool addNonConstIdx) {
116+
TBRAnalyzer::getArrSubVarData(const clang::ArraySubscriptExpr* ASE) {
123117
const auto* idxExpr = ASE->getIdx();
124118
ProfileID idxID;
125-
bool currentIdxIsNonConst = false;
126-
if (const auto* IL = dyn_cast<IntegerLiteral>(idxExpr)) {
119+
if (const auto* IL = dyn_cast<IntegerLiteral>(idxExpr))
127120
idxID = getProfileID(IL);
128-
} else {
121+
else
129122
// Non-const indices are represented with default FoldingSetNodeID.
130123
m_NonConstIndexFound = true;
131-
currentIdxIsNonConst = true;
132-
}
133124

134125
const auto* base = ASE->getBase()->IgnoreImpCasts();
135126
VarData* baseData = getExprVarData(base);
136127

137128
if (!baseData)
138129
return nullptr;
139130

140-
// if non-const index was found and it is not supposed to be added just
141-
// return the current VarData*.
142-
if (currentIdxIsNonConst && !addNonConstIdx)
143-
return nullptr;
144-
145131
auto* baseArrMap = baseData->m_Val.m_ArrData.get();
146132
auto it = baseArrMap->find(idxID);
147133

@@ -159,8 +145,7 @@ TBRAnalyzer::getArrSubVarData(const clang::ArraySubscriptExpr* ASE,
159145
return &it->second;
160146
}
161147

162-
TBRAnalyzer::VarData* TBRAnalyzer::getExprVarData(const clang::Expr* E,
163-
bool addNonConstIdx) {
148+
TBRAnalyzer::VarData* TBRAnalyzer::getExprVarData(const clang::Expr* E) {
164149
// This line is necessary for pointer member expressions (in 'x->y' x would be
165150
// implicitly casted with the * operator).
166151
E = E->IgnoreImpCasts();
@@ -181,9 +166,9 @@ TBRAnalyzer::VarData* TBRAnalyzer::getExprVarData(const clang::Expr* E,
181166
}
182167
}
183168
if (const auto* ME = dyn_cast<clang::MemberExpr>(E))
184-
EData = getMemberVarData(ME, addNonConstIdx);
169+
EData = getMemberVarData(ME);
185170
if (const auto* ASE = dyn_cast<clang::ArraySubscriptExpr>(E))
186-
EData = getArrSubVarData(ASE, addNonConstIdx);
171+
EData = getArrSubVarData(ASE);
187172

188173
if (EData && EData->m_Type == VarData::REF_TYPE && EData->m_Val.m_RefData)
189174
EData = getExprVarData(EData->m_Val.m_RefData);
@@ -258,7 +243,7 @@ void TBRAnalyzer::overlay(const clang::Expr* E) {
258243
}
259244

260245
// Overlay on all the VarData's recursively.
261-
VarData& data = *getExprVarData(innermostDRE, /*addNonConstIdx=*/true);
246+
VarData& data = *getExprVarData(innermostDRE);
262247
overlay(data, IDSequence, IDSequence.size());
263248
}
264249
// NOLINTEND(cppcoreguidelines-pro-type-union-access)
@@ -301,7 +286,7 @@ void TBRAnalyzer::markLocation(const clang::Expr* E) {
301286
void TBRAnalyzer::setIsRequired(const clang::Expr* E, bool isReq) {
302287
if (!isReq ||
303288
(m_ModeStack.back() == (Mode::kMarkingMode | Mode::kNonLinearMode))) {
304-
VarData* data = getExprVarData(E, /*addNonConstIdx=*/isReq);
289+
VarData* data = getExprVarData(E);
305290
if (data && (isReq || !m_NonConstIndexFound))
306291
setIsRequired(*data, isReq);
307292
// If an array element with a non-const element is set to required all the

lib/Differentiator/TBRAnalyzer.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,12 @@ class TBRAnalyzer : public clang::RecursiveASTVisitor<TBRAnalyzer> {
137137

138138
/// Given a MemberExpr*/ArraySubscriptExpr* return a pointer to its
139139
/// corresponding VarData. If the given element of an array does not have a
140-
/// VarData yet it will be added automatically. If addNonConstIdx==false this
141-
/// will return the last VarData before the non-constant index
142-
/// (e.g. for 'x.arr[k+1].y' the return value will be the VarData of x.arr).
140+
/// VarData yet it will be added automatically.
143141
/// Otherwise, non-const indices will be represented as index -1.
144-
VarData* getMemberVarData(const clang::MemberExpr* ME,
145-
bool addNonConstIdx = false);
146-
VarData* getArrSubVarData(const clang::ArraySubscriptExpr* ASE,
147-
bool addNonConstIdx = false);
142+
VarData* getMemberVarData(const clang::MemberExpr* ME);
143+
VarData* getArrSubVarData(const clang::ArraySubscriptExpr* ASE);
148144
/// Given an Expr* returns its corresponding VarData.
149-
VarData* getExprVarData(const clang::Expr* E, bool addNonConstIdx = false);
145+
VarData* getExprVarData(const clang::Expr* E);
150146

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

0 commit comments

Comments
 (0)