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 @@ -2837,9 +2837,13 @@ 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(), - Align(AlignmentVal))); + Align ExistingAlign = Call.getRetAlign().valueOrOne(); + Align NewAlign = Align(AlignmentVal); + if (NewAlign > ExistingAlign) { + Call.removeRetAttr(Attribute::Alignment); + Call.addRetAttr( + Attribute::getWithAlignment(Call.getContext(), NewAlign)); + } } } } diff --git a/llvm/test/Transforms/InstCombine/InferAlignAttribute.ll b/llvm/test/Transforms/InstCombine/InferAlignAttribute.ll --- a/llvm/test/Transforms/InstCombine/InferAlignAttribute.ll +++ b/llvm/test/Transforms/InstCombine/InferAlignAttribute.ll @@ -26,12 +26,10 @@ ret i8* %call } -; BUG: we shouldn't narrow this alignment since we already had a stronger -; constraint, but we do. define i8* @dont_narrow_align_from_allocalign() { ; CHECK-LABEL: @dont_narrow_align_from_allocalign( ; CHECK-NEXT: entry: -; CHECK-NEXT: [[CALL:%.*]] = tail call align 8 i8* @my_aligned_alloc(i32 noundef 320, i32 noundef 8) +; CHECK-NEXT: [[CALL:%.*]] = tail call align 16 i8* @my_aligned_alloc(i32 noundef 320, i32 noundef 8) ; CHECK-NEXT: ret i8* [[CALL]] ; entry: