LLVM 24.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
15#include "llvm/ADT/APInt.h"
20#include "llvm/IR/Instruction.h"
27
28using namespace llvm;
29using namespace llvm::PatternMatch;
30
32 const DataLayout &DL, Instruction *I,
33 function_ref<Align(Value *PtrOp, Align OldAlign, Align PrefAlign)> Fn) {
34
35 if (auto *PtrOp = getLoadStorePointerOperand(I)) {
36 Align OldAlign = getLoadStoreAlignment(I);
37 Align PrefAlign = DL.getPrefTypeAlign(getLoadStoreType(I));
38
39 Align NewAlign = Fn(PtrOp, OldAlign, PrefAlign);
40 if (NewAlign > OldAlign) {
41 setLoadStoreAlignment(I, NewAlign);
42 return true;
43 }
44 }
45
46 Value *PtrOp;
47 const APInt *Const;
48 if (match(I, m_And(m_PtrToIntOrAddr(m_Value(PtrOp)), m_APInt(Const)))) {
49 Align ActualAlign = Fn(PtrOp, Align(1), Align(1));
50 if (Const->ult(ActualAlign.value())) {
51 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
52 return true;
53 }
54 if (Const->uge(
55 APInt::getBitsSetFrom(Const->getBitWidth(), Log2(ActualAlign)))) {
56 I->replaceAllUsesWith(I->getOperand(0));
57 return true;
58 }
59 }
60 if (match(I, m_Trunc(m_PtrToIntOrAddr(m_Value(PtrOp))))) {
61 Align ActualAlign = Fn(PtrOp, Align(1), Align(1));
62 if (Log2(ActualAlign) >= I->getType()->getScalarSizeInBits()) {
63 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
64 return true;
65 }
66 }
67
69 if (!II)
70 return false;
71
72 if (!isa<MemIntrinsic>(II) &&
73 II->getIntrinsicID() != Intrinsic::masked_load &&
74 II->getIntrinsicID() != Intrinsic::masked_store)
75 return false;
76
77 bool Changed = false;
78 for (unsigned ArgNo = 0; ArgNo != II->arg_size(); ++ArgNo) {
79 Value *Arg = II->getArgOperand(ArgNo);
80 if (!Arg->getType()->isPointerTy())
81 continue;
82
83 Align OldAlign = II->getParamAlign(ArgNo).valueOrOne();
84 Align NewAlign = Fn(Arg, OldAlign, Align(1));
85 if (NewAlign <= OldAlign)
86 continue;
87
88 II->addParamAttr(ArgNo,
89 Attribute::getWithAlignment(II->getContext(), NewAlign));
90 Changed = true;
91 }
92 return Changed;
93}
94
95using ScopedHT =
98 // If BB is nullptr, the BB is processed.
100 DomTreeNode::const_iterator Iter;
101 DomTreeNode::const_iterator End;
103
105 : BB(N->getBlock()), Iter(N->begin()), End(N->end()), Scope(Table) {}
106};
107
109 const DataLayout &DL = F.getDataLayout();
110 bool Changed = false;
111
112 // Enforce preferred type alignment if possible. We do this as a separate
113 // pass first, because it may improve the alignments we infer below.
114 for (BasicBlock &BB : F) {
115 for (Instruction &I : BB) {
117 DL, &I, [&](Value *PtrOp, Align OldAlign, Align PrefAlign) {
118 if (PrefAlign > OldAlign)
119 return std::max(OldAlign,
120 tryEnforceAlignment(PtrOp, PrefAlign, DL));
121 return OldAlign;
122 });
123 }
124 }
125
126 // Compute alignment from known bits.
127 auto InferFromKnownBits = [&](Instruction &I, Value *PtrOp) {
128 KnownBits Known = computeKnownBits(PtrOp, DL, &AC, &I, &DT);
129 unsigned TrailZ =
130 std::min(Known.countMinTrailingZeros(), +Value::MaxAlignmentExponent);
131 return Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));
132 };
133
134 // Propagate alignment between loads and stores that originate from the
135 // same base pointer.
136 ScopedHT BestBasePointerAligns;
137 auto InferFromBasePointer = [&](Value *PtrOp, Align LoadStoreAlign) {
138 APInt OffsetFromBase(DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
139 PtrOp = PtrOp->stripAndAccumulateConstantOffsets(DL, OffsetFromBase, true);
140 // Derive the base pointer alignment from the load/store alignment
141 // and the offset from the base pointer.
142 Align BasePointerAlign =
143 commonAlignment(LoadStoreAlign, OffsetFromBase.getLimitedValue());
144
145 if (auto BestAlign = BestBasePointerAligns.lookup(PtrOp);
146 BestAlign != Align()) {
147 // If the stored base pointer alignment is better than the
148 // base pointer alignment we derived, we may be able to use it
149 // to improve the load/store alignment. If not, store the
150 // improved base pointer alignment for future iterations.
151 if (BestAlign > BasePointerAlign) {
152 Align BetterLoadStoreAlign =
153 commonAlignment(BestAlign, OffsetFromBase.getLimitedValue());
154 return BetterLoadStoreAlign;
155 }
156 }
157
158 BestBasePointerAligns.insert(PtrOp, BasePointerAlign);
159 return LoadStoreAlign;
160 };
161
162 // AlignmentScope is unmovable.
163 std::list<AlignmentScope> Stack;
164 Stack.emplace_back(DT.getRootNode(), BestBasePointerAligns);
165 while (!Stack.empty()) {
166 AlignmentScope &Top = Stack.back();
167 if (Top.BB) {
168 for (Instruction &I : *Top.BB) {
170 DL, &I, [&](Value *PtrOp, Align OldAlign, Align PrefAlign) {
171 return std::max(InferFromKnownBits(I, PtrOp),
172 InferFromBasePointer(PtrOp, OldAlign));
173 });
174 }
175 Top.BB = nullptr;
176 }
177
178 if (Top.Iter != Top.End)
179 Stack.emplace_back(*Top.Iter++, BestBasePointerAligns);
180 else
181 Stack.pop_back();
182 }
183
184 return Changed;
185}
186
191 inferAlignment(F, AC, DT);
192 // Changes to alignment shouldn't invalidated analyses.
193 return PreservedAnalyses::all();
194}
This file implements a class to represent arbitrary precision integral constant values and operations...
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)
ScopedHashTable< Value *, Align, DenseMapInfo< Value * >, BumpPtrAllocator > ScopedHT
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition APInt.h:472
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:283
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
static LLVM_ABI Attribute getWithAlignment(LLVMContext &Context, Align Alignment)
Return a uniquified Attribute object that has the specific alignment set.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
A wrapper class for inspecting calls to intrinsic functions.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void insert(const K &Key, const V &Val)
V lookup(const K &Key) const
ScopedHashTableScope< Value *, Align, DenseMapInfo< Value * >, BumpPtrAllocator > ScopeTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
static constexpr unsigned MaxAlignmentExponent
The maximum alignment for instructions.
Definition Value.h:798
An efficient, type-erasing, non-owning reference to a callable.
Changed
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
bool match(Val *V, const Pattern &P)
auto m_Value()
Match an arbitrary value and ignore it.
This is an optimization pass for GlobalISel generic memory operations.
@ Known
Known to have no common set bits.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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.
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:65
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI 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:1522
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
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.
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:390
#define N
ScopedHT::ScopeTy Scope
DomTreeNode::const_iterator End
AlignmentScope(DomTreeNode *N, ScopedHT &Table)
BasicBlock * BB
DomTreeNode::const_iterator Iter
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)