Skip to content

Commit dedd0c2

Browse files
authored
Merge pull request #4664 from kinke/opaque_bitcast
Skip superfluous IR pointer bitcasts, now that they are always opaque
2 parents 4260031 + 4eff494 commit dedd0c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+262
-592
lines changed

gen/aa.cpp

+15-34
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@
2727
using namespace dmd;
2828

2929
// returns the keytype typeinfo
30-
static LLConstant *to_keyti(const Loc &loc, DValue *aa, LLType *targetType) {
30+
static LLConstant *to_keyti(const Loc &loc, DValue *aa) {
3131
// keyti param
3232
assert(aa->type->toBasetype()->ty == TY::Taarray);
3333
TypeAArray *aatype = static_cast<TypeAArray *>(aa->type->toBasetype());
34-
LLConstant *ti = DtoTypeInfoOf(loc, aatype->index, /*base=*/false);
35-
return DtoBitCast(ti, targetType);
34+
return DtoTypeInfoOf(loc, aatype->index);
3635
}
3736

3837
////////////////////////////////////////////////////////////////////////////////
@@ -49,35 +48,28 @@ DLValue *DtoAAIndex(const Loc &loc, Type *type, DValue *aa, DValue *key,
4948
// first get the runtime function
5049
llvm::Function *func =
5150
getRuntimeFunction(loc, gIR->module, lvalue ? "_aaGetY" : "_aaInX");
52-
LLFunctionType *funcTy = func->getFunctionType();
5351

5452
// aa param
5553
LLValue *aaval = lvalue ? DtoLVal(aa) : DtoRVal(aa);
56-
aaval = DtoBitCast(aaval, funcTy->getParamType(0));
54+
assert(aaval->getType()->isPointerTy());
5755

5856
// pkey param
5957
LLValue *pkey = makeLValue(loc, key);
60-
pkey = DtoBitCast(pkey, funcTy->getParamType(lvalue ? 3 : 2));
6158

6259
// call runtime
6360
LLValue *ret;
6461
if (lvalue) {
6562
auto t = mutableOf(unSharedOf(aa->type));
66-
LLValue *rawAATI = DtoTypeInfoOf(loc, t, /*base=*/false);
67-
LLValue *castedAATI = DtoBitCast(rawAATI, funcTy->getParamType(1));
63+
LLValue *aati = DtoTypeInfoOf(loc, t);
6864
LLValue *valsize = DtoConstSize_t(getTypeAllocSize(DtoType(type)));
69-
ret = gIR->CreateCallOrInvoke(func, aaval, castedAATI, valsize, pkey,
65+
ret = gIR->CreateCallOrInvoke(func, aaval, aati, valsize, pkey,
7066
"aa.index");
7167
} else {
72-
LLValue *keyti = to_keyti(loc, aa, funcTy->getParamType(1));
68+
LLValue *keyti = to_keyti(loc, aa);
7369
ret = gIR->CreateCallOrInvoke(func, aaval, keyti, pkey, "aa.index");
7470
}
7571

76-
// cast return value
77-
LLType *targettype = DtoPtrToType(type);
78-
if (ret->getType() != targettype) {
79-
ret = DtoBitCast(ret, targettype);
80-
}
72+
assert(ret->getType()->isPointerTy());
8173

8274
// Only check bounds for rvalues ('aa[key]').
8375
// Lvalue use ('aa[key] = value') auto-adds an element.
@@ -112,34 +104,25 @@ DValue *DtoAAIn(const Loc &loc, Type *type, DValue *aa, DValue *key) {
112104

113105
// first get the runtime function
114106
llvm::Function *func = getRuntimeFunction(loc, gIR->module, "_aaInX");
115-
LLFunctionType *funcTy = func->getFunctionType();
116107

117108
IF_LOG Logger::cout() << "_aaIn = " << *func << '\n';
118109

119110
// aa param
120111
LLValue *aaval = DtoRVal(aa);
112+
assert(aaval->getType()->isPointerTy());
121113
IF_LOG {
122114
Logger::cout() << "aaval: " << *aaval << '\n';
123-
Logger::cout() << "totype: " << *funcTy->getParamType(0) << '\n';
124115
}
125-
aaval = DtoBitCast(aaval, funcTy->getParamType(0));
126116

127117
// keyti param
128-
LLValue *keyti = to_keyti(loc, aa, funcTy->getParamType(1));
118+
LLValue *keyti = to_keyti(loc, aa);
129119

130120
// pkey param
131121
LLValue *pkey = makeLValue(loc, key);
132-
pkey = DtoBitCast(pkey, getVoidPtrType());
133122

134123
// call runtime
135124
LLValue *ret = gIR->CreateCallOrInvoke(func, aaval, keyti, pkey, "aa.in");
136125

137-
// cast return value
138-
LLType *targettype = DtoType(type);
139-
if (ret->getType() != targettype) {
140-
ret = DtoBitCast(ret, targettype);
141-
}
142-
143126
return new DImValue(type, ret);
144127
}
145128

@@ -156,24 +139,21 @@ DValue *DtoAARemove(const Loc &loc, DValue *aa, DValue *key) {
156139

157140
// first get the runtime function
158141
llvm::Function *func = getRuntimeFunction(loc, gIR->module, "_aaDelX");
159-
LLFunctionType *funcTy = func->getFunctionType();
160142

161143
IF_LOG Logger::cout() << "_aaDel = " << *func << '\n';
162144

163145
// aa param
164146
LLValue *aaval = DtoRVal(aa);
147+
assert(aaval->getType()->isPointerTy());
165148
IF_LOG {
166149
Logger::cout() << "aaval: " << *aaval << '\n';
167-
Logger::cout() << "totype: " << *funcTy->getParamType(0) << '\n';
168150
}
169-
aaval = DtoBitCast(aaval, funcTy->getParamType(0));
170151

171152
// keyti param
172-
LLValue *keyti = to_keyti(loc, aa, funcTy->getParamType(1));
153+
LLValue *keyti = to_keyti(loc, aa);
173154

174155
// pkey param
175156
LLValue *pkey = makeLValue(loc, key);
176-
pkey = DtoBitCast(pkey, funcTy->getParamType(2));
177157

178158
// call runtime
179159
LLValue *res = gIR->CreateCallOrInvoke(func, aaval, keyti, pkey);
@@ -188,10 +168,11 @@ LLValue *DtoAAEquals(const Loc &loc, EXP op, DValue *l, DValue *r) {
188168
assert(t == r->type->toBasetype() &&
189169
"aa equality is only defined for aas of same type");
190170
llvm::Function *func = getRuntimeFunction(loc, gIR->module, "_aaEqual");
191-
LLFunctionType *funcTy = func->getFunctionType();
192171

193-
LLValue *aaval = DtoBitCast(DtoRVal(l), funcTy->getParamType(1));
194-
LLValue *abval = DtoBitCast(DtoRVal(r), funcTy->getParamType(2));
172+
LLValue *aaval = DtoRVal(l);
173+
assert(aaval->getType()->isPointerTy());
174+
LLValue *abval = DtoRVal(r);
175+
assert(abval->getType()->isPointerTy());
195176
LLValue *aaTypeInfo = DtoTypeInfoOf(loc, t);
196177
LLValue *res =
197178
gIR->CreateCallOrInvoke(func, aaTypeInfo, aaval, abval, "aaEqRes");

gen/abi/abi.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ using namespace dmd;
3232

3333
llvm::Value *ABIRewrite::getRVal(Type *dty, LLValue *v) {
3434
llvm::Type *t = DtoType(dty);
35-
return DtoLoad(t, DtoBitCast(getLVal(dty, v), t->getPointerTo()));
35+
return DtoLoad(t, getLVal(dty, v));
3636
}
3737

3838
//////////////////////////////////////////////////////////////////////////////
@@ -191,8 +191,8 @@ void TargetABI::rewriteVarargs(IrFuncTy &fty,
191191
//////////////////////////////////////////////////////////////////////////////
192192

193193
LLValue *TargetABI::prepareVaStart(DLValue *ap) {
194-
// pass a i8* pointer to ap to LLVM's va_start intrinsic
195-
return DtoBitCast(DtoLVal(ap), getVoidPtrType());
194+
// pass an opaque pointer to ap to LLVM's va_start intrinsic
195+
return DtoLVal(ap);
196196
}
197197

198198
//////////////////////////////////////////////////////////////////////////////
@@ -209,8 +209,8 @@ void TargetABI::vaCopy(DLValue *dest, DValue *src) {
209209
//////////////////////////////////////////////////////////////////////////////
210210

211211
LLValue *TargetABI::prepareVaArg(DLValue *ap) {
212-
// pass a i8* pointer to ap to LLVM's va_arg intrinsic
213-
return DtoBitCast(DtoLVal(ap), getVoidPtrType());
212+
// pass an opaque pointer to ap to LLVM's va_arg intrinsic
213+
return DtoLVal(ap);
214214
}
215215

216216
//////////////////////////////////////////////////////////////////////////////

gen/abi/generic.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ struct BaseBitcastABIRewrite : ABIRewrite {
148148
return DtoLoad(asType, paddedDump, name);
149149
}
150150

151-
address = DtoBitCast(address, getPtrToType(asType));
152151
return DtoLoad(asType, address, name);
153152
}
154153

@@ -248,7 +247,7 @@ struct IndirectByvalRewrite : ABIRewrite {
248247
}
249248

250249
LLValue *getLVal(Type *dty, LLValue *v) override {
251-
return DtoBitCast(v, DtoPtrToType(dty));
250+
return v;
252251
}
253252

254253
LLType *type(Type *t) override { return DtoPtrToType(t); }

gen/abi/riscv64.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ struct HardfloatRewrite : ABIRewrite {
110110
DtoRawAlloca(asType, alignment, ".HardfloatRewrite_arg_storage");
111111
for (unsigned i = 0; i < (unsigned)flat.length; ++i) {
112112
DtoMemCpy(DtoGEP(asType, buffer, 0, i),
113-
DtoGEP1(getI8Type(), DtoBitCast(address, getVoidPtrType()),
114-
flat.fields[i].offset),
113+
DtoGEP1(getI8Type(), address, flat.fields[i].offset),
115114
DtoConstSize_t(flat.fields[i].ty->size()));
116115
}
117116
return DtoLoad(asType, buffer, ".HardfloatRewrite_arg");
@@ -126,8 +125,7 @@ struct HardfloatRewrite : ABIRewrite {
126125
LLValue *ret = DtoRawAlloca(DtoType(dty), alignment,
127126
".HardfloatRewrite_param_storage");
128127
for (unsigned i = 0; i < (unsigned)flat.length; ++i) {
129-
DtoMemCpy(DtoGEP1(getI8Type(), DtoBitCast(ret, getVoidPtrType()),
130-
flat.fields[i].offset),
128+
DtoMemCpy(DtoGEP1(getI8Type(), ret, flat.fields[i].offset),
131129
DtoGEP(asType, buffer, 0, i),
132130
DtoConstSize_t(flat.fields[i].ty->size()));
133131
}

gen/abi/x86-64.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -352,27 +352,25 @@ LLValue *X86_64TargetABI::prepareVaStart(DLValue *ap) {
352352
// invoking va_start, we first need to allocate the actual __va_list_tag struct
353353
// and set `ap` to its address.
354354
LLValue *valistmem = DtoRawAlloca(getValistType(), 0, "__va_list_mem");
355-
DtoStore(valistmem,
356-
DtoBitCast(DtoLVal(ap), getPtrToType(valistmem->getType())));
357-
// Pass a i8* pointer to the actual struct to LLVM's va_start intrinsic.
358-
return DtoBitCast(valistmem, getVoidPtrType());
355+
DtoStore(valistmem, DtoLVal(ap));
356+
// Pass an opaque pointer to the actual struct to LLVM's va_start intrinsic.
357+
return valistmem;
359358
}
360359

361360
void X86_64TargetABI::vaCopy(DLValue *dest, DValue *src) {
362361
// Analog to va_start, we first need to allocate a new __va_list_tag struct on
363362
// the stack and set `dest` to its address.
364363
LLValue *valistmem = DtoRawAlloca(getValistType(), 0, "__va_list_mem");
365-
DtoStore(valistmem,
366-
DtoBitCast(DtoLVal(dest), getPtrToType(valistmem->getType())));
364+
DtoStore(valistmem, DtoLVal(dest));
367365
// Then fill the new struct with a bitcopy of the source struct.
368366
// `src` is a __va_list_tag* pointer to the source struct.
369367
DtoMemCpy(getValistType(), valistmem, DtoRVal(src));
370368
}
371369

372370
LLValue *X86_64TargetABI::prepareVaArg(DLValue *ap) {
373-
// Pass a i8* pointer to the actual __va_list_tag struct to LLVM's va_arg
371+
// Pass an opaque pointer to the actual __va_list_tag struct to LLVM's va_arg
374372
// intrinsic.
375-
return DtoBitCast(DtoRVal(ap), getVoidPtrType());
373+
return DtoRVal(ap);
376374
}
377375

378376
Type *X86_64TargetABI::vaListType() {

0 commit comments

Comments
 (0)