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" |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Instruction.h" |
| 26 | #include "llvm/InitializePasses.h" |
| 27 | #include "llvm/PassAnalysisSupport.h" |
| 28 | #include "llvm/PassSupport.h" |
| 29 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "objc-arc-aa" |
| 31 | |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 32 | namespace llvm { |
| 33 | class Function; |
| 34 | class Value; |
| 35 | } |
| 36 | |
| 37 | using namespace llvm; |
| 38 | using namespace llvm::objcarc; |
| 39 | |
| 40 | // Register this pass... |
| 41 | char ObjCARCAliasAnalysis::ID = 0; |
| 42 | INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa", |
| 43 | "ObjC-ARC-Based Alias Analysis", false, true, false) |
| 44 | |
| 45 | ImmutablePass *llvm::createObjCARCAliasAnalysisPass() { |
| 46 | return new ObjCARCAliasAnalysis(); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 51 | AU.setPreservesAll(); |
| 52 | AliasAnalysis::getAnalysisUsage(AU); |
| 53 | } |
| 54 | |
| 55 | AliasAnalysis::AliasResult |
| 56 | ObjCARCAliasAnalysis::alias(const Location &LocA, const Location &LocB) { |
| 57 | if (!EnableARCOpts) |
| 58 | return AliasAnalysis::alias(LocA, LocB); |
| 59 | |
| 60 | // First, strip off no-ops, including ObjC-specific no-ops, and try making a |
| 61 | // precise alias query. |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 62 | const Value *SA = GetRCIdentityRoot(LocA.Ptr); |
| 63 | const Value *SB = GetRCIdentityRoot(LocB.Ptr); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 64 | AliasResult Result = |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 65 | AliasAnalysis::alias(Location(SA, LocA.Size, LocA.AATags), |
| 66 | Location(SB, LocB.Size, LocB.AATags)); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 67 | if (Result != MayAlias) |
| 68 | return Result; |
| 69 | |
| 70 | // If that failed, climb to the underlying object, including climbing through |
| 71 | // ObjC-specific no-ops, and try making an imprecise alias query. |
| 72 | const Value *UA = GetUnderlyingObjCPtr(SA); |
| 73 | const Value *UB = GetUnderlyingObjCPtr(SB); |
| 74 | if (UA != SA || UB != SB) { |
| 75 | Result = AliasAnalysis::alias(Location(UA), Location(UB)); |
| 76 | // We can't use MustAlias or PartialAlias results here because |
| 77 | // GetUnderlyingObjCPtr may return an offsetted pointer value. |
| 78 | if (Result == NoAlias) |
| 79 | return NoAlias; |
| 80 | } |
| 81 | |
| 82 | // If that failed, fail. We don't need to chain here, since that's covered |
| 83 | // by the earlier precise query. |
| 84 | return MayAlias; |
| 85 | } |
| 86 | |
| 87 | bool |
| 88 | ObjCARCAliasAnalysis::pointsToConstantMemory(const Location &Loc, |
| 89 | bool OrLocal) { |
| 90 | if (!EnableARCOpts) |
| 91 | return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal); |
| 92 | |
| 93 | // First, strip off no-ops, including ObjC-specific no-ops, and try making |
| 94 | // a precise alias query. |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 95 | const Value *S = GetRCIdentityRoot(Loc.Ptr); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 96 | if (AliasAnalysis::pointsToConstantMemory(Location(S, Loc.Size, Loc.AATags), |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 97 | OrLocal)) |
| 98 | return true; |
| 99 | |
| 100 | // If that failed, climb to the underlying object, including climbing through |
| 101 | // ObjC-specific no-ops, and try making an imprecise alias query. |
| 102 | const Value *U = GetUnderlyingObjCPtr(S); |
| 103 | if (U != S) |
| 104 | return AliasAnalysis::pointsToConstantMemory(Location(U), OrLocal); |
| 105 | |
| 106 | // If that failed, fail. We don't need to chain here, since that's covered |
| 107 | // by the earlier precise query. |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | AliasAnalysis::ModRefBehavior |
| 112 | ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { |
| 113 | // We have nothing to do. Just chain to the next AliasAnalysis. |
| 114 | return AliasAnalysis::getModRefBehavior(CS); |
| 115 | } |
| 116 | |
| 117 | AliasAnalysis::ModRefBehavior |
| 118 | ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) { |
| 119 | if (!EnableARCOpts) |
| 120 | return AliasAnalysis::getModRefBehavior(F); |
| 121 | |
| 122 | switch (GetFunctionClass(F)) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame^] | 123 | case ARCInstKind::NoopCast: |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 124 | return DoesNotAccessMemory; |
| 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | return AliasAnalysis::getModRefBehavior(F); |
| 130 | } |
| 131 | |
| 132 | AliasAnalysis::ModRefResult |
| 133 | ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS, const Location &Loc) { |
| 134 | if (!EnableARCOpts) |
| 135 | return AliasAnalysis::getModRefInfo(CS, Loc); |
| 136 | |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame^] | 137 | switch (GetBasicARCInstKind(CS.getInstruction())) { |
| 138 | case ARCInstKind::Retain: |
| 139 | case ARCInstKind::RetainRV: |
| 140 | case ARCInstKind::Autorelease: |
| 141 | case ARCInstKind::AutoreleaseRV: |
| 142 | case ARCInstKind::NoopCast: |
| 143 | case ARCInstKind::AutoreleasepoolPush: |
| 144 | case ARCInstKind::FusedRetainAutorelease: |
| 145 | case ARCInstKind::FusedRetainAutoreleaseRV: |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 146 | // These functions don't access any memory visible to the compiler. |
| 147 | // Note that this doesn't include objc_retainBlock, because it updates |
| 148 | // pointers when it copies block data. |
| 149 | return NoModRef; |
| 150 | default: |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | return AliasAnalysis::getModRefInfo(CS, Loc); |
| 155 | } |
| 156 | |
| 157 | AliasAnalysis::ModRefResult |
| 158 | ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1, |
| 159 | ImmutableCallSite CS2) { |
| 160 | // TODO: Theoretically we could check for dependencies between objc_* calls |
| 161 | // and OnlyAccessesArgumentPointees calls or other well-behaved calls. |
| 162 | return AliasAnalysis::getModRefInfo(CS1, CS2); |
| 163 | } |