[llvm] b21e328 - [IR] Add ptradd fast path in accumulateConstantOffset() (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 12 03:40:50 PST 2024


Author: Nikita Popov
Date: 2024-02-12T12:40:42+01:00
New Revision: b21e3282864c9f7ad656c64bc375f5869ef76d19

URL: https://github.com/llvm/llvm-project/commit/b21e3282864c9f7ad656c64bc375f5869ef76d19
DIFF: https://github.com/llvm/llvm-project/commit/b21e3282864c9f7ad656c64bc375f5869ef76d19.diff

LOG: [IR] Add ptradd fast path in accumulateConstantOffset() (NFC)

For getelementptr i8 (aka ptradd) we can skip the whole logic and
directly use the offset. As we're now canonicalizing to this form,
it's pretty common and worth having a fast-path for.

Added: 
    

Modified: 
    llvm/lib/IR/Operator.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Operator.cpp b/llvm/lib/IR/Operator.cpp
index 16a89534b4b3ec..caf8fe654a36dc 100644
--- a/llvm/lib/IR/Operator.cpp
+++ b/llvm/lib/IR/Operator.cpp
@@ -108,6 +108,15 @@ bool GEPOperator::accumulateConstantOffset(
 bool GEPOperator::accumulateConstantOffset(
     Type *SourceType, ArrayRef<const Value *> Index, const DataLayout &DL,
     APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) {
+  // Fast path for canonical getelementptr i8 form.
+  if (SourceType->isIntegerTy(8) && !ExternalAnalysis) {
+    if (auto *CI = dyn_cast<ConstantInt>(Index.front())) {
+      Offset += CI->getValue().sextOrTrunc(Offset.getBitWidth());
+      return true;
+    }
+    return false;
+  }
+
   bool UsedExternalAnalysis = false;
   auto AccumulateOffset = [&](APInt Index, uint64_t Size) -> bool {
     Index = Index.sextOrTrunc(Offset.getBitWidth());


        


More information about the llvm-commits mailing list