Jlm
ipgraph-module.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #ifndef JLM_LLVM_IR_IPGRAPH_MODULE_HPP
7 #define JLM_LLVM_IR_IPGRAPH_MODULE_HPP
8 
10 #include <jlm/llvm/ir/ipgraph.hpp>
11 #include <jlm/llvm/ir/tac.hpp>
12 
13 #include <jlm/util/file.hpp>
14 
15 namespace jlm::llvm
16 {
17 
18 class GlobalValue final : public GlobalVariable
19 {
20 public:
21  ~GlobalValue() noexcept override;
22 
24  : GlobalVariable(node->Type(), node->name()),
25  node_(node)
26  {}
27 
28  GlobalValue(const GlobalValue &) = delete;
29 
30  GlobalValue(GlobalValue &&) = delete;
31 
32  GlobalValue &
33  operator=(const GlobalValue &) = delete;
34 
35  GlobalValue &
36  operator=(GlobalValue &&) = delete;
37 
38  DataNode *
39  node() const noexcept
40  {
41  return node_;
42  }
43 
44 private:
46 };
47 
48 static inline std::unique_ptr<GlobalValue>
50 {
51  return std::make_unique<GlobalValue>(node);
52 }
53 
55 {
56  typedef std::unordered_set<const GlobalValue *>::const_iterator const_iterator;
57 
58 public:
59  ~InterProceduralGraphModule() noexcept = default;
60 
62  const jlm::util::FilePath & source_filename,
63  const std::string & target_triple,
64  const std::string & data_layout) noexcept
68  {}
69 
71  ipgraph() noexcept
72  {
73  return clg_;
74  }
75 
76  const InterProceduralGraph &
77  ipgraph() const noexcept
78  {
79  return clg_;
80  }
81 
83  begin() const
84  {
85  return globals_.begin();
86  }
87 
89  end() const
90  {
91  return globals_.end();
92  }
93 
94  GlobalValue *
96  {
97  auto v = llvm::create_gblvalue(node);
98  auto ptr = v.get();
99  globals_.insert(ptr);
100  functions_[node] = ptr;
101  variables_.insert(std::move(v));
102  return ptr;
103  }
104 
105  inline llvm::Variable *
106  create_variable(std::shared_ptr<const jlm::rvsdg::Type> type, const std::string & name)
107  {
108  auto v = std::make_unique<llvm::Variable>(std::move(type), name);
109  auto pv = v.get();
110  variables_.insert(std::move(v));
111  return pv;
112  }
113 
114  inline llvm::Variable *
115  create_variable(std::shared_ptr<const jlm::rvsdg::Type> type)
116  {
117  static uint64_t c = 0;
118  auto v = std::make_unique<llvm::Variable>(std::move(type), jlm::util::strfmt("v", c++));
119  auto pv = v.get();
120  variables_.insert(std::move(v));
121  return pv;
122  }
123 
124  inline llvm::Variable *
126  {
127  JLM_ASSERT(!variable(node));
128 
129  auto v = std::unique_ptr<Variable>(new FunctionVariable(node));
130  auto pv = v.get();
131  functions_[node] = pv;
132  variables_.insert(std::move(v));
133  return pv;
134  }
135 
136  const llvm::Variable *
137  variable(const InterProceduralGraphNode * node) const noexcept
138  {
139  auto it = functions_.find(node);
140  return it != functions_.end() ? it->second : nullptr;
141  }
142 
143  const jlm::util::FilePath &
144  source_filename() const noexcept
145  {
146  return source_filename_;
147  }
148 
149  inline const std::string &
150  target_triple() const noexcept
151  {
152  return target_triple_;
153  }
154 
155  inline const std::string &
156  data_layout() const noexcept
157  {
158  return data_layout_;
159  }
160 
161  static std::unique_ptr<InterProceduralGraphModule>
163  const jlm::util::FilePath & sourceFilename,
164  const std::string & targetTriple,
165  const std::string & dataLayout)
166  {
167  return std::make_unique<InterProceduralGraphModule>(sourceFilename, targetTriple, dataLayout);
168  }
169 
170 private:
172  std::string data_layout_;
173  std::string target_triple_;
175  std::unordered_set<const GlobalValue *> globals_;
176  std::unordered_set<std::unique_ptr<llvm::Variable>> variables_;
177  std::unordered_map<const InterProceduralGraphNode *, const llvm::Variable *> functions_;
178 };
179 
180 static inline size_t
182 {
183  size_t ntacs = 0;
184  for (const auto & n : im.ipgraph())
185  {
186  auto f = dynamic_cast<const FunctionNode *>(&n);
187  if (!f)
188  continue;
189 
190  auto cfg = f->cfg();
191  if (!cfg)
192  continue;
193 
194  for (const auto & node : *f->cfg())
195  {
196  if (auto bb = dynamic_cast<const BasicBlock *>(&node))
197  ntacs += bb->tacs().ntacs();
198  }
199  }
200 
201  return ntacs;
202 }
203 
204 }
205 
206 #endif
llvm::ControlFlowGraph * cfg() const noexcept
Definition: ipgraph.hpp:163
GlobalValue & operator=(GlobalValue &&)=delete
GlobalValue(const GlobalValue &)=delete
GlobalValue(GlobalValue &&)=delete
DataNode * node() const noexcept
~GlobalValue() noexcept override
GlobalValue & operator=(const GlobalValue &)=delete
llvm::Variable * create_variable(std::shared_ptr< const jlm::rvsdg::Type > type, const std::string &name)
llvm::Variable * create_variable(FunctionNode *node)
const jlm::util::FilePath & source_filename() const noexcept
~InterProceduralGraphModule() noexcept=default
std::unordered_set< const GlobalValue * >::const_iterator const_iterator
GlobalValue * create_global_value(DataNode *node)
std::unordered_set< std::unique_ptr< llvm::Variable > > variables_
const InterProceduralGraph & ipgraph() const noexcept
const std::string & target_triple() const noexcept
static std::unique_ptr< InterProceduralGraphModule > create(const jlm::util::FilePath &sourceFilename, const std::string &targetTriple, const std::string &dataLayout)
InterProceduralGraph & ipgraph() noexcept
std::unordered_map< const InterProceduralGraphNode *, const llvm::Variable * > functions_
const std::string & data_layout() const noexcept
const llvm::Variable * variable(const InterProceduralGraphNode *node) const noexcept
llvm::Variable * create_variable(std::shared_ptr< const jlm::rvsdg::Type > type)
std::unordered_set< const GlobalValue * > globals_
const jlm::util::FilePath source_filename_
const std::string & name() const noexcept
Definition: variable.hpp:50
const std::shared_ptr< const jlm::rvsdg::Type > Type() const noexcept
Definition: variable.hpp:62
#define JLM_ASSERT(x)
Definition: common.hpp:16
Global memory state passed between functions.
size_t ntacs(const AggregationNode &root)
static std::unique_ptr< GlobalValue > create_gblvalue(DataNode *node)
static std::string type(const Node *n)
Definition: view.cpp:255
static std::string strfmt(Args... args)
Definition: strfmt.hpp:35