MemoryLocation.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //===- MemoryLocation.cpp - Memory location descriptions -------------------==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Analysis/MemoryLocation.h"
  9. #include "llvm/Analysis/TargetLibraryInfo.h"
  10. #include "llvm/IR/BasicBlock.h"
  11. #include "llvm/IR/DataLayout.h"
  12. #include "llvm/IR/Instructions.h"
  13. #include "llvm/IR/IntrinsicInst.h"
  14. #include "llvm/IR/IntrinsicsARM.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/IR/Type.h"
  18. using namespace llvm;
  19. void LocationSize::print(raw_ostream &OS) const {
  20. OS << "LocationSize::";
  21. if (*this == beforeOrAfterPointer())
  22. OS << "beforeOrAfterPointer";
  23. else if (*this == afterPointer())
  24. OS << "afterPointer";
  25. else if (*this == mapEmpty())
  26. OS << "mapEmpty";
  27. else if (*this == mapTombstone())
  28. OS << "mapTombstone";
  29. else if (isPrecise())
  30. OS << "precise(" << getValue() << ')';
  31. else
  32. OS << "upperBound(" << getValue() << ')';
  33. }
  34. MemoryLocation MemoryLocation::get(const LoadInst *LI) {
  35. const auto &DL = LI->getModule()->getDataLayout();
  36. return MemoryLocation(
  37. LI->getPointerOperand(),
  38. LocationSize::precise(DL.getTypeStoreSize(LI->getType())),
  39. LI->getAAMetadata());
  40. }
  41. MemoryLocation MemoryLocation::get(const StoreInst *SI) {
  42. const auto &DL = SI->getModule()->getDataLayout();
  43. return MemoryLocation(SI->getPointerOperand(),
  44. LocationSize::precise(DL.getTypeStoreSize(
  45. SI->getValueOperand()->getType())),
  46. SI->getAAMetadata());
  47. }
  48. MemoryLocation MemoryLocation::get(const VAArgInst *VI) {
  49. return MemoryLocation(VI->getPointerOperand(),
  50. LocationSize::afterPointer(), VI->getAAMetadata());
  51. }
  52. MemoryLocation MemoryLocation::get(const AtomicCmpXchgInst *CXI) {
  53. const auto &DL = CXI->getModule()->getDataLayout();
  54. return MemoryLocation(CXI->getPointerOperand(),
  55. LocationSize::precise(DL.getTypeStoreSize(
  56. CXI->getCompareOperand()->getType())),
  57. CXI->getAAMetadata());
  58. }
  59. MemoryLocation MemoryLocation::get(const AtomicRMWInst *RMWI) {
  60. const auto &DL = RMWI->getModule()->getDataLayout();
  61. return MemoryLocation(RMWI->getPointerOperand(),
  62. LocationSize::precise(DL.getTypeStoreSize(
  63. RMWI->getValOperand()->getType())),
  64. RMWI->getAAMetadata());
  65. }
  66. Optional<MemoryLocation> MemoryLocation::getOrNone(const Instruction *Inst) {
  67. switch (Inst->getOpcode()) {
  68. case Instruction::Load:
  69. return get(cast<LoadInst>(Inst));
  70. case Instruction::Store:
  71. return get(cast<StoreInst>(Inst));
  72. case Instruction::VAArg:
  73. return get(cast<VAArgInst>(Inst));
  74. case Instruction::AtomicCmpXchg:
  75. return get(cast<AtomicCmpXchgInst>(Inst));
  76. case Instruction::AtomicRMW:
  77. return get(cast<AtomicRMWInst>(Inst));
  78. default:
  79. return None;
  80. }
  81. }
  82. MemoryLocation MemoryLocation::getForSource(const MemTransferInst *MTI) {
  83. return getForSource(cast<AnyMemTransferInst>(MTI));
  84. }
  85. MemoryLocation MemoryLocation::getForSource(const AtomicMemTransferInst *MTI) {
  86. return getForSource(cast<AnyMemTransferInst>(MTI));
  87. }
  88. MemoryLocation MemoryLocation::getForSource(const AnyMemTransferInst *MTI) {
  89. assert(MTI->getRawSource() == MTI->getArgOperand(1));
  90. return getForArgument(MTI, 1, nullptr);
  91. }
  92. MemoryLocation MemoryLocation::getForDest(const MemIntrinsic *MI) {
  93. return getForDest(cast<AnyMemIntrinsic>(MI));
  94. }
  95. MemoryLocation MemoryLocation::getForDest(const AtomicMemIntrinsic *MI) {
  96. return getForDest(cast<AnyMemIntrinsic>(MI));
  97. }
  98. MemoryLocation MemoryLocation::getForDest(const AnyMemIntrinsic *MI) {
  99. assert(MI->getRawDest() == MI->getArgOperand(0));
  100. return getForArgument(MI, 0, nullptr);
  101. }
  102. Optional<MemoryLocation>
  103. MemoryLocation::getForDest(const CallBase *CB, const TargetLibraryInfo &TLI) {
  104. if (!CB->onlyAccessesArgMemory())
  105. return None;
  106. if (CB->hasOperandBundles())
  107. // TODO: remove implementation restriction
  108. return None;
  109. Value *UsedV = nullptr;
  110. Optional<unsigned> UsedIdx;
  111. for (unsigned i = 0; i < CB->arg_size(); i++) {
  112. if (!CB->getArgOperand(i)->getType()->isPointerTy())
  113. continue;
  114. if (CB->onlyReadsMemory(i))
  115. continue;
  116. if (!UsedV) {
  117. // First potentially writing parameter
  118. UsedV = CB->getArgOperand(i);
  119. UsedIdx = i;
  120. continue;
  121. }
  122. UsedIdx = None;
  123. if (UsedV != CB->getArgOperand(i))
  124. // Can't describe writing to two distinct locations.
  125. // TODO: This results in an inprecision when two values derived from the
  126. // same object are passed as arguments to the same function.
  127. return None;
  128. }
  129. if (!UsedV)
  130. // We don't currently have a way to represent a "does not write" result
  131. // and thus have to be conservative and return unknown.
  132. return None;
  133. if (UsedIdx)
  134. return getForArgument(CB, *UsedIdx, &TLI);
  135. return MemoryLocation::getBeforeOrAfter(UsedV, CB->getAAMetadata());
  136. }
  137. MemoryLocation MemoryLocation::getForArgument(const CallBase *Call,
  138. unsigned ArgIdx,
  139. const TargetLibraryInfo *TLI) {
  140. AAMDNodes AATags = Call->getAAMetadata();
  141. const Value *Arg = Call->getArgOperand(ArgIdx);
  142. // We may be able to produce an exact size for known intrinsics.
  143. if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call)) {
  144. const DataLayout &DL = II->getModule()->getDataLayout();
  145. switch (II->getIntrinsicID()) {
  146. default:
  147. break;
  148. case Intrinsic::memset:
  149. case Intrinsic::memcpy:
  150. case Intrinsic::memcpy_inline:
  151. case Intrinsic::memmove:
  152. case Intrinsic::memcpy_element_unordered_atomic:
  153. case Intrinsic::memmove_element_unordered_atomic:
  154. case Intrinsic::memset_element_unordered_atomic:
  155. assert((ArgIdx == 0 || ArgIdx == 1) &&
  156. "Invalid argument index for memory intrinsic");
  157. if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
  158. return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
  159. AATags);
  160. return MemoryLocation::getAfter(Arg, AATags);
  161. case Intrinsic::lifetime_start:
  162. case Intrinsic::lifetime_end:
  163. case Intrinsic::invariant_start:
  164. assert(ArgIdx == 1 && "Invalid argument index");
  165. return MemoryLocation(
  166. Arg,
  167. LocationSize::precise(
  168. cast<ConstantInt>(II->getArgOperand(0))->getZExtValue()),
  169. AATags);
  170. case Intrinsic::masked_load:
  171. assert(ArgIdx == 0 && "Invalid argument index");
  172. return MemoryLocation(
  173. Arg,
  174. LocationSize::upperBound(DL.getTypeStoreSize(II->getType())),
  175. AATags);
  176. case Intrinsic::masked_store:
  177. assert(ArgIdx == 1 && "Invalid argument index");
  178. return MemoryLocation(
  179. Arg,
  180. LocationSize::upperBound(
  181. DL.getTypeStoreSize(II->getArgOperand(0)->getType())),
  182. AATags);
  183. case Intrinsic::invariant_end:
  184. // The first argument to an invariant.end is a "descriptor" type (e.g. a
  185. // pointer to a empty struct) which is never actually dereferenced.
  186. if (ArgIdx == 0)
  187. return MemoryLocation(Arg, LocationSize::precise(0), AATags);
  188. assert(ArgIdx == 2 && "Invalid argument index");
  189. return MemoryLocation(
  190. Arg,
  191. LocationSize::precise(
  192. cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()),
  193. AATags);
  194. case Intrinsic::arm_neon_vld1:
  195. assert(ArgIdx == 0 && "Invalid argument index");
  196. // LLVM's vld1 and vst1 intrinsics currently only support a single
  197. // vector register.
  198. return MemoryLocation(
  199. Arg, LocationSize::precise(DL.getTypeStoreSize(II->getType())),
  200. AATags);
  201. case Intrinsic::arm_neon_vst1:
  202. assert(ArgIdx == 0 && "Invalid argument index");
  203. return MemoryLocation(Arg,
  204. LocationSize::precise(DL.getTypeStoreSize(
  205. II->getArgOperand(1)->getType())),
  206. AATags);
  207. }
  208. assert(
  209. !isa<AnyMemTransferInst>(II) &&
  210. "all memory transfer intrinsics should be handled by the switch above");
  211. }
  212. // We can bound the aliasing properties of memset_pattern16 just as we can
  213. // for memcpy/memset. This is particularly important because the
  214. // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
  215. // whenever possible.
  216. LibFunc F;
  217. if (TLI && TLI->getLibFunc(*Call, F) && TLI->has(F)) {
  218. switch (F) {
  219. case LibFunc_strcpy:
  220. case LibFunc_strcat:
  221. case LibFunc_strncat:
  222. assert((ArgIdx == 0 || ArgIdx == 1) && "Invalid argument index for str function");
  223. return MemoryLocation::getAfter(Arg, AATags);
  224. case LibFunc_memset_chk: {
  225. assert(ArgIdx == 0 && "Invalid argument index for memset_chk");
  226. LocationSize Size = LocationSize::afterPointer();
  227. if (const auto *Len = dyn_cast<ConstantInt>(Call->getArgOperand(2))) {
  228. // memset_chk writes at most Len bytes. It may write less, if Len
  229. // exceeds the specified max size and aborts.
  230. Size = LocationSize::upperBound(Len->getZExtValue());
  231. }
  232. return MemoryLocation(Arg, Size, AATags);
  233. }
  234. case LibFunc_strncpy: {
  235. assert((ArgIdx == 0 || ArgIdx == 1) &&
  236. "Invalid argument index for strncpy");
  237. LocationSize Size = LocationSize::afterPointer();
  238. if (const auto *Len = dyn_cast<ConstantInt>(Call->getArgOperand(2))) {
  239. // strncpy is guaranteed to write Len bytes, but only reads up to Len
  240. // bytes.
  241. Size = ArgIdx == 0 ? LocationSize::precise(Len->getZExtValue())
  242. : LocationSize::upperBound(Len->getZExtValue());
  243. }
  244. return MemoryLocation(Arg, Size, AATags);
  245. }
  246. case LibFunc_memset_pattern16:
  247. case LibFunc_memset_pattern4:
  248. case LibFunc_memset_pattern8:
  249. assert((ArgIdx == 0 || ArgIdx == 1) &&
  250. "Invalid argument index for memset_pattern16");
  251. if (ArgIdx == 1) {
  252. unsigned Size = 16;
  253. if (F == LibFunc_memset_pattern4)
  254. Size = 4;
  255. else if (F == LibFunc_memset_pattern8)
  256. Size = 8;
  257. return MemoryLocation(Arg, LocationSize::precise(Size), AATags);
  258. }
  259. if (const ConstantInt *LenCI =
  260. dyn_cast<ConstantInt>(Call->getArgOperand(2)))
  261. return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
  262. AATags);
  263. return MemoryLocation::getAfter(Arg, AATags);
  264. case LibFunc_bcmp:
  265. case LibFunc_memcmp:
  266. assert((ArgIdx == 0 || ArgIdx == 1) &&
  267. "Invalid argument index for memcmp/bcmp");
  268. if (const ConstantInt *LenCI =
  269. dyn_cast<ConstantInt>(Call->getArgOperand(2)))
  270. return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
  271. AATags);
  272. return MemoryLocation::getAfter(Arg, AATags);
  273. case LibFunc_memchr:
  274. assert((ArgIdx == 0) && "Invalid argument index for memchr");
  275. if (const ConstantInt *LenCI =
  276. dyn_cast<ConstantInt>(Call->getArgOperand(2)))
  277. return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
  278. AATags);
  279. return MemoryLocation::getAfter(Arg, AATags);
  280. case LibFunc_memccpy:
  281. assert((ArgIdx == 0 || ArgIdx == 1) &&
  282. "Invalid argument index for memccpy");
  283. // We only know an upper bound on the number of bytes read/written.
  284. if (const ConstantInt *LenCI =
  285. dyn_cast<ConstantInt>(Call->getArgOperand(3)))
  286. return MemoryLocation(
  287. Arg, LocationSize::upperBound(LenCI->getZExtValue()), AATags);
  288. return MemoryLocation::getAfter(Arg, AATags);
  289. default:
  290. break;
  291. };
  292. }
  293. return MemoryLocation::getBeforeOrAfter(Call->getArgOperand(ArgIdx), AATags);
  294. }