llir-opt
0.0.1
Low-Level Post-Link Optimiser for OCaml and C
|
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>
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"
30 class Func final :
public llvm::ilist_node_with_parent<Func, Prog>,
public Global {
41 using reverse_iterator = BlockListType::reverse_iterator;
42 using const_iterator = BlockListType::const_iterator;
62 using stack_iterator = std::vector<StackObject>::iterator;
69 const std::string_view name,
70 Visibility visibility = Visibility::LOCAL
79 unsigned GetID() {
return id_; }
103 void SetVarArg(
bool varArg =
true) { varArg_ = varArg; }
110 std::optional<llvm::Align>
GetAlignment()
const override {
return align_; }
119 llvm::StringRef getFeatures()
const {
return features_; }
120 void SetFeatures(
const std::string_view features) { features_ = features; }
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; }
129 llvm::StringRef getTuneCPU()
const {
return tuneCPU_; }
130 void SetTuneCPU(
const std::string_view tuneCPU) { tuneCPU_ = tuneCPU; }
135 llvm::ArrayRef<FlaggedType>
params()
const {
return params_; }
145 llvm::ArrayRef<StackObject>
objects()
const {
return objects_; }
150 auto it = objectIndices_.find(I);
151 assert(it != objectIndices_.end() &&
"missing stack object");
152 return objects_[it->second];
157 return const_cast<Func *
>(
this)->
object(I);
172 bool empty()
const {
return blocks_.empty(); }
175 size_t size()
const {
return blocks_.size(); }
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(); }
203 reverse_iterator rbegin() {
return blocks_.rbegin(); }
204 reverse_iterator rend() {
return blocks_.rend(); }
207 size_t inst_size()
const;
216 void dump(llvm::raw_ostream &os = llvm::errs())
const override;
222 void setParent(
Prog *parent) { parent_ = parent; }
234 CallingConv callConv_;
236 std::vector<FlaggedType> params_;
238 std::vector<StackObject> objects_;
240 llvm::DenseMap<unsigned, unsigned> objectIndices_;
244 std::optional<llvm::Align> align_;
248 std::string features_;
252 std::string tuneCPU_;
void remove(iterator it)
Removes a block.
Definition: func.cpp:65
unsigned Size
Size of the object on the stack.
Definition: func.h:50
Prog * getProg() override
Returns the program to which the function belongs.
Definition: func.h:213
void AddBlock(Block *block, Block *before=nullptr)
Adds a new basic block.
Definition: func.cpp:181
bool IsNoInline() const
Checks if the function can be inlined.
Definition: func.h:113
StackObject & object(unsigned I)
Finds a stack object by index.
Definition: func.h:148
bool HasRaise() const
Checks if the function has a raise instruction.
Definition: func.cpp:136
Type of stack objects.
Definition: func.h:46
ConstRef< Global > GetPersonality() const
Return the personality routine.
Definition: func.cpp:43
unsigned GetID()
Returns the unique ID.
Definition: func.h:79
Func(const std::string_view name, Visibility visibility=Visibility::LOCAL)
Definition: func.cpp:19
void SetNoInline(bool noinline=true)
Prevents the function from being inlined.
Definition: func.h:115
llvm::ArrayRef< FlaggedType > params() const
Returns the list of arguments.
Definition: func.h:135
bool IsRoot() const
Checks if the symbol can be externally referenced.
Definition: global.cpp:30
Kind
Enumeration of global kinds.
Definition: global.h:30
void insert(iterator it, Block *block)
Adds a block before another.
Definition: func.cpp:83
unsigned AddStackObject(unsigned index, unsigned size, llvm::Align align)
Adds a stack object.
Definition: func.cpp:191
bool IsVarArg() const
Returns the vararg flags.
Definition: func.h:105
void insertAfter(iterator it, Block *block)
Adds a block.
Definition: func.cpp:77
void SetPersonality(Global *func)
Set the personality routine.
Definition: func.cpp:36
bool HasVAStart() const
Checks if the function has a va_start instruction.
Definition: func.cpp:149
size_t size() const
Returns the size of the function.
Definition: func.h:175
Definition: symbol_table.h:33
void SetAlignment(llvm::Align align)
Sets the alignment of the function.
Definition: func.h:108
void SetParameters(const std::vector< FlaggedType > ¶ms)
Sets the number of fixed parameters.
Definition: func.h:133
~Func() override
Definition: func.cpp:31
CallingConv GetCallingConv() const
Returns the calling convention.
Definition: func.h:100
std::optional< llvm::Align > GetAlignment() const override
Returns the alignment of a function.
Definition: func.h:110
void removeFromParent() override
Removes an instruction from the parent.
Definition: func.cpp:53
BlockListType::iterator iterator
Iterator over the blocks.
Definition: func.h:40
void erase(iterator it)
Erases a block.
Definition: func.cpp:71
bool IsEntry() const
Checks if the function must be present even without uses.
Definition: func.h:180
std::string_view GetTuneCPU() const
Returns the CPU to tune for.
Definition: func.h:128
void SetVarArg(bool varArg=true)
Sets the vararg flag.
Definition: func.h:103
void RemoveUnreachable()
Removes unreachable blocks.
Definition: func.cpp:228
Block & getEntryBlock()
Returns the entry block.
Definition: func.cpp:175
void dump(llvm::raw_ostream &os=llvm::errs()) const override
Dumps the representation of the function.
Definition: func.cpp:267
std::string_view GetCPU() const
Returns the CPU to compile for.
Definition: func.h:123
bool HasIndirectCalls() const
Checks if the function has indirect calls.
Definition: func.cpp:162
llvm::Align Alignment
Alignment of the object, in bytes.
Definition: func.h:52
unsigned GetNumParams() const
Returns the number of parameters.
Definition: func.h:137
std::string_view GetFeatures() const
Returns the function-specific target features.
Definition: func.h:118
unsigned Index
Index of the stack object.
Definition: func.h:48
void RemoveStackObject(unsigned index)
Removes a stack object.
Definition: func.cpp:202
const StackObject & object(unsigned I) const
Finds a stack object by index.
Definition: func.h:155
llvm::ArrayRef< StackObject > objects() const
Iterator over stack objects.
Definition: func.h:145
void clear()
Clears all blocks.
Definition: func.cpp:89
Prog * getParent() const
Returns the parent block.
Definition: func.h:90
bool HasAddressTaken() const
Checks if the function can be used indirectly.
Definition: func.cpp:98
bool empty() const
Checks if the function has any blocks.
Definition: func.h:172
static constexpr Global::Kind kGlobalKind
Kind of the global.
Definition: func.h:33
void SetCallingConv(CallingConv conv)
Sets the calling convention.
Definition: func.h:98
SymbolTableList< Block > BlockListType
Type of the block list.
Definition: func.h:37
void eraseFromParent() override
Removes a function from the program.
Definition: func.cpp:59
bool DoesNotReturn() const
Checks if the function never returns.
Definition: func.cpp:123