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->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 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 Align GEPOperator::getMaxPreservedAlignment(const DataLayout &DL) const { 73 /// compute the worse possible offset for every level of the GEP et accumulate 74 /// the minimum alignment into Result. 75 76 Align Result = Align(llvm::Value::MaximumAlignment); 77 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this); 78 GTI != GTE; ++GTI) { 79 uint64_t Offset; 80 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand()); 81 82 if (StructType *STy = GTI.getStructTypeOrNull()) { 83 const StructLayout *SL = DL.getStructLayout(STy); 84 Offset = SL->getElementOffset(OpC->getZExtValue()); 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 = DL.getTypeAllocSize(GTI.getIndexedType()) * ElemCount; 91 } 92 Result = Align(MinAlign(Offset, Result.value())); 93 } 94 return Result; 95 } 96 97 bool GEPOperator::accumulateConstantOffset( 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."); 103 SmallVector<const Value *> Index(llvm::drop_begin(operand_values())); 104 return GEPOperator::accumulateConstantOffset(getSourceElementType(), Index, 105 DL, Offset, ExternalAnalysis); 106 } 107 108 bool GEPOperator::accumulateConstantOffset( 109 Type *SourceType, ArrayRef<const Value *> Index, const DataLayout &DL, 110 APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) { 111 bool UsedExternalAnalysis = false; 112 auto AccumulateOffset = [&](APInt Index, uint64_t Size) -> bool { 113 Index = Index.sextOrTrunc(Offset.getBitWidth()); 114 APInt IndexedSize = APInt(Offset.getBitWidth(), Size); 115 // For array or vector indices, scale the index by the size of the type. 116 if (!UsedExternalAnalysis) { 117 Offset += Index * IndexedSize; 118 } else { 119 // External Analysis can return a result higher/lower than the value 120 // represents. We need to detect overflow/underflow. 121 bool Overflow = false; 122 APInt OffsetPlus = Index.smul_ov(IndexedSize, Overflow); 123 if (Overflow) 124 return false; 125 Offset = Offset.sadd_ov(OffsetPlus, Overflow); 126 if (Overflow) 127 return false; 128 } 129 return true; 130 }; 131 auto begin = generic_gep_type_iterator<decltype(Index.begin())>::begin( 132 SourceType, Index.begin()); 133 auto end = generic_gep_type_iterator<decltype(Index.end())>::end(Index.end()); 134 for (auto GTI = begin, GTE = end; GTI != GTE; ++GTI) { 135 // Scalable vectors are multiplied by a runtime constant. 136 bool ScalableType = GTI.getIndexedType()->isScalableTy(); 137 138 Value *V = GTI.getOperand(); 139 StructType *STy = GTI.getStructTypeOrNull(); 140 // Handle ConstantInt if possible. 141 if (auto ConstOffset = dyn_cast<ConstantInt>(V)) { 142 if (ConstOffset->isZero()) 143 continue; 144 // if the type is scalable and the constant is not zero (vscale * n * 0 = 145 // 0) bailout. 146 if (ScalableType) 147 return false; 148 // Handle a struct index, which adds its field offset to the pointer. 149 if (STy) { 150 unsigned ElementIdx = ConstOffset->getZExtValue(); 151 const StructLayout *SL = DL.getStructLayout(STy); 152 // Element offset is in bytes. 153 if (!AccumulateOffset( 154 APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)), 155 1)) 156 return false; 157 continue; 158 } 159 if (!AccumulateOffset(ConstOffset->getValue(), 160 DL.getTypeAllocSize(GTI.getIndexedType()))) 161 return false; 162 continue; 163 } 164 165 // The operand is not constant, check if an external analysis was provided. 166 // External analsis is not applicable to a struct type. 167 if (!ExternalAnalysis || STy || ScalableType) 168 return false; 169 APInt AnalysisIndex; 170 if (!ExternalAnalysis(*V, AnalysisIndex)) 171 return false; 172 UsedExternalAnalysis = true; 173 if (!AccumulateOffset(AnalysisIndex, 174 DL.getTypeAllocSize(GTI.getIndexedType()))) 175 return false; 176 } 177 return true; 178 } 179 180 bool GEPOperator::collectOffset( 181 const DataLayout &DL, unsigned BitWidth, 182 MapVector<Value *, APInt> &VariableOffsets, 183 APInt &ConstantOffset) const { 184 assert(BitWidth == DL.getIndexSizeInBits(getPointerAddressSpace()) && 185 "The offset bit width does not match DL specification."); 186 187 auto CollectConstantOffset = [&](APInt Index, uint64_t Size) { 188 Index = Index.sextOrTrunc(BitWidth); 189 APInt IndexedSize = APInt(BitWidth, Size); 190 ConstantOffset += Index * IndexedSize; 191 }; 192 193 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this); 194 GTI != GTE; ++GTI) { 195 // Scalable vectors are multiplied by a runtime constant. 196 bool ScalableType = GTI.getIndexedType()->isScalableTy(); 197 198 Value *V = GTI.getOperand(); 199 StructType *STy = GTI.getStructTypeOrNull(); 200 // Handle ConstantInt if possible. 201 if (auto ConstOffset = dyn_cast<ConstantInt>(V)) { 202 if (ConstOffset->isZero()) 203 continue; 204 // If the type is scalable and the constant is not zero (vscale * n * 0 = 205 // 0) bailout. 206 // TODO: If the runtime value is accessible at any point before DWARF 207 // emission, then we could potentially keep a forward reference to it 208 // in the debug value to be filled in later. 209 if (ScalableType) 210 return false; 211 // Handle a struct index, which adds its field offset to the pointer. 212 if (STy) { 213 unsigned ElementIdx = ConstOffset->getZExtValue(); 214 const StructLayout *SL = DL.getStructLayout(STy); 215 // Element offset is in bytes. 216 CollectConstantOffset(APInt(BitWidth, SL->getElementOffset(ElementIdx)), 217 1); 218 continue; 219 } 220 CollectConstantOffset(ConstOffset->getValue(), 221 DL.getTypeAllocSize(GTI.getIndexedType())); 222 continue; 223 } 224 225 if (STy || ScalableType) 226 return false; 227 APInt IndexedSize = 228 APInt(BitWidth, DL.getTypeAllocSize(GTI.getIndexedType())); 229 // Insert an initial offset of 0 for V iff none exists already, then 230 // increment the offset by IndexedSize. 231 if (!IndexedSize.isZero()) { 232 VariableOffsets.insert({V, APInt(BitWidth, 0)}); 233 VariableOffsets[V] += IndexedSize; 234 } 235 } 236 return true; 237 } 238 239 void FastMathFlags::print(raw_ostream &O) const { 240 if (all()) 241 O << " fast"; 242 else { 243 if (allowReassoc()) 244 O << " reassoc"; 245 if (noNaNs()) 246 O << " nnan"; 247 if (noInfs()) 248 O << " ninf"; 249 if (noSignedZeros()) 250 O << " nsz"; 251 if (allowReciprocal()) 252 O << " arcp"; 253 if (allowContract()) 254 O << " contract"; 255 if (approxFunc()) 256 O << " afn"; 257 } 258 } 259 } // namespace llvm 260