Skip to content

Commit

Permalink
[clang] Remove no-op ptr-to-ptr bitcasts (NFC)
Browse files Browse the repository at this point in the history
Opaque ptr cleanup effort (NFC).
  • Loading branch information
JOE1994 committed Nov 7, 2023
1 parent 466dc5c commit 45ca24e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
36 changes: 11 additions & 25 deletions clang/lib/CodeGen/CGObjCGNU.cpp
Expand Up @@ -1784,7 +1784,7 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
}
}
if (!IsCOFF)
classFields.add(llvm::ConstantExpr::getBitCast(SuperClass, PtrTy));
classFields.add(SuperClass);
else
classFields.addNullPointer(PtrTy);
} else
Expand Down Expand Up @@ -1959,16 +1959,14 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
// Resolve the class aliases, if they exist.
// FIXME: Class pointer aliases shouldn't exist!
if (ClassPtrAlias) {
ClassPtrAlias->replaceAllUsesWith(
llvm::ConstantExpr::getBitCast(classStruct, IdTy));
ClassPtrAlias->replaceAllUsesWith(classStruct);
ClassPtrAlias->eraseFromParent();
ClassPtrAlias = nullptr;
}
if (auto Placeholder =
TheModule.getNamedGlobal(SymbolForClass(className)))
if (Placeholder != classStruct) {
Placeholder->replaceAllUsesWith(
llvm::ConstantExpr::getBitCast(classStruct, Placeholder->getType()));
Placeholder->replaceAllUsesWith(classStruct);
Placeholder->eraseFromParent();
classStruct->setName(SymbolForClass(className));
}
Expand Down Expand Up @@ -2415,7 +2413,7 @@ llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
false,
llvm::GlobalValue::ExternalLinkage,
nullptr, "__objc_id_type_info");
return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty);
return IDEHType;
}

const ObjCObjectPointerType *PT =
Expand All @@ -2429,9 +2427,8 @@ llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
std::string typeinfoName = "__objc_eh_typeinfo_" + className;

// Return the existing typeinfo if it exists
llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName);
if (typeinfo)
return llvm::ConstantExpr::getBitCast(typeinfo, PtrToInt8Ty);
if (llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName))
return typeinfo;

// Otherwise create it.

Expand Down Expand Up @@ -2495,9 +2492,7 @@ ConstantAddress CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
Fields.add(isa);
Fields.add(MakeConstantString(Str));
Fields.addInt(IntTy, Str.size());
llvm::Constant *ObjCStr =
Fields.finishAndCreateGlobal(".objc_str", Align);
ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
llvm::Constant *ObjCStr = Fields.finishAndCreateGlobal(".objc_str", Align);
ObjCStrings[Str] = ObjCStr;
ConstantStrings.push_back(ObjCStr);
return ConstantAddress(ObjCStr, Int8Ty, Align);
Expand Down Expand Up @@ -3060,8 +3055,7 @@ llvm::Constant *CGObjCGNU::GenerateClassStructure(
Elements.finishAndCreateGlobal(ClassSym, CGM.getPointerAlign(), false,
llvm::GlobalValue::ExternalLinkage);
if (ClassRef) {
ClassRef->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(Class,
ClassRef->getType()));
ClassRef->replaceAllUsesWith(Class);
ClassRef->removeFromParent();
Class->setName(ClassSym);
}
Expand Down Expand Up @@ -3227,9 +3221,7 @@ void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
Elements.add(PropertyList);
Elements.add(OptionalPropertyList);
ExistingProtocols[ProtocolName] =
llvm::ConstantExpr::getBitCast(
Elements.finishAndCreateGlobal(".objc_protocol", CGM.getPointerAlign()),
IdTy);
Elements.finishAndCreateGlobal(".objc_protocol", CGM.getPointerAlign());
}
void CGObjCGNU::GenerateProtocolHolderCategory() {
// Collect information about instance methods
Expand Down Expand Up @@ -3263,9 +3255,8 @@ void CGObjCGNU::GenerateProtocolHolderCategory() {
ProtocolList.finishAndCreateGlobal(".objc_protocol_list",
CGM.getPointerAlign()),
PtrTy);
Categories.push_back(llvm::ConstantExpr::getBitCast(
Elements.finishAndCreateGlobal("", CGM.getPointerAlign()),
PtrTy));
Categories.push_back(
Elements.finishAndCreateGlobal("", CGM.getPointerAlign()));
}

/// Libobjc2 uses a bitfield representation where small(ish) bitfields are
Expand Down Expand Up @@ -3736,7 +3727,6 @@ llvm::Function *CGObjCGNU::ModuleInitFunction() {

statics = allStaticsArray.finishAndCreateGlobal(".objc_statics_ptr",
CGM.getPointerAlign());
statics = llvm::ConstantExpr::getBitCast(statics, PtrTy);
}

// Array of classes, categories, and constant objects.
Expand Down Expand Up @@ -3799,9 +3789,6 @@ llvm::Function *CGObjCGNU::ModuleInitFunction() {
// FIXME: We're generating redundant loads and stores here!
llvm::Constant *selPtr = llvm::ConstantExpr::getGetElementPtr(
selectorList->getValueType(), selectorList, idxs);
// If selectors are defined as an opaque type, cast the pointer to this
// type.
selPtr = llvm::ConstantExpr::getBitCast(selPtr, SelectorTy);
selectorAliases[i]->replaceAllUsesWith(selPtr);
selectorAliases[i]->eraseFromParent();
}
Expand Down Expand Up @@ -3922,7 +3909,6 @@ llvm::Function *CGObjCGNU::ModuleInitFunction() {
llvm::Constant *TheClass =
TheModule.getGlobalVariable("_OBJC_CLASS_" + iter->first, true);
if (TheClass) {
TheClass = llvm::ConstantExpr::getBitCast(TheClass, PtrTy);
Builder.CreateCall(RegisterAlias,
{TheClass, MakeConstantString(iter->second)});
}
Expand Down
9 changes: 1 addition & 8 deletions clang/lib/CodeGen/CGVTables.cpp
Expand Up @@ -465,10 +465,6 @@ void CodeGenFunction::generateThunk(llvm::Function *Fn,

llvm::Constant *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);

// Fix up the function type for an unprototyped musttail call.
if (IsUnprototyped)
Callee = llvm::ConstantExpr::getBitCast(Callee, Fn->getType());

// Make the call and return the result.
EmitCallAndReturnForThunk(llvm::FunctionCallee(Fn->getFunctionType(), Callee),
&Thunk, IsUnprototyped);
Expand Down Expand Up @@ -537,11 +533,8 @@ llvm::Constant *CodeGenVTables::maybeEmitThunk(GlobalDecl GD,
Name.str(), &CGM.getModule());
CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn, /*IsThunk=*/false);

// If needed, replace the old thunk with a bitcast.
if (!OldThunkFn->use_empty()) {
llvm::Constant *NewPtrForOldDecl =
llvm::ConstantExpr::getBitCast(ThunkFn, OldThunkFn->getType());
OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
OldThunkFn->replaceAllUsesWith(ThunkFn);
}

// Remove the old thunk.
Expand Down

0 comments on commit 45ca24e

Please sign in to comment.