AtomicExpandUtils.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- AtomicExpandUtils.h - Utilities for expanding atomic instructions --===//
  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_CODEGEN_ATOMICEXPANDUTILS_H
  14. #define LLVM_CODEGEN_ATOMICEXPANDUTILS_H
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/IR/IRBuilder.h"
  17. #include "llvm/Support/AtomicOrdering.h"
  18. namespace llvm {
  19. class AtomicRMWInst;
  20. class Value;
  21. /// Parameters (see the expansion example below):
  22. /// (the builder, %addr, %loaded, %new_val, ordering,
  23. /// /* OUT */ %success, /* OUT */ %new_loaded)
  24. using CreateCmpXchgInstFun =
  25. function_ref<void(IRBuilder<> &, Value *, Value *, Value *, Align,
  26. AtomicOrdering, SyncScope::ID, Value *&, Value *&)>;
  27. /// Expand an atomic RMW instruction into a loop utilizing
  28. /// cmpxchg. You'll want to make sure your target machine likes cmpxchg
  29. /// instructions in the first place and that there isn't another, better,
  30. /// transformation available (for example AArch32/AArch64 have linked loads).
  31. ///
  32. /// This is useful in passes which can't rewrite the more exotic RMW
  33. /// instructions directly into a platform specific intrinsics (because, say,
  34. /// those intrinsics don't exist). If such a pass is able to expand cmpxchg
  35. /// instructions directly however, then, with this function, it could avoid two
  36. /// extra module passes (avoiding passes by `-atomic-expand` and itself). A
  37. /// specific example would be PNaCl's `RewriteAtomics` pass.
  38. ///
  39. /// Given: atomicrmw some_op iN* %addr, iN %incr ordering
  40. ///
  41. /// The standard expansion we produce is:
  42. /// [...]
  43. /// %init_loaded = load atomic iN* %addr
  44. /// br label %loop
  45. /// loop:
  46. /// %loaded = phi iN [ %init_loaded, %entry ], [ %new_loaded, %loop ]
  47. /// %new = some_op iN %loaded, %incr
  48. /// ; This is what -atomic-expand will produce using this function on i686
  49. /// targets:
  50. /// %pair = cmpxchg iN* %addr, iN %loaded, iN %new_val
  51. /// %new_loaded = extractvalue { iN, i1 } %pair, 0
  52. /// %success = extractvalue { iN, i1 } %pair, 1
  53. /// ; End callback produced IR
  54. /// br i1 %success, label %atomicrmw.end, label %loop
  55. /// atomicrmw.end:
  56. /// [...]
  57. ///
  58. /// Returns true if the containing function was modified.
  59. bool expandAtomicRMWToCmpXchg(AtomicRMWInst *AI, CreateCmpXchgInstFun CreateCmpXchg);
  60. } // end namespace llvm
  61. #endif // LLVM_CODEGEN_ATOMICEXPANDUTILS_H
  62. #ifdef __GNUC__
  63. #pragma GCC diagnostic pop
  64. #endif