Index: flang/lib/Evaluate/fold-implementation.h =================================================================== --- flang/lib/Evaluate/fold-implementation.h +++ flang/lib/Evaluate/fold-implementation.h @@ -1761,7 +1761,12 @@ } auto &operand{x.left()}; if (auto *nn{std::get_if>(&x.left().u)}) { - return std::move(nn->left()); // -(-x) -> x + // -(-x) -> (x) + if (IsVariable(nn->left())) { + return FoldOperation(context, Parentheses{std::move(nn->left())}); + } else { + return std::move(nn->left()); + } } else if (auto value{GetScalarConstantValue(operand)}) { if constexpr (T::category == TypeCategory::Integer) { auto negated{value->Negate()}; @@ -1880,9 +1885,13 @@ if (c->IsZero()) { return std::move(x.left()); } else if (c->CompareSigned(Scalar{1}) == Ordering::Equal) { - return std::move(x.right()); + if (IsVariable(x.right())) { + return FoldOperation(context, Parentheses{std::move(x.right())}); + } else { + return std::move(x.right()); + } } else if (c->CompareSigned(Scalar{-1}) == Ordering::Equal) { - return Expr{Negate{std::move(x.right())}}; + return FoldOperation(context, Negate{std::move(x.right())}); } } } Index: flang/test/Evaluate/rewrite04.f90 =================================================================== --- /dev/null +++ flang/test/Evaluate/rewrite04.f90 @@ -0,0 +1,5 @@ +! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s +! Ensure folding of 1*j is a parenthesized (j) when j is a variable. +call foo(1*j) +!CHECK: CALL foo((j)) +end