-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathdvalue.cpp
More file actions
288 lines (230 loc) · 9 KB
/
dvalue.cpp
File metadata and controls
288 lines (230 loc) · 9 KB
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
285
286
287
288
//===-- dvalue.cpp --------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "gen/dvalue.h"
#include "dmd/declaration.h"
#include "gen/irstate.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/optimizer.h"
#include "gen/tollvm.h"
#include "llvm/IR/MDBuilder.h"
namespace {
bool isDefinedInFuncEntryBB(LLValue *v) {
auto instr = llvm::dyn_cast<llvm::Instruction>(v);
if (!instr) {
// Global, constant, ...
return true;
}
auto bb = instr->getParent();
if (bb != &(bb->getParent()->getEntryBlock())) {
return false;
}
// An invoke instruction in the entry BB does not necessarily dominate the
// rest of the function because of the failure path.
return !llvm::isa<llvm::InvokeInst>(instr);
}
}
////////////////////////////////////////////////////////////////////////////////
LLValue *DtoLVal(DValue *v) {
auto lval = v->isLVal();
assert(lval);
return lval->getLVal()->val;
}
////////////////////////////////////////////////////////////////////////////////
DValue::DValue(Type *t, LLValue *v) : type(t), val(v) {
assert(type);
assert(val);
}
bool DValue::definedInFuncEntryBB() { return isDefinedInFuncEntryBB(val); }
////////////////////////////////////////////////////////////////////////////////
DRValue::DRValue(Type *t, LLValue *v) : DValue(t, v) {
assert(!DtoIsInMemoryOnly(t) &&
"Cannot represent memory-only type as DRValue");
}
////////////////////////////////////////////////////////////////////////////////
DImValue::DImValue(Type *t, llvm::Value *v) : DRValue(t, v) {
#ifndef NDEBUG
const auto tb = t->toBasetype();
#endif
assert(tb->ty != TY::Tarray && "use DSliceValue for dynamic arrays");
assert(!tb->isFunction_Delegate_PtrToFunction() &&
"use DFuncValue for function pointers and delegates");
// v may be an addrspace qualified pointer so strip it before doing a pointer
// equality check.
assert(stripAddrSpaces(v->getType()) == DtoType(t));
}
////////////////////////////////////////////////////////////////////////////////
DConstValue::DConstValue(Type *t, LLConstant *con) : DRValue(t, con) {
assert(con->getType() == DtoType(t));
}
////////////////////////////////////////////////////////////////////////////////
DSliceValue::DSliceValue(Type *t, LLValue *pair, LLValue *length, LLValue *ptr)
: DRValue(t, pair), length(length), ptr(ptr) {
assert(t->toBasetype()->ty == TY::Tarray);
// v may be an addrspace qualified pointer so strip it before doing a pointer
// equality check.
assert(stripAddrSpaces(pair->getType()) == DtoType(t));
}
DSliceValue::DSliceValue(Type *t, LLValue *pair)
: DSliceValue(t, pair, nullptr, nullptr) {}
DSliceValue::DSliceValue(Type *t, LLValue *length, LLValue *ptr)
: DSliceValue(t, DtoAggrPair(length, ptr), length, ptr) {}
LLValue *DSliceValue::getLength() {
return length ? length : DtoExtractValue(val, 0, ".len");
}
LLValue *DSliceValue::getPtr() {
return ptr ? ptr : DtoExtractValue(val, 1, ".ptr");
}
////////////////////////////////////////////////////////////////////////////////
namespace {
LLValue *createFuncRValue(Type *t, LLValue *funcPtr, LLValue *vthis) {
if (t->toBasetype()->ty == TY::Tdelegate) {
assert(vthis);
return DtoAggrPair(vthis, funcPtr);
}
return funcPtr;
}
}
DFuncValue::DFuncValue(Type *t, FuncDeclaration *fd, LLValue *funcPtr,
LLValue *vt, LLValue *vtable)
: DRValue(t, createFuncRValue(t, funcPtr, vt)), func(fd), funcPtr(funcPtr),
vthis(vt), vtable(vtable) {
assert(t->toBasetype()->isFunction_Delegate_PtrToFunction());
}
DFuncValue::DFuncValue(FuncDeclaration *fd, LLValue *funcPtr, LLValue *vt,
LLValue *vtable)
: DFuncValue(fd->type, fd, funcPtr, vt, vtable) {}
bool DFuncValue::definedInFuncEntryBB() {
return isDefinedInFuncEntryBB(val) &&
(!vthis || isDefinedInFuncEntryBB(vthis));
}
DFuncValue *DFuncValue::paintAs(Type *t) {
if (t == type)
return this;
return new DFuncValue(
t, func, funcPtr,
vthis && t->toBasetype()->ty == TY::Tdelegate ? vthis : nullptr, vtable);
}
////////////////////////////////////////////////////////////////////////////////
DLValue::DLValue(Type *t, LLValue *v) : DValue(t, v) {
assert(t->toBasetype()->ty == TY::Ttuple || v->getType()->isPointerTy());
}
DRValue *DLValue::getRVal() {
if (DtoIsInMemoryOnly(type)) {
llvm_unreachable("getRVal() for memory-only type");
return nullptr;
}
LLValue *rval = DtoLoad(DtoMemType(type), val);
const auto tb = type->toBasetype();
if (tb->ty == TY::Tbool) {
assert(rval->getType() == llvm::Type::getInt8Ty(gIR->context()));
if (isOptimizationEnabled()) {
// attach range metadata for i8 being loaded: [0, 2)
llvm::MDBuilder mdBuilder(gIR->context());
llvm::cast<llvm::LoadInst>(rval)->setMetadata(
llvm::LLVMContext::MD_range,
mdBuilder.createRange(llvm::APInt(8, 0), llvm::APInt(8, 2)));
}
// truncate to i1
rval = gIR->ir->CreateTrunc(rval, llvm::Type::getInt1Ty(gIR->context()));
} else if (tb->ty == TY::Tarray) {
return new DSliceValue(type, rval);
} else if (tb->isPtrToFunction()) {
return new DFuncValue(type, nullptr, rval);
} else if (tb->ty == TY::Tdelegate) {
const auto contextPtr = DtoExtractValue(rval, 0, ".ptr");
const auto funcPtr = DtoExtractValue(rval, 1, ".funcptr");
return new DFuncValue(type, nullptr, funcPtr, contextPtr);
}
return new DImValue(type, rval);
}
////////////////////////////////////////////////////////////////////////////////
DSpecialRefValue::DSpecialRefValue(Type *t, LLValue *v) : DLValue(v, t) {
assert(v->getType()->isPointerTy());
}
DRValue *DSpecialRefValue::getRVal() {
return DLValue(type, DtoLoad(getOpaquePtrType(), val)).getRVal();
}
DLValue *DSpecialRefValue::getLVal() {
return new DLValue(type, DtoLoad(getOpaquePtrType(), val));
}
////////////////////////////////////////////////////////////////////////////////
DBitFieldLValue::DBitFieldLValue(Type *t, LLValue *ptr, BitFieldDeclaration *bf)
: DValue(t, ptr), bf(bf),
intType(LLIntegerType::get(
gIR->context(), (bf->bitOffset + bf->fieldWidth + 7) / 8 * 8)) {
assert(ptr->getType()->isPointerTy());
}
DRValue *DBitFieldLValue::getRVal() {
const auto sizeInBits = intType->getBitWidth();
const auto ptr = val;
LLValue *v = gIR->ir->CreateAlignedLoad(intType, ptr, llvm::MaybeAlign(1));
// TODO: byte-swap v for big-endian targets?
if (bf->type->isunsigned()) {
if (auto n = bf->bitOffset)
v = gIR->ir->CreateLShr(v, n);
const auto mask = llvm::APInt::getLowBitsSet(sizeInBits, bf->fieldWidth);
v = gIR->ir->CreateAnd(v, mask);
v = gIR->ir->CreateZExtOrTrunc(v, DtoType(bf->type));
} else {
// shift-left to make the MSB the sign bit
if (auto n = sizeInBits - (bf->bitOffset + bf->fieldWidth))
v = gIR->ir->CreateShl(v, n);
// then arithmetic-shift-right
if (auto n = sizeInBits - bf->fieldWidth)
v = gIR->ir->CreateAShr(v, n);
v = gIR->ir->CreateSExtOrTrunc(v, DtoType(bf->type));
}
return new DImValue(type, v);
}
void DBitFieldLValue::store(LLValue *value) {
assert(value->getType()->isIntegerTy());
const auto ptr = val;
const auto mask =
llvm::APInt::getLowBitsSet(intType->getBitWidth(), bf->fieldWidth);
const auto oldVal =
gIR->ir->CreateAlignedLoad(intType, ptr, llvm::MaybeAlign(1));
// TODO: byte-swap oldVal for big-endian targets?
const auto maskedOldVal =
gIR->ir->CreateAnd(oldVal, ~(mask << bf->bitOffset));
auto bfVal = gIR->ir->CreateZExtOrTrunc(value, intType);
bfVal = gIR->ir->CreateAnd(bfVal, mask);
if (auto n = bf->bitOffset)
bfVal = gIR->ir->CreateShl(bfVal, n);
const auto newVal = gIR->ir->CreateOr(maskedOldVal, bfVal);
// TODO: byte-swap newVal for big-endian targets?
gIR->ir->CreateAlignedStore(newVal, ptr, llvm::MaybeAlign(1));
}
DDcomputeLValue::DDcomputeLValue(Type *t, llvm::Type * llt, LLValue *v) : DLValue(t, v) {
lltype = llt;
}
DRValue *DDcomputeLValue::getRVal() {
if (DtoIsInMemoryOnly(type)) {
llvm_unreachable("getRVal() for memory-only type");
return nullptr;
}
LLValue *rval = DtoLoad(lltype, val);
const auto ty = type->toBasetype()->ty;
if (ty == TY::Tbool) {
assert(rval->getType() == llvm::Type::getInt8Ty(gIR->context()));
if (isOptimizationEnabled()) {
// attach range metadata for i8 being loaded: [0, 2)
llvm::MDBuilder mdBuilder(gIR->context());
llvm::cast<llvm::LoadInst>(rval)->setMetadata(
llvm::LLVMContext::MD_range,
mdBuilder.createRange(llvm::APInt(8, 0), llvm::APInt(8, 2)));
}
// truncate to i1
rval = gIR->ir->CreateTrunc(rval, llvm::Type::getInt1Ty(gIR->context()));
} else if (ty == TY::Tarray) {
return new DSliceValue(type, rval);
}
return new DImValue(type, rval);
}