LowerAtomicPass.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===- LowerAtomic.cpp - Lower atomic intrinsics --------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This pass lowers atomic intrinsics to non-atomic form for use in a known
  10. // non-preemptible environment.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Scalar/LowerAtomicPass.h"
  14. #include "llvm/IR/Function.h"
  15. #include "llvm/IR/IRBuilder.h"
  16. #include "llvm/InitializePasses.h"
  17. #include "llvm/Pass.h"
  18. #include "llvm/Transforms/Scalar.h"
  19. #include "llvm/Transforms/Utils/LowerAtomic.h"
  20. using namespace llvm;
  21. #define DEBUG_TYPE "loweratomic"
  22. static bool LowerFenceInst(FenceInst *FI) {
  23. FI->eraseFromParent();
  24. return true;
  25. }
  26. static bool LowerLoadInst(LoadInst *LI) {
  27. LI->setAtomic(AtomicOrdering::NotAtomic);
  28. return true;
  29. }
  30. static bool LowerStoreInst(StoreInst *SI) {
  31. SI->setAtomic(AtomicOrdering::NotAtomic);
  32. return true;
  33. }
  34. static bool runOnBasicBlock(BasicBlock &BB) {
  35. bool Changed = false;
  36. for (Instruction &Inst : make_early_inc_range(BB)) {
  37. if (FenceInst *FI = dyn_cast<FenceInst>(&Inst))
  38. Changed |= LowerFenceInst(FI);
  39. else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&Inst))
  40. Changed |= lowerAtomicCmpXchgInst(CXI);
  41. else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&Inst))
  42. Changed |= lowerAtomicRMWInst(RMWI);
  43. else if (LoadInst *LI = dyn_cast<LoadInst>(&Inst)) {
  44. if (LI->isAtomic())
  45. LowerLoadInst(LI);
  46. } else if (StoreInst *SI = dyn_cast<StoreInst>(&Inst)) {
  47. if (SI->isAtomic())
  48. LowerStoreInst(SI);
  49. }
  50. }
  51. return Changed;
  52. }
  53. static bool lowerAtomics(Function &F) {
  54. bool Changed = false;
  55. for (BasicBlock &BB : F) {
  56. Changed |= runOnBasicBlock(BB);
  57. }
  58. return Changed;
  59. }
  60. PreservedAnalyses LowerAtomicPass::run(Function &F, FunctionAnalysisManager &) {
  61. if (lowerAtomics(F))
  62. return PreservedAnalyses::none();
  63. return PreservedAnalyses::all();
  64. }
  65. namespace {
  66. class LowerAtomicLegacyPass : public FunctionPass {
  67. public:
  68. static char ID;
  69. LowerAtomicLegacyPass() : FunctionPass(ID) {
  70. initializeLowerAtomicLegacyPassPass(*PassRegistry::getPassRegistry());
  71. }
  72. bool runOnFunction(Function &F) override {
  73. // Don't skip optnone functions; atomics still need to be lowered.
  74. FunctionAnalysisManager DummyFAM;
  75. auto PA = Impl.run(F, DummyFAM);
  76. return !PA.areAllPreserved();
  77. }
  78. private:
  79. LowerAtomicPass Impl;
  80. };
  81. }
  82. char LowerAtomicLegacyPass::ID = 0;
  83. INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic",
  84. "Lower atomic intrinsics to non-atomic form", false, false)
  85. Pass *llvm::createLowerAtomicPass() { return new LowerAtomicLegacyPass(); }