Skip to content

Commit 9f0df3a

Browse files
committed
[InstCombine] Address review comments.
1 parent 74ee165 commit 9f0df3a

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -5818,20 +5818,15 @@ static void collectOffsetOp(Value *V, SmallVectorImpl<OffsetOp> &Offsets,
58185818

58195819
switch (Inst->getOpcode()) {
58205820
case Instruction::Add:
5821-
if (isGuaranteedNotToBeUndefOrPoison(Inst->getOperand(1)))
5822-
Offsets.emplace_back(Instruction::Sub, Inst->getOperand(1));
5823-
if (isGuaranteedNotToBeUndefOrPoison(Inst->getOperand(0)))
5824-
Offsets.emplace_back(Instruction::Sub, Inst->getOperand(0));
5821+
Offsets.emplace_back(Instruction::Sub, Inst->getOperand(1));
5822+
Offsets.emplace_back(Instruction::Sub, Inst->getOperand(0));
58255823
break;
58265824
case Instruction::Sub:
5827-
if (isGuaranteedNotToBeUndefOrPoison(Inst->getOperand(1)))
5828-
Offsets.emplace_back(Instruction::Add, Inst->getOperand(1));
5825+
Offsets.emplace_back(Instruction::Add, Inst->getOperand(1));
58295826
break;
58305827
case Instruction::Xor:
5831-
if (isGuaranteedNotToBeUndefOrPoison(Inst->getOperand(1)))
5832-
Offsets.emplace_back(Instruction::Xor, Inst->getOperand(1));
5833-
if (isGuaranteedNotToBeUndefOrPoison(Inst->getOperand(0)))
5834-
Offsets.emplace_back(Instruction::Xor, Inst->getOperand(0));
5828+
Offsets.emplace_back(Instruction::Xor, Inst->getOperand(1));
5829+
Offsets.emplace_back(Instruction::Xor, Inst->getOperand(0));
58355830
break;
58365831
case Instruction::Select:
58375832
if (AllowRecursion) {
@@ -5892,7 +5887,11 @@ static Instruction *foldICmpEqualityWithOffset(ICmpInst &I,
58925887
// Avoid infinite loops by checking if RHS is an identity for the BinOp.
58935888
if (!Simplified || Simplified == V)
58945889
return nullptr;
5895-
return Simplified;
5890+
// Reject constant expressions as they don't simplify things.
5891+
if (isa<Constant>(Simplified) && !match(Simplified, m_ImmConstant()))
5892+
return nullptr;
5893+
// Check if the transformation introduces poison.
5894+
return impliesPoison(RHS, V) ? Simplified : nullptr;
58965895
};
58975896

58985897
auto ApplyOffset = [&](Value *V, unsigned BinOpc,

llvm/test/Transforms/InstCombine/icmp-select.ll

+16
Original file line numberDiff line numberDiff line change
@@ -831,3 +831,19 @@ entry:
831831
%res = icmp eq i8 %sel1, %sel2
832832
ret i1 %res
833833
}
834+
835+
@g = external global i8
836+
837+
; Do not introduce constant expressions.
838+
define i1 @discr_eq_constantexpr(ptr %p) {
839+
; CHECK-LABEL: @discr_eq_constantexpr(
840+
; CHECK-NEXT: [[I:%.*]] = ptrtoint ptr [[P:%.*]] to i64
841+
; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[I]], ptrtoint (ptr @g to i64)
842+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[SUB]], -1
843+
; CHECK-NEXT: ret i1 [[CMP]]
844+
;
845+
%i = ptrtoint ptr %p to i64
846+
%sub = sub i64 %i, ptrtoint (ptr @g to i64)
847+
%cmp = icmp eq i64 %sub, -1
848+
ret i1 %cmp
849+
}

0 commit comments

Comments
 (0)