-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLLVMUtils.cpp
134 lines (104 loc) · 4.2 KB
/
LLVMUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*=====================================================================
LLVMUtils.cpp
-------------
Copyright Glare Technologies Limited 2019 -
=====================================================================*/
#include "LLVMUtils.h"
#include "wnt_ASTNode.h"
#ifdef _MSC_VER // If compiling with Visual C++
#pragma warning(push, 0) // Disable warnings
#endif
#include "llvm/IR/Module.h"
#include <llvm/IR/DataLayout.h>
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/IPO.h"
#include <llvm/IR/IRBuilder.h>
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/IR/LLVMContext.h"
#ifdef _MSC_VER
#pragma warning(pop) // Re-enable warnings
#endif
namespace Winter
{
namespace LLVMUtils
{
llvm::Value* getNthArg(llvm::Function *func, int n)
{
llvm::Function::arg_iterator args = func->arg_begin();
for(int i=0; i<n; ++i)
args++;
return args;
}
llvm::Value* getLastArg(llvm::Function *func)
{
return getNthArg(func, (int)func->arg_size() - 1);
}
llvm::Value* createStructGEP(llvm::IRBuilder</*true, */llvm::ConstantFolder, llvm::IRBuilderDefaultInserter/*<true>*/ >* builder,
llvm::Value* struct_ptr, unsigned int field_index, llvm::Type* struct_llvm_type, const llvm::Twine& name)
{
assert(llvm::isa<llvm::StructType>(struct_llvm_type));
return builder->CreateStructGEP(struct_llvm_type, struct_ptr, field_index);
}
llvm::LoadInst* createLoadFromStruct(llvm::IRBuilder<>* builder, llvm::Value* src_ptr, unsigned int field_index, llvm::Type* struct_llvm_type, const llvm::Twine& field_val_name)
{
assert(llvm::isa<llvm::StructType>(struct_llvm_type));
llvm::Value* field_ptr = createStructGEP(builder, src_ptr, field_index, struct_llvm_type);
return createLoad(builder, field_ptr, llvm::cast<llvm::StructType>(struct_llvm_type)->getElementType(field_index), field_val_name);
}
//llvm::Value* createFieldLoad(llvm::Value* structure_ptr, int field_index,
// llvm::IRBuilder<>* builder, const llvm::Twine& name)
//{
// llvm::Value* field_ptr = builder->CreateStructGEP(structure_ptr->get, structure_ptr,
// field_index, // field index
// name
// );
//
// return builder->CreateLoad(field_ptr, name);
//}
llvm::Value* createCollectionCopy(const TypeVRef& collection_type, llvm::Value* dest_ptr, llvm::Value* src_ptr, EmitLLVMCodeParams& params)
{
//if(collection_type->getType() == Type::ArrayTypeType)
//{
// const ArrayType* array_type = collection_type.downcastToPtr<ArrayType>();
// const TypeRef elem_type = array_type->elem_type;
// params.target_data->getABITypeAlignment
const bool use_memcpy =
collection_type->getType() == Type::ArrayTypeType; //||
// gives module verification errors: collection_type->getType() == Type::StructureTypeType;
if(use_memcpy)
{
const auto type_alignment = params.target_data->getABITypeAlignment(collection_type->LLVMType(*params.module));
llvm::Type* llvm_type = collection_type->LLVMType(*params.module);
const uint64_t size_B = params.target_data->getTypeAllocSize(llvm_type);
llvm::Value* size = llvm::ConstantInt::get(*params.context, llvm::APInt(64, size_B, /*signed=*/false));
return createMemCpy(params.builder, dest_ptr, src_ptr, size, /*align=*/(unsigned int)type_alignment);
}
else
{
return params.builder->CreateStore(
createLoad(params.builder, src_ptr, collection_type, params.module),
dest_ptr
);
}
}
llvm::Function* getFunctionFromModule(llvm::Module* module, const std::string& func_name, llvm::FunctionType* functype)
{
#if TARGET_LLVM_VERSION >= 110
llvm::FunctionCallee callee = module->getOrInsertFunction(
func_name, // Name
functype // Type
);
llvm::Value* llvm_func_constant = callee.getCallee();
#else
llvm::Constant* llvm_func_constant = module->getOrInsertFunction(
func_name, // Name
functype // Type
);
#endif
assert(llvm::isa<llvm::Function>(llvm_func_constant));
if(!llvm::isa<llvm::Function>(llvm_func_constant))
throw BaseException("Internal error: While tring to get function " + func_name + " with getOrInsertFunction(): was not a function.");
return static_cast<llvm::Function*>(llvm_func_constant);
}
}; // end namespace LLVMUtils
}; // end namespace Winter