LLVM 20.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 // nusw implies nsw for the offset arithmetic.
29 bool NSW = GEPOp->hasNoUnsignedSignedWrap() && !NoAssumptions;
30 bool NUW = GEPOp->hasNoUnsignedWrap() && !NoAssumptions;
31 auto AddOffset = [&](Value *Offset) {
32 if (Result)
33 Result = Builder->CreateAdd(Result, Offset, GEP->getName() + ".offs",
34 NUW, NSW);
35 else
36 Result = Offset;
37 };
38
40 for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
41 ++i, ++GTI) {
42 Value *Op = *i;
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 uint64_t Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
51 if (!Size)
52 continue;
53
54 AddOffset(ConstantInt::get(IntIdxTy, Size));
55 continue;
56 }
57 }
58
59 // Splat the index if needed.
60 if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy())
61 Op = Builder->CreateVectorSplat(
62 cast<VectorType>(IntIdxTy)->getElementCount(), Op);
63
64 // Convert to correct type.
65 if (Op->getType() != IntIdxTy)
66 Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c");
68 if (TSize != TypeSize::getFixed(1)) {
69 Value *Scale = Builder->CreateTypeSize(IntIdxTy->getScalarType(), TSize);
70 if (IntIdxTy->isVectorTy())
71 Scale = Builder->CreateVectorSplat(
72 cast<VectorType>(IntIdxTy)->getElementCount(), Scale);
73 // We'll let instcombine(mul) convert this to a shl if possible.
74 Op = Builder->CreateMul(Op, Scale, GEP->getName() + ".idx", NUW, NSW);
75 }
76 AddOffset(Op);
77 }
78 return Result ? Result : Constant::getNullValue(IntIdxTy);
79}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
uint64_t Size
Hexagon Common GEP
This is an important base class in LLVM.
Definition: Constant.h:42
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
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:63
bool hasNoUnsignedSignedWrap() const
Definition: Operator.h:437
bool hasNoUnsignedWrap() const
Definition: Operator.h:441
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:91
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1152
Value * CreateTypeSize(Type *DstType, TypeSize Size)
Create an expression which evaluates to the number of units in Size at runtime.
Definition: IRBuilder.cpp:103
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1350
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:2227
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1384
Class to represent struct types.
Definition: DerivedTypes.h:218
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:345
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:270
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
TypeSize getSequentialElementStride(const DataLayout &DL) const
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
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
gep_type_iterator gep_type_begin(const User *GEP)