LoopUnrollAnalyzer.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //===- LoopUnrollAnalyzer.cpp - Unrolling Effect Estimation -----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements UnrolledInstAnalyzer class. It's used for predicting
  10. // potential effects that loop unrolling might have, such as enabling constant
  11. // propagation and other optimizations.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Analysis/LoopUnrollAnalyzer.h"
  15. #include "llvm/Analysis/InstructionSimplify.h"
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  18. #include "llvm/IR/Operator.h"
  19. using namespace llvm;
  20. /// Try to simplify instruction \param I using its SCEV expression.
  21. ///
  22. /// The idea is that some AddRec expressions become constants, which then
  23. /// could trigger folding of other instructions. However, that only happens
  24. /// for expressions whose start value is also constant, which isn't always the
  25. /// case. In another common and important case the start value is just some
  26. /// address (i.e. SCEVUnknown) - in this case we compute the offset and save
  27. /// it along with the base address instead.
  28. bool UnrolledInstAnalyzer::simplifyInstWithSCEV(Instruction *I) {
  29. if (!SE.isSCEVable(I->getType()))
  30. return false;
  31. const SCEV *S = SE.getSCEV(I);
  32. if (auto *SC = dyn_cast<SCEVConstant>(S)) {
  33. SimplifiedValues[I] = SC->getValue();
  34. return true;
  35. }
  36. // If we have a loop invariant computation, we only need to compute it once.
  37. // Given that, all but the first occurance are free.
  38. if (!IterationNumber->isZero() && SE.isLoopInvariant(S, L))
  39. return true;
  40. auto *AR = dyn_cast<SCEVAddRecExpr>(S);
  41. if (!AR || AR->getLoop() != L)
  42. return false;
  43. const SCEV *ValueAtIteration = AR->evaluateAtIteration(IterationNumber, SE);
  44. // Check if the AddRec expression becomes a constant.
  45. if (auto *SC = dyn_cast<SCEVConstant>(ValueAtIteration)) {
  46. SimplifiedValues[I] = SC->getValue();
  47. return true;
  48. }
  49. // Check if the offset from the base address becomes a constant.
  50. auto *Base = dyn_cast<SCEVUnknown>(SE.getPointerBase(S));
  51. if (!Base)
  52. return false;
  53. auto *Offset =
  54. dyn_cast<SCEVConstant>(SE.getMinusSCEV(ValueAtIteration, Base));
  55. if (!Offset)
  56. return false;
  57. SimplifiedAddress Address;
  58. Address.Base = Base->getValue();
  59. Address.Offset = Offset->getValue();
  60. SimplifiedAddresses[I] = Address;
  61. return false;
  62. }
  63. /// Try to simplify binary operator I.
  64. ///
  65. /// TODO: Probably it's worth to hoist the code for estimating the
  66. /// simplifications effects to a separate class, since we have a very similar
  67. /// code in InlineCost already.
  68. bool UnrolledInstAnalyzer::visitBinaryOperator(BinaryOperator &I) {
  69. Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
  70. if (!isa<Constant>(LHS))
  71. if (Value *SimpleLHS = SimplifiedValues.lookup(LHS))
  72. LHS = SimpleLHS;
  73. if (!isa<Constant>(RHS))
  74. if (Value *SimpleRHS = SimplifiedValues.lookup(RHS))
  75. RHS = SimpleRHS;
  76. Value *SimpleV = nullptr;
  77. const DataLayout &DL = I.getModule()->getDataLayout();
  78. if (auto FI = dyn_cast<FPMathOperator>(&I))
  79. SimpleV =
  80. simplifyBinOp(I.getOpcode(), LHS, RHS, FI->getFastMathFlags(), DL);
  81. else
  82. SimpleV = simplifyBinOp(I.getOpcode(), LHS, RHS, DL);
  83. if (SimpleV) {
  84. SimplifiedValues[&I] = SimpleV;
  85. return true;
  86. }
  87. return Base::visitBinaryOperator(I);
  88. }
  89. /// Try to fold load I.
  90. bool UnrolledInstAnalyzer::visitLoad(LoadInst &I) {
  91. Value *AddrOp = I.getPointerOperand();
  92. auto AddressIt = SimplifiedAddresses.find(AddrOp);
  93. if (AddressIt == SimplifiedAddresses.end())
  94. return false;
  95. ConstantInt *SimplifiedAddrOp = AddressIt->second.Offset;
  96. auto *GV = dyn_cast<GlobalVariable>(AddressIt->second.Base);
  97. // We're only interested in loads that can be completely folded to a
  98. // constant.
  99. if (!GV || !GV->hasDefinitiveInitializer() || !GV->isConstant())
  100. return false;
  101. ConstantDataSequential *CDS =
  102. dyn_cast<ConstantDataSequential>(GV->getInitializer());
  103. if (!CDS)
  104. return false;
  105. // We might have a vector load from an array. FIXME: for now we just bail
  106. // out in this case, but we should be able to resolve and simplify such
  107. // loads.
  108. if (CDS->getElementType() != I.getType())
  109. return false;
  110. unsigned ElemSize = CDS->getElementType()->getPrimitiveSizeInBits() / 8U;
  111. if (SimplifiedAddrOp->getValue().getActiveBits() > 64)
  112. return false;
  113. int64_t SimplifiedAddrOpV = SimplifiedAddrOp->getSExtValue();
  114. if (SimplifiedAddrOpV < 0) {
  115. // FIXME: For now we conservatively ignore out of bound accesses, but
  116. // we're allowed to perform the optimization in this case.
  117. return false;
  118. }
  119. uint64_t Index = static_cast<uint64_t>(SimplifiedAddrOpV) / ElemSize;
  120. if (Index >= CDS->getNumElements()) {
  121. // FIXME: For now we conservatively ignore out of bound accesses, but
  122. // we're allowed to perform the optimization in this case.
  123. return false;
  124. }
  125. Constant *CV = CDS->getElementAsConstant(Index);
  126. assert(CV && "Constant expected.");
  127. SimplifiedValues[&I] = CV;
  128. return true;
  129. }
  130. /// Try to simplify cast instruction.
  131. bool UnrolledInstAnalyzer::visitCastInst(CastInst &I) {
  132. Value *Op = I.getOperand(0);
  133. if (Value *Simplified = SimplifiedValues.lookup(Op))
  134. Op = Simplified;
  135. // The cast can be invalid, because SimplifiedValues contains results of SCEV
  136. // analysis, which operates on integers (and, e.g., might convert i8* null to
  137. // i32 0).
  138. if (CastInst::castIsValid(I.getOpcode(), Op, I.getType())) {
  139. const DataLayout &DL = I.getModule()->getDataLayout();
  140. if (Value *V = simplifyCastInst(I.getOpcode(), Op, I.getType(), DL)) {
  141. SimplifiedValues[&I] = V;
  142. return true;
  143. }
  144. }
  145. return Base::visitCastInst(I);
  146. }
  147. /// Try to simplify cmp instruction.
  148. bool UnrolledInstAnalyzer::visitCmpInst(CmpInst &I) {
  149. Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
  150. // First try to handle simplified comparisons.
  151. if (!isa<Constant>(LHS))
  152. if (Value *SimpleLHS = SimplifiedValues.lookup(LHS))
  153. LHS = SimpleLHS;
  154. if (!isa<Constant>(RHS))
  155. if (Value *SimpleRHS = SimplifiedValues.lookup(RHS))
  156. RHS = SimpleRHS;
  157. if (!isa<Constant>(LHS) && !isa<Constant>(RHS)) {
  158. auto SimplifiedLHS = SimplifiedAddresses.find(LHS);
  159. if (SimplifiedLHS != SimplifiedAddresses.end()) {
  160. auto SimplifiedRHS = SimplifiedAddresses.find(RHS);
  161. if (SimplifiedRHS != SimplifiedAddresses.end()) {
  162. SimplifiedAddress &LHSAddr = SimplifiedLHS->second;
  163. SimplifiedAddress &RHSAddr = SimplifiedRHS->second;
  164. if (LHSAddr.Base == RHSAddr.Base) {
  165. LHS = LHSAddr.Offset;
  166. RHS = RHSAddr.Offset;
  167. }
  168. }
  169. }
  170. }
  171. const DataLayout &DL = I.getModule()->getDataLayout();
  172. if (Value *V = simplifyCmpInst(I.getPredicate(), LHS, RHS, DL)) {
  173. SimplifiedValues[&I] = V;
  174. return true;
  175. }
  176. return Base::visitCmpInst(I);
  177. }
  178. bool UnrolledInstAnalyzer::visitPHINode(PHINode &PN) {
  179. // Run base visitor first. This way we can gather some useful for later
  180. // analysis information.
  181. if (Base::visitPHINode(PN))
  182. return true;
  183. // The loop induction PHI nodes are definitionally free.
  184. return PN.getParent() == L->getHeader();
  185. }
  186. bool UnrolledInstAnalyzer::visitInstruction(Instruction &I) {
  187. return simplifyInstWithSCEV(&I);
  188. }