1 //===-- Operator.cpp - Implement the LLVM operators -----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the non-inline methods for the LLVM Operator classes.
11 //===----------------------------------------------------------------------===//
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 #include "llvm/IR/Type.h"
19 #include "ConstantsContext.h"
22 Type *GEPOperator::getSourceElementType() const {
23 if (auto *I = dyn_cast<GetElementPtrInst>(this))
24 return I->getSourceElementType();
25 return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
28 Type *GEPOperator::getResultElementType() const {
29 if (auto *I = dyn_cast<GetElementPtrInst>(this))
30 return I->getResultElementType();
31 return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
34 Align GEPOperator::getMaxPreservedAlignment(const DataLayout &DL) const {
35 /// compute the worse possible offset for every level of the GEP et accumulate
36 /// the minimum alignment into Result.
38 Align Result = Align(llvm::Value::MaximumAlignment);
39 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
42 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
44 if (StructType *STy = GTI.getStructTypeOrNull()) {
45 const StructLayout *SL = DL.getStructLayout(STy);
46 Offset = SL->getElementOffset(OpC->getZExtValue());
48 assert(GTI.isSequential() && "should be sequencial");
49 /// If the index isn't know we take 1 because it is the index that will
50 /// give the worse alignment of the offset.
51 int64_t ElemCount = 1;
53 ElemCount = OpC->getZExtValue();
54 Offset = DL.getTypeAllocSize(GTI.getIndexedType()) * ElemCount;
56 Result = Align(MinAlign(Offset, Result.value()));
61 bool GEPOperator::accumulateConstantOffset(
62 const DataLayout &DL, APInt &Offset,
63 function_ref<bool(Value &, APInt &)> ExternalAnalysis) const {
64 assert(Offset.getBitWidth() ==
65 DL.getIndexSizeInBits(getPointerAddressSpace()) &&
66 "The offset bit width does not match DL specification.");
68 bool UsedExternalAnalysis = false;
69 auto AccumulateOffset = [&](APInt Index, uint64_t Size) -> bool {
70 Index = Index.sextOrTrunc(Offset.getBitWidth());
71 APInt IndexedSize = APInt(Offset.getBitWidth(), Size);
72 // For array or vector indices, scale the index by the size of the type.
73 if (!UsedExternalAnalysis) {
74 Offset += Index * IndexedSize;
76 // External Analysis can return a result higher/lower than the value
77 // represents. We need to detect overflow/underflow.
78 bool Overflow = false;
79 APInt OffsetPlus = Index.smul_ov(IndexedSize, Overflow);
82 Offset = Offset.sadd_ov(OffsetPlus, Overflow);
89 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
91 // Scalable vectors are multiplied by a runtime constant.
92 bool ScalableType = false;
93 if (isa<ScalableVectorType>(GTI.getIndexedType()))
96 Value *V = GTI.getOperand();
97 StructType *STy = GTI.getStructTypeOrNull();
98 // Handle ConstantInt if possible.
99 if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
100 if (ConstOffset->isZero())
102 // if the type is scalable and the constant is not zero (vscale * n * 0 =
106 // Handle a struct index, which adds its field offset to the pointer.
108 unsigned ElementIdx = ConstOffset->getZExtValue();
109 const StructLayout *SL = DL.getStructLayout(STy);
110 // Element offset is in bytes.
111 if (!AccumulateOffset(
112 APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
117 if (!AccumulateOffset(ConstOffset->getValue(),
118 DL.getTypeAllocSize(GTI.getIndexedType())))
123 // The operand is not constant, check if an external analysis was provided.
124 // External analsis is not applicable to a struct type.
125 if (!ExternalAnalysis || STy || ScalableType)
128 if (!ExternalAnalysis(*V, AnalysisIndex))
130 UsedExternalAnalysis = true;
131 if (!AccumulateOffset(AnalysisIndex,
132 DL.getTypeAllocSize(GTI.getIndexedType())))