forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirstate.h
284 lines (232 loc) · 10.1 KB
/
irstate.h
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//===-- gen/irstate.h - Global codegen state --------------------*- C++ -*-===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the global state used and modified when generating the
// code (i.e. LLVM IR) for a given D module.
//
//===----------------------------------------------------------------------===//
#pragma once
#include "dmd/aggregate.h"
#include "gen/dibuilder.h"
#include "gen/objcgen.h"
#include "ir/iraggr.h"
#include "ir/irvar.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ProfileData/InstrProfReader.h"
#include <deque>
#include <memory>
#include <set>
#include <sstream>
#include <vector>
namespace llvm {
class LLVMContext;
class TargetMachine;
class IndexedInstrProfReader;
}
class FuncGenState;
struct IRState;
struct TargetABI;
class DComputeTarget;
extern IRState *gIR;
extern llvm::TargetMachine *gTargetMachine;
extern const llvm::DataLayout *gDataLayout;
extern TargetABI *gABI;
class TypeFunction;
class TypeStruct;
class ClassDeclaration;
class FuncDeclaration;
class Module;
class TypeStruct;
struct BaseClass;
class AnonDeclaration;
class StructLiteralExp;
struct IrFunction;
struct IrModule;
// Saves the IRBuilder state and restores it on destruction.
struct IRBuilderScope {
private:
llvm::IRBuilderBase::InsertPointGuard ipGuard;
llvm::IRBuilderBase::FastMathFlagGuard fmfGuard;
public:
explicit IRBuilderScope(llvm::IRBuilderBase &builder)
: ipGuard(builder), fmfGuard(builder) {}
};
struct IRBuilderHelper {
IRState *state;
IRBuilder<> *operator->();
};
struct IRAsmStmt {
IRAsmStmt() : isBranchToLabel(nullptr) {}
std::string code;
std::string out_c;
std::string in_c;
std::vector<LLValue *> out;
std::vector<LLValue *> in;
// if this is nonzero, it contains the target label
LabelDsymbol *isBranchToLabel;
};
struct IRAsmBlock {
std::deque<IRAsmStmt *> s;
std::set<std::string> clobs;
size_t outputcount;
// stores the labels within the asm block
std::vector<Identifier *> internalLabels;
CompoundAsmStatement *asmBlock;
LLType *retty;
unsigned retn;
bool retemu; // emulate abi ret with a temporary
LLValue *(*retfixup)(IRBuilderHelper b, LLValue *orig); // Modifies retval
explicit IRAsmBlock(CompoundAsmStatement *b)
: outputcount(0), asmBlock(b), retty(nullptr), retn(0), retemu(false),
retfixup(nullptr) {}
};
// represents the LLVM module (object file)
struct IRState {
private:
IRBuilder<> builder;
friend struct IRBuilderHelper;
std::vector<std::pair<llvm::GlobalVariable *, llvm::Constant *>>
globalsToReplace;
Array<Loc> inlineAsmLocs; // tracked by GC
// Cache of (possibly bitcast) global variables for taking the address of
// struct literal constants. (Also) used to resolve self-references. Must be
// cached per IR module: https://github.com/ldc-developers/ldc/issues/2990
// [The real key type is `StructLiteralExp *`; a fwd class declaration isn't
// enough to use it directly.]
llvm::DenseMap<void *, llvm::Constant *> structLiteralConstants;
// Global variables bound to string literals. Once created such a variable
// is reused whenever an equivalent string literal is referenced in the
// module, to prevent duplicates.
llvm::StringMap<llvm::GlobalVariable *> cachedStringLiterals;
llvm::StringMap<llvm::GlobalVariable *> cachedWstringLiterals;
llvm::StringMap<llvm::GlobalVariable *> cachedDstringLiterals;
public:
IRState(const char *name, llvm::LLVMContext &context);
~IRState();
IRState(IRState const &) = delete;
IRState &operator=(IRState const &) = delete;
llvm::Module module;
llvm::LLVMContext &context() const { return module.getContext(); }
Module *dmodule = nullptr;
LLStructType *moduleRefType = nullptr;
ObjCState objc;
// Stack of currently codegen'd functions (more than one for lambdas or other
// nested functions, inlining-only codegen'ing, -linkonce-templates etc.),
// and some convenience accessors for the top-most one.
std::vector<std::unique_ptr<FuncGenState>> funcGenStates;
FuncGenState &funcGen();
IrFunction *func();
llvm::Function *topfunc();
llvm::Instruction *topallocapoint();
// Use this to set the IRBuilder's insertion point for a new function.
// The previous IRBuilder state is restored when the returned value is
// destructed. Use `ir->SetInsertPoint()` instead to change the insertion
// point inside the same function.
std::unique_ptr<IRBuilderScope> setInsertPoint(llvm::BasicBlock *bb);
// Use this to have the IRBuilder's current insertion point (incl. debug
// location) restored when the returned value is destructed.
std::unique_ptr<llvm::IRBuilderBase::InsertPointGuard> saveInsertPoint();
// Returns the basic block the IRBuilder currently inserts into.
llvm::BasicBlock *scopebb() { return ir->GetInsertBlock(); }
bool scopereturned();
// Creates a new basic block and inserts it before the specified one.
llvm::BasicBlock *insertBBBefore(llvm::BasicBlock *successor,
const llvm::Twine &name);
// Creates a new basic block and inserts it after the specified one.
llvm::BasicBlock *insertBBAfter(llvm::BasicBlock *predecessor,
const llvm::Twine &name);
// Creates a new basic block and inserts it after the current scope basic
// block (`scopebb()`).
llvm::BasicBlock *insertBB(const llvm::Twine &name);
// create a call or invoke, depending on the landing pad info
llvm::Instruction *CreateCallOrInvoke(LLFunction *Callee,
const char *Name = "");
llvm::Instruction *CreateCallOrInvoke(LLFunction *Callee,
llvm::ArrayRef<LLValue *> Args,
const char *Name = "",
bool isNothrow = false);
llvm::Instruction *CreateCallOrInvoke(LLFunction *Callee, LLValue *Arg1,
const char *Name = "");
llvm::Instruction *CreateCallOrInvoke(LLFunction *Callee, LLValue *Arg1,
LLValue *Arg2, const char *Name = "");
llvm::Instruction *CreateCallOrInvoke(LLFunction *Callee, LLValue *Arg1,
LLValue *Arg2, LLValue *Arg3,
const char *Name = "");
llvm::Instruction *CreateCallOrInvoke(LLFunction *Callee, LLValue *Arg1,
LLValue *Arg2, LLValue *Arg3,
LLValue *Arg4, const char *Name = "");
// this holds the array being indexed or sliced so $ will work
// might be a better way but it works. problem is I only get a
// VarDeclaration for __dollar, but I can't see how to get the
// array pointer from this :(
std::vector<DValue *> arrays;
// builder helper
IRBuilderHelper ir;
// debug info helper
ldc::DIBuilder DBuilder;
// PGO data file reader
std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
// for inline asm
IRAsmBlock *asmBlock = nullptr;
std::ostringstream nakedAsm;
// Globals to pin in the llvm.used array to make sure they are not
// eliminated.
std::vector<LLConstant *> usedArray;
/// Whether to emit array bounds checking in the current function.
bool emitArrayBoundsChecks();
// Sets the initializer for a global LL variable.
// If the types don't match, this entails creating a new helper global
// matching the initializer type and replacing all existing uses of globalVar
// by a bitcast pointer to the helper global's payload.
// Returns either the specified globalVar if the types match, or the bitcast
// pointer replacing globalVar (and resets globalVar to the new helper
// global).
llvm::Constant *
setGlobalVarInitializer(llvm::GlobalVariable *&globalVar,
llvm::Constant *initializer,
Dsymbol *symbolForLinkageAndVisibility);
// To be called when finalizing the IR module in order to perform a second
// replacement pass for global variables replaced (and registered) by
// setGlobalVarInitializer().
void replaceGlobals();
llvm::Constant *getStructLiteralConstant(StructLiteralExp *sle) const;
void setStructLiteralConstant(StructLiteralExp *sle,
llvm::Constant *constant);
// Constructs a global variable for a StringExp.
// Caches the result based on StringExp::peekData() such that any subsequent
// calls with a StringExp with matching data will return the same variable.
llvm::GlobalVariable *getCachedStringLiteral(StringExp *se);
llvm::GlobalVariable *getCachedStringLiteral(llvm::StringRef s);
// List of functions with cpu or features attributes overriden by user
std::vector<IrFunction *> targetCpuOrFeaturesOverridden;
struct RtCompiledFuncDesc {
llvm::GlobalVariable *thunkVar;
llvm::Function *thunkFunc;
};
std::map<llvm::Function *, RtCompiledFuncDesc> dynamicCompiledFunctions;
std::set<IrGlobal *> dynamicCompiledVars;
/// Vector of options passed to the linker as metadata in object file.
llvm::SmallVector<llvm::MDNode *, 5> linkerOptions;
llvm::SmallVector<llvm::MDNode *, 5> linkerDependentLibs;
void addLinkerOption(llvm::ArrayRef<llvm::StringRef> options);
void addLinkerDependentLib(llvm::StringRef libraryName);
llvm::CallInst *createInlineAsmCall(const Loc &loc, llvm::InlineAsm *ia,
llvm::ArrayRef<llvm::Value *> args);
void addInlineAsmSrcLoc(const Loc &loc, llvm::CallInst *inlineAsmCall);
const Loc &getInlineAsmSrcLoc(unsigned srcLocCookie) const;
// MS C++ compatible type descriptors
llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap;
llvm::DenseMap<ClassDeclaration *, llvm::GlobalVariable *> TypeDescriptorMap;
// Target for dcompute. If not nullptr, it owns this.
DComputeTarget *dcomputetarget = nullptr;
};
void Statement_toIR(Statement *s, IRState *irs);
bool useMSVCEH();