LoopUtils.cpp 18 KB

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