34#define DEBUG_TYPE "alignment-from-assumptions"
38 "Number of loads changed by alignment assumptions");
40 "Number of stores changed by alignment assumptions");
42 "Number of memory intrinsics changed by alignment assumptions");
51 const SCEV *AlignSCEV,
56 LLVM_DEBUG(
dbgs() <<
"\talignment relative to " << *AlignSCEV <<
" is "
57 << *DiffUnitsSCEV <<
" (diff: " << *DiffSCEV <<
")\n");
60 dyn_cast<SCEVConstant>(DiffUnitsSCEV)) {
61 int64_t DiffUnits = ConstDUSCEV->getValue()->getSExtValue();
67 return cast<SCEVConstant>(AlignSCEV)->getValue()->getAlignValue();
71 uint64_t DiffUnitsAbs = std::abs(DiffUnits);
73 return Align(DiffUnitsAbs);
88 if (isa<SCEVCouldNotCompute>(DiffSCEV))
100 << *AlignSCEV <<
" and offset " << *OffSCEV
101 <<
" using diff " << *DiffSCEV <<
"\n");
105 return *NewAlignment;
108 if (
const SCEVAddRecExpr *DiffARSCEV = dyn_cast<SCEVAddRecExpr>(DiffSCEV)) {
116 const SCEV *DiffStartSCEV = DiffARSCEV->getStart();
117 const SCEV *DiffIncSCEV = DiffARSCEV->getStepRecurrence(*SE);
120 << *DiffStartSCEV <<
" and inc " << *DiffIncSCEV <<
"\n");
135 if (!NewAlignment || !NewIncAlignment)
138 const Align NewAlign = *NewAlignment;
139 const Align NewIncAlign = *NewIncAlignment;
140 if (NewAlign > NewIncAlign) {
145 if (NewIncAlign > NewAlign) {
150 assert(NewIncAlign == NewAlign);
162 const SCEV *&AlignSCEV,
163 const SCEV *&OffSCEV) {
169 AAPtr = AlignOB.
Inputs[0].get();
174 if (!isa<SCEVConstant>(AlignSCEV))
178 if (!cast<SCEVConstant>(AlignSCEV)->getAPInt().isPowerOf2())
181 if (AlignOB.
Inputs.size() == 3)
192 const SCEV *AlignSCEV, *OffSCEV;
198 if (isa<ConstantData>(AAPtr))
215 while (!WorkList.
empty()) {
217 if (
LoadInst *LI = dyn_cast<LoadInst>(J)) {
221 LI->getPointerOperand(),
SE);
222 if (NewAlignment > LI->getAlign()) {
223 LI->setAlignment(NewAlignment);
224 ++NumLoadAlignChanged;
226 }
else if (
StoreInst *SI = dyn_cast<StoreInst>(J)) {
230 SI->getPointerOperand(),
SE);
231 if (NewAlignment > SI->getAlign()) {
232 SI->setAlignment(NewAlignment);
233 ++NumStoreAlignChanged;
238 Align NewDestAlignment =
243 if (NewDestAlignment > *
MI->getDestAlign()) {
244 MI->setDestAlignment(NewDestAlignment);
245 ++NumMemIntAlignChanged;
251 Align NewSrcAlignment =
257 if (NewSrcAlignment > *MTI->getSourceAlign()) {
258 MTI->setSourceAlignment(NewSrcAlignment);
259 ++NumMemIntAlignChanged;
267 if (isa<GetElementPtrInst>(J) || isa<PHINode>(J))
268 for (
auto &U : J->
uses()) {
269 if (U->getType()->isPointerTy()) {
272 if (SI && SI->getPointerOperandIndex() != U.getOperandNo())
274 if (!Visited.
count(K))
289 bool Changed =
false;
292 CallInst *Call = cast<CallInst>(AssumeVH);
293 for (
unsigned Idx = 0;
Idx < Call->getNumOperandBundles();
Idx++)
static MaybeAlign getNewAlignmentDiff(const SCEV *DiffSCEV, const SCEV *AlignSCEV, ScalarEvolution *SE)
static Align getNewAlignment(const SCEV *AASCEV, const SCEV *AlignSCEV, const SCEV *OffSCEV, Value *Ptr, ScalarEvolution *SE)
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This is the interface for a simple mod/ref and alias analysis over globals.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
A container for analyses that lazily runs them and caches their results.
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.
MutableArrayRef< ResultElem > assumptions()
Access the list of assumption handles currently tracked for this function.
Represents analyses that only rely on functions' control flow.
This class represents a function call, abstracting a target machine's calling convention.
Analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
const Function * getFunction() const
Return the function this instruction belongs to.
An instruction for reading from memory.
This is the common base class for memset/memcpy/memmove.
This class wraps the llvm.memcpy/memmove intrinsics.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void preserveSet()
Mark an analysis set as preserved.
void preserve()
Mark an analysis as preserved.
This node represents a polynomial recurrence on the trip count of the specified loop.
This class represents a constant integer value.
This class represents an analyzed expression in the program.
Type * getType() const
Return the LLVM type of this SCEV expression.
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
const SCEV * getURemExpr(const SCEV *LHS, const SCEV *RHS)
Represents an unsigned remainder expression based on unsigned division.
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
The instances of the Type class are immutable: once they are created, they are never changed.
static IntegerType * getInt64Ty(LLVMContext &C)
LLVM Value Representation.
iterator_range< user_iterator > users()
const Value * stripPointerCastsSameRepresentation() const
Strip off pointer casts, all-zero GEPs and address space casts but ensures the representation of the ...
iterator_range< use_iterator > uses()
This is an optimization pass for GlobalISel generic memory operations.
bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
This struct is a compact representation of a valid (non-zero power of two) alignment.
bool extractAlignmentInfo(CallInst *I, unsigned Idx, Value *&AAPtr, const SCEV *&AlignSCEV, const SCEV *&OffSCEV)
bool processAssumption(CallInst *I, unsigned Idx)
bool runImpl(Function &F, AssumptionCache &AC, ScalarEvolution *SE_, DominatorTree *DT_)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
A lightweight accessor for an operand bundle meant to be passed around by value.
StringRef getTagName() const
Return the tag of this operand bundle as a string.