LLVM 18.0.0git
Local.cpp
Go to the documentation of this file.
1//===- Local.cpp - Functions to perform local transformations -------------===//
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 family of functions perform various local transformations to the
10// program.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/Twine.h"
16#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/IRBuilder.h"
19
20using namespace llvm;
21
23 User *GEP, bool NoAssumptions) {
24 GEPOperator *GEPOp = cast<GEPOperator>(GEP);
25 Type *IntIdxTy = DL.getIndexType(GEP->getType());
26 Value *Result = nullptr;
27
28 // If the GEP is inbounds, we know that none of the addressing operations will
29 // overflow in a signed sense.
30 bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
31
32 // Build a mask for high order bits.
33 unsigned IntPtrWidth = IntIdxTy->getScalarType()->getIntegerBitWidth();
34 uint64_t PtrSizeMask =
35 std::numeric_limits<uint64_t>::max() >> (64 - IntPtrWidth);
36
38 for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
39 ++i, ++GTI) {
40 Value *Op = *i;
41 uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
43 if (Constant *OpC = dyn_cast<Constant>(Op)) {
44 if (OpC->isZeroValue())
45 continue;
46
47 // Handle a struct index, which adds its field offset to the pointer.
48 if (StructType *STy = GTI.getStructTypeOrNull()) {
49 uint64_t OpValue = OpC->getUniqueInteger().getZExtValue();
50 Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
51 if (!Size)
52 continue;
53
54 Offset = ConstantInt::get(IntIdxTy, Size);
55 } else {
56 // Splat the constant if needed.
57 if (IntIdxTy->isVectorTy() && !OpC->getType()->isVectorTy())
59 cast<VectorType>(IntIdxTy)->getElementCount(), OpC);
60
61 Constant *Scale = ConstantInt::get(IntIdxTy, Size);
62 Constant *OC =
63 ConstantExpr::getIntegerCast(OpC, IntIdxTy, true /*SExt*/);
64 Offset =
65 ConstantExpr::getMul(OC, Scale, false /*NUW*/, isInBounds /*NSW*/);
66 }
67 } else {
68 // Splat the index if needed.
69 if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy())
70 Op = Builder->CreateVectorSplat(
71 cast<FixedVectorType>(IntIdxTy)->getNumElements(), Op);
72
73 // Convert to correct type.
74 if (Op->getType() != IntIdxTy)
75 Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c");
76 if (Size != 1) {
77 // We'll let instcombine(mul) convert this to a shl if possible.
78 Op = Builder->CreateMul(Op, ConstantInt::get(IntIdxTy, Size),
79 GEP->getName() + ".idx", false /*NUW*/,
80 isInBounds /*NSW*/);
81 }
82 Offset = Op;
83 }
84
85 if (Result)
86 Result = Builder->CreateAdd(Result, Offset, GEP->getName() + ".offs",
87 false /*NUW*/, isInBounds /*NSW*/);
88 else
89 Result = Offset;
90 }
91 return Result ? Result : Constant::getNullValue(IntIdxTy);
92}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
assume Assume Builder
uint64_t Size
Hexagon Common GEP
static Constant * getIntegerCast(Constant *C, Type *Ty, bool IsSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:2051
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2580
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:888
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1385
This is an important base class in LLVM.
Definition: Constant.h:41
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:356
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition: Operator.h:389
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:94
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
unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:348
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:440
Value * emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
Definition: Local.cpp:22
DWARFExpression::Operation Op
gep_type_iterator gep_type_begin(const User *GEP)