[llvm] [EarlyCSE] Compare GEP instructions based on offset (PR #65875)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 16 09:18:02 PDT 2023


================
@@ -553,6 +552,77 @@ bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) {
   return LHSI->isIdenticalTo(RHSI);
 }
 
+//===----------------------------------------------------------------------===//
+// GEPValue
+//===----------------------------------------------------------------------===//
+
+namespace {
+
+struct GEPValue {
+  Instruction *Inst;
+  APInt ConstantOffset;
+  bool HasConstantOffset;
+
+  GEPValue(Instruction *I) : Inst(I), HasConstantOffset(false) {
+      assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
+  }
+  GEPValue(Instruction *I, APInt ConstantOffset, bool HasConstantOffset)
+      : Inst(I), ConstantOffset(ConstantOffset),
+        HasConstantOffset(HasConstantOffset) {
+      assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
+  }
+
+  bool isSentinel() const {
+      return Inst == DenseMapInfo<Instruction *>::getEmptyKey() ||
+             Inst == DenseMapInfo<Instruction *>::getTombstoneKey();
+  }
+
+  static bool canHandle(Instruction *Inst) {
+      return isa<GetElementPtrInst>(Inst);
+  }
+};
+
+} // namespace
+
+namespace llvm {
+
+template <> struct DenseMapInfo<GEPValue> {
+  static inline GEPValue getEmptyKey() {
+      return DenseMapInfo<Instruction *>::getEmptyKey();
+  }
+
+  static inline GEPValue getTombstoneKey() {
+      return DenseMapInfo<Instruction *>::getTombstoneKey();
+  }
+
+  static unsigned getHashValue(GEPValue Val);
+  static bool isEqual(GEPValue LHS, GEPValue RHS);
+};
+
+} // end namespace llvm
+
+unsigned DenseMapInfo<GEPValue>::getHashValue(GEPValue Val) {
----------------
nikic wrote:

```suggestion
unsigned DenseMapInfo<GEPValue>::getHashValue(const GEPValue &Val) {
```
Here and elsewhere.

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


More information about the llvm-commits mailing list