LLVM 17.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::GetElementPtr: {
36 auto *GEP = cast<GEPOperator>(this);
37 // Note: inrange exists on constexpr only
38 return GEP->isInBounds() || GEP->getInRangeIndex() != std::nullopt;
39 }
40 default:
41 if (const auto *FP = dyn_cast<FPMathOperator>(this))
42 return FP->hasNoNaNs() || FP->hasNoInfs();
43 return false;
44 }
45}
46
49 return true;
50 auto *I = dyn_cast<Instruction>(this);
51 return I && I->hasPoisonGeneratingMetadata();
52}
53
55 if (auto *I = dyn_cast<GetElementPtrInst>(this))
56 return I->getSourceElementType();
57 return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
58}
59
61 if (auto *I = dyn_cast<GetElementPtrInst>(this))
62 return I->getResultElementType();
63 return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
64}
65
67 /// compute the worse possible offset for every level of the GEP et accumulate
68 /// the minimum alignment into Result.
69
71 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
72 GTI != GTE; ++GTI) {
74 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
75
76 if (StructType *STy = GTI.getStructTypeOrNull()) {
77 const StructLayout *SL = DL.getStructLayout(STy);
79 } else {
80 assert(GTI.isSequential() && "should be sequencial");
81 /// If the index isn't known, we take 1 because it is the index that will
82 /// give the worse alignment of the offset.
83 const uint64_t ElemCount = OpC ? OpC->getZExtValue() : 1;
84 Offset = DL.getTypeAllocSize(GTI.getIndexedType()) * ElemCount;
85 }
86 Result = Align(MinAlign(Offset, Result.value()));
87 }
88 return Result;
89}
90
92 const DataLayout &DL, APInt &Offset,
93 function_ref<bool(Value &, APInt &)> ExternalAnalysis) const {
94 assert(Offset.getBitWidth() ==
95 DL.getIndexSizeInBits(getPointerAddressSpace()) &&
96 "The offset bit width does not match DL specification.");
99 DL, Offset, ExternalAnalysis);
100}
101
103 Type *SourceType, ArrayRef<const Value *> Index, const DataLayout &DL,
104 APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) {
105 bool UsedExternalAnalysis = false;
106 auto AccumulateOffset = [&](APInt Index, uint64_t Size) -> bool {
107 Index = Index.sextOrTrunc(Offset.getBitWidth());
108 APInt IndexedSize = APInt(Offset.getBitWidth(), Size);
109 // For array or vector indices, scale the index by the size of the type.
110 if (!UsedExternalAnalysis) {
111 Offset += Index * IndexedSize;
112 } else {
113 // External Analysis can return a result higher/lower than the value
114 // represents. We need to detect overflow/underflow.
115 bool Overflow = false;
116 APInt OffsetPlus = Index.smul_ov(IndexedSize, Overflow);
117 if (Overflow)
118 return false;
119 Offset = Offset.sadd_ov(OffsetPlus, Overflow);
120 if (Overflow)
121 return false;
122 }
123 return true;
124 };
125 auto begin = generic_gep_type_iterator<decltype(Index.begin())>::begin(
126 SourceType, Index.begin());
127 auto end = generic_gep_type_iterator<decltype(Index.end())>::end(Index.end());
128 for (auto GTI = begin, GTE = end; GTI != GTE; ++GTI) {
129 // Scalable vectors are multiplied by a runtime constant.
130 bool ScalableType = false;
131 if (isa<ScalableVectorType>(GTI.getIndexedType()))
132 ScalableType = true;
133
134 Value *V = GTI.getOperand();
135 StructType *STy = GTI.getStructTypeOrNull();
136 // Handle ConstantInt if possible.
137 if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
138 if (ConstOffset->isZero())
139 continue;
140 // if the type is scalable and the constant is not zero (vscale * n * 0 =
141 // 0) bailout.
142 if (ScalableType)
143 return false;
144 // Handle a struct index, which adds its field offset to the pointer.
145 if (STy) {
146 unsigned ElementIdx = ConstOffset->getZExtValue();
147 const StructLayout *SL = DL.getStructLayout(STy);
148 // Element offset is in bytes.
149 if (!AccumulateOffset(
150 APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
151 1))
152 return false;
153 continue;
154 }
155 if (!AccumulateOffset(ConstOffset->getValue(),
156 DL.getTypeAllocSize(GTI.getIndexedType())))
157 return false;
158 continue;
159 }
160
161 // The operand is not constant, check if an external analysis was provided.
162 // External analsis is not applicable to a struct type.
163 if (!ExternalAnalysis || STy || ScalableType)
164 return false;
165 APInt AnalysisIndex;
166 if (!ExternalAnalysis(*V, AnalysisIndex))
167 return false;
168 UsedExternalAnalysis = true;
169 if (!AccumulateOffset(AnalysisIndex,
170 DL.getTypeAllocSize(GTI.getIndexedType())))
171 return false;
172 }
173 return true;
174}
175
177 const DataLayout &DL, unsigned BitWidth,
178 MapVector<Value *, APInt> &VariableOffsets,
179 APInt &ConstantOffset) const {
180 assert(BitWidth == DL.getIndexSizeInBits(getPointerAddressSpace()) &&
181 "The offset bit width does not match DL specification.");
182
183 auto CollectConstantOffset = [&](APInt Index, uint64_t Size) {
184 Index = Index.sextOrTrunc(BitWidth);
185 APInt IndexedSize = APInt(BitWidth, Size);
186 ConstantOffset += Index * IndexedSize;
187 };
188
189 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
190 GTI != GTE; ++GTI) {
191 // Scalable vectors are multiplied by a runtime constant.
192 bool ScalableType = isa<ScalableVectorType>(GTI.getIndexedType());
193
194 Value *V = GTI.getOperand();
195 StructType *STy = GTI.getStructTypeOrNull();
196 // Handle ConstantInt if possible.
197 if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
198 if (ConstOffset->isZero())
199 continue;
200 // If the type is scalable and the constant is not zero (vscale * n * 0 =
201 // 0) bailout.
202 // TODO: If the runtime value is accessible at any point before DWARF
203 // emission, then we could potentially keep a forward reference to it
204 // in the debug value to be filled in later.
205 if (ScalableType)
206 return false;
207 // Handle a struct index, which adds its field offset to the pointer.
208 if (STy) {
209 unsigned ElementIdx = ConstOffset->getZExtValue();
210 const StructLayout *SL = DL.getStructLayout(STy);
211 // Element offset is in bytes.
212 CollectConstantOffset(APInt(BitWidth, SL->getElementOffset(ElementIdx)),
213 1);
214 continue;
215 }
216 CollectConstantOffset(ConstOffset->getValue(),
217 DL.getTypeAllocSize(GTI.getIndexedType()));
218 continue;
219 }
220
221 if (STy || ScalableType)
222 return false;
223 APInt IndexedSize =
224 APInt(BitWidth, DL.getTypeAllocSize(GTI.getIndexedType()));
225 // Insert an initial offset of 0 for V iff none exists already, then
226 // increment the offset by IndexedSize.
227 if (!IndexedSize.isZero()) {
228 VariableOffsets.insert({V, APInt(BitWidth, 0)});
229 VariableOffsets[V] += IndexedSize;
230 }
231 }
232 return true;
233}
234
236 if (all())
237 O << " fast";
238 else {
239 if (allowReassoc())
240 O << " reassoc";
241 if (noNaNs())
242 O << " nnan";
243 if (noInfs())
244 O << " ninf";
245 if (noSignedZeros())
246 O << " nsz";
247 if (allowReciprocal())
248 O << " arcp";
249 if (allowContract())
250 O << " contract";
251 if (approxFunc())
252 O << " afn";
253 }
254}
255} // 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:75
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:366
APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1958
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:78
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:145
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:69
bool noInfs() const
Definition: FMF.h:68
bool all() const
Definition: FMF.h:60
bool allowReciprocal() const
Definition: FMF.h:70
void print(raw_ostream &O) const
Print fast-math flags to O.
Definition: Operator.cpp:235
bool allowReassoc() const
Flag queries.
Definition: FMF.h:66
bool approxFunc() const
Definition: FMF.h:72
bool noNaNs() const
Definition: FMF.h:67
bool allowContract() const
Definition: FMF.h:71
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:176
Type * getSourceElementType() const
Definition: Operator.cpp:54
Type * getResultElementType() const
Definition: Operator.cpp:60
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:91
Align getMaxPreservedAlignment(const DataLayout &DL) const
Compute the maximum alignment that this GEP is garranteed to preserve.
Definition: Operator.cpp:66
unsigned getPointerAddressSpace() const
Method to return the address space of the pointer operand.
Definition: Operator.h:439
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:37
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:118
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:47
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:1200
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:618
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:648
Class to represent struct types.
Definition: DerivedTypes.h:213
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
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:790
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:413
@ Offset
Definition: DWP.cpp:406
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:439
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:184
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