From 00a3ccd91db71b26a6d36208bd5546097857ac4e Mon Sep 17 00:00:00 2001 From: Eric Schweitz Date: Tue, 19 Mar 2019 09:07:11 -0700 Subject: [PATCH] [flang] FIR: more code cleanup 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 | 33 +++++++++++++++++---------------- flang/lib/FIR/statements.h | 24 +++++++++++++++--------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/flang/lib/FIR/statements.cc b/flang/lib/FIR/statements.cc index 8cab875..41a3ad3 100644 --- a/flang/lib/FIR/statements.cc +++ b/flang/lib/FIR/statements.cc @@ -52,8 +52,8 @@ static std::list 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 SwitchRankStmt::succ_blocks() const { return SuccBlocks(valueSuccPairs_); } -template 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(branch.getTrueSucc())) + diff --git a/flang/lib/FIR/statements.h b/flang/lib/FIR/statements.h index 5e94266..b7e8498 100644 --- a/flang/lib/FIR/statements.h +++ b/flang/lib/FIR/statements.h @@ -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 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 address_; + Value address_; }; // Store value(s) from an applied expression to a location @@ -585,8 +589,10 @@ public: return reinterpret_cast(&static_cast(nullptr)->u); } static Statement *From(Stmt_impl *stmt) { - return reinterpret_cast( - reinterpret_cast(stmt) - Statement::offsetof_impl()); + char *cp{reinterpret_cast(stmt)}; + auto adjust{Statement::offsetof_impl()}; + CHECK(adjust == 0); + return reinterpret_cast(cp - adjust); } }; @@ -599,8 +605,8 @@ inline std::list 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) { -- 2.7.4