-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[ConstantFolding] Support ptrtoaddr in cast folds #162480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+54
−16
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1495,30 +1495,31 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C, | |
default: | ||
llvm_unreachable("Missing case"); | ||
case Instruction::PtrToAddr: | ||
// TODO: Add some of the ptrtoint folds here as well. | ||
break; | ||
case Instruction::PtrToInt: | ||
if (auto *CE = dyn_cast<ConstantExpr>(C)) { | ||
Constant *FoldedValue = nullptr; | ||
// If the input is a inttoptr, eliminate the pair. This requires knowing | ||
// If the input is an inttoptr, eliminate the pair. This requires knowing | ||
// the width of a pointer, so it can't be done in ConstantExpr::getCast. | ||
if (CE->getOpcode() == Instruction::IntToPtr) { | ||
// zext/trunc the inttoptr to pointer size. | ||
FoldedValue = ConstantFoldIntegerCast(CE->getOperand(0), | ||
DL.getIntPtrType(CE->getType()), | ||
// zext/trunc the inttoptr to pointer/address size. | ||
Type *MidTy = Opcode == Instruction::PtrToInt | ||
? DL.getAddressType(CE->getType()) | ||
: DL.getIntPtrType(CE->getType()); | ||
FoldedValue = ConstantFoldIntegerCast(CE->getOperand(0), MidTy, | ||
/*IsSigned=*/false, DL); | ||
} else if (auto *GEP = dyn_cast<GEPOperator>(CE)) { | ||
// If we have GEP, we can perform the following folds: | ||
// (ptrtoint (gep null, x)) -> x | ||
// (ptrtoint (gep (gep null, x), y) -> x + y, etc. | ||
// (ptrtoint/ptrtoaddr (gep null, x)) -> x | ||
// (ptrtoint/ptrtoaddr (gep (gep null, x), y) -> x + y, etc. | ||
unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType()); | ||
APInt BaseOffset(BitWidth, 0); | ||
auto *Base = cast<Constant>(GEP->stripAndAccumulateConstantOffsets( | ||
DL, BaseOffset, /*AllowNonInbounds=*/true)); | ||
if (Base->isNullValue()) { | ||
FoldedValue = ConstantInt::get(CE->getContext(), BaseOffset); | ||
} else { | ||
// ptrtoint (gep i8, Ptr, (sub 0, V)) -> sub (ptrtoint Ptr), V | ||
// ptrtoint/ptrtoaddr (gep i8, Ptr, (sub 0, V)) | ||
// -> sub (ptrtoint/ptrtoaddr Ptr), V | ||
if (GEP->getNumIndices() == 1 && | ||
GEP->getSourceElementType()->isIntegerTy(8)) { | ||
auto *Ptr = cast<Constant>(GEP->getPointerOperand()); | ||
|
@@ -1528,12 +1529,13 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C, | |
Sub->getOpcode() == Instruction::Sub && | ||
Sub->getOperand(0)->isNullValue()) | ||
FoldedValue = ConstantExpr::getSub( | ||
ConstantExpr::getPtrToInt(Ptr, IntIdxTy), Sub->getOperand(1)); | ||
ConstantExpr::getCast(Opcode, Ptr, IntIdxTy), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure these folds are valid for unstable GC pointers, but since this has been like this for a long time and I have no detailed knowledge of GC pointer support I imagine it's fine. |
||
Sub->getOperand(1)); | ||
} | ||
} | ||
} | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (FoldedValue) { | ||
// Do a zext or trunc to get to the ptrtoint dest size. | ||
// Do a zext or trunc to get to the ptrtoint/ptrtoaddr dest size. | ||
return ConstantFoldIntegerCast(FoldedValue, DestTy, /*IsSigned=*/false, | ||
DL); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.