19 #define AA_NAME "alignment-from-assumptions" 20 #define DEBUG_TYPE AA_NAME 41 "Number of loads changed by alignment assumptions");
43 "Number of stores changed by alignment assumptions");
45 "Number of memory intrinsics changed by alignment assumptions");
74 static const char aip_name[] =
"Alignment from assumptions";
76 aip_name,
false,
false)
81 aip_name,
false, false)
84 return new AlignmentFromAssumptions();
94 const SCEV *AlignSCEV,
101 LLVM_DEBUG(
dbgs() <<
"\talignment relative to " << *AlignSCEV <<
" is " 102 << *DiffUnitsSCEV <<
" (diff: " << *DiffSCEV <<
")\n");
105 dyn_cast<SCEVConstant>(DiffUnitsSCEV)) {
106 int64_t DiffUnits = ConstDUSCEV->getValue()->getSExtValue();
113 cast<SCEVConstant>(AlignSCEV)->getValue()->getSExtValue();
117 uint64_t DiffUnitsAbs =
std::abs(DiffUnits);
119 return (
unsigned) DiffUnitsAbs;
142 LLVM_DEBUG(
dbgs() <<
"AFI: alignment of " << *Ptr <<
" relative to " 143 << *AlignSCEV <<
" and offset " << *OffSCEV
144 <<
" using diff " << *DiffSCEV <<
"\n");
147 LLVM_DEBUG(
dbgs() <<
"\tnew alignment: " << NewAlignment <<
"\n");
152 dyn_cast<SCEVAddRecExpr>(DiffSCEV)) {
160 const SCEV *DiffStartSCEV = DiffARSCEV->getStart();
161 const SCEV *DiffIncSCEV = DiffARSCEV->getStepRecurrence(*SE);
164 << *DiffStartSCEV <<
" and inc " << *DiffIncSCEV <<
"\n");
173 LLVM_DEBUG(
dbgs() <<
"\tnew start alignment: " << NewAlignment <<
"\n");
174 LLVM_DEBUG(
dbgs() <<
"\tnew inc alignment: " << NewIncAlignment <<
"\n");
176 if (!NewAlignment || !NewIncAlignment) {
178 }
else if (NewAlignment > NewIncAlignment) {
179 if (NewAlignment % NewIncAlignment == 0) {
180 LLVM_DEBUG(
dbgs() <<
"\tnew start/inc alignment: " << NewIncAlignment
182 return NewIncAlignment;
184 }
else if (NewIncAlignment > NewAlignment) {
185 if (NewIncAlignment % NewAlignment == 0) {
186 LLVM_DEBUG(
dbgs() <<
"\tnew start/inc alignment: " << NewAlignment
190 }
else if (NewIncAlignment == NewAlignment) {
191 LLVM_DEBUG(
dbgs() <<
"\tnew start/inc alignment: " << NewAlignment
202 const SCEV *&AlignSCEV,
203 const SCEV *&OffSCEV) {
217 const SCEV *CmpLHSSCEV = SE->getSCEV(CmpLHS);
218 const SCEV *CmpRHSSCEV = SE->getSCEV(CmpRHS);
221 else if (!CmpRHSSCEV->
isZero())
225 if (!CmpBO || CmpBO->
getOpcode() != Instruction::And)
232 const SCEV *AndLHSSCEV = SE->getSCEV(AndLHS);
233 const SCEV *AndRHSSCEV = SE->getSCEV(AndRHS);
234 if (isa<SCEVConstant>(AndLHSSCEV)) {
252 TrailingOnes = std::min(TrailingOnes,
253 unsigned(
sizeof(
unsigned) * CHAR_BIT - 1));
257 AlignSCEV = SE->getConstant(Int64Ty, Alignment);
263 if (
PtrToIntInst *PToI = dyn_cast<PtrToIntInst>(AndLHS)) {
264 AAPtr = PToI->getPointerOperand();
265 OffSCEV = SE->getZero(Int64Ty);
267 dyn_cast<SCEVAddExpr>(AndLHSSCEV)) {
270 JE = AndLHSAddSCEV->op_end(); J != JE; ++J)
271 if (
const SCEVUnknown *OpUnk = dyn_cast<SCEVUnknown>(*J))
272 if (
PtrToIntInst *PToI = dyn_cast<PtrToIntInst>(OpUnk->getValue())) {
273 AAPtr = PToI->getPointerOperand();
274 OffSCEV = SE->getMinusSCEV(AndLHSAddSCEV, *J);
285 if (OffSCEVBits < 64)
286 OffSCEV = SE->getSignExtendExpr(OffSCEV, Int64Ty);
287 else if (OffSCEVBits > 64)
296 const SCEV *AlignSCEV, *OffSCEV;
297 if (!extractAlignmentInfo(ACall, AAPtr, AlignSCEV, OffSCEV))
302 if (isa<ConstantData>(AAPtr))
305 const SCEV *AASCEV = SE->getSCEV(AAPtr);
319 while (!WorkList.
empty()) {
322 if (
LoadInst *LI = dyn_cast<LoadInst>(J)) {
324 LI->getPointerOperand(), SE);
326 if (NewAlignment > LI->getAlignment()) {
327 LI->setAlignment(NewAlignment);
328 ++NumLoadAlignChanged;
330 }
else if (
StoreInst *
SI = dyn_cast<StoreInst>(J)) {
332 SI->getPointerOperand(), SE);
334 if (NewAlignment >
SI->getAlignment()) {
335 SI->setAlignment(NewAlignment);
336 ++NumStoreAlignChanged;
339 unsigned NewDestAlignment =
getNewAlignment(AASCEV, AlignSCEV, OffSCEV,
342 LLVM_DEBUG(
dbgs() <<
"\tmem inst: " << NewDestAlignment <<
"\n";);
343 if (NewDestAlignment >
MI->getDestAlignment()) {
344 MI->setDestAlignment(NewDestAlignment);
345 ++NumMemIntAlignChanged;
352 MTI->getSource(), SE);
354 LLVM_DEBUG(
dbgs() <<
"\tmem trans: " << NewSrcAlignment <<
"\n";);
356 if (NewSrcAlignment > MTI->getSourceAlignment()) {
357 MTI->setSourceAlignment(NewSrcAlignment);
358 ++NumMemIntAlignChanged;
380 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
381 ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
382 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
384 return Impl.runImpl(F, AC, SE, DT);
393 bool Changed =
false;
396 Changed |= processAssumption(cast<CallInst>(AssumeVH));
Legacy wrapper pass to provide the GlobalsAAResult object.
INITIALIZE_PASS_BEGIN(AlignmentFromAssumptions, AA_NAME, aip_name, false, false) INITIALIZE_PASS_END(AlignmentFromAssumptions
static bool runImpl(Function &F, TargetLibraryInfo &TLI, DominatorTree &DT)
This is the entry point for all transforms.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class represents lattice values for constants.
BinaryOps getOpcode() const
This is the interface for a simple mod/ref and alias analysis over globals.
static unsigned getNewAlignment(const SCEV *AASCEV, const SCEV *AlignSCEV, const SCEV *OffSCEV, Value *Ptr, ScalarEvolution *SE)
void push_back(const T &Elt)
The main scalar evolution driver.
bool isZero() const
Return true if the expression is a constant zero.
This class represents a function call, abstracting a target machine's calling convention.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr)
Return true if it is valid to use the assumptions provided by an assume intrinsic, I, at the point in the control-flow identified by the context instruction, CxtI.
STATISTIC(NumFunctions, "Total number of functions")
Analysis pass which computes a DominatorTree.
An instruction for reading from memory.
static IntegerType * getInt64Ty(LLVMContext &C)
FunctionPass * createAlignmentFromAssumptionsPass()
MutableArrayRef< WeakTrackingVH > assumptions()
Access the list of assumption handles currently tracked for this function.
Value * getArgOperand(unsigned i) const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
static const unsigned MaximumAlignment
This class represents a cast from a pointer to an integer.
const APInt & getAPInt() const
This node represents a polynomial recurrence on the trip count of the specified loop.
An instruction for storing to memory.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Value * getOperand(unsigned i) const
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static bool runOnFunction(Function &F, bool PostInlining)
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
A set of analyses that are preserved following a run of a transformation pass.
The instances of the Type class are immutable: once they are created, they are never changed...
static const char aip_name[]
A manager for alias analyses.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
This instruction compares its operands according to the predicate given to the constructor.
Analysis pass providing a never-invalidated alias analysis result.
FunctionPass class - This class is used to implement most global optimizations.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, and aliases.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
const SCEV * getMulExpr(SmallVectorImpl< const SCEV *> &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
A function analysis which provides an AssumptionCache.
This is the common base class for memset/memcpy/memmove.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Type * getType() const
Return the LLVM type of this SCEV expression.
const SCEV *const * op_iterator
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_NODISCARD T pop_back_val()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
bool extractAlignmentInfo(CallInst *I, Value *&AAPtr, const SCEV *&AlignSCEV, const SCEV *&OffSCEV)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
unsigned countTrailingOnes() const
Count the number of trailing one bits.
This node represents an addition of some number of SCEVs.
bool processAssumption(CallInst *I)
iterator_range< user_iterator > users()
Represents analyses that only rely on functions' control flow.
Analysis pass that exposes the ScalarEvolution for a function.
Predicate getPredicate() const
Return the predicate for this instruction.
This class wraps the llvm.memcpy/memmove intrinsics.
This class represents an analyzed expression in the program.
static unsigned getNewAlignmentDiff(const SCEV *DiffSCEV, const SCEV *AlignSCEV, ScalarEvolution *SE)
LLVM_NODISCARD bool empty() const
void preserveSet()
Mark an analysis set as preserved.
const Function * getParent() const
Return the enclosing method, or null if none.
void initializeAlignmentFromAssumptionsPass(PassRegistry &)
APFloat abs(APFloat X)
Returns the absolute value of the argument.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void preserve()
Mark an analysis as preserved.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
LLVM Value Representation.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
const SCEV * getUDivExpr(const SCEV *LHS, const SCEV *RHS)
Get a canonical unsigned division expression, or something simpler if possible.
The legacy pass manager's analysis pass to compute loop information.
bool runImpl(Function &F, AssumptionCache &AC, ScalarEvolution *SE_, DominatorTree *DT_)
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
const BasicBlock * getParent() const
This class represents a constant integer value.