[clang] [llvm] [X86] Replace X86 specific PDEP/PEXT handling with generic intrinsics (PR #204144)

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 18 03:22:37 PDT 2026


================
@@ -2660,6 +2660,64 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
       return &CI;
     break;
   }
+  case Intrinsic::pdep:
+    if (auto *MaskC = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
+      if (MaskC->isNullValue())
+        return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), 0));
+
+      if (MaskC->isAllOnesValue())
+        return replaceInstUsesWith(*II, II->getArgOperand(0));
+
+      unsigned MaskIdx, MaskLen;
+      if (MaskC->getValue().isShiftedMask(MaskIdx, MaskLen)) {
+        // any single contingous sequence of 1s anywhere in the mask simply
+        // describes a subset of the input bits shifted to the appropriate
+        // position.  Replace with the straight forward IR.
+        Value *Input = II->getArgOperand(0);
+        Value *ShiftAmt = ConstantInt::get(II->getType(), MaskIdx);
+        Value *Shifted = Builder.CreateShl(Input, ShiftAmt);
+        Value *Masked = Builder.CreateAnd(Shifted, II->getArgOperand(1));
+        return replaceInstUsesWith(*II, Masked);
+      }
+
+      if (auto *SrcC = dyn_cast<ConstantInt>(II->getArgOperand(0))) {
+        // constant folding.
+        APInt Result =
+            llvm::APIntOps::expandBits(SrcC->getValue(), MaskC->getValue());
+        return replaceInstUsesWith(*II,
+                                   ConstantInt::get(II->getType(), Result));
----------------
nikic wrote:

Should go in ConstantFolding

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


More information about the cfe-commits mailing list