Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -2808,9 +2808,13 @@ SplatBitSize = SplatBitSize * 2) SplatValue |= SplatValue.shl(SplatBitSize); - Constant = APInt::getAllOnesValue(BitWidth); - for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i) - Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth); + // Make sure that variable 'Constant' is only set if 'SplatBitSize' is a + // multiple of 'BitWidth'. Otherwise, we could propagate a wrong value. + if (SplatBitSize % BitWidth == 0) { + Constant = APInt::getAllOnesValue(BitWidth); + for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i) + Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth); + } } } Index: test/CodeGen/X86/and-load-fold.ll =================================================================== --- test/CodeGen/X86/and-load-fold.ll +++ test/CodeGen/X86/and-load-fold.ll @@ -0,0 +1,15 @@ +; RUN: llc -mtriple=x86_64-unknown-unknown -mcpu=generic < %s | FileCheck %s + +; Verify that the DAGCombiner doesn't wrongly remove the 'and' from the dag. + +define i8 @foo(<4 x i8>* %V) { +; CHECK-LABEL: foo: +; CHECK: pand +; CHECK: ret +entry: + %Vp = bitcast <4 x i8>* %V to <3 x i8>* + %V3i8 = load <3 x i8>, <3 x i8>* %Vp, align 4 + %0 = and <3 x i8> %V3i8, + %1 = extractelement <3 x i8> %0, i64 2 + ret i8 %1 +}