[flang-commits] [flang] 47c998c - [flang] Improve handling of NULL pointers when folding components of named constants

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Sep 23 09:23:08 PDT 2022


Author: Peter Klausler
Date: 2022-09-23T09:22:52-07:00
New Revision: 47c998cc2e3fea8047868c0394b752d9d63c2329

URL: https://github.com/llvm/llvm-project/commit/47c998cc2e3fea8047868c0394b752d9d63c2329
DIFF: https://github.com/llvm/llvm-project/commit/47c998cc2e3fea8047868c0394b752d9d63c2329.diff

LOG: [flang] Improve handling of NULL pointers when folding components of named constants

When a component reference to a named constant of derived type should
fold down to NULL() without a MOLD=, do so.

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

Added: 
    

Modified: 
    flang/include/flang/Evaluate/tools.h
    flang/lib/Evaluate/fold.cpp
    flang/lib/Evaluate/tools.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Evaluate/tools.h b/flang/include/flang/Evaluate/tools.h
index 70c94557f3e24..cb5618ab7bc1f 100644
--- a/flang/include/flang/Evaluate/tools.h
+++ b/flang/include/flang/Evaluate/tools.h
@@ -938,7 +938,7 @@ bool IsAllocatableDesignator(const Expr<SomeType> &);
 bool IsProcedure(const Expr<SomeType> &);
 bool IsFunction(const Expr<SomeType> &);
 bool IsProcedurePointerTarget(const Expr<SomeType> &);
-bool IsBareNullPointer(const Expr<SomeType> *); // NULL() w/o MOLD=
+bool IsBareNullPointer(const Expr<SomeType> *); // NULL() w/o MOLD= or type
 bool IsNullObjectPointer(const Expr<SomeType> &);
 bool IsNullProcedurePointer(const Expr<SomeType> &);
 bool IsNullPointer(const Expr<SomeType> &);

diff  --git a/flang/lib/Evaluate/fold.cpp b/flang/lib/Evaluate/fold.cpp
index 72257a3b89590..2f4ba2ae5ee1d 100644
--- a/flang/lib/Evaluate/fold.cpp
+++ b/flang/lib/Evaluate/fold.cpp
@@ -72,7 +72,11 @@ Expr<SomeDerived> FoldOperation(
   for (auto &&[symbol, value] : std::move(structure)) {
     auto expr{Fold(context, std::move(value.value()))};
     if (IsPointer(symbol)) {
-      if (IsProcedure(symbol)) {
+      if (IsNullPointer(expr)) {
+        // Handle x%c when x designates a named constant of derived
+        // type and %c is NULL() in that constant.
+        expr = Expr<SomeType>{NullPointer{}};
+      } else if (IsProcedure(symbol)) {
         isConstant &= IsInitialProcedureTarget(expr);
       } else {
         isConstant &= IsInitialDataTarget(expr);

diff  --git a/flang/lib/Evaluate/tools.cpp b/flang/lib/Evaluate/tools.cpp
index 009e8d531f8f6..5836e47c21971 100644
--- a/flang/lib/Evaluate/tools.cpp
+++ b/flang/lib/Evaluate/tools.cpp
@@ -784,11 +784,8 @@ bool IsObjectPointer(const Expr<SomeType> &expr, FoldingContext &context) {
   }
 }
 
-bool IsBareNullPointer(const Expr<SomeType> *expr) {
-  return expr && std::holds_alternative<NullPointer>(expr->u);
-}
+// IsNullPointer() & variations
 
-// IsNullObjectPointetr, IsNullProcedurePointer(), IsNullPointer()
 template <bool IS_PROC_PTR> struct IsNullPointerHelper {
   template <typename A> bool operator()(const A &) const { return false; }
   bool operator()(const ProcedureRef &call) const {
@@ -811,6 +808,27 @@ template <bool IS_PROC_PTR> struct IsNullPointerHelper {
               characteristics::Procedure::Attr::NullPointer);
     }
   }
+  template <typename T> bool operator()(const Designator<T> &x) const {
+    if (const auto *component{std::get_if<Component>(&x.u)}) {
+      if (const auto *baseSym{std::get_if<SymbolRef>(&component->base().u)}) {
+        const Symbol &base{**baseSym};
+        if (const auto *object{
+                base.detailsIf<semantics::ObjectEntityDetails>()}) {
+          // TODO: nested component and array references
+          if (IsNamedConstant(base) && object->init()) {
+            if (auto structCons{
+                    GetScalarConstantValue<SomeDerived>(*object->init())}) {
+              auto iter{structCons->values().find(component->GetLastSymbol())};
+              if (iter != structCons->values().end()) {
+                return (*this)(iter->second.value());
+              }
+            }
+          }
+        }
+      }
+    }
+    return false;
+  }
   bool operator()(const NullPointer &) const { return true; }
   template <typename T> bool operator()(const Parentheses<T> &x) const {
     return (*this)(x.left());
@@ -823,13 +841,19 @@ template <bool IS_PROC_PTR> struct IsNullPointerHelper {
 bool IsNullObjectPointer(const Expr<SomeType> &expr) {
   return IsNullPointerHelper<false>{}(expr);
 }
+
 bool IsNullProcedurePointer(const Expr<SomeType> &expr) {
   return IsNullPointerHelper<true>{}(expr);
 }
+
 bool IsNullPointer(const Expr<SomeType> &expr) {
   return IsNullObjectPointer(expr) || IsNullProcedurePointer(expr);
 }
 
+bool IsBareNullPointer(const Expr<SomeType> *expr) {
+  return expr && std::holds_alternative<NullPointer>(expr->u);
+}
+
 // GetSymbolVector()
 auto GetSymbolVectorHelper::operator()(const Symbol &x) const -> Result {
   if (const auto *details{x.detailsIf<semantics::AssocEntityDetails>()}) {


        


More information about the flang-commits mailing list