blob: 5fd45b00af178c24222ef60d0d620ab1e1eefa99 [file] [log] [blame]
Michael Gottesman6eb95dc2013-07-10 18:49:00 +00001//===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===//
Michael Gottesman08904e32013-01-28 03:28:38 +00002//
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 Kramera7c40ef2014-08-13 16:26:38 +000023#ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
24#define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
Michael Gottesman08904e32013-01-28 03:28:38 +000025
26#include "llvm/ADT/StringSwitch.h"
Michael Gottesman65cb7372015-03-16 07:02:27 +000027#include "llvm/ADT/Optional.h"
Michael Gottesman08904e32013-01-28 03:28:38 +000028#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth0f792182015-08-20 08:06:03 +000029#include "llvm/Analysis/ObjCARCAnalysisUtils.h"
30#include "llvm/Analysis/ObjCARCInstKind.h"
Michael Gottesman08904e32013-01-28 03:28:38 +000031#include "llvm/Analysis/Passes.h"
Michael Gottesman294e7da2013-01-28 05:51:54 +000032#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000033#include "llvm/IR/CallSite.h"
Chandler Carruth83948572014-03-04 10:30:26 +000034#include "llvm/IR/InstIterator.h"
Michael Gottesman08904e32013-01-28 03:28:38 +000035#include "llvm/IR/Module.h"
36#include "llvm/Pass.h"
Michael Gottesman08904e32013-01-28 03:28:38 +000037#include "llvm/Transforms/ObjCARC.h"
Michael Gottesman778138e2013-01-29 03:03:03 +000038#include "llvm/Transforms/Utils/Local.h"
Michael Gottesman08904e32013-01-28 03:28:38 +000039
40namespace llvm {
Michael Gottesman13a5f1a2013-01-29 04:51:59 +000041class raw_ostream;
42}
43
44namespace llvm {
Michael Gottesman08904e32013-01-28 03:28:38 +000045namespace objcarc {
46
Michael Gottesman778138e2013-01-29 03:03:03 +000047/// \brief Erase the given instruction.
48///
49/// Many ObjC calls return their argument verbatim,
50/// so if it's such a call and the return value has users, replace them with the
51/// argument value.
52///
53static inline void EraseInstruction(Instruction *CI) {
54 Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
55
56 bool Unused = CI->use_empty();
57
58 if (!Unused) {
59 // Replace the return value with the argument.
Michael Gottesman6f729fa2015-02-19 19:51:32 +000060 assert((IsForwarding(GetBasicARCInstKind(CI)) ||
61 (IsNoopOnNull(GetBasicARCInstKind(CI)) &&
Michael Gottesmanc1b648f2013-07-08 23:30:23 +000062 isa<ConstantPointerNull>(OldArg))) &&
Michael Gottesman778138e2013-01-29 03:03:03 +000063 "Can't delete non-forwarding instruction with users!");
64 CI->replaceAllUsesWith(OldArg);
65 }
66
67 CI->eraseFromParent();
68
69 if (Unused)
70 RecursivelyDeleteTriviallyDeadInstructions(OldArg);
71}
72
Michael Gottesman08904e32013-01-28 03:28:38 +000073} // end namespace objcarc
74} // end namespace llvm
75
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000076#endif