diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -4466,10 +4466,12 @@ Call.addAttribute(AttributeList::ReturnIndex, Attribute::getWithDereferenceableOrNullBytes( Call.getContext(), Op1C->getZExtValue())); - // Add alignment attribute if alignment is a power of two constant. + // Add an alignment attribute if the alignment is a power of two constant + // less than the maximum alignment. if (Op0C) { uint64_t AlignmentVal = Op0C->getZExtValue(); - if (llvm::isPowerOf2_64(AlignmentVal)) + if (isPowerOf2_64(AlignmentVal) && + Op0C->getValue().ule(Value::MaximumAlignment)) Call.addAttribute(AttributeList::ReturnIndex, Attribute::getWithAlignment(Call.getContext(), Align(AlignmentVal))); diff --git a/llvm/test/Transforms/InstCombine/deref-alloc-fns.ll b/llvm/test/Transforms/InstCombine/deref-alloc-fns.ll --- a/llvm/test/Transforms/InstCombine/deref-alloc-fns.ll +++ b/llvm/test/Transforms/InstCombine/deref-alloc-fns.ll @@ -38,19 +38,24 @@ ret i8* %call } -declare noalias i8* @foo(i8*, i8*, i8*) +declare noalias i8* @escape(i8*, i8*, i8*, i8*) define noalias i8* @aligned_alloc_dynamic_args(i64 %align, i64 %size) { ; CHECK-LABEL: @aligned_alloc_dynamic_args( ; CHECK-NEXT: tail call noalias dereferenceable_or_null(1024) i8* @aligned_alloc(i64 %{{.*}}, i64 1024) ; CHECK-NEXT: tail call noalias i8* @aligned_alloc(i64 0, i64 1024) ; CHECK-NEXT: tail call noalias i8* @aligned_alloc(i64 32, i64 %{{.*}}) +; CHECK-NEXT: tail call noalias dereferenceable_or_null(4096) i8* @aligned_alloc(i64 9223372036854775807, i64 4096) ; + ; No alignment attribute will be added in these cases. %call = tail call noalias i8* @aligned_alloc(i64 %align, i64 1024) %call_1 = tail call noalias i8* @aligned_alloc(i64 0, i64 1024) %call_2 = tail call noalias i8* @aligned_alloc(i64 32, i64 %size) + ; Alignment is more than the maximum alignment allowed. + ; 9223372036854775807 is 2^63 - 1. + %call_3 = tail call noalias i8* @aligned_alloc(i64 9223372036854775807, i64 4096) - call i8* @foo(i8* %call, i8* %call_1, i8* %call_2) + call i8* @escape(i8* %call, i8* %call_1, i8* %call_2, i8* %call_3) ret i8* %call }