diff mbox

[GIMPLE,FE] Avoid ICE with __builtin_abs

Message ID alpine.LSU.2.20.1702061625250.15777@zhemvz.fhfr.qr
State New
Headers show

Commit Message

Richard Biener Feb. 6, 2017, 3:26 p.m. UTC
On Mon, 6 Feb 2017, Richard Biener wrote:

> On Sat, 4 Feb 2017, Prathamesh Kulkarni wrote:
> 
> > Hi,
> > The following test-case ICE's with -fgimple:
> > 
> > int __GIMPLE foo(int a)
> > {
> >   int t1;
> >   t1_1 = __builtin_abs (a);
> >   return t1_1;
> > }
> > 
> > gimplefe-2.c:4:3: internal compiler error: in get_callee_fndecl, at tree.c:9500
> >    t1_1 = __builtin_abs (a);
> >    ^~~~
> > 0xe96e8d get_callee_fndecl(tree_node const*)
> > ../../gcc/gcc/tree.c:9500
> > 0x924d75 gimple_build_call_from_tree(tree_node*)
> > ../../gcc/gcc/gimple.c:351
> > 0x6c86b3 c_parser_gimple_statement
> > ../../gcc/gcc/c/gimple-parser.c:393
> > 0x6c86b3 c_parser_gimple_compound_statement
> > ../../gcc/gcc/c/gimple-parser.c:216
> > 0x6c86b3 c_parser_parse_gimple_body(c_parser*)
> > ../../gcc/gcc/c/gimple-parser.c:93
> > 0x6b04f1 c_parser_declaration_or_fndef
> > ../../gcc/gcc/c/c-parser.c:2081
> > 0x6b883b c_parser_external_declaration
> > ../../gcc/gcc/c/c-parser.c:1464
> > 0x6b92a1 c_parser_translation_unit
> > ../../gcc/gcc/c/c-parser.c:1344
> > 0x6b92a1 c_parse_file()
> > ../../gcc/gcc/c/c-parser.c:18141
> > 0x717832 c_common_parse_file()
> > ../../gcc/gcc/c-family/c-opts.c:1102
> > 
> > This happens because __builtin_abs(a) gets folded to <nop_expr<abs_expr<a>>
> > and get_callee_fndecl expects CALL_EXPR.
> > 
> > The attached patch tries to fix the issue by building gimple_assign
> > with appropriate subcode
> > for functions that get folded to expression instead of trying to build
> > it as a function-call.
> > Is it OK to commit after bootstrap+test ?
> 
> No.  The proper fix is to not use the C frontend call-expr parsing
> and building -- it does have many more issues I think.

Sth as simple as


for example fixes the bogus promotions inserted for

short int __GIMPLE ()
foo (short int s)
{
  short int D_1803;

  bb_2:
  D_1803 = s;

L0:
  return D_1803;

}


int __GIMPLE ()
main (int argc, char * * argv)
{
  short int s;
  int D_1805;
  int _1;
  short _2;

  bb_2:
  s = (short int) argc;
  _1 = (int) s;
  _2 = foo (_1);
  D_1805 = (int) _2;

L0:
  return D_1805;

}

it should also fix the folding you see.  Otherwise untested, of course
(and we shouldn't build a CALL_EXPR but instead refactor this so we
can build a GIMPLE_CALL directly)

Richard.
diff mbox

Patch

Index: gcc/c/gimple-parser.c
===================================================================
--- gcc/c/gimple-parser.c	(revision 245203)
+++ gcc/c/gimple-parser.c	(working copy)
@@ -946,17 +946,11 @@ 
 	    orig_expr = expr;
 	    start = expr.get_start ();
 	    finish = c_parser_tokens_buf (parser, 0)->get_finish ();
-	    expr.value = c_build_function_call_vec (expr_loc, arg_loc,
-						    expr.value,
-						    exprlist, origtypes);
+	    expr.value = build_call_vec (TREE_TYPE (TREE_TYPE (expr.value)),
+					 expr.value, exprlist);
+	    SET_EXPR_LOCATION (expr.value, expr_loc);
 	    set_c_expr_source_range (&expr, start, finish);
-
 	    expr.original_code = ERROR_MARK;
-	    if (TREE_CODE (expr.value) == INTEGER_CST
-		&& TREE_CODE (orig_expr.value) == FUNCTION_DECL
-		&& DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
-		&& DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
-	      expr.original_code = C_MAYBE_CONST_EXPR;
 	    expr.original_type = NULL;
 	    if (exprlist)
 	      {