TLSVariableHoist.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- TLSVariableHoist.h ------ Remove Redundant TLS Loads -------*- 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 identifies/eliminates Redundant TLS Loads if related option is set.
  15. // For example:
  16. // static __thread int x;
  17. // int g();
  18. // int f(int c) {
  19. // int *px = &x;
  20. // while (c--)
  21. // *px += g();
  22. // return *px;
  23. // }
  24. //
  25. // will generate Redundant TLS Loads by compiling it with
  26. // clang++ -fPIC -ftls-model=global-dynamic -O2 -S
  27. //
  28. // .LBB0_2: # %while.body
  29. // # =>This Inner Loop Header: Depth=1
  30. // callq _Z1gv@PLT
  31. // movl %eax, %ebp
  32. // leaq _ZL1x@TLSLD(%rip), %rdi
  33. // callq __tls_get_addr@PLT
  34. // addl _ZL1x@DTPOFF(%rax), %ebp
  35. // movl %ebp, _ZL1x@DTPOFF(%rax)
  36. // addl $-1, %ebx
  37. // jne .LBB0_2
  38. // jmp .LBB0_3
  39. // .LBB0_4: # %entry.while.end_crit_edge
  40. // leaq _ZL1x@TLSLD(%rip), %rdi
  41. // callq __tls_get_addr@PLT
  42. // movl _ZL1x@DTPOFF(%rax), %ebp
  43. //
  44. // The Redundant TLS Loads will hurt the performance, especially in loops.
  45. // So we try to eliminate/move them if required by customers, let it be:
  46. //
  47. // # %bb.0: # %entry
  48. // ...
  49. // movl %edi, %ebx
  50. // leaq _ZL1x@TLSLD(%rip), %rdi
  51. // callq __tls_get_addr@PLT
  52. // leaq _ZL1x@DTPOFF(%rax), %r14
  53. // testl %ebx, %ebx
  54. // je .LBB0_1
  55. // .LBB0_2: # %while.body
  56. // # =>This Inner Loop Header: Depth=1
  57. // callq _Z1gv@PLT
  58. // addl (%r14), %eax
  59. // movl %eax, (%r14)
  60. // addl $-1, %ebx
  61. // jne .LBB0_2
  62. // jmp .LBB0_3
  63. //
  64. //===----------------------------------------------------------------------===//
  65. #ifndef LLVM_TRANSFORMS_SCALAR_TLSVARIABLEHOIST_H
  66. #define LLVM_TRANSFORMS_SCALAR_TLSVARIABLEHOIST_H
  67. #include "llvm/ADT/MapVector.h"
  68. #include "llvm/ADT/SmallVector.h"
  69. #include "llvm/Analysis/LoopInfo.h"
  70. #include "llvm/IR/PassManager.h"
  71. namespace llvm {
  72. class BasicBlock;
  73. class DominatorTree;
  74. class Function;
  75. class GlobalVariable;
  76. class Instruction;
  77. /// A private "module" namespace for types and utilities used by
  78. /// TLSVariableHoist. These are implementation details and should
  79. /// not be used by clients.
  80. namespace tlshoist {
  81. /// Keeps track of the user of a TLS variable and the operand index
  82. /// where the variable is used.
  83. struct TLSUser {
  84. Instruction *Inst;
  85. unsigned OpndIdx;
  86. TLSUser(Instruction *Inst, unsigned Idx) : Inst(Inst), OpndIdx(Idx) {}
  87. };
  88. /// Keeps track of a TLS variable candidate and its users.
  89. struct TLSCandidate {
  90. SmallVector<TLSUser, 8> Users;
  91. /// Add the user to the use list and update the cost.
  92. void addUser(Instruction *Inst, unsigned Idx) {
  93. Users.push_back(TLSUser(Inst, Idx));
  94. }
  95. };
  96. } // end namespace tlshoist
  97. class TLSVariableHoistPass : public PassInfoMixin<TLSVariableHoistPass> {
  98. public:
  99. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  100. // Glue for old PM.
  101. bool runImpl(Function &F, DominatorTree &DT, LoopInfo &LI);
  102. private:
  103. DominatorTree *DT;
  104. LoopInfo *LI;
  105. /// Keeps track of TLS variable candidates found in the function.
  106. using TLSCandMapType = MapVector<GlobalVariable *, tlshoist::TLSCandidate>;
  107. TLSCandMapType TLSCandMap;
  108. void collectTLSCandidates(Function &Fn);
  109. void collectTLSCandidate(Instruction *Inst);
  110. Instruction *getNearestLoopDomInst(BasicBlock *BB, Loop *L);
  111. Instruction *getDomInst(Instruction *I1, Instruction *I2);
  112. BasicBlock::iterator findInsertPos(Function &Fn, GlobalVariable *GV,
  113. BasicBlock *&PosBB);
  114. Instruction *genBitCastInst(Function &Fn, GlobalVariable *GV);
  115. bool tryReplaceTLSCandidates(Function &Fn);
  116. bool tryReplaceTLSCandidate(Function &Fn, GlobalVariable *GV);
  117. };
  118. } // end namespace llvm
  119. #endif // LLVM_TRANSFORMS_SCALAR_TLSVARIABLEHOIST_H
  120. #ifdef __GNUC__
  121. #pragma GCC diagnostic pop
  122. #endif