SelectionDAGAddressAnalysis.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==//
  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/CodeGen/SelectionDAGAddressAnalysis.h"
  9. #include "llvm/CodeGen/ISDOpcodes.h"
  10. #include "llvm/CodeGen/MachineFrameInfo.h"
  11. #include "llvm/CodeGen/MachineFunction.h"
  12. #include "llvm/CodeGen/SelectionDAG.h"
  13. #include "llvm/CodeGen/SelectionDAGNodes.h"
  14. #include "llvm/CodeGen/TargetLowering.h"
  15. #include "llvm/Support/Casting.h"
  16. #include <cstdint>
  17. using namespace llvm;
  18. bool BaseIndexOffset::equalBaseIndex(const BaseIndexOffset &Other,
  19. const SelectionDAG &DAG,
  20. int64_t &Off) const {
  21. // Conservatively fail if we a match failed..
  22. if (!Base.getNode() || !Other.Base.getNode())
  23. return false;
  24. if (!hasValidOffset() || !Other.hasValidOffset())
  25. return false;
  26. // Initial Offset difference.
  27. Off = *Other.Offset - *Offset;
  28. if ((Other.Index == Index) && (Other.IsIndexSignExt == IsIndexSignExt)) {
  29. // Trivial match.
  30. if (Other.Base == Base)
  31. return true;
  32. // Match GlobalAddresses
  33. if (auto *A = dyn_cast<GlobalAddressSDNode>(Base))
  34. if (auto *B = dyn_cast<GlobalAddressSDNode>(Other.Base))
  35. if (A->getGlobal() == B->getGlobal()) {
  36. Off += B->getOffset() - A->getOffset();
  37. return true;
  38. }
  39. // Match Constants
  40. if (auto *A = dyn_cast<ConstantPoolSDNode>(Base))
  41. if (auto *B = dyn_cast<ConstantPoolSDNode>(Other.Base)) {
  42. bool IsMatch =
  43. A->isMachineConstantPoolEntry() == B->isMachineConstantPoolEntry();
  44. if (IsMatch) {
  45. if (A->isMachineConstantPoolEntry())
  46. IsMatch = A->getMachineCPVal() == B->getMachineCPVal();
  47. else
  48. IsMatch = A->getConstVal() == B->getConstVal();
  49. }
  50. if (IsMatch) {
  51. Off += B->getOffset() - A->getOffset();
  52. return true;
  53. }
  54. }
  55. const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
  56. // Match FrameIndexes.
  57. if (auto *A = dyn_cast<FrameIndexSDNode>(Base))
  58. if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base)) {
  59. // Equal FrameIndexes - offsets are directly comparable.
  60. if (A->getIndex() == B->getIndex())
  61. return true;
  62. // Non-equal FrameIndexes - If both frame indices are fixed
  63. // we know their relative offsets and can compare them. Otherwise
  64. // we must be conservative.
  65. if (MFI.isFixedObjectIndex(A->getIndex()) &&
  66. MFI.isFixedObjectIndex(B->getIndex())) {
  67. Off += MFI.getObjectOffset(B->getIndex()) -
  68. MFI.getObjectOffset(A->getIndex());
  69. return true;
  70. }
  71. }
  72. }
  73. return false;
  74. }
  75. bool BaseIndexOffset::computeAliasing(const SDNode *Op0,
  76. const Optional<int64_t> NumBytes0,
  77. const SDNode *Op1,
  78. const Optional<int64_t> NumBytes1,
  79. const SelectionDAG &DAG, bool &IsAlias) {
  80. BaseIndexOffset BasePtr0 = match(Op0, DAG);
  81. BaseIndexOffset BasePtr1 = match(Op1, DAG);
  82. if (!(BasePtr0.getBase().getNode() && BasePtr1.getBase().getNode()))
  83. return false;
  84. int64_t PtrDiff;
  85. if (NumBytes0.hasValue() && NumBytes1.hasValue() &&
  86. BasePtr0.equalBaseIndex(BasePtr1, DAG, PtrDiff)) {
  87. // BasePtr1 is PtrDiff away from BasePtr0. They alias if none of the
  88. // following situations arise:
  89. IsAlias = !(
  90. // [----BasePtr0----]
  91. // [---BasePtr1--]
  92. // ========PtrDiff========>
  93. (*NumBytes0 <= PtrDiff) ||
  94. // [----BasePtr0----]
  95. // [---BasePtr1--]
  96. // =====(-PtrDiff)====>
  97. (PtrDiff + *NumBytes1 <= 0)); // i.e. *NumBytes1 < -PtrDiff.
  98. return true;
  99. }
  100. // If both BasePtr0 and BasePtr1 are FrameIndexes, we will not be
  101. // able to calculate their relative offset if at least one arises
  102. // from an alloca. However, these allocas cannot overlap and we
  103. // can infer there is no alias.
  104. if (auto *A = dyn_cast<FrameIndexSDNode>(BasePtr0.getBase()))
  105. if (auto *B = dyn_cast<FrameIndexSDNode>(BasePtr1.getBase())) {
  106. MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
  107. // If the base are the same frame index but the we couldn't find a
  108. // constant offset, (indices are different) be conservative.
  109. if (A != B && (!MFI.isFixedObjectIndex(A->getIndex()) ||
  110. !MFI.isFixedObjectIndex(B->getIndex()))) {
  111. IsAlias = false;
  112. return true;
  113. }
  114. }
  115. bool IsFI0 = isa<FrameIndexSDNode>(BasePtr0.getBase());
  116. bool IsFI1 = isa<FrameIndexSDNode>(BasePtr1.getBase());
  117. bool IsGV0 = isa<GlobalAddressSDNode>(BasePtr0.getBase());
  118. bool IsGV1 = isa<GlobalAddressSDNode>(BasePtr1.getBase());
  119. bool IsCV0 = isa<ConstantPoolSDNode>(BasePtr0.getBase());
  120. bool IsCV1 = isa<ConstantPoolSDNode>(BasePtr1.getBase());
  121. // If of mismatched base types or checkable indices we can check
  122. // they do not alias.
  123. if ((BasePtr0.getIndex() == BasePtr1.getIndex() || (IsFI0 != IsFI1) ||
  124. (IsGV0 != IsGV1) || (IsCV0 != IsCV1)) &&
  125. (IsFI0 || IsGV0 || IsCV0) && (IsFI1 || IsGV1 || IsCV1)) {
  126. IsAlias = false;
  127. return true;
  128. }
  129. return false; // Cannot determine whether the pointers alias.
  130. }
  131. bool BaseIndexOffset::contains(const SelectionDAG &DAG, int64_t BitSize,
  132. const BaseIndexOffset &Other,
  133. int64_t OtherBitSize, int64_t &BitOffset) const {
  134. int64_t Offset;
  135. if (!equalBaseIndex(Other, DAG, Offset))
  136. return false;
  137. if (Offset >= 0) {
  138. // Other is after *this:
  139. // [-------*this---------]
  140. // [---Other--]
  141. // ==Offset==>
  142. BitOffset = 8 * Offset;
  143. return BitOffset + OtherBitSize <= BitSize;
  144. }
  145. // Other starts strictly before *this, it cannot be fully contained.
  146. // [-------*this---------]
  147. // [--Other--]
  148. return false;
  149. }
  150. /// Parses tree in Ptr for base, index, offset addresses.
  151. static BaseIndexOffset matchLSNode(const LSBaseSDNode *N,
  152. const SelectionDAG &DAG) {
  153. SDValue Ptr = N->getBasePtr();
  154. // (((B + I*M) + c)) + c ...
  155. SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr);
  156. SDValue Index = SDValue();
  157. int64_t Offset = 0;
  158. bool IsIndexSignExt = false;
  159. // pre-inc/pre-dec ops are components of EA.
  160. if (N->getAddressingMode() == ISD::PRE_INC) {
  161. if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
  162. Offset += C->getSExtValue();
  163. else // If unknown, give up now.
  164. return BaseIndexOffset(SDValue(), SDValue(), 0, false);
  165. } else if (N->getAddressingMode() == ISD::PRE_DEC) {
  166. if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
  167. Offset -= C->getSExtValue();
  168. else // If unknown, give up now.
  169. return BaseIndexOffset(SDValue(), SDValue(), 0, false);
  170. }
  171. // Consume constant adds & ors with appropriate masking.
  172. while (true) {
  173. switch (Base->getOpcode()) {
  174. case ISD::OR:
  175. // Only consider ORs which act as adds.
  176. if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1)))
  177. if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) {
  178. Offset += C->getSExtValue();
  179. Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
  180. continue;
  181. }
  182. break;
  183. case ISD::ADD:
  184. if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) {
  185. Offset += C->getSExtValue();
  186. Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
  187. continue;
  188. }
  189. break;
  190. case ISD::LOAD:
  191. case ISD::STORE: {
  192. auto *LSBase = cast<LSBaseSDNode>(Base.getNode());
  193. unsigned int IndexResNo = (Base->getOpcode() == ISD::LOAD) ? 1 : 0;
  194. if (LSBase->isIndexed() && Base.getResNo() == IndexResNo)
  195. if (auto *C = dyn_cast<ConstantSDNode>(LSBase->getOffset())) {
  196. auto Off = C->getSExtValue();
  197. if (LSBase->getAddressingMode() == ISD::PRE_DEC ||
  198. LSBase->getAddressingMode() == ISD::POST_DEC)
  199. Offset -= Off;
  200. else
  201. Offset += Off;
  202. Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr());
  203. continue;
  204. }
  205. break;
  206. }
  207. }
  208. // If we get here break out of the loop.
  209. break;
  210. }
  211. if (Base->getOpcode() == ISD::ADD) {
  212. // TODO: The following code appears to be needless as it just
  213. // bails on some Ptrs early, reducing the cases where we
  214. // find equivalence. We should be able to remove this.
  215. // Inside a loop the current BASE pointer is calculated using an ADD and a
  216. // MUL instruction. In this case Base is the actual BASE pointer.
  217. // (i64 add (i64 %array_ptr)
  218. // (i64 mul (i64 %induction_var)
  219. // (i64 %element_size)))
  220. if (Base->getOperand(1)->getOpcode() == ISD::MUL)
  221. return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
  222. // Look at Base + Index + Offset cases.
  223. Index = Base->getOperand(1);
  224. SDValue PotentialBase = Base->getOperand(0);
  225. // Skip signextends.
  226. if (Index->getOpcode() == ISD::SIGN_EXTEND) {
  227. Index = Index->getOperand(0);
  228. IsIndexSignExt = true;
  229. }
  230. // Check if Index Offset pattern
  231. if (Index->getOpcode() != ISD::ADD ||
  232. !isa<ConstantSDNode>(Index->getOperand(1)))
  233. return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt);
  234. Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue();
  235. Index = Index->getOperand(0);
  236. if (Index->getOpcode() == ISD::SIGN_EXTEND) {
  237. Index = Index->getOperand(0);
  238. IsIndexSignExt = true;
  239. } else
  240. IsIndexSignExt = false;
  241. Base = PotentialBase;
  242. }
  243. return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
  244. }
  245. BaseIndexOffset BaseIndexOffset::match(const SDNode *N,
  246. const SelectionDAG &DAG) {
  247. if (const auto *LS0 = dyn_cast<LSBaseSDNode>(N))
  248. return matchLSNode(LS0, DAG);
  249. if (const auto *LN = dyn_cast<LifetimeSDNode>(N)) {
  250. if (LN->hasOffset())
  251. return BaseIndexOffset(LN->getOperand(1), SDValue(), LN->getOffset(),
  252. false);
  253. return BaseIndexOffset(LN->getOperand(1), SDValue(), false);
  254. }
  255. return BaseIndexOffset();
  256. }
  257. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  258. LLVM_DUMP_METHOD void BaseIndexOffset::dump() const {
  259. print(dbgs());
  260. }
  261. void BaseIndexOffset::print(raw_ostream& OS) const {
  262. OS << "BaseIndexOffset base=[";
  263. Base->print(OS);
  264. OS << "] index=[";
  265. if (Index)
  266. Index->print(OS);
  267. OS << "] offset=" << Offset;
  268. }
  269. #endif