Skip to content

[InferAlignment][NFC] Unify Load/Store handling in tryToImproveAlign #112699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4952,6 +4952,16 @@ inline Align getLoadStoreAlignment(const Value *I) {
return cast<StoreInst>(I)->getAlign();
}

/// A helper function that set the alignment of load or store instruction.
inline void setLoadStoreAlignment(Value *I, Align NewAlign) {
assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&
"Expected Load or Store instruction");
if (auto *LI = dyn_cast<LoadInst>(I))
LI->setAlignment(NewAlign);
else
cast<StoreInst>(I)->setAlignment(NewAlign);
}

/// A helper function that returns the address space of the pointer operand of
/// load or store instruction.
inline unsigned getLoadStoreAddressSpace(const Value *I) {
Expand Down
21 changes: 7 additions & 14 deletions llvm/lib/Transforms/Scalar/InferAlignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,14 @@ using namespace llvm;
static bool tryToImproveAlign(
const DataLayout &DL, Instruction *I,
function_ref<Align(Value *PtrOp, Align OldAlign, Align PrefAlign)> Fn) {
if (auto *LI = dyn_cast<LoadInst>(I)) {
Value *PtrOp = LI->getPointerOperand();
Align OldAlign = LI->getAlign();
Align NewAlign = Fn(PtrOp, OldAlign, DL.getPrefTypeAlign(LI->getType()));
if (NewAlign > OldAlign) {
LI->setAlignment(NewAlign);
return true;
}
} else if (auto *SI = dyn_cast<StoreInst>(I)) {
Value *PtrOp = SI->getPointerOperand();
Value *ValOp = SI->getValueOperand();
Align OldAlign = SI->getAlign();
Align NewAlign = Fn(PtrOp, OldAlign, DL.getPrefTypeAlign(ValOp->getType()));

if (auto *PtrOp = getLoadStorePointerOperand(I)) {
Align OldAlign = getLoadStoreAlignment(I);
Align PrefAlign = DL.getPrefTypeAlign(getLoadStoreType(I));

Align NewAlign = Fn(PtrOp, OldAlign, PrefAlign);
if (NewAlign > OldAlign) {
SI->setAlignment(NewAlign);
setLoadStoreAlignment(I, NewAlign);
return true;
}
}
Expand Down
Loading