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