llvm.org GIT mirror llvm / 81aa712
[CallSite removal] Migrate all Alias Analysis APIs to use the newly minted `CallBase` class instead of the `CallSite` wrapper. This moves the largest interwoven collection of APIs that traffic in `CallSite`s. While a handful of these could have been migrated with a minorly more shallow migration by converting from a `CallSite` to a `CallBase`, it hardly seemed worth it. Most of the APIs needed to migrate together because of the complex interplay of AA APIs and the fact that converting from a `CallBase` to a `CallSite` isn't free in its current implementation. Out of tree users of these APIs can fairly reliably migrate with some combination of `.getInstruction()` on the `CallSite` instance and casting the resulting pointer. The most generic form will look like `CS` -> `cast_or_null<CallBase>(CS.getInstruction())` but in most cases there is a more elegant migration. Hopefully, this migrates enough APIs for users to fully move from `CallSite` to the base class. All of the in-tree users were easily migrated in that fashion. Thanks for the review from Saleem! Differential Revision: https://reviews.llvm.org/D55641 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@350503 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 10 months ago
32 changed file(s) with 472 addition(s) and 505 deletion(s). Raw diff Collapse all Expand all
4242 #include "llvm/ADT/SmallVector.h"
4343 #include "llvm/Analysis/MemoryLocation.h"
4444 #include "llvm/Analysis/TargetLibraryInfo.h"
45 #include "llvm/IR/CallSite.h"
4645 #include "llvm/IR/Function.h"
4746 #include "llvm/IR/Instruction.h"
4847 #include "llvm/IR/Instructions.h"
381380 /// \name Simple mod/ref information
382381 /// @{
383382
384 /// Get the ModRef info associated with a pointer argument of a callsite. The
383 /// Get the ModRef info associated with a pointer argument of a call. The
385384 /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note
386385 /// that these bits do not necessarily account for the overall behavior of
387386 /// the function, but rather only provide additional per-argument
388387 /// information. This never sets ModRefInfo::Must.
389 ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx);
388 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
390389
391390 /// Return the behavior of the given call site.
392 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS);
391 FunctionModRefBehavior getModRefBehavior(const CallBase *Call);
393392
394393 /// Return the behavior when calling the given function.
395394 FunctionModRefBehavior getModRefBehavior(const Function *F);
405404 /// property (e.g. calls to 'sin' and 'cos').
406405 ///
407406 /// This property corresponds to the GCC 'const' attribute.
408 bool doesNotAccessMemory(ImmutableCallSite CS) {
409 return getModRefBehavior(CS) == FMRB_DoesNotAccessMemory;
407 bool doesNotAccessMemory(const CallBase *Call) {
408 return getModRefBehavior(Call) == FMRB_DoesNotAccessMemory;
410409 }
411410
412411 /// Checks if the specified function is known to never read or write memory.
433432 /// absence of interfering store instructions, such as CSE of strlen calls.
434433 ///
435434 /// This property corresponds to the GCC 'pure' attribute.
436 bool onlyReadsMemory(ImmutableCallSite CS) {
437 return onlyReadsMemory(getModRefBehavior(CS));
435 bool onlyReadsMemory(const CallBase *Call) {
436 return onlyReadsMemory(getModRefBehavior(Call));
438437 }
439438
440439 /// Checks if the specified function is known to only read from non-volatile
499498
500499 /// getModRefInfo (for call sites) - Return information about whether
501500 /// a particular call site modifies or reads the specified memory location.
502 ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc);
501 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc);
503502
504503 /// getModRefInfo (for call sites) - A convenience wrapper.
505 ModRefInfo getModRefInfo(ImmutableCallSite CS, const Value *P,
504 ModRefInfo getModRefInfo(const CallBase *Call, const Value *P,
506505 LocationSize Size) {
507 return getModRefInfo(CS, MemoryLocation(P, Size));
508 }
509
510 /// getModRefInfo (for calls) - Return information about whether
511 /// a particular call modifies or reads the specified memory location.
512 ModRefInfo getModRefInfo(const CallInst *C, const MemoryLocation &Loc) {
513 return getModRefInfo(ImmutableCallSite(C), Loc);
514 }
515
516 /// getModRefInfo (for calls) - A convenience wrapper.
517 ModRefInfo getModRefInfo(const CallInst *C, const Value *P,
518 LocationSize Size) {
519 return getModRefInfo(C, MemoryLocation(P, Size));
520 }
521
522 /// getModRefInfo (for invokes) - Return information about whether
523 /// a particular invoke modifies or reads the specified memory location.
524 ModRefInfo getModRefInfo(const InvokeInst *I, const MemoryLocation &Loc) {
525 return getModRefInfo(ImmutableCallSite(I), Loc);
526 }
527
528 /// getModRefInfo (for invokes) - A convenience wrapper.
529 ModRefInfo getModRefInfo(const InvokeInst *I, const Value *P,
530 LocationSize Size) {
531 return getModRefInfo(I, MemoryLocation(P, Size));
506 return getModRefInfo(Call, MemoryLocation(P, Size));
532507 }
533508
534509 /// getModRefInfo (for loads) - Return information about whether
625600 ModRefInfo getModRefInfo(const Instruction *I,
626601 const Optional &OptLoc) {
627602 if (OptLoc == None) {
628 if (auto CS = ImmutableCallSite(I)) {
629 return createModRefInfo(getModRefBehavior(CS));
603 if (const auto *Call = dyn_cast(I)) {
604 return createModRefInfo(getModRefBehavior(Call));
630605 }
631606 }
632607
660635
661636 /// Return information about whether a call and an instruction may refer to
662637 /// the same memory locations.
663 ModRefInfo getModRefInfo(Instruction *I, ImmutableCallSite Call);
638 ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call);
664639
665640 /// Return information about whether two call sites may refer to the same set
666641 /// of memory locations. See the AA documentation for details:
667642 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
668 ModRefInfo getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2);
643 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2);
669644
670645 /// Return information about whether a particular call site modifies
671646 /// or reads the specified memory location \p MemLoc before instruction \p I
776751 /// that these bits do not necessarily account for the overall behavior of
777752 /// the function, but rather only provide additional per-argument
778753 /// information.
779 virtual ModRefInfo getArgModRefInfo(ImmutableCallSite CS,
754 virtual ModRefInfo getArgModRefInfo(const CallBase *Call,
780755 unsigned ArgIdx) = 0;
781756
782757 /// Return the behavior of the given call site.
783 virtual FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) = 0;
758 virtual FunctionModRefBehavior getModRefBehavior(const CallBase *Call) = 0;
784759
785760 /// Return the behavior when calling the given function.
786761 virtual FunctionModRefBehavior getModRefBehavior(const Function *F) = 0;
787762
788763 /// getModRefInfo (for call sites) - Return information about whether
789764 /// a particular call site modifies or reads the specified memory location.
790 virtual ModRefInfo getModRefInfo(ImmutableCallSite CS,
765 virtual ModRefInfo getModRefInfo(const CallBase *Call,
791766 const MemoryLocation &Loc) = 0;
792767
793768 /// Return information about whether two call sites may refer to the same set
794769 /// of memory locations. See the AA documentation for details:
795770 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
796 virtual ModRefInfo getModRefInfo(ImmutableCallSite CS1,
797 ImmutableCallSite CS2) = 0;
771 virtual ModRefInfo getModRefInfo(const CallBase *Call1,
772 const CallBase *Call2) = 0;
798773
799774 /// @}
800775 };
826801 return Result.pointsToConstantMemory(Loc, OrLocal);
827802 }
828803
829 ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) override {
830 return Result.getArgModRefInfo(CS, ArgIdx);
831 }
832
833 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) override {
834 return Result.getModRefBehavior(CS);
804 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) override {
805 return Result.getArgModRefInfo(Call, ArgIdx);
806 }
807
808 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) override {
809 return Result.getModRefBehavior(Call);
835810 }
836811
837812 FunctionModRefBehavior getModRefBehavior(const Function *F) override {
838813 return Result.getModRefBehavior(F);
839814 }
840815
841 ModRefInfo getModRefInfo(ImmutableCallSite CS,
816 ModRefInfo getModRefInfo(const CallBase *Call,
842817 const MemoryLocation &Loc) override {
843 return Result.getModRefInfo(CS, Loc);
844 }
845
846 ModRefInfo getModRefInfo(ImmutableCallSite CS1,
847 ImmutableCallSite CS2) override {
848 return Result.getModRefInfo(CS1, CS2);
818 return Result.getModRefInfo(Call, Loc);
819 }
820
821 ModRefInfo getModRefInfo(const CallBase *Call1,
822 const CallBase *Call2) override {
823 return Result.getModRefInfo(Call1, Call2);
849824 }
850825 };
851826
900875 : CurrentResult.pointsToConstantMemory(Loc, OrLocal);
901876 }
902877
903 ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) {
904 return AAR ? AAR->getArgModRefInfo(CS, ArgIdx) : CurrentResult.getArgModRefInfo(CS, ArgIdx);
878 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
879 return AAR ? AAR->getArgModRefInfo(Call, ArgIdx)
880 : CurrentResult.getArgModRefInfo(Call, ArgIdx);
905881 }
906882
907 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) {
908 return AAR ? AAR->getModRefBehavior(CS) : CurrentResult.getModRefBehavior(CS);
883 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) {
884 return AAR ? AAR->getModRefBehavior(Call)
885 : CurrentResult.getModRefBehavior(Call);
909886 }
910887
911888 FunctionModRefBehavior getModRefBehavior(const Function *F) {
912889 return AAR ? AAR->getModRefBehavior(F) : CurrentResult.getModRefBehavior(F);
913890 }
914891
915 ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc) {
916 return AAR ? AAR->getModRefInfo(CS, Loc)
917 : CurrentResult.getModRefInfo(CS, Loc);
892 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc) {
893 return AAR ? AAR->getModRefInfo(Call, Loc)
894 : CurrentResult.getModRefInfo(Call, Loc);
918895 }
919896
920 ModRefInfo getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
921 return AAR ? AAR->getModRefInfo(CS1, CS2) : CurrentResult.getModRefInfo(CS1, CS2);
897 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2) {
898 return AAR ? AAR->getModRefInfo(Call1, Call2)
899 : CurrentResult.getModRefInfo(Call1, Call2);
922900 }
923901 };
924902
950928 return false;
951929 }
952930
953 ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) {
931 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
954932 return ModRefInfo::ModRef;
955933 }
956934
957 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) {
935 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) {
958936 return FMRB_UnknownModRefBehavior;
959937 }
960938
962940 return FMRB_UnknownModRefBehavior;
963941 }
964942
965 ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc) {
943 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc) {
966944 return ModRefInfo::ModRef;
967945 }
968946
969 ModRefInfo getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
947 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2) {
970948 return ModRefInfo::ModRef;
971949 }
972950 };
2020 #include "llvm/Analysis/AliasAnalysis.h"
2121 #include "llvm/Analysis/AssumptionCache.h"
2222 #include "llvm/Analysis/MemoryLocation.h"
23 #include "llvm/IR/CallSite.h"
23 #include "llvm/IR/InstrTypes.h"
2424 #include "llvm/IR/PassManager.h"
2525 #include "llvm/Pass.h"
2626 #include
8383
8484 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
8585
86 ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc);
87
88 ModRefInfo getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2);
86 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc);
87
88 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2);
8989
9090 /// Chases pointers until we find a (constant global) or not.
9191 bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal);
9292
9393 /// Get the location associated with a pointer argument of a callsite.
94 ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx);
94 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
9595
9696 /// Returns the behavior when calling the given call site.
97 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS);
97 FunctionModRefBehavior getModRefBehavior(const CallBase *Call);
9898
9999 /// Returns the behavior when calling the given function. For use when the
100100 /// call site is not known.
8787 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
8888
8989 using AAResultBase::getModRefInfo;
90 ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc);
90 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc);
9191
9292 /// getModRefBehavior - Return the behavior of the specified function if
9393 /// called from the specified call site. The call site may be null in which
9797 /// getModRefBehavior - Return the behavior of the specified function if
9898 /// called from the specified call site. The call site may be null in which
9999 /// case the most generic behavior of this function should be returned.
100 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS);
100 FunctionModRefBehavior getModRefBehavior(const CallBase *Call);
101101
102102 private:
103103 FunctionInfo *getFunctionInfo(const Function *F);
112112 void CollectSCCMembership(CallGraph &CG);
113113
114114 bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V);
115 ModRefInfo getModRefInfoForArgument(ImmutableCallSite CS,
115 ModRefInfo getModRefInfoForArgument(const CallBase *Call,
116116 const GlobalValue *GV);
117117 };
118118
3636 namespace llvm {
3737
3838 class AssumptionCache;
39 class CallSite;
4039 class DominatorTree;
4140 class Function;
4241 class Instruction;
397396 /// invalidated on the next non-local query or when an instruction is
398397 /// removed. Clients must copy this data if they want it around longer than
399398 /// that.
400 const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
399 const NonLocalDepInfo &getNonLocalCallDependency(CallBase *QueryCall);
401400
402401 /// Perform a full dependency query for an access to the QueryInst's
403402 /// specified memory location, returning the set of instructions that either
481480 void releaseMemory();
482481
483482 private:
484 MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
485 BasicBlock::iterator ScanIt,
486 BasicBlock *BB);
483 MemDepResult getCallDependencyFrom(CallBase *Call, bool isReadOnlyCall,
484 BasicBlock::iterator ScanIt,
485 BasicBlock *BB);
487486 bool getNonLocalPointerDepFromBB(Instruction *QueryInst,
488487 const PHITransAddr &Pointer,
489488 const MemoryLocation &Loc, bool isLoad,
1515 #ifndef LLVM_ANALYSIS_MEMORYLOCATION_H
1616 #define LLVM_ANALYSIS_MEMORYLOCATION_H
1717
18 #include "llvm/ADT/DenseMapInfo.h"
1819 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/DenseMapInfo.h"
20 #include "llvm/IR/CallSite.h"
20 #include "llvm/IR/Instructions.h"
2121 #include "llvm/IR/Metadata.h"
2222
2323 namespace llvm {
233233 static MemoryLocation getForDest(const AnyMemIntrinsic *MI);
234234
235235 /// Return a location representing a particular argument of a call.
236 static MemoryLocation getForArgument(ImmutableCallSite CS, unsigned ArgIdx,
236 static MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx,
237237 const TargetLibraryInfo *TLI);
238 static MemoryLocation getForArgument(ImmutableCallSite CS, unsigned ArgIdx,
238 static MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx,
239239 const TargetLibraryInfo &TLI) {
240 return getForArgument(CS, ArgIdx, &TLI);
240 return getForArgument(Call, ArgIdx, &TLI);
241241 }
242242
243243 explicit MemoryLocation(const Value *Ptr = nullptr,
5959 FunctionModRefBehavior getModRefBehavior(const Function *F);
6060
6161 using AAResultBase::getModRefInfo;
62 ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc);
62 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc);
6363 };
6464
6565 /// Analysis pass providing a never-invalidated alias analysis result.
1515 #define LLVM_ANALYSIS_SCOPEDNOALIASAA_H
1616
1717 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/IR/CallSite.h"
18 #include "llvm/IR/InstrTypes.h"
1919 #include "llvm/IR/PassManager.h"
2020 #include "llvm/Pass.h"
2121 #include
4040 }
4141
4242 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
43 ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc);
44 ModRefInfo getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2);
43 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc);
44 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2);
4545
4646 private:
4747 bool mayAliasInScopes(const MDNode *Scopes, const MDNode *NoAlias) const;
1616 #define LLVM_ANALYSIS_TYPEBASEDALIASANALYSIS_H
1717
1818 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/InstrTypes.h"
2020 #include "llvm/IR/PassManager.h"
2121 #include "llvm/Pass.h"
2222 #include
4242
4343 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
4444 bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal);
45 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS);
45 FunctionModRefBehavior getModRefBehavior(const CallBase *Call);
4646 FunctionModRefBehavior getModRefBehavior(const Function *F);
47 ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc);
48 ModRefInfo getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2);
47 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc);
48 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2);
4949
5050 private:
5151 bool Aliases(const MDNode *A, const MDNode *B) const;
296296
297297 /// This function returns call pointer argument that is considered the same by
298298 /// aliasing rules. You CAN'T use it to replace one value with another.
299 const Value *getArgumentAliasingToReturnedPointer(ImmutableCallSite CS);
300 inline Value *getArgumentAliasingToReturnedPointer(CallSite CS) {
301 return const_cast(
302 getArgumentAliasingToReturnedPointer(ImmutableCallSite(CS)));
299 const Value *getArgumentAliasingToReturnedPointer(const CallBase *Call);
300 inline Value *getArgumentAliasingToReturnedPointer(CallBase *Call) {
301 return const_cast(getArgumentAliasingToReturnedPointer(
302 const_cast(Call)));
303303 }
304304
305305 // {launder,strip}.invariant.group returns pointer that aliases its argument,
308308 // considered as capture. The arguments are not marked as returned neither,
309309 // because it would make it useless.
310310 bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
311 ImmutableCallSite CS);
311 const CallBase *Call);
312312
313313 /// This method strips off any GEP address adjustments and pointer casts from
314314 /// the specified value, returning the original object being addressed. Note
3939 #include "llvm/IR/Argument.h"
4040 #include "llvm/IR/Attributes.h"
4141 #include "llvm/IR/BasicBlock.h"
42 #include "llvm/IR/CallSite.h"
4342 #include "llvm/IR/Instruction.h"
4443 #include "llvm/IR/Instructions.h"
4544 #include "llvm/IR/Module.h"
117116 return false;
118117 }
119118
120 ModRefInfo AAResults::getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) {
119 ModRefInfo AAResults::getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
121120 ModRefInfo Result = ModRefInfo::ModRef;
122121
123122 for (const auto &AA : AAs) {
124 Result = intersectModRef(Result, AA->getArgModRefInfo(CS, ArgIdx));
123 Result = intersectModRef(Result, AA->getArgModRefInfo(Call, ArgIdx));
125124
126125 // Early-exit the moment we reach the bottom of the lattice.
127126 if (isNoModRef(Result))
131130 return Result;
132131 }
133132
134 ModRefInfo AAResults::getModRefInfo(Instruction *I, ImmutableCallSite Call) {
133 ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2) {
135134 // We may have two calls.
136 if (auto CS = ImmutableCallSite(I)) {
135 if (const auto *Call1 = dyn_cast(I)) {
137136 // Check if the two calls modify the same memory.
138 return getModRefInfo(CS, Call);
137 return getModRefInfo(Call1, Call2);
139138 } else if (I->isFenceLike()) {
140139 // If this is a fence, just return ModRef.
141140 return ModRefInfo::ModRef;
145144 // is that if the call references what this instruction
146145 // defines, it must be clobbered by this location.
147146 const MemoryLocation DefLoc = MemoryLocation::get(I);
148 ModRefInfo MR = getModRefInfo(Call, DefLoc);
147 ModRefInfo MR = getModRefInfo(Call2, DefLoc);
149148 if (isModOrRefSet(MR))
150149 return setModAndRef(MR);
151150 }
152151 return ModRefInfo::NoModRef;
153152 }
154153
155 ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS,
154 ModRefInfo AAResults::getModRefInfo(const CallBase *Call,
156155 const MemoryLocation &Loc) {
157156 ModRefInfo Result = ModRefInfo::ModRef;
158157
159158 for (const auto &AA : AAs) {
160 Result = intersectModRef(Result, AA->getModRefInfo(CS, Loc));
159 Result = intersectModRef(Result, AA->getModRefInfo(Call, Loc));
161160
162161 // Early-exit the moment we reach the bottom of the lattice.
163162 if (isNoModRef(Result))
166165
167166 // Try to refine the mod-ref info further using other API entry points to the
168167 // aggregate set of AA results.
169 auto MRB = getModRefBehavior(CS);
168 auto MRB = getModRefBehavior(Call);
170169 if (MRB == FMRB_DoesNotAccessMemory ||
171170 MRB == FMRB_OnlyAccessesInaccessibleMem)
172171 return ModRefInfo::NoModRef;
180179 bool IsMustAlias = true;
181180 ModRefInfo AllArgsMask = ModRefInfo::NoModRef;
182181 if (doesAccessArgPointees(MRB)) {
183 for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) {
182 for (auto AI = Call->arg_begin(), AE = Call->arg_end(); AI != AE; ++AI) {
184183 const Value *Arg = *AI;
185184 if (!Arg->getType()->isPointerTy())
186185 continue;
187 unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
188 MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, TLI);
186 unsigned ArgIdx = std::distance(Call->arg_begin(), AI);
187 MemoryLocation ArgLoc =
188 MemoryLocation::getForArgument(Call, ArgIdx, TLI);
189189 AliasResult ArgAlias = alias(ArgLoc, Loc);
190190 if (ArgAlias != NoAlias) {
191 ModRefInfo ArgMask = getArgModRefInfo(CS, ArgIdx);
191 ModRefInfo ArgMask = getArgModRefInfo(Call, ArgIdx);
192192 AllArgsMask = unionModRef(AllArgsMask, ArgMask);
193193 }
194194 // Conservatively clear IsMustAlias unless only MustAlias is found.
212212 return Result;
213213 }
214214
215 ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS1,
216 ImmutableCallSite CS2) {
215 ModRefInfo AAResults::getModRefInfo(const CallBase *Call1,
216 const CallBase *Call2) {
217217 ModRefInfo Result = ModRefInfo::ModRef;
218218
219219 for (const auto &AA : AAs) {
220 Result = intersectModRef(Result, AA->getModRefInfo(CS1, CS2));
220 Result = intersectModRef(Result, AA->getModRefInfo(Call1, Call2));
221221
222222 // Early-exit the moment we reach the bottom of the lattice.
223223 if (isNoModRef(Result))
227227 // Try to refine the mod-ref info further using other API entry points to the
228228 // aggregate set of AA results.
229229
230 // If CS1 or CS2 are readnone, they don't interact.
231 auto CS1B = getModRefBehavior(CS1);
232 if (CS1B == FMRB_DoesNotAccessMemory)
230 // If Call1 or Call2 are readnone, they don't interact.
231 auto Call1B = getModRefBehavior(Call1);
232 if (Call1B == FMRB_DoesNotAccessMemory)
233233 return ModRefInfo::NoModRef;
234234
235 auto CS2B = getModRefBehavior(CS2);
236 if (CS2B == FMRB_DoesNotAccessMemory)
235 auto Call2B = getModRefBehavior(Call2);
236 if (Call2B == FMRB_DoesNotAccessMemory)
237237 return ModRefInfo::NoModRef;
238238
239239 // If they both only read from memory, there is no dependence.
240 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
240 if (onlyReadsMemory(Call1B) && onlyReadsMemory(Call2B))
241241 return ModRefInfo::NoModRef;
242242
243 // If CS1 only reads memory, the only dependence on CS2 can be
244 // from CS1 reading memory written by CS2.
245 if (onlyReadsMemory(CS1B))
243 // If Call1 only reads memory, the only dependence on Call2 can be
244 // from Call1 reading memory written by Call2.
245 if (onlyReadsMemory(Call1B))
246246 Result = clearMod(Result);
247 else if (doesNotReadMemory(CS1B))
247 else if (doesNotReadMemory(Call1B))
248248 Result = clearRef(Result);
249249
250 // If CS2 only access memory through arguments, accumulate the mod/ref
251 // information from CS1's references to the memory referenced by
252 // CS2's arguments.
253 if (onlyAccessesArgPointees(CS2B)) {
254 if (!doesAccessArgPointees(CS2B))
250 // If Call2 only access memory through arguments, accumulate the mod/ref
251 // information from Call1's references to the memory referenced by
252 // Call2's arguments.
253 if (onlyAccessesArgPointees(Call2B)) {
254 if (!doesAccessArgPointees(Call2B))
255255 return ModRefInfo::NoModRef;
256256 ModRefInfo R = ModRefInfo::NoModRef;
257257 bool IsMustAlias = true;
258 for (auto I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
258 for (auto I = Call2->arg_begin(), E = Call2->arg_end(); I != E; ++I) {
259259 const Value *Arg = *I;
260260 if (!Arg->getType()->isPointerTy())
261261 continue;
262 unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I);
263 auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, TLI);
264
265 // ArgModRefCS2 indicates what CS2 might do to CS2ArgLoc, and the
266 // dependence of CS1 on that location is the inverse:
267 // - If CS2 modifies location, dependence exists if CS1 reads or writes.
268 // - If CS2 only reads location, dependence exists if CS1 writes.
269 ModRefInfo ArgModRefCS2 = getArgModRefInfo(CS2, CS2ArgIdx);
262 unsigned Call2ArgIdx = std::distance(Call2->arg_begin(), I);
263 auto Call2ArgLoc =
264 MemoryLocation::getForArgument(Call2, Call2ArgIdx, TLI);
265
266 // ArgModRefC2 indicates what Call2 might do to Call2ArgLoc, and the
267 // dependence of Call1 on that location is the inverse:
268 // - If Call2 modifies location, dependence exists if Call1 reads or
269 // writes.
270 // - If Call2 only reads location, dependence exists if Call1 writes.
271 ModRefInfo ArgModRefC2 = getArgModRefInfo(Call2, Call2ArgIdx);
270272 ModRefInfo ArgMask = ModRefInfo::NoModRef;
271 if (isModSet(ArgModRefCS2))
273 if (isModSet(ArgModRefC2))
272274 ArgMask = ModRefInfo::ModRef;
273 else if (isRefSet(ArgModRefCS2))
275 else if (isRefSet(ArgModRefC2))
274276 ArgMask = ModRefInfo::Mod;
275277
276 // ModRefCS1 indicates what CS1 might do to CS2ArgLoc, and we use
278 // ModRefC1 indicates what Call1 might do to Call2ArgLoc, and we use
277279 // above ArgMask to update dependence info.
278 ModRefInfo ModRefCS1 = getModRefInfo(CS1, CS2ArgLoc);
279 ArgMask = intersectModRef(ArgMask, ModRefCS1);
280 ModRefInfo ModRefC1 = getModRefInfo(Call1, Call2ArgLoc);
281 ArgMask = intersectModRef(ArgMask, ModRefC1);
280282
281283 // Conservatively clear IsMustAlias unless only MustAlias is found.
282 IsMustAlias &= isMustSet(ModRefCS1);
284 IsMustAlias &= isMustSet(ModRefC1);
283285
284286 R = intersectModRef(unionModRef(R, ArgMask), Result);
285287 if (R == Result) {
297299 return IsMustAlias ? setMust(R) : clearMust(R);
298300 }
299301
300 // If CS1 only accesses memory through arguments, check if CS2 references
301 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
302 if (onlyAccessesArgPointees(CS1B)) {
303 if (!doesAccessArgPointees(CS1B))
302 // If Call1 only accesses memory through arguments, check if Call2 references
303 // any of the memory referenced by Call1's arguments. If not, return NoModRef.
304 if (onlyAccessesArgPointees(Call1B)) {
305 if (!doesAccessArgPointees(Call1B))
304306 return ModRefInfo::NoModRef;
305307 ModRefInfo R = ModRefInfo::NoModRef;
306308 bool IsMustAlias = true;
307 for (auto I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
309 for (auto I = Call1->arg_begin(), E = Call1->arg_end(); I != E; ++I) {
308310 const Value *Arg = *I;
309311 if (!Arg->getType()->isPointerTy())
310312 continue;
311 unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I);
312 auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, TLI);
313
314 // ArgModRefCS1 indicates what CS1 might do to CS1ArgLoc; if CS1 might
315 // Mod CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If
316 // CS1 might Ref, then we care only about a Mod by CS2.
317 ModRefInfo ArgModRefCS1 = getArgModRefInfo(CS1, CS1ArgIdx);
318 ModRefInfo ModRefCS2 = getModRefInfo(CS2, CS1ArgLoc);
319 if ((isModSet(ArgModRefCS1) && isModOrRefSet(ModRefCS2)) ||
320 (isRefSet(ArgModRefCS1) && isModSet(ModRefCS2)))
321 R = intersectModRef(unionModRef(R, ArgModRefCS1), Result);
313 unsigned Call1ArgIdx = std::distance(Call1->arg_begin(), I);
314 auto Call1ArgLoc =
315 MemoryLocation::getForArgument(Call1, Call1ArgIdx, TLI);
316
317 // ArgModRefC1 indicates what Call1 might do to Call1ArgLoc; if Call1
318 // might Mod Call1ArgLoc, then we care about either a Mod or a Ref by
319 // Call2. If Call1 might Ref, then we care only about a Mod by Call2.
320 ModRefInfo ArgModRefC1 = getArgModRefInfo(Call1, Call1ArgIdx);
321 ModRefInfo ModRefC2 = getModRefInfo(Call2, Call1ArgLoc);
322 if ((isModSet(ArgModRefC1) && isModOrRefSet(ModRefC2)) ||
323 (isRefSet(ArgModRefC1) && isModSet(ModRefC2)))
324 R = intersectModRef(unionModRef(R, ArgModRefC1), Result);
322325
323326 // Conservatively clear IsMustAlias unless only MustAlias is found.
324 IsMustAlias &= isMustSet(ModRefCS2);
327 IsMustAlias &= isMustSet(ModRefC2);
325328
326329 if (R == Result) {
327330 // On early exit, not all args were checked, cannot set Must.
341344 return Result;
342345 }
343346
344 FunctionModRefBehavior AAResults::getModRefBehavior(ImmutableCallSite CS) {
347 FunctionModRefBehavior AAResults::getModRefBehavior(const CallBase *Call) {
345348 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
346349
347350 for (const auto &AA : AAs) {
348 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(CS));
351 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(Call));
349352
350353 // Early-exit the moment we reach the bottom of the lattice.
351354 if (Result == FMRB_DoesNotAccessMemory)
557560 isa(Object))
558561 return ModRefInfo::ModRef;
559562
560 ImmutableCallSite CS(I);
561 if (!CS.getInstruction() || CS.getInstruction() == Object)
563 const auto *Call = dyn_cast(I);
564 if (!Call || Call == Object)
562565 return ModRefInfo::ModRef;
563566
564567 if (PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
571574 ModRefInfo R = ModRefInfo::NoModRef;
572575 bool IsMustAlias = true;
573576 // Set flag only if no May found and all operands processed.
574 for (auto CI = CS.data_operands_begin(), CE = CS.data_operands_end();
577 for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
575578 CI != CE; ++CI, ++ArgNo) {
576579 // Only look at the no-capture or byval pointer arguments. If this
577580 // pointer were passed to arguments that were neither of these, then it
578581 // couldn't be no-capture.
579582 if (!(*CI)->getType()->isPointerTy() ||
580 (!CS.doesNotCapture(ArgNo) &&
581 ArgNo < CS.getNumArgOperands() && !CS.isByValArgument(ArgNo)))
583 (!Call->doesNotCapture(ArgNo) && ArgNo < Call->getNumArgOperands() &&
584 !Call->isByValArgument(ArgNo)))
582585 continue;
583586
584587 AliasResult AR = alias(MemoryLocation(*CI), MemoryLocation(Object));
590593 IsMustAlias = false;
591594 if (AR == NoAlias)
592595 continue;
593 if (CS.doesNotAccessMemory(ArgNo))
596 if (Call->doesNotAccessMemory(ArgNo))
594597 continue;
595 if (CS.onlyReadsMemory(ArgNo)) {
598 if (Call->onlyReadsMemory(ArgNo)) {
596599 R = ModRefInfo::Ref;
597600 continue;
598601 }
774777 }
775778
776779 bool llvm::isNoAliasCall(const Value *V) {
777 if (auto CS = ImmutableCallSite(V))
778 return CS.hasRetAttr(Attribute::NoAlias);
780 if (const auto *Call = dyn_cast(V))
781 return Call->hasRetAttr(Attribute::NoAlias);
779782 return false;
780783 }
781784
6565 }
6666 }
6767
68 static inline void PrintModRefResults(const char *Msg, bool P, CallSite CSA,
69 CallSite CSB, Module *M) {
68 static inline void PrintModRefResults(const char *Msg, bool P, CallBase *CallA,
69 CallBase *CallB, Module *M) {
7070 if (PrintAll || P) {
71 errs() << " " << Msg << ": " << *CSA.getInstruction() << " <-> "
72 << *CSB.getInstruction() << '\n';
71 errs() << " " << Msg << ": " << *CallA << " <-> " << *CallB << '\n';
7372 }
7473 }
7574
9796 ++FunctionCount;
9897
9998 SetVector Pointers;
100 SmallSetVectorSite, 16> CallSites;
99 SmallSetVectorBase *, 16> Calls;
101100 SetVector Loads;
102101 SetVector Stores;
103102
113112 if (EvalAAMD && isa(&*I))
114113 Stores.insert(&*I);
115114 Instruction &Inst = *I;
116 if (auto CS = CallSite(&Inst)) {
117 Value *Callee = CS.getCalledValue();
115 if (auto *Call = dyn_cast(&Inst)) {
116 Value *Callee = Call->getCalledValue();
118117 // Skip actual functions for direct function calls.
119118 if (!isa(Callee) && isInterestingPointer(Callee))
120119 Pointers.insert(Callee);
121120 // Consider formals.
122 for (Use &DataOp : CS.data_ops())
121 for (Use &DataOp : Call->data_ops())
123122 if (isInterestingPointer(DataOp))
124123 Pointers.insert(DataOp);
125 CallSites.insert(CS);
124 Calls.insert(Call);
126125 } else {
127126 // Consider all operands.
128127 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
135134 if (PrintAll || PrintNoAlias || PrintMayAlias || PrintPartialAlias ||
136135 PrintMustAlias || PrintNoModRef || PrintMod || PrintRef || PrintModRef)
137136 errs() << "Function: " << F.getName() << ": " << Pointers.size()
138 << " pointers, " << CallSites.size() << " call sites\n";
137 << " pointers, " << Calls.size() << " call sites\n";
139138
140139 // iterate over the worklist, and run the full (n^2)/2 disambiguations
141140 for (SetVector::iterator I1 = Pointers.begin(), E = Pointers.end();
229228 }
230229
231230 // Mod/ref alias analysis: compare all pairs of calls and values
232 for (CallSite C : CallSites) {
233 Instruction *I = C.getInstruction();
234
231 for (CallBase *Call : Calls) {
235232 for (auto Pointer : Pointers) {
236233 auto Size = LocationSize::unknown();
237234 Type *ElTy = cast(Pointer->getType())->getElementType();
238235 if (ElTy->isSized())
239236 Size = LocationSize::precise(DL.getTypeStoreSize(ElTy));
240237
241 switch (AA.getModRefInfo(C, Pointer, Size)) {
238 switch (AA.getModRefInfo(Call, Pointer, Size)) {
242239 case ModRefInfo::NoModRef:
243 PrintModRefResults("NoModRef", PrintNoModRef, I, Pointer,
240 PrintModRefResults("NoModRef", PrintNoModRef, Call, Pointer,
244241 F.getParent());
245242 ++NoModRefCount;
246243 break;
247244 case ModRefInfo::Mod:
248 PrintModRefResults("Just Mod", PrintMod, I, Pointer, F.getParent());
245 PrintModRefResults("Just Mod", PrintMod, Call, Pointer, F.getParent());
249246 ++ModCount;
250247 break;
251248 case ModRefInfo::Ref:
252 PrintModRefResults("Just Ref", PrintRef, I, Pointer, F.getParent());
249 PrintModRefResults("Just Ref", PrintRef, Call, Pointer, F.getParent());
253250 ++RefCount;
254251 break;
255252 case ModRefInfo::ModRef:
256 PrintModRefResults("Both ModRef", PrintModRef, I, Pointer,
253 PrintModRefResults("Both ModRef", PrintModRef, Call, Pointer,
257254 F.getParent());
258255 ++ModRefCount;
259256 break;
260257 case ModRefInfo::Must:
261 PrintModRefResults("Must", PrintMust, I, Pointer, F.getParent());
258 PrintModRefResults("Must", PrintMust, Call, Pointer, F.getParent());
262259 ++MustCount;
263260 break;
264261 case ModRefInfo::MustMod:
265 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, I, Pointer,
262 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, Call, Pointer,
266263 F.getParent());
267264 ++MustModCount;
268265 break;
269266 case ModRefInfo::MustRef:
270 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, I, Pointer,
267 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, Call, Pointer,
271268 F.getParent());
272269 ++MustRefCount;
273270 break;
274271 case ModRefInfo::MustModRef:
275 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, I,
272 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, Call,
276273 Pointer, F.getParent());
277274 ++MustModRefCount;
278275 break;
281278 }
282279
283280 // Mod/ref alias analysis: compare all pairs of calls
284 for (auto C = CallSites.begin(), Ce = CallSites.end(); C != Ce; ++C) {
285 for (auto D = CallSites.begin(); D != Ce; ++D) {
286 if (D == C)
281 for (CallBase *CallA : Calls) {
282 for (CallBase *CallB : Calls) {
283 if (CallA == CallB)
287284 continue;
288 switch (AA.getModRefInfo(*C, *D)) {
285 switch (AA.getModRefInfo(CallA, CallB)) {
289286 case ModRefInfo::NoModRef:
290 PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent());
287 PrintModRefResults("NoModRef", PrintNoModRef, CallA, CallB,
288 F.getParent());
291289 ++NoModRefCount;
292290 break;
293291 case ModRefInfo::Mod:
294 PrintModRefResults("Just Mod", PrintMod, *C, *D, F.getParent());
292 PrintModRefResults("Just Mod", PrintMod, CallA, CallB, F.getParent());
295293 ++ModCount;
296294 break;
297295 case ModRefInfo::Ref:
298 PrintModRefResults("Just Ref", PrintRef, *C, *D, F.getParent());
296 PrintModRefResults("Just Ref", PrintRef, CallA, CallB, F.getParent());
299297 ++RefCount;
300298 break;
301299 case ModRefInfo::ModRef:
302 PrintModRefResults("Both ModRef", PrintModRef, *C, *D, F.getParent());
300 PrintModRefResults("Both ModRef", PrintModRef, CallA, CallB,
301 F.getParent());
303302 ++ModRefCount;
304303 break;
305304 case ModRefInfo::Must:
306 PrintModRefResults("Must", PrintMust, *C, *D, F.getParent());
305 PrintModRefResults("Must", PrintMust, CallA, CallB, F.getParent());
307306 ++MustCount;
308307 break;
309308 case ModRefInfo::MustMod:
310 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, *C, *D,
309 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, CallA, CallB,
311310 F.getParent());
312311 ++MustModCount;
313312 break;
314313 case ModRefInfo::MustRef:
315 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, *C, *D,
314 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, CallA, CallB,
316315 F.getParent());
317316 ++MustRefCount;
318317 break;
319318 case ModRefInfo::MustModRef:
320 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, *C, *D,
321 F.getParent());
319 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, CallA,
320 CallB, F.getParent());
322321 ++MustModRefCount;
323322 break;
324323 }
1515 #include "llvm/Analysis/GuardUtils.h"
1616 #include "llvm/Analysis/MemoryLocation.h"
1717 #include "llvm/Config/llvm-config.h"
18 #include "llvm/IR/CallSite.h"
1918 #include "llvm/IR/Constants.h"
2019 #include "llvm/IR/DataLayout.h"
2120 #include "llvm/IR/Function.h"
235234
236235 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
237236 if (auto *UnknownInst = getUnknownInst(i)) {
238 ImmutableCallSite C1(UnknownInst), C2(Inst);
237 const auto *C1 = dyn_cast(UnknownInst);
238 const auto *C2 = dyn_cast(Inst);
239239 if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
240240 isModOrRefSet(AA.getModRefInfo(C2, C1)))
241241 return true;
445445 return add(MTI);
446446
447447 // Handle all calls with known mod/ref sets genericall
448 CallSite CS(I);
449 if (CS && CS.onlyAccessesArgMemory()) {
450 auto getAccessFromModRef = [](ModRefInfo MRI) {
451 if (isRefSet(MRI) && isModSet(MRI))
452 return AliasSet::ModRefAccess;
453 else if (isModSet(MRI))
454 return AliasSet::ModAccess;
455 else if (isRefSet(MRI))
456 return AliasSet::RefAccess;
457 else
458 return AliasSet::NoAccess;
459
460 };
461
462 ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(CS));
463
464 // Some intrinsics are marked as modifying memory for control flow
465 // modelling purposes, but don't actually modify any specific memory
466 // location.
467 using namespace PatternMatch;
468 if (I->use_empty() && match(I, m_Intrinsic()))
469 CallMask = clearMod(CallMask);
470
471 for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) {
472 const Value *Arg = *AI;
473 if (!Arg->getType()->isPointerTy())
474 continue;
475 unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
476 MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx,
477 nullptr);
478 ModRefInfo ArgMask = AA.getArgModRefInfo(CS, ArgIdx);
479 ArgMask = intersectModRef(CallMask, ArgMask);
480 if (!isNoModRef(ArgMask))
481 addPointer(ArgLoc, getAccessFromModRef(ArgMask));
482 }
483 return;
484 }
485
448 if (auto *Call = dyn_cast(I))
449 if (Call->onlyAccessesArgMemory()) {
450 auto getAccessFromModRef = [](ModRefInfo MRI) {
451 if (isRefSet(MRI) && isModSet(MRI))
452 return AliasSet::ModRefAccess;
453 else if (isModSet(MRI))
454 return AliasSet::ModAccess;
455 else if (isRefSet(MRI))
456 return AliasSet::RefAccess;
457 else
458 return AliasSet::NoAccess;
459 };
460
461 ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(Call));
462
463 // Some intrinsics are marked as modifying memory for control flow
464 // modelling purposes, but don't actually modify any specific memory
465 // location.
466 using namespace PatternMatch;
467 if (Call->use_empty() &&
468 match(Call, m_Intrinsic()))
469 CallMask = clearMod(CallMask);
470
471 for (auto IdxArgPair : enumerate(Call->args())) {
472 int ArgIdx = IdxArgPair.index();
473 const Value *Arg = IdxArgPair.value();
474 if (!Arg->getType()->isPointerTy())
475 continue;
476 MemoryLocation ArgLoc =
477 MemoryLocation::getForArgument(Call, ArgIdx, nullptr);
478 ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);
479 ArgMask = intersectModRef(CallMask, ArgMask);
480 if (!isNoModRef(ArgMask))
481 addPointer(ArgLoc, getAccessFromModRef(ArgMask));
482 }
483 return;
484 }
485
486486 return addUnknown(I);
487487 }
488488
3030 #include "llvm/Analysis/PhiValues.h"
3131 #include "llvm/IR/Argument.h"
3232 #include "llvm/IR/Attributes.h"
33 #include "llvm/IR/CallSite.h"
3433 #include "llvm/IR/Constant.h"
3534 #include "llvm/IR/Constants.h"
3635 #include "llvm/IR/DataLayout.h"
143142 /// Returns true if the pointer is one which would have been considered an
144143 /// escape by isNonEscapingLocalObject.
145144 static bool isEscapeSource(const Value *V) {
146 if (ImmutableCallSite(V))
145 if (isa(V))
147146 return true;
148147
149148 if (isa(V))
453452
454453 const GEPOperator *GEPOp = dyn_cast(Op);
455454 if (!GEPOp) {
456 if (auto CS = ImmutableCallSite(V)) {
455 if (const auto *Call = dyn_cast(V)) {
457456 // CaptureTracking can know about special capturing properties of some
458457 // intrinsics like launder.invariant.group, that can't be expressed with
459458 // the attributes, but have properties like returning aliasing pointer.
463462 // because it should be in sync with CaptureTracking. Not using it may
464463 // cause weird miscompilations where 2 aliasing pointers are assumed to
465464 // noalias.
466 if (auto *RP = getArgumentAliasingToReturnedPointer(CS)) {
465 if (auto *RP = getArgumentAliasingToReturnedPointer(Call)) {
467466 V = RP;
468467 continue;
469468 }
672671 }
673672
674673 /// Returns the behavior when calling the given call site.
675 FunctionModRefBehavior BasicAAResult::getModRefBehavior(ImmutableCallSite CS) {
676 if (CS.doesNotAccessMemory())
674 FunctionModRefBehavior BasicAAResult::getModRefBehavior(const CallBase *Call) {
675 if (Call->doesNotAccessMemory())
677676 // Can't do better than this.
678677 return FMRB_DoesNotAccessMemory;
679678
681680
682681 // If the callsite knows it only reads memory, don't return worse
683682 // than that.
684 if (CS.onlyReadsMemory())
683 if (Call->onlyReadsMemory())
685684 Min = FMRB_OnlyReadsMemory;
686 else if (CS.doesNotReadMemory())
685 else if (Call->doesNotReadMemory())
687686 Min = FMRB_DoesNotReadMemory;
688687
689 if (CS.onlyAccessesArgMemory())
688 if (Call->onlyAccessesArgMemory())
690689 Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
691 else if (CS.onlyAccessesInaccessibleMemory())
690 else if (Call->onlyAccessesInaccessibleMemory())
692691 Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleMem);
693 else if (CS.onlyAccessesInaccessibleMemOrArgMem())
692 else if (Call->onlyAccessesInaccessibleMemOrArgMem())
694693 Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleOrArgMem);
695694
696 // If CS has operand bundles then aliasing attributes from the function it
697 // calls do not directly apply to the CallSite. This can be made more
698 // precise in the future.
699 if (!CS.hasOperandBundles())
700 if (const Function *F = CS.getCalledFunction())
695 // If the call has operand bundles then aliasing attributes from the function
696 // it calls do not directly apply to the call. This can be made more precise
697 // in the future.
698 if (!Call->hasOperandBundles())
699 if (const Function *F = Call->getCalledFunction())
701700 Min =
702701 FunctionModRefBehavior(Min & getBestAAResults().getModRefBehavior(F));
703702
730729 }
731730
732731 /// Returns true if this is a writeonly (i.e Mod only) parameter.
733 static bool isWriteOnlyParam(ImmutableCallSite CS, unsigned ArgIdx,
732 static bool isWriteOnlyParam(const CallBase *Call, unsigned ArgIdx,
734733 const TargetLibraryInfo &TLI) {
735 if (CS.paramHasAttr(ArgIdx, Attribute::WriteOnly))
734 if (Call->paramHasAttr(ArgIdx, Attribute::WriteOnly))
736735 return true;
737736
738737 // We can bound the aliasing properties of memset_pattern16 just as we can
742741 // FIXME Consider handling this in InferFunctionAttr.cpp together with other
743742 // attributes.
744743 LibFunc F;
745 if (CS.getCalledFunction() && TLI.getLibFunc(*CS.getCalledFunction(), F) &&
744 if (Call->getCalledFunction() &&
745 TLI.getLibFunc(*Call->getCalledFunction(), F) &&
746746 F == LibFunc_memset_pattern16 && TLI.has(F))
747747 if (ArgIdx == 0)
748748 return true;
754754 return false;
755755 }
756756
757 ModRefInfo BasicAAResult::getArgModRefInfo(ImmutableCallSite CS,
757 ModRefInfo BasicAAResult::getArgModRefInfo(const CallBase *Call,
758758 unsigned ArgIdx) {
759759 // Checking for known builtin intrinsics and target library functions.
760 if (isWriteOnlyParam(CS, ArgIdx, TLI))
760 if (isWriteOnlyParam(Call, ArgIdx, TLI))
761761 return ModRefInfo::Mod;
762762
763 if (CS.paramHasAttr(ArgIdx, Attribute::ReadOnly))
763 if (Call->paramHasAttr(ArgIdx, Attribute::ReadOnly))
764764 return ModRefInfo::Ref;
765765
766 if (CS.paramHasAttr(ArgIdx, Attribute::ReadNone))
766 if (Call->paramHasAttr(ArgIdx, Attribute::ReadNone))
767767 return ModRefInfo::NoModRef;
768768
769 return AAResultBase::getArgModRefInfo(CS, ArgIdx);
770 }
771
772 static bool isIntrinsicCall(ImmutableCallSite CS, Intrinsic::ID IID) {
773 const IntrinsicInst *II = dyn_cast(CS.getInstruction());
769 return AAResultBase::getArgModRefInfo(Call, ArgIdx);
770 }
771
772 static bool isIntrinsicCall(const CallBase *Call, Intrinsic::ID IID) {
773 const IntrinsicInst *II = dyn_cast(Call);
774774 return II && II->getIntrinsicID() == IID;
775775 }
776776
826826 /// Since we only look at local properties of this function, we really can't
827827 /// say much about this query. We do, however, use simple "address taken"
828828 /// analysis on local objects.
829 ModRefInfo BasicAAResult::getModRefInfo(ImmutableCallSite CS,
829 ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call,
830830 const MemoryLocation &Loc) {
831 assert(notDifferentParent(CS.getInstruction(), Loc.Ptr) &&
831 assert(notDifferentParent(Call, Loc.Ptr) &&
832832 "AliasAnalysis query involving multiple functions!");
833833
834834 const Value *Object = GetUnderlyingObject(Loc.Ptr, DL);
839839 // contents of the alloca into argument registers or stack slots, so there is
840840 // no lifetime issue.
841841 if (isa(Object))
842 if (const CallInst *CI = dyn_cast(CS.getInstruction()))
842 if (const CallInst *CI = dyn_cast(Call))
843843 if (CI->isTailCall() &&
844844 !CI->getAttributes().hasAttrSomewhere(Attribute::ByVal))
845845 return ModRefInfo::NoModRef;
847847 // Stack restore is able to modify unescaped dynamic allocas. Assume it may
848848 // modify them even though the alloca is not escaped.
849849 if (auto *AI = dyn_cast(Object))
850 if (!AI->isStaticAlloca() && isIntrinsicCall(CS, Intrinsic::stackrestore))
850 if (!AI->isStaticAlloca() && isIntrinsicCall(Call, Intrinsic::stackrestore))
851851 return ModRefInfo::Mod;
852852
853853 // If the pointer is to a locally allocated object that does not escape,
854854 // then the call can not mod/ref the pointer unless the call takes the pointer
855855 // as an argument, and itself doesn't capture it.
856 if (!isa(Object) && CS.getInstruction() != Object &&
856 if (!isa(Object) && Call != Object &&
857857 isNonEscapingLocalObject(Object)) {
858858
859859 // Optimistically assume that call doesn't touch Object and check this
862862 bool IsMustAlias = true;
863863
864864 unsigned OperandNo = 0;
865 for (auto CI = CS.data_operands_begin(), CE = CS.data_operands_end();
865 for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
866866 CI != CE; ++CI, ++OperandNo) {
867867 // Only look at the no-capture or byval pointer arguments. If this
868868 // pointer were passed to arguments that were neither of these, then it
869869 // couldn't be no-capture.
870870 if (!(*CI)->getType()->isPointerTy() ||
871 (!CS.doesNotCapture(OperandNo) &&
872 OperandNo < CS.getNumArgOperands() && !CS.isByValArgument(OperandNo)))
871 (!Call->doesNotCapture(OperandNo) &&
872 OperandNo < Call->getNumArgOperands() &&
873 !Call->isByValArgument(OperandNo)))
873874 continue;
874875
875876 // Call doesn't access memory through this operand, so we don't care
876877 // if it aliases with Object.
877 if (CS.doesNotAccessMemory(OperandNo))
878 if (Call->doesNotAccessMemory(OperandNo))
878879 continue;
879880
880881 // If this is a no-capture pointer argument, see if we can tell that it
888889 continue;
889890 // Operand aliases 'Object', but call doesn't modify it. Strengthen
890891 // initial assumption and keep looking in case if there are more aliases.
891 if (CS.onlyReadsMemory(OperandNo)) {
892 if (Call->onlyReadsMemory(OperandNo)) {
892893 Result = setRef(Result);
893894 continue;
894895 }
895896 // Operand aliases 'Object' but call only writes into it.
896 if (CS.doesNotReadMemory(OperandNo)) {
897 if (Call->doesNotReadMemory(OperandNo)) {
897898 Result = setMod(Result);
898899 continue;
899900 }
917918 }
918919 }
919920
920 // If the CallSite is to malloc or calloc, we can assume that it doesn't
921 // If the call is to malloc or calloc, we can assume that it doesn't
921922 // modify any IR visible value. This is only valid because we assume these
922923 // routines do not read values visible in the IR. TODO: Consider special
923924 // casing realloc and strdup routines which access only their arguments as
924925 // well. Or alternatively, replace all of this with inaccessiblememonly once
925926 // that's implemented fully.
926 auto *Inst = CS.getInstruction();
927 if (isMallocOrCallocLikeFn(Inst, &TLI)) {
927 if (isMallocOrCallocLikeFn(Call, &TLI)) {
928928 // Be conservative if the accessed pointer may alias the allocation -
929929 // fallback to the generic handling below.
930 if (getBestAAResults().alias(MemoryLocation(Inst), Loc) == NoAlias)
930 if (getBestAAResults().alias(MemoryLocation(Call), Loc) == NoAlias)
931931 return ModRefInfo::NoModRef;
932932 }
933933
935935 // operands, i.e., source and destination of any given memcpy must no-alias.
936936 // If Loc must-aliases either one of these two locations, then it necessarily
937937 // no-aliases the other.
938 if (auto *Inst = dyn_cast(CS.getInstruction())) {
938 if (auto *Inst = dyn_cast(Call)) {
939939 AliasResult SrcAA, DestAA;
940940
941941 if ((SrcAA = getBestAAResults().alias(MemoryLocation::getForSource(Inst),
959959 // While the assume intrinsic is marked as arbitrarily writing so that
960960 // proper control dependencies will be maintained, it never aliases any
961961 // particular memory location.
962 if (isIntrinsicCall(CS, Intrinsic::assume))
962 if (isIntrinsicCall(Call, Intrinsic::assume))
963963 return ModRefInfo::NoModRef;
964964
965965 // Like assumes, guard intrinsics are also marked as arbitrarily writing so
969969 // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
970970 // heap state at the point the guard is issued needs to be consistent in case
971971 // the guard invokes the "deopt" continuation.
972 if (isIntrinsicCall(CS, Intrinsic::experimental_guard))
972 if (isIntrinsicCall(Call, Intrinsic::experimental_guard))
973973 return ModRefInfo::Ref;
974974
975975 // Like assumes, invariant.start intrinsics were also marked as arbitrarily
995995 // The transformation will cause the second store to be ignored (based on
996996 // rules of invariant.start) and print 40, while the first program always
997997 // prints 50.
998 if (isIntrinsicCall(CS, Intrinsic::invariant_start))
998 if (isIntrinsicCall(Call, Intrinsic::invariant_start))
999999 return ModRefInfo::Ref;
10001000
10011001 // The AAResultBase base class has some smarts, lets use them.
1002 return AAResultBase::getModRefInfo(CS, Loc);
1003 }
1004
1005 ModRefInfo BasicAAResult::getModRefInfo(ImmutableCallSite CS1,
1006 ImmutableCallSite CS2) {
1002 return AAResultBase::getModRefInfo(Call, Loc);
1003 }
1004
1005 ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call1,
1006 const CallBase *Call2) {
10071007 // While the assume intrinsic is marked as arbitrarily writing so that
10081008 // proper control dependencies will be maintained, it never aliases any
10091009 // particular memory location.
1010 if (isIntrinsicCall(CS1, Intrinsic::assume) ||
1011 isIntrinsicCall(CS2, Intrinsic::assume))
1010 if (isIntrinsicCall(Call1, Intrinsic::assume) ||
1011 isIntrinsicCall(Call2, Intrinsic::assume))
10121012 return ModRefInfo::NoModRef;
10131013
10141014 // Like assumes, guard intrinsics are also marked as arbitrarily writing so
10221022 // NB! This function is *not* commutative, so we specical case two
10231023 // possibilities for guard intrinsics.
10241024
1025 if (isIntrinsicCall(CS1, Intrinsic::experimental_guard))
1026 return isModSet(createModRefInfo(getModRefBehavior(CS2)))
1025 if (isIntrinsicCall(Call1, Intrinsic::experimental_guard))
1026 return isModSet(createModRefInfo(getModRefBehavior(Call2)))
10271027 ? ModRefInfo::Ref
10281028 : ModRefInfo::NoModRef;
10291029
1030 if (isIntrinsicCall(CS2, Intrinsic::experimental_guard))
1031 return isModSet(createModRefInfo(getModRefBehavior(CS1)))
1030 if (isIntrinsicCall(Call2, Intrinsic::experimental_guard))
1031 return isModSet(createModRefInfo(getModRefBehavior(Call1)))
10321032 ? ModRefInfo::Mod
10331033 : ModRefInfo::NoModRef;
10341034
10351035 // The AAResultBase base class has some smarts, lets use them.
1036 return AAResultBase::getModRefInfo(CS1, CS2);
1036 return AAResultBase::getModRefInfo(Call1, Call2);
10371037 }
10381038
10391039 /// Provide ad-hoc rules to disambiguate accesses through two GEP operators,
2222 #include "llvm/Analysis/CFG.h"
2323 #include "llvm/Analysis/OrderedBasicBlock.h"
2424 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/IR/CallSite.h"
2625 #include "llvm/IR/Constants.h"
2726 #include "llvm/IR/Dominators.h"
2827 #include "llvm/IR/Instructions.h"
239238 switch (I->getOpcode()) {
240239 case Instruction::Call:
241240 case Instruction::Invoke: {
242 CallSite CS(I);
241 auto *Call = cast(I);
243242 // Not captured if the callee is readonly, doesn't return a copy through
244243 // its return value and doesn't unwind (a readonly function can leak bits
245244 // by throwing an exception or not depending on the input value).
246 if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy())
245 if (Call->onlyReadsMemory() && Call->doesNotThrow() &&
246 Call->getType()->isVoidTy())
247247 break;
248248
249249 // The pointer is not captured if returned pointer is not captured.
251251 // marked with nocapture do not capture. This means that places like
252252 // GetUnderlyingObject in ValueTracking or DecomposeGEPExpression
253253 // in BasicAA also need to know about this property.
254 if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(CS)) {
255 AddUses(I);
254 if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(Call)) {
255 AddUses(Call);
256256 break;
257257 }
258258
259259 // Volatile operations effectively capture the memory location that they
260260 // load and store to.
261 if (auto *MI = dyn_cast(I))
261 if (auto *MI = dyn_cast(Call))
262262 if (MI->isVolatile())
263263 if (Tracker->captured(U))
264264 return;
270270 // that loading a value from a pointer does not cause the pointer to be
271271 // captured, even though the loaded value might be the pointer itself
272272 // (think of self-referential objects).
273 CallSite::data_operand_iterator B =
274 CS.data_operands_begin(), E = CS.data_operands_end();
275 for (CallSite::data_operand_iterator A = B; A != E; ++A)
276 if (A->get() == V && !CS.doesNotCapture(A - B))
273 for (auto IdxOpPair : enumerate(Call->data_ops())) {
274 int Idx = IdxOpPair.index();
275 Value *A = IdxOpPair.value();
276 if (A == V && !Call->doesNotCapture(Idx))
277277 // The parameter is not marked 'nocapture' - captured.
278278 if (Tracker->captured(U))
279279 return;
280 }
280281 break;
281282 }
282283 case Instruction::Load:
254254 }
255255
256256 FunctionModRefBehavior
257 GlobalsAAResult::getModRefBehavior(ImmutableCallSite CS) {
257 GlobalsAAResult::getModRefBehavior(const CallBase *Call) {
258258 FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
259259
260 if (!CS.hasOperandBundles())
261 if (const Function *F = CS.getCalledFunction())
260 if (!Call->hasOperandBundles())
261 if (const Function *F = Call->getCalledFunction())
262262 if (FunctionInfo *FI = getFunctionInfo(F)) {
263263 if (!isModOrRefSet(FI->getModRefInfo()))
264264 Min = FMRB_DoesNotAccessMemory;
266266 Min = FMRB_OnlyReadsMemory;
267267 }
268268
269 return FunctionModRefBehavior(AAResultBase::getModRefBehavior(CS) & Min);
269 return FunctionModRefBehavior(AAResultBase::getModRefBehavior(Call) & Min);
270270 }
271271
272272 /// Returns the function info for the function, or null if we don't have
365365 } else if (Operator::getOpcode(I) == Instruction::BitCast) {
366366 if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
367367 return true;
368 } else if (auto CS = CallSite(I)) {
368 } else if (auto *Call = dyn_cast(I)) {
369369 // Make sure that this is just the function being called, not that it is
370370 // passing into the function.
371 if (CS.isDataOperand(&U)) {
371 if (Call->isDataOperand(&U)) {
372372 // Detect calls to free.
373 if (CS.isArgOperand(&U) && isFreeCall(I, &TLI)) {
373 if (Call->isArgOperand(&U) && isFreeCall(I, &TLI)) {
374374 if (Writers)
375 Writers->insert(CS->getParent()->getParent());
375 Writers->insert(Call->getParent()->getParent());
376376 } else {
377377 return true; // Argument of an unknown call.
378378 }
575575
576576 // We handle calls specially because the graph-relevant aspects are
577577 // handled above.
578 if (auto CS = CallSite(&I)) {
579 if (isAllocationFn(&I, &TLI) || isFreeCall(&I, &TLI)) {
578 if (auto *Call = dyn_cast(&I)) {
579 if (isAllocationFn(Call, &TLI) || isFreeCall(Call, &TLI)) {
580580 // FIXME: It is completely unclear why this is necessary and not
581581 // handled by the above graph code.
582582 FI.addModRefInfo(ModRefInfo::ModRef);
583 } else if (Function *Callee = CS.getCalledFunction()) {
583 } else if (Function *Callee = Call->getCalledFunction()) {
584584 // The callgraph doesn't include intrinsic calls.
585585 if (Callee->isIntrinsic()) {
586 if (isa(I))
586 if (isa(Call))
587587 // Don't let dbg intrinsics affect alias info.
588588 continue;
589589
884884 return AAResultBase::alias(LocA, LocB);
885885 }
886886
887 ModRefInfo GlobalsAAResult::getModRefInfoForArgument(ImmutableCallSite CS,
887 ModRefInfo GlobalsAAResult::getModRefInfoForArgument(const CallBase *Call,
888888 const GlobalValue *GV) {
889 if (CS.doesNotAccessMemory())
889 if (Call->doesNotAccessMemory())
890890 return ModRefInfo::NoModRef;
891891 ModRefInfo ConservativeResult =
892 CS.onlyReadsMemory() ? ModRefInfo::Ref : ModRefInfo::ModRef;
892 Call->onlyReadsMemory() ? ModRefInfo::Ref : ModRefInfo::ModRef;
893893
894894 // Iterate through all the arguments to the called function. If any argument
895895 // is based on GV, return the conservative result.
896 for (auto &A : CS.args()) {
896 for (auto &A : Call->args()) {
897897 SmallVector Objects;
898898 GetUnderlyingObjects(A, Objects, DL);
899899
913913 return ModRefInfo::NoModRef;
914914 }
915915
916 ModRefInfo GlobalsAAResult::getModRefInfo(ImmutableCallSite CS,
916 ModRefInfo GlobalsAAResult::getModRefInfo(const CallBase *Call,
917917 const MemoryLocation &Loc) {
918918 ModRefInfo Known = ModRefInfo::ModRef;
919919
922922 if (const GlobalValue *GV =
923923 dyn_cast(GetUnderlyingObject(Loc.Ptr, DL)))
924924 if (GV->hasLocalLinkage())
925 if (const Function *F = CS.getCalledFunction())
925 if (const Function *F = Call->getCalledFunction())
926926 if (NonAddressTakenGlobals.count(GV))
927927 if (const FunctionInfo *FI = getFunctionInfo(F))
928928 Known = unionModRef(FI->getModRefInfoForGlobal(*GV),
929 getModRefInfoForArgument(CS, GV));
929 getModRefInfoForArgument(Call, GV));
930930
931931 if (!isModOrRefSet(Known))
932932 return ModRefInfo::NoModRef; // No need to query other mod/ref analyses
933 return intersectModRef(Known, AAResultBase::getModRefInfo(CS, Loc));
933 return intersectModRef(Known, AAResultBase::getModRefInfo(Call, Loc));
934934 }
935935
936936 GlobalsAAResult::GlobalsAAResult(const DataLayout &DL,
106106 return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, Size,
107107 DL, CtxI, DT, Visited);
108108
109 if (auto CS = ImmutableCallSite(V))
110 if (auto *RP = getArgumentAliasingToReturnedPointer(CS))
109 if (const auto *Call = dyn_cast(V))
110 if (auto *RP = getArgumentAliasingToReturnedPointer(Call))
111111 return isDereferenceableAndAlignedPointer(RP, Align, Size, DL, CtxI, DT,
112112 Visited);
113113
1212 #include "llvm/ADT/SetVector.h"
1313 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
1414 #include "llvm/Analysis/Passes.h"
15 #include "llvm/IR/CallSite.h"
1615 #include "llvm/IR/InstIterator.h"
1716 #include "llvm/IR/LLVMContext.h"
1817 #include "llvm/Support/ErrorHandling.h"
105104 if (!Res.isNonLocal()) {
106105 Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
107106 static_cast(nullptr)));
108 } else if (auto CS = CallSite(Inst)) {
107 } else if (auto *Call = dyn_cast(Inst)) {
109108 const MemoryDependenceResults::NonLocalDepInfo &NLDI =
110 MDA.getNonLocalCallDependency(CS);
109 MDA.getNonLocalCallDependency(Call);
111110
112111 DepSet &InstDeps = Deps[Inst];
113112 for (const NonLocalDepEntry &I : NLDI) {
3030 #include "llvm/Analysis/ValueTracking.h"
3131 #include "llvm/IR/Attributes.h"
3232 #include "llvm/IR/BasicBlock.h"
33 #include "llvm/IR/CallSite.h"
3433 #include "llvm/IR/Constants.h"
3534 #include "llvm/IR/DataLayout.h"
3635 #include "llvm/IR/DerivedTypes.h"
181180 }
182181
183182 /// Private helper for finding the local dependencies of a call site.
184 MemDepResult MemoryDependenceResults::getCallSiteDependencyFrom(
185 CallSite CS, bool isReadOnlyCall, BasicBlock::iterator ScanIt,
183 MemDepResult MemoryDependenceResults::getCallDependencyFrom(
184 CallBase *Call, bool isReadOnlyCall, BasicBlock::iterator ScanIt,
186185 BasicBlock *BB) {
187186 unsigned Limit = BlockScanLimit;
188187
204203 ModRefInfo MR = GetLocation(Inst, Loc, TLI);
205204 if (Loc.Ptr) {
206205 // A simple instruction.
207 if (isModOrRefSet(AA.getModRefInfo(CS, Loc)))
206 if (isModOrRefSet(AA.getModRefInfo(Call, Loc)))
208207 return MemDepResult::getClobber(Inst);
209208 continue;
210209 }
211210
212 if (auto InstCS = CallSite(Inst)) {
211 if (auto *CallB = dyn_cast(Inst)) {
213212 // If these two calls do not interfere, look past it.
214 if (isNoModRef(AA.getModRefInfo(CS, InstCS))) {
215 // If the two calls are the same, return InstCS as a Def, so that
216 // CS can be found redundant and eliminated.
213 if (isNoModRef(AA.getModRefInfo(Call, CallB))) {
214 // If the two calls are the same, return Inst as a Def, so that
215 // Call can be found redundant and eliminated.
217216 if (isReadOnlyCall && !isModSet(MR) &&
218 CS.getInstruction()->isIdenticalToWhenDefined(Inst))
217 Call->isIdenticalToWhenDefined(CallB))
219218 return MemDepResult::getDef(Inst);
220219
221 // Otherwise if the two calls don't interact (e.g. InstCS is readnone)
220 // Otherwise if the two calls don't interact (e.g. CallB is readnone)
222221 // keep scanning.
223222 continue;
224223 } else
749748
750749 LocalCache = getPointerDependencyFrom(
751750 MemLoc, isLoad, ScanPos->getIterator(), QueryParent, QueryInst);
752 } else if (isa(QueryInst) || isa(QueryInst)) {
753 CallSite QueryCS(QueryInst);
754 bool isReadOnly = AA.onlyReadsMemory(QueryCS);
755 LocalCache = getCallSiteDependencyFrom(
756 QueryCS, isReadOnly, ScanPos->getIterator(), QueryParent);
751 } else if (auto *QueryCall = dyn_cast(QueryInst)) {
752 bool isReadOnly = AA.onlyReadsMemory(QueryCall);
753 LocalCache = getCallDependencyFrom(QueryCall, isReadOnly,
754 ScanPos->getIterator(), QueryParent);
757755 } else
758756 // Non-memory instruction.
759757 LocalCache = MemDepResult::getUnknown();
779777 #endif
780778
781779 const MemoryDependenceResults::NonLocalDepInfo &
782 MemoryDependenceResults::getNonLocalCallDependency(CallSite QueryCS) {
783 assert(getDependency(QueryCS.getInstruction()).isNonLocal() &&
780 MemoryDependenceResults::getNonLocalCallDependency(CallBase *QueryCall) {
781 assert(getDependency(QueryCall).isNonLocal() &&
784782 "getNonLocalCallDependency should only be used on calls with "
785783 "non-local deps!");
786 PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()];
784 PerInstNLInfo &CacheP = NonLocalDeps[QueryCall];
787785 NonLocalDepInfo &Cache = CacheP.first;
788786
789787 // This is the set of blocks that need to be recomputed. In the cached case,
813811 // << Cache.size() << " cached: " << *QueryInst;
814812 } else {
815813 // Seed DirtyBlocks with each of the preds of QueryInst's block.
816 BasicBlock *QueryBB = QueryCS.getInstruction()->getParent();
814 BasicBlock *QueryBB = QueryCall->getParent();
817815 for (BasicBlock *Pred : PredCache.get(QueryBB))
818816 DirtyBlocks.push_back(Pred);
819817 ++NumUncacheNonLocal;
820818 }
821819
822820 // isReadonlyCall - If this is a read-only call, we can be more aggressive.
823 bool isReadonlyCall = AA.onlyReadsMemory(QueryCS);
821 bool isReadonlyCall = AA.onlyReadsMemory(QueryCall);
824822
825823 SmallPtrSet Visited;
826824
864862 if (Instruction *Inst = ExistingResult->getResult().getInst()) {
865863 ScanPos = Inst->getIterator();
866864 // We're removing QueryInst's use of Inst.
867 RemoveFromReverseMap(ReverseNonLocalDeps, Inst,
868 QueryCS.getInstruction());
865 RemoveFromReverseMap(ReverseNonLocalDeps, Inst,
866 QueryCall);
869867 }
870868 }
871869
873871 MemDepResult Dep;
874872
875873 if (ScanPos != DirtyBB->begin()) {
876 Dep =
877 getCallSiteDependencyFrom(QueryCS, isReadonlyCall, ScanPos, DirtyBB);
874 Dep = getCallDependencyFrom(QueryCall, isReadonlyCall, ScanPos, DirtyBB);
878875 } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {
879876 // No dependence found. If this is the entry block of the function, it is
880877 // a clobber, otherwise it is unknown.
896893 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
897894 // update this when we remove instructions.
898895 if (Instruction *Inst = Dep.getInst())
899 ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction());
896 ReverseNonLocalDeps[Inst].insert(QueryCall);
900897 } else {
901898
902899 // If the block *is* completely transparent to the load, we need to check
124124 return MemoryLocation(MI->getRawDest(), Size, AATags);
125125 }
126126
127 MemoryLocation MemoryLocation::getForArgument(ImmutableCallSite CS,
127 MemoryLocation MemoryLocation::getForArgument(const CallBase *Call,
128128 unsigned ArgIdx,
129129 const TargetLibraryInfo *TLI) {
130130 AAMDNodes AATags;
131 CS->getAAMetadata(AATags);
132 const Value *Arg = CS.getArgument(ArgIdx);
131 Call->getAAMetadata(AATags);
132 const Value *Arg = Call->getArgOperand(ArgIdx);
133133
134134 // We may be able to produce an exact size for known intrinsics.
135 if (const IntrinsicInst *II = dyn_cast(CS.getInstruction())) {
135 if (const IntrinsicInst *II = dyn_cast(Call)) {
136136 const DataLayout &DL = II->getModule()->getDataLayout();
137137
138138 switch (II->getIntrinsicID()) {
192192 // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
193193 // whenever possible.
194194 LibFunc F;
195 if (TLI && CS.getCalledFunction() &&
196 TLI->getLibFunc(*CS.getCalledFunction(), F) &&
195 if (TLI && Call->getCalledFunction() &&
196 TLI->getLibFunc(*Call->getCalledFunction(), F) &&
197197 F == LibFunc_memset_pattern16 && TLI->has(F)) {
198198 assert((ArgIdx == 0 || ArgIdx == 1) &&
199199 "Invalid argument index for memset_pattern16");
200200 if (ArgIdx == 1)
201201 return MemoryLocation(Arg, LocationSize::precise(16), AATags);
202 if (const ConstantInt *LenCI = dyn_cast(CS.getArgument(2)))
202 if (const ConstantInt *LenCI =
203 dyn_cast(Call->getArgOperand(2)))
203204 return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
204205 AATags);
205206 }
206207 // FIXME: Handle memset_pattern4 and memset_pattern8 also.
207208
208 return MemoryLocation(CS.getArgument(ArgIdx), LocationSize::unknown(),
209 AATags);
210 }
209 return MemoryLocation(Call->getArgOperand(ArgIdx), LocationSize::unknown(),
210 AATags);
211 }
2929 #include "llvm/Config/llvm-config.h"
3030 #include "llvm/IR/AssemblyAnnotationWriter.h"
3131 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/CallSite.h"
3332 #include "llvm/IR/Dominators.h"
3433 #include "llvm/IR/Function.h"
3534 #include "llvm/IR/Instruction.h"
130129 : MemoryLocOrCall(MUD->getMemoryInst()) {}
131130
132131 MemoryLocOrCall(Instruction *Inst) {
133 if (ImmutableCallSite(Inst)) {
132 if (auto *C = dyn_cast(Inst)) {
134133 IsCall = true;
135 CS = ImmutableCallSite(Inst);
134 Call = C;
136135 } else {
137136 IsCall = false;
138137 // There is no such thing as a memorylocation for a fence inst, and it is
144143
145144 explicit MemoryLocOrCall(const MemoryLocation &Loc) : Loc(Loc) {}
146145
147 ImmutableCallSite getCS() const {
146 const CallBase *getCall() const {
148147 assert(IsCall);
149 return CS;
148 return Call;
150149 }
151150
152151 MemoryLocation getLoc() const {
161160 if (!IsCall)
162161 return Loc == Other.Loc;
163162
164 if (CS.getCalledValue() != Other.CS.getCalledValue())
163 if (Call->getCalledValue() != Other.Call->getCalledValue())
165164 return false;
166165
167 return CS.arg_size() == Other.CS.arg_size() &&
168 std::equal(CS.arg_begin(), CS.arg_end(), Other.CS.arg_begin());
166 return Call->arg_size() == Other.Call->arg_size() &&
167 std::equal(Call->arg_begin(), Call->arg_end(),
168 Other.Call->arg_begin());
169169 }
170170
171171 private:
172172 union {
173 ImmutableCallSite CS;
173 const CallBase *Call;
174174 MemoryLocation Loc;
175175 };
176176 };
196196
197197 hash_code hash =
198198 hash_combine(MLOC.IsCall, DenseMapInfo::getHashValue(
199 MLOC.getCS().getCalledValue()));
200
201 for (const Value *Arg : MLOC.getCS().args())
199 MLOC.getCall()->getCalledValue()));
200
201 for (const Value *Arg : MLOC.getCall()->args())
202202 hash = hash_combine(hash, DenseMapInfo::getHashValue(Arg));
203203 return hash;
204204 }
257257 AliasAnalysis &AA) {
258258 Instruction *DefInst = MD->getMemoryInst();
259259 assert(DefInst && "Defining instruction not actually an instruction");
260 ImmutableCallSite UseCS(UseInst);
260 const auto *UseCall = dyn_cast(UseInst);
261261 Optional AR;
262262
263263 if (const IntrinsicInst *II = dyn_cast(DefInst)) {
270270 // context.
271271 switch (II->getIntrinsicID()) {
272272 case Intrinsic::lifetime_start:
273 if (UseCS)
273 if (UseCall)
274274 return {false, NoAlias};
275275 AR = AA.alias(MemoryLocation(II->getArgOperand(1)), UseLoc);
276276 return {AR != NoAlias, AR};
284284 }
285285 }
286286
287 if (UseCS) {
288 ModRefInfo I = AA.getModRefInfo(DefInst, UseCS);
287 if (UseCall) {
288 ModRefInfo I = AA.getModRefInfo(DefInst, UseCall);
289289 AR = isMustSet(I) ? MustAlias : MayAlias;
290290 return {isModOrRefSet(I), AR};
291291 }
335335 UpwardsMemoryQuery() = default;
336336
337337 UpwardsMemoryQuery(const Instruction *Inst, const MemoryAccess *Access)
338 : IsCall(ImmutableCallSite(Inst)), Inst(Inst), OriginalAccess(Access) {
338 : IsCall(isa(Inst)), Inst(Inst), OriginalAccess(Access) {
339339 if (!IsCall)
340340 StartingLoc = MemoryLocation::get(Inst);
341341 }
21612161
21622162 // Conservatively, fences are always clobbers, so don't perform the walk if we
21632163 // hit a fence.
2164 if (!ImmutableCallSite(I) && I->isFenceLike())
2164 if (!isa(I) && I->isFenceLike())
21652165 return StartingUseOrDef;
21662166
21672167 UpwardsMemoryQuery Q;
22012201 // We can't sanely do anything with a fence, since they conservatively clobber
22022202 // all memory, and have no locations to get pointers from to try to
22032203 // disambiguate.
2204 if (!ImmutableCallSite(I) && I->isFenceLike())
2204 if (!isa(I) && I->isFenceLike())
22052205 return StartingAccess;
22062206
22072207 UpwardsMemoryQuery Q(I, StartingAccess);
105105 return AAResultBase::getModRefBehavior(F);
106106 }
107107
108 ModRefInfo ObjCARCAAResult::getModRefInfo(ImmutableCallSite CS,
108 ModRefInfo ObjCARCAAResult::getModRefInfo(const CallBase *Call,
109109 const MemoryLocation &Loc) {
110110 if (!EnableARCOpts)
111 return AAResultBase::getModRefInfo(CS, Loc);
111 return AAResultBase::getModRefInfo(Call, Loc);
112112
113 switch (GetBasicARCInstKind(CS.getInstruction())) {
113 switch (GetBasicARCInstKind(Call)) {
114114 case ARCInstKind::Retain:
115115 case ARCInstKind::RetainRV:
116116 case ARCInstKind::Autorelease:
127127 break;
128128 }
129129
130 return AAResultBase::getModRefInfo(CS, Loc);
130 return AAResultBase::getModRefInfo(Call, Loc);
131131 }
132132
133133 ObjCARCAAResult ObjCARCAA::run(Function &F, FunctionAnalysisManager &AM) {
9494 return AAResultBase::alias(LocA, LocB);
9595 }
9696
97 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS,
97 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call,
9898 const MemoryLocation &Loc) {
9999 if (!EnableScopedNoAlias)
100 return AAResultBase::getModRefInfo(CS, Loc);
101
102 if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata(
103 LLVMContext::MD_noalias)))
104 return ModRefInfo::NoModRef;
105
106 if (!mayAliasInScopes(
107 CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
108 Loc.AATags.NoAlias))
109 return ModRefInfo::NoModRef;
110
111 return AAResultBase::getModRefInfo(CS, Loc);
112 }
113
114 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS1,
115 ImmutableCallSite CS2) {
100 return AAResultBase::getModRefInfo(Call, Loc);
101
102 if (!mayAliasInScopes(Loc.AATags.Scope,
103 Call->getMetadata(LLVMContext::MD_noalias)))
104 return ModRefInfo::NoModRef;
105
106 if (!mayAliasInScopes(Call->getMetadata(LLVMContext::MD_alias_scope),
107 Loc.AATags.NoAlias))
108 return ModRefInfo::NoModRef;
109
110 return AAResultBase::getModRefInfo(Call, Loc);
111 }
112
113 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call1,
114 const CallBase *Call2) {
116115 if (!EnableScopedNoAlias)
117 return AAResultBase::getModRefInfo(CS1, CS2);
118
119 if (!mayAliasInScopes(
120 CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
121 CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
122 return ModRefInfo::NoModRef;
123
124 if (!mayAliasInScopes(
125 CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
126 CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
127 return ModRefInfo::NoModRef;
128
129 return AAResultBase::getModRefInfo(CS1, CS2);
116 return AAResultBase::getModRefInfo(Call1, Call2);
117
118 if (!mayAliasInScopes(Call1->getMetadata(LLVMContext::MD_alias_scope),
119 Call2->getMetadata(LLVMContext::MD_noalias)))
120 return ModRefInfo::NoModRef;
121
122 if (!mayAliasInScopes(Call2->getMetadata(LLVMContext::MD_alias_scope),
123 Call1->getMetadata(LLVMContext::MD_noalias)))
124 return ModRefInfo::NoModRef;
125
126 return AAResultBase::getModRefInfo(Call1, Call2);
130127 }
131128
132129 static void collectMDInDomain(const MDNode *List, const MDNode *Domain,
398398 }
399399
400400 FunctionModRefBehavior
401 TypeBasedAAResult::getModRefBehavior(ImmutableCallSite CS) {
401 TypeBasedAAResult::getModRefBehavior(const CallBase *Call) {
402402 if (!EnableTBAA)
403 return AAResultBase::getModRefBehavior(CS);
403 return AAResultBase::getModRefBehavior(Call);
404404
405405 FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
406406
407407 // If this is an "immutable" type, we can assume the call doesn't write
408408 // to memory.
409 if (const MDNode *M = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
409 if (const MDNode *M = Call->getMetadata(LLVMContext::MD_tbaa))
410410 if ((!isStructPathTBAA(M) && TBAANode(M).isTypeImmutable()) ||
411411 (isStructPathTBAA(M) && TBAAStructTagNode(M).isTypeImmutable()))
412412 Min = FMRB_OnlyReadsMemory;
413413
414 return FunctionModRefBehavior(AAResultBase::getModRefBehavior(CS) & Min);
414 return FunctionModRefBehavior(AAResultBase::getModRefBehavior(Call) & Min);
415415 }
416416
417417 FunctionModRefBehavior TypeBasedAAResult::getModRefBehavior(const Function *F) {
419419 return AAResultBase::getModRefBehavior(F);
420420 }
421421
422 ModRefInfo TypeBasedAAResult::getModRefInfo(ImmutableCallSite CS,
422 ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
423423 const MemoryLocation &Loc) {
424424 if (!EnableTBAA)
425 return AAResultBase::getModRefInfo(CS, Loc);
425 return AAResultBase::getModRefInfo(Call, Loc);
426426
427427 if (const MDNode *L = Loc.AATags.TBAA)
428 if (const MDNode *M =
429 CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
428 if (const MDNode *M = Call->getMetadata(LLVMContext::MD_tbaa))
430429 if (!Aliases(L, M))
431430 return ModRefInfo::NoModRef;
432431
433 return AAResultBase::getModRefInfo(CS, Loc);
434 }
435
436 ModRefInfo TypeBasedAAResult::getModRefInfo(ImmutableCallSite CS1,
437 ImmutableCallSite CS2) {
432 return AAResultBase::getModRefInfo(Call, Loc);
433 }
434
435 ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call1,
436 const CallBase *Call2) {
438437 if (!EnableTBAA)
439 return AAResultBase::getModRefInfo(CS1, CS2);
440
441 if (const MDNode *M1 =
442 CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
443 if (const MDNode *M2 =
444 CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
438 return AAResultBase::getModRefInfo(Call1, Call2);
439
440 if (const MDNode *M1 = Call1->getMetadata(LLVMContext::MD_tbaa))
441 if (const MDNode *M2 = Call2->getMetadata(LLVMContext::MD_tbaa))
445442 if (!Aliases(M1, M2))
446443 return ModRefInfo::NoModRef;
447444
448 return AAResultBase::getModRefInfo(CS1, CS2);
445 return AAResultBase::getModRefInfo(Call1, Call2);
449446 }
450447
451448 bool MDNode::isTBAAVtableAccess() const {
20222022 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull))
20232023 return true;
20242024
2025 if (auto CS = ImmutableCallSite(V)) {
2026 if (CS.isReturnNonNull())
2025 if (const auto *Call = dyn_cast(V)) {
2026 if (Call->isReturnNonNull())
20272027 return true;
2028 if (const auto *RP = getArgumentAliasingToReturnedPointer(CS))
2028 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call))
20292029 return isKnownNonZero(RP, Depth, Q);
20302030 }
20312031 }
36233623 return Len == ~0ULL ? 1 : Len;
36243624 }
36253625
3626 const Value *llvm::getArgumentAliasingToReturnedPointer(ImmutableCallSite CS) {
3627 assert(CS &&
3628 "getArgumentAliasingToReturnedPointer only works on nonnull CallSite");
3629 if (const Value *RV = CS.getReturnedArgOperand())
3626 const Value *llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call) {
3627 assert(Call &&
3628 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
3629 if (const Value *RV = Call->getReturnedArgOperand())
36303630 return RV;
36313631 // This can be used only as a aliasing property.
3632 if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(CS))
3633 return CS.getArgOperand(0);
3632 if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(Call))
3633 return Call->getArgOperand(0);
36343634 return nullptr;
36353635 }
36363636
36373637 bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
3638 ImmutableCallSite CS) {
3639 return CS.getIntrinsicID() == Intrinsic::launder_invariant_group ||
3640 CS.getIntrinsicID() == Intrinsic::strip_invariant_group;
3638 const CallBase *Call) {
3639 return Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||
3640 Call->getIntrinsicID() == Intrinsic::strip_invariant_group;
36413641 }
36423642
36433643 /// \p PN defines a loop-variant pointer to an object. Check if the
36853685 // An alloca can't be further simplified.
36863686 return V;
36873687 } else {
3688 if (auto CS = CallSite(V)) {
3688 if (auto *Call = dyn_cast(V)) {
36893689 // CaptureTracking can know about special capturing properties of some
36903690 // intrinsics like launder.invariant.group, that can't be expressed with
36913691 // the attributes, but have properties like returning aliasing pointer.
36953695 // because it should be in sync with CaptureTracking. Not using it may
36963696 // cause weird miscompilations where 2 aliasing pointers are assumed to
36973697 // noalias.
3698 if (auto *RP = getArgumentAliasingToReturnedPointer(CS)) {
3698 if (auto *RP = getArgumentAliasingToReturnedPointer(Call)) {
36993699 V = RP;
37003700 continue;
37013701 }
129129
130130 // Some instructions can be ignored even if they read or write memory.
131131 // Detect these now, skipping to the next instruction if one is found.
132 CallSite CS(cast(I));
133 if (CS) {
132 if (auto *Call = dyn_cast(I)) {
134133 // Ignore calls to functions in the same SCC, as long as the call sites
135134 // don't have operand bundles. Calls with operand bundles are allowed to
136135 // have memory effects not described by the memory effects of the call
137136 // target.
138 if (!CS.hasOperandBundles() && CS.getCalledFunction() &&
139 SCCNodes.count(CS.getCalledFunction()))
137 if (!Call->hasOperandBundles() && Call->getCalledFunction() &&
138 SCCNodes.count(Call->getCalledFunction()))
140139 continue;
141 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
140 FunctionModRefBehavior MRB = AAR.getModRefBehavior(Call);
142141 ModRefInfo MRI = createModRefInfo(MRB);
143142
144143 // If the call doesn't access memory, we're done.
157156
158157 // Check whether all pointer arguments point to local memory, and
159158 // ignore calls that only access local memory.
160 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
159 for (CallSite::arg_iterator CI = Call->arg_begin(), CE = Call->arg_end();
161160 CI != CE; ++CI) {
162161 Value *Arg = *CI;
163162 if (!Arg->getType()->isPtrOrPtrVectorTy())
4444 default: break;
4545 }
4646
47 ImmutableCallSite CS(Inst);
48 assert(CS && "Only calls can alter reference counts!");
47 const auto *Call = cast(Inst);
4948
5049 // See if AliasAnalysis can help us with the call.
51 FunctionModRefBehavior MRB = PA.getAA()->getModRefBehavior(CS);
50 FunctionModRefBehavior MRB = PA.getAA()->getModRefBehavior(Call);
5251 if (AliasAnalysis::onlyReadsMemory(MRB))
5352 return false;
5453 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
5554 const DataLayout &DL = Inst->getModule()->getDataLayout();
56 for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
57 I != E; ++I) {
58 const Value *Op = *I;
55 for (const Value *Op : Call->args()) {
5956 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
6057 PA.related(Ptr, Op, DL))
6158 return true;
833833 continue;
834834 }
835835
836 if (auto CS = CallSite(&*BBI)) {
836 if (auto *Call = dyn_cast(&*BBI)) {
837837 // Remove allocation function calls from the list of dead stack objects;
838838 // there can't be any references before the definition.
839839 if (isAllocLikeFn(&*BBI, TLI))
841841
842842 // If this call does not access memory, it can't be loading any of our
843843 // pointers.
844 if (AA->doesNotAccessMemory(CS))
844 if (AA->doesNotAccessMemory(Call))
845845 continue;
846846
847847 // If the call might load from any of our allocas, then any store above
848848 // the call is live.
849849 DeadStackObjects.remove_if([&](Value *I) {
850850 // See if the call site touches the value.
851 return isRefSet(AA->getModRefInfo(CS, I, getPointerSize(I, DL, *TLI,
852 BB.getParent())));
851 return isRefSet(AA->getModRefInfo(
852 Call, I, getPointerSize(I, DL, *TLI, BB.getParent())));
853853 });
854854
855855 // If all of the allocas were clobbered by the call then we're not going
436436
437437 // Non-local case.
438438 const MemoryDependenceResults::NonLocalDepInfo &deps =
439 MD->getNonLocalCallDependency(CallSite(C));
439 MD->getNonLocalCallDependency(C);
440440 // FIXME: Move the checking logic to MemDep!
441441 CallInst* cdep = nullptr;
442442
359359 bool LoopVersioningLICM::instructionSafeForVersioning(Instruction *I) {
360360 assert(I != nullptr && "Null instruction found!");
361361 // Check function call safety
362 if (isa(I) && !AA->doesNotAccessMemory(CallSite(I))) {
363 LLVM_DEBUG(dbgs() << " Unsafe call site found.\n");
364 return false;
365 }
362 if (auto *Call = dyn_cast(I))
363 if (!AA->doesNotAccessMemory(Call)) {
364 LLVM_DEBUG(dbgs() << " Unsafe call site found.\n");
365 return false;
366 }
366367 // Avoid loops with possiblity of throw
367368 if (I->mayThrow()) {
368369 LLVM_DEBUG(dbgs() << " May throw instruction found in loop body\n");
545545 // Memory locations of lifted instructions.
546546 SmallVector MemLocs{StoreLoc};
547547
548 // Lifted callsites.
549 SmallVector CallSites;
548 // Lifted calls.
549 SmallVector Calls;
550550
551551 const MemoryLocation LoadLoc = MemoryLocation::get(LI);
552552
564564 });
565565
566566 if (!NeedLift)
567 NeedLift =
568 llvm::any_of(CallSites, [C, &AA](const ImmutableCallSite &CS) {
569 return isModOrRefSet(AA.getModRefInfo(C, CS));
570 });
567 NeedLift = llvm::any_of(Calls, [C, &AA](const CallBase *Call) {
568 return isModOrRefSet(AA.getModRefInfo(C, Call));
569 });
571570 }
572571
573572 if (!NeedLift)
578577 // none of them may modify its source.
579578 if (isModSet(AA.getModRefInfo(C, LoadLoc)))
580579 return false;
581 else if (auto CS = ImmutableCallSite(C)) {
580 else if (const auto *Call = dyn_cast(C)) {
582581 // If we can't lift this before P, it's game over.
583 if (isModOrRefSet(AA.getModRefInfo(P, CS)))
582 if (isModOrRefSet(AA.getModRefInfo(P, Call)))
584583 return false;
585584
586 CallSites.push_back(CS);
585 Calls.push_back(Call);
587586 } else if (isa(C) || isa(C) || isa(C)) {
588587 // If we can't lift this before P, it's game over.
589588 auto ML = MemoryLocation::get(C);
7575 Inst->mayThrow())
7676 return false;
7777
78 if (auto CS = CallSite(Inst)) {
78 if (auto *Call = dyn_cast(Inst)) {
7979 // Convergent operations cannot be made control-dependent on additional
8080 // values.
81 if (CS.hasFnAttr(Attribute::Convergent))
81 if (Call->hasFnAttr(Attribute::Convergent))
8282 return false;
8383
8484 for (Instruction *S : Stores)
85 if (isModSet(AA.getModRefInfo(S, CS)))
85 if (isModSet(AA.getModRefInfo(S, Call)))
8686 return false;
8787 }
8888
998998 PtrArgs.push_back(CXI->getPointerOperand());
999999 else if (const AtomicRMWInst *RMWI = dyn_cast(I))
10001000 PtrArgs.push_back(RMWI->getPointerOperand());
1001 else if (ImmutableCallSite ICS = ImmutableCallSite(I)) {
1001 else if (const auto *Call = dyn_cast(I)) {
10021002 // If we know that the call does not access memory, then we'll still
10031003 // know that about the inlined clone of this call site, and we don't
10041004 // need to add metadata.
1005 if (ICS.doesNotAccessMemory())
1005 if (Call->doesNotAccessMemory())
10061006 continue;
10071007
10081008 IsFuncCall = true;
10091009 if (CalleeAAR) {
1010 FunctionModRefBehavior MRB = CalleeAAR->getModRefBehavior(ICS);
1010 FunctionModRefBehavior MRB = CalleeAAR->getModRefBehavior(Call);
10111011 if (MRB == FMRB_OnlyAccessesArgumentPointees ||
10121012 MRB == FMRB_OnlyReadsArgumentPointees)
10131013 IsArgMemOnlyCall = true;
10141014 }
10151015
1016 for (Value *Arg : ICS.args()) {
1016 for (Value *Arg : Call->args()) {
10171017 // We need to check the underlying objects of all arguments, not just
10181018 // the pointer arguments, because we might be passing pointers as
10191019 // integers, etc.