Skip to content

[IRBuilder] Use AAMDNodes helper class in CreateMem* routines [nfc-ish] #139950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 51 additions & 80 deletions llvm/include/llvm/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,43 +608,33 @@ class IRBuilderBase {
/// Create and insert a memset to the specified pointer and the
/// specified value.
///
/// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
/// specified, it will be added to the instruction. Likewise with alias.scope
/// and noalias tags.
/// If the pointer isn't an i8*, it will be converted. If alias metadata is
/// specified, it will be added to the instruction.
CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size,
MaybeAlign Align, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr) {
return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
TBAATag, ScopeTag, NoAliasTag);
const AAMDNodes &AAInfo = AAMDNodes()) {
return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, AAInfo);
}

CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align,
bool isVolatile = false, MDNode *TBAATag = nullptr,
MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr);
bool isVolatile = false,
const AAMDNodes &AAInfo = AAMDNodes());

CallInst *CreateMemSetInline(Value *Dst, MaybeAlign DstAlign, Value *Val,
Value *Size, bool IsVolatile = false,
MDNode *TBAATag = nullptr,
MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr);
const AAMDNodes &AAInfo = AAMDNodes());

/// Create and insert an element unordered-atomic memset of the region of
/// memory starting at the given pointer to the given value.
///
/// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
/// specified, it will be added to the instruction. Likewise with alias.scope
/// and noalias tags.
CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
uint64_t Size, Align Alignment,
uint32_t ElementSize,
MDNode *TBAATag = nullptr,
MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr) {
return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size),
Align(Alignment), ElementSize,
TBAATag, ScopeTag, NoAliasTag);
/// If the pointer isn't an i8*, it will be converted. If alias metadata is
/// specified, it will be added to the instruction.
CallInst *
CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, uint64_t Size,
Align Alignment, uint32_t ElementSize,
const AAMDNodes &AAInfo = AAMDNodes()) {
return CreateElementUnorderedAtomicMemSet(
Ptr, Val, getInt64(Size), Align(Alignment), ElementSize, AAInfo);
}

CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
Expand All @@ -662,88 +652,72 @@ class IRBuilderBase {
/// Generate the IR for a call to the builtin free function.
CallInst *CreateFree(Value *Source, ArrayRef<OperandBundleDef> Bundles = {});

CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
Value *Size, Align Alignment,
uint32_t ElementSize,
MDNode *TBAATag = nullptr,
MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr);
CallInst *
CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, Value *Size,
Align Alignment, uint32_t ElementSize,
const AAMDNodes &AAInfo = AAMDNodes());

/// Create and insert a memcpy between the specified pointers.
///
/// If the pointers aren't i8*, they will be converted. If a TBAA tag is
/// specified, it will be added to the instruction. Likewise with alias.scope
/// If the pointers aren't i8*, they will be converted. If alias metadata is
/// specified, it will be added to the instruction.
/// and noalias tags.
CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
MaybeAlign SrcAlign, uint64_t Size,
bool isVolatile = false, MDNode *TBAATag = nullptr,
MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr) {
bool isVolatile = false,
const AAMDNodes &AAInfo = AAMDNodes()) {
return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
isVolatile, TBAATag, TBAAStructTag, ScopeTag,
NoAliasTag);
isVolatile, AAInfo);
}

CallInst *CreateMemTransferInst(
Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src,
MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
CallInst *CreateMemTransferInst(Intrinsic::ID IntrID, Value *Dst,
MaybeAlign DstAlign, Value *Src,
MaybeAlign SrcAlign, Value *Size,
bool isVolatile = false,
const AAMDNodes &AAInfo = AAMDNodes());

CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
MaybeAlign SrcAlign, Value *Size,
bool isVolatile = false, MDNode *TBAATag = nullptr,
MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr) {
bool isVolatile = false,
const AAMDNodes &AAInfo = AAMDNodes()) {
return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src,
SrcAlign, Size, isVolatile, TBAATag,
TBAAStructTag, ScopeTag, NoAliasTag);
SrcAlign, Size, isVolatile, AAInfo);
}

CallInst *
CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src,
MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) {
CallInst *CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src,
MaybeAlign SrcAlign, Value *Size,
bool isVolatile = false,
const AAMDNodes &AAInfo = AAMDNodes()) {
return CreateMemTransferInst(Intrinsic::memcpy_inline, Dst, DstAlign, Src,
SrcAlign, Size, isVolatile, TBAATag,
TBAAStructTag, ScopeTag, NoAliasTag);
SrcAlign, Size, isVolatile, AAInfo);
}

