SpeculateAroundPHIs.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SpeculateAroundPHIs.h - Speculate around PHIs ------------*- 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. #ifndef LLVM_TRANSFORMS_SCALAR_SPECULATEAROUNDPHIS_H
  14. #define LLVM_TRANSFORMS_SCALAR_SPECULATEAROUNDPHIS_H
  15. #include "llvm/ADT/SetVector.h"
  16. #include "llvm/Analysis/AssumptionCache.h"
  17. #include "llvm/IR/Dominators.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/PassManager.h"
  20. #include "llvm/Support/Compiler.h"
  21. #include <vector>
  22. namespace llvm {
  23. /// This pass handles simple speculating of instructions around PHIs when
  24. /// doing so is profitable for a particular target despite duplicated
  25. /// instructions.
  26. ///
  27. /// The motivating example are PHIs of constants which will require
  28. /// materializing the constants along each edge. If the PHI is used by an
  29. /// instruction where the target can materialize the constant as part of the
  30. /// instruction, it is profitable to speculate those instructions around the
  31. /// PHI node. This can reduce dynamic instruction count as well as decrease
  32. /// register pressure.
  33. ///
  34. /// Consider this IR for example:
  35. /// ```
  36. /// entry:
  37. /// br i1 %flag, label %a, label %b
  38. ///
  39. /// a:
  40. /// br label %exit
  41. ///
  42. /// b:
  43. /// br label %exit
  44. ///
  45. /// exit:
  46. /// %p = phi i32 [ 7, %a ], [ 11, %b ]
  47. /// %sum = add i32 %arg, %p
  48. /// ret i32 %sum
  49. /// ```
  50. /// To materialize the inputs to this PHI node may require an explicit
  51. /// instruction. For example, on x86 this would turn into something like
  52. /// ```
  53. /// testq %eax, %eax
  54. /// movl $7, %rNN
  55. /// jne .L
  56. /// movl $11, %rNN
  57. /// .L:
  58. /// addl %edi, %rNN
  59. /// movl %rNN, %eax
  60. /// retq
  61. /// ```
  62. /// When these constants can be folded directly into another instruction, it
  63. /// would be preferable to avoid the potential for register pressure (above we
  64. /// can easily avoid it, but that isn't always true) and simply duplicate the
  65. /// instruction using the PHI:
  66. /// ```
  67. /// entry:
  68. /// br i1 %flag, label %a, label %b
  69. ///
  70. /// a:
  71. /// %sum.1 = add i32 %arg, 7
  72. /// br label %exit
  73. ///
  74. /// b:
  75. /// %sum.2 = add i32 %arg, 11
  76. /// br label %exit
  77. ///
  78. /// exit:
  79. /// %p = phi i32 [ %sum.1, %a ], [ %sum.2, %b ]
  80. /// ret i32 %p
  81. /// ```
  82. /// Which will generate something like the following on x86:
  83. /// ```
  84. /// testq %eax, %eax
  85. /// addl $7, %edi
  86. /// jne .L
  87. /// addl $11, %edi
  88. /// .L:
  89. /// movl %edi, %eax
  90. /// retq
  91. /// ```
  92. ///
  93. /// It is important to note that this pass is never intended to handle more
  94. /// complex cases where speculating around PHIs allows simplifications of the
  95. /// IR itself or other subsequent optimizations. Those can and should already
  96. /// be handled before this pass is ever run by a more powerful analysis that
  97. /// can reason about equivalences and common subexpressions. Classically, those
  98. /// cases would be handled by a GVN-powered PRE or similar transform. This
  99. /// pass, in contrast, is *only* interested in cases where despite no
  100. /// simplifications to the IR itself, speculation is *faster* to execute. The
  101. /// result of this is that the cost models which are appropriate to consider
  102. /// here are relatively simple ones around execution and codesize cost, without
  103. /// any need to consider simplifications or other transformations.
  104. struct SpeculateAroundPHIsPass : PassInfoMixin<SpeculateAroundPHIsPass> {
  105. /// Run the pass over the function.
  106. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  107. };
  108. } // end namespace llvm
  109. #endif // LLVM_TRANSFORMS_SCALAR_SPECULATEAROUNDPHIS_H
  110. #ifdef __GNUC__
  111. #pragma GCC diagnostic pop
  112. #endif