Hi Mehdi,<br><br>It's not altogether surprising- after type legalisation BUILD_VECTOR can't have any illegal types feeding it, and i16 is illegal. I32 is the smallest legal type. <br><br>Therefore it must be able deal with truncation!<br><br>Cheers,<br><br>James<br><div class="gmail_quote">On Thu, 16 Apr 2015 at 20:24, Mehdi Amini <<a href="mailto:mehdi.amini@apple.com">mehdi.amini@apple.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
> On Apr 16, 2015, at 10:52 AM, Ahmed Bougacha <<a href="mailto:ahmed.bougacha@gmail.com" target="_blank">ahmed.bougacha@gmail.com</a>> wrote:<br>
><br>
> On Thu, Apr 16, 2015 at 10:44 AM, Mehdi Amini <<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>> wrote:<br>
>> Hi Simon,<br>
>><br>
>> I have an out-of-tree target broken by this commits, and I have a feeling that the problem is in this change.<br>
>><br>
>> I have this node:  v2i32  BUILD_VECTOR { i32 = Constant<0>,  i32 = undef  }<br>
>> That I try to truncate:<br>
>> getNode(ISD::TRUNCATE, dl, v2i16, N)<br>
>><br>
>> After your change, getNode() returns this node:<br>
>><br>
>> N = v2i16  BUILD_VECTOR { i32 = Constant<0>,  i32 = undef  }<br>
>><br>
>> Note that the operands are not truncated, which breaks the DAG type system.<br>
><br>
> It does?  BUILD_VECTOR can truncate integers, and that's the point of<br>
> this fix, no?<br>
<br>
I didn’t know BUILD_VECTOR had an implicit TRUNCATE.  Does it carry only implicit truncate or other conversion as well (EXTEND for instance)?<br>
<br>
So we are not allowed to fold naively an EXTRACT_VECTOR_ELEMENT that is fed with a BUILD_VECTOR?<br>
This does not seem in-line with usual behavior in the DAG and it makes combining more complex.<br>
<br>
<br>
Thanks,<br>
<br>
Mehdi<br>
<br>
><br>
> -Ahmed<br>
><br>
>> I assume you are relying on FoldConstantArithmetic() to magically convert the constant to the truncated type, however it does not work with undef.<br>
>><br>
>> I have the feeling that the proper way requires to wrap every operand with a TRUNCATE before calling getNode, but you may see an alternative?<br>
>><br>
>> Thanks,<br>
>><br>
>> Mehdi<br>
>><br>
>><br>
>><br>
>>> On Apr 16, 2015, at 1:21 AM, Simon Pilgrim <<a href="mailto:llvm-dev@redking.me.uk" target="_blank">llvm-dev@redking.me.uk</a>> wrote:<br>
>>><br>
>>> Author: rksimon<br>
>>> Date: Thu Apr 16 03:21:09 2015<br>
>>> New Revision: 235079<br>
>>><br>
>>> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=235079&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=235079&view=rev</a><br>
>>> Log:<br>
>>> TRUNCATE constant folding - minor fix for rL233224<br>
>>><br>
>>> Fix for test case found by James Molloy - TRUNCATE of constant build vectors can be more simply achieved by simply replacing with a new build vector node with the truncated value type - no need to touch the scalar operands at all.<br>
>>><br>
>>> Added:<br>
>>>   llvm/trunk/test/CodeGen/AArch64/fold-constants.ll<br>
>>> Modified:<br>
>>>   llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp<br>
>>><br>
>>> Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp<br>
>>> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp?rev=235079&r1=235078&r2=235079&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp?rev=235079&r1=235078&r2=235079&view=diff</a><br>
>>> ==============================================================================<br>
>>> --- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (original)<br>
>>> +++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp Thu Apr 16 03:21:09 2015<br>
>>> @@ -2851,13 +2851,16 @@ SDValue SelectionDAG::getNode(unsigned O<br>
>>>        // FIXME: Entirely reasonable to perform folding of other unary<br>
>>>        // operations here as the need arises.<br>
>>>        break;<br>
>>> +      case ISD::TRUNCATE:<br>
>>> +        // Constant build vector truncation can be done with the original scalar<br>
>>> +        // operands but with a new build vector with the truncated value type.<br>
>>> +        return getNode(ISD::BUILD_VECTOR, DL, VT, BV->ops());<br>
>>>      case ISD::FNEG:<br>
>>>      case ISD::FABS:<br>
>>>      case ISD::FCEIL:<br>
>>>      case ISD::FTRUNC:<br>
>>>      case ISD::FFLOOR:<br>
>>>      case ISD::FP_EXTEND:<br>
>>> -      case ISD::TRUNCATE:<br>
>>>      case ISD::UINT_TO_FP:<br>
>>>      case ISD::SINT_TO_FP: {<br>
>>>        // Let the above scalar folding handle the folding of each element.<br>
>>><br>
>>> Added: llvm/trunk/test/CodeGen/AArch64/fold-constants.ll<br>
>>> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AArch64/fold-constants.ll?rev=235079&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AArch64/fold-constants.ll?rev=235079&view=auto</a><br>
>>> ==============================================================================<br>
>>> --- llvm/trunk/test/CodeGen/AArch64/fold-constants.ll (added)<br>
>>> +++ llvm/trunk/test/CodeGen/AArch64/fold-constants.ll Thu Apr 16 03:21:09 2015<br>
>>> @@ -0,0 +1,21 @@<br>
>>> +; RUN: llc -mtriple=aarch64-linux-gnu -o - %s | FileCheck %s<br>
>>> +<br>
>>> +define i64 @dotests_616() {<br>
>>> +; CHECK-LABEL: dotests_616<br>
>>> +; CHECK:       movi d0, #0000000000000000<br>
>>> +; CHECK-NEXT:  umov w8, v0.b[2]<br>
>>> +; CHECK-NEXT:  sbfx w8, w8, #0, #1<br>
>>> +; CHECK-NEXT:  fmov s0, w8<br>
>>> +; CHECK-NEXT:  fmov x0, d0<br>
>>> +; CHECK-NEXT:  ret<br>
>>> +entry:<br>
>>> +  %0 = bitcast <2 x i64> zeroinitializer to <8 x i16><br>
>>> +  %1 = and <8 x i16> zeroinitializer, %0<br>
>>> +  %2 = icmp ne <8 x i16> %1, zeroinitializer<br>
>>> +  %3 = extractelement <8 x i1> %2, i32 2<br>
>>> +  %vgetq_lane285 = sext i1 %3 to i16<br>
>>> +  %vset_lane = insertelement <4 x i16> undef, i16 %vgetq_lane285, i32 0<br>
>>> +  %4 = bitcast <4 x i16> %vset_lane to <1 x i64><br>
>>> +  %vget_lane = extractelement <1 x i64> %4, i32 0<br>
>>> +  ret i64 %vget_lane<br>
>>> +}<br>
>>><br>
>>><br>
>>> _______________________________________________<br>
>>> llvm-commits mailing list<br>
>>> <a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
>>> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
>><br>
>><br>
>> _______________________________________________<br>
>> llvm-commits mailing list<br>
>> <a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
>> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>