http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43716

--- Comment #39 from Dominique d'Humieres <dominiq at lps dot ens.fr> 
2012-07-22 10:47:42 UTC ---
> If I compile with -fno-tree-loop-if-convert the exception occurs 
> somewhere else:

It occurs in s00018 in the loop between lines 3782 to 3796

     DO j = 1 , n1
        dtpar = V0012
        IF ( i.EQ.NIV .AND. j.EQ.ISLni ) THEN
           k1 = 2
           DO k = 1 , k1
          dtpa = V0012
          IF ( DABS(DTMi(k)).GT.eps )                           &
     &                 dtpa = 2.*sens*DABS(TMI(k)/DTMi(k))
          dtpar = DMIN1(dtpar,dtpa)
           ENDDO
        ELSEIF ( DABS(DTGai(i,j)).GT.eps ) THEN
           dtpar = sens*DABS(TGAi(i,j)/DTGai(i,j))
        ENDIF
        DTTemp = DMIN1(DTTemp,dtpar)
     ENDDO

Before the loop i=2, n1=2, NIV=0, ISLni=0, eps=1.0e-8, sens=0.01,
DTTemp=0.05, DTGai(2,1)=3.4750524086054519, DTGai(2,2)=2.8245839253949421,
TGAi(2,1)=905.00000000000000, and TGAi(2,1)=1040.0000000000000.  So
(i.EQ.NIV .AND. j.EQ.ISLni) is false, DTGai(2,:)>eps and dtpar>20.0>DTTemp,
hence the loop does nothing and should not generate any "Floating-point
exception" as it does.

If I read correctly the assembly bellow, the loop

           k1 = 2
           DO k = 1 , k1
          dtpa = V0012
          IF ( DABS(DTMi(k)).GT.eps )                           &
     &                 dtpa = 2.*sens*DABS(TMI(k)/DTMi(k))
          dtpar = DMIN1(dtpar,dtpa)
           ENDDO

is unrolled and moved outside the 'DO j = 1 , n1' loop when s00018.f90 is 
compiled with -O2 -ffpe-trap=invalid -fno-trapping-math. The worst part of 
the optimization and the cause of the floating-point exception seems that 
the test 'IF ( DABS(DTMi(k)).GT.eps )' is removed (or delayed, even if I
replace eps 
with 1.0) while both TMI and DTMI are filled with zeroes.

Note that if I replace

          IF ( DABS(DTMi(k)).GT.eps )                           &
     &                 dtpa = 2.*sens*DABS(TMI(k)/DTMi(k))

with

                  dtpa = 2.*sens*DABS(TMI(k))/max(DABS(DTMi(k)),eps)

there is no exception.

IMO computing TMI(k)/DTMi(k) without the IF is over-agressive: if
DABS(DTMi(k)).LT.eps, dtpa will have to be recomputed. In addition, if the 
k loop is moved outside the j loop, it should at least protected by 
IF ( i.EQ.NIV), or better by
IF ( i.EQ.NIV .AND. ISLni.GE.1 .AND. ISLni.LE.n1).
Also since the ELSEIF depends on j and the IF is taken at most once,
moving the k loop outside the j loop seems dubious.

Note that the exception is (should be) harmless as it occurs in a dead 
branch.

