[llvm] [InstCombine] Fold icmp(constants[x]) when the range of x is given (PR #67093)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 29 01:19:07 PST 2023


================
@@ -187,43 +167,60 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
   // the array, this will fully represent all the comparison results.
   uint64_t MagicBitvector = 0;
 
+  Value *Idx = nullptr;
+
   // Scan the array and see if one of our patterns matches.
-  Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
-  for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
-    Constant *Elt = Init->getAggregateElement(i);
-    if (!Elt) return nullptr;
-
-    // If this is indexing an array of structures, get the structure element.
-    if (!LaterIndices.empty()) {
-      Elt = ConstantFoldExtractValueInstruction(Elt, LaterIndices);
-      if (!Elt)
-        return nullptr;
-    }
+  Constant *ComparedRHS = cast<Constant>(ICI.getOperand(1));
+  // The longest step we can reach once.
+  APInt OffsetStep = VariableOffsets.front().second;
+
+  // BeginOffset is the offset from constant pointer where we begin scanning the
+  // constant. Make BeginOffset the smallest offset greater than 0
+  APInt BeginOffset = ConstantOffset.srem(OffsetStep);
+  if (BeginOffset.slt(0))
+    BeginOffset += OffsetStep;
+
+  uint64_t ElementCountToTraverse =
+      (DataSize - BeginOffset).udiv(OffsetStep).getZExtValue() + 1;
+
+  // Don't traverse too many times.
+  if (ElementCountToTraverse > MaxArraySizeForCombine)
+    return nullptr;
+
+  APInt CurOffset = BeginOffset;
+  for (uint64_t i = 0; i < ElementCountToTraverse;
+       ++i, CurOffset += OffsetStep) {
+    Constant *Elt = ConstantFoldLoadFromConstPtr(GV, LoadedTy, CurOffset, DL);
+
+    if (!Elt)
+      return nullptr;
 
     // If the element is masked, handle it.
     if (AndCst) {
       Elt = ConstantFoldBinaryOpOperands(Instruction::And, Elt, AndCst, DL);
       if (!Elt)
         return nullptr;
     }
+    errs() << *Elt << "\n";
----------------
dtcxzyw wrote:

Temporary debug-print statements?

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


More information about the llvm-commits mailing list