Skip to content

Commit c53e595

Browse files
committed
Reviewer feedback
1 parent d696eee commit c53e595

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

llvm/lib/Transforms/Scalar/InferAlignment.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,23 @@ static bool tryToPropagateAlign(Function &F, const DataLayout &DL) {
3535
for (Instruction &I : BB) {
3636
if (auto *PtrOp = getLoadStorePointerOperand(&I)) {
3737
Align LoadStoreAlign = getLoadStoreAlignment(&I);
38-
APInt OffsetFromBase = APInt(
39-
DL.getIndexSizeInBits(PtrOp->getType()->getPointerAddressSpace()),
40-
0);
41-
PtrOp = PtrOp->stripAndAccumulateInBoundsConstantOffsets(
42-
DL, OffsetFromBase);
38+
APInt OffsetFromBase =
39+
APInt(DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
40+
PtrOp = PtrOp->stripAndAccumulateConstantOffsets(DL, OffsetFromBase, true);
4341
Align BasePointerAlign =
4442
commonAlignment(LoadStoreAlign, OffsetFromBase.getLimitedValue());
4543

46-
if (BestBasePointerAligns.count(PtrOp) &&
47-
BestBasePointerAligns[PtrOp] > BasePointerAlign) {
48-
Align BetterLoadStoreAlign = commonAlignment(
49-
BestBasePointerAligns[PtrOp], OffsetFromBase.getLimitedValue());
50-
setLoadStoreAlignment(&I, BetterLoadStoreAlign);
51-
Changed = true;
52-
} else {
53-
BestBasePointerAligns[PtrOp] = BasePointerAlign;
44+
auto [It, Inserted] =
45+
BestBasePointerAligns.try_emplace(PtrOp, BasePointerAlign);
46+
if (!Inserted) {
47+
if (It->second > BasePointerAlign) {
48+
Align BetterLoadStoreAlign =
49+
commonAlignment(It->second, OffsetFromBase.getLimitedValue());
50+
setLoadStoreAlignment(&I, BetterLoadStoreAlign);
51+
Changed = true;
52+
} else {
53+
It->second = BasePointerAlign;
54+
}
5455
}
5556
}
5657
}

llvm/test/Transforms/InferAlignment/propagate-from-other-load-stores.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
; Test that we can propagate the align 16 to the load and store that are set to align 4
99
; ------------------------------------------------------------------------------
1010

11-
; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite)
12-
define void @prop_align(ptr noundef readonly captures(none) %v, ptr noundef writeonly captures(none) initializes((0, 32)) %vout) local_unnamed_addr #0 {
11+
define void @prop_align(ptr %v, ptr %vout) {
1312
; CHECK-LABEL: define void @prop_align(
14-
; CHECK-SAME: ptr noundef readonly captures(none) [[V:%.*]], ptr noundef writeonly captures(none) initializes((0, 32)) [[VOUT:%.*]]) local_unnamed_addr {
13+
; CHECK-SAME: ptr [[V:%.*]], ptr [[VOUT:%.*]]) {
1514
; CHECK-NEXT: [[DOTUNPACK_UNPACK:%.*]] = load float, ptr [[V]], align 16
1615
; CHECK-NEXT: [[DOTUNPACK_ELT7:%.*]] = getelementptr inbounds nuw i8, ptr [[V]], i64 4
1716
; CHECK-NEXT: [[DOTUNPACK_UNPACK8:%.*]] = load float, ptr [[DOTUNPACK_ELT7]], align 4
@@ -81,10 +80,9 @@ define void @prop_align(ptr noundef readonly captures(none) %v, ptr noundef writ
8180
; Test that alignment is not propagated from a source that does not dominate the destination
8281
; ------------------------------------------------------------------------------
8382

84-
; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite)
85-
define void @no_prop_align(ptr noundef readonly captures(none) %v, ptr noundef writeonly captures(none) initializes((0, 32)) %vout, i1 %cond) local_unnamed_addr #0 {
83+
define void @no_prop_align(ptr %v, ptr %vout, i1 %cond) {
8684
; CHECK-LABEL: define void @no_prop_align(
87-
; CHECK-SAME: ptr noundef readonly captures(none) [[V:%.*]], ptr noundef writeonly captures(none) initializes((0, 32)) [[VOUT:%.*]], i1 [[COND:%.*]]) local_unnamed_addr {
85+
; CHECK-SAME: ptr [[V:%.*]], ptr [[VOUT:%.*]], i1 [[COND:%.*]]) {
8886
; CHECK-NEXT: br i1 [[COND]], label %[[BRANCH1:.*]], label %[[BRANCH2:.*]]
8987
; CHECK: [[BRANCH1]]:
9088
; CHECK-NEXT: [[DOTUNPACK_UNPACK:%.*]] = load float, ptr [[V]], align 16

0 commit comments

Comments
 (0)