1 //===----------------------- AlignmentFromAssumptions.cpp -----------------===// 2 // Set Load/Store Alignments From Assumptions 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 // 11 // This file implements a ScalarEvolution-based transformation to set 12 // the alignments of load, stores and memory intrinsics based on the truth 13 // expressions of assume intrinsics. The primary motivation is to handle 14 // complex alignment assumptions that apply to vector loads and stores that 15 // appear after vectorization and unrolling. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #define AA_NAME "alignment-from-assumptions" 20 #define DEBUG_TYPE AA_NAME 21 #include "llvm/Transforms/Scalar.h" 22 #include "llvm/ADT/SmallPtrSet.h" 23 #include "llvm/ADT/Statistic.h" 24 #include "llvm/Analysis/AliasAnalysis.h" 25 #include "llvm/Analysis/GlobalsModRef.h" 26 #include "llvm/Analysis/AssumptionCache.h" 27 #include "llvm/Analysis/LoopInfo.h" 28 #include "llvm/Analysis/ScalarEvolution.h" 29 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 30 #include "llvm/Analysis/ValueTracking.h" 31 #include "llvm/IR/Constant.h" 32 #include "llvm/IR/Dominators.h" 33 #include "llvm/IR/Instruction.h" 34 #include "llvm/IR/IntrinsicInst.h" 35 #include "llvm/IR/Intrinsics.h" 36 #include "llvm/IR/Module.h" 37 #include "llvm/Support/Debug.h" 38 #include "llvm/Support/raw_ostream.h" 39 using namespace llvm; 40 41 STATISTIC(NumLoadAlignChanged, 42 "Number of loads changed by alignment assumptions"); 43 STATISTIC(NumStoreAlignChanged, 44 "Number of stores changed by alignment assumptions"); 45 STATISTIC(NumMemIntAlignChanged, 46 "Number of memory intrinsics changed by alignment assumptions"); 47 48 namespace { 49 struct AlignmentFromAssumptions : public FunctionPass { 50 static char ID; // Pass identification, replacement for typeid 51 AlignmentFromAssumptions() : FunctionPass(ID) { 52 initializeAlignmentFromAssumptionsPass(*PassRegistry::getPassRegistry()); 53 } 54 55 bool runOnFunction(Function &F) override; 56 57 void getAnalysisUsage(AnalysisUsage &AU) const override { 58 AU.addRequired<AssumptionCacheTracker>(); 59 AU.addRequired<ScalarEvolutionWrapperPass>(); 60 AU.addRequired<DominatorTreeWrapperPass>(); 61 62 AU.setPreservesCFG(); 63 AU.addPreserved<AAResultsWrapperPass>(); 64 AU.addPreserved<GlobalsAAWrapperPass>(); 65 AU.addPreserved<LoopInfoWrapperPass>(); 66 AU.addPreserved<DominatorTreeWrapperPass>(); 67 AU.addPreserved<ScalarEvolutionWrapperPass>(); 68 } 69 70 // For memory transfers, we need a common alignment for both the source and 71 // destination. If we have a new alignment for only one operand of a transfer 72 // instruction, save it in these maps. If we reach the other operand through 73 // another assumption later, then we may change the alignment at that point. 74 DenseMap<MemTransferInst *, unsigned> NewDestAlignments, NewSrcAlignments; 75 76 ScalarEvolution *SE; 77 DominatorTree *DT; 78 79 bool extractAlignmentInfo(CallInst *I, Value *&AAPtr, const SCEV *&AlignSCEV, 80 const SCEV *&OffSCEV); 81 bool processAssumption(CallInst *I); 82 }; 83 } 84 85 char AlignmentFromAssumptions::ID = 0; 86 static const char aip_name[] = "Alignment from assumptions"; 87 INITIALIZE_PASS_BEGIN(AlignmentFromAssumptions, AA_NAME, 88 aip_name, false, false) 89 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 90 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 91 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) 92 INITIALIZE_PASS_END(AlignmentFromAssumptions, AA_NAME, 93 aip_name, false, false) 94 95 FunctionPass *llvm::createAlignmentFromAssumptionsPass() { 96 return new AlignmentFromAssumptions(); 97 } 98 99 // Given an expression for the (constant) alignment, AlignSCEV, and an 100 // expression for the displacement between a pointer and the aligned address, 101 // DiffSCEV, compute the alignment of the displaced pointer if it can be reduced 102 // to a constant. Using SCEV to compute alignment handles the case where 103 // DiffSCEV is a recurrence with constant start such that the aligned offset 104 // is constant. e.g. {16,+,32} % 32 -> 16. 105 static unsigned getNewAlignmentDiff(const SCEV *DiffSCEV, 106 const SCEV *AlignSCEV, 107 ScalarEvolution *SE) { 108 // DiffUnits = Diff % int64_t(Alignment) 109 const SCEV *DiffAlignDiv = SE->getUDivExpr(DiffSCEV, AlignSCEV); 110 const SCEV *DiffAlign = SE->getMulExpr(DiffAlignDiv, AlignSCEV); 111 const SCEV *DiffUnitsSCEV = SE->getMinusSCEV(DiffAlign, DiffSCEV); 112 113 DEBUG(dbgs() << "\talignment relative to " << *AlignSCEV << " is " << 114 *DiffUnitsSCEV << " (diff: " << *DiffSCEV << ")\n"); 115 116 if (const SCEVConstant *ConstDUSCEV = 117 dyn_cast<SCEVConstant>(DiffUnitsSCEV)) { 118 int64_t DiffUnits = ConstDUSCEV->getValue()->getSExtValue(); 119 120 // If the displacement is an exact multiple of the alignment, then the 121 // displaced pointer has the same alignment as the aligned pointer, so 122 // return the alignment value. 123 if (!DiffUnits) 124 return (unsigned) 125 cast<SCEVConstant>(AlignSCEV)->getValue()->getSExtValue(); 126 127 // If the displacement is not an exact multiple, but the remainder is a 128 // constant, then return this remainder (but only if it is a power of 2). 129 uint64_t DiffUnitsAbs = std::abs(DiffUnits); 130 if (isPowerOf2_64(DiffUnitsAbs)) 131 return (unsigned) DiffUnitsAbs; 132 } 133 134 return 0; 135 } 136 137 // There is an address given by an offset OffSCEV from AASCEV which has an 138 // alignment AlignSCEV. Use that information, if possible, to compute a new 139 // alignment for Ptr. 140 static unsigned getNewAlignment(const SCEV *AASCEV, const SCEV *AlignSCEV, 141 const SCEV *OffSCEV, Value *Ptr, 142 ScalarEvolution *SE) { 143 const SCEV *PtrSCEV = SE->getSCEV(Ptr); 144 const SCEV *DiffSCEV = SE->getMinusSCEV(PtrSCEV, AASCEV); 145 146 // On 32-bit platforms, DiffSCEV might now have type i32 -- we've always 147 // sign-extended OffSCEV to i64, so make sure they agree again. 148 DiffSCEV = SE->getNoopOrSignExtend(DiffSCEV, OffSCEV->getType()); 149 150 // What we really want to know is the overall offset to the aligned 151 // address. This address is displaced by the provided offset. 152 DiffSCEV = SE->getMinusSCEV(DiffSCEV, OffSCEV); 153 154 DEBUG(dbgs() << "AFI: alignment of " << *Ptr << " relative to " << 155 *AlignSCEV << " and offset " << *OffSCEV << 156 " using diff " << *DiffSCEV << "\n"); 157 158 unsigned NewAlignment = getNewAlignmentDiff(DiffSCEV, AlignSCEV, SE); 159 DEBUG(dbgs() << "\tnew alignment: " << NewAlignment << "\n"); 160 161 if (NewAlignment) { 162 return NewAlignment; 163 } else if (const SCEVAddRecExpr *DiffARSCEV = 164 dyn_cast<SCEVAddRecExpr>(DiffSCEV)) { 165 // The relative offset to the alignment assumption did not yield a constant, 166 // but we should try harder: if we assume that a is 32-byte aligned, then in 167 // for (i = 0; i < 1024; i += 4) r += a[i]; not all of the loads from a are 168 // 32-byte aligned, but instead alternate between 32 and 16-byte alignment. 169 // As a result, the new alignment will not be a constant, but can still 170 // be improved over the default (of 4) to 16. 171 172 const SCEV *DiffStartSCEV = DiffARSCEV->getStart(); 173 const SCEV *DiffIncSCEV = DiffARSCEV->getStepRecurrence(*SE); 174 175 DEBUG(dbgs() << "\ttrying start/inc alignment using start " << 176 *DiffStartSCEV << " and inc " << *DiffIncSCEV << "\n"); 177 178 // Now compute the new alignment using the displacement to the value in the 179 // first iteration, and also the alignment using the per-iteration delta. 180 // If these are the same, then use that answer. Otherwise, use the smaller 181 // one, but only if it divides the larger one. 182 NewAlignment = getNewAlignmentDiff(DiffStartSCEV, AlignSCEV, SE); 183 unsigned NewIncAlignment = getNewAlignmentDiff(DiffIncSCEV, AlignSCEV, SE); 184 185 DEBUG(dbgs() << "\tnew start alignment: " << NewAlignment << "\n"); 186 DEBUG(dbgs() << "\tnew inc alignment: " << NewIncAlignment << "\n"); 187 188 if (!NewAlignment || !NewIncAlignment) { 189 return 0; 190 } else if (NewAlignment > NewIncAlignment) { 191 if (NewAlignment % NewIncAlignment == 0) { 192 DEBUG(dbgs() << "\tnew start/inc alignment: " << 193 NewIncAlignment << "\n"); 194 return NewIncAlignment; 195 } 196 } else if (NewIncAlignment > NewAlignment) { 197 if (NewIncAlignment % NewAlignment == 0) { 198 DEBUG(dbgs() << "\tnew start/inc alignment: " << 199 NewAlignment << "\n"); 200 return NewAlignment; 201 } 202 } else if (NewIncAlignment == NewAlignment) { 203 DEBUG(dbgs() << "\tnew start/inc alignment: " << 204 NewAlignment << "\n"); 205 return NewAlignment; 206 } 207 } 208 209 return 0; 210 } 211 212 bool AlignmentFromAssumptions::extractAlignmentInfo(CallInst *I, 213 Value *&AAPtr, const SCEV *&AlignSCEV, 214 const SCEV *&OffSCEV) { 215 // An alignment assume must be a statement about the least-significant 216 // bits of the pointer being zero, possibly with some offset. 217 ICmpInst *ICI = dyn_cast<ICmpInst>(I->getArgOperand(0)); 218 if (!ICI) 219 return false; 220 221 // This must be an expression of the form: x & m == 0. 222 if (ICI->getPredicate() != ICmpInst::ICMP_EQ) 223 return false; 224 225 // Swap things around so that the RHS is 0. 226 Value *CmpLHS = ICI->getOperand(0); 227 Value *CmpRHS = ICI->getOperand(1); 228 const SCEV *CmpLHSSCEV = SE->getSCEV(CmpLHS); 229 const SCEV *CmpRHSSCEV = SE->getSCEV(CmpRHS); 230 if (CmpLHSSCEV->isZero()) 231 std::swap(CmpLHS, CmpRHS); 232 else if (!CmpRHSSCEV->isZero()) 233 return false; 234 235 BinaryOperator *CmpBO = dyn_cast<BinaryOperator>(CmpLHS); 236 if (!CmpBO || CmpBO->getOpcode() != Instruction::And) 237 return false; 238 239 // Swap things around so that the right operand of the and is a constant 240 // (the mask); we cannot deal with variable masks. 241 Value *AndLHS = CmpBO->getOperand(0); 242 Value *AndRHS = CmpBO->getOperand(1); 243 const SCEV *AndLHSSCEV = SE->getSCEV(AndLHS); 244 const SCEV *AndRHSSCEV = SE->getSCEV(AndRHS); 245 if (isa<SCEVConstant>(AndLHSSCEV)) { 246 std::swap(AndLHS, AndRHS); 247 std::swap(AndLHSSCEV, AndRHSSCEV); 248 } 249 250 const SCEVConstant *MaskSCEV = dyn_cast<SCEVConstant>(AndRHSSCEV); 251 if (!MaskSCEV) 252 return false; 253 254 // The mask must have some trailing ones (otherwise the condition is 255 // trivial and tells us nothing about the alignment of the left operand). 256 unsigned TrailingOnes = 257 MaskSCEV->getValue()->getValue().countTrailingOnes(); 258 if (!TrailingOnes) 259 return false; 260 261 // Cap the alignment at the maximum with which LLVM can deal (and make sure 262 // we don't overflow the shift). 263 uint64_t Alignment; 264 TrailingOnes = std::min(TrailingOnes, 265 unsigned(sizeof(unsigned) * CHAR_BIT - 1)); 266 Alignment = std::min(1u << TrailingOnes, +Value::MaximumAlignment); 267 268 Type *Int64Ty = Type::getInt64Ty(I->getParent()->getParent()->getContext()); 269 AlignSCEV = SE->getConstant(Int64Ty, Alignment); 270 271 // The LHS might be a ptrtoint instruction, or it might be the pointer 272 // with an offset. 273 AAPtr = nullptr; 274 OffSCEV = nullptr; 275 if (PtrToIntInst *PToI = dyn_cast<PtrToIntInst>(AndLHS)) { 276 AAPtr = PToI->getPointerOperand(); 277 OffSCEV = SE->getZero(Int64Ty); 278 } else if (const SCEVAddExpr* AndLHSAddSCEV = 279 dyn_cast<SCEVAddExpr>(AndLHSSCEV)) { 280 // Try to find the ptrtoint; subtract it and the rest is the offset. 281 for (SCEVAddExpr::op_iterator J = AndLHSAddSCEV->op_begin(), 282 JE = AndLHSAddSCEV->op_end(); J != JE; ++J) 283 if (const SCEVUnknown *OpUnk = dyn_cast<SCEVUnknown>(*J)) 284 if (PtrToIntInst *PToI = dyn_cast<PtrToIntInst>(OpUnk->getValue())) { 285 AAPtr = PToI->getPointerOperand(); 286 OffSCEV = SE->getMinusSCEV(AndLHSAddSCEV, *J); 287 break; 288 } 289 } 290 291 if (!AAPtr) 292 return false; 293 294 // Sign extend the offset to 64 bits (so that it is like all of the other 295 // expressions). 296 unsigned OffSCEVBits = OffSCEV->getType()->getPrimitiveSizeInBits(); 297 if (OffSCEVBits < 64) 298 OffSCEV = SE->getSignExtendExpr(OffSCEV, Int64Ty); 299 else if (OffSCEVBits > 64) 300 return false; 301 302 AAPtr = AAPtr->stripPointerCasts(); 303 return true; 304 } 305 306 bool AlignmentFromAssumptions::processAssumption(CallInst *ACall) { 307 Value *AAPtr; 308 const SCEV *AlignSCEV, *OffSCEV; 309 if (!extractAlignmentInfo(ACall, AAPtr, AlignSCEV, OffSCEV)) 310 return false; 311 312 const SCEV *AASCEV = SE->getSCEV(AAPtr); 313 314 // Apply the assumption to all other users of the specified pointer. 315 SmallPtrSet<Instruction *, 32> Visited; 316 SmallVector<Instruction*, 16> WorkList; 317 for (User *J : AAPtr->users()) { 318 if (J == ACall) 319 continue; 320 321 if (Instruction *K = dyn_cast<Instruction>(J)) 322 if (isValidAssumeForContext(ACall, K, DT)) 323 WorkList.push_back(K); 324 } 325 326 while (!WorkList.empty()) { 327 Instruction *J = WorkList.pop_back_val(); 328 329 if (LoadInst *LI = dyn_cast<LoadInst>(J)) { 330 unsigned NewAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV, 331 LI->getPointerOperand(), SE); 332 333 if (NewAlignment > LI->getAlignment()) { 334 LI->setAlignment(NewAlignment); 335 ++NumLoadAlignChanged; 336 } 337 } else if (StoreInst *SI = dyn_cast<StoreInst>(J)) { 338 unsigned NewAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV, 339 SI->getPointerOperand(), SE); 340 341 if (NewAlignment > SI->getAlignment()) { 342 SI->setAlignment(NewAlignment); 343 ++NumStoreAlignChanged; 344 } 345 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(J)) { 346 unsigned NewDestAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV, 347 MI->getDest(), SE); 348 349 // For memory transfers, we need a common alignment for both the 350 // source and destination. If we have a new alignment for this 351 // instruction, but only for one operand, save it. If we reach the 352 // other operand through another assumption later, then we may 353 // change the alignment at that point. 354 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { 355 unsigned NewSrcAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV, 356 MTI->getSource(), SE); 357 358 DenseMap<MemTransferInst *, unsigned>::iterator DI = 359 NewDestAlignments.find(MTI); 360 unsigned AltDestAlignment = (DI == NewDestAlignments.end()) ? 361 0 : DI->second; 362 363 DenseMap<MemTransferInst *, unsigned>::iterator SI = 364 NewSrcAlignments.find(MTI); 365 unsigned AltSrcAlignment = (SI == NewSrcAlignments.end()) ? 366 0 : SI->second; 367 368 DEBUG(dbgs() << "\tmem trans: " << NewDestAlignment << " " << 369 AltDestAlignment << " " << NewSrcAlignment << 370 " " << AltSrcAlignment << "\n"); 371 372 // Of these four alignments, pick the largest possible... 373 unsigned NewAlignment = 0; 374 if (NewDestAlignment <= std::max(NewSrcAlignment, AltSrcAlignment)) 375 NewAlignment = std::max(NewAlignment, NewDestAlignment); 376 if (AltDestAlignment <= std::max(NewSrcAlignment, AltSrcAlignment)) 377 NewAlignment = std::max(NewAlignment, AltDestAlignment); 378 if (NewSrcAlignment <= std::max(NewDestAlignment, AltDestAlignment)) 379 NewAlignment = std::max(NewAlignment, NewSrcAlignment); 380 if (AltSrcAlignment <= std::max(NewDestAlignment, AltDestAlignment)) 381 NewAlignment = std::max(NewAlignment, AltSrcAlignment); 382 383 if (NewAlignment > MI->getAlignment()) { 384 MI->setAlignment(ConstantInt::get(Type::getInt32Ty( 385 MI->getParent()->getContext()), NewAlignment)); 386 ++NumMemIntAlignChanged; 387 } 388 389 NewDestAlignments.insert(std::make_pair(MTI, NewDestAlignment)); 390 NewSrcAlignments.insert(std::make_pair(MTI, NewSrcAlignment)); 391 } else if (NewDestAlignment > MI->getAlignment()) { 392 assert((!isa<MemIntrinsic>(MI) || isa<MemSetInst>(MI)) && 393 "Unknown memory intrinsic"); 394 395 MI->setAlignment(ConstantInt::get(Type::getInt32Ty( 396 MI->getParent()->getContext()), NewDestAlignment)); 397 ++NumMemIntAlignChanged; 398 } 399 } 400 401 // Now that we've updated that use of the pointer, look for other uses of 402 // the pointer to update. 403 Visited.insert(J); 404 for (User *UJ : J->users()) { 405 Instruction *K = cast<Instruction>(UJ); 406 if (!Visited.count(K) && isValidAssumeForContext(ACall, K, DT)) 407 WorkList.push_back(K); 408 } 409 } 410 411 return true; 412 } 413 414 bool AlignmentFromAssumptions::runOnFunction(Function &F) { 415 bool Changed = false; 416 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 417 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 418 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 419 420 NewDestAlignments.clear(); 421 NewSrcAlignments.clear(); 422 423 for (auto &AssumeVH : AC.assumptions()) 424 if (AssumeVH) 425 Changed |= processAssumption(cast<CallInst>(AssumeVH)); 426 427 return Changed; 428 } 429 430