LoopUnrollAnalyzer.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //===- llvm/Analysis/LoopUnrollAnalyzer.h - Loop Unroll Analyzer-*- 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. #ifndef LLVM_ANALYSIS_LOOPUNROLLANALYZER_H
  15. #define LLVM_ANALYSIS_LOOPUNROLLANALYZER_H
  16. #include "llvm/Analysis/InstructionSimplify.h"
  17. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  18. #include "llvm/IR/InstVisitor.h"
  19. // This class is used to get an estimate of the optimization effects that we
  20. // could get from complete loop unrolling. It comes from the fact that some
  21. // loads might be replaced with concrete constant values and that could trigger
  22. // a chain of instruction simplifications.
  23. //
  24. // E.g. we might have:
  25. // int a[] = {0, 1, 0};
  26. // v = 0;
  27. // for (i = 0; i < 3; i ++)
  28. // v += b[i]*a[i];
  29. // If we completely unroll the loop, we would get:
  30. // v = b[0]*a[0] + b[1]*a[1] + b[2]*a[2]
  31. // Which then will be simplified to:
  32. // v = b[0]* 0 + b[1]* 1 + b[2]* 0
  33. // And finally:
  34. // v = b[1]
  35. namespace llvm {
  36. class UnrolledInstAnalyzer : private InstVisitor<UnrolledInstAnalyzer, bool> {
  37. typedef InstVisitor<UnrolledInstAnalyzer, bool> Base;
  38. friend class InstVisitor<UnrolledInstAnalyzer, bool>;
  39. struct SimplifiedAddress {
  40. Value *Base = nullptr;
  41. ConstantInt *Offset = nullptr;
  42. };
  43. public:
  44. UnrolledInstAnalyzer(unsigned Iteration,
  45. DenseMap<Value *, Value *> &SimplifiedValues,
  46. ScalarEvolution &SE, const Loop *L)
  47. : SimplifiedValues(SimplifiedValues), SE(SE), L(L) {
  48. IterationNumber = SE.getConstant(APInt(64, Iteration));
  49. }
  50. // Allow access to the initial visit method.
  51. using Base::visit;
  52. private:
  53. /// A cache of pointer bases and constant-folded offsets corresponding
  54. /// to GEP (or derived from GEP) instructions.
  55. ///
  56. /// In order to find the base pointer one needs to perform non-trivial
  57. /// traversal of the corresponding SCEV expression, so it's good to have the
  58. /// results saved.
  59. DenseMap<Value *, SimplifiedAddress> SimplifiedAddresses;
  60. /// SCEV expression corresponding to number of currently simulated
  61. /// iteration.
  62. const SCEV *IterationNumber;
  63. /// While we walk the loop instructions, we build up and maintain a mapping
  64. /// of simplified values specific to this iteration. The idea is to propagate
  65. /// any special information we have about loads that can be replaced with
  66. /// constants after complete unrolling, and account for likely simplifications
  67. /// post-unrolling.
  68. DenseMap<Value *, Value *> &SimplifiedValues;
  69. ScalarEvolution &SE;
  70. const Loop *L;
  71. bool simplifyInstWithSCEV(Instruction *I);
  72. bool visitInstruction(Instruction &I);
  73. bool visitBinaryOperator(BinaryOperator &I);
  74. bool visitLoad(LoadInst &I);
  75. bool visitCastInst(CastInst &I);
  76. bool visitCmpInst(CmpInst &I);
  77. bool visitPHINode(PHINode &PN);
  78. };
  79. }
  80. #endif