From 4ed0d8f2f07d0e17942366d48a29c165384ace52 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Fri, 22 Jan 2021 12:51:40 +0300 Subject: [PATCH] [NFC][InstCombine] Extract freelyInvertAllUsersOf() out of canonicalizeICmpPredicate() I'd like to use it in an upcoming fold. --- .../llvm/Transforms/InstCombine/InstCombiner.h | 3 +-- .../Transforms/InstCombine/InstCombineCompares.cpp | 22 ++------------------ .../Transforms/InstCombine/InstCombineInternal.h | 2 ++ .../InstCombine/InstructionCombining.cpp | 24 ++++++++++++++++++++++ 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h index a5aed72..aae0694 100644 --- a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h +++ b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h @@ -263,8 +263,7 @@ public: } /// Given i1 V, can every user of V be freely adapted if V is changed to !V ? - /// InstCombine's canonicalizeICmpPredicate() must be kept in sync with this - /// fn. + /// InstCombine's freelyInvertAllUsersOf() must be kept in sync with this fn. /// /// See also: isFreeToInvert() static bool canFreelyInvertAllUsersOf(Value *V, Value *IgnoredUser) { diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 9b3cfb3..cd9a036 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -5328,26 +5328,8 @@ CmpInst *InstCombinerImpl::canonicalizeICmpPredicate(CmpInst &I) { I.setPredicate(CmpInst::getInversePredicate(Pred)); I.setName(I.getName() + ".not"); - // And now let's adjust every user. - for (User *U : I.users()) { - switch (cast(U)->getOpcode()) { - case Instruction::Select: { - auto *SI = cast(U); - SI->swapValues(); - SI->swapProfMetadata(); - break; - } - case Instruction::Br: - cast(U)->swapSuccessors(); // swaps prof metadata too - break; - case Instruction::Xor: - replaceInstUsesWith(cast(*U), &I); - break; - default: - llvm_unreachable("Got unexpected user - out of sync with " - "canFreelyInvertAllUsersOf() ?"); - } - } + // And, adapt users. + freelyInvertAllUsersOf(&I); return &I; } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h index 6468e40..16bc265 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -323,6 +323,8 @@ private: Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN); Instruction *matchSAddSubSat(SelectInst &MinMax1); + void freelyInvertAllUsersOf(Value *V); + /// Determine if a pair of casts can be replaced by a single cast. /// /// \param CI1 The first of a pair of casts. diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 570a07e..2f8a80a 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -870,6 +870,30 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, return SI; } +/// Freely adapt every user of V as-if V was changed to !V. +/// WARNING: only if canFreelyInvertAllUsersOf() said this can be done. +void InstCombinerImpl::freelyInvertAllUsersOf(Value *I) { + for (User *U : I->users()) { + switch (cast(U)->getOpcode()) { + case Instruction::Select: { + auto *SI = cast(U); + SI->swapValues(); + SI->swapProfMetadata(); + break; + } + case Instruction::Br: + cast(U)->swapSuccessors(); // swaps prof metadata too + break; + case Instruction::Xor: + replaceInstUsesWith(cast(*U), I); + break; + default: + llvm_unreachable("Got unexpected user - out of sync with " + "canFreelyInvertAllUsersOf() ?"); + } + } +} + /// Given a 'sub' instruction, return the RHS of the instruction if the LHS is a /// constant zero (which is the 'negate' form). Value *InstCombinerImpl::dyn_castNegVal(Value *V) const { -- 2.7.4