Skip to content

Commit

Permalink
Merge pull request #4710 from kinke/getOpaquePtrType
Browse files Browse the repository at this point in the history
Remove some typed-IR-pointer left-overs
  • Loading branch information
kinke authored Jul 26, 2024
2 parents 90bcdbf + 6bfa6d9 commit a3d4f32
Show file tree
Hide file tree
Showing 26 changed files with 97 additions and 161 deletions.
6 changes: 3 additions & 3 deletions driver/codegenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ void emitLLVMUsedArray(IRState &irs) {
return;
}

auto *i8PtrType = getVoidPtrType(irs.context());
auto ptrType = LLPointerType::get(irs.context(), 0);

// Convert all elements to i8* (the expected type for llvm.used)
for (auto &elem : irs.usedArray) {
elem = llvm::ConstantExpr::getBitCast(elem, i8PtrType);
elem = llvm::ConstantExpr::getBitCast(elem, ptrType);
}

auto *arrayType = llvm::ArrayType::get(i8PtrType, irs.usedArray.size());
auto *arrayType = llvm::ArrayType::get(ptrType, irs.usedArray.size());
auto *llvmUsed = new llvm::GlobalVariable(
irs.module, arrayType, false, llvm::GlobalValue::AppendingLinkage,
llvm::ConstantArray::get(arrayType, irs.usedArray), "llvm.used");
Expand Down
2 changes: 1 addition & 1 deletion gen/abi/generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ struct IndirectByvalRewrite : ABIRewrite {
return v;
}

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

