[llvm] [SeparateConstOffsetFromGEP] Decompose constant xor operand if possible (PR #135788)
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Thu May 22 02:43:20 PDT 2025
================
@@ -1162,6 +1202,165 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
return true;
}
+// Helper function to check if an instruction has at least one GEP user
+bool XorToOrDisjointTransformer::hasGEPUser(const Value *V) {
+ for (const User *U : V->users()) {
+ if (isa<GetElementPtrInst>(U)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool XorToOrDisjointTransformer::dominatesAllXors(
+ BinaryOperator *BaseXor, const XorOpList &XorsInGroup) {
+ for (const auto &XorEntry : XorsInGroup) {
+ BinaryOperator *XorInst = XorEntry.first;
+ if (XorInst != BaseXor && !DT.dominates(BaseXor, XorInst)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool XorToOrDisjointTransformer::processXorGroup(Instruction *OriginalBaseInst,
+ XorOpList &XorsInGroup) {
+ bool Changed = false;
+ if (XorsInGroup.size() <= 1)
+ return false;
+
+ // Sort XorsInGroup by the constant offset value in increasing order.
+ llvm::sort(
+ XorsInGroup.begin(), XorsInGroup.end(),
+ [](const auto &A, const auto &B) { return A.second.ult(B.second); });
+
+ // Dominance check
+ // The "base" XOR for dominance purposes is the one with the smallest
+ // constant.
+ BinaryOperator *XorWithSmallConst = XorsInGroup[0].first;
+
+ if (!dominatesAllXors(XorWithSmallConst, XorsInGroup)) {
+ LLVM_DEBUG(dbgs() << DEBUG_TYPE
+ << ": Cloning and inserting XOR with smallest constant ("
+ << *XorWithSmallConst
+ << ") as it does not dominate all other XORs"
+ << " in function " << F.getName() << "\n");
+
+ BinaryOperator *ClonedXor =
+ cast<BinaryOperator>(XorWithSmallConst->clone());
+ ClonedXor->setName(XorWithSmallConst->getName() + ".dom_clone");
+ ClonedXor->insertAfter(OriginalBaseInst);
+ LLVM_DEBUG(dbgs() << " Cloned Inst: " << *ClonedXor << "\n");
+ Changed = true;
+ XorWithSmallConst = ClonedXor;
+ }
+
+ SmallVector<Instruction *, 8> InstructionsToErase;
+ const APInt SmallestConst =
+ cast<ConstantInt>(XorWithSmallConst->getOperand(1))->getValue();
+
+ // Main transformation loop: Iterate over the original XORs in the sorted
+ // group.
+ for (const auto &XorEntry : XorsInGroup) {
+ BinaryOperator *XorInst = XorEntry.first; // Original XOR instruction
+ const APInt ConstOffsetVal = XorEntry.second;
+
+ // Do not process the one with smallest constant as it is the base.
+ if (XorInst == XorWithSmallConst)
+ continue;
+
+ // Disjointness Check 1
+ APInt NewConstVal = ConstOffsetVal - SmallestConst;
+ if ((NewConstVal & SmallestConst) != 0) {
+ LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Cannot transform XOR in function "
+ << F.getName() << ":\n"
+ << " New Const: " << NewConstVal << "\n"
+ << " Smallest Const: " << SmallestConst << "\n"
+ << " are not disjoint \n");
+ continue;
+ }
+
+ // Disjointness Check 2
+ KnownBits KnownBaseBits(
+ XorWithSmallConst->getType()->getScalarSizeInBits());
+ computeKnownBits(XorWithSmallConst, KnownBaseBits, DL, 0, nullptr,
+ XorWithSmallConst, &DT);
+ if ((KnownBaseBits.Zero & NewConstVal) == NewConstVal) {
+ LLVM_DEBUG(dbgs() << DEBUG_TYPE
+ << ": Transforming XOR to OR (disjoint) in function "
+ << F.getName() << ":\n"
+ << " Xor: " << *XorInst << "\n"
+ << " Base Val: " << *XorWithSmallConst << "\n"
+ << " New Const: " << NewConstVal << "\n");
+
+ auto *NewOrInst = BinaryOperator::CreateDisjointOr(
+ XorWithSmallConst,
+ ConstantInt::get(OriginalBaseInst->getType(), NewConstVal),
+ XorInst->getName() + ".or_disjoint", XorInst->getIterator());
+
+ NewOrInst->copyMetadata(*XorInst);
+ XorInst->replaceAllUsesWith(NewOrInst);
+ LLVM_DEBUG(dbgs() << " New Inst: " << *NewOrInst << "\n");
+ InstructionsToErase.push_back(XorInst); // Mark original XOR for deletion
+
+ Changed = true;
+ } else {
+ LLVM_DEBUG(
+ dbgs() << DEBUG_TYPE
+ << ": Cannot transform XOR (not proven disjoint) in function "
+ << F.getName() << ":\n"
+ << " Xor: " << *XorInst << "\n"
+ << " Base Val: " << *XorWithSmallConst << "\n"
+ << " New Const: " << NewConstVal << "\n");
+ }
+ }
+
+ for (Instruction *I : InstructionsToErase)
+ I->eraseFromParent();
+
+ return Changed;
+}
+
+// Try to transform XOR(A, B+C) in to XOR(A,C) + B where XOR(A,C) becomes
+// the base for memory operations. This transformation is true under the
+// following conditions
+// Check 1 - B and C are disjoint.
+// Check 2 - XOR(A,C) and B are disjoint.
+//
+// This transformation is beneficial particularly for GEPs because:
+// 1. OR operations often map better to addressing modes than XOR
+// 2. Disjoint OR operations preserve the semantics of the original XOR
+// 3. This can enable further optimizations in the GEP offset folding pipeline
+bool XorToOrDisjointTransformer::run() {
+ bool Changed = false;
+
+ // Collect all candidate XORs
+ for (Instruction &I : instructions(F)) {
+ Instruction *Op0 = nullptr;
+ ConstantInt *C1 = nullptr;
+ BinaryOperator *MatchedXorOp = nullptr;
+
+ // Attempt to match the instruction 'I' as XOR operation.
+ if (match(&I, m_CombineAnd(m_Xor(m_Instruction(Op0), m_ConstantInt(C1)),
+ m_BinOp(MatchedXorOp))) &&
+ hasGEPUser(MatchedXorOp))
+ XorGroups[Op0].push_back({MatchedXorOp, C1->getValue()});
+ }
+
+ if (XorGroups.empty())
+ return false;
+
+ // Process each group of XORs
+ for (auto &GroupPair : XorGroups) {
+ Instruction *OriginalBaseInst = GroupPair.first;
+ XorOpList &XorsInGroup = GroupPair.second;
----------------
jayfoad wrote:
Nit:
```suggestion
for (auto &[OriginalBaseInst, XorsInGroup] : XorGroups) {
```
https://github.com/llvm/llvm-project/pull/135788
More information about the llvm-commits
mailing list