Skip to content

ValueFlow: avoid various unnecessary copies #7583

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
a5d5e0e
vf_settokenvalue.cpp: avoid unnecessary copies with `setTokenValueCas…
firewave Jun 16, 2024
750aa37
valueflow.cpp: avoid unnecessary copies in `valueFlowContainerSize()`
firewave Aug 31, 2024
321f5d1
vf_settokenvalue.cpp: avoid unnecessary objects in `setTokenValue()`
firewave Aug 31, 2024
a838908
valueflow.cpp: avoid unnecessary copies in `valueFlowFunctionReturn()`
firewave Aug 31, 2024
820df8c
valueflow.cpp: avoid unnecessary copy in `valueFlowLifetime()`
firewave Aug 31, 2024
78aff9c
programmemory.cpp: added comment
firewave Aug 31, 2024
2b51368
valueflow.cpp: avoid unnecessary copy in `valueFlowSymbolicOperators()`
firewave Sep 29, 2024
989e3bc
ValueFlow: avoid unnecessary copy in `getLifetimeObjValue()`
firewave Dec 25, 2024
7007d20
valueflow.cpp: avoid unnecessary copy in `valueFlowAfterAssign()`
firewave Dec 25, 2024
20319bb
valueflow.cpp: avoid unnecessary copies in `valueFlowInferCondition()`
firewave Dec 25, 2024
58b9af5
valueflow.cpp: avoid unnecessary copy in `valueFlowContainerSize()`
firewave Dec 25, 2024
503a76a
valueflow.cpp: avoid unnecessary copy in `valueFlowSafeFunctions()`
firewave Dec 25, 2024
a1da6fa
valueflow.cpp: avoid unnecessary copies with `valueFlowInjectParamete…
firewave Jan 8, 2025
bd75fca
programmemory.cpp: avoid unnecessary copy in `programMemoryParseCondi…
firewave Aug 7, 2024
347823d
vf_settokenvalue.cpp: void unnecessary copy in `setTokenValue()`
firewave Jun 10, 2025
cb30d60
valueflow.cpp: avoid unnecessary copies in `valueFlowFunctionReturn()`
firewave Jun 10, 2025
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
10 changes: 6 additions & 4 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ std::size_t ExprIdToken::Hash::operator()(ExprIdToken etok) const
void ProgramMemory::setValue(const Token* expr, const ValueFlow::Value& value) {
copyOnWrite();

(*mValues)[expr] = value;
(*mValues)[expr] = value; // copy
ValueFlow::Value subvalue = value;
const Token* subexpr = solveExprValue(
expr,
Expand Down Expand Up @@ -327,11 +327,13 @@ static void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, con
if (endTok && findExpressionChanged(vartok, tok->next(), endTok, settings))
return;
const bool impossible = (tok->str() == "==" && !then) || (tok->str() == "!=" && then);
const ValueFlow::Value& v = then ? truevalue : falsevalue;
pm.setValue(vartok, impossible ? asImpossible(v) : v);
ValueFlow::Value& v = then ? truevalue : falsevalue;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be ValueFlow::Value&& v = then ? std::move(truevalue) : std::move(falsevalue).

const auto iv = v.intvalue;
// cppcheck-suppress accessMoved - FP #13628
pm.setValue(vartok, impossible ? asImpossible(std::move(v)) : v);
const Token* containerTok = settings.library.getContainerFromYield(vartok, Library::Container::Yield::SIZE);
if (containerTok)
pm.setContainerSizeValue(containerTok, v.intvalue, !impossible);
pm.setContainerSizeValue(containerTok, iv, !impossible);
} else if (Token::simpleMatch(tok, "!")) {
programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, !then);
} else if (then && Token::simpleMatch(tok, "&&")) {
Expand Down
45 changes: 22 additions & 23 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@ ValueFlow::Value ValueFlow::getLifetimeObjValue(const Token *tok, bool inconclus
// There should only be one lifetime
if (values.size() != 1)
return ValueFlow::Value{};
return values.front();
return std::move(values.front());
}

template<class Predicate>
Expand Down Expand Up @@ -3155,10 +3155,10 @@ static void valueFlowLifetime(TokenList &tokenlist, ErrorLogger &errorLogger, co
else if (tok->isUnaryOp("&")) {
if (Token::simpleMatch(tok->astParent(), "*"))
continue;
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok->astOperand1(), settings)) {
for (ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok->astOperand1(), settings)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be written as for (ValueFlow::LifetimeToken&& lt : ValueFlow::getLifetimeTokens(tok->astOperand1(), settings)), although I think you might need to use move_iterators.

if (!settings.certainty.isEnabled(Certainty::inconclusive) && lt.inconclusive)
continue;
ErrorPath errorPath = lt.errorPath;
ErrorPath& errorPath = lt.errorPath;
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be written as ErrorPath&& errorPath = std::move(lt.errorPath).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would require two moves in the code so wouldn't that pessimize the code? Need to look into this. See also a somewhat related discussion I recently stumbled on: llvm/llvm-project#94798.

Copy link
Contributor

Choose a reason for hiding this comment

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

That would require two moves in the code so wouldn't that pessimize the code?

Not its not. std::move is just a cast. A move will happen when its passed to a value parameter. Another std::move will still be needed since an rvalue reference variable will be an lvalue reference instead. So you will need to keep the std::move(errorPath) below.

See also a somewhat related discussion I recently stumbled on: llvm/llvm-project#94798.

Thats not related. That does call two move constructors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not its not. std::move is just a cast. A move will happen when its passed to a value parameter. Another std::move will still be needed since an rvalue reference variable will be an lvalue reference instead.

Thanks. As usual I have been stupid here. But the latter sentence answered a question while looking at the code.

What would be a case where a second std::move() would be unnecessary? I am wondering if the existing Clang/clang-tidy check would detect those.

Copy link
Contributor

Choose a reason for hiding this comment

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

What would be a case where a second std::move() would be unnecessary?

If you want to make a copy. But it seems like the check should require using std::move or std::as_const to make moves and copies explicit.

errorPath.emplace_back(tok, "Address of variable taken here.");

ValueFlow::Value value;
Expand Down Expand Up @@ -3801,7 +3801,7 @@ static void valueFlowSymbolicOperators(const SymbolDatabase& symboldatabase, con
continue;

ValueFlow::Value v = makeSymbolic(arg);
v.errorPath = c.errorPath;
v.errorPath = std::move(c.errorPath);
v.errorPath.emplace_back(tok, "Passed to " + tok->str());
if (c.intvalue == 0)
v.setImpossible();
Expand Down Expand Up @@ -4355,7 +4355,7 @@ static void valueFlowAfterAssign(TokenList &tokenlist,
continue;
ids.insert(value.tokvalue->exprId());
}
for (ValueFlow::Value value : values) {
for (ValueFlow::Value& value : values) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be for (ValueFlow::Value&& value : std::move(values)). This will make clear that the elements in the values vector has been moved.

Copy link
Contributor

Choose a reason for hiding this comment

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

So we can't just move, we need to use move iterators. Something like this would do it:

template <class Container>
struct move_range_adaptor {
    move_range_adaptor(Container&& c) : container(std::forward<Container>(c)) {}
    
    auto begin() { return std::make_move_iterator(container.begin()); }
    auto end() { return std::make_move_iterator(container.end()); }

private:
    Container container;
};

template<class Container>
move_range_adaptor<Container> move_range(Container&& c) {
    return {std::forward<Container>(c)};
}

We just need to update our analysis to treat move_range as a move.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This will make clear that the elements in the values vector has been moved.

That is a good point. It could even be a readability check.

I have been avoiding using rvalues explicitly ever since they were causing copies instead of moves in a project I worked on in the past and we had to nuke them all over the place to get rid of performance issues (I no longer have access to the code base but I think there might be affected parts in a public repo and will try to reproduce that).

Copy link
Contributor

Choose a reason for hiding this comment

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

I have been avoiding using rvalues explicitly ever since they were causing copies instead of moves in a project I worked on in the past and we had to nuke them all over the place to get rid of performance issues

I would assume this issue came from not moving rvalue references since rvalue reference variables become lvalues so they need to be moved again to be an rvalue.

Ideally there should be a check for this. Every implicit copy on an rvalue reference should be made as an explicit move or copy(althougth I am not sure how an explicit copy should be expressed).

Copy link
Contributor

Choose a reason for hiding this comment

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

Every implicit copy on an rvalue reference should be made as an explicit move or copy(althougth I am not sure how an explicit copy should be expressed).

Actually, std::as_const could be used to explicity copy.

if (!value.isSymbolicValue())
continue;
const Token* expr = value.tokvalue;
Expand Down Expand Up @@ -5198,7 +5198,7 @@ static void valueFlowInferCondition(TokenList& tokenlist, const Settings& settin
for (const ValuePtr<InferModel>& model : iteratorModels) {
std::vector<ValueFlow::Value> result =
infer(model, tok->str(), tok->astOperand1()->values(), tok->astOperand2()->values());
for (ValueFlow::Value value : result) {
for (ValueFlow::Value& value : result) {
value.valueType = ValueFlow::Value::ValueType::INT;
setTokenValue(tok, std::move(value), settings);
}
Expand All @@ -5216,8 +5216,7 @@ static void valueFlowInferCondition(TokenList& tokenlist, const Settings& settin
std::vector<ValueFlow::Value> result = infer(makeIntegralInferModel(), "!=", tok->values(), 0);
if (result.size() != 1)
continue;
ValueFlow::Value value = result.front();
setTokenValue(tok, std::move(value), settings);
setTokenValue(tok, std::move(result.front()), settings);
}
}
}
Expand Down Expand Up @@ -5636,7 +5635,7 @@ static void valueFlowInjectParameter(const TokenList& tokenlist,
const Settings& settings,
const Variable* arg,
const Scope* functionScope,
const std::list<ValueFlow::Value>& argvalues)
std::list<ValueFlow::Value> argvalues)
{
// Is argument passed by value or const reference, and is it a known non-class type?
if (arg->isReference() && !arg->isConst() && !arg->isClass())
Expand All @@ -5650,7 +5649,7 @@ static void valueFlowInjectParameter(const TokenList& tokenlist,
valueFlowForward(const_cast<Token*>(functionScope->bodyStart->next()),
functionScope->bodyEnd,
arg->nameToken(),
argvalues,
std::move(argvalues),
tokenlist,
errorLogger,
settings);
Expand Down Expand Up @@ -5876,7 +5875,7 @@ static void valueFlowFunctionDefaultParameter(const TokenList& tokenlist, const
argvalues.push_back(std::move(v));
}
if (!argvalues.empty())
valueFlowInjectParameter(tokenlist, errorLogger, settings, var, scope, argvalues);
valueFlowInjectParameter(tokenlist, errorLogger, settings, var, scope, std::move(argvalues));
}
}
}
Expand Down Expand Up @@ -5961,10 +5960,10 @@ static void valueFlowFunctionReturn(TokenList& tokenlist, ErrorLogger& errorLogg

bool hasKnownValue = false;

for (const ValueFlow::Value& v : getCommonValuesFromTokens(returns)) {
setFunctionReturnValue(function, tok, v, settings, false);
for (ValueFlow::Value& v : getCommonValuesFromTokens(returns)) {
if (v.isKnown())
hasKnownValue = true;
setFunctionReturnValue(function, tok, std::move(v), settings, false);
}

if (hasKnownValue)
Expand All @@ -5990,10 +5989,10 @@ static void valueFlowFunctionReturn(TokenList& tokenlist, ErrorLogger& errorLogg
if (programMemory.empty() && !arguments.empty())
continue;
std::vector<ValueFlow::Value> values = execute(function->functionScope, programMemory, settings);
for (const ValueFlow::Value& v : values) {
for (ValueFlow::Value& v : values) {
if (v.isUninitValue())
continue;
setFunctionReturnValue(function, tok, v, settings);
setFunctionReturnValue(function, tok, std::move(v), settings);
}
}
}
Expand Down Expand Up @@ -6799,8 +6798,8 @@ static void valueFlowContainerSize(const TokenList& tokenlist,
continue;
}

for (const ValueFlow::Value& value : values) {
valueFlowForward(nameToken->next(), var->nameToken(), value, tokenlist, errorLogger, settings);
for (ValueFlow::Value& value : values) {
valueFlowForward(nameToken->next(), var->nameToken(), std::move(value), tokenlist, errorLogger, settings);
}
}

Expand Down Expand Up @@ -6846,8 +6845,8 @@ static void valueFlowContainerSize(const TokenList& tokenlist,
const Token* constructorArgs = tok;
values = getContainerSizeFromConstructor(constructorArgs, tok->valueType(), settings, true);
}
for (const ValueFlow::Value& value : values)
setTokenValue(tok, value, settings);
for (ValueFlow::Value& value : values)
setTokenValue(tok, std::move(value), settings);
} else if (Token::Match(tok, ";|{|} %var% =") && Token::Match(tok->tokAt(2)->astOperand2(), "[({]") &&
// init list
((tok->tokAt(2) == tok->tokAt(2)->astOperand2()->astParent() && !tok->tokAt(2)->astOperand2()->astOperand2() && tok->tokAt(2)->astOperand2()->str() == "{") ||
Expand All @@ -6860,8 +6859,8 @@ static void valueFlowContainerSize(const TokenList& tokenlist,
Token* rhs = tok->tokAt(2)->astOperand2();
std::vector<ValueFlow::Value> values = getInitListSize(rhs, containerTok->valueType(), settings);
valueFlowContainerSetTokValue(tokenlist, errorLogger, settings, containerTok, rhs);
for (const ValueFlow::Value& value : values)
valueFlowForward(containerTok->next(), containerTok, value, tokenlist, errorLogger, settings);
for (ValueFlow::Value& value : values)
valueFlowForward(containerTok->next(), containerTok, std::move(value), tokenlist, errorLogger, settings);
}
} else if (Token::Match(tok, ". %name% (") && tok->astOperand1() && tok->astOperand1()->valueType() &&
tok->astOperand1()->valueType()->container) {
Expand Down Expand Up @@ -7125,8 +7124,8 @@ static void valueFlowSafeFunctions(const TokenList& tokenlist, const SymbolDatab
argValues.back().valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
argValues.back().errorPath.emplace_back(arg.nameToken(), "Assuming " + arg.name() + " size is 1000000");
argValues.back().safe = true;
for (const ValueFlow::Value &value : argValues)
valueFlowForward(const_cast<Token*>(functionScope->bodyStart), arg.nameToken(), value, tokenlist, errorLogger, settings);
for (ValueFlow::Value &value : argValues)
valueFlowForward(const_cast<Token*>(functionScope->bodyStart), arg.nameToken(), std::move(value), tokenlist, errorLogger, settings);
continue;
}

Expand Down
96 changes: 46 additions & 50 deletions lib/vf_settokenvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,32 +126,31 @@ namespace ValueFlow
return value.isIntValue() || value.isFloatValue();
}

static void setTokenValueCast(Token *parent, const ValueType &valueType, const Value &value, const Settings &settings)
static void setTokenValueCast(Token *parent, const ValueType &valueType, Value value, const Settings &settings)
{
if (valueType.pointer || value.isImpossible())
setTokenValue(parent,value,settings);
setTokenValue(parent,std::move(value),settings);
else if (valueType.type == ValueType::Type::CHAR)
setTokenValue(parent, castValue(value, valueType.sign, settings.platform.char_bit), settings);
setTokenValue(parent, castValue(std::move(value), valueType.sign, settings.platform.char_bit), settings);
else if (valueType.type == ValueType::Type::SHORT)
setTokenValue(parent, castValue(value, valueType.sign, settings.platform.short_bit), settings);
setTokenValue(parent, castValue(std::move(value), valueType.sign, settings.platform.short_bit), settings);
else if (valueType.type == ValueType::Type::INT)
setTokenValue(parent, castValue(value, valueType.sign, settings.platform.int_bit), settings);
setTokenValue(parent, castValue(std::move(value), valueType.sign, settings.platform.int_bit), settings);
else if (valueType.type == ValueType::Type::LONG)
setTokenValue(parent, castValue(value, valueType.sign, settings.platform.long_bit), settings);
setTokenValue(parent, castValue(std::move(value), valueType.sign, settings.platform.long_bit), settings);
else if (valueType.type == ValueType::Type::LONGLONG)
setTokenValue(parent, castValue(value, valueType.sign, settings.platform.long_long_bit), settings);
setTokenValue(parent, castValue(std::move(value), valueType.sign, settings.platform.long_long_bit), settings);
else if (valueType.isFloat() && isNumeric(value)) {
Value floatValue = value;
floatValue.valueType = Value::ValueType::FLOAT;
if (value.isIntValue())
floatValue.floatValue = static_cast<double>(value.intvalue);
setTokenValue(parent, std::move(floatValue), settings);
value.floatValue = static_cast<double>(value.intvalue);
value.valueType = Value::ValueType::FLOAT;
setTokenValue(parent, std::move(value), settings);
} else if (value.isIntValue()) {
const long long charMax = settings.platform.signedCharMax();
const long long charMin = settings.platform.signedCharMin();
if (charMin <= value.intvalue && value.intvalue <= charMax) {
// unknown type, but value is small so there should be no truncation etc
setTokenValue(parent,value,settings);
setTokenValue(parent,std::move(value),settings);
}
}
}
Expand Down Expand Up @@ -307,22 +306,23 @@ namespace ValueFlow
value.valueType = Value::ValueType::INT;
setTokenValue(next, std::move(value), settings);
} else if (yields == Library::Container::Yield::EMPTY) {
Value v(value);
v.valueType = Value::ValueType::INT;
v.bound = Value::Bound::Point;
const Value::Bound bound = value.bound;
const long long intvalue = value.intvalue;
value.valueType = Value::ValueType::INT;
value.bound = Value::Bound::Point;
if (value.isImpossible()) {
if (value.intvalue == 0)
v.setKnown();
else if ((value.bound == Value::Bound::Upper && value.intvalue > 0) ||
(value.bound == Value::Bound::Lower && value.intvalue < 0)) {
v.intvalue = 0;
v.setKnown();
if (intvalue == 0)
value.setKnown();
else if ((bound == Value::Bound::Upper && intvalue > 0) ||
(bound == Value::Bound::Lower && intvalue < 0)) {
value.intvalue = 0;
value.setKnown();
} else
v.setPossible();
value.setPossible();
} else {
v.intvalue = !v.intvalue;
value.intvalue = !value.intvalue;
}
setTokenValue(next, std::move(v), settings);
setTokenValue(next, std::move(value), settings);
}
return;
}
Expand All @@ -346,27 +346,26 @@ namespace ValueFlow
setTokenValue(parent, std::move(value), settings);
return;
}
Value pvalue = value;
if (!value.subexpressions.empty() && Token::Match(parent, ". %var%")) {
if (contains(value.subexpressions, parent->strAt(1)))
pvalue.subexpressions.clear();
value.subexpressions.clear();
else
return;
}
if (parent->isUnaryOp("&")) {
pvalue.indirect++;
setTokenValue(parent, std::move(pvalue), settings);
value.indirect++;
setTokenValue(parent, std::move(value), settings);
} else if (Token::Match(parent, ". %var%") && parent->astOperand1() == tok && parent->astOperand2()) {
if (parent->originalName() == "->" && pvalue.indirect > 0)
pvalue.indirect--;
setTokenValue(parent->astOperand2(), std::move(pvalue), settings);
if (parent->originalName() == "->" && value.indirect > 0)
value.indirect--;
setTokenValue(parent->astOperand2(), std::move(value), settings);
} else if (Token::Match(parent->astParent(), ". %var%") && parent->astParent()->astOperand1() == parent) {
if (parent->astParent()->originalName() == "->" && pvalue.indirect > 0)
pvalue.indirect--;
setTokenValue(parent->astParent()->astOperand2(), std::move(pvalue), settings);
} else if (parent->isUnaryOp("*") && pvalue.indirect > 0) {
pvalue.indirect--;
setTokenValue(parent, std::move(pvalue), settings);
if (parent->astParent()->originalName() == "->" && value.indirect > 0)
value.indirect--;
setTokenValue(parent->astParent()->astOperand2(), std::move(value), settings);
} else if (parent->isUnaryOp("*") && value.indirect > 0) {
value.indirect--;
setTokenValue(parent, std::move(value), settings);
}
return;
}
Expand All @@ -382,7 +381,7 @@ namespace ValueFlow
&& getSizeOf(*tok->valueType(), settings, ValueFlow::Accuracy::ExactOrZero)
>= getSizeOf(valueType, settings, ValueFlow::Accuracy::ExactOrZero))
return;
setTokenValueCast(parent, valueType, value, settings);
setTokenValueCast(parent, valueType, std::move(value), settings);
}

else if (parent->str() == ":") {
Expand Down Expand Up @@ -422,11 +421,10 @@ namespace ValueFlow
if (ret)
return;

Value v(std::move(value));
v.conditional = true;
v.changeKnownToPossible();
value.conditional = true;
value.changeKnownToPossible();

setTokenValue(parent, std::move(v), settings);
setTokenValue(parent, std::move(value), settings);
}
}

Expand Down Expand Up @@ -718,15 +716,13 @@ namespace ValueFlow
std::vector<const Token*> args = getArguments(value.tokvalue);
if (const Library::Function* f = settings.library.getFunction(parent->previous())) {
if (f->containerYield == Library::Container::Yield::SIZE) {
Value v(std::move(value));
v.valueType = Value::ValueType::INT;
v.intvalue = args.size();
setTokenValue(parent, std::move(v), settings);
value.valueType = Value::ValueType::INT;
value.intvalue = args.size();
setTokenValue(parent, std::move(value), settings);
} else if (f->containerYield == Library::Container::Yield::EMPTY) {
Value v(std::move(value));
v.intvalue = args.empty();
v.valueType = Value::ValueType::INT;
setTokenValue(parent, std::move(v), settings);
value.intvalue = args.empty();
value.valueType = Value::ValueType::INT;
setTokenValue(parent, std::move(value), settings);
}
}
}
Expand Down
Loading