[llvm] 24962ce - [Loads] Handle simple cases with same base pointer with constant offsets in FindAvailableLoadedValue when AA is null.
Hiroshi Yamauchi via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 29 13:06:07 PST 2020
Author: Hiroshi Yamauchi
Date: 2020-01-29T13:05:46-08:00
New Revision: 24962ced814123195be1a8cb41b615f255da824f
URL: https://github.com/llvm/llvm-project/commit/24962ced814123195be1a8cb41b615f255da824f
DIFF: https://github.com/llvm/llvm-project/commit/24962ced814123195be1a8cb41b615f255da824f.diff
LOG: [Loads] Handle simple cases with same base pointer with constant offsets in FindAvailableLoadedValue when AA is null.
Summary:
This will help with devirtualization (store forwarding with vtable pointers in
the presence of other stores into members in the constructor.) During inlining,
we don't have AA.
Reviewers: davidxl
Subscribers: mgorny, Prazek, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D71307
Added:
llvm/unittests/Analysis/LoadsTest.cpp
Modified:
llvm/lib/Analysis/Loads.cpp
llvm/unittests/Analysis/CMakeLists.txt
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/Loads.cpp b/llvm/lib/Analysis/Loads.cpp
index 218fc3e0030d..c2e9b8b882e4 100644
--- a/llvm/lib/Analysis/Loads.cpp
+++ b/llvm/lib/Analysis/Loads.cpp
@@ -381,6 +381,28 @@ Value *llvm::FindAvailableLoadedValue(LoadInst *Load,
ScanFrom, MaxInstsToScan, AA, IsLoad, NumScanedInst);
}
+// Check if the load and the store have the same base, constant offsets and
+// non-overlapping access ranges.
+static bool AreNonOverlapSameBaseLoadAndStore(
+ Value *LoadPtr, Type *LoadTy, Value *StorePtr, Type *StoreTy,
+ const DataLayout &DL) {
+ APInt LoadOffset(DL.getTypeSizeInBits(LoadPtr->getType()), 0);
+ APInt StoreOffset(DL.getTypeSizeInBits(StorePtr->getType()), 0);
+ Value *LoadBase = LoadPtr->stripAndAccumulateConstantOffsets(
+ DL, LoadOffset, /* AllowNonInbounds */ false);
+ Value *StoreBase = StorePtr->stripAndAccumulateConstantOffsets(
+ DL, StoreOffset, /* AllowNonInbounds */ false);
+ if (LoadBase != StoreBase)
+ return false;
+ auto LoadAccessSize = LocationSize::precise(DL.getTypeStoreSize(LoadTy));
+ auto StoreAccessSize = LocationSize::precise(DL.getTypeStoreSize(StoreTy));
+ ConstantRange LoadRange(LoadOffset,
+ LoadOffset + LoadAccessSize.toRaw());
+ ConstantRange StoreRange(StoreOffset,
+ StoreOffset + StoreAccessSize.toRaw());
+ return LoadRange.intersectWith(StoreRange).isEmptySet();
+}
+
Value *llvm::FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy,
bool AtLeastAtomic, BasicBlock *ScanBB,
BasicBlock::iterator &ScanFrom,
@@ -459,10 +481,21 @@ Value *llvm::FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy,
StrippedPtr != StorePtr)
continue;
- // If we have alias analysis and it says the store won't modify the loaded
- // value, ignore the store.
- if (AA && !isModSet(AA->getModRefInfo(SI, StrippedPtr, AccessSize)))
- continue;
+ if (!AA) {
+ // When AA isn't available, but if the load and the store have the same
+ // base, constant offsets and non-overlapping access ranges, ignore the
+ // store. This is a simple form of alias analysis that is used by the
+ // inliner. FIXME: use BasicAA if possible.
+ if (AreNonOverlapSameBaseLoadAndStore(
+ Ptr, AccessTy, SI->getPointerOperand(),
+ SI->getValueOperand()->getType(), DL))
+ continue;
+ } else {
+ // If we have alias analysis and it says the store won't modify the
+ // loaded value, ignore the store.
+ if (!isModSet(AA->getModRefInfo(SI, StrippedPtr, AccessSize)))
+ continue;
+ }
// Otherwise the store that may or may not alias the pointer, bail out.
++ScanFrom;
diff --git a/llvm/unittests/Analysis/CMakeLists.txt b/llvm/unittests/Analysis/CMakeLists.txt
index 4b4ea416ed8d..669277fb1586 100644
--- a/llvm/unittests/Analysis/CMakeLists.txt
+++ b/llvm/unittests/Analysis/CMakeLists.txt
@@ -20,6 +20,7 @@ add_llvm_unittest(AnalysisTests
GlobalsModRefTest.cpp
IVDescriptorsTest.cpp
LazyCallGraphTest.cpp
+ LoadsTest.cpp
LoopInfoTest.cpp
MemoryBuiltinsTest.cpp
MemorySSATest.cpp
diff --git a/llvm/unittests/Analysis/LoadsTest.cpp b/llvm/unittests/Analysis/LoadsTest.cpp
new file mode 100644
index 000000000000..64758ebd39d1
--- /dev/null
+++ b/llvm/unittests/Analysis/LoadsTest.cpp
@@ -0,0 +1,61 @@
+//===- LoadsTest.cpp - local load analysis unit tests ---------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/Loads.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/SourceMgr.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
+ SMDiagnostic Err;
+ std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
+ if (!Mod)
+ Err.print("AnalysisTests", errs());
+ return Mod;
+}
+
+TEST(LoadsTest, FindAvailableLoadedValueSameBasePtrConstantOffsetsNullAA) {
+ LLVMContext C;
+ std::unique_ptr<Module> M = parseIR(C,
+ R"IR(
+%class = type <{ i32, i32 }>
+
+define i32 @f() {
+entry:
+ %o = alloca %class
+ %f1 = getelementptr inbounds %class, %class* %o, i32 0, i32 0
+ store i32 42, i32* %f1
+ %f2 = getelementptr inbounds %class, %class* %o, i32 0, i32 1
+ store i32 43, i32* %f2
+ %v = load i32, i32* %f1
+ ret i32 %v
+}
+)IR");
+ auto *GV = M->getNamedValue("f");
+ ASSERT_TRUE(GV);
+ auto *F = dyn_cast<Function>(GV);
+ ASSERT_TRUE(F);
+ Instruction *Inst = &F->front().front();
+ auto *AI = dyn_cast<AllocaInst>(Inst);
+ ASSERT_TRUE(AI);
+ Inst = &*++F->front().rbegin();
+ auto *LI = dyn_cast<LoadInst>(Inst);
+ ASSERT_TRUE(LI);
+ BasicBlock::iterator BBI(LI);
+ Value *Loaded = FindAvailableLoadedValue(
+ LI, LI->getParent(), BBI, 0, nullptr, nullptr);
+ ASSERT_TRUE(Loaded);
+ auto *CI = dyn_cast<ConstantInt>(Loaded);
+ ASSERT_TRUE(CI);
+ ASSERT_TRUE(CI->equalsInt(42));
+}
More information about the llvm-commits
mailing list