llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (kimyounhoex1)

<details>
<summary>Changes</summary>

These X86 builtins (`__builtin_ia32_pslldqi128/256_byteshift` and
`__builtin_ia32_psrldqi128/256_byteshift`) can now be evaluated at
compile time when the shift amount is a constant integer.

This improves consistency with other vector shift intrinsics and
reduces unnecessary runtime evaluation.

Fixes #<!-- -->156494

---
Full diff: https://github.com/llvm/llvm-project/pull/157403.diff


1 Files Affected:

- (modified) clang/lib/AST/ExprConstant.cpp (+65) 


``````````diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index b4f1e76187e25..2b06705a4870c 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -12039,6 +12039,71 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr 
*E) {
     }
     return Success(APValue(ResultElements.data(), ResultElements.size()), E);
   }
+  case X86::BI__builtin_ia32_pslldqi128_byteshift:
+  case X86::BI__builtin_ia32_psrldqi128_byteshift: {
+    unsigned BuiltinID = E->getBuiltinCallee();
+
+    APSInt Amt;
+    if (!EvaluateInteger(E->getArg(1), Amt, Info))
+      break;
+    unsigned Shift = (unsigned)Amt.getZExtValue();
+
+    APValue Vec;
+    if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector())
+      break;
+
+    SmallVector<APValue, 16> ResultElements;
+    ResultElements.reserve(16);
+
+    bool isLeft = (BuiltinID == X86::BI__builtin_ia32_pslldqi128_byteshift);
+
+    for (unsigned i = 0; i < 16; i++) {
+      int SrcIdx = -1;
+      if (isLeft)
+        SrcIdx = i + Shift;
+      else if (i >= Shift)
+        SrcIdx = i - Shift;
+
+      if (SrcIdx >= 0 && (unsigned)SrcIdx < 16)
+        ResultElements.push_back(Vec.getVectorElt(SrcIdx));
+      else
+        ResultElements.push_back(APValue(0));
+    }
+    return Success(APValue(ResultElements.data(), ResultElements.size()), E);
+  }
+
+  case X86::BI__builtin_ia32_pslldqi256_byteshift:
+  case X86::BI__builtin_ia32_psrldqi256_byteshift: {
+    unsigned BuiltinID = E->getBuiltinCallee();
+
+    APSInt Amt;
+    if (!EvaluateInteger(E->getArg(1), Amt, Info))
+      break;
+    unsigned Shift = (unsigned)Amt.getZExtValue();
+
+    APValue Vec;
+    if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector())
+      break;
+
+    SmallVector<APValue, 32> ResultElements;
+    ResultElements.reserve(32);
+
+    bool isLeft = (BuiltinID == X86::BI__builtin_ia32_pslldqi256_byteshift);
+
+    for (unsigned i = 0; i < 32; i++) {
+      int SrcIdx = -1;
+      if (isLeft)
+        SrcIdx = i + Shift;
+      else if (i >= Shift)
+        SrcIdx = i - Shift;
+
+      if (SrcIdx >= 0 && (unsigned)SrcIdx < 32)
+        ResultElements.push_back(Vec.getVectorElt(SrcIdx));
+      else
+        ResultElements.push_back(APValue(0));
+    }
+    return Success(APValue(ResultElements.data(), ResultElements.size()), E);
+  }
   }
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/157403
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to