Michael Gottesman | 6eb95dc | 2013-07-10 18:49:00 +0000 | [diff] [blame] | 1 | //===- ObjCARCAliasAnalysis.cpp - ObjC ARC Optimization -------------------===// |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// This file defines a simple ARC-aware AliasAnalysis using special knowledge |
| 10 | /// of Objective C to enhance other optimization passes which rely on the Alias |
| 11 | /// Analysis infrastructure. |
| 12 | /// |
| 13 | /// WARNING: This file knows about certain library functions. It recognizes them |
| 14 | /// by name, and hardwires knowledge of their semantics. |
| 15 | /// |
| 16 | /// WARNING: This file knows about how certain Objective-C library functions are |
| 17 | /// used. Naive LLVM IR transformations which would otherwise be |
| 18 | /// behavior-preserving may break these assumptions. |
| 19 | /// |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 20 | /// TODO: Theoretically we could check for dependencies between objc_* calls |
| 21 | /// and FMRB_OnlyAccessesArgumentPointees calls or other well-behaved calls. |
| 22 | /// |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Chandler Carruth | 0f79218 | 2015-08-20 08:06:03 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ObjCARCAliasAnalysis.h" |
| 26 | #include "llvm/Analysis/ObjCARCAnalysisUtils.h" |
Chandler Carruth | b4ebdf3 | 2015-08-14 03:55:36 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Function.h" |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | b4ebdf3 | 2015-08-14 03:55:36 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Value.h" |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 30 | #include "llvm/InitializePasses.h" |
| 31 | #include "llvm/PassAnalysisSupport.h" |
| 32 | #include "llvm/PassSupport.h" |
| 33 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "objc-arc-aa" |
| 35 | |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | using namespace llvm::objcarc; |
| 38 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 39 | AliasResult ObjCARCAAResult::alias(const MemoryLocation &LocA, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 40 | const MemoryLocation &LocB, |
| 41 | AAQueryInfo &AAQI) { |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 42 | if (!EnableARCOpts) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 43 | return AAResultBase::alias(LocA, LocB, AAQI); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 44 | |
| 45 | // First, strip off no-ops, including ObjC-specific no-ops, and try making a |
| 46 | // precise alias query. |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 47 | const Value *SA = GetRCIdentityRoot(LocA.Ptr); |
| 48 | const Value *SB = GetRCIdentityRoot(LocB.Ptr); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 49 | AliasResult Result = |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 50 | AAResultBase::alias(MemoryLocation(SA, LocA.Size, LocA.AATags), |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 51 | MemoryLocation(SB, LocB.Size, LocB.AATags), AAQI); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 52 | if (Result != MayAlias) |
| 53 | return Result; |
| 54 | |
| 55 | // If that failed, climb to the underlying object, including climbing through |
| 56 | // ObjC-specific no-ops, and try making an imprecise alias query. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 57 | const Value *UA = GetUnderlyingObjCPtr(SA, DL); |
| 58 | const Value *UB = GetUnderlyingObjCPtr(SB, DL); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 59 | if (UA != SA || UB != SB) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 60 | Result = AAResultBase::alias(MemoryLocation(UA), MemoryLocation(UB), AAQI); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 61 | // We can't use MustAlias or PartialAlias results here because |
| 62 | // GetUnderlyingObjCPtr may return an offsetted pointer value. |
| 63 | if (Result == NoAlias) |
| 64 | return NoAlias; |
| 65 | } |
| 66 | |
| 67 | // If that failed, fail. We don't need to chain here, since that's covered |
| 68 | // by the earlier precise query. |
| 69 | return MayAlias; |
| 70 | } |
| 71 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 72 | bool ObjCARCAAResult::pointsToConstantMemory(const MemoryLocation &Loc, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 73 | AAQueryInfo &AAQI, bool OrLocal) { |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 74 | if (!EnableARCOpts) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 75 | return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 76 | |
| 77 | // First, strip off no-ops, including ObjC-specific no-ops, and try making |
| 78 | // a precise alias query. |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 79 | const Value *S = GetRCIdentityRoot(Loc.Ptr); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 80 | if (AAResultBase::pointsToConstantMemory( |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 81 | MemoryLocation(S, Loc.Size, Loc.AATags), AAQI, OrLocal)) |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 82 | return true; |
| 83 | |
| 84 | // If that failed, climb to the underlying object, including climbing through |
| 85 | // ObjC-specific no-ops, and try making an imprecise alias query. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 86 | const Value *U = GetUnderlyingObjCPtr(S, DL); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 87 | if (U != S) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 88 | return AAResultBase::pointsToConstantMemory(MemoryLocation(U), AAQI, |
| 89 | OrLocal); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 90 | |
| 91 | // If that failed, fail. We don't need to chain here, since that's covered |
| 92 | // by the earlier precise query. |
| 93 | return false; |
| 94 | } |
| 95 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 96 | FunctionModRefBehavior ObjCARCAAResult::getModRefBehavior(const Function *F) { |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 97 | if (!EnableARCOpts) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 98 | return AAResultBase::getModRefBehavior(F); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 99 | |
| 100 | switch (GetFunctionClass(F)) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 101 | case ARCInstKind::NoopCast: |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 102 | return FMRB_DoesNotAccessMemory; |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 103 | default: |
| 104 | break; |
| 105 | } |
| 106 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 107 | return AAResultBase::getModRefBehavior(F); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 110 | ModRefInfo ObjCARCAAResult::getModRefInfo(const CallBase *Call, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 111 | const MemoryLocation &Loc, |
| 112 | AAQueryInfo &AAQI) { |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 113 | if (!EnableARCOpts) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 114 | return AAResultBase::getModRefInfo(Call, Loc, AAQI); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 115 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 116 | switch (GetBasicARCInstKind(Call)) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 117 | case ARCInstKind::Retain: |
| 118 | case ARCInstKind::RetainRV: |
| 119 | case ARCInstKind::Autorelease: |
| 120 | case ARCInstKind::AutoreleaseRV: |
| 121 | case ARCInstKind::NoopCast: |
| 122 | case ARCInstKind::AutoreleasepoolPush: |
| 123 | case ARCInstKind::FusedRetainAutorelease: |
| 124 | case ARCInstKind::FusedRetainAutoreleaseRV: |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 125 | // These functions don't access any memory visible to the compiler. |
| 126 | // Note that this doesn't include objc_retainBlock, because it updates |
| 127 | // pointers when it copies block data. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 128 | return ModRefInfo::NoModRef; |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 129 | default: |
| 130 | break; |
| 131 | } |
| 132 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 133 | return AAResultBase::getModRefInfo(Call, Loc, AAQI); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 136 | ObjCARCAAResult ObjCARCAA::run(Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 137 | return ObjCARCAAResult(F.getParent()->getDataLayout()); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 140 | char ObjCARCAAWrapperPass::ID = 0; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 141 | INITIALIZE_PASS(ObjCARCAAWrapperPass, "objc-arc-aa", |
| 142 | "ObjC-ARC-Based Alias Analysis", false, true) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 143 | |
| 144 | ImmutablePass *llvm::createObjCARCAAWrapperPass() { |
| 145 | return new ObjCARCAAWrapperPass(); |
| 146 | } |
| 147 | |
| 148 | ObjCARCAAWrapperPass::ObjCARCAAWrapperPass() : ImmutablePass(ID) { |
| 149 | initializeObjCARCAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 150 | } |
| 151 | |
| 152 | bool ObjCARCAAWrapperPass::doInitialization(Module &M) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 153 | Result.reset(new ObjCARCAAResult(M.getDataLayout())); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 154 | return false; |
| 155 | } |
| 156 | |
| 157 | bool ObjCARCAAWrapperPass::doFinalization(Module &M) { |
| 158 | Result.reset(); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | void ObjCARCAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 163 | AU.setPreservesAll(); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 164 | } |