Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProgramMemory: avoid several unnecessary copies #5943

Merged
merged 6 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void ProgramMemory::setValue(const Token* expr, const ValueFlow::Value& value) {
},
subvalue);
if (subexpr)
mValues[subexpr] = subvalue;
mValues[subexpr] = std::move(subvalue);
}
const ValueFlow::Value* ProgramMemory::getValue(nonneg int exprid, bool impossible) const
{
Expand Down Expand Up @@ -183,10 +183,10 @@ bool ProgramMemory::empty() const
return mValues.empty();
}

void ProgramMemory::replace(const ProgramMemory &pm)
void ProgramMemory::replace(ProgramMemory pm)
{
for (auto&& p : pm.mValues) {
mValues[p.first] = p.second;
mValues[p.first] = std::move(p.second);
}
}

Expand Down Expand Up @@ -448,12 +448,12 @@ void ProgramMemoryState::insert(const ProgramMemory &pm, const Token* origin)
state.insert(pm);
}

void ProgramMemoryState::replace(const ProgramMemory &pm, const Token* origin)
void ProgramMemoryState::replace(ProgramMemory pm, const Token* origin)
{
if (origin)
for (auto&& p : pm)
for (const auto& p : pm)
origins[p.first.getExpressionId()] = origin;
state.replace(pm);
state.replace(std::move(pm));
}

static void addVars(ProgramMemory& pm, const ProgramMemory::Map& vars)
Expand All @@ -472,7 +472,7 @@ void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& va
ProgramMemory local = pm;
fillProgramMemoryFromAssignments(pm, tok, settings, local, vars);
addVars(pm, vars);
replace(pm, tok);
replace(std::move(pm), tok);
}

void ProgramMemoryState::assume(const Token* tok, bool b, bool isEmpty)
Expand All @@ -495,7 +495,7 @@ void ProgramMemoryState::assume(const Token* tok, bool b, bool isEmpty)

void ProgramMemoryState::removeModifiedVars(const Token* tok)
{
ProgramMemory pm = state;
const ProgramMemory& pm = state;
auto eval = [&](const Token* cond) -> std::vector<MathLib::bigint> {
if (conditionIsTrue(cond, pm, settings))
return {1};
Expand Down Expand Up @@ -1270,9 +1270,10 @@ namespace {
ValueFlow::Value r = state.execute(tok);
if (r.isUninitValue())
continue;
result.insert(std::make_pair(tok->exprId(), r));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filed llvm/llvm-project#79213 about detecting the unnecessary temporary object.

const bool brk = b && isTrueOrFalse(r, *b);
result.emplace(tok->exprId(), std::move(r));
// Short-circuit evaluation
if (b && isTrueOrFalse(r, *b))
if (brk)
break;
}
return result;
Expand Down Expand Up @@ -1496,13 +1497,13 @@ namespace {
if (expr->isComparisonOp() && (r.isUninitValue() || r.isImpossible())) {
if (rhs.isIntValue()) {
std::vector<ValueFlow::Value> result =
infer(ValueFlow::makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {rhs});
infer(ValueFlow::makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {std::move(rhs)});
if (!result.empty() && result.front().isKnown())
return result.front();
}
if (lhs.isIntValue()) {
std::vector<ValueFlow::Value> result =
infer(ValueFlow::makeIntegralInferModel(), expr->str(), {lhs}, expr->astOperand2()->values());
infer(ValueFlow::makeIntegralInferModel(), expr->str(), {std::move(lhs)}, expr->astOperand2()->values());
if (!result.empty() && result.front().isKnown())
return result.front();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/programmemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ struct ProgramMemory {

bool empty() const;

void replace(const ProgramMemory &pm);
void replace(ProgramMemory pm);

void insert(const ProgramMemory &pm);

Expand Down Expand Up @@ -171,7 +171,7 @@ struct ProgramMemoryState {
explicit ProgramMemoryState(const Settings* s);

void insert(const ProgramMemory &pm, const Token* origin = nullptr);
void replace(const ProgramMemory &pm, const Token* origin = nullptr);
void replace(ProgramMemory pm, const Token* origin = nullptr);

void addState(const Token* tok, const ProgramMemory::Map& vars);

Expand Down
Loading