LLVM API Documentation

AlignmentFromAssumptions.cpp
Go to the documentation of this file.
00001 //===----------------------- AlignmentFromAssumptions.cpp -----------------===//
00002 //                  Set Load/Store Alignments From Assumptions
00003 //
00004 //                     The LLVM Compiler Infrastructure
00005 //
00006 // This file is distributed under the University of Illinois Open Source
00007 // License. See LICENSE.TXT for details.
00008 //
00009 //===----------------------------------------------------------------------===//
00010 //
00011 // This file implements a ScalarEvolution-based transformation to set
00012 // the alignments of load, stores and memory intrinsics based on the truth
00013 // expressions of assume intrinsics. The primary motivation is to handle
00014 // complex alignment assumptions that apply to vector loads and stores that
00015 // appear after vectorization and unrolling.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #define AA_NAME "alignment-from-assumptions"
00020 #define DEBUG_TYPE AA_NAME
00021 #include "llvm/Transforms/Scalar.h"
00022 #include "llvm/ADT/SmallPtrSet.h"
00023 #include "llvm/ADT/Statistic.h"
00024 #include "llvm/Analysis/AssumptionTracker.h"
00025 #include "llvm/Analysis/LoopInfo.h"
00026 #include "llvm/Analysis/ValueTracking.h"
00027 #include "llvm/Analysis/ScalarEvolution.h"
00028 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
00029 #include "llvm/IR/Constant.h"
00030 #include "llvm/IR/Dominators.h"
00031 #include "llvm/IR/Instruction.h"
00032 #include "llvm/IR/IntrinsicInst.h"
00033 #include "llvm/IR/Intrinsics.h"
00034 #include "llvm/IR/DataLayout.h"
00035 #include "llvm/Support/Debug.h"
00036 #include "llvm/Support/raw_ostream.h"
00037 using namespace llvm;
00038 
00039 STATISTIC(NumLoadAlignChanged,
00040   "Number of loads changed by alignment assumptions");
00041 STATISTIC(NumStoreAlignChanged,
00042   "Number of stores changed by alignment assumptions");
00043 STATISTIC(NumMemIntAlignChanged,
00044   "Number of memory intrinsics changed by alignment assumptions");
00045 
00046 namespace {
00047 struct AlignmentFromAssumptions : public FunctionPass {
00048   static char ID; // Pass identification, replacement for typeid
00049   AlignmentFromAssumptions() : FunctionPass(ID) {
00050     initializeAlignmentFromAssumptionsPass(*PassRegistry::getPassRegistry());
00051   }
00052 
00053   bool runOnFunction(Function &F);
00054 
00055   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00056     AU.addRequired<AssumptionTracker>();
00057     AU.addRequired<ScalarEvolution>();
00058     AU.addRequired<DominatorTreeWrapperPass>();
00059 
00060     AU.setPreservesCFG();
00061     AU.addPreserved<LoopInfo>();
00062     AU.addPreserved<DominatorTreeWrapperPass>();
00063     AU.addPreserved<ScalarEvolution>();
00064   }
00065 
00066   // For memory transfers, we need a common alignment for both the source and
00067   // destination. If we have a new alignment for only one operand of a transfer
00068   // instruction, save it in these maps.  If we reach the other operand through
00069   // another assumption later, then we may change the alignment at that point.
00070   DenseMap<MemTransferInst *, unsigned> NewDestAlignments, NewSrcAlignments;
00071 
00072   AssumptionTracker *AT;
00073   ScalarEvolution *SE;
00074   DominatorTree *DT;
00075   const DataLayout *DL;
00076 
00077   bool extractAlignmentInfo(CallInst *I, Value *&AAPtr, const SCEV *&AlignSCEV,
00078                             const SCEV *&OffSCEV);
00079   bool processAssumption(CallInst *I);
00080 };
00081 }
00082 
00083 char AlignmentFromAssumptions::ID = 0;
00084 static const char aip_name[] = "Alignment from assumptions";
00085 INITIALIZE_PASS_BEGIN(AlignmentFromAssumptions, AA_NAME,
00086                       aip_name, false, false)
00087 INITIALIZE_PASS_DEPENDENCY(AssumptionTracker)
00088 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
00089 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
00090 INITIALIZE_PASS_END(AlignmentFromAssumptions, AA_NAME,
00091                     aip_name, false, false)
00092 
00093 FunctionPass *llvm::createAlignmentFromAssumptionsPass() {
00094   return new AlignmentFromAssumptions();
00095 }
00096 
00097 // Given an expression for the (constant) alignment, AlignSCEV, and an
00098 // expression for the displacement between a pointer and the aligned address,
00099 // DiffSCEV, compute the alignment of the displaced pointer if it can be reduced
00100 // to a constant. Using SCEV to compute alignment handles the case where
00101 // DiffSCEV is a recurrence with constant start such that the aligned offset
00102 // is constant. e.g. {16,+,32} % 32 -> 16.
00103 static unsigned getNewAlignmentDiff(const SCEV *DiffSCEV,
00104                                     const SCEV *AlignSCEV,
00105                                     ScalarEvolution *SE) {
00106   // DiffUnits = Diff % int64_t(Alignment)
00107   const SCEV *DiffAlignDiv = SE->getUDivExpr(DiffSCEV, AlignSCEV);
00108   const SCEV *DiffAlign = SE->getMulExpr(DiffAlignDiv, AlignSCEV);
00109   const SCEV *DiffUnitsSCEV = SE->getMinusSCEV(DiffAlign, DiffSCEV);
00110 
00111   DEBUG(dbgs() << "\talignment relative to " << *AlignSCEV << " is " <<
00112                   *DiffUnitsSCEV << " (diff: " << *DiffSCEV << ")\n");
00113 
00114   if (const SCEVConstant *ConstDUSCEV =
00115       dyn_cast<SCEVConstant>(DiffUnitsSCEV)) {
00116     int64_t DiffUnits = ConstDUSCEV->getValue()->getSExtValue();
00117 
00118     // If the displacement is an exact multiple of the alignment, then the
00119     // displaced pointer has the same alignment as the aligned pointer, so
00120     // return the alignment value.
00121     if (!DiffUnits)
00122       return (unsigned)
00123         cast<SCEVConstant>(AlignSCEV)->getValue()->getSExtValue();
00124 
00125     // If the displacement is not an exact multiple, but the remainder is a
00126     // constant, then return this remainder (but only if it is a power of 2).
00127     uint64_t DiffUnitsAbs = abs64(DiffUnits);
00128     if (isPowerOf2_64(DiffUnitsAbs))
00129       return (unsigned) DiffUnitsAbs;
00130   }
00131 
00132   return 0;
00133 }
00134 
00135 // There is an address given by an offset OffSCEV from AASCEV which has an
00136 // alignment AlignSCEV. Use that information, if possible, to compute a new
00137 // alignment for Ptr.
00138 static unsigned getNewAlignment(const SCEV *AASCEV, const SCEV *AlignSCEV,
00139                                 const SCEV *OffSCEV, Value *Ptr,
00140                                 ScalarEvolution *SE) {
00141   const SCEV *PtrSCEV = SE->getSCEV(Ptr);
00142   const SCEV *DiffSCEV = SE->getMinusSCEV(PtrSCEV, AASCEV);
00143 
00144   // On 32-bit platforms, DiffSCEV might now have type i32 -- we've always
00145   // sign-extended OffSCEV to i64, so make sure they agree again.
00146   DiffSCEV = SE->getNoopOrSignExtend(DiffSCEV, OffSCEV->getType());
00147 
00148   // What we really want to know is the overall offset to the aligned
00149   // address. This address is displaced by the provided offset.
00150   DiffSCEV = SE->getMinusSCEV(DiffSCEV, OffSCEV);
00151 
00152   DEBUG(dbgs() << "AFI: alignment of " << *Ptr << " relative to " <<
00153                   *AlignSCEV << " and offset " << *OffSCEV <<
00154                   " using diff " << *DiffSCEV << "\n");
00155 
00156   unsigned NewAlignment = getNewAlignmentDiff(DiffSCEV, AlignSCEV, SE);
00157   DEBUG(dbgs() << "\tnew alignment: " << NewAlignment << "\n");
00158 
00159   if (NewAlignment) {
00160     return NewAlignment;
00161   } else if (const SCEVAddRecExpr *DiffARSCEV =
00162              dyn_cast<SCEVAddRecExpr>(DiffSCEV)) {
00163     // The relative offset to the alignment assumption did not yield a constant,
00164     // but we should try harder: if we assume that a is 32-byte aligned, then in
00165     // for (i = 0; i < 1024; i += 4) r += a[i]; not all of the loads from a are
00166     // 32-byte aligned, but instead alternate between 32 and 16-byte alignment.
00167     // As a result, the new alignment will not be a constant, but can still
00168     // be improved over the default (of 4) to 16.
00169 
00170     const SCEV *DiffStartSCEV = DiffARSCEV->getStart();
00171     const SCEV *DiffIncSCEV = DiffARSCEV->getStepRecurrence(*SE);
00172 
00173     DEBUG(dbgs() << "\ttrying start/inc alignment using start " <<
00174                     *DiffStartSCEV << " and inc " << *DiffIncSCEV << "\n");
00175 
00176     // Now compute the new alignment using the displacement to the value in the
00177     // first iteration, and also the alignment using the per-iteration delta.
00178     // If these are the same, then use that answer. Otherwise, use the smaller
00179     // one, but only if it divides the larger one.
00180     NewAlignment = getNewAlignmentDiff(DiffStartSCEV, AlignSCEV, SE);
00181     unsigned NewIncAlignment = getNewAlignmentDiff(DiffIncSCEV, AlignSCEV, SE);
00182 
00183     DEBUG(dbgs() << "\tnew start alignment: " << NewAlignment << "\n");
00184     DEBUG(dbgs() << "\tnew inc alignment: " << NewIncAlignment << "\n");
00185 
00186     if (!NewAlignment || !NewIncAlignment) {
00187       return 0;
00188     } else if (NewAlignment > NewIncAlignment) {
00189       if (NewAlignment % NewIncAlignment == 0) {
00190         DEBUG(dbgs() << "\tnew start/inc alignment: " <<
00191                         NewIncAlignment << "\n");
00192         return NewIncAlignment;
00193       }
00194     } else if (NewIncAlignment > NewAlignment) {
00195       if (NewIncAlignment % NewAlignment == 0) {
00196         DEBUG(dbgs() << "\tnew start/inc alignment: " <<
00197                         NewAlignment << "\n");
00198         return NewAlignment;
00199       }
00200     } else if (NewIncAlignment == NewAlignment) {
00201       DEBUG(dbgs() << "\tnew start/inc alignment: " <<
00202                       NewAlignment << "\n");
00203       return NewAlignment;
00204     }
00205   }
00206 
00207   return 0;
00208 }
00209 
00210 bool AlignmentFromAssumptions::extractAlignmentInfo(CallInst *I,
00211                                  Value *&AAPtr, const SCEV *&AlignSCEV,
00212                                  const SCEV *&OffSCEV) {
00213   // An alignment assume must be a statement about the least-significant
00214   // bits of the pointer being zero, possibly with some offset.
00215   ICmpInst *ICI = dyn_cast<ICmpInst>(I->getArgOperand(0));
00216   if (!ICI)
00217     return false;
00218 
00219   // This must be an expression of the form: x & m == 0.
00220   if (ICI->getPredicate() != ICmpInst::ICMP_EQ)
00221     return false;
00222 
00223   // Swap things around so that the RHS is 0.
00224   Value *CmpLHS = ICI->getOperand(0);
00225   Value *CmpRHS = ICI->getOperand(1);
00226   const SCEV *CmpLHSSCEV = SE->getSCEV(CmpLHS);
00227   const SCEV *CmpRHSSCEV = SE->getSCEV(CmpRHS);
00228   if (CmpLHSSCEV->isZero())
00229     std::swap(CmpLHS, CmpRHS);
00230   else if (!CmpRHSSCEV->isZero())
00231     return false;
00232 
00233   BinaryOperator *CmpBO = dyn_cast<BinaryOperator>(CmpLHS);
00234   if (!CmpBO || CmpBO->getOpcode() != Instruction::And)
00235     return false;
00236 
00237   // Swap things around so that the right operand of the and is a constant
00238   // (the mask); we cannot deal with variable masks.
00239   Value *AndLHS = CmpBO->getOperand(0);
00240   Value *AndRHS = CmpBO->getOperand(1);
00241   const SCEV *AndLHSSCEV = SE->getSCEV(AndLHS);
00242   const SCEV *AndRHSSCEV = SE->getSCEV(AndRHS);
00243   if (isa<SCEVConstant>(AndLHSSCEV)) {
00244     std::swap(AndLHS, AndRHS);
00245     std::swap(AndLHSSCEV, AndRHSSCEV);
00246   }
00247 
00248   const SCEVConstant *MaskSCEV = dyn_cast<SCEVConstant>(AndRHSSCEV);
00249   if (!MaskSCEV)
00250     return false;
00251 
00252   // The mask must have some trailing ones (otherwise the condition is
00253   // trivial and tells us nothing about the alignment of the left operand).
00254   unsigned TrailingOnes =
00255     MaskSCEV->getValue()->getValue().countTrailingOnes();
00256   if (!TrailingOnes)
00257     return false;
00258 
00259   // Cap the alignment at the maximum with which LLVM can deal (and make sure
00260   // we don't overflow the shift).
00261   uint64_t Alignment;
00262   TrailingOnes = std::min(TrailingOnes,
00263     unsigned(sizeof(unsigned) * CHAR_BIT - 1));
00264   Alignment = std::min(1u << TrailingOnes, +Value::MaximumAlignment);
00265 
00266   Type *Int64Ty = Type::getInt64Ty(I->getParent()->getParent()->getContext());
00267   AlignSCEV = SE->getConstant(Int64Ty, Alignment);
00268 
00269   // The LHS might be a ptrtoint instruction, or it might be the pointer
00270   // with an offset.
00271   AAPtr = nullptr;
00272   OffSCEV = nullptr;
00273   if (PtrToIntInst *PToI = dyn_cast<PtrToIntInst>(AndLHS)) {
00274     AAPtr = PToI->getPointerOperand();
00275     OffSCEV = SE->getConstant(Int64Ty, 0);
00276   } else if (const SCEVAddExpr* AndLHSAddSCEV =
00277              dyn_cast<SCEVAddExpr>(AndLHSSCEV)) {
00278     // Try to find the ptrtoint; subtract it and the rest is the offset.
00279     for (SCEVAddExpr::op_iterator J = AndLHSAddSCEV->op_begin(),
00280          JE = AndLHSAddSCEV->op_end(); J != JE; ++J)
00281       if (const SCEVUnknown *OpUnk = dyn_cast<SCEVUnknown>(*J))
00282         if (PtrToIntInst *PToI = dyn_cast<PtrToIntInst>(OpUnk->getValue())) {
00283           AAPtr = PToI->getPointerOperand();
00284           OffSCEV = SE->getMinusSCEV(AndLHSAddSCEV, *J);
00285           break;
00286         }
00287   }
00288 
00289   if (!AAPtr)
00290     return false;
00291 
00292   // Sign extend the offset to 64 bits (so that it is like all of the other
00293   // expressions). 
00294   unsigned OffSCEVBits = OffSCEV->getType()->getPrimitiveSizeInBits();
00295   if (OffSCEVBits < 64)
00296     OffSCEV = SE->getSignExtendExpr(OffSCEV, Int64Ty);
00297   else if (OffSCEVBits > 64)
00298     return false;
00299 
00300   AAPtr = AAPtr->stripPointerCasts();
00301   return true;
00302 }
00303 
00304 bool AlignmentFromAssumptions::processAssumption(CallInst *ACall) {
00305   Value *AAPtr;
00306   const SCEV *AlignSCEV, *OffSCEV;
00307   if (!extractAlignmentInfo(ACall, AAPtr, AlignSCEV, OffSCEV))
00308     return false;
00309 
00310   const SCEV *AASCEV = SE->getSCEV(AAPtr);
00311 
00312   // Apply the assumption to all other users of the specified pointer.
00313   SmallPtrSet<Instruction *, 32> Visited;
00314   SmallVector<Instruction*, 16> WorkList;
00315   for (User *J : AAPtr->users()) {
00316     if (J == ACall)
00317       continue;
00318 
00319     if (Instruction *K = dyn_cast<Instruction>(J))
00320       if (isValidAssumeForContext(ACall, K, DL, DT))
00321         WorkList.push_back(K);
00322   }
00323 
00324   while (!WorkList.empty()) {
00325     Instruction *J = WorkList.pop_back_val();
00326 
00327     if (LoadInst *LI = dyn_cast<LoadInst>(J)) {
00328       unsigned NewAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV,
00329         LI->getPointerOperand(), SE);
00330 
00331       if (NewAlignment > LI->getAlignment()) {
00332         LI->setAlignment(NewAlignment);
00333         ++NumLoadAlignChanged;
00334       }
00335     } else if (StoreInst *SI = dyn_cast<StoreInst>(J)) {
00336       unsigned NewAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV,
00337         SI->getPointerOperand(), SE);
00338 
00339       if (NewAlignment > SI->getAlignment()) {
00340         SI->setAlignment(NewAlignment);
00341         ++NumStoreAlignChanged;
00342       }
00343     } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(J)) {
00344       unsigned NewDestAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV,
00345         MI->getDest(), SE);
00346 
00347       // For memory transfers, we need a common alignment for both the
00348       // source and destination. If we have a new alignment for this
00349       // instruction, but only for one operand, save it. If we reach the
00350       // other operand through another assumption later, then we may
00351       // change the alignment at that point.
00352       if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
00353         unsigned NewSrcAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV,
00354           MTI->getSource(), SE);
00355 
00356         DenseMap<MemTransferInst *, unsigned>::iterator DI =
00357           NewDestAlignments.find(MTI);
00358         unsigned AltDestAlignment = (DI == NewDestAlignments.end()) ?
00359                                     0 : DI->second;
00360 
00361         DenseMap<MemTransferInst *, unsigned>::iterator SI =
00362           NewSrcAlignments.find(MTI);
00363         unsigned AltSrcAlignment = (SI == NewSrcAlignments.end()) ?
00364                                    0 : SI->second;
00365 
00366         DEBUG(dbgs() << "\tmem trans: " << NewDestAlignment << " " <<
00367                         AltDestAlignment << " " << NewSrcAlignment <<
00368                         " " << AltSrcAlignment << "\n");
00369 
00370         // Of these four alignments, pick the largest possible...
00371         unsigned NewAlignment = 0;
00372         if (NewDestAlignment <= std::max(NewSrcAlignment, AltSrcAlignment))
00373           NewAlignment = std::max(NewAlignment, NewDestAlignment);
00374         if (AltDestAlignment <= std::max(NewSrcAlignment, AltSrcAlignment))
00375           NewAlignment = std::max(NewAlignment, AltDestAlignment);
00376         if (NewSrcAlignment <= std::max(NewDestAlignment, AltDestAlignment))
00377           NewAlignment = std::max(NewAlignment, NewSrcAlignment);
00378         if (AltSrcAlignment <= std::max(NewDestAlignment, AltDestAlignment))
00379           NewAlignment = std::max(NewAlignment, AltSrcAlignment);
00380 
00381         if (NewAlignment > MI->getAlignment()) {
00382           MI->setAlignment(ConstantInt::get(Type::getInt32Ty(
00383             MI->getParent()->getContext()), NewAlignment));
00384           ++NumMemIntAlignChanged;
00385         }
00386 
00387         NewDestAlignments.insert(std::make_pair(MTI, NewDestAlignment));
00388         NewSrcAlignments.insert(std::make_pair(MTI, NewSrcAlignment));
00389       } else if (NewDestAlignment > MI->getAlignment()) {
00390         assert((!isa<MemIntrinsic>(MI) || isa<MemSetInst>(MI)) &&
00391                "Unknown memory intrinsic");
00392 
00393         MI->setAlignment(ConstantInt::get(Type::getInt32Ty(
00394           MI->getParent()->getContext()), NewDestAlignment));
00395         ++NumMemIntAlignChanged;
00396       }
00397     }
00398 
00399     // Now that we've updated that use of the pointer, look for other uses of
00400     // the pointer to update.
00401     Visited.insert(J);
00402     for (User *UJ : J->users()) {
00403       Instruction *K = cast<Instruction>(UJ);
00404       if (!Visited.count(K) && isValidAssumeForContext(ACall, K, DL, DT))
00405         WorkList.push_back(K);
00406     }
00407   }
00408 
00409   return true;
00410 }
00411 
00412 bool AlignmentFromAssumptions::runOnFunction(Function &F) {
00413   bool Changed = false;
00414   AT = &getAnalysis<AssumptionTracker>();
00415   SE = &getAnalysis<ScalarEvolution>();
00416   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
00417   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
00418   DL = DLP ? &DLP->getDataLayout() : nullptr;
00419 
00420   NewDestAlignments.clear();
00421   NewSrcAlignments.clear();
00422 
00423   for (auto &I : AT->assumptions(&F))
00424     Changed |= processAssumption(I);
00425 
00426   return Changed;
00427 }
00428