1 //===-- LoopUtils.cpp - Loop Utility functions -------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines common loop utility functions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Utils/LoopUtils.h" 15 #include "llvm/Analysis/AliasAnalysis.h" 16 #include "llvm/Analysis/BasicAliasAnalysis.h" 17 #include "llvm/Analysis/GlobalsModRef.h" 18 #include "llvm/Analysis/GlobalsModRef.h" 19 #include "llvm/Analysis/LoopInfo.h" 20 #include "llvm/Analysis/LoopPass.h" 21 #include "llvm/Analysis/ScalarEvolution.h" 22 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 23 #include "llvm/Analysis/ScalarEvolutionExpander.h" 24 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 25 #include "llvm/IR/Dominators.h" 26 #include "llvm/IR/Instructions.h" 27 #include "llvm/IR/Module.h" 28 #include "llvm/IR/PatternMatch.h" 29 #include "llvm/IR/ValueHandle.h" 30 #include "llvm/Pass.h" 31 #include "llvm/Support/Debug.h" 32 33 using namespace llvm; 34 using namespace llvm::PatternMatch; 35 36 #define DEBUG_TYPE "loop-utils" 37 38 bool RecurrenceDescriptor::areAllUsesIn(Instruction *I, 39 SmallPtrSetImpl<Instruction *> &Set) { 40 for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; ++Use) 41 if (!Set.count(dyn_cast<Instruction>(*Use))) 42 return false; 43 return true; 44 } 45 46 bool RecurrenceDescriptor::isIntegerRecurrenceKind(RecurrenceKind Kind) { 47 switch (Kind) { 48 default: 49 break; 50 case RK_IntegerAdd: 51 case RK_IntegerMult: 52 case RK_IntegerOr: 53 case RK_IntegerAnd: 54 case RK_IntegerXor: 55 case RK_IntegerMinMax: 56 return true; 57 } 58 return false; 59 } 60 61 bool RecurrenceDescriptor::isFloatingPointRecurrenceKind(RecurrenceKind Kind) { 62 return (Kind != RK_NoRecurrence) && !isIntegerRecurrenceKind(Kind); 63 } 64 65 bool RecurrenceDescriptor::isArithmeticRecurrenceKind(RecurrenceKind Kind) { 66 switch (Kind) { 67 default: 68 break; 69 case RK_IntegerAdd: 70 case RK_IntegerMult: 71 case RK_FloatAdd: 72 case RK_FloatMult: 73 return true; 74 } 75 return false; 76 } 77 78 Instruction * 79 RecurrenceDescriptor::lookThroughAnd(PHINode *Phi, Type *&RT, 80 SmallPtrSetImpl<Instruction *> &Visited, 81 SmallPtrSetImpl<Instruction *> &CI) { 82 if (!Phi->hasOneUse()) 83 return Phi; 84 85 const APInt *M = nullptr; 86 Instruction *I, *J = cast<Instruction>(Phi->use_begin()->getUser()); 87 88 // Matches either I & 2^x-1 or 2^x-1 & I. If we find a match, we update RT 89 // with a new integer type of the corresponding bit width. 90 if (match(J, m_CombineOr(m_And(m_Instruction(I), m_APInt(M)), 91 m_And(m_APInt(M), m_Instruction(I))))) { 92 int32_t Bits = (*M + 1).exactLogBase2(); 93 if (Bits > 0) { 94 RT = IntegerType::get(Phi->getContext(), Bits); 95 Visited.insert(Phi); 96 CI.insert(J); 97 return J; 98 } 99 } 100 return Phi; 101 } 102 103 bool RecurrenceDescriptor::getSourceExtensionKind( 104 Instruction *Start, Instruction *Exit, Type *RT, bool &IsSigned, 105 SmallPtrSetImpl<Instruction *> &Visited, 106 SmallPtrSetImpl<Instruction *> &CI) { 107 108 SmallVector<Instruction *, 8> Worklist; 109 bool FoundOneOperand = false; 110 unsigned DstSize = RT->getPrimitiveSizeInBits(); 111 Worklist.push_back(Exit); 112 113 // Traverse the instructions in the reduction expression, beginning with the 114 // exit value. 115 while (!Worklist.empty()) { 116 Instruction *I = Worklist.pop_back_val(); 117 for (Use &U : I->operands()) { 118 119 // Terminate the traversal if the operand is not an instruction, or we 120 // reach the starting value. 121 Instruction *J = dyn_cast<Instruction>(U.get()); 122 if (!J || J == Start) 123 continue; 124 125 // Otherwise, investigate the operation if it is also in the expression. 126 if (Visited.count(J)) { 127 Worklist.push_back(J); 128 continue; 129 } 130 131 // If the operand is not in Visited, it is not a reduction operation, but 132 // it does feed into one. Make sure it is either a single-use sign- or 133 // zero-extend instruction. 134 CastInst *Cast = dyn_cast<CastInst>(J); 135 bool IsSExtInst = isa<SExtInst>(J); 136 if (!Cast || !Cast->hasOneUse() || !(isa<ZExtInst>(J) || IsSExtInst)) 137 return false; 138 139 // Ensure the source type of the extend is no larger than the reduction 140 // type. It is not necessary for the types to be identical. 141 unsigned SrcSize = Cast->getSrcTy()->getPrimitiveSizeInBits(); 142 if (SrcSize > DstSize) 143 return false; 144 145 // Furthermore, ensure that all such extends are of the same kind. 146 if (FoundOneOperand) { 147 if (IsSigned != IsSExtInst) 148 return false; 149 } else { 150 FoundOneOperand = true; 151 IsSigned = IsSExtInst; 152 } 153 154 // Lastly, if the source type of the extend matches the reduction type, 155 // add the extend to CI so that we can avoid accounting for it in the 156 // cost model. 157 if (SrcSize == DstSize) 158 CI.insert(Cast); 159 } 160 } 161 return true; 162 } 163 164 bool RecurrenceDescriptor::AddReductionVar(PHINode *Phi, RecurrenceKind Kind, 165 Loop *TheLoop, bool HasFunNoNaNAttr, 166 RecurrenceDescriptor &RedDes) { 167 if (Phi->getNumIncomingValues() != 2) 168 return false; 169 170 // Reduction variables are only found in the loop header block. 171 if (Phi->getParent() != TheLoop->getHeader()) 172 return false; 173 174 // Obtain the reduction start value from the value that comes from the loop 175 // preheader. 176 Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader()); 177 178 // ExitInstruction is the single value which is used outside the loop. 179 // We only allow for a single reduction value to be used outside the loop. 180 // This includes users of the reduction, variables (which form a cycle 181 // which ends in the phi node). 182 Instruction *ExitInstruction = nullptr; 183 // Indicates that we found a reduction operation in our scan. 184 bool FoundReduxOp = false; 185 186 // We start with the PHI node and scan for all of the users of this 187 // instruction. All users must be instructions that can be used as reduction 188 // variables (such as ADD). We must have a single out-of-block user. The cycle 189 // must include the original PHI. 190 bool FoundStartPHI = false; 191 192 // To recognize min/max patterns formed by a icmp select sequence, we store 193 // the number of instruction we saw from the recognized min/max pattern, 194 // to make sure we only see exactly the two instructions. 195 unsigned NumCmpSelectPatternInst = 0; 196 InstDesc ReduxDesc(false, nullptr); 197 198 // Data used for determining if the recurrence has been type-promoted. 199 Type *RecurrenceType = Phi->getType(); 200 SmallPtrSet<Instruction *, 4> CastInsts; 201 Instruction *Start = Phi; 202 bool IsSigned = false; 203 204 SmallPtrSet<Instruction *, 8> VisitedInsts; 205 SmallVector<Instruction *, 8> Worklist; 206 207 // Return early if the recurrence kind does not match the type of Phi. If the 208 // recurrence kind is arithmetic, we attempt to look through AND operations 209 // resulting from the type promotion performed by InstCombine. Vector 210 // operations are not limited to the legal integer widths, so we may be able 211 // to evaluate the reduction in the narrower width. 212 if (RecurrenceType->isFloatingPointTy()) { 213 if (!isFloatingPointRecurrenceKind(Kind)) 214 return false; 215 } else { 216 if (!isIntegerRecurrenceKind(Kind)) 217 return false; 218 if (isArithmeticRecurrenceKind(Kind)) 219 Start = lookThroughAnd(Phi, RecurrenceType, VisitedInsts, CastInsts); 220 } 221 222 Worklist.push_back(Start); 223 VisitedInsts.insert(Start); 224 225 // A value in the reduction can be used: 226 // - By the reduction: 227 // - Reduction operation: 228 // - One use of reduction value (safe). 229 // - Multiple use of reduction value (not safe). 230 // - PHI: 231 // - All uses of the PHI must be the reduction (safe). 232 // - Otherwise, not safe. 233 // - By instructions outside of the loop (safe). 234 // * One value may have several outside users, but all outside 235 // uses must be of the same value. 236 // - By an instruction that is not part of the reduction (not safe). 237 // This is either: 238 // * An instruction type other than PHI or the reduction operation. 239 // * A PHI in the header other than the initial PHI. 240 while (!Worklist.empty()) { 241 Instruction *Cur = Worklist.back(); 242 Worklist.pop_back(); 243 244 // No Users. 245 // If the instruction has no users then this is a broken chain and can't be 246 // a reduction variable. 247 if (Cur->use_empty()) 248 return false; 249 250 bool IsAPhi = isa<PHINode>(Cur); 251 252 // A header PHI use other than the original PHI. 253 if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent()) 254 return false; 255 256 // Reductions of instructions such as Div, and Sub is only possible if the 257 // LHS is the reduction variable. 258 if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) && 259 !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) && 260 !VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0)))) 261 return false; 262 263 // Any reduction instruction must be of one of the allowed kinds. We ignore 264 // the starting value (the Phi or an AND instruction if the Phi has been 265 // type-promoted). 266 if (Cur != Start) { 267 ReduxDesc = isRecurrenceInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr); 268 if (!ReduxDesc.isRecurrence()) 269 return false; 270 } 271 272 // A reduction operation must only have one use of the reduction value. 273 if (!IsAPhi && Kind != RK_IntegerMinMax && Kind != RK_FloatMinMax && 274 hasMultipleUsesOf(Cur, VisitedInsts)) 275 return false; 276 277 // All inputs to a PHI node must be a reduction value. 278 if (IsAPhi && Cur != Phi && !areAllUsesIn(Cur, VisitedInsts)) 279 return false; 280 281 if (Kind == RK_IntegerMinMax && 282 (isa<ICmpInst>(Cur) || isa<SelectInst>(Cur))) 283 ++NumCmpSelectPatternInst; 284 if (Kind == RK_FloatMinMax && (isa<FCmpInst>(Cur) || isa<SelectInst>(Cur))) 285 ++NumCmpSelectPatternInst; 286 287 // Check whether we found a reduction operator. 288 FoundReduxOp |= !IsAPhi && Cur != Start; 289 290 // Process users of current instruction. Push non-PHI nodes after PHI nodes 291 // onto the stack. This way we are going to have seen all inputs to PHI 292 // nodes once we get to them. 293 SmallVector<Instruction *, 8> NonPHIs; 294 SmallVector<Instruction *, 8> PHIs; 295 for (User *U : Cur->users()) { 296 Instruction *UI = cast<Instruction>(U); 297 298 // Check if we found the exit user. 299 BasicBlock *Parent = UI->getParent(); 300 if (!TheLoop->contains(Parent)) { 301 // If we already know this instruction is used externally, move on to 302 // the next user. 303 if (ExitInstruction == Cur) 304 continue; 305 306 // Exit if you find multiple values used outside or if the header phi 307 // node is being used. In this case the user uses the value of the 308 // previous iteration, in which case we would loose "VF-1" iterations of 309 // the reduction operation if we vectorize. 310 if (ExitInstruction != nullptr || Cur == Phi) 311 return false; 312 313 // The instruction used by an outside user must be the last instruction 314 // before we feed back to the reduction phi. Otherwise, we loose VF-1 315 // operations on the value. 316 if (!is_contained(Phi->operands(), Cur)) 317 return false; 318 319 ExitInstruction = Cur; 320 continue; 321 } 322 323 // Process instructions only once (termination). Each reduction cycle 324 // value must only be used once, except by phi nodes and min/max 325 // reductions which are represented as a cmp followed by a select. 326 InstDesc IgnoredVal(false, nullptr); 327 if (VisitedInsts.insert(UI).second) { 328 if (isa<PHINode>(UI)) 329 PHIs.push_back(UI); 330 else 331 NonPHIs.push_back(UI); 332 } else if (!isa<PHINode>(UI) && 333 ((!isa<FCmpInst>(UI) && !isa<ICmpInst>(UI) && 334 !isa<SelectInst>(UI)) || 335 !isMinMaxSelectCmpPattern(UI, IgnoredVal).isRecurrence())) 336 return false; 337 338 // Remember that we completed the cycle. 339 if (UI == Phi) 340 FoundStartPHI = true; 341 } 342 Worklist.append(PHIs.begin(), PHIs.end()); 343 Worklist.append(NonPHIs.begin(), NonPHIs.end()); 344 } 345 346 // This means we have seen one but not the other instruction of the 347 // pattern or more than just a select and cmp. 348 if ((Kind == RK_IntegerMinMax || Kind == RK_FloatMinMax) && 349 NumCmpSelectPatternInst != 2) 350 return false; 351 352 if (!FoundStartPHI || !FoundReduxOp || !ExitInstruction) 353 return false; 354 355 // If we think Phi may have been type-promoted, we also need to ensure that 356 // all source operands of the reduction are either SExtInsts or ZEstInsts. If 357 // so, we will be able to evaluate the reduction in the narrower bit width. 358 if (Start != Phi) 359 if (!getSourceExtensionKind(Start, ExitInstruction, RecurrenceType, 360 IsSigned, VisitedInsts, CastInsts)) 361 return false; 362 363 // We found a reduction var if we have reached the original phi node and we 364 // only have a single instruction with out-of-loop users. 365 366 // The ExitInstruction(Instruction which is allowed to have out-of-loop users) 367 // is saved as part of the RecurrenceDescriptor. 368 369 // Save the description of this reduction variable. 370 RecurrenceDescriptor RD( 371 RdxStart, ExitInstruction, Kind, ReduxDesc.getMinMaxKind(), 372 ReduxDesc.getUnsafeAlgebraInst(), RecurrenceType, IsSigned, CastInsts); 373 RedDes = RD; 374 375 return true; 376 } 377 378 /// Returns true if the instruction is a Select(ICmp(X, Y), X, Y) instruction 379 /// pattern corresponding to a min(X, Y) or max(X, Y). 380 RecurrenceDescriptor::InstDesc 381 RecurrenceDescriptor::isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev) { 382 383 assert((isa<ICmpInst>(I) || isa<FCmpInst>(I) || isa<SelectInst>(I)) && 384 "Expect a select instruction"); 385 Instruction *Cmp = nullptr; 386 SelectInst *Select = nullptr; 387 388 // We must handle the select(cmp()) as a single instruction. Advance to the 389 // select. 390 if ((Cmp = dyn_cast<ICmpInst>(I)) || (Cmp = dyn_cast<FCmpInst>(I))) { 391 if (!Cmp->hasOneUse() || !(Select = dyn_cast<SelectInst>(*I->user_begin()))) 392 return InstDesc(false, I); 393 return InstDesc(Select, Prev.getMinMaxKind()); 394 } 395 396 // Only handle single use cases for now. 397 if (!(Select = dyn_cast<SelectInst>(I))) 398 return InstDesc(false, I); 399 if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) && 400 !(Cmp = dyn_cast<FCmpInst>(I->getOperand(0)))) 401 return InstDesc(false, I); 402 if (!Cmp->hasOneUse()) 403 return InstDesc(false, I); 404 405 Value *CmpLeft; 406 Value *CmpRight; 407 408 // Look for a min/max pattern. 409 if (m_UMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 410 return InstDesc(Select, MRK_UIntMin); 411 else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 412 return InstDesc(Select, MRK_UIntMax); 413 else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 414 return InstDesc(Select, MRK_SIntMax); 415 else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 416 return InstDesc(Select, MRK_SIntMin); 417 else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 418 return InstDesc(Select, MRK_FloatMin); 419 else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 420 return InstDesc(Select, MRK_FloatMax); 421 else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 422 return InstDesc(Select, MRK_FloatMin); 423 else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 424 return InstDesc(Select, MRK_FloatMax); 425 426 return InstDesc(false, I); 427 } 428 429 RecurrenceDescriptor::InstDesc 430 RecurrenceDescriptor::isRecurrenceInstr(Instruction *I, RecurrenceKind Kind, 431 InstDesc &Prev, bool HasFunNoNaNAttr) { 432 bool FP = I->getType()->isFloatingPointTy(); 433 Instruction *UAI = Prev.getUnsafeAlgebraInst(); 434 if (!UAI && FP && !I->hasUnsafeAlgebra()) 435 UAI = I; // Found an unsafe (unvectorizable) algebra instruction. 436 437 switch (I->getOpcode()) { 438 default: 439 return InstDesc(false, I); 440 case Instruction::PHI: 441 return InstDesc(I, Prev.getMinMaxKind(), Prev.getUnsafeAlgebraInst()); 442 case Instruction::Sub: 443 case Instruction::Add: 444 return InstDesc(Kind == RK_IntegerAdd, I); 445 case Instruction::Mul: 446 return InstDesc(Kind == RK_IntegerMult, I); 447 case Instruction::And: 448 return InstDesc(Kind == RK_IntegerAnd, I); 449 case Instruction::Or: 450 return InstDesc(Kind == RK_IntegerOr, I); 451 case Instruction::Xor: 452 return InstDesc(Kind == RK_IntegerXor, I); 453 case Instruction::FMul: 454 return InstDesc(Kind == RK_FloatMult, I, UAI); 455 case Instruction::FSub: 456 case Instruction::FAdd: 457 return InstDesc(Kind == RK_FloatAdd, I, UAI); 458 case Instruction::FCmp: 459 case Instruction::ICmp: 460 case Instruction::Select: 461 if (Kind != RK_IntegerMinMax && 462 (!HasFunNoNaNAttr || Kind != RK_FloatMinMax)) 463 return InstDesc(false, I); 464 return isMinMaxSelectCmpPattern(I, Prev); 465 } 466 } 467 468 bool RecurrenceDescriptor::hasMultipleUsesOf( 469 Instruction *I, SmallPtrSetImpl<Instruction *> &Insts) { 470 unsigned NumUses = 0; 471 for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; 472 ++Use) { 473 if (Insts.count(dyn_cast<Instruction>(*Use))) 474 ++NumUses; 475 if (NumUses > 1) 476 return true; 477 } 478 479 return false; 480 } 481 bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop, 482 RecurrenceDescriptor &RedDes) { 483 484 BasicBlock *Header = TheLoop->getHeader(); 485 Function &F = *Header->getParent(); 486 bool HasFunNoNaNAttr = 487 F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true"; 488 489 if (AddReductionVar(Phi, RK_IntegerAdd, TheLoop, HasFunNoNaNAttr, RedDes)) { 490 DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n"); 491 return true; 492 } 493 if (AddReductionVar(Phi, RK_IntegerMult, TheLoop, HasFunNoNaNAttr, RedDes)) { 494 DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n"); 495 return true; 496 } 497 if (AddReductionVar(Phi, RK_IntegerOr, TheLoop, HasFunNoNaNAttr, RedDes)) { 498 DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n"); 499 return true; 500 } 501 if (AddReductionVar(Phi, RK_IntegerAnd, TheLoop, HasFunNoNaNAttr, RedDes)) { 502 DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n"); 503 return true; 504 } 505 if (AddReductionVar(Phi, RK_IntegerXor, TheLoop, HasFunNoNaNAttr, RedDes)) { 506 DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n"); 507 return true; 508 } 509 if (AddReductionVar(Phi, RK_IntegerMinMax, TheLoop, HasFunNoNaNAttr, 510 RedDes)) { 511 DEBUG(dbgs() << "Found a MINMAX reduction PHI." << *Phi << "\n"); 512 return true; 513 } 514 if (AddReductionVar(Phi, RK_FloatMult, TheLoop, HasFunNoNaNAttr, RedDes)) { 515 DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n"); 516 return true; 517 } 518 if (AddReductionVar(Phi, RK_FloatAdd, TheLoop, HasFunNoNaNAttr, RedDes)) { 519 DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n"); 520 return true; 521 } 522 if (AddReductionVar(Phi, RK_FloatMinMax, TheLoop, HasFunNoNaNAttr, RedDes)) { 523 DEBUG(dbgs() << "Found an float MINMAX reduction PHI." << *Phi << "\n"); 524 return true; 525 } 526 // Not a reduction of known type. 527 return false; 528 } 529 530 bool RecurrenceDescriptor::isFirstOrderRecurrence(PHINode *Phi, Loop *TheLoop, 531 DominatorTree *DT) { 532 533 // Ensure the phi node is in the loop header and has two incoming values. 534 if (Phi->getParent() != TheLoop->getHeader() || 535 Phi->getNumIncomingValues() != 2) 536 return false; 537 538 // Ensure the loop has a preheader and a single latch block. The loop 539 // vectorizer will need the latch to set up the next iteration of the loop. 540 auto *Preheader = TheLoop->getLoopPreheader(); 541 auto *Latch = TheLoop->getLoopLatch(); 542 if (!Preheader || !Latch) 543 return false; 544 545 // Ensure the phi node's incoming blocks are the loop preheader and latch. 546 if (Phi->getBasicBlockIndex(Preheader) < 0 || 547 Phi->getBasicBlockIndex(Latch) < 0) 548 return false; 549 550 // Get the previous value. The previous value comes from the latch edge while 551 // the initial value comes form the preheader edge. 552 auto *Previous = dyn_cast<Instruction>(Phi->getIncomingValueForBlock(Latch)); 553 if (!Previous || !TheLoop->contains(Previous) || isa<PHINode>(Previous)) 554 return false; 555 556 // Ensure every user of the phi node is dominated by the previous value. The 557 // dominance requirement ensures the loop vectorizer will not need to 558 // vectorize the initial value prior to the first iteration of the loop. 559 for (User *U : Phi->users()) 560 if (auto *I = dyn_cast<Instruction>(U)) 561 if (!DT->dominates(Previous, I)) 562 return false; 563 564 return true; 565 } 566 567 /// This function returns the identity element (or neutral element) for 568 /// the operation K. 569 Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurrenceKind K, 570 Type *Tp) { 571 switch (K) { 572 case RK_IntegerXor: 573 case RK_IntegerAdd: 574 case RK_IntegerOr: 575 // Adding, Xoring, Oring zero to a number does not change it. 576 return ConstantInt::get(Tp, 0); 577 case RK_IntegerMult: 578 // Multiplying a number by 1 does not change it. 579 return ConstantInt::get(Tp, 1); 580 case RK_IntegerAnd: 581 // AND-ing a number with an all-1 value does not change it. 582 return ConstantInt::get(Tp, -1, true); 583 case RK_FloatMult: 584 // Multiplying a number by 1 does not change it. 585 return ConstantFP::get(Tp, 1.0L); 586 case RK_FloatAdd: 587 // Adding zero to a number does not change it. 588 return ConstantFP::get(Tp, 0.0L); 589 default: 590 llvm_unreachable("Unknown recurrence kind"); 591 } 592 } 593 594 /// This function translates the recurrence kind to an LLVM binary operator. 595 unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurrenceKind Kind) { 596 switch (Kind) { 597 case RK_IntegerAdd: 598 return Instruction::Add; 599 case RK_IntegerMult: 600 return Instruction::Mul; 601 case RK_IntegerOr: 602 return Instruction::Or; 603 case RK_IntegerAnd: 604 return Instruction::And; 605 case RK_IntegerXor: 606 return Instruction::Xor; 607 case RK_FloatMult: 608 return Instruction::FMul; 609 case RK_FloatAdd: 610 return Instruction::FAdd; 611 case RK_IntegerMinMax: 612 return Instruction::ICmp; 613 case RK_FloatMinMax: 614 return Instruction::FCmp; 615 default: 616 llvm_unreachable("Unknown recurrence operation"); 617 } 618 } 619 620 Value *RecurrenceDescriptor::createMinMaxOp(IRBuilder<> &Builder, 621 MinMaxRecurrenceKind RK, 622 Value *Left, Value *Right) { 623 CmpInst::Predicate P = CmpInst::ICMP_NE; 624 switch (RK) { 625 default: 626 llvm_unreachable("Unknown min/max recurrence kind"); 627 case MRK_UIntMin: 628 P = CmpInst::ICMP_ULT; 629 break; 630 case MRK_UIntMax: 631 P = CmpInst::ICMP_UGT; 632 break; 633 case MRK_SIntMin: 634 P = CmpInst::ICMP_SLT; 635 break; 636 case MRK_SIntMax: 637 P = CmpInst::ICMP_SGT; 638 break; 639 case MRK_FloatMin: 640 P = CmpInst::FCMP_OLT; 641 break; 642 case MRK_FloatMax: 643 P = CmpInst::FCMP_OGT; 644 break; 645 } 646 647 // We only match FP sequences with unsafe algebra, so we can unconditionally 648 // set it on any generated instructions. 649 IRBuilder<>::FastMathFlagGuard FMFG(Builder); 650 FastMathFlags FMF; 651 FMF.setUnsafeAlgebra(); 652 Builder.setFastMathFlags(FMF); 653 654 Value *Cmp; 655 if (RK == MRK_FloatMin || RK == MRK_FloatMax) 656 Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp"); 657 else 658 Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp"); 659 660 Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select"); 661 return Select; 662 } 663 664 InductionDescriptor::InductionDescriptor(Value *Start, InductionKind K, 665 const SCEV *Step, BinaryOperator *BOp) 666 : StartValue(Start), IK(K), Step(Step), InductionBinOp(BOp) { 667 assert(IK != IK_NoInduction && "Not an induction"); 668 669 // Start value type should match the induction kind and the value 670 // itself should not be null. 671 assert(StartValue && "StartValue is null"); 672 assert((IK != IK_PtrInduction || StartValue->getType()->isPointerTy()) && 673 "StartValue is not a pointer for pointer induction"); 674 assert((IK != IK_IntInduction || StartValue->getType()->isIntegerTy()) && 675 "StartValue is not an integer for integer induction"); 676 677 // Check the Step Value. It should be non-zero integer value. 678 assert((!getConstIntStepValue() || !getConstIntStepValue()->isZero()) && 679 "Step value is zero"); 680 681 assert((IK != IK_PtrInduction || getConstIntStepValue()) && 682 "Step value should be constant for pointer induction"); 683 assert((IK == IK_FpInduction || Step->getType()->isIntegerTy()) && 684 "StepValue is not an integer"); 685 686 assert((IK != IK_FpInduction || Step->getType()->isFloatingPointTy()) && 687 "StepValue is not FP for FpInduction"); 688 assert((IK != IK_FpInduction || (InductionBinOp && 689 (InductionBinOp->getOpcode() == Instruction::FAdd || 690 InductionBinOp->getOpcode() == Instruction::FSub))) && 691 "Binary opcode should be specified for FP induction"); 692 } 693 694 int InductionDescriptor::getConsecutiveDirection() const { 695 ConstantInt *ConstStep = getConstIntStepValue(); 696 if (ConstStep && (ConstStep->isOne() || ConstStep->isMinusOne())) 697 return ConstStep->getSExtValue(); 698 return 0; 699 } 700 701 ConstantInt *InductionDescriptor::getConstIntStepValue() const { 702 if (isa<SCEVConstant>(Step)) 703 return dyn_cast<ConstantInt>(cast<SCEVConstant>(Step)->getValue()); 704 return nullptr; 705 } 706 707 Value *InductionDescriptor::transform(IRBuilder<> &B, Value *Index, 708 ScalarEvolution *SE, 709 const DataLayout& DL) const { 710 711 SCEVExpander Exp(*SE, DL, "induction"); 712 assert(Index->getType() == Step->getType() && 713 "Index type does not match StepValue type"); 714 switch (IK) { 715 case IK_IntInduction: { 716 assert(Index->getType() == StartValue->getType() && 717 "Index type does not match StartValue type"); 718 719 // FIXME: Theoretically, we can call getAddExpr() of ScalarEvolution 720 // and calculate (Start + Index * Step) for all cases, without 721 // special handling for "isOne" and "isMinusOne". 722 // But in the real life the result code getting worse. We mix SCEV 723 // expressions and ADD/SUB operations and receive redundant 724 // intermediate values being calculated in different ways and 725 // Instcombine is unable to reduce them all. 726 727 if (getConstIntStepValue() && 728 getConstIntStepValue()->isMinusOne()) 729 return B.CreateSub(StartValue, Index); 730 if (getConstIntStepValue() && 731 getConstIntStepValue()->isOne()) 732 return B.CreateAdd(StartValue, Index); 733 const SCEV *S = SE->getAddExpr(SE->getSCEV(StartValue), 734 SE->getMulExpr(Step, SE->getSCEV(Index))); 735 return Exp.expandCodeFor(S, StartValue->getType(), &*B.GetInsertPoint()); 736 } 737 case IK_PtrInduction: { 738 assert(isa<SCEVConstant>(Step) && 739 "Expected constant step for pointer induction"); 740 const SCEV *S = SE->getMulExpr(SE->getSCEV(Index), Step); 741 Index = Exp.expandCodeFor(S, Index->getType(), &*B.GetInsertPoint()); 742 return B.CreateGEP(nullptr, StartValue, Index); 743 } 744 case IK_FpInduction: { 745 assert(Step->getType()->isFloatingPointTy() && "Expected FP Step value"); 746 assert(InductionBinOp && 747 (InductionBinOp->getOpcode() == Instruction::FAdd || 748 InductionBinOp->getOpcode() == Instruction::FSub) && 749 "Original bin op should be defined for FP induction"); 750 751 Value *StepValue = cast<SCEVUnknown>(Step)->getValue(); 752 753 // Floating point operations had to be 'fast' to enable the induction. 754 FastMathFlags Flags; 755 Flags.setUnsafeAlgebra(); 756 757 Value *MulExp = B.CreateFMul(StepValue, Index); 758 if (isa<Instruction>(MulExp)) 759 // We have to check, the MulExp may be a constant. 760 cast<Instruction>(MulExp)->setFastMathFlags(Flags); 761 762 Value *BOp = B.CreateBinOp(InductionBinOp->getOpcode() , StartValue, 763 MulExp, "induction"); 764 if (isa<Instruction>(BOp)) 765 cast<Instruction>(BOp)->setFastMathFlags(Flags); 766 767 return BOp; 768 } 769 case IK_NoInduction: 770 return nullptr; 771 } 772 llvm_unreachable("invalid enum"); 773 } 774 775 bool InductionDescriptor::isFPInductionPHI(PHINode *Phi, const Loop *TheLoop, 776 ScalarEvolution *SE, 777 InductionDescriptor &D) { 778 779 // Here we only handle FP induction variables. 780 assert(Phi->getType()->isFloatingPointTy() && "Unexpected Phi type"); 781 782 if (TheLoop->getHeader() != Phi->getParent()) 783 return false; 784 785 // The loop may have multiple entrances or multiple exits; we can analyze 786 // this phi if it has a unique entry value and a unique backedge value. 787 if (Phi->getNumIncomingValues() != 2) 788 return false; 789 Value *BEValue = nullptr, *StartValue = nullptr; 790 if (TheLoop->contains(Phi->getIncomingBlock(0))) { 791 BEValue = Phi->getIncomingValue(0); 792 StartValue = Phi->getIncomingValue(1); 793 } else { 794 assert(TheLoop->contains(Phi->getIncomingBlock(1)) && 795 "Unexpected Phi node in the loop"); 796 BEValue = Phi->getIncomingValue(1); 797 StartValue = Phi->getIncomingValue(0); 798 } 799 800 BinaryOperator *BOp = dyn_cast<BinaryOperator>(BEValue); 801 if (!BOp) 802 return false; 803 804 Value *Addend = nullptr; 805 if (BOp->getOpcode() == Instruction::FAdd) { 806 if (BOp->getOperand(0) == Phi) 807 Addend = BOp->getOperand(1); 808 else if (BOp->getOperand(1) == Phi) 809 Addend = BOp->getOperand(0); 810 } else if (BOp->getOpcode() == Instruction::FSub) 811 if (BOp->getOperand(0) == Phi) 812 Addend = BOp->getOperand(1); 813 814 if (!Addend) 815 return false; 816 817 // The addend should be loop invariant 818 if (auto *I = dyn_cast<Instruction>(Addend)) 819 if (TheLoop->contains(I)) 820 return false; 821 822 // FP Step has unknown SCEV 823 const SCEV *Step = SE->getUnknown(Addend); 824 D = InductionDescriptor(StartValue, IK_FpInduction, Step, BOp); 825 return true; 826 } 827 828 bool InductionDescriptor::isInductionPHI(PHINode *Phi, const Loop *TheLoop, 829 PredicatedScalarEvolution &PSE, 830 InductionDescriptor &D, 831 bool Assume) { 832 Type *PhiTy = Phi->getType(); 833 834 // Handle integer and pointer inductions variables. 835 // Now we handle also FP induction but not trying to make a 836 // recurrent expression from the PHI node in-place. 837 838 if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy() && 839 !PhiTy->isFloatTy() && !PhiTy->isDoubleTy() && !PhiTy->isHalfTy()) 840 return false; 841 842 if (PhiTy->isFloatingPointTy()) 843 return isFPInductionPHI(Phi, TheLoop, PSE.getSE(), D); 844 845 const SCEV *PhiScev = PSE.getSCEV(Phi); 846 const auto *AR = dyn_cast<SCEVAddRecExpr>(PhiScev); 847 848 // We need this expression to be an AddRecExpr. 849 if (Assume && !AR) 850 AR = PSE.getAsAddRec(Phi); 851 852 if (!AR) { 853 DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n"); 854 return false; 855 } 856 857 return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, AR); 858 } 859 860 bool InductionDescriptor::isInductionPHI(PHINode *Phi, const Loop *TheLoop, 861 ScalarEvolution *SE, 862 InductionDescriptor &D, 863 const SCEV *Expr) { 864 Type *PhiTy = Phi->getType(); 865 // We only handle integer and pointer inductions variables. 866 if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy()) 867 return false; 868 869 // Check that the PHI is consecutive. 870 const SCEV *PhiScev = Expr ? Expr : SE->getSCEV(Phi); 871 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev); 872 873 if (!AR) { 874 DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n"); 875 return false; 876 } 877 878 if (AR->getLoop() != TheLoop) { 879 // FIXME: We should treat this as a uniform. Unfortunately, we 880 // don't currently know how to handled uniform PHIs. 881 DEBUG(dbgs() << "LV: PHI is a recurrence with respect to an outer loop.\n"); 882 return false; 883 } 884 885 Value *StartValue = 886 Phi->getIncomingValueForBlock(AR->getLoop()->getLoopPreheader()); 887 const SCEV *Step = AR->getStepRecurrence(*SE); 888 // Calculate the pointer stride and check if it is consecutive. 889 // The stride may be a constant or a loop invariant integer value. 890 const SCEVConstant *ConstStep = dyn_cast<SCEVConstant>(Step); 891 if (!ConstStep && !SE->isLoopInvariant(Step, TheLoop)) 892 return false; 893 894 if (PhiTy->isIntegerTy()) { 895 D = InductionDescriptor(StartValue, IK_IntInduction, Step); 896 return true; 897 } 898 899 assert(PhiTy->isPointerTy() && "The PHI must be a pointer"); 900 // Pointer induction should be a constant. 901 if (!ConstStep) 902 return false; 903 904 ConstantInt *CV = ConstStep->getValue(); 905 Type *PointerElementType = PhiTy->getPointerElementType(); 906 // The pointer stride cannot be determined if the pointer element type is not 907 // sized. 908 if (!PointerElementType->isSized()) 909 return false; 910 911 const DataLayout &DL = Phi->getModule()->getDataLayout(); 912 int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType)); 913 if (!Size) 914 return false; 915 916 int64_t CVSize = CV->getSExtValue(); 917 if (CVSize % Size) 918 return false; 919 auto *StepValue = SE->getConstant(CV->getType(), CVSize / Size, 920 true /* signed */); 921 D = InductionDescriptor(StartValue, IK_PtrInduction, StepValue); 922 return true; 923 } 924 925 /// \brief Returns the instructions that use values defined in the loop. 926 SmallVector<Instruction *, 8> llvm::findDefsUsedOutsideOfLoop(Loop *L) { 927 SmallVector<Instruction *, 8> UsedOutside; 928 929 for (auto *Block : L->getBlocks()) 930 // FIXME: I believe that this could use copy_if if the Inst reference could 931 // be adapted into a pointer. 932 for (auto &Inst : *Block) { 933 auto Users = Inst.users(); 934 if (any_of(Users, [&](User *U) { 935 auto *Use = cast<Instruction>(U); 936 return !L->contains(Use->getParent()); 937 })) 938 UsedOutside.push_back(&Inst); 939 } 940 941 return UsedOutside; 942 } 943 944 void llvm::getLoopAnalysisUsage(AnalysisUsage &AU) { 945 // By definition, all loop passes need the LoopInfo analysis and the 946 // Dominator tree it depends on. Because they all participate in the loop 947 // pass manager, they must also preserve these. 948 AU.addRequired<DominatorTreeWrapperPass>(); 949 AU.addPreserved<DominatorTreeWrapperPass>(); 950 AU.addRequired<LoopInfoWrapperPass>(); 951 AU.addPreserved<LoopInfoWrapperPass>(); 952 953 // We must also preserve LoopSimplify and LCSSA. We locally access their IDs 954 // here because users shouldn't directly get them from this header. 955 extern char &LoopSimplifyID; 956 extern char &LCSSAID; 957 AU.addRequiredID(LoopSimplifyID); 958 AU.addPreservedID(LoopSimplifyID); 959 AU.addRequiredID(LCSSAID); 960 AU.addPreservedID(LCSSAID); 961 // This is used in the LPPassManager to perform LCSSA verification on passes 962 // which preserve lcssa form 963 AU.addRequired<LCSSAVerificationPass>(); 964 AU.addPreserved<LCSSAVerificationPass>(); 965 966 // Loop passes are designed to run inside of a loop pass manager which means 967 // that any function analyses they require must be required by the first loop 968 // pass in the manager (so that it is computed before the loop pass manager 969 // runs) and preserved by all loop pasess in the manager. To make this 970 // reasonably robust, the set needed for most loop passes is maintained here. 971 // If your loop pass requires an analysis not listed here, you will need to 972 // carefully audit the loop pass manager nesting structure that results. 973 AU.addRequired<AAResultsWrapperPass>(); 974 AU.addPreserved<AAResultsWrapperPass>(); 975 AU.addPreserved<BasicAAWrapperPass>(); 976 AU.addPreserved<GlobalsAAWrapperPass>(); 977 AU.addPreserved<SCEVAAWrapperPass>(); 978 AU.addRequired<ScalarEvolutionWrapperPass>(); 979 AU.addPreserved<ScalarEvolutionWrapperPass>(); 980 } 981 982 /// Manually defined generic "LoopPass" dependency initialization. This is used 983 /// to initialize the exact set of passes from above in \c 984 /// getLoopAnalysisUsage. It can be used within a loop pass's initialization 985 /// with: 986 /// 987 /// INITIALIZE_PASS_DEPENDENCY(LoopPass) 988 /// 989 /// As-if "LoopPass" were a pass. 990 void llvm::initializeLoopPassPass(PassRegistry &Registry) { 991 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 992 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 993 INITIALIZE_PASS_DEPENDENCY(LoopSimplify) 994 INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass) 995 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 996 INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass) 997 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) 998 INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass) 999 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) 1000 } 1001 1002 /// \brief Find string metadata for loop 1003 /// 1004 /// If it has a value (e.g. {"llvm.distribute", 1} return the value as an 1005 /// operand or null otherwise. If the string metadata is not found return 1006 /// Optional's not-a-value. 1007 Optional<const MDOperand *> llvm::findStringMetadataForLoop(Loop *TheLoop, 1008 StringRef Name) { 1009 MDNode *LoopID = TheLoop->getLoopID(); 1010 // Return none if LoopID is false. 1011 if (!LoopID) 1012 return None; 1013 1014 // First operand should refer to the loop id itself. 1015 assert(LoopID->getNumOperands() > 0 && "requires at least one operand"); 1016 assert(LoopID->getOperand(0) == LoopID && "invalid loop id"); 1017 1018 // Iterate over LoopID operands and look for MDString Metadata 1019 for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) { 1020 MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); 1021 if (!MD) 1022 continue; 1023 MDString *S = dyn_cast<MDString>(MD->getOperand(0)); 1024 if (!S) 1025 continue; 1026 // Return true if MDString holds expected MetaData. 1027 if (Name.equals(S->getString())) 1028 switch (MD->getNumOperands()) { 1029 case 1: 1030 return nullptr; 1031 case 2: 1032 return &MD->getOperand(1); 1033 default: 1034 llvm_unreachable("loop metadata has 0 or 1 operand"); 1035 } 1036 } 1037 return None; 1038 } 1039 1040 /// Returns true if the instruction in a loop is guaranteed to execute at least 1041 /// once. 1042 bool llvm::isGuaranteedToExecute(const Instruction &Inst, 1043 const DominatorTree *DT, const Loop *CurLoop, 1044 const LoopSafetyInfo *SafetyInfo) { 1045 // We have to check to make sure that the instruction dominates all 1046 // of the exit blocks. If it doesn't, then there is a path out of the loop 1047 // which does not execute this instruction, so we can't hoist it. 1048 1049 // If the instruction is in the header block for the loop (which is very 1050 // common), it is always guaranteed to dominate the exit blocks. Since this 1051 // is a common case, and can save some work, check it now. 1052 if (Inst.getParent() == CurLoop->getHeader()) 1053 // If there's a throw in the header block, we can't guarantee we'll reach 1054 // Inst. 1055 return !SafetyInfo->HeaderMayThrow; 1056 1057 // Somewhere in this loop there is an instruction which may throw and make us 1058 // exit the loop. 1059 if (SafetyInfo->MayThrow) 1060 return false; 1061 1062 // Get the exit blocks for the current loop. 1063 SmallVector<BasicBlock *, 8> ExitBlocks; 1064 CurLoop->getExitBlocks(ExitBlocks); 1065 1066 // Verify that the block dominates each of the exit blocks of the loop. 1067 for (BasicBlock *ExitBlock : ExitBlocks) 1068 if (!DT->dominates(Inst.getParent(), ExitBlock)) 1069 return false; 1070 1071 // As a degenerate case, if the loop is statically infinite then we haven't 1072 // proven anything since there are no exit blocks. 1073 if (ExitBlocks.empty()) 1074 return false; 1075 1076 // FIXME: In general, we have to prove that the loop isn't an infinite loop. 1077 // See http::llvm.org/PR24078 . (The "ExitBlocks.empty()" check above is 1078 // just a special case of this.) 1079 return true; 1080 } 1081 1082 Optional<unsigned> llvm::getLoopEstimatedTripCount(Loop *L) { 1083 // Only support loops with a unique exiting block, and a latch. 1084 if (!L->getExitingBlock()) 1085 return None; 1086 1087 // Get the branch weights for the the loop's backedge. 1088 BranchInst *LatchBR = 1089 dyn_cast<BranchInst>(L->getLoopLatch()->getTerminator()); 1090 if (!LatchBR || LatchBR->getNumSuccessors() != 2) 1091 return None; 1092 1093 assert((LatchBR->getSuccessor(0) == L->getHeader() || 1094 LatchBR->getSuccessor(1) == L->getHeader()) && 1095 "At least one edge out of the latch must go to the header"); 1096 1097 // To estimate the number of times the loop body was executed, we want to 1098 // know the number of times the backedge was taken, vs. the number of times 1099 // we exited the loop. 1100 uint64_t TrueVal, FalseVal; 1101 if (!LatchBR->extractProfMetadata(TrueVal, FalseVal)) 1102 return None; 1103 1104 if (!TrueVal || !FalseVal) 1105 return 0; 1106 1107 // Divide the count of the backedge by the count of the edge exiting the loop, 1108 // rounding to nearest. 1109 if (LatchBR->getSuccessor(0) == L->getHeader()) 1110 return (TrueVal + (FalseVal / 2)) / FalseVal; 1111 else 1112 return (FalseVal + (TrueVal / 2)) / TrueVal; 1113 } 1114