[flang] Implement constant folding for the NOT intrinsic
authorPeter Steinfeld <psteinfeld@nvidia.com>
Sat, 19 Jun 2021 02:18:27 +0000 (19:18 -0700)
committerPeter Steinfeld <psteinfeld@nvidia.com>
Sun, 20 Jun 2021 14:25:05 +0000 (07:25 -0700)
I implemented constant folding for the NOT intrinsic and added some tests.

Differential Revision: https://reviews.llvm.org/D104587

flang/lib/Evaluate/fold-integer.cpp
flang/test/Evaluate/folding01.f90

index 19b9f92..01f30b0 100644 (file)
@@ -523,6 +523,9 @@ Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
               }
               return result.value;
             }));
+  } else if (name == "not") {
+    return FoldElementalIntrinsic<T, T>(
+        context, std::move(funcRef), &Scalar<T>::NOT);
   } else if (name == "precision") {
     if (const auto *cx{UnwrapExpr<Expr<SomeReal>>(args[0])}) {
       return Expr<T>{std::visit(
@@ -657,7 +660,7 @@ Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
   // TODO:
   // cshift, dot_product, eoshift,
   // findloc, iall, iany, iparity, ibits, image_status, ishftc,
-  // matmul, maxloc, minloc, not, pack, product, reduce,
+  // matmul, maxloc, minloc, pack, product, reduce,
   // sign, spread, sum, transfer, transpose, unpack
   return Expr<T>{std::move(funcRef)};
 }
index 29708ce..fddc034 100644 (file)
@@ -135,4 +135,8 @@ module m
   logical, parameter :: test_max_a1 = all(max(x1a, x2a).EQ.[11, 12, 13, 14])
   logical, parameter :: test_min_a1 = all(min(x1a, x2a).EQ.[1, 2, 3, 4])
 
+  logical, parameter :: test_not_zero = not(0).EQ.-1
+  logical, parameter :: test_not_neg_one = not(-1).EQ.0
+  logical, parameter :: test_not_array = all(not([5, 6, 7]).EQ.[-6, -7, -8])
 end module