LoopUtils.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. RecurrenceInstDesc 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. RecurrenceInstDesc 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. RecurrenceInstDesc
  179. RecurrenceDescriptor::isMinMaxSelectCmpPattern(Instruction *I,
  180. RecurrenceInstDesc &Prev) {
  181. assert((isa<ICmpInst>(I) || isa<FCmpInst>(I) || isa<SelectInst>(I)) &&
  182. "Expect a select instruction");
  183. Instruction *Cmp = nullptr;
  184. SelectInst *Select = nullptr;
  185. // We must handle the select(cmp()) as a single instruction. Advance to the
  186. // select.
  187. if ((Cmp = dyn_cast<ICmpInst>(I)) || (Cmp = dyn_cast<FCmpInst>(I))) {
  188. if (!Cmp->hasOneUse() || !(Select = dyn_cast<SelectInst>(*I->user_begin())))
  189. return RecurrenceInstDesc(false, I);
  190. return RecurrenceInstDesc(Select, Prev.getMinMaxKind());
  191. }
  192. // Only handle single use cases for now.
  193. if (!(Select = dyn_cast<SelectInst>(I)))
  194. return RecurrenceInstDesc(false, I);
  195. if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) &&
  196. !(Cmp = dyn_cast<FCmpInst>(I->getOperand(0))))
  197. return RecurrenceInstDesc(false, I);
  198. if (!Cmp->hasOneUse())
  199. return RecurrenceInstDesc(false, I);
  200. Value *CmpLeft;
  201. Value *CmpRight;
  202. // Look for a min/max pattern.
  203. if (m_UMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
  204. return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_UIntMin);
  205. else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
  206. return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_UIntMax);
  207. else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
  208. return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_SIntMax);
  209. else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
  210. return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_SIntMin);
  211. else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
  212. return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_FloatMin);
  213. else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
  214. return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_FloatMax);
  215. else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
  216. return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_FloatMin);
  217. else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
  218. return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_FloatMax);
  219. return RecurrenceInstDesc(false, I);
  220. }
  221. RecurrenceInstDesc
  222. RecurrenceDescriptor::isRecurrenceInstr(Instruction *I, RecurrenceKind Kind,
  223. RecurrenceInstDesc &Prev,
  224. bool HasFunNoNaNAttr) {
  225. bool FP = I->getType()->isFloatingPointTy();
  226. bool FastMath = FP && I->hasUnsafeAlgebra();
  227. switch (I->getOpcode()) {
  228. default:
  229. return RecurrenceInstDesc(false, I);
  230. case Instruction::PHI:
  231. if (FP &&
  232. (Kind != RK_FloatMult && Kind != RK_FloatAdd && Kind != RK_FloatMinMax))
  233. return RecurrenceInstDesc(false, I);
  234. return RecurrenceInstDesc(I, Prev.getMinMaxKind());
  235. case Instruction::Sub:
  236. case Instruction::Add:
  237. return RecurrenceInstDesc(Kind == RK_IntegerAdd, I);
  238. case Instruction::Mul:
  239. return RecurrenceInstDesc(Kind == RK_IntegerMult, I);
  240. case Instruction::And:
  241. return RecurrenceInstDesc(Kind == RK_IntegerAnd, I);
  242. case Instruction::Or:
  243. return RecurrenceInstDesc(Kind == RK_IntegerOr, I);
  244. case Instruction::Xor:
  245. return RecurrenceInstDesc(Kind == RK_IntegerXor, I);
  246. case Instruction::FMul:
  247. return RecurrenceInstDesc(Kind == RK_FloatMult && FastMath, I);
  248. case Instruction::FSub:
  249. case Instruction::FAdd:
  250. return RecurrenceInstDesc(Kind == RK_FloatAdd && FastMath, I);
  251. case Instruction::FCmp:
  252. case Instruction::ICmp:
  253. case Instruction::Select:
  254. if (Kind != RK_IntegerMinMax &&
  255. (!HasFunNoNaNAttr || Kind != RK_FloatMinMax))
  256. return RecurrenceInstDesc(false, I);
  257. return isMinMaxSelectCmpPattern(I, Prev);
  258. }
  259. }
  260. bool RecurrenceDescriptor::hasMultipleUsesOf(
  261. Instruction *I, SmallPtrSetImpl<Instruction *> &Insts) {
  262. unsigned NumUses = 0;
  263. for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E;
  264. ++Use) {
  265. if (Insts.count(dyn_cast<Instruction>(*Use)))
  266. ++NumUses;
  267. if (NumUses > 1)
  268. return true;
  269. }
  270. return false;
  271. }
  272. bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
  273. RecurrenceDescriptor &RedDes) {
  274. bool HasFunNoNaNAttr = false;
  275. BasicBlock *Header = TheLoop->getHeader();
  276. Function &F = *Header->getParent();
  277. if (F.hasFnAttribute("no-nans-fp-math"))
  278. HasFunNoNaNAttr =
  279. F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
  280. if (AddReductionVar(Phi, RK_IntegerAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
  281. DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n");
  282. return true;
  283. }
  284. if (AddReductionVar(Phi, RK_IntegerMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
  285. DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n");
  286. return true;
  287. }
  288. if (AddReductionVar(Phi, RK_IntegerOr, TheLoop, HasFunNoNaNAttr, RedDes)) {
  289. DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n");
  290. return true;
  291. }
  292. if (AddReductionVar(Phi, RK_IntegerAnd, TheLoop, HasFunNoNaNAttr, RedDes)) {
  293. DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n");
  294. return true;
  295. }
  296. if (AddReductionVar(Phi, RK_IntegerXor, TheLoop, HasFunNoNaNAttr, RedDes)) {
  297. DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n");
  298. return true;
  299. }
  300. if (AddReductionVar(Phi, RK_IntegerMinMax, TheLoop, HasFunNoNaNAttr,
  301. RedDes)) {
  302. DEBUG(dbgs() << "Found a MINMAX reduction PHI." << *Phi << "\n");
  303. return true;
  304. }
  305. if (AddReductionVar(Phi, RK_FloatMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
  306. DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n");
  307. return true;
  308. }
  309. if (AddReductionVar(Phi, RK_FloatAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
  310. DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n");
  311. return true;
  312. }
  313. if (AddReductionVar(Phi, RK_FloatMinMax, TheLoop, HasFunNoNaNAttr, RedDes)) {
  314. DEBUG(dbgs() << "Found an float MINMAX reduction PHI." << *Phi << "\n");
  315. return true;
  316. }
  317. // Not a reduction of known type.
  318. return false;
  319. }
  320. /// This function returns the identity element (or neutral element) for
  321. /// the operation K.
  322. Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurrenceKind K,
  323. Type *Tp) {
  324. switch (K) {
  325. case RK_IntegerXor:
  326. case RK_IntegerAdd:
  327. case RK_IntegerOr:
  328. // Adding, Xoring, Oring zero to a number does not change it.
  329. return ConstantInt::get(Tp, 0);
  330. case RK_IntegerMult:
  331. // Multiplying a number by 1 does not change it.
  332. return ConstantInt::get(Tp, 1);
  333. case RK_IntegerAnd:
  334. // AND-ing a number with an all-1 value does not change it.
  335. return ConstantInt::get(Tp, -1, true);
  336. case RK_FloatMult:
  337. // Multiplying a number by 1 does not change it.
  338. return ConstantFP::get(Tp, 1.0L);
  339. case RK_FloatAdd:
  340. // Adding zero to a number does not change it.
  341. return ConstantFP::get(Tp, 0.0L);
  342. default:
  343. llvm_unreachable("Unknown recurrence kind");
  344. }
  345. }
  346. /// This function translates the recurrence kind to an LLVM binary operator.
  347. unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurrenceKind Kind) {
  348. switch (Kind) {
  349. case RK_IntegerAdd:
  350. return Instruction::Add;
  351. case RK_IntegerMult:
  352. return Instruction::Mul;
  353. case RK_IntegerOr:
  354. return Instruction::Or;
  355. case RK_IntegerAnd:
  356. return Instruction::And;
  357. case RK_IntegerXor:
  358. return Instruction::Xor;
  359. case RK_FloatMult:
  360. return Instruction::FMul;
  361. case RK_FloatAdd:
  362. return Instruction::FAdd;
  363. case RK_IntegerMinMax:
  364. return Instruction::ICmp;
  365. case RK_FloatMinMax:
  366. return Instruction::FCmp;
  367. default:
  368. llvm_unreachable("Unknown recurrence operation");
  369. }
  370. }
  371. Value *RecurrenceDescriptor::createMinMaxOp(
  372. IRBuilder<> &Builder, RecurrenceInstDesc::MinMaxRecurrenceKind RK,
  373. Value *Left, Value *Right) {
  374. CmpInst::Predicate P = CmpInst::ICMP_NE;
  375. switch (RK) {
  376. default:
  377. llvm_unreachable("Unknown min/max recurrence kind");
  378. case RecurrenceInstDesc::MRK_UIntMin:
  379. P = CmpInst::ICMP_ULT;
  380. break;
  381. case RecurrenceInstDesc::MRK_UIntMax:
  382. P = CmpInst::ICMP_UGT;
  383. break;
  384. case RecurrenceInstDesc::MRK_SIntMin:
  385. P = CmpInst::ICMP_SLT;
  386. break;
  387. case RecurrenceInstDesc::MRK_SIntMax:
  388. P = CmpInst::ICMP_SGT;
  389. break;
  390. case RecurrenceInstDesc::MRK_FloatMin:
  391. P = CmpInst::FCMP_OLT;
  392. break;
  393. case RecurrenceInstDesc::MRK_FloatMax:
  394. P = CmpInst::FCMP_OGT;
  395. break;
  396. }
  397. Value *Cmp;
  398. if (RK == RecurrenceInstDesc::MRK_FloatMin ||
  399. RK == RecurrenceInstDesc::MRK_FloatMax)
  400. Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp");
  401. else
  402. Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp");
  403. Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
  404. return Select;
  405. }
  406. bool llvm::isInductionPHI(PHINode *Phi, ScalarEvolution *SE,
  407. ConstantInt *&StepValue) {
  408. Type *PhiTy = Phi->getType();
  409. // We only handle integer and pointer inductions variables.
  410. if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy())
  411. return false;
  412. // Check that the PHI is consecutive.
  413. const SCEV *PhiScev = SE->getSCEV(Phi);
  414. const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
  415. if (!AR) {
  416. DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
  417. return false;
  418. }
  419. const SCEV *Step = AR->getStepRecurrence(*SE);
  420. // Calculate the pointer stride and check if it is consecutive.
  421. const SCEVConstant *C = dyn_cast<SCEVConstant>(Step);
  422. if (!C)
  423. return false;
  424. ConstantInt *CV = C->getValue();
  425. if (PhiTy->isIntegerTy()) {
  426. StepValue = CV;
  427. return true;
  428. }
  429. assert(PhiTy->isPointerTy() && "The PHI must be a pointer");
  430. Type *PointerElementType = PhiTy->getPointerElementType();
  431. // The pointer stride cannot be determined if the pointer element type is not
  432. // sized.
  433. if (!PointerElementType->isSized())
  434. return false;
  435. const DataLayout &DL = Phi->getModule()->getDataLayout();
  436. int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType));
  437. if (!Size)
  438. return false;
  439. int64_t CVSize = CV->getSExtValue();
  440. if (CVSize % Size)
  441. return false;
  442. StepValue = ConstantInt::getSigned(CV->getType(), CVSize / Size);
  443. return true;
  444. }