L29:
    movl    36404(%r9,%rdi,4), %ecx    # aaa77.nasl, n1
    movsd    %xmm9, 936(%r11)    # x, aaa11.dtphy
    movsd    856(%r11), %xmm8    # aaa11.v22202, sens
    movl    $1, 396(%rsp)    #, j
    testl    %ecx, %ecx    # n1
    jle    L41    #,
    movsd    19416(%r9), %xmm7    # aaa77.dtmi, D.2813
    movapd    %xmm8, %xmm11    # sens, D.2817
    movl    $0, %edx    #, tmp908
    movsd    19400(%r9), %xmm0    # aaa77.tmi, x
    addsd    %xmm8, %xmm11    # sens, D.2817
    movapd    %xmm7, %xmm10    # D.2813, D.2814
    movq    _aaa13_@GOTPCREL(%rip), %rax    #,
    divsd    %xmm7, %xmm0    # D.2813, x
    andpd    %xmm1, %xmm10    # tmp1323, D.2814
    movsd    19424(%r9), %xmm12    # aaa77.dtmi, D.2813
    movsd    19408(%r9), %xmm7    # aaa77.tmi, x
    movsd    944(%r11), %xmm6    # aaa11.dttemp, x
    movl    240(%rax), %esi    # aaa13.niv, pretmp.160
    movl    244(%rax), %r14d    # aaa13.islni, D.2806
    xorl    %eax, %eax    # pretmp.165
    divsd    %xmm12, %xmm7    # D.2813, x
    andpd    %xmm1, %xmm0    # tmp1323, x
    mulsd    %xmm11, %xmm0    # D.2817, x
    ucomisd    %xmm0, %xmm2    # x, x
    seta    %al    #, pretmp.165
    ucomisd    %xmm3, %xmm10    # tmp1324, D.2814
    cmovbe    %edx, %eax    # pretmp.165,, tmp908, pretmp.165
    xorl    %edx, %edx    #
    ucomisd    %xmm2, %xmm2    # x, x
    setp    %dl    #,
    addl    $1, %ecx    #,
    movl    %ecx, 12(%rsp)    #, %sfp
    xorl    %ecx, %ecx    #
    movl    %edx, 336(%rsp)    #, %sfp
    leaq    30032(%r9,%rdi,8), %rdx    #, ivtmp.264
    andpd    %xmm1, %xmm7    # tmp1323, x
    mulsd    %xmm11, %xmm7    # D.2817, x
    movapd    %xmm3, %xmm11    # tmp1324,
    cmpltsd    %xmm10, %xmm11    #, D.2814,
    movapd    %xmm11, %xmm10    #, tmp923
    andpd    %xmm11, %xmm0    # tmp923, tmp924
    movapd    %xmm2, %xmm11    # x, x
    andnpd    %xmm2, %xmm10    # x, tmp921
    orpd    %xmm0, %xmm10    # tmp924, tmp921
    ucomisd    %xmm10, %xmm10    # tmp921, tmp921
    setp    %cl    #,
    orl    336(%rsp), %eax    # %sfp, pretmp.165
    movl    %ecx, 364(%rsp)    #, %sfp
    movl    %eax, 336(%rsp)    # pretmp.165, %sfp
    je    L34    #,
    movapd    %xmm10, %xmm11    # tmp921, x
L34:
    movapd    %xmm3, %xmm0    # tmp1324,
    andpd    %xmm1, %xmm12    # tmp1323, D.2814
    xorl    %eax, %eax    #
    cmpltsd    %xmm12, %xmm0    #, D.2814,
    movapd    %xmm1, %xmm10    # tmp1323, tmp1385
    movl    12(%rsp), %ecx    # %sfp, D.3602
    movapd    %xmm0, %xmm12    #, tmp933
    andpd    %xmm0, %xmm7    # tmp933, tmp934
    andnpd    %xmm2, %xmm12    # x, x
    orpd    %xmm7, %xmm12    # tmp934, x
    ucomisd    %xmm12, %xmm11    # x, x
    seta    %al    #,
    movl    %eax, 360(%rsp)    #, %sfp
    movl    $1, %eax    #, prephitmp.161
    jmp    L42    #
    .align 4,0x90
L255:
    movsd    -10560(%rdx), %xmm0    # MEM[base: D.3603_170, offset: -10560B], x
    divsd    %xmm7, %xmm0    # D.2831, x
    andpd    %xmm10, %xmm0    # tmp1385, x
    mulsd    %xmm8, %xmm0    # sens, x
L37:
    ucomisd    %xmm0, %xmm6    # x, x
    ja    L160    #,
    ucomisd    %xmm6, %xmm6    # x, x
    jnp    L39    #,
L160:
    movapd    %xmm0, %xmm6    # x, x
L39:
    addl    $1, %eax    #, prephitmp.161
    addq    $176, %rdx    #, ivtmp.264
    cmpl    %ecx, %eax    # D.3602, prephitmp.161
    je    L253    #,
L42:
    cmpl    %esi, %r10d    # pretmp.160, prephitmp.132
    je    L254    #,
L35:
    movsd    (%rdx), %xmm7    # MEM[base: D.3603_170, offset: 0B], D.2831
    movapd    %xmm7, %xmm0    # D.2831, D.2832
    andpd    %xmm1, %xmm0    # tmp1323, D.2832
    ucomisd    %xmm3, %xmm0    # tmp1324, D.2832
    ja    L255    #,
    movapd    %xmm2, %xmm0    # x, x
    jmp    L37    #
    .align 4,0x90
L254:
    cmpl    %eax, %r14d    # prephitmp.161, D.2806
    jne    L35    #,
    cmpl    $0, 336(%rsp)    #, %sfp
    je    L238    #,
    cmpl    $0, 364(%rsp)    #, %sfp
    jne    L158    #,
L238:
    cmpl    $0, 360(%rsp)    #, %sfp
    movapd    %xmm11, %xmm0    # x, x
    je    L37    #,
L158:
    movapd    %xmm12, %xmm0    # x, x
    jmp    L37    #
    .align 4,0x90
L253:
    movl    %eax, 396(%rsp)    # prephitmp.161, j
    movsd    %xmm6, 944(%r11)    # x, aaa11.dttemp

Reply via email to