[llvm] [InstCombine] recognize missed i128 split optimization (PR #129363)

Muhammad Bassiouni via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 28 21:49:39 PST 2025


https://github.com/bassiounix created https://github.com/llvm/llvm-project/pull/129363

This pr fixes #126056, recognising a split i128 extension optimization.

Proof for working optimization:

```llvm
define i128 @src(i32 noundef %x) {
entry:
  %coerce.sroa.0.0.extract.trunc = sext i32 %x to i64
  %0 = ashr i32 %x, 31
  %coerce.sroa.2.0.extract.trunc = sext i32 %0 to i64
  %x.sroa.2.0.insert.ext.i = zext i64 %coerce.sroa.2.0.extract.trunc to i128
  %x.sroa.2.0.insert.shift.i = shl nuw i128 %x.sroa.2.0.insert.ext.i, 64
  %x.sroa.0.0.insert.ext.i = zext i64 %coerce.sroa.0.0.extract.trunc to i128
  %x.sroa.0.0.insert.insert.i = or disjoint i128 %x.sroa.2.0.insert.shift.i, %x.sroa.0.0.insert.ext.i
  ret i128 %x.sroa.0.0.insert.insert.i
}

define i128 @tgt(i32 noundef %x)  {
  %x.sroa.0.0.insert.insert.i = sext i32 %x to i128
  ret i128 %x.sroa.0.0.insert.insert.i
}
```

>From eb3a1f15d0ffee624ed87d9f496c0c3ed2f8e27f Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sat, 1 Mar 2025 07:24:38 +0200
Subject: [PATCH] [InstCombine] recognize missed i128 split optimization

---
 llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 175c653f17f07..cff95338650f2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3119,6 +3119,13 @@ static Value *matchOrConcat(Instruction &Or, InstCombiner::BuilderTy &Builder) {
       match(UpperSrc, m_BitReverse(m_Value(UpperBRev))))
     return ConcatIntrinsicCalls(Intrinsic::bitreverse, UpperBRev, LowerBRev);
 
+  Value *X;
+  if (match(LowerSrc, m_SExt(m_Value(X))) &&
+      match(UpperSrc,
+            m_SExt(m_AShr(m_Specific(X), m_SpecificInt(HalfWidth / 2 - 1))))) {
+    return Builder.CreateSExt(X, Ty);
+  }
+
   return nullptr;
 }
 



More information about the llvm-commits mailing list