summaryrefslogtreecommitdiff
path: root/gcc/gimple-rewriter.c
blob: 7518844d658f117a21a095bf556d478f5fd2107f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tree.h"
#include "options.h"
#include "cgraph.h"
#include "tree-pass.h"
#include "tree-cfg.h"
#include "tree-pretty-print.h"
#include "gimple-pretty-print.h"
#include "stringpool.h"  //get_identifier
#include "basic-block.h" //needed for gimple.h
#include "function.h"    //needed for gimple.h
#include "gimple.h"
#include "cfg.h" // needed for gimple-iterator.h
#include "gimple-iterator.h"
#include "stor-layout.h"   // layout_type
#include "fold-const.h"    //build_fold_addr_expr
#include "gimple-ssa.h"    // update_stmt
#include "attribs.h"       // decl_attributes
#include "gimplify.h"      //unshare_expr
#include "value-range.h"   // make_ssa_name dependency
#include "tree-ssanames.h" // make_ssa_name
#include "ssa.h"
#include "tree-into-ssa.h"
#include "gimple-rewriter.hpp"

void
GimpleTypeRewriter::_walk_pre(const_tree e)
{
  // This is for local variables
  // and other declarations
  exprTypeRewriter.walk(e);
  const bool is_interesting = exprTypeRewriter.is_interesting_type(TREE_TYPE(e));
  const bool is_var_decl = TREE_CODE(e) == VAR_DECL;
  const bool is_valid = is_interesting && is_var_decl;
  if (!is_valid) return;
  relayout_decl((tree)e);
}

void
GimpleTypeRewriter::_walk_pre(gimple *s)
{
}

void
GimpleTypeRewriter::_walk_pre(gcall *s)
{
}

void
GimpleTypeRewriter::_walk_pre(greturn *s)
{
}

void
GimpleTypeRewriter::handle_pointer_arithmetic(gimple *s)
{
  const enum tree_code p = POINTER_PLUS_EXPR;
  const enum tree_code d = POINTER_DIFF_EXPR;
  const enum tree_code e = gimple_expr_code(s);
  const bool is_pointer_plus = p == e;
  const bool is_pointer_diff = d == e;
  bool is_valid_input = is_pointer_plus != is_pointer_diff;
  gcc_assert(is_valid_input);
  // TODO: Implement pointer diff
  
  const enum gimple_rhs_class rhs_class = gimple_assign_rhs_class(s);
  is_valid_input = GIMPLE_BINARY_RHS == rhs_class;
  gcc_assert(is_valid_input);

  tree op_0 = gimple_assign_rhs1(s);
  tree op_1 = gimple_assign_rhs2(s);
  tree op_0_t = TREE_TYPE(op_0);
  tree op_1_t = TREE_TYPE(op_1);
  const bool is_op_0_t_interesting = exprTypeRewriter.is_interesting_type(op_0_t);
  const bool is_op_1_t_interesting = exprTypeRewriter.is_interesting_type(op_1_t);
  bool is_interesting_case = is_op_0_t_interesting || is_op_1_t_interesting;
  if (!is_interesting_case) return;

  const enum tree_code op_1_code = TREE_CODE(op_1);
  const enum tree_code op_0_code = TREE_CODE(op_0);
  const bool is_op_0_icst = INTEGER_CST == op_0_code;
  const bool is_op_1_icst = INTEGER_CST == op_1_code;
  const bool is_constant_case = is_op_0_icst != is_op_1_icst;
  if (!is_constant_case)
  {
    exprTypeRewriter.handle_pointer_arithmetic_nonconstant(s, op_0, op_1, is_pointer_plus);
    return;
  }

  tree integer_constant = is_op_0_icst ? op_0 : op_1;
  tree maybe_pointer = is_op_0_icst ? op_1 : op_0;
  const_tree maybe_pointer_t = TREE_TYPE(maybe_pointer);
  assert_is_type(maybe_pointer_t, POINTER_TYPE);
  tree pointer_variable = maybe_pointer;

  exprTypeRewriter.handle_pointer_arithmetic_constants(s, pointer_variable, integer_constant, is_pointer_plus);
}


void
GimpleTypeRewriter::_walk_pre(gassign *s)
{
  const enum gimple_rhs_class code = gimple_assign_rhs_class(s);

  switch (code)
  {
    case GIMPLE_TERNARY_RHS:
    {
      const_tree rhs3 = gimple_assign_rhs3(s);
      exprTypeRewriter.walk(rhs3);
    }
    /* fall-through */
    case GIMPLE_BINARY_RHS:
    {
      const_tree rhs2 = gimple_assign_rhs2(s);
      exprTypeRewriter.walk(rhs2);
    }
    /* fall-through */
    case GIMPLE_UNARY_RHS:
    case GIMPLE_SINGLE_RHS:
    {
      const_tree rhs1 = gimple_assign_rhs1(s);
      exprTypeRewriter.walk(rhs1);
      const_tree lhs = gimple_assign_lhs(s);
      if (!lhs) break;
      exprTypeRewriter.walk(lhs);
    }
    break;
    default:
    gcc_unreachable();
    break;
  }

  const enum tree_code e_code = gimple_expr_code(s);
  switch (e_code)
  {
    case POINTER_PLUS_EXPR:
    case POINTER_DIFF_EXPR:
	    handle_pointer_arithmetic(s);
    break;
    default:
    break;
  }

}

void
GimpleTypeRewriter::_walk_pre(gcond *s)
{
}