Skip to content

Commit

Permalink
[llvm] Remove uses of Type::getPointerTo() (NFC)
Browse files Browse the repository at this point in the history
Partial progress towards removing in-tree uses of `getPointerTo()`,
by employing the following options:

* Drop the call entirely if the sole purpose of it is to support
  a no-op bitcast (remove the no-op bitcast as well).

* Replace with `PointerType::get()`/`PointerType::getUnqual()`.

Also, remove no-op function `EmitBitCastOfLValueToProperType()`.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D154392
  • Loading branch information
JOE1994 committed Jul 8, 2023
1 parent 820be30 commit f69b9b7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 70 deletions.
58 changes: 15 additions & 43 deletions llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,7 @@ OpenMPIRBuilder::getOrCreateRuntimeFunction(Module &M, RuntimeFunction FnID) {

assert(Fn && "Failed to create OpenMP runtime function");

// Cast the function to the expected type if necessary
Constant *C = ConstantExpr::getBitCast(Fn, FnTy->getPointerTo());
return {FnTy, C};
return {FnTy, Fn};
}

Function *OpenMPIRBuilder::getOrCreateRuntimeFunctionPtr(RuntimeFunction FnID) {
Expand Down Expand Up @@ -1533,12 +1531,6 @@ OpenMPIRBuilder::createTask(const LocationDescription &Loc,
(Twine(OutlinedFn.getName()) + ".wrapper").str(),
FunctionType::get(Builder.getInt32Ty(), WrapperArgTys, false));
Function *WrapperFunc = dyn_cast<Function>(WrapperFuncVal.getCallee());
PointerType *WrapperFuncBitcastType =
FunctionType::get(Builder.getInt32Ty(),
{Builder.getInt32Ty(), Builder.getInt8PtrTy()}, false)
->getPointerTo();
Value *WrapperFuncBitcast =
ConstantExpr::getBitCast(WrapperFunc, WrapperFuncBitcastType);

// Emit the @__kmpc_omp_task_alloc runtime call
// The runtime call returns a pointer to an area where the task captured
Expand All @@ -1547,7 +1539,7 @@ OpenMPIRBuilder::createTask(const LocationDescription &Loc,
TaskAllocFn,
{/*loc_ref=*/Ident, /*gtid=*/ThreadID, /*flags=*/Flags,
/*sizeof_task=*/TaskSize, /*sizeof_shared=*/Builder.getInt64(0),
/*task_func=*/WrapperFuncBitcast});
/*task_func=*/WrapperFunc});

