[llvm] fb93659 - [MemoryBuiltins] Add field for alignment argument [NFC]
Philip Reames via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 10 09:15:30 PST 2022
Author: Bryce Wilson
Date: 2022-01-10T09:15:20-08:00
New Revision: fb936595faa44ad9c8d8550b05ea95e7be5f4703
URL: https://github.com/llvm/llvm-project/commit/fb936595faa44ad9c8d8550b05ea95e7be5f4703
DIFF: https://github.com/llvm/llvm-project/commit/fb936595faa44ad9c8d8550b05ea95e7be5f4703.diff
LOG: [MemoryBuiltins] Add field for alignment argument [NFC]
There are a few places where the alignment argument for AlignedAllocLike functions was previously hardcoded. This patch adds an getAllocAlignment function and a change to the MemoryBuiltin table to allow alignment arguments to be found generically.
This will shortly allow alignment inference on operator new's with align_val params and an extension to Attributor's HeapToStack. The former will follow shortly - I split Bryce's patch for purpose of having the large change be NFC. The later will be reviewed separately.
Differential Revision: https://reviews.llvm.org/D116851 (part 1 of 2)
Added:
Modified:
llvm/include/llvm/Analysis/MemoryBuiltins.h
llvm/lib/Analysis/MemoryBuiltins.cpp
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/MemoryBuiltins.h b/llvm/include/llvm/Analysis/MemoryBuiltins.h
index 95881bd2480e..fcd09744de2f 100644
--- a/llvm/include/llvm/Analysis/MemoryBuiltins.h
+++ b/llvm/include/llvm/Analysis/MemoryBuiltins.h
@@ -114,6 +114,9 @@ inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
// Properties of allocation functions
//
+/// Gets the alignment argument for an aligned_alloc-like function
+Value *getAllocAlignment(const CallBase *V, const TargetLibraryInfo *TLI);
+
/// If this allocation function initializes memory to a fixed value, return
/// said value in the requested type. Otherwise, return nullptr.
Constant *getInitialValueOfAllocation(const CallBase *Alloc,
diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp
index 86e711884e2e..c9f2eaac8e7a 100644
--- a/llvm/lib/Analysis/MemoryBuiltins.cpp
+++ b/llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -66,53 +66,51 @@ struct AllocFnsTy {
unsigned NumParams;
// First and Second size parameters (or -1 if unused)
int FstParam, SndParam;
+ // Alignment parameter for aligned_alloc and aligned new
+ int AlignParam;
};
// FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
// know which functions are nounwind, noalias, nocapture parameters, etc.
static const std::pair<LibFunc, AllocFnsTy> AllocationFnData[] = {
- {LibFunc_malloc, {MallocLike, 1, 0, -1}},
- {LibFunc_vec_malloc, {MallocLike, 1, 0, -1}},
- {LibFunc_valloc, {MallocLike, 1, 0, -1}},
- {LibFunc_Znwj, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
- {LibFunc_ZnwjRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
- {LibFunc_ZnwjSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new(unsigned int, align_val_t)
- {LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t, // new(unsigned int, align_val_t, nothrow)
- {MallocLike, 3, 0, -1}},
- {LibFunc_Znwm, {OpNewLike, 1, 0, -1}}, // new(unsigned long)
- {LibFunc_ZnwmRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned long, nothrow)
- {LibFunc_ZnwmSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new(unsigned long, align_val_t)
- {LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t, // new(unsigned long, align_val_t, nothrow)
- {MallocLike, 3, 0, -1}},
- {LibFunc_Znaj, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
- {LibFunc_ZnajRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
- {LibFunc_ZnajSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new[](unsigned int, align_val_t)
- {LibFunc_ZnajSt11align_val_tRKSt9nothrow_t, // new[](unsigned int, align_val_t, nothrow)
- {MallocLike, 3, 0, -1}},
- {LibFunc_Znam, {OpNewLike, 1, 0, -1}}, // new[](unsigned long)
- {LibFunc_ZnamRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned long, nothrow)
- {LibFunc_ZnamSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new[](unsigned long, align_val_t)
- {LibFunc_ZnamSt11align_val_tRKSt9nothrow_t, // new[](unsigned long, align_val_t, nothrow)
- {MallocLike, 3, 0, -1}},
- {LibFunc_msvc_new_int, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
- {LibFunc_msvc_new_int_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
- {LibFunc_msvc_new_longlong, {OpNewLike, 1, 0, -1}}, // new(unsigned long long)
- {LibFunc_msvc_new_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned long long, nothrow)
- {LibFunc_msvc_new_array_int, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
- {LibFunc_msvc_new_array_int_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
- {LibFunc_msvc_new_array_longlong, {OpNewLike, 1, 0, -1}}, // new[](unsigned long long)
- {LibFunc_msvc_new_array_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned long long, nothrow)
- {LibFunc_aligned_alloc, {AlignedAllocLike, 2, 1, -1}},
- {LibFunc_memalign, {AlignedAllocLike, 2, 1, -1}},
- {LibFunc_calloc, {CallocLike, 2, 0, 1}},
- {LibFunc_vec_calloc, {CallocLike, 2, 0, 1}},
- {LibFunc_realloc, {ReallocLike, 2, 1, -1}},
- {LibFunc_vec_realloc, {ReallocLike, 2, 1, -1}},
- {LibFunc_reallocf, {ReallocLike, 2, 1, -1}},
- {LibFunc_strdup, {StrDupLike, 1, -1, -1}},
- {LibFunc_strndup, {StrDupLike, 2, 1, -1}},
- {LibFunc___kmpc_alloc_shared, {MallocLike, 1, 0, -1}},
- // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
+ {LibFunc_malloc, {MallocLike, 1, 0, -1, -1}},
+ {LibFunc_vec_malloc, {MallocLike, 1, 0, -1, -1}},
+ {LibFunc_valloc, {MallocLike, 1, 0, -1, -1}},
+ {LibFunc_Znwj, {OpNewLike, 1, 0, -1, -1}}, // new(unsigned int)
+ {LibFunc_ZnwjRKSt9nothrow_t, {MallocLike, 2, 0, -1, -1}}, // new(unsigned int, nothrow)
+ {LibFunc_ZnwjSt11align_val_t, {OpNewLike, 2, 0, -1, -1}}, // new(unsigned int, align_val_t)
+ {LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t, {MallocLike, 3, 0, -1, -1}}, // new(unsigned int, align_val_t, nothrow)
+ {LibFunc_Znwm, {OpNewLike, 1, 0, -1, -1}}, // new(unsigned long)
+ {LibFunc_ZnwmRKSt9nothrow_t, {MallocLike, 2, 0, -1, -1}}, // new(unsigned long, nothrow)
+ {LibFunc_ZnwmSt11align_val_t, {OpNewLike, 2, 0, -1, -1}}, // new(unsigned long, align_val_t)
+ {LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t, {MallocLike, 3, 0, -1, -1}}, // new(unsigned long, align_val_t, nothrow)
+ {LibFunc_Znaj, {OpNewLike, 1, 0, -1, -1}}, // new[](unsigned int)
+ {LibFunc_ZnajRKSt9nothrow_t, {MallocLike, 2, 0, -1, -1}}, // new[](unsigned int, nothrow)
+ {LibFunc_ZnajSt11align_val_t, {OpNewLike, 2, 0, -1, -1}}, // new[](unsigned int, align_val_t)
+ {LibFunc_ZnajSt11align_val_tRKSt9nothrow_t, {MallocLike, 3, 0, -1, -1}}, // new[](unsigned int, align_val_t, nothrow)
+ {LibFunc_Znam, {OpNewLike, 1, 0, -1, -1}}, // new[](unsigned long)
+ {LibFunc_ZnamRKSt9nothrow_t, {MallocLike, 2, 0, -1, -1}}, // new[](unsigned long, nothrow)
+ {LibFunc_ZnamSt11align_val_t, {OpNewLike, 2, 0, -1, -1}}, // new[](unsigned long, align_val_t)
+ {LibFunc_ZnamSt11align_val_tRKSt9nothrow_t, {MallocLike, 3, 0, -1, -1}}, // new[](unsigned long, align_val_t, nothrow)
+ {LibFunc_msvc_new_int, {OpNewLike, 1, 0, -1, -1}}, // new(unsigned int)
+ {LibFunc_msvc_new_int_nothrow, {MallocLike, 2, 0, -1, -1}}, // new(unsigned int, nothrow)
+ {LibFunc_msvc_new_longlong, {OpNewLike, 1, 0, -1, -1}}, // new(unsigned long long)
+ {LibFunc_msvc_new_longlong_nothrow, {MallocLike, 2, 0, -1, -1}}, // new(unsigned long long, nothrow)
+ {LibFunc_msvc_new_array_int, {OpNewLike, 1, 0, -1, -1}}, // new[](unsigned int)
+ {LibFunc_msvc_new_array_int_nothrow, {MallocLike, 2, 0, -1, -1}}, // new[](unsigned int, nothrow)
+ {LibFunc_msvc_new_array_longlong, {OpNewLike, 1, 0, -1, -1}}, // new[](unsigned long long)
+ {LibFunc_msvc_new_array_longlong_nothrow, {MallocLike, 2, 0, -1, -1}}, // new[](unsigned long long, nothrow)
+ {LibFunc_aligned_alloc, {AlignedAllocLike, 2, 1, -1, 0}},
+ {LibFunc_memalign, {AlignedAllocLike, 2, 1, -1, 0}},
+ {LibFunc_calloc, {CallocLike, 2, 0, 1, -1}},
+ {LibFunc_vec_calloc, {CallocLike, 2, 0, 1, -1}},
+ {LibFunc_realloc, {ReallocLike, 2, 1, -1, -1}},
+ {LibFunc_vec_realloc, {ReallocLike, 2, 1, -1, -1}},
+ {LibFunc_reallocf, {ReallocLike, 2, 1, -1, -1}},
+ {LibFunc_strdup, {StrDupLike, 1, -1, -1, -1}},
+ {LibFunc_strndup, {StrDupLike, 2, 1, -1, -1}},
+ {LibFunc___kmpc_alloc_shared, {MallocLike, 1, 0, -1, -1}},
+ // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
};
static const Function *getCalledFunction(const Value *V,
@@ -219,6 +217,8 @@ static Optional<AllocFnsTy> getAllocationSize(const Value *V,
Result.NumParams = Callee->getNumOperands();
Result.FstParam = Args.first;
Result.SndParam = Args.second.getValueOr(-1);
+ // Allocsize has no way to specify an alignment argument
+ Result.AlignParam = -1;
return Result;
}
@@ -255,6 +255,15 @@ bool llvm::isAlignedAllocLikeFn(
return getAllocationData(V, AlignedAllocLike, GetTLI)
.hasValue();
}
+/// Gets the alignment argument for an aligned_alloc-like function
+Value *llvm::getAllocAlignment(const CallBase *V,
+ const TargetLibraryInfo *TLI) {
+ const Optional<AllocFnsTy> FnData = getAllocationData(V, AnyAlloc, TLI);
+ if (!FnData.hasValue() || FnData->AlignParam < 0) {
+ return nullptr;
+ }
+ return V->getOperand(FnData->AlignParam);
+}
/// Tests if a value is a call or invoke to a library function that
/// allocates zero-filled memory (such as calloc).
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 922aaad41a73..790424298b6e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2590,12 +2590,13 @@ void InstCombinerImpl::annotateAnyAllocSite(CallBase &Call, const TargetLibraryI
}
// Add alignment attribute if alignment is a power of two constant.
- if (!isAlignedAllocLikeFn(&Call, TLI))
+ Value *Alignment = getAllocAlignment(&Call, TLI);
+ if (!Alignment)
return;
- ConstantInt *Op0C = dyn_cast<ConstantInt>(Call.getOperand(0));
- if (Op0C && Op0C->getValue().ult(llvm::Value::MaximumAlignment)) {
- uint64_t AlignmentVal = Op0C->getZExtValue();
+ ConstantInt *AlignOpC = dyn_cast<ConstantInt>(Alignment);
+ if (AlignOpC && AlignOpC->getValue().ult(llvm::Value::MaximumAlignment)) {
+ uint64_t AlignmentVal = AlignOpC->getZExtValue();
if (llvm::isPowerOf2_64(AlignmentVal)) {
Call.removeRetAttr(Attribute::Alignment);
Call.addRetAttr(Attribute::getWithAlignment(Call.getContext(),
More information about the llvm-commits
mailing list