Skip to content

Commit 925d029

Browse files
committed
Introduce ConstantFoldCastOperand function and migrate some callers of ConstantFoldInstOperands to use it. NFC.
Summary: Although this is a slight cleanup on its own, the main motivation is to refactor the constant folding API to ease migration to opaque pointers. This will be follow-up work. Reviewers: eddyb Subscribers: zzheng, dblaikie, llvm-commits Differential Revision: http://reviews.llvm.org/D16380 llvm-svn: 258390
1 parent a61ca37 commit 925d029

File tree

4 files changed

+78
-61
lines changed

4 files changed

+78
-61
lines changed

llvm/include/llvm/Analysis/ConstantFolding.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS,
7171
Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
7272
Constant *RHS, const DataLayout &DL);
7373

74+
/// \brief Attempt to constant fold a cast with the specified operand. If it
75+
/// fails, it returns a constant expression of the specified operand.
76+
Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
77+
const DataLayout &DL);
78+
7479
/// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
7580
/// instruction with the specified operands and indices. The constant result is
7681
/// returned if successful; if not, null is returned.

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,9 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
10131013
if (Instruction::isBinaryOp(Opcode))
10141014
return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL);
10151015

1016+
if (Instruction::isCast(Opcode))
1017+
return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL);
1018+
10161019
switch (Opcode) {
10171020
default: return nullptr;
10181021
case Instruction::ICmp:
@@ -1022,58 +1025,6 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
10221025
if (canConstantFoldCallTo(F))
10231026
return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI);
10241027
return nullptr;
1025-
case Instruction::PtrToInt:
1026-
// If the input is a inttoptr, eliminate the pair. This requires knowing
1027-
// the width of a pointer, so it can't be done in ConstantExpr::getCast.
1028-
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
1029-
if (CE->getOpcode() == Instruction::IntToPtr) {
1030-
Constant *Input = CE->getOperand(0);
1031-
unsigned InWidth = Input->getType()->getScalarSizeInBits();
1032-
unsigned PtrWidth = DL.getPointerTypeSizeInBits(CE->getType());
1033-
if (PtrWidth < InWidth) {
1034-
Constant *Mask =
1035-
ConstantInt::get(CE->getContext(),
1036-
APInt::getLowBitsSet(InWidth, PtrWidth));
1037-
Input = ConstantExpr::getAnd(Input, Mask);
1038-
}
1039-
// Do a zext or trunc to get to the dest size.
1040-
return ConstantExpr::getIntegerCast(Input, DestTy, false);
1041-
}
1042-
}
1043-
return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
1044-
case Instruction::IntToPtr:
1045-
// If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
1046-
// the int size is >= the ptr size and the address spaces are the same.
1047-
// This requires knowing the width of a pointer, so it can't be done in
1048-
// ConstantExpr::getCast.
1049-
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
1050-
if (CE->getOpcode() == Instruction::PtrToInt) {
1051-
Constant *SrcPtr = CE->getOperand(0);
1052-
unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType());
1053-
unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1054-
1055-
if (MidIntSize >= SrcPtrSize) {
1056-
unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
1057-
if (SrcAS == DestTy->getPointerAddressSpace())
1058-
return FoldBitCast(CE->getOperand(0), DestTy, DL);
1059-
}
1060-
}
1061-
}
1062-
1063-
return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
1064-
case Instruction::Trunc:
1065-
case Instruction::ZExt:
1066-
case Instruction::SExt:
1067-
case Instruction::FPTrunc:
1068-
case Instruction::FPExt:
1069-
case Instruction::UIToFP:
1070-
case Instruction::SIToFP:
1071-
case Instruction::FPToUI:
1072-
case Instruction::FPToSI:
1073-
case Instruction::AddrSpaceCast:
1074-
return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
1075-
case Instruction::BitCast:
1076-
return FoldBitCast(Ops[0], DestTy, DL);
10771028
case Instruction::Select:
10781029
return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
10791030
case Instruction::ExtractElement:
@@ -1188,6 +1139,67 @@ Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
11881139
return ConstantExpr::get(Opcode, LHS, RHS);
11891140
}
11901141

