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/Analysis/LoopInfo.h" 15 #include "llvm/IR/Instructions.h" 16 #include "llvm/IR/PatternMatch.h" 17 #include "llvm/IR/ValueHandle.h" 18 #include "llvm/Support/Debug.h" 19 #include "llvm/Analysis/ScalarEvolution.h" 20 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 21 #include "llvm/IR/Module.h" 22 #include "llvm/Transforms/Utils/LoopUtils.h" 23 24 using namespace llvm; 25 using namespace llvm::PatternMatch; 26 27 #define DEBUG_TYPE "loop-utils" 28 29 bool ReductionDescriptor::areAllUsesIn(Instruction *I, 30 SmallPtrSetImpl<Instruction *> &Set) { 31 for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; ++Use) 32 if (!Set.count(dyn_cast<Instruction>(*Use))) 33 return false; 34 return true; 35 } 36 37 bool ReductionDescriptor::AddReductionVar(PHINode *Phi, ReductionKind Kind, 38 Loop *TheLoop, bool HasFunNoNaNAttr, 39 ReductionDescriptor &RedDes) { 40 if (Phi->getNumIncomingValues() != 2) 41 return false; 42 43 // Reduction variables are only found in the loop header block. 44 if (Phi->getParent() != TheLoop->getHeader()) 45 return false; 46 47 // Obtain the reduction start value from the value that comes from the loop 48 // preheader. 49 Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader()); 50 51 // ExitInstruction is the single value which is used outside the loop. 52 // We only allow for a single reduction value to be used outside the loop. 53 // This includes users of the reduction, variables (which form a cycle 54 // which ends in the phi node). 55 Instruction *ExitInstruction = nullptr; 56 // Indicates that we found a reduction operation in our scan. 57 bool FoundReduxOp = false; 58 59 // We start with the PHI node and scan for all of the users of this 60 // instruction. All users must be instructions that can be used as reduction 61 // variables (such as ADD). We must have a single out-of-block user. The cycle 62 // must include the original PHI. 63 bool FoundStartPHI = false; 64 65 // To recognize min/max patterns formed by a icmp select sequence, we store 66 // the number of instruction we saw from the recognized min/max pattern, 67 // to make sure we only see exactly the two instructions. 68 unsigned NumCmpSelectPatternInst = 0; 69 ReductionInstDesc ReduxDesc(false, nullptr); 70 71 SmallPtrSet<Instruction *, 8> VisitedInsts; 72 SmallVector<Instruction *, 8> Worklist; 73 Worklist.push_back(Phi); 74 VisitedInsts.insert(Phi); 75 76 // A value in the reduction can be used: 77 // - By the reduction: 78 // - Reduction operation: 79 // - One use of reduction value (safe). 80 // - Multiple use of reduction value (not safe). 81 // - PHI: 82 // - All uses of the PHI must be the reduction (safe). 83 // - Otherwise, not safe. 84 // - By one instruction outside of the loop (safe). 85 // - By further instructions outside of the loop (not safe). 86 // - By an instruction that is not part of the reduction (not safe). 87 // This is either: 88 // * An instruction type other than PHI or the reduction operation. 89 // * A PHI in the header other than the initial PHI. 90 while (!Worklist.empty()) { 91 Instruction *Cur = Worklist.back(); 92 Worklist.pop_back(); 93 94 // No Users. 95 // If the instruction has no users then this is a broken chain and can't be 96 // a reduction variable. 97 if (Cur->use_empty()) 98 return false; 99 100 bool IsAPhi = isa<PHINode>(Cur); 101 102 // A header PHI use other than the original PHI. 103 if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent()) 104 return false; 105 106 // Reductions of instructions such as Div, and Sub is only possible if the 107 // LHS is the reduction variable. 108 if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) && 109 !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) && 110 !VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0)))) 111 return false; 112 113 // Any reduction instruction must be of one of the allowed kinds. 114 ReduxDesc = isReductionInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr); 115 if (!ReduxDesc.isReduction()) 116 return false; 117 118 // A reduction operation must only have one use of the reduction value. 119 if (!IsAPhi && Kind != RK_IntegerMinMax && Kind != RK_FloatMinMax && 120 hasMultipleUsesOf(Cur, VisitedInsts)) 121 return false; 122 123 // All inputs to a PHI node must be a reduction value. 124 if (IsAPhi && Cur != Phi && !areAllUsesIn(Cur, VisitedInsts)) 125 return false; 126 127 if (Kind == RK_IntegerMinMax && 128 (isa<ICmpInst>(Cur) || isa<SelectInst>(Cur))) 129 ++NumCmpSelectPatternInst; 130 if (Kind == RK_FloatMinMax && (isa<FCmpInst>(Cur) || isa<SelectInst>(Cur))) 131 ++NumCmpSelectPatternInst; 132 133 // Check whether we found a reduction operator. 134 FoundReduxOp |= !IsAPhi; 135 136 // Process users of current instruction. Push non-PHI nodes after PHI nodes 137 // onto the stack. This way we are going to have seen all inputs to PHI 138 // nodes once we get to them. 139 SmallVector<Instruction *, 8> NonPHIs; 140 SmallVector<Instruction *, 8> PHIs; 141 for (User *U : Cur->users()) { 142 Instruction *UI = cast<Instruction>(U); 143 144 // Check if we found the exit user. 145 BasicBlock *Parent = UI->getParent(); 146 if (!TheLoop->contains(Parent)) { 147 // Exit if you find multiple outside users or if the header phi node is 148 // being used. In this case the user uses the value of the previous 149 // iteration, in which case we would loose "VF-1" iterations of the 150 // reduction operation if we vectorize. 151 if (ExitInstruction != nullptr || Cur == Phi) 152 return false; 153 154 // The instruction used by an outside user must be the last instruction 155 // before we feed back to the reduction phi. Otherwise, we loose VF-1 156 // operations on the value. 157 if (std::find(Phi->op_begin(), Phi->op_end(), Cur) == Phi->op_end()) 158 return false; 159 160 ExitInstruction = Cur; 161 continue; 162 } 163 164 // Process instructions only once (termination). Each reduction cycle 165 // value must only be used once, except by phi nodes and min/max 166 // reductions which are represented as a cmp followed by a select. 167 ReductionInstDesc IgnoredVal(false, nullptr); 168 if (VisitedInsts.insert(UI).second) { 169 if (isa<PHINode>(UI)) 170 PHIs.push_back(UI); 171 else 172 NonPHIs.push_back(UI); 173 } else if (!isa<PHINode>(UI) && 174 ((!isa<FCmpInst>(UI) && !isa<ICmpInst>(UI) && 175 !isa<SelectInst>(UI)) || 176 !isMinMaxSelectCmpPattern(UI, IgnoredVal).isReduction())) 177 return false; 178 179 // Remember that we completed the cycle. 180 if (UI == Phi) 181 FoundStartPHI = true; 182 } 183 Worklist.append(PHIs.begin(), PHIs.end()); 184 Worklist.append(NonPHIs.begin(), NonPHIs.end()); 185 } 186 187 // This means we have seen one but not the other instruction of the 188 // pattern or more than just a select and cmp. 189 if ((Kind == RK_IntegerMinMax || Kind == RK_FloatMinMax) && 190 NumCmpSelectPatternInst != 2) 191 return false; 192 193 if (!FoundStartPHI || !FoundReduxOp || !ExitInstruction) 194 return false; 195 196 // We found a reduction var if we have reached the original phi node and we 197 // only have a single instruction with out-of-loop users. 198 199 // The ExitInstruction(Instruction which is allowed to have out-of-loop users) 200 // is saved as part of the ReductionDescriptor. 201 202 // Save the description of this reduction variable. 203 ReductionDescriptor RD(RdxStart, ExitInstruction, Kind, 204 ReduxDesc.getMinMaxKind()); 205 206 RedDes = RD; 207 208 return true; 209 } 210 211 /// Returns true if the instruction is a Select(ICmp(X, Y), X, Y) instruction 212 /// pattern corresponding to a min(X, Y) or max(X, Y). 213 ReductionInstDesc 214 ReductionDescriptor::isMinMaxSelectCmpPattern(Instruction *I, 215 ReductionInstDesc &Prev) { 216 217 assert((isa<ICmpInst>(I) || isa<FCmpInst>(I) || isa<SelectInst>(I)) && 218 "Expect a select instruction"); 219 Instruction *Cmp = nullptr; 220 SelectInst *Select = nullptr; 221 222 // We must handle the select(cmp()) as a single instruction. Advance to the 223 // select. 224 if ((Cmp = dyn_cast<ICmpInst>(I)) || (Cmp = dyn_cast<FCmpInst>(I))) { 225 if (!Cmp->hasOneUse() || !(Select = dyn_cast<SelectInst>(*I->user_begin()))) 226 return ReductionInstDesc(false, I); 227 return ReductionInstDesc(Select, Prev.getMinMaxKind()); 228 } 229 230 // Only handle single use cases for now. 231 if (!(Select = dyn_cast<SelectInst>(I))) 232 return ReductionInstDesc(false, I); 233 if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) && 234 !(Cmp = dyn_cast<FCmpInst>(I->getOperand(0)))) 235 return ReductionInstDesc(false, I); 236 if (!Cmp->hasOneUse()) 237 return ReductionInstDesc(false, I); 238 239 Value *CmpLeft; 240 Value *CmpRight; 241 242 // Look for a min/max pattern. 243 if (m_UMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 244 return ReductionInstDesc(Select, ReductionInstDesc::MRK_UIntMin); 245 else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 246 return ReductionInstDesc(Select, ReductionInstDesc::MRK_UIntMax); 247 else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 248 return ReductionInstDesc(Select, ReductionInstDesc::MRK_SIntMax); 249 else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 250 return ReductionInstDesc(Select, ReductionInstDesc::MRK_SIntMin); 251 else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 252 return ReductionInstDesc(Select, ReductionInstDesc::MRK_FloatMin); 253 else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 254 return ReductionInstDesc(Select, ReductionInstDesc::MRK_FloatMax); 255 else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 256 return ReductionInstDesc(Select, ReductionInstDesc::MRK_FloatMin); 257 else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select)) 258 return ReductionInstDesc(Select, ReductionInstDesc::MRK_FloatMax); 259 260 return ReductionInstDesc(false, I); 261 } 262 263 ReductionInstDesc ReductionDescriptor::isReductionInstr(Instruction *I, 264 ReductionKind Kind, 265 ReductionInstDesc &Prev, 266 bool HasFunNoNaNAttr) { 267 bool FP = I->getType()->isFloatingPointTy(); 268 bool FastMath = FP && I->hasUnsafeAlgebra(); 269 switch (I->getOpcode()) { 270 default: 271 return ReductionInstDesc(false, I); 272 case Instruction::PHI: 273 if (FP && 274 (Kind != RK_FloatMult && Kind != RK_FloatAdd && Kind != RK_FloatMinMax)) 275 return ReductionInstDesc(false, I); 276 return ReductionInstDesc(I, Prev.getMinMaxKind()); 277 case Instruction::Sub: 278 case Instruction::Add: 279 return ReductionInstDesc(Kind == RK_IntegerAdd, I); 280 case Instruction::Mul: 281 return ReductionInstDesc(Kind == RK_IntegerMult, I); 282 case Instruction::And: 283 return ReductionInstDesc(Kind == RK_IntegerAnd, I); 284 case Instruction::Or: 285 return ReductionInstDesc(Kind == RK_IntegerOr, I); 286 case Instruction::Xor: 287 return ReductionInstDesc(Kind == RK_IntegerXor, I); 288 case Instruction::FMul: 289 return ReductionInstDesc(Kind == RK_FloatMult && FastMath, I); 290 case Instruction::FSub: 291 case Instruction::FAdd: 292 return ReductionInstDesc(Kind == RK_FloatAdd && FastMath, I); 293 case Instruction::FCmp: 294 case Instruction::ICmp: 295 case Instruction::Select: 296 if (Kind != RK_IntegerMinMax && 297 (!HasFunNoNaNAttr || Kind != RK_FloatMinMax)) 298 return ReductionInstDesc(false, I); 299 return isMinMaxSelectCmpPattern(I, Prev); 300 } 301 } 302 303 bool ReductionDescriptor::hasMultipleUsesOf( 304 Instruction *I, SmallPtrSetImpl<Instruction *> &Insts) { 305 unsigned NumUses = 0; 306 for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; 307 ++Use) { 308 if (Insts.count(dyn_cast<Instruction>(*Use))) 309 ++NumUses; 310 if (NumUses > 1) 311 return true; 312 } 313 314 return false; 315 } 316 bool ReductionDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop, 317 ReductionDescriptor &RedDes) { 318 319 bool HasFunNoNaNAttr = false; 320 BasicBlock *Header = TheLoop->getHeader(); 321 Function &F = *Header->getParent(); 322 if (F.hasFnAttribute("no-nans-fp-math")) 323 HasFunNoNaNAttr = 324 F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true"; 325 326 if (AddReductionVar(Phi, RK_IntegerAdd, TheLoop, HasFunNoNaNAttr, RedDes)) { 327 DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n"); 328 return true; 329 } 330 if (AddReductionVar(Phi, RK_IntegerMult, TheLoop, HasFunNoNaNAttr, RedDes)) { 331 DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n"); 332 return true; 333 } 334 if (AddReductionVar(Phi, RK_IntegerOr, TheLoop, HasFunNoNaNAttr, RedDes)) { 335 DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n"); 336 return true; 337 } 338 if (AddReductionVar(Phi, RK_IntegerAnd, TheLoop, HasFunNoNaNAttr, RedDes)) { 339 DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n"); 340 return true; 341 } 342 if (AddReductionVar(Phi, RK_IntegerXor, TheLoop, HasFunNoNaNAttr, RedDes)) { 343 DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n"); 344 return true; 345 } 346 if (AddReductionVar(Phi, RK_IntegerMinMax, TheLoop, HasFunNoNaNAttr, 347 RedDes)) { 348 DEBUG(dbgs() << "Found a MINMAX reduction PHI." << *Phi << "\n"); 349 return true; 350 } 351 if (AddReductionVar(Phi, RK_FloatMult, TheLoop, HasFunNoNaNAttr, RedDes)) { 352 DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n"); 353 return true; 354 } 355 if (AddReductionVar(Phi, RK_FloatAdd, TheLoop, HasFunNoNaNAttr, RedDes)) { 356 DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n"); 357 return true; 358 } 359 if (AddReductionVar(Phi, RK_FloatMinMax, TheLoop, HasFunNoNaNAttr, RedDes)) { 360 DEBUG(dbgs() << "Found an float MINMAX reduction PHI." << *Phi << "\n"); 361 return true; 362 } 363 // Not a reduction of known type. 364 return false; 365 } 366 367 /// This function returns the identity element (or neutral element) for 368 /// the operation K. 369 Constant *ReductionDescriptor::getReductionIdentity(ReductionKind K, Type *Tp) { 370 switch (K) { 371 case RK_IntegerXor: 372 case RK_IntegerAdd: 373 case RK_IntegerOr: 374 // Adding, Xoring, Oring zero to a number does not change it. 375 return ConstantInt::get(Tp, 0); 376 case RK_IntegerMult: 377 // Multiplying a number by 1 does not change it. 378 return ConstantInt::get(Tp, 1); 379 case RK_IntegerAnd: 380 // AND-ing a number with an all-1 value does not change it. 381 return ConstantInt::get(Tp, -1, true); 382 case RK_FloatMult: 383 // Multiplying a number by 1 does not change it. 384 return ConstantFP::get(Tp, 1.0L); 385 case RK_FloatAdd: 386 // Adding zero to a number does not change it. 387 return ConstantFP::get(Tp, 0.0L); 388 default: 389 llvm_unreachable("Unknown reduction kind"); 390 } 391 } 392 393 /// This function translates the reduction kind to an LLVM binary operator. 394 unsigned ReductionDescriptor::getReductionBinOp(ReductionKind Kind) { 395 switch (Kind) { 396 case RK_IntegerAdd: 397 return Instruction::Add; 398 case RK_IntegerMult: 399 return Instruction::Mul; 400 case RK_IntegerOr: 401 return Instruction::Or; 402 case RK_IntegerAnd: 403 return Instruction::And; 404 case RK_IntegerXor: 405 return Instruction::Xor; 406 case RK_FloatMult: 407 return Instruction::FMul; 408 case RK_FloatAdd: 409 return Instruction::FAdd; 410 case RK_IntegerMinMax: 411 return Instruction::ICmp; 412 case RK_FloatMinMax: 413 return Instruction::FCmp; 414 default: 415 llvm_unreachable("Unknown reduction operation"); 416 } 417 } 418 419 Value * 420 ReductionDescriptor::createMinMaxOp(IRBuilder<> &Builder, 421 ReductionInstDesc::MinMaxReductionKind RK, 422 Value *Left, Value *Right) { 423 CmpInst::Predicate P = CmpInst::ICMP_NE; 424 switch (RK) { 425 default: 426 llvm_unreachable("Unknown min/max reduction kind"); 427 case ReductionInstDesc::MRK_UIntMin: 428 P = CmpInst::ICMP_ULT; 429 break; 430 case ReductionInstDesc::MRK_UIntMax: 431 P = CmpInst::ICMP_UGT; 432 break; 433 case ReductionInstDesc::MRK_SIntMin: 434 P = CmpInst::ICMP_SLT; 435 break; 436 case ReductionInstDesc::MRK_SIntMax: 437 P = CmpInst::ICMP_SGT; 438 break; 439 case ReductionInstDesc::MRK_FloatMin: 440 P = CmpInst::FCMP_OLT; 441 break; 442 case ReductionInstDesc::MRK_FloatMax: 443 P = CmpInst::FCMP_OGT; 444 break; 445 } 446 447 Value *Cmp; 448 if (RK == ReductionInstDesc::MRK_FloatMin || 449 RK == ReductionInstDesc::MRK_FloatMax) 450 Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp"); 451 else 452 Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp"); 453 454 Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select"); 455 return Select; 456 } 457 458 bool llvm::isInductionPHI(PHINode *Phi, ScalarEvolution *SE, 459 ConstantInt *&StepValue) { 460 Type *PhiTy = Phi->getType(); 461 // We only handle integer and pointer inductions variables. 462 if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy()) 463 return false; 464 465 // Check that the PHI is consecutive. 466 const SCEV *PhiScev = SE->getSCEV(Phi); 467 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev); 468 if (!AR) { 469 DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n"); 470 return false; 471 } 472 473 const SCEV *Step = AR->getStepRecurrence(*SE); 474 // Calculate the pointer stride and check if it is consecutive. 475 const SCEVConstant *C = dyn_cast<SCEVConstant>(Step); 476 if (!C) 477 return false; 478 479 ConstantInt *CV = C->getValue(); 480 if (PhiTy->isIntegerTy()) { 481 StepValue = CV; 482 return true; 483 } 484 485 assert(PhiTy->isPointerTy() && "The PHI must be a pointer"); 486 Type *PointerElementType = PhiTy->getPointerElementType(); 487 // The pointer stride cannot be determined if the pointer element type is not 488 // sized. 489 if (!PointerElementType->isSized()) 490 return false; 491 492 const DataLayout &DL = Phi->getModule()->getDataLayout(); 493 int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType)); 494 int64_t CVSize = CV->getSExtValue(); 495 if (CVSize % Size) 496 return false; 497 StepValue = ConstantInt::getSigned(CV->getType(), CVSize / Size); 498 return true; 499 } 500