ObjCARCExpand.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //===- ObjCARCExpand.cpp - ObjC ARC Optimization --------------------------===//
  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. /// \file
  9. /// This file defines ObjC ARC optimizations. ARC stands for Automatic
  10. /// Reference Counting and is a system for managing reference counts for objects
  11. /// in Objective C.
  12. ///
  13. /// This specific file deals with early optimizations which perform certain
  14. /// cleanup operations.
  15. ///
  16. /// WARNING: This file knows about certain library functions. It recognizes them
  17. /// by name, and hardwires knowledge of their semantics.
  18. ///
  19. /// WARNING: This file knows about how certain Objective-C library functions are
  20. /// used. Naive LLVM IR transformations which would otherwise be
  21. /// behavior-preserving may break these assumptions.
  22. ///
  23. //===----------------------------------------------------------------------===//
  24. #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/InstIterator.h"
  27. #include "llvm/IR/Instruction.h"
  28. #include "llvm/IR/Instructions.h"
  29. #include "llvm/IR/PassManager.h"
  30. #include "llvm/IR/Value.h"
  31. #include "llvm/Support/Casting.h"
  32. #include "llvm/Support/Debug.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include "llvm/Transforms/ObjCARC.h"
  35. #define DEBUG_TYPE "objc-arc-expand"
  36. using namespace llvm;
  37. using namespace llvm::objcarc;
  38. namespace {
  39. static bool runImpl(Function &F) {
  40. if (!EnableARCOpts)
  41. return false;
  42. // If nothing in the Module uses ARC, don't do anything.
  43. if (!ModuleHasARC(*F.getParent()))
  44. return false;
  45. bool Changed = false;
  46. LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName()
  47. << "\n");
  48. for (Instruction &Inst : instructions(&F)) {
  49. LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << Inst << "\n");
  50. switch (GetBasicARCInstKind(&Inst)) {
  51. case ARCInstKind::Retain:
  52. case ARCInstKind::RetainRV:
  53. case ARCInstKind::Autorelease:
  54. case ARCInstKind::AutoreleaseRV:
  55. case ARCInstKind::FusedRetainAutorelease:
  56. case ARCInstKind::FusedRetainAutoreleaseRV: {
  57. // These calls return their argument verbatim, as a low-level
  58. // optimization. However, this makes high-level optimizations
  59. // harder. Undo any uses of this optimization that the front-end
  60. // emitted here. We'll redo them in the contract pass.
  61. Changed = true;
  62. Value *Value = cast<CallInst>(&Inst)->getArgOperand(0);
  63. LLVM_DEBUG(dbgs() << "ObjCARCExpand: Old = " << Inst
  64. << "\n"
  65. " New = "
  66. << *Value << "\n");
  67. Inst.replaceAllUsesWith(Value);
  68. break;
  69. }
  70. default:
  71. break;
  72. }
  73. }
  74. LLVM_DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
  75. return Changed;
  76. }
  77. } // namespace
  78. PreservedAnalyses ObjCARCExpandPass::run(Function &F,
  79. FunctionAnalysisManager &AM) {
  80. if (!runImpl(F))
  81. return PreservedAnalyses::all();
  82. PreservedAnalyses PA;
  83. PA.preserveSet<CFGAnalyses>();
  84. return PA;
  85. }