1 /* Plugin that process internal tests for sreal.  */
2 #include "config.h"
3 #include "gcc-plugin.h"
4 #include "system.h"
5 #include "coretypes.h"
6 #include "tree.h"
7 #include "tm.h"
8 #include "toplev.h"
9 #include "hash-table.h"
10 #include "vec.h"
11 #include "ggc.h"
12 #include "basic-block.h"
13 #include "tree-ssa-alias.h"
14 #include "internal-fn.h"
15 #include "gimple-fold.h"
16 #include "tree-eh.h"
17 #include "gimple-expr.h"
18 #include "is-a.h"
19 #include "gimple.h"
20 #include "tree-pass.h"
21 #include "intl.h"
22 #include "context.h"
23 #include "sreal.h"
24 
25 int plugin_is_GPL_compatible;
26 
27 namespace {
28 
assert(bool c)29 static void assert (bool c)
30 {
31   if (!c)
32     abort ();
33 }
34 
35 const pass_data pass_data_sreal_pass =
36 {
37   GIMPLE_PASS, /* type */
38   "sreal", /* name */
39   OPTGROUP_NONE, /* optinfo_flags */
40   TV_NONE, /* tv_id */
41   PROP_gimple_any, /* properties_required */
42   0, /* properties_provided */
43   0, /* properties_destroyed */
44   0, /* todo_flags_start */
45   0, /* todo_flags_finish */
46 };
47 
48 class sreal_pass : public gimple_opt_pass
49 {
50 public:
sreal_pass(gcc::context * ctxt)51   sreal_pass(gcc::context *ctxt)
52     : gimple_opt_pass(pass_data_sreal_pass, ctxt)
53   {}
54 
55   /* opt_pass methods: */
56   virtual bool gate (function *);
57   virtual unsigned int execute (function *);
58 
59 private:
60   void check_sreal ();
61 
62   static void verify_aritmetics (int a, int b);
63   static void verify_shifting (int a);
64 }; // class one_pass
65 
66 } // anon namespace
67 
68 void
verify_aritmetics(int a,int b)69 sreal_pass::verify_aritmetics (int a, int b)
70 {
71   assert (a == -(-(sreal (a))).to_int ());
72   assert ((a < b) == (sreal (a) < sreal (b)));
73   assert ((a <= b) == (sreal (a) <= sreal (b)));
74   assert ((a == b) == (sreal (a) == sreal (b)));
75   assert ((a != b) == (sreal (a) != sreal (b)));
76   assert ((a > b) == (sreal (a) > sreal (b)));
77   assert ((a >= b) == (sreal (a) >= sreal (b)));
78   assert ((a + b) == (sreal (a) + sreal (b)).to_int ());
79   assert ((a - b) == (sreal (a) - sreal (b)).to_int ());
80   assert ((b + a) == (sreal (b) + sreal (a)).to_int ());
81   assert ((b - a) == (sreal (b) - sreal (a)).to_int ());
82 }
83 
84 void
verify_shifting(int a)85 sreal_pass::verify_shifting (int a)
86 {
87   sreal v = a;
88 
89   for (unsigned i = 0; i < 16; i++)
90     assert ((a << i) == (v << i).to_int());
91 
92   a = a << 16;
93   v = v << 16;
94 
95   for (unsigned i = 0; i < 16; i++)
96     assert ((a >> i) == (v >> i).to_int());
97 }
98 
99 void
check_sreal()100 sreal_pass::check_sreal ()
101 {
102   sreal minimum = INT_MIN;
103   sreal maximum = INT_MAX;
104   sreal seven = 7;
105   sreal minus_two = -2;
106   sreal minus_nine = -9;
107 
108   assert (minimum.to_int () == INT_MIN);
109   assert (maximum.to_int () == INT_MAX);
110 
111   assert (!(minus_two < minus_two));
112   assert (!(seven < seven));
113   assert (seven > minus_two);
114   assert (minus_two < seven);
115   assert (minus_two != seven);
116   assert (minus_two == minus_two);
117   assert (seven == seven);
118 
119   assert (seven == ((seven << 10) >> 10));
120 
121   assert ((seven + minus_two) == 5);
122   assert ((seven + minus_nine) == -2);
123 
124   for (int a = -100; a < 100; a++)
125     for (int b = -100; b < 100; b++)
126       {
127         verify_aritmetics (a, b);
128         verify_aritmetics (INT_MIN + 100, b);
129         verify_aritmetics (INT_MAX - 100, b);
130       }
131 
132   srand (123456);
133 
134   for (int i = 0; i < 1000 * 1000; i++)
135     {
136       verify_aritmetics (rand () % 10, rand () % 1000000);
137       verify_aritmetics (rand () % 100, rand () % 10000);
138     }
139 
140   for (int a = -100; a < 100; a++)
141     verify_shifting (a);
142 }
143 
gate(function *)144 bool sreal_pass::gate (function *)
145 {
146   return true;
147 }
148 
149 unsigned int
execute(function *)150 sreal_pass::execute (function *)
151 {
152   check_sreal ();
153 
154   return 0;
155 }
156 
plugin_init(struct plugin_name_args * plugin_info,struct plugin_gcc_version * version)157 int plugin_init (struct plugin_name_args *plugin_info,
158                  struct plugin_gcc_version *version)
159 {
160   struct register_pass_info p;
161 
162   p.pass = new sreal_pass (g);
163   p.reference_pass_name = "cfg";
164   p.ref_pass_instance_number = 1;
165   p.pos_op = PASS_POS_INSERT_AFTER;
166 
167   register_callback ("sreal", PLUGIN_PASS_MANAGER_SETUP, NULL, &p);
168 
169   return 0;
170 }
171