ObjCARCExpand.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===- ObjCARCExpand.cpp - ObjC ARC Optimization --------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. /// \file
  10. /// This file defines ObjC ARC optimizations. ARC stands for Automatic
  11. /// Reference Counting and is a system for managing reference counts for objects
  12. /// in Objective C.
  13. ///
  14. /// This specific file deals with early optimizations which perform certain
  15. /// cleanup operations.
  16. ///
  17. /// WARNING: This file knows about certain library functions. It recognizes them
  18. /// by name, and hardwires knowledge of their semantics.
  19. ///
  20. /// WARNING: This file knows about how certain Objective-C library functions are
  21. /// used. Naive LLVM IR transformations which would otherwise be
  22. /// behavior-preserving may break these assumptions.
  23. ///
  24. //===----------------------------------------------------------------------===//
  25. #include "ObjCARC.h"
  26. #include "llvm/ADT/StringRef.h"
  27. #include "llvm/IR/Function.h"
  28. #include "llvm/IR/InstIterator.h"
  29. #include "llvm/IR/Instruction.h"
  30. #include "llvm/IR/Instructions.h"
  31. #include "llvm/IR/Value.h"
  32. #include "llvm/Pass.h"
  33. #include "llvm/PassAnalysisSupport.h"
  34. #include "llvm/PassRegistry.h"
  35. #include "llvm/PassSupport.h"
  36. #include "llvm/Support/Casting.h"
  37. #include "llvm/Support/Debug.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #define DEBUG_TYPE "objc-arc-expand"
  40. namespace llvm {
  41. class Module;
  42. }
  43. using namespace llvm;
  44. using namespace llvm::objcarc;
  45. namespace {
  46. /// \brief Early ARC transformations.
  47. class ObjCARCExpand : public FunctionPass {
  48. void getAnalysisUsage(AnalysisUsage &AU) const override;
  49. bool doInitialization(Module &M) override;
  50. bool runOnFunction(Function &F) override;
  51. /// A flag indicating whether this optimization pass should run.
  52. bool Run;
  53. public:
  54. static char ID;
  55. ObjCARCExpand() : FunctionPass(ID) {
  56. initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
  57. }
  58. };
  59. }
  60. char ObjCARCExpand::ID = 0;
  61. INITIALIZE_PASS(ObjCARCExpand,
  62. "objc-arc-expand", "ObjC ARC expansion", false, false)
  63. Pass *llvm::createObjCARCExpandPass() {
  64. return new ObjCARCExpand();
  65. }
  66. void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
  67. AU.setPreservesCFG();
  68. }
  69. bool ObjCARCExpand::doInitialization(Module &M) {
  70. Run = ModuleHasARC(M);
  71. return false;
  72. }
  73. bool ObjCARCExpand::runOnFunction(Function &F) {
  74. if (!EnableARCOpts)
  75. return false;
  76. // If nothing in the Module uses ARC, don't do anything.
  77. if (!Run)
  78. return false;
  79. bool Changed = false;
  80. DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() << "\n");
  81. for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
  82. Instruction *Inst = &*I;
  83. DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
  84. switch (GetBasicARCInstKind(Inst)) {
  85. case ARCInstKind::Retain:
  86. case ARCInstKind::RetainRV:
  87. case ARCInstKind::Autorelease:
  88. case ARCInstKind::AutoreleaseRV:
  89. case ARCInstKind::FusedRetainAutorelease:
  90. case ARCInstKind::FusedRetainAutoreleaseRV: {
  91. // These calls return their argument verbatim, as a low-level
  92. // optimization. However, this makes high-level optimizations
  93. // harder. Undo any uses of this optimization that the front-end
  94. // emitted here. We'll redo them in the contract pass.
  95. Changed = true;
  96. Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
  97. DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n"
  98. " New = " << *Value << "\n");
  99. Inst->replaceAllUsesWith(Value);
  100. break;
  101. }
  102. default:
  103. break;
  104. }
  105. }
  106. DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
  107. return Changed;
  108. }