Skip to content

Commit

Permalink
[ConstantFold] Check for constant global earlier (NFC)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Feb 15, 2023
1 parent 8b50048 commit 07916ce
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 07916ce

Please sign in to comment.