mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-06 23:32:27 +00:00
Extracted pass ObjCARCExpand from ObjCARC.cpp => ObjCARCExpand.cpp.
I also added the local header ObjCARC.h for common functions used by the various passes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173651 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
410b46a390
commit
6504255a22
@ -1,6 +1,7 @@
|
|||||||
add_llvm_library(LLVMObjCARCOpts
|
add_llvm_library(LLVMObjCARCOpts
|
||||||
ObjCARC.cpp
|
ObjCARC.cpp
|
||||||
ObjCARCOpts.cpp
|
ObjCARCOpts.cpp
|
||||||
|
ObjCARCExpand.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_dependencies(LLVMObjCARCOpts intrinsics_gen)
|
add_dependencies(LLVMObjCARCOpts intrinsics_gen)
|
||||||
|
@ -13,15 +13,25 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "llvm/Transforms/ObjCARC.h"
|
#include "ObjCARC.h"
|
||||||
|
|
||||||
#include "llvm-c/Initialization.h"
|
#include "llvm-c/Initialization.h"
|
||||||
#include "llvm/Analysis/Passes.h"
|
#include "llvm/Analysis/Passes.h"
|
||||||
#include "llvm/Analysis/Verifier.h"
|
#include "llvm/Analysis/Verifier.h"
|
||||||
#include "llvm/IR/DataLayout.h"
|
#include "llvm/IR/DataLayout.h"
|
||||||
#include "llvm/InitializePasses.h"
|
#include "llvm/InitializePasses.h"
|
||||||
#include "llvm/PassManager.h"
|
#include "llvm/PassManager.h"
|
||||||
|
#include "llvm/Support/Commandline.h"
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
using namespace llvm::objcarc;
|
||||||
|
|
||||||
|
/// \brief A handy option to enable/disable all ARC Optimizations.
|
||||||
|
bool llvm::objcarc::EnableARCOpts;
|
||||||
|
static cl::opt<bool, true>
|
||||||
|
EnableARCOptimizations("enable-objc-arc-opts",
|
||||||
|
cl::location(EnableARCOpts),
|
||||||
|
cl::init(true));
|
||||||
|
|
||||||
/// initializeObjCARCOptsPasses - Initialize all passes linked into the
|
/// initializeObjCARCOptsPasses - Initialize all passes linked into the
|
||||||
/// ObjCARCOpts library.
|
/// ObjCARCOpts library.
|
||||||
|
242
lib/Transforms/ObjCARC/ObjCARC.h
Normal file
242
lib/Transforms/ObjCARC/ObjCARC.h
Normal file
@ -0,0 +1,242 @@
|
|||||||
|
//===- ObjCARC.h - ObjC ARC Optimization --------------*- mode: c++ -*-----===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
/// \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.
|
||||||
|
///
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#ifndef LLVM_TRANSFORMS_SCALAR_OBJCARC_H
|
||||||
|
#define LLVM_TRANSFORMS_SCALAR_OBJCARC_H
|
||||||
|
|
||||||
|
#include "llvm/ADT/StringSwitch.h"
|
||||||
|
#include "llvm/Analysis/AliasAnalysis.h"
|
||||||
|
#include "llvm/Analysis/Passes.h"
|
||||||
|
#include "llvm/IR/Module.h"
|
||||||
|
#include "llvm/Pass.h"
|
||||||
|
#include "llvm/Support/Debug.h"
|
||||||
|
#include "llvm/Support/InstIterator.h"
|
||||||
|
#include "llvm/Support/raw_ostream.h"
|
||||||
|
#include "llvm/Transforms/ObjCARC.h"
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
namespace objcarc {
|
||||||
|
|
||||||
|
/// \brief A handy option to enable/disable all ARC Optimizations.
|
||||||
|
extern bool EnableARCOpts;
|
||||||
|
|
||||||
|
/// \brief Test if the given module looks interesting to run ARC optimization
|
||||||
|
/// on.
|
||||||
|
static inline bool ModuleHasARC(const Module &M) {
|
||||||
|
return
|
||||||
|
M.getNamedValue("objc_retain") ||
|
||||||
|
M.getNamedValue("objc_release") ||
|
||||||
|
M.getNamedValue("objc_autorelease") ||
|
||||||
|
M.getNamedValue("objc_retainAutoreleasedReturnValue") ||
|
||||||
|
M.getNamedValue("objc_retainBlock") ||
|
||||||
|
M.getNamedValue("objc_autoreleaseReturnValue") ||
|
||||||
|
M.getNamedValue("objc_autoreleasePoolPush") ||
|
||||||
|
M.getNamedValue("objc_loadWeakRetained") ||
|
||||||
|
M.getNamedValue("objc_loadWeak") ||
|
||||||
|
M.getNamedValue("objc_destroyWeak") ||
|
||||||
|
M.getNamedValue("objc_storeWeak") ||
|
||||||
|
M.getNamedValue("objc_initWeak") ||
|
||||||
|
M.getNamedValue("objc_moveWeak") ||
|
||||||
|
M.getNamedValue("objc_copyWeak") ||
|
||||||
|
M.getNamedValue("objc_retainedObject") ||
|
||||||
|
M.getNamedValue("objc_unretainedObject") ||
|
||||||
|
M.getNamedValue("objc_unretainedPointer");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \enum InstructionClass
|
||||||
|
/// \brief A simple classification for instructions.
|
||||||
|
enum InstructionClass {
|
||||||
|
IC_Retain, ///< objc_retain
|
||||||
|
IC_RetainRV, ///< objc_retainAutoreleasedReturnValue
|
||||||
|
IC_RetainBlock, ///< objc_retainBlock
|
||||||
|
IC_Release, ///< objc_release
|
||||||
|
IC_Autorelease, ///< objc_autorelease
|
||||||
|
IC_AutoreleaseRV, ///< objc_autoreleaseReturnValue
|
||||||
|
IC_AutoreleasepoolPush, ///< objc_autoreleasePoolPush
|
||||||
|
IC_AutoreleasepoolPop, ///< objc_autoreleasePoolPop
|
||||||
|
IC_NoopCast, ///< objc_retainedObject, etc.
|
||||||
|
IC_FusedRetainAutorelease, ///< objc_retainAutorelease
|
||||||
|
IC_FusedRetainAutoreleaseRV, ///< objc_retainAutoreleaseReturnValue
|
||||||
|
IC_LoadWeakRetained, ///< objc_loadWeakRetained (primitive)
|
||||||
|
IC_StoreWeak, ///< objc_storeWeak (primitive)
|
||||||
|
IC_InitWeak, ///< objc_initWeak (derived)
|
||||||
|
IC_LoadWeak, ///< objc_loadWeak (derived)
|
||||||
|
IC_MoveWeak, ///< objc_moveWeak (derived)
|
||||||
|
IC_CopyWeak, ///< objc_copyWeak (derived)
|
||||||
|
IC_DestroyWeak, ///< objc_destroyWeak (derived)
|
||||||
|
IC_StoreStrong, ///< objc_storeStrong (derived)
|
||||||
|
IC_CallOrUser, ///< could call objc_release and/or "use" pointers
|
||||||
|
IC_Call, ///< could call objc_release
|
||||||
|
IC_User, ///< could "use" a pointer
|
||||||
|
IC_None ///< anything else
|
||||||
|
};
|
||||||
|
|
||||||
|
static raw_ostream &operator<<(raw_ostream &OS, const InstructionClass Class)
|
||||||
|
LLVM_ATTRIBUTE_USED;
|
||||||
|
|
||||||
|
static raw_ostream &operator<<(raw_ostream &OS, const InstructionClass Class) {
|
||||||
|
switch (Class) {
|
||||||
|
case IC_Retain:
|
||||||
|
return OS << "IC_Retain";
|
||||||
|
case IC_RetainRV:
|
||||||
|
return OS << "IC_RetainRV";
|
||||||
|
case IC_RetainBlock:
|
||||||
|
return OS << "IC_RetainBlock";
|
||||||
|
case IC_Release:
|
||||||
|
return OS << "IC_Release";
|
||||||
|
case IC_Autorelease:
|
||||||
|
return OS << "IC_Autorelease";
|
||||||
|
case IC_AutoreleaseRV:
|
||||||
|
return OS << "IC_AutoreleaseRV";
|
||||||
|
case IC_AutoreleasepoolPush:
|
||||||
|
return OS << "IC_AutoreleasepoolPush";
|
||||||
|
case IC_AutoreleasepoolPop:
|
||||||
|
return OS << "IC_AutoreleasepoolPop";
|
||||||
|
case IC_NoopCast:
|
||||||
|
return OS << "IC_NoopCast";
|
||||||
|
case IC_FusedRetainAutorelease:
|
||||||
|
return OS << "IC_FusedRetainAutorelease";
|
||||||
|
case IC_FusedRetainAutoreleaseRV:
|
||||||
|
return OS << "IC_FusedRetainAutoreleaseRV";
|
||||||
|
case IC_LoadWeakRetained:
|
||||||
|
return OS << "IC_LoadWeakRetained";
|
||||||
|
case IC_StoreWeak:
|
||||||
|
return OS << "IC_StoreWeak";
|
||||||
|
case IC_InitWeak:
|
||||||
|
return OS << "IC_InitWeak";
|
||||||
|
case IC_LoadWeak:
|
||||||
|
return OS << "IC_LoadWeak";
|
||||||
|
case IC_MoveWeak:
|
||||||
|
return OS << "IC_MoveWeak";
|
||||||
|
case IC_CopyWeak:
|
||||||
|
return OS << "IC_CopyWeak";
|
||||||
|
case IC_DestroyWeak:
|
||||||
|
return OS << "IC_DestroyWeak";
|
||||||
|
case IC_StoreStrong:
|
||||||
|
return OS << "IC_StoreStrong";
|
||||||
|
case IC_CallOrUser:
|
||||||
|
return OS << "IC_CallOrUser";
|
||||||
|
case IC_Call:
|
||||||
|
return OS << "IC_Call";
|
||||||
|
case IC_User:
|
||||||
|
return OS << "IC_User";
|
||||||
|
case IC_None:
|
||||||
|
return OS << "IC_None";
|
||||||
|
}
|
||||||
|
llvm_unreachable("Unknown instruction class!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// \brief Determine if F is one of the special known Functions. If it isn't,
|
||||||
|
/// return IC_CallOrUser.
|
||||||
|
static inline InstructionClass GetFunctionClass(const Function *F) {
|
||||||
|
Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
|
||||||
|
|
||||||
|
// No arguments.
|
||||||
|
if (AI == AE)
|
||||||
|
return StringSwitch<InstructionClass>(F->getName())
|
||||||
|
.Case("objc_autoreleasePoolPush", IC_AutoreleasepoolPush)
|
||||||
|
.Default(IC_CallOrUser);
|
||||||
|
|
||||||
|
// One argument.
|
||||||
|
const Argument *A0 = AI++;
|
||||||
|
if (AI == AE)
|
||||||
|
// Argument is a pointer.
|
||||||
|
if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
|
||||||
|
Type *ETy = PTy->getElementType();
|
||||||
|
// Argument is i8*.
|
||||||
|
if (ETy->isIntegerTy(8))
|
||||||
|
return StringSwitch<InstructionClass>(F->getName())
|
||||||
|
.Case("objc_retain", IC_Retain)
|
||||||
|
.Case("objc_retainAutoreleasedReturnValue", IC_RetainRV)
|
||||||
|
.Case("objc_retainBlock", IC_RetainBlock)
|
||||||
|
.Case("objc_release", IC_Release)
|
||||||
|
.Case("objc_autorelease", IC_Autorelease)
|
||||||
|
.Case("objc_autoreleaseReturnValue", IC_AutoreleaseRV)
|
||||||
|
.Case("objc_autoreleasePoolPop", IC_AutoreleasepoolPop)
|
||||||
|
.Case("objc_retainedObject", IC_NoopCast)
|
||||||
|
.Case("objc_unretainedObject", IC_NoopCast)
|
||||||
|
.Case("objc_unretainedPointer", IC_NoopCast)
|
||||||
|
.Case("objc_retain_autorelease", IC_FusedRetainAutorelease)
|
||||||
|
.Case("objc_retainAutorelease", IC_FusedRetainAutorelease)
|
||||||
|
.Case("objc_retainAutoreleaseReturnValue",IC_FusedRetainAutoreleaseRV)
|
||||||
|
.Default(IC_CallOrUser);
|
||||||
|
|
||||||
|
// Argument is i8**
|
||||||
|
if (PointerType *Pte = dyn_cast<PointerType>(ETy))
|
||||||
|
if (Pte->getElementType()->isIntegerTy(8))
|
||||||
|
return StringSwitch<InstructionClass>(F->getName())
|
||||||
|
.Case("objc_loadWeakRetained", IC_LoadWeakRetained)
|
||||||
|
.Case("objc_loadWeak", IC_LoadWeak)
|
||||||
|
.Case("objc_destroyWeak", IC_DestroyWeak)
|
||||||
|
.Default(IC_CallOrUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Two arguments, first is i8**.
|
||||||
|
const Argument *A1 = AI++;
|
||||||
|
if (AI == AE)
|
||||||
|
if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
|
||||||
|
if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
|
||||||
|
if (Pte->getElementType()->isIntegerTy(8))
|
||||||
|
if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
|
||||||
|
Type *ETy1 = PTy1->getElementType();
|
||||||
|
// Second argument is i8*
|
||||||
|
if (ETy1->isIntegerTy(8))
|
||||||
|
return StringSwitch<InstructionClass>(F->getName())
|
||||||
|
.Case("objc_storeWeak", IC_StoreWeak)
|
||||||
|
.Case("objc_initWeak", IC_InitWeak)
|
||||||
|
.Case("objc_storeStrong", IC_StoreStrong)
|
||||||
|
.Default(IC_CallOrUser);
|
||||||
|
// Second argument is i8**.
|
||||||
|
if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
|
||||||
|
if (Pte1->getElementType()->isIntegerTy(8))
|
||||||
|
return StringSwitch<InstructionClass>(F->getName())
|
||||||
|
.Case("objc_moveWeak", IC_MoveWeak)
|
||||||
|
.Case("objc_copyWeak", IC_CopyWeak)
|
||||||
|
.Default(IC_CallOrUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Anything else.
|
||||||
|
return IC_CallOrUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \brief Determine which objc runtime call instruction class V belongs to.
|
||||||
|
///
|
||||||
|
/// This is similar to GetInstructionClass except that it only detects objc
|
||||||
|
/// runtime calls. This allows it to be faster.
|
||||||
|
///
|
||||||
|
static inline InstructionClass GetBasicInstructionClass(const Value *V) {
|
||||||
|
if (const CallInst *CI = dyn_cast<CallInst>(V)) {
|
||||||
|
if (const Function *F = CI->getCalledFunction())
|
||||||
|
return GetFunctionClass(F);
|
||||||
|
// Otherwise, be conservative.
|
||||||
|
return IC_CallOrUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, be conservative.
|
||||||
|
return isa<InvokeInst>(V) ? IC_CallOrUser : IC_User;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end namespace objcarc
|
||||||
|
} // end namespace llvm
|
||||||
|
|
||||||
|
#endif // LLVM_TRANSFORMS_SCALAR_OBJCARC_H
|
113
lib/Transforms/ObjCARC/ObjCARCExpand.cpp
Normal file
113
lib/Transforms/ObjCARC/ObjCARCExpand.cpp
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
//===- ObjCARCExpand.cpp - ObjC ARC Optimization --------------------------===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
/// \file
|
||||||
|
/// This file defines ObjC ARC optimizations. ARC stands for Automatic
|
||||||
|
/// Reference Counting and is a system for managing reference counts for objects
|
||||||
|
/// in Objective C.
|
||||||
|
///
|
||||||
|
/// This specific file deals with early optimizations which perform certain
|
||||||
|
/// cleanup operations.
|
||||||
|
///
|
||||||
|
/// 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.
|
||||||
|
///
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#define DEBUG_TYPE "objc-arc-expand"
|
||||||
|
#include "ObjCARC.h"
|
||||||
|
|
||||||
|
using namespace llvm;
|
||||||
|
using namespace llvm::objcarc;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
/// \brief Early ARC transformations.
|
||||||
|
class ObjCARCExpand : public FunctionPass {
|
||||||
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
||||||
|
virtual bool doInitialization(Module &M);
|
||||||
|
virtual bool runOnFunction(Function &F);
|
||||||
|
|
||||||
|
/// A flag indicating whether this optimization pass should run.
|
||||||
|
bool Run;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static char ID;
|
||||||
|
ObjCARCExpand() : FunctionPass(ID) {
|
||||||
|
initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
char ObjCARCExpand::ID = 0;
|
||||||
|
INITIALIZE_PASS(ObjCARCExpand,
|
||||||
|
"objc-arc-expand", "ObjC ARC expansion", false, false)
|
||||||
|
|
||||||
|
Pass *llvm::createObjCARCExpandPass() {
|
||||||
|
return new ObjCARCExpand();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
|
||||||
|
AU.setPreservesCFG();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ObjCARCExpand::doInitialization(Module &M) {
|
||||||
|
Run = ModuleHasARC(M);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ObjCARCExpand::runOnFunction(Function &F) {
|
||||||
|
if (!EnableARCOpts)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// If nothing in the Module uses ARC, don't do anything.
|
||||||
|
if (!Run)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
bool Changed = false;
|
||||||
|
|
||||||
|
DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() << "\n");
|
||||||
|
|
||||||
|
for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
|
||||||
|
Instruction *Inst = &*I;
|
||||||
|
|
||||||
|
DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
|
||||||
|
|
||||||
|
switch (GetBasicInstructionClass(Inst)) {
|
||||||
|
case IC_Retain:
|
||||||
|
case IC_RetainRV:
|
||||||
|
case IC_Autorelease:
|
||||||
|
case IC_AutoreleaseRV:
|
||||||
|
case IC_FusedRetainAutorelease:
|
||||||
|
case IC_FusedRetainAutoreleaseRV: {
|
||||||
|
// These calls return their argument verbatim, as a low-level
|
||||||
|
// optimization. However, this makes high-level optimizations
|
||||||
|
// harder. Undo any uses of this optimization that the front-end
|
||||||
|
// emitted here. We'll redo them in the contract pass.
|
||||||
|
Changed = true;
|
||||||
|
Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
|
||||||
|
DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n"
|
||||||
|
" New = " << *Value << "\n");
|
||||||
|
Inst->replaceAllUsesWith(Value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
|
||||||
|
|
||||||
|
return Changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @}
|
||||||
|
///
|
@ -28,16 +28,12 @@
|
|||||||
///
|
///
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#define DEBUG_TYPE "objc-arc"
|
#define DEBUG_TYPE "objc-arc-opts"
|
||||||
|
#include "ObjCARC.h"
|
||||||
#include "llvm/ADT/DenseMap.h"
|
#include "llvm/ADT/DenseMap.h"
|
||||||
#include "llvm/ADT/SmallPtrSet.h"
|
#include "llvm/ADT/SmallPtrSet.h"
|
||||||
#include "llvm/Support/CommandLine.h"
|
|
||||||
#include "llvm/Support/Debug.h"
|
|
||||||
#include "llvm/Support/raw_ostream.h"
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
using namespace llvm::objcarc;
|
||||||
/// \brief A handy option to enable/disable all optimizations in this file.
|
|
||||||
static cl::opt<bool> EnableARCOpts("enable-objc-arc-opts", cl::init(true));
|
|
||||||
|
|
||||||
/// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
|
/// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
|
||||||
/// @{
|
/// @{
|
||||||
@ -132,97 +128,11 @@ namespace {
|
|||||||
/// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
|
/// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
|
||||||
/// @{
|
/// @{
|
||||||
|
|
||||||
#include "llvm/ADT/StringSwitch.h"
|
|
||||||
#include "llvm/Analysis/ValueTracking.h"
|
#include "llvm/Analysis/ValueTracking.h"
|
||||||
#include "llvm/IR/Intrinsics.h"
|
#include "llvm/IR/Intrinsics.h"
|
||||||
#include "llvm/IR/Module.h"
|
|
||||||
#include "llvm/Support/CallSite.h"
|
#include "llvm/Support/CallSite.h"
|
||||||
#include "llvm/Transforms/Utils/Local.h"
|
#include "llvm/Transforms/Utils/Local.h"
|
||||||
|
|
||||||
namespace {
|
|
||||||
/// \enum InstructionClass
|
|
||||||
/// \brief A simple classification for instructions.
|
|
||||||
enum InstructionClass {
|
|
||||||
IC_Retain, ///< objc_retain
|
|
||||||
IC_RetainRV, ///< objc_retainAutoreleasedReturnValue
|
|
||||||
IC_RetainBlock, ///< objc_retainBlock
|
|
||||||
IC_Release, ///< objc_release
|
|
||||||
IC_Autorelease, ///< objc_autorelease
|
|
||||||
IC_AutoreleaseRV, ///< objc_autoreleaseReturnValue
|
|
||||||
IC_AutoreleasepoolPush, ///< objc_autoreleasePoolPush
|
|
||||||
IC_AutoreleasepoolPop, ///< objc_autoreleasePoolPop
|
|
||||||
IC_NoopCast, ///< objc_retainedObject, etc.
|
|
||||||
IC_FusedRetainAutorelease, ///< objc_retainAutorelease
|
|
||||||
IC_FusedRetainAutoreleaseRV, ///< objc_retainAutoreleaseReturnValue
|
|
||||||
IC_LoadWeakRetained, ///< objc_loadWeakRetained (primitive)
|
|
||||||
IC_StoreWeak, ///< objc_storeWeak (primitive)
|
|
||||||
IC_InitWeak, ///< objc_initWeak (derived)
|
|
||||||
IC_LoadWeak, ///< objc_loadWeak (derived)
|
|
||||||
IC_MoveWeak, ///< objc_moveWeak (derived)
|
|
||||||
IC_CopyWeak, ///< objc_copyWeak (derived)
|
|
||||||
IC_DestroyWeak, ///< objc_destroyWeak (derived)
|
|
||||||
IC_StoreStrong, ///< objc_storeStrong (derived)
|
|
||||||
IC_CallOrUser, ///< could call objc_release and/or "use" pointers
|
|
||||||
IC_Call, ///< could call objc_release
|
|
||||||
IC_User, ///< could "use" a pointer
|
|
||||||
IC_None ///< anything else
|
|
||||||
};
|
|
||||||
|
|
||||||
raw_ostream &operator<<(raw_ostream &OS, const InstructionClass Class)
|
|
||||||
LLVM_ATTRIBUTE_USED;
|
|
||||||
raw_ostream &operator<<(raw_ostream &OS, const InstructionClass Class) {
|
|
||||||
switch (Class) {
|
|
||||||
case IC_Retain:
|
|
||||||
return OS << "IC_Retain";
|
|
||||||
case IC_RetainRV:
|
|
||||||
return OS << "IC_RetainRV";
|
|
||||||
case IC_RetainBlock:
|
|
||||||
return OS << "IC_RetainBlock";
|
|
||||||
case IC_Release:
|
|
||||||
return OS << "IC_Release";
|
|
||||||
case IC_Autorelease:
|
|
||||||
return OS << "IC_Autorelease";
|
|
||||||
case IC_AutoreleaseRV:
|
|
||||||
return OS << "IC_AutoreleaseRV";
|
|
||||||
case IC_AutoreleasepoolPush:
|
|
||||||
return OS << "IC_AutoreleasepoolPush";
|
|
||||||
case IC_AutoreleasepoolPop:
|
|
||||||
return OS << "IC_AutoreleasepoolPop";
|
|
||||||
case IC_NoopCast:
|
|
||||||
return OS << "IC_NoopCast";
|
|
||||||
case IC_FusedRetainAutorelease:
|
|
||||||
return OS << "IC_FusedRetainAutorelease";
|
|
||||||
case IC_FusedRetainAutoreleaseRV:
|
|
||||||
return OS << "IC_FusedRetainAutoreleaseRV";
|
|
||||||
case IC_LoadWeakRetained:
|
|
||||||
return OS << "IC_LoadWeakRetained";
|
|
||||||
case IC_StoreWeak:
|
|
||||||
return OS << "IC_StoreWeak";
|
|
||||||
case IC_InitWeak:
|
|
||||||
return OS << "IC_InitWeak";
|
|
||||||
case IC_LoadWeak:
|
|
||||||
return OS << "IC_LoadWeak";
|
|
||||||
case IC_MoveWeak:
|
|
||||||
return OS << "IC_MoveWeak";
|
|
||||||
case IC_CopyWeak:
|
|
||||||
return OS << "IC_CopyWeak";
|
|
||||||
case IC_DestroyWeak:
|
|
||||||
return OS << "IC_DestroyWeak";
|
|
||||||
case IC_StoreStrong:
|
|
||||||
return OS << "IC_StoreStrong";
|
|
||||||
case IC_CallOrUser:
|
|
||||||
return OS << "IC_CallOrUser";
|
|
||||||
case IC_Call:
|
|
||||||
return OS << "IC_Call";
|
|
||||||
case IC_User:
|
|
||||||
return OS << "IC_User";
|
|
||||||
case IC_None:
|
|
||||||
return OS << "IC_None";
|
|
||||||
}
|
|
||||||
llvm_unreachable("Unknown instruction class!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Test whether the given value is possible a retainable object pointer.
|
/// \brief Test whether the given value is possible a retainable object pointer.
|
||||||
static bool IsPotentialRetainableObjPtr(const Value *Op) {
|
static bool IsPotentialRetainableObjPtr(const Value *Op) {
|
||||||
// Pointers to static or stack storage are not valid retainable object pointers.
|
// Pointers to static or stack storage are not valid retainable object pointers.
|
||||||
@ -257,79 +167,6 @@ static InstructionClass GetCallSiteClass(ImmutableCallSite CS) {
|
|||||||
return CS.onlyReadsMemory() ? IC_None : IC_Call;
|
return CS.onlyReadsMemory() ? IC_None : IC_Call;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Determine if F is one of the special known Functions. If it isn't,
|
|
||||||
/// return IC_CallOrUser.
|
|
||||||
static InstructionClass GetFunctionClass(const Function *F) {
|
|
||||||
Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
|
|
||||||
|
|
||||||
// No arguments.
|
|
||||||
if (AI == AE)
|
|
||||||
return StringSwitch<InstructionClass>(F->getName())
|
|
||||||
.Case("objc_autoreleasePoolPush", IC_AutoreleasepoolPush)
|
|
||||||
.Default(IC_CallOrUser);
|
|
||||||
|
|
||||||
// One argument.
|
|
||||||
const Argument *A0 = AI++;
|
|
||||||
if (AI == AE)
|
|
||||||
// Argument is a pointer.
|
|
||||||
if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
|
|
||||||
Type *ETy = PTy->getElementType();
|
|
||||||
// Argument is i8*.
|
|
||||||
if (ETy->isIntegerTy(8))
|
|
||||||
return StringSwitch<InstructionClass>(F->getName())
|
|
||||||
.Case("objc_retain", IC_Retain)
|
|
||||||
.Case("objc_retainAutoreleasedReturnValue", IC_RetainRV)
|
|
||||||
.Case("objc_retainBlock", IC_RetainBlock)
|
|
||||||
.Case("objc_release", IC_Release)
|
|
||||||
.Case("objc_autorelease", IC_Autorelease)
|
|
||||||
.Case("objc_autoreleaseReturnValue", IC_AutoreleaseRV)
|
|
||||||
.Case("objc_autoreleasePoolPop", IC_AutoreleasepoolPop)
|
|
||||||
.Case("objc_retainedObject", IC_NoopCast)
|
|
||||||
.Case("objc_unretainedObject", IC_NoopCast)
|
|
||||||
.Case("objc_unretainedPointer", IC_NoopCast)
|
|
||||||
.Case("objc_retain_autorelease", IC_FusedRetainAutorelease)
|
|
||||||
.Case("objc_retainAutorelease", IC_FusedRetainAutorelease)
|
|
||||||
.Case("objc_retainAutoreleaseReturnValue",IC_FusedRetainAutoreleaseRV)
|
|
||||||
.Default(IC_CallOrUser);
|
|
||||||
|
|
||||||
// Argument is i8**
|
|
||||||
if (PointerType *Pte = dyn_cast<PointerType>(ETy))
|
|
||||||
if (Pte->getElementType()->isIntegerTy(8))
|
|
||||||
return StringSwitch<InstructionClass>(F->getName())
|
|
||||||
.Case("objc_loadWeakRetained", IC_LoadWeakRetained)
|
|
||||||
.Case("objc_loadWeak", IC_LoadWeak)
|
|
||||||
.Case("objc_destroyWeak", IC_DestroyWeak)
|
|
||||||
.Default(IC_CallOrUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Two arguments, first is i8**.
|
|
||||||
const Argument *A1 = AI++;
|
|
||||||
if (AI == AE)
|
|
||||||
if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
|
|
||||||
if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
|
|
||||||
if (Pte->getElementType()->isIntegerTy(8))
|
|
||||||
if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
|
|
||||||
Type *ETy1 = PTy1->getElementType();
|
|
||||||
// Second argument is i8*
|
|
||||||
if (ETy1->isIntegerTy(8))
|
|
||||||
return StringSwitch<InstructionClass>(F->getName())
|
|
||||||
.Case("objc_storeWeak", IC_StoreWeak)
|
|
||||||
.Case("objc_initWeak", IC_InitWeak)
|
|
||||||
.Case("objc_storeStrong", IC_StoreStrong)
|
|
||||||
.Default(IC_CallOrUser);
|
|
||||||
// Second argument is i8**.
|
|
||||||
if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
|
|
||||||
if (Pte1->getElementType()->isIntegerTy(8))
|
|
||||||
return StringSwitch<InstructionClass>(F->getName())
|
|
||||||
.Case("objc_moveWeak", IC_MoveWeak)
|
|
||||||
.Case("objc_copyWeak", IC_CopyWeak)
|
|
||||||
.Default(IC_CallOrUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Anything else.
|
|
||||||
return IC_CallOrUser;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Determine what kind of construct V is.
|
/// \brief Determine what kind of construct V is.
|
||||||
static InstructionClass GetInstructionClass(const Value *V) {
|
static InstructionClass GetInstructionClass(const Value *V) {
|
||||||
if (const Instruction *I = dyn_cast<Instruction>(V)) {
|
if (const Instruction *I = dyn_cast<Instruction>(V)) {
|
||||||
@ -420,23 +257,6 @@ static InstructionClass GetInstructionClass(const Value *V) {
|
|||||||
return IC_None;
|
return IC_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Determine which objc runtime call instruction class V belongs to.
|
|
||||||
///
|
|
||||||
/// This is similar to GetInstructionClass except that it only detects objc
|
|
||||||
/// runtime calls. This allows it to be faster.
|
|
||||||
///
|
|
||||||
static InstructionClass GetBasicInstructionClass(const Value *V) {
|
|
||||||
if (const CallInst *CI = dyn_cast<CallInst>(V)) {
|
|
||||||
if (const Function *F = CI->getCalledFunction())
|
|
||||||
return GetFunctionClass(F);
|
|
||||||
// Otherwise, be conservative.
|
|
||||||
return IC_CallOrUser;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, be conservative.
|
|
||||||
return isa<InvokeInst>(V) ? IC_CallOrUser : IC_User;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Test if the given class is objc_retain or equivalent.
|
/// \brief Test if the given class is objc_retain or equivalent.
|
||||||
static bool IsRetain(InstructionClass Class) {
|
static bool IsRetain(InstructionClass Class) {
|
||||||
return Class == IC_Retain ||
|
return Class == IC_Retain ||
|
||||||
@ -648,29 +468,6 @@ static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Test if the given module looks interesting to run ARC optimization
|
|
||||||
/// on.
|
|
||||||
static bool ModuleHasARC(const Module &M) {
|
|
||||||
return
|
|
||||||
M.getNamedValue("objc_retain") ||
|
|
||||||
M.getNamedValue("objc_release") ||
|
|
||||||
M.getNamedValue("objc_autorelease") ||
|
|
||||||
M.getNamedValue("objc_retainAutoreleasedReturnValue") ||
|
|
||||||
M.getNamedValue("objc_retainBlock") ||
|
|
||||||
M.getNamedValue("objc_autoreleaseReturnValue") ||
|
|
||||||
M.getNamedValue("objc_autoreleasePoolPush") ||
|
|
||||||
M.getNamedValue("objc_loadWeakRetained") ||
|
|
||||||
M.getNamedValue("objc_loadWeak") ||
|
|
||||||
M.getNamedValue("objc_destroyWeak") ||
|
|
||||||
M.getNamedValue("objc_storeWeak") ||
|
|
||||||
M.getNamedValue("objc_initWeak") ||
|
|
||||||
M.getNamedValue("objc_moveWeak") ||
|
|
||||||
M.getNamedValue("objc_copyWeak") ||
|
|
||||||
M.getNamedValue("objc_retainedObject") ||
|
|
||||||
M.getNamedValue("objc_unretainedObject") ||
|
|
||||||
M.getNamedValue("objc_unretainedPointer");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Test whether the given pointer, which is an Objective C block
|
/// \brief Test whether the given pointer, which is an Objective C block
|
||||||
/// pointer, does not "escape".
|
/// pointer, does not "escape".
|
||||||
///
|
///
|
||||||
@ -756,10 +553,6 @@ static bool DoesObjCBlockEscape(const Value *BlockPtr) {
|
|||||||
/// \defgroup ARCAA Extends alias analysis using ObjC specific knowledge.
|
/// \defgroup ARCAA Extends alias analysis using ObjC specific knowledge.
|
||||||
/// @{
|
/// @{
|
||||||
|
|
||||||
#include "llvm/Analysis/AliasAnalysis.h"
|
|
||||||
#include "llvm/Analysis/Passes.h"
|
|
||||||
#include "llvm/Pass.h"
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
/// \brief This is a simple alias analysis implementation that uses knowledge
|
/// \brief This is a simple alias analysis implementation that uses knowledge
|
||||||
/// of ARC constructs to answer queries.
|
/// of ARC constructs to answer queries.
|
||||||
@ -926,94 +719,6 @@ ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
|
|||||||
return AliasAnalysis::getModRefInfo(CS1, CS2);
|
return AliasAnalysis::getModRefInfo(CS1, CS2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @}
|
|
||||||
///
|
|
||||||
/// \defgroup ARCExpansion Early ARC Optimizations.
|
|
||||||
/// @{
|
|
||||||
|
|
||||||
#include "llvm/Support/InstIterator.h"
|
|
||||||
#include "llvm/Transforms/ObjCARC.h"
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
/// \brief Early ARC transformations.
|
|
||||||
class ObjCARCExpand : public FunctionPass {
|
|
||||||
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
||||||
virtual bool doInitialization(Module &M);
|
|
||||||
virtual bool runOnFunction(Function &F);
|
|
||||||
|
|
||||||
/// A flag indicating whether this optimization pass should run.
|
|
||||||
bool Run;
|
|
||||||
|
|
||||||
public:
|
|
||||||
static char ID;
|
|
||||||
ObjCARCExpand() : FunctionPass(ID) {
|
|
||||||
initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
char ObjCARCExpand::ID = 0;
|
|
||||||
INITIALIZE_PASS(ObjCARCExpand,
|
|
||||||
"objc-arc-expand", "ObjC ARC expansion", false, false)
|
|
||||||
|
|
||||||
Pass *llvm::createObjCARCExpandPass() {
|
|
||||||
return new ObjCARCExpand();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
||||||
AU.setPreservesCFG();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ObjCARCExpand::doInitialization(Module &M) {
|
|
||||||
Run = ModuleHasARC(M);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ObjCARCExpand::runOnFunction(Function &F) {
|
|
||||||
if (!EnableARCOpts)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// If nothing in the Module uses ARC, don't do anything.
|
|
||||||
if (!Run)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
bool Changed = false;
|
|
||||||
|
|
||||||
DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() << "\n");
|
|
||||||
|
|
||||||
for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
|
|
||||||
Instruction *Inst = &*I;
|
|
||||||
|
|
||||||
DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
|
|
||||||
|
|
||||||
switch (GetBasicInstructionClass(Inst)) {
|
|
||||||
case IC_Retain:
|
|
||||||
case IC_RetainRV:
|
|
||||||
case IC_Autorelease:
|
|
||||||
case IC_AutoreleaseRV:
|
|
||||||
case IC_FusedRetainAutorelease:
|
|
||||||
case IC_FusedRetainAutoreleaseRV: {
|
|
||||||
// These calls return their argument verbatim, as a low-level
|
|
||||||
// optimization. However, this makes high-level optimizations
|
|
||||||
// harder. Undo any uses of this optimization that the front-end
|
|
||||||
// emitted here. We'll redo them in the contract pass.
|
|
||||||
Changed = true;
|
|
||||||
Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
|
|
||||||
DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n"
|
|
||||||
" New = " << *Value << "\n");
|
|
||||||
Inst->replaceAllUsesWith(Value);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
|
|
||||||
|
|
||||||
return Changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
///
|
///
|
||||||
/// \defgroup ARCAPElim ARC Autorelease Pool Elimination.
|
/// \defgroup ARCAPElim ARC Autorelease Pool Elimination.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user