Skip to content

Commit 8c60efe

Browse files
authored
[InferAlignment][NFC] Unify Load/Store handling in tryToImproveAlign (#112699)
Removes code duplication in tryToImproveAlign by unifying load and store instruction handling with getLoadStore helper functions.
1 parent f9d0789 commit 8c60efe

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

llvm/include/llvm/IR/Instructions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4960,6 +4960,16 @@ inline Align getLoadStoreAlignment(const Value *I) {
49604960
return cast<StoreInst>(I)->getAlign();
49614961
}
49624962

4963+
/// A helper function that set the alignment of load or store instruction.
4964+
inline void setLoadStoreAlignment(Value *I, Align NewAlign) {
4965+
assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&
4966+
"Expected Load or Store instruction");
4967+
if (auto *LI = dyn_cast<LoadInst>(I))
4968+
LI->setAlignment(NewAlign);
4969+
else
4970+
cast<StoreInst>(I)->setAlignment(NewAlign);
4971+
}
4972+
49634973
/// A helper function that returns the address space of the pointer operand of
49644974
/// load or store instruction.
49654975
inline unsigned getLoadStoreAddressSpace(const Value *I) {

llvm/lib/Transforms/Scalar/InferAlignment.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,14 @@ using namespace llvm;
2525
static bool tryToImproveAlign(
2626
const DataLayout &DL, Instruction *I,
2727
function_ref<Align(Value *PtrOp, Align OldAlign, Align PrefAlign)> Fn) {
28-
if (auto *LI = dyn_cast<LoadInst>(I)) {
29-
Value *PtrOp = LI->getPointerOperand();
30-
Align OldAlign = LI->getAlign();
31-
Align NewAlign = Fn(PtrOp, OldAlign, DL.getPrefTypeAlign(LI->getType()));
32-
if (NewAlign > OldAlign) {
33-
LI->setAlignment(NewAlign);
34-
return true;
35-
}
36-
} else if (auto *SI = dyn_cast<StoreInst>(I)) {
37-
Value *PtrOp = SI->getPointerOperand();
38-
Value *ValOp = SI->getValueOperand();
39-
Align OldAlign = SI->getAlign();
40-
Align NewAlign = Fn(PtrOp, OldAlign, DL.getPrefTypeAlign(ValOp->getType()));
28+
29+
if (auto *PtrOp = getLoadStorePointerOperand(I)) {
30+
Align OldAlign = getLoadStoreAlignment(I);
31+
Align PrefAlign = DL.getPrefTypeAlign(getLoadStoreType(I));
32+
33+
Align NewAlign = Fn(PtrOp, OldAlign, PrefAlign);
4134
if (NewAlign > OldAlign) {
42-
SI->setAlignment(NewAlign);
35+
setLoadStoreAlignment(I, NewAlign);
4336
return true;
4437
}
4538
}

0 commit comments

Comments
 (0)