diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -15427,13 +15427,13 @@ !ST1->getBasePtr().isUndef()) { const BaseIndexOffset STBase = BaseIndexOffset::match(ST, DAG); const BaseIndexOffset ChainBase = BaseIndexOffset::match(ST1, DAG); - unsigned STByteSize = ST->getMemoryVT().getSizeInBits() / 8; - unsigned ChainByteSize = ST1->getMemoryVT().getSizeInBits() / 8; + unsigned STBitSize = ST->getMemoryVT().getSizeInBits(); + unsigned ChainBitSize = ST1->getMemoryVT().getSizeInBits(); // If this is a store who's preceding store to a subset of the current // location and no one other node is chained to that store we can // effectively drop the store. Do not remove stores to undef as they may // be used as data sinks. - if (STBase.contains(STByteSize, ChainBase, ChainByteSize, DAG)) { + if (STBase.contains((STBitSize+7)/8, ChainBase, (ChainBitSize+7)/8, DAG)) { CombineTo(ST1, ST1->getChain()); return SDValue(); } @@ -15443,13 +15443,13 @@ // the other uses of ST1's chain are unconcerned with ST, this folding // will not affect those nodes. int64_t Offset; - if (ChainBase.contains(ChainByteSize, STBase, STByteSize, DAG, + if (ChainBase.contains((ChainBitSize+7)/8, STBase, (STBitSize+7)/8, DAG, Offset)) { SDValue ChainValue = ST1->getValue(); if (auto *C1 = dyn_cast(ChainValue)) { if (auto *C = dyn_cast(Value)) { APInt Val = C1->getAPIntValue(); - APInt InsertVal = C->getAPIntValue().zextOrTrunc(STByteSize * 8); + APInt InsertVal = C->getAPIntValue().zextOrTrunc(STBitSize); // FIXME: Handle Big-endian mode. if (!DAG.getDataLayout().isBigEndian()) { Val.insertBits(InsertVal, Offset * 8); diff --git a/llvm/test/CodeGen/X86/constant-combines.ll b/llvm/test/CodeGen/X86/constant-combines.ll --- a/llvm/test/CodeGen/X86/constant-combines.ll +++ b/llvm/test/CodeGen/X86/constant-combines.ll @@ -38,3 +38,15 @@ store float %8, float* %0, align 4 ret void } + + +define void @bitstore_fold() { +; CHECK-LABEL: bitstore_fold: +; CHECK: # %bb.0: # %BB +; CHECK-NEXT: movl $-2, 0 +; CHECK-NEXT: retq +BB: + store i32 -1, i32* null + store i1 false, i1* null + ret void +}