1023d2bbbbedc6ed991b11381a987673133be2c81Michael Gottesman//===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===// 26504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman// 36504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman// The LLVM Compiler Infrastructure 46504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman// 56504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman// This file is distributed under the University of Illinois Open Source 66504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman// License. See LICENSE.TXT for details. 76504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman// 86504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman//===----------------------------------------------------------------------===// 96504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// \file 106504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// This file defines common definitions/declarations used by the ObjC ARC 116504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// Optimizer. ARC stands for Automatic Reference Counting and is a system for 126504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// managing reference counts for objects in Objective C. 136504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// 146504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// WARNING: This file knows about certain library functions. It recognizes them 156504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// by name, and hardwires knowledge of their semantics. 166504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// 176504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// WARNING: This file knows about how certain Objective-C library functions are 186504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// used. Naive LLVM IR transformations which would otherwise be 196504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// behavior-preserving may break these assumptions. 206504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// 216504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman//===----------------------------------------------------------------------===// 226504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman 2337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H 2437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H 256504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman 266504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman#include "llvm/ADT/StringSwitch.h" 274c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar#include "llvm/ADT/Optional.h" 286504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman#include "llvm/Analysis/AliasAnalysis.h" 296504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman#include "llvm/Analysis/Passes.h" 306086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman#include "llvm/Analysis/ValueTracking.h" 3136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/CallSite.h" 3236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/InstIterator.h" 336504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman#include "llvm/IR/Module.h" 346504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman#include "llvm/Pass.h" 356504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman#include "llvm/Transforms/ObjCARC.h" 363a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman#include "llvm/Transforms/Utils/Local.h" 37ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "ARCInstKind.h" 386504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman 396504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesmannamespace llvm { 409ab758b9bc2fe51af6dabbdeb30f4a2e600bdcd0Michael Gottesmanclass raw_ostream; 419ab758b9bc2fe51af6dabbdeb30f4a2e600bdcd0Michael Gottesman} 429ab758b9bc2fe51af6dabbdeb30f4a2e600bdcd0Michael Gottesman 439ab758b9bc2fe51af6dabbdeb30f4a2e600bdcd0Michael Gottesmannamespace llvm { 446504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesmannamespace objcarc { 456504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman 466504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// \brief A handy option to enable/disable all ARC Optimizations. 476504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesmanextern bool EnableARCOpts; 486504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman 496504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// \brief Test if the given module looks interesting to run ARC optimization 506504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman/// on. 516504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesmanstatic inline bool ModuleHasARC(const Module &M) { 526504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman return 536504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_retain") || 546504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_release") || 556504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_autorelease") || 566504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_retainAutoreleasedReturnValue") || 576504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_retainBlock") || 586504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_autoreleaseReturnValue") || 596504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_autoreleasePoolPush") || 606504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_loadWeakRetained") || 616504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_loadWeak") || 626504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_destroyWeak") || 636504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_storeWeak") || 646504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_initWeak") || 656504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_moveWeak") || 666504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_copyWeak") || 676504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_retainedObject") || 686504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman M.getNamedValue("objc_unretainedObject") || 693e9f3a0389488701bd1cb5c778d0e785c827d790Michael Gottesman M.getNamedValue("objc_unretainedPointer") || 703e9f3a0389488701bd1cb5c778d0e785c827d790Michael Gottesman M.getNamedValue("clang.arc.use"); 716504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman} 726504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman 736086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman/// \brief This is a wrapper around getUnderlyingObject which also knows how to 746086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman/// look through objc_retain and objc_autorelease calls, which we know to return 756086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman/// their argument verbatim. 764c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainarstatic inline const Value *GetUnderlyingObjCPtr(const Value *V, 774c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar const DataLayout &DL) { 786086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman for (;;) { 794c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar V = GetUnderlyingObject(V, DL); 80ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (!IsForwarding(GetBasicARCInstKind(V))) 816086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman break; 826086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman V = cast<CallInst>(V)->getArgOperand(0); 836086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman } 846086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman 856086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman return V; 866086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman} 876086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman 88ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// The RCIdentity root of a value \p V is a dominating value U for which 89ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// retaining or releasing U is equivalent to retaining or releasing V. In other 90ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// words, ARC operations on \p V are equivalent to ARC operations on \p U. 91ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// 92ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// We use this in the ARC optimizer to make it easier to match up ARC 93ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// operations by always mapping ARC operations to RCIdentityRoots instead of 94ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// pointers themselves. 95ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// 96ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// The two ways that we see RCIdentical values in ObjC are via: 97ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// 98ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// 1. PointerCasts 99ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// 2. Forwarding Calls that return their argument verbatim. 100ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// 101ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Thus this function strips off pointer casts and forwarding calls. *NOTE* 102ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// This implies that two RCIdentical values must alias. 103ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstatic inline const Value *GetRCIdentityRoot(const Value *V) { 1046086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman for (;;) { 1056086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman V = V->stripPointerCasts(); 106ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (!IsForwarding(GetBasicARCInstKind(V))) 1076086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman break; 1086086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman V = cast<CallInst>(V)->getArgOperand(0); 1096086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman } 1106086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman return V; 1116086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman} 1126086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman 113ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Helper which calls const Value *GetRCIdentityRoot(const Value *V) and just 114ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// casts away the const of the result. For documentation about what an 115ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// RCIdentityRoot (and by extension GetRCIdentityRoot is) look at that 116ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// function. 117ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstatic inline Value *GetRCIdentityRoot(Value *V) { 118ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines return const_cast<Value *>(GetRCIdentityRoot((const Value *)V)); 1196086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman} 1206086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman 1213a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// \brief Assuming the given instruction is one of the special calls such as 122ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// objc_retain or objc_release, return the RCIdentity root of the argument of 123ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// the call. 124ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstatic inline Value *GetArgRCIdentityRoot(Value *Inst) { 125ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0)); 1263a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman} 1273a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 128f0a15d88afce23453ff55894400035014ad46a15Michael Gottesmanstatic inline bool IsNullOrUndef(const Value *V) { 1293a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return isa<ConstantPointerNull>(V) || isa<UndefValue>(V); 1303a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman} 1313a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 132f0a15d88afce23453ff55894400035014ad46a15Michael Gottesmanstatic inline bool IsNoopInstruction(const Instruction *I) { 1333a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return isa<BitCastInst>(I) || 1343a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman (isa<GetElementPtrInst>(I) && 1353a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman cast<GetElementPtrInst>(I)->hasAllZeroIndices()); 1363a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman} 1373a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 1383a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 1393a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// \brief Erase the given instruction. 1403a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// 1413a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// Many ObjC calls return their argument verbatim, 1423a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// so if it's such a call and the return value has users, replace them with the 1433a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// argument value. 1443a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// 1453a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesmanstatic inline void EraseInstruction(Instruction *CI) { 1463a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman Value *OldArg = cast<CallInst>(CI)->getArgOperand(0); 1473a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 1483a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman bool Unused = CI->use_empty(); 1493a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 1503a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (!Unused) { 1513a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // Replace the return value with the argument. 152ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines assert((IsForwarding(GetBasicARCInstKind(CI)) || 153ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines (IsNoopOnNull(GetBasicARCInstKind(CI)) && 154a75b293e4fa702d3e233839dc347ebf565be2d4dMichael Gottesman isa<ConstantPointerNull>(OldArg))) && 1553a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman "Can't delete non-forwarding instruction with users!"); 1563a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman CI->replaceAllUsesWith(OldArg); 1573a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman } 1583a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 1593a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman CI->eraseFromParent(); 1603a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 1613a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (Unused) 1623a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman RecursivelyDeleteTriviallyDeadInstructions(OldArg); 1633a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman} 1643a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 1653a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// \brief Test whether the given value is possible a retainable object pointer. 1663a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesmanstatic inline bool IsPotentialRetainableObjPtr(const Value *Op) { 1673dcfdab267f157aeb0e6aed896f7acdeb11481baMichael Gottesman // Pointers to static or stack storage are not valid retainable object 1683dcfdab267f157aeb0e6aed896f7acdeb11481baMichael Gottesman // pointers. 1693a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (isa<Constant>(Op) || isa<AllocaInst>(Op)) 1703a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return false; 1713a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // Special arguments can not be a valid retainable object pointer. 1723a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (const Argument *Arg = dyn_cast<Argument>(Op)) 1733a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (Arg->hasByValAttr() || 17436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Arg->hasInAllocaAttr() || 1753a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman Arg->hasNestAttr() || 1763a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman Arg->hasStructRetAttr()) 1773a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return false; 1783a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // Only consider values with pointer types. 1793a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // 1803a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // It seemes intuitive to exclude function pointer types as well, since 1813a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // functions are never retainable object pointers, however clang occasionally 1823a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // bitcasts retainable object pointers to function-pointer type temporarily. 1833a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman PointerType *Ty = dyn_cast<PointerType>(Op->getType()); 1843a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (!Ty) 1853a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return false; 1863dcfdab267f157aeb0e6aed896f7acdeb11481baMichael Gottesman // Conservatively assume anything else is a potential retainable object 1873dcfdab267f157aeb0e6aed896f7acdeb11481baMichael Gottesman // pointer. 1883a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return true; 1893a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman} 1903a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 1913a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesmanstatic inline bool IsPotentialRetainableObjPtr(const Value *Op, 1923a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman AliasAnalysis &AA) { 1933a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // First make the rudimentary check. 1943a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (!IsPotentialRetainableObjPtr(Op)) 1953a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return false; 1963a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 1973a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // Objects in constant memory are not reference-counted. 1983a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (AA.pointsToConstantMemory(Op)) 1993a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return false; 2003a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 2013a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // Pointers in constant memory are not pointing to reference-counted objects. 2023a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (const LoadInst *LI = dyn_cast<LoadInst>(Op)) 2033a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (AA.pointsToConstantMemory(LI->getPointerOperand())) 2043a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return false; 2053a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 2063a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // Otherwise assume the worst. 2073a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return true; 2083a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman} 2093a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 210ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Helper for GetARCInstKind. Determines what kind of construct CS 2113a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// is. 212ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstatic inline ARCInstKind GetCallSiteClass(ImmutableCallSite CS) { 2133a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); 2143a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman I != E; ++I) 2153a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (IsPotentialRetainableObjPtr(*I)) 216ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines return CS.onlyReadsMemory() ? ARCInstKind::User : ARCInstKind::CallOrUser; 2173a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 218ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines return CS.onlyReadsMemory() ? ARCInstKind::None : ARCInstKind::Call; 2193a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman} 2203a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 2213a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// \brief Return true if this value refers to a distinct and identifiable 2223a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// object. 2233a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// 2243a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses 2253a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// special knowledge of ObjC conventions. 2263a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesmanstatic inline bool IsObjCIdentifiedObject(const Value *V) { 2273a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // Assume that call results and arguments have their own "provenance". 2283a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // Constants (including GlobalVariables) and Allocas are never 2293a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // reference-counted. 2303a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (isa<CallInst>(V) || isa<InvokeInst>(V) || 2313a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman isa<Argument>(V) || isa<Constant>(V) || 2323a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman isa<AllocaInst>(V)) 2333a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return true; 2343a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 2353a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (const LoadInst *LI = dyn_cast<LoadInst>(V)) { 2363a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman const Value *Pointer = 237ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines GetRCIdentityRoot(LI->getPointerOperand()); 2383a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) { 2393a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // A constant pointer can't be pointing to an object on the heap. It may 2403a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // be reference-counted, but it won't be deleted. 2413a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman if (GV->isConstant()) 2423a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return true; 2433a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman StringRef Name = GV->getName(); 2443a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // These special variables are known to hold values which are not 2453a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman // reference-counted pointers. 24637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (Name.startswith("\01l_objc_msgSend_fixup_")) 24737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines return true; 24837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 24937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines StringRef Section = GV->getSection(); 25037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (Section.find("__message_refs") != StringRef::npos || 25137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Section.find("__objc_classrefs") != StringRef::npos || 25237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Section.find("__objc_superrefs") != StringRef::npos || 25337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Section.find("__objc_methname") != StringRef::npos || 25437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Section.find("__cstring") != StringRef::npos) 2553a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return true; 2563a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman } 2573a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman } 2583a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman 2593a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman return false; 2603a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman} 2616086847bfbc538b99305b4d7e0a53ab610f6a9bbMichael Gottesman 2624c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainarenum class ARCMDKindID { 2634c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar ImpreciseRelease, 2644c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar CopyOnEscape, 2654c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar NoObjCARCExceptions, 2664c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar}; 2674c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar 2684c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar/// A cache of MDKinds used by various ARC optimizations. 2694c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainarclass ARCMDKindCache { 2704c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar Module *M; 2714c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar 2724c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar /// The Metadata Kind for clang.imprecise_release metadata. 2734c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar llvm::Optional<unsigned> ImpreciseReleaseMDKind; 2744c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar 2754c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar /// The Metadata Kind for clang.arc.copy_on_escape metadata. 2764c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar llvm::Optional<unsigned> CopyOnEscapeMDKind; 2774c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar 2784c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata. 2794c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar llvm::Optional<unsigned> NoObjCARCExceptionsMDKind; 2804c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar 2814c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainarpublic: 2824c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar void init(Module *Mod) { 2834c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar M = Mod; 2844c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar ImpreciseReleaseMDKind = NoneType::None; 2854c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar CopyOnEscapeMDKind = NoneType::None; 2864c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar NoObjCARCExceptionsMDKind = NoneType::None; 2874c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar } 2884c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar 2894c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar unsigned get(ARCMDKindID ID) { 2904c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar switch (ID) { 2914c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar case ARCMDKindID::ImpreciseRelease: 2924c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar if (!ImpreciseReleaseMDKind) 2934c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar ImpreciseReleaseMDKind = 2944c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar M->getContext().getMDKindID("clang.imprecise_release"); 2954c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar return *ImpreciseReleaseMDKind; 2964c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar case ARCMDKindID::CopyOnEscape: 2974c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar if (!CopyOnEscapeMDKind) 2984c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar CopyOnEscapeMDKind = 2994c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar M->getContext().getMDKindID("clang.arc.copy_on_escape"); 3004c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar return *CopyOnEscapeMDKind; 3014c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar case ARCMDKindID::NoObjCARCExceptions: 3024c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar if (!NoObjCARCExceptionsMDKind) 3034c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar NoObjCARCExceptionsMDKind = 3044c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar M->getContext().getMDKindID("clang.arc.no_objc_arc_exceptions"); 3054c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar return *NoObjCARCExceptionsMDKind; 3064c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar } 3074c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar llvm_unreachable("Covered switch isn't covered?!"); 3084c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar } 3094c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar}; 3104c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar 3116504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman} // end namespace objcarc 3126504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman} // end namespace llvm 3136504255a2257c53df8ab191c1db4517139f5dc8cMichael Gottesman 31437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#endif 315