/// Create and insert an element unordered-atomic memcpy between the
/// specified pointers.
///
/// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
/// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
/// respectively.
///
/// If the pointers aren't i8*, they will be converted. If a TBAA tag is
/// specified, it will be added to the instruction. Likewise with alias.scope
/// and noalias tags.
/// If the pointers aren't i8*, they will be converted. If alias metadata is
/// specified, it will be added to the instruction.
CallInst *CreateElementUnorderedAtomicMemCpy(
Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
uint32_t ElementSize, MDNode *TBAATag = nullptr,
MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr);
uint32_t ElementSize, const AAMDNodes &AAInfo = AAMDNodes());

CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
MaybeAlign SrcAlign, uint64_t Size,
bool isVolatile = false, MDNode *TBAATag = nullptr,
MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr) {
bool isVolatile = false,
const AAMDNodes &AAInfo = AAMDNodes()) {
return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
isVolatile, TBAATag, ScopeTag, NoAliasTag);
isVolatile, AAInfo);
}

CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
MaybeAlign SrcAlign, Value *Size,
bool isVolatile = false, MDNode *TBAATag = nullptr,
MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr) {
bool isVolatile = false,
const AAMDNodes &AAInfo = AAMDNodes()) {
return CreateMemTransferInst(Intrinsic::memmove, Dst, DstAlign, Src,
SrcAlign, Size, isVolatile, TBAATag,
/*TBAAStructTag=*/nullptr, ScopeTag,
NoAliasTag);
SrcAlign, Size, isVolatile, AAInfo);
}

/// \brief Create and insert an element unordered-atomic memmove between the
Expand All @@ -752,14 +726,11 @@ class IRBuilderBase {
/// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
/// respectively.
///
/// If the pointers aren't i8*, they will be converted. If a TBAA tag is
/// specified, it will be added to the instruction. Likewise with alias.scope
/// and noalias tags.
/// If the pointers aren't i8*, they will be converted. If alias metadata is
/// specified, it will be added to the instruction.
CallInst *CreateElementUnorderedAtomicMemMove(
Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
uint32_t ElementSize, MDNode *TBAATag = nullptr,
MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
MDNode *NoAliasTag = nullptr);
uint32_t ElementSize, const AAMDNodes &AAInfo = AAMDNodes());

private:
CallInst *getReductionIntrinsic(Intrinsic::ID ID, Value *Src);
Expand Down
109 changes: 17 additions & 92 deletions llvm/lib/IR/IRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,58 +169,36 @@ Value *IRBuilderBase::CreateStepVector(Type *DstType, const Twine &Name) {

CallInst *IRBuilderBase::CreateMemSet(Value *Ptr, Value *Val, Value *Size,
MaybeAlign Align, bool isVolatile,
MDNode *TBAATag, MDNode *ScopeTag,
MDNode *NoAliasTag) {
const AAMDNodes &AAInfo) {
Value *Ops[] = {Ptr, Val, Size, getInt1(isVolatile)};
Type *Tys[] = {Ptr->getType(), Size->getType()};

CallInst *CI = CreateIntrinsic(Intrinsic::memset, Tys, Ops);

if (Align)
cast<MemSetInst>(CI)->setDestAlignment(*Align);

// Set the TBAA info if present.
if (TBAATag)
CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);

if (ScopeTag)
CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);

if (NoAliasTag)
CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);

CI->setAAMetadata(AAInfo);
return CI;
}

CallInst *IRBuilderBase::CreateMemSetInline(Value *Dst, MaybeAlign DstAlign,
Value *Val, Value *Size,
bool IsVolatile, MDNode *TBAATag,
MDNode *ScopeTag,
MDNode *NoAliasTag) {
bool IsVolatile,
const AAMDNodes &AAInfo) {
Value *Ops[] = {Dst, Val, Size, getInt1(IsVolatile)};
Type *Tys[] = {Dst->getType(), Size->getType()};

CallInst *CI = CreateIntrinsic(Intrinsic::memset_inline, Tys, Ops);

if (DstAlign)
cast<MemSetInst>(CI)->setDestAlignment(*DstAlign);

// Set the TBAA info if present.
if (TBAATag)
CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);

