Skip to content

Commit 70feb18

Browse files
committed
Unify Load/Store handling in tryToImproveAlign
1 parent a9ba1b6 commit 70feb18

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
@@ -4952,6 +4952,16 @@ inline Align getLoadStoreAlignment(const Value *I) {
49524952
return cast<StoreInst>(I)->getAlign();
49534953
}
49544954

4955+
/// A helper function that set the alignment of load or store instruction.
4956+
inline void setLoadStoreAlignment(Value *I, Align NewAlign) {
4957+
assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&
4958+
"Expected Load or Store instruction");
4959+
if (auto *LI = dyn_cast<LoadInst>(I))
4960+
LI->setAlignment(NewAlign);
4961+
else
4962+
cast<StoreInst>(I)->setAlignment(NewAlign);
4963+
}
4964+
49554965
/// A helper function that returns the address space of the pointer operand of
49564966
/// load or store instruction.
49574967
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)