some simple optimizations for execution speed
[libfirm] / ir / ir / irop.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irop.c
4  * Purpose:     Representation of opcode of intermediate operation.
5  * Author:      Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 # include <string.h>
18
19 # include "irop_t.h"
20 # include "irnode_t.h"
21
22 # include "xmalloc.h"
23
24 ir_op *op_Block;           ir_op *get_op_Block     () { return op_Block;     }
25
26 ir_op *op_Start;           ir_op *get_op_Start     () { return op_Start;     }
27 ir_op *op_End;             ir_op *get_op_End       () { return op_End;       }
28 ir_op *op_Jmp;             ir_op *get_op_Jmp       () { return op_Jmp;       }
29 ir_op *op_Cond;            ir_op *get_op_Cond      () { return op_Cond;      }
30 ir_op *op_Return;          ir_op *get_op_Return    () { return op_Return;    }
31 ir_op *op_Raise;           ir_op *get_op_Raise     () { return op_Raise;     }
32
33 ir_op *op_Sel;             ir_op *get_op_Sel       () { return op_Sel;       }
34 ir_op *op_InstOf;          ir_op *get_op_InstOf    () { return op_InstOf;    }
35
36 ir_op *op_Const;           ir_op *get_op_Const     () { return op_Const;     }
37 ir_op *op_SymConst;        ir_op *get_op_SymConst  () { return op_SymConst;  }
38
39 ir_op *op_Call;            ir_op *get_op_Call      () { return op_Call;      }
40 ir_op *op_Add;             ir_op *get_op_Add       () { return op_Add;       }
41 ir_op *op_Sub;             ir_op *get_op_Sub       () { return op_Sub;       }
42 ir_op *op_Minus;           ir_op *get_op_Minus     () { return op_Minus;     }
43 ir_op *op_Mul;             ir_op *get_op_Mul       () { return op_Mul;       }
44 ir_op *op_Quot;            ir_op *get_op_Quot      () { return op_Quot;      }
45 ir_op *op_DivMod;          ir_op *get_op_DivMod    () { return op_DivMod;    }
46 ir_op *op_Div;             ir_op *get_op_Div       () { return op_Div;       }
47 ir_op *op_Mod;             ir_op *get_op_Mod       () { return op_Mod;       }
48 ir_op *op_Abs;             ir_op *get_op_Abs       () { return op_Abs;       }
49 ir_op *op_And;             ir_op *get_op_And       () { return op_And;       }
50 ir_op *op_Or;              ir_op *get_op_Or        () { return op_Or;        }
51 ir_op *op_Eor;             ir_op *get_op_Eor       () { return op_Eor;       }
52 ir_op *op_Not;             ir_op *get_op_Not       () { return op_Not;       }
53 ir_op *op_Cmp;             ir_op *get_op_Cmp       () { return op_Cmp;       }
54 ir_op *op_Shl;             ir_op *get_op_Shl       () { return op_Shl;       }
55 ir_op *op_Shr;             ir_op *get_op_Shr       () { return op_Shr;       }
56 ir_op *op_Shrs;            ir_op *get_op_Shrs      () { return op_Shrs;      }
57 ir_op *op_Rot;             ir_op *get_op_Rot       () { return op_Rot;       }
58 ir_op *op_Conv;            ir_op *get_op_Conv      () { return op_Conv;      }
59 ir_op *op_Cast;            ir_op *get_op_Cast      () { return op_Cast;      }
60
61 ir_op *op_Phi;             ir_op *get_op_Phi       () { return op_Phi;       }
62
63 ir_op *op_Load;            ir_op *get_op_Load      () { return op_Load;      }
64 ir_op *op_Store;           ir_op *get_op_Store     () { return op_Store;     }
65 ir_op *op_Alloc;           ir_op *get_op_Alloc     () { return op_Alloc;     }
66 ir_op *op_Free;            ir_op *get_op_Free      () { return op_Free;      }
67 ir_op *op_Sync;            ir_op *get_op_Sync      () { return op_Sync;      }
68
69 ir_op *op_Tuple;           ir_op *get_op_Tuple     () { return op_Tuple;     }
70 ir_op *op_Proj;            ir_op *get_op_Proj      () { return op_Proj;      }
71 ir_op *op_Id;              ir_op *get_op_Id        () { return op_Id;        }
72 ir_op *op_Bad;             ir_op *get_op_Bad       () { return op_Bad;       }
73 ir_op *op_Confirm;         ir_op *get_op_Confirm   () { return op_Confirm;   }
74
75 ir_op *op_Unknown;         ir_op *get_op_Unknown   () { return op_Unknown;   }
76 ir_op *op_Filter;          ir_op *get_op_Filter    () { return op_Filter;    }
77 ir_op *op_Break;           ir_op *get_op_Break     () { return op_Break;     }
78 ir_op *op_CallBegin;       ir_op *get_op_CallBegin () { return op_CallBegin; }
79 ir_op *op_EndReg;          ir_op *get_op_EndReg    () { return op_EndReg;    }
80 ir_op *op_EndExcept;       ir_op *get_op_EndExcept () { return op_EndExcept; }
81
82 ir_op *op_FuncCall;        ir_op *get_op_FuncCall  () { return op_FuncCall; }
83
84
85 ir_op *
86 new_ir_op (opcode code, const char *name, op_pinned p, int labeled, op_arity opar, size_t attr_size)
87 {
88   ir_op *res;
89
90   res = (ir_op *) xmalloc (sizeof (ir_op));
91   res->code = code;
92   res->name = id_from_str(name, strlen(name));
93   res->pinned = p;
94   res->attr_size = attr_size;
95   res->labeled = labeled;   /* For vcg dumping.
96                                Set labeled = 1 if the edges should be
97                                enumarated in vcg output, otherwise set
98                                labeled = 0. */
99   res->opar = opar;
100   return res;
101 }
102
103 void free_ir_op (ir_op *code) {
104   free(code);
105 }
106
107 void
108 init_op(void)
109 {
110   op_Block     = new_ir_op (iro_Block,     "Block",     pinned, 1, oparity_variable, sizeof (block_attr));
111
112   op_Start     = new_ir_op (iro_Start,     "Start",     pinned, 0, oparity_zero,     sizeof (start_attr));
113   op_End       = new_ir_op (iro_End,       "End",       pinned, 0, oparity_dynamic,  0);
114   op_Jmp       = new_ir_op (iro_Jmp,       "Jmp",       pinned, 0, oparity_zero,     0);
115   op_Cond      = new_ir_op (iro_Cond,      "Cond",      pinned, 1, oparity_any,      sizeof(cond_attr));
116   op_Return    = new_ir_op (iro_Return,    "Return",    pinned, 1, oparity_zero,     0);
117   op_Raise     = new_ir_op (iro_Raise,     "Raise",     pinned, 1, oparity_any,      0);
118
119   op_Const     = new_ir_op (iro_Const,     "Const",     floats, 0, oparity_zero,     sizeof (const_attr));
120   op_SymConst  = new_ir_op (iro_SymConst,  "SymConst",  floats, 0, oparity_zero,     sizeof (symconst_attr));
121
122   op_Sel       = new_ir_op (iro_Sel,       "Sel",       floats, 1, oparity_any,      sizeof (sel_attr));
123   op_InstOf    = new_ir_op (iro_InstOf,    "InstOf",    floats, 1, oparity_any,      sizeof (sel_attr));
124
125   op_Call      = new_ir_op (iro_Call,      "Call",      pinned, 1, oparity_variable, sizeof (call_attr));
126   op_Add       = new_ir_op (iro_Add,       "Add",       floats, 0, oparity_binary,   0);
127   op_Minus     = new_ir_op (iro_Minus,     "Minus",     floats, 0, oparity_unary,    0);
128   op_Sub       = new_ir_op (iro_Sub,       "Sub",       floats, 1, oparity_binary,   0);
129   op_Mul       = new_ir_op (iro_Mul,       "Mul",       floats, 0, oparity_binary,   0);
130   op_Quot      = new_ir_op (iro_Quot,      "Quot",      pinned, 1, oparity_binary,   sizeof(struct irnode **));
131   op_DivMod    = new_ir_op (iro_DivMod,    "DivMod",    pinned, 1, oparity_binary,   sizeof(struct irnode **));
132   op_Div       = new_ir_op (iro_Div,       "Div",       pinned, 1, oparity_binary,   sizeof(struct irnode **));
133   op_Mod       = new_ir_op (iro_Mod,       "Mod",       pinned, 1, oparity_binary,   sizeof(struct irnode **));
134   op_Abs       = new_ir_op (iro_Abs,       "Abs",       floats, 0, oparity_unary,    0);
135   op_And       = new_ir_op (iro_And,       "And",       floats, 0, oparity_binary,   0);
136   op_Or        = new_ir_op (iro_Or,        "Or",        floats, 0, oparity_binary,   0);
137   op_Eor       = new_ir_op (iro_Eor,       "Eor",       floats, 0, oparity_binary,   0);
138   op_Not       = new_ir_op (iro_Not,       "Not",       floats, 0, oparity_unary,    0);
139   op_Cmp       = new_ir_op (iro_Cmp,       "Cmp",       floats, 1, oparity_binary,   0);
140   op_Shl       = new_ir_op (iro_Shl,       "Shl",       floats, 1, oparity_binary,   0);
141   op_Shr       = new_ir_op (iro_Shr,       "Shr",       floats, 1, oparity_binary,   0);
142   op_Shrs      = new_ir_op (iro_Shrs,      "Shrs",      floats, 1, oparity_binary,   0);
143   op_Rot       = new_ir_op (iro_Rot,       "Rot",       floats, 1, oparity_binary,   0);
144   op_Conv      = new_ir_op (iro_Conv,      "Conv",      floats, 0, oparity_unary,    0);
145   op_Cast      = new_ir_op (iro_Cast,      "Cast",      floats, 0, oparity_unary,    sizeof (cast_attr));
146
147   op_Phi       = new_ir_op (iro_Phi,       "Phi",       pinned, 1, oparity_variable, sizeof (int));
148
149   op_Load      = new_ir_op (iro_Load,      "Load",      pinned, 1, oparity_any,      sizeof(struct irnode **));
150   op_Store     = new_ir_op (iro_Store,     "Store",     pinned, 1, oparity_any,      sizeof(struct irnode **));
151   op_Alloc     = new_ir_op (iro_Alloc,     "Alloc",     pinned, 1, oparity_any,      sizeof (alloc_attr));
152   op_Free      = new_ir_op (iro_Free,      "Free",      pinned, 1, oparity_any,      sizeof (type *));
153   op_Sync      = new_ir_op (iro_Sync,      "Sync",      pinned, 0, oparity_any,      0);
154
155   op_Proj      = new_ir_op (iro_Proj,      "Proj",      floats, 0, oparity_any,      sizeof (long));
156   op_Tuple     = new_ir_op (iro_Tuple,     "Tuple",     floats, 1, oparity_variable, 0);
157   op_Id        = new_ir_op (iro_Id,        "Id",        floats, 0, oparity_any,      0);
158   op_Bad       = new_ir_op (iro_Bad,       "Bad",       floats, 0, oparity_zero,     0);
159   op_Confirm   = new_ir_op (iro_Confirm,   "Confirm",   floats, 1, oparity_any,      sizeof (confirm_attr));
160
161   op_Unknown   = new_ir_op (iro_Unknown,   "Unknown",   floats, 0, oparity_zero,     0);
162   op_Filter    = new_ir_op (iro_Filter,    "Filter",    pinned, 1, oparity_variable, sizeof(filter_attr));
163   op_Break     = new_ir_op (iro_Break,     "Break",     pinned, 0, oparity_zero,     0);
164   op_CallBegin = new_ir_op (iro_CallBegin, "CallBegin", pinned, 0, oparity_any,      sizeof(callbegin_attr));
165   op_EndReg    = new_ir_op (iro_EndReg,    "EndReg",    pinned, 0, oparity_any,      sizeof(end_attr));
166   op_EndExcept = new_ir_op (iro_EndExcept, "EndExcept", pinned, 0, oparity_any,      sizeof(end_attr));
167
168   op_FuncCall  = new_ir_op (iro_FuncCall,  "FuncCall",  floats, 1, oparity_any,      sizeof (call_attr));
169 }
170
171
172 /* free memory used by irop module. */
173 void finish_op() {
174   free_ir_op (op_Block); op_Block     = NULL;
175
176   free_ir_op (op_Start    ); op_Start     = NULL;
177   free_ir_op (op_End      ); op_End       = NULL;
178   free_ir_op (op_Jmp      ); op_Jmp       = NULL;
179   free_ir_op (op_Cond     ); op_Cond      = NULL;
180   free_ir_op (op_Return   ); op_Return    = NULL;
181   free_ir_op (op_Raise    ); op_Raise     = NULL;
182
183   free_ir_op (op_Const    ); op_Const     = NULL;
184   free_ir_op (op_SymConst ); op_SymConst  = NULL;
185
186   free_ir_op (op_Sel      ); op_Sel       = NULL;
187   free_ir_op (op_InstOf   ); op_InstOf    = NULL;
188
189   free_ir_op (op_Call     ); op_Call      = NULL;
190   free_ir_op (op_Add      ); op_Add       = NULL;
191   free_ir_op (op_Minus    ); op_Minus     = NULL;
192   free_ir_op (op_Sub      ); op_Sub       = NULL;
193   free_ir_op (op_Mul      ); op_Mul       = NULL;
194   free_ir_op (op_Quot     ); op_Quot      = NULL;
195   free_ir_op (op_DivMod   ); op_DivMod    = NULL;
196   free_ir_op (op_Div      ); op_Div       = NULL;
197   free_ir_op (op_Mod      ); op_Mod       = NULL;
198   free_ir_op (op_Abs      ); op_Abs       = NULL;
199   free_ir_op (op_And      ); op_And       = NULL;
200   free_ir_op (op_Or       ); op_Or        = NULL;
201   free_ir_op (op_Eor      ); op_Eor       = NULL;
202   free_ir_op (op_Not      ); op_Not       = NULL;
203   free_ir_op (op_Cmp      ); op_Cmp       = NULL;
204   free_ir_op (op_Shl      ); op_Shl       = NULL;
205   free_ir_op (op_Shr      ); op_Shr       = NULL;
206   free_ir_op (op_Shrs     ); op_Shrs      = NULL;
207   free_ir_op (op_Rot      ); op_Rot       = NULL;
208   free_ir_op (op_Conv     ); op_Conv      = NULL;
209   free_ir_op (op_Cast     ); op_Cast      = NULL;
210
211   free_ir_op (op_Phi      ); op_Phi       = NULL;
212
213   free_ir_op (op_Load     ); op_Load      = NULL;
214   free_ir_op (op_Store    ); op_Store     = NULL;
215   free_ir_op (op_Alloc    ); op_Alloc     = NULL;
216   free_ir_op (op_Free     ); op_Free      = NULL;
217   free_ir_op (op_Sync     ); op_Sync      = NULL;
218
219   free_ir_op (op_Proj     ); op_Proj      = NULL;
220   free_ir_op (op_Tuple    ); op_Tuple     = NULL;
221   free_ir_op (op_Id       ); op_Id        = NULL;
222   free_ir_op (op_Bad      ); op_Bad       = NULL;
223   free_ir_op (op_Confirm  ); op_Confirm   = NULL;
224
225   free_ir_op (op_Unknown  ); op_Unknown   = NULL;
226   free_ir_op (op_Filter   ); op_Filter    = NULL;
227   free_ir_op (op_Break    ); op_Break     = NULL;
228   free_ir_op (op_CallBegin); op_CallBegin = NULL;
229   free_ir_op (op_EndReg   ); op_EndReg    = NULL;
230   free_ir_op (op_EndExcept); op_EndExcept = NULL;
231 }
232
233 /* Returns the string for the opcode. */
234 const char *get_op_name (const ir_op *op) {
235   return get_id_str(op->name);
236 }
237
238 opcode get_op_code (const ir_op *op){
239   return op->code;
240 }
241
242 ident *get_op_ident(ir_op *op){
243   return op->name;
244 }
245
246 op_pinned get_op_pinned (const ir_op *op){
247   return op->pinned;
248 }
249
250 /* Sets pinned in the opcode.  Setting it to floating has no effect
251    for Phi, Block and control flow nodes. */
252 void      set_op_pinned(ir_op *op, op_pinned pinned) {
253   if (op == op_Block || op == op_Phi || is_cfopcode(op)) return;
254   op->pinned = pinned;
255 }
256
257
258 /* returns the attribute size of the operator. */
259 int get_op_attr_size (const ir_op *op) {
260   return op->attr_size;
261 }
262
263 int is_cfopcode(const ir_op *op) {
264   return ((op == op_Start)
265           || (op == op_Jmp)
266           || (op == op_Cond)
267           || (op == op_Return)
268           || (op == op_Raise)
269           || (op == op_Bad)
270           || (op == op_End)
271           || (op == op_Unknown)
272           || (op == op_Break)
273           || (op == op_CallBegin)
274           || (op == op_EndReg)
275           || (op == op_EndExcept));
276 }
277
278 /* Returns true if the operation manipulates interprocedural control flow:
279    CallBegin, EndReg, EndExcept */
280 int is_ip_cfopcode(const ir_op *op) {
281   return ((op == op_CallBegin)
282           || (op == op_EndReg)
283           || (op == op_EndExcept));
284 }