From patchwork Thu Sep 12 12:47:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1161568 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-508960-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="qq+mvUP+"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46Tdpn04xtz9sCJ for ; Thu, 12 Sep 2019 22:47:59 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=bHtZe5Kk/Je5ARzUW6RxAeu8CNgfcdSdHWZzQpg8a9ZB5pbzUxaKQ i/ZAiW8T8A35WJGDvS+HEa7vcM0A83rEQE+2UtGWnfEZYPrnZQ4ouVYE64XcHdzl flox4ggORugmOkuln65XiD75C60lD2+5Y2zkKbFc14a5caieFheOi0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=wAILlywILrqGbNHUKJrlg8ebfkM=; b=qq+mvUP+y7O3Ut5mv05F DjEwrXcQSKjBQAl1asZ/tA4TJU4QYzK4CaRI22LOw+b2asxSSMNdOJ0ldER+2ntA iBQ9ifyLHcHRLBqaTDb+2uKYFPDdiwnLs7pvG4WHbrox2Ku0XNtRSS6KyzbFWwAZ KZfyNw3V9g15TFCN/49wcy8= Received: (qmail 12444 invoked by alias); 12 Sep 2019 12:47:51 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 12436 invoked by uid 89); 12 Sep 2019 12:47:51 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-12.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_NUMSUBJECT, SPF_PASS autolearn=ham version=3.3.1 spammy=integral_type_p, INTEGRAL_TYPE_P, vectype, GSI_SAME_STMT X-HELO: mx1.suse.de Received: from mx2.suse.de (HELO mx1.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 12 Sep 2019 12:47:49 +0000 Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id C7A0AAE4B for ; Thu, 12 Sep 2019 12:47:46 +0000 (UTC) Date: Thu, 12 Sep 2019 14:47:46 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix PR91750 Message-ID: User-Agent: Alpine 2.21 (LSU 202 2017-01-01) MIME-Version: 1.0 The following fixes induction vectorization to use the appropriate type for the IV init and update computations avoiding to create undefined behavior from inappropriately using a signed integer type. Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk. Richard. 2019-09-12 Richard Biener PR tree-optimization/91750 * tree-vect-loop.c (vectorizable_induction): Compute IV increments in the type of the evolution. * gcc.dg/vect/pr91750.c: New testcase. Index: gcc/tree-vect-loop.c =================================================================== --- gcc/tree-vect-loop.c (revision 275681) +++ gcc/tree-vect-loop.c (working copy) @@ -7605,6 +7605,7 @@ vectorizable_induction (stmt_vec_info st step_expr = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info); gcc_assert (step_expr != NULL_TREE); + tree step_vectype = get_same_sized_vectype (TREE_TYPE (step_expr), vectype); pe = loop_preheader_edge (iv_loop); init_expr = PHI_ARG_DEF_FROM_EDGE (phi, @@ -7613,8 +7614,8 @@ vectorizable_induction (stmt_vec_info st stmts = NULL; if (!nested_in_vect_loop) { - /* Convert the initial value to the desired type. */ - tree new_type = TREE_TYPE (vectype); + /* Convert the initial value to the IV update type. */ + tree new_type = TREE_TYPE (step_expr); init_expr = gimple_convert (&stmts, new_type, init_expr); /* If we are using the loop mask to "peel" for alignment then we need @@ -7634,9 +7635,6 @@ vectorizable_induction (stmt_vec_info st } } - /* Convert the step to the desired type. */ - step_expr = gimple_convert (&stmts, TREE_TYPE (vectype), step_expr); - if (stmts) { new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts); @@ -7669,8 +7667,8 @@ vectorizable_induction (stmt_vec_info st if (! CONSTANT_CLASS_P (new_name)) new_name = vect_init_vector (stmt_info, new_name, TREE_TYPE (step_expr), NULL); - new_vec = build_vector_from_val (vectype, new_name); - vec_step = vect_init_vector (stmt_info, new_vec, vectype, NULL); + new_vec = build_vector_from_val (step_vectype, new_name); + vec_step = vect_init_vector (stmt_info, new_vec, step_vectype, NULL); /* Now generate the IVs. */ unsigned group_size = SLP_TREE_SCALAR_STMTS (slp_node).length (); @@ -7683,7 +7681,7 @@ vectorizable_induction (stmt_vec_info st unsigned ivn; for (ivn = 0; ivn < nivs; ++ivn) { - tree_vector_builder elts (vectype, const_nunits, 1); + tree_vector_builder elts (step_vectype, const_nunits, 1); stmts = NULL; for (unsigned eltn = 0; eltn < const_nunits; ++eltn) { @@ -7694,6 +7692,7 @@ vectorizable_induction (stmt_vec_info st elts.quick_push (elt); } vec_init = gimple_build_vector (&stmts, &elts); + vec_init = gimple_convert (&stmts, vectype, vec_init); if (stmts) { new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts); @@ -7708,10 +7707,13 @@ vectorizable_induction (stmt_vec_info st induc_def = PHI_RESULT (induction_phi); /* Create the iv update inside the loop */ - vec_def = make_ssa_name (vec_dest); - new_stmt = gimple_build_assign (vec_def, PLUS_EXPR, induc_def, vec_step); - gsi_insert_before (&si, new_stmt, GSI_SAME_STMT); - loop_vinfo->add_stmt (new_stmt); + gimple_seq stmts = NULL; + vec_def = gimple_convert (&stmts, step_vectype, induc_def); + vec_def = gimple_build (&stmts, + PLUS_EXPR, step_vectype, vec_def, vec_step); + vec_def = gimple_convert (&stmts, vectype, vec_def); + loop_vinfo->add_stmt (SSA_NAME_DEF_STMT (vec_def)); + gsi_insert_seq_before (&si, stmts, GSI_SAME_STMT); /* Set the arguments of the phi node: */ add_phi_arg (induction_phi, vec_init, pe, UNKNOWN_LOCATION); @@ -7739,8 +7741,8 @@ vectorizable_induction (stmt_vec_info st if (! CONSTANT_CLASS_P (new_name)) new_name = vect_init_vector (stmt_info, new_name, TREE_TYPE (step_expr), NULL); - new_vec = build_vector_from_val (vectype, new_name); - vec_step = vect_init_vector (stmt_info, new_vec, vectype, NULL); + new_vec = build_vector_from_val (step_vectype, new_name); + vec_step = vect_init_vector (stmt_info, new_vec, step_vectype, NULL); for (; ivn < nvects; ++ivn) { gimple *iv = SLP_TREE_VEC_STMTS (slp_node)[ivn - nivs]->stmt; @@ -7749,18 +7751,20 @@ vectorizable_induction (stmt_vec_info st def = gimple_phi_result (iv); else def = gimple_assign_lhs (iv); - new_stmt = gimple_build_assign (make_ssa_name (vectype), - PLUS_EXPR, - def, vec_step); + gimple_seq stmts = NULL; + def = gimple_convert (&stmts, step_vectype, def); + def = gimple_build (&stmts, + PLUS_EXPR, step_vectype, def, vec_step); + def = gimple_convert (&stmts, vectype, def); if (gimple_code (iv) == GIMPLE_PHI) - gsi_insert_before (&si, new_stmt, GSI_SAME_STMT); + gsi_insert_seq_before (&si, stmts, GSI_SAME_STMT); else { gimple_stmt_iterator tgsi = gsi_for_stmt (iv); - gsi_insert_after (&tgsi, new_stmt, GSI_CONTINUE_LINKING); + gsi_insert_seq_after (&tgsi, stmts, GSI_CONTINUE_LINKING); } SLP_TREE_VEC_STMTS (slp_node).quick_push - (loop_vinfo->add_stmt (new_stmt)); + (loop_vinfo->add_stmt (SSA_NAME_DEF_STMT (def))); } } @@ -7796,12 +7800,12 @@ vectorizable_induction (stmt_vec_info st /* iv_loop is the loop to be vectorized. Create: vec_init = [X, X+S, X+2*S, X+3*S] (S = step_expr, X = init_expr) */ stmts = NULL; - new_name = gimple_convert (&stmts, TREE_TYPE (vectype), init_expr); + new_name = gimple_convert (&stmts, TREE_TYPE (step_expr), init_expr); unsigned HOST_WIDE_INT const_nunits; if (nunits.is_constant (&const_nunits)) { - tree_vector_builder elts (vectype, const_nunits, 1); + tree_vector_builder elts (step_vectype, const_nunits, 1); elts.quick_push (new_name); for (i = 1; i < const_nunits; i++) { @@ -7816,7 +7820,7 @@ vectorizable_induction (stmt_vec_info st } else if (INTEGRAL_TYPE_P (TREE_TYPE (step_expr))) /* Build the initial value directly from a VEC_SERIES_EXPR. */ - vec_init = gimple_build (&stmts, VEC_SERIES_EXPR, vectype, + vec_init = gimple_build (&stmts, VEC_SERIES_EXPR, step_vectype, new_name, step_expr); else { @@ -7825,17 +7829,18 @@ vectorizable_induction (stmt_vec_info st + (vectype) [0, 1, 2, ...] * [step, step, step, ...]. */ gcc_assert (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr))); gcc_assert (flag_associative_math); - tree index = build_index_vector (vectype, 0, 1); - tree base_vec = gimple_build_vector_from_val (&stmts, vectype, + tree index = build_index_vector (step_vectype, 0, 1); + tree base_vec = gimple_build_vector_from_val (&stmts, step_vectype, new_name); - tree step_vec = gimple_build_vector_from_val (&stmts, vectype, + tree step_vec = gimple_build_vector_from_val (&stmts, step_vectype, step_expr); - vec_init = gimple_build (&stmts, FLOAT_EXPR, vectype, index); - vec_init = gimple_build (&stmts, MULT_EXPR, vectype, + vec_init = gimple_build (&stmts, FLOAT_EXPR, step_vectype, index); + vec_init = gimple_build (&stmts, MULT_EXPR, step_vectype, vec_init, step_vec); - vec_init = gimple_build (&stmts, PLUS_EXPR, vectype, + vec_init = gimple_build (&stmts, PLUS_EXPR, step_vectype, vec_init, base_vec); } + vec_init = gimple_convert (&stmts, vectype, vec_init); if (stmts) { @@ -7874,8 +7879,8 @@ vectorizable_induction (stmt_vec_info st t = unshare_expr (new_name); gcc_assert (CONSTANT_CLASS_P (new_name) || TREE_CODE (new_name) == SSA_NAME); - new_vec = build_vector_from_val (vectype, t); - vec_step = vect_init_vector (stmt_info, new_vec, vectype, NULL); + new_vec = build_vector_from_val (step_vectype, t); + vec_step = vect_init_vector (stmt_info, new_vec, step_vectype, NULL); /* Create the following def-use cycle: @@ -7896,9 +7901,12 @@ vectorizable_induction (stmt_vec_info st induc_def = PHI_RESULT (induction_phi); /* Create the iv update inside the loop */ - vec_def = make_ssa_name (vec_dest); - new_stmt = gimple_build_assign (vec_def, PLUS_EXPR, induc_def, vec_step); - gsi_insert_before (&si, new_stmt, GSI_SAME_STMT); + stmts = NULL; + vec_def = gimple_convert (&stmts, step_vectype, induc_def); + vec_def = gimple_build (&stmts, PLUS_EXPR, step_vectype, vec_def, vec_step); + vec_def = gimple_convert (&stmts, vectype, vec_def); + gsi_insert_seq_before (&si, stmts, GSI_SAME_STMT); + new_stmt = SSA_NAME_DEF_STMT (vec_def); stmt_vec_info new_stmt_info = loop_vinfo->add_stmt (new_stmt); /* Set the arguments of the phi node: */ @@ -7940,20 +7948,22 @@ vectorizable_induction (stmt_vec_info st t = unshare_expr (new_name); gcc_assert (CONSTANT_CLASS_P (new_name) || TREE_CODE (new_name) == SSA_NAME); - new_vec = build_vector_from_val (vectype, t); - vec_step = vect_init_vector (stmt_info, new_vec, vectype, NULL); + new_vec = build_vector_from_val (step_vectype, t); + vec_step = vect_init_vector (stmt_info, new_vec, step_vectype, NULL); vec_def = induc_def; prev_stmt_vinfo = induction_phi_info; for (i = 1; i < ncopies; i++) { /* vec_i = vec_prev + vec_step */ - new_stmt = gimple_build_assign (vec_dest, PLUS_EXPR, - vec_def, vec_step); - vec_def = make_ssa_name (vec_dest, new_stmt); - gimple_assign_set_lhs (new_stmt, vec_def); + gimple_seq stmts = NULL; + vec_def = gimple_convert (&stmts, step_vectype, vec_def); + vec_def = gimple_build (&stmts, + PLUS_EXPR, step_vectype, vec_def, vec_step); + vec_def = gimple_convert (&stmts, vectype, vec_def); - gsi_insert_before (&si, new_stmt, GSI_SAME_STMT); + gsi_insert_seq_before (&si, stmts, GSI_SAME_STMT); + new_stmt = SSA_NAME_DEF_STMT (vec_def); new_stmt_info = loop_vinfo->add_stmt (new_stmt); STMT_VINFO_RELATED_STMT (prev_stmt_vinfo) = new_stmt_info; prev_stmt_vinfo = new_stmt_info; Index: gcc/testsuite/gcc.dg/vect/pr91750.c =================================================================== --- gcc/testsuite/gcc.dg/vect/pr91750.c (nonexistent) +++ gcc/testsuite/gcc.dg/vect/pr91750.c (working copy) @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target vect_int } */ + +int val[1024]; +void +foo (int n) +{ + int i; + for (int j = 0, i = n; j < 1024; ++j, i=(unsigned)i+1) + val[j] = i; +} + +/* Make sure the induction IV uses an unsigned increment. */ +/* { dg-final { scan-tree-dump "vector\\\(\[0-9\]*\\\) unsigned int" "vect" } } */ +/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */