[llvm] [InstCombine] Convert load from LUT into a select (PR #98339)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 10 08:51:14 PDT 2024


================
@@ -998,6 +998,89 @@ static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op) {
   return false;
 }
 
+static Value *foldLoadFromIndexedGlobal(LoadInst &LI, IRBuilderBase &Builder) {
+  if (LI.isVolatile())
+    return nullptr;
+
+  auto *GEP = dyn_cast<GetElementPtrInst>(LI.getPointerOperand());
+  if (!GEP || LI.getType() != GEP->getResultElementType())
+    return nullptr;
+
+  auto *GV = dyn_cast<GlobalVariable>(GEP->getPointerOperand());
+  if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
+      GV->getValueType() != GEP->getSourceElementType())
+    return nullptr;
+
+  Constant *Init = GV->getInitializer();
+  if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
+    return nullptr;
+
+  uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
+  // Don't blow up on huge arrays.
+  // This threshold is chosen based on statistics on a dataset
+  // which is collected from real-world applications.
----------------
dtcxzyw wrote:

See https://github.com/dtcxzyw/llvm-tools/blob/main/lutscan.cpp
```
Threshold: Checks Folds
2: 1424 712
3: 10478 1529
4: 24997 1605
5: 55930 1646
6: 63276 1656
7: 66608 1671
8: 84613 1686
9: 86974 1688
10: 91222 1691
12: 96041 1695
14: 98018 1699
15: 98975 1706
19: 109062 1708
20: 114191 1710
32: 150399 1711
41: 156531 1712
51: 162192 1713
77: 173370 1714
```

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


More information about the llvm-commits mailing list