[flang] FIR: more code cleanup
authorEric Schweitz <eschweitz@nvidia.com>
Tue, 19 Mar 2019 16:07:11 +0000 (09:07 -0700)
committerEric <eschweitz@nvidia.com>
Sat, 23 Mar 2019 18:14:20 +0000 (11:14 -0700)
Original-commit: flang-compiler/f18@eaac944aca994e64d970506c426c222d28e680b5
Reviewed-on: https://github.com/flang-compiler/f18/pull/354
Tree-same-pre-rewrite: false

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

index 8cab875..41a3ad3 100644 (file)
@@ -52,8 +52,8 @@ static std::list<BasicBlock *> SuccBlocks(
   return result.second;
 }
 
-ReturnStmt::ReturnStmt(Statement *exp) : returnValue_{GetApplyExpr(exp)} {
-  CHECK(returnValue_);
+ReturnStmt::ReturnStmt(Statement *exp) : value_{GetApplyExpr(exp)} {
+  CHECK(value_);
 }
 
 SwitchStmt::SwitchStmt(const Value &cond, BasicBlock *defaultBlock,
@@ -96,17 +96,24 @@ std::list<BasicBlock *> SwitchRankStmt::succ_blocks() const {
   return SuccBlocks<SwitchRankStmt>(valueSuccPairs_);
 }
 
-template<typename T> bool PointerNotNull(const T &variant) {
-  return std::visit(
+// check LoadInsn constraints
+static void CheckLoadInsn(const Value &v) {
+  std::visit(
       common::visitors{
-          [](const Addressable_impl *p) { return p != nullptr; },
-          [](const Value &value) { return !IsNothing(value); },
+          [](DataObject *) { /* ok */ },
+          [](Statement *s) { CHECK(GetAddressable(s)); },
+          [](auto) { CHECK(!"invalid load input"); },
       },
-      variant);
+      v.u);
 }
-
-LoadInsn::LoadInsn(Statement *addr) : address_{GetAddressable(addr)} {
-  CHECK(PointerNotNull(address_));
+LoadInsn::LoadInsn(const Value &addr) : address_{addr} {
+  CheckLoadInsn(address_);
+}
+LoadInsn::LoadInsn(Value &&addr) : address_{std::move(addr)} {
+  CheckLoadInsn(address_);
+}
+LoadInsn::LoadInsn(Statement *addr) : address_{addr} {
+  CHECK(GetAddressable(addr));
 }
 
 StoreInsn::StoreInsn(Statement *addr, Statement *val)
@@ -120,7 +127,6 @@ StoreInsn::StoreInsn(Statement *addr, Statement *val)
     value_ = expr;
   }
 }
-
 StoreInsn::StoreInsn(Statement *addr, BasicBlock *val)
   : address_{GetAddressable(addr)}, value_{val} {
   CHECK(address_);
@@ -139,11 +145,6 @@ std::string Statement::dump() const {
           [](const BranchStmt &branch) {
             if (branch.hasCondition()) {
               std::string cond{"???"};
-#if 0
-              if (auto expr{GetApplyExpr(branch.getCond())}) {
-                cond = FIR::dump(expr->expression());
-              }
-#endif
               return "branch (" + cond + ") " +
                   std::to_string(
                       reinterpret_cast<std::intptr_t>(branch.getTrueSucc())) +
index 5e94266..b7e8498 100644 (file)
@@ -68,11 +68,13 @@ public:
 class ReturnStmt : public TerminatorStmt_impl {
 public:
   static ReturnStmt Create(Statement *stmt) { return ReturnStmt{stmt}; }
+  static ReturnStmt Create() { return ReturnStmt{nullptr}; }
   std::list<BasicBlock *> succ_blocks() const override { return {}; }
-  Statement *returnValue() const;
+  bool has_value() const { return value_ != nullptr; }
+  Statement *value() const;
 
 private:
-  ApplyExprStmt *returnValue_;
+  ApplyExprStmt *value_;
   explicit ReturnStmt(Statement *exp);
 };
 
@@ -393,13 +395,15 @@ private:
 // Load value(s) from a location
 class LoadInsn : public MemoryStmt_impl {
 public:
-  static LoadInsn Create(Value addr) { return LoadInsn{addr}; }
+  static LoadInsn Create(const Value &addr) { return LoadInsn{addr}; }
+  static LoadInsn Create(Value &&addr) { return LoadInsn{addr}; }
   static LoadInsn Create(Statement *addr) { return LoadInsn{addr}; }
 
 private:
-  explicit LoadInsn(Value addr) : address_{addr} {}
+  explicit LoadInsn(const Value &addr);
+  explicit LoadInsn(Value &&addr);
   explicit LoadInsn(Statement *addr);
-  std::variant<Addressable_impl *, Value> address_;
+  Value address_;
 };
 
 // Store value(s) from an applied expression to a location
@@ -585,8 +589,10 @@ public:
     return reinterpret_cast<std::size_t>(&static_cast<Statement *>(nullptr)->u);
   }
   static Statement *From(Stmt_impl *stmt) {
-    return reinterpret_cast<Statement *>(
-        reinterpret_cast<char *>(stmt) - Statement::offsetof_impl());
+    char *cp{reinterpret_cast<char *>(stmt)};
+    auto adjust{Statement::offsetof_impl()};
+    CHECK(adjust == 0);
+    return reinterpret_cast<Statement *>(cp - adjust);
   }
 };
 
@@ -599,8 +605,8 @@ inline std::list<BasicBlock *> succ_list(BasicBlock &block) {
   return {};
 }
 
-inline Statement *ReturnStmt::returnValue() const {
-  return Statement::From(returnValue_);
+inline Statement *ReturnStmt::value() const {
+  return Statement::From(value_);
 }
 
 inline ApplyExprStmt *GetApplyExpr(Statement *stmt) {