[llvm] [InstCombine] Convert load from LUT into a select (PR #98339)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 17 09:25:08 PDT 2024
================
@@ -1003,6 +1003,106 @@ static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op) {
return false;
}
+static Value *foldLoadFromIndexedGlobal(LoadInst &LI, IRBuilderBase &Builder,
+ TargetLibraryInfo &TLI) {
+ if (LI.isVolatile())
+ return nullptr;
+
+ auto *GEP = dyn_cast<GetElementPtrInst>(LI.getPointerOperand());
+ if (!GEP)
+ return nullptr;
+
+ auto *GV = dyn_cast<GlobalVariable>(GEP->getPointerOperand());
+ if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
+ return nullptr;
+
+ Constant *Init = GV->getInitializer();
+ auto &DL = LI.getDataLayout();
+
+ uint64_t IndexBW = DL.getIndexTypeSizeInBits(GEP->getType());
+ APInt ConstOffset(IndexBW, 0);
+ MapVector<Value *, APInt> VariableOffsets;
+ if (!GEP->collectOffset(DL, IndexBW, VariableOffsets, ConstOffset))
+ return nullptr;
+
+ if (!ConstOffset.isZero() || VariableOffsets.size() != 1)
+ return nullptr;
+
+ auto &Step = VariableOffsets.front().second;
+ if (Step.isNonPositive())
+ return nullptr;
+ uint64_t ArraySize = DL.getTypeAllocSize(Init->getType()).getFixedValue();
+ // Don't blow up on huge arrays.
+ // This threshold is chosen based on statistics on a dataset
+ // which is collected from real-world applications.
+ constexpr uint64_t MaxArraySize = 16;
+ if (ArraySize > MaxArraySize * Step.getZExtValue())
----------------
dtcxzyw wrote:
```
Thres(Byte) ScanCount FoldCount
2: 752 376
3: 8216 2348
4: 10589 2430
5: 55154 2439
6: 56425 2446
7: 57124 2452
8: 71137 2720
10: 73154 2726
12: 77412 2859
14: 78321 2867
15: 78661 2869
16: 94733 3997
20: 122138 4010
22: 123309 4029
24: 137531 4270
28: 140128 4283
32: 177082 4377
36: 185826 4381
40: 194747 4417
41: 194862 4418
43: 195173 4422
48: 211237 4436
55: 214016 4440
56: 219882 4443
60: 220588 4448
64: 236973 4488
72: 241460 4489
76: 242053 4490
77: 242295 4492
80: 245371 4497
81: 245581 4502
90: 248057 4503
96: 251053 4506
```
https://github.com/llvm/llvm-project/pull/98339
More information about the llvm-commits
mailing list