Operator.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //===-- Operator.cpp - Implement the LLVM operators -----------------------===//
  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. //
  9. // This file implements the non-inline methods for the LLVM Operator classes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/IR/Operator.h"
  13. #include "llvm/IR/DataLayout.h"
  14. #include "llvm/IR/GetElementPtrTypeIterator.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "ConstantsContext.h"
  17. namespace llvm {
  18. bool Operator::hasPoisonGeneratingFlags() const {
  19. switch (getOpcode()) {
  20. case Instruction::Add:
  21. case Instruction::Sub:
  22. case Instruction::Mul:
  23. case Instruction::Shl: {
  24. auto *OBO = cast<OverflowingBinaryOperator>(this);
  25. return OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap();
  26. }
  27. case Instruction::UDiv:
  28. case Instruction::SDiv:
  29. case Instruction::AShr:
  30. case Instruction::LShr:
  31. return cast<PossiblyExactOperator>(this)->isExact();
  32. case Instruction::GetElementPtr: {
  33. auto *GEP = cast<GEPOperator>(this);
  34. // Note: inrange exists on constexpr only
  35. return GEP->isInBounds() || GEP->getInRangeIndex() != None;
  36. }
  37. default:
  38. if (const auto *FP = dyn_cast<FPMathOperator>(this))
  39. return FP->hasNoNaNs() || FP->hasNoInfs();
  40. return false;
  41. }
  42. }
  43. Type *GEPOperator::getSourceElementType() const {
  44. if (auto *I = dyn_cast<GetElementPtrInst>(this))
  45. return I->getSourceElementType();
  46. return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
  47. }
  48. Type *GEPOperator::getResultElementType() const {
  49. if (auto *I = dyn_cast<GetElementPtrInst>(this))
  50. return I->getResultElementType();
  51. return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
  52. }
  53. Align GEPOperator::getMaxPreservedAlignment(const DataLayout &DL) const {
  54. /// compute the worse possible offset for every level of the GEP et accumulate
  55. /// the minimum alignment into Result.
  56. Align Result = Align(llvm::Value::MaximumAlignment);
  57. for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
  58. GTI != GTE; ++GTI) {
  59. int64_t Offset = 1;
  60. ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
  61. if (StructType *STy = GTI.getStructTypeOrNull()) {
  62. const StructLayout *SL = DL.getStructLayout(STy);
  63. Offset = SL->getElementOffset(OpC->getZExtValue());
  64. } else {
  65. assert(GTI.isSequential() && "should be sequencial");
  66. /// If the index isn't know we take 1 because it is the index that will
  67. /// give the worse alignment of the offset.
  68. int64_t ElemCount = 1;
  69. if (OpC)
  70. ElemCount = OpC->getZExtValue();
  71. Offset = DL.getTypeAllocSize(GTI.getIndexedType()) * ElemCount;
  72. }
  73. Result = Align(MinAlign(Offset, Result.value()));
  74. }
  75. return Result;
  76. }
  77. bool GEPOperator::accumulateConstantOffset(
  78. const DataLayout &DL, APInt &Offset,
  79. function_ref<bool(Value &, APInt &)> ExternalAnalysis) const {
  80. assert(Offset.getBitWidth() ==
  81. DL.getIndexSizeInBits(getPointerAddressSpace()) &&
  82. "The offset bit width does not match DL specification.");
  83. SmallVector<const Value *> Index(llvm::drop_begin(operand_values()));
  84. return GEPOperator::accumulateConstantOffset(getSourceElementType(), Index,
  85. DL, Offset, ExternalAnalysis);
  86. }
  87. bool GEPOperator::accumulateConstantOffset(
  88. Type *SourceType, ArrayRef<const Value *> Index, const DataLayout &DL,
  89. APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) {
  90. bool UsedExternalAnalysis = false;
  91. auto AccumulateOffset = [&](APInt Index, uint64_t Size) -> bool {
  92. Index = Index.sextOrTrunc(Offset.getBitWidth());
  93. APInt IndexedSize = APInt(Offset.getBitWidth(), Size);
  94. // For array or vector indices, scale the index by the size of the type.
  95. if (!UsedExternalAnalysis) {
  96. Offset += Index * IndexedSize;
  97. } else {
  98. // External Analysis can return a result higher/lower than the value
  99. // represents. We need to detect overflow/underflow.
  100. bool Overflow = false;
  101. APInt OffsetPlus = Index.smul_ov(IndexedSize, Overflow);
  102. if (Overflow)
  103. return false;
  104. Offset = Offset.sadd_ov(OffsetPlus, Overflow);
  105. if (Overflow)
  106. return false;
  107. }
  108. return true;
  109. };
  110. auto begin = generic_gep_type_iterator<decltype(Index.begin())>::begin(
  111. SourceType, Index.begin());
  112. auto end = generic_gep_type_iterator<decltype(Index.end())>::end(Index.end());
  113. for (auto GTI = begin, GTE = end; GTI != GTE; ++GTI) {
  114. // Scalable vectors are multiplied by a runtime constant.
  115. bool ScalableType = false;
  116. if (isa<ScalableVectorType>(GTI.getIndexedType()))
  117. ScalableType = true;
  118. Value *V = GTI.getOperand();
  119. StructType *STy = GTI.getStructTypeOrNull();
  120. // Handle ConstantInt if possible.
  121. if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
  122. if (ConstOffset->isZero())
  123. continue;
  124. // if the type is scalable and the constant is not zero (vscale * n * 0 =
  125. // 0) bailout.
  126. if (ScalableType)
  127. return false;
  128. // Handle a struct index, which adds its field offset to the pointer.
  129. if (STy) {
  130. unsigned ElementIdx = ConstOffset->getZExtValue();
  131. const StructLayout *SL = DL.getStructLayout(STy);
  132. // Element offset is in bytes.
  133. if (!AccumulateOffset(
  134. APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
  135. 1))
  136. return false;
  137. continue;
  138. }
  139. if (!AccumulateOffset(ConstOffset->getValue(),
  140. DL.getTypeAllocSize(GTI.getIndexedType())))
  141. return false;
  142. continue;
  143. }
  144. // The operand is not constant, check if an external analysis was provided.
  145. // External analsis is not applicable to a struct type.
  146. if (!ExternalAnalysis || STy || ScalableType)
  147. return false;
  148. APInt AnalysisIndex;
  149. if (!ExternalAnalysis(*V, AnalysisIndex))
  150. return false;
  151. UsedExternalAnalysis = true;
  152. if (!AccumulateOffset(AnalysisIndex,
  153. DL.getTypeAllocSize(GTI.getIndexedType())))
  154. return false;
  155. }
  156. return true;
  157. }
  158. bool GEPOperator::collectOffset(
  159. const DataLayout &DL, unsigned BitWidth,
  160. MapVector<Value *, APInt> &VariableOffsets,
  161. APInt &ConstantOffset) const {
  162. assert(BitWidth == DL.getIndexSizeInBits(getPointerAddressSpace()) &&
  163. "The offset bit width does not match DL specification.");
  164. auto CollectConstantOffset = [&](APInt Index, uint64_t Size) {
  165. Index = Index.sextOrTrunc(BitWidth);
  166. APInt IndexedSize = APInt(BitWidth, Size);
  167. ConstantOffset += Index * IndexedSize;
  168. };
  169. for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
  170. GTI != GTE; ++GTI) {
  171. // Scalable vectors are multiplied by a runtime constant.
  172. bool ScalableType = isa<ScalableVectorType>(GTI.getIndexedType());
  173. Value *V = GTI.getOperand();
  174. StructType *STy = GTI.getStructTypeOrNull();
  175. // Handle ConstantInt if possible.
  176. if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
  177. if (ConstOffset->isZero())
  178. continue;
  179. // If the type is scalable and the constant is not zero (vscale * n * 0 =
  180. // 0) bailout.
  181. // TODO: If the runtime value is accessible at any point before DWARF
  182. // emission, then we could potentially keep a forward reference to it
  183. // in the debug value to be filled in later.
  184. if (ScalableType)
  185. return false;
  186. // Handle a struct index, which adds its field offset to the pointer.
  187. if (STy) {
  188. unsigned ElementIdx = ConstOffset->getZExtValue();
  189. const StructLayout *SL = DL.getStructLayout(STy);
  190. // Element offset is in bytes.
  191. CollectConstantOffset(APInt(BitWidth, SL->getElementOffset(ElementIdx)),
  192. 1);
  193. continue;
  194. }
  195. CollectConstantOffset(ConstOffset->getValue(),
  196. DL.getTypeAllocSize(GTI.getIndexedType()));
  197. continue;
  198. }
  199. if (STy || ScalableType)
  200. return false;
  201. APInt IndexedSize =
  202. APInt(BitWidth, DL.getTypeAllocSize(GTI.getIndexedType()));
  203. // Insert an initial offset of 0 for V iff none exists already, then
  204. // increment the offset by IndexedSize.
  205. if (!IndexedSize.isZero()) {
  206. VariableOffsets.insert({V, APInt(BitWidth, 0)});
  207. VariableOffsets[V] += IndexedSize;
  208. }
  209. }
  210. return true;
  211. }
  212. void FastMathFlags::print(raw_ostream &O) const {
  213. if (all())
  214. O << " fast";
  215. else {
  216. if (allowReassoc())
  217. O << " reassoc";
  218. if (noNaNs())
  219. O << " nnan";
  220. if (noInfs())
  221. O << " ninf";
  222. if (noSignedZeros())
  223. O << " nsz";
  224. if (allowReciprocal())
  225. O << " arcp";
  226. if (allowContract())
  227. O << " contract";
  228. if (approxFunc())
  229. O << " afn";
  230. }
  231. }
  232. } // namespace llvm