SpeculativeExecution.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SpeculativeExecution.h -----------------------------------*- 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 pass hoists instructions to enable speculative execution on
  15. // targets where branches are expensive. This is aimed at GPUs. It
  16. // currently works on simple if-then and if-then-else
  17. // patterns.
  18. //
  19. // Removing branches is not the only motivation for this
  20. // pass. E.g. consider this code and assume that there is no
  21. // addressing mode for multiplying by sizeof(*a):
  22. //
  23. // if (b > 0)
  24. // c = a[i + 1]
  25. // if (d > 0)
  26. // e = a[i + 2]
  27. //
  28. // turns into
  29. //
  30. // p = &a[i + 1];
  31. // if (b > 0)
  32. // c = *p;
  33. // q = &a[i + 2];
  34. // if (d > 0)
  35. // e = *q;
  36. //
  37. // which could later be optimized to
  38. //
  39. // r = &a[i];
  40. // if (b > 0)
  41. // c = r[1];
  42. // if (d > 0)
  43. // e = r[2];
  44. //
  45. // Later passes sink back much of the speculated code that did not enable
  46. // further optimization.
  47. //
  48. // This pass is more aggressive than the function SpeculativeyExecuteBB in
  49. // SimplifyCFG. SimplifyCFG will not speculate if no selects are introduced and
  50. // it will speculate at most one instruction. It also will not speculate if
  51. // there is a value defined in the if-block that is only used in the then-block.
  52. // These restrictions make sense since the speculation in SimplifyCFG seems
  53. // aimed at introducing cheap selects, while this pass is intended to do more
  54. // aggressive speculation while counting on later passes to either capitalize on
  55. // that or clean it up.
  56. //
  57. // If the pass was created by calling
  58. // createSpeculativeExecutionIfHasBranchDivergencePass or the
  59. // -spec-exec-only-if-divergent-target option is present, this pass only has an
  60. // effect on targets where TargetTransformInfo::hasBranchDivergence() is true;
  61. // on other targets, it is a nop.
  62. //
  63. // This lets you include this pass unconditionally in the IR pass pipeline, but
  64. // only enable it for relevant targets.
  65. //
  66. //===----------------------------------------------------------------------===//
  67. #ifndef LLVM_TRANSFORMS_SCALAR_SPECULATIVEEXECUTION_H
  68. #define LLVM_TRANSFORMS_SCALAR_SPECULATIVEEXECUTION_H
  69. #include "llvm/IR/PassManager.h"
  70. namespace llvm {
  71. class TargetTransformInfo;
  72. class SpeculativeExecutionPass
  73. : public PassInfoMixin<SpeculativeExecutionPass> {
  74. public:
  75. SpeculativeExecutionPass(bool OnlyIfDivergentTarget = false);
  76. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  77. // Glue for old PM
  78. bool runImpl(Function &F, TargetTransformInfo *TTI);
  79. private:
  80. bool runOnBasicBlock(BasicBlock &B);
  81. bool considerHoistingFromTo(BasicBlock &FromBlock, BasicBlock &ToBlock);
  82. // If true, this pass is a nop unless the target architecture has branch
  83. // divergence.
  84. const bool OnlyIfDivergentTarget = false;
  85. TargetTransformInfo *TTI = nullptr;
  86. };
  87. }
  88. #endif // LLVM_TRANSFORMS_SCALAR_SPECULATIVEEXECUTION_H
  89. #ifdef __GNUC__
  90. #pragma GCC diagnostic pop
  91. #endif