// Copy the arguments for outlined function
if (HasTaskData) {
Expand Down Expand Up @@ -1982,10 +1974,9 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createReductions(
BasicBlock *ReductionFuncBlock =
BasicBlock::Create(Module->getContext(), "", ReductionFunc);
Builder.SetInsertPoint(ReductionFuncBlock);
Value *LHSArrayPtr = Builder.CreateBitCast(ReductionFunc->getArg(0),
RedArrayTy->getPointerTo());
Value *RHSArrayPtr = Builder.CreateBitCast(ReductionFunc->getArg(1),
RedArrayTy->getPointerTo());
Value *LHSArrayPtr = ReductionFunc->getArg(0);
Value *RHSArrayPtr = ReductionFunc->getArg(1);

for (auto En : enumerate(ReductionInfos)) {
const ReductionInfo &RI = En.value();
Value *LHSI8PtrPtr = Builder.CreateConstInBoundsGEP2_64(
Expand Down Expand Up @@ -4438,7 +4429,8 @@ Value *OpenMPIRBuilder::getOMPCriticalRegionLock(StringRef CriticalName) {

Value *OpenMPIRBuilder::getSizeInBytes(Value *BasePtr) {
LLVMContext &Ctx = Builder.getContext();
Value *Null = Constant::getNullValue(BasePtr->getType()->getPointerTo());
Value *Null =
Constant::getNullValue(PointerType::getUnqual(BasePtr->getContext()));
Value *SizeGep =
Builder.CreateGEP(BasePtr->getType(), Null, Builder.getInt32(1));
Value *SizePtrToInt = Builder.CreatePtrToInt(SizeGep, Type::getInt64Ty(Ctx));
Expand Down Expand Up @@ -4499,7 +4491,8 @@ void OpenMPIRBuilder::emitMapperCall(const LocationDescription &Loc,
Value *ArgSizesGEP =
Builder.CreateInBoundsGEP(ArrI64Ty, MapperAllocas.ArgSizes,
{Builder.getInt32(0), Builder.getInt32(0)});
Value *NullPtr = Constant::getNullValue(Int8Ptr->getPointerTo());
Value *NullPtr =
Constant::getNullValue(PointerType::getUnqual(Int8Ptr->getContext()));
Builder.CreateCall(MapperFunc,
{SrcLocInfo, Builder.getInt64(DeviceID),
Builder.getInt32(NumOperands), ArgsBaseGEP, ArgsGEP,
Expand Down Expand Up @@ -4971,14 +4964,11 @@ OpenMPIRBuilder::createAtomicRead(const LocationDescription &Loc,
XLD->setAtomic(AO);
XRead = cast<Value>(XLD);
} else {
// We need to bitcast and perform atomic op as integer
unsigned Addrspace = cast<PointerType>(XTy)->getAddressSpace();
// We need to perform atomic op as integer
IntegerType *IntCastTy =
IntegerType::get(M.getContext(), XElemTy->getScalarSizeInBits());
Value *XBCast = Builder.CreateBitCast(
X.Var, IntCastTy->getPointerTo(Addrspace), "atomic.src.int.cast");
LoadInst *XLoad =
Builder.CreateLoad(IntCastTy, XBCast, X.IsVolatile, "omp.atomic.load");
Builder.CreateLoad(IntCastTy, X.Var, X.IsVolatile, "omp.atomic.load");
XLoad->setAtomic(AO);
if (XElemTy->isFloatingPointTy()) {
XRead = Builder.CreateBitCast(XLoad, XElemTy, "atomic.flt.cast");
Expand Down Expand Up @@ -5120,13 +5110,10 @@ std::pair<Value *, Value *> OpenMPIRBuilder::emitAtomicUpdate(
else
Res.second = emitRMWOpAsInstruction(Res.first, Expr, RMWOp);
} else {
unsigned Addrspace = cast<PointerType>(X->getType())->getAddressSpace();
IntegerType *IntCastTy =
IntegerType::get(M.getContext(), XElemTy->getScalarSizeInBits());
Value *XBCast =
Builder.CreateBitCast(X, IntCastTy->getPointerTo(Addrspace));
LoadInst *OldVal =
Builder.CreateLoad(IntCastTy, XBCast, X->getName() + ".atomic.load");
Builder.CreateLoad(IntCastTy, X, X->getName() + ".atomic.load");
OldVal->setAtomic(AO);
// CurBB
// | /---\
Expand All @@ -5147,14 +5134,7 @@ std::pair<Value *, Value *> OpenMPIRBuilder::emitAtomicUpdate(
Builder.SetInsertPoint(ContBB);
llvm::PHINode *PHI = Builder.CreatePHI(OldVal->getType(), 2);
PHI->addIncoming(OldVal, CurBB);
IntegerType *NewAtomicCastTy =
IntegerType::get(M.getContext(), XElemTy->getScalarSizeInBits());
bool IsIntTy = XElemTy->isIntegerTy();
Value *NewAtomicIntAddr =
(IsIntTy)
? NewAtomicAddr
: Builder.CreateBitCast(NewAtomicAddr,
NewAtomicCastTy->getPointerTo(Addrspace));
Value *OldExprVal = PHI;
if (!IsIntTy) {
if (XElemTy->isFloatingPointTy()) {
Expand All @@ -5168,15 +5148,11 @@ std::pair<Value *, Value *> OpenMPIRBuilder::emitAtomicUpdate(

Value *Upd = UpdateOp(OldExprVal, Builder);
Builder.CreateStore(Upd, NewAtomicAddr);
LoadInst *DesiredVal = Builder.CreateLoad(IntCastTy, NewAtomicIntAddr);
Value *XAddr =
(IsIntTy)
? X
: Builder.CreateBitCast(X, IntCastTy->getPointerTo(Addrspace));
LoadInst *DesiredVal = Builder.CreateLoad(IntCastTy, NewAtomicAddr);
AtomicOrdering Failure =
llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
AtomicCmpXchgInst *Result = Builder.CreateAtomicCmpXchg(
XAddr, PHI, DesiredVal, llvm::MaybeAlign(), AO, Failure);
X, PHI, DesiredVal, llvm::MaybeAlign(), AO, Failure);
Result->setVolatile(VolatileX);
Value *PreviousVal = Builder.CreateExtractValue(Result, /*Idxs=*/0);
Value *SuccessFailureVal = Builder.CreateExtractValue(Result, /*Idxs=*/1);
Expand Down Expand Up @@ -5256,15 +5232,11 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
AtomicOrdering Failure = AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
AtomicCmpXchgInst *Result = nullptr;
if (!IsInteger) {
unsigned Addrspace =
cast<PointerType>(X.Var->getType())->getAddressSpace();
IntegerType *IntCastTy =
IntegerType::get(M.getContext(), X.ElemTy->getScalarSizeInBits());
Value *XBCast =
Builder.CreateBitCast(X.Var, IntCastTy->getPointerTo(Addrspace));
Value *EBCast = Builder.CreateBitCast(E, IntCastTy);
Value *DBCast = Builder.CreateBitCast(D, IntCastTy);
Result = Builder.CreateAtomicCmpXchg(XBCast, EBCast, DBCast, MaybeAlign(),
Result = Builder.CreateAtomicCmpXchg(X.Var, EBCast, DBCast, MaybeAlign(),
AO, Failure);
} else {
Result =
Expand Down
11 changes: 4 additions & 7 deletions llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,16 +1210,15 @@ instCombineSVELD1(InstCombiner &IC, IntrinsicInst &II, const DataLayout &DL) {
Value *Pred = II.getOperand(0);
Value *PtrOp = II.getOperand(1);
Type *VecTy = II.getType();
Value *VecPtr = IC.Builder.CreateBitCast(PtrOp, VecTy->getPointerTo());

if (isAllActivePredicate(Pred)) {
LoadInst *Load = IC.Builder.CreateLoad(VecTy, VecPtr);
LoadInst *Load = IC.Builder.CreateLoad(VecTy, PtrOp);
Load->copyMetadata(II);
return IC.replaceInstUsesWith(II, Load);
}

CallInst *MaskedLoad =
IC.Builder.CreateMaskedLoad(VecTy, VecPtr, PtrOp->getPointerAlignment(DL),
IC.Builder.CreateMaskedLoad(VecTy, PtrOp, PtrOp->getPointerAlignment(DL),
Pred, ConstantAggregateZero::get(VecTy));
MaskedLoad->copyMetadata(II);
return IC.replaceInstUsesWith(II, MaskedLoad);
Expand All @@ -1230,17 +1229,15 @@ instCombineSVEST1(InstCombiner &IC, IntrinsicInst &II, const DataLayout &DL) {
Value *VecOp = II.getOperand(0);
Value *Pred = II.getOperand(1);
Value *PtrOp = II.getOperand(2);
Value *VecPtr =
IC.Builder.CreateBitCast(PtrOp, VecOp->getType()->getPointerTo());

if (isAllActivePredicate(Pred)) {
StoreInst *Store = IC.Builder.CreateStore(VecOp, VecPtr);
StoreInst *Store = IC.Builder.CreateStore(VecOp, PtrOp);
Store->copyMetadata(II);
return IC.eraseInstFromFunction(II);
}

CallInst *MaskedStore = IC.Builder.CreateMaskedStore(
VecOp, VecPtr, PtrOp->getPointerAlignment(DL), Pred);
VecOp, PtrOp, PtrOp->getPointerAlignment(DL), Pred);
MaskedStore->copyMetadata(II);
return IC.eraseInstFromFunction(II);
}
Expand Down
8 changes: 2 additions & 6 deletions llvm/lib/Target/X86/X86InterleavedAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,18 @@ void X86InterleavedAccessGroup::decompose(

// Decompose the load instruction.
LoadInst *LI = cast<LoadInst>(VecInst);
Type *VecBaseTy, *VecBasePtrTy;
Value *VecBasePtr;
Type *VecBaseTy;
unsigned int NumLoads = NumSubVectors;
// In the case of stride 3 with a vector of 32 elements load the information
// in the following way:
// [0,1...,VF/2-1,VF/2+VF,VF/2+VF+1,...,2VF-1]
unsigned VecLength = DL.getTypeSizeInBits(VecWidth);
Value *VecBasePtr = LI->getPointerOperand();
if (VecLength == 768 || VecLength == 1536) {
VecBaseTy = FixedVectorType::get(Type::getInt8Ty(LI->getContext()), 16);
VecBasePtrTy = VecBaseTy->getPointerTo(LI->getPointerAddressSpace());
VecBasePtr = Builder.CreateBitCast(LI->getPointerOperand(), VecBasePtrTy);
NumLoads = NumSubVectors * (VecLength / 384);
} else {
VecBaseTy = SubVecTy;
VecBasePtrTy = VecBaseTy->getPointerTo(LI->getPointerAddressSpace());
VecBasePtr = Builder.CreateBitCast(LI->getPointerOperand(), VecBasePtrTy);
}
// Generate N loads of T type.
assert(VecBaseTy->getPrimitiveSizeInBits().isKnownMultipleOf(8) &&
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/Target/X86/X86WinEHState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ Type *WinEHStatePass::getEHLinkRegistrationType() {
LLVMContext &Context = TheModule->getContext();
EHLinkRegistrationTy = StructType::create(Context, "EHRegistrationNode");
Type *FieldTys[] = {
EHLinkRegistrationTy->getPointerTo(0), // EHRegistrationNode *Next
PointerType::getUnqual(
EHLinkRegistrationTy->getContext()), // EHRegistrationNode *Next
Type::getInt8PtrTy(Context) // EXCEPTION_DISPOSITION (*Handler)(...)
};
EHLinkRegistrationTy->setBody(FieldTys, false);
Expand Down Expand Up @@ -404,11 +405,9 @@ Function *WinEHStatePass::generateLSDAInEAXThunk(Function *ParentFunc) {
BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", Trampoline);
IRBuilder<> Builder(EntryBB);
Value *LSDA = emitEHLSDA(Builder, ParentFunc);
Value *CastPersonality =
Builder.CreateBitCast(PersonalityFn, TargetFuncTy->getPointerTo());
auto AI = Trampoline->arg_begin();
Value *Args[5] = {LSDA, &*AI++, &*AI++, &*AI++, &*AI++};
CallInst *Call = Builder.CreateCall(TargetFuncTy, CastPersonality, Args);
CallInst *Call = Builder.CreateCall(TargetFuncTy, PersonalityFn, Args);
// Can't use musttail due to prototype mismatch, but we can use tail.
Call->setTailCall(true);
// Set inreg so we pass it in EAX.
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,15 +1650,14 @@ class LowerMatrixIntrinsics {
auto *ArrayTy = ArrayType::get(VT->getElementType(), VT->getNumElements());
AllocaInst *Alloca =
Builder.CreateAlloca(ArrayTy, Load->getPointerAddressSpace());
Value *BC = Builder.CreateBitCast(Alloca, VT->getPointerTo());

Builder.CreateMemCpy(BC, Alloca->getAlign(), Load->getPointerOperand(),
Builder.CreateMemCpy(Alloca, Alloca->getAlign(), Load->getPointerOperand(),
Load->getAlign(), LoadLoc.Size.getValue());
Builder.SetInsertPoint(Fusion, Fusion->begin());
PHINode *PHI = Builder.CreatePHI(Load->getPointerOperandType(), 3);
PHI->addIncoming(Load->getPointerOperand(), Check0);
PHI->addIncoming(Load->getPointerOperand(), Check1);
PHI->addIncoming(BC, Copy);
PHI->addIncoming(Alloca, Copy);

// Adjust DT.
DTUpdates.push_back({DT->Insert, Check0, Check1});
Expand Down
12 changes: 5 additions & 7 deletions llvm/unittests/IR/PatternMatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1756,18 +1756,16 @@ TEST_F(PatternMatchTest, VScale) {
DataLayout DL = M->getDataLayout();

Type *VecTy = ScalableVectorType::get(IRB.getInt8Ty(), 1);
Type *VecPtrTy = VecTy->getPointerTo();
Value *NullPtrVec = Constant::getNullValue(VecPtrTy);
Value *NullPtrVec =
Constant::getNullValue(PointerType::getUnqual(VecTy->getContext()));
Value *GEP = IRB.CreateGEP(VecTy, NullPtrVec, IRB.getInt64(1));
Value *PtrToInt = IRB.CreatePtrToInt(GEP, DL.getIntPtrType(GEP->getType()));
EXPECT_TRUE(match(PtrToInt, m_VScale()));

// This used to cause assertion failures when attempting to match m_VScale.
// With opaque pointers the bitcast is no longer present.
Type *VecTy2 = ScalableVectorType::get(IRB.getInt8Ty(), 2);
Value *NullPtrVec2 = Constant::getNullValue(VecTy2->getPointerTo());
Value *BitCast = IRB.CreateBitCast(NullPtrVec2, VecPtrTy);
Value *GEP2 = IRB.CreateGEP(VecTy, BitCast, IRB.getInt64(1));
Value *NullPtrVec2 =
Constant::getNullValue(PointerType::getUnqual(VecTy2->getContext()));
Value *GEP2 = IRB.CreateGEP(VecTy, NullPtrVec2, IRB.getInt64(1));
Value *PtrToInt2 =
IRB.CreatePtrToInt(GEP2, DL.getIntPtrType(GEP2->getType()));
EXPECT_TRUE(match(PtrToInt2, m_VScale()));
Expand Down

0 comments on commit f69b9b7

Please sign in to comment.