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