[llvm] 07916ce - [ConstantFold] Check for constant global earlier (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 15 06:17:14 PST 2023
Author: Nikita Popov
Date: 2023-02-15T15:17:05+01:00
New Revision: 07916cea2e7e1e3360c7c0bf1fc1bd3c9551a66a
URL: https://github.com/llvm/llvm-project/commit/07916cea2e7e1e3360c7c0bf1fc1bd3c9551a66a
DIFF: https://github.com/llvm/llvm-project/commit/07916cea2e7e1e3360c7c0bf1fc1bd3c9551a66a.diff
LOG: [ConstantFold] Check for constant global earlier (NFC)
Check that the underlying object is a constant global with
definitive initializer upfront, so we can skip the more expensive
offset calculation logic if we can't perform the fold anyway.
Added:
Modified:
llvm/lib/Analysis/ConstantFolding.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 6a2d6ba767e7a..a43402d2b62ce 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -729,26 +729,23 @@ Constant *llvm::ConstantFoldLoadFromConst(Constant *C, Type *Ty,
Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
APInt Offset,
const DataLayout &DL) {
+ // We can only fold loads from constant globals with a definitive initializer.
+ // Check this upfront, to skip expensive offset calculations.
+ auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(C));
+ if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
+ return nullptr;
+
C = cast<Constant>(C->stripAndAccumulateConstantOffsets(
DL, Offset, /* AllowNonInbounds */ true));
- if (auto *GV = dyn_cast<GlobalVariable>(C))
- if (GV->isConstant() && GV->hasDefinitiveInitializer())
- if (Constant *Result = ConstantFoldLoadFromConst(GV->getInitializer(), Ty,
- Offset, DL))
- return Result;
+ if (C == GV)
+ if (Constant *Result = ConstantFoldLoadFromConst(GV->getInitializer(), Ty,
+ Offset, DL))
+ return Result;
// If this load comes from anywhere in a uniform constant global, the value
// is always the same, regardless of the loaded offset.
- if (auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(C))) {
- if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
- if (Constant *Res =
- ConstantFoldLoadFromUniformValue(GV->getInitializer(), Ty))
- return Res;
- }
- }
-
- return nullptr;
+ return ConstantFoldLoadFromUniformValue(GV->getInitializer(), Ty);
}
Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
More information about the llvm-commits
mailing list