[llvm] [SeparateConstOffsetFromGEP] Decompose constant xor operand if possible (PR #135788)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Thu May 22 03:00:29 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");
----------------
arsenm wrote:
```suggestion
<< F.getName() << ":\n"
<< " New Const: " << NewConstVal
<< " Smallest Const: " << SmallestConst << " are not disjoint\n");
```
https://github.com/llvm/llvm-project/pull/135788
More information about the llvm-commits
mailing list