[llvm] [InstCombine] Lower flag check pattern to use a bitmask-shift (PR #169557)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 6 07:30:19 PST 2025


================
@@ -2410,6 +2410,59 @@ Value *InstCombinerImpl::reassociateBooleanAndOr(Value *LHS, Value *X, Value *Y,
   return nullptr;
 }
 
+static Value *combineAndOrOfImmCmpToBitExtract(Instruction &Or,
+                                               InstCombiner::BuilderTy &Builder,
+                                               const DataLayout &DL) {
+  ConstantComparesGatherer ConstantCompare(&Or, DL, /*OneUse=*/true);
+  // Unpack the result
+  SmallVectorImpl<ConstantInt *> &Values = ConstantCompare.Vals;
+  Value *Index = ConstantCompare.CompValue;
+
+  // TODO: Handle ConstantCompare.Extra case
+  // If expanding an existing case, only adding one extra case is still good
+  if (!Index || !isGuaranteedNotToBeUndefOrPoison(Index) ||
+      (ConstantCompare.UsedICmps + ConstantCompare.ExpansionCase) < 3 ||
+      ConstantCompare.Extra)
+    return nullptr;
+
+  unsigned MaxRegWidth = DL.getLargestLegalIntTypeSizeInBits();
+  unsigned MaxVal = 0;
+  // TODO: Handle case where some values are too large for map but some are not.
+  for (auto *CI : Values) {
+    unsigned Val = CI->getValue().getLimitedValue();
+    if (Val >= MaxRegWidth)
+      return nullptr;
+    if (Val > MaxVal)
+      MaxVal = Val;
+  }
+  LLVMContext &Context = Or.getContext();
+  APInt BitMapAP(MaxVal + 1, 0);
+  for (auto *CI : Values) {
+    unsigned Val = CI->getValue().getLimitedValue();
+    BitMapAP.setBit(Val);
+  }
+  ConstantInt *BitMap = ConstantInt::get(Context, BitMapAP);
+  Value *Result = ConstantComparesGatherer::createBitMapSeq(
+      BitMap, Index, &Builder, /*BitMapElementTy=*/Type::getInt1Ty(Context));
+
+  // If the maximum value in the bitmap is larger than can be stored
+  // in Index, don't have to worry about overflow on the shift
+  unsigned IndexBits = dyn_cast<IntegerType>(Index->getType())->getBitWidth();
----------------
dtcxzyw wrote:

Use getScalarSizeInBits()

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


More information about the llvm-commits mailing list