aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp26
1 files changed, 21 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 80d6cec..698abbb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6087,12 +6087,12 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
// Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
// integer type is the same size as the pointer type.
- auto CompatibleSizes = [&](Type *SrcTy, Type *DestTy) {
- if (isa<VectorType>(SrcTy)) {
- SrcTy = cast<VectorType>(SrcTy)->getElementType();
- DestTy = cast<VectorType>(DestTy)->getElementType();
+ auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {
+ if (isa<VectorType>(PtrTy)) {
+ PtrTy = cast<VectorType>(PtrTy)->getElementType();
+ IntTy = cast<VectorType>(IntTy)->getElementType();
}
- return DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth();
+ return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();
};
if (CastOp0->getOpcode() == Instruction::PtrToInt &&
CompatibleSizes(SrcTy, DestTy)) {
@@ -6109,6 +6109,22 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
}
+ // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
+ if (CastOp0->getOpcode() == Instruction::IntToPtr &&
+ CompatibleSizes(DestTy, SrcTy)) {
+ Value *NewOp1 = nullptr;
+ if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) {
+ Value *IntSrc = IntToPtrOp1->getOperand(0);
+ if (IntSrc->getType() == Op0Src->getType())
+ NewOp1 = IntToPtrOp1->getOperand(0);
+ } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
+ NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);
+ }
+
+ if (NewOp1)
+ return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
+ }
+
if (Instruction *R = foldICmpWithTrunc(ICmp))
return R;