MemoryLocation.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //===- MemoryLocation.cpp - Memory location descriptions -------------------==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Analysis/MemoryLocation.h"
  10. #include "llvm/Analysis/TargetLibraryInfo.h"
  11. #include "llvm/IR/BasicBlock.h"
  12. #include "llvm/IR/DataLayout.h"
  13. #include "llvm/IR/Instructions.h"
  14. #include "llvm/IR/IntrinsicInst.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/IR/Type.h"
  18. using namespace llvm;
  19. MemoryLocation MemoryLocation::get(const LoadInst *LI) {
  20. AAMDNodes AATags;
  21. LI->getAAMetadata(AATags);
  22. const auto &DL = LI->getModule()->getDataLayout();
  23. return MemoryLocation(LI->getPointerOperand(),
  24. DL.getTypeStoreSize(LI->getType()), AATags);
  25. }
  26. MemoryLocation MemoryLocation::get(const StoreInst *SI) {
  27. AAMDNodes AATags;
  28. SI->getAAMetadata(AATags);
  29. const auto &DL = SI->getModule()->getDataLayout();
  30. return MemoryLocation(SI->getPointerOperand(),
  31. DL.getTypeStoreSize(SI->getValueOperand()->getType()),
  32. AATags);
  33. }
  34. MemoryLocation MemoryLocation::get(const VAArgInst *VI) {
  35. AAMDNodes AATags;
  36. VI->getAAMetadata(AATags);
  37. return MemoryLocation(VI->getPointerOperand(), UnknownSize, AATags);
  38. }
  39. MemoryLocation MemoryLocation::get(const AtomicCmpXchgInst *CXI) {
  40. AAMDNodes AATags;
  41. CXI->getAAMetadata(AATags);
  42. const auto &DL = CXI->getModule()->getDataLayout();
  43. return MemoryLocation(
  44. CXI->getPointerOperand(),
  45. DL.getTypeStoreSize(CXI->getCompareOperand()->getType()), AATags);
  46. }
  47. MemoryLocation MemoryLocation::get(const AtomicRMWInst *RMWI) {
  48. AAMDNodes AATags;
  49. RMWI->getAAMetadata(AATags);
  50. const auto &DL = RMWI->getModule()->getDataLayout();
  51. return MemoryLocation(RMWI->getPointerOperand(),
  52. DL.getTypeStoreSize(RMWI->getValOperand()->getType()),
  53. AATags);
  54. }
  55. MemoryLocation MemoryLocation::getForSource(const MemTransferInst *MTI) {
  56. uint64_t Size = UnknownSize;
  57. if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
  58. Size = C->getValue().getZExtValue();
  59. // memcpy/memmove can have AA tags. For memcpy, they apply
  60. // to both the source and the destination.
  61. AAMDNodes AATags;
  62. MTI->getAAMetadata(AATags);
  63. return MemoryLocation(MTI->getRawSource(), Size, AATags);
  64. }
  65. MemoryLocation MemoryLocation::getForDest(const MemIntrinsic *MTI) {
  66. uint64_t Size = UnknownSize;
  67. if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
  68. Size = C->getValue().getZExtValue();
  69. // memcpy/memmove can have AA tags. For memcpy, they apply
  70. // to both the source and the destination.
  71. AAMDNodes AATags;
  72. MTI->getAAMetadata(AATags);
  73. return MemoryLocation(MTI->getRawDest(), Size, AATags);
  74. }
  75. // FIXME: This code is duplicated with BasicAliasAnalysis and should be hoisted
  76. // to some common utility location.
  77. static bool isMemsetPattern16(const Function *MS,
  78. const TargetLibraryInfo &TLI) {
  79. if (TLI.has(LibFunc::memset_pattern16) &&
  80. MS->getName() == "memset_pattern16") {
  81. FunctionType *MemsetType = MS->getFunctionType();
  82. if (!MemsetType->isVarArg() && MemsetType->getNumParams() == 3 &&
  83. isa<PointerType>(MemsetType->getParamType(0)) &&
  84. isa<PointerType>(MemsetType->getParamType(1)) &&
  85. isa<IntegerType>(MemsetType->getParamType(2)))
  86. return true;
  87. }
  88. return false;
  89. }
  90. MemoryLocation MemoryLocation::getForArgument(ImmutableCallSite CS,
  91. unsigned ArgIdx,
  92. const TargetLibraryInfo &TLI) {
  93. AAMDNodes AATags;
  94. CS->getAAMetadata(AATags);
  95. const Value *Arg = CS.getArgument(ArgIdx);
  96. // We may be able to produce an exact size for known intrinsics.
  97. if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
  98. const DataLayout &DL = II->getModule()->getDataLayout();
  99. (void)DL; // HLSL Change - unreferenced local variable
  100. switch (II->getIntrinsicID()) {
  101. default:
  102. break;
  103. case Intrinsic::memset:
  104. case Intrinsic::memcpy:
  105. case Intrinsic::memmove:
  106. assert((ArgIdx == 0 || ArgIdx == 1) &&
  107. "Invalid argument index for memory intrinsic");
  108. if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
  109. return MemoryLocation(Arg, LenCI->getZExtValue(), AATags);
  110. break;
  111. case Intrinsic::lifetime_start:
  112. case Intrinsic::lifetime_end:
  113. case Intrinsic::invariant_start:
  114. assert(ArgIdx == 1 && "Invalid argument index");
  115. return MemoryLocation(
  116. Arg, cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(), AATags);
  117. case Intrinsic::invariant_end:
  118. assert(ArgIdx == 2 && "Invalid argument index");
  119. return MemoryLocation(
  120. Arg, cast<ConstantInt>(II->getArgOperand(1))->getZExtValue(), AATags);
  121. #if 0 // HLSL Change - remove platform intrinsics
  122. case Intrinsic::arm_neon_vld1:
  123. assert(ArgIdx == 0 && "Invalid argument index");
  124. // LLVM's vld1 and vst1 intrinsics currently only support a single
  125. // vector register.
  126. return MemoryLocation(Arg, DL.getTypeStoreSize(II->getType()), AATags);
  127. case Intrinsic::arm_neon_vst1:
  128. assert(ArgIdx == 0 && "Invalid argument index");
  129. return MemoryLocation(
  130. Arg, DL.getTypeStoreSize(II->getArgOperand(1)->getType()), AATags);
  131. #endif // HLSL Change - remove platform intrinsics
  132. }
  133. }
  134. // We can bound the aliasing properties of memset_pattern16 just as we can
  135. // for memcpy/memset. This is particularly important because the
  136. // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
  137. // whenever possible.
  138. if (CS.getCalledFunction() &&
  139. isMemsetPattern16(CS.getCalledFunction(), TLI)) {
  140. assert((ArgIdx == 0 || ArgIdx == 1) &&
  141. "Invalid argument index for memset_pattern16");
  142. if (ArgIdx == 1)
  143. return MemoryLocation(Arg, 16, AATags);
  144. if (const ConstantInt *LenCI = dyn_cast<ConstantInt>(CS.getArgument(2)))
  145. return MemoryLocation(Arg, LenCI->getZExtValue(), AATags);
  146. }
  147. // FIXME: Handle memset_pattern4 and memset_pattern8 also.
  148. return MemoryLocation(CS.getArgument(ArgIdx), UnknownSize, AATags);
  149. }