Skip to content

Commit 6f51431

Browse files
committed
[ConstantFold] Support ptrtoaddr in global alignment fold
Treat it the same way as ptrtoint.
1 parent f345d9b commit 6f51431

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,8 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
741741
assert(!CI2->isZero() && "And zero handled above");
742742
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
743743
// If and'ing the address of a global with a constant, fold it.
744-
if (CE1->getOpcode() == Instruction::PtrToInt &&
744+
if ((CE1->getOpcode() == Instruction::PtrToInt ||
745+
CE1->getOpcode() == Instruction::PtrToAddr) &&
745746
isa<GlobalValue>(CE1->getOperand(0))) {
746747
GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
747748

llvm/unittests/IR/ConstantsTest.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,13 +564,17 @@ TEST(ConstantsTest, FoldGlobalVariablePtr) {
564564

565565
Global->setAlignment(Align(4));
566566

567-
ConstantInt *TheConstant(ConstantInt::get(IntType, 2));
567+
ConstantInt *TheConstant = ConstantInt::get(IntType, 2);
568568

569-
Constant *TheConstantExpr(ConstantExpr::getPtrToInt(Global.get(), IntType));
569+
Constant *PtrToInt = ConstantExpr::getPtrToInt(Global.get(), IntType);
570+
ASSERT_TRUE(
571+
ConstantFoldBinaryInstruction(Instruction::And, PtrToInt, TheConstant)
572+
->isNullValue());
570573

571-
ASSERT_TRUE(ConstantFoldBinaryInstruction(Instruction::And, TheConstantExpr,
572-
TheConstant)
573-
->isNullValue());
574+
Constant *PtrToAddr = ConstantExpr::getPtrToAddr(Global.get(), IntType);
575+
ASSERT_TRUE(
576+
ConstantFoldBinaryInstruction(Instruction::And, PtrToAddr, TheConstant)
577+
->isNullValue());
574578
}
575579

576580
// Check that containsUndefOrPoisonElement and containsPoisonElement is working

0 commit comments

Comments
 (0)