Jlm
CfgStructureTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #include <gtest/gtest.h>
7 
12 #include <jlm/rvsdg/TestType.hpp>
13 
14 TEST(ControlFlowGraphStructureTests, test_straightening)
15 {
16  using namespace jlm::llvm;
17  using namespace jlm::rvsdg;
18 
21 
22  ControlFlowGraph cfg(module);
23  auto bb1 = BasicBlock::create(cfg);
24  auto bb2 = BasicBlock::create(cfg);
25  auto bb3 = BasicBlock::create(cfg);
26 
27  cfg.exit()->divert_inedges(bb1);
28  bb1->add_outedge(bb2);
29  bb2->add_outedge(bb3);
30  bb3->add_outedge(cfg.exit());
31 
32  auto arg = cfg.entry()->append_argument(Argument::create("arg", vt));
33  bb1->append_last(ThreeAddressCode::create(TestOperation::create({ vt }, { vt }), { arg }));
34  bb2->append_last(ThreeAddressCode::create(TestOperation::create({ vt }, { vt }), { arg }));
35  bb3->append_last(ThreeAddressCode::create(TestOperation::create({ vt }, { vt }), { arg }));
36 
37  auto bb3_last = static_cast<const BasicBlock *>(bb3)->tacs().last();
38  straighten(cfg);
39 
40  EXPECT_EQ(cfg.nnodes(), 1u);
41  auto node = cfg.entry()->OutEdge(0)->sink();
42 
43  EXPECT_TRUE(is<BasicBlock>(node));
44  auto & tacs = static_cast<const BasicBlock *>(node)->tacs();
45  EXPECT_EQ(tacs.ntacs(), 3u);
46  EXPECT_EQ(tacs.last(), bb3_last);
47 }
48 
49 TEST(ControlFlowGraphStructureTests, test_is_structured)
50 {
51  using namespace jlm::llvm;
52 
54 
55  ControlFlowGraph cfg(module);
56  auto split = BasicBlock::create(cfg);
57  auto bb = BasicBlock::create(cfg);
58  auto join = BasicBlock::create(cfg);
59 
60  cfg.exit()->divert_inedges(split);
61  split->add_outedge(join);
62  split->add_outedge(bb);
63  bb->add_outedge(join);
64  join->add_outedge(cfg.exit());
65 
66  std::cout << ControlFlowGraph::ToAscii(cfg) << std::flush;
67  EXPECT_TRUE(is_structured(cfg));
68 }
TEST(ControlFlowGraphStructureTests, test_straightening)
static const auto vt
Definition: PullTests.cpp:16
static std::unique_ptr< Argument > create(const std::string &name, std::shared_ptr< const jlm::rvsdg::Type > type, const AttributeSet &attributes)
Definition: cfg.hpp:59
ThreeAddressCode * last() const noexcept
Definition: basic-block.hpp:86
static BasicBlock * create(ControlFlowGraph &cfg)
Definition: basic-block.cpp:37
ControlFlowGraphNode * sink() const noexcept
Definition: cfg-node.hpp:57
ControlFlowGraphEdge * OutEdge(size_t n) const
Definition: cfg-node.hpp:115
void divert_inedges(llvm::ControlFlowGraphNode *new_successor)
Definition: cfg-node.hpp:171
static std::string ToAscii(const ControlFlowGraph &controlFlowGraph)
Definition: cfg.cpp:151
EntryNode * entry() const noexcept
Definition: cfg.hpp:206
ExitNode * exit() const noexcept
Definition: cfg.hpp:212
size_t nnodes() const noexcept
Definition: cfg.hpp:241
llvm::Argument * append_argument(std::unique_ptr< llvm::Argument > arg)
Definition: cfg.hpp:100
static std::unique_ptr< llvm::ThreeAddressCode > create(std::unique_ptr< rvsdg::SimpleOperation > operation, const std::vector< const Variable * > &operands)
Definition: tac.hpp:135
static std::shared_ptr< const TestType > createValueType()
Definition: TestType.cpp:67
Global memory state passed between functions.
bool is_structured(const ControlFlowGraph &cfg)
void straighten(ControlFlowGraph &cfg)