if (ScopeTag)
CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);

if (NoAliasTag)
CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);

CI->setAAMetadata(AAInfo);
return CI;
}

CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemSet(
Value *Ptr, Value *Val, Value *Size, Align Alignment, uint32_t ElementSize,
MDNode *TBAATag, MDNode *ScopeTag, MDNode *NoAliasTag) {
const AAMDNodes &AAInfo) {

Value *Ops[] = {Ptr, Val, Size, getInt32(ElementSize)};
Type *Tys[] = {Ptr->getType(), Size->getType()};
Expand All @@ -229,24 +207,15 @@ CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemSet(
CreateIntrinsic(Intrinsic::memset_element_unordered_atomic, Tys, Ops);

cast<AnyMemSetInst>(CI)->setDestAlignment(Alignment);

// Set the TBAA info if present.
if (TBAATag)
CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);

if (ScopeTag)
CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);

if (NoAliasTag)
CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);

CI->setAAMetadata(AAInfo);
return CI;
}

CallInst *IRBuilderBase::CreateMemTransferInst(
Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src,
MaybeAlign SrcAlign, Value *Size, bool isVolatile, MDNode *TBAATag,
MDNode *TBAAStructTag, MDNode *ScopeTag, MDNode *NoAliasTag) {
CallInst *IRBuilderBase::CreateMemTransferInst(Intrinsic::ID IntrID, Value *Dst,
MaybeAlign DstAlign, Value *Src,
MaybeAlign SrcAlign, Value *Size,
bool isVolatile,
const AAMDNodes &AAInfo) {
assert((IntrID == Intrinsic::memcpy || IntrID == Intrinsic::memcpy_inline ||
IntrID == Intrinsic::memmove) &&
"Unexpected intrinsic ID");
Expand All @@ -260,28 +229,13 @@ CallInst *IRBuilderBase::CreateMemTransferInst(
MCI->setDestAlignment(*DstAlign);
if (SrcAlign)
MCI->setSourceAlignment(*SrcAlign);

// Set the TBAA info if present.
if (TBAATag)
CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);

// Set the TBAA Struct info if present.
if (TBAAStructTag)
CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);

if (ScopeTag)
CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);

if (NoAliasTag)
CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);

MCI->setAAMetadata(AAInfo);
return CI;
}

CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemCpy(
Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
uint32_t ElementSize, MDNode *TBAATag, MDNode *TBAAStructTag,
MDNode *ScopeTag, MDNode *NoAliasTag) {
uint32_t ElementSize, const AAMDNodes &AAInfo) {
assert(DstAlign >= ElementSize &&
"Pointer alignment must be at least element size");
assert(SrcAlign >= ElementSize &&
Expand All @@ -296,21 +250,7 @@ CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemCpy(
auto *AMCI = cast<AnyMemCpyInst>(CI);
AMCI->setDestAlignment(DstAlign);
AMCI->setSourceAlignment(SrcAlign);

// Set the TBAA info if present.
if (TBAATag)
CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);

// Set the TBAA Struct info if present.
if (TBAAStructTag)
CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);

if (ScopeTag)
CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);

if (NoAliasTag)
CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);

AMCI->setAAMetadata(AAInfo);
return CI;
}

Expand Down Expand Up @@ -394,8 +334,7 @@ CallInst *IRBuilderBase::CreateFree(Value *Source,

CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemMove(
Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
uint32_t ElementSize, MDNode *TBAATag, MDNode *TBAAStructTag,
MDNode *ScopeTag, MDNode *NoAliasTag) {
uint32_t ElementSize, const AAMDNodes &AAInfo) {
assert(DstAlign >= ElementSize &&
"Pointer alignment must be at least element size");
assert(SrcAlign >= ElementSize &&
Expand All @@ -409,21 +348,7 @@ CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemMove(
// Set the alignment of the pointer args.
CI->addParamAttr(0, Attribute::getWithAlignment(CI->getContext(), DstAlign));
CI->addParamAttr(1, Attribute::getWithAlignment(CI->getContext(), SrcAlign));

// Set the TBAA info if present.
if (TBAATag)
CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);

// Set the TBAA Struct info if present.
if (TBAAStructTag)
CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);

if (ScopeTag)
CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);

if (NoAliasTag)
CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);

CI->setAAMetadata(AAInfo);
return CI;
}

Expand Down
Loading
Loading