LLVM 20.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
21
22using namespace llvm;
23
25 const DataLayout &DL, Instruction *I,
26 function_ref<Align(Value *PtrOp, Align OldAlign, Align PrefAlign)> Fn) {
27
28 if (auto *PtrOp = getLoadStorePointerOperand(I)) {
29 Align OldAlign = getLoadStoreAlignment(I);
30 Align PrefAlign = DL.getPrefTypeAlign(getLoadStoreType(I));
31
32 Align NewAlign = Fn(PtrOp, OldAlign, PrefAlign);
33 if (NewAlign > OldAlign) {
34 setLoadStoreAlignment(I, NewAlign);
35 return true;
36 }
37 }
38 // TODO: Also handle memory intrinsics.
39 return false;
40}
41
43 const DataLayout &DL = F.getDataLayout();
44 bool Changed = false;
45
46 // Enforce preferred type alignment if possible. We do this as a separate
47 // pass first, because it may improve the alignments we infer below.
48 for (BasicBlock &BB : F) {
49 for (Instruction &I : BB) {
50 Changed |= tryToImproveAlign(
51 DL, &I, [&](Value *PtrOp, Align OldAlign, Align PrefAlign) {
52 if (PrefAlign > OldAlign)
53 return std::max(OldAlign,
54 tryEnforceAlignment(PtrOp, PrefAlign, DL));
55 return OldAlign;
56 });
57 }
58 }
59
60 // Compute alignment from known bits.
61 for (BasicBlock &BB : F) {
62 for (Instruction &I : BB) {
63 Changed |= tryToImproveAlign(
64 DL, &I, [&](Value *PtrOp, Align OldAlign, Align PrefAlign) {
65 KnownBits Known = computeKnownBits(PtrOp, DL, 0, &AC, &I, &DT);
66 unsigned TrailZ = std::min(Known.countMinTrailingZeros(),
68 return Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));
69 });
70 }
71 }
72
73 return Changed;
74}
75
80 inferAlignment(F, AC, DT);
81 // Changes to alignment shouldn't invalidated analyses.
83}
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:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
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:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
LLVM Value Representation.
Definition: Value.h:74
static constexpr unsigned MaxAlignmentExponent
The maximum alignment for instructions.
Definition: Value.h:810
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
Align getLoadStoreAlignment(const Value *I)
A helper function that returns the alignment of load or store instruction.
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:1529
Type * getLoadStoreType(const Value *I)
A helper function that returns the type of a load or store instruction.
void setLoadStoreAlignment(Value *I, Align NewAlign)
A helper function that set the alignment of load or store instruction.
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:234
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:43