LoopUnrollAnalyzer.h 3.4 KB

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