Fix incorrect GEP bitwidth in areNonOverlapSameBaseLoadAndStore()
authorAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>
Tue, 28 Sep 2021 14:10:39 +0000 (15:10 +0100)
committerAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>
Tue, 28 Sep 2021 16:57:36 +0000 (17:57 +0100)
When using a datalayout that has pointer index width != pointer size this
code triggers an assertion in Value::stripAndAccumulateConstantOffsets().
I encountered this this while compiling FreeBSD for CHERI-RISC-V.
Also update LoadsTest.cpp to use a DataLayout with index width != pointer
width to ensure this case is tested.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D110406

llvm/lib/Analysis/Loads.cpp
llvm/unittests/Analysis/LoadsTest.cpp

index 1c55f48..2e5f80d 100644 (file)
@@ -451,8 +451,8 @@ static bool areNonOverlapSameBaseLoadAndStore(const Value *LoadPtr,
                                               const Value *StorePtr,
                                               Type *StoreTy,
                                               const DataLayout &DL) {
-  APInt LoadOffset(DL.getTypeSizeInBits(LoadPtr->getType()), 0);
-  APInt StoreOffset(DL.getTypeSizeInBits(StorePtr->getType()), 0);
+  APInt LoadOffset(DL.getIndexTypeSizeInBits(LoadPtr->getType()), 0);
+  APInt StoreOffset(DL.getIndexTypeSizeInBits(StorePtr->getType()), 0);
   const Value *LoadBase = LoadPtr->stripAndAccumulateConstantOffsets(
       DL, LoadOffset, /* AllowNonInbounds */ false);
   const Value *StoreBase = StorePtr->stripAndAccumulateConstantOffsets(
index 0ee1a5f..5570b74 100644 (file)
@@ -28,6 +28,7 @@ TEST(LoadsTest, FindAvailableLoadedValueSameBasePtrConstantOffsetsNullAA) {
   LLVMContext C;
   std::unique_ptr<Module> M = parseIR(C,
                                       R"IR(
+target datalayout = "p:64:64:64:32"
 %class = type <{ i32, i32 }>
 
 define i32 @f() {