1 /*
2  * Copyright (c) 2010-2019, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 /**
19    \file
20    \brief Main source module to translate into LLVM
21  */
22 
23 #include "cgmain.h"
24 #include "dtypeutl.h"
25 #include "ll_ftn.h"
26 #include "exp_rte.h"
27 #include "error.h"
28 #include "machreg.h"
29 #include "dinit.h"
30 #include "cg.h"
31 #include "x86.h"
32 #include "fih.h"
33 #include "pd.h"
34 #include "llutil.h"
35 #include "lldebug.h"
36 #include "go.h"
37 #include "sharedefs.h"
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include "llassem.h"
41 #include "ll_write.h"
42 #include "expand.h"
43 #include "outliner.h"
44 #include "mth.h"
45 #if defined(SOCPTRG)
46 #include "soc.h"
47 #endif
48 #include "llvm/Config/llvm-config.h"
49 #include "mwd.h"
50 #include "ccffinfo.h"
51 #include "main.h"
52 #include "symfun.h"
53 
54 #ifdef OMP_OFFLOAD_LLVM
55 #include "ompaccel.h"
56 #define ISNVVMCODEGEN gbl.ompaccel_isdevice
57 #else
58 #define ISNVVMCODEGEN false
59 #endif
60 
61 #include "upper.h"
62 
63 typedef enum SincosOptimizationFlags {
64   /* used only for sincos() optimization */
65   SINCOS_SIN = 1,
66   SINCOS_COS = 2,
67   SINCOS_EXTRACT = 4,
68   SINCOS_MASK = SINCOS_SIN | SINCOS_COS
69 } SincosOptimizationFlags;
70 
71 /* clang-format off */
72 
73 const int max_operands[I_LAST + 1] = {
74     1,  1,  -1, -1, /* I_NONE, I_RET, I_BR, I_SW, */
75     -1, -1, -1,     /* I_INVOKE, I_UNWIND, I_UNREACH */
76     2,  2,  2,  2,  /* I_ADD, I_FADD, I_SUB,  I_FSUB, */
77     2,  2,  2,      /* I_MUL, I_FMUL, I_UDIV */
78     2,  2,  2,  2,  /* I_SDIV, I_FDIV, I_UREM, I_SREM, */
79     2,  2,  2,      /* I_FREM, I_SHL, I_LSHR */
80     2,  2,  2,  2,  /* I_ASHR, I_AND, I_OR, I_XOR, */
81     2,  3,  3,      /* I_EXTELE, I_INSELE, I_SHUFFVEC */
82     -1, -1, -1, -1, /* I_EXTRACTVAL, I_INSERTVAL, I_MALLOC, I_FREE, */
83     -1, 1,  2,      /* I_ALLOCA, I_LOAD, I_STORE */
84     -1, 1,  1,  1,  /* I_GEP, I_TRUNC, I_ZEXT, I_SEXT, */
85     1,  1,  1,      /* I_FPTRUNC, I_FPEXT, I_FPTOUI */
86     1,  1,  1,  1,  /* I_FPTOSI, I_UITOFP, I_SITOFP, I_PTRTOINT, */
87     1,  1,  3,      /* I_INTTOPTR, I_BITCAST, I_ICMP */
88     3,  3,  3,  -1, /* I_FCMP, I_VICMP, I_VFCMP, I_PHI, */
89     3,  -1, 1,      /* I_SELECT, I_CALL, I_VA_ARG */
90     1,  2,  1,  1,  /* I_DECL, I_LANDINGPAD,  I_RESUME, I_CLEANUP, */
91     1,  1,  1,      /* I_CATCH, I_BARRIER, I_ATOMICRMW */
92     3,  -1, -1, -1, /* I_CMPXCHG, I_PICALL, I_INDBR, I_FILTER */
93     -1              /* I_NONE */
94 };
95 
96 static const char *const llvm_instr_names[I_LAST] = {
97   "none", "ret", "br", "switch", "invoke", "unwind", "unreachable",
98   "add nsw",
99   "fadd",
100   "sub nsw",
101   "fsub",
102   "mul nsw",
103   "fmul", "udiv", "sdiv", "fdiv", "urem", "srem", "frem",
104   "shl nsw",
105   "lshr", "ashr", "and", "or", "xor", "extractelement", "insertelement",
106   "shufflevector", "extractvalue", "insertvalue", "malloc", "free", "alloca",
107   "load", "store", "getelementptr", "trunc", "zext", "sext", "fptrunc",
108   "fpext", "fptoui", "fptosi", "uitofp", "sitofp", "ptrtoint", "inttoptr",
109   "bitcast", "icmp", "fcmp", "vicmp", "vfcmp", "phi", "select", "call",
110   "va_arg", "=", "landingpad", "resume", "cleanup", "catch", "fence",
111   "atomicrmw", "cmpxchg", "fence", "call", "indirectbr", "filter"
112 };
113 
114 static const char *const stmt_names[STMT_LAST] = {
115     "STMT_NONE", "STMT_RET",  "STMT_EXPR",  "STMT_LABEL", "STMT_BR",
116     "STMT_ST",   "STMT_CALL", "STMT_SMOVE", "STMT_SZERO", "STMT_DECL"
117 };
118 
119 /* clang-format on */
120 
121 const int MEM_EXTRA = 500;
122 
123 static int fn_sig_len = MAXARGLEN;
124 static char *fn_sig_ptr = NULL;
125 static void insert_entry_label(int);
126 static void insert_jump_entry_instr(int);
127 static void store_return_value_for_entry(OPERAND *, int);
128 
129 static int openacc_prefix_sptr = 0;
130 static unsigned addressElementSize;
131 
132 #define ENTOCL_PREFIX "__pgocl_"
133 
134 #define HOMEFORDEBUG(sptr) (XBIT(183, 8) && SCG(sptr) == SC_DUMMY)
135 
136 #define ENABLE_CSE_OPT ((flg.opt >= 1) && !XBIT(183, 0x20) && !killCSE)
137 #define ENABLE_BLK_OPT ((flg.opt >= 2) && XBIT(183, 0x400))
138 #define ENABLE_ENHANCED_CSE_OPT (flg.opt >= 2 && !XBIT(183, 0x200000))
139 
140 #ifdef TARGET_LLVM_ARM
141 /* TO DO: to be revisited, for now we assume we always target NEON unit */
142 #define NEON_ENABLED 0 /* TEST_FEATURE(FEATURE_NEON) */
143 #endif
144 
145 /* debug switches:
146    -Mq,11,16 dump ili right before ILI -> LLVM translation
147    -Mq,12,16 provides dinit info, ilt trace, and some basic preprocessing info
148    -Mq,12,32 provides complete flow debug info through the LLVM routines
149 */
150 
151 #if defined(TARGET_LLVM_X8632) || defined(TARGET_LLVM_X8664)
152 
153 #ifndef TEST_FEATURE
154 #define TEST_FEATURE(M) 0
155 #endif
156 
157 #define HAS_AVX TEST_FEATURE(FEATURE_AVX)
158 #endif
159 
160 #define DBGTRACEIN(str) DBGXTRACEIN(DBGBIT(12, 0x20), 1, str)
161 #define DBGTRACEIN1(str, p1) DBGXTRACEIN1(DBGBIT(12, 0x20), 1, str, p1)
162 #define DBGTRACEIN2(str, p1, p2) DBGXTRACEIN2(DBGBIT(12, 0x20), 1, str, p1, p2)
163 #define DBGTRACEIN3(str, p1, p2, p3) \
164   DBGXTRACEIN3(DBGBIT(12, 0x20), 1, str, p1, p2, p3)
165 #define DBGTRACEIN4(str, p1, p2, p3, p4) \
166   DBGXTRACEIN4(DBGBIT(12, 0x20), 1, str, p1, p2, p3, p4)
167 #define DBGTRACEIN7(str, p1, p2, p3, p4, p5, p6, p7) \
168   DBGXTRACEIN7(DBGBIT(12, 0x20), 1, str, p1, p2, p3, p4, p5, p6, p7)
169 
170 #define DBGTRACEOUT(str) DBGXTRACEOUT(DBGBIT(12, 0x20), 1, str)
171 #define DBGTRACEOUT1(str, p1) DBGXTRACEOUT1(DBGBIT(12, 0x20), 1, str, p1)
172 #define DBGTRACEOUT2(str, p1, p2) \
173   DBGXTRACEOUT2(DBGBIT(12, 0x20), 1, str, p1, p2)
174 #define DBGTRACEOUT3(str, p1, p2, p3) \
175   DBGXTRACEOUT3(DBGBIT(12, 0x20), 1, str, p1, p2, p3)
176 #define DBGTRACEOUT4(str, p1, p2, p3, p4) \
177   DBGXTRACEOUT4(DBGBIT(12, 0x20), 1, str, p1, p2, p3, p4)
178 
179 #define DBGDUMPLLTYPE(str, llt) DBGXDUMPLLTYPE(DBGBIT(12, 0x20), 1, str, llt)
180 
181 #define DBGTRACE(str) DBGXTRACE(DBGBIT(12, 0x20), 1, str)
182 #define DBGTRACE1(str, p1) DBGXTRACE1(DBGBIT(12, 0x20), 1, str, p1)
183 #define DBGTRACE2(str, p1, p2) DBGXTRACE2(DBGBIT(12, 0x20), 1, str, p1, p2)
184 #define DBGTRACE3(str, p1, p2, p3) \
185   DBGXTRACE3(DBGBIT(12, 0x20), 1, str, p1, p2, p3)
186 #define DBGTRACE4(str, p1, p2, p3, p4) \
187   DBGXTRACE4(DBGBIT(12, 0x20), 1, str, p1, p2, p3, p4)
188 #define DBGTRACE5(str, p1, p2, p3, p4, p5) \
189   DBGXTRACE5(DBGBIT(12, 0x20), 1, str, p1, p2, p3, p4, p5)
190 
191 #if defined(TARGET_LLVM_X8664)
192 #define USE_FMA_EXTENSIONS 1
193 #endif
194 
195 /* Exported variables */
196 
197 SPTRINFO_T sptrinfo;
198 
199 /* This should live in llvm_info, but we need to access this module from other
200  * translation units temporarily */
201 LL_Module *current_module = NULL;
202 LL_Module *cpu_llvm_module = NULL;
203 #ifdef OMP_OFFLOAD_LLVM
204 LL_Module *gpu_llvm_module = NULL;
205 #endif
206 
207 
208 /* File static variables */
209 
210 static struct {
211   unsigned _new_ebb : 1;
212   unsigned _killCSE : 1;
213   unsigned _init_once : 1;
214   unsigned _cpp_init_once : 1;
215   unsigned _ftn_init_once : 1;
216   unsigned _float_jmp : 1;
217   unsigned _fcmp_negate : 1;
218   unsigned _last_stmt_is_branch : 1;
219   unsigned _rw_no_dep_check : 1;
220 } CGMain;
221 
222 #define new_ebb (CGMain._new_ebb)
223 #define killCSE (CGMain._killCSE)
224 #define init_once (CGMain._init_once)
225 #define cpp_init_once (CGMain._cpp_init_once)
226 #define ftn_init_once (CGMain._ftn_init_once)
227 #define float_jmp (CGMain._float_jmp)
228 #define fcmp_negate (CGMain._fcmp_negate)
229 #define last_stmt_is_branch (CGMain._last_stmt_is_branch)
230 #define rw_nodepcheck (CGMain._rw_no_dep_check)
231 
232 static int funcId;
233 static int fnegcc[17] = LLCCF_NEG;
234 static int expr_id;
235 static int entry_bih = 0;
236 static int routine_count;
237 static int addr_func_ptrs;
238 static STMT_Type curr_stmt_type;
239 static int *idxstack = NULL;
240 static hashmap_t sincos_map;
241 static hashmap_t sincos_imap;
242 static LL_MDRef cached_loop_metadata;
243 
244 static bool CG_cpu_compile = false;
245 
246 static struct ret_tag {
247   /** If ILI uses a hidden pointer argument to return a struct, this is it. */
248   SPTR sret_sptr;
249   bool emit_sret; /**< Should we emit an sret argument in LLVM IR? */
250 } ret_info;
251 
252 static struct llvm_tag {
253   GBL_LIST *last_global;
254   INSTR_LIST *last_instr;
255   INSTR_LIST *curr_instr;
256   LL_ABI_Info *abi_info;
257 
258   /** The LLVM function currently being built. */
259   LL_Function *curr_func;
260 
261   /** LLVM representation of the current function's return type.
262       See comment before analyze_ret_info(). */
263   LL_Type *return_ll_type;
264 
265   char *buf;
266 
267   /** Map sptr -> OPERAND* for those formal function arguments that are saved
268       to a local variable in the prolog by process_formal_arguments(). The
269       OPERAND* can be used to access the actual LLVM function argument while
270       the normal SNAME(sptr) refers to the local variable created by
271       process_formal_arguments(). */
272   hashmap_t homed_args;
273 
274   /** Map name -> func_type for intrinsics that have already been declared by
275       get_intrinsic(). */
276   hashmap_t declared_intrinsics;
277 
278   int last_sym_avail;
279   int last_dtype_avail;
280   int buf_idx;
281   int buf_sz;
282 
283   DTYPE curr_ret_dtype;
284 
285   unsigned no_debug_info : 1; /* set to emit engineering diagnostics */
286 } llvm_info;
287 
288 typedef struct temp_buf {
289   char *buffer;
290   int size;
291 } TEMP_BUF;
292 static TEMP_BUF sbuf;
293 
294 typedef struct char_len {
295   int sptr;
296   int base_sptr;
297   struct char_len *next;
298 } sclen;
299 static sclen *c_len;
300 
301 typedef struct temp_buf_list {
302   TEMP_BUF buf;
303   struct temp_buf_list *next;
304 } TEMP_BUF_LIST;
305 
306 static GBL_LIST *Globals;
307 static GBL_LIST *recorded_Globals;
308 static INSTR_LIST *Instructions;
309 static CSED_ITEM *csedList;
310 
311 typedef struct TmpsMap {
312   unsigned size;
313   TMPS **map;
314 } TmpsMap;
315 static TmpsMap tempsMap;
316 
317 /** \brief list for tracking calls with complex result types */
318 typedef struct ComplexResultList_t {
319   int *list;
320   unsigned size;
321   unsigned entries;
322 } ComplexResultList_t;
323 static ComplexResultList_t complexResultList;
324 
325 /* ---  static prototypes (exported prototypes belong in cgllvm.h) --- */
326 
327 static void gen_store_instr(SPTR, TMPS *, LL_Type *);
328 static void fma_rewrite(INSTR_LIST *isns);
329 static void undo_recip_div(INSTR_LIST *isns);
330 static char *set_local_sname(int sptr, const char *name);
331 static int is_special_return_symbol(int sptr);
332 static bool cgmain_init_call(int);
333 static OPERAND *gen_call_llvm_intrinsic(const char *, OPERAND *, LL_Type *,
334                                         INSTR_LIST *, LL_InstrName);
335 static OPERAND *gen_llvm_atomicrmw_instruction(int, int, OPERAND *, DTYPE);
336 static void gen_llvm_fence_instruction(int ilix);
337 static const char *get_atomicrmw_opname(LL_InstrListFlags);
338 static const char *get_atomic_memory_order_name(int);
339 static void insert_llvm_memcpy(int, int, OPERAND *, OPERAND *, int, int, int);
340 static void insert_llvm_memset(int, int, OPERAND *, int, int, int, int);
341 static SPTR get_call_sptr(int);
342 static LL_Type *make_function_type_from_args(LL_Type *return_type,
343                                              OPERAND *first_arg_op,
344                                              bool is_varargs);
345 static bool match_prototypes(LL_Type *ty1, LL_Type *ty2);
346 static MATCH_Kind match_types(LL_Type *, LL_Type *);
347 static int decimal_value_from_oct(int, int, int);
348 static char *vect_llvm_intrinsic_name(int);
349 static const char *vect_power_intrinsic_name(int);
350 static void build_unused_global_define_from_params(void);
351 static void print_function_signature(int func_sptr, const char *fn_name,
352                                      LL_ABI_Info *abi, bool print_arg_names);
353 static void write_global_and_static_defines(void);
354 static char *gen_constant(SPTR, DTYPE, INT, INT, int);
355 static char *process_string(char *, int, int);
356 static void make_stmt(STMT_Type, int, bool, SPTR next_bih_label, int ilt);
357 static INSTR_LIST *make_instr(LL_InstrName);
358 static INSTR_LIST *gen_instr(LL_InstrName, TMPS *, LL_Type *, OPERAND *);
359 static OPERAND *ad_csed_instr(LL_InstrName, int, LL_Type *, OPERAND *,
360                               LL_InstrListFlags, bool);
361 static void ad_instr(int, INSTR_LIST *);
362 static OPERAND *gen_call_expr(int ilix, DTYPE ret_dtype, INSTR_LIST *call_instr,
363                               int call_sptr);
364 static INSTR_LIST *gen_switch(int ilix);
365 static OPERAND *gen_unary_expr(int, LL_InstrName);
366 static OPERAND *gen_binary_vexpr(int, int, int, int);
367 static OPERAND *gen_binary_expr(int, int);
368 static OPERAND *gen_va_start(int);
369 static OPERAND *gen_va_arg(int);
370 static OPERAND *gen_va_end(int);
371 static OPERAND *gen_gep_index(OPERAND *, LL_Type *, int);
372 static OPERAND *gen_insert_value(OPERAND *aggr, OPERAND *elem, unsigned index);
373 static char *gen_vconstant(const char *, int, DTYPE, int);
374 static LL_Type *make_vtype(DTYPE, int);
375 static LL_Type *make_type_from_msz(MSZ);
376 static LL_Type *make_type_from_msz_with_addrspace(MSZ, int);
377 static LL_Type *make_type_from_opc(ILI_OP);
378 static bool add_to_cselist(int ilix);
379 static void clear_csed_list(void);
380 static void remove_from_csed_list(int);
381 static void set_csed_operand(OPERAND **, OPERAND *);
382 static OPERAND **get_csed_operand(int ilix);
383 static void build_csed_list(int);
384 static OPERAND *gen_base_addr_operand(int, LL_Type *);
385 static OPERAND *gen_optext_comp_operand(OPERAND *, ILI_OP, int, int, int, int,
386                                         LL_InstrName, int, int);
387 static OPERAND *gen_sptr(SPTR sptr);
388 static OPERAND *gen_load(OPERAND *addr, LL_Type *type, LL_InstrListFlags flags);
389 static void make_store(OPERAND *, OPERAND *, LL_InstrListFlags);
390 static OPERAND *make_load(int, OPERAND *, LL_Type *, MSZ, unsigned flags);
391 static OPERAND *convert_operand(OPERAND *convert_op, LL_Type *rslt_type,
392                                 LL_InstrName convert_instruction);
393 static OPERAND *convert_float_size(OPERAND *, LL_Type *);
394 static int follow_sptr_hashlk(SPTR sptr);
395 static DTYPE follow_ptr_dtype(DTYPE);
396 static bool same_op(OPERAND *, OPERAND *);
397 static void write_instructions(LL_Module *);
398 static LLIntegerConditionCodes convert_to_llvm_intcc(CC_RELATION cc);
399 static LLIntegerConditionCodes convert_to_llvm_uintcc(CC_RELATION cc);
400 static LLFloatingPointConditionCodes convert_to_llvm_fltcc(CC_RELATION cc);
401 static int convert_to_llvm_cc(CC_RELATION cc, int cc_type);
402 static OPERAND *get_intrinsic(const char *name, LL_Type *func_type);
403 static OPERAND *get_intrinsic_call_ops(const char *name, LL_Type *return_type,
404                                        OPERAND *args);
405 static OPERAND *sign_extend_int(OPERAND *op, unsigned result_bits);
406 static OPERAND *zero_extend_int(OPERAND *op, unsigned result_bits);
407 static bool repeats_in_binary(union xx_u);
408 static bool zerojump(ILI_OP);
409 static bool exprjump(ILI_OP);
410 static OPERAND *gen_resized_vect(OPERAND *, int, int);
411 static bool is_blockaddr_store(int, int, int);
412 static SPTR process_blockaddr_sptr(int, int);
413 static bool is_256_or_512_bit_math_intrinsic(int);
414 static bool have_masked_intrinsic(int);
415 static OPERAND *make_bitcast(OPERAND *, LL_Type *);
416 static void update_llvm_sym_arrays(void);
417 static bool need_debug_info(SPTR sptr);
418 static OPERAND *convert_int_size(int, OPERAND *, LL_Type *);
419 static OPERAND *convert_int_to_ptr(OPERAND *, LL_Type *);
420 static OPERAND *gen_call_vminmax_intrinsic(int ilix, OPERAND *op1,
421                                            OPERAND *op2);
422 static OPERAND *gen_extract_value_ll(OPERAND *, LL_Type *, LL_Type *, int);
423 static OPERAND *gen_extract_value(OPERAND *, DTYPE, DTYPE, int);
424 static OPERAND *gen_vect_compare_operand(int);
425 
426 #if defined(TARGET_LLVM_POWER)
427 static OPERAND *gen_call_vminmax_power_intrinsic(int ilix, OPERAND *op1,
428                                                  OPERAND *op2);
429 #endif
430 #if defined(TARGET_LLVM_ARM) && NEON_ENABLED
431 static OPERAND *gen_call_vminmax_neon_intrinsic(int ilix, OPERAND *op1,
432                                                 OPERAND *op2);
433 #endif
434 static INSTR_LIST *remove_instr(INSTR_LIST *instr, bool update_usect_only);
435 
436 #ifdef __cplusplus
ILI_ccOPND(int i,int j)437 inline static CC_RELATION ILI_ccOPND(int i, int j) {
438   CC_RELATION result = static_cast<CC_RELATION>(ILI_OPND(i, j));
439   assert((result <= CC_NOTGT) && (result >= -CC_NOTGT), "out of range", result,
440          ERR_Fatal);
441   return result;
442 }
443 #else
444 #define ILI_ccOPND ILI_OPND
445 #endif
446 
447 static void
consTempMap(unsigned size)448 consTempMap(unsigned size)
449 {
450   if (tempsMap.map) {
451     free(tempsMap.map);
452   }
453   tempsMap.size = size;
454   tempsMap.map = (TMPS **)calloc(sizeof(struct TmpsMap), size);
455 }
456 
457 static void
gcTempMap(void)458 gcTempMap(void)
459 {
460   free(tempsMap.map);
461   tempsMap.size = 0;
462   tempsMap.map = NULL;
463 }
464 
465 static TMPS *
getTempMap(unsigned ilix)466 getTempMap(unsigned ilix)
467 {
468   return (ilix < tempsMap.size) ? tempsMap.map[ilix] : NULL;
469 }
470 
471 static void
setTempMap(unsigned ilix,OPERAND * op)472 setTempMap(unsigned ilix, OPERAND *op)
473 {
474   if (ilix < tempsMap.size) {
475     tempsMap.map[ilix] = op->tmps;
476   }
477 }
478 
479 /* Convert the name of a built-in function to the LLVM intrinsic that
480    implements it.  This only works when the built-in function and the LLVM
481    intrinsic have the same signature, so no manipulation of the arguments or
482    return value is necessary.  (If the list of names gets much longer than two,
483    then a table driven approach should be used.  If the list gets really long,
484    then a hash table should be considered.) */
485 static const char *
map_to_llvm_name(const char * function_name)486 map_to_llvm_name(const char *function_name)
487 {
488   if (function_name == NULL) {
489     return NULL;
490   }
491   if (strcmp(function_name, "__builtin_return_address") == 0) {
492     return "llvm.returnaddress";
493   }
494   if (strcmp(function_name, "__builtin_frame_address") == 0) {
495     return "llvm.frameaddress";
496   }
497   return (char *)function_name;
498 }
499 
500 void
set_llvm_sptr_name(OPERAND * operand)501 set_llvm_sptr_name(OPERAND *operand)
502 {
503   const int sptr = operand->val.sptr;
504   operand->string = SNAME(sptr);
505 }
506 
507 char *
get_label_name(int sptr)508 get_label_name(int sptr)
509 {
510   char *nm = SNAME(sptr);
511   if (*nm == '@')
512     nm++;
513   return nm;
514 }
515 
516 char *
get_llvm_sname(SPTR sptr)517 get_llvm_sname(SPTR sptr)
518 {
519   char *p = SNAME(sptr);
520   if (p == NULL) {
521     process_sptr(sptr);
522     p = SNAME(sptr);
523   }
524   if (p == NULL) {
525     p = SYMNAME(sptr);
526     if (p == NULL)
527       return "";
528     p = (char*) map_to_llvm_name(p); // ???
529     SNAME(sptr) = (char *)getitem(LLVM_LONGTERM_AREA, strlen(p) + 1);
530     p = strcpy(SNAME(sptr), p);
531     return p;
532   }
533   if (*p == '@')
534     p++;
535   return p;
536 }
537 
538 char *
get_llvm_mips_sname(SPTR sptr)539 get_llvm_mips_sname(SPTR sptr)
540 {
541   return get_llvm_sname(sptr);
542 }
543 
544 DTYPE
cg_get_type(int n,TY_KIND v1,int v2)545 cg_get_type(int n, TY_KIND v1, int v2)
546 {
547   DTYPE ret_dtype = get_type(n, v1, v2);
548   update_llvm_sym_arrays();
549   return ret_dtype;
550 }
551 
552 INSTR_LIST *
llvm_info_last_instr(void)553 llvm_info_last_instr(void)
554 {
555   return llvm_info.last_instr;
556 }
557 
558 /**
559    \brief Check if the TY_STRUCT fits in registers per the ABI
560 
561    This is a backdoor for the expander to access the LLVM bridge.
562  */
563 bool
ll_check_struct_return(DTYPE dtype)564 ll_check_struct_return(DTYPE dtype)
565 {
566   LL_ABI_Info *abi;
567   TY_KIND ty = DTY(dtype);
568 
569   DEBUG_ASSERT((ty == TY_STRUCT) || (ty == TY_UNION) || DT_ISCMPLX(dtype),
570                "must be aggregate type");
571   abi = ll_abi_for_func_sptr(cpu_llvm_module, gbl.currsub, DT_NONE);
572   ll_abi_classify_return_dtype(abi, dtype);
573   return !LL_ABI_HAS_SRET(abi);
574 }
575 
576 /*
577  * Return value handling.
578  *
579  * Functions that return a struct or other aggregrate that doesn't fit in
580  * registers may require the caller to pass in a return value pointer as a
581  * hidden first argument. The callee wil store the returned struct to the
582  * pointer.
583  *
584  * In LLVM IR, this is represented by an sret attribute on the hidden pointer
585  * argument:
586  *
587  *   %struct.S = type { [10 x i32] }
588  *
589  *   define void @f(%struct.S* noalias sret %agg.result) ...
590  *
591  * Some structs can be returned in registers, depending on ABI-specific rules.
592  * For example, x86-64 can return a struct {long x, y; } struct in registers
593  * %rax and %rdx:
594  *
595  *   define { i64, i64 } @f() ...
596  *
597  * When targeting LLVM, ILI for a function returning a struct looks like the
598  * caller passed in an sret pointer, no matter how the ABI specifies the struct
599  * should be returned. This simplifies the ILI, and we will translate here if
600  * the struct can actually be returned in registers for the current ABI.
601  */
602 
603 /*
604  * Analyze the return value of the current function and determine how it should
605  * be translated to LLVM IR.
606  *
607  * If the LLVM IR representation uses an sret argument, set:
608  *
609  *   ret_info.emit_sret = true.
610  *   ret_info.sret_sptr = symbol table entry for sret argument.
611  *   llvm_info.return_ll_type = void.
612  *
613  * If the ILI representation uses a hidden struct argument, but the LLVM IR
614  * returns in registers, set:
615  *
616  *   ret_info.emit_sret = false.
617  *   ret_info.sret_sptr = symbol table entry for sret argument.
618  *   llvm_info.return_ll_type = LLVM function return type.
619  *
620  * Otherwise when both ILI and LLVM IR return in a register, set:
621  *
622  *   ret_info.emit_sret = false.
623  *   ret_info.sret_sptr = 0.
624  *   llvm_info.return_ll_type = LLVM function return type.
625  */
626 static void
analyze_ret_info(SPTR func_sptr)627 analyze_ret_info(SPTR func_sptr)
628 {
629   DTYPE return_dtype;
630 
631 #if defined(ENTRYG)
632   /* Get the symbol table entry for the function's return value. If ILI is
633    * using a hidden sret argument, this will be it.
634    *
635    * Fortran complex return values are handled differently, and don't get an
636    * 'sret' attribute.
637    */
638   ret_info.sret_sptr = aux.entry_base[ENTRYG(func_sptr)].ret_var;
639 #endif
640 
641   if (gbl.arets) {
642     return_dtype = DT_INT;
643   } else {
644     /* get return type from ag_table or ll_abi table */
645     return_dtype = get_return_type(func_sptr);
646     /* do not set the sret_sptr for 'bind(c)' complex functions in the presence
647        of multiple entries */
648     if (!has_multiple_entries(gbl.currsub))
649       if ((DT_ISCMPLX(return_dtype) && (CFUNCG(func_sptr) || CMPLXFUNC_C)) ||
650           LL_ABI_HAS_SRET(llvm_info.abi_info)) {
651         ret_info.sret_sptr = FVALG(func_sptr);
652       }
653   }
654 
655   DBGTRACE2("sret_sptr=%d, return_dtype=%d", ret_info.sret_sptr, return_dtype);
656 
657   llvm_info.return_ll_type = make_lltype_from_dtype(return_dtype);
658 
659   ret_info.emit_sret = LL_ABI_HAS_SRET(llvm_info.abi_info);
660 
661   if (ret_info.emit_sret) {
662     assert(ret_info.sret_sptr, "ILI should use a ret_var", func_sptr,
663            ERR_Fatal);
664     llvm_info.return_ll_type = make_void_lltype();
665   } else if (llvm_info.return_ll_type != llvm_info.abi_info->arg[0].type) {
666     /* Make sure the return type matches the ABI type. */
667     llvm_info.return_ll_type =
668         make_lltype_from_abi_arg(&llvm_info.abi_info->arg[0]);
669   }
670 
671   /* Process sret_sptr *after* setting up ret_info. Some decisions in
672    * process_auto_sptr() depends on ret_info. */
673   if (ret_info.sret_sptr)
674     process_sptr(ret_info.sret_sptr);
675 }
676 
677 /**
678    \brief Generate a return operand when ILI didn't provide a return value.
679 
680    LLVM requires a return instruction, even if it is only a "return undef".
681    Also handle the case where we have a special return value symbol but want to
682    return a value in registers.
683  */
684 INLINE static OPERAND *
gen_return_operand(int ilix)685 gen_return_operand(int ilix)
686 {
687   LL_Type *rtype = llvm_info.return_ll_type;
688   DTYPE dtype = DTYPEG(gbl.currsub);
689   TY_KIND dty = DTY(dtype);
690 
691   if (has_multiple_entries(gbl.currsub) && (rtype->data_type == LL_VOID) &&
692       (dty != TY_NONE) && (dty != TY_CHAR) && (dty != TY_NCHAR)
693 #if !defined(TARGET_LLVM_POWER)
694       && (dty != TY_CMPLX) && (dty != TY_DCMPLX)
695 #endif
696   ) {
697     LL_Type *rtype = make_lltype_from_dtype(dtype);
698     LL_Type *pTy = make_ptr_lltype(rtype);
699     const SPTR rv_sptr = FVALG(ILI_OPND(ilix, 1));
700     OPERAND *bcast = make_bitcast(gen_sptr(rv_sptr), pTy);
701     LL_InstrListFlags flgs = ldst_instr_flags_from_dtype(DTYPEG(rv_sptr));
702     return gen_load(bcast, rtype, flgs);
703   }
704   if (rtype->data_type == LL_VOID) {
705     OPERAND *op = make_operand();
706     op->ll_type = rtype;
707     return op;
708   }
709 
710   /* ret_sptr is the return value symbol which we want to return in registers.
711    *
712    * Coerce it to the correct type by bitcasting the pointer and loading
713    * the return value type from the stack slot.
714    */
715   if (ret_info.sret_sptr) {
716     /* Bitcast sret_sptr to a pointer to the return type. */
717     LL_Type *prtype = make_ptr_lltype(rtype);
718     OPERAND *sret_as_prtype =
719         make_bitcast(gen_sptr(ret_info.sret_sptr), prtype);
720     /* Load sret_sptr as the return type and return that. */
721     return gen_load(sret_as_prtype, rtype,
722                     ldst_instr_flags_from_dtype(DTYPEG(ret_info.sret_sptr)));
723   }
724   if (CFUNCG(gbl.currsub) &&
725       bindC_function_return_struct_in_registers(gbl.currsub)) {
726     /* returning a small struct */
727     LL_Type *pTy = make_ptr_lltype(rtype);
728     const SPTR rv_sptr = FVALG(ILI_OPND(ilix, 1));
729     OPERAND *bcast = make_bitcast(gen_sptr(rv_sptr), pTy);
730     LL_InstrListFlags flgs = ldst_instr_flags_from_dtype(DTYPEG(rv_sptr));
731     return gen_load(bcast, rtype, flgs);
732   }
733 
734   (void)ilix; // just to disable any unused warnings
735 
736   /* No return value symbol available, so just return undef. */
737   return make_undef_op(rtype);
738 }
739 
740 INLINE static bool
on_prescan_complex_list(int ilix)741 on_prescan_complex_list(int ilix)
742 {
743   int i;
744   for (i = 0; i < complexResultList.entries; ++i)
745     if (complexResultList.list[i] == ilix)
746       return true;
747   return false;
748 }
749 
750 static void
add_prescan_complex_list(int ilix)751 add_prescan_complex_list(int ilix)
752 {
753   if (on_prescan_complex_list(ilix))
754     return;
755   if (complexResultList.size == complexResultList.entries) {
756     int size;
757     if (complexResultList.size == 0)
758       complexResultList.size = 8;
759     else
760       complexResultList.size = complexResultList.size * 2;
761     size = complexResultList.size * sizeof(int);
762     complexResultList.list = (int *)realloc(complexResultList.list, size);
763   }
764   complexResultList.list[complexResultList.entries++] = ilix;
765 }
766 
767 INLINE static void
clear_prescan_complex_list(void)768 clear_prescan_complex_list(void)
769 {
770   if (complexResultList.list) {
771     free(complexResultList.list);
772     complexResultList.list = NULL;
773     complexResultList.size = complexResultList.entries = 0;
774   }
775 }
776 
777 INLINE static void
fix_nodepchk_flag(int bih)778 fix_nodepchk_flag(int bih)
779 {
780   if (block_branches_to(bih, bih))
781     return;
782   if (block_branches_to(BIH_NEXT(bih), bih)) {
783     BIH_NODEPCHK(BIH_NEXT(bih)) = true;
784     BIH_NODEPCHK2(BIH_NEXT(bih)) = true;
785     return;
786   }
787   if (!BIH_NODEPCHK2(bih)) {
788     BIH_NODEPCHK(bih) = false;
789   }
790 }
791 
792 INLINE static void
mark_rw_nodepchk(int bih)793 mark_rw_nodepchk(int bih)
794 {
795   rw_nodepcheck = 1;
796   if (!BIH_NODEPCHK2(bih))
797     cached_loop_metadata = ll_get_md_null();
798 }
799 
800 INLINE static void
clear_rw_nodepchk(void)801 clear_rw_nodepchk(void)
802 {
803   rw_nodepcheck = 0;
804   cached_loop_metadata = ll_get_md_null();
805 }
806 
807 void
print_personality(void)808 print_personality(void)
809 {
810   print_token(
811       " personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)");
812 }
813 
814 /**
815    \brief Clear \c SNAME for \p sptr
816    \param sptr  the symbol
817    Used by auto parallel in C when the optimizer uses the same compiler
818    generated variable across loops
819  */
820 void
llvmResetSname(int sptr)821 llvmResetSname(int sptr)
822 {
823   SNAME(sptr) = NULL;
824 }
825 
826 bool
currsub_is_sret(void)827 currsub_is_sret(void)
828 {
829   return LL_ABI_HAS_SRET(llvm_info.abi_info);
830 }
831 
832 INLINE static INSTR_LIST *
find_last_executable(INSTR_LIST * i)833 find_last_executable(INSTR_LIST *i)
834 {
835   INSTR_LIST *cursor = i;
836   for (;;) {
837     if (i->i_name != I_NONE)
838       return i;
839     i = i->prev;
840     if ((i == NULL) || (i == cursor))
841       return NULL;
842   }
843 }
844 
845 /* --------------------------------------------------------- */
846 
847 static int
processOutlinedByConcur(int bih)848 processOutlinedByConcur(int bih)
849 {
850   int eili, bili, bilt, eilt, gtid;
851   int bbih, ebih, bopc, eopc;
852   int bconcur = 0;
853   SPTR display = SPTR_NULL;
854   static int workingBih = 0;
855 
856   if (workingBih == 0)
857     workingBih = BIH_NEXT(workingBih);
858 
859   /* does not support nested auto parallel */
860   for (bbih = workingBih; bbih; bbih = BIH_NEXT(bbih)) {
861 
862     /* if IL_BCONCUR is always be the first - we can just check the first ilt */
863     for (bilt = BIH_ILTFIRST(bbih); bilt; bilt = ILT_NEXT(bilt)) {
864       bili = ILT_ILIP(bilt);
865       bopc = ILI_OPC(bili);
866 
867       if (bopc == IL_BCONCUR) {
868         ++bconcur;
869 
870         GBL_CURRFUNC = ILI_SymOPND(bili, 1);
871         display = llvmAddConcurEntryBlk(bbih);
872 
873         /* if IL_ECONCUR is always be the first - we can just check the first
874          * ilt */
875         for (ebih = bbih; ebih; ebih = BIH_NEXT(ebih)) {
876           for (eilt = BIH_ILTFIRST(ebih); eilt; eilt = ILT_NEXT(eilt)) {
877             eili = ILT_ILIP(eilt);
878             eopc = ILI_OPC(eili);
879             if (eopc == IL_ECONCUR) {
880               --bconcur;
881               llvmAddConcurExitBlk(ebih);
882               display = SPTR_NULL;
883               workingBih = BIH_NEXT(ebih); /* bih after IL_ECONCUR block */
884               BIH_NEXT(ebih) = 0;
885 
886               /* Reset SNAME field for gtid which needs to be done for C/C++.
887                * gtid can be have SC_LOCAL and ENCLFUNC of the host rotine and
888                * the code generator will not process if SNAME already exist.  We
889                * want this variable declared in the Mconcur outlined routine.
890                */
891               gtid = ll_get_gtid();
892               if (gtid)
893                 llvmResetSname(gtid);
894               ll_save_gtid_val(0);
895 
896 #if DEBUG
897               if (DBGBIT(10, 4)) {
898                 dump_blocks(gbl.dbgfil, gbl.entbih,
899                             "***** BIHs for Function \"%s\" *****", 0);
900               }
901 
902 #endif
903               return ebih;
904             }
905             if ((eopc == IL_BCONCUR) && (bbih != ebih))
906               return 0; /* error happens */
907           }
908         }
909       }
910     }
911   }
912   workingBih = 0; /* no more concur */
913   return 0;
914 }
915 
916 /*
917  * Inspect all variables in the symbol table and change their storage
918  * class from SC_LOCAL to SC_STATIC if appropriate.  The CG needs to
919  * know the final storage class of variables before it begins code
920  * generation.
921  */
922 static void
assign_fortran_storage_classes(void)923 assign_fortran_storage_classes(void)
924 {
925   int sptr;
926 
927   for (sptr = stb.firstusym; sptr < stb.stg_avail; ++sptr) {
928     switch (STYPEG(sptr)) {
929     case ST_PLIST:
930     case ST_VAR:
931     case ST_ARRAY:
932     case ST_STRUCT:
933     case ST_UNION:
934       if (REFG(sptr))
935         break;
936 
937       if (SCG(sptr) != SC_LOCAL && SCG(sptr) != SC_NONE)
938         break;
939 
940       if (DINITG(sptr) || SAVEG(sptr)) {
941         SCP(sptr, SC_STATIC);
942         if ((flg.smp || (XBIT(34, 0x200) || gbl.usekmpc)) && PARREFG(sptr))
943           PARREFP(sptr, 0);
944       } else if (STYPEG(sptr) != ST_VAR && !flg.recursive &&
945                  (!CCSYMG(sptr) || INLNG(sptr))) {
946         SCP(sptr, SC_STATIC);
947         if ((flg.smp || (XBIT(34, 0x200) || gbl.usekmpc)) && PARREFG(sptr))
948           PARREFP(sptr, 0);
949       }
950       break;
951     default:
952       break;
953     }
954   }
955 } /* end assign_fortran_storage_classes() */
956 
957 INLINE static LL_MDRef
cons_novectorize_metadata(void)958 cons_novectorize_metadata(void)
959 {
960   LL_MDRef lvcomp[2];
961   LL_MDRef loopVect;
962   LL_MDRef rv;
963 
964   if (cpu_llvm_module->loop_md)
965     return cpu_llvm_module->loop_md;
966   rv = ll_create_flexible_md_node(cpu_llvm_module);
967   lvcomp[0] = ll_get_md_string(cpu_llvm_module, "llvm.loop.vectorize.enable");
968   lvcomp[1] = ll_get_md_i1(0);
969   loopVect = ll_get_md_node(cpu_llvm_module, LL_PlainMDNode, lvcomp, 2);
970   ll_extend_md_node(cpu_llvm_module, rv, rv);
971   ll_extend_md_node(cpu_llvm_module, rv, loopVect);
972   cpu_llvm_module->loop_md = rv;
973   return rv;
974 }
975 
976 INLINE static LL_MDRef
cons_vectorize_metadata(void)977 cons_vectorize_metadata(void)
978 {
979   LL_MDRef lvcomp[2];
980 
981   lvcomp[0] = ll_get_md_string(cpu_llvm_module, "llvm.loop.vectorize.enable");
982   lvcomp[1] = ll_get_md_i1(1);
983   return ll_get_md_node(cpu_llvm_module, LL_PlainMDNode, lvcomp, 2);
984 }
985 
986 /**
987    \brief Second pass to clean up all the dead sincos callsites
988    \param isns  The list of instructions
989  */
990 INLINE static void
remove_dead_sincos_calls(INSTR_LIST * isns)991 remove_dead_sincos_calls(INSTR_LIST *isns)
992 {
993   INSTR_LIST *p;
994   for (p = isns; p; p = p->next) {
995     hash_data_t data;
996     if (!hashmap_lookup(sincos_map, p, &data))
997       continue;
998     if ((p->i_name == I_CALL) && (HKEY2INT(data) != SINCOS_EXTRACT) &&
999         ((HKEY2INT(data) & SINCOS_MASK) != SINCOS_MASK)) {
1000       p->operands->next = NULL;
1001       remove_instr(p, false);
1002     }
1003   }
1004 
1005   // finalize
1006   if (sincos_map)
1007     hashmap_free(sincos_map);
1008   sincos_map = NULL;
1009   if (sincos_imap)
1010     hashmap_free(sincos_imap);
1011   sincos_imap = NULL;
1012 }
1013 
1014 INLINE static bool
sincos_seen(void)1015 sincos_seen(void)
1016 {
1017   return sincos_imap != NULL;
1018 }
1019 
1020 INLINE static void
sincos_clear_all_args(void)1021 sincos_clear_all_args(void)
1022 {
1023   hashmap_clear(sincos_imap);
1024 }
1025 
1026 /**
1027    \brief First pass to rewrite degenerate sincos to sin (or cos) as needed
1028    \param isns  The list of instructions
1029  */
1030 INLINE static void
cleanup_unneeded_sincos_calls(INSTR_LIST * isns)1031 cleanup_unneeded_sincos_calls(INSTR_LIST *isns)
1032 {
1033   INSTR_LIST *p;
1034 
1035   DEBUG_ASSERT(sincos_seen(), "function must be marked as containing sincos");
1036   for (p = isns; p; p = p->next) {
1037     if (!hashmap_lookup(sincos_map, p, NULL))
1038       continue;
1039     if (p->i_name == I_EXTRACTVAL) {
1040       hash_data_t data;
1041       const LL_Type *retTy;
1042       const LL_Type *floatTy;
1043       char name[36]; /* make_math_name buffer is 32 */
1044       OPERAND *op;
1045       TMPS *t;
1046       INSTR_LIST *call = p->operands->tmps->info.idef;
1047 
1048       if (!hashmap_lookup(sincos_map, call, &data))
1049         continue;
1050       if ((HKEY2INT(data) & SINCOS_MASK) == SINCOS_MASK)
1051         continue;
1052 
1053       // replace this use (scalar)
1054       retTy = p->ll_type->sub_types[0];
1055       floatTy = make_lltype_from_dtype(DT_FLOAT);
1056       if (ILI_OPC(call->ilix) == IL_VSINCOS) {
1057         const int vecLen = retTy->sub_elements;
1058         LL_Type *eleTy = retTy->sub_types[0];
1059         bool hasMask = false;
1060         int opndCount = ili_get_vect_arg_count(call->ilix);
1061         DEBUG_ASSERT(retTy->data_type == LL_VECTOR, "vector type expected");
1062         if (ILI_OPC(ILI_OPND(call->ilix, opndCount - 1)) != IL_NULL) {
1063           hasMask = true;
1064         }
1065         llmk_math_name(name, (HKEY2INT(data) & SINCOS_COS) ? MTH_cos : MTH_sin,
1066                        vecLen, hasMask,
1067                        (eleTy == floatTy) ? DT_FLOAT : DT_DBLE);
1068       } else {
1069         llmk_math_name(name, (HKEY2INT(data) & SINCOS_COS) ? MTH_cos : MTH_sin,
1070                        1, false, (retTy == floatTy) ? DT_FLOAT : DT_DBLE);
1071       }
1072       t = p->tmps;
1073       op = call->operands->next;
1074       op = gen_call_to_builtin(call->ilix, name, op, retTy, p, I_CALL);
1075       p->i_name = I_CALL;
1076       p->tmps = t;
1077       DEBUG_ASSERT(t->use_count > 0, "must have positive use count");
1078       DEBUG_ASSERT(t->info.idef == op->tmps->info.idef, "instruction differs");
1079     }
1080   }
1081 }
1082 
1083 /**
1084    \brief Is the store ILT really a homing store?
1085    \param rIli  The value to be stored
1086    \param nme   The NME argument of the store
1087  */
1088 INLINE static bool
store_for_homing(int rIli,int nme)1089 store_for_homing(int rIli, int nme)
1090 {
1091   const int fnSym = gbl.currsub;
1092   const int sym = NME_SYM(nme);
1093   if ((sym > 0) && (SCG(sym) == SC_DUMMY))
1094     return true;
1095   if (CFUNCG(fnSym) && (DTY(DTYPEG(fnSym)) == TY_STRUCT) &&
1096       bindC_function_return_struct_in_registers(fnSym) &&
1097       (ILI_OPC(rIli) == IL_LDA)) {
1098     const int rrIli = ILI_OPND(rIli, 1);
1099     return (ILI_OPC(rrIli) == IL_ACON) &&
1100            (SCG(CONVAL1G(ILI_OPND(rrIli, 1))) == SC_DUMMY);
1101   }
1102   return false;
1103 }
1104 
1105 static void
add_external_function_declaration(const char * key,EXFUNC_LIST * exfunc)1106 add_external_function_declaration(const char *key, EXFUNC_LIST *exfunc)
1107 {
1108   const SPTR sptr = exfunc->sptr;
1109 
1110   if (sptr) {
1111     LL_ABI_Info *abi =
1112         ll_abi_for_func_sptr(cpu_llvm_module, sptr, DTYPEG(sptr));
1113 
1114     if(strstr(key, "@llvm.x86") != NULL) {
1115       int i;
1116       for(i = 0; i <= abi->nargs; i++){
1117         if(is_vector_x86_mmx(abi->arg[i].type)) {
1118         /* For x86 intrinsics, transform any vectors with overall 64 bits to
1119            X86_mmx. */
1120           if(abi->arg[i].type->data_type == LL_PTR) {
1121             abi->arg[i].type = ll_get_pointer_type(ll_create_basic_type(
1122                                  abi->arg[i].type->module, LL_X86_MMX, 0));
1123           }
1124           else {
1125             abi->arg[i].type = ll_create_basic_type(
1126                                  abi->arg[i].type->module, LL_X86_MMX, 0);
1127           }
1128         } else if (abi->arg[i].type->data_type == LL_PTR) {
1129         /* All pointer types for x86 intrinsics (that aren't x86_mmx*), becomes
1130            i8* pointers.*/
1131           abi->arg[i].type = ll_get_pointer_type(ll_convert_dtype(
1132                                abi->arg[i].type->module, DT_CHAR));
1133         }
1134       }
1135     }
1136 
1137     ll_proto_add_sptr(sptr, abi);
1138     if (exfunc->flags & EXF_INTRINSIC)
1139       ll_proto_set_intrinsic(ll_proto_key(sptr), exfunc->func_def);
1140 #ifdef WEAKG
1141     if (WEAKG(sptr))
1142       ll_proto_set_weak(ll_proto_key(sptr), true);
1143 #endif
1144   } else {
1145     DEBUG_ASSERT(key, "key must not be NULL");
1146     assert(exfunc->func_def && (exfunc->flags & EXF_INTRINSIC),
1147            "Invalid external function descriptor", 0, ERR_Fatal);
1148     if (*key == '@')
1149       ++key; /* do not include leading '@' in the key */
1150     ll_proto_add(key, NULL);
1151     ll_proto_set_intrinsic(key, exfunc->func_def);
1152   }
1153 }
1154 
1155 static void
add_profile_decl(const char * key,char * gname)1156 add_profile_decl(const char *key, char *gname)
1157 {
1158   EXFUNC_LIST *exfunc =
1159       (EXFUNC_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(EXFUNC_LIST));
1160   memset(exfunc, 0, sizeof(EXFUNC_LIST));
1161   exfunc->func_def = gname;
1162   exfunc->flags |= EXF_INTRINSIC;
1163   add_external_function_declaration(key, exfunc);
1164 }
1165 
1166 /**
1167    \brief Shared code to emit a call to a profile function
1168  */
1169 INLINE static void
write_profile_call(const char * profFn)1170 write_profile_call(const char *profFn)
1171 {
1172   fprintf(ASMFIL, "\tcall void @%s(i8* %%prof.thisfn, i8* %%prof.callsite)\n",
1173           profFn);
1174 }
1175 
1176 #undef PROF_ENTER
1177 #undef PROF_EXIT
1178 #undef PROF_CALLSITE
1179 #define PROF_ENTER "__cyg_profile_func_enter"
1180 #define PROF_EXIT "__cyg_profile_func_exit"
1181 #define PROF_CALLSITE "llvm.returnaddress"
1182 
1183 /**
1184    \brief Write a call to the profile entry routine
1185    \param sptr      The symbol of the function we are generating
1186    \param currFnTy  The type of the function, \p sptr
1187 
1188    \c -finstrument-functions adds an entry and exit profile call to each
1189    function
1190  */
1191 INLINE static void
write_profile_enter(SPTR sptr,LL_Type * currFnTy)1192 write_profile_enter(SPTR sptr, LL_Type *currFnTy)
1193 {
1194   static bool protos_defined = false;
1195   const char *currFn = get_llvm_name(sptr);
1196   fprintf(ASMFIL,
1197           "\t%%prof.thisfn = bitcast %s* @%s to i8*\n"
1198           "\t%%prof.callsite = call i8*(i32) @" PROF_CALLSITE "(i32 0)\n",
1199           currFnTy->str, currFn);
1200   write_profile_call(PROF_ENTER);
1201   if (!protos_defined) {
1202     /* add the declarations for output */
1203     const char retAddr[] = "declare i8* @" PROF_CALLSITE "(i32)";
1204     const char entFn[] = "declare void @" PROF_ENTER "(i8*, i8*)";
1205     const char extFn[] = "declare void @" PROF_EXIT "(i8*, i8*)";
1206     char *gname;
1207 
1208     protos_defined = true;
1209     gname = (char *)getitem(LLVM_LONGTERM_AREA, sizeof(retAddr));
1210     strcpy(gname, retAddr);
1211     add_profile_decl(PROF_CALLSITE, gname);
1212     gname = (char *)getitem(LLVM_LONGTERM_AREA, sizeof(entFn));
1213     strcpy(gname, entFn);
1214     add_profile_decl(PROF_ENTER, gname);
1215     gname = (char *)getitem(LLVM_LONGTERM_AREA, sizeof(extFn));
1216     strcpy(gname, extFn);
1217     add_profile_decl(PROF_EXIT, gname);
1218   }
1219 }
1220 
1221 /**
1222    \brief Write a call to the profile exit routine
1223 
1224    This should be done before each \c ret in the function.
1225  */
1226 INLINE static void
write_profile_exit(void)1227 write_profile_exit(void)
1228 {
1229   write_profile_call(PROF_EXIT);
1230 }
1231 
1232 #undef PROF_ENTER
1233 #undef PROF_EXIT
1234 #undef PROF_CALLSITE
1235 
1236 /**
1237    \brief Write out the start of an LLVM function definition
1238  */
1239 INLINE static void
write_routine_definition(SPTR func_sptr,LL_ABI_Info * abi,LL_Module * module,LL_Type * funcTy)1240 write_routine_definition(SPTR func_sptr, LL_ABI_Info *abi, LL_Module *module,
1241                          LL_Type *funcTy)
1242 {
1243   if (has_multiple_entries(func_sptr)) {
1244     write_master_entry_routine();
1245     return;
1246   }
1247   build_routine_and_parameter_entries(func_sptr, abi, module);
1248   if (XBIT(129, 0x800)) {
1249     /* -finstrument-functions */
1250     write_profile_enter(func_sptr, funcTy);
1251   }
1252 }
1253 
1254 INLINE static void
finish_routine(void)1255 finish_routine(void)
1256 {
1257   const int currFn = GBL_CURRFUNC;
1258   /***** "{" so vi matches *****/
1259   print_line("}");
1260   llassem_end_func(cpu_llvm_module->debug_info, currFn);
1261   if (flg.smp) {
1262     ll_reset_outlined_func();
1263   }
1264 }
1265 
1266 static LL_MDRef
cons_no_depchk_metadata(void)1267 cons_no_depchk_metadata(void)
1268 {
1269   if (LL_MDREF_IS_NULL(cached_loop_metadata)) {
1270     LL_MDRef vectorize = cons_vectorize_metadata();
1271     LL_MDRef md = ll_create_flexible_md_node(cpu_llvm_module);
1272     ll_extend_md_node(cpu_llvm_module, md, md);
1273     ll_extend_md_node(cpu_llvm_module, md, vectorize);
1274     cached_loop_metadata = md;
1275   }
1276   return cached_loop_metadata;
1277 }
1278 
1279 INLINE static bool
ignore_simd_block(int bih)1280 ignore_simd_block(int bih)
1281 {
1282   return (!XBIT(183, 0x4000000)) && BIH_SIMD(bih);
1283 }
1284 
1285 /**
1286    \brief Remove all deletable instructions from the instruction list
1287  */
1288 INLINE static void
remove_dead_instrs(void)1289 remove_dead_instrs(void)
1290 {
1291   INSTR_LIST *instr;
1292   for (instr = llvm_info.last_instr; instr;) {
1293     if ((instr->i_name == I_STORE) && (instr->flags & DELETABLE))
1294       instr = remove_instr(instr, false);
1295     else if ((instr->i_name != I_CALL) && (instr->i_name != I_INVOKE) &&
1296              (instr->i_name != I_ATOMICRMW) && (instr->tmps != NULL) &&
1297              (instr->tmps->use_count <= 0))
1298       instr = remove_instr(instr, false);
1299     else
1300       instr = instr->prev;
1301   }
1302 }
1303 
1304 /**
1305    \brief Perform code translation from ILI to LLVM for one routine
1306  */
1307 void
schedule(void)1308 schedule(void)
1309 {
1310   LL_Type *func_type;
1311   int bihx, ilt, ilix, ilix2, nme;
1312   ILI_OP opc;
1313   int rhs_ili, lhs_ili, sptr;
1314   int bih, bihprev, bihcurr, bihnext, li;
1315   int concurBih = 0;
1316   bool made_return;
1317   bool merge_next_block;
1318   bool targetNVVM = false;
1319   bool processHostConcur = true;
1320   SPTR func_sptr = GBL_CURRFUNC;
1321   bool first = true;
1322   CG_cpu_compile = true;
1323 
1324   funcId++;
1325   assign_fortran_storage_classes();
1326   if (XBIT(183, 0x10000000)) {
1327     if (XBIT(68, 0x1) && (!XBIT(183, 0x40000000)))
1328       widenAddressArith();
1329     if (gbl.outlined && funcHasNoDepChk())
1330       redundantLdLdElim();
1331   }
1332 
1333 restartConcur:
1334   FTN_HOST_REG() = 1;
1335   func_sptr = GBL_CURRFUNC;
1336   entry_bih = gbl.entbih;
1337 
1338   cg_llvm_init();
1339 #ifdef OMP_OFFLOAD_LLVM
1340   if (ISNVVMCODEGEN) {
1341     current_module = gpu_llvm_module;
1342     use_gpu_output_file();
1343   } else
1344 #endif
1345   {
1346     current_module = cpu_llvm_module;
1347   }
1348   if (!XBIT(53, 0x10000))
1349     current_module->omnipotentPtr = ll_get_md_null();
1350 
1351   consTempMap(ilib.stg_avail);
1352 
1353   store_llvm_localfptr();
1354 
1355   /* inititalize the definition lists per routine */
1356   csedList = NULL;
1357   memset(&ret_info, 0, sizeof(ret_info));
1358   llvm_info.curr_func = NULL;
1359 
1360 #if DEBUG
1361   if (DBGBIT(11, 1))
1362     dumpblocks("just before LLVM translation");
1363   if (DBGBIT(11, 0x810) || DBGBIT(12, 0x30)) {
1364     fprintf(ll_dfile, "--- ROUTINE %s (sptr# %d) ---\n", SYMNAME(func_sptr),
1365             func_sptr);
1366   }
1367   if (DBGBIT(11, 0x10)) {
1368     bihx = gbl.entbih;
1369     for (;;) {
1370       dmpilt(bihx);
1371       if (BIH_LAST(bihx))
1372         break;
1373       bihx = BIH_NEXT(bihx);
1374     }
1375     dmpili();
1376   }
1377 
1378 #endif
1379 
1380   /* Start the LLVM translation here */
1381   llvm_info.last_instr = NULL;
1382   llvm_info.curr_instr = NULL;
1383   Instructions = NULL;
1384   /* Update symbol table before we process any routine arguments, this must be
1385    * called before ll_abi_for_func_sptr()
1386    */
1387   stb_process_routine_parameters();
1388 
1389   hashmap_clear(llvm_info.homed_args);
1390   llvm_info.abi_info = ll_abi_for_func_sptr(current_module, func_sptr, DT_NONE);
1391   func_type = ll_abi_function_type(llvm_info.abi_info);
1392   process_sptr(func_sptr);
1393 
1394 #ifdef OMP_OFFLOAD_LLVM
1395   if (ISNVVMCODEGEN) {
1396     /* for now, we generate two ll_function one for host one device. */
1397     /* it is kernel function in gpu module */
1398     LL_Function *llfunc;
1399     if (OMPACCFUNCKERNELG(func_sptr)) {
1400       llfunc = ll_create_device_function_from_type(current_module, func_type,
1401                                                    &(SNAME(func_sptr)[1]), 1, 0,
1402                                                    "ptx_kernel", LL_NO_LINKAGE);
1403     } else if (OMPACCFUNCDEVG(func_sptr)) {
1404       llfunc = ll_create_device_function_from_type(current_module, func_type,
1405                                                    &(SNAME(func_sptr)[1]), 0, 0,
1406                                                    "", LL_INTERNAL_LINKAGE);
1407     }
1408     ll_set_device_function_arguments(current_module, llfunc,
1409                                      llvm_info.abi_info);
1410   }
1411 #endif
1412   llvm_info.curr_func =
1413       ll_create_function_from_type(func_type, SNAME(func_sptr));
1414 
1415   if (LL_ABI_HAS_SRET(llvm_info.abi_info) && CONTAINEDG(func_sptr)) {
1416     assert(false, "inner function returning derived type not yet implemented",
1417            func_sptr, ERR_Fatal);
1418   }
1419   ad_instr(0, gen_instr(I_NONE, NULL, NULL, make_label_op(SPTR_NULL)));
1420 
1421   ll_proto_add_sptr(func_sptr, llvm_info.abi_info);
1422 
1423   if (flg.debug || XBIT(120, 0x1000)) {
1424     if (!CCSYMG(func_sptr) || BIH_FINDEX(gbl.entbih)) {
1425       const DTYPE funcType = get_return_type(func_sptr);
1426       LL_Value *func_ptr = ll_create_pointer_value_from_type(
1427               current_module, func_type, SNAME(func_sptr), 0);
1428 
1429 #ifdef OMP_OFFLOAD_LLVM
1430       if(XBIT(232, 0x8))
1431         targetNVVM = true;
1432       if (!ISNVVMCODEGEN && !TEXTSTARTUPG(func_sptr))
1433 #endif
1434       {
1435         lldbg_emit_subprogram(current_module->debug_info, func_sptr, funcType,
1436                               BIH_FINDEX(gbl.entbih), targetNVVM);
1437         lldbg_set_func_ptr(current_module->debug_info, func_ptr);
1438       }
1439 
1440       if (!ISNVVMCODEGEN) {
1441         /* FIXME: should this be done for C, C++? */
1442         lldbg_reset_dtype_array(current_module->debug_info, DT_DEFERCHAR + 1);
1443       }
1444     }
1445   }
1446 
1447   /* set the return type of the function */
1448   analyze_ret_info(func_sptr);
1449 
1450   /* Build up the additional items/dummys needed for the master sptr if there
1451      are entries, and call process_formal_arguments on that information. */
1452   if (has_multiple_entries(gbl.currsub) && get_entries_argnum())
1453     process_formal_arguments(
1454         process_ll_abi_func_ftn_mod(current_module, get_master_sptr(), 1));
1455   else
1456     process_formal_arguments(llvm_info.abi_info);
1457   made_return = false;
1458 
1459   get_local_overlap_size();
1460   expr_id = 0;
1461   last_stmt_is_branch = 0;
1462 
1463   bih = BIH_NEXT(0);
1464   if ((XBIT(34, 0x200) || gbl.usekmpc) && !processHostConcur)
1465     bih = gbl.entbih;
1466   /* construct the body of the function */
1467   for (; bih; bih = BIH_NEXT(bih))
1468     for (ilt = BIH_ILTFIRST(bih); ilt; ilt = ILT_NEXT(ilt))
1469       build_csed_list(ILT_ILIP(ilt));
1470 
1471   merge_next_block = false;
1472   bih = BIH_NEXT(0);
1473   if ((XBIT(34, 0x200) || gbl.usekmpc) && !processHostConcur)
1474     bih = gbl.entbih;
1475   for (; bih; bih = BIH_NEXT(bih)) {
1476 
1477 #if DEBUG
1478     if (DBGBIT(12, 0x10)) {
1479       fprintf(ll_dfile, "schedule(): at bih %d\n", bih);
1480     }
1481 #endif
1482     DBGTRACE1("Processing bih %d", bih)
1483     bihcurr = bih;
1484     if (sincos_seen())
1485       sincos_clear_all_args();
1486 
1487     /* skip over an entry bih  */
1488     if (BIH_EN(bih)) {
1489       if (BIH_ILTFIRST(bih) != BIH_ILTLAST(bih))
1490         goto do_en_bih;
1491       else if (has_multiple_entries(gbl.currsub))
1492         goto do_en_bih;
1493       bihprev = bih;
1494       continue;
1495     }
1496     /* do we have a label that's the target of a branch? Either a
1497      * user label (via a goto) or a compiler created label for branching.
1498      */
1499     else if ((sptr = BIH_LABEL(bih)) && (DEFDG(sptr) || CCSYMG(sptr))) {
1500       assert(STYPEG(sptr) == ST_LABEL, "schedule(), not ST_LABEL", sptr,
1501              ERR_Fatal);
1502       clear_csed_list();
1503       make_stmt(STMT_LABEL, sptr, false, SPTR_NULL, 0);
1504     }
1505 
1506   do_en_bih:
1507 
1508     /* in general, ilts will correspond to statements */
1509     for (ilt = BIH_ILTFIRST(bih); ilt; ilt = ILT_NEXT(ilt))
1510       build_csed_list(ILT_ILIP(ilt));
1511 
1512     bihnext = BIH_NEXT(bih);
1513 
1514     if (merge_next_block == false)
1515       new_ebb = true;
1516 
1517     if (((flg.opt == 1 && BIH_EN(bih)) || (flg.opt >= 2 && !BIH_TAIL(bih))) &&
1518         bihnext && (!BIH_LABEL(bihnext)) && BIH_PAR(bihnext) == BIH_PAR(bih) &&
1519         BIH_CS(bihnext) == BIH_CS(bih) && BIH_TASK(bihnext) == BIH_TASK(bih) &&
1520         !BIH_NOMERGE(bih) && !BIH_NOMERGE(bihnext)) {
1521       merge_next_block = true;
1522     } else {
1523       merge_next_block = false;
1524     }
1525 
1526     if (XBIT(183, 0x10000000)) {
1527       if ((!XBIT(69, 0x100000)) && BIH_NODEPCHK(bih) &&
1528           (!ignore_simd_block(bih))) {
1529         fix_nodepchk_flag(bih);
1530         mark_rw_nodepchk(bih);
1531       } else {
1532         clear_rw_nodepchk();
1533       }
1534     }
1535 
1536     for (ilt = BIH_ILTFIRST(bih); ilt; ilt = ILT_NEXT(ilt)) {
1537       if (BIH_EN(bih) && ilt == BIH_ILTFIRST(bih)) {
1538         if (!has_multiple_entries(gbl.currsub))
1539           continue;
1540         if (first) {
1541           insert_jump_entry_instr(ilt);
1542           first = false;
1543         }
1544         insert_entry_label(ilt);
1545         continue;
1546       }
1547 #if DEBUG
1548       if (DBGBIT(12, 0x10)) {
1549         fprintf(ll_dfile, "\tat ilt %d\n", ilt);
1550       }
1551 #endif
1552 
1553       if (!ISNVVMCODEGEN && (flg.debug || XBIT(120, 0x1000))) {
1554         lldbg_emit_line(current_module->debug_info, ILT_LINENO(ilt));
1555       }
1556       ilix = ILT_ILIP(ilt);
1557       opc = ILI_OPC(ilix);
1558 
1559       if (processHostConcur && (XBIT(34, 0x200) || gbl.usekmpc)) {
1560         if (opc == IL_BCONCUR) {
1561           ++concurBih;
1562         } else if (opc == IL_ECONCUR) {
1563           --concurBih;
1564         }
1565         if (concurBih)
1566           continue;
1567       }
1568 
1569       if (ILT_BR(ilt)) { /* branch */
1570         SPTR next_bih_label = SPTR_NULL;
1571 
1572         if (!ILT_NEXT(ilt) && bihnext) {
1573           const SPTR t_next_bih_label = BIH_LABEL(bihnext);
1574           if (t_next_bih_label &&
1575               (DEFDG(t_next_bih_label) || CCSYMG(t_next_bih_label)))
1576             next_bih_label = t_next_bih_label;
1577         }
1578         make_stmt(STMT_BR, ilix, false, next_bih_label, ilt);
1579         if (XBIT(183, 0x10000000) && (!XBIT(69, 0x100000)) &&
1580             BIH_NODEPCHK(bih) && (!BIH_NODEPCHK2(bih)) &&
1581             (!ignore_simd_block(bih))) {
1582           LL_MDRef loop_md = cons_no_depchk_metadata();
1583           INSTR_LIST *i = find_last_executable(llvm_info.last_instr);
1584           if (i) {
1585             i->flags |= SIMD_BACKEDGE_FLAG;
1586             i->misc_metadata = loop_md;
1587           }
1588         }
1589         if (ignore_simd_block(bih)) {
1590           LL_MDRef loop_md = cons_novectorize_metadata();
1591           llvm_info.last_instr->flags |= SIMD_BACKEDGE_FLAG;
1592           llvm_info.last_instr->misc_metadata = loop_md;
1593         }
1594       } else if ((ILT_ST(ilt) || ILT_DELETE(ilt)) &&
1595                  (IL_TYPE(opc) == ILTY_STORE)) {
1596         /* store */
1597         rhs_ili = ILI_OPND(ilix, 1);
1598         lhs_ili = ILI_OPND(ilix, 2);
1599         nme = ILI_OPND(ilix, 3);
1600         /* can we ignore homing code? Try it here */
1601         if (is_rgdfili_opcode(ILI_OPC(rhs_ili)))
1602           continue;
1603         if (BIH_EN(bih) && store_for_homing(rhs_ili, nme))
1604           continue;
1605         make_stmt(STMT_ST, ilix,
1606                   ENABLE_CSE_OPT && ILT_DELETE(ilt) &&
1607                       (IL_TYPE(opc) == ILTY_STORE),
1608                   SPTR_NULL, ilt);
1609       } else if (opc == IL_JSR && cgmain_init_call(ILI_OPND(ilix, 1))) {
1610         make_stmt(STMT_SZERO, ILI_OPND(ilix, 2), false, SPTR_NULL, ilt);
1611       } else if (opc == IL_SMOVE) {
1612         make_stmt(STMT_SMOVE, ilix, false, SPTR_NULL, ilt);
1613       } else if (ILT_EX(ilt)) {
1614         // ilt contains a call
1615         if (opc == IL_LABEL)
1616           continue; /* gen_llvm_expr does not handle IL_LABEL */
1617         switch (opc) {
1618         case IL_DFRSP:
1619         case IL_DFRDP:
1620         case IL_DFRCS:
1621 #ifdef LONG_DOUBLE_FLOAT128
1622         case IL_FLOAT128RESULT:
1623 #endif
1624           ilix = ILI_OPND(ilix, 1);
1625           opc = ILI_OPC(ilix);
1626           break;
1627         default:
1628           break;
1629         }
1630         if (is_mvili_opcode(opc)) {
1631           /* call part of the return */
1632           goto return_with_call;
1633         } else if (is_freeili_opcode(opc)) {
1634           remove_from_csed_list(ilix);
1635           make_stmt(STMT_DECL, ilix, false, SPTR_NULL, ilt);
1636         } else if ((opc == IL_JSR) || (opc == IL_QJSR) || (opc == IL_JSRA)
1637 #ifdef SJSR
1638                    || (opc == IL_SJSR) || (opc == IL_SJSRA)
1639 #endif
1640         ) {
1641           /* call not in a return */
1642           make_stmt(STMT_CALL, ilix, false, SPTR_NULL, ilt);
1643         } else if ((opc != IL_DEALLOC) && (opc != IL_NOP)) {
1644           make_stmt(STMT_DECL, ilix, false, SPTR_NULL, ilt);
1645         }
1646       } else if (opc == IL_FENCE) {
1647         gen_llvm_fence_instruction(ilix);
1648       } else {
1649       /* may be a return; otherwise mostly ignored */
1650       /* However, need to keep track of FREE* ili, to match them
1651        * with CSE uses, since simple dependences need to be checked
1652        */
1653       return_with_call:
1654         if (is_mvili_opcode(opc)) { /* routine return */
1655           if (ret_info.sret_sptr == 0) {
1656             ilix2 = ILI_OPND(ilix, 1);
1657             /* what type of return value */
1658             switch (IL_TYPE(ILI_OPC(ilix2))) {
1659             case ILTY_LOAD:
1660             case ILTY_CONS:
1661             case ILTY_ARTH:
1662             case ILTY_DEFINE:
1663             case ILTY_MOVE:
1664               make_stmt(STMT_RET, ilix2, false, SPTR_NULL, ilt);
1665               break;
1666             case ILTY_OTHER:
1667               /* handle complex builtin */
1668               if (XBIT(70, 0x40000000) && (IL_RES(ILI_OPC(ilix2)) == ILIA_DP ||
1669                                            IL_RES(ILI_OPC(ilix2)) == ILIA_SP)) {
1670                 make_stmt(STMT_RET, ilix2, false, SPTR_NULL, ilt);
1671                 break;
1672               }
1673               // fall through
1674             default:
1675               switch (ILI_OPC(ilix2)) {
1676               case IL_ISELECT:
1677               case IL_KSELECT:
1678               case IL_ASELECT:
1679               case IL_FSELECT:
1680               case IL_DSELECT:
1681               case IL_ATOMICRMWI:
1682               case IL_ATOMICRMWKR:
1683               case IL_ATOMICRMWA:
1684               case IL_ATOMICRMWSP:
1685               case IL_ATOMICRMWDP:
1686                 make_stmt(STMT_RET, ilix2, false, SPTR_NULL, ilt);
1687                 break;
1688               default:
1689                 assert(0, "schedule(): incompatible return type",
1690                        IL_TYPE(ILI_OPC(ilix2)), ERR_Fatal);
1691               }
1692             }
1693             made_return = true;
1694           }
1695         } else if (is_freeili_opcode(opc)) {
1696 #if DEBUG
1697           if (DBGBIT(12, 0x10)) {
1698             fprintf(ll_dfile, "\tfound free ili: %d(%s)\n", ilix, IL_NAME(opc));
1699           }
1700 #endif
1701           remove_from_csed_list(ilix);
1702           make_stmt(STMT_DECL, ilix, false, SPTR_NULL, ilt);
1703         } else if (opc == IL_LABEL) {
1704           continue; /* ignore IL_LABEL */
1705         } else if (BIH_LAST(bih) && !made_return) {
1706           /* at end, make a NULL return statement if return not already made */
1707           make_stmt(STMT_RET, ilix, false, SPTR_NULL, ilt);
1708         } else if (opc == IL_SMOVE) {
1709           /* moving/storing a block of memory */
1710           make_stmt(STMT_SMOVE, ilix, false, SPTR_NULL, ilt);
1711         }
1712       }
1713     }
1714     bihprev = bih;
1715   }
1716 
1717   build_unused_global_define_from_params();
1718 
1719 /* header already printed; now print global and static defines */
1720   write_ftn_typedefs();
1721   write_global_and_static_defines();
1722 
1723 #ifdef OMP_OFFLOAD_LLVM
1724   if (flg.omptarget && ISNVVMCODEGEN)
1725     use_cpu_output_file();
1726 #endif
1727   assem_data();
1728 #ifdef OMP_OFFLOAD_LLVM
1729   if (flg.omptarget && ISNVVMCODEGEN)
1730     use_gpu_output_file();
1731   if (flg.omptarget)
1732     write_libomtparget();
1733 #endif
1734   /* perform setup for each routine */
1735   write_routine_definition(func_sptr, llvm_info.abi_info, current_module,
1736                            func_type);
1737 
1738   /* write out local variable defines */
1739   ll_write_local_objects(llvm_file(), llvm_info.curr_func);
1740   /* Emit alloca for local equivalence, c.f. get_local_overlap_var(). */
1741   write_local_overlap();
1742 
1743   if (ENABLE_BLK_OPT)
1744     optimize_block(llvm_info.last_instr);
1745 
1746   /*
1747    * similar code in llvect.c, cgoptim1.c, and llvm's cgmain.c & llvect.c
1748    * 01/17/17 -- we are no longer attempting to transform the divide into
1749    *             a multiply by recip; we are simply depending on the user
1750    *             adding -Mfprelaxed[=div]
1751    * 02/10/17 -- enabled with -Mnouniform
1752    *
1753    */
1754   if (XBIT_NOUNIFORM && (!XBIT(183, 0x8000)) && XBIT(15, 4) && (!flg.ieee)) {
1755     undo_recip_div(Instructions);
1756   }
1757   if (sincos_seen()) {
1758     cleanup_unneeded_sincos_calls(Instructions);
1759     remove_dead_sincos_calls(Instructions);
1760   }
1761   /* try FMA rewrite */
1762   if (XBIT_GENERATE_SCALAR_FMA /* HAS_FMA and x-flag 164 */
1763       && (get_llvm_version() >= LL_Version_3_7)) {
1764     fma_rewrite(Instructions);
1765   }
1766 
1767   if (ENABLE_CSE_OPT) {
1768     remove_dead_instrs();
1769     Instructions->prev = NULL;
1770     if (XBIT(183, 0x40))
1771       sched_instructions(Instructions);
1772   }
1773 
1774   /* print out the instructions */
1775   write_instructions(current_module);
1776 
1777   finish_routine();
1778 
1779 #ifdef OMP_OFFLOAD_LLVM
1780   if (ISNVVMCODEGEN) {
1781     use_cpu_output_file();
1782   }
1783 #endif
1784 
1785   clear_prescan_complex_list();
1786   if (!ISNVVMCODEGEN && (flg.debug || XBIT(120, 0x1000)))
1787     lldbg_cleanup_missing_bounds(current_module->debug_info,
1788                                  BIH_FINDEX(gbl.entbih));
1789   hashmap_clear(llvm_info.homed_args); /* Don't home entry trampoline parms */
1790   if (processHostConcur)
1791     print_entry_subroutine(current_module);
1792   ll_destroy_function(llvm_info.curr_func);
1793   llvm_info.curr_func = NULL;
1794 
1795   assem_end();
1796   /* we need to set init_once to zero here because for cuda fortran combine with
1797    * acc - the constructors can be created without one after the other and
1798    * cg_llvm_end will not get call between those.  If init_once is not reset,
1799    * cg_llvm_init will not go through.
1800    */
1801   init_once = false;
1802 
1803   if (--routine_count > 0)
1804   {
1805     /* free CG_MEDTERM_AREA - done on a per-routine basis */
1806     freearea(CG_MEDTERM_AREA);
1807   }
1808   FTN_HOST_REG() = 1;
1809 
1810   if ((XBIT(34, 0x200) || gbl.usekmpc) &&
1811       (concurBih = processOutlinedByConcur(concurBih))) {
1812     processHostConcur = false;
1813     goto restartConcur;
1814   }
1815   ll_reset_gtid();
1816   if (flg.smp || (XBIT(34, 0x200) || gbl.usekmpc))
1817     ll_reset_gtid();
1818 
1819   if ((gbl.outlined || ISTASKDUPG(GBL_CURRFUNC)) &&
1820       ((flg.inliner && !XBIT(14, 0x10000)) || flg.autoinline)) {
1821       GBL_CURRFUNC = SPTR_NULL;
1822   }
1823   gcTempMap();
1824   CG_cpu_compile = false;
1825 } /* schedule */
1826 
1827 INLINE static bool
call_sym_is(SPTR sptr,const char * sym_name)1828 call_sym_is(SPTR sptr, const char *sym_name)
1829 {
1830   return sptr && (strncmp(SYMNAME(sptr), sym_name, strlen(sym_name)) == 0);
1831 }
1832 
1833 static OPERAND *
gen_llvm_instr(int ilix,ILI_OP opc,LL_Type * return_type,LL_Type * param_lltype,LL_InstrName itype)1834 gen_llvm_instr(int ilix, ILI_OP opc, LL_Type *return_type,
1835                LL_Type *param_lltype, LL_InstrName itype)
1836 {
1837   OPERAND *operand;
1838   OPERAND *param_op;
1839   INSTR_LIST *Curr_Instr;
1840   int arg_ili = ILI_OPND(ilix, 2);
1841 
1842   operand = make_tmp_op(return_type, make_tmps());
1843   Curr_Instr = gen_instr(itype, operand->tmps, operand->ll_type, NULL);
1844   assert(ILI_OPC(arg_ili) == opc,
1845          "gen_llvm_instr(): unexpected opc for parameter ", ILI_OPC(arg_ili),
1846          ERR_Fatal);
1847   param_op = gen_llvm_expr(ILI_OPND(arg_ili, 1), param_lltype);
1848   Curr_Instr->operands = param_op;
1849   arg_ili = ILI_OPND(arg_ili, 2);
1850   while ((arg_ili > 0) && (ILI_OPC(arg_ili) != IL_NULL)) {
1851     assert(ILI_OPC(arg_ili) == opc,
1852            "gen_llvm_instr(): unexpected opc for parameter ", ILI_OPC(arg_ili),
1853            ERR_Fatal);
1854     param_op->next = gen_llvm_expr(ILI_OPND(arg_ili, 1), param_lltype);
1855     param_op = param_op->next;
1856     arg_ili = ILI_OPND(arg_ili, 2);
1857   }
1858   ad_instr(ilix, Curr_Instr);
1859 
1860   return operand;
1861 }
1862 
1863 static OPERAND *
gen_llvm_atomic_intrinsic_for_builtin(int pdnum,int sptr,int ilix,INSTR_LIST * Call_Instr)1864 gen_llvm_atomic_intrinsic_for_builtin(int pdnum, int sptr, int ilix,
1865                                       INSTR_LIST *Call_Instr)
1866 {
1867   OPERAND *operand;
1868   int call_sptr = sptr;
1869   LL_Type *op_type;
1870   char routine_name[MAXIDLEN];
1871   DTYPE base_dtype;
1872   int first_arg_ili;
1873   bool incdec = false;
1874   int arg_ili = ILI_OPND(ilix, 2);
1875   DTYPE call_dtype = DTYPEG(call_sptr);
1876   DTYPE return_dtype = DTyReturnType(call_dtype);
1877   int params = DTyParamList(call_dtype);
1878 
1879   switch (pdnum) {
1880   default:
1881     assert(0, "gen_llvm_atomic_intrinsic_for_builtin(): invalid pdnum value ",
1882            pdnum, ERR_Fatal);
1883   }
1884   base_dtype = return_dtype;
1885   first_arg_ili = ILI_OPND(arg_ili, 1);
1886   switch (DTY(base_dtype)) {
1887   case TY_BINT:
1888     strcat(routine_name, "i8.p0i8");
1889     break;
1890   case TY_USINT:
1891     strcat(routine_name, "i16.p0i16");
1892     break;
1893   case TY_SINT:
1894     strcat(routine_name, "i16.p0i16");
1895     break;
1896   case TY_UINT:
1897   case TY_INT:
1898     strcat(routine_name, "i32.p0i32");
1899     break;
1900   case TY_INT8:
1901   case TY_UINT8:
1902     strcat(routine_name, "i64.p0i64");
1903     break;
1904   case TY_REAL:
1905     return NULL;
1906   default:
1907     assert(0,
1908            "gen_llvm_atomic_intrinsic_for_builtin(): invalid base type for "
1909            "call to sptr",
1910            sptr, ERR_Fatal);
1911   }
1912   op_type = make_lltype_from_dtype(cg_get_type(2, TY_PTR, return_dtype));
1913   operand = gen_llvm_expr(first_arg_ili, op_type);
1914   op_type = make_lltype_from_dtype(return_dtype);
1915   if (incdec) {
1916     operand->next = gen_llvm_expr(ad_icon(1), op_type);
1917   } else {
1918     int next_arg_ili = ILI_OPND(arg_ili, 2);
1919     operand->next = gen_llvm_expr(ILI_OPND(next_arg_ili, 1), op_type);
1920     next_arg_ili = ILI_OPND(next_arg_ili, 2);
1921     if (ILI_OPC(next_arg_ili) != IL_NULL) {
1922       int next = ILI_OPND(next_arg_ili, 1);
1923       operand->next->next = gen_llvm_expr(next, op_type);
1924     }
1925   }
1926 
1927   return gen_llvm_atomicrmw_instruction(ilix, pdnum, operand, return_dtype);
1928 }
1929 
1930 static OPERAND *
gen_llvm_intrinsic_for_builtin(int sptr,int arg_ili,INSTR_LIST * Call_Instr)1931 gen_llvm_intrinsic_for_builtin(int sptr, int arg_ili, INSTR_LIST *Call_Instr)
1932 {
1933   OPERAND *operand;
1934   int call_sptr = sptr;
1935   int call_dtype;
1936   int return_dtype;
1937   int params, param_dtype;
1938   int pd_sym;
1939   LL_Type *return_type;
1940   char routine_name[MAXIDLEN];
1941   int base_dtype;
1942   int first_arg_ili;
1943   return operand;
1944 }
1945 
1946 static OPERAND *
gen_call_vminmax_intrinsic(int ilix,OPERAND * op1,OPERAND * op2)1947 gen_call_vminmax_intrinsic(int ilix, OPERAND *op1, OPERAND *op2)
1948 {
1949   DTYPE vect_dtype;
1950   int vect_size;
1951   int type_size;
1952   char sign = 'u';
1953   char type = 'i';
1954   char *mstr = "maxnum";
1955   static char buf[MAXIDLEN];
1956 
1957   if (ILI_OPC(ilix) == IL_VMIN) {
1958     mstr = "minnum";
1959   } else {
1960     assert(ILI_OPC(ilix) == IL_VMAX, "gen_call_vminmax_intrinsic(): bad opc",
1961            ILI_OPC(ilix), ERR_Fatal);
1962   }
1963   vect_dtype = ILI_DTyOPND(ilix, 3);
1964   vect_size = DTyVecLength(vect_dtype);
1965   switch (DTY(DTySeqTyElement(vect_dtype))) {
1966   case TY_FLOAT:
1967   case TY_DBLE:
1968     type = 'f';
1969   case TY_INT:
1970     sign = 's';
1971   case TY_UINT:
1972     if (vect_size != 2 && vect_size != 4 && vect_size != 8 && vect_size != 16)
1973       return NULL;
1974     break;
1975   case TY_SINT:
1976     sign = 's';
1977   case TY_USINT:
1978     if (vect_size != 4 && vect_size != 8 && vect_size != 16)
1979       return NULL;
1980     break;
1981   case TY_BINT:
1982     sign = 's';
1983   default:
1984     return NULL;
1985   }
1986   op1->next = op2;
1987   type_size = zsize_of(DTySeqTyElement(vect_dtype)) * 8;
1988   sprintf(buf, "@llvm.%s.v%d%c%d", mstr, vect_size, type, type_size);
1989   return gen_call_to_builtin(ilix, buf, op1, make_lltype_from_dtype(vect_dtype),
1990                              NULL, I_PICALL);
1991 }
1992 
1993 #if defined(TARGET_LLVM_POWER)
1994 static OPERAND *
gen_call_vminmax_power_intrinsic(int ilix,OPERAND * op1,OPERAND * op2)1995 gen_call_vminmax_power_intrinsic(int ilix, OPERAND *op1, OPERAND *op2)
1996 {
1997   DTYPE vect_dtype;
1998   int vect_size; /* number of elements per vector */
1999   int type_size;
2000   char *type = "sp";
2001   const char *mstr = "max";
2002   static char buf[MAXIDLEN];
2003 
2004   if (ILI_OPC(ilix) == IL_VMIN)
2005     mstr = "min";
2006   vect_dtype = ILI_DTyOPND(ilix, 3);
2007   vect_size = DTyVecLength(vect_dtype);
2008   if (vect_size != 2 && vect_size != 4)
2009     return NULL;
2010 
2011   if (vect_size == 2)
2012     type = "dp";
2013 
2014   switch (DTY(DTySeqTyElement(vect_dtype))) {
2015   case TY_FLOAT:
2016   case TY_DBLE:
2017     break;
2018   default:
2019     return NULL;
2020   }
2021   op1->next = op2;
2022   type_size = zsize_of(DTySeqTyElement(vect_dtype)) * 8;
2023   sprintf(buf, "@llvm.ppc.vsx.xv%s%s", mstr, type);
2024   return gen_call_to_builtin(ilix, buf, op1, make_lltype_from_dtype(vect_dtype),
2025                              NULL, I_PICALL);
2026 }
2027 #endif
2028 
2029 #if defined(TARGET_LLVM_ARM) && NEON_ENABLED
2030 static OPERAND *
gen_call_vminmax_neon_intrinsic(int ilix,OPERAND * op1,OPERAND * op2)2031 gen_call_vminmax_neon_intrinsic(int ilix, OPERAND *op1, OPERAND *op2)
2032 {
2033   DTYPE vect_dtype;
2034   int vect_size;
2035   int type_size;
2036   char sign = 'u';
2037   char type = 'i';
2038   char *mstr = "vmax";
2039   static char buf[MAXIDLEN];
2040 
2041   if (!NEON_ENABLED)
2042     return NULL;
2043   if (ILI_OPC(ilix) == IL_VMIN)
2044     mstr = "vmin";
2045   vect_dtype = (DTYPE)ILI_OPND(ilix, 3);
2046   vect_size = DTyVecLength(vect_dtype);
2047   switch (DTY(DTySeqTyElement(vect_dtype))) {
2048   case TY_FLOAT:
2049     type = 'f';
2050   case TY_INT:
2051     sign = 's';
2052   case TY_UINT:
2053     if (vect_size != 2 && vect_size != 4)
2054       return NULL;
2055     break;
2056   case TY_SINT:
2057     sign = 's';
2058   case TY_USINT:
2059     if (vect_size != 4 && vect_size != 8)
2060       return NULL;
2061     break;
2062   case TY_BINT:
2063     sign = 's';
2064   default:
2065     return NULL;
2066   }
2067   op1->next = op2;
2068   type_size = zsize_of(DTySeqTyElement(vect_dtype)) * 8;
2069   sprintf(buf, "@llvm.arm.neon.%s%c.v%d%c%d", mstr, sign, vect_size, type,
2070           type_size);
2071   return gen_call_to_builtin(ilix, buf, op1, make_lltype_from_dtype(vect_dtype),
2072                              NULL, I_PICALL);
2073 }
2074 #endif
2075 
2076 /* If the function being called is __builtin_alloca(n), generate an alloca
2077    instruction.  Otherwise, do nothing. */
2078 static OPERAND *
gen_alloca_call_if_necessary(SPTR sptr,int ilix)2079 gen_alloca_call_if_necessary(SPTR sptr, int ilix)
2080 {
2081   if (call_sym_is(sptr, "__builtin_alloca")) {
2082     if (size_of(DT_CPTR) == 8)
2083       return gen_llvm_instr(ilix, IL_ARGKR, make_lltype_from_dtype(DT_CPTR),
2084                             make_lltype_from_dtype(DT_INT8), I_ALLOCA);
2085     return gen_llvm_instr(ilix, IL_ARGIR, make_lltype_from_dtype(DT_CPTR),
2086                           make_lltype_from_dtype(DT_INT), I_ALLOCA);
2087   }
2088   return NULL;
2089 }
2090 
2091 static OPERAND *
gen_unreachable_if_necessary(SPTR sptr,int ilix)2092 gen_unreachable_if_necessary(SPTR sptr, int ilix)
2093 {
2094   if (call_sym_is(sptr, "__builtin_unreachable")) {
2095     ad_instr(ilix, gen_instr(I_UNREACH, NULL, NULL, NULL));
2096     return make_undef_op(make_void_lltype());
2097   }
2098   return NULL;
2099 }
2100 
2101 OPERAND *
gen_call_as_llvm_instr(SPTR sptr,int ilix)2102 gen_call_as_llvm_instr(SPTR sptr, int ilix)
2103 {
2104   OPERAND *special_call;
2105   special_call = gen_alloca_call_if_necessary(sptr, ilix);
2106   if (special_call == NULL) {
2107     special_call = gen_unreachable_if_necessary(sptr, ilix);
2108   }
2109   return special_call;
2110 }
2111 
2112 static bool
cgmain_init_call(int sptr)2113 cgmain_init_call(int sptr)
2114 {
2115   return sptr && (strncmp(SYMNAME(sptr), "__c_bzero", 9) == 0);
2116 }
2117 
2118 DTYPE
msz_dtype(MSZ msz)2119 msz_dtype(MSZ msz)
2120 {
2121   switch (msz) {
2122   case MSZ_SBYTE:
2123     return DT_BINT;
2124   case MSZ_SHWORD:
2125     return DT_SINT;
2126   case MSZ_SWORD:
2127     return DT_INT;
2128   case MSZ_SLWORD:
2129     return DT_INT;
2130   case MSZ_BYTE:
2131     return DT_BINT;
2132   case MSZ_UHWORD:
2133     return DT_USINT;
2134   case MSZ_UWORD:
2135     return DT_UINT;
2136   case MSZ_ULWORD:
2137     return DT_INT;
2138   case MSZ_FWORD:
2139     return DT_FLOAT;
2140   case MSZ_DFLWORD:
2141     return DT_DBLE;
2142   case MSZ_I8:
2143     return DT_INT8;
2144   case MSZ_PTR:
2145     return DT_CPTR;
2146   case MSZ_F16:
2147 #if defined(LONG_DOUBLE_FLOAT128)
2148     return DT_FLOAT128;
2149 #elif defined(TARGET_LLVM_X8664)
2150     return DT_128;
2151 #else
2152     return DT_QUAD;
2153 #endif
2154   case MSZ_F32:
2155     return DT_256;
2156   default:
2157     assert(0, "msz_dtype, bad value", msz, ERR_Fatal);
2158   }
2159   return DT_NONE;
2160 }
2161 
2162 /* Begin define calling conventions */
2163 #define CALLCONV                         \
2164   PRESENT(cc_default, "")                \
2165   , PRESENT(arm_aapcscc, "arm_aapcscc"), \
2166       PRESENT(arm_aapcs_vfpcc, "arm_aapcs_vfpcc")
2167 
2168 #define PRESENT(x, y) x
2169 enum calling_conventions { CALLCONV };
2170 #undef PRESENT
2171 
2172 #define PRESENT(x, y) y
2173 char *cc_as_str[] = {CALLCONV};
2174 #undef PRESENT
2175 
2176 #undef CALLCONV
2177 /* End define calling conventions */
2178 
2179 /**
2180    \brief Create and append a !dbg info metadata from \p module
2181    \param module   The module from which to get \c debug_info
2182  */
2183 static void
emit_dbg_from_module(LL_Module * module)2184 emit_dbg_from_module(LL_Module *module)
2185 {
2186   const LL_MDRef linemd = lldbg_cons_line(module->debug_info);
2187   if (!LL_MDREF_IS_NULL(linemd)) {
2188     print_dbg_line(linemd);
2189   }
2190 }
2191 
2192 static LL_Type *
fixup_x86_abi_return(LL_Type * sig)2193 fixup_x86_abi_return(LL_Type *sig)
2194 {
2195   LL_Type *rv;
2196   const unsigned numArgs = sig->sub_elements;
2197   const bool isVarArgs = (sig->flags & LL_TYPE_IS_VARARGS_FUNC) != 0;
2198   LL_Type **args = (LL_Type **)malloc(numArgs * sizeof(LL_Type *));
2199   memcpy(args, sig->sub_types, numArgs * sizeof(LL_Type *));
2200   args[0] = make_lltype_from_dtype(DT_INT);
2201   rv = ll_create_function_type(sig->module, args, numArgs - 1, isVarArgs);
2202   free(args);
2203   return rv;
2204 }
2205 
2206 #if defined(TARGET_LLVM_X8664)
2207 LL_Type *
maybe_fixup_x86_abi_return(LL_Type * sig)2208 maybe_fixup_x86_abi_return(LL_Type *sig)
2209 {
2210   if (!XBIT(183, 0x400000) && (sig->data_type == LL_PTR)) {
2211     LL_Type *pt = sig->sub_types[0];
2212     if (pt->data_type == LL_FUNCTION) {
2213       LL_Type *rt = pt->sub_types[0];
2214       if (rt->data_type == LL_I16)
2215         return ll_get_pointer_type(fixup_x86_abi_return(pt));
2216     }
2217   }
2218   return sig;
2219 }
2220 #endif
2221 
2222 /**
2223  * \brief write \c I_CALL instruction
2224  * \param curr_instr  pointer to current instruction instance
2225  * \param emit_func_signature_for_call
2226  * \return 1 if debug op was written, 0 otherwise
2227  */
2228 static int
write_I_CALL(INSTR_LIST * curr_instr,bool emit_func_signature_for_call)2229 write_I_CALL(INSTR_LIST *curr_instr, bool emit_func_signature_for_call)
2230 {
2231   /* Function invocation description as a list of OPERAND values */
2232   int i_name = curr_instr->i_name;
2233   /* get the return type of the call */
2234   LL_Type *return_type = curr_instr->ll_type;
2235   /* Get invocation description */
2236   OPERAND *call_op = curr_instr->operands;
2237   /* Debug has not been printed yet */
2238   bool dbg_line_op_written = false;
2239   bool routine_label_written = false;
2240   /* Start with default calling conventions */
2241   enum calling_conventions c_conv = cc_default;
2242   bool callRequiresTrunc = false;
2243   bool simple_callee = true;
2244   LL_Type *callee_type = call_op->ll_type;
2245   int sptr, call_dtype, param, param_dtype;
2246   char callRequiresTruncName[32];
2247 
2248   /* operand pattern:
2249    *   result (optional - only if return type of call not null)
2250    *   if var_args need to provide call signature
2251    *   call sptr (if null return type, this is the first operand)
2252    *   zero or more operands for the call arguments
2253    */
2254   print_token("\t");
2255 #if defined(TARGET_LLVM_X8664)
2256   /* by default on X86-64, a function returning INTEGER*2 is promoted to return INTEGER*4
2257      and the return value truncated.*/
2258   if (return_type->data_type == LL_I16) {
2259       callRequiresTrunc = !XBIT(183, 0x400000);
2260   }
2261 #endif
2262   assert(return_type, "write_I_CALL: missing return type for call instruction",
2263          0, ERR_Fatal);
2264   assert(call_op, "write_I_CALL: missing operand for call instruction", 0,
2265          ERR_Fatal);
2266 
2267   /* The callee is either a function pointer (before LLVM 3.7) or a
2268    * function (3.7).
2269    *
2270    * We don't have to print the entire callee type unless it is a varargs
2271    * function or a function returning a function pointer.  In the common case,
2272    * print the function return type instead of the whole function type. LLVM
2273    * will infer the rest from the arguments.
2274    *
2275    * FIXME: We still generate function calls with bad callee types that
2276    * are not function pointers:
2277    * - gen_call_to_builtin()
2278    * - gen_va_start()
2279    */
2280 
2281   /* This should really be an assertion, see above: */
2282   if (ll_type_is_pointer_to_function(callee_type)) {
2283     callee_type = callee_type->sub_types[0];
2284 
2285     /* Varargs callee => print whole function pointer type. */
2286     if (callee_type->flags & LL_TYPE_IS_VARARGS_FUNC)
2287       simple_callee = false;
2288     /* Call returns pointer to function => print whole type. */
2289     if (ll_type_is_pointer_to_function(return_type))
2290       simple_callee = false;
2291   }
2292 
2293   if (return_type->data_type != LL_VOID) {
2294     if (callRequiresTrunc) {
2295       snprintf(callRequiresTruncName, 32, "%%call.%d", expr_id);
2296       print_token(callRequiresTruncName);
2297     } else {
2298       print_tmp_name(curr_instr->tmps);
2299     }
2300     print_token(" = ");
2301   }
2302   print_token(llvm_instr_names[i_name]);
2303   print_space(1);
2304 
2305   if ((!flg.ieee || XBIT(216, 1)) && (curr_instr->flags & FAST_MATH_FLAG))
2306     print_token("fast ");
2307 
2308   /* Print calling conventions */
2309   if (curr_instr->flags & CALLCONV_MASK) {
2310     enum LL_CallConv cc = (enum LL_CallConv)(
2311         (curr_instr->flags & CALLCONV_MASK) >> CALLCONV_SHIFT);
2312     print_token(ll_get_calling_conv_str(cc));
2313     print_space(1);
2314   }
2315 
2316   sptr = call_op->val.sptr;
2317   /* write out call signature if var_args */
2318   if (curr_instr->flags & FAST_CALL) {
2319     print_token("fastcc ");
2320   }
2321 
2322   if (simple_callee) {
2323     LL_Type *retTy = return_type;
2324     /* In simple case it is sufficient to write just the return type */
2325     if (callRequiresTrunc)
2326       retTy = make_lltype_from_dtype(DT_INT);
2327     write_type(retTy);
2328   } else {
2329     LL_Type *sig =
2330         emit_func_signature_for_call ? callee_type : call_op->ll_type;
2331     if (callRequiresTrunc)
2332       sig = fixup_x86_abi_return(sig);
2333     /* Write out either function type or pointer type for the callee */
2334     write_type(sig);
2335   }
2336   print_space(1);
2337 
2338     if (!routine_label_written)
2339       write_operand(call_op, " (", FLG_OMIT_OP_TYPE);
2340     write_operands(call_op->next, 0);
2341     /* if no arguments, write out the parens */
2342     print_token(")");
2343   if (callRequiresTrunc) {
2344     print_dbg_line(curr_instr->dbg_line_op);
2345     print_token("\n\t");
2346     print_tmp_name(curr_instr->tmps);
2347     print_token(" = trunc i32 ");
2348     print_token(callRequiresTruncName);
2349     print_token(" to i16");
2350   }
2351   {
2352     const bool wrDbg = true;
2353     if (wrDbg && cpu_llvm_module->debug_info &&
2354         ll_feature_subprogram_not_in_cu(&cpu_llvm_module->ir) &&
2355         LL_MDREF_IS_NULL(curr_instr->dbg_line_op)) {
2356       /* we must emit !dbg metadata in this case */
2357       emit_dbg_from_module(cpu_llvm_module);
2358       return true;
2359     }
2360   }
2361   return dbg_line_op_written;
2362 } /* write_I_CALL */
2363 
2364 /**
2365    \brief Create the root and omnipotent pointer nodes of the TBAA tree
2366 
2367    These metadata nodes are unique per LLVM module and should be cached there.
2368  */
2369 static LL_MDRef
get_omnipotent_pointer(LL_Module * module)2370 get_omnipotent_pointer(LL_Module *module)
2371 {
2372   LL_MDRef omni = module->omnipotentPtr;
2373   if (LL_MDREF_IS_NULL(omni)) {
2374     LL_MDRef s0;
2375     LL_MDRef r0;
2376     LL_MDRef a[3];
2377     char baseBuff[32];
2378     const char *baseName = "Flang FAA";
2379     const char *const omniName = "unlimited ptr";
2380     const char *const unObjName = "unref ptr";
2381     snprintf(baseBuff, 32, "%s %x", baseName, funcId);
2382     s0 = ll_get_md_string(module, baseBuff);
2383     r0 = ll_get_md_node(module, LL_PlainMDNode, &s0, 1);
2384     a[0] = ll_get_md_string(module, unObjName);
2385     a[1] = r0;
2386     a[2] = ll_get_md_i64(module, 0);
2387     module->unrefPtr = ll_get_md_node(module, LL_PlainMDNode, a, 3);
2388     a[0] = ll_get_md_string(module, omniName);
2389     a[1] = r0;
2390     a[2] = ll_get_md_i64(module, 0);
2391     omni = ll_get_md_node(module, LL_PlainMDNode, a, 3);
2392     module->omnipotentPtr = omni;
2393   }
2394   return omni;
2395 }
2396 
2397 static bool
assumeWillAlias(int nme)2398 assumeWillAlias(int nme)
2399 {
2400   do {
2401     int sym = NME_SYM(nme);
2402     if (sym > 0) {
2403 #if defined(VARIANTG)
2404       const int variant = VARIANTG(sym);
2405       if (variant > 0)
2406         sym = variant;
2407 #endif
2408       if (NOCONFLICTG(sym) || CCSYMG(sym)) {
2409         ; /* do nothing */
2410 #if defined(PTRSAFEG)
2411       } else if (PTRSAFEG(sym)) {
2412         ; /* do nothing */
2413 #endif
2414       } else if (DTY(DTYPEG(sym)) == TY_PTR) {
2415         return true;
2416 #if defined(POINTERG)
2417       } else if (POINTERG(sym)) {
2418         return true;
2419 #endif
2420       }
2421     }
2422     switch (NME_TYPE(nme)) {
2423     default:
2424       return false;
2425     case NT_MEM:
2426     case NT_IND:
2427     case NT_ARR:
2428     case NT_SAFE:
2429       nme = NME_NM(nme);
2430       break;
2431     }
2432   } while (nme != 0);
2433   return false;
2434 }
2435 
2436 /**
2437    \brief Fortran location set to "TBAA" translation
2438 
2439    In Fortran, there isn't any TBAA. But we use the LLVM mechanism to hint to
2440    the backend what may alias.
2441  */
2442 static LL_MDRef
locset_to_tbaa_info(LL_Module * module,LL_MDRef omniPtr,int ilix)2443 locset_to_tbaa_info(LL_Module *module, LL_MDRef omniPtr, int ilix)
2444 {
2445   const int NAME_SZ = 32;
2446   char name[NAME_SZ];
2447   LL_MDRef a[3];
2448   int bsym, rv;
2449   const ILI_OP opc = ILI_OPC(ilix);
2450   const ILTY_KIND ty = IL_TYPE(opc);
2451   const int nme = ILI_OPND(ilix, (ty == ILTY_LOAD) ? 2 : 3);
2452   const int base = basenme_of(nme);
2453 
2454   if (!base)
2455     return omniPtr;
2456 
2457   bsym = NME_SYM(base);
2458   switch (STYPEG(bsym)) {
2459   case ST_VAR:
2460   case ST_ARRAY:
2461   case ST_STRUCT:
2462     /* do nothing */
2463     break;
2464   default:
2465     return module->unrefPtr;
2466   }
2467 
2468   if (!strncmp(SYMNAME(bsym), "reshap$r", 8))
2469     return LL_MDREF_ctor(0, 0);
2470   if ((NME_SYM(nme) != bsym) && assumeWillAlias(nme))
2471     return omniPtr;
2472 
2473 #if defined(REVMIDLNKG)
2474   if (REVMIDLNKG(bsym)) {
2475     const int ptr = REVMIDLNKG(bsym);
2476     if (!NOCONFLICTG(ptr) && !PTRSAFEG(ptr) && !TARGETG(ptr))
2477       return LL_MDREF_ctor(0, 0);
2478     bsym = ptr;
2479   }
2480 #endif
2481 
2482   if (NOCONFLICTG(bsym) || CCSYMG(bsym)) {
2483     ; /* do nothing */
2484 #if defined(PTRSAFEG)
2485   } else if (PTRSAFEG(bsym)) {
2486     ; /* do nothing */
2487 #endif
2488   } else if (DTY(DTYPEG(bsym)) == TY_PTR) {
2489     return omniPtr;
2490 #if defined(POINTERG)
2491   } else if (POINTERG(bsym)) {
2492     return omniPtr;
2493 #endif
2494   }
2495 
2496 #if defined(SOCPTRG)
2497   if (SOCPTRG(bsym)) {
2498     int ysoc = SOCPTRG(bsym);
2499     while (SOC_NEXT(ysoc))
2500       ysoc = SOC_NEXT(ysoc);
2501     rv = snprintf(name, NAME_SZ, "s%x.%x", funcId, ysoc);
2502     DEBUG_ASSERT(rv < NAME_SZ, "buffer overrun");
2503     a[0] = ll_get_md_string(module, name);
2504     a[1] = omniPtr;
2505     a[2] = ll_get_md_i64(module, 0);
2506     return ll_get_md_node(module, LL_PlainMDNode, a, 3);
2507   }
2508 #endif
2509   /* variable can't alias type-wise. It's Fortran! */
2510   rv = snprintf(name, NAME_SZ, "t%x.%x", funcId, base);
2511   DEBUG_ASSERT(rv < NAME_SZ, "buffer overrun");
2512   a[0] = ll_get_md_string(module, name);
2513   a[1] = omniPtr;
2514   a[2] = ll_get_md_i64(module, 0);
2515   return ll_get_md_node(module, LL_PlainMDNode, a, 3);
2516 }
2517 
2518 /**
2519    \brief Write TBAA metadata for the address \p opnd
2520    \param module  The module
2521    \param opnd  a pointer to a typed location
2522    \param isVol   Is this a volatile access?
2523 
2524    To do this correctly for C, we have use the effective type.
2525  */
2526 static LL_MDRef
get_tbaa_metadata(LL_Module * module,int ilix,OPERAND * opnd,bool isVol)2527 get_tbaa_metadata(LL_Module *module, int ilix, OPERAND *opnd, bool isVol)
2528 {
2529   LL_MDRef a[3];
2530   LL_MDRef myPtr, omniPtr;
2531   LL_Type *ty;
2532 
2533   ty = opnd->ll_type;
2534   assert(ty->data_type == LL_PTR, "must be a ptr", ty->data_type, ERR_Fatal);
2535   omniPtr = get_omnipotent_pointer(module);
2536 
2537   /* volatile memory access aliases all */
2538   if (isVol)
2539     return omniPtr;
2540 
2541   ty = ty->sub_types[0];
2542   assert(ty->data_type != LL_NOTYPE, "must be a type", 0, ERR_Fatal);
2543 
2544   myPtr = locset_to_tbaa_info(module, omniPtr, ilix);
2545 
2546 cons_indirect:
2547   if (!myPtr)
2548     return myPtr;
2549 
2550   a[0] = a[1] = myPtr;
2551   a[2] = ll_get_md_i64(module, 0);
2552   return ll_get_md_node(module, LL_PlainMDNode, a, 3);
2553 }
2554 
2555 /**
2556    \brief Is TBAA disabled?
2557  */
2558 INLINE static bool
tbaa_disabled(void)2559 tbaa_disabled(void)
2560 {
2561 #ifdef OMP_OFFLOAD_LLVM
2562   /* Always disable tbaa for device code. */
2563   if (ISNVVMCODEGEN)
2564     return true;
2565 #endif
2566   return (flg.opt < 2) || XBIT(183, 0x20000);
2567 }
2568 
2569 /**
2570    \brief Write out the TBAA metadata, if needed
2571  */
2572 static void
write_tbaa_metadata(LL_Module * mod,int ilix,OPERAND * opnd,int flags)2573 write_tbaa_metadata(LL_Module *mod, int ilix, OPERAND *opnd, int flags)
2574 {
2575   if (!tbaa_disabled()) {
2576     const bool isVol = (flags & VOLATILE_FLAG) != 0;
2577     LL_MDRef md = get_tbaa_metadata(mod, ilix, opnd, isVol);
2578     if (!LL_MDREF_IS_NULL(md)) {
2579       print_token(", !tbaa ");
2580       write_mdref(gbl.asmfil, mod, md, 1);
2581     }
2582   }
2583 }
2584 
2585 /**
2586    \brief Test for improperly constructed instruction streams
2587    \param insn   The instruction under the cursor
2588    \return true  iff we don't need to emit a dummy label
2589  */
2590 INLINE static int
dont_force_a_dummy_label(INSTR_LIST * insn)2591 dont_force_a_dummy_label(INSTR_LIST *insn)
2592 {
2593   const int i_name = insn->i_name;
2594   if (i_name == I_NONE) {
2595     /* insn is a label, no need for a dummy */
2596     return true;
2597   }
2598   if ((i_name == I_BR) && insn->next && (insn->next->i_name == I_NONE)) {
2599     /* odd case: two terminators appearing back-to-back followed by a
2600        label. write_instructions() will skip over this 'insn' and
2601        emit the next one. Don't emit two labels back-to-back. */
2602     return true;
2603   }
2604   return false;
2605 }
2606 
2607 /**
2608    \brief For the given instruction, write [singlethread] <memory ordering>
2609    to the LLVM IR output file.
2610  */
2611 static void
write_memory_order(INSTR_LIST * instrs)2612 write_memory_order(INSTR_LIST *instrs)
2613 {
2614   if (instrs->flags & ATOMIC_SINGLETHREAD_FLAG) {
2615     print_token(" singlethread");
2616   }
2617   print_space(1);
2618   print_token(get_atomic_memory_order_name(instrs->flags));
2619 }
2620 
2621 /**
2622    \brief For the given instruction, write [singlethread] <memory ordering>,
2623    <alignment> to the LLVM IR output file.
2624  */
2625 static void
write_memory_order_and_alignment(INSTR_LIST * instrs)2626 write_memory_order_and_alignment(INSTR_LIST *instrs)
2627 {
2628   int align;
2629   DEBUG_ASSERT(
2630       instrs->i_name == I_LOAD || instrs->i_name == I_STORE,
2631       "write_memory_order_and_alignment: not a load or store instruction");
2632 
2633   /* Print memory order if instruction is atomic. */
2634   if (instrs->flags & ATOMIC_MEM_ORD_FLAGS) {
2635     write_memory_order(instrs);
2636   } else {
2637     DEBUG_ASSERT(
2638         (instrs->flags & ATOMIC_SINGLETHREAD_FLAG) == 0,
2639         "write_memory_order_and_alignment: inappropriate singlethread");
2640   }
2641 
2642   /* Extract the alignment in bytes from the flags field. It's stored as
2643    * log2(bytes). */
2644   align = LDST_BYTEALIGN(instrs->flags);
2645   if (align) {
2646     char align_token[4];
2647     print_token(", align ");
2648     sprintf(align_token, "%d", align);
2649     print_token(align_token);
2650   }
2651 }
2652 
2653 INLINE static void
write_no_depcheck_metadata(LL_Module * module,INSTR_LIST * insn)2654 write_no_depcheck_metadata(LL_Module *module, INSTR_LIST *insn)
2655 {
2656   if (insn->flags & LDST_HAS_METADATA) {
2657     char buf[64];
2658     int n;
2659     DEBUG_ASSERT(insn->misc_metadata, "missing metadata");
2660     n = snprintf(buf, 64, ", !llvm.mem.parallel_loop_access !%u",
2661                  LL_MDREF_value(insn->misc_metadata));
2662     DEBUG_ASSERT(n < 64, "buffer overrun");
2663     print_token(buf);
2664   }
2665 }
2666 
2667 /* write out the struct member types */
2668 static void
write_verbose_type(LL_Type * ll_type)2669 write_verbose_type(LL_Type *ll_type)
2670 {
2671   print_token(ll_type->str);
2672 }
2673 
2674 /**
2675    \brief Write the instruction list to the LLVM IR output file
2676  */
2677 static void
write_instructions(LL_Module * module)2678 write_instructions(LL_Module *module)
2679 {
2680   INSTR_LIST *instrs;
2681   OPERAND *p, *call_op, *p1;
2682   DTYPE call_dtype, param_dtype;
2683   LL_InstrName i_name;
2684   int align;
2685   SPTR sptr;
2686   int param;
2687   bool forceLabel = true;
2688   bool dbg_line_op_written;
2689   bool routine_label_written;
2690 
2691   DBGTRACEIN("")
2692 
2693   for (instrs = Instructions; instrs; instrs = instrs->next) {
2694     llvm_info.curr_instr = instrs;
2695     i_name = instrs->i_name;
2696     dbg_line_op_written = false;
2697 
2698     asrt(i_name >= 0 && i_name < I_LAST);
2699     DBGTRACE3("#instruction(%d) %s for ilix %d\n", i_name,
2700               llvm_instr_names[i_name], instrs->ilix);
2701 
2702     if (dont_force_a_dummy_label(instrs))
2703       forceLabel = false;
2704     if (forceLabel) {
2705       char buff[32];
2706       static unsigned counter = 0;
2707       snprintf(buff, 32, "L.dead%u:\n", counter++);
2708       print_token(buff);
2709       forceLabel = false;
2710     }
2711     if (instrs->flags & CANCEL_CALL_DBG_VALUE) {
2712       DBGTRACE("#instruction llvm.dbg.value canceled")
2713       continue;
2714     } else if (BINOP(i_name) || BITOP(i_name)) {
2715       print_token("\t");
2716       print_tmp_name(instrs->tmps);
2717       print_token(" = ");
2718       print_token(llvm_instr_names[i_name]);
2719       if ((!flg.ieee) || XBIT(216, 1))
2720         switch (i_name) {
2721         case I_FADD:
2722           if (XBIT(216, 2))
2723             break;
2724         case I_FDIV:
2725           if (XBIT(216, 4))
2726             break;
2727         case I_FSUB:
2728         case I_FMUL:
2729         case I_FREM:
2730           print_token(" fast");
2731           break;
2732         default:
2733           break;
2734         }
2735       p = instrs->operands;
2736       assert(p->ll_type, "write_instruction(): missing binary type", 0,
2737              ERR_Fatal);
2738       asrt(match_types(instrs->ll_type, p->ll_type) == MATCH_OK);
2739       print_space(1);
2740       /* write_type(p->ll_type); */
2741       write_type(instrs->ll_type);
2742       print_space(1);
2743       write_operand(p, ", ", FLG_OMIT_OP_TYPE);
2744       p = p->next;
2745       assert(p->ll_type, "write_instruction(): missing binary type", 0,
2746              ERR_Fatal);
2747       asrt(match_types(instrs->ll_type, p->ll_type) == MATCH_OK);
2748       write_operand(p, "", FLG_OMIT_OP_TYPE);
2749     } else if (CONVERT(i_name)) {
2750       p = instrs->operands;
2751       assert(p->next == NULL, "write_instructions(),bad next ptr", 0,
2752              ERR_Fatal);
2753       print_token("\t");
2754       print_tmp_name(instrs->tmps);
2755       print_token(" = ");
2756       print_token(llvm_instr_names[i_name]);
2757       print_space(1);
2758 #if defined(PGFTN) && defined(TARGET_LLVM_X8664)
2759       write_operand(p, " to ", FLG_FIXUP_RETURN_TYPE);
2760       write_type(maybe_fixup_x86_abi_return(instrs->ll_type));
2761 #else
2762       write_operand(p, " to ", 0);
2763       write_type(instrs->ll_type);
2764 #endif
2765     } else {
2766       switch (i_name) {
2767       case I_NONE: /* should be a label */
2768         forceLabel = false;
2769         sptr = instrs->operands->val.sptr;
2770         if (instrs->prev == NULL && sptr == 0) {
2771           /* entry label we just ignore it*/
2772           break;
2773         }
2774         assert(sptr, "write_instructions(): missing symbol", 0, ERR_Fatal);
2775         if (sptr != instrs->operands->val.sptr)
2776           printf("sptr mixup sptr= %d, val = %d\n", sptr,
2777                  instrs->operands->val.sptr);
2778         /* Every label must be immediately preceded by a branch or other
2779            terminal instruction. */
2780         if (!INSTR_PREV(instrs) || !INSTR_IS_TERMINAL(INSTR_PREV(instrs))) {
2781           print_token("\t");
2782           print_token(llvm_instr_names[I_BR]);
2783           print_token(" label %L");
2784           print_token(get_llvm_name(sptr));
2785           print_nl();
2786         }
2787 
2788         write_operand(instrs->operands, "", 0);
2789         /* if label is last instruction in the module we need
2790          * a return instruction as llvm requires a termination
2791          * instruction at the end of the block.
2792          */
2793         if (!instrs->next) {
2794           print_nl();
2795           print_token("\t");
2796           print_token(llvm_instr_names[I_RET]);
2797           print_space(1);
2798           if (has_multiple_entries(gbl.currsub)) {
2799             if (gbl.arets)
2800               llvm_info.return_ll_type = make_lltype_from_dtype(DT_INT);
2801             else
2802               llvm_info.return_ll_type = make_lltype_from_dtype(DT_NONE);
2803           }
2804           write_type(llvm_info.abi_info->extend_abi_return
2805                          ? make_lltype_from_dtype(DT_INT)
2806                          : llvm_info.return_ll_type);
2807           if (llvm_info.return_ll_type->data_type != LL_VOID) {
2808             switch (llvm_info.return_ll_type->data_type) {
2809             case LL_PTR:
2810               print_token(" null");
2811               break;
2812             case LL_I1:
2813             case LL_I8:
2814             case LL_I16:
2815             case LL_I24:
2816             case LL_I32:
2817             case LL_I40:
2818             case LL_I48:
2819             case LL_I56:
2820             case LL_I64:
2821             case LL_I128:
2822             case LL_I256:
2823               print_token(" 0");
2824               break;
2825             case LL_DOUBLE:
2826             case LL_FLOAT:
2827               print_token(" 0.0");
2828               break;
2829             case LL_X86_FP80:
2830               print_token(" 0xK00000000000000000000");
2831               break;
2832             case LL_FP128:
2833               print_token(" 0xL00000000000000000000000000000000");
2834               break;
2835             case LL_PPC_FP128:
2836               print_token(" 0xM00000000000000000000000000000000");
2837               break;
2838             default:
2839               print_token(" zeroinitializer");
2840             }
2841           }
2842         }
2843         break;
2844       case I_EXTRACTVAL:
2845       case I_INSERTVAL: {
2846         /* extractvalue lhs, rhs, int
2847          * lhs = extractvalue rhs_type rhs, int
2848          * lhs = insertvalue rhs_type rhs, int
2849          */
2850         OPERAND *cc = instrs->operands;
2851         print_token("\t");
2852         print_tmp_name(instrs->tmps);
2853         print_token(" = ");
2854         print_token(llvm_instr_names[i_name]);
2855         print_space(1);
2856         write_verbose_type(instrs->ll_type);
2857         print_space(1);
2858         write_operand(cc, ", ", FLG_OMIT_OP_TYPE);
2859         cc = cc->next;
2860         if (i_name == I_INSERTVAL) {
2861           write_operand(cc, ", ", 0);
2862           cc = cc->next;
2863         }
2864         write_operand(cc, "", FLG_OMIT_OP_TYPE);
2865       } break;
2866       case I_RESUME: {
2867         /* resume { i8*, i32 } %33 */
2868         OPERAND *cc;
2869         forceLabel = true; // is needed here
2870         cc = instrs->operands;
2871         print_token("\t");
2872         print_token(llvm_instr_names[I_RESUME]);
2873         print_space(1);
2874         write_verbose_type(cc->ll_type);
2875         print_space(1);
2876         write_operand(cc, " ", FLG_OMIT_OP_TYPE);
2877       } break;
2878       case I_CLEANUP:
2879         print_token("\t");
2880         print_token(llvm_instr_names[I_CLEANUP]);
2881         break;
2882       case I_LANDINGPAD:
2883         /* landingpad: typeinfo_var, catch_clause_sptr,
2884          * caught_object_sptr
2885          */
2886         /* LABEL */
2887         print_token("\t");
2888         print_tmp_name(instrs->tmps);
2889         print_token(" = ");
2890         print_token(llvm_instr_names[I_LANDINGPAD]);
2891         print_space(1);
2892         write_verbose_type(instrs->ll_type);
2893         if (ll_feature_eh_personality_on_landingpad(&module->ir))
2894           print_personality();
2895         dbg_line_op_written = true;
2896         break;
2897       case I_CATCH: {
2898         OPERAND *cc;
2899         cc = instrs->operands;
2900 
2901         if (cc->ot_type == OT_CONSTVAL) {
2902           print_token("\tcatch i8* ");
2903           write_operand(cc, " ", FLG_OMIT_OP_TYPE);
2904         } else {
2905           print_token("\tcatch i8* bitcast ( ");
2906           write_type(cc->ll_type);
2907           print_token("* ");
2908           write_operand(cc, " ", FLG_OMIT_OP_TYPE);
2909           print_token(" to i8*)");
2910         }
2911       } break;
2912       case I_FILTER: {
2913         /* "filter <array-type> [ <array-of-typeinfo-vars> ]"
2914            Each operand is a typeinfo variable for a type in the exception
2915            specification. */
2916         if (instrs->operands == NULL) {
2917           /* A no-throw exception spec, "throw()" */
2918           /* LLVM documentation says that "filter [0xi8*] undef" is fine, but
2919              the LLVM compiler rejects it.  So we have to do it differently. */
2920           print_token("\t\tfilter [0 x i8*] zeroinitializer");
2921         } else {
2922           OPERAND *esti;       /* One typeinfo var for the exception spec. */
2923           int count = 0;       /* Number of types in the exception spec. */
2924           char buffer[19 + 9]; /* Format string + small integer */
2925           for (esti = instrs->operands; esti != NULL; esti = esti->next) {
2926             ++count;
2927           }
2928           snprintf(buffer, sizeof buffer, "\tfilter [%d x i8*] [", count);
2929           print_token(buffer);
2930           for (esti = instrs->operands; esti != NULL; esti = esti->next) {
2931             print_token("i8* bitcast ( ");
2932             write_type(esti->ll_type);
2933             print_token("* ");
2934             write_operand(esti, NULL, FLG_OMIT_OP_TYPE);
2935             print_token(" to i8*)");
2936             if (esti->next != NULL) {
2937               print_token(", ");
2938             }
2939           }
2940           print_token("]");
2941         }
2942         break;
2943       }
2944       case I_INVOKE:
2945       /* forceLabel = true; is not needed here, already handled */
2946       case I_PICALL:
2947       case I_CALL:
2948         dbg_line_op_written = write_I_CALL(
2949             instrs, ll_feature_emit_func_signature_for_call(&module->ir));
2950         break;
2951       case I_SW:
2952         forceLabel = true;
2953         p = instrs->operands;
2954         print_token("\t");
2955         print_token(llvm_instr_names[i_name]);
2956         print_space(1);
2957         write_operand(p, ", ", 0);
2958         write_operand(p->next, "[\n\t\t", 0);
2959         p1 = p->next->next;
2960         while (p1) {
2961           write_operand(p1, ", ", 0);
2962           p1 = p1->next;
2963           if (p1) {
2964             write_operand(p1, "\n\t\t", 0);
2965             p1 = p1->next;
2966           }
2967         }
2968         print_token("]");
2969         break;
2970       case I_RET:
2971         forceLabel = true;
2972         p = instrs->operands;
2973         if (XBIT(129, 0x800)) {
2974           /* -finstrument-functions */
2975           write_profile_exit();
2976         }
2977         /* This is a way to return value for multiple entries with return type
2978          * pass as argument to the master/common routine */
2979         if (has_multiple_entries(gbl.currsub) && FVALG(gbl.currsub) &&
2980             SCG(FVALG(gbl.currsub)) != SC_DUMMY) {
2981           /* (1) bitcast result(second argument) from i8* to type of p->ll_type
2982            * (2) store result into (1)
2983            * (3) return void.
2984            */
2985           store_return_value_for_entry(p, i_name);
2986           break;
2987         }
2988         print_token("\t");
2989         print_token(llvm_instr_names[i_name]);
2990         print_space(1);
2991         write_type(llvm_info.abi_info->extend_abi_return
2992                        ? make_lltype_from_dtype(DT_INT)
2993                        : llvm_info.return_ll_type);
2994         /*  If a function return type is VOID, we don't have to
2995          *  append any operands after LLVM instruction "ret void" */
2996         if (llvm_info.return_ll_type->data_type != LL_VOID &&
2997             (p->ot_type != OT_NONE) && (p->ll_type->data_type != LL_VOID)) {
2998           print_space(1);
2999           write_operand(p, "", FLG_OMIT_OP_TYPE);
3000           assert(p->next == NULL, "write_instructions(), bad next ptr", 0,
3001                  ERR_Fatal);
3002         }
3003         break;
3004       case I_LOAD:
3005         p = instrs->operands;
3006         print_token("\t");
3007         print_tmp_name(instrs->tmps);
3008         print_token(" = ");
3009         print_token(llvm_instr_names[i_name]);
3010         print_space(1);
3011         if (instrs->flags & ATOMIC_MEM_ORD_FLAGS) {
3012           print_token("atomic ");
3013         }
3014         if (instrs->flags & VOLATILE_FLAG) {
3015           print_token("volatile ");
3016         }
3017 
3018         /* Print out the loaded type. */
3019         if (ll_feature_explicit_gep_load_type(&module->ir)) {
3020           LL_Type *t = p->ll_type;
3021           assert(t && t->data_type == LL_PTR, "load operand must be a pointer",
3022                  0, ERR_Fatal);
3023           t = t->sub_types[0];
3024           print_token(t->str);
3025           print_token(", ");
3026         }
3027 
3028         /* Print out the pointer operand. */
3029         write_operand(p, "", 0);
3030 
3031         write_memory_order_and_alignment(instrs);
3032 
3033         assert(p->next == NULL, "write_instructions(), bad next ptr", 0,
3034                ERR_Fatal);
3035         write_no_depcheck_metadata(module, instrs);
3036         write_tbaa_metadata(module, instrs->ilix, instrs->operands,
3037                             instrs->flags);
3038         break;
3039       case I_STORE:
3040         p = instrs->operands;
3041         print_token("\t");
3042         print_token(llvm_instr_names[i_name]);
3043         print_space(1);
3044         if (instrs->flags & ATOMIC_MEM_ORD_FLAGS) {
3045           print_token("atomic ");
3046         }
3047         if (instrs->flags & VOLATILE_FLAG) {
3048           print_token("volatile ");
3049         }
3050         write_operand(p, ", ", 0);
3051         p = p->next;
3052         write_operand(p, "", 0);
3053 
3054         write_memory_order_and_alignment(instrs);
3055         write_no_depcheck_metadata(module, instrs);
3056         write_tbaa_metadata(module, instrs->ilix, instrs->operands->next,
3057                             instrs->flags & VOLATILE_FLAG);
3058         break;
3059       case I_BR:
3060         if (!INSTR_PREV(instrs) || !INSTR_IS_TERMINAL(INSTR_PREV(instrs))) {
3061           forceLabel = true;
3062           print_token("\t");
3063           print_token(llvm_instr_names[i_name]);
3064           print_space(1);
3065           write_operands(instrs->operands, 0);
3066           if (instrs->flags & SIMD_BACKEDGE_FLAG) {
3067             char buf[32];
3068             LL_MDRef loop_md = instrs->misc_metadata;
3069             snprintf(buf, 32, ", !llvm.loop !%u", LL_MDREF_value(loop_md));
3070             print_token(buf);
3071           }
3072         } else {
3073           /* The branch is dead code.  Don't write it out.  And don't write out
3074              the debug information either. */
3075           dbg_line_op_written = true;
3076         }
3077         break;
3078       case I_INDBR:
3079         forceLabel = true;
3080         print_token("\t");
3081         print_token(llvm_instr_names[i_name]);
3082         print_space(1);
3083         write_operands(instrs->operands, 0);
3084         break;
3085       case I_GEP:
3086         p = instrs->operands;
3087         print_token("\t");
3088         print_tmp_name(instrs->tmps);
3089         print_token(" = ");
3090         print_token(llvm_instr_names[i_name]);
3091         print_space(1);
3092 
3093         /* Print out the indexed type. */
3094         if (ll_feature_explicit_gep_load_type(&module->ir)) {
3095           LL_Type *t = p->ll_type;
3096           assert(t && t->data_type == LL_PTR, "gep operand must be a pointer",
3097                  0, ERR_Fatal);
3098           t = t->sub_types[0];
3099           print_token(t->str);
3100           print_token(", ");
3101         }
3102 
3103         write_operands(p, FLG_AS_UNSIGNED);
3104         break;
3105       case I_VA_ARG:
3106         p = instrs->operands;
3107         print_token("\t");
3108         print_tmp_name(instrs->tmps);
3109         print_token(" = ");
3110         print_token(llvm_instr_names[i_name]);
3111         print_space(1);
3112         write_operand(p, "", 0);
3113         write_type(instrs->ll_type);
3114         break;
3115       case I_DECL:
3116         break;
3117       case I_FCMP:
3118       case I_ICMP:
3119         print_token("\t");
3120         print_tmp_name(instrs->tmps);
3121         print_token(" = ");
3122         print_token(llvm_instr_names[i_name]);
3123         if ((i_name == I_FCMP) && ((!flg.ieee) || XBIT(216, 1)))
3124           print_token(" fast");
3125         print_space(1);
3126         p = instrs->operands;
3127         write_operand(p, " ", 0);
3128         /* use the type of the comparison operators */
3129         write_type(instrs->operands->next->ll_type);
3130         print_space(1);
3131         p = p->next;
3132         write_operand(p, ", ", FLG_OMIT_OP_TYPE);
3133         p = p->next;
3134         write_operand(p, "", FLG_OMIT_OP_TYPE);
3135         break;
3136       case I_ALLOCA:
3137         print_token("\t");
3138         print_tmp_name(instrs->tmps);
3139         print_token(" = ");
3140         print_token(llvm_instr_names[i_name]);
3141         print_space(1);
3142         write_type(instrs->ll_type->sub_types[0]);
3143         p = instrs->operands;
3144         if (p) {
3145           print_token(", ");
3146           write_operand(p, "", 0);
3147         }
3148         break;
3149       case I_SELECT:
3150       case I_EXTELE:
3151       case I_INSELE:
3152       case I_SHUFFVEC:
3153         print_token("\t");
3154         print_tmp_name(instrs->tmps);
3155         print_token(" = ");
3156         print_token(llvm_instr_names[i_name]);
3157         p = instrs->operands;
3158         print_space(1);
3159         write_operands(p, 0);
3160         break;
3161       case I_BARRIER:
3162         print_token("\t");
3163         print_token(llvm_instr_names[i_name]);
3164         print_space(1);
3165         print_token("acq_rel");
3166         break;
3167       case I_ATOMICRMW:
3168         print_token("\t");
3169         print_tmp_name(instrs->tmps);
3170         print_token(" = ");
3171         print_token(llvm_instr_names[i_name]);
3172         print_space(1);
3173         print_token(get_atomicrmw_opname(instrs->flags));
3174         print_space(1);
3175         write_operands(instrs->operands, 0);
3176         write_memory_order(instrs);
3177         break;
3178       case I_CMPXCHG:
3179         print_token("\t");
3180         print_tmp_name(instrs->tmps);
3181         print_token(" = ");
3182         print_token(llvm_instr_names[i_name]);
3183         print_space(1);
3184         write_operands(instrs->operands, 0);
3185         print_space(1);
3186         print_token(get_atomic_memory_order_name(instrs->flags));
3187         print_space(1);
3188         print_token(get_atomic_memory_order_name(
3189             FROM_CMPXCHG_MEMORDER_FAIL(instrs->flags)));
3190         break;
3191       case I_FENCE:
3192         print_token("\t");
3193         print_token(llvm_instr_names[i_name]);
3194         write_memory_order(instrs);
3195         break;
3196       case I_UNREACH:
3197         forceLabel = true;
3198         print_token("\t");
3199         print_token(llvm_instr_names[i_name]);
3200         break;
3201       default:
3202         DBGTRACE1("### write_instructions(): unknown instr name: %s",
3203                   llvm_instr_names[i_name])
3204         assert(0, "write_instructions(): unknown instr name", instrs->i_name,
3205                ERR_Fatal);
3206       }
3207     }
3208     if (!ISNVVMCODEGEN &&
3209         (!LL_MDREF_IS_NULL(instrs->dbg_line_op) && !dbg_line_op_written)) {
3210       print_dbg_line(instrs->dbg_line_op);
3211     }
3212 #if DEBUG
3213     if (instrs->traceComment) {
3214       print_token("\t\t;) ");
3215       print_token(instrs->traceComment);
3216     }
3217     if (XBIT(183, 0x800)) {
3218       char buf[200];
3219 
3220       if (instrs->tmps)
3221         sprintf(buf, "\t\t\t; ilix %d, usect %d", instrs->ilix,
3222                 instrs->tmps->use_count);
3223       else
3224         sprintf(buf, "\t\t\t; ilix %d", instrs->ilix);
3225 
3226       if (instrs->flags & DELETABLE)
3227         strcat(buf, " deletable");
3228       if (instrs->flags & STARTEBB)
3229         strcat(buf, " startebb");
3230       if (instrs->flags & ROOTDG)
3231         strcat(buf, " rootdg");
3232       print_token(buf);
3233       sprintf(buf, "\ti%d", instrs->rank);
3234       print_token(buf);
3235     }
3236 #endif
3237     print_nl();
3238   }
3239 
3240   DBGTRACEOUT("")
3241 } /* write_instructions */
3242 
3243 OPERAND *
mk_alloca_instr(LL_Type * ptrTy)3244 mk_alloca_instr(LL_Type *ptrTy)
3245 {
3246   OPERAND *op = make_tmp_op(ptrTy, make_tmps());
3247   INSTR_LIST *insn = gen_instr(I_ALLOCA, op->tmps, ptrTy, NULL);
3248   ad_instr(0, insn);
3249   return op;
3250 }
3251 
3252 INSTR_LIST *
mk_store_instr(OPERAND * val,OPERAND * addr)3253 mk_store_instr(OPERAND *val, OPERAND *addr)
3254 {
3255   INSTR_LIST *insn;
3256   val->next = addr;
3257   insn = gen_instr(I_STORE, NULL, NULL, val);
3258   if (rw_nodepcheck) {
3259     insn->flags |= LDST_HAS_METADATA;
3260     insn->misc_metadata = cons_no_depchk_metadata();
3261   }
3262   ad_instr(0, insn);
3263   return insn;
3264 }
3265 
3266 #if DEBUG
3267 void
dump_type_for_debug(LL_Type * ll_type)3268 dump_type_for_debug(LL_Type *ll_type)
3269 {
3270   if (ll_type == NULL) {
3271     fprintf(ll_dfile, "(UNKNOWN)\n");
3272   }
3273   fprintf(ll_dfile, "%s\n", ll_type->str);
3274 }
3275 #endif
3276 
3277 /* create the instructions for:
3278  * 	%new_tmp =  extractvalue { struct mems } %tmp, index
3279  *  or
3280  * 	%new_tmp =  insertvalue { struct mems } %tmp, tmp2_type tmp2, index
3281 
3282  */
3283 TMPS *
gen_extract_insert(LL_InstrName i_name,LL_Type * struct_type,TMPS * tmp,LL_Type * tmp_type,TMPS * tmp2,LL_Type * tmp2_type,int index)3284 gen_extract_insert(LL_InstrName i_name, LL_Type *struct_type, TMPS *tmp,
3285                    LL_Type *tmp_type, TMPS *tmp2, LL_Type *tmp2_type, int index)
3286 {
3287   OPERAND *cc;
3288   int num[2];
3289   TMPS *new_tmp;
3290   INSTR_LIST *Curr_Instr;
3291 
3292   /* %new_tmp =  extractvalue { struct mems } %tmp, index */
3293   if (tmp)
3294     cc = make_tmp_op(tmp_type, tmp);
3295   else
3296     cc = make_undef_op(tmp_type);
3297   new_tmp = make_tmps();
3298   Curr_Instr = gen_instr(i_name, new_tmp, struct_type, cc);
3299 
3300   /* insertval requires one more temp */
3301   if (tmp2) {
3302     cc->next = make_tmp_op(tmp2_type, tmp2);
3303     cc = cc->next;
3304   }
3305   cc->next = make_operand();
3306   cc = cc->next;
3307 
3308   cc->ot_type = OT_CONSTSPTR;
3309   cc->ll_type = make_lltype_from_dtype(DT_INT);
3310   switch (index) {
3311   case 0:
3312     cc->val.sptr = stb.i0;
3313     break;
3314   case 1:
3315     cc->val.sptr = stb.i1;
3316     break;
3317   default:
3318     num[0] = 0;
3319     num[1] = index;
3320     cc->val.sptr = getcon(num, DT_INT);
3321     break;
3322   }
3323 
3324   ad_instr(0, Curr_Instr);
3325   return new_tmp;
3326 }
3327 
3328 /**
3329    \brief Generate an insertvalue instruction
3330    \param aggr    an aggregate (destination object)
3331    \param elem    an element (item to be inserted)
3332    \param index   index to insert element
3333  */
3334 static OPERAND *
gen_insert_value(OPERAND * aggr,OPERAND * elem,unsigned index)3335 gen_insert_value(OPERAND *aggr, OPERAND *elem, unsigned index)
3336 {
3337   aggr->next = elem;
3338   elem->next = make_constval32_op(index);
3339   return ad_csed_instr(I_INSERTVAL, 0, aggr->ll_type, aggr, InstrListFlagsNull,
3340                        false);
3341 }
3342 
3343 static void
gen_store_instr(SPTR sptr_lhs,TMPS * tmp,LL_Type * tmp_type)3344 gen_store_instr(SPTR sptr_lhs, TMPS *tmp, LL_Type *tmp_type)
3345 {
3346   INSTR_LIST *Curr_Instr;
3347   OPERAND *addr = make_operand();
3348 
3349   addr->val.sptr = sptr_lhs;
3350   addr->ot_type = OT_VAR;
3351   addr->ll_type = make_ptr_lltype(make_lltype_from_dtype(DTYPEG(sptr_lhs)));
3352   Curr_Instr = mk_store_instr(make_tmp_op(tmp_type, tmp), addr);
3353   set_llvm_sptr_name(addr);
3354 }
3355 
3356 /**
3357    \brief Construct an \c INSTR_LIST object
3358 
3359    Initializes fields i_name (and dbg_line_op if appropriate).  Zeros the other
3360    fields.
3361  */
3362 static INSTR_LIST *
make_instr(LL_InstrName instr_name)3363 make_instr(LL_InstrName instr_name)
3364 {
3365   INSTR_LIST *iptr;
3366 
3367   iptr = (INSTR_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(INSTR_LIST));
3368   memset(iptr, 0, sizeof(INSTR_LIST));
3369   iptr->i_name = instr_name;
3370   if (flg.debug || XBIT(120, 0x1000)) {
3371     switch (instr_name) {
3372     default:
3373       iptr->dbg_line_op = lldbg_get_line(cpu_llvm_module->debug_info);
3374       break;
3375     case I_NONE:
3376     case I_DECL:
3377     case I_CLEANUP:
3378     case I_CATCH:
3379       break;
3380     }
3381   }
3382   return iptr;
3383 } /* make_instr */
3384 
3385 /**
3386    \brief Like make_instr, but also sets tmps, ll_type, and operands.
3387  */
3388 static INSTR_LIST *
gen_instr(LL_InstrName instr_name,TMPS * tmps,LL_Type * ll_type,OPERAND * operands)3389 gen_instr(LL_InstrName instr_name, TMPS *tmps, LL_Type *ll_type,
3390           OPERAND *operands)
3391 {
3392   INSTR_LIST *iptr;
3393 
3394   iptr = make_instr(instr_name);
3395   iptr->tmps = tmps;
3396   if (tmps != NULL)
3397     tmps->info.idef = iptr;
3398   iptr->ll_type = ll_type;
3399   iptr->operands = operands;
3400   return iptr;
3401 }
3402 
3403 INLINE static OPERAND *
convert_sint_to_float(OPERAND * convert_op,LL_Type * rslt_type)3404 convert_sint_to_float(OPERAND *convert_op, LL_Type *rslt_type)
3405 {
3406   return convert_operand(convert_op, rslt_type, I_SITOFP);
3407 }
3408 
3409 INLINE static OPERAND *
convert_float_to_sint(OPERAND * convert_op,LL_Type * rslt_type)3410 convert_float_to_sint(OPERAND *convert_op, LL_Type *rslt_type)
3411 {
3412   return convert_operand(convert_op, rslt_type, I_FPTOSI);
3413 }
3414 
3415 INLINE static OPERAND *
convert_uint_to_float(OPERAND * convert_op,LL_Type * rslt_type)3416 convert_uint_to_float(OPERAND *convert_op, LL_Type *rslt_type)
3417 {
3418   return convert_operand(convert_op, rslt_type, I_UITOFP);
3419 }
3420 
3421 INLINE static OPERAND *
convert_float_to_uint(OPERAND * convert_op,LL_Type * rslt_type)3422 convert_float_to_uint(OPERAND *convert_op, LL_Type *rslt_type)
3423 {
3424   return convert_operand(convert_op, rslt_type, I_FPTOUI);
3425 }
3426 
3427 INLINE static OPERAND *
convert_ptr_to_int(OPERAND * convert_op,LL_Type * rslt_type)3428 convert_ptr_to_int(OPERAND *convert_op, LL_Type *rslt_type)
3429 {
3430   return convert_operand(convert_op, rslt_type, I_PTRTOINT);
3431 }
3432 
3433 static OPERAND *
convert_mismatched_types(OPERAND * operand,LL_Type * expected_type,int ilix)3434 convert_mismatched_types(OPERAND *operand, LL_Type *expected_type, int ilix)
3435 {
3436   if (ll_type_int_bits(operand->ll_type) && ll_type_int_bits(expected_type)) {
3437     return convert_int_size(ilix, operand, expected_type);
3438   } else if (expected_type->data_type == LL_PTR &&
3439              operand->ll_type->data_type == LL_PTR) {
3440     DBGDUMPLLTYPE("#adding bitcast to match expected type:", expected_type);
3441     return make_bitcast(operand, expected_type);
3442   } else if (ll_type_is_fp(expected_type) && ll_type_is_fp(operand->ll_type)) {
3443     return convert_float_size(operand, expected_type);
3444   } else if (ll_type_is_fp(operand->ll_type) &&
3445              ll_type_int_bits(expected_type)) {
3446     return convert_float_to_sint(operand, expected_type);
3447   } else if (ll_type_int_bits(operand->ll_type) &&
3448              (expected_type->data_type == LL_PTR)) {
3449     return convert_int_to_ptr(operand, expected_type);
3450   } else if ((operand->ll_type->data_type == LL_PTR) &&
3451              ll_type_int_bits(expected_type)) {
3452     return convert_ptr_to_int(operand, expected_type);
3453   } else if (ll_type_int_bits(operand->ll_type) &&
3454              (expected_type->data_type == LL_X86_FP80)) {
3455     assert(ll_type_bytes(operand->ll_type) <= ll_type_bytes(expected_type),
3456            "bitcast of int larger than long long to FP80",
3457            ll_type_bytes(operand->ll_type), ERR_Fatal);
3458     return convert_sint_to_float(operand, expected_type);
3459   } else if (ll_type_int_bits(operand->ll_type) &&
3460              ll_type_is_fp(expected_type)) {
3461     assert(ll_type_bytes(operand->ll_type) == ll_type_bytes(expected_type),
3462            "bitcast with differing sizes",
3463            ll_type_bytes(operand->ll_type) - ll_type_bytes(expected_type),
3464            ERR_Fatal);
3465     return make_bitcast(operand, expected_type);
3466   }
3467   assert(false, "no type conversion available", 0, ERR_Fatal);
3468   return operand;
3469 }
3470 
3471 static OPERAND *
ad_csed_instr(LL_InstrName instr_name,int ilix,LL_Type * ll_type,OPERAND * operands,LL_InstrListFlags flags,bool do_cse)3472 ad_csed_instr(LL_InstrName instr_name, int ilix, LL_Type *ll_type,
3473               OPERAND *operands, LL_InstrListFlags flags, bool do_cse)
3474 {
3475   OPERAND *operand, *new_op;
3476   INSTR_LIST *instr;
3477   if (do_cse && ENABLE_CSE_OPT && !new_ebb) {
3478     instr = llvm_info.last_instr;
3479     while (instr) {
3480       if (instr->i_name == instr_name) {
3481         operand = instr->operands;
3482         new_op = operands;
3483         while (operand && new_op) {
3484           if (!same_op(operand, new_op))
3485             break;
3486           operand = operand->next;
3487           new_op = new_op->next;
3488         }
3489         if (operand == NULL && new_op == NULL) {
3490           new_op = make_tmp_op(instr->ll_type, instr->tmps);
3491           if (instr->ll_type != ll_type) {
3492             new_op = convert_mismatched_types(new_op, ll_type, ilix);
3493           }
3494           return new_op;
3495         }
3496       }
3497       switch (instr->i_name) {
3498       case I_SW:
3499       case I_INVOKE:
3500       case I_CALL:
3501         instr = NULL;
3502         break;
3503       case I_BR:
3504       case I_INDBR:
3505       case I_NONE:
3506         if (!ENABLE_ENHANCED_CSE_OPT) {
3507           instr = NULL;
3508           break;
3509         }
3510         // fall through
3511       default:
3512         instr = (instr->flags & STARTEBB) ? NULL : instr->prev;
3513         break;
3514       }
3515     }
3516   }
3517   operand = make_tmp_op(ll_type, make_tmps());
3518   instr = gen_instr(instr_name, operand->tmps, ll_type, operands);
3519   if ((instr_name == I_LOAD) && rw_nodepcheck) {
3520     flags |= LDST_HAS_METADATA;
3521     instr->misc_metadata = cons_no_depchk_metadata();
3522   }
3523   instr->flags = flags;
3524   ad_instr(ilix, instr);
3525   return operand;
3526 }
3527 
3528 static void
ad_instr(int ilix,INSTR_LIST * instr)3529 ad_instr(int ilix, INSTR_LIST *instr)
3530 {
3531   OPERAND *operand;
3532 
3533   if (instr == NULL)
3534     return;
3535 
3536   instr->ilix = ilix;
3537   DEBUG_ASSERT(instr != llvm_info.last_instr, "looped link");
3538 
3539   for (operand = instr->operands; operand; operand = operand->next) {
3540     if (operand->ot_type == OT_TMP) {
3541       assert(operand->tmps, "ad_instr(): missing last instruction", 0,
3542              ERR_Fatal);
3543       operand->tmps->use_count++;
3544     }
3545   }
3546   if (Instructions) {
3547     assert(llvm_info.last_instr, "ad_instr(): missing last instruction", 0,
3548            ERR_Fatal);
3549     llvm_info.last_instr->next = instr;
3550     instr->prev = llvm_info.last_instr;
3551   } else {
3552     assert(!llvm_info.last_instr, "ad_instr(): last instruction not NULL", 0,
3553            ERR_Fatal);
3554     Instructions = instr;
3555   }
3556   llvm_info.last_instr = instr;
3557   if (new_ebb) {
3558     instr->flags |= STARTEBB;
3559     new_ebb = false;
3560   }
3561 }
3562 
3563 static bool
cancel_store(int ilix,int op_ili,int addr_ili)3564 cancel_store(int ilix, int op_ili, int addr_ili)
3565 {
3566   ILI_OP op_opc = ILI_OPC(op_ili);
3567   bool csed = false;
3568 
3569   if (is_cseili_opcode(op_opc)) {
3570     op_ili = ILI_OPND(op_ili, 1);
3571     op_opc = ILI_OPC(op_ili);
3572     csed = true;
3573   }
3574   if (IL_TYPE(op_opc) == ILTY_LOAD) {
3575     bool ret_val = (ILI_OPND(op_ili, 1) == addr_ili);
3576     if (ret_val && csed) {
3577       DBGTRACE1("#store of CSE'd operand removed for ilix(%d)", ilix);
3578     }
3579     return ret_val;
3580   }
3581   return false;
3582 }
3583 
3584 static LL_InstrListFlags
ll_instr_flags_from_memory_order(MEMORY_ORDER mo)3585 ll_instr_flags_from_memory_order(MEMORY_ORDER mo)
3586 {
3587   switch (mo) {
3588   default:
3589     assert(false,
3590            "ll_instr_flags_from_memory_order:"
3591            " unimplemented memory order",
3592            mo, ERR_Fatal);
3593     return (LL_InstrListFlags)0;
3594   case MO_RELAXED:
3595     return ATOMIC_MONOTONIC_FLAG;
3596   case MO_CONSUME:
3597   /* LLVM does not support "consume", so round up to acquire. */
3598   case MO_ACQUIRE:
3599     return ATOMIC_ACQUIRE_FLAG;
3600   case MO_RELEASE:
3601     return ATOMIC_RELEASE_FLAG;
3602   case MO_ACQ_REL:
3603     return ATOMIC_ACQ_REL_FLAG;
3604   case MO_SEQ_CST:
3605     return ATOMIC_SEQ_CST_FLAG;
3606   }
3607 }
3608 
3609 /**
3610   \brief From an ILI atomic instruction with a fence,
3611          get instruction flags for [singlethread] <memory order>.
3612  */
3613 static LL_InstrListFlags
ll_instr_flags_for_memory_order_and_scope(int ilix)3614 ll_instr_flags_for_memory_order_and_scope(int ilix)
3615 {
3616   LL_InstrListFlags flags =
3617       ll_instr_flags_from_memory_order(memory_order(ilix));
3618   ATOMIC_INFO info = atomic_info(ilix);
3619   if (info.scope == SS_SINGLETHREAD)
3620     flags |= ATOMIC_SINGLETHREAD_FLAG;
3621   return flags;
3622 }
3623 
3624 /**
3625    \brief Invalidate cached sincos intrinsics on write to input expression
3626  */
3627 static bool
sincos_input_uses(int ilix,int nme)3628 sincos_input_uses(int ilix, int nme)
3629 {
3630   int i;
3631   const ILI_OP opc = ILI_OPC(ilix);
3632   const int noprs = ilis[opc].oprs;
3633   const ILTY_KIND ilty = IL_TYPE(opc);
3634   if (ilty == ILTY_LOAD)
3635     return (ILI_OPND(ilix, 2) == nme);
3636   for (i = 1; i <= noprs; ++i) {
3637     if (IL_ISLINK(opc, i)) {
3638       bool isUse = sincos_input_uses(ILI_OPND(ilix, i), nme);
3639       if (isUse)
3640         return true;
3641     }
3642   }
3643   return false;
3644 }
3645 
3646 /**
3647    \brief Remove all loads that correspond to a given NME
3648    \param key      an ILI value
3649    \param data     is NULL for a load
3650    \param context  the NME we want to remove
3651  */
3652 static void
sincos_clear_arg_helper(hash_key_t key,hash_data_t data,void * context)3653 sincos_clear_arg_helper(hash_key_t key, hash_data_t data, void *context)
3654 {
3655   const int lhs_ili = ((int *)context)[0];
3656   const int seek_nme = ((int *)context)[1];
3657   const int ilix = HKEY2INT(key);
3658   const int ilix_nme = ILI_OPND(ilix, 2);
3659   if ((ilix == lhs_ili) || ((data == NULL) && (seek_nme == ilix_nme)) ||
3660       sincos_input_uses(ilix, seek_nme))
3661     hashmap_erase(sincos_imap, key, NULL);
3662 }
3663 
3664 INLINE static void
sincos_clear_specific_arg(int lhs_ili,int nme)3665 sincos_clear_specific_arg(int lhs_ili, int nme)
3666 {
3667   int ctxt[] = {lhs_ili, nme};
3668   hashmap_iterate(sincos_imap, sincos_clear_arg_helper, (void *)ctxt);
3669 }
3670 
3671 INLINE static OPERAND *
gen_sret_expr(int ilix,LL_Type * expected_type)3672 gen_sret_expr(int ilix, LL_Type *expected_type)
3673 {
3674   OPERAND *value = gen_llvm_expr(ilix, expected_type);
3675   ret_info.sret_sptr = NME_SYM(ILI_OPND(ilix, 2));
3676   process_sptr(ret_info.sret_sptr);
3677   value = make_operand();
3678   value->ll_type = make_lltype_from_dtype(DT_NONE);
3679   return value;
3680 }
3681 
3682 static void
make_stmt(STMT_Type stmt_type,int ilix,bool deletable,SPTR next_bih_label,int ilt)3683 make_stmt(STMT_Type stmt_type, int ilix, bool deletable, SPTR next_bih_label,
3684           int ilt)
3685 {
3686   int lhs_ili, rhs_ili, sc, nme, i, size1, size2;
3687   SPTR sptr, sptr_lab;
3688   int offset_to, offset_from, stype, ts;
3689   SPTR sym, pd_sym;
3690   DTYPE dtype;
3691   int to_ili, from_ili, length_ili, opnd, bytes, from_nme, cc;
3692   ILI_OP opc;
3693   char *name, *lname, *tmp_name, *retc;
3694   TMPS *tmps, *last_tmps, *new_tmps;
3695   LL_Type *llt, *op_type, *last_type, *ty1, *ty2, *load_type, *switch_type;
3696   INSTR_LIST *instr;
3697   OPERAND *ret_op, *store_op, *operand1, *operand2, *op_tmp, *op1, *op2;
3698   OPERAND *load_op, *dst_op, *src_op, *first_label, *second_label;
3699   int match, conversion_instr, d1, d2;
3700   bool mark_daddr, sta, has_entries;
3701   MSZ msz;
3702   LL_Type *llt_expected;
3703   int alignment;
3704   INSTR_LIST *Curr_Instr;
3705 
3706   DBGTRACEIN2(" type: %s ilix: %d", stmt_names[stmt_type], ilix)
3707 
3708   curr_stmt_type = stmt_type;
3709   if (last_stmt_is_branch && stmt_type != STMT_LABEL) {
3710     sptr_lab = getlab();
3711     update_llvm_sym_arrays();
3712     make_stmt(STMT_LABEL, sptr_lab, false, SPTR_NULL, ilt);
3713   }
3714   last_stmt_is_branch = 0;
3715   switch (stmt_type) {
3716   case STMT_RET: {
3717     LL_Type *retTy = llvm_info.abi_info->extend_abi_return
3718                          ? make_lltype_from_dtype(DT_INT)
3719                          : llvm_info.return_ll_type;
3720     last_stmt_is_branch = 1;
3721     has_entries = has_multiple_entries(gbl.currsub);
3722     switch (ILI_OPC(ilix)) {
3723     case IL_AADD:
3724     case IL_ASUB:
3725     case IL_ACON:
3726     case IL_IAMV:
3727     case IL_KAMV:
3728     case IL_LDA:
3729       if (has_entries && !gbl.arets) {
3730         ret_op = gen_base_addr_operand(ilix, NULL);
3731       } else if (llvm_info.abi_info->is_iso_c) {
3732         if (currsub_is_sret()) {
3733           ret_op = gen_sret_expr(ilix, llvm_info.abi_info->arg[0].type);
3734         } else {
3735           ret_op = gen_base_addr_operand(ilix, make_ptr_lltype(retTy));
3736           ret_op = gen_load(ret_op, retTy, InstrListFlagsNull);
3737         }
3738       } else {
3739         if ((ILI_OPC(ilix) != IL_LDA) && (retTy->data_type != LL_PTR))
3740           retTy = make_ptr_lltype(retTy);
3741         ret_op = gen_base_addr_operand(ilix, retTy);
3742       }
3743       break;
3744     default:
3745       /* IL_EXIT */
3746       if (has_entries && !gbl.arets) {
3747         ret_op = gen_llvm_expr(ilix, NULL);
3748       } else {
3749         ret_op = gen_llvm_expr(ilix, retTy);
3750       }
3751     }
3752     if (ret_op) {
3753       Curr_Instr = gen_instr(I_RET, NULL, NULL, ret_op);
3754       ad_instr(ilix, Curr_Instr);
3755     }
3756   } break;
3757   case STMT_DECL:
3758     Curr_Instr = gen_instr(I_DECL, NULL, NULL, gen_llvm_expr(ilix, NULL));
3759     ad_instr(ilix, Curr_Instr);
3760     break;
3761   case STMT_LABEL: {
3762     sptr = (SPTR)ilix; // FIXME: is this a bug?
3763     process_sptr(sptr);
3764     Curr_Instr = gen_instr(I_NONE, NULL, NULL, make_label_op(sptr));
3765     ad_instr(ilix, Curr_Instr);
3766 
3767     break;
3768   }
3769   case STMT_CALL:
3770     if (getTempMap(ilix))
3771       return;
3772     sym = pd_sym = get_call_sptr(ilix);
3773 
3774     if (sym != pd_sym && STYPEG(pd_sym) == ST_PD) {
3775       switch (PDNUMG(pd_sym)) {
3776       default:
3777         break;
3778       }
3779     }
3780     if (gen_alloca_call_if_necessary(sym, ilix) != NULL ||
3781         gen_unreachable_if_necessary(sym, ilix) != NULL) {
3782       /* A builtin function that gets special handling. */
3783       goto end_make_stmt;
3784     }
3785     gen_call_expr(ilix, DT_NONE, NULL, sym);
3786     break;
3787   continue_call:
3788     /* Add instruction if it hasn't been added already by gen_call_expr(). */
3789     if (!Instructions || !Curr_Instr->prev)
3790       ad_instr(ilix, Curr_Instr);
3791     break;
3792 
3793   case STMT_BR:
3794     opc = ILI_OPC(ilix);
3795     if (opc == IL_JMP) { /* unconditional jump */
3796       last_stmt_is_branch = 1;
3797       sptr = ILI_SymOPND(ilix, 1);
3798       /* also in gen_new_landingpad_jump */
3799       process_sptr(sptr);
3800       Curr_Instr = gen_instr(I_BR, NULL, NULL, make_target_op(sptr));
3801       ad_instr(ilix, Curr_Instr);
3802     } else if (exprjump(opc) || zerojump(opc)) { /* cond or zero jump */
3803       if (exprjump(opc)) { /* get sptr pointing to jump label */
3804         sptr = ILI_SymOPND(ilix, 4);
3805         cc = ILI_OPND(ilix, 3);
3806       } else {
3807         sptr = ILI_SymOPND(ilix, 3);
3808         cc = ILI_OPND(ilix, 2);
3809       }
3810       process_sptr(sptr);
3811       Curr_Instr = make_instr(I_BR);
3812       tmps = make_tmps();
3813       Curr_Instr->operands = make_tmp_op(make_int_lltype(1), tmps);
3814 
3815       /* make the condition code */
3816       switch (opc) {
3817       case IL_FCJMP:
3818       case IL_FCJMPZ:
3819       case IL_DCJMP:
3820       case IL_DCJMPZ:
3821         ad_instr(ilix, gen_instr(I_FCMP, tmps, Curr_Instr->operands->ll_type,
3822                                  gen_llvm_expr(ilix, NULL)));
3823         break;
3824       default:
3825         ad_instr(ilix, gen_instr(I_ICMP, tmps, Curr_Instr->operands->ll_type,
3826                                  gen_llvm_expr(ilix, NULL)));
3827       }
3828       first_label = make_target_op(sptr);
3829       /* need to make a label for the false condition -- llvm conditional
3830        * branch requires this step.
3831        */
3832       if (next_bih_label) {
3833         sptr_lab = next_bih_label;
3834       } else {
3835         sptr_lab = getlab();
3836         update_llvm_sym_arrays();
3837       }
3838       second_label = make_target_op(sptr_lab);
3839       first_label->next = second_label;
3840       Curr_Instr->operands->next = first_label;
3841       ad_instr(ilix, Curr_Instr);
3842       /* now add the label instruction */
3843       if (!next_bih_label)
3844         make_stmt(STMT_LABEL, sptr_lab, false, SPTR_NULL, ilt);
3845       DBGTRACE1("#goto statement: jump to label sptr %d", sptr);
3846     } else if (opc == IL_JMPM || opc == IL_JMPMK) {
3847       /* unconditional jump */
3848       Curr_Instr = gen_switch(ilix);
3849       last_stmt_is_branch = 1;
3850     } else if (opc == IL_JMPA) {
3851       int arg1 = ILI_OPND(ilix, 1);
3852       last_stmt_is_branch = 1;
3853       op1 = gen_llvm_expr(arg1, make_lltype_from_dtype(DT_CPTR));
3854       Curr_Instr = gen_instr(I_INDBR, NULL, NULL, op1);
3855       ad_instr(ilix, Curr_Instr);
3856     } else {
3857       /* unknown jump type */
3858       assert(0, "ilt branch: unexpected branch code", opc, ERR_Fatal);
3859     }
3860     break;
3861   case STMT_SMOVE:
3862     from_ili = ILI_OPND(ilix, 1);
3863     to_ili = ILI_OPND(ilix, 2);
3864     length_ili = ILI_OPND(ilix, 3);
3865     opnd = ILI_OPND(length_ili, 1);
3866     assert(DTYPEG(opnd) == DT_CPTR, "make_stmt(): expected DT_CPTR",
3867            DTYPEG(opnd), ERR_Fatal);
3868     bytes = CONVAL2G(opnd);
3869 /* IL_SMOVE 3rd opnd has a 4-byte or 8-byte unit, the rest of the
3870    data are copied using other STORE ili.
3871    we use it as bytes.
3872 */
3873     bytes = bytes * 8;
3874     assert(bytes, "make_stmt(): expected smove byte size", 0, ERR_Fatal);
3875     from_nme = ILI_OPND(ilix, 4);
3876     ts = 8 * size_of(DT_CPTR);
3877     src_op = gen_llvm_expr(from_ili, make_lltype_from_dtype(DT_CPTR));
3878     dst_op = gen_llvm_expr(to_ili, make_lltype_from_dtype(DT_CPTR));
3879     dtype = dt_nme(from_nme);
3880 #ifdef DT_ANY
3881     if (dtype == DT_ANY)
3882       alignment = align_of(DT_CPTR);
3883     else
3884 #endif
3885         if (dtype)
3886       alignment = align_of(dtype);
3887     else
3888       alignment = 1;
3889 
3890     insert_llvm_memcpy(ilix, ts, dst_op, src_op, bytes, alignment, 0);
3891     break;
3892   case STMT_SZERO:
3893     assert(ILI_OPC(ilix) == IL_ARGIR || ILI_OPC(ilix) == IL_DAIR,
3894            "make_stmt(): expected ARGIR/DAIR for ilix ", ilix, ERR_Fatal);
3895     length_ili = ILI_OPND(ilix, 1);
3896     opnd = ILI_OPND(length_ili, 1);
3897     if (ILI_OPC(ilix) == IL_DAIR)
3898       to_ili = ILI_OPND(ilix, 3);
3899     else
3900       to_ili = ILI_OPND(ilix, 2);
3901     assert(ILI_OPC(to_ili) == IL_ARGAR || ILI_OPC(to_ili) == IL_DAAR,
3902            "make_stmt(): expected ARGAR/DAAR for ili ", to_ili, ERR_Fatal);
3903     to_ili = ILI_OPND(to_ili, 1);
3904     bytes = CONVAL2G(opnd);
3905     assert(bytes, "make_stmt(): expected szero byte size", 0, ERR_Fatal);
3906     ts = 8 * size_of(DT_CPTR);
3907     dst_op = gen_llvm_expr(to_ili, make_lltype_from_dtype(DT_CPTR));
3908     insert_llvm_memset(ilix, ts, dst_op, bytes, 0, 1, 0);
3909     break;
3910   case STMT_ST:
3911     /* STORE statement */
3912     llvm_info.curr_ret_dtype = DT_NONE;
3913     nme = ILI_OPND(ilix, 3);
3914     lhs_ili = ILI_OPND(ilix, 2);
3915     rhs_ili = ILI_OPND(ilix, 1);
3916     if (sincos_seen())
3917       sincos_clear_specific_arg(lhs_ili, nme);
3918     if (!cancel_store(ilix, rhs_ili, lhs_ili)) {
3919       DTYPE vect_dtype = DT_NONE;
3920       LL_InstrListFlags store_flags = InstrListFlagsNull;
3921       LL_Type *int_llt = NULL;
3922       LL_Type *v4_llt = NULL;
3923       msz = ILI_MSZ_OF_ST(ilix);
3924       vect_dtype = ili_get_vect_dtype(ilix);
3925 #if defined(TARGET_LLVM_ARM)
3926       if (vect_dtype) {
3927         store_flags = ldst_instr_flags_from_dtype(vect_dtype);
3928         if ((DTyVecLength(vect_dtype) == 3) && (ILI_OPC(ilix) == IL_VST)) {
3929           v4_llt = make_lltype_sz4v3_from_dtype(vect_dtype);
3930         } else {
3931           switch (zsize_of(vect_dtype)) {
3932           case 2:
3933             int_llt = make_lltype_from_dtype(DT_SINT);
3934             break;
3935           case 4:
3936             if (DTyVecLength(vect_dtype) != 3)
3937               int_llt = make_lltype_from_dtype(DT_INT);
3938             break;
3939           default:
3940             break;
3941           }
3942         }
3943       }
3944 #endif
3945       if (ILI_OPC(ilix) == IL_STA) {
3946         LL_Type *ptrTy = make_lltype_from_dtype(DT_CPTR);
3947         op1 = gen_base_addr_operand(rhs_ili, ptrTy);
3948         store_flags = ldst_instr_flags_from_dtype(DT_CPTR);
3949       } else {
3950         if (vect_dtype) {
3951           if (v4_llt) {
3952             op1 = gen_llvm_expr(rhs_ili, v4_llt);
3953           } else {
3954             LL_Type *ty = make_lltype_from_dtype(vect_dtype);
3955             if (ILI_OPC(rhs_ili) == IL_VCMP)
3956               ty = 0;
3957             op1 = gen_llvm_expr(rhs_ili, ty);
3958             if (ILI_OPC(rhs_ili) == IL_VCMP)
3959               int_llt = op1->ll_type;
3960           }
3961           if (int_llt)
3962             op1 = make_bitcast(op1, int_llt);
3963           /* Clear alignment bits ==> alignment = 1 byte. */
3964           if (ILI_OPC(ilix) == IL_VSTU)
3965             store_flags &= (LL_InstrListFlags)~LDST_LOGALIGN_MASK;
3966         } else if (is_blockaddr_store(ilix, rhs_ili, lhs_ili)) {
3967           return;
3968         } else if (ILI_OPC(ilix) == IL_STSCMPLX) {
3969           LL_Type *ty = make_lltype_from_dtype(DT_CMPLX);
3970           op1 = gen_llvm_expr(rhs_ili, ty);
3971           store_flags = ldst_instr_flags_from_dtype(DT_CMPLX);
3972         } else if (ILI_OPC(ilix) == IL_STDCMPLX) {
3973           LL_Type *ty = make_lltype_from_dtype(DT_DCMPLX);
3974           op1 = gen_llvm_expr(rhs_ili, ty);
3975           store_flags = ldst_instr_flags_from_dtype(DT_DCMPLX);
3976 #ifdef LONG_DOUBLE_FLOAT128
3977         } else if (ILI_OPC(ilix) == IL_FLOAT128ST) {
3978           LL_Type *ty = make_lltype_from_dtype(DT_FLOAT128);
3979           op1 = gen_llvm_expr(rhs_ili, ty);
3980           store_flags = ldst_instr_flags_from_dtype(DT_FLOAT128);
3981 #endif
3982           /* we let any other LONG_DOUBLE_X87 fall into the default case as
3983            * conversion is done (if needed) implicitly via convert_float_size()
3984            */
3985         } else {
3986           LL_Type *ty = make_type_from_msz(msz);
3987           op1 = gen_llvm_expr(rhs_ili, ty);
3988           store_flags = ldst_instr_flags_from_dtype(msz_dtype(msz));
3989         }
3990       }
3991       llt_expected = NULL;
3992       if ((ILI_OPC(ilix) == IL_STA) || (op1->ll_type->data_type == LL_STRUCT)) {
3993         llt_expected = make_ptr_lltype(op1->ll_type);
3994       }
3995       if (vect_dtype) {
3996         if (v4_llt)
3997           llt_expected = make_ptr_lltype(v4_llt);
3998         else if (int_llt)
3999           llt_expected = make_ptr_lltype(int_llt);
4000         else
4001           llt_expected = make_ptr_lltype(make_lltype_from_dtype(vect_dtype));
4002         store_op =
4003             gen_address_operand(lhs_ili, nme, false, llt_expected, (MSZ)-1);
4004       } else {
4005         store_op = gen_address_operand(lhs_ili, nme, false, llt_expected, msz);
4006       }
4007       if ((store_op->ll_type->data_type == LL_PTR) &&
4008           ll_type_int_bits(store_op->ll_type->sub_types[0]) &&
4009           ll_type_int_bits(op1->ll_type) &&
4010           (ll_type_bytes(store_op->ll_type->sub_types[0]) !=
4011            ll_type_bytes(op1->ll_type))) {
4012         /* Need to add a conversion here */
4013         op1 = convert_int_size(ilix, op1, store_op->ll_type->sub_types[0]);
4014       }
4015 
4016       if (nme == NME_VOL)
4017         store_flags |= VOLATILE_FLAG;
4018       if (IL_HAS_FENCE(ILI_OPC(ilix)))
4019         store_flags |= ll_instr_flags_for_memory_order_and_scope(ilix);
4020       DBGTRACE2("#store_op %p, op1 %p\n", store_op, op1);
4021       if (deletable)
4022         store_flags |= DELETABLE;
4023       Curr_Instr = mk_store_instr(op1, store_op);
4024       Curr_Instr->ilix = ilix;
4025       Curr_Instr->flags |= store_flags;
4026     }
4027     break;
4028   default:
4029     assert(0, "make_stmt(): unknown statment type", stmt_type, ERR_Fatal);
4030     break;
4031   }
4032 end_make_stmt:;
4033 
4034   DBGTRACEOUT("")
4035 } /* make_stmt */
4036 
4037 static OPERAND *
gen_va_start(int ilix)4038 gen_va_start(int ilix)
4039 {
4040   OPERAND *call_op, *arg_op;
4041   char *va_start_name, *gname;
4042   int arg;
4043   static bool va_start_defined = false;
4044   EXFUNC_LIST *exfunc;
4045   LL_Type *expected_type;
4046 
4047   DBGTRACEIN1(" called with ilix %d\n", ilix)
4048 
4049   call_op = make_operand();
4050   call_op->ot_type = OT_CALL;
4051   call_op->ll_type = make_void_lltype();
4052   va_start_name = (char *)getitem(LLVM_LONGTERM_AREA, 17);
4053   sprintf(va_start_name, "@llvm.va_start");
4054   call_op->string = va_start_name;
4055   arg = ILI_OPND(ilix, 2);
4056   assert(arg && is_argili_opcode(ILI_OPC(arg)), "gen_va_start(): bad argument",
4057          arg, ERR_Fatal);
4058   expected_type = make_lltype_from_dtype(DT_CPTR);
4059   arg_op = gen_llvm_expr(ILI_OPND(arg, 1), expected_type);
4060   call_op->next = arg_op;
4061   /* add prototype if needed */
4062   if (!va_start_defined) {
4063     va_start_defined = true;
4064     gname = (char *)getitem(LLVM_LONGTERM_AREA, strlen(va_start_name) + 35);
4065     sprintf(gname, "declare void %s(i8*)", va_start_name);
4066     exfunc = (EXFUNC_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(EXFUNC_LIST));
4067     memset(exfunc, 0, sizeof(EXFUNC_LIST));
4068     exfunc->func_def = gname;
4069     exfunc->flags |= EXF_INTRINSIC;
4070     add_external_function_declaration(va_start_name, exfunc);
4071   }
4072 
4073   DBGTRACEOUT1(" returns operand %p", call_op)
4074 
4075   return call_op;
4076 } /* gen_va_start */
4077 
4078 /**
4079    \brief Create a variable of type \p dtype
4080    \param ilix
4081    \param dtype
4082    \param align  Log of alignment (in bytes)
4083    \return an sptr to the newly created instance.
4084 
4085    This is a convenience routine only used by gen_va_arg.
4086  */
4087 static int
make_va_arg_tmp(int ilix,DTYPE dtype,int align)4088 make_va_arg_tmp(int ilix, DTYPE dtype, int align)
4089 {
4090   int tmp;
4091   char tmp_name[32];
4092 
4093   NEWSYM(tmp);
4094   snprintf(tmp_name, sizeof(tmp_name), ".vargtmp.%d", ilix);
4095   NMPTRP(tmp, putsname(tmp_name, strlen(tmp_name)));
4096   STYPEP(tmp, ST_STRUCT);
4097   SCP(tmp, SC_AUTO);
4098   DTYPEP(tmp, dtype);
4099   PDALNP(tmp, align);
4100   return tmp;
4101 }
4102 
4103 /**
4104  * \brief Expand an IL_VA_ARG instruction
4105  *
4106  * <tt>VA_ARG arlnk dtype</tt>
4107  * The first argument is a pointer to the va_list, the second is the dtype of
4108  * the argument to be extracted. Produce a pointer where the argument can be
4109  * loaded.
4110  *
4111  * There are two versions of this function (one for x86-64 and one for
4112  * non-x86-64).
4113  */
4114 static OPERAND *
gen_va_arg(int ilix)4115 gen_va_arg(int ilix)
4116 {
4117 
4118   /*
4119    * va_arg for other targets: va_list is an i8**, arguments are contiguous in
4120    * memory.
4121    *
4122    * %ap_cast = bitcast i8** %ap to uintptr_t*
4123    * %addr = load uintptr_t* %ap_cast
4124    * if (arg_align > reg_size)
4125    *   %addr = round-up-to-align(arg_align)
4126    *
4127    * %next = getelementptr argtype* %addr, 1
4128    * store argtype* %next, %ap_cast
4129    * return argtype %ptr
4130    */
4131   int tmp;
4132   OPERAND *addr_op, *result_op, *next_op;
4133   const int ap_ili = ILI_OPND(ilix, 1);
4134   const DTYPE arg_dtype = ILI_DTyOPND(ilix, 2);
4135   const unsigned reg_size = size_of(DT_CPTR);
4136   unsigned arg_align = alignment(arg_dtype) + 1;
4137   unsigned arg_size = size_of(arg_dtype);
4138   LL_Type *uintptr_type = make_int_lltype(8 * reg_size);
4139   OPERAND *ap_cast = gen_llvm_expr(ap_ili, make_ptr_lltype(uintptr_type));
4140   const LL_InstrListFlags flags = ldst_instr_flags_from_dtype(DT_CPTR);
4141 
4142   addr_op = gen_load(ap_cast, uintptr_type, flags);
4143 
4144   switch (DTY(arg_dtype)) {
4145 #ifdef LONG_DOUBLE_FLOAT128
4146   case TY_FLOAT128:
4147   case TY_CMPLX128:
4148 #endif
4149     /* These types are (needlessly) aligned to 16 bytes when laying out
4150      * structures, but treated as pairs or quadruplets of doubles in the
4151      * context of argument passing.
4152      */
4153     arg_align = 8;
4154     break;
4155   }
4156 
4157   if (arg_align > reg_size) {
4158     /* This argument has alignment greater than the pointer register size.
4159      * We need to dynamically align the address. */
4160     /* addr_op += arg_align-1 */
4161     addr_op->next = make_constval_op(uintptr_type, arg_align - 1, 0);
4162     addr_op =
4163         ad_csed_instr(I_ADD, 0, uintptr_type, addr_op, NOUNSIGNEDWRAP, false);
4164     /* addr_op &= -arg_align */
4165     addr_op->next = make_constval_op(uintptr_type, -arg_align, -1);
4166     addr_op = ad_csed_instr(I_AND, 0, uintptr_type, addr_op, InstrListFlagsNull,
4167                             false);
4168   }
4169   result_op = convert_int_to_ptr(
4170       addr_op, make_ptr_lltype(make_lltype_from_dtype(arg_dtype)));
4171 
4172 #ifdef TARGET_POWER
4173   /* POWER ABI: va_args are passed in the parameter save region of the stack.
4174    * The caller is responsible for setting up the stack space for this (LLVM
4175    * will do this for us).
4176    *
4177    * The special case here is for 'float complex' where each complex has
4178    * two components, treated as the same type and alignment as the first
4179    * component (the real component of the complex value).
4180    *
4181    * The reason for this special case is because we need to treat the
4182    * components of the complex as coming from two separate float arguments.
4183    * These are stored into a temp complex {float, float} and a pointer to that
4184    * temp is returned.
4185    */
4186   if (arg_dtype == DT_CMPLX) {
4187     LL_Type *llt_float = make_lltype_from_dtype(DT_FLOAT);
4188     LL_Type *llt_float_ptr = make_ptr_lltype(llt_float);
4189     LL_Type *llt_cptr = make_lltype_from_dtype(DT_CPTR);
4190     const LL_InstrListFlags flt_flags =
4191         (LL_InstrListFlags)ldst_instr_flags_from_dtype(DT_FLOAT);
4192     OPERAND *tmp_op, *cmplx_op, *val_op;
4193 
4194     /* Pointer to temp real */
4195     tmp = make_va_arg_tmp(ilix, arg_dtype, 0);
4196     cmplx_op = tmp_op = make_var_op((SPTR)tmp); /* points to {float,float} */
4197     tmp_op = make_bitcast(tmp_op, llt_cptr);
4198     tmp_op = gen_gep_index(tmp_op, llt_cptr, 0);
4199     tmp_op = make_bitcast(tmp_op, llt_float_ptr);
4200 
4201     /* Pointer to actual real */
4202     result_op = make_bitcast(result_op, llt_cptr);
4203     result_op = gen_gep_index(result_op, llt_cptr, 0);
4204     result_op = make_bitcast(result_op, llt_float_ptr);
4205     val_op = gen_load(result_op, llt_float, flt_flags);
4206     make_store(val_op, tmp_op, flt_flags);
4207 
4208     /* Now for imaginary (must skip 2 * DT_FLOAT bytes) */
4209     tmp_op = make_bitcast(tmp_op, llt_cptr);
4210     tmp_op = gen_gep_index(tmp_op, llt_cptr, size_of(DT_FLOAT));
4211     tmp_op = make_bitcast(tmp_op, llt_float_ptr);
4212     result_op = make_bitcast(result_op, llt_cptr);
4213     result_op = gen_gep_index(result_op, llt_cptr, size_of(DT_FLOAT) * 2);
4214     result_op = make_bitcast(result_op, llt_float_ptr);
4215     val_op = gen_load(result_op, llt_float, flt_flags);
4216     make_store(val_op, tmp_op, flt_flags);
4217 
4218     result_op = gen_copy_op(cmplx_op);
4219     arg_size *= 2; /* Skip two floats instead of one float */
4220   }
4221 #endif /* TARGET_POWER */
4222 
4223   /* Compute the address of the next argument.
4224    * Round up to a multiple of reg_size.
4225    */
4226   arg_size = (arg_size + reg_size - 1) & -reg_size;
4227   addr_op = gen_copy_op(addr_op);
4228   addr_op->next = make_constval_op(uintptr_type, arg_size, 0);
4229   next_op =
4230       ad_csed_instr(I_ADD, 0, uintptr_type, addr_op, NOUNSIGNEDWRAP, false);
4231   make_store(next_op, gen_copy_op(ap_cast), flags);
4232 
4233   return result_op;
4234 } /* gen_va_arg */
4235 
4236 static OPERAND *
gen_va_end(int ilix)4237 gen_va_end(int ilix)
4238 {
4239   OPERAND *call_op, *arg_op;
4240   char *va_end_name, *gname;
4241   int arg;
4242   static bool va_end_defined = false;
4243   EXFUNC_LIST *exfunc;
4244   LL_Type *expected_type;
4245 
4246   DBGTRACEIN1(" called with ilix %d\n", ilix)
4247 
4248   call_op = make_operand();
4249   call_op->ot_type = OT_CALL;
4250   call_op->ll_type = make_void_lltype();
4251   va_end_name = (char *)getitem(LLVM_LONGTERM_AREA, 17);
4252   sprintf(va_end_name, "@llvm.va_end");
4253   call_op->string = va_end_name;
4254   arg = ILI_OPND(ilix, 2);
4255   assert(arg && is_argili_opcode(ILI_OPC(arg)), "gen_va_end(): bad argument",
4256          arg, ERR_Fatal);
4257   expected_type = make_lltype_from_dtype(DT_CPTR);
4258   arg_op = gen_llvm_expr(ILI_OPND(arg, 1), expected_type);
4259   call_op->next = arg_op;
4260   /* add prototype if needed */
4261   if (!va_end_defined) {
4262     va_end_defined = true;
4263     gname = (char *)getitem(LLVM_LONGTERM_AREA, strlen(va_end_name) + 35);
4264     sprintf(gname, "declare void %s(i8*)", va_end_name);
4265     exfunc = (EXFUNC_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(EXFUNC_LIST));
4266     memset(exfunc, 0, sizeof(EXFUNC_LIST));
4267     exfunc->func_def = gname;
4268     exfunc->flags |= EXF_INTRINSIC;
4269     add_external_function_declaration(va_end_name, exfunc);
4270   }
4271 
4272   DBGTRACEOUT1(" returns operand %p", call_op)
4273 
4274   return call_op;
4275 } /* gen_va_end */
4276 
4277 OPERAND *
gen_call_to_builtin(int ilix,char * fname,OPERAND * params,LL_Type * return_ll_type,INSTR_LIST * Call_Instr,LL_InstrName i_name)4278 gen_call_to_builtin(int ilix, char *fname, OPERAND *params,
4279                     LL_Type *return_ll_type, INSTR_LIST *Call_Instr,
4280                     LL_InstrName i_name)
4281 {
4282   OPERAND *call_op, *operand = NULL;
4283   char *intrinsic_name, *gname;
4284   static char buf[MAXIDLEN];
4285   INSTR_LIST *Curr_Instr;
4286 
4287   DBGTRACEIN1(" for ilix %d\n", ilix)
4288 
4289   intrinsic_name = (char *)getitem(LLVM_LONGTERM_AREA, strlen(fname) + 1);
4290   strcpy(intrinsic_name, fname);
4291   operand = make_tmp_op(return_ll_type, make_tmps());
4292   if (!Call_Instr)
4293     Curr_Instr = make_instr(i_name);
4294   else
4295     Curr_Instr = Call_Instr;
4296   Curr_Instr->flags |= CALL_INTRINSIC_FLAG;
4297   Curr_Instr->tmps = operand->tmps; /* result operand */
4298   Curr_Instr->tmps->info.idef = Curr_Instr;
4299   Curr_Instr->ll_type = return_ll_type;
4300   Curr_Instr->operands =
4301       get_intrinsic_call_ops(intrinsic_name, return_ll_type, params);
4302   if (!Call_Instr)
4303     ad_instr(ilix, Curr_Instr);
4304 
4305   DBGTRACEOUT("")
4306 
4307   return operand;
4308 } /* gen_call_to_builtin */
4309 
4310 static const char *
get_atomicrmw_opname(LL_InstrListFlags instr_flags)4311 get_atomicrmw_opname(LL_InstrListFlags instr_flags)
4312 {
4313   switch (instr_flags & ATOMIC_RMW_OP_FLAGS) {
4314   case ATOMIC_SUB_FLAG:
4315     return "sub";
4316   case ATOMIC_ADD_FLAG:
4317     return "add";
4318   case ATOMIC_XCHG_FLAG:
4319     return "xchg";
4320   case ATOMIC_UMIN_FLAG:
4321     return "umin";
4322   case ATOMIC_MIN_FLAG:
4323     return "min";
4324   case ATOMIC_UMAX_FLAG:
4325     return "umax";
4326   case ATOMIC_MAX_FLAG:
4327     return "max";
4328   case ATOMIC_AND_FLAG:
4329     return "and";
4330   case ATOMIC_OR_FLAG:
4331     return "or";
4332   case ATOMIC_XOR_FLAG:
4333     return "xor";
4334   default:
4335     interr("Unexpected atomic rmw flag: ", instr_flags & ATOMIC_RMW_OP_FLAGS,
4336            ERR_Severe);
4337     return "";
4338   }
4339 }
4340 
4341 static const char *
get_atomic_memory_order_name(int instr_flags)4342 get_atomic_memory_order_name(int instr_flags)
4343 {
4344   switch (instr_flags & ATOMIC_MEM_ORD_FLAGS) {
4345   case ATOMIC_MONOTONIC_FLAG:
4346     return "monotonic";
4347   case ATOMIC_ACQUIRE_FLAG:
4348     return "acquire";
4349   case ATOMIC_RELEASE_FLAG:
4350     return "release";
4351   case ATOMIC_ACQ_REL_FLAG:
4352     return "acq_rel";
4353   case ATOMIC_SEQ_CST_FLAG:
4354     return "seq_cst";
4355   default:
4356     interr("Unexpected atomic mem ord flag: ",
4357            instr_flags & ATOMIC_MEM_ORD_FLAGS, ERR_Severe);
4358     return "";
4359   }
4360 }
4361 
4362 static OPERAND *
gen_llvm_atomicrmw_instruction(int ilix,int pdnum,OPERAND * params,DTYPE return_dtype)4363 gen_llvm_atomicrmw_instruction(int ilix, int pdnum, OPERAND *params,
4364                                DTYPE return_dtype)
4365 {
4366   return NULL;
4367 }
4368 
4369 static OPERAND *
gen_call_llvm_intrinsic(const char * fname,OPERAND * params,LL_Type * return_ll_type,INSTR_LIST * Call_Instr,LL_InstrName i_name)4370 gen_call_llvm_intrinsic(const char *fname, OPERAND *params,
4371                         LL_Type *return_ll_type, INSTR_LIST *Call_Instr,
4372                         LL_InstrName i_name)
4373 {
4374   static char buf[MAXIDLEN];
4375 
4376   sprintf(buf, "@llvm.%s", fname);
4377   return gen_call_to_builtin(0, buf, params, return_ll_type, Call_Instr,
4378                              i_name);
4379 }
4380 
4381 static OPERAND *
gen_call_pgocl_intrinsic(char * fname,OPERAND * params,LL_Type * return_ll_type,INSTR_LIST * Call_Instr,LL_InstrName i_name)4382 gen_call_pgocl_intrinsic(char *fname, OPERAND *params, LL_Type *return_ll_type,
4383                          INSTR_LIST *Call_Instr, LL_InstrName i_name)
4384 {
4385   static char buf[MAXIDLEN];
4386 
4387   sprintf(buf, "@%s%s", ENTOCL_PREFIX, fname);
4388   return gen_call_to_builtin(0, buf, params, return_ll_type, Call_Instr,
4389                              i_name);
4390 }
4391 
4392 static void
insert_llvm_memset(int ilix,int size,OPERAND * dest_op,int len,int value,int align,int is_volatile)4393 insert_llvm_memset(int ilix, int size, OPERAND *dest_op, int len, int value,
4394                    int align, int is_volatile)
4395 {
4396   EXFUNC_LIST *exfunc;
4397   OPERAND *call_op;
4398   static bool memset_defined = false;
4399   char *memset_name, *gname;
4400   INSTR_LIST *Curr_Instr;
4401 
4402   DBGTRACEIN("")
4403 
4404   memset_name = (char *)getitem(LLVM_LONGTERM_AREA, 22);
4405   sprintf(memset_name, "@llvm.memset.p0i8.i%d", size);
4406   Curr_Instr = make_instr(I_CALL);
4407   Curr_Instr->flags |= CALL_INTRINSIC_FLAG;
4408   Curr_Instr->operands = call_op = make_operand();
4409   call_op->ot_type = OT_CALL;
4410   call_op->ll_type = make_void_lltype();
4411   Curr_Instr->ll_type = call_op->ll_type;
4412   call_op->string = memset_name;
4413   call_op->next = dest_op;
4414 
4415   dest_op->next = make_constval_op(make_int_lltype(8), value, 0);
4416   /* length in bytes of memset */
4417   dest_op->next->next = make_constval_op(make_int_lltype(size), len, 0);
4418   /* alignment */
4419   dest_op->next->next->next = make_constval32_op(align);
4420   dest_op->next->next->next->next =
4421       make_constval_op(make_int_lltype(1), is_volatile, 0);
4422   ad_instr(ilix, Curr_Instr);
4423   /* add global define of llvm.memset to external function list, if needed */
4424   if (!memset_defined) {
4425     memset_defined = true;
4426     gname = (char *)getitem(LLVM_LONGTERM_AREA, strlen(memset_name) + 45);
4427     sprintf(gname, "declare void %s(i8*, i8, i%d, i32, i1)", memset_name, size);
4428     exfunc = (EXFUNC_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(EXFUNC_LIST));
4429     memset(exfunc, 0, sizeof(EXFUNC_LIST));
4430     exfunc->func_def = gname;
4431     exfunc->flags |= EXF_INTRINSIC;
4432     add_external_function_declaration(memset_name, exfunc);
4433   }
4434   DBGTRACEOUT("")
4435 } /* insert_llvm_memset */
4436 
4437 static void
insert_llvm_memcpy(int ilix,int size,OPERAND * dest_op,OPERAND * src_op,int len,int align,int is_volatile)4438 insert_llvm_memcpy(int ilix, int size, OPERAND *dest_op, OPERAND *src_op,
4439                    int len, int align, int is_volatile)
4440 {
4441   EXFUNC_LIST *exfunc;
4442   OPERAND *call_op;
4443   static bool memcpy_defined = false;
4444   char *memcpy_name, *gname;
4445   INSTR_LIST *Curr_Instr;
4446 
4447   DBGTRACEIN("")
4448 
4449   memcpy_name = (char *)getitem(LLVM_LONGTERM_AREA, 27);
4450   sprintf(memcpy_name, "@llvm.memcpy.p0i8.p0i8.i%d", size);
4451   Curr_Instr = make_instr(I_CALL);
4452   Curr_Instr->flags |= CALL_INTRINSIC_FLAG;
4453   Curr_Instr->operands = call_op = make_operand();
4454   call_op->ot_type = OT_CALL;
4455   call_op->ll_type = make_void_lltype();
4456   Curr_Instr->ll_type = call_op->ll_type;
4457   call_op->string = memcpy_name;
4458   call_op->next = dest_op;
4459   dest_op->next = src_op;
4460   /* length in bytes of memcpy */
4461   src_op->next = make_constval_op(make_int_lltype(size), len, 0);
4462   src_op->next->next = make_constval32_op(align); /* alignment */
4463   src_op->next->next->next =
4464       make_constval_op(make_int_lltype(1), is_volatile, 0);
4465   ad_instr(ilix, Curr_Instr);
4466   /* add global define of llvm.memcpy to external function list, if needed */
4467   if (!memcpy_defined) {
4468     memcpy_defined = true;
4469     gname = (char *)getitem(LLVM_LONGTERM_AREA, strlen(memcpy_name) + 49);
4470     sprintf(gname, "declare void %s(i8*, i8*, i%d, i32, i1)", memcpy_name,
4471             size);
4472     exfunc = (EXFUNC_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(EXFUNC_LIST));
4473     memset(exfunc, 0, sizeof(EXFUNC_LIST));
4474     exfunc->func_def = gname;
4475     exfunc->flags |= EXF_INTRINSIC;
4476     add_external_function_declaration(memcpy_name, exfunc);
4477   }
4478 
4479   DBGTRACEOUT("")
4480 } /* insert_llvm_memcpy */
4481 
4482 /**
4483    \brief Insert <tt>@llvm.dbg.declare</tt> call for debug
4484    \param mdnode  metadata node
4485    \param sptr    symbol
4486    \param llTy    preferred type of \p sptr or \c NULL
4487  */
4488 static void
insert_llvm_dbg_declare(LL_MDRef mdnode,SPTR sptr,LL_Type * llTy,OPERAND * exprMDOp,OperandFlag_t opflag)4489 insert_llvm_dbg_declare(LL_MDRef mdnode, SPTR sptr, LL_Type *llTy,
4490                         OPERAND *exprMDOp, OperandFlag_t opflag)
4491 {
4492   EXFUNC_LIST *exfunc;
4493   OPERAND *call_op;
4494   static bool dbg_declare_defined = false;
4495   char *gname;
4496   INSTR_LIST *Curr_Instr;
4497 
4498   Curr_Instr = make_instr(I_CALL);
4499   Curr_Instr->flags |= CALL_INTRINSIC_FLAG;
4500   Curr_Instr->operands = call_op = make_operand();
4501   Curr_Instr->dbg_line_op =
4502       lldbg_get_var_line(cpu_llvm_module->debug_info, sptr);
4503   call_op->ot_type = OT_CALL;
4504   call_op->ll_type = make_void_lltype();
4505   Curr_Instr->ll_type = call_op->ll_type;
4506   call_op->string = "@llvm.dbg.declare";
4507 
4508   call_op->next = make_metadata_wrapper_op(sptr, llTy);
4509   call_op->next->flags |= opflag;
4510   call_op->next->next = make_mdref_op(mdnode);
4511   if (ll_feature_dbg_declare_needs_expression_md(&cpu_llvm_module->ir)) {
4512     if (exprMDOp) {
4513       call_op->next->next->next = exprMDOp;
4514     } else {
4515       LL_DebugInfo *di = cpu_llvm_module->debug_info;
4516       LL_MDRef md;
4517       /* Handle the Fortran allocatable array cases. Emit expression
4518        * mdnode with sigle argument of DW_OP_deref to workaround known
4519        * gdb bug not able to debug array bounds.
4520        */
4521       if (ftn_array_need_debug_info(sptr)) {
4522         const unsigned deref = lldbg_encode_expression_arg(LL_DW_OP_deref, 0);
4523         md = lldbg_emit_expression_mdnode(di, 1, deref);
4524       } else
4525         md = lldbg_emit_empty_expression_mdnode(di);
4526       call_op->next->next->next = make_mdref_op(md);
4527     }
4528   }
4529 
4530   ad_instr(0, Curr_Instr);
4531   /* add global define of llvm.dbg.declare to external function list, if needed
4532    */
4533   if (!dbg_declare_defined) {
4534     dbg_declare_defined = true;
4535     if (ll_feature_dbg_declare_needs_expression_md(&cpu_llvm_module->ir)) {
4536       gname = "declare void @llvm.dbg.declare(metadata, metadata, metadata)";
4537     } else {
4538       gname = "declare void @llvm.dbg.declare(metadata, metadata)";
4539     }
4540     exfunc = (EXFUNC_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(EXFUNC_LIST));
4541     memset(exfunc, 0, sizeof(EXFUNC_LIST));
4542     exfunc->func_def = gname;
4543     exfunc->flags |= EXF_INTRINSIC;
4544     add_external_function_declaration("llvm.dbg.declare", exfunc);
4545   }
4546 }
4547 
4548 char *
match_names(MATCH_Kind match_val)4549 match_names(MATCH_Kind match_val)
4550 {
4551   char *tt;
4552 
4553   switch (match_val) {
4554   case MATCH_NO:
4555     return "MATCH_NO";
4556   case MATCH_OK:
4557     return "MATCH_OK";
4558   case MATCH_MEM:
4559     return "MATCH_MEM";
4560   default:
4561     asrt(match_val > 1);
4562     tt = (char *)getitem(LLVM_LONGTERM_AREA, 10);
4563     sprintf(tt, "MATCH_%d", match_val);
4564     return tt;
4565   }
4566 } /* match_names */
4567 
4568 static OPERAND *
gen_const_expr(int ilix,LL_Type * expected_type)4569 gen_const_expr(int ilix, LL_Type *expected_type)
4570 {
4571   OPERAND *operand;
4572   SPTR sptr = ILI_SymOPND(ilix, 1);
4573 
4574   /* Generate null pointer operands when requested.
4575    * Accept both IL_ICON and IL_KCON nulls. */
4576   if (expected_type && (expected_type->data_type == LL_PTR) &&
4577       CONVAL2G(sptr) == 0 && CONVAL1G(sptr) == 0)
4578     return make_null_op(expected_type);
4579 
4580   operand = make_operand();
4581   operand->ot_type = OT_CONSTSPTR;
4582 
4583   switch (ILI_OPC(ilix)) {
4584   case IL_KCON:
4585     operand->ll_type = make_lltype_from_dtype(DT_INT8);
4586     operand->val.sptr = sptr;
4587     break;
4588   case IL_ICON:
4589     if (expected_type && ll_type_int_bits(expected_type) &&
4590         (ll_type_int_bits(expected_type) < 32)) {
4591       operand->ot_type = OT_CONSTVAL;
4592       operand->val.conval[0] = CONVAL2G(sptr);
4593       operand->val.conval[1] = 0;
4594       assert(ll_type_int_bits(expected_type), "expected int",
4595              expected_type->data_type, ERR_Fatal);
4596       operand->ll_type = expected_type;
4597     } else {
4598       operand->ll_type = make_lltype_from_dtype(DT_INT);
4599       operand->val.sptr = sptr;
4600     }
4601     break;
4602   case IL_FCON:
4603     operand->ll_type = make_lltype_from_dtype(DT_FLOAT);
4604     operand->val.sptr = sptr;
4605     break;
4606   case IL_DCON:
4607     operand->ll_type = make_lltype_from_dtype(DT_DBLE);
4608     operand->val.sptr = sptr;
4609     break;
4610   case IL_VCON:
4611     operand->ll_type = make_lltype_from_sptr(sptr);
4612     operand->val.sptr = sptr;
4613     break;
4614   case IL_SCMPLXCON:
4615   case IL_DCMPLXCON:
4616     operand->ll_type = make_lltype_from_dtype(DTYPEG(sptr));
4617     operand->val.sptr = sptr;
4618     break;
4619 #ifdef LONG_DOUBLE_FLOAT128
4620   case IL_FLOAT128CON:
4621     operand->ll_type = make_lltype_from_dtype(DT_FLOAT128);
4622     operand->val.sptr = sptr;
4623     break;
4624 #endif
4625   default:
4626     interr("Unknown gen_const_expr opcode", ILI_OPC(ilix), ERR_Fatal);
4627   }
4628   return operand;
4629 } /* gen_const_expr */
4630 
4631 static OPERAND *
gen_unary_expr(int ilix,LL_InstrName itype)4632 gen_unary_expr(int ilix, LL_InstrName itype)
4633 {
4634   int op_ili;
4635   ILI_OP opc = ILI_OPC(ilix);
4636   OPERAND *operand;
4637   LL_Type *opc_type, *instr_type;
4638   TMPS *new_tmps;
4639 
4640   DBGTRACEIN2(" ilix: %d(%s) \n", ilix, IL_NAME(opc))
4641 
4642   instr_type = opc_type = make_type_from_opc(opc);
4643   assert(opc_type != NULL, "gen_unary_expr(): no type information", 0,
4644          ERR_Fatal);
4645 
4646   op_ili = ILI_OPND(ilix, 1);
4647 
4648   switch (opc) {
4649   case IL_DFIXUK:
4650   case IL_DFIXU:
4651   case IL_DFIX:
4652   case IL_DFIXK:
4653   case IL_SNGL:
4654     opc_type = make_lltype_from_dtype(DT_DBLE);
4655     break;
4656   case IL_DBLE:
4657   case IL_UFIX:
4658   case IL_FIX:
4659   case IL_FIXK:
4660   case IL_FIXUK:
4661     opc_type = make_lltype_from_dtype(DT_FLOAT);
4662     break;
4663   case IL_FLOAT:
4664   case IL_FLOATU:
4665   case IL_DFLOATU:
4666   case IL_DFLOAT:
4667   case IL_ALLOC:
4668     opc_type = make_lltype_from_dtype(DT_INT);
4669     break;
4670   case IL_FLOATK:
4671   case IL_FLOATUK:
4672   case IL_DFLOATUK:
4673   case IL_DFLOATK:
4674     opc_type = make_lltype_from_dtype(DT_INT8);
4675     break;
4676 #ifdef LONG_DOUBLE_FLOAT128
4677   case IL_FLOAT128FROM:
4678     opc_type = make_lltype_from_dtype(DT_DBLE);
4679     break;
4680   case IL_FLOAT128TO:
4681     opc_type = make_lltype_from_dtype(DT_FLOAT128);
4682     break;
4683 #endif
4684   default:
4685     break;
4686   }
4687 
4688   DBGTRACE2("#generating unary operand, op_ili: %d(%s)", op_ili,
4689             IL_NAME(ILI_OPC(op_ili)));
4690 
4691   /* now make the new unary expression */
4692   operand =
4693       ad_csed_instr(itype, ilix, instr_type, gen_llvm_expr(op_ili, opc_type),
4694                     InstrListFlagsNull, true);
4695 
4696   DBGTRACEOUT1(" return operand %p\n", operand)
4697 
4698   return operand;
4699 } /* gen_unary_expr */
4700 
4701 static OPERAND *
gen_abs_expr(int ilix)4702 gen_abs_expr(int ilix)
4703 {
4704   int lhs_ili;
4705   ILI_OP opc = ILI_OPC(ilix);
4706   OPERAND *operand, *cmp_op, *op1, *op2, *zero_op, *comp_operands;
4707   LL_Type *opc_type, *bool_type;
4708   LL_InstrName cc_itype;
4709   int cc_val;
4710   INT tmp[2];
4711   union {
4712     double d;
4713     INT tmp[2];
4714   } dtmp;
4715   float f;
4716   double d;
4717   INSTR_LIST *Curr_Instr;
4718 
4719   DBGTRACEIN2(" ilix: %d(%s) \n", ilix, IL_NAME(opc))
4720 
4721   lhs_ili = ILI_OPND(ilix, 1);
4722   opc_type = make_type_from_opc(opc);
4723   assert(opc_type, "gen_abs_expr(): no type information", 0, ERR_Fatal);
4724   operand = make_tmp_op(opc_type, make_tmps());
4725   op1 = gen_llvm_expr(lhs_ili, operand->ll_type);
4726   /* now make the new binary expression */
4727   Curr_Instr = gen_instr(I_SELECT, operand->tmps, operand->ll_type, NULL);
4728   bool_type = make_int_lltype(1);
4729   switch (ILI_OPC(ilix)) {
4730   case IL_IABS:
4731     cc_itype = I_ICMP;
4732     cc_val = convert_to_llvm_intcc(CC_LT);
4733     op2 = gen_llvm_expr(ad1ili(IL_INEG, lhs_ili), operand->ll_type);
4734     zero_op = gen_llvm_expr(ad_icon(0), operand->ll_type);
4735     break;
4736   case IL_KABS:
4737     cc_itype = I_ICMP;
4738     cc_val = convert_to_llvm_intcc(CC_LT);
4739     op2 = gen_llvm_expr(ad1ili(IL_KNEG, lhs_ili), operand->ll_type);
4740     zero_op = gen_llvm_expr(ad_kconi(0), operand->ll_type);
4741     break;
4742   case IL_FABS:
4743     cc_itype = I_FCMP;
4744     cc_val = convert_to_llvm_fltcc(CC_LT);
4745     op2 = gen_llvm_expr(ad1ili(IL_FNEG, lhs_ili), operand->ll_type);
4746     tmp[0] = 0;
4747     f = 0.0;
4748     mftof(f, tmp[1]);
4749     zero_op =
4750         gen_llvm_expr(ad1ili(IL_FCON, getcon(tmp, DT_FLOAT)), operand->ll_type);
4751     break;
4752   case IL_DABS:
4753     cc_itype = I_FCMP;
4754     cc_val = convert_to_llvm_fltcc(CC_LT);
4755     op2 = gen_llvm_expr(ad1ili(IL_DNEG, lhs_ili), operand->ll_type);
4756     d = 0.0;
4757     xmdtod(d, dtmp.tmp);
4758     zero_op = gen_llvm_expr(ad1ili(IL_DCON, getcon(dtmp.tmp, DT_DBLE)),
4759                             operand->ll_type);
4760     break;
4761 #ifdef LONG_DOUBLE_FLOAT128
4762   case IL_FLOAT128ABS:
4763     cc_itype = IL_FLOAT128CMP;
4764     cc_val = convert_to_llvm_fltcc(CC_LT);
4765     op2 = gen_llvm_expr(ad1ili(IL_FLOAT128CHS, lhs_ili), operand->ll_type);
4766     zero_op =
4767         gen_llvm_expr(ad1ili(IL_FLOAT128CON, stb.float128_0), operand->ll_type);
4768     break;
4769 #endif
4770   }
4771   cmp_op = make_tmp_op(bool_type, make_tmps());
4772 
4773   Curr_Instr->operands = cmp_op;
4774   Curr_Instr->operands->next = op2;
4775   Curr_Instr->operands->next->next = op1;
4776 
4777   comp_operands = make_operand();
4778   comp_operands->ot_type = OT_CC;
4779   comp_operands->val.cc = cc_val;
4780   comp_operands->ll_type = bool_type;
4781   comp_operands->next = gen_copy_op(op1);
4782   comp_operands->next->next = gen_copy_op(zero_op);
4783 
4784   ad_instr(ilix,
4785            gen_instr(cc_itype, cmp_op->tmps, cmp_op->ll_type, comp_operands));
4786 
4787   ad_instr(ilix, Curr_Instr);
4788 
4789   DBGTRACEOUT1(" returns operand %p", operand)
4790 
4791   return operand;
4792 }
4793 
4794 static OPERAND *
gen_minmax_expr(int ilix,OPERAND * op1,OPERAND * op2)4795 gen_minmax_expr(int ilix, OPERAND *op1, OPERAND *op2)
4796 {
4797   ILI_OP opc = ILI_OPC(ilix);
4798   OPERAND *operand, *cmp_op;
4799   LL_Type *llt, *bool_type;
4800   LL_InstrName cc_itype;
4801   int cc_val;
4802   CC_RELATION cc_ctype;
4803   DTYPE vect_dtype;
4804   INSTR_LIST *Curr_Instr;
4805 
4806   DBGTRACEIN2(" ilix: %d(%s)", ilix, IL_NAME(opc))
4807 
4808   operand = make_tmp_op(NULL, make_tmps());
4809   vect_dtype = ili_get_vect_dtype(ilix);
4810   if (vect_dtype) {
4811     llt = make_lltype_from_dtype(vect_dtype);
4812     operand->ll_type = llt->sub_types[0];
4813   } else
4814   {
4815     llt = make_type_from_opc(opc);
4816     operand->ll_type = llt;
4817   }
4818 
4819   /* now make the new binary expression */
4820   bool_type = make_int_lltype(1);
4821   switch (opc) {
4822   case IL_UIMIN:
4823   case IL_UKMIN:
4824     cc_itype = I_ICMP;
4825     cc_val = convert_to_llvm_uintcc(CC_LT);
4826     break;
4827   case IL_IMIN:
4828   case IL_KMIN:
4829     cc_itype = I_ICMP;
4830     cc_val = convert_to_llvm_intcc(CC_LT);
4831     break;
4832   case IL_FMIN:
4833   case IL_DMIN:
4834     cc_itype = I_FCMP;
4835     cc_val = convert_to_llvm_fltcc(CC_NOTGE);
4836     break;
4837   case IL_UIMAX:
4838   case IL_UKMAX:
4839     cc_itype = I_ICMP;
4840     cc_val = convert_to_llvm_uintcc(CC_GT);
4841     break;
4842   case IL_IMAX:
4843   case IL_KMAX:
4844     cc_itype = I_ICMP;
4845     cc_val = convert_to_llvm_intcc(CC_GT);
4846     break;
4847   case IL_FMAX:
4848   case IL_DMAX:
4849     cc_itype = I_FCMP;
4850     cc_val = convert_to_llvm_fltcc(CC_NOTLE);
4851     break;
4852   case IL_VMIN:
4853     cc_ctype = CC_NOTGE;
4854     goto common_minmax;
4855   /* Fall through */
4856   case IL_VMAX:
4857     cc_ctype = CC_NOTLE;
4858   common_minmax:
4859     switch (DTY(DTySeqTyElement(vect_dtype))) {
4860     case TY_FLOAT:
4861     case TY_DBLE:
4862       cc_itype = I_FCMP;
4863       cc_val = convert_to_llvm_fltcc(cc_ctype);
4864       break;
4865     default:
4866       cc_itype = I_ICMP;
4867       if (DT_ISUNSIGNED(DTySeqTyElement(vect_dtype)))
4868         cc_val = convert_to_llvm_uintcc(cc_ctype);
4869       else
4870         cc_val = convert_to_llvm_intcc(cc_ctype);
4871       break;
4872     }
4873     break;
4874   default:
4875     break; /*TODO: can this happen? */
4876   }
4877   cmp_op = make_tmp_op(bool_type, make_tmps());
4878 
4879   Curr_Instr = gen_instr(cc_itype, cmp_op->tmps, bool_type, NULL);
4880   Curr_Instr->operands = make_operand();
4881   Curr_Instr->operands->ot_type = OT_CC;
4882   Curr_Instr->operands->val.cc = cc_val;
4883   Curr_Instr->operands->ll_type = llt; /* opc type computed at top of routine */
4884   Curr_Instr->operands->next = op1;
4885   Curr_Instr->operands->next->next = op2;
4886   ad_instr(ilix, Curr_Instr);
4887 
4888   cmp_op->next = gen_copy_op(op1);
4889   cmp_op->next->next = gen_copy_op(op2);
4890   Curr_Instr = gen_instr(I_SELECT, operand->tmps, operand->ll_type, cmp_op);
4891   ad_instr(ilix, Curr_Instr);
4892 
4893   DBGTRACEOUT1(" returns operand %p", operand)
4894 
4895   return operand;
4896 }
4897 
4898 static OPERAND *
gen_select_expr(int ilix)4899 gen_select_expr(int ilix)
4900 {
4901   int cmp_ili, lhs_ili, rhs_ili;
4902   ILI_OP opc = ILI_OPC(ilix);
4903   OPERAND *operand;
4904   LL_Type *opc_type, *bool_type;
4905   INSTR_LIST *Curr_Instr;
4906 
4907   DBGTRACEIN2(" ilix: %d(%s) \n", ilix, IL_NAME(opc))
4908 
4909   cmp_ili = ILI_OPND(ilix, 1);
4910   lhs_ili = ILI_OPND(ilix, 3);
4911   rhs_ili = ILI_OPND(ilix, 2);
4912   opc_type = make_type_from_opc(opc);
4913   assert(opc_type, "gen_select_expr(): no type information", 0, ERR_Fatal);
4914   operand = make_tmp_op(opc_type, make_tmps());
4915 
4916   /* now make the new binary expression */
4917   Curr_Instr = gen_instr(I_SELECT, operand->tmps, operand->ll_type, NULL);
4918 
4919   DBGTRACE2("#generating comparison operand, cmp_ili: %d(%s)", cmp_ili,
4920             IL_NAME(ILI_OPC(cmp_ili)))
4921 
4922   bool_type = make_int_lltype(1);
4923   if (IEEE_CMP)
4924     float_jmp = true;
4925   Curr_Instr->operands = gen_llvm_expr(cmp_ili, bool_type);
4926   float_jmp = false;
4927 
4928   DBGTRACE2("#generating second operand, lhs_ili: %d(%s)", lhs_ili,
4929             IL_NAME(ILI_OPC(lhs_ili)))
4930 
4931   Curr_Instr->operands->next = gen_llvm_expr(lhs_ili, operand->ll_type);
4932 
4933   DBGTRACE2("#generating third operand, rhs_ili: %d(%s)", rhs_ili,
4934             IL_NAME(ILI_OPC(rhs_ili)))
4935 
4936   Curr_Instr->operands->next->next = gen_llvm_expr(rhs_ili, operand->ll_type);
4937   ad_instr(ilix, Curr_Instr);
4938 
4939   DBGTRACEOUT1(" returns operand %p", operand)
4940 
4941   return operand;
4942 }
4943 
4944 static int
get_vconi(DTYPE dtype,INT value)4945 get_vconi(DTYPE dtype, INT value)
4946 {
4947   INT v[TY_VECT_MAXLEN];
4948   int i;
4949 
4950   for (i = 0; i < DTyVecLength(dtype); i++) {
4951     v[i] = value;
4952   }
4953   return get_vcon(v, dtype);
4954 }
4955 
4956 static SPTR
get_vcon0_n(DTYPE dtype,int start,int N)4957 get_vcon0_n(DTYPE dtype, int start, int N)
4958 {
4959   INT v[TY_VECT_MAXLEN];
4960   int i;
4961 
4962   for (i = 0; i < N; i++) {
4963     v[i] = start + i;
4964   }
4965   return get_vcon(v, dtype);
4966 }
4967 
4968 static OPERAND *
gen_imask(SPTR sptr)4969 gen_imask(SPTR sptr)
4970 {
4971   OPERAND *operand;
4972   DTYPE vdtype = DTYPEG(sptr);
4973 
4974   operand = make_operand();
4975   operand->ot_type = OT_CONSTSPTR;
4976   operand->ll_type = make_vtype(DT_INT, DTyVecLength(vdtype));
4977   operand->val.sptr = sptr;
4978 
4979   return operand;
4980 }
4981 
4982 /*
4983  * Here we generate LLVM instruction to insert scalar operand <sop> at index
4984  * <idx>
4985  * into vector operand <vop>
4986  * So in LLVM it will tranlate into:
4987  * %0 = insertelement <<sz> x <ty>> <vop>, <sop>, i32 <idx>
4988  */
4989 static OPERAND *
gen_insert_vector(OPERAND * vop,OPERAND * sop,int idx)4990 gen_insert_vector(OPERAND *vop, OPERAND *sop, int idx)
4991 {
4992   OPERAND *operand;
4993   INSTR_LIST *Curr_Instr;
4994 
4995   operand = make_tmp_op(vop->ll_type, make_tmps());
4996 
4997   Curr_Instr = gen_instr(I_INSELE, operand->tmps, operand->ll_type, vop);
4998   vop->next = sop;
4999   vop->next->next = make_constval32_op(idx);
5000   ad_instr(0, Curr_Instr);
5001 
5002   return operand;
5003 }
5004 
5005 /*
5006  * Here we generate LLVM instruction to extract a scalar at a index <idx>
5007  * from vector operand <vop>
5008  * So in LLVM it will tranlate into:
5009  * %0 = extractelement <<sz> x <ty>> <vop>, i32 <idx>
5010  */
5011 static OPERAND *
gen_extract_vector(OPERAND * vop,int idx)5012 gen_extract_vector(OPERAND *vop, int idx)
5013 {
5014   OPERAND *operand;
5015   INSTR_LIST *Curr_Instr;
5016 
5017   assert(vop->ll_type->data_type == LL_VECTOR,
5018          "gen_extract_vector(): vector type expected for operand\n",
5019          vop->ll_type->data_type, ERR_Fatal);
5020   operand = make_tmp_op(vop->ll_type->sub_types[0], make_tmps());
5021 
5022   Curr_Instr = gen_instr(I_EXTELE, operand->tmps, operand->ll_type, vop);
5023   vop->next = make_constval32_op(idx);
5024   ad_instr(0, Curr_Instr);
5025 
5026   return operand;
5027 }
5028 
5029 /**
5030    \brief Create a new vector
5031 
5032    The new vactor will have the same type as \p vop with size \p new_size and
5033    filed with values from \p vop.  This is useful for converting 3 element
5034    vectors to 4 element vectors and vice-versa.
5035 
5036    Let's assume \p vop is a vector of 4 floats and \p new_size is 3.  This will
5037    be expanded into following LLVM instruction:
5038 
5039    <pre>
5040    %0 = shufflevector <4 x float> %vop, <4 x float> undef,
5041              <3 x i32> <i32 0, i32 1, i32 2>
5042    </pre>
5043 
5044    This will build a vector of 3 floats with 3 first elements from \p vop.
5045  */
5046 static OPERAND *
gen_resized_vect(OPERAND * vop,int new_size,int start)5047 gen_resized_vect(OPERAND *vop, int new_size, int start)
5048 {
5049   OPERAND *operand, *undefop;
5050   LL_Type *llt;
5051   INSTR_LIST *Curr_Instr;
5052   INT v[TY_VECT_MAXLEN];
5053   int i;
5054 
5055   assert(vop->ll_type->data_type == LL_VECTOR, "expecting vector type",
5056          vop->ll_type->data_type, ERR_Fatal);
5057   llt = ll_get_vector_type(vop->ll_type->sub_types[0], new_size);
5058   operand = make_tmp_op(llt, make_tmps());
5059 
5060   Curr_Instr = gen_instr(I_SHUFFVEC, operand->tmps, operand->ll_type, vop);
5061 
5062   vop->next = make_operand();
5063   vop->next->ot_type = OT_UNDEF;
5064   vop->next->ll_type = vop->ll_type;
5065 
5066   if ((ll_type_bytes(vop->ll_type) * 8) > new_size) {
5067     vop->next->next = gen_imask(
5068         get_vcon0_n(get_vector_dtype(DT_INT, new_size), start, new_size));
5069   } else {
5070     for (i = 0; i < ll_type_bytes(vop->ll_type) * 8; i++)
5071       v[i] = i + start;
5072     for (; i < new_size; i++)
5073       v[i] = ll_type_bytes(vop->ll_type) * 8 + start;
5074     vop->next->next =
5075         gen_imask(get_vcon(v, get_vector_dtype(DT_INT, new_size)));
5076   }
5077 
5078   ad_instr(0, Curr_Instr);
5079 
5080   return operand;
5081 }
5082 
5083 static OPERAND *
gen_scalar_to_vector_helper(int ilix,int from_ili,LL_Type * ll_vecttype)5084 gen_scalar_to_vector_helper(int ilix, int from_ili, LL_Type *ll_vecttype)
5085 {
5086   OPERAND *operand, *undefop, *arg;
5087   INSTR_LIST *Curr_Instr;
5088 
5089   operand = make_tmp_op(ll_vecttype, make_tmps());
5090 
5091   Curr_Instr = gen_instr(I_SHUFFVEC, operand->tmps, operand->ll_type, NULL);
5092 
5093   undefop = make_undef_op(ll_vecttype);
5094   arg = gen_llvm_expr(from_ili, ll_vecttype->sub_types[0]);
5095   Curr_Instr->operands = gen_insert_vector(undefop, arg, 0);
5096 
5097   Curr_Instr->operands->next = make_undef_op(ll_vecttype);
5098 
5099   Curr_Instr->operands->next->next = gen_imask((SPTR)get_vcon0( // ???
5100       get_vector_dtype(DT_INT, ll_vecttype->sub_elements)));
5101   ad_instr(ilix, Curr_Instr);
5102 
5103   return operand;
5104 }
5105 
5106 /*
5107  * Create a vector from a scalar value represented by 'ilix'
5108  * ilix -> <ilix, ilix, ilix, ilix>
5109  * Let's assume ilix needs to be promoted to a vector of 4 floats
5110  * This will be expanded into following LLVM instructions:
5111  * %0 = insertelement <4 x float> undef, <ilix>, i32 0
5112  * %1 = shufflevector <4 x float> %0, <4 x float> undef, <4 x i32> <i32 0, i32
5113  * 0, i32, 0 i32 0>
5114  */
5115 INLINE static OPERAND *
gen_scalar_to_vector(int ilix,LL_Type * ll_vecttype)5116 gen_scalar_to_vector(int ilix, LL_Type *ll_vecttype)
5117 {
5118   const int from_ili = ILI_OPND(ilix, 1);
5119   return gen_scalar_to_vector_helper(ilix, from_ili, ll_vecttype);
5120 }
5121 
5122 static OPERAND *
gen_scalar_to_vector_no_shuffle(int ilix,LL_Type * ll_vecttype)5123 gen_scalar_to_vector_no_shuffle(int ilix, LL_Type *ll_vecttype)
5124 {
5125   const int from_ili = ILI_OPND(ilix, 1);
5126   OPERAND *undefop = make_undef_op(ll_vecttype);
5127   OPERAND *arg = gen_llvm_expr(from_ili, ll_vecttype->sub_types[0]);
5128   OPERAND *operand = gen_insert_vector(undefop, arg, 0);
5129   return operand;
5130 }
5131 
5132 INLINE static OPERAND *
gen_temp_to_vector(int from_ili,LL_Type * ll_vecttype)5133 gen_temp_to_vector(int from_ili, LL_Type *ll_vecttype)
5134 {
5135   const int ilix = 0;
5136   return gen_scalar_to_vector_helper(ilix, from_ili, ll_vecttype);
5137 }
5138 
5139 static OPERAND *
gen_gep_op(int ilix,OPERAND * base_op,LL_Type * llt,OPERAND * index_op)5140 gen_gep_op(int ilix, OPERAND *base_op, LL_Type *llt, OPERAND *index_op)
5141 {
5142   base_op->next = index_op;
5143   return ad_csed_instr(I_GEP, ilix, llt, base_op, InstrListFlagsNull, true);
5144 }
5145 
5146 INLINE static OPERAND *
gen_gep_index(OPERAND * base_op,LL_Type * llt,int index)5147 gen_gep_index(OPERAND *base_op, LL_Type *llt, int index)
5148 {
5149   return gen_gep_op(0, base_op, llt, make_constval32_op(index));
5150 }
5151 
5152 static void
insertLLVMDbgValue(OPERAND * load,LL_MDRef mdnode,SPTR sptr,LL_Type * type)5153 insertLLVMDbgValue(OPERAND *load, LL_MDRef mdnode, SPTR sptr, LL_Type *type)
5154 {
5155   static bool defined = false;
5156   OPERAND *callOp;
5157   OPERAND *oper;
5158   LLVMModuleRef mod = cpu_llvm_module;
5159   LL_DebugInfo *di = mod->debug_info;
5160   INSTR_LIST *callInsn = make_instr(I_CALL);
5161 
5162   if (!defined) {
5163     EXFUNC_LIST *exfunc;
5164     char *gname =
5165         "declare void @llvm.dbg.value(metadata, i64, metadata, metadata)";
5166     exfunc = (EXFUNC_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(EXFUNC_LIST));
5167     memset(exfunc, 0, sizeof(EXFUNC_LIST));
5168     exfunc->func_def = gname;
5169     exfunc->flags |= EXF_INTRINSIC;
5170     add_external_function_declaration("llvm.dbg.value", exfunc);
5171     defined = true;
5172   }
5173 
5174   callInsn->flags |= CALL_INTRINSIC_FLAG;
5175   callInsn->operands = callOp = make_operand();
5176   callInsn->dbg_line_op = lldbg_get_var_line(di, sptr);
5177   callOp->ot_type = OT_CALL;
5178   callOp->ll_type = make_void_lltype();
5179   callInsn->ll_type = callOp->ll_type;
5180   callOp->string = "@llvm.dbg.value";
5181 
5182   callOp->next = oper = make_operand();
5183   oper->ot_type = OT_MDNODE;
5184   oper->tmps = load->tmps;
5185   oper->ll_type = type;
5186   oper->flags |= OPF_WRAPPED_MD;
5187   oper = make_constval_op(ll_create_int_type(mod, 64), 0, 0);
5188   callOp->next->next = oper;
5189   oper->next = make_mdref_op(mdnode);
5190   oper->next->next = make_mdref_op(lldbg_emit_empty_expression_mdnode(di));
5191 
5192   ad_instr(0, callInsn);
5193 }
5194 
5195 /**
5196    \brief Construct llvm.dbg.value calls on the load sites
5197    \param ld    The load
5198    \param addr  The address being loaded
5199    \param type  The type of the object being loaded
5200  */
5201 static void
consLoadDebug(OPERAND * ld,OPERAND * addr,LL_Type * type)5202 consLoadDebug(OPERAND *ld, OPERAND *addr, LL_Type *type)
5203 {
5204   SPTR sptr = addr->val.sptr;
5205   if (sptr && need_debug_info(sptr)) {
5206     LL_DebugInfo *di = cpu_llvm_module->debug_info;
5207     int fin = BIH_FINDEX(gbl.entbih);
5208     LL_MDRef lcl = lldbg_emit_local_variable(di, sptr, fin, true);
5209     insertLLVMDbgValue(ld, lcl, sptr, type);
5210   }
5211 }
5212 
5213 /**
5214    \brief Insert an LLVM load instruction and return the loaded value.
5215 
5216    The address operand must be a pointer type that points to the load type.
5217 
5218    The flags provide the alignment and volatility, see
5219    ldst_instr_flags_from_dtype().
5220  */
5221 static OPERAND *
gen_load(OPERAND * addr,LL_Type * type,LL_InstrListFlags flags)5222 gen_load(OPERAND *addr, LL_Type *type, LL_InstrListFlags flags)
5223 {
5224   OPERAND *ld = ad_csed_instr(I_LOAD, 0, type, addr, flags, false);
5225   consLoadDebug(ld, addr, type);
5226   return ld;
5227 }
5228 
5229 INLINE static void
make_store(OPERAND * sop,OPERAND * address_op,LL_InstrListFlags flags)5230 make_store(OPERAND *sop, OPERAND *address_op, LL_InstrListFlags flags)
5231 {
5232   INSTR_LIST *Curr_Instr = mk_store_instr(sop, address_op);
5233   Curr_Instr->flags |= flags;
5234 }
5235 
5236 static OPERAND *
gen_convert_vector(int ilix)5237 gen_convert_vector(int ilix)
5238 {
5239   int itype;
5240   LL_Type *ll_src, *ll_dst;
5241   OPERAND *operand;
5242   DTYPE dtype_dst = ILI_DTyOPND(ilix, 2);
5243   DTYPE dtype_src = ILI_DTyOPND(ilix, 3);
5244 
5245   ll_dst = make_lltype_from_dtype(dtype_dst);
5246   ll_src = make_lltype_from_dtype(dtype_src);
5247   assert(ll_dst->data_type == LL_VECTOR,
5248          "gen_convert_vector(): vector type expected for dst",
5249          ll_dst->data_type, ERR_Fatal);
5250   assert(ll_src->data_type == LL_VECTOR,
5251          "gen_convert_vector(): vector type expected for src",
5252          ll_src->data_type, ERR_Fatal);
5253   operand = gen_llvm_expr(ILI_OPND(ilix, 1), ll_src);
5254   switch (ll_dst->sub_types[0]->data_type) {
5255   case LL_I1:
5256   case LL_I8:
5257   case LL_I16:
5258   case LL_I24:
5259   case LL_I32:
5260   case LL_I40:
5261   case LL_I48:
5262   case LL_I56:
5263   case LL_I64:
5264   case LL_I128:
5265   case LL_I256:
5266     switch (ll_src->sub_types[0]->data_type) {
5267     case LL_I1:
5268     case LL_I8:
5269     case LL_I16:
5270     case LL_I24:
5271     case LL_I32:
5272     case LL_I40:
5273     case LL_I48:
5274     case LL_I56:
5275     case LL_I64:
5276     case LL_I128:
5277     case LL_I256:
5278       if (DT_ISUNSIGNED(dtype_dst))
5279         operand->flags |= OPF_ZEXT;
5280       return convert_int_size(ilix, operand, ll_dst);
5281     case LL_FLOAT:
5282     case LL_DOUBLE:
5283       if (DT_ISUNSIGNED(dtype_dst))
5284         return convert_float_to_uint(operand, ll_dst);
5285       return convert_float_to_sint(operand, ll_dst);
5286     default:
5287       break;
5288     }
5289     break;
5290   case LL_FLOAT:
5291     switch (ll_src->sub_types[0]->data_type) {
5292     case LL_I1:
5293     case LL_I8:
5294     case LL_I16:
5295     case LL_I24:
5296     case LL_I32:
5297     case LL_I40:
5298     case LL_I48:
5299     case LL_I56:
5300     case LL_I64:
5301     case LL_I128:
5302     case LL_I256:
5303       if (DT_ISUNSIGNED(dtype_src))
5304         return convert_uint_to_float(operand, ll_dst);
5305       return convert_sint_to_float(operand, ll_dst);
5306     case LL_DOUBLE:
5307       return convert_float_size(operand, ll_dst);
5308     default:
5309       break;
5310     }
5311     break;
5312   case LL_DOUBLE:
5313     switch (ll_src->sub_types[0]->data_type) {
5314     case LL_I1:
5315     case LL_I8:
5316     case LL_I16:
5317     case LL_I24:
5318     case LL_I32:
5319     case LL_I40:
5320     case LL_I48:
5321     case LL_I56:
5322     case LL_I64:
5323     case LL_I128:
5324     case LL_I256:
5325       if (DT_ISUNSIGNED(dtype_src))
5326         return convert_uint_to_float(operand, ll_dst);
5327       return convert_sint_to_float(operand, ll_dst);
5328     case LL_FLOAT:
5329       return convert_float_size(operand, ll_dst);
5330     default:
5331       break;
5332     }
5333     break;
5334   default:
5335     assert(0, "gen_convert_vector(): unhandled vector type for dst",
5336            ll_dst->sub_types[0]->data_type, ERR_Fatal);
5337   }
5338   assert(0, "gen_convert_vector(): unhandled vector type for src",
5339          ll_src->sub_types[0]->data_type, ERR_Fatal);
5340   return NULL;
5341 }
5342 
5343 static OPERAND *
gen_bitcast_vector(int ilix)5344 gen_bitcast_vector(int ilix)
5345 {
5346   LL_Type *ll_src, *ll_dst;
5347   OPERAND *operand;
5348 
5349   ll_src = make_lltype_from_dtype((DTYPE)ILI_OPND(ilix, 3));
5350   ll_dst = make_lltype_from_dtype((DTYPE)ILI_OPND(ilix, 2));
5351   assert(ll_src->data_type == LL_VECTOR,
5352          "gen_bitcast_vector(): source type is not a vector", ll_src->data_type,
5353          ERR_Fatal);
5354   assert(ll_dst->data_type == LL_VECTOR,
5355          "gen_bitcast_vector(): destination type is not a vector",
5356          ll_dst->data_type, ERR_Fatal);
5357   operand = gen_llvm_expr(ILI_OPND(ilix, 1), ll_src);
5358   return convert_operand(operand, ll_dst, I_BITCAST);
5359 }
5360 
5361 static OPERAND *
gen_binary_vexpr(int ilix,int itype_int,int itype_uint,int itype_float)5362 gen_binary_vexpr(int ilix, int itype_int, int itype_uint, int itype_float)
5363 {
5364   DTYPE vect_dtype = ili_get_vect_dtype(ilix);
5365   assert(vect_dtype,
5366          "gen_binary_vexpr(): called with non vector type for ilix ", ilix,
5367          ERR_Fatal);
5368   switch (DTY(DTySeqTyElement(vect_dtype))) {
5369   case TY_REAL:
5370   case TY_DBLE:
5371     return gen_binary_expr(ilix, itype_float);
5372   case TY_INT:
5373   case TY_SINT:
5374   case TY_BINT:
5375   case TY_INT8:
5376     return gen_binary_expr(ilix, itype_int);
5377   case TY_UINT:
5378   case TY_USINT:
5379   case TY_LOG:
5380   case TY_UINT8:
5381     return gen_binary_expr(ilix, itype_uint);
5382   default:
5383     assert(0, "gen_binary_vexpr(): vector type not yet handled for ilix ", ilix,
5384            ERR_Fatal);
5385   }
5386   return NULL;
5387 }
5388 
5389 /**
5390    \brief Is \p ilix a candidate for translation to FMA?
5391    \param ilix   The index of the ILI
5392    \return ILI of the multiply operand or 0 if not a candidate
5393 
5394    This inspects the operation at \p ilix to determine if it matches any of the
5395    suitable forms for a fused multiply add instruction.
5396    <pre>
5397    ([-] (([-] A) * ([-] B))) + [-] C
5398    </pre>
5399    where <tt>[-]</tt> is an optional negation/subraction.
5400  */
5401 static int
fused_multiply_add_candidate(int ilix)5402 fused_multiply_add_candidate(int ilix)
5403 {
5404   int l, r, lx, rx;
5405 
5406   switch (ILI_OPC(ilix)) {
5407 #if defined(USE_FMA_EXTENSIONS)
5408   case IL_FSUB:
5409 #endif
5410   case IL_FADD:
5411     lx = ILI_OPND(ilix, 1);
5412     l = ILI_OPC(lx);
5413     if (l == IL_FMUL)
5414       return lx;
5415     rx = ILI_OPND(ilix, 2);
5416     r = ILI_OPC(rx);
5417     if (r == IL_FMUL)
5418       return rx;
5419 #if defined(USE_FMA_EXTENSIONS)
5420     if ((l == IL_FNEG) && (ILI_OPC(ILI_OPND(lx, 1)) == IL_FMUL))
5421       return lx;
5422     if ((r == IL_FNEG) && (ILI_OPC(ILI_OPND(rx, 1)) == IL_FMUL))
5423       return rx;
5424 #endif
5425     break;
5426 #if defined(USE_FMA_EXTENSIONS)
5427   case IL_DSUB:
5428 #endif
5429   case IL_DADD:
5430     lx = ILI_OPND(ilix, 1);
5431     l = ILI_OPC(lx);
5432     if (l == IL_DMUL)
5433       return lx;
5434     rx = ILI_OPND(ilix, 2);
5435     r = ILI_OPC(rx);
5436     if (r == IL_DMUL)
5437       return rx;
5438 #if defined(USE_FMA_EXTENSIONS)
5439     if ((l == IL_DNEG) && (ILI_OPC(ILI_OPND(lx, 1)) == IL_DMUL))
5440       return lx;
5441     if ((r == IL_DNEG) && (ILI_OPC(ILI_OPND(rx, 1)) == IL_DMUL))
5442       return rx;
5443 #endif
5444     break;
5445   }
5446   return 0;
5447 }
5448 
5449 #if defined(TARGET_LLVM_X8664)
5450 /**
5451    \brief Get the x86 intrinsic name of the MAC instruction
5452    \param swap  [output] true if caller must swap arguments
5453    \param fneg  [output] true if multiply result is negated
5454    \param ilix  The root of the MAC (must be an ADD or SUB)
5455    \param l     The (original) lhs ili
5456    \param r     The (original) rhs ili
5457    \return the name of the intrinsic (sans "llvm." prefix)
5458  */
5459 static const char *
get_mac_name(int * swap,int * fneg,int ilix,int matches,int l,int r)5460 get_mac_name(int *swap, int *fneg, int ilix, int matches, int l, int r)
5461 {
5462   int opc;
5463 
5464   *swap = (matches == r);
5465   opc = ILI_OPC((*swap) ? r : l);
5466   *fneg = (opc == IL_FNEG) || (opc == IL_DNEG);
5467   switch (ILI_OPC(ilix)) {
5468   case IL_FADD:
5469     return (*fneg) ? "x86.fma.vfnmadd.ss" : "x86.fma.vfmadd.ss";
5470   case IL_DADD:
5471     return (*fneg) ? "x86.fma.vfnmadd.sd" : "x86.fma.vfmadd.sd";
5472   case IL_FSUB:
5473     if (*swap) {
5474       return (*fneg) ? "x86.fma.vfmadd.ss" : "x86.fma.vfnmadd.ss";
5475     }
5476     return (*fneg) ? "x86.fma.vfnmsub.ss" : "x86.fma.vfmsub.ss";
5477   case IL_DSUB:
5478     if (*swap) {
5479       return (*fneg) ? "x86.fma.vfmadd.sd" : "x86.fma.vfnmadd.sd";
5480     }
5481     return (*fneg) ? "x86.fma.vfnmsub.sd" : "x86.fma.vfmsub.sd";
5482   }
5483   assert(0, "does not match MAC", opc, ERR_Fatal);
5484   return "";
5485 }
5486 #endif
5487 
5488 /**
5489    \brief Put the candidate in proper canonical form
5490 
5491    The canonical form is lhs = (a * b), rhs = c, lhs + rhs.
5492  */
5493 static void
fused_multiply_add_canonical_form(INSTR_LIST * addInsn,int matches,ILI_OP opc,OPERAND ** l,OPERAND ** r,int * lhs_ili,int * rhs_ili)5494 fused_multiply_add_canonical_form(INSTR_LIST *addInsn, int matches, ILI_OP opc,
5495                                   OPERAND **l, OPERAND **r, int *lhs_ili,
5496                                   int *rhs_ili)
5497 {
5498   ILI_OP lopc;
5499   ILI_OP negOpc;
5500 
5501   (*l)->next = (*r)->next = NULL;
5502 
5503   if (opc == IL_FSUB) {
5504     /* negate the rhs. t1 - t2 => t1 + (-t2) */
5505     negOpc = IL_FNEG;
5506   } else if (opc == IL_DSUB) {
5507     negOpc = IL_DNEG;
5508   } else {
5509     /* it's already an ADD */
5510     negOpc = IL_NONE;
5511   }
5512 
5513   if (matches == *rhs_ili) {
5514     /* multiply on right, exchange lhs and rhs. Don't rewrite anything yet. */
5515     int tmp = *rhs_ili;
5516     OPERAND *t = *r;
5517     *rhs_ili = *lhs_ili;
5518     *r = *l;
5519     *lhs_ili = tmp;
5520     *l = t;
5521   } else if (negOpc) {
5522     /* handle subtract form when multiply was already on the left */
5523     const int ropc = ILI_OPC(*rhs_ili);
5524     if ((ropc == IL_FNEG) || (ropc == IL_DNEG)) {
5525       /* double negative */
5526       (*r)->tmps->use_count--;
5527       *r = (*r)->tmps->info.idef->operands->next;
5528     } else {
5529       OPERAND *neg = gen_llvm_expr(ad1ili(negOpc, *rhs_ili), (*r)->ll_type);
5530       if (neg->tmps)
5531         neg->tmps->info.idef->operands->next = *r;
5532       *r = neg;
5533     }
5534     negOpc = IL_NONE;
5535   }
5536 
5537   /* negOpc implies a swap was made. Fixup any negations now. */
5538   lopc = ILI_OPC(*lhs_ili);
5539   if (negOpc && ((lopc == IL_FNEG) || (lopc == IL_DNEG))) {
5540     /* double negation. -(-(a * b)) => (a * b) */
5541     (*l)->tmps->use_count--;
5542     *l = (*l)->tmps->info.idef->operands->next;
5543   } else if (negOpc || (lopc == IL_FNEG) || (lopc == IL_DNEG)) {
5544     /* swap mult and negate.  -(a * b) => (-a) * b */
5545     OPERAND *n, *newMul;
5546     /* l has form: (a * b) or (0 - (a * b)) */
5547     OPERAND *mul = negOpc ? *l : (*l)->tmps->info.idef->operands->next;
5548     OPERAND *mul_l = mul->tmps->info.idef->operands; /* a term */
5549     OPERAND *mul_r = mul_l->next;                    /* b term */
5550     int mulili = negOpc ? *lhs_ili : ILI_OPND(*lhs_ili, 1);
5551     int muliliop = ILI_OPC(mulili);
5552     int mulili_l = ILI_OPND(mulili, 1);
5553     LL_Type *fTy = mul_l->ll_type;
5554     /* create n, where n ::= -a */
5555     if (muliliop == IL_FMUL) {
5556       n = gen_llvm_expr(ad1ili(IL_FNEG, mulili_l), fTy);
5557     } else {
5558       assert(ILI_OPC(mulili) == IL_DMUL, "unexpected expr", mulili, ERR_Fatal);
5559       n = gen_llvm_expr(ad1ili(IL_DNEG, mulili_l), fTy);
5560     }
5561     /* rebuild the multiply */
5562     if (n->tmps)
5563       n->tmps->info.idef->operands->next = mul_l;
5564     n->next = mul_r;
5565     newMul = make_tmp_op(mul->ll_type, make_tmps());
5566     ad_instr(mulili, gen_instr(I_FMUL, newMul->tmps, mul->ll_type, n));
5567     *l = newMul; /* l ::= (n * b) = (-a * b) */
5568   }
5569 }
5570 
5571 /**
5572    \brief Does this multiply op have more than one use?
5573    \param multop A multiply operation (unchecked)
5574  */
5575 INLINE static bool
fma_mult_has_mult_uses(OPERAND * multop)5576 fma_mult_has_mult_uses(OPERAND *multop)
5577 {
5578   return (!multop->tmps) || (multop->tmps->use_count > 1);
5579 }
5580 
5581 static void
overwrite_fma_add(INSTR_LIST * oldAdd,INSTR_LIST * newFma,INSTR_LIST * last)5582 overwrite_fma_add(INSTR_LIST *oldAdd, INSTR_LIST *newFma, INSTR_LIST *last)
5583 {
5584   INSTR_LIST *prev = oldAdd->prev;
5585   INSTR_LIST *start = last->next;
5586   OPERAND *op;
5587   last->next = NULL;
5588   start->prev = NULL;
5589   if (start != newFma) {
5590     prev->next = start;
5591     start->prev = prev;
5592     newFma->prev->next = oldAdd;
5593     oldAdd->prev = newFma->prev;
5594   }
5595   /* updated in place since uses have pointers to oldAdd */
5596   oldAdd->rank = newFma->rank;
5597   oldAdd->i_name = newFma->i_name;
5598   oldAdd->ilix = newFma->ilix;
5599   oldAdd->flags = newFma->flags;
5600   oldAdd->ll_type = newFma->ll_type;
5601   oldAdd->operands = newFma->operands;
5602   /* force the DCE pass to keep the FMA arguments alive */
5603   for (op = oldAdd->operands; op; op = op->next) {
5604     if (op->tmps)
5605       op->tmps->use_count += 100;
5606   }
5607 }
5608 
5609 #if defined(TARGET_LLVM_X8664)
5610 static OPERAND *
x86_promote_to_vector(LL_Type * eTy,LL_Type * vTy,OPERAND * op)5611 x86_promote_to_vector(LL_Type *eTy, LL_Type *vTy, OPERAND *op)
5612 {
5613   OPERAND *op1, *undefop, *next;
5614 
5615   if (op == NULL)
5616     return NULL;
5617 
5618   undefop = make_undef_op(vTy);
5619   next = op->next;
5620   op1 = gen_insert_vector(undefop, gen_copy_op(op), 0);
5621   op1->next = x86_promote_to_vector(eTy, vTy, next);
5622   return op1;
5623 }
5624 #endif
5625 
5626 /**
5627    \brief Replace add instruction with an FMA when all conditions are met
5628  */
5629 static void
maybe_generate_fma(int ilix,INSTR_LIST * insn)5630 maybe_generate_fma(int ilix, INSTR_LIST *insn)
5631 {
5632   int lhs_ili = ILI_OPND(ilix, 1);
5633   int rhs_ili = ILI_OPND(ilix, 2);
5634   int matches;
5635   ILI_OP opc;
5636   int isSinglePrec;
5637   const char *intrinsicName;
5638   OPERAND *l_l, *l_r, *l, *r, *binops, *fmaop;
5639   LL_Type *llTy;
5640   INSTR_LIST *fma, *last;
5641 #if defined(USE_FMA_EXTENSIONS)
5642   int swap, fneg;
5643   OPERAND *mul;
5644 #endif
5645 #if defined(TARGET_LLVM_X8664)
5646   LL_Type *vTy;
5647   INSTR_LIST *mulPrev, *mulNext;
5648 #endif
5649 
5650   last = llvm_info.last_instr;
5651   binops = insn->operands;
5652   if (lhs_ili == rhs_ili)
5653     return;
5654 
5655   matches = fused_multiply_add_candidate(ilix);
5656   if (!matches)
5657     return;
5658 
5659   l = (matches == lhs_ili) ? binops : binops->next;
5660   if (fma_mult_has_mult_uses(l))
5661     return;
5662 
5663   opc = ILI_OPC(matches);
5664   if ((opc == IL_FNEG) || (opc == IL_DNEG))
5665     if (fma_mult_has_mult_uses(l->tmps->info.idef->operands->next))
5666       return;
5667   l = binops;
5668   r = binops->next;
5669   opc = ILI_OPC(ilix);
5670   /* put in canonical form: left:mult, right:_ */
5671   killCSE = true;
5672   clear_csed_list();
5673   isSinglePrec = (opc == IL_FADD) || (opc == IL_FSUB);
5674   llTy = make_lltype_from_dtype(isSinglePrec ? DT_FLOAT : DT_DBLE);
5675 #if defined(USE_FMA_EXTENSIONS)
5676   /* use intrinsics for specific target instructions */
5677   intrinsicName = get_mac_name(&swap, &fneg, ilix, matches, lhs_ili, rhs_ili);
5678   if (swap) {
5679     OPERAND *t = r;
5680     r = l;
5681     l = t;
5682   }
5683   mul = fneg ? l->tmps->info.idef->operands->next : l;
5684   l_l = mul->tmps->info.idef->operands;
5685   l_r = l_l->next;
5686   l_r->next = r;
5687   r->next = NULL;
5688   l->tmps->use_count--;
5689 #if defined(TARGET_LLVM_X8664)
5690   vTy = ll_get_vector_type(llTy, (isSinglePrec ? 4 : 2));
5691   l_l = x86_promote_to_vector(llTy, vTy, l_l);
5692   llTy = vTy;
5693 #endif
5694 #else /* not Power/LLVM or X86-64/LLVM */
5695   /* use the documented LLVM intrinsic: '@llvm.fma.*' */
5696   fused_multiply_add_canonical_form(insn, matches, opc, &l, &r, &lhs_ili,
5697                                     &rhs_ili);
5698   /* llvm.fma ::= madd(l.l * l.r + r), assemble args in the LLVM order */
5699   l_l = l->tmps->info.idef->operands;
5700   l_r = l_l->next;
5701   l_r->next = r;
5702   l->tmps->use_count--;
5703   intrinsicName = isSinglePrec ? "fma.f32" : "fma.f64";
5704 #endif
5705   fmaop = gen_call_llvm_intrinsic(intrinsicName, l_l, llTy, NULL, I_PICALL);
5706 #if defined(TARGET_LLVM_X8664)
5707   fmaop->tmps->use_count++;
5708   fmaop = gen_extract_vector(fmaop, 0);
5709 #endif
5710   fmaop->tmps->use_count++;
5711   fma = fmaop->tmps->info.idef;
5712   overwrite_fma_add(insn, fma, last);
5713   if (DBGBIT(12, 0x40000))
5714     printf("fma %s inserted at ili %d\n", intrinsicName, ilix);
5715   ccff_info(MSGOPT, "OPT051", gbl.findex, gbl.lineno,
5716             "FMA (fused multiply-add) instruction(s) generated", NULL);
5717   llvm_info.last_instr = last;
5718   killCSE = false;
5719 }
5720 
5721 /**
5722    \brief Find and rewrite any multiply-add opportunities
5723    \param isns  The list of instructions
5724  */
5725 static void
fma_rewrite(INSTR_LIST * isns)5726 fma_rewrite(INSTR_LIST *isns)
5727 {
5728   INSTR_LIST *p;
5729 
5730   for (p = isns; p; p = p->next) {
5731     int ilx = p->ilix;
5732     if (ilx && p->tmps && p->operands && (p->tmps->use_count > 0))
5733       maybe_generate_fma(ilx, p);
5734   }
5735 }
5736 
5737 /**
5738    \brief Undoes the multiply-by-reciprocal transformation
5739    \param isns  The list of instructions
5740  */
5741 static void
undo_recip_div(INSTR_LIST * isns)5742 undo_recip_div(INSTR_LIST *isns)
5743 {
5744   INSTR_LIST *p;
5745 
5746   for (p = isns; p; p = p->next)
5747     if (p->ilix && p->tmps && p->operands && (p->tmps->use_count > 0) &&
5748         (p->i_name == I_FMUL))
5749       maybe_undo_recip_div(p);
5750 }
5751 
5752 static OPERAND *
gen_binary_expr(int ilix,int itype)5753 gen_binary_expr(int ilix, int itype)
5754 {
5755   int lhs_ili, rhs_ili, ret_match, size1, size2;
5756   int vect_type;
5757   DTYPE vect_dtype = DT_NONE;
5758   LL_InstrListFlags flags = InstrListFlagsNull;
5759   ILI_OP opc = ILI_OPC(ilix);
5760   OPERAND *operand, *binops, *load_op;
5761   LL_Type *opc_type, *instr_type, *ll_tmp, *load_type;
5762   TMPS *new_tmps;
5763   INT val[2];
5764   union {
5765     double d;
5766     INT tmp[2];
5767   } dtmp;
5768   float f;
5769   double d;
5770 
5771   DBGTRACEIN2(" ilix: %d(%s)", ilix, IL_NAME(opc))
5772 
5773   lhs_ili = ILI_OPND(ilix, 1);
5774   rhs_ili = ILI_OPND(ilix, 2);
5775 
5776   switch (opc) {
5777   case IL_VMUL:
5778   case IL_IMUL:
5779   case IL_KMUL:
5780   case IL_VSUB:
5781   case IL_ISUB:
5782   case IL_KSUB:
5783   case IL_VADD:
5784   case IL_IADD:
5785   case IL_KADD:
5786   case IL_VLSHIFTV:
5787   case IL_VLSHIFTS:
5788   case IL_LSHIFT:
5789   case IL_KLSHIFT:
5790   case IL_VNEG:
5791   case IL_INEG:
5792   case IL_KNEG:
5793     flags |= NOSIGNEDWRAP;
5794     break;
5795   default:
5796     break;
5797   }
5798 
5799   /* handle conditional vectorization  where we want the inverse mask -
5800    * (1) in the case opc == IL_VNOT and the lhs_ili is a VCMP
5801    * (2) in the case opc == IL_VNOT and the lhs_ili is a VPERMUTE pointing
5802    * to a VCMP. Here we have a half-size predicate compared to computation.
5803    */
5804   if (opc == IL_VNOT && (ILI_OPC(lhs_ili) == IL_VCMP ||
5805                          (ILI_OPC(lhs_ili) == IL_VPERMUTE &&
5806                           ILI_OPC(ILI_OPND(lhs_ili, 1)) == IL_VCMP))) {
5807     int num_elem, constant;
5808     LL_Type *bit_type, *mask_type;
5809     OPERAND *bit_mask_of_ones;
5810     DTYPE vdt, ones_dtype;
5811     SPTR vcon1_sptr;
5812     vect_dtype = ili_get_vect_dtype(lhs_ili);
5813     num_elem = DTyVecLength(vect_dtype);
5814     switch (DTySeqTyElement(vect_dtype)) {
5815     case DT_INT:
5816     case DT_FLOAT:
5817       if (ILI_OPC(lhs_ili) == IL_VPERMUTE)
5818         bit_type = make_int_lltype(64); /* half-size predicate */
5819       else
5820         bit_type = make_int_lltype(32);
5821       ones_dtype = DT_INT;
5822       vdt = get_vector_dtype(ones_dtype, num_elem);
5823       vcon1_sptr = get_vcon_scalar(0xffffffff, vdt);
5824       break;
5825     case DT_INT8:
5826     case DT_DBLE:
5827       bit_type = make_int_lltype(64);
5828       val[0] = 0xffffffff;
5829       val[1] = 0xffffffff;
5830       ones_dtype = DT_INT8;
5831       vdt = get_vector_dtype(ones_dtype, num_elem);
5832       constant = getcon(val, ones_dtype);
5833       vcon1_sptr = get_vcon_scalar(constant, vdt);
5834       break;
5835     default:
5836       assert(0, "Unexpected dtype for VNOT", DTySeqTyElement(vect_dtype),
5837              ERR_Fatal);
5838     }
5839     bit_mask_of_ones = gen_llvm_expr(ad1ili(IL_VCON, vcon1_sptr), 0);
5840     mask_type = ll_get_vector_type(bit_type, num_elem);
5841     binops = gen_llvm_expr(lhs_ili, mask_type);
5842     instr_type = binops->ll_type;
5843     binops->next = convert_int_size(ilix, bit_mask_of_ones, instr_type);
5844     goto make_binary_expression;
5845   }
5846 
5847   /* account for the *NEG ili - LLVM treats all of these as subtractions
5848    * from zero.
5849    */
5850   if (!rhs_ili || !IL_ISLINK(opc, 2)) {
5851     rhs_ili = lhs_ili;
5852     switch (opc) {
5853     case IL_NOT:
5854     case IL_UNOT:
5855       lhs_ili = ad_icon(-1);
5856       break;
5857     case IL_KNOT:
5858     case IL_UKNOT:
5859       lhs_ili = ad_kconi(-1);
5860       break;
5861     case IL_VNOT:
5862       vect_dtype = ili_get_vect_dtype(ilix);
5863       switch (DTY(DTySeqTyElement(vect_dtype))) {
5864       case TY_UINT8:
5865       case TY_INT8: {
5866         INT num[2];
5867         ISZ_2_INT64(-1, num);
5868         lhs_ili = ad1ili(
5869             IL_VCON, get_vconi((DTYPE)ILI_OPND(ilix, 2), getcon(num, DT_INT8)));
5870       } break;
5871       case TY_REAL:
5872       case TY_DBLE:
5873         assert(0, "gen_binary_expr(): VNOT of float/double not handled yet", 0,
5874                ERR_Fatal);
5875         break;
5876       default:
5877         lhs_ili = ad1ili(IL_VCON, get_vconi((DTYPE)ILI_OPND(ilix, 2), -1));
5878       }
5879       break;
5880     case IL_INEG:
5881     case IL_UINEG:
5882       lhs_ili = ad_icon(0);
5883       break;
5884     case IL_KNEG:
5885     case IL_UKNEG:
5886       lhs_ili = ad_kconi(0);
5887       break;
5888     case IL_FNEG:
5889       lhs_ili = ad1ili(IL_FCON, stb.fltm0);
5890       break;
5891     case IL_DNEG:
5892       lhs_ili = ad1ili(IL_DCON, stb.dblm0);
5893       break;
5894 #ifdef LONG_DOUBLE_FLOAT128
5895     case IL_FLOAT128CHS:
5896       lhs_ili = ad1ili(IL_FLOAT128CON, stb.float128_0);
5897       break;
5898 #endif
5899     case IL_VNEG:
5900       vect_dtype = ILI_DTyOPND(ilix, 2);
5901       lhs_ili = ad1ili(IL_VCON, get_vconm0(vect_dtype));
5902       vect_type = DTY(DTySeqTyElement(vect_dtype));
5903       break;
5904     default:
5905       DBGTRACE1("#opcode %s not handled as *NEG ili", IL_NAME(opc))
5906       assert(0, "gen_binary_expr(): confusion with opcode", opc, ERR_Fatal);
5907     }
5908   }
5909   vect_dtype = ili_get_vect_dtype(ilix);
5910   if (vect_dtype) {
5911     instr_type = make_lltype_from_dtype(vect_dtype);
5912   } else
5913       if ((instr_type = make_type_from_opc(opc)) == NULL) {
5914     assert(0, "gen_binary_expr(): no type information", 0, ERR_Fatal);
5915   }
5916 
5917   DBGTRACE2("#generating first binary operand, lhs_ili: %d(%s)", lhs_ili,
5918             IL_NAME(ILI_OPC(lhs_ili)))
5919 
5920   binops = gen_llvm_expr(lhs_ili, instr_type);
5921 
5922   DBGTRACE2("#generating second binary operand, rhs_ili: %d(%s)", rhs_ili,
5923             IL_NAME(ILI_OPC(rhs_ili)))
5924 
5925   switch (opc) {
5926   case IL_KLSHIFT:
5927   case IL_KURSHIFT:
5928   case IL_KARSHIFT:
5929     binops->next = gen_llvm_expr(rhs_ili, make_lltype_from_dtype(DT_UINT8));
5930     break;
5931   case IL_LSHIFT:
5932   case IL_ULSHIFT:
5933   case IL_URSHIFT:
5934   case IL_RSHIFT:
5935   case IL_ARSHIFT:
5936     binops->next = gen_llvm_expr(rhs_ili, make_lltype_from_dtype(DT_UINT));
5937     break;
5938   case IL_VLSHIFTS:
5939   case IL_VRSHIFTS:
5940   case IL_VURSHIFTS:
5941     binops->next =
5942         gen_temp_to_vector(rhs_ili, make_vtype(DTySeqTyElement(vect_dtype),
5943                                                DTyVecLength(vect_dtype)));
5944     break;
5945   default:
5946     binops->next = gen_llvm_expr(rhs_ili, instr_type);
5947   }
5948 
5949 make_binary_expression:
5950   /* now make the new binary expression */
5951   operand =
5952       ad_csed_instr((LL_InstrName)itype, ilix, instr_type, binops, flags, true);
5953 
5954   DBGTRACEOUT1(" returns operand %p", operand)
5955 
5956   return operand;
5957 } /* gen_binary_expr */
5958 
5959 /* Compute the high result bits of a multiplication.
5960  *
5961  * ilix should be an IL_KMULH or IL_UKMULH instruction.
5962  */
5963 static OPERAND *
gen_mulh_expr(int ilix)5964 gen_mulh_expr(int ilix)
5965 {
5966   LL_InstrName ext_instr, shr_instr;
5967   LL_InstrListFlags mul_flags;
5968   int bits = 64;
5969   OPERAND *lhs, *rhs, *result;
5970   LL_Type *op_llt, *big_llt;
5971 
5972   switch (ILI_OPC(ilix)) {
5973   case IL_KMULH:
5974     ext_instr = I_SEXT;
5975     mul_flags = NOSIGNEDWRAP;
5976     shr_instr = I_ASHR;
5977     break;
5978   case IL_UKMULH:
5979     ext_instr = I_ZEXT;
5980     mul_flags = NOUNSIGNEDWRAP;
5981     shr_instr = I_LSHR;
5982     break;
5983   default:
5984     interr("Unknown mulh opcode", ILI_OPC(ilix), ERR_Fatal);
5985   }
5986 
5987   /* Extend both sides to i128. */
5988   op_llt = make_int_lltype(bits);
5989   big_llt = make_int_lltype(2 * bits);
5990   lhs = gen_llvm_expr(ILI_OPND(ilix, 1), op_llt);
5991   rhs = gen_llvm_expr(ILI_OPND(ilix, 2), op_llt);
5992   lhs = ad_csed_instr(ext_instr, ilix, big_llt, lhs, InstrListFlagsNull, true);
5993   rhs = ad_csed_instr(ext_instr, ilix, big_llt, rhs, InstrListFlagsNull, true);
5994 
5995   /* Do the multiplication in 128 bits. */
5996   lhs->next = rhs;
5997   result = ad_csed_instr(I_MUL, ilix, big_llt, lhs, mul_flags, true);
5998 
5999   /* Shift down to get the high bits. */
6000   result->next = make_constval_op(big_llt, bits, 0);
6001   result =
6002       ad_csed_instr(shr_instr, ilix, big_llt, result, InstrListFlagsNull, true);
6003 
6004   /* Finally truncate down to 64 bits */
6005   return ad_csed_instr(I_TRUNC, ilix, op_llt, result, InstrListFlagsNull, true);
6006 }
6007 
6008 /**
6009    return new operand of type OT_TMP as result of converting cast_op.  If
6010    cast_op has a next, make sure the next pointer is dealt with properly BEFORE
6011    the call to make_bitcast().
6012  */
6013 static OPERAND *
make_bitcast(OPERAND * cast_op,LL_Type * rslt_type)6014 make_bitcast(OPERAND *cast_op, LL_Type *rslt_type)
6015 {
6016   OPERAND *operand;
6017   TMPS *new_tmps;
6018   INSTR_LIST *Curr_Instr, *instr;
6019 
6020   if (strict_match(cast_op->ll_type, rslt_type))
6021     return gen_copy_op(cast_op);
6022 
6023   assert(ll_type_bytes(cast_op->ll_type) == ll_type_bytes(rslt_type),
6024          "sizes do not match", 0, ERR_Fatal);
6025 
6026   if (cast_op->ot_type == OT_TMP) {
6027     instr = cast_op->tmps->info.idef;
6028     if (instr && (instr->i_name == I_BITCAST) &&
6029         strict_match(instr->operands->ll_type, rslt_type)) {
6030       return gen_copy_op(instr->operands);
6031     }
6032   }
6033 
6034   DBGTRACEIN1(" cast op: %p", cast_op)
6035   DBGDUMPLLTYPE("result type ", rslt_type)
6036   DBGDUMPLLTYPE("cast_op type ", cast_op->ll_type)
6037 
6038   if (ENABLE_CSE_OPT) {
6039     instr = llvm_info.last_instr;
6040     while (instr) {
6041       switch (instr->i_name) {
6042       case I_BR:
6043       case I_INDBR:
6044       case I_NONE:
6045         instr = NULL;
6046         break;
6047       case I_BITCAST:
6048         if (same_op(cast_op, instr->operands) &&
6049             strict_match(rslt_type, instr->ll_type)) {
6050           operand = make_tmp_op(rslt_type, instr->tmps);
6051           DBGTRACEOUT1(" returns CSE'd operand %p\n", operand)
6052 
6053           return operand;
6054         }
6055       default:
6056         if (instr->flags & STARTEBB)
6057           instr = NULL;
6058         else
6059           instr = instr->prev;
6060       }
6061     }
6062   }
6063   Curr_Instr = gen_instr(I_BITCAST, new_tmps = make_tmps(), rslt_type, cast_op);
6064   cast_op->next = NULL;
6065   ad_instr(0, Curr_Instr);
6066   /* now build the operand */
6067   operand = make_tmp_op(rslt_type, new_tmps);
6068 
6069   DBGTRACEOUT1(" returns operand %p\n", operand)
6070 
6071   return operand;
6072 } /* make_bitcast */
6073 
6074 /* return new operand of type OT_TMP as result of converting convert_op,
6075  * which is floating pt but needs coercion to the larger size within rslt_type.
6076  * If the passed operand convert_op has a next pointer, make sure it
6077  * is handled BEFORE this call!
6078  */
6079 static OPERAND *
convert_float_size(OPERAND * convert_op,LL_Type * rslt_type)6080 convert_float_size(OPERAND *convert_op, LL_Type *rslt_type)
6081 {
6082   LL_Type *ty1, *ty2;
6083   int kind1, kind2;
6084   int conversion_instr;
6085   OPERAND *op_tmp;
6086   TMPS *new_tmps;
6087   INSTR_LIST *Curr_Instr;
6088 
6089   DBGTRACEIN1(" convert op %p", convert_op)
6090   DBGDUMPLLTYPE("result type ", rslt_type)
6091 
6092   ty1 = convert_op->ll_type;
6093   ty2 = rslt_type;
6094   kind1 = (ty1->data_type == LL_VECTOR) ? ty1->sub_types[0]->data_type
6095                                         : ty1->data_type;
6096   kind2 = (ty2->data_type == LL_VECTOR) ? ty2->sub_types[0]->data_type
6097                                         : ty2->data_type;
6098   if (kind1 > kind2)
6099     conversion_instr = I_FPTRUNC;
6100   else
6101     conversion_instr = I_FPEXT;
6102   new_tmps = make_tmps();
6103   op_tmp = make_tmp_op(ty2, new_tmps);
6104   Curr_Instr = gen_instr((LL_InstrName)conversion_instr, new_tmps,
6105                          op_tmp->ll_type, convert_op);
6106   convert_op->next = NULL;
6107   ad_instr(0, Curr_Instr);
6108 
6109   DBGTRACEOUT1(" returns operand %p", op_tmp)
6110   return op_tmp;
6111 } /* convert_float_size */
6112 
6113 /** return new operand of type OT_TMP as result of converting convert_op, which
6114     is an int but needs coercion to the int size within rslt_type.  If the
6115     passed operand convert_op has a next pointer, make sure it is handled BEFORE
6116     this call! */
6117 static OPERAND *
convert_int_size(int ilix,OPERAND * convert_op,LL_Type * rslt_type)6118 convert_int_size(int ilix, OPERAND *convert_op, LL_Type *rslt_type)
6119 {
6120   LL_Type *ty1, *ty2, *ll_type;
6121   int size1, size2, flags1, flags2, conversion_instr;
6122   enum LL_BaseDataType kind1, kind2;
6123   OPERAND *op_tmp;
6124   TMPS *new_tmps;
6125   INSTR_LIST *Curr_Instr;
6126 
6127   DBGTRACEIN1(" convert op %p", convert_op)
6128   DBGDUMPLLTYPE("result type ", rslt_type)
6129 
6130   ty1 = convert_op->ll_type;
6131   ty2 = rslt_type;
6132   if (ty1->data_type == LL_VECTOR) {
6133     kind1 = ty1->sub_types[0]->data_type;
6134     size1 = ll_type_int_bits(ty1->sub_types[0]);
6135     if (!size1)
6136       size1 = ll_type_bytes(ty1->sub_types[0]) * 8;
6137   } else {
6138     kind1 = ty1->data_type;
6139     size1 = ll_type_int_bits(ty1);
6140     if (!size1)
6141       size1 = ll_type_bytes(ty1) * 8;
6142   }
6143   flags1 = convert_op->flags;
6144   if (ty2->data_type == LL_VECTOR) {
6145     kind2 = ty2->sub_types[0]->data_type;
6146     size2 = ll_type_int_bits(ty2->sub_types[0]);
6147     if (!size2)
6148       size2 = ll_type_bytes(ty2->sub_types[0]) * 8;
6149   } else {
6150     kind2 = ty2->data_type;
6151     size2 = ll_type_int_bits(ty2);
6152     if (!size2)
6153       size2 = ll_type_bytes(ty2) * 8;
6154   }
6155   if (ty1->data_type != LL_VECTOR) {
6156     assert(ll_type_int_bits(ty1),
6157            "convert_int_size(): expected int type for src", kind1, ERR_Fatal);
6158   }
6159   if (ty2->data_type != LL_VECTOR) {
6160     assert(ll_type_int_bits(ty2),
6161            "convert_int_size(): expected int type for dst", kind2, ERR_Fatal);
6162   }
6163   /* need conversion, either extension or truncation */
6164   if (size1 < size2) {
6165     /* extension */
6166     conversion_instr = (flags1 & OPF_ZEXT) ? I_ZEXT : I_SEXT;
6167   } else if (size1 > size2) {
6168     /* size1 > size2, truncation */
6169     conversion_instr = I_TRUNC;
6170   } else {
6171     DBGTRACE("#conversion of same size, should be a conversion signed/unsigned")
6172     DBGTRACEOUT1(" returns operand %p", convert_op)
6173     return convert_op;
6174   }
6175 
6176   DBGTRACE2("#coercing ints to size %d with instruction %s", size2,
6177             llvm_instr_names[conversion_instr])
6178 
6179   new_tmps = make_tmps();
6180   ll_type = ty2;
6181   op_tmp = ad_csed_instr((LL_InstrName)conversion_instr, ilix, ll_type,
6182                          convert_op, InstrListFlagsNull, true);
6183 
6184   DBGTRACEOUT1(" returns operand %p", op_tmp)
6185   return op_tmp;
6186 } /* convert_int_size */
6187 
6188 static OPERAND *
convert_operand(OPERAND * convert_op,LL_Type * rslt_type,LL_InstrName convert_instruction)6189 convert_operand(OPERAND *convert_op, LL_Type *rslt_type,
6190                 LL_InstrName convert_instruction)
6191 {
6192   LL_Type *ty, *ll_type;
6193   int size;
6194   OPERAND *op_tmp;
6195   TMPS *new_tmps;
6196   INSTR_LIST *Curr_Instr;
6197 
6198   DBGTRACEIN1(" convert op %p", convert_op)
6199   DBGDUMPLLTYPE("result type ", rslt_type)
6200 
6201   ty = convert_op->ll_type;
6202   size = ll_type_bytes(ty) * 8;
6203   new_tmps = make_tmps();
6204   ll_type = rslt_type;
6205   op_tmp = make_tmp_op(ll_type, new_tmps);
6206   Curr_Instr = gen_instr(convert_instruction, new_tmps, ll_type, convert_op);
6207   ad_instr(0, Curr_Instr);
6208   DBGTRACEOUT1(" returns operand %p", op_tmp)
6209   return op_tmp;
6210 }
6211 
6212 static OPERAND *
convert_int_to_ptr(OPERAND * convert_op,LL_Type * rslt_type)6213 convert_int_to_ptr(OPERAND *convert_op, LL_Type *rslt_type)
6214 {
6215   const LL_Type *llt = convert_op->ll_type;
6216   OPERAND* operand;
6217   assert(llt,"convert_int_to_ptr(): missing incoming type",0,ERR_Fatal);
6218   assert(ll_type_int_bits(llt) == 8 * size_of(DT_CPTR),
6219          "Unsafe type for inttoptr", ll_type_int_bits(llt), ERR_Fatal);
6220   return convert_operand(convert_op, rslt_type, I_INTTOPTR);
6221 }
6222 
6223 static OPERAND *
sign_extend_int(OPERAND * op,unsigned result_bits)6224 sign_extend_int(OPERAND *op, unsigned result_bits)
6225 {
6226   const LL_Type *llt = op->ll_type;
6227   assert(ll_type_int_bits(llt) && (ll_type_int_bits(llt) < result_bits),
6228          "sign_extend_int: bad type", ll_type_int_bits(llt), ERR_Fatal);
6229   return convert_operand(op, make_int_lltype(result_bits), I_SEXT);
6230 }
6231 
6232 static OPERAND *
zero_extend_int(OPERAND * op,unsigned result_bits)6233 zero_extend_int(OPERAND *op, unsigned result_bits)
6234 {
6235   const LL_Type *llt = op->ll_type;
6236   assert(ll_type_int_bits(llt) && (ll_type_int_bits(llt) < result_bits),
6237          "zero_extend_int: bad type", ll_type_int_bits(llt), ERR_Fatal);
6238   return convert_operand(op, make_int_lltype(result_bits), I_ZEXT);
6239 }
6240 
6241 static INSTR_LIST *
remove_instr(INSTR_LIST * instr,bool update_usect_only)6242 remove_instr(INSTR_LIST *instr, bool update_usect_only)
6243 {
6244   INSTR_LIST *prev, *next;
6245   OPERAND *operand;
6246 
6247   prev = instr->prev;
6248   next = instr->next;
6249   if (!update_usect_only) {
6250     if (next)
6251       next->prev = prev;
6252     else
6253       llvm_info.last_instr = prev;
6254     if (prev)
6255       prev->next = next;
6256     else
6257       Instructions = next;
6258   }
6259   for (operand = instr->operands; operand; operand = operand->next) {
6260     if (operand->ot_type == OT_TMP) {
6261       assert(operand->tmps, "remove_instr(): missing temp operand", 0,
6262              ERR_Fatal);
6263       operand->tmps->use_count--;
6264     }
6265   }
6266 
6267   return prev;
6268 }
6269 
6270 static bool
same_op(OPERAND * op1,OPERAND * op2)6271 same_op(OPERAND *op1, OPERAND *op2)
6272 {
6273   if (op1->ot_type != op2->ot_type)
6274     return false;
6275   switch (op1->ot_type) {
6276   case OT_TMP:
6277     return (op1->tmps == op2->tmps);
6278   case OT_VAR:
6279     return (op1->val.sptr == op2->val.sptr);
6280   case OT_CONSTVAL:
6281     return (op1->val.conval[0] == op2->val.conval[0]) &&
6282            (op1->val.conval[1] == op2->val.conval[1]);
6283   default:
6284     return false;
6285   }
6286 }
6287 
6288 /** Return true if a load can be moved upwards (backwards in time)
6289     over fencing specified by the given instruction. */
6290 static bool
can_move_load_up_over_fence(INSTR_LIST * instr)6291 can_move_load_up_over_fence(INSTR_LIST *instr)
6292 {
6293   switch (instr->flags & ATOMIC_MEM_ORD_FLAGS) {
6294   case ATOMIC_ACQUIRE_FLAG:
6295   case ATOMIC_ACQ_REL_FLAG:
6296   case ATOMIC_SEQ_CST_FLAG:
6297     return false;
6298   default:
6299     return true;
6300   }
6301 }
6302 
6303 /**
6304    \brief Clear DELETABLE flag from previous instructions
6305    \param ilix  The ILI of a LOAD instruction
6306  */
6307 void
clear_deletable_flags(int ilix)6308 clear_deletable_flags(int ilix)
6309 {
6310   INSTR_LIST *instr;
6311   int ld_nme;
6312 
6313   DEBUG_ASSERT(IL_TYPE(ILI_OPC(ilix)) == ILTY_LOAD, "must be load");
6314   ld_nme = ILI_OPND(ilix, 2);
6315   for (instr = llvm_info.last_instr; instr; instr = instr->prev) {
6316     if (instr->i_name == I_STORE) {
6317       if (instr->ilix == 0) {
6318         instr->flags &= ~DELETABLE;
6319         continue;
6320       }
6321       if (ld_nme == ILI_OPND(instr->ilix, 3)) {
6322         instr->flags &= ~DELETABLE;
6323         break;
6324       }
6325     }
6326   }
6327 }
6328 
6329 INLINE static OPERAND *
find_load_cse(int ilix,OPERAND * load_op,LL_Type * llt)6330 find_load_cse(int ilix, OPERAND *load_op, LL_Type *llt)
6331 {
6332   INSTR_LIST *instr, *del_store_instr, *last_instr;
6333   int del_store_flags;
6334   int ld_nme;
6335   int c;
6336 
6337   if (new_ebb || (!ilix) || (IL_TYPE(ILI_OPC(ilix)) != ILTY_LOAD))
6338     return NULL;
6339 
6340   ld_nme = ILI_OPND(ilix, 2);
6341   if (ld_nme == NME_VOL) /* don't optimize a VOLATILE load */
6342     return NULL;
6343 
6344   /* If there is a deletable store to 'ld_nme', 'del_store_li', set
6345    * its 'deletable' flag to false.  We do this because 'ld_ili'
6346    * loads from that address, so we mustn't delete the preceding
6347    * store to it.  However, if the following LILI scan reaches
6348    * 'del_store_li', *and* we return the expression that is stored
6349    * by 'del_store_li', then we restore its 'deletable' flag, since
6350    * in that case the store *can* be deleted.
6351    * We track deletable store in EBB but perform CSE load opt only in
6352    * BB to avoid LLVM opt to fail, so we have to mark stores in EBB as
6353    * undeletable
6354    */
6355   del_store_instr = NULL;
6356   last_instr = NULL;
6357 
6358   for (instr = llvm_info.last_instr; instr; instr = instr->prev) {
6359     if ((instr->i_name == I_STORE) && instr->ilix &&
6360         (ld_nme == ILI_OPND(instr->ilix, 3))) {
6361       del_store_instr = instr;
6362       del_store_flags = del_store_instr->flags;
6363       del_store_instr->flags &= ~DELETABLE;
6364       break;
6365     }
6366     if (instr->flags & STARTEBB) {
6367       last_instr = (instr->i_name != I_NONE) ? instr : instr->prev;
6368       break;
6369     }
6370   }
6371 
6372   for (instr = llvm_info.last_instr; instr != last_instr; instr = instr->prev) {
6373     if (instr->ilix == ilix)
6374       return same_op(instr->operands, load_op)
6375                  ? make_tmp_op(instr->ll_type, instr->tmps)
6376                  : NULL;
6377 
6378     switch (instr->i_name) {
6379     case I_LOAD:
6380     case I_CMPXCHG:
6381     case I_ATOMICRMW:
6382     case I_FENCE:
6383       DEBUG_ASSERT(instr->ilix != 0 || instr->i_name == I_LOAD,
6384                    "missing ilix for I_CMPXCHG, I_ATOMICRMW, or I_FENCE");
6385       if (!can_move_load_up_over_fence(instr))
6386         return NULL;
6387       if (instr->i_name == I_LOAD)
6388         break;
6389       goto check_conflict;
6390     case I_STORE:
6391       if (instr->ilix == 0)
6392         return NULL;
6393       if (IL_TYPE(ILI_OPC(instr->ilix)) != ILTY_STORE)
6394         return NULL;
6395       /* must use ili_opnd() call to skip by CSExx, otherwise
6396        * may not get latest store to the load location.
6397        */
6398       if (ILI_OPND(ilix, 1) == ili_opnd(instr->ilix, 2)) {
6399         /* Maybe revisited to add conversion op */
6400         if (match_types(instr->operands->ll_type, llt) != MATCH_OK)
6401           return NULL;
6402         if (!same_op(instr->operands->next, load_op))
6403           return NULL;
6404         if (instr == del_store_instr)
6405           instr->flags = (LL_InstrListFlags)del_store_flags;
6406         return gen_copy_op(instr->operands);
6407       }
6408     check_conflict:
6409       c = enhanced_conflict(ld_nme, ILI_OPND(instr->ilix, 3));
6410       if (c == SAME || (flg.depchk && c != NOCONFLICT))
6411         return NULL;
6412       break;
6413     case I_INVOKE:
6414     case I_CALL:
6415       if (!(instr->flags & FAST_CALL))
6416         return NULL;
6417       break;
6418     case I_NONE:
6419     case I_BR:
6420     case I_INDBR:
6421       if (!ENABLE_ENHANCED_CSE_OPT)
6422         return NULL;
6423       break;
6424     default:
6425       break;
6426     }
6427   }
6428 
6429   return NULL;
6430 }
6431 
6432 /**
6433    \brief return new operand of type OT_TMP as result of loading \p load_op
6434 
6435    If \p load_op has a next, make sure the next pointer is dealt with properly
6436    \e BEFORE the call to make_load().
6437 
6438    \p flags is the instruction flags to set. Should usually be
6439    ldst_instr_flags_from_dtype() for natural alignment.
6440  */
6441 static OPERAND *
make_load(int ilix,OPERAND * load_op,LL_Type * rslt_type,MSZ msz,unsigned flags)6442 make_load(int ilix, OPERAND *load_op, LL_Type *rslt_type, MSZ msz,
6443           unsigned flags)
6444 {
6445   OPERAND *operand, *fptrs_op, *cse_op;
6446   TMPS *new_tmps;
6447   LL_Type *load_type;
6448   int array_var, array_dtype, dtype;
6449   INSTR_LIST *Curr_Instr;
6450 
6451   assert(((int)msz) != -1, "make_load():adding a load because of a matchmem ?",
6452          0, ERR_Fatal);
6453 
6454   cse_op = NULL;
6455   if (ENABLE_CSE_OPT && !is_omp_atomic_ld(ilix)) {
6456     operand = find_load_cse(ilix, load_op, rslt_type);
6457     if (operand != NULL) {
6458       const int bits = ll_type_int_bits(operand->ll_type);
6459       if ((bits > 0) && (bits < 32)) {
6460         LL_Type *ll_tmp;
6461 
6462         switch (msz) {
6463         case MSZ_SBYTE:
6464         case MSZ_SHWORD:
6465           ll_tmp = operand->ll_type;
6466           operand->flags |= OPF_SEXT;
6467           operand->ll_type = ll_tmp;
6468           break;
6469         case MSZ_BYTE:
6470         case MSZ_UHWORD:
6471           ll_tmp = make_lltype_from_dtype(DT_UINT);
6472           operand->flags |= OPF_ZEXT;
6473           operand = convert_int_size(0, operand, ll_tmp);
6474         default:
6475           break;
6476         }
6477       }
6478       cse_op = operand;
6479       return cse_op;
6480     }
6481   }
6482   if (load_op->ot_type == OT_VAR && ll_type_is_pointer_to_function(rslt_type)) {
6483     load_type = LLTYPE(load_op->val.sptr);
6484   } else {
6485     load_type = load_op->ll_type;
6486   }
6487 
6488   DBGTRACEIN2(" ilix %d, load op: %p", ilix, load_op)
6489   DBGDUMPLLTYPE("result type ", rslt_type)
6490 
6491   assert(load_type->data_type == LL_PTR, "make_load(): op not ptr type",
6492          load_type->data_type, ERR_Fatal);
6493   assert(match_types(load_type->sub_types[0], rslt_type) == MATCH_OK,
6494          "make_load(): types don't match", 0, ERR_Fatal);
6495   new_tmps = make_tmps();
6496   Curr_Instr = gen_instr(I_LOAD, new_tmps, rslt_type, load_op);
6497   if (rw_nodepcheck) {
6498     flags |= LDST_HAS_METADATA;
6499     Curr_Instr->misc_metadata = cons_no_depchk_metadata();
6500   }
6501   Curr_Instr->flags = (LL_InstrListFlags)flags;
6502   load_op->next = NULL;
6503   ad_instr(ilix, Curr_Instr);
6504   /* make the new operand to be the temp */
6505   operand = make_tmp_op(rslt_type, new_tmps);
6506   consLoadDebug(operand, load_op, rslt_type);
6507   /* Need to make sure the char type is unsigned */
6508   if (ll_type_int_bits(operand->ll_type) &&
6509       (ll_type_int_bits(operand->ll_type) < 16)) {
6510     switch (msz) {
6511     case MSZ_UBYTE:
6512     case MSZ_UHWORD:
6513       operand->flags |= OPF_ZEXT;
6514       break;
6515     default:
6516       break;
6517     }
6518   }
6519   if (ll_type_int_bits(operand->ll_type) &&
6520       (ll_type_int_bits(operand->ll_type) < 32)) {
6521     switch (msz) {
6522     case MSZ_BYTE:
6523     case MSZ_UHWORD: {
6524       LL_Type *ll_tmp = make_lltype_from_dtype(DT_UINT);
6525       operand->flags |= OPF_ZEXT;
6526       operand = convert_int_size(0, operand, ll_tmp);
6527     } break;
6528     default:
6529       break;
6530     }
6531   }
6532 
6533   DBGTRACEOUT1(" returns operand %p", operand);
6534   return cse_op ? cse_op : operand;
6535 }
6536 
6537 SPTR
find_pointer_to_function(int ilix)6538 find_pointer_to_function(int ilix)
6539 {
6540   int addr, addr_acon_ptr;
6541   SPTR sptr = SPTR_NULL;
6542 
6543   addr = ILI_OPND(ilix, 1);
6544   while (ILI_OPC(addr) == IL_LDA) {
6545     if (ILI_OPC(ILI_OPND(addr, 1)) == IL_ACON) {
6546       addr_acon_ptr = ILI_OPND(addr, 1);
6547       sptr = ILI_SymOPND(addr_acon_ptr, 1);
6548       if (CONVAL1G(sptr)) {
6549         sptr = SymConval1(sptr);
6550       }
6551     } else if (ILI_OPC(ILI_OPND(addr, 1)) == IL_AADD) {
6552       if (ILI_OPC(ILI_OPND(ILI_OPND(addr, 1), 1)) == IL_ACON) {
6553         addr_acon_ptr = ILI_OPND(ILI_OPND(addr, 1), 1);
6554         sptr = SymConval1(ILI_SymOPND(addr_acon_ptr, 1));
6555       }
6556       addr = ILI_OPND(addr, 1);
6557     }
6558     addr = ILI_OPND(addr, 1);
6559   }
6560 
6561   return sptr;
6562 }
6563 
6564 static SPTR
get_call_sptr(int ilix)6565 get_call_sptr(int ilix)
6566 {
6567   SPTR sptr;
6568   int addr;
6569   SPTR addr_acon_ptr;
6570   ILI_OP opc = ILI_OPC(ilix);
6571 
6572   DBGTRACEIN2(" called with ilix %d (opc=%s)", ilix, IL_NAME(opc))
6573 
6574   switch (opc) {
6575   case IL_JSR:
6576   case IL_GJSR:
6577   case IL_QJSR:
6578     sptr = ILI_SymOPND(ilix, 1);
6579     break;
6580   case IL_GJSRA:
6581   case IL_JSRA:
6582     addr = ILI_OPND(ilix, 1);
6583     if (ILI_OPC(addr) == IL_LDA) {
6584       sptr = find_pointer_to_function(ilix);
6585     } else if (ILI_OPC(addr) == IL_ACON) {
6586       addr_acon_ptr = ILI_SymOPND(addr, 1);
6587       if (!CONVAL1G(addr_acon_ptr))
6588         sptr = addr_acon_ptr;
6589       else
6590         sptr = SymConval1(addr_acon_ptr);
6591     } else if (ILI_OPC(addr) == IL_DFRAR) {
6592       const int addr_acon_ptr = ILI_OPND(addr, 1);
6593       if (ILI_OPC(addr_acon_ptr) == IL_JSR)
6594         /* this sptr is the called function, but the DFRAR is
6595          * returning a function pointer from that sptr, and that
6596          * returned indirect function sptr is unknown.
6597          */
6598         /* sptr = ILI_OPND(addr_acon_ptr,1); */
6599         sptr = SPTR_NULL;
6600       else if (ILI_OPC(addr_acon_ptr) == IL_JSRA)
6601         return get_call_sptr(addr_acon_ptr);
6602       else
6603         assert(false, "get_call_sptr(): indirect call via DFRAR not JSR/JSRA",
6604                ILI_OPC(addr_acon_ptr), ERR_Fatal);
6605     } else {
6606       assert(false, "get_call_sptr(): indirect call not via LDA/ACON",
6607              ILI_OPC(addr), ERR_Fatal);
6608     }
6609     break;
6610   default:
6611     DBGTRACE2("###get_call_sptr unknown opc %d (%s)", opc, IL_NAME(opc))
6612     assert(false, "get_call_sptr(): unknown opc", opc, ERR_Fatal);
6613     break;
6614   }
6615 
6616   DBGTRACEOUT1(" returns %d", sptr)
6617 
6618   return sptr;
6619 } /* get_call_sptr */
6620 
6621 static void
update_return_type_for_ccfunc(int ilix,ILI_OP opc)6622 update_return_type_for_ccfunc(int ilix, ILI_OP opc)
6623 {
6624   int sptr = ILI_OPND(ilix, 1);
6625   DTYPE dtype = DTYPEG(sptr);
6626   DTYPE new_dtype;
6627   switch (opc) {
6628   case IL_DFRAR:
6629     new_dtype = cg_get_type(3, DTY(dtype), DT_CPTR);
6630     break;
6631 #ifdef IL_DFRSPX87
6632   case IL_DFRSPX87:
6633 #endif
6634   case IL_DFRSP:
6635     new_dtype = cg_get_type(3, DTY(dtype), DT_FLOAT);
6636     break;
6637 #ifdef IL_DFRDPX87
6638   case IL_DFRDPX87:
6639 #endif
6640   case IL_DFRDP:
6641     new_dtype = cg_get_type(3, DTY(dtype), DT_DBLE);
6642     break;
6643   case IL_DFRIR:
6644     new_dtype = cg_get_type(3, DTY(dtype), DT_INT);
6645     break;
6646   case IL_DFRKR:
6647     new_dtype = cg_get_type(3, DTY(dtype), DT_INT8);
6648     break;
6649   case IL_DFRCS:
6650     new_dtype = cg_get_type(3, DTY(dtype), DT_CMPLX);
6651     break;
6652 #ifdef LONG_DOUBLE_FLOAT128
6653   case IL_FLOAT128RESULT:
6654     new_dtype = cg_get_type(3, DTY(dtype), DT_FLOAT128);
6655     break;
6656 #endif
6657   default:
6658     assert(false,
6659            "update_return_type_for_ccfunc(): return type not handled for opc ",
6660            opc, ERR_Fatal);
6661   }
6662   DTySetParamList(new_dtype, DTyParamList(dtype));
6663   DTYPEP(sptr, new_dtype);
6664 }
6665 
6666 /**
6667    \brief Create a function type from a return type and an argument list
6668  */
6669 static LL_Type *
make_function_type_from_args(LL_Type * return_type,OPERAND * first_arg_op,bool is_varargs)6670 make_function_type_from_args(LL_Type *return_type, OPERAND *first_arg_op,
6671                              bool is_varargs)
6672 {
6673   unsigned nargs = 0;
6674   LL_Type **types;
6675   OPERAND *op;
6676   unsigned i = 0;
6677   LL_Type *func_type;
6678 
6679   /* Count the arguments. */
6680   for (op = first_arg_op; op; op = op->next)
6681     nargs++;
6682 
6683   /* [0] = return type, [1..] = args. */
6684   types = (LL_Type **)calloc(1 + nargs, sizeof(LL_Type *));
6685   types[i++] = return_type;
6686   for (op = first_arg_op; op; op = op->next)
6687     types[i++] = op->ll_type;
6688 
6689   func_type =
6690       ll_create_function_type(cpu_llvm_module, types, nargs, is_varargs);
6691   free(types);
6692   return func_type;
6693 }
6694 
6695 /*
6696  * Generate a single operand for a function call.
6697  *
6698  * abi_arg is the index into abi->args. 0 is the return value, 1 is the first
6699  * argument, ...
6700  * arg_ili is the ILI instruction setting up the argument, IL_ARG*, IL_GARG, or
6701  * IL_DA*
6702  */
6703 static OPERAND *
gen_arg_operand(LL_ABI_Info * abi,unsigned abi_arg,int arg_ili)6704 gen_arg_operand(LL_ABI_Info *abi, unsigned abi_arg, int arg_ili)
6705 {
6706   int val_res;
6707   const ILI_OP arg_opc = ILI_OPC(arg_ili);
6708   const int value_ili = ILI_OPND(arg_ili, 1);
6709   LL_ABI_ArgInfo arg_info, *arg;
6710   LL_Type *arg_type;
6711   DTYPE dtype = DT_NONE;
6712   /* Is the ILI value argument a pointer to the value? */
6713   bool indirect_ili_value = false;
6714   bool need_load = false;
6715   unsigned flags = 0;
6716   OPERAND *operand;
6717   bool missing = false;
6718 
6719   /* Determine the dtype of the argument, or at least an approximation. Also
6720    * compute whether indirect_ili_value should be set. */
6721   switch (arg_opc) {
6722   case IL_GARGRET:
6723     assert(abi_arg == 0, "GARGRET out of place", arg_ili, ERR_Fatal);
6724     /* GARGRET value next-lnk dtype */
6725     dtype = ILI_DTyOPND(arg_ili, 3);
6726     /* The GARGRET value is a pointer to where the return value should be
6727      * stored. */
6728     indirect_ili_value = true;
6729     break;
6730 
6731   case IL_GARG:
6732     /* GARG value next-lnk dtype */
6733     dtype = ILI_DTyOPND(arg_ili, 3);
6734 
6735     /* The ili argument may be a pointer to the value to be passed. This
6736      * happens when passing structs by value, for example.  Assume
6737      * that pointers are never passed indirectly.  This also considers
6738      * LDSCMPLX and LDDCMPLX (complex value loads).
6739      */
6740     val_res = IL_RES(ILI_OPC(value_ili));
6741     if ((DTY(dtype) != TY_PTR) && ILIA_ISAR(val_res))
6742       indirect_ili_value = true;
6743     break;
6744 
6745   default:
6746     /* Without a GARG, we'll assume that any pointers (IL_ARGAR) are passed
6747      * by value. The indirect_ili_value stays false, and we don't support
6748      * passing structs by value. */
6749     dtype = get_dtype_from_arg_opc(arg_opc);
6750   }
6751 
6752   /* Make sure arg points to relevant lowering information, generate it if
6753    * required. */
6754   if (abi_arg <= abi->nargs) {
6755     /* This is one of the known arguments. */
6756     arg = &abi->arg[abi_arg];
6757   } else {
6758     missing = true;
6759     /* This is a trailing argument to a varargs function, or we don't have a
6760      * prototype. */
6761     memset(&arg_info, 0, sizeof(arg_info));
6762     arg = &arg_info;
6763     assert(dtype, "Can't infer argument dtype from ILI", arg_ili, ERR_Fatal);
6764     if (abi->is_fortran && !abi->is_iso_c && indirect_ili_value) {
6765       arg->kind = LL_ARG_INDIRECT;
6766       ll_abi_classify_arg_dtype(abi, arg, DT_ADDR);
6767       ll_abi_complete_arg_info(abi, arg, DT_ADDR);
6768     } else
6769     {
6770       ll_abi_classify_arg_dtype(abi, arg, dtype);
6771       ll_abi_complete_arg_info(abi, arg, dtype);
6772     }
6773   }
6774 
6775 /* For fortan we want to follow the ILI as close as possible.
6776  * The exception is if GARG ILI field '4' is set (the arg is byval).
6777  * Return early in the case of fortran.
6778  * TODO: Allow code to pass straight through this routine and not return
6779  * early. Just set 'need_load' properly.
6780  */
6781   arg_type = make_lltype_from_abi_arg(arg);
6782   if (arg->kind != LL_ARG_BYVAL && indirect_ili_value &&
6783       (ILI_OPND(arg_ili, 4) || arg->kind == LL_ARG_COERCE)) {
6784     operand = gen_llvm_expr(value_ili, make_ptr_lltype(arg_type));
6785     return gen_load(operand, arg_type, ldst_instr_flags_from_dtype(dtype));
6786   }
6787     operand = gen_llvm_expr(value_ili, arg_type);
6788   if (arg->kind == LL_ARG_BYVAL && !missing)
6789     operand->flags |= OPF_SRARG_TYPE;
6790   return operand;
6791 
6792   arg_type = make_lltype_from_abi_arg(arg);
6793 
6794   switch (arg->kind) {
6795   case LL_ARG_DIRECT:
6796     need_load = indirect_ili_value;
6797     break;
6798 
6799   case LL_ARG_ZEROEXT:
6800     need_load = indirect_ili_value;
6801     /* flags |= OP_ZEROEXT_FLAG */
6802     break;
6803 
6804   case LL_ARG_SIGNEXT:
6805     need_load = indirect_ili_value;
6806     /* flags |= OP_SIGNEXT_FLAG */
6807     break;
6808 
6809   case LL_ARG_COERCE:
6810     /* It is possible to coerce with a bitcast, but we only implement
6811      * coercion via memory for now.
6812      *
6813      * Complex values are treated as coercion types, due to different abi
6814      * representations.
6815      *
6816      * Note that bitcast coercion works differently on little-endian and
6817      * big-endian architectures. The coercion cast always works as if the
6818      * value was stored with the old type and loaded with the new type.
6819      */
6820     assert(indirect_ili_value, "Can only coerce indirect args", arg_ili,
6821            ERR_Fatal);
6822     need_load = true;
6823     break;
6824 
6825   case LL_ARG_INDIRECT:
6826     assert(indirect_ili_value, "Indirect arg required", arg_ili, ERR_Fatal);
6827     /* Tag an 'sret' attribute on an indirect return value. */
6828     if (abi_arg == 0)
6829       flags |= OPF_SRET_TYPE;
6830     break;
6831 
6832   case LL_ARG_BYVAL:
6833     assert(indirect_ili_value, "Indirect arg required for byval", arg_ili,
6834            ERR_Fatal);
6835     flags |= OPF_SRARG_TYPE;
6836     break;
6837 
6838   default:
6839     interr("Unknown ABI argument kind", arg->kind, ERR_Fatal);
6840   }
6841 
6842   if (need_load) {
6843     LL_Type *ptr_type = make_ptr_lltype(arg_type);
6844     OPERAND *ptr = gen_llvm_expr(value_ili, ptr_type);
6845     operand = gen_load(ptr, arg_type, ldst_instr_flags_from_dtype(dtype));
6846   } else {
6847       operand = gen_llvm_expr(value_ili, arg_type);
6848   }
6849 
6850   /* Set sret, byval, sign/zeroext flags. */
6851   operand->flags |= flags;
6852   return operand;
6853 }
6854 
6855 /* Get the next argument ILI from an IL_ARG* or IL_DA* ILI. The list will be
6856  * terminated by ILI_NULL. */
6857 static int
get_next_arg(int arg_ili)6858 get_next_arg(int arg_ili)
6859 {
6860   switch (ILI_OPC(arg_ili)) {
6861   case IL_ARGAR:
6862   case IL_ARGDP:
6863   case IL_ARGIR:
6864   case IL_ARGKR:
6865   case IL_ARGSP:
6866   case IL_GARG:
6867   case IL_GARGRET:
6868 #ifdef LONG_DOUBLE_FLOAT128
6869   case IL_FLOAT128ARG:
6870 #endif
6871     return ILI_OPND(arg_ili, 2);
6872 
6873   case IL_DAAR:
6874   case IL_DADP:
6875   case IL_DAIR:
6876   case IL_DAKR:
6877   case IL_DASP:
6878     return ILI_OPND(arg_ili, 3);
6879 
6880   default:
6881     interr("Unknown IL_ARG opcode", arg_ili, ERR_Fatal);
6882     return IL_NULL;
6883   }
6884 }
6885 
6886 /*
6887  * Generate linked list of operands for a call.
6888  *
6889  * The returned operand list represent the function arguments. The operand
6890  * representing the callee is not included.
6891  */
6892 static OPERAND *
gen_arg_operand_list(LL_ABI_Info * abi,int arg_ili)6893 gen_arg_operand_list(LL_ABI_Info *abi, int arg_ili)
6894 {
6895   bool fastcall;
6896   unsigned abi_arg, max_abi_arg = ~0u;
6897   OPERAND *first_arg_op = NULL, *arg_op = NULL;
6898 
6899   if (LL_ABI_HAS_SRET(abi)) {
6900     /* ABI requires a hidden argument to return a struct. We require ILI to
6901      * contain a GARGRET instruction in this case. */
6902     /* GARGRET value next-lnk dtype */
6903     first_arg_op = arg_op = gen_arg_operand(abi, 0, arg_ili);
6904     arg_ili = get_next_arg(arg_ili);
6905   } else if (ILI_OPC(arg_ili) == IL_GARGRET) {
6906     /* It is up to gen_call_expr() to save the function return value in
6907      * this case. We'll just ignore the GARGRET. */
6908     arg_ili = get_next_arg(arg_ili);
6909   }
6910 
6911   /* If we know the exact prototype of the callee and it isn't a varargs
6912    * function, don't create more arguments than the function accepts.
6913    * Old-style C allows functions to be called with extra arguments, but LLVM
6914    * does not. */
6915   if (!abi->missing_prototype && !abi->is_varargs)
6916     max_abi_arg = abi->nargs;
6917 
6918   /* Generate operands for all the provided call arguments. */
6919   for (abi_arg = 1; abi_arg <= max_abi_arg && ILI_OPC(arg_ili) != IL_NULL;
6920        arg_ili = get_next_arg(arg_ili), abi_arg++) {
6921     OPERAND *op = gen_arg_operand(abi, abi_arg, arg_ili);
6922     if (arg_op == NULL)
6923       first_arg_op = op;
6924     else
6925       arg_op->next = op;
6926     arg_op = op;
6927   }
6928 
6929   return first_arg_op;
6930 }
6931 
6932 /**
6933    \brief Generate LLVM instructions for a call.
6934    \param ilix      is the IL_JSR or IL_JSRA instruction representing the call
6935    \param ret_dtype is the dtype for the return value, or 0 if return unused
6936    \param call_instr is either a newly allocated I_CALL or NULL; if NULL, a new
6937    instruction will be allocated
6938    \param call_sptr is the sptr of the called function or function pointer, if
6939    known
6940    \returns an OPERAND representing the value returned by the call
6941 */
6942 static OPERAND *
gen_call_expr(int ilix,DTYPE ret_dtype,INSTR_LIST * call_instr,int call_sptr)6943 gen_call_expr(int ilix, DTYPE ret_dtype, INSTR_LIST *call_instr, int call_sptr)
6944 {
6945   int first_arg_ili;
6946   LL_ABI_Info *abi;
6947   LL_Type *return_type;
6948   OPERAND *first_arg_op;
6949   OPERAND *callee_op = NULL;
6950   LL_Type *func_type = NULL;
6951   OPERAND *result_op = NULL;
6952   bool intrinsic_modified = false;
6953   int throw_label = ili_throw_label(ilix);
6954 
6955   if (call_instr == NULL)
6956     call_instr = make_instr((throw_label > 0) ? I_INVOKE : I_CALL);
6957 
6958   /* Prefer IL_GJSR / IL_GJSRA when available. */
6959   if (ILI_ALT(ilix))
6960     ilix = ILI_ALT(ilix);
6961 
6962   /* GJSR sym args, or GJSRA addr args flags. */
6963   first_arg_ili = ILI_OPND(ilix, 2);
6964 
6965   /* Get an ABI descriptor which at least knows about the function return type.
6966      We may have more arguments than the descriptor knows about if this is a
6967      varargs call, or if the prototype is missing. */
6968   abi = ll_abi_from_call_site(cpu_llvm_module, ilix, ret_dtype);
6969 
6970   first_arg_op = gen_arg_operand_list(abi, first_arg_ili);
6971 
6972   /* Set the calling convention, read by write_I_CALL. */
6973   call_instr->flags |= (LL_InstrListFlags)(abi->call_conv << CALLCONV_SHIFT);
6974 
6975   if (abi->fast_math)
6976     call_instr->flags |= FAST_MATH_FLAG;
6977 
6978   /* Functions without a prototype are represented in LLVM IR as f(...) varargs
6979      functions.  Do what clang does and bitcast to a function pointer which is
6980      varargs, but with all the actual argument types filled in. */
6981   if (abi->missing_prototype) {
6982 #if defined(TARGET_LLVM_X8664)
6983     /* Fortran argument lists of dtype currently not precsise. So when
6984      * we make 256/512-bit math intrinsic calls, which are not really covered
6985      * by the ABI, LLVM can get confused with stack alignment. This
6986      * check is just a temporary workaround (which is to not generate
6987      * the bitcast into a varargs, but just use the known argument.)
6988      */
6989     const DTYPE ddsize = DTYPEG(call_sptr);
6990     int dsize = (ddsize == DT_NONE) ? 0 : zsize_of(ddsize);
6991     if ((dsize == 32 || dsize == 64) &&
6992         is_256_or_512_bit_math_intrinsic(call_sptr) && !XBIT(183, 0x4000))
6993       func_type = make_function_type_from_args(ll_abi_return_type(abi),
6994                                                first_arg_op, 0);
6995     else
6996 #endif
6997       func_type = make_function_type_from_args(
6998           ll_abi_return_type(abi), first_arg_op, abi->call_as_varargs);
6999   }
7000 
7001   /* Now figure out the callee itself. */
7002   switch (ILI_OPC(ilix)) {
7003   case IL_JSR:
7004   case IL_GJSR:
7005   case IL_QJSR: {
7006     /* Direct call: JSR sym arg-lnk */
7007     SPTR callee_sptr = ILI_SymOPND(ilix, 1);
7008     callee_op = make_var_op(callee_sptr);
7009     /* Create an alternative function type from arguments for a function that
7010        is defined in the current source file and does not have an alternative
7011        function type defined yet.  Don't perform this conversion for varargs
7012        functions, because for those arguments the invocation doesn't match the
7013        definition. */
7014     if (!func_type && !abi->is_varargs) {
7015       func_type = make_function_type_from_args(
7016           ll_abi_return_type(abi), first_arg_op, abi->call_as_varargs);
7017       if(intrinsic_modified) {
7018         /* For llvm intrinsics whose arg op has been modified, correct
7019            callee_op to match and to prevent function bitcast.*/
7020         callee_op->ll_type = make_ptr_lltype(func_type);
7021       }
7022     }
7023     /* Cast function points that are missing a prototype. */
7024     if (func_type && func_type != callee_op->ll_type->sub_types[0]) {
7025       callee_op = make_bitcast(callee_op, make_ptr_lltype(func_type));
7026     }
7027     break;
7028   }
7029   case IL_JSRA:
7030   case IL_GJSRA: {
7031     /* Indirect call: JSRA addr arg-lnk flags */
7032     int addr_ili = ILI_OPND(ilix, 1);
7033     if (!func_type) {
7034       func_type = abi->is_varargs ? ll_abi_function_type(abi) :
7035         make_function_type_from_args(
7036             ll_abi_return_type(abi), first_arg_op, abi->call_as_varargs);
7037     }
7038     /* Now that we know the desired type we can create the callee address
7039        expression. */
7040     callee_op = gen_llvm_expr(addr_ili, make_ptr_lltype(func_type));
7041     call_instr->flags |= CALL_FUNC_PTR_FLAG;
7042     break;
7043   }
7044   default:
7045     interr("Unhandled call instruction", ilix, ERR_Fatal);
7046     break;
7047   }
7048 
7049   callee_op->next = first_arg_op;
7050   call_instr->operands = callee_op;
7051 
7052   if (throw_label == -1) {
7053     /* The function might throw, but the exception should just propagate out to
7054        the calling function.  Nothing to do. */
7055   } else if (throw_label == 0) {
7056     if (callee_op->string &&
7057         (!strcmp("@__cxa_call_unexpected", callee_op->string))) {
7058       /* Ignore __cxa_call_unexpected as nounwind, due to bugs in PowerPC
7059          backend. */
7060     } else {
7061       /* The called function should never throw. */
7062       call_instr->flags |= NOUNWIND_CALL_FLAG;
7063     }
7064   } else {
7065     /* The function might throw, and if it does, control should jump to the
7066        given label. The normal return label and the exception label are added
7067        to the end of the operand list. */
7068     OPERAND *label_op;
7069     OPERAND *op = call_instr->operands;
7070     while (op->next) {
7071       op = op->next;
7072     }
7073     label_op = make_label_op(getlab());
7074     op->next = label_op;
7075     op = label_op;
7076     label_op = make_operand();
7077     label_op->ot_type = OT_LABEL;
7078     label_op->val.cc = throw_label;
7079     op->next = label_op;
7080   }
7081 
7082   return_type = ll_abi_return_type(abi);
7083   if (return_type->data_type == LL_VOID) {
7084     call_instr->ll_type = make_void_lltype();
7085     /* This function may return a struct via a hidden argument.  See if ILI is
7086        expecting the function to return the hidden argument pointer, like the
7087        x86-64 ABI requires.  LLVM handles this in the code generator, so we
7088        have produced a void return type for the LLVM IR.  Just return the
7089        hidden argument directly for the ret_dtype to consume. */
7090     if (LL_ABI_HAS_SRET(abi) &&
7091         (DTY(ret_dtype) == TY_STRUCT || DTY(ret_dtype) == TY_UNION)) {
7092       result_op = gen_copy_op(first_arg_op);
7093       result_op->flags = 0;
7094     }
7095   } else {
7096     /* When we stop wrapping alt_type, this can simply be return_type. */
7097     call_instr->ll_type = make_lltype_from_abi_arg(&abi->arg[0]);
7098     call_instr->tmps = make_tmps();
7099 
7100     /* If ret_dtype is not set, no return value is expected. */
7101     if (ret_dtype) {
7102       result_op = make_tmp_op(call_instr->ll_type, call_instr->tmps);
7103     }
7104   }
7105 
7106   ad_instr(ilix, call_instr);
7107 
7108   /* Check if the expander created a GARGRET call but the ABI returns in
7109      registers.  In that case, coerce the returned value by storing it to the
7110      GARGRET value pointer. */
7111   if (ILI_OPC(first_arg_ili) == IL_GARGRET && !LL_ABI_HAS_SRET(abi)) {
7112     int addr_ili = ILI_OPND(first_arg_ili, 1);
7113     DTYPE return_dtype = ILI_DTyOPND(first_arg_ili, 3);
7114     OPERAND *addr;
7115     assert(ILIA_ISAR(IL_RES(ILI_OPC(addr_ili))),
7116            "GARGRET must be indirect value", ilix, ERR_Fatal);
7117     addr = gen_llvm_expr(addr_ili, make_ptr_lltype(call_instr->ll_type));
7118     make_store(make_tmp_op(call_instr->ll_type, call_instr->tmps), addr,
7119                ldst_instr_flags_from_dtype(return_dtype));
7120 
7121     /* Does ILI expect the function call to return the hidden argument, like an
7122        x86-64 sret function call? */
7123     if (DTY(ret_dtype) == TY_STRUCT || DTY(ret_dtype) == TY_UNION) {
7124       result_op = gen_copy_op(addr);
7125     }
7126   }
7127 
7128   return result_op;
7129 } /* gen_call_expr */
7130 
7131 static bool
is_256_or_512_bit_math_intrinsic(int sptr)7132 is_256_or_512_bit_math_intrinsic(int sptr)
7133 {
7134   int new_num, new_type;
7135   const char *sptrName;
7136   bool is_g_name = false; /* the first cut at generic names */
7137   bool is_newest_name = false;
7138 
7139   if (sptr == 0 || !CCSYMG(sptr))
7140     return false;
7141   sptrName = SYMNAME(sptr);
7142   if (sptrName == NULL)
7143     return false;
7144 
7145   /* test for generic name that matches "__gv<s|d|c|z>_<math-func>_<2|4|8>" */
7146   if (!strncmp(sptrName, "__gv", 4)) {
7147     is_g_name = true;
7148     new_type = sptrName[4];
7149   }
7150 
7151   /* test for newest name that matches
7152    * "__<frp><s|d|c|z>_<math-func>_<2|4|8|16>
7153    */
7154   else if (!strncmp(sptrName, "__f", 4) || !strncmp(sptrName, "__p", 4) ||
7155            !strncmp(sptrName, "__r", 4)) {
7156     new_type = sptrName[3];
7157     switch (new_type) {
7158     case 's':
7159     case 'd':
7160     case 'c':
7161     case 'z':
7162       is_newest_name = true;
7163       break;
7164     default:
7165       break;
7166     }
7167   }
7168 
7169   /* names match: generic name or  "__<f|r>v<s|d>_<math-func>_<vex|fma4>_256" */
7170   if (is_newest_name || is_g_name || !strncmp(sptrName, "__fv", 4) ||
7171       !strncmp(sptrName, "__rv", 4))
7172     sptrName += 4;
7173   else
7174     return false;
7175 
7176   if (is_newest_name)
7177     sptrName++;
7178   else if ((*sptrName) &&
7179            ((*sptrName == 's') || (*sptrName == 'd') || (*sptrName == 'c') ||
7180             (*sptrName == 'z')) &&
7181            (sptrName[1] == '_'))
7182     sptrName += 2;
7183   else
7184     return false;
7185 
7186   if (!(*sptrName))
7187     return false;
7188   if ((!strncmp(sptrName, "sin", 3)) || (!strncmp(sptrName, "cos", 3)) ||
7189       (!strncmp(sptrName, "tan", 3)) || (!strncmp(sptrName, "pow", 3)) ||
7190       (!strncmp(sptrName, "log", 3)) || (!strncmp(sptrName, "exp", 3)) ||
7191       (!strncmp(sptrName, "mod", 3)) || (!strncmp(sptrName, "div", 3)))
7192     sptrName += 3;
7193   else if ((!strncmp(sptrName, "sinh", 4)) || (!strncmp(sptrName, "cosh", 4)))
7194     sptrName += 4;
7195   else if (!strncmp(sptrName, "log10", 5))
7196     sptrName += 5;
7197   else if (!strncmp(sptrName, "sincos", 6))
7198     sptrName += 6;
7199   else
7200     return false;
7201 
7202   if (is_newest_name) {
7203     sptrName++;
7204     new_num = atoi(sptrName);
7205     switch (new_type) {
7206     case 's':
7207       if (new_num == 8 || new_num == 16)
7208         return true;
7209       break;
7210     case 'd':
7211       if (new_num == 4 || new_num == 8)
7212         return true;
7213       break;
7214     case 'c':
7215       if (new_num == 4 || new_num == 8)
7216         return true;
7217       break;
7218     case 'z':
7219       if (new_num == 2 || new_num == 4)
7220         return true;
7221       break;
7222     default:
7223       return false;
7224     }
7225     return false;
7226   } else if (is_g_name) {
7227     new_num = atoi(sptrName);
7228     if (isdigit(sptrName[0]))
7229       switch (new_type) {
7230       case 's':
7231         if (new_num == 8)
7232           return true;
7233         break;
7234       case 'd':
7235         if (new_num == 4)
7236           return true;
7237         break;
7238       default:
7239         return false;
7240       }
7241     return false;
7242   } else if (*sptrName == '_') {
7243     sptrName++;
7244   } else {
7245     return false;
7246   }
7247 
7248   if (!(*sptrName))
7249     return false;
7250   if (!strncmp(sptrName, "vex_", 4))
7251     sptrName += 4;
7252   else if (!strncmp(sptrName, "fma4_", 5))
7253     sptrName += 5;
7254   else
7255     return false;
7256 
7257   return (!strcmp(sptrName, "256")); /* strcmp: check for trailing garbage */
7258 }
7259 
7260 static bool
have_masked_intrinsic(int ilix)7261 have_masked_intrinsic(int ilix)
7262 {
7263   ILI_OP vopc;
7264   DTYPE vdtype;
7265   int mask;
7266 
7267   vopc = ILI_OPC(ilix);
7268   vdtype = ili_get_vect_dtype(ilix);
7269   if (!vdtype)
7270     return false;
7271 
7272   switch (vopc) {
7273   case IL_VDIV:
7274   case IL_VMOD:
7275   case IL_VSQRT:
7276   case IL_VSIN:
7277   case IL_VCOS:
7278   case IL_VTAN:
7279   case IL_VSINCOS:
7280   case IL_VASIN:
7281   case IL_VACOS:
7282   case IL_VATAN:
7283   case IL_VATAN2:
7284   case IL_VSINH:
7285   case IL_VCOSH:
7286   case IL_VTANH:
7287   case IL_VEXP:
7288   case IL_VLOG:
7289   case IL_VLOG10:
7290   case IL_VPOW:
7291   case IL_VPOWI:
7292   case IL_VPOWK:
7293   case IL_VPOWIS:
7294   case IL_VPOWKS:
7295   case IL_VFPOWK:
7296   case IL_VFPOWKS:
7297   case IL_VDPOWI:
7298   case IL_VDPOWIS:
7299   case IL_VRSQRT:
7300   case IL_VRCP:
7301   /* case IL_VFLOOR:*/
7302   /* case IL_VCEIL: */
7303   /* case IL_VAINT: */
7304     mask = ILI_OPND(ilix, IL_OPRS(vopc) - 1); /* get potential mask */
7305     if (ILI_OPC(mask) != IL_NULL) {
7306       /* have mask */
7307       return true;
7308     }
7309     break;
7310   default:
7311     break;
7312   }
7313   return false;
7314 }
7315 
7316 static INSTR_LIST *Void_Call_Instr = NULL;
7317 
7318 /* LLVM extractvalue instruction:
7319  * Given an aggregate and index return the value at that index.
7320  *
7321  * <result> = extractvalue <aggregate type> <val>, <idx>{, <idx>}*
7322  */
7323 static OPERAND *
gen_extract_value_ll(OPERAND * aggr,LL_Type * aggr_ty,LL_Type * elt_ty,int idx)7324 gen_extract_value_ll(OPERAND *aggr, LL_Type *aggr_ty, LL_Type *elt_ty, int idx)
7325 {
7326   OPERAND *res = make_tmp_op(elt_ty, make_tmps());
7327   INSTR_LIST *Curr_Instr = gen_instr(I_EXTRACTVAL, res->tmps, aggr_ty, aggr);
7328   aggr->next = make_constval32_op(idx);
7329   ad_instr(0, Curr_Instr);
7330   return res;
7331 }
7332 
7333 /* Like gen_extract_value_ll, but takes DTYPE instead of LL_TYPE argumentss. */
7334 static OPERAND *
gen_extract_value(OPERAND * aggr,DTYPE aggr_dtype,DTYPE elt_dtype,int idx)7335 gen_extract_value(OPERAND *aggr, DTYPE aggr_dtype, DTYPE elt_dtype, int idx)
7336 {
7337   LL_Type *aggr_ty = make_lltype_from_dtype(aggr_dtype);
7338   LL_Type *elt_ty = make_lltype_from_dtype(elt_dtype);
7339   return gen_extract_value_ll(aggr, aggr_ty, elt_ty, idx);
7340 }
7341 
7342 static OPERAND *
gen_eval_cmplx_value(int ilix,DTYPE dtype)7343 gen_eval_cmplx_value(int ilix, DTYPE dtype)
7344 {
7345   OPERAND *c1;
7346   INSTR_LIST *Curr_Instr;
7347   LL_Type *cmplx_type = make_lltype_from_dtype(dtype);
7348 
7349   c1 = gen_llvm_expr(ilix, cmplx_type);
7350 
7351   /* should move this to a %temp? */
7352 
7353   return c1;
7354 }
7355 
7356 static OPERAND *
gen_copy_operand(OPERAND * opnd)7357 gen_copy_operand(OPERAND *opnd)
7358 {
7359   OPERAND *curr;
7360   OPERAND *head;
7361 
7362   /* copy operand opnd -> c1 */
7363   head = gen_copy_op(opnd);
7364   curr = head;
7365   while (opnd->next) {
7366     curr->next = gen_copy_op(opnd->next);
7367     opnd = opnd->next;
7368     curr = curr->next;
7369   }
7370 
7371   return head;
7372 }
7373 
7374 /* Math operations for complex values.
7375  * 'itype' should be the I_FADD, I_FSUB, I_xxxx etc.
7376  * 'dtype' should either be DT_CMPLX or DT_DCMPLX.
7377  */
7378 static OPERAND *
gen_cmplx_math(int ilix,DTYPE dtype,LL_InstrName itype)7379 gen_cmplx_math(int ilix, DTYPE dtype, LL_InstrName itype)
7380 {
7381   OPERAND *r1, *r2, *i1, *i2, *rmath, *imath, *res, *c1, *c2, *cse1, *cse2;
7382   LL_Type *cmplx_type, *cmpnt_type;
7383   const DTYPE cmpnt = (dtype == DT_CMPLX) ? DT_FLOAT : DT_DBLE;
7384 
7385   assert(DT_ISCMPLX(dtype), "gen_cmplx_math: Expected DT_CMPLX or DT_DCMPLX",
7386          dtype, ERR_Fatal);
7387 
7388   cmplx_type = make_lltype_from_dtype(dtype);
7389   cmpnt_type = make_lltype_from_dtype(cmpnt);
7390 
7391   /* Obtain the components (real and imaginary) for both operands */
7392 
7393   c1 = gen_eval_cmplx_value(ILI_OPND(ilix, 1), dtype);
7394   c2 = gen_eval_cmplx_value(ILI_OPND(ilix, 2), dtype);
7395   cse1 = gen_copy_operand(c1);
7396   cse2 = gen_copy_operand(c2);
7397 
7398   r1 = gen_extract_value(c1, dtype, cmpnt, 0);
7399   i1 = gen_extract_value(cse1, dtype, cmpnt, 1);
7400 
7401   r2 = gen_extract_value(c2, dtype, cmpnt, 0);
7402   i2 = gen_extract_value(cse2, dtype, cmpnt, 1);
7403 
7404   r1->next = r2;
7405   i1->next = i2;
7406 
7407   rmath = ad_csed_instr(itype, 0, cmpnt_type, r1, InstrListFlagsNull, true);
7408   imath = ad_csed_instr(itype, 0, cmpnt_type, i1, InstrListFlagsNull, true);
7409 
7410   /* Build a temp complex in registers and store the mathed values in that */
7411   res = make_undef_op(cmplx_type);
7412   res = gen_insert_value(res, rmath, 0);
7413   return gen_insert_value(res, imath, 1);
7414 }
7415 
7416 /* Complex multiply:
7417  * (a + bi) * (c + di) ==  (a*c) + (a*di) + (bi*c) + (bi*di)
7418  */
7419 static OPERAND *
gen_cmplx_mul(int ilix,DTYPE dtype)7420 gen_cmplx_mul(int ilix, DTYPE dtype)
7421 {
7422   const DTYPE elt_dt = (dtype == DT_CMPLX) ? DT_FLOAT : DT_DBLE;
7423   LL_Type *cmpnt_type = make_lltype_from_dtype(elt_dt);
7424   OPERAND *a, *bi, *c, *di, *cse1, *cse2;
7425   OPERAND *r1, *r2, *r3, *r4, *imag, *real, *res, *c1, *c2;
7426 
7427   c1 = gen_eval_cmplx_value(ILI_OPND(ilix, 1), dtype);
7428   c2 = gen_eval_cmplx_value(ILI_OPND(ilix, 2), dtype);
7429   cse1 = gen_copy_operand(c1);
7430   cse2 = gen_copy_operand(c2);
7431 
7432   a = gen_extract_value(c1, dtype, elt_dt, 0);
7433   bi = gen_extract_value(cse1, dtype, elt_dt, 1);
7434   c = gen_extract_value(c2, dtype, elt_dt, 0);
7435   di = gen_extract_value(cse2, dtype, elt_dt, 1);
7436 
7437   /* r1 = (a * c) */
7438   a->next = c;
7439   r1 = ad_csed_instr(I_FMUL, 0, cmpnt_type, a, InstrListFlagsNull, true);
7440 
7441   /* r2 = (a * di) */
7442   cse1 = gen_copy_operand(c1);
7443   a = gen_extract_value(cse1, dtype, elt_dt, 0);
7444   a->next = di;
7445   r2 = ad_csed_instr(I_FMUL, 0, cmpnt_type, a, InstrListFlagsNull, true);
7446 
7447   /* r3 = (bi * c) */
7448   bi->next = c;
7449   r3 = ad_csed_instr(I_FMUL, 0, cmpnt_type, bi, InstrListFlagsNull, true);
7450 
7451   /* r4 = (bi * di) */
7452   cse1 = gen_copy_operand(c1);
7453   bi = gen_extract_value(cse1, dtype, elt_dt, 1);
7454   bi->next = di;
7455   r4 = ad_csed_instr(I_FMUL, 0, cmpnt_type, bi, InstrListFlagsNull, true);
7456 
7457   /* Real: r1 - r4 */
7458   r1->next = r4;
7459   real = ad_csed_instr(I_FSUB, 0, cmpnt_type, r1, InstrListFlagsNull, true);
7460 
7461   /* Imag: r2 + r3 */
7462   r2->next = r3;
7463   imag = ad_csed_instr(I_FADD, 0, cmpnt_type, r2, InstrListFlagsNull, true);
7464 
7465   res = make_undef_op(make_lltype_from_dtype(dtype));
7466   res = gen_insert_value(res, real, 0);
7467   return gen_insert_value(res, imag, 1);
7468 }
7469 
7470 static OPERAND *
gen_llvm_atomicrmw_expr(int ilix)7471 gen_llvm_atomicrmw_expr(int ilix)
7472 {
7473   MEMORY_ORDER mo;
7474   OPERAND *result;
7475   ATOMIC_INFO info = atomic_info(ilix);
7476   LL_Type *instr_type = make_type_from_msz((MSZ)info.msz);
7477   /* LLVM instruction atomicrmw has operands in opposite order of ILI
7478    * instruction. */
7479   OPERAND *op1 = gen_llvm_expr(ILI_OPND(ilix, 2), make_ptr_lltype(instr_type));
7480   OPERAND *op2 = gen_llvm_expr(ILI_OPND(ilix, 1), instr_type);
7481   LL_InstrListFlags flags;
7482   op1->next = op2;
7483   flags = ll_instr_flags_for_memory_order_and_scope(ilix);
7484   if (ILI_OPND(ilix, 3) == NME_VOL)
7485     flags |= VOLATILE_FLAG;
7486   flags |= ll_instr_flags_from_aop((ATOMIC_RMW_OP)info.op);
7487   /* Caller will deal with doing zero-extend/sign-extend if necessary. */
7488   result = ad_csed_instr(I_ATOMICRMW, ilix, instr_type, op1, flags, false);
7489   return result;
7490 }
7491 
7492 static void
gen_llvm_fence_instruction(int ilix)7493 gen_llvm_fence_instruction(int ilix)
7494 {
7495   LL_InstrListFlags flags = ll_instr_flags_for_memory_order_and_scope(ilix);
7496   INSTR_LIST *fence = gen_instr(I_FENCE, NULL, NULL, NULL);
7497   fence->flags |= flags;
7498   ad_instr(0, fence);
7499 }
7500 
7501 INLINE static OPERAND *
gen_llvm_cmpxchg(int ilix)7502 gen_llvm_cmpxchg(int ilix)
7503 {
7504   LL_Type *aggr_type;
7505   LL_InstrListFlags flags;
7506   OPERAND *op1, *op2, *op3;
7507   LL_Type *elements[2];
7508   TMPS *tmps;
7509   CMPXCHG_MEMORY_ORDER order;
7510 
7511   /* Construct aggregate type for result of cmpxchg. */
7512   MSZ msz = atomic_info(ilix).msz;
7513   LL_Module *module = cpu_llvm_module;
7514   elements[0] = make_type_from_msz(msz);
7515   elements[1] = ll_create_basic_type(module, LL_I1, 0);
7516   aggr_type = ll_create_anon_struct_type(module, elements, 2,
7517                                          /*is_packed=*/false, LL_AddrSp_Default);
7518 
7519   /* address of location */
7520   op1 = gen_llvm_expr(cmpxchg_loc(ilix), make_ptr_lltype(elements[0]));
7521   /* comparand */
7522   op2 = gen_llvm_expr(ILI_OPND(ilix, 5), elements[0]);
7523   /* new value */
7524   op3 = gen_llvm_expr(ILI_OPND(ilix, 1), elements[0]);
7525   op1->next = op2;
7526   op2->next = op3;
7527 
7528   /* Construct flags for memory order, volatile, and weak. */
7529   order = cmpxchg_memory_order(ilix);
7530   flags = ll_instr_flags_for_memory_order_and_scope(ilix);
7531   flags |=
7532       TO_CMPXCHG_MEMORDER_FAIL(ll_instr_flags_from_memory_order(order.failure));
7533   if (ILI_OPND(ilix, 3) == NME_VOL)
7534     flags |= VOLATILE_FLAG;
7535   if (cmpxchg_is_weak(ilix))
7536     flags |= CMPXCHG_WEAK_FLAG;
7537   return ad_csed_instr(I_CMPXCHG, ilix, aggr_type, op1, flags, false);
7538 }
7539 
7540 static OPERAND *
gen_llvm_cmpxchg_component(int ilix,int idx)7541 gen_llvm_cmpxchg_component(int ilix, int idx)
7542 {
7543   OPERAND *result, *ll_cmpxchg;
7544   int ilix_cmpxchg = ILI_OPND(ilix, 1);
7545 
7546   DEBUG_ASSERT((unsigned)idx < 2u, "gen_llvm_cmpxchg_component: bad index");
7547   /* Generate the cmpxchg */
7548   ll_cmpxchg = gen_llvm_expr(ilix_cmpxchg, NULL);
7549   ll_cmpxchg->next = make_constval32_op(idx);
7550   result = gen_extract_value_ll(ll_cmpxchg, ll_cmpxchg->ll_type,
7551                                 ll_cmpxchg->ll_type->sub_types[idx], idx);
7552   return result;
7553 }
7554 
7555 static void
add_sincos_map(INSTR_LIST * insn,unsigned flag)7556 add_sincos_map(INSTR_LIST *insn, unsigned flag)
7557 {
7558   hash_data_t data = NULL;
7559   if (!sincos_map)
7560     sincos_map = hashmap_alloc(hash_functions_direct);
7561   if (hashmap_lookup(sincos_map, insn, &data) && (HKEY2INT(data) & flag))
7562     return;
7563   data = INT2HKEY(HKEY2INT(data) | flag);
7564   hashmap_replace(sincos_map, insn, &data);
7565 }
7566 
7567 /**
7568    \brief Generate the \c extractvalue for the \c sin or \c cos part
7569  */
7570 INLINE static OPERAND *
gen_llvm_select_sin_or_cos(OPERAND * op,LL_Type * argTy,LL_Type * retTy,int component)7571 gen_llvm_select_sin_or_cos(OPERAND *op, LL_Type *argTy, LL_Type *retTy,
7572                            int component)
7573 {
7574   OPERAND *rv;
7575   add_sincos_map(op->tmps->info.idef, (component ? SINCOS_COS : SINCOS_SIN));
7576   rv = gen_extract_value_ll(op, retTy, argTy, component);
7577   add_sincos_map(rv->tmps->info.idef, SINCOS_EXTRACT);
7578   return rv;
7579 }
7580 
7581 INLINE static OPERAND *
gen_llvm_select_vsincos(OPERAND * op,LL_Type * argTy,LL_Type * retTy,int component)7582 gen_llvm_select_vsincos(OPERAND *op, LL_Type *argTy, LL_Type *retTy,
7583                         int component)
7584 {
7585   OPERAND *rv;
7586   add_sincos_map(op->tmps->info.idef, (component ? SINCOS_COS : SINCOS_SIN));
7587   rv = gen_extract_value_ll(op, retTy, argTy, component);
7588   add_sincos_map(rv->tmps->info.idef, SINCOS_EXTRACT);
7589   return rv;
7590 }
7591 
7592 static void
add_sincos_imap_loads(int ilix)7593 add_sincos_imap_loads(int ilix)
7594 {
7595   int i;
7596   const ILI_OP opc = ILI_OPC(ilix);
7597   const int noprs = ilis[opc].oprs;
7598   const ILTY_KIND ilty = IL_TYPE(opc);
7599   if (ilty == ILTY_LOAD) {
7600     hash_data_t data = NULL;
7601     hashmap_replace(sincos_imap, INT2HKEY(ilix), &data);
7602   }
7603   for (i = 1; i <= noprs; ++i) {
7604     if (IL_ISLINK(opc, i))
7605       add_sincos_imap_loads(ILI_OPND(ilix, i));
7606   }
7607 }
7608 
7609 INLINE static void
add_sincos_imap(int ilix,hash_data_t data)7610 add_sincos_imap(int ilix, hash_data_t data)
7611 {
7612   hashmap_replace(sincos_imap, INT2HKEY(ilix), &data);
7613 }
7614 
7615 /**
7616    \brief Generate a CALL to \c sincos, the scalar version
7617  */
7618 INLINE static OPERAND *
gen_llvm_sincos_call(int ilix)7619 gen_llvm_sincos_call(int ilix)
7620 {
7621   OPERAND *rv;
7622   const ILI_OP opc = ILI_OPC(ilix);
7623   const bool isSingle = (opc == IL_FSINCOS);
7624   LL_Type *llTy = make_lltype_from_dtype(isSingle ? DT_FLOAT : DT_DBLE);
7625   OPERAND *arg = gen_llvm_expr(ILI_OPND(ilix, 1), llTy);
7626   LL_Type *retTy = make_lltype_from_dtype(isSingle ? DT_CMPLX : DT_DCMPLX);
7627   char sincosName[36]; /* make_math_name buffer is 32 */
7628   llmk_math_name(sincosName, MTH_sincos, 1, false,
7629                  isSingle ? DT_FLOAT : DT_DBLE);
7630   if (!sincos_imap)
7631     sincos_imap = hashmap_alloc(hash_functions_direct);
7632   add_sincos_imap_loads(ILI_OPND(ilix, 1));
7633   rv = gen_call_to_builtin(ilix, sincosName, arg, retTy, NULL, I_CALL);
7634   add_sincos_imap(ilix, rv);
7635   return rv;
7636 }
7637 
7638 INLINE static LL_Type *
gen_vsincos_return_type(LL_Type * vecTy)7639 gen_vsincos_return_type(LL_Type *vecTy)
7640 {
7641   LL_Type *elements[2] = {vecTy, vecTy};
7642   return ll_create_anon_struct_type(cpu_llvm_module, elements, 2, false, LL_AddrSp_Default);
7643 }
7644 
7645 INLINE static OPERAND *
gen_llvm_vsincos_call(int ilix)7646 gen_llvm_vsincos_call(int ilix)
7647 {
7648   const DTYPE dtype = ili_get_vect_dtype(ilix);
7649   LL_Type *floatTy = make_lltype_from_dtype(DT_FLOAT);
7650   LL_Type *vecTy = make_lltype_from_dtype(dtype);
7651   DTYPE mask_dtype;
7652   LL_Type *maskTy;
7653   DTYPE dtypeName = (vecTy->sub_types[0] == floatTy) ? DT_FLOAT : DT_DBLE;
7654   LL_Type *retTy = gen_vsincos_return_type(vecTy);
7655   OPERAND *opnd = gen_llvm_expr(ILI_OPND(ilix, 1), vecTy);
7656   char sincosName[36]; /* make_math_name buffer is 32 */
7657   int vecLen = vecTy->sub_elements;
7658   int opndCount = ili_get_vect_arg_count(ilix);
7659   int mask_arg_ili = ILI_OPND(ilix, opndCount - 1);
7660   bool hasMask = false;
7661 
7662   /* Mask operand is always the one before the last operand */
7663   if (ILI_OPC(mask_arg_ili) != IL_NULL) {
7664      /* mask is always a vector of integers; same number and size as
7665       * the regular argument.
7666       */
7667      mask_dtype = get_vector_dtype(dtypeName==DT_FLOAT?DT_INT:DT_INT8,vecLen);
7668      maskTy = make_lltype_from_dtype(mask_dtype);
7669       opnd->next = gen_llvm_expr(mask_arg_ili, maskTy);
7670     hasMask = true;
7671   }
7672   llmk_math_name(sincosName, MTH_sincos, vecLen, hasMask, dtypeName);
7673   if (!sincos_imap)
7674     sincos_imap = hashmap_alloc(hash_functions_direct);
7675   add_sincos_imap_loads(ILI_OPND(ilix, 1));
7676   opnd = gen_call_to_builtin(ilix, sincosName, opnd, retTy, NULL, I_CALL);
7677   add_sincos_imap(ilix, opnd);
7678   return opnd;
7679 }
7680 
7681 static bool
sincos_argument_valid(int ilix)7682 sincos_argument_valid(int ilix)
7683 {
7684   int i;
7685   const ILI_OP opc = ILI_OPC(ilix);
7686   const int noprs = ilis[opc].oprs;
7687   const ILTY_KIND ilty = IL_TYPE(opc);
7688   if (ilty == ILTY_LOAD) {
7689     if (!hashmap_lookup(sincos_imap, INT2HKEY(ilix), NULL))
7690       return false;
7691   }
7692   for (i = 1; i <= noprs; ++i) {
7693     if (IL_ISLINK(opc, i) && (!sincos_argument_valid(ILI_OPND(ilix, i))))
7694       return false;
7695   }
7696   return true;
7697 }
7698 
7699 INLINE static OPERAND *
get_last_sincos(int ilix)7700 get_last_sincos(int ilix)
7701 {
7702   hash_data_t data = NULL;
7703   if (sincos_imap && hashmap_lookup(sincos_imap, INT2HKEY(ilix), &data) &&
7704       sincos_argument_valid(ILI_OPND(ilix, 1)))
7705     return (OPERAND *)data;
7706   return NULL;
7707 }
7708 
7709 INLINE static OPERAND *
gen_llvm_sincos_builtin(int ilix)7710 gen_llvm_sincos_builtin(int ilix)
7711 {
7712   OPERAND *sincos = get_last_sincos(ilix);
7713   return sincos ? sincos : gen_llvm_sincos_call(ilix);
7714 }
7715 
7716 INLINE static OPERAND *
gen_llvm_vsincos_builtin(int ilix)7717 gen_llvm_vsincos_builtin(int ilix)
7718 {
7719   OPERAND *vsincos = get_last_sincos(ilix);
7720   return vsincos ? vsincos : gen_llvm_vsincos_call(ilix);
7721 }
7722 
7723 INLINE static bool
is_complex_result(int ilix)7724 is_complex_result(int ilix)
7725 {
7726   if (on_prescan_complex_list(ILI_OPND(ilix, 1))) {
7727     ILI_OP opc = ILI_OPC(ilix);
7728     switch (opc) {
7729 #ifdef LONG_DOUBLE_FLOAT128
7730     case IL_FLOAT128RESULT:
7731       return true;
7732 #endif
7733     default:
7734       break;
7735     }
7736   }
7737   return false;
7738 }
7739 
7740 INLINE static LL_Type *
complex_result_type(ILI_OP opc)7741 complex_result_type(ILI_OP opc)
7742 {
7743   switch (opc) {
7744 #ifdef LONG_DOUBLE_FLOAT128
7745   case IL_FLOAT128RESULT:
7746     return make_lltype_from_dtype(DT_CMPLX128);
7747 #endif
7748   default:
7749     return NULL;
7750   }
7751 }
7752 
7753 INLINE static OPERAND *
gen_comp_operand(OPERAND * operand,ILI_OP opc,int lhs_ili,int rhs_ili,int cc_ili,int cc_type,LL_InstrName itype)7754 gen_comp_operand(OPERAND *operand, ILI_OP opc, int lhs_ili, int rhs_ili,
7755                  int cc_ili, int cc_type, LL_InstrName itype)
7756 {
7757   return gen_optext_comp_operand(operand, opc, lhs_ili, rhs_ili, cc_ili,
7758                                  cc_type, itype, 1, 0);
7759 }
7760 
7761 OPERAND *
gen_llvm_expr(int ilix,LL_Type * expected_type)7762 gen_llvm_expr(int ilix, LL_Type *expected_type)
7763 {
7764   int nme_ili, ld_ili, flags;
7765   SPTR sptr;
7766   MSZ msz;
7767   int lhs_ili, rhs_ili;
7768   CC_RELATION ili_cc;
7769   int zero_ili = 0;
7770   int first_ili, second_ili;
7771   int ct;
7772   DTYPE dt, cmpnt, dtype;
7773   ILI_OP opc = IL_NONE;
7774   bool cse_opc = false;
7775   DTYPE call_dtype = DT_NONE;
7776   SPTR call_sptr = SPTR_NULL;
7777   MATCH_Kind ret_match;
7778   LL_Type *comp_exp_type = (LL_Type *)0;
7779   LL_Type *intrinsic_type;
7780   OPERAND *operand, *args, *call_op;
7781   OPERAND *cc_op1, *cc_op2, *c1, *cse1;
7782   INT tmp[2];
7783   const char *intrinsic_name;
7784   union {
7785     double d;
7786     INT tmp[2];
7787   } dtmp;
7788   float f;
7789   double d;
7790 
7791   switch (ILI_OPC(ilix)) {
7792   case IL_JSR:
7793   case IL_JSRA:
7794     /*  ILI_ALT may be IL_GJSR/IL_GJSRA */
7795     break;
7796   case IL_VFLOOR:
7797   case IL_VCEIL:
7798   case IL_VAINT:
7799   case IL_FFLOOR:
7800   case IL_DFLOOR:
7801   case IL_FCEIL:
7802   case IL_DCEIL:
7803   case IL_AINT:
7804   case IL_DINT:
7805     /* floor/ceil/aint use llvm intrinsics, not calls via alt-ili */
7806     break;
7807   case IL_VSIN:
7808   case IL_VCOS:
7809     if (ILI_OPC(ILI_OPND(ilix, 1)) == IL_VSINCOS)
7810       break;
7811     /* fall-through */
7812   default:
7813     if (ILI_ALT(ilix)) {
7814       ilix = ILI_ALT(ilix);
7815     }
7816     break;
7817   }
7818   opc = ILI_OPC(ilix);
7819 
7820   DBGTRACEIN2(" ilix: %d(%s)", ilix, IL_NAME(opc));
7821   DBGDUMPLLTYPE("#expected type: ", expected_type);
7822 
7823   assert(ilix, "gen_llvm_expr(): no incoming ili", 0, ERR_Fatal);
7824   operand = make_operand();
7825 
7826   switch (opc) {
7827   case IL_JSRA:
7828   case IL_GJSRA:
7829     call_dtype = ILI_DTyOPND(ilix, 4);
7830     if (call_dtype) {
7831       /* iface symbol table value */
7832       call_dtype = (DTYPE)DTY(DTYPEG(call_dtype)); // FIXME: bug?
7833     } else {
7834       call_dtype = llvm_info.curr_ret_dtype;
7835     }
7836     goto call_processing;
7837   case IL_QJSR:
7838   case IL_JSR:
7839   case IL_GJSR:
7840     sptr = ILI_SymOPND(ilix, 1);
7841     call_op = gen_call_as_llvm_instr(sptr, ilix);
7842     if (call_op) {
7843       operand = call_op;
7844       break;
7845     }
7846 
7847 /* check for return dtype */
7848     call_dtype = (DTYPE)DTY(DTYPEG(sptr)); // FIXME: is this a bug?
7849 
7850     DBGTRACE1("#CALL to %s", SYMNAME(sptr))
7851 
7852     call_sptr = sptr;
7853 
7854   call_processing:
7855     call_op = gen_call_expr(ilix, call_dtype, NULL, call_sptr);
7856     if (call_op) {
7857       operand = call_op;
7858       if (DT_ISUNSIGNED(call_dtype))
7859         operand->flags |= OPF_ZEXT;
7860       else if (DT_ISINT(call_dtype))
7861         operand->flags |= OPF_SEXT;
7862     } else {
7863       operand->ll_type = make_void_lltype();
7864     }
7865     break;
7866   case IL_EXIT:
7867     operand = gen_return_operand(ilix);
7868     break;
7869   case IL_VA_ARG:
7870     operand = gen_va_arg(ilix);
7871     break;
7872   case IL_ACON:
7873     operand = gen_base_addr_operand(ilix, expected_type);
7874     break;
7875   case IL_LDA:
7876     nme_ili = ILI_OPND(ilix, 2);
7877     ld_ili = ILI_OPND(ilix, 1);
7878     if (ILI_OPC(ld_ili) != IL_ACON && expected_type &&
7879         (expected_type->data_type == LL_PTR)) {
7880       LL_Type *pt_expected_type = make_ptr_lltype(expected_type);
7881       operand = gen_base_addr_operand(ld_ili, pt_expected_type);
7882     } else {
7883       operand = gen_address_operand(ld_ili, nme_ili, true, NULL,
7884                                     MSZ_ILI_OPND(ilix, 3));
7885     }
7886     sptr = basesym_of(nme_ili);
7887     if ((operand->ll_type->data_type == LL_PTR) ||
7888         (operand->ll_type->data_type == LL_ARRAY)) {
7889       DTYPE dtype = DTYPEG(sptr);
7890 
7891       /* If no type found assume generic pointer */
7892       if (dtype == DT_NONE)
7893         dtype = DT_CPTR;
7894 
7895       if (operand->ll_type->sub_types[0]->data_type == LL_PTR ||
7896           ILI_OPC(ld_ili) != IL_ACON) {
7897         operand =
7898             make_load(ilix, operand, operand->ll_type->sub_types[0], (MSZ)-2,
7899                       ldst_instr_flags_from_dtype_nme(dtype, nme_ili));
7900       } else {
7901         if ((SCG(sptr) != SC_DUMMY))
7902         {
7903           LL_Type *llt = make_ptr_lltype(expected_type);
7904           operand = make_bitcast(operand, llt);
7905           /* ??? what is the magic constant -2? */
7906           operand =
7907               make_load(ilix, operand, operand->ll_type->sub_types[0], (MSZ)-2,
7908                         ldst_instr_flags_from_dtype_nme(dtype, nme_ili));
7909         }
7910       }
7911     }
7912     break;
7913   case IL_VLD: {
7914     LL_Type *llt, *vect_lltype, *int_llt = NULL;
7915     DTYPE vect_dtype = ILI_DTyOPND(ilix, 3);
7916 
7917     nme_ili = ILI_OPND(ilix, 2);
7918     ld_ili = ILI_OPND(ilix, 1);
7919     vect_lltype = make_lltype_from_dtype(vect_dtype);
7920     llt = make_ptr_lltype(vect_lltype);
7921     if (expected_type && (expected_type->data_type == LL_VECTOR) &&
7922         (expected_type->sub_elements == 4 ||
7923          expected_type->sub_elements == 3) &&
7924         (llt->sub_types[0]->data_type == LL_VECTOR) &&
7925         (llt->sub_types[0]->sub_elements == 3)) {
7926       LL_Type *veleTy = llt->sub_types[0]->sub_types[0];
7927       LL_Type *vTy = ll_get_vector_type(veleTy, 4);
7928       llt = make_ptr_lltype(vTy);
7929     }
7930 #ifdef TARGET_LLVM_ARM
7931     switch (zsize_of(vect_dtype)) {
7932     case 2:
7933       int_llt = make_ptr_lltype(make_lltype_from_dtype(DT_SINT));
7934       break;
7935     case 3:
7936     case 4:
7937       int_llt = make_ptr_lltype(make_lltype_from_dtype(DT_INT));
7938       break;
7939     default:
7940       break;
7941     }
7942 #endif
7943     operand = gen_address_operand(ld_ili, nme_ili, false,
7944                                   (int_llt ? int_llt : llt), (MSZ)-1);
7945     if ((operand->ll_type->data_type == LL_PTR) ||
7946         (operand->ll_type->data_type == LL_ARRAY)) {
7947       operand = make_load(ilix, operand, operand->ll_type->sub_types[0],
7948                           (MSZ)-2, ldst_instr_flags_from_dtype(vect_dtype));
7949       if (int_llt != NULL) {
7950         if (expected_type == NULL ||
7951             !strict_match(operand->ll_type, int_llt->sub_types[0]))
7952           operand = make_bitcast(operand, llt->sub_types[0]);
7953       }
7954     } else if (int_llt) {
7955       operand = make_bitcast(operand, llt);
7956     }
7957     if (expected_type && (expected_type->data_type == LL_VECTOR) &&
7958         !strict_match(operand->ll_type, expected_type)) {
7959       if (expected_type->sub_elements == 3) {
7960         if (int_llt && (zsize_of(vect_dtype) == 4))
7961           operand = make_bitcast(operand, vect_lltype);
7962       } else {
7963         operand = make_bitcast(operand, expected_type);
7964       }
7965     }
7966   } break;
7967   case IL_VLDU: {
7968     LL_Type *llt, *vect_lltype, *int_llt = NULL;
7969     DTYPE vect_dtype = ILI_DTyOPND(ilix, 3);
7970 
7971     nme_ili = ILI_OPND(ilix, 2);
7972     ld_ili = ILI_OPND(ilix, 1);
7973     vect_lltype = make_lltype_from_dtype(vect_dtype);
7974     llt = make_ptr_lltype(vect_lltype);
7975     if (expected_type && (expected_type->data_type == LL_VECTOR) &&
7976         (expected_type->sub_elements == 4) &&
7977         (llt->sub_types[0]->data_type == LL_VECTOR) &&
7978         (llt->sub_types[0]->sub_elements == 3)) {
7979       LL_Type *veleTy = llt->sub_types[0]->sub_types[0];
7980       LL_Type *vTy = ll_get_vector_type(veleTy, 4);
7981       llt = make_ptr_lltype(vTy);
7982     }
7983 #ifdef TARGET_LLVM_ARM
7984     if (vect_lltype->sub_elements != 3) {
7985       if (vect_lltype->sub_elements != 3) {
7986         switch (zsize_of(vect_dtype)) {
7987         case 2:
7988           int_llt = make_ptr_lltype(make_lltype_from_dtype(DT_SINT));
7989           break;
7990         case 4:
7991           if (expected_type && (expected_type->data_type == LL_VECTOR) &&
7992               (expected_type->sub_elements != 3))
7993             int_llt = make_ptr_lltype(make_lltype_from_dtype(DT_INT));
7994           break;
7995         default:
7996           break;
7997         }
7998       } else if (expected_type && ll_type_int_bits(expected_type)) {
7999         int_llt = make_ptr_lltype(expected_type);
8000       }
8001     }
8002 #endif
8003     operand = gen_address_operand(ld_ili, nme_ili, false,
8004                                   (int_llt ? int_llt : llt), (MSZ)-1);
8005     if (ll_type_is_mem_seq(operand->ll_type)) {
8006       operand = make_load(ilix, operand, operand->ll_type->sub_types[0],
8007                           (MSZ)-2, /* unaligned */ 0);
8008       if (int_llt != NULL) {
8009         if (expected_type == NULL ||
8010             !strict_match(operand->ll_type, int_llt->sub_types[0]))
8011           operand = make_bitcast(operand, llt->sub_types[0]);
8012       } else if (vect_lltype->sub_elements == 3 && expected_type &&
8013                  ll_type_int_bits(expected_type) &&
8014                  !strict_match(operand->ll_type, expected_type)) {
8015         operand = gen_resized_vect(operand, ll_type_bytes(expected_type), 0);
8016         operand = make_bitcast(operand, expected_type);
8017       }
8018     } else if (int_llt) {
8019       operand = make_bitcast(operand, llt);
8020     }
8021     if (expected_type && expected_type->data_type == LL_VECTOR &&
8022         !strict_match(operand->ll_type, expected_type)) {
8023       if (expected_type->sub_elements == 3) {
8024         if (int_llt && (zsize_of(vect_dtype) == 4))
8025           operand = make_bitcast(operand, vect_lltype);
8026       } else {
8027         operand = make_bitcast(operand, expected_type);
8028       }
8029     }
8030   } break;
8031   case IL_LDSCMPLX:
8032   case IL_LDDCMPLX: {
8033     unsigned flags;
8034     ld_ili = ILI_OPND(ilix, 1);
8035     nme_ili = ILI_OPND(ilix, 2);
8036     msz = (MSZ)ILI_OPND(ilix, 3);
8037     flags = opc == IL_LDSCMPLX ? DT_CMPLX : DT_DCMPLX;
8038     operand = gen_address_operand(ld_ili, nme_ili, false,
8039                                   make_ptr_lltype(expected_type), (MSZ)-1);
8040     assert(operand->ll_type->data_type == LL_PTR,
8041            "Invalid operand for cmplx load", ilix, ERR_Fatal);
8042     operand =
8043         make_load(ilix, operand, operand->ll_type->sub_types[0], msz, flags);
8044   } break;
8045   case IL_LD:
8046   case IL_LDSP:
8047   case IL_LDDP:
8048   case IL_LDKR:
8049 #ifdef LONG_DOUBLE_FLOAT128
8050   case IL_FLOAT128LD:
8051 #endif
8052 #ifdef TARGET_LLVM_X8664
8053   case IL_LDQ:
8054   case IL_LD256:
8055 #endif
8056     ld_ili = ILI_OPND(ilix, 1);
8057     nme_ili = ILI_OPND(ilix, 2);
8058     msz = MSZ_ILI_OPND(ilix, 3);
8059     operand = gen_address_operand(ld_ili, nme_ili, false, NULL, msz);
8060     if ((operand->ll_type->data_type == LL_PTR) ||
8061         (operand->ll_type->data_type == LL_ARRAY)) {
8062       LL_InstrListFlags flags =
8063           ldst_instr_flags_from_dtype_nme(msz_dtype(msz), nme_ili);
8064       operand =
8065           make_load(ilix, operand, operand->ll_type->sub_types[0], msz, flags);
8066     }
8067     break;
8068   case IL_ATOMICLDSP:
8069   case IL_ATOMICLDDP: {
8070     DTYPE fromdtype, todtype;
8071     MSZ newmsz;
8072     LL_InstrListFlags flags;
8073     ld_ili = ILI_OPND(ilix, 1);
8074     nme_ili = ILI_OPND(ilix, 2);
8075     msz = ILI_MSZ_OF_LD(ilix);
8076     if (opc == IL_ATOMICLDSP) {
8077       fromdtype = DT_FLOAT;
8078       todtype = DT_INT;
8079       newmsz = MSZ_WORD;
8080     } else {
8081       fromdtype = DT_DBLE;
8082       todtype = DT_INT8;
8083       newmsz = MSZ_I8;
8084     }
8085     flags = (LL_InstrListFlags)(
8086         ll_instr_flags_for_memory_order_and_scope(ilix) |
8087         ldst_instr_flags_from_dtype_nme(msz_dtype(msz), nme_ili));
8088     operand = gen_address_operand(ld_ili, nme_ili, false, NULL, newmsz);
8089     operand =
8090         make_load(ilix, operand, operand->ll_type->sub_types[0], newmsz, flags);
8091     operand = make_bitcast(operand, make_lltype_from_dtype(fromdtype));
8092   } break;
8093 
8094   case IL_ATOMICLDA:
8095   case IL_ATOMICLDI:
8096   case IL_ATOMICLDKR: {
8097     LL_InstrListFlags flags;
8098     ld_ili = ILI_OPND(ilix, 1);
8099     nme_ili = ILI_OPND(ilix, 2);
8100     msz = (MSZ)ILI_MSZ_OF_LD(ilix);
8101     flags = (LL_InstrListFlags)(
8102         ll_instr_flags_for_memory_order_and_scope(ilix) |
8103         ldst_instr_flags_from_dtype_nme(msz_dtype(msz), nme_ili));
8104     operand = gen_address_operand(ld_ili, nme_ili, false, NULL, msz);
8105     operand =
8106         make_load(ilix, operand, operand->ll_type->sub_types[0], msz, flags);
8107   } break;
8108   case IL_VCON:
8109   case IL_KCON:
8110   case IL_ICON:
8111   case IL_FCON:
8112   case IL_DCON:
8113   case IL_SCMPLXCON:
8114   case IL_DCMPLXCON:
8115 #ifdef LONG_DOUBLE_FLOAT128
8116   case IL_FLOAT128CON:
8117 #endif
8118     operand = gen_const_expr(ilix, expected_type);
8119     break;
8120   case IL_FIX:
8121   case IL_DFIX:
8122     operand = gen_unary_expr(ilix, I_FPTOSI);
8123     break;
8124   case IL_FIXK:
8125   case IL_DFIXK:
8126     operand = gen_unary_expr(ilix, I_FPTOSI);
8127     break;
8128   case IL_FIXUK:
8129   case IL_DFIXUK:
8130   case IL_DFIXU:
8131   case IL_UFIX:
8132     operand = gen_unary_expr(ilix, I_FPTOUI);
8133     break;
8134   case IL_FLOATU:
8135   case IL_DFLOATU:
8136   case IL_FLOATUK:
8137   case IL_DFLOATUK:
8138     operand = gen_unary_expr(ilix, I_UITOFP);
8139     break;
8140   case IL_FLOAT:
8141   case IL_DFLOAT:
8142   case IL_DFLOATK:
8143   case IL_FLOATK:
8144     operand = gen_unary_expr(ilix, I_SITOFP);
8145     break;
8146   case IL_SNGL:
8147     operand = gen_unary_expr(ilix, I_FPTRUNC);
8148     break;
8149   case IL_DBLE:
8150     operand = gen_unary_expr(ilix, I_FPEXT);
8151     break;
8152 #ifdef LONG_DOUBLE_FLOAT128
8153   case IL_FLOAT128FROM:
8154     operand = gen_unary_expr(ilix, I_FPEXT);
8155     break;
8156   case IL_FLOAT128TO:
8157     operand = gen_unary_expr(ilix, I_FPTRUNC);
8158     break;
8159 #endif
8160   case IL_ALLOC:
8161     operand = gen_unary_expr(ilix, I_ALLOCA);
8162     break;
8163   case IL_DEALLOC:
8164     break;
8165   case IL_VADD:
8166     operand = gen_binary_vexpr(ilix, I_ADD, I_ADD, I_FADD);
8167     break;
8168   case IL_IADD:
8169   case IL_KADD:
8170   case IL_UKADD:
8171   case IL_UIADD:
8172     operand = gen_binary_expr(ilix, I_ADD);
8173     break;
8174   case IL_FADD:
8175   case IL_DADD:
8176 #ifdef LONG_DOUBLE_FLOAT128
8177   case IL_FLOAT128ADD:
8178 #endif
8179     operand = gen_binary_expr(ilix, I_FADD);
8180     break;
8181   case IL_SCMPLXADD:
8182     operand = gen_cmplx_math(ilix, DT_CMPLX, I_FADD);
8183     break;
8184   case IL_DCMPLXADD:
8185     operand = gen_cmplx_math(ilix, DT_DCMPLX, I_FADD);
8186     break;
8187   case IL_VSUB:
8188     operand = gen_binary_vexpr(ilix, I_SUB, I_SUB, I_FSUB);
8189     break;
8190   case IL_ISUB:
8191   case IL_KSUB:
8192   case IL_UKSUB:
8193   case IL_UISUB:
8194     operand = gen_binary_expr(ilix, I_SUB);
8195     break;
8196   case IL_FSUB:
8197   case IL_DSUB:
8198 #ifdef LONG_DOUBLE_FLOAT128
8199   case IL_FLOAT128SUB:
8200 #endif
8201     operand = gen_binary_expr(ilix, I_FSUB);
8202     break;
8203   case IL_SCMPLXSUB:
8204     operand = gen_cmplx_math(ilix, DT_CMPLX, I_FSUB);
8205     break;
8206   case IL_DCMPLXSUB:
8207     operand = gen_cmplx_math(ilix, DT_DCMPLX, I_FSUB);
8208     break;
8209   case IL_VMUL:
8210     operand = gen_binary_vexpr(ilix, I_MUL, I_MUL, I_FMUL);
8211     break;
8212   case IL_IMUL:
8213   case IL_KMUL:
8214   case IL_UKMUL:
8215   case IL_UIMUL:
8216     operand = gen_binary_expr(ilix, I_MUL);
8217     break;
8218   case IL_KMULH:
8219   case IL_UKMULH:
8220     operand = gen_mulh_expr(ilix);
8221     break;
8222   case IL_FMUL:
8223   case IL_DMUL:
8224 #ifdef LONG_DOUBLE_FLOAT128
8225   case IL_FLOAT128MUL:
8226 #endif
8227     operand = gen_binary_expr(ilix, I_FMUL);
8228     break;
8229   case IL_SCMPLXMUL:
8230     operand = gen_cmplx_mul(ilix, DT_CMPLX);
8231     break;
8232   case IL_DCMPLXMUL:
8233     operand = gen_cmplx_mul(ilix, DT_DCMPLX);
8234     break;
8235   case IL_VDIV:
8236     operand = gen_binary_vexpr(ilix, I_SDIV, I_UDIV, I_FDIV);
8237     break;
8238   case IL_KDIV:
8239   case IL_IDIV:
8240     operand = gen_binary_expr(ilix, I_SDIV);
8241     break;
8242   case IL_UKDIV:
8243   case IL_UIDIV:
8244     operand = gen_binary_expr(ilix, I_UDIV);
8245     break;
8246   case IL_FDIV:
8247   case IL_DDIV:
8248 #ifdef LONG_DOUBLE_FLOAT128
8249   case IL_FLOAT128DIV:
8250 #endif
8251     operand = gen_binary_expr(ilix, I_FDIV);
8252     break;
8253   case IL_VLSHIFTV:
8254   case IL_VLSHIFTS:
8255   case IL_LSHIFT:
8256   case IL_ULSHIFT:
8257   case IL_KLSHIFT:
8258     operand = gen_binary_expr(ilix, I_SHL);
8259     break;
8260   case IL_VRSHIFTV:
8261   case IL_VRSHIFTS:
8262     operand = gen_binary_vexpr(ilix, I_ASHR, I_LSHR, I_ASHR);
8263     break;
8264   case IL_VURSHIFTS:
8265     operand = gen_binary_vexpr(ilix, I_LSHR, I_LSHR, I_LSHR);
8266     break;
8267   case IL_URSHIFT:
8268   case IL_KURSHIFT:
8269     operand = gen_binary_expr(ilix, I_LSHR);
8270     break;
8271   case IL_RSHIFT:
8272   case IL_ARSHIFT:
8273   case IL_KARSHIFT:
8274     operand = gen_binary_expr(ilix, I_ASHR);
8275     break;
8276   case IL_VAND:
8277     /* need to check dtype - if floating type need special code to
8278      * cast to int, compare, then cast back to float. Similar to
8279      * what is done with the IL_FAND case, except with vectors.
8280      * Else just fall through.
8281      * NB: currently this method only works for float values, not
8282      * doubles (and when using -Mfprelaxed that is all our compiler
8283      * currently operates on anyway.)
8284      */
8285     dtype = (DTYPE)ILI_OPND(ilix, 3); /* get the vector dtype */
8286     assert(TY_ISVECT(DTY(dtype)), "gen_llvm_expr(): expected vect type",
8287            DTY(dtype), ERR_Fatal);
8288     /* check the base type for float/real */
8289     if (DTY(DTySeqTyElement(dtype)) == TY_FLOAT) {
8290       OPERAND *op1, *op2, *op3, *op4, *op5, *op6;
8291       INSTR_LIST *instr1, *instr2, *instr3;
8292       int vsize = DTyVecLength(dtype);
8293       LL_Type *viTy = make_vtype(DT_INT, vsize);
8294       LL_Type *vfTy = make_vtype(DT_FLOAT, vsize);
8295       op1 = gen_llvm_expr(ILI_OPND(ilix, 1), NULL);
8296       op2 = make_tmp_op(viTy, make_tmps());
8297       instr1 = gen_instr(I_BITCAST, op2->tmps, viTy, op1);
8298       ad_instr(ilix, instr1);
8299       op3 = gen_llvm_expr(ILI_OPND(ilix, 2), NULL);
8300       op4 = make_tmp_op(viTy, make_tmps());
8301       instr2 = gen_instr(I_BITCAST, op4->tmps, viTy, op3);
8302       ad_instr(ilix, instr2);
8303       op6 = make_tmp_op(vfTy, make_tmps());
8304       op2->next = op4;
8305       op5 = ad_csed_instr(I_AND, 0, viTy, op2, InstrListFlagsNull, false);
8306       instr3 = gen_instr(I_BITCAST, op6->tmps, vfTy, op5);
8307       ad_instr(ilix, instr3);
8308       operand = op6;
8309       break;
8310     }
8311   case IL_KAND:
8312   case IL_AND:
8313     operand = gen_binary_expr(ilix, I_AND);
8314     break;
8315   case IL_VOR:
8316   case IL_KOR:
8317   case IL_OR:
8318     operand = gen_binary_expr(ilix, I_OR);
8319     break;
8320   case IL_VXOR:
8321   case IL_KXOR:
8322   case IL_XOR:
8323     operand = gen_binary_expr(ilix, I_XOR);
8324     break;
8325   case IL_VMOD:
8326     operand = gen_binary_vexpr(ilix, I_SREM, I_UREM, I_FREM);
8327     break;
8328   case IL_SCMPLXXOR:
8329     operand = gen_cmplx_math(ilix, DT_CMPLX, I_XOR);
8330     break;
8331   case IL_DCMPLXXOR:
8332     operand = gen_cmplx_math(ilix, DT_DCMPLX, I_XOR);
8333     break;
8334   case IL_KMOD:
8335   case IL_MOD:
8336     operand = gen_binary_expr(ilix, I_SREM);
8337     break;
8338   case IL_KUMOD:
8339   case IL_UIMOD:
8340     operand = gen_binary_expr(ilix, I_UREM);
8341     break;
8342   case IL_ASUB:
8343   case IL_AADD: {
8344     LL_Type *t =
8345         expected_type ? expected_type : make_lltype_from_dtype(DT_CPTR);
8346     operand = gen_base_addr_operand(ilix, t);
8347   } break;
8348   /* jumps on zero with cc */
8349   case IL_FCJMPZ:
8350     tmp[0] = 0.0;
8351     f = 0.0;
8352     mftof(f, tmp[1]);
8353     zero_ili = ad1ili(IL_FCON, getcon(tmp, DT_FLOAT));
8354     comp_exp_type = make_lltype_from_dtype(DT_FLOAT);
8355   case IL_DCJMPZ:
8356     if (!zero_ili) {
8357       d = 0.0;
8358       xmdtod(d, dtmp.tmp);
8359       zero_ili = ad1ili(IL_DCON, getcon(dtmp.tmp, DT_DBLE));
8360       comp_exp_type = make_lltype_from_dtype(DT_DBLE);
8361     }
8362     operand->ot_type = OT_CC;
8363     first_ili = ILI_OPND(ilix, 1);
8364     second_ili = zero_ili;
8365     ili_cc = ILI_ccOPND(ilix, 2);
8366     if (IEEE_CMP)
8367       float_jmp = true;
8368     operand->val.cc = convert_to_llvm_fltcc(ili_cc);
8369     float_jmp = false;
8370     operand->ll_type = make_type_from_opc(opc);
8371     goto process_cc;
8372     break;
8373   case IL_UKCJMPZ:
8374     zero_ili = ad_kconi(0);
8375     operand->ot_type = OT_CC;
8376     operand->val.cc = convert_to_llvm_uintcc(ILI_ccOPND(ilix, 2));
8377     operand->ll_type = make_type_from_opc(opc);
8378     first_ili = ILI_OPND(ilix, 1);
8379     second_ili = zero_ili;
8380     comp_exp_type = make_lltype_from_dtype(DT_INT8);
8381     goto process_cc;
8382     break;
8383   case IL_UICJMPZ:
8384     zero_ili = ad_icon(0);
8385     operand->ot_type = OT_CC;
8386     operand->val.cc = convert_to_llvm_uintcc(ILI_ccOPND(ilix, 2));
8387     operand->ll_type = make_type_from_opc(opc);
8388     first_ili = ILI_OPND(ilix, 1);
8389     second_ili = zero_ili;
8390     comp_exp_type = make_lltype_from_dtype(DT_INT);
8391     goto process_cc;
8392     break;
8393 
8394   case IL_KCJMPZ:
8395     zero_ili = ad_kconi(0);
8396     operand->ot_type = OT_CC;
8397     operand->val.cc = convert_to_llvm_intcc(ILI_ccOPND(ilix, 2));
8398     operand->ll_type = make_type_from_opc(opc);
8399     first_ili = ILI_OPND(ilix, 1);
8400     second_ili = zero_ili;
8401     comp_exp_type = make_lltype_from_dtype(DT_INT8);
8402     goto process_cc;
8403     break;
8404   case IL_ICJMPZ:
8405     zero_ili = ad_icon(0);
8406 
8407     operand->ot_type = OT_CC;
8408     operand->val.cc = convert_to_llvm_intcc(ILI_ccOPND(ilix, 2));
8409     operand->ll_type = make_type_from_opc(opc);
8410     first_ili = ILI_OPND(ilix, 1);
8411     second_ili = zero_ili;
8412     comp_exp_type = make_lltype_from_dtype(DT_INT);
8413     goto process_cc;
8414     break;
8415   case IL_ACJMPZ:
8416     zero_ili = ad_icon(0);
8417 
8418     operand->ot_type = OT_CC;
8419     operand->val.cc = convert_to_llvm_uintcc(ILI_ccOPND(ilix, 2));
8420     comp_exp_type = operand->ll_type = make_type_from_opc(opc);
8421     first_ili = ILI_OPND(ilix, 1);
8422     second_ili = zero_ili;
8423     goto process_cc;
8424     break;
8425   /* jumps with cc and expression */
8426   case IL_FCJMP:
8427   case IL_DCJMP:
8428     operand->ot_type = OT_CC;
8429     first_ili = ILI_OPND(ilix, 1);
8430     second_ili = ILI_OPND(ilix, 2);
8431     ili_cc = ILI_ccOPND(ilix, 3);
8432     if (IEEE_CMP)
8433       float_jmp = true;
8434     operand->val.cc = convert_to_llvm_fltcc(ili_cc);
8435     float_jmp = false;
8436     comp_exp_type = operand->ll_type = make_type_from_opc(opc);
8437     goto process_cc;
8438     break;
8439   case IL_UKCJMP:
8440   case IL_UICJMP:
8441     operand->ot_type = OT_CC;
8442     operand->val.cc = convert_to_llvm_uintcc(ILI_ccOPND(ilix, 3));
8443     comp_exp_type = operand->ll_type = make_type_from_opc(opc);
8444     first_ili = ILI_OPND(ilix, 1);
8445     second_ili = ILI_OPND(ilix, 2);
8446     goto process_cc;
8447     break;
8448   case IL_KCJMP:
8449   case IL_ICJMP:
8450     operand->ot_type = OT_CC;
8451     operand->val.cc = convert_to_llvm_intcc(ILI_ccOPND(ilix, 3));
8452     comp_exp_type = operand->ll_type = make_type_from_opc(opc);
8453     first_ili = ILI_OPND(ilix, 1);
8454     second_ili = ILI_OPND(ilix, 2);
8455     goto process_cc;
8456     break;
8457   case IL_ACJMP:
8458     operand->ot_type = OT_CC;
8459     operand->val.cc = convert_to_llvm_uintcc(ILI_ccOPND(ilix, 3));
8460     comp_exp_type = operand->ll_type = make_type_from_opc(opc);
8461     first_ili = ILI_OPND(ilix, 1);
8462     second_ili = ILI_OPND(ilix, 2);
8463   process_cc:
8464     operand->next = cc_op1 = gen_llvm_expr(first_ili, operand->ll_type);
8465     operand->next->next = cc_op2 = gen_llvm_expr(second_ili, comp_exp_type);
8466     break;
8467   case IL_FCMP:
8468   case IL_DCMP:
8469 #ifdef LONG_DOUBLE_FLOAT128
8470   case IL_FLOAT128CMP:
8471 #endif
8472     lhs_ili = ILI_OPND(ilix, 1);
8473     rhs_ili = ILI_OPND(ilix, 2);
8474     ili_cc = ILI_ccOPND(ilix, 3);
8475     if (IEEE_CMP)
8476       float_jmp = true;
8477     operand = gen_comp_operand(operand, opc, lhs_ili, rhs_ili, ili_cc, CMP_FLT,
8478                                I_FCMP);
8479     break;
8480   case IL_CMPNEQSS: {
8481     OPERAND *op1;
8482     INSTR_LIST *instr1;
8483     unsigned bits = 8 * size_of(DT_FLOAT);
8484     LL_Type *iTy = make_int_lltype(bits);
8485     lhs_ili = ILI_OPND(ilix, 1);
8486     rhs_ili = ILI_OPND(ilix, 2);
8487     ili_cc = CC_NE;
8488     if (IEEE_CMP)
8489       float_jmp = true;
8490     operand = gen_optext_comp_operand(operand, opc, lhs_ili, rhs_ili, ili_cc,
8491                                       CMP_FLT, I_FCMP, 0, 0);
8492     /* sext i1 to i32 */
8493     op1 = make_tmp_op(iTy, make_tmps());
8494     instr1 = gen_instr(I_SEXT, op1->tmps, iTy, operand);
8495     ad_instr(ilix, instr1);
8496     operand = op1;
8497   } break;
8498   case IL_VCMPNEQ: {
8499     OPERAND *op1;
8500     INSTR_LIST *instr1;
8501     int vsize;
8502     LL_Type *viTy;
8503     dtype = (DTYPE)ILI_OPND(ilix, 3); /* get the vector dtype */
8504     assert(TY_ISVECT(DTY(dtype)), "gen_llvm_expr(): expected vect type",
8505            DTY(dtype), ERR_Fatal);
8506     vsize = DTyVecLength(dtype);
8507     viTy = make_vtype(DT_INT, vsize);
8508     lhs_ili = ILI_OPND(ilix, 1);
8509     rhs_ili = ILI_OPND(ilix, 2);
8510     ili_cc = CC_NE;
8511     if (IEEE_CMP)
8512       float_jmp = true;
8513     operand = gen_optext_comp_operand(operand, opc, lhs_ili, rhs_ili, ili_cc,
8514                                       CMP_FLT, I_FCMP, 0, ilix);
8515     /* sext i1 to i32 */
8516     op1 = make_tmp_op(viTy, make_tmps());
8517     instr1 = gen_instr(I_SEXT, op1->tmps, viTy, operand);
8518     ad_instr(ilix, instr1);
8519     operand = op1;
8520   } break;
8521   case IL_KCMPZ:
8522     lhs_ili = ILI_OPND(ilix, 1);
8523     rhs_ili = ad_kconi(0);
8524     operand = gen_comp_operand(operand, opc, lhs_ili, rhs_ili,
8525                                ILI_OPND(ilix, 2), CMP_INT, I_ICMP);
8526     break;
8527   case IL_UKCMPZ:
8528     lhs_ili = ILI_OPND(ilix, 1);
8529     rhs_ili = ad_kconi(0);
8530     operand = gen_comp_operand(operand, opc, lhs_ili, rhs_ili,
8531                                ILI_OPND(ilix, 2), CMP_INT | CMP_USG, I_ICMP);
8532     break;
8533   case IL_ICMPZ:
8534     /* what are we testing for here? We may have an ICMPZ pointing to
8535      * an FCMP, which is negating the sense of the FCMP. To account for
8536      * NaNs (hence the IEEE_CMP test) we need to correctly negate
8537      * the floating comparison operator, taking into account both
8538      * ordered and unordered cases. That is why we set fcmp_negate
8539      * for use in convert_to_llvm_cc().
8540      */
8541     if (IEEE_CMP && ILI_OPC(ILI_OPND(ilix, 1)) == IL_FCMP) {
8542       int fcmp_ili = ILI_OPND(ilix, 1);
8543 
8544       lhs_ili = ILI_OPND(fcmp_ili, 1);
8545       rhs_ili = ILI_OPND(fcmp_ili, 2);
8546       fcmp_negate = true;
8547       operand = gen_comp_operand(operand, IL_FCMP, lhs_ili, rhs_ili,
8548                                  ILI_OPND(fcmp_ili, 3), CMP_FLT, I_FCMP);
8549       fcmp_negate = false;
8550       break;
8551     }
8552     lhs_ili = ILI_OPND(ilix, 1);
8553     rhs_ili = ad_icon(0);
8554     operand = gen_comp_operand(operand, opc, lhs_ili, rhs_ili,
8555                                ILI_OPND(ilix, 2), CMP_INT, I_ICMP);
8556     break;
8557   case IL_ACMPZ:
8558     lhs_ili = ILI_OPND(ilix, 1);
8559     rhs_ili = ad_icon(0);
8560     operand = gen_comp_operand(operand, opc, lhs_ili, rhs_ili,
8561                                ILI_OPND(ilix, 2), CMP_INT | CMP_USG, I_ICMP);
8562     break;
8563   case IL_UICMPZ:
8564     lhs_ili = ILI_OPND(ilix, 1);
8565     rhs_ili = ad_icon(0);
8566     operand = gen_comp_operand(operand, opc, lhs_ili, rhs_ili,
8567                                ILI_OPND(ilix, 2), CMP_INT | CMP_USG, I_ICMP);
8568     break;
8569   case IL_UKCMP:
8570   case IL_UICMP:
8571     lhs_ili = ILI_OPND(ilix, 1);
8572     rhs_ili = ILI_OPND(ilix, 2);
8573     operand = gen_comp_operand(operand, opc, lhs_ili, rhs_ili,
8574                                ILI_OPND(ilix, 3), CMP_INT | CMP_USG, I_ICMP);
8575     break;
8576   case IL_ACMP:
8577     lhs_ili = ILI_OPND(ilix, 1);
8578     rhs_ili = ILI_OPND(ilix, 2);
8579     operand = gen_comp_operand(operand, opc, lhs_ili, rhs_ili,
8580                                ILI_OPND(ilix, 3), CMP_INT | CMP_USG, I_ICMP);
8581     break;
8582   case IL_KCMP:
8583   case IL_ICMP:
8584     lhs_ili = ILI_OPND(ilix, 1);
8585     rhs_ili = ILI_OPND(ilix, 2);
8586     operand = gen_comp_operand(operand, opc, lhs_ili, rhs_ili,
8587                                ILI_OPND(ilix, 3), CMP_INT, I_ICMP);
8588     break;
8589   case IL_AIMV:
8590     operand = gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_CPTR));
8591     if (expected_type == NULL)
8592       expected_type = make_lltype_from_dtype(DT_INT);
8593     break;
8594   case IL_AKMV:
8595     operand = gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_CPTR));
8596     if (expected_type == NULL)
8597       expected_type = make_lltype_from_dtype(DT_INT8);
8598     break;
8599   case IL_KIMV:
8600     operand = gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_INT));
8601     break;
8602   case IL_IKMV:
8603     operand = gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_INT));
8604     operand = sign_extend_int(operand, 64);
8605     break;
8606   case IL_UIKMV:
8607     operand = gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_UINT));
8608     operand = zero_extend_int(operand, 64);
8609     break;
8610   case IL_IAMV:
8611     operand = gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_INT));
8612     /* This ILI is sometimes generated on 64-bit targets. Make sure it is
8613      * sign-extended, the LLVM inttoptr instruction zero-extends. */
8614     if (size_of(DT_CPTR) == 8)
8615       operand = sign_extend_int(operand, 64);
8616     break;
8617 
8618   case IL_KAMV:
8619     operand = gen_llvm_expr(ILI_OPND(ilix, 1), make_int_lltype(64));
8620 #if TARGET_PTRSIZE < 8
8621     /* Explicitly truncate to a 32-bit int - convert_int_to_ptr() won't work
8622      * because it can't truncate. */
8623     operand =
8624         convert_int_size(ilix, operand, make_int_lltype(8 * TARGET_PTRSIZE));
8625 #endif
8626     break;
8627 
8628     operand = gen_llvm_expr(ILI_OPND(ilix, 1), expected_type);
8629     break;
8630 #ifdef IL_DFRSPX87
8631   case IL_FREESPX87:
8632     cse_opc = true;
8633   case IL_DFRSPX87:
8634     if (expected_type == NULL)
8635       expected_type = make_lltype_from_dtype(DT_FLOAT);
8636     goto _process_define_ili;
8637   case IL_FREEDPX87:
8638     cse_opc = true;
8639   case IL_DFRDPX87:
8640     if (expected_type == NULL)
8641       expected_type = make_lltype_from_dtype(DT_DBLE);
8642     goto _process_define_ili;
8643 #endif
8644   case IL_FREEKR:
8645     cse_opc = true;
8646   case IL_DFRKR:
8647     if (expected_type == NULL) {
8648       expected_type = make_lltype_from_dtype(DT_INT8);
8649     }
8650     goto _process_define_ili;
8651   case IL_FREEIR:
8652     cse_opc = true;
8653   case IL_DFRIR:
8654     if (expected_type == NULL) {
8655       expected_type = make_lltype_from_dtype(DT_INT);
8656     }
8657     goto _process_define_ili;
8658   case IL_FREESP:
8659     cse_opc = true;
8660   case IL_DFRSP:
8661     if (expected_type == NULL)
8662       expected_type = make_lltype_from_dtype(DT_FLOAT);
8663     goto _process_define_ili;
8664   case IL_FREEDP:
8665     cse_opc = true;
8666   case IL_DFRDP:
8667     if (expected_type == NULL)
8668       expected_type = make_lltype_from_dtype(DT_DBLE);
8669     goto _process_define_ili;
8670   case IL_DFR128:
8671     if (expected_type == NULL)
8672       expected_type = make_lltype_from_dtype(DT_128);
8673     goto _process_define_ili;
8674   case IL_DFR256:
8675     if (expected_type == NULL)
8676       expected_type = make_lltype_from_dtype(DT_256);
8677     goto _process_define_ili;
8678 #ifdef LONG_DOUBLE_FLOAT128
8679   case IL_FLOAT128FREE:
8680     cse_opc = 1;
8681   /* FALLTHROUGH */
8682   case IL_FLOAT128RESULT:
8683     if (expected_type == NULL)
8684       expected_type = make_lltype_from_dtype(DT_FLOAT128);
8685     goto _process_define_ili;
8686 #endif
8687   case IL_FREEAR:
8688     cse_opc = true;
8689   case IL_DFRAR:
8690     if (expected_type == NULL)
8691       expected_type = make_lltype_from_dtype(DT_CPTR);
8692     goto _process_define_ili;
8693   case IL_FREECS:
8694     cse_opc = true;
8695   case IL_DFRCS:
8696     if (expected_type == NULL)
8697       expected_type = make_lltype_from_dtype(DT_CMPLX);
8698     goto _process_define_ili;
8699   case IL_FREECD:
8700     cse_opc = true;
8701   case IL_DFRCD:
8702     if (expected_type == NULL)
8703       expected_type = make_lltype_from_dtype(DT_DCMPLX);
8704 
8705   _process_define_ili:
8706     /* llvm_info.curr_ret_ili = ilix; */
8707     llvm_info.curr_ret_dtype = cse_opc ? DT_NONE : dtype_from_return_type(opc);
8708     switch (ILI_OPC(ILI_OPND(ilix, 1))) {
8709 
8710 #ifdef PGPLUS
8711     case IL_JSRA:
8712 #endif
8713     case IL_QJSR:
8714     case IL_JSR:
8715     case IL_GJSR:
8716       /*
8717        * For compiler-created functions, its DTYPE record is
8718        * believable if its dtype value is not a 'predeclared,
8719        * e.g., DT_IFUNC.
8720        */
8721       if (CCSYMG(ILI_OPND(ILI_OPND(ilix, 1), 1)) &&
8722           DTYPEG(ILI_OPND(ILI_OPND(ilix, 1), 1)) < DT_MAX) {
8723         update_return_type_for_ccfunc(ILI_OPND(ilix, 1), opc);
8724       }
8725     }
8726 
8727     if (is_complex_result(ilix)) {
8728       comp_exp_type = expected_type;
8729       expected_type = complex_result_type(ILI_OPC(ilix));
8730     }
8731 
8732     /* Identical calls in the same block must be csed for correctness,
8733      * identical calls that are supposed to be repeated are given different
8734      * ILI numbers.
8735      *
8736      * Don't cse QJSR/GJSR calls. They are hashed as other instructions, so
8737      * cse'ing could inadvertently move loads across stores. See
8738      * pgc_correctll/gf40.c on an architecture that calls __mth_i_floatk
8739      * with QJSR.
8740      */
8741     if (ILI_OPC(ILI_OPND(ilix, 1)) == IL_QJSR ||
8742         ILI_OPC(ILI_OPND(ilix, 1)) == IL_GJSR) {
8743       operand = gen_llvm_expr(ILI_OPND(ilix, 1), expected_type);
8744     } else {
8745       OPERAND **csed_operand = get_csed_operand(ILI_OPND(ilix, 1));
8746 
8747       if (csed_operand == NULL) {
8748         operand = gen_llvm_expr(ILI_OPND(ilix, 1), expected_type);
8749         add_to_cselist(ILI_OPND(ilix, 1));
8750         csed_operand = get_csed_operand(ILI_OPND(ilix, 1));
8751         set_csed_operand(csed_operand, operand);
8752       } else if (!ILI_COUNT(ILI_OPND(ilix, 1))) {
8753         operand = gen_llvm_expr(ILI_OPND(ilix, 1), expected_type);
8754       } else if (*csed_operand == NULL) {
8755         operand = gen_llvm_expr(ILI_OPND(ilix, 1), expected_type);
8756         set_csed_operand(csed_operand, operand);
8757       } else {
8758         operand = gen_copy_op(*csed_operand);
8759       }
8760       assert(operand, "null operand in cse list for ilix ", ILI_OPND(ilix, 1),
8761              ERR_Fatal);
8762     }
8763     if (is_complex_result(ilix)) {
8764       operand = gen_extract_value_ll(operand, expected_type, comp_exp_type,
8765                                      (ILI_OPND(ilix, 2) == 'r') ? 0 : 1);
8766       expected_type = comp_exp_type;
8767     }
8768     break;
8769   case IL_FREE:
8770     if (expected_type == NULL)
8771       expected_type = make_lltype_from_dtype(ILI_DTyOPND(ilix, 2));
8772     goto _process_define_ili;
8773 
8774   case IL_VCVTV:
8775     operand = gen_convert_vector(ilix);
8776     break;
8777 
8778   case IL_VCVTR:
8779     operand = gen_bitcast_vector(ilix);
8780     break;
8781 
8782   case IL_VCVTS:
8783     operand = gen_scalar_to_vector(
8784         ilix, make_lltype_from_dtype(ILI_DTyOPND(ilix, 2)));
8785     break;
8786 
8787   case IL_VNOT:
8788   case IL_NOT:
8789   case IL_UNOT:
8790   case IL_KNOT:
8791   case IL_UKNOT:
8792     operand = gen_binary_expr(ilix, I_XOR);
8793     if (opc == IL_VNOT && (ILI_OPC(ILI_OPND(ilix, 1)) == IL_VCMP ||
8794                            ILI_OPC(ILI_OPND(ilix, 1)) == IL_VPERMUTE))
8795       expected_type = operand->ll_type;
8796     break;
8797   case IL_VNEG:
8798     operand = gen_binary_vexpr(ilix, I_SUB, I_SUB, I_FSUB);
8799     break;
8800   case IL_INEG:
8801   case IL_UINEG:
8802   case IL_KNEG:
8803   case IL_UKNEG:
8804     operand = gen_binary_expr(ilix, I_SUB);
8805     break;
8806   case IL_DNEG:
8807   case IL_FNEG:
8808 #ifdef LONG_DOUBLE_FLOAT128
8809   case IL_FLOAT128CHS:
8810 #endif
8811     operand = gen_binary_expr(ilix, I_FSUB);
8812     break;
8813   case IL_SCMPLXNEG:
8814   case IL_DCMPLXNEG: {
8815     OPERAND *res, *op_rneg, *op_ineg, *c1, *cse1;
8816     LL_Type *cmplx_ty, *cmpnt_ty;
8817     const DTYPE dt = opc == IL_SCMPLXNEG ? DT_CMPLX : DT_DCMPLX;
8818     const DTYPE et = opc == IL_SCMPLXNEG ? DT_FLOAT : DT_DBLE;
8819 
8820     cmpnt_ty = make_lltype_from_dtype(dt == DT_CMPLX ? DT_FLOAT : DT_DBLE);
8821 
8822     c1 = gen_eval_cmplx_value(ILI_OPND(ilix, 1), dt);
8823     cse1 = gen_copy_operand(c1);
8824 
8825     /* real = 0 - real */
8826     op_rneg = make_constval_op(cmpnt_ty, 0, 0);
8827     op_rneg->next = gen_extract_value(c1, dt, et, 0);
8828     op_rneg =
8829         ad_csed_instr(I_FSUB, 0, cmpnt_ty, op_rneg, InstrListFlagsNull, true);
8830 
8831     /* imag = 0 - imag */
8832     op_ineg = make_constval_op(cmpnt_ty, 0, 0);
8833     op_ineg->next = gen_extract_value(cse1, dt, et, 1);
8834     op_ineg =
8835         ad_csed_instr(I_FSUB, 0, cmpnt_ty, op_ineg, InstrListFlagsNull, true);
8836 
8837     /* {real, imag} */
8838     res = make_undef_op(make_lltype_from_dtype(dt));
8839     res = gen_insert_value(res, op_rneg, 0);
8840     operand = gen_insert_value(res, op_ineg, 1);
8841   } break;
8842   case IL_CSE:
8843   case IL_CSEKR:
8844   case IL_CSEIR:
8845   case IL_CSESP:
8846   case IL_CSEDP:
8847   case IL_CSEAR:
8848   case IL_CSECS:
8849   case IL_CSECD:
8850 #ifdef LONG_DOUBLE_FLOAT128
8851   case IL_FLOAT128CSE:
8852 #endif
8853   {
8854     int csed_ilix;
8855     OPERAND **csed_operand;
8856 
8857     csed_ilix = ILI_OPND(ilix, 1);
8858     if (ILI_ALT(csed_ilix))
8859       csed_ilix = ILI_ALT(csed_ilix);
8860     csed_operand = get_csed_operand(csed_ilix);
8861 
8862     assert(csed_operand, "missing cse operand list for ilix ", csed_ilix,
8863            ERR_Fatal);
8864     if (!ILI_COUNT(csed_ilix)) {
8865       operand = gen_llvm_expr(csed_ilix, expected_type);
8866     } else {
8867       operand = gen_copy_op(*csed_operand);
8868     }
8869     assert(operand, "null operand in cse list for ilix ", csed_ilix, ERR_Fatal);
8870   } break;
8871   case IL_IR2SP:
8872     operand = make_bitcast(gen_llvm_expr(ILI_OPND(ilix, 1), 0),
8873                            make_lltype_from_dtype(DT_REAL));
8874     break;
8875   case IL_KR2DP:
8876     operand = make_bitcast(gen_llvm_expr(ILI_OPND(ilix, 1), 0),
8877                            make_lltype_from_dtype(DT_DBLE));
8878     break;
8879   /* these next ILI are currently generated by idiom recognition within
8880    * induc, and as arguments to our __c_mset* routines we want them treated
8881    * as integer bits without conversion.
8882    */
8883   case IL_SP2IR:
8884     operand = make_bitcast(gen_llvm_expr(ILI_OPND(ilix, 1), 0),
8885                            make_lltype_from_dtype(DT_INT));
8886     break;
8887   case IL_DP2KR:
8888     operand = make_bitcast(gen_llvm_expr(ILI_OPND(ilix, 1), 0),
8889                            make_lltype_from_dtype(DT_INT8));
8890     break;
8891   case IL_CS2KR:
8892     comp_exp_type = make_lltype_from_dtype(DT_CMPLX);
8893     cc_op1 = gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_CMPLX));
8894 
8895     /* ILI_OPND(ilix, 1) can be expression */
8896     if (ILTY_CONS == IL_TYPE(ILI_OPC(ILI_OPND(ilix, 1)))) {
8897       cc_op2 = make_var_op(ILI_SymOPND(ILI_OPND(ilix, 1), 1));
8898     } else {
8899       assert(0, "gen_llvm_expr(): unsupport operand for CS2KR ", opc,
8900              ERR_Fatal);
8901       /* it is not worth it to do it */
8902     }
8903 
8904     operand =
8905         make_bitcast(cc_op2, make_ptr_lltype(make_lltype_from_dtype(DT_INT8)));
8906     operand = make_load(ilix, operand, operand->ll_type->sub_types[0], (MSZ)-2,
8907                         ldst_instr_flags_from_dtype(DT_INT8));
8908     break;
8909   case IL_SCMPLX2REAL:
8910     dt = DT_CMPLX;
8911     cmpnt = DT_NONE;
8912     goto component;
8913   case IL_DCMPLX2REAL:
8914     dt = DT_DCMPLX;
8915     cmpnt = DT_NONE;
8916     goto component;
8917   case IL_SCMPLX2IMAG:
8918     dt = DT_CMPLX;
8919     cmpnt = (DTYPE)1;
8920     goto component;
8921   case IL_DCMPLX2IMAG:
8922     dt = DT_DCMPLX;
8923     cmpnt = (DTYPE)1;
8924     goto component;
8925   component:
8926     c1 = gen_eval_cmplx_value(ILI_OPND(ilix, 1), dt);
8927     operand =
8928         gen_extract_value(c1, dt, dt == DT_CMPLX ? DT_FLOAT : DT_DBLE, cmpnt);
8929     break;
8930   case IL_SPSP2SCMPLX:
8931   case IL_DPDP2DCMPLX: {
8932     LL_Type *dt, *et;
8933     if (opc == IL_SPSP2SCMPLX) {
8934       dt = make_lltype_from_dtype(DT_CMPLX);
8935       et = make_lltype_from_dtype(DT_FLOAT);
8936     } else {
8937       dt = make_lltype_from_dtype(DT_DCMPLX);
8938       et = make_lltype_from_dtype(DT_DBLE);
8939     }
8940     cc_op1 = gen_llvm_expr(ILI_OPND(ilix, 1), et);
8941     cc_op2 = gen_llvm_expr(ILI_OPND(ilix, 2), et);
8942     operand = make_undef_op(dt);
8943     operand = gen_insert_value(operand, cc_op1, 0);
8944     operand = gen_insert_value(operand, cc_op2, 1);
8945   } break;
8946   case IL_SPSP2SCMPLXI0:
8947     dt = DT_CMPLX;
8948     cmpnt = DT_FLOAT;
8949     goto component_zero;
8950   case IL_DPDP2DCMPLXI0:
8951     dt = DT_DCMPLX;
8952     cmpnt = DT_DBLE;
8953     goto component_zero;
8954   component_zero: /* Set imaginary value to 0 */
8955     cc_op1 = gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(cmpnt));
8956     cc_op2 = make_constval_op(make_lltype_from_dtype(cmpnt), 0, 0);
8957     operand = make_undef_op(make_lltype_from_dtype(dt));
8958     operand = gen_insert_value(operand, cc_op1, 0);
8959     operand = gen_insert_value(operand, cc_op2, 1);
8960     break;
8961   case IL_SCMPLXCONJG:
8962     dt = DT_CMPLX;
8963     cmpnt = DT_FLOAT;
8964     goto cmplx_conj;
8965   case IL_DCMPLXCONJG:
8966     dt = DT_DCMPLX;
8967     cmpnt = DT_DBLE;
8968     goto cmplx_conj;
8969   cmplx_conj:
8970     /* result = {real , 0 - imag} */
8971     c1 = gen_eval_cmplx_value(ILI_OPND(ilix, 1), dt);
8972     cse1 = gen_copy_operand(c1);
8973     cc_op1 = gen_extract_value(c1, dt, cmpnt, 1);
8974     cc_op2 = make_constval_op(make_lltype_from_dtype(cmpnt), 0, 0);
8975     cc_op2->next = cc_op1;
8976     cc_op2 = ad_csed_instr(I_FSUB, 0, make_lltype_from_dtype(cmpnt), cc_op2,
8977                            InstrListFlagsNull, true);
8978     cc_op1 = gen_extract_value(cse1, dt, cmpnt, 0);
8979     operand = make_undef_op(make_lltype_from_dtype(dt));
8980     operand = gen_insert_value(operand, cc_op1, 0);
8981     operand = gen_insert_value(operand, cc_op2, 1);
8982     break;
8983   case IL_FABS:
8984     operand = gen_call_llvm_intrinsic(
8985         "fabs.f32",
8986         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
8987         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
8988     break;
8989   case IL_VABS:
8990   case IL_VFLOOR:
8991   case IL_VCEIL:
8992   case IL_VAINT:
8993   case IL_VSQRT:
8994     intrinsic_name = vect_llvm_intrinsic_name(ilix);
8995     intrinsic_type = make_lltype_from_dtype(ili_get_vect_dtype(ilix));
8996     operand = gen_call_llvm_intrinsic(
8997         intrinsic_name, gen_llvm_expr(ILI_OPND(ilix, 1), intrinsic_type),
8998         make_lltype_from_dtype(ili_get_vect_dtype(ilix)), NULL, I_PICALL);
8999     break;
9000   case IL_VRSQRT: {
9001     int vsize;
9002     const int arg = ILI_OPND(ilix, 1);
9003     dtype = ili_get_vect_dtype(ilix); /* get the vector dtype */
9004     intrinsic_type = make_lltype_from_dtype(dtype);
9005     assert(TY_ISVECT(DTY(dtype)), "gen_llvm_expr(): expected vect type",
9006            DTY(dtype), ERR_Fatal);
9007     vsize = DTyVecLength(dtype);
9008 #if defined(TARGET_LLVM_POWER)
9009     operand = gen_llvm_expr(arg, intrinsic_type);
9010     intrinsic_name = "ppc.vsx.xvrsqrtesp";
9011 #elif defined(TARGET_LLVM_X8632) || defined(TARGET_LLVM_X8664)
9012     if (vsize == 4) {
9013       operand = gen_llvm_expr(arg, intrinsic_type);
9014       intrinsic_name = "x86.sse.rsqrt.ps";
9015     } else if (vsize == 8) {
9016       operand = gen_llvm_expr(arg, intrinsic_type);
9017       intrinsic_name = "x86.avx.rsqrt.ps.256";
9018     } else if (vsize == 16) {
9019       LL_Type *i16Ty = ll_create_int_type(cpu_llvm_module, 16);
9020       OPERAND *op3 = gen_llvm_expr(ad_icon(~0), i16Ty);
9021       OPERAND *op2 = gen_llvm_expr(arg, intrinsic_type);
9022       operand = gen_copy_op(op2);
9023       operand->next = op2;
9024       op2->next = op3;
9025       intrinsic_name = "x86.avx512.rsqrt14.ps.512";
9026       // Xeon Phi also supports 28 bit precision
9027     } else {
9028       assert(false, "gen_llvm_expr(): unexpected vector size", vsize,
9029              ERR_Fatal);
9030     }
9031 #else
9032     assert(false, "gen_llvm_expr(): unsupported target", vsize, ERR_Fatal);
9033 #endif
9034     operand = gen_call_llvm_intrinsic(intrinsic_name, operand, intrinsic_type,
9035                                       NULL, I_PICALL);
9036   } break;
9037   case IL_VRCP: {
9038     int vsize;
9039     const int arg = ILI_OPND(ilix, 1);
9040     dtype = ili_get_vect_dtype(ilix); /* get the vector dtype */
9041     intrinsic_type = make_lltype_from_dtype(dtype);
9042     assert(TY_ISVECT(DTY(dtype)), "gen_llvm_expr(): expected vect type",
9043            DTY(dtype), ERR_Fatal);
9044     vsize = DTyVecLength(dtype);
9045 #if defined(TARGET_LLVM_POWER)
9046     operand = gen_llvm_expr(arg, intrinsic_type);
9047     intrinsic_name = "ppc.vsx.xvresp";
9048 #elif defined(TARGET_LLVM_X8632) || defined(TARGET_LLVM_X8664)
9049     if (vsize == 4) {
9050       operand = gen_llvm_expr(arg, intrinsic_type);
9051       intrinsic_name = "x86.sse.rcp.ps";
9052     } else if (vsize == 8) {
9053       operand = gen_llvm_expr(arg, intrinsic_type);
9054       intrinsic_name = "x86.avx.rcp.ps.256";
9055     } else if (vsize == 16) {
9056       LL_Type *i16Ty = ll_create_int_type(cpu_llvm_module, 16);
9057       OPERAND *op3 = gen_llvm_expr(ad_icon(~0), i16Ty);
9058       OPERAND *op2 = gen_llvm_expr(arg, intrinsic_type);
9059       operand = gen_copy_op(op2);
9060       operand->next = op2;
9061       op2->next = op3;
9062       intrinsic_name = "x86.avx512.rcp14.ps.512";
9063       // Xeon Phi also supports 28 bit precision
9064     } else {
9065       assert(false, "gen_llvm_expr(): unexpected vector size", vsize,
9066              ERR_Fatal);
9067     }
9068 #else
9069     assert(false, "gen_llvm_expr(): unsupported target", vsize, ERR_Fatal);
9070 #endif
9071     operand = gen_call_llvm_intrinsic(intrinsic_name, operand, intrinsic_type,
9072                                       NULL, I_PICALL);
9073   } break;
9074   case IL_VFMA1:
9075   case IL_VFMA2:
9076   case IL_VFMA3:
9077   case IL_VFMA4:
9078     intrinsic_name = vect_llvm_intrinsic_name(ilix);
9079     intrinsic_type = make_lltype_from_dtype(ili_get_vect_dtype(ilix));
9080     args = gen_llvm_expr(ILI_OPND(ilix, 1), intrinsic_type);
9081     args->next = gen_llvm_expr(ILI_OPND(ilix, 2), intrinsic_type);
9082     args->next->next = gen_llvm_expr(ILI_OPND(ilix, 3), intrinsic_type);
9083     operand = gen_call_llvm_intrinsic(intrinsic_name, args, intrinsic_type,
9084                                       NULL, I_PICALL);
9085     break;
9086   case IL_VSINCOS:
9087     operand = gen_llvm_vsincos_builtin(ilix);
9088     break;
9089   case IL_VCOS:
9090   case IL_VSIN: {
9091     LL_Type *vecTy = make_lltype_from_dtype(ili_get_vect_dtype(ilix));
9092     if (ILI_OPC(ILI_OPND(ilix, 1)) == IL_VSINCOS) {
9093       // overloaded use: this is an extract value operation
9094       LL_Type *retTy = gen_vsincos_return_type(vecTy);
9095       OPERAND *op = gen_copy_op(gen_llvm_expr(ILI_OPND(ilix, 1), retTy));
9096       operand = gen_llvm_select_vsincos(op, vecTy, retTy, (opc == IL_VCOS));
9097     } else {
9098       // standard call to vector sin (or vector cos)
9099       OPERAND *opnd = gen_llvm_expr(ILI_OPND(ilix, 1), vecTy);
9100       char *name = vect_llvm_intrinsic_name(ilix);
9101       operand = gen_call_llvm_intrinsic(name, opnd, vecTy, NULL, I_PICALL);
9102     }
9103   } break;
9104   case IL_DABS:
9105     operand = gen_call_llvm_intrinsic(
9106         "fabs.f64",
9107         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9108         make_lltype_from_dtype(DT_DBLE), NULL, I_PICALL);
9109     break;
9110   case IL_IABS:
9111   case IL_KABS:
9112     operand = gen_abs_expr(ilix);
9113     break;
9114   case IL_FFLOOR:
9115     operand = gen_call_llvm_intrinsic(
9116         "floor.f32",
9117         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9118         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9119     break;
9120   case IL_DFLOOR:
9121     operand = gen_call_llvm_intrinsic(
9122         "floor.f64",
9123         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9124         make_lltype_from_dtype(DT_DBLE), NULL, I_PICALL);
9125     break;
9126   case IL_FCEIL:
9127     operand = gen_call_llvm_intrinsic(
9128         "ceil.f32",
9129         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9130         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9131     break;
9132   case IL_DCEIL:
9133     operand = gen_call_llvm_intrinsic(
9134         "ceil.f64",
9135         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9136         make_lltype_from_dtype(DT_DBLE), NULL, I_PICALL);
9137     break;
9138   case IL_AINT:
9139     operand = gen_call_llvm_intrinsic(
9140         "trunc.f32",
9141         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9142         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9143     break;
9144   case IL_DINT:
9145     operand = gen_call_llvm_intrinsic(
9146         "trunc.f64",
9147         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9148         make_lltype_from_dtype(DT_DBLE), NULL, I_PICALL);
9149     break;
9150   case IL_IMIN:
9151   case IL_UIMIN:
9152   case IL_KMIN:
9153   case IL_UKMIN:
9154   case IL_FMIN:
9155   case IL_DMIN:
9156   case IL_IMAX:
9157   case IL_UIMAX:
9158   case IL_KMAX:
9159   case IL_UKMAX:
9160   case IL_FMAX:
9161   case IL_DMAX: {
9162     LL_Type *llTy;
9163     lhs_ili = ILI_OPND(ilix, 2);
9164     rhs_ili = ILI_OPND(ilix, 1);
9165     llTy = make_type_from_opc(opc);
9166     operand = gen_minmax_expr(ilix, gen_llvm_expr(lhs_ili, llTy),
9167                               gen_llvm_expr(rhs_ili, llTy));
9168   } break;
9169   case IL_VMIN:
9170   case IL_VMAX: {
9171     DTYPE vect_dtype = ili_get_vect_dtype(ilix);
9172     OPERAND *op1, *op2;
9173     LL_Type *llTy;
9174     lhs_ili = ILI_OPND(ilix, 2);
9175     rhs_ili = ILI_OPND(ilix, 1);
9176     llTy = make_lltype_from_dtype(vect_dtype);
9177     op1 = gen_llvm_expr(lhs_ili, llTy);
9178     op2 = gen_llvm_expr(rhs_ili, llTy);
9179 #if defined(TARGET_LLVM_POWER)
9180     if ((operand = gen_call_vminmax_power_intrinsic(ilix, op1, op2)) == NULL) {
9181       operand = gen_minmax_expr(ilix, op1, op2);
9182     }
9183 #elif defined(TARGET_LLVM_ARM) && NEON_ENABLED
9184     if ((operand = gen_call_vminmax_neon_intrinsic(ilix, op1, op2)) == NULL) {
9185       operand = gen_minmax_expr(ilix, op1, op2);
9186     }
9187 #else
9188     if ((operand = gen_call_vminmax_intrinsic(ilix, op1, op2)) == NULL) {
9189       operand = gen_minmax_expr(ilix, op1, op2);
9190     }
9191 #endif
9192   } break;
9193   case IL_ISELECT:
9194   case IL_KSELECT:
9195   case IL_ASELECT:
9196   case IL_FSELECT:
9197   case IL_DSELECT:
9198   case IL_CSSELECT:
9199   case IL_CDSELECT:
9200     operand = gen_select_expr(ilix);
9201     break;
9202   case IL_FSQRT:
9203     operand = gen_call_llvm_intrinsic(
9204         "sqrt.f32",
9205         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9206         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9207     break;
9208   case IL_DSQRT:
9209     operand = gen_call_llvm_intrinsic(
9210         "sqrt.f64",
9211         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9212         make_lltype_from_dtype(DT_DBLE), NULL, I_PICALL);
9213     break;
9214   case IL_FLOG:
9215     operand = gen_call_llvm_intrinsic(
9216         "log.f32",
9217         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9218         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9219     break;
9220   case IL_DLOG:
9221     operand = gen_call_llvm_intrinsic(
9222         "log.f64",
9223         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9224         make_lltype_from_dtype(DT_DBLE), NULL, I_PICALL);
9225     break;
9226   case IL_FLOG10:
9227     operand = gen_call_llvm_intrinsic(
9228         "log10.f32",
9229         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9230         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9231     break;
9232   case IL_DLOG10:
9233     operand = gen_call_llvm_intrinsic(
9234         "log10.f64",
9235         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9236         make_lltype_from_dtype(DT_DBLE), NULL, I_PICALL);
9237     break;
9238   case IL_FSIN:
9239     operand = gen_call_llvm_intrinsic(
9240         "sin.f32",
9241         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9242         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9243     break;
9244   case IL_DSIN:
9245     operand = gen_call_llvm_intrinsic(
9246         "sin.f64",
9247         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9248         make_lltype_from_dtype(DT_DBLE), NULL, I_PICALL);
9249     break;
9250   case IL_FTAN:
9251     operand = gen_call_pgocl_intrinsic(
9252         "tan_f",
9253         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9254         make_lltype_from_dtype(DT_FLOAT), NULL, I_CALL);
9255     break;
9256   case IL_DTAN:
9257     operand = gen_call_pgocl_intrinsic(
9258         "tan_d",
9259         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9260         make_lltype_from_dtype(DT_DBLE), NULL, I_CALL);
9261     break;
9262   case IL_FPOWF:
9263     operand =
9264         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT));
9265     operand->next =
9266         gen_llvm_expr(ILI_OPND(ilix, 2), make_lltype_from_dtype(DT_FLOAT));
9267     operand = gen_call_pgocl_intrinsic(
9268         "pow_f", operand, make_lltype_from_dtype(DT_FLOAT), NULL, I_CALL);
9269     break;
9270   case IL_DPOWD:
9271     operand = gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE));
9272     operand->next =
9273         gen_llvm_expr(ILI_OPND(ilix, 2), make_lltype_from_dtype(DT_DBLE));
9274     operand = gen_call_pgocl_intrinsic(
9275         "pow_d", operand, make_lltype_from_dtype(DT_DBLE), NULL, I_CALL);
9276     break;
9277   case IL_DPOWI:
9278     // TODO: won't work because our builtins expect args in registers (xxm0 in
9279     // this case) and the call generated here (with llc) puts the args on the
9280     // stack
9281     assert(ILI_ALT(ilix), "gen_llvm_expr: missing ILI_ALT field for DPOWI ili",
9282            ilix, ERR_Fatal);
9283     operand = gen_llvm_expr(ilix, make_lltype_from_dtype(DT_DBLE));
9284     break;
9285   case IL_FCOS:
9286     operand = gen_call_llvm_intrinsic(
9287         "cos.f32",
9288         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9289         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9290     break;
9291   case IL_DCOS:
9292     operand = gen_call_llvm_intrinsic(
9293         "cos.f64",
9294         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9295         make_lltype_from_dtype(DT_DBLE), NULL, I_PICALL);
9296     break;
9297   case IL_FEXP:
9298     operand = gen_call_llvm_intrinsic(
9299         "exp.f32",
9300         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9301         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9302     break;
9303   case IL_DEXP:
9304     operand = gen_call_llvm_intrinsic(
9305         "exp.f64",
9306         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_DBLE)),
9307         make_lltype_from_dtype(DT_DBLE), NULL, I_PICALL);
9308     break;
9309   case IL_FAND: {
9310     /* bitwise logical AND op. operand has floating-point type
9311        %copnd1 = bitcast float %opnd1 to iX
9312        %copnd2 = bitcast float %opnd1 to iX
9313        %crslt = and iX %copnd1, %copnd2
9314        %result = bitcast iX %crslt to float
9315     */
9316     OPERAND *op3, *op4, *op5, *op6;
9317     INSTR_LIST *instr2, *instr3;
9318     unsigned bits = 8 * size_of(DT_FLOAT);
9319     LL_Type *iTy = make_int_lltype(bits);
9320     LL_Type *fltTy = make_lltype_from_dtype(DT_FLOAT);
9321     OPERAND *op1 = gen_llvm_expr(ILI_OPND(ilix, 1), NULL);
9322     OPERAND *op2 = make_tmp_op(iTy, make_tmps());
9323     INSTR_LIST *instr1 = gen_instr(I_BITCAST, op2->tmps, iTy, op1);
9324     ad_instr(ilix, instr1);
9325     op3 = gen_llvm_expr(ILI_OPND(ilix, 2), NULL);
9326     op4 = make_tmp_op(iTy, make_tmps());
9327     instr2 = gen_instr(I_BITCAST, op4->tmps, iTy, op3);
9328     ad_instr(ilix, instr2);
9329     op6 = make_tmp_op(fltTy, make_tmps());
9330     op2->next = op4;
9331     op5 = ad_csed_instr(I_AND, 0, iTy, op2, InstrListFlagsNull, false);
9332     instr3 = gen_instr(I_BITCAST, op6->tmps, fltTy, op5);
9333     ad_instr(ilix, instr3);
9334     operand = op6;
9335   } break;
9336   case IL_RSQRTSS:
9337 #if defined(TARGET_LLVM_POWER)
9338     operand = gen_call_llvm_intrinsic(
9339         "ppc.vsx.xsrsqrtesp",
9340         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9341         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9342 #endif
9343 #if defined(TARGET_LLVM_X8632) || defined(TARGET_LLVM_X8664)
9344     {
9345       /* intrinsic has type <4 x float> -> <4 x float>, so need to build
9346          and extract from vectors */
9347       const char *nm = "x86.sse.rsqrt.ss";
9348       LL_Type *vTy = make_vtype(DT_FLOAT, 4);
9349       OPERAND *op1 = gen_scalar_to_vector_no_shuffle(ilix, vTy);
9350       OPERAND *op2 = gen_call_llvm_intrinsic(nm, op1, vTy, NULL, I_PICALL);
9351       operand = gen_extract_vector(op2, 0);
9352     }
9353 #endif
9354     break;
9355   case IL_RCPSS:
9356 #if defined(TARGET_LLVM_POWER)
9357     operand = gen_call_llvm_intrinsic(
9358         "ppc.vsx.xsresp",
9359         gen_llvm_expr(ILI_OPND(ilix, 1), make_lltype_from_dtype(DT_FLOAT)),
9360         make_lltype_from_dtype(DT_FLOAT), NULL, I_PICALL);
9361 #endif
9362 #if defined(TARGET_LLVM_X8632) || defined(TARGET_LLVM_X8664)
9363     {
9364       /* intrinsic has type <4 x float> -> <4 x float>, so need to build
9365          and extract from vectors */
9366       const char *nm = "x86.sse.rcp.ss";
9367       LL_Type *vTy = make_vtype(DT_FLOAT, 4);
9368       OPERAND *op1 = gen_scalar_to_vector(ilix, vTy);
9369       OPERAND *op2 = gen_call_llvm_intrinsic(nm, op1, vTy, NULL, I_PICALL);
9370       operand = gen_extract_vector(op2, 0);
9371     }
9372 #endif
9373     break;
9374   case IL_VPERMUTE: {
9375     OPERAND *op1;
9376     OPERAND *mask_op;
9377     LL_Type *vect_lltype, *int_type, *op_lltype;
9378     DTYPE vect_dtype = ili_get_vect_dtype(ilix);
9379     int mask_ili, num_elem;
9380     int edtype;
9381     unsigned long long undef_mask = 0;
9382 
9383     /* LLVM shufflevector instruction has a mask whose selector takes
9384      * the concatenation of two vectors and numbers the elements as
9385      * 0,1,2,3,... from left to right.
9386      */
9387 
9388     vect_lltype = make_lltype_from_dtype(vect_dtype);
9389     lhs_ili = ILI_OPND(ilix, 1);
9390     rhs_ili = ILI_OPND(ilix, 2);
9391     mask_ili = ILI_OPND(ilix, 3);
9392     op_lltype = make_lltype_from_dtype(ili_get_vect_dtype(lhs_ili));
9393 
9394     op1 = gen_llvm_expr(lhs_ili, op_lltype);
9395     if (ILI_OPC(rhs_ili) == IL_NULL) /* a don't care, generate an undef */
9396       op1->next = make_undef_op(op1->ll_type);
9397     else
9398       op1->next = gen_llvm_expr(rhs_ili, op_lltype);
9399     mask_op = gen_llvm_expr(mask_ili, 0);
9400     edtype = CONVAL1G(mask_op->val.sptr);
9401     // VPERMUTE mask values of -1 are considered llvm undef.
9402     int i;
9403     for(i = mask_op->ll_type->sub_elements - 1; i >= 0; i--) {
9404       undef_mask <<= 1;
9405       if(VCON_CONVAL(edtype + i) == -1) {
9406         undef_mask |= 1;
9407         mask_op->flags |= OPF_CONTAINS_UNDEF;
9408       } else if(VCON_CONVAL(edtype + i) < -1) {
9409         interr("VPERMUTE shuffle mask cannot contain values less than -1.",
9410                ilix, ERR_Severe);
9411       }
9412     }
9413     if(mask_op->flags & OPF_CONTAINS_UNDEF) {
9414       mask_op->val.sptr_undef.sptr = mask_op->val.sptr;
9415       mask_op->val.sptr_undef.undef_mask = undef_mask;
9416     }
9417     op1->next->next = mask_op;
9418     if (vect_lltype->sub_elements != op1->next->next->ll_type->sub_elements) {
9419       assert(0,
9420              "VPERMUTE: result and mask must have the same number of elements.",
9421              vect_lltype->sub_elements, ERR_Severe);
9422     }
9423     operand = ad_csed_instr(I_SHUFFVEC, ilix, vect_lltype, op1,
9424                             InstrListFlagsNull, true);
9425   } break;
9426   case IL_VBLEND: {
9427     int num_elem;
9428     OPERAND *op1;
9429     LL_Type *vect_lltype, *int_type, *select_type, *op1_subtype,
9430         *compare_ll_type;
9431     DTYPE vect_dtype = ili_get_vect_dtype(ilix);
9432     int mask_ili = ILI_OPND(ilix, 1);
9433     lhs_ili = ILI_OPND(ilix, 2);
9434     rhs_ili = ILI_OPND(ilix, 3);
9435 
9436     select_type = 0;
9437     vect_lltype = make_lltype_from_dtype(vect_dtype);
9438     if (ILI_OPC(mask_ili) == IL_VCMP) {
9439       op1 = gen_vect_compare_operand(mask_ili);
9440     } else if (ILI_OPC(mask_ili) == IL_VPERMUTE) {
9441       /* half size predicate */
9442       op1 = gen_llvm_expr(mask_ili, 0);
9443       num_elem = DTyVecLength(vect_dtype);
9444       int_type = make_int_lltype(1);
9445       select_type = ll_get_vector_type(int_type, num_elem);
9446       /* The result of the VPERMUTE will be a shuffle of bit mask values,
9447          so need to set the type correctly. */
9448       op1->ll_type = select_type;
9449     } else {
9450       num_elem = DTyVecLength(vect_dtype);
9451       int_type = make_int_lltype(1);
9452       select_type = ll_get_vector_type(int_type, num_elem);
9453       op1 = gen_llvm_expr(mask_ili, 0);
9454       asrt(op1->ll_type->data_type == LL_VECTOR);
9455       op1_subtype = op1->ll_type->sub_types[0];
9456       /* because DTYPEs do not support i1 bit masks we need to narrow */
9457       if (ll_type_int_bits(op1_subtype) > 0) /* int type */
9458         op1 = convert_int_size(mask_ili, op1, select_type);
9459       else if (ll_type_is_fp(op1_subtype) > 0) /* fp type */
9460       {
9461         INT val[2];
9462         enum LL_BaseDataType bdt = expected_type->sub_types[0]->data_type;
9463         OPERAND *opm;
9464         SPTR vcon1_sptr, constant;
9465         DTYPE vdt;
9466         switch (bdt) {
9467         case LL_FLOAT:
9468           vdt = get_vector_dtype(DT_FLOAT, num_elem);
9469           vcon1_sptr = get_vcon_scalar(0xffffffff, vdt);
9470           break;
9471         case LL_DOUBLE:
9472           vdt = get_vector_dtype(DT_DBLE, num_elem);
9473           val[0] = 0xffffffff;
9474           val[1] = 0xffffffff;
9475           constant = getcon(val, DT_DBLE);
9476           vcon1_sptr = get_vcon_scalar(constant, vdt);
9477           break;
9478         default:
9479           assert(0, "Unexpected basic type for VBLEND mask", bdt, ERR_Fatal);
9480         }
9481         opm = make_operand();
9482         opm->ot_type = OT_CC;
9483         opm->val.cc = LLCCF_OEQ;
9484         opm->tmps = make_tmps();
9485         /* type of the compare is the operands: convert from vect_dtype  */
9486         compare_ll_type = make_lltype_from_dtype(vect_dtype);
9487         opm->ll_type = compare_ll_type;
9488         opm->next = op1;
9489         opm->next->next =
9490             gen_llvm_expr(ad1ili(IL_VCON, vcon1_sptr), compare_ll_type);
9491         op1 = ad_csed_instr(I_FCMP, mask_ili, select_type, opm,
9492                             InstrListFlagsNull, true);
9493       } else
9494         assert(false, "gen_llvm_expr(): bad VCMP type", op1_subtype->data_type,
9495                ERR_Fatal);
9496     }
9497     op1->next = gen_llvm_expr(lhs_ili, vect_lltype);
9498     op1->next->next = gen_llvm_expr(rhs_ili, vect_lltype);
9499     operand = ad_csed_instr(I_SELECT, ilix, vect_lltype, op1,
9500                             InstrListFlagsNull, true);
9501   } break;
9502   case IL_VCMP:
9503     /* VCMP is either to select the value from conditional branch or is
9504      * part of an argument to a masked intrinsic call.
9505      */
9506     operand = gen_vect_compare_operand(ilix);
9507       expected_type = operand->ll_type; /* turn into bit-vector */
9508     break;
9509   case IL_ATOMICRMWI:
9510   case IL_ATOMICRMWA:
9511   case IL_ATOMICRMWKR:
9512     operand = gen_llvm_atomicrmw_expr(ilix);
9513     break;
9514   case IL_CMPXCHG_OLDA:
9515   case IL_CMPXCHG_OLDI:
9516   case IL_CMPXCHG_OLDKR:
9517     operand = gen_llvm_cmpxchg_component(ilix, 0);
9518     break;
9519   case IL_CMPXCHG_SUCCESS:
9520     operand = gen_llvm_cmpxchg_component(ilix, 1);
9521     /* Any widening should do zero-extend, not sign-extend. */
9522     operand->flags |= OPF_ZEXT;
9523     break;
9524   case IL_CMPXCHGA:
9525   case IL_CMPXCHGI:
9526   case IL_CMPXCHGKR:
9527     operand = gen_llvm_cmpxchg(ilix);
9528     break;
9529   case IL_FNSIN:
9530   case IL_DNSIN:
9531   case IL_FNCOS:
9532   case IL_DNCOS: {
9533     DTYPE ety = ((opc == IL_FNSIN) || (opc == IL_FNCOS)) ? DT_FLOAT : DT_DBLE;
9534     LL_Type *argTy = make_lltype_from_dtype(ety);
9535     DTYPE dty = ((opc == IL_FNSIN) || (opc == IL_FNCOS)) ? DT_CMPLX : DT_DCMPLX;
9536     LL_Type *retTy = make_lltype_from_dtype(dty);
9537     const int isCos = (opc == IL_FNCOS) || (opc == IL_DNCOS);
9538     operand = gen_copy_op(gen_llvm_expr(ILI_OPND(ilix, 1), retTy));
9539     operand = gen_llvm_select_sin_or_cos(operand, argTy, retTy, isCos);
9540   } break;
9541   case IL_FSINCOS:
9542   case IL_DSINCOS:
9543     operand = gen_llvm_sincos_builtin(ilix);
9544     break;
9545   default:
9546     DBGTRACE3("### gen_llvm_expr; ilix %d, unknown opcode: %d(%s)\n", ilix, opc,
9547               IL_NAME(opc))
9548     assert(false, "gen_llvm_expr(): unknown opcode", opc, ERR_Fatal);
9549     break;
9550   } /* End of switch(opc) */
9551 
9552   assert(operand, "gen_llvm_expr(): missing operand", ilix, ERR_Fatal);
9553   if (!operand->ll_type) {
9554     DBGTRACE2("# missing type for operand %p (ilix %d)", operand, ilix)
9555     assert(false, "gen_llvm_expr(): missing type", ilix, ERR_Fatal);
9556   }
9557   {
9558     OPERAND **csed_operand = get_csed_operand(ilix);
9559     if (csed_operand != NULL)
9560       set_csed_operand(csed_operand, operand);
9561     if (sincos_seen() && (IL_HAS_FENCE(opc) || (IL_TYPE(opc) == ILTY_PROC)))
9562       sincos_clear_all_args();
9563   }
9564   ILI_COUNT(ilix)++;
9565   if (expected_type) {
9566     LL_Type *tty1, *tty2;
9567     ret_match = match_types(expected_type, operand->ll_type);
9568     switch (ret_match) {
9569     case MATCH_MEM:
9570       if ((operand->ll_type->data_type == LL_PTR) &&
9571           ll_type_int_bits(expected_type)) {
9572         operand = convert_ptr_to_int(operand, expected_type);
9573       } else {
9574         operand = make_bitcast(operand, expected_type);
9575       }
9576       break;
9577     case MATCH_OK:
9578       if ((operand->ll_type->data_type == LL_VECTOR) &&
9579           (expected_type->data_type == LL_VECTOR) &&
9580           (operand->ll_type->sub_types[0] == expected_type->sub_types[0]) &&
9581           (ll_type_bytes(operand->ll_type) != ll_type_bytes(expected_type))) {
9582         operand = gen_resized_vect(operand, expected_type->sub_elements, 0);
9583         break;
9584       }
9585       tty1 = expected_type;
9586       tty2 = operand->ll_type;
9587       ct = 0;
9588       while (tty1->data_type == tty2->data_type) {
9589         if ((tty1->data_type == LL_PTR) || (tty1->data_type == LL_ARRAY)) {
9590           tty1 = tty1->sub_types[0];
9591           tty2 = tty2->sub_types[0];
9592           ct++;
9593         } else {
9594           break;
9595         }
9596       }
9597       if (tty1 != tty2) {
9598         operand = make_bitcast(operand, expected_type);
9599       }
9600       break;
9601     case MATCH_NO:
9602       /* binop1 points to int of different size than instr_type */
9603       operand = convert_mismatched_types(operand, expected_type, ilix);
9604       break;
9605     default:
9606       assert(0, "gen_llvm_expr(): bad match type for operand", ret_match,
9607              ERR_Fatal);
9608     }
9609   }
9610 
9611   DBGDUMPLLTYPE("#returned type: ", operand->ll_type);
9612   DBGTRACEOUT2(" returns operand %p, count %d", operand, ILI_COUNT(ilix));
9613   setTempMap(ilix, operand);
9614   return operand;
9615 } /* gen_llvm_expr */
9616 
9617 static LLIntegerConditionCodes
convert_to_llvm_intcc(CC_RELATION cc)9618 convert_to_llvm_intcc(CC_RELATION cc)
9619 {
9620   switch (cc) {
9621   case CC_EQ:
9622   case CC_NOTNE: return LLCC_EQ;
9623   case CC_NE:
9624   case CC_NOTEQ: return LLCC_NE;
9625   case CC_LT:
9626   case CC_NOTGE: return LLCC_SLT;
9627   case CC_GE:
9628   case CC_NOTLT: return LLCC_SGE;
9629   case CC_LE:
9630   case CC_NOTGT: return LLCC_SLE;
9631   case CC_GT:
9632   case CC_NOTLE: return LLCC_SGT;
9633   default:
9634     assert(false, "unknown condition code", cc, ERR_Fatal);
9635   }
9636   return LLCC_NONE;
9637 }
9638 
9639 static LLIntegerConditionCodes
convert_to_llvm_uintcc(CC_RELATION cc)9640 convert_to_llvm_uintcc(CC_RELATION cc)
9641 {
9642   switch (cc) {
9643   case CC_EQ:
9644   case CC_NOTNE: return LLCC_EQ;
9645   case CC_NE:
9646   case CC_NOTEQ: return LLCC_NE;
9647   case CC_LT:
9648   case CC_NOTGE: return LLCC_ULT;
9649   case CC_GE:
9650   case CC_NOTLT: return LLCC_UGE;
9651   case CC_LE:
9652   case CC_NOTGT: return LLCC_ULE;
9653   case CC_GT:
9654   case CC_NOTLE: return LLCC_UGT;
9655   default:
9656     assert(false, "unknown condition code", cc, ERR_Fatal);
9657   }
9658   return LLCC_NONE;
9659 }
9660 
9661 static LLFloatingPointConditionCodes
convert_to_llvm_fltcc(CC_RELATION cc)9662 convert_to_llvm_fltcc(CC_RELATION cc)
9663 {
9664   switch (cc) {
9665   case CC_EQ:
9666   case CC_NOTNE: return LLCCF_OEQ; // see section 5.11 of IEEE 754
9667   case CC_NE:
9668   case CC_NOTEQ: return LLCCF_UNE;
9669   case CC_LT:    return LLCCF_OLT;
9670   case CC_NOTGE: return LLCCF_ULT;
9671   case CC_GE:    return LLCCF_OGE;
9672   case CC_NOTLT: return LLCCF_UGE;
9673   case CC_LE:    return LLCCF_OLE;
9674   case CC_NOTGT: return LLCCF_ULE;
9675   case CC_GT:    return LLCCF_OGT;
9676   case CC_NOTLE: return LLCCF_UGT;
9677   default:
9678     assert(false, "unknown condition code", cc, ERR_Fatal);
9679   }
9680   return LLCCF_NONE;
9681 }
9682 
9683 static OPERAND *
gen_vect_compare_operand(int mask_ili)9684 gen_vect_compare_operand(int mask_ili)
9685 {
9686   int num_elem;
9687   CC_RELATION incoming_cc_code;
9688   int lhs_ili, rhs_ili, cmp_type;
9689   LL_Type *int_type, *instr_type, *compare_ll_type;
9690   LL_InstrName cmp_inst_name;
9691   DTYPE vect_dtype, elem_dtype;
9692   OPERAND *operand, *op1;
9693   ILI_OP mask_opc = ILI_OPC(mask_ili);
9694 
9695   assert(mask_opc == IL_VCMP,
9696          "gen_vect_compare_operand(): expected vector compare", mask_opc,
9697          ERR_Fatal);
9698 
9699   incoming_cc_code = ILI_ccOPND(mask_ili, 1);
9700   lhs_ili = ILI_OPND(mask_ili, 2);
9701   rhs_ili = ILI_OPND(mask_ili, 3);
9702   vect_dtype = ili_get_vect_dtype(mask_ili);
9703   elem_dtype = DTySeqTyElement(vect_dtype);
9704   num_elem = DTyVecLength(vect_dtype);
9705 
9706   int_type = make_int_lltype(1);
9707   instr_type = ll_get_vector_type(int_type, num_elem);
9708   compare_ll_type = make_lltype_from_dtype(vect_dtype);
9709   if (DT_ISINT(elem_dtype)) {
9710     cmp_inst_name = I_ICMP;
9711     cmp_type = CMP_INT;
9712     if (DT_ISUNSIGNED(elem_dtype)) {
9713       cmp_type = cmp_type | CMP_USG;
9714     }
9715   } else if (DT_ISREAL(elem_dtype)) {
9716     cmp_inst_name = I_FCMP;
9717     cmp_type = CMP_FLT;
9718   } else {
9719     assert(false, "gen_vect_compare_operand(): unsupported dtype", elem_dtype,
9720            ERR_Fatal);
9721   }
9722   op1 = make_operand();
9723   op1->ot_type = OT_CC;
9724   op1->val.cc = convert_to_llvm_cc(incoming_cc_code, cmp_type);
9725   op1->tmps = make_tmps();
9726   /* type of the compare is the operands: use compare_ll_type */
9727   op1->ll_type = compare_ll_type;
9728   op1->next = gen_llvm_expr(lhs_ili, compare_ll_type);
9729   op1->next->next = gen_llvm_expr(rhs_ili, compare_ll_type);
9730   /* type of the instruction is a bit-vector: use instr_type */
9731   operand = ad_csed_instr(cmp_inst_name, mask_ili, instr_type, op1,
9732                           InstrListFlagsNull, true);
9733   return operand;
9734 } /* gen_vect_compare_operand */
9735 
9736 static char *
vect_llvm_intrinsic_name(int ilix)9737 vect_llvm_intrinsic_name(int ilix)
9738 {
9739   int type, n, fsize;
9740   DTYPE dtype;
9741   ILI_OP opc = ILI_OPC(ilix);
9742   char *basename, *retc;
9743   assert(IL_VECT(opc), "vect_llvm_intrinsic_name(): not vect ili", ilix,
9744          ERR_Fatal);
9745   dtype = ili_get_vect_dtype(ilix);
9746 
9747   assert(DTY(dtype) == TY_VECT, "vect_llvm_intrinsic_name(): not vect dtype",
9748          DTY(dtype), ERR_Fatal);
9749   type = DTySeqTyElement(dtype);
9750   retc = getitem(LLVM_LONGTERM_AREA, 20);
9751   n = DTyVecLength(dtype);
9752   switch (opc) {
9753   case IL_VSQRT:
9754     basename = "sqrt";
9755     break;
9756   case IL_VABS:
9757     basename = "fabs";
9758     break;
9759   case IL_VFLOOR:
9760     basename = "floor";
9761     break;
9762   case IL_VCEIL:
9763     basename = "ceil";
9764     break;
9765   case IL_VAINT:
9766     basename = "trunc";
9767     break;
9768   case IL_VFMA1:
9769     basename = "fma";
9770     break;
9771   case IL_VSIN: /* VSIN here for testing purposes */
9772     basename = "sin";
9773     break;
9774   default:
9775     assert(0, "vect_llvm_intrinsic_name(): unhandled opc", opc, ERR_Fatal);
9776   }
9777   switch (type) {
9778   case DT_FLOAT:
9779     fsize = 32;
9780     break;
9781   case DT_DBLE:
9782     fsize = 64;
9783     break;
9784   default:
9785     assert(0, "vect_llvm_intrinsic_name(): unhandled type", type, ERR_Fatal);
9786   }
9787 
9788   sprintf(retc, "%s.v%df%d", basename, n, fsize);
9789 
9790   return retc;
9791 } /* vect_llvm_intrinsic_name */
9792 
9793 /**
9794    \brief Generate comparison operand. Optionally extending the result.
9795    \param optext  if this is false, do not extend the result to 32 bits.
9796  */
9797 static OPERAND *
gen_optext_comp_operand(OPERAND * operand,ILI_OP opc,int lhs_ili,int rhs_ili,int cc_ili,int cc_type,LL_InstrName itype,int optext,int ilix)9798 gen_optext_comp_operand(OPERAND *operand, ILI_OP opc, int lhs_ili, int rhs_ili,
9799                         int cc_ili, int cc_type, LL_InstrName itype, int optext,
9800                         int ilix)
9801 {
9802   LL_Type *expected_type, *op_type;
9803   INSTR_LIST *Curr_Instr;
9804   DTYPE dtype;
9805   int vsize;
9806 
9807   operand->ot_type = OT_TMP;
9808   operand->tmps = make_tmps();
9809 
9810   operand->ll_type = make_int_lltype(1);
9811   if (opc == IL_VCMPNEQ) {
9812     assert(ilix, "gen_optext_comp_operand(): missing ilix", 0, ERR_Fatal);
9813     dtype = ILI_DTyOPND(ilix, 3);
9814     vsize = DTyVecLength(dtype);
9815     op_type = operand->ll_type;
9816     operand->ll_type = make_vector_lltype(vsize, op_type);
9817   }
9818 
9819   /* now make the new binary expression */
9820   Curr_Instr =
9821       gen_instr(itype, operand->tmps, operand->ll_type, make_operand());
9822   Curr_Instr->operands->ot_type = OT_CC;
9823   Curr_Instr->operands->val.cc =
9824     convert_to_llvm_cc((CC_RELATION)cc_ili, cc_type);
9825   if (opc == IL_VCMPNEQ)
9826     Curr_Instr->operands->ll_type = expected_type =
9827         make_lltype_from_dtype(dtype);
9828   else
9829     Curr_Instr->operands->ll_type = expected_type = make_type_from_opc(opc);
9830   Curr_Instr->operands->next = gen_llvm_expr(lhs_ili, expected_type);
9831   if (opc == IL_ACMPZ || opc == IL_ACMP) {
9832     LL_Type *ty0 = Curr_Instr->operands->next->ll_type;
9833     OPERAND *opTo = Curr_Instr->operands->next;
9834     opTo->next = gen_base_addr_operand(rhs_ili, ty0);
9835   } else {
9836     Curr_Instr->operands->next->next = gen_llvm_expr(rhs_ili, expected_type);
9837   }
9838   if (opc == IL_ACMPZ)
9839     Curr_Instr->operands->next->next->flags |= OPF_NULL_TYPE;
9840 
9841   ad_instr(0, Curr_Instr);
9842   if (!optext)
9843     return operand;
9844   if (XBIT(125, 0x8))
9845     return zero_extend_int(operand, 32);
9846   else
9847     /* Result type is bool which is signed, -1 for true, 0 for false. */
9848     return sign_extend_int(operand, 32);
9849 }
9850 
9851 /*
9852  * Given an ilix that is either an IL_JMPM or an IL_JMPMK, generate and insert
9853  * the corresponding switch instruction.
9854  *
9855  * Return the switch instruction.
9856  */
9857 static INSTR_LIST *
gen_switch(int ilix)9858 gen_switch(int ilix)
9859 {
9860   int is_64bit = false;
9861   LL_Type *switch_type;
9862   INSTR_LIST *instr;
9863   OPERAND *last_op;
9864   int switch_sptr;
9865   int sw_elt;
9866 
9867   switch (ILI_OPC(ilix)) {
9868   case IL_JMPM:
9869     is_64bit = false;
9870     break;
9871   case IL_JMPMK:
9872     is_64bit = true;
9873     break;
9874   default:
9875     interr("gen_switch(): Unexpected jump ili", ilix, ERR_Fatal);
9876   }
9877 
9878   instr = make_instr(I_SW);
9879   switch_type = make_int_lltype(is_64bit ? 64 : 32);
9880 
9881   /*
9882    * JMPM  irlnk1 irlnk2 sym1 sym2
9883    * JMPMK krlnk1 irlnk2 sym1 sym2
9884    *
9885    * irlnk1 / krlnk1 is the value being switched on.
9886    * irlnk2 is the table size.
9887    * sym1 is the label for the memory table.
9888    * sym2 is the default label.
9889    *
9890    * Produce: switch <expr>, <default> [value, label]+
9891    */
9892   instr->operands = last_op = gen_llvm_expr(ILI_OPND(ilix, 1), switch_type);
9893 
9894   switch_sptr = ILI_OPND(ilix, 3);
9895 
9896   /* Add the default case. */
9897   last_op->next = make_target_op(DEFLABG(switch_sptr));
9898   last_op = last_op->next;
9899 
9900   /* Get all the switch elements out of the switch_base table. */
9901   for (sw_elt = SWELG(switch_sptr); sw_elt; sw_elt = switch_base[sw_elt].next) {
9902     OPERAND *label = make_target_op(switch_base[sw_elt].clabel);
9903     OPERAND *value;
9904     if (is_64bit)
9905       value = make_constsptr_op((SPTR)switch_base[sw_elt].val); // ???
9906     else
9907       value = make_constval32_op(switch_base[sw_elt].val);
9908     /* Remaining switch operands are (value, target) pairs. */
9909     last_op->next = value;
9910     value->next = label;
9911     last_op = label;
9912   }
9913 
9914   ad_instr(ilix, instr);
9915   return instr;
9916 }
9917 
9918 /**
9919    \brief Add \p ilix to the CSE list
9920    \param ilix  The ILI index to be added
9921    \return true iff \p ilix already appears in the CSE list
9922  */
9923 static bool
add_to_cselist(int ilix)9924 add_to_cselist(int ilix)
9925 {
9926   CSED_ITEM *csed;
9927 
9928   if (ILI_ALT(ilix))
9929     ilix = ILI_ALT(ilix);
9930 
9931   DBGTRACE1("#adding to cse list ilix %d", ilix)
9932 
9933   for (csed = csedList; csed; csed = csed->next) {
9934     if (ilix == csed->ilix) {
9935       DBGTRACE2("#ilix %d already in cse list, count %d", ilix, ILI_COUNT(ilix))
9936       return true;
9937     }
9938   }
9939   csed = (CSED_ITEM *)getitem(LLVM_LONGTERM_AREA, sizeof(CSED_ITEM));
9940   memset(csed, 0, sizeof(CSED_ITEM));
9941   csed->ilix = ilix;
9942   csed->next = csedList;
9943   csedList = csed;
9944   build_csed_list(ilix);
9945   return false;
9946 }
9947 
9948 static void
set_csed_operand(OPERAND ** csed_operand,OPERAND * operand)9949 set_csed_operand(OPERAND **csed_operand, OPERAND *operand)
9950 {
9951   if (operand->tmps) {
9952     OPERAND *new_op;
9953 
9954     DBGTRACE("#set_csed_operand using tmps created for operand")
9955     if (*csed_operand && (*csed_operand)->tmps &&
9956         (*csed_operand)->tmps != operand->tmps) {
9957       DBGTRACE("#tmps are different")
9958     }
9959     new_op = make_tmp_op(operand->ll_type, operand->tmps);
9960     *csed_operand = new_op;
9961   } else {
9962     DBGTRACE("#set_csed_operand replace csed operand")
9963     *csed_operand = operand;
9964   }
9965 }
9966 
9967 static void
clear_csed_list(void)9968 clear_csed_list(void)
9969 {
9970   CSED_ITEM *csed;
9971 
9972   for (csed = csedList; csed; csed = csed->next) {
9973     ILI_COUNT(csed->ilix) = 0;
9974     csed->operand = NULL;
9975   }
9976 }
9977 
9978 static void
remove_from_csed_list(int ili)9979 remove_from_csed_list(int ili)
9980 {
9981   int i, noprs;
9982   ILI_OP opc;
9983   CSED_ITEM *csed;
9984 
9985   opc = ILI_OPC(ili);
9986   for (csed = csedList; csed; csed = csed->next) {
9987     if (is_cseili_opcode(ILI_OPC(ili)))
9988       return;
9989     if (ili == csed->ilix) {
9990       DBGTRACE1("#remove_from_csed_list ilix(%d)", ili)
9991       ILI_COUNT(ili) = 0;
9992       csed->operand = NULL;
9993     }
9994   }
9995 
9996   noprs = ilis[opc].oprs;
9997   for (i = 1; i <= noprs; ++i) {
9998     if (IL_ISLINK(opc, i))
9999       remove_from_csed_list(ILI_OPND(ili, i));
10000   }
10001 }
10002 
10003 static OPERAND **
get_csed_operand(int ilix)10004 get_csed_operand(int ilix)
10005 {
10006   CSED_ITEM *csed;
10007 
10008   if (ILI_ALT(ilix))
10009     ilix = ILI_ALT(ilix);
10010   for (csed = csedList; csed; csed = csed->next) {
10011     if (ilix == csed->ilix) {
10012       OPERAND *p = csed->operand;
10013 
10014       if (p != NULL) {
10015         int sptr = p->val.sptr;
10016         DBGTRACE3(
10017             "#get_csed_operand for ilix %d, operand found %p, with type (%s)",
10018             ilix, p, OTNAMEG(p))
10019         DBGDUMPLLTYPE("cse'd operand type ", p->ll_type)
10020       } else {
10021         DBGTRACE1("#get_csed_operand for ilix %d, operand found is null", ilix);
10022       }
10023       return &csed->operand;
10024     }
10025   }
10026 
10027   DBGTRACE1("#get_csed_operand for ilix %d not found", ilix)
10028 
10029   return NULL;
10030 }
10031 
10032 static void
build_csed_list(int ilix)10033 build_csed_list(int ilix)
10034 {
10035   int i, noprs;
10036   ILI_OP opc = ILI_OPC(ilix);
10037 
10038   if (is_cseili_opcode(opc)) {
10039     int csed_ilix = ILI_OPND(ilix, 1);
10040     if (ILI_ALT(csed_ilix))
10041       csed_ilix = ILI_ALT(csed_ilix);
10042     if (add_to_cselist(csed_ilix))
10043       return;
10044   }
10045   switch (opc) {
10046 #ifdef LONG_DOUBLE_FLOAT128
10047   case IL_FLOAT128RESULT:
10048     if (ILI_OPND(ilix, 2) == 'i')
10049       add_prescan_complex_list(ILI_OPND(ilix, 1));
10050     break;
10051 #endif
10052   default:
10053     break;
10054   }
10055 
10056   noprs = ilis[opc].oprs;
10057   for (i = 1; i <= noprs; ++i) {
10058     if (IL_ISLINK(opc, i))
10059       build_csed_list(ILI_OPND(ilix, i));
10060   }
10061 }
10062 
10063 static int
convert_to_llvm_cc(CC_RELATION cc,int cc_type)10064 convert_to_llvm_cc(CC_RELATION cc, int cc_type)
10065 {
10066   int ret_code;
10067   if (cc_type & CMP_INT) {
10068     if (cc_type & CMP_USG)
10069       ret_code = convert_to_llvm_uintcc(cc);
10070     else
10071       ret_code = convert_to_llvm_intcc(cc);
10072   } else {
10073     ret_code = convert_to_llvm_fltcc(cc);
10074   }
10075   if (IEEE_CMP && fcmp_negate)
10076     ret_code = fnegcc[ret_code];
10077   return ret_code;
10078 }
10079 
10080 INLINE static bool
check_global_define(GBL_LIST * cgl)10081 check_global_define(GBL_LIST *cgl)
10082 {
10083   GBL_LIST *gl, *gitem;
10084 
10085   for (gl = recorded_Globals; gl; gl = gl->next) {
10086     if (gl->sptr > 0 && gl->sptr == cgl->sptr) {
10087       DBGTRACE1("#sptr %d already in Global list; exiting", gl->sptr)
10088       return true;
10089     }
10090   }
10091   gitem = (GBL_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(GBL_LIST));
10092   memset(gitem, 0, sizeof(GBL_LIST));
10093   gitem->sptr = cgl->sptr;
10094   gitem->global_def = cgl->global_def;
10095   gitem->next = recorded_Globals;
10096   recorded_Globals = gitem;
10097   return false;
10098 }
10099 
10100 static void
add_global_define(GBL_LIST * gitem)10101 add_global_define(GBL_LIST *gitem)
10102 {
10103   GBL_LIST *gl;
10104 
10105   DBGTRACEIN2(": '%s', (sptr %d)", gitem->global_def, gitem->sptr);
10106 
10107   /* make sure the global def for this sptr has not already been added;
10108    * can occur with -Mipa=inline that multiple versions exist.
10109    */
10110   if (!check_global_define(gitem)) {
10111     if (Globals) {
10112       llvm_info.last_global->next = gitem;
10113     } else {
10114       Globals = gitem;
10115     }
10116     llvm_info.last_global = gitem;
10117     if (flg.debug) {
10118       if (gitem->sptr && ST_ISVAR(STYPEG(gitem->sptr)) &&
10119           !CCSYMG(gitem->sptr)) {
10120         LL_Type *type = make_lltype_from_sptr(gitem->sptr);
10121         LL_Value *value = ll_create_value_from_type(cpu_llvm_module, type,
10122                                                     SNAME(gitem->sptr));
10123         lldbg_emit_global_variable(cpu_llvm_module->debug_info, gitem->sptr, 0,
10124                                    1, value);
10125       }
10126     }
10127   }
10128 
10129   DBGTRACEOUT("");
10130 } /* add_global_define */
10131 
10132 void
update_external_function_declarations(const char * name,char * decl,unsigned flags)10133 update_external_function_declarations(const char *name, char *decl,
10134                                       unsigned flags)
10135 {
10136   EXFUNC_LIST *efl;
10137   char *gname;
10138 
10139   gname = (char *)getitem(LLVM_LONGTERM_AREA, strlen(decl) + 1);
10140   strcpy(gname, decl);
10141   efl = (EXFUNC_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(EXFUNC_LIST));
10142   memset(efl, 0, sizeof(EXFUNC_LIST));
10143   efl->func_def = gname;
10144   efl->flags |= flags;
10145   add_external_function_declaration(name, efl);
10146 }
10147 
10148 /**
10149    \brief Get an intrinsic function with \p name and \p func_type.
10150    \param name       The name of the function
10151    \param func_type  The signature of the function
10152 
10153    Create an external function declaration if necessary, or verify that the
10154    requested function type matches previous uses.
10155 
10156    Use this to generate calls to LLVM intrinsics or runtime library functions.
10157 
10158    The function name must include the leading '@'. The \p name string will not
10159    be copied.
10160  */
10161 static OPERAND *
get_intrinsic(const char * name,LL_Type * func_type)10162 get_intrinsic(const char *name, LL_Type *func_type)
10163 {
10164   hash_data_t old_type = NULL;
10165   OPERAND *op;
10166 
10167   if (hashmap_lookup(llvm_info.declared_intrinsics, name, &old_type)) {
10168     assert(old_type == func_type,
10169            "Intrinsic already declared with different signature", 0, ERR_Fatal);
10170   } else {
10171     /* First time we see this intrinsic. */
10172     int i;
10173     char *decl = (char *)getitem(LLVM_LONGTERM_AREA,
10174                                  strlen(name) + strlen(func_type->str) + 50);
10175     if (!strncmp(name, "asm ", 4)) {
10176       /* do nothing - CALL asm() */
10177     } else {
10178       sprintf(decl, "declare %s %s(", func_type->sub_types[0]->str, name);
10179       for (i = 1; i < func_type->sub_elements; i++) {
10180         if (i > 1)
10181           strcat(decl, ", ");
10182         strcat(decl, func_type->sub_types[i]->str);
10183       }
10184       strcat(decl, ")");
10185       update_external_function_declarations(name, decl, EXF_INTRINSIC);
10186       hashmap_insert(llvm_info.declared_intrinsics, name, func_type);
10187     }
10188   }
10189 
10190   op = make_operand();
10191   op->ot_type = OT_CALL;
10192   op->ll_type = make_ptr_lltype(func_type);
10193   op->string = (char *)name;
10194   return op;
10195 }
10196 
10197 /**
10198    \brief Prepend the callee to a list of operands for an intrinsic call
10199 
10200    When preparing a call to <code>float @llvm.foo(i32 %a, i32 %b)</code>, pass
10201    the \c %a, \c %b operands to
10202    <code>get_intrinsic_call_ops("@llvm.foo", float, a_b_ops);</code>
10203  */
10204 static OPERAND *
get_intrinsic_call_ops(const char * name,LL_Type * return_type,OPERAND * args)10205 get_intrinsic_call_ops(const char *name, LL_Type *return_type, OPERAND *args)
10206 {
10207   LL_Type *func_type = make_function_type_from_args(return_type, args, false);
10208   OPERAND *op = get_intrinsic(name, func_type);
10209   op->next = args;
10210   return op;
10211 }
10212 
10213 #define OCTVAL(v) ((v >= 48) && (v <= 55))
10214 
10215 static int
decimal_value_from_oct(int c,int b,int a)10216 decimal_value_from_oct(int c, int b, int a)
10217 {
10218   int val, vc, vb, va;
10219 
10220   vc = c - 48;
10221   vb = b - 48;
10222   va = a - 48;
10223   val = vc * 64 + vb * 8 + va;
10224   return val;
10225 } /* decimal value */
10226 
10227 /**
10228    \brief Format a string for LLVM output
10229 
10230    LLVM uses hex ASCII characters in strings in place of escape sequences. So
10231    process the string here making all needed replacements.
10232  */
10233 static char *
process_string(char * name,int pad,int string_length)10234 process_string(char *name, int pad, int string_length)
10235 {
10236   int i, value, remain, count = 0;
10237   int len = strlen(name);
10238   char *new_name = (char *)getitem(LLVM_LONGTERM_AREA, 3 * (len + pad) + 2);
10239 
10240   DBGTRACEIN4(" arg name: %s, pad: %d, len: %d, string_length %d", name, pad,
10241               len, string_length);
10242 
10243   for (i = 0; i <= len; i++) {
10244     if (name[i] == 92 && i < len) /* backslash that might be an escape */
10245     {
10246       switch (name[i + 1]) {
10247       case 39: /* \' in string => ' */
10248         new_name[count++] = name[i + 1];
10249         i++;
10250         break;
10251       case 48: /* look for octal values */
10252       case 49:
10253       case 50:
10254       case 51:
10255       case 52:
10256       case 53:
10257       case 54:
10258       case 55:
10259         if (i <= len - 2 && OCTVAL(name[i + 2]) && OCTVAL(name[i + 3])) {
10260           value = decimal_value_from_oct(name[i + 1], name[i + 2], name[i + 3]);
10261           remain = value % 16;
10262           value = value / 16;
10263           new_name[count++] = name[i]; /* copy the \ character */
10264           if (value < 10)
10265             new_name[count++] = 48 + value;
10266           else
10267             new_name[count++] = 55 + value;
10268           if (remain < 10)
10269             new_name[count++] = 48 + remain;
10270           else
10271             new_name[count++] = 55 + remain;
10272           i += 3;
10273         } else
10274           new_name[count++] = name[i]; /* copy the \ character */
10275         break;
10276       case 97: /* bell character (bel) - \a in string => \07 */
10277         new_name[count++] = name[i]; /* copy the \ character */
10278         new_name[count++] = 48;
10279         new_name[count++] = 55;
10280         i++;
10281         break;
10282       case 98:                       /* backspace (bs) - \b in string => \08 */
10283         new_name[count++] = name[i]; /* copy the \ character */
10284         new_name[count++] = 48;
10285         new_name[count++] = 56;
10286         i++;
10287         break;
10288       case 116: /* horizontal tab (ht) - \t in string => \09 */
10289         new_name[count++] = name[i]; /* copy the \ character */
10290         new_name[count++] = 48;
10291         new_name[count++] = 57;
10292         i++;
10293         break;
10294       case 110:                      /* newline (nl) - \n in string => \0a */
10295         new_name[count++] = name[i]; /* copy the \ character */
10296         new_name[count++] = 48;
10297         new_name[count++] = 97;
10298         i++;
10299         break;
10300       case 102:                      /* form feed (np) - \f in string => \0c */
10301         new_name[count++] = name[i]; /* copy the \ character */
10302         new_name[count++] = 48;
10303         new_name[count++] = 99;
10304         i++;
10305         break;
10306       case 114: /* carriage return (cr) - \r in string => \0d */
10307         new_name[count++] = name[i]; /* copy the \ character */
10308         new_name[count++] = 48;
10309         new_name[count++] = 100;
10310         i++;
10311         break;
10312       case 34:                       /* quote character - \" in string => \22 */
10313         new_name[count++] = name[i]; /* copy the \ character */
10314         new_name[count++] = 50;
10315         new_name[count++] = 50;
10316         i++;
10317         break;
10318       case 92: /* backslash  character - \\ in string => \5C */
10319         new_name[count++] = name[i]; /* copy the \ character */
10320         new_name[count++] = 53;
10321         new_name[count++] = 67;
10322         i++;
10323         break;
10324       default:                       /* don't do anything */
10325         new_name[count++] = name[i]; /* copy the \ character */
10326         break;
10327       }
10328     } else {
10329       switch (name[i]) {
10330       case 10:
10331         new_name[count++] = 92; /* copy the \ character */
10332         new_name[count++] = 48;
10333         new_name[count++] = 97;
10334         break;
10335       case 34:
10336         if (i && i != (len - 1)) {
10337           new_name[count++] = 92; /* copy the \ character */
10338           new_name[count++] = '2';
10339           new_name[count++] = '2';
10340           break;
10341         }
10342       default:
10343         new_name[count++] = name[i];
10344       }
10345     }
10346   }
10347   len = strlen(new_name);
10348   /* add any needed padding */
10349   for (i = 0; i < pad; i++) {
10350     new_name[len + (i * 3 - 1)] = 92; /* \ */
10351     new_name[len + (i * 3)] = 48;     /* 0 */
10352     new_name[len + (i * 3 + 1)] = 48; /* 0 */
10353   }
10354 
10355   if (pad) /* if needed, fix up the end of the padding */
10356   {
10357     new_name[len + (3 * pad - 1)] = 34; /* " */
10358     new_name[len + (3 * pad)] = 0;      /* '\0' */
10359   }
10360 
10361   len = strlen(new_name);
10362   /* need to have the string end with \00" unless tight
10363    * character array initialization.
10364    */
10365   if (!string_length || len - 2 != string_length) {
10366     new_name[len - 1] = 92; /* \ */
10367     new_name[len] = 48;     /* 0 */
10368     new_name[len + 1] = 48; /* 0 */
10369     new_name[len + 2] = 34; /* " */
10370     new_name[len + 3] = 0;  /* '\0' */
10371   }
10372 
10373   DBGTRACEOUT1(" returns '%s'", new_name)
10374 
10375   return new_name;
10376 } /* process_string */
10377 
10378 /**
10379     \brief Get string name for a struct type
10380     \param dtype  dtype index
10381     \return string containing dtype name
10382  */
10383 char *
dtype_struct_name(DTYPE dtype)10384 dtype_struct_name(DTYPE dtype)
10385 {
10386   char *dtype_str = process_dtype_struct(dtype);
10387   return dtype_str;
10388 }
10389 
10390 /* Set the LLVM name of a global sptr to '@' + name.
10391  *
10392  * This is appropriate for external identifiers and internal identifiers with a
10393  * module-unique name.
10394  */
10395 static char *
set_global_sname(int sptr,const char * name)10396 set_global_sname(int sptr, const char *name)
10397 {
10398   name = map_to_llvm_name(name);
10399   SNAME(sptr) = (char *)getitem(LLVM_LONGTERM_AREA, strlen(name) + 2);
10400   sprintf(SNAME(sptr), "@%s", name);
10401   return SNAME(sptr);
10402 }
10403 
10404 /* Set the LLVM name of a global sptr to '@' + name + '.' + sptr.
10405  *
10406  * This is appropriate for internal globals that don't have a unique name
10407  * because they belong to some scope. The sptr suffix makes the name unique.
10408  */
10409 static char *
set_numbered_global_sname(int sptr,const char * name)10410 set_numbered_global_sname(int sptr, const char *name)
10411 {
10412   SNAME(sptr) = (char *)getitem(LLVM_LONGTERM_AREA, strlen(name) + 12);
10413   sprintf(SNAME(sptr), "@%s.%d", name, sptr);
10414   return SNAME(sptr);
10415 }
10416 
10417 /* Set the LLVM name of a local sptr to '%' + name.
10418  *
10419  * This is appropriate for function-local identifiers.
10420  */
10421 static char *
set_local_sname(int sptr,const char * name)10422 set_local_sname(int sptr, const char *name)
10423 {
10424   SNAME(sptr) = (char *)getitem(LLVM_LONGTERM_AREA, strlen(name) + 2);
10425   sprintf(SNAME(sptr), "%%%s", name);
10426   return SNAME(sptr);
10427 }
10428 
10429 /* Create an LLVM initializer for a global define and record it as
10430  * gitem->global_def.
10431  *
10432  * This will either use the dinit_string() or an appropriate zero-initializer.
10433  *
10434  * The flag_str modifies the global variable linkage, visibility, and other
10435  * flags.
10436  *
10437  * The type_str is the name of of global type as returned from char_type().
10438  *
10439  */
10440 static void
create_global_initializer(GBL_LIST * gitem,const char * flag_str,const char * type_str)10441 create_global_initializer(GBL_LIST *gitem, const char *flag_str,
10442                           const char *type_str)
10443 {
10444   int dty, stype;
10445   int sptr = gitem->sptr;
10446   const char *initializer;
10447   char *gname;
10448 
10449   assert(sptr, "gitem must be initialized", 0, ERR_Fatal);
10450   assert(gitem->global_def == NULL, "gitem already has an initializer", sptr,
10451          ERR_Fatal);
10452   assert(SNAME(sptr), "sptr must have an LLVM name", sptr, ERR_Fatal);
10453 
10454   /* Create an initializer string. */
10455   if (DINITG(sptr))
10456     return;
10457 
10458   dty = DTY(DTYPEG(sptr));
10459   stype = STYPEG(sptr);
10460 
10461   if (
10462       (stype == ST_VAR && dty == TY_PTR))
10463     initializer = "null";
10464   else if (AGGREGATE_STYPE(stype) || COMPLEX_DTYPE(DTYPEG(sptr)) ||
10465            VECTOR_DTYPE(DTYPEG(sptr)))
10466     initializer = "zeroinitializer";
10467   else if (stype == ST_VAR && TY_ISREAL(dty))
10468     initializer = "0.0";
10469   else
10470     initializer = "0";
10471   gname = (char *)getitem(LLVM_LONGTERM_AREA,
10472                           strlen(SNAME(sptr)) + strlen(flag_str) +
10473                               strlen(type_str) + strlen(initializer) + 8);
10474   sprintf(gname, "%s = %s %s %s", SNAME(sptr), flag_str, type_str, initializer);
10475   gitem->global_def = gname;
10476 }
10477 
10478 /**
10479    \brief Check if sptr is the midnum of an array and the array has descriptor
10480    \param sptr  A symbol
10481  */
10482 bool
ftn_array_need_debug_info(SPTR sptr)10483 ftn_array_need_debug_info(SPTR sptr)
10484 {
10485   if ((sptr > NOSYM) && REVMIDLNKG(sptr)) {
10486     SPTR array_sptr = (SPTR)REVMIDLNKG(sptr);
10487     if (!CCSYMG(array_sptr) && SDSCG(array_sptr))
10488       return true;
10489   }
10490   return false;
10491 }
10492 
10493 /**
10494    \brief Separate symbols that should NOT have debug information
10495    \param sptr  a symbol
10496    \return false iff \p sptr ought NOT to have debug info
10497  */
10498 INLINE static bool
needDebugInfoFilt(SPTR sptr)10499 needDebugInfoFilt(SPTR sptr)
10500 {
10501   if (!sptr)
10502     return true;
10503   /* Fortran case needs to be revisited when we start to support debug, for now
10504    * just the obvious case */
10505   return (!CCSYMG(sptr) || DCLDG(sptr) || ftn_array_need_debug_info(sptr));
10506 }
10507 #ifdef OMP_OFFLOAD_LLVM
10508 INLINE static bool
is_ompaccel(SPTR sptr)10509 is_ompaccel(SPTR sptr)
10510 {
10511   return OMPACCDEVSYMG(sptr);
10512 }
10513 #endif
10514 INLINE static bool
generating_debug_info(void)10515 generating_debug_info(void)
10516 {
10517   return flg.debug && cpu_llvm_module->debug_info;
10518 }
10519 
10520 /**
10521    \brief Determine if debug information is needed for a particular symbol
10522    \param sptr  The symbol
10523 
10524    Checks debug flags and symbol properties.
10525  */
10526 INLINE static bool
need_debug_info(SPTR sptr)10527 need_debug_info(SPTR sptr)
10528 {
10529 #ifdef OMP_OFFLOAD_LLVM
10530   if (is_ompaccel(sptr) && ISNVVMCODEGEN)
10531     return false;
10532 #endif
10533   return generating_debug_info() && needDebugInfoFilt(sptr);
10534 }
10535 
10536 /**
10537    \brief Fixup debug info types
10538 
10539    The declared type and the type of the symbol may have diverged. We want to
10540    use the declared type for debug info so the user sees the expected
10541    representation.
10542  */
10543 INLINE static LL_Type *
mergeDebugTypesForGlobal(const char ** glob,LL_Type * symTy,LL_Type * declTy)10544 mergeDebugTypesForGlobal(const char **glob, LL_Type *symTy, LL_Type *declTy)
10545 {
10546   if (symTy != declTy) {
10547     const size_t strlenSum =
10548         16 + strlen(symTy->str) + strlen(declTy->str) + strlen(*glob);
10549     char *buff = (char *)getitem(LLVM_LONGTERM_AREA, strlenSum);
10550     snprintf(buff, strlenSum, "bitcast (%s %s to %s)", symTy->str, *glob,
10551              declTy->str);
10552     *glob = buff;
10553   }
10554   return declTy;
10555 }
10556 
10557 static void
addDebugForGlobalVar(SPTR sptr,ISZ_T off)10558 addDebugForGlobalVar(SPTR sptr, ISZ_T off)
10559 {
10560   if (need_debug_info(sptr)) {
10561     LL_Module *mod = cpu_llvm_module;
10562     /* TODO: defeat unwanted side-effects. make_lltype_from_sptr() will update
10563        the LLTYPE() type (sptr_type_array) along some paths. This may be
10564        undesirable at this point, because the array gets updated with an
10565        unexpected/incorrect type. Work around this buggy behavior by caching and
10566        restoring the type value.  Figure out why this works the way it does. */
10567     LL_Type *cache = LLTYPE(sptr);
10568     LL_Type *sty = make_lltype_from_sptr(sptr);
10569     LL_Type *dty = ll_get_pointer_type(make_lltype_from_dtype(DTYPEG(sptr)));
10570     const char *glob = SNAME(sptr);
10571     LL_Type *vty = mergeDebugTypesForGlobal(&glob, sty, dty);
10572     LL_Value *val = ll_create_value_from_type(mod, vty, glob);
10573     lldbg_emit_global_variable(mod->debug_info, sptr, off, 1, val);
10574     LLTYPE(sptr) = cache;
10575   }
10576 }
10577 
10578 static void
process_cmnblk_data(SPTR sptr,ISZ_T off)10579 process_cmnblk_data(SPTR sptr, ISZ_T off)
10580 {
10581   SPTR cmnblk = MIDNUMG(sptr);
10582   SPTR scope = SCOPEG(cmnblk);
10583 
10584   if (flg.debug && !CCSYMG(cmnblk) && (scope > 0)) {
10585     const char *name = new_debug_name(SYMNAME(scope), SYMNAME(cmnblk), NULL);
10586     if (!ll_get_module_debug(cpu_llvm_module->common_debug_map, name))
10587       lldbg_emit_common_block_mdnode(cpu_llvm_module->debug_info, cmnblk);
10588   }
10589 }
10590 
10591 /**
10592    \brief process \c SC_STATIC \p sptr representing a file-local variable
10593    \param sptr  A symbol
10594    \param off   offset into a structure (should be >= 0)
10595  */
10596 static void
process_static_sptr(SPTR sptr,ISZ_T off)10597 process_static_sptr(SPTR sptr, ISZ_T off)
10598 {
10599   const int stype = STYPEG(sptr);
10600 
10601   DEBUG_ASSERT(SCG(sptr) == SC_STATIC, "Expected static variable sptr");
10602   DEBUG_ASSERT(!SNAME(sptr), "Already processed sptr");
10603 
10604   set_global_sname(sptr, get_llvm_name(sptr));
10605   sym_is_refd(sptr);
10606 
10607   if ((stype == ST_ENTRY) || (stype == ST_PROC))
10608     return;
10609   if ((stype == ST_CONST) || (stype == ST_PARAM))
10610     return;
10611 
10612   addDebugForGlobalVar(sptr, off);
10613 }
10614 
10615 static bool
is_blockaddr_store(int ilix,int rhs,int lhs)10616 is_blockaddr_store(int ilix, int rhs, int lhs)
10617 {
10618   if (ILI_OPC(rhs) == IL_AIMV || ILI_OPC(rhs) == IL_AKMV)
10619     rhs = ILI_OPND(rhs, 1);
10620 
10621   if (ILI_OPC(rhs) == IL_ACEXT) {
10622     SPTR gl_sptr;
10623     int ili, newnme;
10624     int nme = ILI_OPND(ilix, 3);
10625     SPTR sptr = basesym_of(nme);
10626     SPTR label = SymConval1(ILI_SymOPND(rhs, 1));
10627     process_sptr(label);
10628     gl_sptr = process_blockaddr_sptr(sptr, label);
10629 
10630     /* MSZ could be 64 if it is 64-bit */
10631     ili = ad_acon(gl_sptr, 0);
10632     STYPEP(gl_sptr, ST_VAR);
10633     newnme = addnme(NT_VAR, gl_sptr, 0, 0);
10634     ili = ad3ili(IL_LD, ili, newnme, MSZ_WORD);
10635 
10636     ili = ad4ili(IL_ST, ili, lhs, nme, MSZ_WORD);
10637     make_stmt(STMT_ST, ili, false, SPTR_NULL, 0);
10638 
10639     return true;
10640   }
10641   return false;
10642 }
10643 
10644 /**
10645    \brief Process block address symbol
10646 
10647    We want to generate something similar to:
10648    \verbatim
10649      @MAIN_iab = internal global  i8*  blockaddress(@MAIN_, %L.LB1_351)
10650 
10651    MAIN_:
10652       %0 = load i8** @iab2
10653       store i8* %0, i8** %iab
10654       ; next instruction is use when branching
10655       ; indirectbr i8** %iab, [label %the_label]
10656    \endverbatim
10657   */
10658 static SPTR
process_blockaddr_sptr(int sptr,int label)10659 process_blockaddr_sptr(int sptr, int label)
10660 {
10661   SPTR gl_sptr;
10662   char *curfnm = getsname(gbl.currsub);
10663   char *sptrnm = SYMNAME(sptr);
10664   int size = strlen(curfnm) + strlen(sptrnm);
10665 
10666   DEBUG_ASSERT(size <= MXIDLN, "strcat exceeds available space");
10667   gl_sptr = getsymbol(strcat(curfnm, sptrnm));
10668   DTYPEP(gl_sptr, DT_CPTR);
10669   STYPEP(gl_sptr, ST_VAR);
10670   SCP(gl_sptr, SC_EXTERN);
10671   ADDRESSP(gl_sptr, 0);
10672   CCSYMP(gl_sptr, 1);
10673 
10674   if (SNAME(gl_sptr) == NULL) {
10675     LL_Type *ttype;
10676     char *sname, *gname;
10677     const char *retc;
10678     char *labelName;
10679     GBL_LIST *gitem = (GBL_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(GBL_LIST));
10680     memset(gitem, 0, sizeof(GBL_LIST));
10681     gitem->sptr = gl_sptr;
10682 
10683     sname = (char *)getitem(LLVM_LONGTERM_AREA, strlen(SYMNAME(gl_sptr)));
10684     sprintf(sname, "@%s", SYMNAME(gl_sptr));
10685     SNAME(gl_sptr) = sname;
10686     ttype = make_lltype_sz4v3_from_sptr(gl_sptr);
10687     LLTYPE(gl_sptr) = ttype;
10688 
10689     size = size + 80;
10690 
10691     retc = char_type(DTYPEG(gl_sptr), gl_sptr);
10692     // FIXME: should use snprintf or check. How do we know +80 is big enough?
10693     gname = (char *)getitem(LLVM_LONGTERM_AREA, size);
10694     labelName = get_label_name(label);
10695     sprintf(gname, "@%s = internal global %s blockaddress(@%s, %%L%s)",
10696             SYMNAME(gl_sptr), retc, getsname(gbl.currsub), labelName);
10697     gitem->global_def = gname;
10698     add_global_define(gitem);
10699   }
10700 
10701   return gl_sptr;
10702 }
10703 
10704 /**
10705    \brief Process \p sptr and initialize \c SNAME(sptr)
10706    \param sptr  an external function
10707  */
10708 static void
process_extern_function_sptr(SPTR sptr)10709 process_extern_function_sptr(SPTR sptr)
10710 {
10711   DTYPE dtype = DTYPEG(sptr);
10712   DTYPE return_dtype;
10713   EXFUNC_LIST *exfunc;
10714   char *name, *gname, *extend_prefix;
10715   LL_Type *ll_ttype;
10716 
10717   assert(SCG(sptr) == SC_EXTERN, "Expected extern sptr", sptr, ERR_Fatal);
10718   assert(SNAME(sptr) == NULL, "Already processed sptr", sptr, ERR_Fatal);
10719   assert(STYPEG(sptr) == ST_PROC || STYPEG(sptr) == ST_ENTRY,
10720          "Can only process extern procedures", sptr, ERR_Fatal);
10721 
10722   name = set_global_sname(sptr, get_llvm_name(sptr));
10723 
10724   sym_is_refd(sptr);
10725   if (CFUNCG(sptr) && STYPEG(sptr) == ST_PROC) {
10726     DTYPE ttype = DDTG(dtype);
10727     if (DTY(ttype) == TY_CHAR) {
10728       ll_ttype = make_ptr_lltype(make_lltype_from_dtype(DT_BINT));
10729       LLTYPE(sptr) = ll_ttype;
10730     }
10731   }
10732   return_dtype = dtype;
10733 
10734   if (DEFDG(sptr))
10735     return; /* defined in the file, so no need to declare separately */
10736   if (INMODULEG(sptr) && INMODULEG(sptr) == INMODULEG(gbl.currsub) &&
10737       FUNCLINEG(sptr))
10738     return; /* module subroutine call its module subroutine*/
10739 
10740 #if defined ALIASG && defined WEAKG
10741   /* Don't emit an external reference if the name needs to be defined
10742    * as a weak alias in write_aliases().
10743    */
10744   if (ALIASG(sptr) > NOSYM && WEAKG(sptr))
10745     return;
10746 #endif
10747 
10748   /* In the case of a function, we want the return type, not the type of
10749    * the sptr, which we know is "function"
10750    */
10751   exfunc = (EXFUNC_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(EXFUNC_LIST));
10752   memset(exfunc, 0, sizeof(EXFUNC_LIST));
10753   exfunc->sptr = sptr;
10754   if (cgmain_init_call(sptr)) {
10755     gname = (char *)getitem(LLVM_LONGTERM_AREA, 34);
10756     sprintf(gname, "declare void @__c_bzero(i32, i8*)");
10757     exfunc->flags |= EXF_INTRINSIC;
10758   } else {
10759     const DTYPE dTy =
10760         get_return_dtype(return_dtype, &(exfunc->flags), EXF_STRUCT_RETURN);
10761     const char *retc = char_type(dTy, SPTR_NULL);
10762     const int size = strlen(retc) + strlen(name) + 50;
10763     gname = getitem(LLVM_LONGTERM_AREA, size);
10764 #ifdef VARARGG
10765     if (VARARGG(sptr))
10766       exfunc->flags |= EXF_VARARG;
10767 #endif
10768     /* do we return a char? If so, must add
10769      * attribute "zeroext" or "signext"
10770      */
10771     switch (DTY(dTy)) {
10772     default:
10773       extend_prefix = "";
10774       break;
10775     case TY_SINT:
10776 #if defined(TARGET_LLVM_X8664)
10777       /* Workaround: LLVM on x86 does not sign extend i16 types */
10778       retc = char_type(DT_INT, SPTR_NULL);
10779 #endif
10780     case TY_BINT:
10781       extend_prefix = "signext";
10782       break;
10783     case TY_USINT:
10784 #if defined(TARGET_LLVM_X8664)
10785       /* Workaround: LLVM on x86 does not sign extend i16 types */
10786       retc = char_type(DT_INT, SPTR_NULL);
10787 #endif
10788       extend_prefix = "zeroext";
10789       break;
10790     }
10791     sprintf(gname, "%s %s %s", extend_prefix, retc, name);
10792   }
10793   exfunc->func_def = gname;
10794   exfunc->use_dtype = DTYPEG(sptr);
10795   add_external_function_declaration(name, exfunc);
10796 }
10797 
10798 INLINE static bool
externVarHasDefinition(SPTR sptr)10799 externVarHasDefinition(SPTR sptr)
10800 {
10801   return DEFDG(sptr);
10802 }
10803 
10804 INLINE static bool
externVarMustInitialize(SPTR sptr)10805 externVarMustInitialize(SPTR sptr)
10806 {
10807   return true;
10808 }
10809 
10810 /**
10811    \brief Process extern variable \p sptr and initialize <tt>SNAME(sptr)</tt>
10812    \param sptr  a symbol
10813    \param off
10814  */
10815 static void
process_extern_variable_sptr(SPTR sptr,ISZ_T off)10816 process_extern_variable_sptr(SPTR sptr, ISZ_T off)
10817 {
10818   char *name;
10819   const char *retc;
10820   const char *flag_str;
10821   GBL_LIST *gitem;
10822 
10823   DEBUG_ASSERT(SCG(sptr) == SC_EXTERN, "Expected extern sptr");
10824   DEBUG_ASSERT(!SNAME(sptr), "Already processed sptr");
10825 
10826   name = set_global_sname(sptr, get_llvm_name(sptr));
10827   retc = char_type(DTYPEG(sptr), sptr);
10828 
10829   gitem = (GBL_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(GBL_LIST));
10830   memset(gitem, 0, sizeof(GBL_LIST));
10831   gitem->sptr = sptr;
10832   gitem->alignment = align_of_var(sptr);
10833 
10834   /* Add debug information for global variable */
10835   addDebugForGlobalVar(sptr, off);
10836 
10837   if (!externVarHasDefinition(sptr))
10838     return;
10839 
10840   flag_str = IS_TLS(sptr) ? "thread_local global" : "global";
10841 
10842   /* Defined as a global, not initialized. */
10843   if (!DINITG(sptr))
10844     flag_str = IS_TLS(sptr) ? "common thread_local global" : "common global";
10845 
10846   if (externVarMustInitialize(sptr))
10847     create_global_initializer(gitem, flag_str, retc);
10848 }
10849 
10850 /**
10851    \brief add debug information for variable \p sptr
10852    \param sptr  the symbol to be added
10853    \param type  the type to be used for \p sptr
10854  */
10855 INLINE static void
addDebugForLocalVar(SPTR sptr,LL_Type * type)10856 addDebugForLocalVar(SPTR sptr, LL_Type *type)
10857 {
10858   if (need_debug_info(sptr)) {
10859     /* Dummy sptrs are treated as local (see above) */
10860     LL_MDRef param_md = lldbg_emit_local_variable(
10861         cpu_llvm_module->debug_info, sptr, BIH_FINDEX(gbl.entbih), true);
10862     insert_llvm_dbg_declare(param_md, sptr, type, NULL, OPF_NONE);
10863   }
10864 }
10865 
10866 /**
10867    \brief process an \c SC_LOCAL \p sptr and initialize \c SNAME(sptr)
10868    \param sptr  a symbol
10869  */
10870 static void
process_local_sptr(SPTR sptr)10871 process_local_sptr(SPTR sptr)
10872 {
10873   LL_Type *type = NULL;
10874   assert(SCG(sptr) == SC_LOCAL, "Expected local sptr", sptr, ERR_Fatal);
10875   assert(SNAME(sptr) == NULL, "Already processed sptr", sptr, ERR_Fatal);
10876 
10877   sym_is_refd(sptr);
10878 
10879   if (REFG(sptr) && DINITG(sptr)) {
10880     char *name = get_llvm_name(sptr);
10881     if (SCOPEG(sptr) == 0) {
10882       name = set_global_sname(sptr, name);
10883     } else {
10884       name = set_numbered_global_sname(sptr, name);
10885     }
10886     DBGTRACE2("#variable #%d(%s) is data initialized", sptr, SYMNAME(sptr))
10887   } else if (DINITG(sptr) || SAVEG(sptr)) {
10888     char *name = get_llvm_name(sptr);
10889     GBL_LIST *gitem = (GBL_LIST *)getitem(LLVM_LONGTERM_AREA, sizeof(GBL_LIST));
10890     memset(gitem, 0, sizeof(GBL_LIST));
10891     gitem->sptr = sptr;
10892 
10893     if (SCOPEG(sptr) == 0) {
10894       name = set_global_sname(sptr, name);
10895     } else {
10896       name = set_numbered_global_sname(sptr, name);
10897     }
10898 
10899     DBGTRACE2("#variable #%d(%s) is data initialized", sptr, SYMNAME(sptr));
10900     create_global_initializer(gitem, "internal global",
10901                               char_type(DTYPEG(sptr), sptr));
10902     add_global_define(gitem);
10903   } else if (SOCPTRG(sptr)) {
10904     SNAME(sptr) = get_local_overlap_var();
10905   } else {
10906     /* This is an actual local variable. Create an alloca. */
10907     LL_Object *local;
10908     type = LLTYPE(sptr);
10909 
10910     /* make_lltype_from_sptr() should have added a pointer to the type of
10911      * this local variable. Remove it */
10912     CHECK(type->data_type == LL_PTR);
10913     type = type->sub_types[0];
10914 
10915     /* Now create the alloca for this variable.
10916      * FIXME: Apparently, the AG table is keeping track of local symbols by
10917      * name, but we have no guarantee that locval names are unique. This
10918      * will end in tears. */
10919     local =
10920         ll_create_local_object(llvm_info.curr_func, type, align_of_var(sptr),
10921                                "%s", get_llvm_name(sptr));
10922     SNAME(sptr) = (char *)local->address.data;
10923   }
10924 
10925   addDebugForLocalVar(sptr, type);
10926 }
10927 
10928 static void
gen_name_private_sptr(SPTR sptr)10929 gen_name_private_sptr(SPTR sptr)
10930 {
10931   /* This is an actual local variable. Create an alloca. */
10932   LL_Type *type = LLTYPE(sptr);
10933   LL_Object *local;
10934 
10935   /* make_lltype_from_sptr() should have added a pointer to the type of
10936    * this local variable. Remove it */
10937   CHECK(type->data_type == LL_PTR);
10938   type = type->sub_types[0];
10939 
10940   /* Now create the alloca for this variable.
10941    * FIXME: Apparently, the AG table is keeping track of local symbols by
10942    * name, but we have no guarantee that locval names are unique. This
10943    * will end in tears.
10944    */
10945   local = ll_create_local_object(llvm_info.curr_func, type, align_of_var(sptr),
10946                                  "%s", get_llvm_name(sptr));
10947   SNAME(sptr) = (char *)local->address.data;
10948   addDebugForLocalVar(sptr, type);
10949 }
10950 /* May need to be revisited */
10951 static void
process_private_sptr(SPTR sptr)10952 process_private_sptr(SPTR sptr)
10953 {
10954   if (!gbl.outlined && !TASKG(sptr) && !ISTASKDUPG(GBL_CURRFUNC))
10955     return;
10956 
10957   assert(SCG(sptr) == SC_PRIVATE, "Expected local sptr", sptr, ERR_Fatal);
10958   assert(SNAME(sptr) == NULL, "Already processed sptr", sptr, ERR_Fatal);
10959 
10960   /* TODO: Check enclfuncg's scope and if its is not the same as the
10961    * scope level for -g, then return early, this is not a private sptr
10962    */
10963   sym_is_refd(sptr);
10964 
10965   gen_name_private_sptr(sptr);
10966 }
10967 
10968 /*
10969  * if compiling CPU code, return nonzero if this procedure is attributes(global)
10970  * if compiling GPU code, return nonzero if this proceudre is attributes(global)
10971  *   OR attributes(device)
10972  * in particular, when compiling for CPU, return zero for attributes(device,host),
10973  *   because we're generating the host code
10974  */
10975 INLINE static int
compilingGlobalOrDevice()10976 compilingGlobalOrDevice()
10977 {
10978   int cudag = CUDA_GLOBAL;
10979   if (!CG_cpu_compile)
10980     cudag |= CUDA_DEVICE;
10981   return CUDAG(gbl.currsub) & cudag;
10982 }
10983 
10984 /**
10985    \brief Does this arg's pointer type really need to be dereferenced?
10986    \param sptr   The argument
10987    \return true iff this argument should NOT use the pointer's base type
10988  */
10989 INLINE static bool
processAutoSptr_skip(SPTR sptr)10990 processAutoSptr_skip(SPTR sptr)
10991 {
10992   if (compilingGlobalOrDevice() && DEVICEG(sptr) && !PASSBYVALG(sptr)) {
10993     return (SCG(sptr) == SC_DUMMY) ||
10994            ((SCG(sptr) == SC_BASED) && (SCG(MIDNUMG(sptr)) == SC_DUMMY));
10995   }
10996   return false;
10997 }
10998 
10999 INLINE static LL_Type *
fixup_argument_type(SPTR sptr,LL_Type * type)11000 fixup_argument_type(SPTR sptr, LL_Type *type)
11001 {
11002   if (processAutoSptr_skip(sptr))
11003     return type;
11004   /* type = pointer base type */
11005   return type->sub_types[0];
11006 }
11007 
11008 /**
11009    \brief Process an \c SC_AUTO or \c SC_REGISTER \p sptr
11010    \param sptr  A symbol
11011    Also initialize <tt>SNAME(sptr)</tt>.
11012  */
11013 static void
process_auto_sptr(SPTR sptr)11014 process_auto_sptr(SPTR sptr)
11015 {
11016   LL_Type *type = LLTYPE(sptr);
11017   LL_Object *local;
11018 
11019   /* Accept SC_DUMMY sptrs if they are arguments that have been given local
11020    * variable storage. */
11021   if (SCG(sptr) == SC_DUMMY) {
11022     assert(hashmap_lookup(llvm_info.homed_args, INT2HKEY(sptr), NULL),
11023            "Expected coerced dummy sptr", sptr, ERR_Fatal);
11024   } else {
11025   }
11026   assert(SNAME(sptr) == NULL, "Already processed sptr", sptr, ERR_Fatal);
11027 
11028   /* The hidden return argument is created as an SC_AUTO sptr containing the
11029    * pointer, but it does not need a local entry if we're actually going to
11030    * emit an LLVM IR sret argument which is just a constant pointer.
11031    */
11032   if (ret_info.emit_sret && is_special_return_symbol(sptr)) {
11033     SNAME(sptr) = (char *)ll_create_local_name(llvm_info.curr_func, "sretaddr");
11034     return;
11035   }
11036 
11037   /* make_lltype_from_sptr() should have added a pointer to the type of this
11038    * local variable. Remove it */
11039   CHECK(type->data_type == LL_PTR);
11040   type = fixup_argument_type(sptr, type);
11041 
11042   /* Now create the alloca for this variable. Since the alloca produces the
11043    * address of the local, name it "%foo.addr". */
11044   local = ll_create_local_object(llvm_info.curr_func, type, align_of_var(sptr),
11045                                  "%s.addr", SYMNAME(sptr));
11046   SNAME(sptr) = (char *)local->address.data;
11047 
11048   addDebugForLocalVar(sptr, type);
11049 }
11050 
11051 static void
process_label_sptr_c(SPTR sptr)11052 process_label_sptr_c(SPTR sptr)
11053 {
11054   const char *name = get_llvm_name(sptr);
11055   SNAME(sptr) = (char *)getitem(LLVM_LONGTERM_AREA, strlen(name) + 1);
11056   strcpy(SNAME(sptr), name);
11057 }
11058 
11059 /**
11060    \brief Process an <tt>SC_NONE</tt> \p sptr
11061    \param sptr represents a label
11062    Also initialize <tt>SNAME(sptr)</tt>.
11063  */
11064 static void
process_label_sptr(SPTR sptr)11065 process_label_sptr(SPTR sptr)
11066 {
11067   assert(SCG(sptr) == SC_NONE, "Expected label sptr", sptr, ERR_Fatal);
11068   assert(SNAME(sptr) == NULL, "Already processed sptr", sptr, ERR_Fatal);
11069 
11070   switch (STYPEG(sptr)) {
11071   case ST_CONST:
11072     /* TODO: Move this sooner, into the bridge */
11073     sym_is_refd(sptr);
11074     return;
11075   case ST_MEMBER:
11076     return;
11077   default:
11078     break;
11079   }
11080   process_label_sptr_c(sptr);
11081 }
11082 
11083 static void
process_sptr_offset(SPTR sptr,ISZ_T off)11084 process_sptr_offset(SPTR sptr, ISZ_T off)
11085 {
11086   SC_KIND sc;
11087   DTYPE dtype;
11088   int midnum;
11089   LL_Type *ttype;
11090 
11091   sym_is_refd(sptr);
11092   update_llvm_sym_arrays();
11093   sc = SCG(sptr);
11094 
11095   if (SNAME(sptr))
11096     return;
11097 
11098   DBGTRACEIN7(" sptr %d = '%s' (%s) SNAME(%d)=%p, sc %d, ADDRTKNG(%d)", sptr,
11099               getprint(sptr), stb.scnames[sc], sptr, SNAME(sptr), sc,
11100               ADDRTKNG(sptr));
11101 
11102   ttype = make_lltype_sz4v3_from_sptr(sptr);
11103   LLTYPE(sptr) = ttype;
11104 
11105   switch (sc) {
11106   case SC_CMBLK:
11107     process_cmnblk_data(sptr, off);
11108     set_global_sname(sptr, get_llvm_name(sptr));
11109     break;
11110   case SC_STATIC:
11111     process_static_sptr(sptr, off);
11112     break;
11113 
11114   case SC_EXTERN:
11115     if (
11116         STYPEG(sptr) == ST_PROC || STYPEG(sptr) == ST_ENTRY
11117     ) {
11118       process_extern_function_sptr(sptr);
11119     } else {
11120       process_extern_variable_sptr(sptr, off);
11121     }
11122     break;
11123 
11124   case SC_DUMMY:
11125     midnum = MIDNUMG(sptr);
11126     if (DTYPEG(sptr) == DT_ADDR && midnum &&
11127         hashmap_lookup(llvm_info.homed_args, INT2HKEY(midnum), NULL)) {
11128       LLTYPE(sptr) = LLTYPE(midnum);
11129       SNAME(sptr) = SNAME(midnum);
11130       return;
11131     }
11132     if (hashmap_lookup(llvm_info.homed_args, INT2HKEY(sptr), NULL)) {
11133       process_auto_sptr(sptr);
11134     } else {
11135       set_local_sname(sptr, get_llvm_name(sptr));
11136     }
11137     if ((flg.smp || (XBIT(34, 0x200) || gbl.usekmpc)) &&
11138         (gbl.outlined || ISTASKDUPG(GBL_CURRFUNC))) {
11139       if (sptr == ll_get_shared_arg(gbl.currsub)) {
11140         LLTYPE(sptr) = make_ptr_lltype(make_lltype_from_dtype(DT_INT8));
11141       }
11142     }
11143     DBGTRACE1("#dummy argument: %s", SNAME(sptr));
11144     break;
11145 
11146   case SC_LOCAL:
11147     process_local_sptr(sptr);
11148     break;
11149 
11150   case SC_BASED:
11151     if (compilingGlobalOrDevice() && DEVICEG(sptr)) {
11152       if (hashmap_lookup(llvm_info.homed_args, INT2HKEY(MIDNUMG(sptr)), NULL)) {
11153         process_auto_sptr(sptr);
11154         LLTYPE(MIDNUMG(sptr)) = LLTYPE(sptr);
11155         SNAME(MIDNUMG(sptr)) = SNAME(sptr);
11156       } else {
11157         set_local_sname(sptr, get_llvm_name(sptr));
11158       }
11159     } else
11160       set_local_sname(sptr, get_llvm_name(sptr));
11161     DBGTRACE1("#dummy argument: %s", SNAME(sptr));
11162     break;
11163 
11164   case SC_NONE: /* should be a label */
11165     process_label_sptr(sptr);
11166     break;
11167 
11168 #ifdef SC_PRIVATE
11169   case SC_PRIVATE: /* OpenMP */
11170     process_private_sptr(sptr);
11171     if(!SNAME(sptr)) {
11172       gen_name_private_sptr(sptr);
11173     }
11174     break;
11175 #endif
11176   default:
11177     assert(false, "process_sptr(): unexpected storage type", sc, ERR_Fatal);
11178   }
11179 
11180   DBGTRACEOUT("")
11181 }
11182 
11183 /**
11184    \brief Computes byte offset into aggregate structure of \p sptr
11185    \param sptr  the symbol
11186    \param idx   additional addend to offset
11187    \return an offset into a memory object or 0
11188 
11189    NB: sym_is_refd() must be called prior to this function in order to return
11190    the correct result.
11191  */
11192 static ISZ_T
variable_offset_in_aggregate(SPTR sptr,ISZ_T idx)11193 variable_offset_in_aggregate(SPTR sptr, ISZ_T idx)
11194 {
11195   if (ADDRESSG(sptr) && (SCG(sptr) != SC_DUMMY) && (SCG(sptr) != SC_LOCAL)) {
11196     /* expect:
11197           int2                           int
11198           sptr:301  dtype:6  nmptr:2848  sc:4=CMBLK  stype:6=variable
11199           symlk:1=NOSYM
11200           address:8  enclfunc:295=mymod
11201           midnum:302=_mymod$0
11202 
11203        This can be found in a common block.  Don't add address on stack for
11204        local/dummy arguments */
11205     idx += ADDRESSG(sptr);
11206   } else if ((SCG(sptr) == SC_LOCAL) && SOCPTRG(sptr)) {
11207     idx += get_socptr_offset(sptr);
11208   }
11209   return idx;
11210 }
11211 
11212 void
process_sptr(SPTR sptr)11213 process_sptr(SPTR sptr)
11214 {
11215   process_sptr_offset(sptr, variable_offset_in_aggregate(sptr, 0));
11216 }
11217 
11218 /* ipa sometimes makes additional symbol entries for external variables (I have
11219  * noticed this mainly on globally-defined anonymous structures). However, since
11220  * LLVM requires all references to be declared within the file that they are
11221  * referenced, this may result in multiple declararions of the same symbol. Does
11222  * not work with the opt and llc tools of LLVM.  Thus we try to track down, if
11223  * possible, the original symbol of storage class extern. Follow the links as
11224  * far as possible.
11225  */
11226 static int
follow_sptr_hashlk(SPTR sptr)11227 follow_sptr_hashlk(SPTR sptr)
11228 {
11229   char *hash_name, *name = get_llvm_name(sptr);
11230   int ret_val = 0;
11231   int hashlk = HASHLKG(sptr);
11232   while (hashlk > 0) {
11233     hash_name = get_llvm_name((SPTR)hashlk);
11234     if (SCG(hashlk) == SC_EXTERN && !strcmp(name, hash_name))
11235       ret_val = hashlk;
11236     ret_val = hashlk;
11237     hashlk = HASHLKG(hashlk);
11238   }
11239   return ret_val;
11240 }
11241 
11242 static DTYPE
follow_ptr_dtype(DTYPE dtype)11243 follow_ptr_dtype(DTYPE dtype)
11244 {
11245   DTYPE dty = dtype;
11246   while (DTY(dty) == TY_PTR)
11247     dty = DTySeqTyElement(dty);
11248   return dty;
11249 }
11250 
11251 bool
strict_match(LL_Type * ty1,LL_Type * ty2)11252 strict_match(LL_Type *ty1, LL_Type *ty2)
11253 {
11254   return (ty1 == ty2);
11255 }
11256 
11257 /*
11258  * both ty1 & ty2 are of kind LLT_FUNCTION
11259  * check that protoypes are matchings
11260  */
11261 INLINE static bool
match_prototypes(LL_Type * ty1,LL_Type * ty2)11262 match_prototypes(LL_Type *ty1, LL_Type *ty2)
11263 {
11264   return strict_match(ty1, ty2);
11265 }
11266 
11267 /**
11268    \brief Does \c ty2 "match" \c ty1 or can \c ty2 be converted to \c ty1?
11269    \param ty1  the result type
11270    \param ty2  a type
11271 
11272    If \c ty1 is a ptr and \c ty2 is not, we have an error.  In general, if the
11273    nesting level of \c ty1 is greater than that of \c ty2, then we have an
11274    error.
11275 
11276    NB: The original algorithm did \e NOT enforce the latter condition above. The
11277    old algorithm would peel off all outer levels of array types blindly and
11278    until a non-array element type is found. This implied that this function
11279    would return \c MATCH_OK when the input types are \c i32 and <code>[A x [B x
11280    [C x [D x i32]]]]</code>.
11281  */
11282 static MATCH_Kind
match_types(LL_Type * ty1,LL_Type * ty2)11283 match_types(LL_Type *ty1, LL_Type *ty2)
11284 {
11285   MATCH_Kind ret_type;
11286   int base_ty1, base_ty2, ct1, ct2;
11287   LL_Type *llt1, *llt2;
11288 
11289   assert(ty1 && ty2, "match_types(): missing argument", 0, ERR_Fatal);
11290 
11291   DBGTRACEIN2("match_types: ty1=%s, ty2=%s\n", ty1->str, ty2->str);
11292   if (ty1 == ty2)
11293     return MATCH_OK;
11294 
11295   if (ty1->data_type == LL_ARRAY) {
11296     LL_Type *ele1 = ll_type_array_elety(ty1);
11297     LL_Type *ele2 = ll_type_array_elety(ty2);
11298     return ele2 ? match_types(ele1, ele2) : MATCH_NO;
11299   }
11300 
11301   if ((ty1->data_type == LL_PTR) || (ty2->data_type == LL_PTR)) {
11302     /* at least one pointer type */
11303     if (ty2->data_type != LL_PTR) {
11304       /* reject as only ty1 is a ptr */
11305       ret_type = MATCH_NO;
11306     } else {
11307       /* get the depth of each pointer type */
11308       ct1 = 0;
11309       llt1 = ty1;
11310       while (llt1->data_type == LL_PTR) {
11311         asrt(llt1);
11312         ct1++;
11313         llt1 = llt1->sub_types[0];
11314       }
11315       ct2 = 0;
11316       llt2 = ty2;
11317       while (llt2->data_type == LL_PTR) {
11318         asrt(llt2);
11319         ct2++;
11320         llt2 = llt2->sub_types[0];
11321       }
11322       if (ct1 > ct2) {
11323         ret_type = MATCH_NO;
11324       } else if (match_types(llt1, llt2) == MATCH_OK) {
11325         if (ct1 == ct2)
11326           ret_type = MATCH_OK; // ptrs have same level of indirection only
11327         else if (ct1 + 1 == ct2)
11328           ret_type = MATCH_MEM;
11329         else
11330           ret_type = MATCH_NO;
11331       } else if ((llt1->data_type == LL_VOID) || (llt2->data_type == LL_VOID)) {
11332         // one or the other is ptr-to-void; implies, void* == T*****
11333         ret_type = MATCH_OK;
11334       } else {
11335         ret_type = MATCH_NO;
11336       }
11337     }
11338   } else if (ty1->data_type == ty2->data_type) {
11339     if (ty1->data_type == LL_STRUCT) {
11340       ret_type = ((ty1 == ty2) ? MATCH_OK : MATCH_NO);
11341     } else if (ty1->data_type == LL_FUNCTION) {
11342       /* ??? used to check if both FUNC types were "old-style" or not.
11343          "Old-style" meant that (DTY(dtype) == TY_FUNC). Why would it matter?
11344          This doesn't otherwise check the signature. */
11345       ret_type = MATCH_OK;
11346     } else {
11347       ret_type = MATCH_OK;
11348     }
11349   } else {
11350     ret_type = MATCH_NO;
11351   }
11352 
11353   if (ll_type_int_bits(ty1)) {
11354     DBGTRACEOUT4(" returns %d(%s) ty1 = %s%d", ret_type, match_names(ret_type),
11355                  ty1->str, (int)(ll_type_bytes(ty1) * 8))
11356   } else {
11357     DBGTRACEOUT3(" returns %d(%s) ty1 = %s", ret_type, match_names(ret_type),
11358                  ty1->str)
11359   }
11360   return ret_type;
11361 } /* match_types */
11362 
11363 int
match_llvm_types(LL_Type * ty1,LL_Type * ty2)11364 match_llvm_types(LL_Type *ty1, LL_Type *ty2)
11365 {
11366   return match_types(ty1, ty2);
11367 }
11368 
11369 static LL_Type *
make_type_from_opc(ILI_OP opc)11370 make_type_from_opc(ILI_OP opc)
11371 {
11372   LL_Type *llt;
11373 
11374   DBGTRACEIN1(" (%s)", IL_NAME(opc))
11375   /* these opcodes will come from conversion operations and expression
11376    * evaluation without a store, such as:
11377    *           if( j << 2 )
11378    *           if (j - (float)3.0)
11379    * the other possibility is jump ILI with expressions, or cast due
11380    * to array manipulations. These are mostly
11381    * of integer type, as the the evaluation of a condition is inherently
11382    * integral. However, notice first two cases, which are of type LLT_PTR.
11383    */
11384   switch (opc) {
11385   case IL_ACMP:
11386   case IL_ACMPZ:
11387   case IL_ACJMP:
11388   case IL_ACJMPZ:
11389   case IL_ASELECT:
11390     llt = make_lltype_from_dtype(DT_CPTR);
11391     break;
11392   case IL_ICJMP:
11393   case IL_UICJMP:
11394   case IL_FIX:
11395   case IL_AND:
11396   case IL_OR:
11397   case IL_XOR:
11398   case IL_NOT:
11399   case IL_MOD:
11400   case IL_MODZ:
11401   case IL_LSHIFT:
11402   case IL_RSHIFT:
11403   case IL_ARSHIFT:
11404   case IL_ICON:
11405   case IL_ICJMPZ:
11406   case IL_UICJMPZ:
11407   case IL_UICMPZ:
11408   case IL_IADD:
11409   case IL_ISUB:
11410   case IL_IMUL:
11411   case IL_IDIVZ:
11412   case IL_IDIV:
11413   case IL_UIADD:
11414   case IL_UISUB:
11415   case IL_UIMUL:
11416   case IL_UIDIV:
11417   case IL_INEG:
11418   case IL_UINEG:
11419   case IL_DFIX:
11420   case IL_DFIXU:
11421   case IL_ICMP:
11422   case IL_ICMPZ:
11423   case IL_ISELECT:
11424   case IL_IMIN:
11425   case IL_IMAX:
11426   case IL_UIMIN:
11427   case IL_UIMAX:
11428   case IL_IABS:
11429   case IL_CMPXCHG_OLDI:
11430     llt = make_lltype_from_dtype(DT_INT);
11431     break;
11432   case IL_KAND:
11433   case IL_KLSHIFT:
11434   case IL_KCJMP:
11435   case IL_KCON:
11436   case IL_KADD:
11437   case IL_KSUB:
11438   case IL_KMUL:
11439   case IL_KDIV:
11440   case IL_KDIVZ:
11441   case IL_KNOT:
11442   case IL_KCJMPZ:
11443   case IL_KOR:
11444   case IL_FIXK:
11445   case IL_KXOR:
11446   case IL_KMOD:
11447   case IL_KARSHIFT:
11448   case IL_KNEG:
11449   case IL_KCMP:
11450   case IL_KCMPZ:
11451   case IL_UKMIN:
11452   case IL_UKMAX:
11453   case IL_KMIN:
11454   case IL_KMAX:
11455   case IL_KSELECT:
11456   case IL_KABS:
11457   case IL_CMPXCHG_OLDKR:
11458     llt = make_lltype_from_dtype(DT_INT8);
11459     break;
11460   case IL_KUMOD:
11461   case IL_KUMODZ:
11462   case IL_UKDIV:
11463   case IL_UKDIVZ:
11464   case IL_FIXUK:
11465   case IL_KURSHIFT:
11466   case IL_UKCMP:
11467   case IL_DFIXK:
11468   case IL_DFIXUK:
11469   case IL_UKCJMP:
11470   case IL_UKADD:
11471   case IL_UKSUB:
11472   case IL_UKMUL:
11473   case IL_UKCJMPZ:
11474   case IL_UKCMPZ:
11475   case IL_UKNEG:
11476   case IL_UKNOT:
11477     llt = make_lltype_from_dtype(DT_UINT8);
11478     break;
11479   case IL_UNOT:
11480   case IL_UFIX:
11481   case IL_UIMOD:
11482   case IL_UIMODZ:
11483   case IL_UIDIVZ:
11484   case IL_ULSHIFT:
11485   case IL_URSHIFT:
11486   case IL_UICMP:
11487     llt = make_lltype_from_dtype(DT_UINT);
11488     break;
11489   case IL_FLOAT:
11490   case IL_FLOATU:
11491   case IL_FLOATK:
11492   case IL_FLOATUK:
11493   case IL_FMOD:
11494   case IL_SNGL:
11495   case IL_FSUB:
11496   case IL_FMUL:
11497   case IL_FDIV:
11498   case IL_FADD:
11499   case IL_FCON:
11500   case IL_FNEG:
11501   case IL_FCJMP:
11502   case IL_FCJMPZ:
11503   case IL_FCMP:
11504   case IL_CMPNEQSS:
11505   case IL_FMIN:
11506   case IL_FMAX:
11507   case IL_FABS:
11508   case IL_FSELECT:
11509     llt = make_lltype_from_dtype(DT_FLOAT);
11510     break;
11511   case IL_DCJMP:
11512   case IL_DCJMPZ:
11513   case IL_DFLOAT:
11514   case IL_DFLOATU:
11515   case IL_DFLOATK:
11516   case IL_DFLOATUK:
11517   case IL_DMOD:
11518   case IL_DBLE:
11519   case IL_DADD:
11520   case IL_DSUB:
11521   case IL_DNEG:
11522   case IL_DMAX:
11523   case IL_DMIN:
11524   case IL_DMUL:
11525   case IL_DDIV:
11526   case IL_DCON:
11527   case IL_DCMP:
11528   case IL_DSELECT:
11529   case IL_DABS:
11530     llt = make_lltype_from_dtype(DT_DBLE);
11531     break;
11532   case IL_CSSELECT:
11533   case IL_SCMPLXADD:
11534     llt = make_lltype_from_dtype(DT_CMPLX);
11535     break;
11536   case IL_CDSELECT:
11537   case IL_DCMPLXADD:
11538     llt = make_lltype_from_dtype(DT_DCMPLX);
11539     break;
11540   case IL_ALLOC:
11541     llt = make_lltype_from_dtype(DT_CPTR);
11542     break;
11543 #ifdef LONG_DOUBLE_FLOAT128
11544   case IL_FLOAT128CON:
11545   case IL_FLOAT128ABS:
11546   case IL_FLOAT128CHS:
11547   case IL_FLOAT128RNDINT:
11548   case IL_FLOAT128FROM:
11549   case IL_FLOAT128ADD:
11550   case IL_FLOAT128SUB:
11551   case IL_FLOAT128MUL:
11552   case IL_FLOAT128DIV:
11553   case IL_FLOAT128CMP:
11554     llt = make_lltype_from_dtype(DT_FLOAT128);
11555     break;
11556   case IL_FLOAT128TO:
11557     llt = make_lltype_from_dtype(DT_DBLE);
11558     break;
11559 #endif
11560   default:
11561     DBGTRACE2("###make_type_from_opc(): unknown opc %d(%s)", opc, IL_NAME(opc))
11562     assert(0, "make_type_from_opc: unknown opc", opc, ERR_Fatal);
11563     llt = NULL;
11564   }
11565 
11566   DBGTRACEOUT1(" returns %p", llt)
11567   return llt;
11568 } /* make_type_from_opc */
11569 
11570 static LL_Type *
make_type_from_msz_with_addrspace(MSZ msz,int addrspace)11571 make_type_from_msz_with_addrspace(MSZ msz, int addrspace)
11572 {
11573   return make_lltype_from_dtype_with_addrspace(msz_dtype(msz), addrspace);
11574 } /* make_type_from_msz_with_addrspace */
11575 
11576 static LL_Type *
make_type_from_msz(MSZ msz)11577 make_type_from_msz(MSZ msz)
11578 {
11579   return make_type_from_msz_with_addrspace(msz, LL_AddrSp_Default);
11580 } /* make_type_from_msz */
11581 
11582 static LL_Type *
make_vtype(DTYPE dtype,int sz)11583 make_vtype(DTYPE dtype, int sz)
11584 {
11585   LL_Type *llt;
11586   DTYPE vect_dtype;
11587   vect_dtype = get_vector_dtype(dtype, sz);
11588   return make_lltype_from_dtype(vect_dtype);
11589 } /* make_vtype */
11590 
11591 static int
is_special_return_symbol(int sptr)11592 is_special_return_symbol(int sptr)
11593 {
11594   return ret_info.sret_sptr == sptr;
11595 }
11596 
11597 int
need_ptr(int sptr,int sc,DTYPE sdtype)11598 need_ptr(int sptr, int sc, DTYPE sdtype)
11599 {
11600   if (is_special_return_symbol(sptr))
11601     return DTY(sdtype) != TY_PTR;
11602 
11603   switch (sc) {
11604   case SC_EXTERN:
11605     return true;
11606 
11607   case SC_STATIC:
11608     return !DINITG(sptr) || !SAVEG(sptr);
11609 
11610 #ifdef SC_PRIVATE
11611   case SC_PRIVATE:
11612     return true;
11613 #endif
11614   case SC_LOCAL:
11615   case SC_CMBLK:
11616     return true;
11617 
11618   case SC_DUMMY:
11619     /* process_formal_arguments() homes all dummies. */
11620     return true;
11621   }
11622 
11623   if (sptr)
11624     switch (STYPEG(sptr)) {
11625     case ST_ARRAY:
11626       return true;
11627     case ST_MEMBER:
11628       if (DTY(sdtype) == TY_ARRAY)
11629         return true;
11630       break;
11631     default:
11632       break;
11633     }
11634 
11635   return false;
11636 }
11637 
11638 static OPERAND *
gen_sptr(SPTR sptr)11639 gen_sptr(SPTR sptr)
11640 {
11641   SC_KIND sc;
11642   DTYPE dtype;
11643   OPERAND *sptr_operand, *operand2;
11644 
11645   DBGTRACEIN2(" sptr %d (%s)", sptr, SYMNAME(sptr))
11646 
11647   sptr_operand = make_operand();
11648   sc = SCG(sptr);
11649   process_sptr(sptr);
11650   sptr_operand->ll_type = LLTYPE(sptr);
11651   switch (sc) {
11652   case SC_CMBLK:
11653   case SC_DUMMY:
11654 #ifdef SC_PRIVATE
11655   case SC_PRIVATE:
11656 #endif
11657   case SC_STATIC:
11658   case SC_EXTERN:
11659   case SC_AUTO:
11660     DBGTRACE2("#using this name for %s; %s", SYMNAME(sptr), SNAME(sptr))
11661 
11662     sptr_operand->ot_type = OT_VAR;
11663     sptr_operand->val.sptr = sptr;
11664     sptr_operand->string = SNAME(sptr);
11665     break;
11666   case SC_NONE:
11667     /* For some constants, we need its address to pass to our runtime.
11668        They also need to be initialized.
11669      */
11670     if (STYPEG(sptr) == ST_CONST) {
11671       sptr_operand->ot_type = OT_VAR;
11672       sptr_operand->val.sptr = sptr;
11673       sptr_operand->string = SNAME(sptr);
11674       break;
11675     }
11676   /* TBD */
11677   case SC_BASED:
11678   default:
11679     assert(0, "gen_sptr(): unexpected storage type", sc, ERR_Fatal);
11680   }
11681 #ifdef OMP_OFFLOAD_LLVM
11682 #endif
11683   DBGTRACEOUT1(" returns operand %p", sptr_operand)
11684   return sptr_operand;
11685 } /* gen_sptr */
11686 
11687 /**
11688    \brief Generate an address expression as an operand
11689    \param addr_op	ILI of address expression
11690    \param nme		NME value
11691    \param lda		is this an IL_LDA?
11692    \param llt_expected  expected LL_Type
11693    \param msz		memory access size
11694  */
11695 OPERAND *
gen_address_operand(int addr_op,int nme,bool lda,LL_Type * llt_expected,MSZ msz)11696 gen_address_operand(int addr_op, int nme, bool lda, LL_Type *llt_expected,
11697                     MSZ msz)
11698 {
11699   OPERAND *operand;
11700   OPERAND **csed_operand;
11701   LL_Type *llt = llt_expected;
11702   SPTR sptr = basesym_of(nme);
11703   unsigned savedAddressSize = addressElementSize;
11704   int addrspace = LL_AddrSp_Default;
11705   DBGTRACEIN2(" for ilix: %d(%s)", addr_op, IL_NAME(ILI_OPC(addr_op)))
11706   DBGDUMPLLTYPE("expected type ", llt_expected)
11707 
11708 #ifdef OMP_OFFLOAD_LLVM
11709   addrspace = OMPACCSHMEMG(sptr) ? LL_AddrSp_NVVM_Shared : LL_AddrSp_NVVM_Generic;
11710 #endif
11711 
11712   if (!llt && !lda && (((int)msz) >= 0)) {
11713     llt = make_ptr_lltype(make_type_from_msz_with_addrspace(msz, addrspace));
11714   }
11715   sptr = basesym_of(nme);
11716 
11717   if (llt) {
11718     /* do nothing */
11719   } else {
11720     if (sptr <= 0) {
11721       int sub_add = addr_op, sub_opc;
11722       llt = make_lltype_from_dtype(DT_CPTR);
11723       while (sub_add) {
11724         sub_opc = ILI_OPC(sub_add);
11725         switch (sub_opc) {
11726         case IL_AADD:
11727         case IL_ASUB:
11728           sub_add = ILI_OPND(sub_add, 1);
11729           continue;
11730         case IL_DFRAR:
11731         case IL_ACON:
11732           llt = make_ptr_lltype(llt);
11733           sub_add = 0;
11734           break;
11735         case IL_LDA:
11736           llt = make_ptr_lltype(llt);
11737           sub_add = ILI_OPND(sub_add, 1);
11738           break;
11739         default:
11740           sub_add = 0;
11741         }
11742       }
11743     } else {
11744       llt = make_lltype_from_sptr(sptr);
11745       DBGTRACE4("#lda of sptr '%s' for type %d %d %d", getprint(sptr),
11746                 STYPEG(sptr), SCG(sptr), llt->data_type);
11747 
11748       if ((SCG(sptr) == SC_DUMMY) || (SCG(sptr) == SC_AUTO)) {
11749         const int midnum = MIDNUMG(sptr);
11750         if (midnum && STYPEG(midnum) == ST_PROC) {
11751           assert(LLTYPE(midnum), "process_sptr() never called for sptr", sptr,
11752                  ERR_Fatal);
11753           llt = LLTYPE(midnum);
11754         } else if ((flg.smp || XBIT(34, 0x200) || gbl.usekmpc) &&
11755                    (gbl.outlined || ISTASKDUPG(GBL_CURRFUNC)) &&
11756                    (sptr == ll_get_shared_arg(gbl.currsub))) {
11757           llt = LLTYPE(sptr);
11758         } else
11759 #ifdef TARGET_LLVM_ARM
11760             if ((llt->data_type == LL_STRUCT) && (NME_SYM(nme) != sptr)) {
11761           llt = make_ptr_lltype(make_lltype_from_dtype(DT_CPTR));
11762         } else
11763 #endif
11764             if ((llt->data_type == LL_PTR) &&
11765                 (llt->sub_types[0]->data_type != LL_PTR) &&
11766                 NME_SYM(nme) != sptr) {
11767           llt = make_ptr_lltype(make_lltype_from_dtype(DT_CPTR));
11768         }
11769         if ((STYPEG(sptr) != ST_VAR) && (ASSNG(sptr) || ADDRTKNG(sptr))) {
11770           if ((llt->data_type == LL_PTR) &&
11771               (llt->sub_types[0]->data_type != LL_PTR)) {
11772             llt = make_ptr_lltype(make_lltype_from_dtype(DT_CPTR));
11773           }
11774         }
11775       } else if ((STYPEG(sptr) != ST_VAR) &&
11776                  ((llt->data_type != LL_PTR) ||
11777                   (llt->sub_types[0]->data_type != LL_PTR))) {
11778         llt = make_ptr_lltype(make_lltype_from_dtype(DT_CPTR));
11779       }
11780     }
11781   }
11782   addressElementSize = (llt->data_type == LL_PTR)
11783                            ? ll_type_bytes_unchecked(llt->sub_types[0])
11784                            : 0;
11785   operand = gen_base_addr_operand(addr_op, llt);
11786 
11787   DBGTRACEOUT("")
11788 
11789   csed_operand = get_csed_operand(addr_op);
11790   if (csed_operand != NULL) {
11791     set_csed_operand(csed_operand, operand);
11792   }
11793 
11794   ILI_COUNT(addr_op)++;
11795   addressElementSize = savedAddressSize;
11796   return operand;
11797 }
11798 
11799 /**
11800    \brief Generate an OPERAND representing the value of the ACON ilix.
11801    \param ilix           An IL_ACON ilix
11802    \param expected_type  The expected type of the result
11803    \return an OPERAND
11804  */
11805 static OPERAND *
gen_acon_expr(int ilix,LL_Type * expected_type)11806 gen_acon_expr(int ilix, LL_Type *expected_type)
11807 {
11808   SPTR sptr;
11809   DTYPE dtype;
11810   ISZ_T idx;
11811   LL_Type *ty1;
11812   OPERAND *base_op, *index_op;
11813   OPERAND *operand = NULL;
11814   const SPTR opnd = ILI_SymOPND(ilix, 1);
11815   const int ptrbits = 8 * size_of(DT_CPTR);
11816   INT val[2];
11817   ISZ_T num;
11818 
11819   assert(ILI_OPC(ilix) == IL_ACON, "gen_acon_expr: acon expected", ilix,
11820          ERR_Fatal);
11821   assert(STYPEG(opnd) == ST_CONST, "gen_acon_expr: ST_CONST argument expected",
11822          ilix, ERR_Fatal);
11823 
11824   /* Handle integer constants, converting to a pointer-sized integer */
11825   dtype = DTYPEG(opnd);
11826   if (DT_ISINT(dtype)) {
11827     INT hi = CONVAL1G(opnd);
11828     INT lo = CONVAL2G(opnd);
11829 
11830     /* Sign-extend DT_INT to 64 bits */
11831     if (dtype == DT_INT && ptrbits == 64)
11832       hi = lo < 0 ? -1 : 0;
11833     return make_constval_op(make_int_lltype(ptrbits), lo, hi);
11834   }
11835 
11836   /* With integers handled above, there should only be DT_CPTR constants left.
11837    * Apparently we sometimes generate DT_IPTR constants too (for wide string
11838    * constants) */
11839   assert(DTY(dtype) == TY_PTR,
11840          "gen_acon_expr: Expected pointer or integer constant", ilix,
11841          ERR_Fatal);
11842 
11843   /* Handle pointer constants with no base symbol table pointer.
11844    * This also becomes a pointer-sized integer */
11845   sptr = SymConval1(opnd);
11846   if (!sptr) {
11847     num = ACONOFFG(opnd);
11848     ISZ_2_INT64(num, val);
11849     return make_constval_op(make_int_lltype(ptrbits), val[1], val[0]);
11850   }
11851   sym_is_refd(sptr);
11852   idx = (STYPEG(sptr) == ST_STRUCT || STYPEG(sptr) == ST_ARRAY
11853          || ACONOFFG(opnd) < 0) ? 0 : ACONOFFG(opnd);
11854   process_sptr_offset(sptr, variable_offset_in_aggregate(sptr, idx));
11855   idx = ACONOFFG(opnd); /* byte offset */
11856 
11857   ty1 = make_lltype_from_dtype(DT_ADDR);
11858   idx = variable_offset_in_aggregate(sptr, idx);
11859   if (idx) {
11860     base_op = gen_sptr(sptr);
11861     index_op = NULL;
11862     base_op = make_bitcast(base_op, ty1);
11863     ISZ_2_INT64(idx, val); /* make a index operand */
11864     index_op = make_constval_op(make_int_lltype(ptrbits), val[1], val[0]);
11865     operand = gen_gep_op(ilix, base_op, ty1, index_op);
11866   } else {
11867     operand = gen_sptr(sptr);
11868     /* SC_DUMMY - address constant .cxxxx */
11869     if (compilingGlobalOrDevice() && SCG(sptr) == SC_DUMMY &&
11870         DTYPEG(sptr) == DT_ADDR) {
11871       /* scalar argument */
11872       int midnum = MIDNUMG(sptr);
11873       if (midnum && DEVICEG(midnum) && !PASSBYVALG(midnum))
11874         operand->ll_type = make_ptr_lltype(operand->ll_type);
11875       else if (DTY(DTYPEG(midnum)) == TY_PTR ||
11876                DTY(DTYPEG(midnum)) == TY_ARRAY) /* pointer */
11877         operand->ll_type = make_ptr_lltype(operand->ll_type);
11878     }
11879   }
11880 
11881   if (operand->ll_type && VOLG(sptr))
11882     operand->flags |= OPF_VOLATILE;
11883   return operand;
11884 }
11885 
11886 /**
11887    \brief Pattern match the ILI tree and fold when there is a match
11888    \param addr  The ILI to pattern match
11889    \param size  The expected type size
11890  */
11891 static OPERAND *
attempt_gep_folding(int addr,BIGINT64 size)11892 attempt_gep_folding(int addr, BIGINT64 size)
11893 {
11894   int kmul, kcon;
11895   BIGINT64 val;
11896   OPERAND *op;
11897 
11898   if (ILI_OPC(addr) != IL_KAMV)
11899     return NULL;
11900   kmul = ILI_OPND(addr, 1);
11901   if (ILI_OPC(kmul) != IL_KMUL)
11902     return NULL;
11903   kcon = ILI_OPND(kmul, 2);
11904   if (ILI_OPC(kcon) != IL_KCON)
11905     return NULL;
11906   val = ((BIGINT64)CONVAL1G(ILI_OPND(kcon, 1))) << 32;
11907   val |= CONVAL2G(ILI_OPND(kcon, 1)) & (0xFFFFFFFF);
11908   if (val != size)
11909     return NULL;
11910   /* at this point we are going to drop the explicit multiply */
11911   op = gen_llvm_expr(ILI_OPND(kmul, 1), make_int_lltype(64));
11912   return op;
11913 }
11914 
11915 /**
11916    \brief Attempt to convert explicit pointer scaling into GEP
11917    \param aadd	An IL_AADD
11918    \param idxOp	The index expression to be checked
11919 
11920    Do \e not assume \p idxOp is the same as <tt>ILI_OPND(aadd, 2)</tt>.
11921  */
11922 static OPERAND *
maybe_do_gep_folding(int aadd,int idxOp,LL_Type * ty)11923 maybe_do_gep_folding(int aadd, int idxOp, LL_Type *ty)
11924 {
11925   int baseOp;
11926   OPERAND *rv;
11927   LL_Type *i8ptr;
11928   unsigned savedAddressElementSize;
11929 
11930   if (addressElementSize == 0)
11931     return NULL;
11932 
11933   baseOp = ILI_OPND(aadd, 1);
11934   i8ptr = make_lltype_from_dtype(DT_CPTR);
11935   if (ty == i8ptr) {
11936     if (addressElementSize != TARGET_PTRSIZE)
11937       return NULL;
11938     ty = ll_get_pointer_type(ty);
11939   }
11940 
11941   savedAddressElementSize = addressElementSize;
11942   addressElementSize = 0;
11943 
11944   /* 1. check if idxOp is a scaled expression */
11945   rv = attempt_gep_folding(idxOp, savedAddressElementSize);
11946   if (rv) {
11947     OPERAND *base = gen_base_addr_operand(baseOp, ty);
11948     rv = gen_gep_op(aadd, base, ty, rv);
11949     return rv;
11950   }
11951 
11952   /* 2. check if baseOp is a scaled expression */
11953   rv = attempt_gep_folding(baseOp, savedAddressElementSize);
11954   if (rv) {
11955     OPERAND *index = gen_base_addr_operand(idxOp, ty);
11956     rv = gen_gep_op(aadd, index, ty, rv);
11957     return rv;
11958   }
11959 
11960   addressElementSize = savedAddressElementSize;
11961   return NULL;
11962 }
11963 
11964 static OPERAND *
gen_base_addr_operand(int ilix,LL_Type * expected_type)11965 gen_base_addr_operand(int ilix, LL_Type *expected_type)
11966 {
11967   OPERAND *operand = NULL, *base_op, *index_op, *cast_op;
11968   OPERAND **csed_operand;
11969   LL_Type *ty1, *ty2;
11970   int opnd = 0;
11971   int nme;
11972 
11973   DBGTRACEIN2(" for ilix: %d(%s), expected_type ", ilix, IL_NAME(ILI_OPC(ilix)))
11974   DBGDUMPLLTYPE("expected type ", expected_type)
11975 
11976   switch (ILI_OPC(ilix)) {
11977   case IL_ASUB:
11978     if (!ll_type_int_bits(expected_type)) {
11979       switch (ILI_OPC(ILI_OPND(ilix, 2))) {
11980       case IL_IAMV:
11981         opnd = ad1ili(IL_AIMV, ILI_OPND(ilix, 2));
11982         opnd = ad2ili(IL_ISUB, ad_icon(0), opnd);
11983         break;
11984       case IL_KAMV:
11985         opnd = ad1ili(IL_AKMV, ILI_OPND(ilix, 2));
11986         opnd = ad2ili(IL_KSUB, ad_kconi(0), opnd);
11987         break;
11988       default:
11989         if (size_of(DT_CPTR) == 8) {
11990           opnd = ad1ili(IL_AKMV, ILI_OPND(ilix, 2));
11991           opnd = ad2ili(IL_KSUB, ad_kconi(0), opnd);
11992         } else {
11993           opnd = ad1ili(IL_AIMV, ILI_OPND(ilix, 2));
11994           opnd = ad2ili(IL_ISUB, ad_icon(0), opnd);
11995         }
11996       }
11997     } else {
11998       if (size_of(DT_CPTR) == 8) {
11999         opnd = ad1ili(IL_AKMV, ILI_OPND(ilix, 2));
12000         opnd = ad2ili(IL_KSUB, ad_kconi(0), opnd);
12001         opnd = ad1ili(IL_KAMV, opnd);
12002       } else {
12003         opnd = ad1ili(IL_AIMV, ILI_OPND(ilix, 2));
12004         opnd = ad2ili(IL_ISUB, ad_icon(0), opnd);
12005         opnd = ad1ili(IL_IAMV, opnd);
12006       }
12007     }
12008   case IL_AADD:
12009     opnd = opnd ? opnd : ILI_OPND(ilix, 2);
12010     operand = (XBIT(183, 0x40000))
12011                   ? NULL
12012                   : maybe_do_gep_folding(ilix, opnd, expected_type);
12013     if (!operand) {
12014       ty1 = make_lltype_from_dtype(DT_CPTR);
12015       base_op = gen_base_addr_operand(ILI_OPND(ilix, 1), ty1);
12016       ty2 = make_int_lltype(8 * size_of(DT_CPTR));
12017       index_op = gen_base_addr_operand(opnd, ty2);
12018       operand = gen_gep_op(ilix, base_op, ty1, index_op);
12019     }
12020     break;
12021   case IL_ACON:
12022     operand = gen_acon_expr(ilix, expected_type);
12023     break;
12024   default:
12025     /* third arg must be 0 since we're not generating a GEP in this case */
12026     operand = gen_llvm_expr(ilix, expected_type);
12027   }
12028   if (expected_type)
12029     ty1 = expected_type;
12030   else
12031     goto _exit_gen_base_addr_operand;
12032   ty2 = operand->ll_type;
12033 
12034   DBGDUMPLLTYPE("#operand type ", ty2);
12035   DBGDUMPLLTYPE("#expected type ", ty1);
12036 
12037   if (ll_type_int_bits(ty1) && ll_type_int_bits(ty2)) {
12038     if (ll_type_int_bits(ty1) != ll_type_int_bits(ty2)) {
12039       operand = convert_int_size(ilix, operand, ty1);
12040     }
12041     goto _exit_gen_base_addr_operand;
12042   } else if ((ty1->data_type == LL_PTR) && (ty2->data_type == LL_PTR)) {
12043     /* both pointers, but pointing to different types */
12044     LL_Type *tty1 = NULL, *tty2 = NULL;
12045 
12046     DBGTRACE("#both are pointers, checking if they are pointing to same type")
12047 
12048     if (ty2->sub_types[0]->data_type == LL_ARRAY) {
12049       tty1 = ty1;
12050       tty2 = ty2;
12051     }
12052     if (tty1 || tty2) {
12053 
12054       while (tty1->data_type == tty2->data_type) {
12055         if ((tty1->data_type == LL_PTR) || (tty1->data_type == LL_ARRAY)) {
12056           tty1 = tty1->sub_types[0];
12057           tty2 = tty2->sub_types[0];
12058         } else {
12059           break;
12060         }
12061       }
12062       if (ll_type_int_bits(tty1) && ll_type_int_bits(tty2) &&
12063           (ll_type_int_bits(tty1) != ll_type_int_bits(tty2))) {
12064         const int flags = operand->flags & (OPF_SEXT | OPF_ZEXT | OPF_VOLATILE);
12065         operand = make_bitcast(operand, ty1);
12066         operand->flags |= flags;
12067       } else if (tty1->data_type != LL_NOTYPE) {
12068         operand = make_bitcast(operand, ty1);
12069       }
12070     } else if (!strict_match(ty1->sub_types[0], ty2->sub_types[0])) {
12071       DBGTRACE("#no strict match between pointers")
12072 
12073       operand = make_bitcast(operand, ty1);
12074     } else {
12075       LL_Type *ety1 = ty1->sub_types[0];
12076       LL_Type *ety2 = ty2->sub_types[0];
12077       DBGTRACE("#strict match between pointers,"
12078                " checking signed/unsigned conflicts");
12079       while (ety1->data_type == ety2->data_type) {
12080         if ((ety1->data_type == LL_PTR) || (ety1->data_type == LL_ARRAY)) {
12081           ety1 = ety1->sub_types[0];
12082           ety2 = ety2->sub_types[0];
12083         } else {
12084           break;
12085         }
12086       }
12087       if (ll_type_int_bits(ety1) && ll_type_int_bits(ety2) &&
12088           (ll_type_int_bits(ety1) != ll_type_int_bits(ety2))) {
12089         const int flags = operand->flags & (OPF_SEXT | OPF_ZEXT | OPF_VOLATILE);
12090         operand = make_bitcast(operand, ty1);
12091         operand->flags |= flags;
12092       }
12093     }
12094   } else if ((ty1->data_type == LL_PTR) && ll_type_int_bits(ty2)) {
12095     if ((operand->ot_type == OT_CONSTVAL) && (!operand->val.conval[0]) &&
12096         (!operand->val.conval[1])) {
12097       // rewrite: cast(iN 0) to T*  ==>  (T* null)
12098       operand = make_constval_op(ty1, 0, 0);
12099       operand->flags |= OPF_NULL_TYPE;
12100     } else if ((operand->ot_type != OT_VAR) ||
12101                (!ll_type_int_bits(ty1->sub_types[0]))) {
12102       operand = convert_int_to_ptr(operand, ty1);
12103     }
12104   } else if (ty1->data_type == LL_PTR && ty2->data_type == LL_STRUCT) {
12105     operand->ll_type = make_ptr_lltype(ty2);
12106     operand = make_bitcast(operand, ty1);
12107   } else if (ty1->data_type == LL_PTR && ty2->data_type == LL_VECTOR &&
12108              !strict_match(ty1->sub_types[0], ty2)) {
12109     operand->ll_type = make_ptr_lltype(ty2);
12110     operand = make_bitcast(operand, ty1);
12111   } else if (ll_type_int_bits(ty1) && (ty2->data_type == LL_PTR)) {
12112     operand = convert_ptr_to_int(operand, ty1);
12113   } else if (ty1->data_type == LL_PTR && ty2->data_type == LL_ARRAY) {
12114     operand = make_bitcast(operand, ty1);
12115   } else if (ty1->data_type != ty2->data_type) {
12116     if (ty1->data_type == LL_PTR && operand->ot_type == OT_VAR) {
12117       ty1 = ty1->sub_types[0];
12118       while (ty1->data_type == ty2->data_type) {
12119         if (ty1->data_type == LL_PTR || ty1->data_type == LL_ARRAY) {
12120           ty1 = ty1->sub_types[0];
12121           ty2 = ty2->sub_types[0];
12122         } else {
12123           break;
12124         }
12125       }
12126       if (ty1->data_type == ty2->data_type || ty1->data_type == LL_VOID)
12127         goto _exit_gen_base_addr_operand;
12128       if (ll_type_int_bits(ty1) && (ty2->data_type == LL_FLOAT) &&
12129           ll_type_bytes(ty1) == 4) {
12130         operand = make_bitcast(operand, ty1);
12131         goto _exit_gen_base_addr_operand;
12132       }
12133     }
12134     assert(0, "gen_base_addr_operand(): unexpected conversion", 0, ERR_Fatal);
12135   }
12136 _exit_gen_base_addr_operand:
12137   csed_operand = get_csed_operand(ilix);
12138   if (csed_operand != NULL)
12139     set_csed_operand(csed_operand, operand);
12140   ILI_COUNT(ilix)++;
12141 
12142   DBGTRACEOUT4(" returns operand %p, tmps %p, count %d for ilix %d", operand,
12143                operand->tmps, ILI_COUNT(ilix), ilix)
12144   setTempMap(ilix, operand);
12145   return operand;
12146 }
12147 
12148 void
print_tmp_name(TMPS * t)12149 print_tmp_name(TMPS *t)
12150 {
12151   char tmp[10];
12152   int idx = 0;
12153 
12154   if (!t) {
12155     idx = ++expr_id;
12156     sprintf(tmp, "%%%d", idx - 1);
12157     print_token(tmp);
12158     return;
12159   }
12160 
12161   if (!t->id)
12162     t->id = ++expr_id;
12163   sprintf(tmp, "%%%d", t->id - 1);
12164   print_token(tmp);
12165 }
12166 
12167 static bool
repeats_in_binary(union xx_u xx)12168 repeats_in_binary(union xx_u xx)
12169 {
12170   bool ret_val;
12171   double dd = (double)xx.ff;
12172 
12173   if (!llvm_info.no_debug_info) {
12174     DBGTRACEIN1(" input value: %g \n", dd)
12175   }
12176 
12177   ret_val = true;
12178   if (!llvm_info.no_debug_info) {
12179     DBGTRACEOUT1(" returns %s", ret_val ? "True" : "False")
12180   }
12181   return ret_val;
12182 } /* repeats_in_binary */
12183 
12184 static char *
gen_vconstant(const char * ctype,int sptr,DTYPE tdtype,int flags)12185 gen_vconstant(const char *ctype, int sptr, DTYPE tdtype, int flags)
12186 {
12187   DTYPE vdtype;
12188   int vsize;
12189   int i;
12190   int edtype;
12191   static char tmp_vcon_buf[2000];
12192   char *vctype, *constant;
12193 
12194   vdtype = DTySeqTyElement(tdtype);
12195   vsize = DTyVecLength(tdtype);
12196   edtype = CONVAL1G(sptr);
12197 
12198   if (flags & FLG_OMIT_OP_TYPE) {
12199     tmp_vcon_buf[0] = '<';
12200     tmp_vcon_buf[1] = '\0';
12201   } else
12202     sprintf(tmp_vcon_buf, "%s <", ctype);
12203 
12204   for (i = 0; i < vsize; i++) {
12205     if (i)
12206       strcat(tmp_vcon_buf, ", ");
12207     switch (DTY(vdtype)) {
12208     case TY_REAL:
12209       strcat(tmp_vcon_buf,
12210              gen_constant(SPTR_NULL, vdtype, VCON_CONVAL(edtype + i), 0,
12211                           flags & ~FLG_OMIT_OP_TYPE));
12212       break;
12213     case TY_INT8:
12214     case TY_DBLE:
12215       strcat(tmp_vcon_buf, gen_constant((SPTR)VCON_CONVAL(edtype + i), DT_NONE,
12216                                         0, 0, flags & ~FLG_OMIT_OP_TYPE));
12217       break;
12218     default:
12219       strcat(tmp_vcon_buf,
12220              gen_constant(SPTR_NULL, vdtype, VCON_CONVAL(edtype + i), 0,
12221                           flags & ~FLG_OMIT_OP_TYPE));
12222     }
12223   }
12224   strcat(tmp_vcon_buf, ">");
12225   constant = (char *)getitem(LLVM_LONGTERM_AREA,
12226                              strlen(tmp_vcon_buf) + 1); /* room for \0 */
12227   strcpy(constant, tmp_vcon_buf);
12228   return constant;
12229 }
12230 
12231 char *
gen_llvm_vconstant(const char * ctype,int sptr,DTYPE tdtype,int flags)12232 gen_llvm_vconstant(const char *ctype, int sptr, DTYPE tdtype, int flags)
12233 {
12234   return gen_vconstant(ctype, sptr, tdtype, flags);
12235 }
12236 
12237 static char *
gen_constant(SPTR sptr,DTYPE tdtype,INT conval0,INT conval1,int flags)12238 gen_constant(SPTR sptr, DTYPE tdtype, INT conval0, INT conval1, int flags)
12239 {
12240   DTYPE dtype;
12241   INT num[2] = {0, 0};
12242   union xx_u xx;
12243   union {
12244     double d;
12245     INT tmp[2];
12246   } dtmp, dtmp2;
12247   char *constant, *constant1, *constant2;
12248   char *ctype = "";
12249   int size = 0;
12250 
12251   static char d[MAXIDLEN];
12252   static char *b = NULL;
12253 
12254   if (b == NULL) {
12255     NEW(b, char, 100);
12256   }
12257 
12258   assert((sptr || tdtype), "gen_constant(): missing arguments", 0, ERR_Fatal);
12259   if (sptr)
12260     dtype = DTYPEG(sptr);
12261   else
12262     dtype = tdtype;
12263 
12264   if (!llvm_info.no_debug_info) {
12265     DBGTRACEIN3(" sptr %d, dtype:%d(%s)", sptr, dtype, stb.tynames[DTY(dtype)])
12266   }
12267 
12268   if (!(flags & FLG_OMIT_OP_TYPE)) {
12269     ctype = llvm_fc_type(dtype);
12270     size += strlen(ctype) + 1; /* include room for space after the type */
12271   }
12272 /* Use an enum's underlying type. */
12273 
12274   if (dtype && DTY(dtype) == TY_VECT)
12275     return gen_vconstant(ctype, sptr, dtype, flags);
12276 
12277   switch (dtype) {
12278   case DT_INT:
12279   case DT_SINT:
12280   case DT_BINT:
12281   case DT_USINT:
12282   case DT_UINT:
12283   case DT_LOG:
12284   case DT_SLOG:
12285   case DT_BLOG:
12286 #if !LONG_IS_64
12287 #endif
12288 
12289     if (sptr)
12290       sprintf(b, "%ld", (long)CONVAL2G(sptr));
12291     else
12292       sprintf(b, "%ld", (long)conval0); /* from dinit info */
12293     size += strlen(b);
12294 
12295     if (!llvm_info.no_debug_info) {
12296       DBGTRACE2("#generating integer value: %s %s\n", char_type(dtype, sptr), b)
12297     }
12298 
12299     constant = (char *)getitem(LLVM_LONGTERM_AREA, size + 1); /* room for \0 */
12300     if (flags & FLG_OMIT_OP_TYPE)
12301       sprintf(constant, "%s", b);
12302     else
12303       sprintf(constant, "%s %s", ctype, b);
12304     break;
12305 #if LONG_IS_64
12306 #endif
12307   case DT_INT8:
12308   case DT_UINT8:
12309   case DT_LOG8:
12310     if (sptr) {
12311       num[1] = CONVAL2G(sptr);
12312       num[0] = CONVAL1G(sptr);
12313     } else {
12314       num[1] = conval0;
12315       num[0] = conval1;
12316     }
12317     ui64toax(num, b, 22, 0, 10);
12318     size += strlen(b);
12319 
12320     if (!llvm_info.no_debug_info) {
12321       DBGTRACE2("#generating integer value: %s %s\n", char_type(dtype, sptr), b)
12322     }
12323 
12324     constant = (char *)getitem(LLVM_LONGTERM_AREA, size + 1); /* room for \0 */
12325     if (flags & FLG_OMIT_OP_TYPE)
12326       sprintf(constant, "%s", b);
12327     else
12328       sprintf(constant, "%s %s", ctype, b);
12329     break;
12330 
12331   case DT_DBLE:
12332   case DT_QUAD:
12333 
12334     if (sptr) {
12335       num[0] = CONVAL1G(sptr);
12336       num[1] = CONVAL2G(sptr);
12337     } else {
12338       num[0] = conval0;
12339       num[1] = conval1;
12340     }
12341 
12342     cprintf(d, "%.17le", num);
12343     /* Check for  `+/-Infinity` and 'NaN' based on the IEEE bit patterns */
12344     if ((num[0] & 0x7ff00000) == 0x7ff00000) /* exponent == 2047 */
12345       sprintf(d, "0x%08x00000000", num[0]);
12346     /* also check for -0 */
12347     else if (num[0] == 0x80000000 && num[1] == 0x00000000)
12348       sprintf(d, "-0.00000000e+00");
12349     /* remember to make room for /0 */
12350     constant =
12351         (char *)getitem(LLVM_LONGTERM_AREA, strlen(d) + strlen(ctype) + 1);
12352     if (flags & FLG_OMIT_OP_TYPE)
12353       sprintf(constant, "%s", d);
12354     else
12355       sprintf(constant, "%s %s", ctype, d);
12356     if (!llvm_info.no_debug_info) {
12357       DBGTRACE1("#set double exponent value to %s", d)
12358     }
12359     break;
12360   case DT_REAL:
12361     /* our internal representation of floats is in 8 digit hex form;
12362      * internal LLVM representation of floats in hex form is 16 digits;
12363      * thus we must make the conversion. Also need to decide when to
12364      * represent final float form in exponential or hexadecimal form.
12365      */
12366     if (sptr)
12367       xx.ww = CONVAL2G(sptr);
12368     else
12369       xx.ww = conval0;
12370     xdble(xx.ww, dtmp2.tmp);
12371     xdtomd(dtmp2.tmp, &dtmp.d);
12372     snprintf(d, 200, "%.8e", dtmp.d);
12373     size += 19;
12374     constant = (char *)getitem(
12375         LLVM_LONGTERM_AREA,
12376         size + 2); /* room for \0  and potentially a '-' sign for neg_zero */
12377     constant1 = (char *)getitem(LLVM_LONGTERM_AREA, 9);
12378     constant2 = (char *)getitem(LLVM_LONGTERM_AREA, 9);
12379     if (repeats_in_binary(xx)) {
12380       /* put in hexadecimal form unless neg 0 */
12381       if (dtmp.tmp[0] == -1) /* pick up the quiet nan */
12382         sprintf(constant1, "7FF80000");
12383       else if (!dtmp.tmp[1])
12384         sprintf(constant1, "00000000");
12385       else
12386         sprintf(constant1, "%X", dtmp.tmp[1]);
12387       if (!dtmp.tmp[0] || dtmp.tmp[0] == -1)
12388         sprintf(constant2, "00000000");
12389       else
12390         sprintf(constant2, "%X", dtmp.tmp[0]);
12391       if (flags & FLG_OMIT_OP_TYPE)
12392         sprintf(constant, "0x%s%s", constant1, constant2);
12393       else
12394         sprintf(constant, "%s 0x%s%s", ctype, constant1, constant2);
12395 
12396       /* check for negative zero */
12397       if (dtmp.tmp[1] == 0x80000000 && !dtmp.tmp[0]) {
12398         if (flags & FLG_OMIT_OP_TYPE)
12399           sprintf(constant, "-0.000000e+00");
12400         else
12401           sprintf(constant, "%s -0.000000e+00", ctype);
12402       }
12403     } else {
12404       /*  put in exponential form */
12405       if (flags & FLG_OMIT_OP_TYPE)
12406         sprintf(constant, "%s", d);
12407       else
12408         sprintf(constant, "%s %s", ctype, d);
12409     }
12410 
12411     if (!llvm_info.no_debug_info) {
12412       DBGTRACE1("#set float exp value to %s", d)
12413       DBGTRACE2("#set float hex value to 0x%X%x", dtmp.tmp[1], dtmp.tmp[0])
12414     }
12415 
12416     break;
12417 
12418 #ifdef LONG_DOUBLE_FLOAT128
12419   case DT_FLOAT128:
12420     size += 36;
12421     constant = getitem(LLVM_LONGTERM_AREA, size);
12422     if (flags & FLG_OMIT_OP_TYPE)
12423       snprintf(constant, size, "0xL%08x%08x%08x%08x", CONVAL1G(sptr),
12424                CONVAL2G(sptr), CONVAL3G(sptr), CONVAL4G(sptr));
12425     else
12426       snprintf(constant, size, "%s 0xL%08x%08x%08x%08x", ctype, CONVAL1G(sptr),
12427                CONVAL2G(sptr), CONVAL3G(sptr), CONVAL4G(sptr));
12428     break;
12429 #endif
12430 
12431   default:
12432     if (!llvm_info.no_debug_info) {
12433       DBGTRACE3("### gen_constant; sptr %d, unknown dtype: %d(%s)", sptr, dtype,
12434                 stb.tynames[DTY(dtype)])
12435     }
12436     assert(0, "gen_constant(): unexpected constant dtype", dtype, ERR_Fatal);
12437   }
12438 
12439   if (!llvm_info.no_debug_info) {
12440     DBGTRACEOUT1(" returns %s", constant)
12441   }
12442   return constant;
12443 } /* gen_constant */
12444 
12445 static char *
add_tmp_buf_list_item(TEMP_BUF_LIST ** tempbuflist_ptr,int sz)12446 add_tmp_buf_list_item(TEMP_BUF_LIST **tempbuflist_ptr, int sz)
12447 {
12448   int i;
12449   TEMP_BUF_LIST *last;
12450 
12451   for (last = *tempbuflist_ptr; last && last->next; last = last->next)
12452     ;
12453 
12454   if (*tempbuflist_ptr) {
12455     last->next =
12456         (TEMP_BUF_LIST *)getitem(CG_MEDTERM_AREA, sizeof(TEMP_BUF_LIST));
12457     last = last->next;
12458   } else {
12459     *tempbuflist_ptr = last =
12460         (TEMP_BUF_LIST *)getitem(CG_MEDTERM_AREA, sizeof(TEMP_BUF_LIST));
12461   }
12462 
12463   last->next = NULL;
12464   last->buf.buffer = (char *)getitem(CG_MEDTERM_AREA, sz);
12465   *last->buf.buffer = '\0';
12466   return last->buf.buffer;
12467 }
12468 
12469 #ifdef OMP_OFFLOAD_LLVM
12470 INLINE static bool
isNVVM(char * fn_name)12471 isNVVM(char *fn_name)
12472 {
12473   if (!flg.omptarget)
12474     return false;
12475   return (strncmp(fn_name, "__kmpc", 6) == 0) ||
12476          (strncmp(fn_name, "llvm.nvvm", 9) == 0) ||
12477          (strncmp(fn_name, "omp_", 4) == 0) ||
12478          (strncmp(fn_name, "llvm.fma", 8) == 0);
12479 }
12480 #endif
12481 
12482 static void
write_extern_fndecl(struct LL_FnProto_ * proto)12483 write_extern_fndecl(struct LL_FnProto_ *proto)
12484 {
12485   /* Only print decls if we have not seen a body (must be external) */
12486   if (!proto->has_defined_body) {
12487 #ifdef OMP_OFFLOAD_LLVM
12488     bool isnvvm = false;
12489     do {
12490 #endif
12491       if (proto->intrinsic_decl_str) {
12492         print_line(proto->intrinsic_decl_str);
12493       } else {
12494         print_token("declare");
12495         if (proto->is_weak)
12496           print_token(" extern_weak");
12497         print_function_signature(0, proto->fn_name, proto->abi, false);
12498         if (proto->abi->is_pure)
12499           print_token(" nounwind");
12500         if (proto->abi->is_pure)
12501           print_token(" readnone");
12502         if ((!flg.ieee || XBIT(216, 1)) && proto->abi->fast_math)
12503           print_token(" \"no-infs-fp-math\"=\"true\" "
12504                       "\"no-nans-fp-math\"=\"true\" "
12505                       "\"unsafe-fp-math\"=\"true\" \"use-soft-float\"=\"false\""
12506                       " \"no-signed-zeros-fp-math\"=\"true\"");
12507         print_nl();
12508       }
12509 #ifdef OMP_OFFLOAD_LLVM
12510       if (isnvvm) {
12511         isnvvm = false;
12512         use_cpu_output_file();
12513       } else if (isNVVM(proto->fn_name)) {
12514         isnvvm = true;
12515         use_gpu_output_file();
12516       }
12517     } while (isnvvm);
12518 #endif
12519   }
12520 }
12521 
12522 void
write_external_function_declarations(int first_time)12523 write_external_function_declarations(int first_time)
12524 {
12525   DBGTRACEIN("");
12526 
12527   if (first_time)
12528     print_nl();
12529   ll_proto_iterate(write_extern_fndecl);
12530   DBGTRACEOUT("");
12531 } /* write_external_function_declarations */
12532 
12533 /**
12534    \brief Emit function attributes in debugging mode output
12535 
12536    The <code>"no-frame-pointer-elim-non-leaf"</code> flag is included to
12537    generate better coverage of the function in the \c .eh_frame section. This is
12538    done primarily to help the profiler unwind the stack.
12539  */
12540 INLINE static void
write_function_attributes(void)12541 write_function_attributes(void)
12542 {
12543   if (!need_debug_info(SPTR_NULL))
12544     return;
12545 
12546   if (XBIT(183, 0x10)) {
12547     print_token("attributes #0 = { \"no-frame-pointer-elim-non-leaf\" }\n");
12548   } else {
12549     print_token("attributes #0 = { "
12550                 "noinline \"no-frame-pointer-elim-non-leaf\" }\n");
12551   }
12552 }
12553 
12554 static void
write_global_and_static_defines(void)12555 write_global_and_static_defines(void)
12556 {
12557   GBL_LIST *gl;
12558   for (gl = Globals; gl; gl = gl->next) {
12559     if ((STYPEG(gl->sptr) == ST_CONST) ||
12560         ((SCG(gl->sptr) == SC_LOCAL) && DINITG(gl->sptr) && !REFG(gl->sptr)) ||
12561         ((SCG(gl->sptr) == SC_EXTERN) && (STYPEG(gl->sptr) == ST_VAR) &&
12562          (DTYPEG(gl->sptr) == DT_ADDR))) {
12563       print_line(gl->global_def);
12564     }
12565   }
12566   Globals = NULL;
12567 }
12568 
12569 static void
build_unused_global_define_from_params(void)12570 build_unused_global_define_from_params(void)
12571 {
12572   return;
12573 }
12574 
12575 /**
12576    \brief Helper function: In Fortran, test if \c MIDNUM is not \c SC_DUMMY
12577    \param sptr   a symbol
12578  */
12579 INLINE static bool
formalsMidnumNotDummy(SPTR sptr)12580 formalsMidnumNotDummy(SPTR sptr)
12581 {
12582   return SCG(MIDNUMG(sptr)) != SC_DUMMY;
12583 }
12584 
12585 /**
12586    \brief Helper function: Get \c DTYPE of \p s
12587    \param s  a symbol
12588    Fortran requires special handling for ST_PROC.
12589  */
12590 INLINE static DTYPE
formalsGetDtype(SPTR s)12591 formalsGetDtype(SPTR s)
12592 {
12593   return ((STYPEG(s) == ST_PROC) && (!DTYPEG(s))) ? DT_ADDR : DTYPEG(s);
12594 }
12595 
12596 INLINE static OPERAND *
cons_expression_metadata_operand(LL_Type * llTy)12597 cons_expression_metadata_operand(LL_Type *llTy)
12598 {
12599   // FIXME: we don't need to always do this, do we? do a type check here
12600   if (llTy->data_type == LL_PTR) {
12601     LL_DebugInfo *di = cpu_llvm_module->debug_info;
12602     unsigned v = lldbg_encode_expression_arg(LL_DW_OP_deref, 0);
12603     LL_MDRef exprMD = lldbg_emit_expression_mdnode(di, 1, v);
12604     return make_mdref_op(exprMD);
12605   }
12606   return NULL;
12607 }
12608 
12609 INLINE static bool
formalsNeedDebugInfo(SPTR sptr)12610 formalsNeedDebugInfo(SPTR sptr)
12611 {
12612 #ifdef OMP_OFFLOAD_LLVM
12613   if(is_ompaccel(sptr)) return false;
12614 #endif
12615   return generating_debug_info();
12616 }
12617 
12618 /**
12619    \brief Helper function: add debug information for formal argument
12620    \param sptr  a symbol
12621    \param i     parameter position
12622  */
12623 INLINE static void
formalsAddDebug(SPTR sptr,unsigned i,LL_Type * llType,bool mayHide)12624 formalsAddDebug(SPTR sptr, unsigned i, LL_Type *llType, bool mayHide)
12625 {
12626   if (formalsNeedDebugInfo(sptr)) {
12627     LL_DebugInfo *db = cpu_llvm_module->debug_info;
12628     LL_MDRef param_md = lldbg_emit_param_variable(
12629         db, sptr, BIH_FINDEX(gbl.entbih), i, CCSYMG(sptr));
12630     if (!LL_MDREF_IS_NULL(param_md)) {
12631       LL_Type *llTy = fixup_argument_type(sptr, llType);
12632       OPERAND *exprMDOp = (STYPEG(sptr) == ST_ARRAY)
12633                               ? NULL
12634                               : cons_expression_metadata_operand(llTy);
12635       OperandFlag_t flag = (mayHide && CCSYMG(sptr)) ? OPF_HIDDEN : OPF_NONE;
12636       insert_llvm_dbg_declare(param_md, sptr, llTy, exprMDOp, flag);
12637     }
12638   }
12639 }
12640 
12641 /**
12642    \brief Process the formal arguments to the current function
12643 
12644    Generate the required prolog code to home all arguments that need it.
12645 
12646    \c llvm_info.abi_info must be initialized before calling this function.
12647 
12648    Populates \c llvm_info.homed_args, which must be allocated and empty before
12649    the call.
12650  */
12651 void
process_formal_arguments(LL_ABI_Info * abi)12652 process_formal_arguments(LL_ABI_Info *abi)
12653 {
12654   /* Entries already have been processed */
12655   unsigned i;
12656 
12657   for (i = 1; i <= abi->nargs; i++) {
12658     OPERAND *arg_op;
12659     OPERAND *store_addr;
12660     LL_Type *llTy;
12661     LL_InstrListFlags flags;
12662     SPTR key;
12663     LL_ABI_ArgInfo *arg = &abi->arg[i];
12664     const char *suffix = ".arg";
12665     bool ftn_byval = false;
12666 
12667     assert(arg->sptr, "Unnamed function argument", i, ERR_Fatal);
12668     assert(SNAME(arg->sptr) == NULL, "Argument sptr already processed",
12669            arg->sptr, ERR_Fatal);
12670     if ((SCG(arg->sptr) != SC_DUMMY) && formalsMidnumNotDummy(arg->sptr)) {
12671       process_sptr(arg->sptr);
12672       continue;
12673     }
12674 
12675     switch (arg->kind) {
12676     case LL_ARG_BYVAL:
12677       if (abi->is_fortran && !abi->is_iso_c) {
12678         ftn_byval = true;
12679         break;
12680       }
12681     /* falls thru */
12682     case LL_ARG_INDIRECT:
12683       /* For device pointer, we need to home it because we will need to pass it
12684        * as &&arg(pointer to pointer), make_var_op will call process_sptr later.
12685        */
12686       if (compilingGlobalOrDevice() && DEVICEG(arg->sptr))
12687         break;
12688       /* These arguments already appear as pointers. Should we make a copy of
12689        * an indirect arg? The caller doesn't expect us to modify the memory.
12690        */
12691       process_sptr(arg->sptr);
12692       key = ((SCG(arg->sptr) == SC_BASED) && MIDNUMG(arg->sptr))
12693                 ? MIDNUMG(arg->sptr)
12694                 : arg->sptr;
12695       llTy = llis_dummied_arg(key) ? make_generic_dummy_lltype() : LLTYPE(key);
12696       formalsAddDebug(key, i, llTy, false);
12697       continue;
12698 
12699     case LL_ARG_COERCE:
12700       /* This argument is passed by value as arg->type which is not the real
12701        * type of the argument. Generate code to save the LLVM argument into a
12702        * local variable of the right type. */
12703       suffix = ".coerce";
12704       break;
12705 
12706     default:
12707       /* Other by-value kinds. */
12708       break;
12709     }
12710 
12711     /* This op represents the real LLVM argument, not the local variable. */
12712     arg_op = make_operand();
12713     arg_op->ot_type = OT_VAR;
12714     arg_op->ll_type = make_lltype_from_abi_arg(arg);
12715     arg_op->val.sptr = arg->sptr;
12716 
12717     key = arg->sptr;
12718     /* if it is a pointer, should use midnum as hash key because most of
12719      * the time, the ILI is referencing to is MIDNUMG(x$p).
12720      * If there will ever be a reference to this SC_BASED directly,
12721      * we should always use its MIDNUMG for hashing.
12722      */
12723     if (SCG(arg->sptr) == SC_BASED && MIDNUMG(arg->sptr))
12724       key = MIDNUMG(arg->sptr);
12725     hashmap_insert(llvm_info.homed_args, INT2HKEY(key), arg_op);
12726 
12727     /* Process the argument sptr *after* updating homed_args.
12728      * process_sptr() will look at this map to treat the argument as an
12729      * auto instead of a dummy. */
12730     store_addr = make_var_op(arg->sptr);
12731 
12732     /* make sure it is pointer to pointer */
12733     if (compilingGlobalOrDevice() && DEVICEG(arg->sptr) &&
12734         !(ftn_byval || PASSBYVALG(arg->sptr)))
12735       store_addr->ll_type = ll_get_pointer_type(store_addr->ll_type);
12736 
12737     /* Make a name for the real LLVM IR argument. This will also be used by
12738      * build_routine_and_parameter_entries(). */
12739     arg_op->string = (char *)ll_create_local_name(
12740         llvm_info.curr_func, "%s%s", get_llvm_name(arg->sptr), suffix);
12741 
12742     /* Emit code in the entry block that saves the argument into the local
12743      * variable. */
12744     if (store_addr->ll_type->sub_types[0] != arg->type) {
12745       LL_Type *var_type = store_addr->ll_type->sub_types[0];
12746       if (ll_type_bytes(arg->type) > ll_type_bytes(var_type)) {
12747         /* This can happen in C (but not C++) with a new-style declaration and
12748            an old-style definition:
12749              int f(int);
12750              int f(c) char c; { ... }
12751            Cast the argument value to the local variable type. */
12752         if (ll_type_int_bits(arg->type) && ll_type_int_bits(var_type)) {
12753           arg_op = convert_operand(arg_op, var_type, I_TRUNC);
12754         } else if (ll_type_is_fp(arg->type) && ll_type_is_fp(var_type)) {
12755           arg_op = convert_operand(arg_op, var_type, I_FPTRUNC);
12756         } else {
12757 #ifdef TARGET_LLVM_ARM64
12758           /* Use a pointer bitcast on the address of the local variable to coerce
12759            the argument to the local variable type.
12760            On ARM64 the ABI requires for instance that a 3 4-byte struct (12 bytes)
12761             be coerced into 2 8-byte registers. This is achieved by casteing the input argument
12762            of type { i32, i32, i32} into  [2 x i64] */
12763           store_addr =
12764             make_bitcast(store_addr, ll_get_pointer_type(arg_op->ll_type));
12765 #else
12766           assert(false,
12767                  "process_formal_arguments: Function argument with mismatched "
12768                  "size that is neither integer nor floating-point",
12769                  0, ERR_Fatal);
12770 #endif
12771         }
12772       } else {
12773         /* Use a pointer bitcast on the address of the local variable to coerce
12774            the argument to the local variable type. */
12775         store_addr =
12776             make_bitcast(store_addr, ll_get_pointer_type(arg_op->ll_type));
12777       }
12778     }
12779 
12780     flags = ldst_instr_flags_from_dtype(formalsGetDtype(arg->sptr));
12781     if (ftn_byval) {
12782       arg_op = make_load(0, arg_op, arg_op->ll_type->sub_types[0],
12783                          mem_size(TY_INT), 0);
12784       store_addr = make_var_op(arg->sptr);
12785     }
12786     make_store(arg_op, store_addr, flags);
12787 
12788     if (CCSYMG(arg->sptr)) {
12789       llTy = arg_op->ll_type;
12790       if (llTy->data_type != LL_PTR)
12791         llTy = ll_get_pointer_type(llTy);
12792     } else {
12793       llTy = LLTYPE(arg->sptr);
12794     }
12795     assert(llTy->data_type == LL_PTR, "expected a pointer type",
12796            llTy->data_type, ERR_Fatal);
12797     /* Emit an @llvm.dbg.declare right after the store. */
12798     formalsAddDebug(arg->sptr, i, llTy, true);
12799   }
12800 }
12801 
12802 /**
12803    \brief Write out attributes for a function argument or return value.
12804    \param arg  an argument's info record
12805  */
12806 static void
print_arg_attributes(LL_ABI_ArgInfo * arg)12807 print_arg_attributes(LL_ABI_ArgInfo *arg)
12808 {
12809   switch (arg->kind) {
12810   case LL_ARG_DIRECT:
12811   case LL_ARG_COERCE:
12812   case LL_ARG_INDIRECT:
12813     break;
12814   case LL_ARG_ZEROEXT:
12815     print_token(" zeroext");
12816     break;
12817   case LL_ARG_SIGNEXT:
12818     print_token(" signext");
12819     break;
12820   case LL_ARG_BYVAL:
12821     print_token(" byval");
12822     break;
12823   default:
12824     interr("Unknown argument kind", arg->kind, ERR_Fatal);
12825   }
12826   if (arg->inreg)
12827     print_token(" inreg");
12828 }
12829 
12830 /**
12831  * \brief Print the signature of func_sptr, omitting the leading define/declare,
12832  * ending after the function attributes.
12833  *
12834  * When print_arg_names is set, also print the names of arguments. (From
12835  * abi->arg[n].sptr).
12836  *
12837  * fn_name is passed separately from the sptr, since Fortran also calls this
12838  * routine.  In the Fortran case, the sptr will not always be valid, but the
12839  * LL_FnProto contains a valid fn_name string.
12840  */
12841 static void
print_function_signature(int func_sptr,const char * fn_name,LL_ABI_Info * abi,bool print_arg_names)12842 print_function_signature(int func_sptr, const char *fn_name, LL_ABI_Info *abi,
12843                          bool print_arg_names)
12844 {
12845   unsigned i;
12846   const char *param;
12847   bool need_comma = false;
12848 
12849   /* Fortran treats functions with unknown prototypes as varargs,
12850    * we cannot decorate them with fastcc.
12851    */
12852   if (abi->call_conv &&
12853       !(abi->call_conv == LL_CallConv_Fast && abi->call_as_varargs)) {
12854     print_space(1);
12855     print_token(
12856         ll_get_calling_conv_str((enum LL_CallConv)abi->call_conv)); // ???
12857   }
12858 #ifdef WEAKG
12859   if (func_sptr > NOSYM && WEAKG(func_sptr)) {
12860     print_token(" weak");
12861   }
12862 #endif
12863 
12864   /* Print function return type with attributes. */
12865   if (LL_ABI_HAS_SRET(abi)) {
12866     print_token(" void");
12867   } else {
12868     print_arg_attributes(&abi->arg[0]);
12869     print_space(1);
12870     print_token(abi->extend_abi_return ? make_lltype_from_dtype(DT_INT)->str
12871                                        : abi->arg[0].type->str);
12872   }
12873 
12874   print_token(" @");
12875   print_token(map_to_llvm_name(fn_name));
12876   print_token("(");
12877 
12878   /* Hidden sret argument for struct returns. */
12879   if (LL_ABI_HAS_SRET(abi)) {
12880     print_token(abi->arg[0].type->str);
12881     print_token(" sret");
12882     if (print_arg_names) {
12883       print_space(1);
12884       print_token(SNAME(ret_info.sret_sptr));
12885     }
12886     need_comma = true;
12887   }
12888 
12889   /* Iterate over function arguments. */
12890   for (i = 1; i <= abi->nargs; i++) {
12891     LL_ABI_ArgInfo *arg = &abi->arg[i];
12892 
12893     if (need_comma)
12894       print_token(", ");
12895 
12896     print_token(arg->type->str);
12897     print_arg_attributes(arg);
12898 
12899     if (print_arg_names && arg->sptr) {
12900       int key;
12901       OPERAND *coerce_op = NULL;
12902       print_space(1);
12903       key = arg->sptr;
12904       if (SCG(arg->sptr) == SC_BASED && MIDNUMG(arg->sptr))
12905         key = MIDNUMG(arg->sptr);
12906 
12907       if (hashmap_lookup(llvm_info.homed_args, INT2HKEY(key),
12908                          (hash_data_t *)&coerce_op)) {
12909         print_token(coerce_op->string);
12910       } else {
12911         assert(SNAME(arg->sptr),
12912                "print_function_signature: "
12913                "No SNAME for sptr",
12914                arg->sptr, ERR_Fatal);
12915         print_token(SNAME(arg->sptr));
12916       }
12917     }
12918     need_comma = true;
12919   }
12920 
12921   /* Finally, append ... for varargs functions. */
12922   if (ll_abi_use_llvm_varargs(abi)) {
12923     if (need_comma)
12924       print_token(", ");
12925     print_token("...");
12926   }
12927 
12928   print_token(")");
12929 
12930   /* Function attributes.  With debugging turned on, the debug attributes
12931      contain "noinline", so there is no need to repeat it here. */
12932   if (need_debug_info(SPTR_NULL)) {
12933     /* 'attributes #0 = { ... }' to be emitted later */
12934     print_token(" #0");
12935   } else if (!XBIT(183, 0x10)) {
12936     /* Nobody sets -x 183 0x10, besides Flang. We're disabling LLVM inlining for
12937      * proprietary compilers. */
12938     print_token(" noinline");
12939   }
12940 
12941   if (func_sptr > NOSYM) {
12942 /* print_function_signature() can be called with func_sptr=0 */
12943   }
12944 
12945 #ifdef ELFSCNG
12946   if (ELFSCNG(func_sptr)) {
12947     print_token(" section \"");
12948     print_token(SYMNAME(ELFSCNG(func_sptr)));
12949     print_token("\"");
12950   }
12951 #endif
12952 #ifdef TEXTSTARTUPG
12953   if (TEXTSTARTUPG(func_sptr)) {
12954     print_token(" section \".text.startup \"");
12955   }
12956 #endif
12957 
12958 }
12959 
12960 #ifdef OMP_OFFLOAD_LLVM
add_property_struct(char * func_name,int nreductions,int reductionsize)12961 INLINE void static add_property_struct(char *func_name, int nreductions,
12962                                        int reductionsize)
12963 {
12964   print_token("@");
12965   print_token(func_name);
12966   print_token("__exec_mode = weak constant i8 0\n");
12967 }
12968 #endif
12969 
12970 /**
12971    \brief write out the header of the function definition
12972 
12973    Writes text from \c define to the label of the entry block.
12974  */
12975 void
build_routine_and_parameter_entries(SPTR func_sptr,LL_ABI_Info * abi,LL_Module * module)12976 build_routine_and_parameter_entries(SPTR func_sptr, LL_ABI_Info *abi,
12977                                     LL_Module *module)
12978 {
12979   const char *linkage = NULL;
12980   int reductionsize = 0;
12981 #ifdef OMP_OFFLOAD_LLVM
12982   if (OMPACCFUNCKERNELG(func_sptr)) {
12983     OMPACCEL_TINFO *tinfo = ompaccel_tinfo_get(func_sptr);
12984     if (tinfo->n_reduction_symbols == 0) {
12985       add_property_struct(SYMNAME(func_sptr), 0, 0);
12986     } else {
12987       for (int i = 0; i < tinfo->n_reduction_symbols; ++i) {
12988         reductionsize +=
12989             (size_of(DTYPEG(tinfo->reduction_symbols[i].shared_sym)) * 8);
12990       }
12991       add_property_struct(SYMNAME(func_sptr), tinfo->n_reduction_symbols,
12992                           reductionsize);
12993     }
12994   }
12995 #endif
12996   /* Start printing the defining line to the output file. */
12997   print_token("define");
12998 
12999 /* Function linkage. */
13000       if (SCG(func_sptr) == SC_STATIC)
13001     linkage = " internal";
13002 #ifdef OMP_OFFLOAD_LLVM
13003   if (OMPACCFUNCKERNELG(func_sptr)) {
13004     linkage = " ptx_kernel";
13005   }
13006 #endif
13007   if (linkage)
13008     print_token(linkage);
13009   if (SCG(func_sptr) != SC_STATIC)
13010     llvm_set_unique_sym(func_sptr);
13011 #ifdef WEAKG
13012   if (WEAKG(func_sptr))
13013     ll_proto_set_weak(ll_proto_key(func_sptr), true);
13014 #endif
13015 
13016   print_function_signature(func_sptr, get_llvm_name(func_sptr), abi, true);
13017 
13018   /* As of LLVM 3.8 the DISubprogram metadata nodes no longer bear
13019    * 'function' members that address the code for the subprogram.
13020    * Now, the references are reverse, the function definition carries
13021    * a !dbg metadata reference to the subprogram.
13022    */
13023   if (module && module->debug_info &&
13024       ll_feature_debug_info_ver38(&module->ir)) {
13025     LL_MDRef subprogram = lldbg_subprogram(module->debug_info);
13026     if (!LL_MDREF_IS_NULL(subprogram)) {
13027       print_dbg_line_no_comma(subprogram);
13028     }
13029   }
13030 
13031   print_line(" {\nL.entry:"); /* } so vi matches */
13032 
13033 #ifdef CONSTRUCTORG
13034   if (CONSTRUCTORG(func_sptr)) {
13035     llvm_ctor_add_with_priority(get_llvm_sname(func_sptr),
13036                                 PRIORITYG(func_sptr));
13037   }
13038 #endif
13039 #ifdef DESTRUCTORG
13040   if (DESTRUCTORG(func_sptr)) {
13041     llvm_dtor_add_with_priority(get_llvm_sname(func_sptr),
13042                                 PRIORITYG(func_sptr));
13043   }
13044 #endif
13045 
13046   ll_proto_set_defined_body(ll_proto_key(func_sptr), true);
13047 }
13048 
13049 static bool
exprjump(ILI_OP opc)13050 exprjump(ILI_OP opc)
13051 {
13052   switch (opc) {
13053   case IL_UKCJMP:
13054   case IL_KCJMP:
13055   case IL_ICJMP:
13056   case IL_FCJMP:
13057   case IL_DCJMP:
13058   case IL_ACJMP:
13059   case IL_UICJMP:
13060     return true;
13061   default:
13062     return false;
13063   }
13064 }
13065 
13066 static bool
zerojump(ILI_OP opc)13067 zerojump(ILI_OP opc)
13068 {
13069   switch (opc) {
13070   case IL_KCJMPZ:
13071   case IL_UKCJMPZ:
13072   case IL_ICJMPZ:
13073   case IL_FCJMPZ:
13074   case IL_DCJMPZ:
13075   case IL_ACJMPZ:
13076   case IL_UICJMPZ:
13077     return true;
13078   default:
13079     return false;
13080   }
13081 }
13082 
13083 /**
13084    \brief Get the string representation of the type of \p sptr or \p dtype
13085    \param sptr  symbol to use for type consing or 0
13086    \param dtype used when \p sptr not provided
13087  */
13088 const char *
char_type(DTYPE dtype,SPTR sptr)13089 char_type(DTYPE dtype, SPTR sptr)
13090 {
13091   LL_Type *ty;
13092 
13093   if (sptr && (DTYPEG(sptr) == dtype)) {
13094     ty = make_lltype_from_sptr(sptr);
13095     if (need_ptr(sptr, SCG(sptr), dtype))
13096       ty = ty->sub_types[0];
13097   } else {
13098     ty = make_lltype_from_dtype(dtype);
13099   }
13100   return ty->str;
13101 }
13102 
13103 /**
13104    \brief Update the shadow symbol arrays
13105 
13106    When adding new symbols or starting a new routine, make sure the shadow
13107    symbol arrays and dtype debug array are updated.
13108  */
13109 static void
update_llvm_sym_arrays(void)13110 update_llvm_sym_arrays(void)
13111 {
13112   const int new_size = stb.stg_avail + MEM_EXTRA;
13113   if ((flg.debug || XBIT(120, 0x1000)) && cpu_llvm_module) {
13114     lldbg_update_arrays(cpu_llvm_module->debug_info, llvm_info.last_dtype_avail,
13115                         stb.dt.stg_avail + MEM_EXTRA);
13116   }
13117 }
13118 
13119 void
cg_llvm_init(void)13120 cg_llvm_init(void)
13121 {
13122   int i, dtype, return_dtype;
13123   const char *triple = "";
13124   enum LL_IRVersion ir_version;
13125 
13126   if (init_once) {
13127     update_llvm_sym_arrays();
13128     return;
13129   }
13130   ll_proto_init();
13131   routine_count = 0;
13132 
13133   CHECK(TARGET_PTRSIZE == size_of(DT_CPTR));
13134 
13135   triple = LLVM_DEFAULT_TARGET_TRIPLE;
13136 
13137   ir_version = get_llvm_version();
13138 
13139   if (!cpu_llvm_module)
13140     cpu_llvm_module = ll_create_module(gbl.file_name, triple, ir_version);
13141 #ifdef OMP_OFFLOAD_LLVM
13142   if (flg.omptarget && !gpu_llvm_module) {
13143     gpu_llvm_module =
13144         ll_create_module(gbl.file_name, ompaccel_get_targetriple(), ir_version);
13145   }
13146 #endif
13147   llvm_info.declared_intrinsics = hashmap_alloc(hash_functions_strings);
13148 
13149   llvm_info.homed_args = hashmap_alloc(hash_functions_direct);
13150 
13151 #if DEBUG
13152   ll_dfile = gbl.dbgfil ? gbl.dbgfil : stderr;
13153 #endif
13154 
13155   llvm_info.last_dtype_avail = stb.dt.stg_avail + 2000;
13156   /* set up sptr array - some extra for symbols that may need to be added */
13157   /* last_sym_avail is used for all the arrays below */
13158   llvm_info.last_sym_avail = stb.stg_avail + MEM_EXTRA;
13159 
13160   if (sptrinfo.array.stg_base) {
13161     STG_CLEAR_ALL(sptrinfo.array);
13162     STG_CLEAR_ALL(sptrinfo.type_array);
13163   } else {
13164     STG_ALLOC_SIDECAR(stb, sptrinfo.array);
13165     /* set up the type array shadowing the symbol table */
13166     STG_ALLOC_SIDECAR(stb, sptrinfo.type_array);
13167   }
13168 
13169   Globals = NULL;
13170   recorded_Globals = NULL;
13171 
13172   /* get a count of the number of routines in this file */
13173   for (i = gbl.entries; i > NOSYM; i = SYMLKG(i)) {
13174     routine_count++;
13175   }
13176 
13177   entry_bih = gbl.entbih;
13178 #if DEBUG
13179   if (DBGBIT(12, 0x10)) {
13180     indent(0);
13181     if (routine_count)
13182       fprintf(ll_dfile, "# %d routines in file %s\n", routine_count,
13183               entry_bih ? FIH_FILENAME(BIH_FINDEX(entry_bih))
13184                         : "FILENAME(gbl.entbih) NOT SET");
13185     else
13186       fprintf(ll_dfile, "# no routine in file\n");
13187   }
13188 #endif
13189 
13190   if (flg.debug || XBIT(120, 0x1000)) {
13191     lldbg_init(cpu_llvm_module);
13192 #ifdef OMP_OFFLOAD_LLVM
13193     if (flg.omptarget && XBIT(232, 0x8))
13194       lldbg_init(gpu_llvm_module);
13195 #endif
13196   }
13197 
13198   init_once = true;
13199   assem_init();
13200   if (!ftn_init_once && FTN_HAS_INIT() == 0)
13201     init_output_file();
13202 #ifdef OMP_OFFLOAD_LLVM
13203   init_gpu_output_file();
13204 #endif
13205   ftn_init_once = true;
13206 
13207   write_ftn_typedefs();
13208 } /* cg_llvm_init */
13209 
13210 /**
13211    \brief Process the end of the file (Fortran)
13212 
13213    Dumps the metadata for the Module.
13214  */
13215 void
cg_llvm_end(void)13216 cg_llvm_end(void)
13217 {
13218   write_function_attributes();
13219   ll_write_metadata(llvm_file(), cpu_llvm_module);
13220 #ifdef OMP_OFFLOAD_LLVM
13221   if (flg.omptarget) {
13222     ll_write_metadata(llvm_file(), gpu_llvm_module);
13223     ll_build_metadata_device(gbl.ompaccfile, gpu_llvm_module);
13224     ll_write_metadata(gbl.ompaccfile, gpu_llvm_module);
13225   }
13226 #endif
13227 }
13228 
13229 /**
13230    \brief Process the end of the SUBROUTINE (Fortran)
13231 
13232    In Fortran, we carry over data from the LONGTERM_AREA to the next subroutine
13233    to be processed.
13234  */
13235 void
cg_llvm_fnend(void)13236 cg_llvm_fnend(void)
13237 {
13238   if (!init_once) {
13239     cg_llvm_init();
13240   }
13241   write_global_and_static_defines();
13242   write_ftn_typedefs();
13243   Globals = NULL;
13244 
13245   /* Note that this function is called for every routine.  */
13246   assem_end();
13247   init_once = false;
13248   llutil_struct_def_reset();
13249   ll_reset_module_types(cpu_llvm_module);
13250 
13251   recorded_Globals = NULL;
13252   SYMLKP(gbl.entries, NOSYM);
13253 
13254   freearea(CG_MEDTERM_AREA);
13255 }
13256 
13257 bool
is_cg_llvm_init(void)13258 is_cg_llvm_init(void)
13259 {
13260   return init_once;
13261 }
13262 
13263 /**
13264    \brief Insert the jump entry instruction
13265 
13266    Insert compare and jump instruction to correct "entry" based on the first
13267    argument of the routine.
13268  */
13269 static void
insert_jump_entry_instr(int ilt)13270 insert_jump_entry_instr(int ilt)
13271 {
13272   SPTR sptr, lab, sym;
13273   SPTR *dpdscp;
13274   INT val = 0;
13275 
13276   if (!has_multiple_entries(gbl.currsub))
13277     return;
13278 
13279   dpdscp = (SPTR *)(aux.dpdsc_base + DPDSCG(master_sptr));
13280   sym = *dpdscp;
13281   assert(hashmap_lookup(llvm_info.homed_args, INT2HKEY(sym), NULL),
13282          "Expected homed master-entry-choice sptr", sym, ERR_Fatal);
13283 
13284   for (sptr = (SPTR)gbl.entries; sptr > NOSYM; sptr = SYMLKG(sptr)) {
13285     /* The first arg (choice) is homed via process_formal_arguments() */
13286     INSTR_LIST *Curr_Instr;
13287     OPERAND *choice_op = make_var_op(sym);
13288     OPERAND *load_op = gen_load(choice_op, make_lltype_from_dtype(DT_INT),
13289                                 ldst_instr_flags_from_dtype(DT_INT));
13290 
13291     OPERAND *operand = make_tmp_op(make_int_lltype(1), make_tmps());
13292     operand->tmps->use_count++;
13293     Curr_Instr =
13294         gen_instr(I_ICMP, operand->tmps, operand->ll_type, make_operand());
13295     Curr_Instr->operands->ot_type = OT_CC;
13296     Curr_Instr->operands->val.cc = convert_to_llvm_intcc(CC_EQ);
13297     Curr_Instr->operands->ll_type = make_type_from_opc(IL_ICMP);
13298     Curr_Instr->operands->next = load_op;
13299     Curr_Instr->operands->next->next =
13300         gen_llvm_expr(ad_icon(val), make_lltype_from_dtype(DT_INT));
13301     ad_instr(0, Curr_Instr);
13302     val++;
13303 
13304     lab = getlab();
13305     Curr_Instr = make_instr(I_BR);
13306     Curr_Instr->operands = make_tmp_op(make_int_lltype(1), operand->tmps);
13307 
13308     Curr_Instr->operands->next = make_target_op(sptr);
13309     Curr_Instr->operands->next->next = make_target_op(lab);
13310     ad_instr(0, Curr_Instr);
13311 
13312     /* label lab: */
13313     Curr_Instr = gen_instr(I_NONE, NULL, NULL, make_label_op(lab));
13314     ad_instr(0, Curr_Instr);
13315   }
13316 }
13317 
13318 static void
insert_entry_label(int ilt)13319 insert_entry_label(int ilt)
13320 {
13321   const int ilix = ILT_ILIP(ilt);
13322   SPTR sptr = ILI_SymOPND(ilix, 1);
13323   INSTR_LIST *Curr_Instr = gen_instr(I_NONE, NULL, NULL, make_label_op(sptr));
13324   ad_instr(0, Curr_Instr);
13325   llvm_info.return_ll_type = make_lltype_from_dtype(
13326       gbl.arets ? DT_INT : get_return_type(sptr)); // FIXME: possible bug
13327 }
13328 
13329 void
reset_expr_id(void)13330 reset_expr_id(void)
13331 {
13332   expr_id = 0;
13333 }
13334 
13335 static void
store_return_value_for_entry(OPERAND * p,int i_name)13336 store_return_value_for_entry(OPERAND *p, int i_name)
13337 {
13338   TMPS *tmp = make_tmps();
13339 
13340   if (p->ot_type != OT_VAR || !DT_ISCMPLX(DTYPEG(p->val.sptr))) {
13341     print_token("\t");
13342     print_tmp_name(tmp);
13343     print_token(" = bitcast ");
13344     write_type(make_generic_dummy_lltype());
13345     print_token(" %");
13346     print_token(get_entret_arg_name());
13347     print_token(" to ");
13348     write_type(make_ptr_lltype(p->ll_type));
13349     print_nl();
13350 
13351     print_token("\tstore ");
13352     write_type(p->ll_type);
13353     print_space(1);
13354     write_operand(p, "", FLG_OMIT_OP_TYPE);
13355     print_token(", ");
13356 
13357     write_type(make_ptr_lltype(p->ll_type));
13358     print_space(1);
13359     print_tmp_name(tmp);
13360     print_token(", align 4\n");
13361   } else {
13362     TMPS *loadtmp;
13363     /*  %10 = bitcast i64* %__master_entry_rslt323 to <{float, float}>*  */
13364     print_token("\t");
13365     print_tmp_name(tmp);
13366     print_token(" = bitcast ");
13367     write_type(make_generic_dummy_lltype());
13368     print_token(" %");
13369     print_token(get_entret_arg_name());
13370     print_token(" to ");
13371     write_type(p->ll_type);
13372     print_nl();
13373 
13374     /*  %11 = load <{float, float}>, <{float, float}>* %cp1_300, align 4  */
13375     loadtmp = make_tmps();
13376     print_token("\t");
13377     print_tmp_name(loadtmp);
13378     print_token(" = load");
13379     write_type(p->ll_type->sub_types[0]);
13380     print_token(", ");
13381     write_type(p->ll_type);
13382     print_space(1);
13383     write_operand(p, "", FLG_OMIT_OP_TYPE);
13384     print_token(", align 4\n");
13385 
13386     /*  store <{float, float}> %11, <{float, float}>* %10, align 4  */
13387     print_token("\tstore ");
13388     write_type(p->ll_type->sub_types[0]);
13389     print_space(1);
13390     print_tmp_name(loadtmp);
13391     print_token(", ");
13392 
13393     write_type(p->ll_type);
13394     print_space(1);
13395     print_tmp_name(tmp);
13396     print_token(", align 4\n");
13397   }
13398 
13399   print_token("\t");
13400   print_token(llvm_instr_names[i_name]);
13401   print_token(" void");
13402 }
13403 
13404 /*
13405  * Global initialization and finalization routines
13406  */
13407 
13408 #define LLVM_DEFAULT_PRIORITY 65535
13409 
13410 typedef struct init_node {
13411   const char *name;
13412   int priority;
13413   struct init_node *next;
13414 } init_node;
13415 
13416 typedef struct init_list_t {
13417   struct init_node *head;
13418   struct init_node *tail;
13419   int size;
13420 } init_list_t;
13421 
13422 static init_list_t llvm_ctor_list;
13423 static init_list_t llvm_dtor_list;
13424 
13425 static void
llvm_add_to_init_list(const char * name,int priority,init_list_t * list)13426 llvm_add_to_init_list(const char *name, int priority, init_list_t *list)
13427 {
13428   init_node *node = (init_node *)malloc(sizeof(init_node));
13429   node->name = llutil_strdup(name);
13430   if (priority < 0 || priority > LLVM_DEFAULT_PRIORITY) {
13431     priority = LLVM_DEFAULT_PRIORITY;
13432   }
13433   node->priority = priority;
13434   node->next = NULL;
13435 
13436   if (list->head == NULL) {
13437     list->head = node;
13438     list->tail = node;
13439   } else {
13440     list->tail->next = node;
13441     list->tail = node;
13442   }
13443   ++(list->size);
13444 }
13445 
13446 void
llvm_ctor_add(const char * name)13447 llvm_ctor_add(const char *name)
13448 {
13449   llvm_add_to_init_list(name, LLVM_DEFAULT_PRIORITY, &llvm_ctor_list);
13450 }
13451 
13452 void
llvm_ctor_add_with_priority(const char * name,int priority)13453 llvm_ctor_add_with_priority(const char *name, int priority)
13454 {
13455   llvm_add_to_init_list(name, priority, &llvm_ctor_list);
13456 }
13457 
13458 void
llvm_dtor_add(const char * name)13459 llvm_dtor_add(const char *name)
13460 {
13461   llvm_add_to_init_list(name, LLVM_DEFAULT_PRIORITY, &llvm_dtor_list);
13462 }
13463 
13464 void
llvm_dtor_add_with_priority(const char * name,int priority)13465 llvm_dtor_add_with_priority(const char *name, int priority)
13466 {
13467   llvm_add_to_init_list(name, priority, &llvm_dtor_list);
13468 }
13469 
13470 static void
llvm_write_ctor_dtor_list(init_list_t * list,const char * global_name)13471 llvm_write_ctor_dtor_list(init_list_t *list, const char *global_name)
13472 {
13473   struct init_node *node;
13474   char int_str_buffer[20];
13475 
13476   if (list->size == 0)
13477     return;
13478 
13479   print_token("@");
13480   print_token(global_name);
13481   print_token(" = appending global [");
13482   sprintf(int_str_buffer, "%d", list->size);
13483   print_token(int_str_buffer);
13484 
13485   if (ll_feature_three_argument_ctor_and_dtor(&current_module->ir)) {
13486     print_token(" x { i32, void ()*, i8* }][");
13487     for (node = list->head; node != NULL; node = node->next) {
13488       print_token("{ i32, void ()*, i8* } { i32 ");
13489       sprintf(int_str_buffer, "%d", node->priority);
13490       print_token(int_str_buffer);
13491       print_token(", void ()* @");
13492       print_token(node->name);
13493       print_token(", i8* null }");
13494       if (node->next != NULL) {
13495         print_token(", ");
13496       }
13497     }
13498   } else {
13499     print_token(" x { i32, void ()* }][");
13500     for (node = list->head; node != NULL; node = node->next) {
13501       print_token("{ i32, void ()* } { i32 ");
13502       sprintf(int_str_buffer, "%d", node->priority);
13503       print_token(int_str_buffer);
13504       print_token(", void ()* @");
13505       print_token(node->name);
13506       print_token(" }");
13507       if (node->next != NULL) {
13508         print_token(", ");
13509       }
13510     }
13511   }
13512 
13513   print_token("]");
13514   print_nl();
13515 }
13516 
13517 void
llvm_write_ctors()13518 llvm_write_ctors()
13519 {
13520   llvm_write_ctor_dtor_list(&llvm_ctor_list, "llvm.global_ctors");
13521   llvm_write_ctor_dtor_list(&llvm_dtor_list, "llvm.global_dtors");
13522 }
13523 
13524 void
cg_fetch_clen_parampos(SPTR * len,int * param,SPTR sptr)13525 cg_fetch_clen_parampos(SPTR *len, int *param, SPTR sptr)
13526 {
13527   if (llvm_info.abi_info) {
13528     int i;
13529     *len = CLENG(sptr);
13530     for (i = 1; i <= llvm_info.abi_info->nargs; ++i)
13531       if (llvm_info.abi_info->arg[i].sptr == *len) {
13532         *param = i;
13533         return;
13534       }
13535   }
13536   *param = -1; /* param not found */
13537 }
13538 
13539 void
add_debug_cmnblk_variables(LL_DebugInfo * db,SPTR sptr)13540 add_debug_cmnblk_variables(LL_DebugInfo *db, SPTR sptr)
13541 {
13542   static hashset_t sptr_added;
13543   SPTR scope, var;
13544   const char *debug_name;
13545   char *save_ptr;
13546   bool has_alias = false;
13547 
13548   if (!sptr_added)
13549     sptr_added = hashset_alloc(hash_functions_strings);
13550   scope = SCOPEG(sptr);
13551   for (var = CMEMFG(sptr); var > NOSYM; var = SYMLKG(var)) {
13552     if ((!SNAME(var)) || strcmp(SNAME(var), SYMNAME(var))) {
13553       if (CCSYMG(sptr)) {
13554         debug_name = new_debug_name(SYMNAME(scope), SYMNAME(var), NULL);
13555       } else {
13556         debug_name = new_debug_name(SYMNAME(scope), SYMNAME(sptr),
13557                                     SYMNAME(var));
13558       }
13559       if (gbl.rutype != RU_BDATA &&
13560           NEEDMODG(scope) && lookup_modvar_alias(var)) {
13561         /* This is a DECLARATION to be imported to a subroutine
13562          * later in lldbg_emit_subprogram(). */
13563         has_alias = true;
13564         lldbg_add_pending_import_entity(db, var, IMPORTED_DECLARATION);
13565       }
13566       if (hashset_lookup(sptr_added, debug_name))
13567         continue;
13568       hashset_insert(sptr_added, debug_name);
13569       save_ptr = SNAME(var);
13570       SNAME(var) = SYMNAME(var);
13571       addDebugForGlobalVar(var, variable_offset_in_aggregate(var, 0));
13572       SNAME(var) = save_ptr;
13573     }
13574   }
13575   if (gbl.rutype != RU_BDATA && NEEDMODG(scope) && !has_alias) {
13576     /* This is a MODULE to be imported to a subroutine
13577      * later in lldbg_emit_subprogram(). */
13578     lldbg_add_pending_import_entity(db, scope, IMPORTED_MODULE);
13579   }
13580 }
13581 
13582 /**
13583    \brief Process symbols with global lifetime and cons their metadata
13584  */
13585 void
process_global_lifetime_debug(void)13586 process_global_lifetime_debug(void)
13587 {
13588   bool host_version = true;
13589   if (cpu_llvm_module->global_debug_map)
13590     hashmap_clear(cpu_llvm_module->global_debug_map);
13591   if (cpu_llvm_module->debug_info && gbl.cmblks) {
13592     LL_DebugInfo *db = cpu_llvm_module->debug_info;
13593     SPTR sptr = gbl.cmblks;
13594     update_llvm_sym_arrays();
13595     lldbg_reset_module(db);
13596     if (gbl.currsub>NOSYM) {
13597        if (CUDAG(gbl.currsub) &&
13598 	   !(CUDAG(gbl.currsub) & CUDA_HOST)) {
13599          host_version = false;
13600        }
13601     }
13602     if (!gbl.cuda_constructor &&
13603         host_version) {
13604       for (; sptr > NOSYM; sptr = SYMLKG(sptr)) {
13605         const SPTR scope = SCOPEG(sptr);
13606         if (scope > 0) {
13607           if (CCSYMG(sptr)) {
13608             lldbg_emit_module_mdnode(db, scope);
13609             add_debug_cmnblk_variables(db, sptr);
13610           } else {
13611             if (FROMMODG(sptr) || (gbl.rutype == RU_BDATA && scope == gbl.currsub)) {
13612               lldbg_emit_common_block_mdnode(db, sptr);
13613               add_debug_cmnblk_variables(db, sptr);
13614             }
13615           }
13616         }
13617       }
13618     }
13619   }
13620 }
13621 
13622 
13623 bool
is_vector_x86_mmx(LL_Type * type)13624 is_vector_x86_mmx(LL_Type *type) {
13625   /* Check if type is a vector with 64 bits overall. Works on pointer types. */
13626   LL_Type *t = type;
13627   if (type->data_type == LL_PTR) {
13628     t = *type->sub_types;
13629   }
13630   if (t->data_type == LL_VECTOR &&
13631       (strcmp(t->str, "<1 x i64>") == 0 ||
13632        strcmp(t->str, "<2 x i32>") == 0 ||
13633        strcmp(t->str, "<4 x i16>") == 0 ||
13634        strcmp(t->str, "<8 x i8>") == 0 ||
13635        strcmp(t->str, "<1 x double>") == 0 ||
13636        strcmp(t->str, "<2 x float>") == 0)) {
13637     return true;
13638   }
13639   return false;
13640 }
13641