aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2021-06-22 12:00:42 +0200
committerNikita Popov <nikita.ppv@gmail.com>2021-06-22 15:51:00 +0200
commite638a290f7d0bb85dbf81ba34eaaeef8c8d1b42d (patch)
treee84f082754ab2bea951c9dc8d7d5dabe1f216ad9 /llvm/lib/Analysis/ConstantFolding.cpp
parent87bdde4962ea926e81a3534119ac8f02901b75b9 (diff)
downloadllvm-e638a290f7d0bb85dbf81ba34eaaeef8c8d1b42d.zip
llvm-e638a290f7d0bb85dbf81ba34eaaeef8c8d1b42d.tar.gz
llvm-e638a290f7d0bb85dbf81ba34eaaeef8c8d1b42d.tar.bz2
[ConstantFold] Delay fetching pointer element type
Don't do this while stipping pointer casts, instead fetch it at the end. This improves compatibility with opaque pointers for the case where the base object is not opaque.
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp14
1 files changed, 6 insertions, 8 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index ceed375..1c6709c 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -867,8 +867,7 @@ Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
}
/// Strip the pointer casts, but preserve the address space information.
-Constant *StripPtrCastKeepAS(Constant *Ptr, Type *&ElemTy,
- bool ForLoadOperand) {
+Constant *StripPtrCastKeepAS(Constant *Ptr, bool ForLoadOperand) {
assert(Ptr->getType()->isPointerTy() && "Not a pointer type");
auto *OldPtrTy = cast<PointerType>(Ptr->getType());
Ptr = cast<Constant>(Ptr->stripPointerCasts());
@@ -881,8 +880,6 @@ Constant *StripPtrCastKeepAS(Constant *Ptr, Type *&ElemTy,
auto *NewPtrTy = cast<PointerType>(Ptr->getType());
- ElemTy = NewPtrTy->getPointerElementType();
-
// Preserve the address space number of the pointer.
if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
Ptr = ConstantExpr::getPointerCast(
@@ -942,7 +939,7 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
DL.getIndexedOffsetInType(
SrcElemTy,
makeArrayRef((Value * const *)Ops.data() + 1, Ops.size() - 1)));
- Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy, ForLoadOperand);
+ Ptr = StripPtrCastKeepAS(Ptr, ForLoadOperand);
// If this is a GEP of a GEP, fold it all into a single GEP.
while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
@@ -964,7 +961,7 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
Ptr = cast<Constant>(GEP->getOperand(0));
SrcElemTy = GEP->getSourceElementType();
Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps));
- Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy, ForLoadOperand);
+ Ptr = StripPtrCastKeepAS(Ptr, ForLoadOperand);
}
// If the base value for this address is a literal integer value, fold the
@@ -988,8 +985,9 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
// we eliminate over-indexing of the notional static type array bounds.
// This makes it easy to determine if the getelementptr is "inbounds".
// Also, this helps GlobalOpt do SROA on GlobalVariables.
- Type *Ty = PTy;
SmallVector<Constant *, 32> NewIdxs;
+ Type *Ty = PTy;
+ SrcElemTy = PTy->getElementType();
do {
if (!Ty->isStructTy()) {
@@ -1074,7 +1072,7 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
// If we ended up indexing a member with a type that doesn't match
// the type of what the original indices indexed, add a cast.
- if (Ty != ResElemTy)
+ if (C->getType() != ResTy)
C = FoldBitCast(C, ResTy, DL);
return C;