Skip to content

Commit 39dc009

Browse files
committed
Reviewer feedback
1 parent c53e595 commit 39dc009

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

llvm/lib/Transforms/Scalar/InferAlignment.cpp

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,6 @@
2121

2222
using namespace llvm;
2323

24-
static bool tryToPropagateAlign(Function &F, const DataLayout &DL) {
25-
bool Changed = false;
26-
27-
for (BasicBlock &BB : F) {
28-
// We need to reset the map for each block because alignment information
29-
// can't be propagated across blocks. This is because control flow could
30-
// be dependent on the address at runtime, making an alignment assumption
31-
// within one block not true in another. Some sort of dominator tree
32-
// approach could be better, but restricting within a basic block is correct
33-
// too.
34-
DenseMap<Value *, Align> BestBasePointerAligns;
35-
for (Instruction &I : BB) {
36-
if (auto *PtrOp = getLoadStorePointerOperand(&I)) {
37-
Align LoadStoreAlign = getLoadStoreAlignment(&I);
38-
APInt OffsetFromBase =
39-
APInt(DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
40-
PtrOp = PtrOp->stripAndAccumulateConstantOffsets(DL, OffsetFromBase, true);
41-
Align BasePointerAlign =
42-
commonAlignment(LoadStoreAlign, OffsetFromBase.getLimitedValue());
43-
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-
}
55-
}
56-
}
57-
}
58-
}
59-
return Changed;
60-
}
61-
6224
static bool tryToImproveAlign(
6325
const DataLayout &DL, Instruction *I,
6426
function_ref<Align(Value *PtrOp, Align OldAlign, Align PrefAlign)> Fn) {
@@ -95,23 +57,51 @@ bool inferAlignment(Function &F, AssumptionCache &AC, DominatorTree &DT) {
9557
}
9658
}
9759

98-
// Compute alignment from known bits.
9960
for (BasicBlock &BB : F) {
61+
// We need to reset the map for each block because alignment information
62+
// can't be propagated across blocks. This is because control flow could
63+
// be dependent on the address at runtime, making an alignment assumption
64+
// within one block not true in another. Some sort of dominator tree
65+
// approach could be better, but restricting within a basic block is correct
66+
// too.
67+
DenseMap<Value *, Align> BestBasePointerAligns;
68+
10069
for (Instruction &I : BB) {
70+
// Compute alignment from known bits.
10171
Changed |= tryToImproveAlign(
10272
DL, &I, [&](Value *PtrOp, Align OldAlign, Align PrefAlign) {
10373
KnownBits Known = computeKnownBits(PtrOp, DL, &AC, &I, &DT);
10474
unsigned TrailZ = std::min(Known.countMinTrailingZeros(),
10575
+Value::MaxAlignmentExponent);
10676
return Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));
10777
});
78+
79+
// Propagate alignment between loads and stores that originate from the
80+
// same base pointer
81+
Changed |= tryToImproveAlign(
82+
DL, &I, [&](Value *PtrOp, Align LoadStoreAlign, Align PrefAlign) {
83+
APInt OffsetFromBase =
84+
APInt(DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
85+
PtrOp = PtrOp->stripAndAccumulateConstantOffsets(DL, OffsetFromBase,
86+
true);
87+
Align BasePointerAlign = commonAlignment(
88+
LoadStoreAlign, OffsetFromBase.getLimitedValue());
89+
90+
auto [It, Inserted] =
91+
BestBasePointerAligns.try_emplace(PtrOp, BasePointerAlign);
92+
if (!Inserted) {
93+
if (It->second > BasePointerAlign) {
94+
Align BetterLoadStoreAlign = commonAlignment(
95+
It->second, OffsetFromBase.getLimitedValue());
96+
return BetterLoadStoreAlign;
97+
}
98+
It->second = BasePointerAlign;
99+
}
100+
return LoadStoreAlign;
101+
});
108102
}
109103
}
110104

111-
// Propagate alignment between loads and stores that originate from the same
112-
// base pointer
113-
Changed |= tryToPropagateAlign(F, DL);
114-
115105
return Changed;
116106
}
117107

0 commit comments

Comments
 (0)