[llvm] [SeparateConstOffsetFromGEP] Decompose constant xor operand if possible (PR #135788)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 23 03:00:36 PDT 2025
================
@@ -1162,6 +1167,179 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
return true;
}
+bool SeparateConstOffsetFromGEP::decomposeXor(Function &F) {
+ bool FunctionChanged = false;
+ SmallVector<std::pair<Instruction *, Value *>, 16> ReplacementsToMake;
+
+ for (BasicBlock &BB : F) {
+ for (Instruction &I : BB) {
+ if (I.getOpcode() == Instruction::Xor) {
+ if (Value *Replacement = tryFoldXorToOrDisjoint(I)) {
+ ReplacementsToMake.push_back({&I, Replacement});
+ FunctionChanged = true;
+ }
+ }
+ }
+ }
+
+ if (!ReplacementsToMake.empty()) {
+ LLVM_DEBUG(dbgs() << "Applying " << ReplacementsToMake.size()
+ << " XOR->OR Disjoint replacements in " << F.getName()
+ << "\n");
+ for (auto &Pair : ReplacementsToMake)
+ Pair.first->replaceAllUsesWith(Pair.second);
+
+ for (auto &Pair : ReplacementsToMake)
+ Pair.first->eraseFromParent();
+ }
+
+ return FunctionChanged;
+}
+
+static llvm::Instruction *findClosestSequentialXor(Value *A, Instruction &I) {
+ llvm::Instruction *ClosestUser = nullptr;
+ for (llvm::User *User : A->users()) {
+ if (auto *UserInst = llvm::dyn_cast<llvm::Instruction>(User)) {
+ if (UserInst->getOpcode() != Instruction::Xor || UserInst == &I)
+ continue;
+ if (!ClosestUser)
+ ClosestUser = UserInst;
+ else {
+ // Compare instruction positions.
+ if (UserInst->comesBefore(ClosestUser)) {
+ ClosestUser = UserInst;
+ }
+ }
+ }
+ }
+ return ClosestUser;
+}
+
+/// Try to transform I = xor(A, C1) into or_disjoint(Y, C2)
+/// where Y = xor(A, C0) is another existing instruction dominating I,
+/// C2 = C1 - C0, and A is known to be disjoint with C2.
----------------
arsenm wrote:
This isn't core to what this pass is doing. This should have occurred in previous passes
https://github.com/llvm/llvm-project/pull/135788
More information about the llvm-commits
mailing list