@@ -5813,19 +5813,20 @@ using OffsetOp = std::pair<Instruction::BinaryOps, Value *>;
5813
5813
static void collectOffsetOp(Value *V, SmallVectorImpl<OffsetOp> &Offsets,
5814
5814
bool AllowRecursion) {
5815
5815
Instruction *Inst = dyn_cast<Instruction>(V);
5816
- if (!Inst)
5816
+ if (!Inst || !Inst->hasOneUse() )
5817
5817
return;
5818
5818
5819
5819
switch (Inst->getOpcode()) {
5820
- case Instruction::Add: {
5821
- Constant *C;
5822
- if (match(Inst->getOperand(1), m_ImmConstant(C)) &&
5823
- !C->containsUndefOrPoisonElement()) {
5824
- if (Constant *NegC = ConstantExpr::getNeg(C))
5825
- Offsets.emplace_back(Instruction::Add, NegC);
5826
- }
5820
+ 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));
5825
+ break;
5826
+ case Instruction::Sub:
5827
+ if (isGuaranteedNotToBeUndefOrPoison(Inst->getOperand(1)))
5828
+ Offsets.emplace_back(Instruction::Add, Inst->getOperand(1));
5827
5829
break;
5828
- }
5829
5830
case Instruction::Xor:
5830
5831
if (isGuaranteedNotToBeUndefOrPoison(Inst->getOperand(1)))
5831
5832
Offsets.emplace_back(Instruction::Xor, Inst->getOperand(1));
@@ -5834,12 +5835,8 @@ static void collectOffsetOp(Value *V, SmallVectorImpl<OffsetOp> &Offsets,
5834
5835
break;
5835
5836
case Instruction::Select:
5836
5837
if (AllowRecursion) {
5837
- Value *TrueV = Inst->getOperand(1);
5838
- if (TrueV->hasOneUse())
5839
- collectOffsetOp(TrueV, Offsets, /*AllowRecursion=*/false);
5840
- Value *FalseV = Inst->getOperand(2);
5841
- if (FalseV->hasOneUse())
5842
- collectOffsetOp(FalseV, Offsets, /*AllowRecursion=*/false);
5838
+ collectOffsetOp(Inst->getOperand(1), Offsets, /*AllowRecursion=*/false);
5839
+ collectOffsetOp(Inst->getOperand(2), Offsets, /*AllowRecursion=*/false);
5843
5840
}
5844
5841
break;
5845
5842
default:
@@ -5887,10 +5884,8 @@ static Instruction *foldICmpEqualityWithOffset(ICmpInst &I,
5887
5884
return nullptr;
5888
5885
5889
5886
SmallVector<OffsetOp, 4> OffsetOps;
5890
- if (Op0->hasOneUse())
5891
- collectOffsetOp(Op0, OffsetOps, /*AllowRecursion=*/true);
5892
- if (Op1->hasOneUse())
5893
- collectOffsetOp(Op1, OffsetOps, /*AllowRecursion=*/true);
5887
+ collectOffsetOp(Op0, OffsetOps, /*AllowRecursion=*/true);
5888
+ collectOffsetOp(Op1, OffsetOps, /*AllowRecursion=*/true);
5894
5889
5895
5890
auto ApplyOffsetImpl = [&](Value *V, unsigned BinOpc, Value *RHS) -> Value * {
5896
5891
Value *Simplified = simplifyBinOp(BinOpc, V, RHS, SQ);
0 commit comments