From 124ce3ab36b59aaa458b8bb545996f385fe92dfc Mon Sep 17 00:00:00 2001 From: Andrea Di Biagio Date: Sun, 13 Jul 2014 21:02:14 +0000 Subject: [PATCH] [DAGCombiner] Fix a crash caused by a missing check for legal type when trying to fold shuffles. Verify that DAGCombiner does not crash when trying to fold a pair of shuffles according to rule (added at r212539): (shuffle (shuffle A, Undef, M0), Undef, M1) -> (shuffle A, Undef, M2) The DAGCombiner avoids folding shuffles if the resulting shuffle dag node is not legal for the target. That means, the resulting shuffle must have legal type and legal mask. Before, the DAGCombiner only called method 'TargetLowering::isShuffleMaskLegal' to check if it was "safe" to fold according to the above-mentioned rule. However, this caused a crash in the x86 backend since method 'isShuffleMaskLegal' always expects to be called on a legal vector type. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212915 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 2 +- test/CodeGen/X86/shuffle-combine-crash.ll | 30 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/CodeGen/X86/shuffle-combine-crash.ll diff --git a/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index 707e48add40..c8daea9a8b6 100644 --- a/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -10764,7 +10764,7 @@ SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) { // It may still be beneficial to combine the two shuffles if the // resulting shuffle is legal. - if (TLI.isShuffleMaskLegal(Mask, VT)) { + if (TLI.isTypeLegal(VT) && TLI.isShuffleMaskLegal(Mask, VT)) { if (!CommuteOperands) // shuffle(shuffle(x, undef, M1), undef, M2) -> shuffle(x, undef, M3). // shuffle(shuffle(x, y, M1), undef, M2) -> shuffle(x, undef, M3) diff --git a/test/CodeGen/X86/shuffle-combine-crash.ll b/test/CodeGen/X86/shuffle-combine-crash.ll new file mode 100644 index 00000000000..6ab7b97e6a7 --- /dev/null +++ b/test/CodeGen/X86/shuffle-combine-crash.ll @@ -0,0 +1,30 @@ +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mcpu=corei7 + +; Verify that DAGCombiner does not crash when checking if it is +; safe to fold the shuffles in function @sample_test according to rule +; (shuffle (shuffle A, Undef, M0), Undef, M1) -> (shuffle A, Undef, M2) +; +; The DAGCombiner avoids folding shuffles if +; the resulting shuffle dag node is not legal for the target. +; That means, the shuffle must have legal type and legal mask. +; +; Before, the DAGCombiner forgot to check if the resulting shuffle +; was legal. It instead just called method +; 'X86TargetLowering::isShuffleMaskLegal'; however, that was not enough since +; that method always expect to have a valid vector type in input. +; As a consequence, compiling the function below would have caused a crash. + +define void @sample_test() { + br i1 undef, label %5, label %1 + +;