Skip to content

Commit 4a873d5

Browse files
authored
[IR] Don't create ptrtoint expression to determine alignment (NFCI) (#161364)
We try to determine the alignment of a constant by creating a ptrtoint expression and seeing if it folds. I believe the only case this can actually handle is where the constant is an inttoptr expression. Handle that directly instead of going through another ptrtoint expression. I ran into this while trying to clean up our isEliminableCastPair() mess, which is going to disable ptrtoint(inttoptr) folding without DataLayout, breaking this code.
1 parent cf50bbf commit 4a873d5

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

llvm/lib/IR/Value.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,14 +1000,12 @@ Align Value::getPointerAlignment(const DataLayout &DL) const {
10001000
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
10011001
return Align(CI->getLimitedValue());
10021002
}
1003-
} else if (auto *CstPtr = dyn_cast<Constant>(this)) {
1004-
// Strip pointer casts to avoid creating unnecessary ptrtoint expression
1005-
// if the only "reduction" is combining a bitcast + ptrtoint.
1006-
CstPtr = CstPtr->stripPointerCasts();
1007-
if (auto *CstInt = dyn_cast_or_null<ConstantInt>(ConstantExpr::getPtrToInt(
1008-
const_cast<Constant *>(CstPtr), DL.getIntPtrType(getType()),
1009-
/*OnlyIfReduced=*/true))) {
1010-
size_t TrailingZeros = CstInt->getValue().countr_zero();
1003+
} else if (auto *CE = dyn_cast<ConstantExpr>(this)) {
1004+
// Determine the alignment of inttoptr(C).
1005+
if (CE->getOpcode() == Instruction::IntToPtr &&
1006+
isa<ConstantInt>(CE->getOperand(0))) {
1007+
ConstantInt *IntPtr = cast<ConstantInt>(CE->getOperand(0));
1008+
size_t TrailingZeros = IntPtr->getValue().countr_zero();
10111009
// While the actual alignment may be large, elsewhere we have
10121010
// an arbitrary upper alignmet limit, so let's clamp to it.
10131011
return Align(TrailingZeros < Value::MaxAlignmentExponent

0 commit comments

Comments
 (0)