Michael Gottesman | 6eb95dc | 2013-07-10 18:49:00 +0000 | [diff] [blame] | 1 | //===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===// |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 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 common definitions/declarations used by the ObjC ARC |
| 11 | /// Optimizer. ARC stands for Automatic Reference Counting and is a system for |
| 12 | /// managing reference counts for objects in Objective C. |
| 13 | /// |
| 14 | /// WARNING: This file knows about certain library functions. It recognizes them |
| 15 | /// by name, and hardwires knowledge of their semantics. |
| 16 | /// |
| 17 | /// WARNING: This file knows about how certain Objective-C library functions are |
| 18 | /// used. Naive LLVM IR transformations which would otherwise be |
| 19 | /// behavior-preserving may break these assumptions. |
| 20 | /// |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 23 | #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H |
| 24 | #define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 25 | |
| 26 | #include "llvm/ADT/StringSwitch.h" |
| 27 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 0f79218 | 2015-08-20 08:06:03 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/ObjCARCAnalysisUtils.h" |
| 29 | #include "llvm/Analysis/ObjCARCInstKind.h" |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/Passes.h" |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 32 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 33 | #include "llvm/IR/InstIterator.h" |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Module.h" |
| 35 | #include "llvm/Pass.h" |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/ObjCARC.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Utils/Local.h" |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 38 | |
| 39 | namespace llvm { |
Michael Gottesman | 13a5f1a | 2013-01-29 04:51:59 +0000 | [diff] [blame] | 40 | class raw_ostream; |
| 41 | } |
| 42 | |
| 43 | namespace llvm { |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 44 | namespace objcarc { |
| 45 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 46 | /// \brief Erase the given instruction. |
| 47 | /// |
| 48 | /// Many ObjC calls return their argument verbatim, |
| 49 | /// so if it's such a call and the return value has users, replace them with the |
| 50 | /// argument value. |
| 51 | /// |
| 52 | static inline void EraseInstruction(Instruction *CI) { |
| 53 | Value *OldArg = cast<CallInst>(CI)->getArgOperand(0); |
| 54 | |
| 55 | bool Unused = CI->use_empty(); |
| 56 | |
| 57 | if (!Unused) { |
| 58 | // Replace the return value with the argument. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 59 | assert((IsForwarding(GetBasicARCInstKind(CI)) || |
| 60 | (IsNoopOnNull(GetBasicARCInstKind(CI)) && |
Michael Gottesman | c1b648f | 2013-07-08 23:30:23 +0000 | [diff] [blame] | 61 | isa<ConstantPointerNull>(OldArg))) && |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 62 | "Can't delete non-forwarding instruction with users!"); |
| 63 | CI->replaceAllUsesWith(OldArg); |
| 64 | } |
| 65 | |
| 66 | CI->eraseFromParent(); |
| 67 | |
| 68 | if (Unused) |
| 69 | RecursivelyDeleteTriviallyDeadInstructions(OldArg); |
| 70 | } |
| 71 | |
Akira Hatanaka | 6fdcb3c | 2017-04-29 00:23:11 +0000 | [diff] [blame] | 72 | /// If Inst is a ReturnRV and its operand is a call or invoke, return the |
| 73 | /// operand. Otherwise return null. |
| 74 | static inline const Instruction *getreturnRVOperand(const Instruction &Inst, |
| 75 | ARCInstKind Class) { |
| 76 | if (Class != ARCInstKind::RetainRV) |
| 77 | return nullptr; |
| 78 | |
| 79 | const auto *Opnd = Inst.getOperand(0)->stripPointerCasts(); |
| 80 | if (const auto *C = dyn_cast<CallInst>(Opnd)) |
| 81 | return C; |
| 82 | return dyn_cast<InvokeInst>(Opnd); |
| 83 | } |
| 84 | |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 85 | } // end namespace objcarc |
| 86 | } // end namespace llvm |
| 87 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 88 | #endif |