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,
99 LLVM_DEBUG(
dbgs() <<
"\talignment relative to " << *AlignSCEV <<
" is " 100 << *DiffUnitsSCEV <<
" (diff: " << *DiffSCEV <<
")\n");
103 dyn_cast<SCEVConstant>(DiffUnitsSCEV)) {
104 int64_t DiffUnits = ConstDUSCEV->getValue()->getSExtValue();
111 cast<SCEVConstant>(AlignSCEV)->getValue()->getSExtValue();
115 uint64_t DiffUnitsAbs =
std::abs(DiffUnits);
117 return (
unsigned) DiffUnitsAbs;
140 LLVM_DEBUG(
dbgs() <<
"AFI: alignment of " << *Ptr <<
" relative to " 141 << *AlignSCEV <<
" and offset " << *OffSCEV
142 <<
" using diff " << *DiffSCEV <<
"\n");
145 LLVM_DEBUG(
dbgs() <<
"\tnew alignment: " << NewAlignment <<
"\n");
150 dyn_cast<SCEVAddRecExpr>(DiffSCEV)) {
158 const SCEV *DiffStartSCEV = DiffARSCEV->getStart();
159 const SCEV *DiffIncSCEV = DiffARSCEV->getStepRecurrence(*SE);
162 << *DiffStartSCEV <<
" and inc " << *DiffIncSCEV <<
"\n");
171 LLVM_DEBUG(
dbgs() <<
"\tnew start alignment: " << NewAlignment <<
"\n");
172 LLVM_DEBUG(
dbgs() <<
"\tnew inc alignment: " << NewIncAlignment <<
"\n");
174 if (!NewAlignment || !NewIncAlignment) {
176 }
else if (NewAlignment > NewIncAlignment) {
177 if (NewAlignment % NewIncAlignment == 0) {
178 LLVM_DEBUG(
dbgs() <<
"\tnew start/inc alignment: " << NewIncAlignment
180 return NewIncAlignment;
182 }
else if (NewIncAlignment > NewAlignment) {
183 if (NewIncAlignment % NewAlignment == 0) {
184 LLVM_DEBUG(
dbgs() <<
"\tnew start/inc alignment: " << NewAlignment
188 }
else if (NewIncAlignment == NewAlignment) {
189 LLVM_DEBUG(
dbgs() <<
"\tnew start/inc alignment: " << NewAlignment
200 const SCEV *&AlignSCEV,
201 const SCEV *&OffSCEV) {
215 const SCEV *CmpLHSSCEV = SE->getSCEV(CmpLHS);
216 const SCEV *CmpRHSSCEV = SE->getSCEV(CmpRHS);
219 else if (!CmpRHSSCEV->
isZero())
223 if (!CmpBO || CmpBO->
getOpcode() != Instruction::And)
230 const SCEV *AndLHSSCEV = SE->getSCEV(AndLHS);
231 const SCEV *AndRHSSCEV = SE->getSCEV(AndRHS);
232 if (isa<SCEVConstant>(AndLHSSCEV)) {
250 TrailingOnes = std::min(TrailingOnes,
251 unsigned(
sizeof(
unsigned) * CHAR_BIT - 1));
255 AlignSCEV = SE->getConstant(Int64Ty, Alignment);
261 if (
PtrToIntInst *PToI = dyn_cast<PtrToIntInst>(AndLHS)) {
262 AAPtr = PToI->getPointerOperand();
263 OffSCEV = SE->getZero(Int64Ty);
265 dyn_cast<SCEVAddExpr>(AndLHSSCEV)) {
268 JE = AndLHSAddSCEV->op_end(); J != JE; ++J)
269 if (
const SCEVUnknown *OpUnk = dyn_cast<SCEVUnknown>(*J))
270 if (
PtrToIntInst *PToI = dyn_cast<PtrToIntInst>(OpUnk->getValue())) {
271 AAPtr = PToI->getPointerOperand();
272 OffSCEV = SE->getMinusSCEV(AndLHSAddSCEV, *J);
283 if (OffSCEVBits < 64)
284 OffSCEV = SE->getSignExtendExpr(OffSCEV, Int64Ty);
285 else if (OffSCEVBits > 64)
294 const SCEV *AlignSCEV, *OffSCEV;
295 if (!extractAlignmentInfo(ACall, AAPtr, AlignSCEV, OffSCEV))
300 if (isa<ConstantData>(AAPtr))
303 const SCEV *AASCEV = SE->getSCEV(AAPtr);
317 while (!WorkList.
empty()) {
320 if (
LoadInst *LI = dyn_cast<LoadInst>(J)) {
322 LI->getPointerOperand(), SE);
324 if (NewAlignment > LI->getAlignment()) {
326 ++NumLoadAlignChanged;
328 }
else if (
StoreInst *
SI = dyn_cast<StoreInst>(J)) {
330 SI->getPointerOperand(), SE);
332 if (NewAlignment >
SI->getAlignment()) {
334 ++NumStoreAlignChanged;
337 unsigned NewDestAlignment =
getNewAlignment(AASCEV, AlignSCEV, OffSCEV,
340 LLVM_DEBUG(
dbgs() <<
"\tmem inst: " << NewDestAlignment <<
"\n";);
341 if (NewDestAlignment >
MI->getDestAlignment()) {
342 MI->setDestAlignment(NewDestAlignment);
343 ++NumMemIntAlignChanged;
350 MTI->getSource(), SE);
352 LLVM_DEBUG(
dbgs() <<
"\tmem trans: " << NewSrcAlignment <<
"\n";);
354 if (NewSrcAlignment > MTI->getSourceAlignment()) {
355 MTI->setSourceAlignment(NewSrcAlignment);
356 ++NumMemIntAlignChanged;
378 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
379 ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
380 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
382 return Impl.runImpl(F, AC, SE, DT);
391 bool Changed =
false;
394 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.
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
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 address space casts.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
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.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
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.
const SCEV * getURemExpr(const SCEV *LHS, const SCEV *RHS)
Represents an unsigned remainder expression based on unsigned division.
LLVM Value Representation.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
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.