2013-07-11 02:49:00 +08:00
|
|
|
//===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===//
|
2013-01-28 11:28:38 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-01-28 11:28:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// This file defines common definitions/declarations used by the ObjC ARC
|
|
|
|
/// Optimizer. ARC stands for Automatic Reference Counting and is a system for
|
|
|
|
/// managing reference counts for objects in Objective C.
|
|
|
|
///
|
|
|
|
/// WARNING: This file knows about certain library functions. It recognizes them
|
|
|
|
/// by name, and hardwires knowledge of their semantics.
|
|
|
|
///
|
|
|
|
/// WARNING: This file knows about how certain Objective-C library functions are
|
|
|
|
/// used. Naive LLVM IR transformations which would otherwise be
|
|
|
|
/// behavior-preserving may break these assumptions.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
|
|
|
|
#define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
|
2013-01-28 11:28:38 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2015-08-20 16:06:03 +08:00
|
|
|
#include "llvm/Analysis/ObjCARCAnalysisUtils.h"
|
|
|
|
#include "llvm/Analysis/ObjCARCInstKind.h"
|
2013-01-28 11:28:38 +08:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2018-06-05 05:23:21 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2013-01-28 13:51:54 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2014-03-04 19:01:28 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2014-03-04 18:30:26 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-28 11:28:38 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Transforms/ObjCARC.h"
|
|
|
|
|
2013-01-29 12:51:59 +08:00
|
|
|
namespace llvm {
|
|
|
|
class raw_ostream;
|
|
|
|
}
|
|
|
|
|
2013-01-28 11:28:38 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace objcarc {
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Erase the given instruction.
|
2013-01-29 11:03:03 +08:00
|
|
|
///
|
|
|
|
/// Many ObjC calls return their argument verbatim,
|
|
|
|
/// so if it's such a call and the return value has users, replace them with the
|
|
|
|
/// argument value.
|
|
|
|
///
|
|
|
|
static inline void EraseInstruction(Instruction *CI) {
|
|
|
|
Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
|
|
|
|
|
|
|
|
bool Unused = CI->use_empty();
|
|
|
|
|
|
|
|
if (!Unused) {
|
|
|
|
// Replace the return value with the argument.
|
2015-02-20 03:51:32 +08:00
|
|
|
assert((IsForwarding(GetBasicARCInstKind(CI)) ||
|
|
|
|
(IsNoopOnNull(GetBasicARCInstKind(CI)) &&
|
2019-01-03 05:00:02 +08:00
|
|
|
IsNullOrUndef(OldArg->stripPointerCasts()))) &&
|
2013-01-29 11:03:03 +08:00
|
|
|
"Can't delete non-forwarding instruction with users!");
|
|
|
|
CI->replaceAllUsesWith(OldArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
CI->eraseFromParent();
|
|
|
|
|
|
|
|
if (Unused)
|
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(OldArg);
|
|
|
|
}
|
|
|
|
|
2017-04-29 08:23:11 +08:00
|
|
|
/// If Inst is a ReturnRV and its operand is a call or invoke, return the
|
|
|
|
/// operand. Otherwise return null.
|
|
|
|
static inline const Instruction *getreturnRVOperand(const Instruction &Inst,
|
|
|
|
ARCInstKind Class) {
|
|
|
|
if (Class != ARCInstKind::RetainRV)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const auto *Opnd = Inst.getOperand(0)->stripPointerCasts();
|
|
|
|
if (const auto *C = dyn_cast<CallInst>(Opnd))
|
|
|
|
return C;
|
|
|
|
return dyn_cast<InvokeInst>(Opnd);
|
|
|
|
}
|
|
|
|
|
2018-01-20 07:51:13 +08:00
|
|
|
/// Return the list of PHI nodes that are equivalent to PN.
|
|
|
|
template<class PHINodeTy, class VectorTy>
|
|
|
|
void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList) {
|
|
|
|
auto *BB = PN.getParent();
|
|
|
|
for (auto &P : BB->phis()) {
|
|
|
|
if (&P == &PN) // Do not add PN to the list.
|
|
|
|
continue;
|
|
|
|
unsigned I = 0, E = PN.getNumIncomingValues();
|
|
|
|
for (; I < E; ++I) {
|
|
|
|
auto *BB = PN.getIncomingBlock(I);
|
|
|
|
auto *PNOpnd = PN.getIncomingValue(I)->stripPointerCasts();
|
|
|
|
auto *POpnd = P.getIncomingValueForBlock(BB)->stripPointerCasts();
|
|
|
|
if (PNOpnd != POpnd)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (I == E)
|
|
|
|
PHIList.push_back(&P);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-28 11:28:38 +08:00
|
|
|
} // end namespace objcarc
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#endif
|