Skip to content

Commit 524739e

Browse files
committed
[InstCombine] Fold align assume into load's !align metadata if possible.
If an alignment assumption is valid in the context of a corresponding load of the pointer the assumption applies to, the assumption can be replaced !align metadata on the load. The benefits of folding it into !align are that existing code makes better use of !align and it allows removing the now-redundant call instructions.
1 parent 76eda76 commit 524739e

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,12 +3076,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
30763076
// TODO: apply range metadata for range check patterns?
30773077
}
30783078

3079-
// Separate storage assumptions apply to the underlying allocations, not any
3080-
// particular pointer within them. When evaluating the hints for AA purposes
3081-
// we getUnderlyingObject them; by precomputing the answers here we can
3082-
// avoid having to do so repeatedly there.
30833079
for (unsigned Idx = 0; Idx < II->getNumOperandBundles(); Idx++) {
30843080
OperandBundleUse OBU = II->getOperandBundleAt(Idx);
3081+
3082+
// Separate storage assumptions apply to the underlying allocations, not any
3083+
// particular pointer within them. When evaluating the hints for AA purposes
3084+
// we getUnderlyingObject them; by precomputing the answers here we can
3085+
// avoid having to do so repeatedly there.
30853086
if (OBU.getTagName() == "separate_storage") {
30863087
assert(OBU.Inputs.size() == 2);
30873088
auto MaybeSimplifyHint = [&](const Use &U) {
@@ -3095,6 +3096,22 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
30953096
MaybeSimplifyHint(OBU.Inputs[0]);
30963097
MaybeSimplifyHint(OBU.Inputs[1]);
30973098
}
3099+
3100+
// Try to fold alignment assumption into a load's !align metadata, if the assumption is valid in the load's context.
3101+
if (OBU.getTagName() == "align" && OBU.Inputs.size() == 2) {
3102+
auto *LI = dyn_cast<LoadInst>(OBU.Inputs[0]);
3103+
if (!LI || !isValidAssumeForContext(II, LI, &DT, /*AllowEphemerals=*/true))
3104+
continue;
3105+
auto *Align = cast<ConstantInt>(OBU.Inputs[1]);
3106+
if (!isPowerOf2_64(Align->getZExtValue()))
3107+
continue;
3108+
LI->setMetadata(LLVMContext::MD_align,
3109+
MDNode::get(II->getContext(),
3110+
ValueAsMetadata::getConstant(
3111+
Align)));
3112+
auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
3113+
return New;
3114+
}
30983115
}
30993116

31003117
// Convert nonnull assume like:

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,9 @@ define i8 @assume_align_non_pow2(ptr %p) {
123123
ret i8 %v
124124
}
125125

126-
; TODO: Can fold alignment assumption into !align metadata on load.
127126
define ptr @fold_assume_align_pow2_of_loaded_pointer_into_align_metadata(ptr %p) {
128127
; CHECK-LABEL: @fold_assume_align_pow2_of_loaded_pointer_into_align_metadata(
129-
; CHECK-NEXT: [[P2:%.*]] = load ptr, ptr [[P:%.*]], align 8
130-
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[P2]], i64 8) ]
128+
; CHECK-NEXT: [[P2:%.*]] = load ptr, ptr [[P:%.*]], align 8, !align [[META0:![0-9]+]]
131129
; CHECK-NEXT: ret ptr [[P2]]
132130
;
133131
%p2 = load ptr, ptr %p
@@ -171,3 +169,6 @@ define ptr @dont_fold_assume_align_zero_of_loaded_pointer_into_align_metadata(pt
171169
call void @llvm.assume(i1 true) [ "align"(ptr %p2, i64 0) ]
172170
ret ptr %p2
173171
}
172+
;.
173+
; CHECK: [[META0]] = !{i64 8}
174+
;.

0 commit comments

Comments
 (0)