Skip to content

Commit 18fce21

Browse files
committed
[InstCombine] Remove redundant alignment assumptions.
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] llvm#108961
1 parent 48803bc commit 18fce21

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3199,12 +3199,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
31993199
// TODO: apply range metadata for range check patterns?
32003200
}
32013201

3202-
// Separate storage assumptions apply to the underlying allocations, not any
3203-
// particular pointer within them. When evaluating the hints for AA purposes
3204-
// we getUnderlyingObject them; by precomputing the answers here we can
3205-
// avoid having to do so repeatedly there.
32063202
for (unsigned Idx = 0; Idx < II->getNumOperandBundles(); Idx++) {
32073203
OperandBundleUse OBU = II->getOperandBundleAt(Idx);
3204+
3205+
// Separate storage assumptions apply to the underlying allocations, not
3206+
// any particular pointer within them. When evaluating the hints for AA
3207+
// purposes we getUnderlyingObject them; by precomputing the answers here
3208+
// we can avoid having to do so repeatedly there.
32083209
if (OBU.getTagName() == "separate_storage") {
32093210
assert(OBU.Inputs.size() == 2);
32103211
auto MaybeSimplifyHint = [&](const Use &U) {
@@ -3218,6 +3219,28 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
32183219
MaybeSimplifyHint(OBU.Inputs[0]);
32193220
MaybeSimplifyHint(OBU.Inputs[1]);
32203221
}
3222+
3223+
// Try to remove redundant alignment assumptions.
3224+
if (OBU.getTagName() == "align" && OBU.Inputs.size() == 2) {
3225+
RetainedKnowledge RK = getKnowledgeFromBundle(
3226+
*cast<AssumeInst>(II), II->bundle_op_info_begin()[Idx]);
3227+
if (!RK || RK.AttrKind != Attribute::Alignment ||
3228+
!isPowerOf2_64(RK.ArgValue))
3229+
continue;
3230+
3231+
// Try to get the instruction before the assumption to use as context.
3232+
Instruction *CtxI = nullptr;
3233+
if (CtxI && II->getParent()->begin() != II->getIterator())
3234+
CtxI = II->getPrevNode();
3235+
3236+
auto Known = computeKnownBits(RK.WasOn, 1, CtxI);
3237+
unsigned KnownAlign = 1 << Known.countMinTrailingZeros();
3238+
if (KnownAlign < RK.ArgValue)
3239+
continue;
3240+
3241+
auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
3242+
return New;
3243+
}
32213244
}
32223245

32233246
// Convert nonnull assume like:

llvm/test/Transforms/InstCombine/assume-align.ll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ define ptr @dont_fold_assume_align_zero_of_loaded_pointer_into_align_metadata(pt
175175
define ptr @redundant_assume_align_1(ptr %p) {
176176
; CHECK-LABEL: @redundant_assume_align_1(
177177
; CHECK-NEXT: [[P2:%.*]] = load ptr, ptr [[P:%.*]], align 8
178-
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[P2]], i32 1) ]
179178
; CHECK-NEXT: call void @foo(ptr [[P2]])
180179
; CHECK-NEXT: ret ptr [[P2]]
181180
;
@@ -189,7 +188,6 @@ define ptr @redundant_assume_align_1(ptr %p) {
189188
define ptr @redundant_assume_align_8_via_align_metadata(ptr %p) {
190189
; CHECK-LABEL: @redundant_assume_align_8_via_align_metadata(
191190
; CHECK-NEXT: [[P2:%.*]] = load ptr, ptr [[P:%.*]], align 8, !align [[META0:![0-9]+]]
192-
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[P2]], i32 8) ]
193191
; CHECK-NEXT: call void @foo(ptr [[P2]])
194192
; CHECK-NEXT: ret ptr [[P2]]
195193
;
@@ -214,8 +212,7 @@ define ptr @assume_align_16_via_align_metadata(ptr %p) {
214212

215213
define ptr @redundant_assume_align_8_via_align_attribute(ptr align 8 %p) {
216214
; CHECK-LABEL: @redundant_assume_align_8_via_align_attribute(
217-
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[P:%.*]], i32 8) ]
218-
; CHECK-NEXT: call void @foo(ptr [[P]])
215+
; CHECK-NEXT: call void @foo(ptr [[P:%.*]])
219216
; CHECK-NEXT: ret ptr [[P]]
220217
;
221218
call void @llvm.assume(i1 true) [ "align"(ptr %p, i32 8) ]

0 commit comments

Comments
 (0)