2013-01-29 13:05:17 +08:00
|
|
|
//===- ObjCARCAPElim.cpp - ObjC ARC Optimization --------------------------===//
|
2013-01-28 12:12:07 +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 12:12:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
2013-01-29 13:05:17 +08:00
|
|
|
///
|
2013-01-28 12:12:07 +08:00
|
|
|
/// 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.
|
|
|
|
///
|
2013-01-29 13:05:17 +08:00
|
|
|
/// This specific file implements optimizations which remove extraneous
|
|
|
|
/// autorelease pools.
|
2013-01-28 12:12:07 +08:00
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ObjCARC.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
2013-01-29 12:51:59 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2013-01-29 17:09:27 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-01-28 12:12:07 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::objcarc;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "objc-arc-ap-elim"
|
|
|
|
|
2013-01-28 12:12:07 +08:00
|
|
|
namespace {
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Autorelease pool elimination.
|
2013-01-28 12:12:07 +08:00
|
|
|
class ObjCARCAPElim : public ModulePass {
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
bool runOnModule(Module &M) override;
|
2013-01-28 12:12:07 +08:00
|
|
|
|
|
|
|
static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
|
|
|
|
static bool OptimizeBB(BasicBlock *BB);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
ObjCARCAPElim() : ModulePass(ID) {
|
|
|
|
initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2013-01-28 12:12:07 +08:00
|
|
|
|
|
|
|
char ObjCARCAPElim::ID = 0;
|
|
|
|
INITIALIZE_PASS(ObjCARCAPElim,
|
|
|
|
"objc-arc-apelim",
|
|
|
|
"ObjC ARC autorelease pool elimination",
|
|
|
|
false, false)
|
|
|
|
|
|
|
|
Pass *llvm::createObjCARCAPElimPass() {
|
|
|
|
return new ObjCARCAPElim();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Interprocedurally determine if calls made by the given call site can
|
|
|
|
/// possibly produce autoreleases.
|
|
|
|
bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) {
|
|
|
|
if (const Function *Callee = CS.getCalledFunction()) {
|
Don't IPO over functions that can be de-refined
Summary:
Fixes PR26774.
If you're aware of the issue, feel free to skip the "Motivation"
section and jump directly to "This patch".
Motivation:
I define "refinement" as discarding behaviors from a program that the
optimizer has license to discard. So transforming:
```
void f(unsigned x) {
unsigned t = 5 / x;
(void)t;
}
```
to
```
void f(unsigned x) { }
```
is refinement, since the behavior went from "if x == 0 then undefined
else nothing" to "nothing" (the optimizer has license to discard
undefined behavior).
Refinement is a fundamental aspect of many mid-level optimizations done
by LLVM. For instance, transforming `x == (x + 1)` to `false` also
involves refinement since the expression's value went from "if x is
`undef` then { `true` or `false` } else { `false` }" to "`false`" (by
definition, the optimizer has license to fold `undef` to any non-`undef`
value).
Unfortunately, refinement implies that the optimizer cannot assume
that the implementation of a function it can see has all of the
behavior an unoptimized or a differently optimized version of the same
function can have. This is a problem for functions with comdat
linkage, where a function can be replaced by an unoptimized or a
differently optimized version of the same source level function.
For instance, FunctionAttrs cannot assume a comdat function is
actually `readnone` even if it does not have any loads or stores in
it; since there may have been loads and stores in the "original
function" that were refined out in the currently visible variant, and
at the link step the linker may in fact choose an implementation with
a load or a store. As an example, consider a function that does two
atomic loads from the same memory location, and writes to memory only
if the two values are not equal. The optimizer is allowed to refine
this function by first CSE'ing the two loads, and the folding the
comparision to always report that the two values are equal. Such a
refined variant will look like it is `readonly`. However, the
unoptimized version of the function can still write to memory (since
the two loads //can// result in different values), and selecting the
unoptimized version at link time will retroactively invalidate
transforms we may have done under the assumption that the function
does not write to memory.
Note: this is not just a problem with atomics or with linking
differently optimized object files. See PR26774 for more realistic
examples that involved neither.
This patch:
This change introduces a new set of linkage types, predicated as
`GlobalValue::mayBeDerefined` that returns true if the linkage type
allows a function to be replaced by a differently optimized variant at
link time. It then changes a set of IPO passes to bail out if they see
such a function.
Reviewers: chandlerc, hfinkel, dexonsmith, joker.eph, rnk
Subscribers: mcrosier, llvm-commits
Differential Revision: http://reviews.llvm.org/D18634
llvm-svn: 265762
2016-04-08 08:48:30 +08:00
|
|
|
if (!Callee->hasExactDefinition())
|
2013-01-28 12:12:07 +08:00
|
|
|
return true;
|
2015-10-20 07:20:14 +08:00
|
|
|
for (const BasicBlock &BB : *Callee) {
|
|
|
|
for (const Instruction &I : BB)
|
|
|
|
if (ImmutableCallSite JCS = ImmutableCallSite(&I))
|
2013-01-28 12:12:07 +08:00
|
|
|
// This recursion depth limit is arbitrary. It's just great
|
|
|
|
// enough to cover known interesting testcases.
|
|
|
|
if (Depth < 3 &&
|
|
|
|
!JCS.onlyReadsMemory() &&
|
|
|
|
MayAutorelease(JCS, Depth + 1))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
Instruction *Push = nullptr;
|
2013-01-28 12:12:07 +08:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
|
2015-10-20 07:20:14 +08:00
|
|
|
Instruction *Inst = &*I++;
|
2015-02-20 03:51:32 +08:00
|
|
|
switch (GetBasicARCInstKind(Inst)) {
|
|
|
|
case ARCInstKind::AutoreleasepoolPush:
|
2013-01-28 12:12:07 +08:00
|
|
|
Push = Inst;
|
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleasepoolPop:
|
2013-01-28 12:12:07 +08:00
|
|
|
// If this pop matches a push and nothing in between can autorelease,
|
|
|
|
// zap the pair.
|
|
|
|
if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) {
|
|
|
|
Changed = true;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop "
|
|
|
|
"autorelease pair:\n"
|
|
|
|
" Pop: "
|
|
|
|
<< *Inst << "\n"
|
|
|
|
<< " Push: " << *Push
|
|
|
|
<< "\n");
|
2013-01-28 12:12:07 +08:00
|
|
|
Inst->eraseFromParent();
|
|
|
|
Push->eraseFromParent();
|
|
|
|
}
|
2014-04-25 13:29:35 +08:00
|
|
|
Push = nullptr;
|
2013-01-28 12:12:07 +08:00
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::CallOrUser:
|
2013-01-28 12:12:07 +08:00
|
|
|
if (MayAutorelease(ImmutableCallSite(Inst)))
|
2014-04-25 13:29:35 +08:00
|
|
|
Push = nullptr;
|
2013-01-28 12:12:07 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjCARCAPElim::runOnModule(Module &M) {
|
|
|
|
if (!EnableARCOpts)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If nothing in the Module uses ARC, don't do anything.
|
|
|
|
if (!ModuleHasARC(M))
|
|
|
|
return false;
|
|
|
|
|
2016-04-23 06:06:11 +08:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
|
2013-01-28 12:12:07 +08:00
|
|
|
// Find the llvm.global_ctors variable, as the first step in
|
|
|
|
// identifying the global constructors. In theory, unnecessary autorelease
|
|
|
|
// pools could occur anywhere, but in practice it's pretty rare. Global
|
|
|
|
// ctors are a place where autorelease pools get inserted automatically,
|
|
|
|
// so it's pretty common for them to be unnecessary, and it's pretty
|
|
|
|
// profitable to eliminate them.
|
|
|
|
GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
|
|
|
|
if (!GV)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
assert(GV->hasDefinitiveInitializer() &&
|
|
|
|
"llvm.global_ctors is uncooperative!");
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Dig the constructor functions out of GV's initializer.
|
|
|
|
ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
|
|
|
|
for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end();
|
|
|
|
OI != OE; ++OI) {
|
|
|
|
Value *Op = *OI;
|
2014-05-17 04:39:27 +08:00
|
|
|
// llvm.global_ctors is an array of three-field structs where the second
|
|
|
|
// members are constructor functions.
|
2013-01-28 12:12:07 +08:00
|
|
|
Function *F = dyn_cast<Function>(cast<ConstantStruct>(Op)->getOperand(1));
|
|
|
|
// If the user used a constructor function with the wrong signature and
|
|
|
|
// it got bitcasted or whatever, look the other way.
|
|
|
|
if (!F)
|
|
|
|
continue;
|
|
|
|
// Only look at function definitions.
|
|
|
|
if (F->isDeclaration())
|
|
|
|
continue;
|
|
|
|
// Only look at functions with one basic block.
|
2014-03-02 20:27:27 +08:00
|
|
|
if (std::next(F->begin()) != F->end())
|
2013-01-28 12:12:07 +08:00
|
|
|
continue;
|
|
|
|
// Ok, a single-block constructor function definition. Try to optimize it.
|
2015-10-20 07:20:14 +08:00
|
|
|
Changed |= OptimizeBB(&F->front());
|
2013-01-28 12:12:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|