LLVM 19.0.0git
InferAlignment.cpp
Go to the documentation of this file.
1//===- InferAlignment.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Infer alignment for load, stores and other memory operations based on
10// trailing zero known bits information.
11//
12//===----------------------------------------------------------------------===//
13
22
23using namespace llvm;
24
26 const DataLayout &DL, Instruction *I,
27 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()));
41 if (NewAlign > OldAlign) {
42 SI->setAlignment(NewAlign);
43 return true;
44 }
45 }
46 // TODO: Also handle memory intrinsics.
47 return false;
48}
49
51 const DataLayout &DL = F.getParent()->getDataLayout();
52 bool Changed = false;
53
54 // Enforce preferred type alignment if possible. We do this as a separate
55 // pass first, because it may improve the alignments we infer below.
56 for (BasicBlock &BB : F) {
57 for (Instruction &I : BB) {
58 Changed |= tryToImproveAlign(
59 DL, &I, [&](Value *PtrOp, Align OldAlign, Align PrefAlign) {
60 if (PrefAlign > OldAlign)
61 return std::max(OldAlign,
62 tryEnforceAlignment(PtrOp, PrefAlign, DL));
63 return OldAlign;
64 });
65 }
66 }
67
68 // Compute alignment from known bits.
69 for (BasicBlock &BB : F) {
70 for (Instruction &I : BB) {
71 Changed |= tryToImproveAlign(
72 DL, &I, [&](Value *PtrOp, Align OldAlign, Align PrefAlign) {
73 KnownBits Known = computeKnownBits(PtrOp, DL, 0, &AC, &I, &DT);
74 unsigned TrailZ = std::min(Known.countMinTrailingZeros(),
76 return Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));
77 });
78 }
79 }
80
81 return Changed;
82}
83
88 inferAlignment(F, AC, DT);
89 // Changes to alignment shouldn't invalidated analyses.
91}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool tryToImproveAlign(const DataLayout &DL, Instruction *I, function_ref< Align(Value *PtrOp, Align OldAlign, Align PrefAlign)> Fn)
bool inferAlignment(Function &F, AssumptionCache &AC, DominatorTree &DT)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:473
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
static constexpr unsigned MaxAlignmentExponent
The maximum alignment for instructions.
Definition: Value.h:806
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
Align tryEnforceAlignment(Value *V, Align PrefAlign, const DataLayout &DL)
If the specified pointer points to an object that we control, try to modify the object's alignment to...
Definition: Local.cpp:1495
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:238
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40