LLVM 19.0.0git
Operator.cpp
Go to the documentation of this file.
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"
17
18#include "ConstantsContext.h"
19
20namespace llvm {
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->getInRangeIndex() != 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
55 return true;
56 auto *I = dyn_cast<Instruction>(this);
57 return I && I->hasPoisonGeneratingMetadata();
58}
59
61 if (auto *I = dyn_cast<GetElementPtrInst>(this))
62 return I->getSourceElementType();
63 return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
64}
65
67 if (auto *I = dyn_cast<GetElementPtrInst>(this))
68 return I->getResultElementType();
69 return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
70}
71
73 /// compute the worse possible offset for every level of the GEP et accumulate
74 /// the minimum alignment into Result.
75
77 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
78 GTI != GTE; ++GTI) {
80 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
81
82 if (StructType *STy = GTI.getStructTypeOrNull()) {
83 const StructLayout *SL = DL.getStructLayout(STy);
85 } else {
86 assert(GTI.isSequential() && "should be sequencial");
87 /// If the index isn't known, we take 1 because it is the index that will
88 /// give the worse alignment of the offset.
89 const uint64_t ElemCount = OpC ? OpC->getZExtValue() : 1;
90 Offset = GTI.getSequentialElementStride(DL) * ElemCount;
91 }
92 Result = Align(MinAlign(Offset, Result.value()));
93 }
94 return Result;
95}
96
98 const DataLayout &DL, APInt &Offset,
99 function_ref<bool(Value &, APInt &)> ExternalAnalysis) const {
100 assert(Offset.getBitWidth() ==
101 DL.getIndexSizeInBits(getPointerAddressSpace()) &&
102 "The offset bit width does not match DL specification.");
105 DL, Offset, ExternalAnalysis);
106}
107
109 Type *SourceType, ArrayRef<const Value *> Index, const DataLayout &DL,
110 APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) {
111 // Fast path for canonical getelementptr i8 form.
112 if (SourceType->isIntegerTy(8) && !ExternalAnalysis) {
113 if (auto *CI = dyn_cast<ConstantInt>(Index.front())) {
114 Offset += CI->getValue().sextOrTrunc(Offset.getBitWidth());
115 return true;
116 }
117 return false;
118 }
119
120 bool UsedExternalAnalysis = false;
121 auto AccumulateOffset = [&](APInt Index, uint64_t Size) -> bool {
122 Index = Index.sextOrTrunc(Offset.getBitWidth());
123 APInt IndexedSize = APInt(Offset.getBitWidth(), Size);
124 // For array or vector indices, scale the index by the size of the type.
125 if (!UsedExternalAnalysis) {
126 Offset += Index * IndexedSize;
127 } else {
128 // External Analysis can return a result higher/lower than the value
129 // represents. We need to detect overflow/underflow.
130 bool Overflow = false;
131 APInt OffsetPlus = Index.smul_ov(IndexedSize, Overflow);
132 if (Overflow)
133 return false;
134 Offset = Offset.sadd_ov(OffsetPlus, Overflow);
135 if (Overflow)
136 return false;
137 }
138 return true;
139 };
140 auto begin = generic_gep_type_iterator<decltype(Index.begin())>::begin(
141 SourceType, Index.begin());
142 auto end = generic_gep_type_iterator<decltype(Index.end())>::end(Index.end());
143 for (auto GTI = begin, GTE = end; GTI != GTE; ++GTI) {
144 // Scalable vectors are multiplied by a runtime constant.
145 bool ScalableType = GTI.getIndexedType()->isScalableTy();
146
147 Value *V = GTI.getOperand();
148 StructType *STy = GTI.getStructTypeOrNull();
149 // Handle ConstantInt if possible.
150 if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
151 if (ConstOffset->isZero())
152 continue;
153 // if the type is scalable and the constant is not zero (vscale * n * 0 =
154 // 0) bailout.
155 if (ScalableType)
156 return false;
157 // Handle a struct index, which adds its field offset to the pointer.
158 if (STy) {
159 unsigned ElementIdx = ConstOffset->getZExtValue();
160 const StructLayout *SL = DL.getStructLayout(STy);
161 // Element offset is in bytes.
162 if (!AccumulateOffset(
163 APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
164 1))
165 return false;
166 continue;
167 }
168 if (!AccumulateOffset(ConstOffset->getValue(),
169 GTI.getSequentialElementStride(DL)))
170 return false;
171 continue;
172 }
173
174 // The operand is not constant, check if an external analysis was provided.
175 // External analsis is not applicable to a struct type.
176 if (!ExternalAnalysis || STy || ScalableType)
177 return false;
178 APInt AnalysisIndex;
179 if (!ExternalAnalysis(*V, AnalysisIndex))
180 return false;
181 UsedExternalAnalysis = true;
182 if (!AccumulateOffset(AnalysisIndex, GTI.getSequentialElementStride(DL)))
183 return false;
184 }
185 return true;
186}
187
189 const DataLayout &DL, unsigned BitWidth,
190 MapVector<Value *, APInt> &VariableOffsets,
191 APInt &ConstantOffset) const {
192 assert(BitWidth == DL.getIndexSizeInBits(getPointerAddressSpace()) &&
193 "The offset bit width does not match DL specification.");
194
195 auto CollectConstantOffset = [&](APInt Index, uint64_t Size) {
196 Index = Index.sextOrTrunc(BitWidth);
197 APInt IndexedSize = APInt(BitWidth, Size);
198 ConstantOffset += Index * IndexedSize;
199 };
200
201 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
202 GTI != GTE; ++GTI) {
203 // Scalable vectors are multiplied by a runtime constant.
204 bool ScalableType = GTI.getIndexedType()->isScalableTy();
205
206 Value *V = GTI.getOperand();
207 StructType *STy = GTI.getStructTypeOrNull();
208 // Handle ConstantInt if possible.
209 if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
210 if (ConstOffset->isZero())
211 continue;
212 // If the type is scalable and the constant is not zero (vscale * n * 0 =
213 // 0) bailout.
214 // TODO: If the runtime value is accessible at any point before DWARF
215 // emission, then we could potentially keep a forward reference to it
216 // in the debug value to be filled in later.
217 if (ScalableType)
218 return false;
219 // Handle a struct index, which adds its field offset to the pointer.
220 if (STy) {
221 unsigned ElementIdx = ConstOffset->getZExtValue();
222 const StructLayout *SL = DL.getStructLayout(STy);
223 // Element offset is in bytes.
224 CollectConstantOffset(APInt(BitWidth, SL->getElementOffset(ElementIdx)),
225 1);
226 continue;
227 }
228 CollectConstantOffset(ConstOffset->getValue(),
229 GTI.getSequentialElementStride(DL));
230 continue;
231 }
232
233 if (STy || ScalableType)
234 return false;
235 APInt IndexedSize = APInt(BitWidth, GTI.getSequentialElementStride(DL));
236 // Insert an initial offset of 0 for V iff none exists already, then
237 // increment the offset by IndexedSize.
238 if (!IndexedSize.isZero()) {
239 auto *It = VariableOffsets.insert({V, APInt(BitWidth, 0)}).first;
240 It->second += IndexedSize;
241 }
242 }
243 return true;
244}
245
247 if (all())
248 O << " fast";
249 else {
250 if (allowReassoc())
251 O << " reassoc";
252 if (noNaNs())
253 O << " nnan";
254 if (noInfs())
255 O << " ninf";
256 if (noSignedZeros())
257 O << " nsz";
258 if (allowReciprocal())
259 O << " arcp";
260 if (allowContract())
261 O << " contract";
262 if (approxFunc())
263 O << " afn";
264 }
265}
266} // namespace llvm
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
uint64_t Size
Hexagon Common GEP
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
Definition: APInt.h:76
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1966
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:153
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
bool noSignedZeros() const
Definition: FMF.h:68
bool noInfs() const
Definition: FMF.h:67
bool all() const
Definition: FMF.h:59
bool allowReciprocal() const
Definition: FMF.h:69
void print(raw_ostream &O) const
Print fast-math flags to O.
Definition: Operator.cpp:246
bool allowReassoc() const
Flag queries.
Definition: FMF.h:65
bool approxFunc() const
Definition: FMF.h:71
bool noNaNs() const
Definition: FMF.h:66
bool allowContract() const
Definition: FMF.h:70
bool collectOffset(const DataLayout &DL, unsigned BitWidth, MapVector< Value *, APInt > &VariableOffsets, APInt &ConstantOffset) const
Collect the offset of this GEP as a map of Values to their associated APInt multipliers,...
Definition: Operator.cpp:188
Type * getSourceElementType() const
Definition: Operator.cpp:60
Type * getResultElementType() const
Definition: Operator.cpp:66
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset, function_ref< bool(Value &, APInt &)> ExternalAnalysis=nullptr) const
Accumulate the constant address offset of this GEP if possible.
Definition: Operator.cpp:97
Align getMaxPreservedAlignment(const DataLayout &DL) const
Compute the maximum alignment that this GEP is garranteed to preserve.
Definition: Operator.cpp:72
unsigned getPointerAddressSpace() const
Method to return the address space of the pointer operand.
Definition: Operator.h:452
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:141
bool hasPoisonGeneratingFlags() const
Return true if this operator has flags which may cause this operator to evaluate to poison despite ha...
Definition: Operator.cpp:21
bool hasPoisonGeneratingFlagsOrMetadata() const
Return true if this operator has poison-generating flags or metadata.
Definition: Operator.cpp:53
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:41
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:622
TypeSize getElementOffset(unsigned Idx) const
Definition: DataLayout.h:651
Class to represent struct types.
Definition: DerivedTypes.h:216
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isScalableTy() const
Return true if this is a type whose size is a known multiple of vscale.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:228
iterator_range< value_op_iterator > operand_values()
Definition: User.h:266
LLVM Value Representation.
Definition: Value.h:74
static constexpr uint64_t MaximumAlignment
Definition: Value.h:807
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329
@ Offset
Definition: DWP.cpp:456
gep_type_iterator gep_type_end(const User *GEP)
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:338
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
gep_type_iterator gep_type_begin(const User *GEP)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39