[llvm] [SeparateConstOffsetFromGEP] Decompose constant xor operand if possible (PR #135788)
Sumanth Gundapaneni via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 24 06:30:53 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)) {
----------------
sgundapa wrote:
The naming is confusing and probably wrong. I will change the name of this function.
B = xor (A, 32)
C = xor (A, 2080) --> modified to C = XOR(B, 2048)
D = xor( A, 4128) --> modified to D = XOR(B, 4096) , here, when I scan for XOR, I see both B and C. This function tries to get B.
https://github.com/llvm/llvm-project/pull/135788
More information about the llvm-commits
mailing list