void applyTo(IrFuncTyArg &arg, LLType *finalLType = nullptr) override {
ABIRewrite::applyTo(arg, finalLType);
Expand Down
14 changes: 7 additions & 7 deletions gen/abi/x86-64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct ImplicitByvalRewrite : ABIRewrite {

LLValue *getLVal(Type *dty, LLValue *v) override { return v; }

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

void applyTo(IrFuncTyArg &arg, LLType *finalLType = nullptr) override {
ABIRewrite::applyTo(arg, finalLType);
Expand Down Expand Up @@ -336,13 +336,13 @@ void X86_64TargetABI::rewriteVarargs(IrFuncTy &fty,

LLType *X86_64TargetABI::getValistType() {
LLType *uintType = LLType::getInt32Ty(gIR->context());
LLType *voidPointerType = getVoidPtrType();
LLType *pointerType = getOpaquePtrType();

std::vector<LLType *> parts; // struct __va_list_tag {
parts.push_back(uintType); // uint gp_offset;
parts.push_back(uintType); // uint fp_offset;
parts.push_back(voidPointerType); // void* overflow_arg_area;
parts.push_back(voidPointerType); // void* reg_save_area; }
std::vector<LLType *> parts; // struct __va_list_tag {
parts.push_back(uintType); // uint gp_offset;
parts.push_back(uintType); // uint fp_offset;
parts.push_back(pointerType); // void* overflow_arg_area;
parts.push_back(pointerType); // void* reg_save_area; }

return LLStructType::get(gIR->context(), parts);
}
Expand Down
29 changes: 5 additions & 24 deletions gen/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ static void DtoSetArray(DValue *array, DValue *rhs);

////////////////////////////////////////////////////////////////////////////////

LLStructType *DtoArrayType(Type *arrayTy) {
assert(arrayTy->nextOf());
llvm::Type *elems[] = {DtoSize_t(), DtoPtrToType(arrayTy->nextOf())};
return llvm::StructType::get(gIR->context(), elems, false);
}

LLStructType *DtoArrayType(LLType *t) {
llvm::Type *elems[] = {DtoSize_t(), getPtrToType(t)};
return llvm::StructType::get(gIR->context(), elems, false);
}

////////////////////////////////////////////////////////////////////////////////

LLArrayType *DtoStaticArrayType(Type *t) {
t = t->toBasetype();
assert(t->ty == TY::Tsarray);
Expand Down Expand Up @@ -714,11 +701,8 @@ LLValue *DtoArrayEqCmp_impl(const Loc &loc, const char *func, DValue *l,

LLSmallVector<LLValue *, 3> args;

// get values, reinterpret cast to void[]
args.push_back(DtoSlicePaint(DtoRVal(l),
DtoArrayType(LLType::getInt8Ty(gIR->context()))));
args.push_back(DtoSlicePaint(DtoRVal(r),
DtoArrayType(LLType::getInt8Ty(gIR->context()))));
args.push_back(DtoRVal(l));
args.push_back(DtoRVal(r));

// pass array typeinfo ?
if (useti) {
Expand Down Expand Up @@ -929,16 +913,13 @@ LLValue *DtoArrayPtr(DValue *v) {
LOG_SCOPE;

Type *t = v->type->toBasetype();
// v's LL array element type may not be the real one
// due to implicit casts (e.g., to base class)
LLType *wantedLLPtrType = DtoPtrToType(t->nextOf());
LLValue *ptr = nullptr;

if (t->ty == TY::Tarray) {
if (v->isNull()) {
ptr = getNullPtr(wantedLLPtrType);
ptr = getNullPtr();
} else if (v->isLVal()) {
ptr = DtoLoad(wantedLLPtrType, DtoGEP(DtoType(v->type), DtoLVal(v), 0, 1), ".ptr");
ptr = DtoLoad(getOpaquePtrType(), DtoGEP(DtoType(v->type), DtoLVal(v), 0, 1), ".ptr");
} else {
auto slice = v->isSlice();
assert(slice);
Expand Down Expand Up @@ -1035,7 +1016,7 @@ DValue *DtoCastArray(const Loc &loc, DValue *u, Type *to) {
if (totype->ty == TY::Tbool) {
// return (arr.ptr !is null)
LLValue *ptr = DtoArrayPtr(u);
LLConstant *nul = getNullPtr(ptr->getType());
LLConstant *nul = getNullPtr();
return new DImValue(to, gIR->ir->CreateICmpNE(ptr, nul));
}

Expand Down
2 changes: 0 additions & 2 deletions gen/arrays.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ struct IRState;
struct Loc;
class Type;

llvm::StructType *DtoArrayType(Type *arrayTy);
llvm::StructType *DtoArrayType(LLType *elemTy);
llvm::ArrayType *DtoStaticArrayType(Type *sarrayTy);

/// Creates a (global) constant with the element data for the given arary
Expand Down
12 changes: 5 additions & 7 deletions gen/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,17 @@ DValue *DtoCastClass(const Loc &loc, DValue *val, Type *_to) {
// it's just a GEP and (maybe) a pointer-to-pointer BitCast, so it
// should be pretty cheap and perfectly safe even if the original was
// null.
LLValue *isNull = gIR->ir->CreateICmpEQ(
orig, LLConstant::getNullValue(orig->getType()), ".nullcheck");
v = gIR->ir->CreateSelect(
isNull, LLConstant::getNullValue(getVoidPtrType()), v, ".interface");
const auto nullPtr = getNullPtr();
LLValue *isNull = gIR->ir->CreateICmpEQ(orig, nullPtr, ".nullcheck");
v = gIR->ir->CreateSelect(isNull, nullPtr, v, ".interface");
// return r-value
return new DImValue(_to, v);
}

if (fc->sym->classKind == ClassKind::cpp) {
Logger::println("C++ class/interface cast");
LLValue *v = tc->sym->classKind == ClassKind::cpp
? DtoRVal(val)
: LLConstant::getNullValue(getVoidPtrType());
LLValue *v =
tc->sym->classKind == ClassKind::cpp ? DtoRVal(val) : getNullPtr();
return new DImValue(_to, v);
}

Expand Down
9 changes: 3 additions & 6 deletions gen/dvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ bool DFuncValue::definedInFuncEntryBB() {
////////////////////////////////////////////////////////////////////////////////

DLValue::DLValue(Type *t, LLValue *v) : DValue(t, v) {
// v may be an addrspace qualified pointer so strip it before doing a pointer
// equality check.
assert(t->toBasetype()->ty == TY::Ttuple ||
stripAddrSpaces(v->getType()) == DtoPtrToType(t));
assert(t->toBasetype()->ty == TY::Ttuple || v->getType()->isPointerTy());
}

DRValue *DLValue::getRVal() {
Expand Down Expand Up @@ -163,11 +160,11 @@ DSpecialRefValue::DSpecialRefValue(Type *t, LLValue *v) : DLValue(v, t) {
}

DRValue *DSpecialRefValue::getRVal() {
return DLValue(type, DtoLoad(DtoPtrToType(type), val)).getRVal();
return DLValue(type, DtoLoad(getOpaquePtrType(), val)).getRVal();
}

DLValue *DSpecialRefValue::getLVal() {
return new DLValue(type, DtoLoad(DtoPtrToType(type), val));
return new DLValue(type, DtoLoad(getOpaquePtrType(), val));
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 0 additions & 5 deletions gen/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,6 @@ llvm::FunctionType *DtoFunctionType(FuncDeclaration *fdecl) {
if (AggregateDeclaration *ad = fdecl->isMember2()) {
IF_LOG Logger::println("isMember = this is: %s", ad->type->toChars());
dthis = ad->type;
LLType *thisty = DtoType(dthis);
// Logger::cout() << "this llvm type: " << *thisty << '\n';
if (ad->isStructDeclaration()) {
thisty = getPtrToType(thisty);
}
} else {
IF_LOG Logger::println("chars: %s type: %s kind: %s", fdecl->toChars(),
fdecl->type->toChars(), fdecl->kind());
Expand Down
15 changes: 7 additions & 8 deletions gen/llvmhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,12 @@ void DtoDeleteInterface(const Loc &loc, DValue *inst) {

void DtoDeleteArray(const Loc &loc, DValue *arr) {
llvm::Function *fn = getRuntimeFunction(loc, gIR->module, "_d_delarray_t");
llvm::FunctionType *fty = fn->getFunctionType();

// the TypeInfo argument must be null if the type has no dtor
Type *elementType = arr->type->nextOf();
bool hasDtor = (elementType->toBasetype()->ty == TY::Tstruct &&
elementType->needsDestruction());
LLValue *typeInfo = !hasDtor ? getNullPtr(fty->getParamType(1))
LLValue *typeInfo = !hasDtor ? getNullPtr()
: DtoTypeInfoOf(loc, elementType);

LLValue *lval = (arr->isLVal() ? DtoLVal(arr) : makeLValue(loc, arr));
Expand Down Expand Up @@ -478,7 +477,7 @@ DValue *DtoNullValue(Type *type, Loc loc) {
// dynamic array
if (basety == TY::Tarray) {
LLValue *len = DtoConstSize_t(0);
LLValue *ptr = getNullPtr(DtoPtrToType(basetype->nextOf()));
LLValue *ptr = getNullPtr();
return new DSliceValue(type, len, ptr);
}
error(loc, "`null` not known for type `%s`", type->toChars());
Expand Down Expand Up @@ -647,7 +646,7 @@ DValue *DtoCastVector(const Loc &loc, DValue *val, Type *to) {
LLValue *vector = DtoLVal(val);
IF_LOG Logger::cout() << "src: " << *vector << " to type: " << *tolltype
<< " (casting address)\n";
return new DLValue(to, DtoBitCast(vector, getPtrToType(tolltype)));
return new DLValue(to, vector);
}

LLValue *vector = DtoRVal(val);
Expand Down Expand Up @@ -900,7 +899,7 @@ void DtoVarDeclaration(VarDeclaration *vd) {
bool isRealAlloca = false;
LLType *lltype = DtoType(type); // void for noreturn
if (lltype->isVoidTy() || gDataLayout->getTypeSizeInBits(lltype) == 0) {
allocainst = llvm::ConstantPointerNull::get(getPtrToType(lltype));
allocainst = getNullPtr();
} else if (type != vd->type) {
allocainst = DtoAlloca(type, vd->toChars());
isRealAlloca = true;
Expand Down Expand Up @@ -1571,9 +1570,9 @@ DValue *DtoSymbolAddress(const Loc &loc, Type *type, Declaration *decl) {
if (tb->ty != TY::Tstruct) {
assert(tb->ty == TY::Tarray && tb->nextOf()->ty == TY::Tvoid);
const auto size = DtoConstSize_t(ad->structsize);
llvm::Constant *ptr = sd && sd->zeroInit()
? getNullValue(getVoidPtrType())
: getIrAggr(ad)->getInitSymbol();
LLConstant *ptr = sd && sd->zeroInit()
? static_cast<LLConstant *>(getNullPtr())
: getIrAggr(ad)->getInitSymbol();
return new DSliceValue(type, size, ptr);
}

Expand Down
6 changes: 4 additions & 2 deletions gen/moduleinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ llvm::Constant *buildImportedModules(Module *m, size_t &count) {
if (importInits.empty())
return nullptr;

const auto type = llvm::ArrayType::get(getVoidPtrType(), importInits.size());
const auto type =
llvm::ArrayType::get(getOpaquePtrType(), importInits.size());
return LLConstantArray::get(type, importInits);
}

Expand Down Expand Up @@ -207,7 +208,8 @@ llvm::Constant *buildLocalClasses(Module *m, size_t &count) {
if (classInfoRefs.empty())
return nullptr;

const auto type = llvm::ArrayType::get(getVoidPtrType(), classInfoRefs.size());
const auto type =
llvm::ArrayType::get(getOpaquePtrType(), classInfoRefs.size());
return LLConstantArray::get(type, classInfoRefs);
}
}
Expand Down
9 changes: 4 additions & 5 deletions gen/modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ LLFunction *build_module_reference_and_ctor(const char *moduleMangle,

// provide the default initializer
LLStructType *modulerefTy = DtoModuleReferenceType();
LLConstant *mrefvalues[] = {LLConstant::getNullValue(getVoidPtrType()),
moduleinfo};
LLConstant *mrefvalues[] = {getNullPtr(), moduleinfo};
LLConstant *thismrefinit = LLConstantStruct::get(
modulerefTy, llvm::ArrayRef<LLConstant *>(mrefvalues));

Expand All @@ -149,10 +148,10 @@ LLFunction *build_module_reference_and_ctor(const char *moduleMangle,
// make sure _Dmodule_ref is declared
const auto mrefIRMangle = getIRMangledVarName("_Dmodule_ref", LINK::c);
LLConstant *mref = gIR->module.getNamedGlobal(mrefIRMangle);
LLType *modulerefPtrTy = getVoidPtrType();
LLType *ptrTy = getOpaquePtrType();
if (!mref) {
mref =
declareGlobal(Loc(), gIR->module, modulerefPtrTy, mrefIRMangle, false,
declareGlobal(Loc(), gIR->module, ptrTy, mrefIRMangle, false,
false, global.params.dllimport != DLLImport::none);
}

Expand All @@ -166,7 +165,7 @@ LLFunction *build_module_reference_and_ctor(const char *moduleMangle,
gIR->DBuilder.EmitModuleCTor(ctor, fname.c_str());

// get current beginning
LLValue *curbeg = builder.CreateLoad(modulerefPtrTy, mref, "current");
LLValue *curbeg = builder.CreateLoad(ptrTy, mref, "current");

// put current beginning as the next of this one
LLValue *gep = builder.CreateStructGEP(
Expand Down
12 changes: 6 additions & 6 deletions gen/ms-cxx-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ bool isCatchSwitchBlock(llvm::BasicBlock *bb) {

// routines for constructing the llvm types for MS RTTI structs.
llvm::StructType *getTypeDescriptorType(IRState &irs,
llvm::Constant *classInfoPtr,
llvm::StringRef TypeInfoString) {
llvm::SmallString<256> TDTypeName("rtti.TypeDescriptor");
TDTypeName += llvm::utostr(TypeInfoString.size());
Expand All @@ -151,9 +150,10 @@ llvm::StructType *getTypeDescriptorType(IRState &irs,
if (TypeDescriptorType)
return TypeDescriptorType;
auto int8Ty = LLType::getInt8Ty(gIR->context());
auto ptrTy = getOpaquePtrType();
llvm::Type *FieldTypes[] = {
classInfoPtr->getType(), // CGM.Int8PtrPtrTy,
getPtrToType(int8Ty), // CGM.Int8PtrTy,
ptrTy, // CGM.Int8PtrPtrTy,
ptrTy, // CGM.Int8PtrTy,
llvm::ArrayType::get(int8Ty, TypeInfoString.size() + 1)};
TypeDescriptorType =
llvm::StructType::create(gIR->context(), FieldTypes, TDTypeName);
Expand All @@ -163,7 +163,7 @@ llvm::StructType *getTypeDescriptorType(IRState &irs,
llvm::GlobalVariable *getTypeDescriptor(IRState &irs, ClassDeclaration *cd) {
if (cd->isCPPclass()) {
const char *name = target.cpp.typeInfoMangle(cd);
return declareGlobal(cd->loc, irs.module, getVoidPtrType(), name,
return declareGlobal(cd->loc, irs.module, getOpaquePtrType(), name,
/*isConstant*/ true, false,
/*useDLLImport*/ cd->isExport());
}
Expand All @@ -185,10 +185,10 @@ llvm::GlobalVariable *getTypeDescriptor(IRState &irs, ClassDeclaration *cd) {
// Declare and initialize the TypeDescriptor.
llvm::Constant *Fields[] = {
classInfoPtr, // VFPtr
llvm::ConstantPointerNull::get(getVoidPtrType()), // Runtime data
getNullPtr(), // Runtime data
llvm::ConstantDataArray::getString(gIR->context(), TypeNameString)};
llvm::StructType *TypeDescriptorType =
getTypeDescriptorType(irs, classInfoPtr, TypeNameString);
getTypeDescriptorType(irs, TypeNameString);

const LinkageWithCOMDAT lwc = {LLGlobalVariable::LinkOnceODRLinkage, true};
Var = defineGlobal(cd->loc, gIR->module, TypeDescName,
Expand Down
1 change: 0 additions & 1 deletion gen/ms-cxx-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "gen/irstate.h"

llvm::StructType *getTypeDescriptorType(IRState &irs,
llvm::Constant *classInfoPtr,
llvm::StringRef TypeInfoString);
llvm::GlobalVariable *getTypeDescriptor(IRState &irs, ClassDeclaration *cd);

Expand Down
13 changes: 5 additions & 8 deletions gen/nested.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ DValue *DtoNestedVariable(const Loc &loc, Type *astype, VarDeclaration *vd,
if (!fd) {
error(loc, "function `%s` cannot access frame of function `%s`",
irfunc->decl->toPrettyChars(), vdparent->toPrettyChars());
return new DLValue(astype, llvm::UndefValue::get(DtoPtrToType(astype)));
return new DLValue(astype, llvm::UndefValue::get(getOpaquePtrType()));
}

// is the nested variable in this scope?
Expand Down Expand Up @@ -128,10 +128,7 @@ DValue *DtoNestedVariable(const Loc &loc, Type *astype, VarDeclaration *vd,
if (irLocal->nestedIndex == -1) {
Logger::println(
"WARNING: isn't actually nested, using invalid null storage");
auto llType = DtoPtrToType(astype);
if (isSpecialRefVar(vd))
llType = llType->getPointerTo();
return makeVarDValue(astype, vd, llvm::ConstantPointerNull::get(llType));
return makeVarDValue(astype, vd, getNullPtr());
}

////////////////////////////////////
Expand Down Expand Up @@ -254,7 +251,7 @@ LLValue *DtoNestedContext(const Loc &loc, Dsymbol *sym) {
if (depth == -1 || (depth == 0 && !symfd->closureVars.empty())) {
Logger::println("function does not have context or creates its own "
"from scratch, returning null");
return llvm::ConstantPointerNull::get(getVoidPtrType());
return getNullPtr();
}
}

Expand Down Expand Up @@ -296,7 +293,7 @@ LLValue *DtoNestedContext(const Loc &loc, Dsymbol *sym) {
sym->toPrettyChars(), irFunc.decl->toPrettyChars());
fatal();
}
return llvm::ConstantPointerNull::get(getVoidPtrType());
return getNullPtr();
}

// The symbol may need a parent context of the current function.
Expand Down Expand Up @@ -528,7 +525,7 @@ void DtoCreateNestedContext(FuncGenState &funcGen) {
}
if (depth > 1) {
DtoMemCpy(frame, src, DtoConstSize_t((depth - 1) * target.ptrsize),
getABITypeAlign(getVoidPtrType()));
getABITypeAlign(getOpaquePtrType()));
}
// Copy nestArg into framelist; the outer frame is not in the list of
// pointers
Expand Down
Loading

0 comments on commit a3d4f32

Please sign in to comment.