Skip to content

Commit

Permalink
[InstCombine] Remove redundant alignment assumptions.
Browse files Browse the repository at this point in the history
Use known bits to remove redundant alignment assumptions.

Libc++ now adds alignment assumptions for std::vector::begin() and
std::vector::end(), so I  expect we will see quite a bit more
assumptions in C++ [1]. Try to clean up some redundant ones to start
with.

[1] #108961
  • Loading branch information
fhahn committed Jan 17, 2025
1 parent 48803bc commit 18fce21
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
31 changes: 27 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3199,12 +3199,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
// TODO: apply range metadata for range check patterns?
}

// Separate storage assumptions apply to the underlying allocations, not any
// particular pointer within them. When evaluating the hints for AA purposes
// we getUnderlyingObject them; by precomputing the answers here we can
// avoid having to do so repeatedly there.
for (unsigned Idx = 0; Idx < II->getNumOperandBundles(); Idx++) {
OperandBundleUse OBU = II->getOperandBundleAt(Idx);

// Separate storage assumptions apply to the underlying allocations, not
// any particular pointer within them. When evaluating the hints for AA
// purposes we getUnderlyingObject them; by precomputing the answers here
// we can avoid having to do so repeatedly there.
if (OBU.getTagName() == "separate_storage") {
assert(OBU.Inputs.size() == 2);
auto MaybeSimplifyHint = [&](const Use &U) {
Expand All @@ -3218,6 +3219,28 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
MaybeSimplifyHint(OBU.Inputs[0]);
MaybeSimplifyHint(OBU.Inputs[1]);
}

// Try to remove redundant alignment assumptions.
if (OBU.getTagName() == "align" && OBU.Inputs.size() == 2) {
RetainedKnowledge RK = getKnowledgeFromBundle(
*cast<AssumeInst>(II), II->bundle_op_info_begin()[Idx]);
if (!RK || RK.AttrKind != Attribute::Alignment ||
!isPowerOf2_64(RK.ArgValue))
continue;

// Try to get the instruction before the assumption to use as context.
Instruction *CtxI = nullptr;
if (CtxI && II->getParent()->begin() != II->getIterator())
CtxI = II->getPrevNode();

auto Known = computeKnownBits(RK.WasOn, 1, CtxI);
unsigned KnownAlign = 1 << Known.countMinTrailingZeros();
if (KnownAlign < RK.ArgValue)
continue;

auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
return New;
}
}

// Convert nonnull assume like:
Expand Down
5 changes: 1 addition & 4 deletions llvm/test/Transforms/InstCombine/assume-align.ll
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ define ptr @dont_fold_assume_align_zero_of_loaded_pointer_into_align_metadata(pt
define ptr @redundant_assume_align_1(ptr %p) {
; CHECK-LABEL: @redundant_assume_align_1(
; CHECK-NEXT: [[P2:%.*]] = load ptr, ptr [[P:%.*]], align 8
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[P2]], i32 1) ]
; CHECK-NEXT: call void @foo(ptr [[P2]])
; CHECK-NEXT: ret ptr [[P2]]
;
Expand All @@ -189,7 +188,6 @@ define ptr @redundant_assume_align_1(ptr %p) {
define ptr @redundant_assume_align_8_via_align_metadata(ptr %p) {
; CHECK-LABEL: @redundant_assume_align_8_via_align_metadata(
; CHECK-NEXT: [[P2:%.*]] = load ptr, ptr [[P:%.*]], align 8, !align [[META0:![0-9]+]]
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[P2]], i32 8) ]
; CHECK-NEXT: call void @foo(ptr [[P2]])
; CHECK-NEXT: ret ptr [[P2]]
;
Expand All @@ -214,8 +212,7 @@ define ptr @assume_align_16_via_align_metadata(ptr %p) {

define ptr @redundant_assume_align_8_via_align_attribute(ptr align 8 %p) {
; CHECK-LABEL: @redundant_assume_align_8_via_align_attribute(
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[P:%.*]], i32 8) ]
; CHECK-NEXT: call void @foo(ptr [[P]])
; CHECK-NEXT: call void @foo(ptr [[P:%.*]])
; CHECK-NEXT: ret ptr [[P]]
;
call void @llvm.assume(i1 true) [ "align"(ptr %p, i32 8) ]
Expand Down

0 comments on commit 18fce21

Please sign in to comment.