[llvm] [SeparateConstOffsetFromGEP] Decompose constant xor operand if possible (PR #135788)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Wed May 21 04:10:58 PDT 2025


================
@@ -1162,6 +1199,167 @@ 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::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;
+
+  for (size_t i = 1; i < XorsInGroup.size(); ++i) {
+    BinaryOperator *currentXorToProcess = XorsInGroup[i].first;
+
+    // Check if the XorWithSmallConst dominates currentXorToProcess.
+    // If not, clone and add the instruction.
+    if (!DT.dominates(XorWithSmallConst, currentXorToProcess)) {
----------------
jmmartinez wrote:

Consider having a helper function to check if "the xor-with-small-const dominates all the other xor".

It's be easier to read

```cpp
if(xor-with-small-const doesn't dominate all other xors) {
  // do the clone
}
```

than

```cpp
for(xor in xors) {
  if(xor-with-small-const doesn't dominate xor) {
    // do the clone
    break;
  }
}
```

https://github.com/llvm/llvm-project/pull/135788


More information about the llvm-commits mailing list