Skip to content

Commit 4ed0d8f

Browse files
committed
[NFC][InstCombine] Extract freelyInvertAllUsersOf() out of canonicalizeICmpPredicate()
I'd like to use it in an upcoming fold.
1 parent efeb8ca commit 4ed0d8f

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
263263
}
264264

265265
/// Given i1 V, can every user of V be freely adapted if V is changed to !V ?
266-
/// InstCombine's canonicalizeICmpPredicate() must be kept in sync with this
267-
/// fn.
266+
/// InstCombine's freelyInvertAllUsersOf() must be kept in sync with this fn.
268267
///
269268
/// See also: isFreeToInvert()
270269
static bool canFreelyInvertAllUsersOf(Value *V, Value *IgnoredUser) {

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5328,26 +5328,8 @@ CmpInst *InstCombinerImpl::canonicalizeICmpPredicate(CmpInst &I) {
53285328
I.setPredicate(CmpInst::getInversePredicate(Pred));
53295329
I.setName(I.getName() + ".not");
53305330

5331-
// And now let's adjust every user.
5332-
for (User *U : I.users()) {
5333-
switch (cast<Instruction>(U)->getOpcode()) {
5334-
case Instruction::Select: {
5335-
auto *SI = cast<SelectInst>(U);
5336-
SI->swapValues();
5337-
SI->swapProfMetadata();
5338-
break;
5339-
}
5340-
case Instruction::Br:
5341-
cast<BranchInst>(U)->swapSuccessors(); // swaps prof metadata too
5342-
break;
5343-
case Instruction::Xor:
5344-
replaceInstUsesWith(cast<Instruction>(*U), &I);
5345-
break;
5346-
default:
5347-
llvm_unreachable("Got unexpected user - out of sync with "
5348-
"canFreelyInvertAllUsersOf() ?");
5349-
}
5350-
}
5331+
// And, adapt users.
5332+
freelyInvertAllUsersOf(&I);
53515333

53525334
return &I;
53535335
}

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
323323
Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
324324
Instruction *matchSAddSubSat(SelectInst &MinMax1);
325325

326+
void freelyInvertAllUsersOf(Value *V);
327+
326328
/// Determine if a pair of casts can be replaced by a single cast.
327329
///
328330
/// \param CI1 The first of a pair of casts.

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,30 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
870870
return SI;
871871
}
872872

873+
/// Freely adapt every user of V as-if V was changed to !V.
874+
/// WARNING: only if canFreelyInvertAllUsersOf() said this can be done.
875+
void InstCombinerImpl::freelyInvertAllUsersOf(Value *I) {
876+
for (User *U : I->users()) {
877+
switch (cast<Instruction>(U)->getOpcode()) {
878+
case Instruction::Select: {
879+
auto *SI = cast<SelectInst>(U);
880+
SI->swapValues();
881+
SI->swapProfMetadata();
882+
break;
883+
}
884+
case Instruction::Br:
885+
cast<BranchInst>(U)->swapSuccessors(); // swaps prof metadata too
886+
break;
887+
case Instruction::Xor:
888+
replaceInstUsesWith(cast<Instruction>(*U), I);
889+
break;
890+
default:
891+
llvm_unreachable("Got unexpected user - out of sync with "
892+
"canFreelyInvertAllUsersOf() ?");
893+
}
894+
}
895+
}
896+
873897
/// Given a 'sub' instruction, return the RHS of the instruction if the LHS is a
874898
/// constant zero (which is the 'negate' form).
875899
Value *InstCombinerImpl::dyn_castNegVal(Value *V) const {

0 commit comments

Comments
 (0)