[flang-commits] [PATCH] D153457: [flang] Rewrite "1*j" to "(j)", not "j", when j is a variable

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Jun 22 07:23:40 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGfa459d988276: [flang] Rewrite "1*j" to "(j)", not "j", when j is a variable (authored by klausler).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153457/new/

https://reviews.llvm.org/D153457

Files:
  flang/lib/Evaluate/fold-implementation.h
  flang/test/Evaluate/rewrite04.f90


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
Index: flang/lib/Evaluate/fold-implementation.h
===================================================================
--- flang/lib/Evaluate/fold-implementation.h
+++ flang/lib/Evaluate/fold-implementation.h
@@ -1764,7 +1764,12 @@
   }
   auto &operand{x.left()};
   if (auto *nn{std::get_if<Negate<T>>(&x.left().u)}) {
-    return std::move(nn->left()); // -(-x) -> x
+    // -(-x) -> (x)
+    if (IsVariable(nn->left())) {
+      return FoldOperation(context, Parentheses<T>{std::move(nn->left())});
+    } else {
+      return std::move(nn->left());
+    }
   } else if (auto value{GetScalarConstantValue<T>(operand)}) {
     if constexpr (T::category == TypeCategory::Integer) {
       auto negated{value->Negate()};
@@ -1883,9 +1888,13 @@
       if (c->IsZero()) {
         return std::move(x.left());
       } else if (c->CompareSigned(Scalar<T>{1}) == Ordering::Equal) {
-        return std::move(x.right());
+        if (IsVariable(x.right())) {
+          return FoldOperation(context, Parentheses<T>{std::move(x.right())});
+        } else {
+          return std::move(x.right());
+        }
       } else if (c->CompareSigned(Scalar<T>{-1}) == Ordering::Equal) {
-        return Expr<T>{Negate<T>{std::move(x.right())}};
+        return FoldOperation(context, Negate<T>{std::move(x.right())});
       }
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153457.533596.patch
Type: text/x-patch
Size: 1673 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230622/b7aa84fd/attachment.bin>


More information about the flang-commits mailing list