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