File indexing completed on 2026-05-10 08:43:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #ifndef LLVM_ANALYSIS_OBJCARCANALYSISUTILS_H
0023 #define LLVM_ANALYSIS_OBJCARCANALYSISUTILS_H
0024
0025 #include "llvm/Analysis/ObjCARCInstKind.h"
0026 #include "llvm/Analysis/ValueTracking.h"
0027 #include "llvm/IR/Constants.h"
0028 #include "llvm/IR/Module.h"
0029 #include "llvm/IR/ValueHandle.h"
0030 #include <optional>
0031
0032 namespace llvm {
0033
0034 class AAResults;
0035
0036 namespace objcarc {
0037
0038
0039 extern bool EnableARCOpts;
0040
0041
0042
0043 inline bool ModuleHasARC(const Module &M) {
0044 return M.getNamedValue("llvm.objc.retain") ||
0045 M.getNamedValue("llvm.objc.release") ||
0046 M.getNamedValue("llvm.objc.autorelease") ||
0047 M.getNamedValue("llvm.objc.retainAutoreleasedReturnValue") ||
0048 M.getNamedValue("llvm.objc.unsafeClaimAutoreleasedReturnValue") ||
0049 M.getNamedValue("llvm.objc.retainBlock") ||
0050 M.getNamedValue("llvm.objc.autoreleaseReturnValue") ||
0051 M.getNamedValue("llvm.objc.autoreleasePoolPush") ||
0052 M.getNamedValue("llvm.objc.loadWeakRetained") ||
0053 M.getNamedValue("llvm.objc.loadWeak") ||
0054 M.getNamedValue("llvm.objc.destroyWeak") ||
0055 M.getNamedValue("llvm.objc.storeWeak") ||
0056 M.getNamedValue("llvm.objc.initWeak") ||
0057 M.getNamedValue("llvm.objc.moveWeak") ||
0058 M.getNamedValue("llvm.objc.copyWeak") ||
0059 M.getNamedValue("llvm.objc.retainedObject") ||
0060 M.getNamedValue("llvm.objc.unretainedObject") ||
0061 M.getNamedValue("llvm.objc.unretainedPointer") ||
0062 M.getNamedValue("llvm.objc.clang.arc.noop.use") ||
0063 M.getNamedValue("llvm.objc.clang.arc.use");
0064 }
0065
0066
0067
0068
0069 inline const Value *GetUnderlyingObjCPtr(const Value *V) {
0070 for (;;) {
0071 V = getUnderlyingObject(V);
0072 if (!IsForwarding(GetBasicARCInstKind(V)))
0073 break;
0074 V = cast<CallInst>(V)->getArgOperand(0);
0075 }
0076
0077 return V;
0078 }
0079
0080
0081 inline const Value *GetUnderlyingObjCPtrCached(
0082 const Value *V,
0083 DenseMap<const Value *, std::pair<WeakVH, WeakTrackingVH>> &Cache) {
0084
0085 auto InCache = Cache.lookup(V);
0086 if (InCache.first && InCache.second)
0087 return InCache.second;
0088
0089 const Value *Computed = GetUnderlyingObjCPtr(V);
0090 Cache[V] =
0091 std::make_pair(const_cast<Value *>(V), const_cast<Value *>(Computed));
0092 return Computed;
0093 }
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110 inline const Value *GetRCIdentityRoot(const Value *V) {
0111 for (;;) {
0112 V = V->stripPointerCasts();
0113 if (!IsForwarding(GetBasicARCInstKind(V)))
0114 break;
0115 V = cast<CallInst>(V)->getArgOperand(0);
0116 }
0117 return V;
0118 }
0119
0120
0121
0122
0123
0124 inline Value *GetRCIdentityRoot(Value *V) {
0125 return const_cast<Value *>(GetRCIdentityRoot((const Value *)V));
0126 }
0127
0128
0129
0130
0131 inline Value *GetArgRCIdentityRoot(Value *Inst) {
0132 return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0));
0133 }
0134
0135 inline bool IsNullOrUndef(const Value *V) {
0136 return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
0137 }
0138
0139 inline bool IsNoopInstruction(const Instruction *I) {
0140 return isa<BitCastInst>(I) ||
0141 (isa<GetElementPtrInst>(I) &&
0142 cast<GetElementPtrInst>(I)->hasAllZeroIndices());
0143 }
0144
0145
0146 inline bool IsPotentialRetainableObjPtr(const Value *Op) {
0147
0148
0149 if (isa<Constant>(Op) || isa<AllocaInst>(Op))
0150 return false;
0151
0152 if (const Argument *Arg = dyn_cast<Argument>(Op))
0153 if (Arg->hasPassPointeeByValueCopyAttr() || Arg->hasNestAttr() ||
0154 Arg->hasStructRetAttr())
0155 return false;
0156
0157
0158
0159
0160
0161 PointerType *Ty = dyn_cast<PointerType>(Op->getType());
0162 if (!Ty)
0163 return false;
0164
0165
0166 return true;
0167 }
0168
0169 bool IsPotentialRetainableObjPtr(const Value *Op, AAResults &AA);
0170
0171
0172
0173 inline ARCInstKind GetCallSiteClass(const CallBase &CB) {
0174 for (const Use &U : CB.args())
0175 if (IsPotentialRetainableObjPtr(U))
0176 return CB.onlyReadsMemory() ? ARCInstKind::User : ARCInstKind::CallOrUser;
0177
0178 return CB.onlyReadsMemory() ? ARCInstKind::None : ARCInstKind::Call;
0179 }
0180
0181
0182
0183
0184
0185
0186 inline bool IsObjCIdentifiedObject(const Value *V) {
0187
0188
0189
0190 if (isa<CallInst>(V) || isa<InvokeInst>(V) ||
0191 isa<Argument>(V) || isa<Constant>(V) ||
0192 isa<AllocaInst>(V))
0193 return true;
0194
0195 if (const LoadInst *LI = dyn_cast<LoadInst>(V)) {
0196 const Value *Pointer =
0197 GetRCIdentityRoot(LI->getPointerOperand());
0198 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) {
0199
0200
0201 if (GV->isConstant())
0202 return true;
0203 StringRef Name = GV->getName();
0204
0205
0206 if (Name.starts_with("\01l_objc_msgSend_fixup_"))
0207 return true;
0208
0209 StringRef Section = GV->getSection();
0210 if (Section.contains("__message_refs") ||
0211 Section.contains("__objc_classrefs") ||
0212 Section.contains("__objc_superrefs") ||
0213 Section.contains("__objc_methname") || Section.contains("__cstring"))
0214 return true;
0215 }
0216 }
0217
0218 return false;
0219 }
0220
0221 enum class ARCMDKindID {
0222 ImpreciseRelease,
0223 CopyOnEscape,
0224 NoObjCARCExceptions,
0225 };
0226
0227
0228 class ARCMDKindCache {
0229 Module *M;
0230
0231
0232 std::optional<unsigned> ImpreciseReleaseMDKind;
0233
0234
0235 std::optional<unsigned> CopyOnEscapeMDKind;
0236
0237
0238 std::optional<unsigned> NoObjCARCExceptionsMDKind;
0239
0240 public:
0241 void init(Module *Mod) {
0242 M = Mod;
0243 ImpreciseReleaseMDKind = std::nullopt;
0244 CopyOnEscapeMDKind = std::nullopt;
0245 NoObjCARCExceptionsMDKind = std::nullopt;
0246 }
0247
0248 unsigned get(ARCMDKindID ID) {
0249 switch (ID) {
0250 case ARCMDKindID::ImpreciseRelease:
0251 if (!ImpreciseReleaseMDKind)
0252 ImpreciseReleaseMDKind =
0253 M->getContext().getMDKindID("clang.imprecise_release");
0254 return *ImpreciseReleaseMDKind;
0255 case ARCMDKindID::CopyOnEscape:
0256 if (!CopyOnEscapeMDKind)
0257 CopyOnEscapeMDKind =
0258 M->getContext().getMDKindID("clang.arc.copy_on_escape");
0259 return *CopyOnEscapeMDKind;
0260 case ARCMDKindID::NoObjCARCExceptions:
0261 if (!NoObjCARCExceptionsMDKind)
0262 NoObjCARCExceptionsMDKind =
0263 M->getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
0264 return *NoObjCARCExceptionsMDKind;
0265 }
0266 llvm_unreachable("Covered switch isn't covered?!");
0267 }
0268 };
0269
0270 }
0271 }
0272
0273 #endif