llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
func.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <string_view>
8 
9 #include <llvm/ADT/ArrayRef.h>
10 #include <llvm/ADT/DenseMap.h>
11 #include <llvm/ADT/ilist_node.h>
12 #include <llvm/ADT/ilist.h>
13 #include <llvm/Support/raw_ostream.h>
14 
15 #include "core/calling_conv.h"
16 #include "core/global.h"
17 #include "core/symbol_table.h"
18 #include "core/type.h"
19 #include "core/value.h"
20 
21 class Block;
22 class Func;
23 class Prog;
24 
25 
26 
30 class Func final : public llvm::ilist_node_with_parent<Func, Prog>, public Global {
31 public:
33  static constexpr Global::Kind kGlobalKind = Global::Kind::FUNC;
34 
35 public:
38 
40  using iterator = BlockListType::iterator;
41  using reverse_iterator = BlockListType::reverse_iterator;
42  using const_iterator = BlockListType::const_iterator;
43 
44 public:
46  struct StackObject {
48  unsigned Index;
50  unsigned Size;
52  llvm::Align Alignment;
53 
54  StackObject(unsigned index, unsigned size, llvm::Align alignment)
55  : Index(index)
56  , Size(size)
57  , Alignment(alignment)
58  {
59  }
60  };
61 
62  using stack_iterator = std::vector<StackObject>::iterator;
63 
64 public:
68  Func(
69  const std::string_view name,
70  Visibility visibility = Visibility::LOCAL
71  );
72 
76  ~Func() override;
77 
79  unsigned GetID() { return id_; }
80 
82  void removeFromParent() override;
84  void eraseFromParent() override;
85 
87  void AddBlock(Block *block, Block *before = nullptr);
88 
90  Prog *getParent() const { return parent_; }
91 
93  unsigned AddStackObject(unsigned index, unsigned size, llvm::Align align);
95  void RemoveStackObject(unsigned index);
96 
98  void SetCallingConv(CallingConv conv) { callConv_ = conv; }
100  CallingConv GetCallingConv() const { return callConv_; }
101 
103  void SetVarArg(bool varArg = true) { varArg_ = varArg; }
105  bool IsVarArg() const { return varArg_; }
106 
108  void SetAlignment(llvm::Align align) { align_ = align; }
110  std::optional<llvm::Align> GetAlignment() const override { return align_; }
111 
113  bool IsNoInline() const { return noinline_; }
115  void SetNoInline(bool noinline = true) { noinline_ = noinline; }
116 
118  std::string_view GetFeatures() const { return features_; }
119  llvm::StringRef getFeatures() const { return features_; }
120  void SetFeatures(const std::string_view features) { features_ = features; }
121 
123  std::string_view GetCPU() const { return cpu_; }
124  llvm::StringRef getCPU() const { return cpu_; }
125  void SetCPU(const std::string_view cpu) { cpu_ = cpu; }
126 
128  std::string_view GetTuneCPU() const { return tuneCPU_; }
129  llvm::StringRef getTuneCPU() const { return tuneCPU_; }
130  void SetTuneCPU(const std::string_view tuneCPU) { tuneCPU_ = tuneCPU; }
131 
133  void SetParameters(const std::vector<FlaggedType> &params) { params_ = params; }
135  llvm::ArrayRef<FlaggedType> params() const { return params_; }
137  unsigned GetNumParams() const { return params_.size(); }
138 
140  void SetPersonality(Global *func);
143 
145  llvm::ArrayRef<StackObject> objects() const { return objects_; }
146 
148  StackObject &object(unsigned I)
149  {
150  auto it = objectIndices_.find(I);
151  assert(it != objectIndices_.end() && "missing stack object");
152  return objects_[it->second];
153  }
155  const StackObject &object(unsigned I) const
156  {
157  return const_cast<Func *>(this)->object(I);
158  }
159 
161  void remove(iterator it);
163  void erase(iterator it);
165  void insertAfter(iterator it, Block *block);
167  void insert(iterator it, Block *block);
169  void clear();
170 
172  bool empty() const { return blocks_.empty(); }
173 
175  size_t size() const { return blocks_.size(); }
176 
178  bool HasAddressTaken() const;
180  bool IsEntry() const { return IsRoot() || HasAddressTaken(); }
182  bool DoesNotReturn() const;
184  bool HasRaise() const;
186  bool HasVAStart() const;
188  bool HasIndirectCalls() const;
189 
191  Block &getEntryBlock();
192  const Block &getEntryBlock() const {
193  return const_cast<Func *>(this)->getEntryBlock();
194  }
195 
196  // Iterator over the blocks.
197  iterator begin() { return blocks_.begin(); }
198  iterator end() { return blocks_.end(); }
199  const_iterator begin() const { return blocks_.begin(); }
200  const_iterator end() const { return blocks_.end(); }
201 
202  // Reverse iterator over the blocks.
203  reverse_iterator rbegin() { return blocks_.rbegin(); }
204  reverse_iterator rend() { return blocks_.rend(); }
205 
206  // Counts the number of instructions.
207  size_t inst_size() const;
208 
210  void RemoveUnreachable();
211 
213  Prog *getProg() override { return parent_; }
214 
216  void dump(llvm::raw_ostream &os = llvm::errs()) const override;
217 
218 private:
219  friend struct SymbolTableListTraits<Func>;
220  friend struct SymbolTableListTraits<Block>;
222  void setParent(Prog *parent) { parent_ = parent; }
223 
224  static BlockListType Func::*getSublistAccess(Block *) { return &Func::blocks_; }
225 
226 private:
228  unsigned id_;
230  Prog *parent_;
232  BlockListType blocks_;
234  CallingConv callConv_;
236  std::vector<FlaggedType> params_;
238  std::vector<StackObject> objects_;
240  llvm::DenseMap<unsigned, unsigned> objectIndices_;
242  bool varArg_;
244  std::optional<llvm::Align> align_;
246  bool noinline_;
248  std::string features_;
250  std::string cpu_;
252  std::string tuneCPU_;
253 };
Func::remove
void remove(iterator it)
Removes a block.
Definition: func.cpp:65
Func::StackObject::Size
unsigned Size
Size of the object on the stack.
Definition: func.h:50
Func::getProg
Prog * getProg() override
Returns the program to which the function belongs.
Definition: func.h:213
Func::AddBlock
void AddBlock(Block *block, Block *before=nullptr)
Adds a new basic block.
Definition: func.cpp:181
Func
Definition: func.h:30
Func::IsNoInline
bool IsNoInline() const
Checks if the function can be inlined.
Definition: func.h:113
Func::object
StackObject & object(unsigned I)
Finds a stack object by index.
Definition: func.h:148
SymbolTableList< Block >
Func::HasRaise
bool HasRaise() const
Checks if the function has a raise instruction.
Definition: func.cpp:136
Func::StackObject
Type of stack objects.
Definition: func.h:46
ConstRef
Definition: ref.h:83
Func::GetPersonality
ConstRef< Global > GetPersonality() const
Return the personality routine.
Definition: func.cpp:43
Func::GetID
unsigned GetID()
Returns the unique ID.
Definition: func.h:79
Func::Func
Func(const std::string_view name, Visibility visibility=Visibility::LOCAL)
Definition: func.cpp:19
Func::SetNoInline
void SetNoInline(bool noinline=true)
Prevents the function from being inlined.
Definition: func.h:115
Func::params
llvm::ArrayRef< FlaggedType > params() const
Returns the list of arguments.
Definition: func.h:135
Global::IsRoot
bool IsRoot() const
Checks if the symbol can be externally referenced.
Definition: global.cpp:30
Global::Kind
Kind
Enumeration of global kinds.
Definition: global.h:30
Func::insert
void insert(iterator it, Block *block)
Adds a block before another.
Definition: func.cpp:83
Func::AddStackObject
unsigned AddStackObject(unsigned index, unsigned size, llvm::Align align)
Adds a stack object.
Definition: func.cpp:191
Func::IsVarArg
bool IsVarArg() const
Returns the vararg flags.
Definition: func.h:105
Func::insertAfter
void insertAfter(iterator it, Block *block)
Adds a block.
Definition: func.cpp:77
Func::SetPersonality
void SetPersonality(Global *func)
Set the personality routine.
Definition: func.cpp:36
Func::HasVAStart
bool HasVAStart() const
Checks if the function has a va_start instruction.
Definition: func.cpp:149
Func::size
size_t size() const
Returns the size of the function.
Definition: func.h:175
SymbolTableListTraits
Definition: symbol_table.h:33
Func::SetAlignment
void SetAlignment(llvm::Align align)
Sets the alignment of the function.
Definition: func.h:108
Func::SetParameters
void SetParameters(const std::vector< FlaggedType > &params)
Sets the number of fixed parameters.
Definition: func.h:133
Func::~Func
~Func() override
Definition: func.cpp:31
Func::GetCallingConv
CallingConv GetCallingConv() const
Returns the calling convention.
Definition: func.h:100
Func::GetAlignment
std::optional< llvm::Align > GetAlignment() const override
Returns the alignment of a function.
Definition: func.h:110
Func::removeFromParent
void removeFromParent() override
Removes an instruction from the parent.
Definition: func.cpp:53
Func::iterator
BlockListType::iterator iterator
Iterator over the blocks.
Definition: func.h:40
Func::erase
void erase(iterator it)
Erases a block.
Definition: func.cpp:71
Func::IsEntry
bool IsEntry() const
Checks if the function must be present even without uses.
Definition: func.h:180
Func::GetTuneCPU
std::string_view GetTuneCPU() const
Returns the CPU to tune for.
Definition: func.h:128
Func::SetVarArg
void SetVarArg(bool varArg=true)
Sets the vararg flag.
Definition: func.h:103
Func::RemoveUnreachable
void RemoveUnreachable()
Removes unreachable blocks.
Definition: func.cpp:228
Func::getEntryBlock
Block & getEntryBlock()
Returns the entry block.
Definition: func.cpp:175
Func::dump
void dump(llvm::raw_ostream &os=llvm::errs()) const override
Dumps the representation of the function.
Definition: func.cpp:267
Func::GetCPU
std::string_view GetCPU() const
Returns the CPU to compile for.
Definition: func.h:123
Prog
Definition: prog.h:33
Func::HasIndirectCalls
bool HasIndirectCalls() const
Checks if the function has indirect calls.
Definition: func.cpp:162
Func::StackObject::Alignment
llvm::Align Alignment
Alignment of the object, in bytes.
Definition: func.h:52
Func::GetNumParams
unsigned GetNumParams() const
Returns the number of parameters.
Definition: func.h:137
Func::GetFeatures
std::string_view GetFeatures() const
Returns the function-specific target features.
Definition: func.h:118
Func::StackObject::Index
unsigned Index
Index of the stack object.
Definition: func.h:48
Func::RemoveStackObject
void RemoveStackObject(unsigned index)
Removes a stack object.
Definition: func.cpp:202
Func::object
const StackObject & object(unsigned I) const
Finds a stack object by index.
Definition: func.h:155
Block
Definition: block.h:29
Func::objects
llvm::ArrayRef< StackObject > objects() const
Iterator over stack objects.
Definition: func.h:145
Func::clear
void clear()
Clears all blocks.
Definition: func.cpp:89
Func::getParent
Prog * getParent() const
Returns the parent block.
Definition: func.h:90
Func::HasAddressTaken
bool HasAddressTaken() const
Checks if the function can be used indirectly.
Definition: func.cpp:98
Func::empty
bool empty() const
Checks if the function has any blocks.
Definition: func.h:172
Func::kGlobalKind
static constexpr Global::Kind kGlobalKind
Kind of the global.
Definition: func.h:33
Func::SetCallingConv
void SetCallingConv(CallingConv conv)
Sets the calling convention.
Definition: func.h:98
Func::BlockListType
SymbolTableList< Block > BlockListType
Type of the block list.
Definition: func.h:37
Func::eraseFromParent
void eraseFromParent() override
Removes a function from the program.
Definition: func.cpp:59
Global
Definition: global.h:23
Func::DoesNotReturn
bool DoesNotReturn() const
Checks if the function never returns.
Definition: func.cpp:123