Skip to content

Commit ceaac7c

Browse files
author
Dan Gohman
committed
Completely short-circuit out ARC optimization if the ARC runtime
functions do not appear in the module. llvm-svn: 133478
1 parent 3343c13 commit ceaac7c

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

llvm/lib/Transforms/Scalar/ObjCARC.cpp

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/Intrinsics.h"
3434
#include "llvm/GlobalVariable.h"
3535
#include "llvm/DerivedTypes.h"
36+
#include "llvm/Module.h"
3637
#include "llvm/Analysis/ValueTracking.h"
3738
#include "llvm/Transforms/Utils/Local.h"
3839
#include "llvm/Support/CallSite.h"
@@ -564,6 +565,29 @@ static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
564565
return 0;
565566
}
566567

568+
/// ModuleHasARC - Test if the given module looks interesting to run ARC
569+
/// optimization on.
570+
static bool ModuleHasARC(const Module &M) {
571+
return
572+
M.getNamedValue("objc_retain") ||
573+
M.getNamedValue("objc_release") ||
574+
M.getNamedValue("objc_autorelease") ||
575+
M.getNamedValue("objc_retainAutoreleasedReturnValue") ||
576+
M.getNamedValue("objc_retainBlock") ||
577+
M.getNamedValue("objc_autoreleaseReturnValue") ||
578+
M.getNamedValue("objc_autoreleasePoolPush") ||
579+
M.getNamedValue("objc_loadWeakRetained") ||
580+
M.getNamedValue("objc_loadWeak") ||
581+
M.getNamedValue("objc_destroyWeak") ||
582+
M.getNamedValue("objc_storeWeak") ||
583+
M.getNamedValue("objc_initWeak") ||
584+
M.getNamedValue("objc_moveWeak") ||
585+
M.getNamedValue("objc_copyWeak") ||
586+
M.getNamedValue("objc_retainedObject") ||
587+
M.getNamedValue("objc_unretainedObject") ||
588+
M.getNamedValue("objc_unretainedPointer");
589+
}
590+
567591
//===----------------------------------------------------------------------===//
568592
// ARC AliasAnalysis.
569593
//===----------------------------------------------------------------------===//
@@ -749,8 +773,12 @@ namespace {
749773
/// ObjCARCExpand - Early ARC transformations.
750774
class ObjCARCExpand : public FunctionPass {
751775
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
776+
virtual bool doInitialization(Module &M);
752777
virtual bool runOnFunction(Function &F);
753778

779+
/// Run - A flag indicating whether this optimization pass should run.
780+
bool Run;
781+
754782
public:
755783
static char ID;
756784
ObjCARCExpand() : FunctionPass(ID) {
@@ -771,10 +799,19 @@ void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
771799
AU.setPreservesCFG();
772800
}
773801

802+
bool ObjCARCExpand::doInitialization(Module &M) {
803+
Run = ModuleHasARC(M);
804+
return false;
805+
}
806+
774807
bool ObjCARCExpand::runOnFunction(Function &F) {
775808
if (!EnableARCOpts)
776809
return false;
777810

811+
// If nothing in the Module uses ARC, don't do anything.
812+
if (!Run)
813+
return false;
814+
778815
bool Changed = false;
779816

780817
for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
@@ -840,8 +877,9 @@ bool ObjCARCExpand::runOnFunction(Function &F) {
840877
// usually can't sink them past other calls, which would be the main
841878
// case where it would be useful.
842879

880+
/// TODO: The pointer returned from objc_loadWeakRetained is retained.
881+
843882
#include "llvm/GlobalAlias.h"
844-
#include "llvm/Module.h"
845883
#include "llvm/Constants.h"
846884
#include "llvm/LLVMContext.h"
847885
#include "llvm/Support/ErrorHandling.h"
@@ -1365,10 +1403,12 @@ namespace {
13651403
bool Changed;
13661404
ProvenanceAnalysis PA;
13671405

1406+
/// Run - A flag indicating whether this optimization pass should run.
1407+
bool Run;
1408+
13681409
/// RetainFunc, RelaseFunc - Declarations for objc_retain,
13691410
/// objc_retainBlock, and objc_release.
1370-
Function *RetainFunc, *RetainBlockFunc, *RetainRVFunc, *ReleaseFunc,
1371-
*AutoreleaseFunc;
1411+
Function *RetainFunc, *RetainBlockFunc, *RetainRVFunc, *ReleaseFunc;
13721412

13731413
/// RetainRVCallee, etc. - Declarations for ObjC runtime
13741414
/// functions, for use in creating calls to them. These are initialized
@@ -3069,6 +3109,10 @@ bool ObjCARCOpt::doInitialization(Module &M) {
30693109
if (!EnableARCOpts)
30703110
return false;
30713111

3112+
Run = ModuleHasARC(M);
3113+
if (!Run)
3114+
return false;
3115+
30723116
// Identify the imprecise release metadata kind.
30733117
ImpreciseReleaseMDKind =
30743118
M.getContext().getMDKindID("clang.imprecise_release");
@@ -3078,7 +3122,6 @@ bool ObjCARCOpt::doInitialization(Module &M) {
30783122
RetainBlockFunc = M.getFunction("objc_retainBlock");
30793123
RetainRVFunc = M.getFunction("objc_retainAutoreleasedReturnValue");
30803124
ReleaseFunc = M.getFunction("objc_release");
3081-
AutoreleaseFunc = M.getFunction("objc_autorelease");
30823125

30833126
// Intuitively, objc_retain and others are nocapture, however in practice
30843127
// they are not, because they return their argument value. And objc_release
@@ -3098,6 +3141,10 @@ bool ObjCARCOpt::runOnFunction(Function &F) {
30983141
if (!EnableARCOpts)
30993142
return false;
31003143

3144+
// If nothing in the Module uses ARC, don't do anything.
3145+
if (!Run)
3146+
return false;
3147+
31013148
Changed = false;
31023149

31033150
PA.setAA(&getAnalysis<AliasAnalysis>());
@@ -3162,6 +3209,9 @@ namespace {
31623209
DominatorTree *DT;
31633210
ProvenanceAnalysis PA;
31643211

3212+
/// Run - A flag indicating whether this optimization pass should run.
3213+
bool Run;
3214+
31653215
/// StoreStrongCallee, etc. - Declarations for ObjC runtime
31663216
/// functions, for use in creating calls to them. These are initialized
31673217
/// lazily to avoid cluttering up the Module with unused declarations.
@@ -3384,6 +3434,10 @@ void ObjCARCContract::ContractRelease(Instruction *Release,
33843434
}
33853435

33863436
bool ObjCARCContract::doInitialization(Module &M) {
3437+
Run = ModuleHasARC(M);
3438+
if (!Run)
3439+
return false;
3440+
33873441
// These are initialized lazily.
33883442
StoreStrongCallee = 0;
33893443
RetainAutoreleaseCallee = 0;
@@ -3407,6 +3461,10 @@ bool ObjCARCContract::runOnFunction(Function &F) {
34073461
if (!EnableARCOpts)
34083462
return false;
34093463

3464+
// If nothing in the Module uses ARC, don't do anything.
3465+
if (!Run)
3466+
return false;
3467+
34103468
Changed = false;
34113469
AA = &getAnalysis<AliasAnalysis>();
34123470
DT = &getAnalysis<DominatorTree>();

0 commit comments

Comments
 (0)