00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <stdint.h>
00047 #include <signal.h>
00048 #include "defs.h"
00049 #include "errors.h"
00050 #include "erglob.h"
00051 #include "ercg.h"
00052 #include "tracing.h"
00053 #include "config.h"
00054 #include "config_debug.h"
00055 #include "mtypes.h"
00056 #include "tn.h"
00057 #include "cg_flags.h"
00058 #include "op.h"
00059 #include "cgexp.h"
00060 #include "cgexp_internals.h"
00061 #include "whirl2ops.h"
00062
00063 #define RESET_COND_DEF_LAST(ops) Set_OP_cond_def_kind(OPS_last(ops),OP_ALWAYS_UNC_DEF)
00064
00065
00066
00067
00068
00069
00070
00071 void
00072 Expand_Float_Divide(TN *result, TN *src1, TN *src2, TYPE_ID mtype, OPS *ops)
00073 {
00074 FmtAssert(FALSE, ("Not Yet Implemented"));
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 #define IS_POWER_OF_2(val) ((val != 0) && ((val & (val-1)) == 0))
00086
00087 static BOOL Is_Power_Of_2(INT64 val, TYPE_ID mtype)
00088 {
00089 if (MTYPE_is_signed(mtype) && val < 0) val = -val;
00090
00091 if (mtype == MTYPE_U4) val &= 0xffffffffull;
00092
00093 return IS_POWER_OF_2(val);
00094 }
00095
00096
00097 static INT
00098 Get_Power_Of_2 (INT64 val, TYPE_ID mtype)
00099 {
00100 INT i;
00101 INT64 pow2mask;
00102
00103 if (MTYPE_is_signed(mtype) && val < 0) val = -val;
00104
00105 if (mtype == MTYPE_U4) val &= 0xffffffffull;
00106
00107 pow2mask = 1;
00108 for ( i = 0; i < MTYPE_size_reg(mtype); ++i ) {
00109 if (val == pow2mask) return i;
00110 pow2mask <<= 1;
00111 }
00112
00113 FmtAssert(FALSE, ("Get_Power_Of_2 unexpected value"));
00114
00115 }
00116
00117
00118
00119
00120
00121 static void
00122 Expand_Power_Of_2_Divide (TN *result, TN *numer, INT64 dvsr,
00123 TYPE_ID mtype, OPS *ops)
00124 {
00125 INT n = Get_Power_Of_2(dvsr, mtype);
00126
00127 if (MTYPE_is_unsigned(mtype)) {
00128 Expand_Shift(result, numer, Gen_Literal_TN(n, 4), mtype,
00129 shift_lright, ops);
00130 } else {
00131 TN *t1 = Build_TN_Of_Mtype(mtype);
00132 INT64 absdvsr = dvsr < 0 ? -dvsr : dvsr;
00133 BOOL is_double = MTYPE_is_size_double(mtype);
00134 TN *t2 = Build_TN_Of_Mtype(mtype);
00135 TN *t3 = Build_TN_Of_Mtype(mtype);
00136 TN *t4 = Build_TN_Of_Mtype(mtype);
00137 TN *t5 = (dvsr < 0) ? Build_TN_Of_Mtype(mtype) : result;
00138 Expand_Shift(t1, numer, Gen_Literal_TN(is_double?63:31, 4), mtype,
00139 shift_aright, ops);
00140 Expand_Mtype_Immediate (t2, Gen_Literal_TN (absdvsr - 1, is_double?8:4),
00141 mtype, ops);
00142 Expand_Binary_And( t3, t1, t2, mtype, ops );
00143 Expand_Add( t4, t3, numer, mtype, ops );
00144 Expand_Shift(t5, t4, Gen_Literal_TN(n, 4), mtype,
00145 shift_aright, ops);
00146 if (dvsr < 0) Expand_Neg(result, t5, mtype, ops);
00147 }
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 UINT64 gen_pseudo_inverse_32bit (UINT64 ld, BOOL *addp, INT64 *shift)
00174 {
00175 int p;
00176 UINT32 nc, delta, q1, r1, q2, r2, d;
00177
00178 d = (unsigned) ld;
00179
00180 *addp = false;
00181
00182 nc = -1 - (-d) %d;
00183 p = 31;
00184 q1 = 1U << 31;
00185 r1 = q1;
00186 q2 = ~q1;
00187 r2 = q2;
00188
00189 q1 /= nc;
00190 r1 -= q1 *nc;
00191 q2 /= d;
00192 r2 -= q2 * d;
00193 do
00194 {
00195 p++;
00196 if (r1 >= (nc - r1))
00197 {
00198 q1 = 2*q1 +1;
00199 r1 = 2*r1 - nc;
00200 }
00201 else
00202 {
00203 q1 = 2*q1;
00204 r1 = 2*r1;
00205 }
00206 if (r2 + 1 >= d - r2)
00207 {
00208 if (q2 >= ~(1U << 31))
00209 *addp = true;
00210 q2 = 2*q2+1;
00211 r2 = 2*r2+1-d;
00212 }
00213 else
00214 {
00215 if (q2 >= (1U<< 31))
00216 *addp = true;
00217 q2 = 2*q2;
00218 r2 = 2*r2+1;
00219 }
00220 delta = d - 1 - r2;
00221 } while (p < 64 && (q1 < delta | (q1 == delta && r1 == 0)));
00222 *shift = p - 32;
00223 return q2 + 1;
00224 }
00225
00226 UINT log2 (UINT i)
00227 {
00228 UINT t = 0;
00229 i = i >> 1;
00230 while(i) {
00231 i = i >> 1;
00232 t ++;
00233 }
00234 return t;
00235 }
00236
00237
00238
00239
00240 static BOOL
00241 Expand_Integer_Divide_By_Constant(TN *result, TN *numer_tn, INT64 denom_val,
00242 TYPE_ID mtype, OPS *ops)
00243 {
00244 UINT64 b;
00245 UINT64 d;
00246 INT64 precision_required;
00247 INT64 n;
00248 BOOL addp;
00249 BOOL is_odd;
00250 TN *abs_tn;
00251 TN *tmp_tn;
00252 TN *d_tn;
00253 TN *mult_tn;
00254 TN *shift_tn;
00255 TOP opc;
00256 TN *p1;
00257 BOOL is_double = MTYPE_is_size_double(mtype);
00258 BOOL is_signed = MTYPE_is_signed(mtype);
00259
00260 FmtAssert(!is_double, ("recip constant gen only supports 32 bits currently"));
00261
00262
00263 if (denom_val == 1)
00264 {
00265 Exp_COPY(result, numer_tn, ops);
00266 return TRUE;
00267 }
00268 else if (is_signed && denom_val == -1)
00269 {
00270 Expand_Neg(result, numer_tn, mtype, ops);
00271 return TRUE;
00272 }
00273
00274
00275 if (Is_Power_Of_2( denom_val, mtype))
00276 {
00277 Expand_Power_Of_2_Divide(result, numer_tn, denom_val, mtype, ops);
00278 return TRUE;
00279 }
00280
00281 if (!CGEXP_cvrt_int_div_to_mult) return FALSE;
00282
00283 if (is_signed)
00284 {
00285 b = denom_val<0 ? -denom_val : denom_val;
00286 is_odd = (b&1);
00287
00288 d = gen_pseudo_inverse_32bit (b, &addp, &n);
00289
00290 if (n > (is_double ? 63 : 31))
00291 return FALSE;
00292
00293 d_tn = Build_TN_Of_Mtype (mtype);
00294 Expand_Mtype_Immediate (d_tn, Gen_Literal_TN (d , (is_double ? 8 : 4)), mtype, ops);
00295
00296
00297 TN *p1;
00298 TOP opc = (TOP) (TOP_setp_lt_s8 + Mtype_Index(mtype));
00299 TN *zero_tn = Expand_Mtype_Immediate_Into_Register (
00300 Gen_Literal_TN (0, (is_double ? 8 : 4)),
00301 is_double ? MTYPE_I8 : MTYPE_I4,
00302 ops);
00303
00304 p1 = Build_RCLASS_TN (ISA_REGISTER_CLASS_predicate);
00305 Build_OP (opc, p1, numer_tn, zero_tn, ops);
00306
00307
00308 abs_tn = Build_TN_Of_Mtype (mtype);
00309 Build_OP ((is_double ? TOP_abs_s64 : TOP_abs_s32), abs_tn, numer_tn, ops);
00310
00311
00312 mult_tn = Build_TN_Of_Mtype (is_double ? MTYPE_U8 : MTYPE_U4);
00313 Expand_High_Multiply (mult_tn, abs_tn, d_tn, (is_double ? MTYPE_U8 : MTYPE_U4), ops);
00314
00315 shift_tn = Build_TN_Of_Mtype (mtype);
00316 if (addp)
00317 {
00318
00319 TN *tmp1_tn = Build_TN_Of_Mtype (mtype);
00320 TN *tmp2_tn = Build_TN_Of_Mtype (mtype);
00321 TN *tmp3_tn = Build_TN_Of_Mtype (mtype);
00322
00323 Build_OP ((is_double ? TOP_sub_u64 : TOP_sub_u32), tmp1_tn, abs_tn, mult_tn, ops);
00324 Expand_Shift (tmp2_tn, tmp1_tn, Gen_Literal_TN (1, 4), mtype, shift_lright, ops);
00325 Build_OP ((is_double ? TOP_add_u64 : TOP_add_u32), tmp3_tn, tmp2_tn, mult_tn, ops);
00326 Expand_Shift (shift_tn, tmp3_tn, Gen_Literal_TN(n-1, 4), mtype, shift_aright, ops);
00327 }
00328 else
00329 {
00330 Expand_Shift (shift_tn, mult_tn, Gen_Literal_TN(n, 4), mtype, shift_aright, ops);
00331 }
00332
00333
00334 if (denom_val < 0)
00335 {
00336 Build_OP ((is_double ? TOP_sub_s64_np : TOP_sub_s32_np),
00337 shift_tn, zero_tn, shift_tn, p1, ops);
00338 }
00339 else
00340 {
00341 Build_OP ((is_double ? TOP_sub_s64_p : TOP_sub_s32_p),
00342 shift_tn, zero_tn, shift_tn, p1, ops);
00343 }
00344
00345 Build_OP ((is_double ? TOP_mov_s64 : TOP_mov_s32), result, shift_tn, ops);
00346
00347 }
00348 else
00349 {
00350
00351 b = denom_val;
00352 is_odd = (b&1);
00353
00354 d = gen_pseudo_inverse_32bit (b, &addp, &n);
00355
00356 if (n > (is_double ? 63 : 31))
00357 return FALSE;
00358
00359 d_tn = Build_TN_Of_Mtype (mtype);
00360 Expand_Mtype_Immediate (d_tn, Gen_Literal_TN (d, (is_double ? 8 : 4)), mtype, ops);
00361
00362
00363 mult_tn = Build_TN_Of_Mtype (is_double ? MTYPE_U8 : MTYPE_U4);
00364 Expand_High_Multiply (mult_tn, numer_tn, d_tn, (is_double ? MTYPE_U8 : MTYPE_U4), ops);
00365
00366 if (addp)
00367 {
00368
00369 TN *tmp1_tn = Build_TN_Of_Mtype (mtype);
00370 TN *tmp2_tn = Build_TN_Of_Mtype (mtype);
00371 TN *tmp3_tn = Build_TN_Of_Mtype (mtype);
00372
00373 Build_OP ((is_double ? TOP_sub_u64 : TOP_sub_u32), tmp1_tn, numer_tn, mult_tn, ops);
00374 Expand_Shift (tmp2_tn, tmp1_tn, Gen_Literal_TN (1, 4), mtype, shift_lright, ops);
00375 Build_OP ((is_double ? TOP_add_u64 : TOP_add_u32), tmp3_tn, tmp2_tn, mult_tn, ops);
00376 Expand_Shift (result, tmp3_tn, Gen_Literal_TN (n-1, 4), mtype, shift_lright, ops);
00377 }
00378 else
00379 {
00380
00381 Expand_Shift (result, mult_tn, Gen_Literal_TN (n, 4), mtype, shift_lright, ops);
00382 }
00383
00384 }
00385 return TRUE;
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 static void
00404 Expand_Power_Of_2_Mod (TN *result, TN *src1, INT64 src2_val, TYPE_ID mtype, OPS *ops)
00405 {
00406 BOOL is_double = MTYPE_is_size_double(mtype);
00407 INT64 absval = src2_val < 0 ? -src2_val : src2_val;
00408 INT n = Get_Power_Of_2(absval, mtype);
00409 INT64 nMask = (1LL << n) - 1;
00410 TN *con = Gen_Literal_TN(nMask, is_double ? 8 : 4);
00411
00412 if (MTYPE_is_signed(mtype) && src2_val < 0) {
00413 TN *tmp1, *tmp2;
00414
00415 tmp1 = Build_TN_Of_Mtype(mtype);
00416 Expand_Neg(tmp1, src1, mtype, ops);
00417
00418 tmp2 = Build_TN_Of_Mtype(mtype);
00419 Expand_Binary_And(tmp2, tmp1, con, mtype, ops);
00420
00421 Expand_Neg(result, tmp2, mtype, ops);
00422 } else {
00423 Expand_Binary_And(result, src1, con, mtype, ops);
00424 }
00425 }
00426
00427
00428
00429
00430
00431
00432
00433 TN *
00434 Expand_Divide (TN *result, TN *src1, TN *src2, TYPE_ID mtype, OPS *ops)
00435 {
00436 TOP opc = (TOP) (TOP_div_s8 + Mtype_Index(mtype));
00437 INT64 val;
00438 BOOL is_double = MTYPE_is_size_double(mtype);
00439
00440 FmtAssert(!TN_is_constant(src1), ("NYI"));
00441
00442
00443 INT64 src2_val;
00444 BOOL const_src2 = TN_Value_At_Op (src2, NULL, &src2_val);
00445
00446
00447
00448
00449
00450 if ((const_src2) && (!is_double))
00451 {
00452 if (Expand_Integer_Divide_By_Constant(result, src1, src2_val, mtype, ops))
00453 return NULL;
00454 }
00455
00456 if (TN_Can_Use_Constant_Value (src2, mtype, &val)) {
00457
00458
00459 opc = (TOP) (TOP_div_s8_lit + Mtype_Index(mtype));
00460 src2 = Gen_Literal_TN_Of_Mtype (val, mtype);
00461 }
00462 else if (TN_is_constant(src2)) {
00463
00464 src2 = Expand_Mtype_Immediate_Into_Register (src2, mtype, ops);
00465 }
00466 Build_OP (opc, result, src1, src2, ops);
00467 }
00468
00469
00470 void
00471 Expand_Rem (TN *result, TN *src1, TN *src2, TYPE_ID mtype, OPS *ops)
00472 {
00473
00474 Expand_Mod(result, src1, src2, mtype, ops);
00475 }
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485 void
00486 Expand_Mod (TN *result, TN *src1, TN *src2, TYPE_ID mtype, OPS *ops)
00487 {
00488 TOP opc = (TOP) (TOP_rem_s8 + Mtype_Index(mtype));
00489 INT64 val;
00490 BOOL is_double = MTYPE_is_size_double(mtype);
00491 TN *mult_tn;
00492 TN *div_tn;
00493
00494 FmtAssert(!TN_is_constant(src1), ("NYI"));
00495
00496 INT64 src2_val;
00497 BOOL const_src2 = TN_Value_At_Op (src2, NULL, &src2_val);
00498
00499
00500
00501
00502
00503
00504
00505 if ((const_src2) && (!is_double))
00506 {
00507 div_tn = Build_TN_Of_Mtype (mtype);
00508 if (Expand_Integer_Divide_By_Constant(div_tn, src1, src2_val, mtype, ops))
00509 {
00510
00511
00512 mult_tn = Build_TN_Of_Mtype (mtype);
00513 Expand_Multiply (mult_tn, div_tn, src2, mtype, ops);
00514
00515 Build_OP ((TOP) (TOP_sub_s8 + Mtype_Index(mtype)), result, src1, mult_tn, ops);
00516 return;
00517 }
00518 }
00519
00520 if (TN_Can_Use_Constant_Value (src2, mtype, &val)) {
00521
00522
00523 opc = (TOP) (TOP_rem_s8_lit + Mtype_Index(mtype));
00524 src2 = Gen_Literal_TN_Of_Mtype (val, mtype);
00525 }
00526 else if (TN_is_constant(src2)) {
00527
00528 src2 = Expand_Mtype_Immediate_Into_Register (src2, mtype, ops);
00529 }
00530
00531 Build_OP (opc, result, src1, src2, ops);
00532 }
00533
00534
00535 void
00536 Expand_DivRem(TN *result, TN *result2, TN *src1, TN *src2, TYPE_ID mtype, OPS *ops)
00537 {
00538 BOOL is_double = MTYPE_is_size_double(mtype);
00539
00540
00541
00542
00543
00544
00545
00546 INT64 src1_val;
00547 BOOL const_src1 = TN_Value_At_Op (src1, NULL, &src1_val);
00548 INT64 src2_val;
00549 BOOL const_src2 = TN_Value_At_Op (src2, NULL, &src2_val);
00550
00551 if (const_src2){
00552 if (src2_val == 0){
00553 DevWarn( "Division by zero detected at compile time.\n" );
00554 const_src2 = FALSE;
00555 }
00556 }
00557
00558 FmtAssert(FALSE, ("Not Yet Implemented"));
00559 }