1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 20:23:11 +01:00

[Local] Clean up enforceKnownAlignment() (NFC)

I want to export this function, and the current API was a bit
weird: It took an additional Alignment argument that didn't really
have anything to do with what the function does. Drop it, and
perform a max at the callsite.

Also rename it to tryEnforceAlignment().
This commit is contained in:
Nikita Popov 2020-09-19 22:26:53 +02:00
parent 5a88990a71
commit 2428bdcb45

View File

@ -1262,54 +1262,53 @@ bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
return EliminateDuplicatePHINodesSetBasedImpl(BB);
}
/// enforceKnownAlignment - If the specified pointer points to an object that
/// we control, modify the object's alignment to PrefAlign. This isn't
/// often possible though. If alignment is important, a more reliable approach
/// is to simply align all global variables and allocation instructions to
/// their preferred alignment from the beginning.
static Align enforceKnownAlignment(Value *V, Align Alignment, Align PrefAlign,
const DataLayout &DL) {
assert(PrefAlign > Alignment);
/// If the specified pointer points to an object that we control, try to modify
/// the object's alignment to PrefAlign. Returns a minimum known alignment of
/// the value after the operation, which may be lower than PrefAlign.
///
/// Increating value alignment isn't often possible though. If alignment is
/// important, a more reliable approach is to simply align all global variables
/// and allocation instructions to their preferred alignment from the beginning.
static Align tryEnforceAlignment(Value *V, Align PrefAlign,
const DataLayout &DL) {
V = V->stripPointerCasts();
if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
// TODO: ideally, computeKnownBits ought to have used
// AllocaInst::getAlignment() in its computation already, making
// the below max redundant. But, as it turns out,
// stripPointerCasts recurses through infinite layers of bitcasts,
// while computeKnownBits is not allowed to traverse more than 6
// levels.
Alignment = std::max(AI->getAlign(), Alignment);
if (PrefAlign <= Alignment)
return Alignment;
// TODO: Ideally, this function would not be called if PrefAlign is smaller
// than the current alignment, as the known bits calculation should have
// already taken it into account. However, this is not always the case,
// as computeKnownBits() has a depth limit, while stripPointerCasts()
// doesn't.
Align CurrentAlign = AI->getAlign();
if (PrefAlign <= CurrentAlign)
return CurrentAlign;
// If the preferred alignment is greater than the natural stack alignment
// then don't round up. This avoids dynamic stack realignment.
if (DL.exceedsNaturalStackAlignment(PrefAlign))
return Alignment;
return CurrentAlign;
AI->setAlignment(PrefAlign);
return PrefAlign;
}
if (auto *GO = dyn_cast<GlobalObject>(V)) {
// TODO: as above, this shouldn't be necessary.
Alignment = max(GO->getAlign(), Alignment);
if (PrefAlign <= Alignment)
return Alignment;
Align CurrentAlign = GO->getPointerAlignment(DL);
if (PrefAlign <= CurrentAlign)
return CurrentAlign;
// If there is a large requested alignment and we can, bump up the alignment
// of the global. If the memory we set aside for the global may not be the
// memory used by the final program then it is impossible for us to reliably
// enforce the preferred alignment.
if (!GO->canIncreaseAlignment())
return Alignment;
return CurrentAlign;
GO->setAlignment(PrefAlign);
return PrefAlign;
}
return Alignment;
return Align(1);
}
Align llvm::getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign,
@ -1331,7 +1330,7 @@ Align llvm::getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign,
Align Alignment = Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));
if (PrefAlign && *PrefAlign > Alignment)
Alignment = enforceKnownAlignment(V, Alignment, *PrefAlign, DL);
Alignment = std::max(Alignment, tryEnforceAlignment(V, *PrefAlign, DL));
// We don't need to make any adjustment.
return Alignment;