Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 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 | |
| 10 | #include "llvm/Analysis/MemoryLocation.h" |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 12 | #include "llvm/IR/BasicBlock.h" |
| 13 | #include "llvm/IR/DataLayout.h" |
| 14 | #include "llvm/IR/Instructions.h" |
| 15 | #include "llvm/IR/IntrinsicInst.h" |
| 16 | #include "llvm/IR/LLVMContext.h" |
| 17 | #include "llvm/IR/Module.h" |
| 18 | #include "llvm/IR/Type.h" |
| 19 | using namespace llvm; |
| 20 | |
George Burgess IV | d98d505 | 2018-10-10 01:35:22 +0000 | [diff] [blame] | 21 | void LocationSize::print(raw_ostream &OS) const { |
| 22 | OS << "LocationSize::"; |
| 23 | if (*this == unknown()) |
| 24 | OS << "unknown"; |
| 25 | else if (*this == mapEmpty()) |
| 26 | OS << "mapEmpty"; |
| 27 | else if (*this == mapTombstone()) |
| 28 | OS << "mapTombstone"; |
| 29 | else |
| 30 | OS << "precise(" << getValue() << ')'; |
| 31 | } |
| 32 | |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 33 | MemoryLocation MemoryLocation::get(const LoadInst *LI) { |
| 34 | AAMDNodes AATags; |
| 35 | LI->getAAMetadata(AATags); |
| 36 | const auto &DL = LI->getModule()->getDataLayout(); |
| 37 | |
| 38 | return MemoryLocation(LI->getPointerOperand(), |
| 39 | DL.getTypeStoreSize(LI->getType()), AATags); |
| 40 | } |
| 41 | |
| 42 | MemoryLocation MemoryLocation::get(const StoreInst *SI) { |
| 43 | AAMDNodes AATags; |
| 44 | SI->getAAMetadata(AATags); |
| 45 | const auto &DL = SI->getModule()->getDataLayout(); |
| 46 | |
| 47 | return MemoryLocation(SI->getPointerOperand(), |
| 48 | DL.getTypeStoreSize(SI->getValueOperand()->getType()), |
| 49 | AATags); |
| 50 | } |
| 51 | |
| 52 | MemoryLocation MemoryLocation::get(const VAArgInst *VI) { |
| 53 | AAMDNodes AATags; |
| 54 | VI->getAAMetadata(AATags); |
| 55 | |
| 56 | return MemoryLocation(VI->getPointerOperand(), UnknownSize, AATags); |
| 57 | } |
| 58 | |
| 59 | MemoryLocation MemoryLocation::get(const AtomicCmpXchgInst *CXI) { |
| 60 | AAMDNodes AATags; |
| 61 | CXI->getAAMetadata(AATags); |
| 62 | const auto &DL = CXI->getModule()->getDataLayout(); |
| 63 | |
| 64 | return MemoryLocation( |
| 65 | CXI->getPointerOperand(), |
| 66 | DL.getTypeStoreSize(CXI->getCompareOperand()->getType()), AATags); |
| 67 | } |
| 68 | |
| 69 | MemoryLocation MemoryLocation::get(const AtomicRMWInst *RMWI) { |
| 70 | AAMDNodes AATags; |
| 71 | RMWI->getAAMetadata(AATags); |
| 72 | const auto &DL = RMWI->getModule()->getDataLayout(); |
| 73 | |
| 74 | return MemoryLocation(RMWI->getPointerOperand(), |
| 75 | DL.getTypeStoreSize(RMWI->getValOperand()->getType()), |
| 76 | AATags); |
| 77 | } |
| 78 | |
| 79 | MemoryLocation MemoryLocation::getForSource(const MemTransferInst *MTI) { |
Daniel Neilson | cc45e92 | 2018-04-23 19:06:49 +0000 | [diff] [blame] | 80 | return getForSource(cast<AnyMemTransferInst>(MTI)); |
| 81 | } |
| 82 | |
| 83 | MemoryLocation MemoryLocation::getForSource(const AtomicMemTransferInst *MTI) { |
| 84 | return getForSource(cast<AnyMemTransferInst>(MTI)); |
| 85 | } |
| 86 | |
| 87 | MemoryLocation MemoryLocation::getForSource(const AnyMemTransferInst *MTI) { |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 88 | uint64_t Size = UnknownSize; |
| 89 | if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) |
| 90 | Size = C->getValue().getZExtValue(); |
| 91 | |
| 92 | // memcpy/memmove can have AA tags. For memcpy, they apply |
| 93 | // to both the source and the destination. |
| 94 | AAMDNodes AATags; |
| 95 | MTI->getAAMetadata(AATags); |
| 96 | |
| 97 | return MemoryLocation(MTI->getRawSource(), Size, AATags); |
| 98 | } |
| 99 | |
Daniel Neilson | cc45e92 | 2018-04-23 19:06:49 +0000 | [diff] [blame] | 100 | MemoryLocation MemoryLocation::getForDest(const MemIntrinsic *MI) { |
| 101 | return getForDest(cast<AnyMemIntrinsic>(MI)); |
| 102 | } |
| 103 | |
| 104 | MemoryLocation MemoryLocation::getForDest(const AtomicMemIntrinsic *MI) { |
| 105 | return getForDest(cast<AnyMemIntrinsic>(MI)); |
| 106 | } |
| 107 | |
| 108 | MemoryLocation MemoryLocation::getForDest(const AnyMemIntrinsic *MI) { |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 109 | uint64_t Size = UnknownSize; |
Daniel Neilson | cc45e92 | 2018-04-23 19:06:49 +0000 | [diff] [blame] | 110 | if (ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength())) |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 111 | Size = C->getValue().getZExtValue(); |
| 112 | |
| 113 | // memcpy/memmove can have AA tags. For memcpy, they apply |
| 114 | // to both the source and the destination. |
| 115 | AAMDNodes AATags; |
Daniel Neilson | cc45e92 | 2018-04-23 19:06:49 +0000 | [diff] [blame] | 116 | MI->getAAMetadata(AATags); |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 117 | |
Daniel Neilson | cc45e92 | 2018-04-23 19:06:49 +0000 | [diff] [blame] | 118 | return MemoryLocation(MI->getRawDest(), Size, AATags); |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 119 | } |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 120 | |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 121 | MemoryLocation MemoryLocation::getForArgument(ImmutableCallSite CS, |
| 122 | unsigned ArgIdx, |
Philip Reames | cb8b327 | 2018-09-07 21:36:11 +0000 | [diff] [blame] | 123 | const TargetLibraryInfo *TLI) { |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 124 | AAMDNodes AATags; |
| 125 | CS->getAAMetadata(AATags); |
| 126 | const Value *Arg = CS.getArgument(ArgIdx); |
| 127 | |
| 128 | // We may be able to produce an exact size for known intrinsics. |
| 129 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) { |
| 130 | const DataLayout &DL = II->getModule()->getDataLayout(); |
| 131 | |
| 132 | switch (II->getIntrinsicID()) { |
| 133 | default: |
| 134 | break; |
| 135 | case Intrinsic::memset: |
| 136 | case Intrinsic::memcpy: |
| 137 | case Intrinsic::memmove: |
| 138 | assert((ArgIdx == 0 || ArgIdx == 1) && |
| 139 | "Invalid argument index for memory intrinsic"); |
| 140 | if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) |
| 141 | return MemoryLocation(Arg, LenCI->getZExtValue(), AATags); |
| 142 | break; |
| 143 | |
| 144 | case Intrinsic::lifetime_start: |
| 145 | case Intrinsic::lifetime_end: |
| 146 | case Intrinsic::invariant_start: |
| 147 | assert(ArgIdx == 1 && "Invalid argument index"); |
| 148 | return MemoryLocation( |
| 149 | Arg, cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(), AATags); |
| 150 | |
| 151 | case Intrinsic::invariant_end: |
Philip Reames | 684fa57 | 2018-08-16 20:48:55 +0000 | [diff] [blame] | 152 | // The first argument to an invariant.end is a "descriptor" type (e.g. a |
| 153 | // pointer to a empty struct) which is never actually dereferenced. |
| 154 | if (ArgIdx == 0) |
| 155 | return MemoryLocation(Arg, 0, AATags); |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 156 | assert(ArgIdx == 2 && "Invalid argument index"); |
| 157 | return MemoryLocation( |
| 158 | Arg, cast<ConstantInt>(II->getArgOperand(1))->getZExtValue(), AATags); |
| 159 | |
| 160 | case Intrinsic::arm_neon_vld1: |
| 161 | assert(ArgIdx == 0 && "Invalid argument index"); |
| 162 | // LLVM's vld1 and vst1 intrinsics currently only support a single |
| 163 | // vector register. |
| 164 | return MemoryLocation(Arg, DL.getTypeStoreSize(II->getType()), AATags); |
| 165 | |
| 166 | case Intrinsic::arm_neon_vst1: |
| 167 | assert(ArgIdx == 0 && "Invalid argument index"); |
| 168 | return MemoryLocation( |
| 169 | Arg, DL.getTypeStoreSize(II->getArgOperand(1)->getType()), AATags); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // We can bound the aliasing properties of memset_pattern16 just as we can |
| 174 | // for memcpy/memset. This is particularly important because the |
| 175 | // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16 |
| 176 | // whenever possible. |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 177 | LibFunc F; |
Philip Reames | cb8b327 | 2018-09-07 21:36:11 +0000 | [diff] [blame] | 178 | if (TLI && CS.getCalledFunction() && |
| 179 | TLI->getLibFunc(*CS.getCalledFunction(), F) && |
| 180 | F == LibFunc_memset_pattern16 && TLI->has(F)) { |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 181 | assert((ArgIdx == 0 || ArgIdx == 1) && |
| 182 | "Invalid argument index for memset_pattern16"); |
| 183 | if (ArgIdx == 1) |
| 184 | return MemoryLocation(Arg, 16, AATags); |
| 185 | if (const ConstantInt *LenCI = dyn_cast<ConstantInt>(CS.getArgument(2))) |
| 186 | return MemoryLocation(Arg, LenCI->getZExtValue(), AATags); |
| 187 | } |
| 188 | // FIXME: Handle memset_pattern4 and memset_pattern8 also. |
| 189 | |
| 190 | return MemoryLocation(CS.getArgument(ArgIdx), UnknownSize, AATags); |
| 191 | } |