[flang] various code cleanups from QualifiedType reviews
authorEric Schweitz <eschweitz@nvidia.com>
Thu, 11 Apr 2019 19:04:18 +0000 (12:04 -0700)
committerEric Schweitz <eschweitz@nvidia.com>
Fri, 12 Apr 2019 16:21:29 +0000 (09:21 -0700)
Original-commit: flang-compiler/f18@b17f24da8fa85b7e7de8c54f430ba5d8299e53b2
Reviewed-on: https://github.com/flang-compiler/f18/pull/408
Tree-same-pre-rewrite: false

flang/lib/FIR/builder.h
flang/lib/FIR/procedure.cc
flang/lib/FIR/statements.cc
flang/lib/FIR/statements.h

index 0a4b41c08793cc5533d2b28515be1de58084066a..53b8d9a44802998a456c0412b9eb0b719ce1548f 100644 (file)
@@ -27,14 +27,14 @@ struct FIRBuilder {
 
   template<typename A> Statement *Insert(A &&s) {
     CHECK(GetInsertionPoint());
-    auto *statement{new Statement(GetInsertionPoint(), std::forward<A>(s))};
+    auto *statement{Statement::Create(GetInsertionPoint(), std::forward<A>(s))};
     return statement;
   }
 
   template<typename A, typename B> QualifiedStmt<A> QualifiedInsert(B &&s) {
     CHECK(GetInsertionPoint());
-    auto *statement{new Statement(GetInsertionPoint(), std::forward<B>(s))};
-    return QualifiedStmt<A>{statement, s};
+    auto *statement{Statement::Create(GetInsertionPoint(), std::forward<B>(s))};
+    return MakeQualifiedStmt<A, B>(statement);
   }
 
   template<typename A> Statement *InsertTerminator(A &&s) {
index ad48312b092be181c4679513a6f12c0f42b462ae..1bc3100e50a4384e51791a8c04952b93638285f4 100644 (file)
@@ -41,7 +41,7 @@ static void AddCountScopes(
     unsigned count, BasicBlock *block, T callback, semantics::Scope *scope) {
   for (; count; --count) {
     block->insertBefore(
-        new Statement(block, callback(scope)), block->terminator());
+        Statement::Create(block, callback(scope)), block->terminator());
   }
 }
 
index 72ca2aa43ef4a5f3c4ed13a322fa8b7c40ba078d..0f7fc7c80b042f1a4d3a9f0857a5b54d5a57e39b 100644 (file)
@@ -125,38 +125,11 @@ LoadInsn::LoadInsn(Statement *addr) : address_{addr} {
   CHECK(GetAddressable(addr));
 }
 
-// Store ctors
-StoreInsn::StoreInsn(QualifiedStmt<Addressable_impl> addr, BasicBlock *val)
-  : address_{addr}, value_{val} {
-  CHECK(address_);
-  CHECK(val);
-}
+// Store ctor
 StoreInsn::StoreInsn(QualifiedStmt<Addressable_impl> addr, Value val)
   : address_{addr}, value_{val} {
   CHECK(address_);
-}
-StoreInsn::StoreInsn(
-    QualifiedStmt<Addressable_impl> addr, QualifiedStmt<ApplyExprStmt> val)
-  : address_{addr}, value_{val} {
-  CHECK(address_);
-  CHECK(val);
-}
-StoreInsn::StoreInsn(
-    QualifiedStmt<Addressable_impl> addr, QualifiedStmt<Addressable_impl> val)
-  : address_{addr}, value_{val} {
-  CHECK(address_);
-  CHECK(val);
-}
-
-static std::string dumpStoreValue(const StoreInsn::ValueType &v) {
-  return std::visit(
-      common::visitors{
-          [](const Value &v) { return v.dump(); },
-          [](const ApplyExprStmt *e) { return FIR::dump(e->expression()); },
-          [](const Addressable_impl *e) { return FIR::dump(e->address()); },
-          [](const BasicBlock *bb) { return ToString(bb); },
-      },
-      v);
+  CHECK(!IsNothing(value_));
 }
 
 // dump is intended for debugging rather than idiomatic FIR output
@@ -210,7 +183,7 @@ std::string Statement::dump() const {
             return '%' + ToString(&u) + ": load " + insn.address().dump();
           },
           [](const StoreInsn &insn) {
-            std::string value{dumpStoreValue(insn.value())};
+            std::string value{insn.value().dump()};
             return "store " + value + " to " +
                 FIR::dump(insn.address()->address());
           },
index e41bbff6fd434bc0e9addaddd8da8f827574e666..764a6e5ac2be45aa1c4d81fefd4b9416ef4f50d3 100644 (file)
@@ -56,11 +56,10 @@ public:
 
 // Some uses of a Statement should be constrained.  These contraints are imposed
 // at compile time.
-template<typename A = Stmt_impl> class QualifiedStmt {
+template<typename A> class QualifiedStmt {
 public:
   QualifiedStmt() = delete;
-  template<typename B, std::enable_if_t<std::is_base_of_v<A, B>, int> = 0>
-  QualifiedStmt(Statement *stmt, const B &) : stmt{stmt} {}
+  QualifiedStmt(Statement *stmt) : stmt{stmt} {}
 
   // create a stub, where stmt == nullptr
   QualifiedStmt(std::nullptr_t) : stmt{nullptr} {}
@@ -71,6 +70,12 @@ public:
   Statement *stmt;
 };
 
+template<typename A, typename B,
+    std::enable_if_t<std::is_base_of_v<A, B>, int> = 0>
+QualifiedStmt<A> MakeQualifiedStmt(Statement *s) {
+  return QualifiedStmt<A>{s};
+}
+
 // Every basic block must end in a terminator
 class TerminatorStmt_impl : virtual public Stmt_impl {
 public:
@@ -401,27 +406,18 @@ private:
 // Store value(s) from an applied expression to a location
 class StoreInsn : public MemoryStmt_impl {
 public:
-  using ValueType = std::variant<Value, QualifiedStmt<ApplyExprStmt>,
-      QualifiedStmt<Addressable_impl>, BasicBlock *>;
-
-  template<typename A>
-  static StoreInsn Create(QualifiedStmt<Addressable_impl> addr, A value) {
+  static StoreInsn Create(QualifiedStmt<Addressable_impl> addr, Value value) {
     return StoreInsn{addr, value};
   }
 
   Addressable_impl *address() const { return address_; }
-  ValueType value() const { return value_; }
+  Value value() const { return value_; }
 
 private:
   explicit StoreInsn(QualifiedStmt<Addressable_impl> addr, Value val);
-  explicit StoreInsn(
-      QualifiedStmt<Addressable_impl> addr, QualifiedStmt<ApplyExprStmt> val);
-  explicit StoreInsn(QualifiedStmt<Addressable_impl> addr,
-      QualifiedStmt<Addressable_impl> val);
-  explicit StoreInsn(QualifiedStmt<Addressable_impl> addr, BasicBlock *val);
 
   QualifiedStmt<Addressable_impl> address_;
-  ValueType value_;
+  Value value_;
 };
 
 // NULLIFY - make pointer object disassociated
@@ -575,12 +571,19 @@ class Statement : public SumTypeMixin<ReturnStmt,  //
                   public ChildMixin<Statement, BasicBlock>,
                   public llvm::ilist_node<Statement> {
 public:
+  template<typename A> static Statement *Create(BasicBlock *p, A &&t) {
+    return new Statement(p, std::forward<A>(t));
+  }
+  std::string dump() const;  // LLVM expected name
+
+  void Dump(std::ostream &os) const { os << dump(); }
+
+private:
   template<typename A>
-  Statement(BasicBlock *p, A &&t)
+  explicit Statement(BasicBlock *p, A &&t)
     : SumTypeMixin{std::forward<A>(t)}, ChildMixin{p} {
     parent->insertBefore(this);
   }
-  std::string dump() const;
 };
 
 template<typename A> inline QualifiedStmt<A>::operator A *() const {