[DAG] Fix constant store folding to handle non-byte sizes.

Avoid crashes from zero-byte values due to sub-byte store sizes.

Reviewers: uabelho, courbet, rnk

Reviewed By: courbet

Subscribers: hiraditya, llvm-commits

Tags: #llvm

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

llvm-svn: 354884
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGAddressAnalysis.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGAddressAnalysis.cpp
index b0dcf47..f5adfa5 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGAddressAnalysis.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGAddressAnalysis.cpp
@@ -135,9 +135,10 @@
   return false; // Cannot determine whether the pointers alias.
 }
 
-bool BaseIndexOffset::contains(int64_t Size, const BaseIndexOffset &Other,
-                               int64_t OtherSize, const SelectionDAG &DAG,
-                               int64_t &Offset) const {
+bool BaseIndexOffset::contains(const SelectionDAG &DAG, int64_t BitSize,
+                               const BaseIndexOffset &Other,
+                               int64_t OtherBitSize, int64_t &BitOffset) const {
+  int64_t Offset;
   if (!equalBaseIndex(Other, DAG, Offset))
     return false;
   if (Offset >= 0) {
@@ -145,7 +146,8 @@
     // [-------*this---------]
     //            [---Other--]
     // ==Offset==>
-    return Offset + OtherSize <= Size;
+    BitOffset = 8 * Offset;
+    return BitOffset + OtherBitSize <= BitSize;
   }
   // Other starts strictly before *this, it cannot be fully contained.
   //    [-------*this---------]