1142+
Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
1143+
Type *DestTy, const DataLayout &DL) {
1144+
assert(Instruction::isCast(Opcode));
1145+
switch (Opcode) {
1146+
default:
1147+
llvm_unreachable("Missing case");
1148+
case Instruction::PtrToInt:
1149+
// If the input is a inttoptr, eliminate the pair. This requires knowing
1150+
// the width of a pointer, so it can't be done in ConstantExpr::getCast.
1151+
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1152+
if (CE->getOpcode() == Instruction::IntToPtr) {
1153+
Constant *Input = CE->getOperand(0);
1154+
unsigned InWidth = Input->getType()->getScalarSizeInBits();
1155+
unsigned PtrWidth = DL.getPointerTypeSizeInBits(CE->getType());
1156+
if (PtrWidth < InWidth) {
1157+
Constant *Mask =
1158+
ConstantInt::get(CE->getContext(),
1159+
APInt::getLowBitsSet(InWidth, PtrWidth));
1160+
Input = ConstantExpr::getAnd(Input, Mask);
1161+
}
1162+
// Do a zext or trunc to get to the dest size.
1163+
return ConstantExpr::getIntegerCast(Input, DestTy, false);
1164+
}
1165+
}
1166+
return ConstantExpr::getCast(Opcode, C, DestTy);
1167+
case Instruction::IntToPtr:
1168+
// If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
1169+
// the int size is >= the ptr size and the address spaces are the same.
1170+
// This requires knowing the width of a pointer, so it can't be done in
1171+
// ConstantExpr::getCast.
1172+
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1173+
if (CE->getOpcode() == Instruction::PtrToInt) {
1174+
Constant *SrcPtr = CE->getOperand(0);
1175+
unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType());
1176+
unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1177+
1178+
if (MidIntSize >= SrcPtrSize) {
1179+
unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
1180+
if (SrcAS == DestTy->getPointerAddressSpace())
1181+
return FoldBitCast(CE->getOperand(0), DestTy, DL);
1182+
}
1183+
}
1184+
}
1185+
1186+
return ConstantExpr::getCast(Opcode, C, DestTy);
1187+
case Instruction::Trunc:
1188+
case Instruction::ZExt:
1189+
case Instruction::SExt:
1190+
case Instruction::FPTrunc:
1191+
case Instruction::FPExt:
1192+
case Instruction::UIToFP:
1193+
case Instruction::SIToFP:
1194+
case Instruction::FPToUI:
1195+
case Instruction::FPToSI:
1196+
case Instruction::AddrSpaceCast:
1197+
return ConstantExpr::getCast(Opcode, C, DestTy);
1198+
case Instruction::BitCast:
1199+
return FoldBitCast(C, DestTy, DL);
1200+
}
1201+
}
1202+
11911203
/// Given a constant and a getelementptr constantexpr, return the constant value
11921204
/// being addressed by the constant expression, or null if something is funny
11931205
/// and we can't decide.

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3639,7 +3639,7 @@ static Value *SimplifyPHINode(PHINode *PN, const Query &Q) {
36393639

36403640
static Value *SimplifyTruncInst(Value *Op, Type *Ty, const Query &Q, unsigned) {
36413641
if (Constant *C = dyn_cast<Constant>(Op))
3642-
return ConstantFoldInstOperands(Instruction::Trunc, Ty, C, Q.DL, Q.TLI);
3642+
return ConstantFoldCastOperand(Instruction::Trunc, C, Ty, Q.DL);
36433643

36443644
return nullptr;
36453645
}

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -895,17 +895,17 @@ static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
895895
// the constant folding APIs to do this so that we get the benefit of
896896
// DataLayout.
897897
if (isa<PointerType>(A->getType()))
898-
A = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
899-
const_cast<Constant *>(A), DL);
898+
A = ConstantFoldCastOperand(Instruction::PtrToInt,
899+
const_cast<Constant *>(A), IntTy, DL);
900900
else if (A->getType() != IntTy)
901-
A = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
902-
const_cast<Constant *>(A), DL);
901+
A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
902+
IntTy, DL);
903903
if (isa<PointerType>(B->getType()))
904-
B = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
905-
const_cast<Constant *>(B), DL);
904+
B = ConstantFoldCastOperand(Instruction::PtrToInt,
905+
const_cast<Constant *>(B), IntTy, DL);
906906
else if (B->getType() != IntTy)
907-
B = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
908-
const_cast<Constant *>(B), DL);
907+
B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
908+
IntTy, DL);
909909

910910
return A == B;
911911
}

0 commit comments

Comments
 (0)