1 /*
2  * Copyright (c) 2011-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 #ifndef LLUTIL_H_
19 #define LLUTIL_H_
20 
21 #include "gbldefs.h"
22 #include "global.h"
23 #include "symtab.h"
24 #include "ll_structure.h"
25 #include "ili.h"
26 
27 /** \brief need a getitem() area that can persist across routine compilation */
28 #define LLVM_LONGTERM_AREA 25
29 
30 /** \brief OPERAND flag values */
31 typedef enum OperandFlag_t {
32   OPF_NONE,
33   OPF_WRAPPED_MD = (1 << 0),
34   OPF_NULL_TYPE = (1 << 1),
35   OPF_SRET_TYPE = (1 << 2),
36   OPF_SRARG_TYPE = (1 << 3),
37   OPF_SEXT = (1 << 4),
38   OPF_ZEXT = (1 << 5),
39   OPF_VOLATILE = (1 << 6),
40   OPF_HIDDEN = (1 << 7),
41   OPF_CONTAINS_UNDEF = (1 << 8),
42 } OperandFlag_t;
43 
44 typedef enum OperandOutputFlag_t {
45   FLG_OMIT_OP_TYPE = (1 << 0),
46   FLG_AS_UNSIGNED = (1 << 1),
47   FLG_FIXUP_RETURN_TYPE = (1 << 2),
48   FLG_INNER_TYPED_COMPLEX = (1 << 3)
49 } OperandOutputFlag_t;
50 
51 #define LLTNAMEG(llt) llvm_type_names[(llt)->kind]
52 
53 /** \brief OPERAND types */
54 typedef enum OperandType_t {
55   OT_NONE, /**< must be first (0) member */
56   OT_CONSTSPTR,
57   OT_VAR,
58   OT_TMP,
59   OT_LABEL,
60   OT_CC,
61   OT_TARGET,
62   OT_CALL,
63   OT_CONSTVAL,
64   OT_UNDEF,
65   OT_MDNODE,
66   OT_MEMBER,
67   OT_DEF,
68   OT_CONSTSTRING,
69   OT_LAST /**< must be last member */
70 } OperandType_t;
71 
72 #if DEBUG
73 const char *get_ot_name(unsigned ot);
74 #define OTNAMEG(p) get_ot_name((p)->ot_type)
75 #endif
76 
77 /** \brief LLVM integer condition codes */
78 typedef enum LLIntegerConditionCodes {
79   LLCC_NONE,
80   LLCC_EQ,  /**< equal */
81   LLCC_NE,  /**< not equal */
82   LLCC_UGT, /**< usigned greater than */
83   LLCC_UGE, /**< unsigned greater or equal */
84   LLCC_ULT, /**< unsigned less than */
85   LLCC_ULE, /**< unsigned less or equal */
86   LLCC_SGT, /**< signed greater than */
87   LLCC_SGE, /**< signed greater or equal */
88   LLCC_SLT, /**< signed less than */
89   LLCC_SLE, /**< signed less or equal */
90   LLCC_LAST
91 } LLIntegerConditionCodes;
92 
93 /** \brief LLVM fp condition codes */
94 typedef enum LLFloatingPointConditionCodes {
95   LLCCF_NONE,
96   LLCCF_FALSE, /**< always false */
97   LLCCF_OEQ,   /**< ordered and equal */
98   LLCCF_OGT,   /**< ordered and greater than */
99   LLCCF_OGE,   /**< ordered and greater than or equal */
100   LLCCF_OLT,   /**< ordered and less than */
101   LLCCF_OLE,   /**< ordered and less than or equal */
102   LLCCF_ONE,   /**< ordered and not equal */
103   LLCCF_ORD,   /**< ordered (no nans) */
104   LLCCF_UEQ,   /**< unordered or equal */
105   LLCCF_UGT,   /**< unordered or greater than */
106   LLCCF_UGE,   /**< unordered or greater than or equal */
107   LLCCF_ULT,   /**< unordered or less than */
108   LLCCF_ULE,   /**< unordered or less than or equal */
109   LLCCF_UNE,   /**< unordered or not equal */
110   LLCCF_UNO,   /**< unordered (either nans) */
111   LLCCF_TRUE,  /**< always true */
112   LLCCF_LAST
113 } LLFloatingPointConditionCodes;
114 
115 /** \brief Dimension information */
116 typedef struct LLDimInfo {
117   int size; /**< size of this dimension */
118   int sptr; /**< corresponding sptr */
119   int dim;  /**< dimension number */
120   /** associated dtype - char_type(dtype) gives sub-array type as string */
121   int dtype;
122 } DIM_INFO;
123 
124 /** \brief Data initialization record */
125 typedef struct DINIT_REC {
126   int dtype; /**< first dtype encountered, so if array then type of elements */
127   ISZ_T conval;
128   int offset;
129   int len;
130   char *payload;
131   struct DINIT_REC *next;
132 } DINIT_REC;
133 
134 /** \brief Symbol pointer drec */
135 typedef struct {
136   int sptr;          /**< symbol sptr */
137   int offset;        /**< offset value */
138   int dtype;         /**< dtype of record entries */
139   int pad;           /**< padding before sptr ? */
140   DINIT_REC *record; /**< link to dinit records */
141 } SPTR_DREC;
142 
143 /** \brief Node type in a list of globals */
144 typedef struct GBL_TAG {
145   SPTR sptr;          /**< sptr of the variable */
146   unsigned alignment; /**< in bytes */
147   char *global_def;   /**< global definition */
148   struct GBL_TAG *next;
149 } GBL_LIST;
150 
151 /** \brief Extern function list node */
152 typedef struct EXFUNC_LIST {
153   SPTR sptr;         /**< sptr of the variable */
154   SPTR ha_sptr;      /**< sptr of hidden structure argument, if present */
155   char *func_def;    /**< external function definition */
156   unsigned flags;    /**< details about the external function */
157   DTYPE use_dtype;   /**< the dtype to use when generating prototypes */
158   struct EXFUNC_LIST *next;
159 } EXFUNC_LIST;
160 
161 struct INSTR_TAG;
162 
163 typedef struct TMP_TAG {
164   int id;		/**< assigned temporary id - current expr_id */
165   union {
166     struct INSTR_TAG *idef; /**< pointer to instr that define this tmp */
167     char *string;	/**< string definition for named metadata (LLVM debug
168                            format 2.9) */
169   } info;
170   int use_count;	/**< use_count for tmp value */
171 } TMPS;
172 
173 typedef struct OPERAND {
174   OperandType_t ot_type; /**< operand type */
175   TMPS *tmps;            /**< for OT_TMP types, the corresponding temporary */
176   LL_Type *ll_type;      /**< operand type */
177   union {
178     int cc;        /**< condition code value */
179     SPTR sptr;      /**< sptr value */
180     INT conval[4]; /**< constant value */
181     struct {
182       SPTR sptr;
183       unsigned long long undef_mask;
184     } sptr_undef;
185   } val;
186   char *string;         /**< hold routine name for llvm intrinsic calls */
187   unsigned flags;       /**< dependent on operand */
188   struct OPERAND *next; /**< link to next in list */
189 } OPERAND;
190 
191 /**
192    \brief LLVM instructions
193  */
194 typedef enum LL_InstrName {
195   I_NONE,	/**< must be 0 */
196   I_RET,
197   I_BR,
198   I_SW,
199   I_INVOKE,
200   I_UNWIND,
201   I_UNREACH,
202   I_ADD,
203   I_FADD,
204   I_SUB,
205   I_FSUB,
206   I_MUL,
207   I_FMUL,
208   I_UDIV,
209   I_SDIV,
210   I_FDIV,
211   I_UREM,
212   I_SREM,
213   I_FREM,
214   I_SHL,
215   I_LSHR,
216   I_ASHR,
217   I_AND,
218   I_OR,
219   I_XOR,
220   I_EXTELE,
221   I_INSELE,
222   I_SHUFFVEC,
223   I_EXTRACTVAL,
224   I_INSERTVAL,
225   I_MALLOC,
226   I_FREE,
227   I_ALLOCA,
228   I_LOAD,
229   I_STORE,
230   I_GEP,
231   I_TRUNC,
232   I_ZEXT,
233   I_SEXT,
234   I_FPTRUNC,
235   I_FPEXT,
236   I_FPTOUI,
237   I_FPTOSI,
238   I_UITOFP,
239   I_SITOFP,
240   I_PTRTOINT,
241   I_INTTOPTR,
242   I_BITCAST,
243   I_ICMP,
244   I_FCMP,
245   I_VICMP,
246   I_VFCMP,
247   I_PHI,
248   I_SELECT,
249   I_CALL,
250   I_VA_ARG,
251   I_DECL,
252   I_LANDINGPAD,
253   I_RESUME,
254   I_CLEANUP,
255   I_CATCH,
256   I_BARRIER,
257   I_ATOMICRMW,
258   I_CMPXCHG,
259   I_FENCE,
260   I_PICALL,
261   I_INDBR,
262   I_FILTER,
263   I_LAST	/**< must be last in enum */
264 } LL_InstrName;
265 
266 /* clang-format off */
267 
268 /**
269    \brief INSTR_LIST flag values
270  */
271 typedef enum LL_InstrListFlags {
272   InstrListFlagsNull,
273   VAR_ARGS_FLAG       = (1 << 0),
274   CALL_FUNC_PTR_FLAG  = (1 << 1),
275   CALL_INTRINSIC_FLAG = (1 << 2),
276   HIDDEN_ARG_FLAG     = (1 << 3),
277   SIMD_BACKEDGE_FLAG  = (1 << 4), /**< I_BR only */
278   FAST_MATH_FLAG      = (1 << 4), /**< I_CALL only */
279   VOLATILE_FLAG       = (1 << 4), /**< I_LOAD, I_STORE, I_ATOMICRMW,
280                                        I_CMPXCHG only */
281 
282   /* Call instruction flags.
283      These call-only flags overlap the load/store-only alignment bits.
284      See LDST_LOGALIGN_MASK */
285           CALL_FUNC_CAST_FLAG     = (1 << 5),
286   CALL_FUNC_INDIRECT_CAST = (1 << 6),
287   FAST_CALL               = (1 << 7),
288 
289   ARM_AAPCS               = (1 << 8),
290   ARM_AAPCS_VFP           = (1 << 9),
291   CANCEL_CALL_DBG_VALUE   = (1 << 10),
292   NOSIGNEDWRAP            = (1 << 11),
293   NOUNSIGNEDWRAP          = (1 << 12),
294   FUNC_RETURN_IS_FUNC_PTR = (1 << 13),
295   LDST_HAS_METADATA       = (1 << 13), /**< I_LOAD, I_STORE only */
296 
297   /* Information for atomic operations.
298      This information overlaps 12 of the calling convention bits.  In earlier
299      versions of the code, these were one-per-bit flags, hence the suffix
300      "FLAG".  The "flags" are all non-zero values so that testing for the
301      presence of any of them can be fast.
302 
303      For load/store instructions, presence of a memory ordering flag will cause
304      the "atomic" modifier to be printed too. */
305 
306   /* 4 bits for the rmw operation */
307           ATOMIC_RMW_OP_FLAGS = (0xF << 13),
308   ATOMIC_XCHG_FLAG = (1 << 13),
309   ATOMIC_ADD_FLAG  = (2 << 13),
310   ATOMIC_SUB_FLAG  = (3 << 13),
311   ATOMIC_AND_FLAG  = (4 << 13),
312   ATOMIC_NAND_FLAG = (5 << 13),
313   ATOMIC_OR_FLAG   = (6 << 13),
314   ATOMIC_XOR_FLAG  = (7 << 13),
315   ATOMIC_MAX_FLAG  = (8 << 13),
316   ATOMIC_MIN_FLAG  = (9 << 13),
317   ATOMIC_UMAX_FLAG = (10 << 13),
318   ATOMIC_UMIN_FLAG = (11 << 13),
319 
320   /* 1 bit for singlethread aspect of memory order */
321           ATOMIC_SINGLETHREAD_FLAG = (1 << 17),
322 
323   /* 3 bits for the memory order (if cmpxchg, then success case) */
324           ATOMIC_MEM_ORD_FLAGS = (0x7 << 18),
325   ATOMIC_MONOTONIC_FLAG = (1 << 18),
326   ATOMIC_ACQUIRE_FLAG = (3 << 18),
327   ATOMIC_RELEASE_FLAG = (4 << 18),
328   ATOMIC_ACQ_REL_FLAG = (5 << 18),
329   ATOMIC_SEQ_CST_FLAG = (6 << 18),
330 
331   /* 3 bits for the memory order for failed cmpxchg.  Use macros
332      TO_CMPXCHG_MEMORDER_FAIL and FROM_CMPXCHG_MEMORDER_FAIL to access them. */
333           ATOMIC_MEM_ORD_FAIL_FLAGS = (0x7 << 21),
334 
335   /* 1 bit for marking "weak" cmpxchg. */
336           CMPXCHG_WEAK_FLAG = (1 << 24),
337 
338   DELETABLE          = (1 << 25),
339   STARTEBB           = (1 << 26),
340   ROOTDG             = (1 << 27),
341   INST_ADDED         = (1 << 28),
342   INST_VISITED       = (1 << 29),
343   NOUNWIND_CALL_FLAG = (1 << 30),
344   INST_LIST_FLAGS_MAX = NOUNWIND_CALL_FLAG + (NOUNWIND_CALL_FLAG - 1)
345 } LL_InstrListFlags;
346 
347 #ifdef __cplusplus
348 // determine a type that is lage enough to cast to and from
349 #include <climits>
350 #if INSTR_LIST_FLAGS_MAX <= UINT_MAX
351 #define ILFint unsigned
352 #elif INSTR_LIST_FLAGS_MAX <= ULLONG_MAX
353 #define ILFint unsigned long long
354 #else
355 #error "LL_InstrListFlags is larger than 'unsigned long long'"
356 #endif
357 
358 inline LL_InstrListFlags operator|=(LL_InstrListFlags& set,
359                                     LL_InstrListFlags f) {
360   set = static_cast<LL_InstrListFlags>(static_cast<ILFint>(set) |
361                                        static_cast<ILFint>(f));
362   return set;
363 }
364 
365 inline LL_InstrListFlags operator&=(LL_InstrListFlags& set,
366                                     LL_InstrListFlags f) {
367   set = static_cast<LL_InstrListFlags>(static_cast<ILFint>(set) &
368                                        static_cast<ILFint>(f));
369   return set;
370 }
371 
372 inline LL_InstrListFlags operator~(const LL_InstrListFlags& set) {
373   return static_cast<LL_InstrListFlags>(~static_cast<ILFint>(set));
374 }
375 
376 #undef ILFint
377 #endif
378 
379 #define TO_CMPXCHG_MEMORDER_FAIL(flags) ((LL_InstrListFlags)((flags)<<3))
380 #define FROM_CMPXCHG_MEMORDER_FAIL(flags) ((LL_InstrListFlags) \
381                                            ((flags)>>3 & ATOMIC_MEM_ORD_FLAGS))
382 
383 #define LDST_LOGALIGN_SHIFT 5
384 /* log2(alignment) is encoded in three bits. See
385    ll_logalign_flags_from_dtype(). */
386 #define LDST_LOGALIGN_MASK (7 << LDST_LOGALIGN_SHIFT)
387 
388 /* convert access alignment from log2 encoding to number of bytes */
389 #define LDST_BYTEALIGN(flags) \
390   (1U << (((flags) & LDST_LOGALIGN_MASK) >> LDST_LOGALIGN_SHIFT))
391 
392 #define CALLCONV_SHIFT 14
393 /* Calling convention encoded in 8 bits on call instructions in the
394    LL_InstrListFlags. See enum LL_CallConv in ll_structure.h */
395 #define CALLCONV_MASK (0xff << CALLCONV_SHIFT)
396 
397 /* clang-format on */
398 
399 typedef struct INSTR_TAG {
400   int rank;             /**< instruction rank for scheduling */
401   LL_InstrName i_name;  /**< see LL_InstrName */
402   int ilix;      /**< original ilix for instruction, required for LOAD/STORE */
403   LL_InstrListFlags flags; /**< dependent on instruction */
404   TMPS *tmps;           /**< used to hold intermediate results */
405   LL_Type *ll_type;     /**< type of intermediate results */
406   OPERAND *operands;    /**< list of instruction operands */
407   LL_MDRef dbg_line_op; /**< line info for debug */
408   LL_MDRef misc_metadata;
409 #if DEBUG
410   const char *traceComment;
411 #endif
412   struct INSTR_TAG *prev;
413   struct INSTR_TAG *next;
414 } INSTR_LIST;
415 
416 /* old-style FUNCTION tag */
417 #define OLD_STYLE_FUNC -99
418 
419 #define INSTR_PREV(i) ((i)->prev)
420 #define INSTR_NEXT(i) ((i)->next)
421 #define INSTR_IS_TERMINAL(i) \
422     ((i)->i_name == I_BR || (i)->i_name == I_SW || (i)->i_name == I_RET || \
423      (i)->i_name == I_RESUME || (i)->i_name == I_UNREACH)
424 
425 typedef struct EXPR_STK_TAG {
426   int tmp_id;
427   INSTR_LIST *instr;
428   int sptr;
429   char *tmp_name;
430   TMPS *tmps;
431   int i_type; /* instruction type: I_LOAD, I_STORE, ... */
432   struct EXPR_STK_TAG *next;
433 } EXPR_STK;
434 
435 typedef struct STRUCT_UNION_TAG {
436   int saved; /* save the current_element for restoring when stack pop */
437   int dtype;
438   bool first;
439   int has_bitfield;
440   struct STRUCT_UNION_TAG *next;
441 } SU_STK;
442 
443 typedef struct CSED_TAG {
444   int ilix; /* cse'd ili */
445   OPERAND *operand;
446   struct CSED_TAG *next;
447 } CSED_ITEM;
448 
449 /* union definition used for float types */
450 union xx_u {
451   float ff;
452   int ww;
453 };
454 
455 typedef enum LLDEF_Flags {
456   LLDEF_NONE = 0,
457   LLDEF_IS_TYPE        = (1 << 0),
458   LLDEF_IS_INITIALIZED = (1 << 1),
459   LLDEF_IS_STATIC      = (1 << 2),
460   LLDEF_IS_EMPTY       = (1 << 3),
461   LLDEF_IS_EXTERNAL    = (1 << 4),
462   LLDEF_IS_STRUCT      = (1 << 5),
463   LLDEF_IS_ARRAY       = (1 << 6),
464   LLDEF_IS_ACCSTRING   = (1 << 7),
465   LLDEF_IS_CONST       = (1 << 8),
466   LLDEF_IS_UNPACKED_STRUCT = (1 << 9)
467 } LLDEF_Flags;
468 
469 LL_Type *get_struct_def_type(char *def_name, LL_Module *module);
470 void write_struct_defs(void);
471 
472 /* Routines defined in cgmain.c for now, it will require too much work to move
473  * them to llutil.c */
474 void append_llvm_used(OPERAND *op);
475 void print_dbg_line_no_comma(LL_MDRef md);
476 void print_dbg_line(LL_MDRef md);
477 
478 #if DEBUG
479 void indent(int);
480 extern FILE *ll_dfile;
481 
482 
483 #define DBGXTRACEIN(xsw, in, str)                    \
484   if (xsw) {                                         \
485     if (in) {                                        \
486       indent(1);                                     \
487     }                                                \
488     fprintf(ll_dfile, "<%s" str "\n", __FUNCTION__); \
489   }
490 #define DBGXTRACEIN1(xsw, in, str, p1)                   \
491   if (xsw) {                                             \
492     if (in) {                                            \
493       indent(1);                                         \
494     }                                                    \
495     fprintf(ll_dfile, "<%s" str "\n", __FUNCTION__, p1); \
496   }
497 #define DBGXTRACEIN2(xsw, in, str, p1, p2)                   \
498   if (xsw) {                                                 \
499     if (in) {                                                \
500       indent(1);                                             \
501     }                                                        \
502     fprintf(ll_dfile, "<%s" str "\n", __FUNCTION__, p1, p2); \
503   }
504 #define DBGXTRACEIN3(xsw, in, str, p1, p2, p3)                   \
505   if (xsw) {                                                     \
506     if (in) {                                                    \
507       indent(1);                                                 \
508     }                                                            \
509     fprintf(ll_dfile, "<%s" str "\n", __FUNCTION__, p1, p2, p3); \
510   }
511 #define DBGXTRACEIN4(xsw, in, str, p1, p2, p3, p4)                   \
512   if (xsw) {                                                         \
513     if (in) {                                                        \
514       indent(1);                                                     \
515     }                                                                \
516     fprintf(ll_dfile, "<%s" str "\n", __FUNCTION__, p1, p2, p3, p4); \
517   }
518 #define DBGXTRACEIN7(xsw, in, str, p1, p2, p3, p4, p5, p6, p7)              \
519   if (xsw) {                                                                \
520     if (in) {                                                               \
521       indent(1);                                                            \
522     }                                                                       \
523     fprintf(ll_dfile, "<%s" str "\n", __FUNCTION__, p1, p2, p3, p4, p5, p6, \
524             p7);                                                            \
525   }
526 #define DBGXTRACEOUT(xsw, in, str)                   \
527   if (xsw) {                                         \
528     if (in) {                                        \
529       indent(-1);                                    \
530     }                                                \
531     fprintf(ll_dfile, ">%s" str "\n", __FUNCTION__); \
532   }
533 #define DBGXTRACEOUT1(xsw, in, str, p1)                  \
534   if (xsw) {                                             \
535     if (in) {                                            \
536       indent(-1);                                        \
537     }                                                    \
538     fprintf(ll_dfile, ">%s" str "\n", __FUNCTION__, p1); \
539   }
540 #define DBGXTRACEOUT2(xsw, in, str, p1, p2)                  \
541   if (xsw) {                                                 \
542     if (in) {                                                \
543       indent(-1);                                            \
544     }                                                        \
545     fprintf(ll_dfile, ">%s" str "\n", __FUNCTION__, p1, p2); \
546   }
547 #define DBGXTRACEOUT3(xsw, in, str, p1, p2, p3)                  \
548   if (xsw) {                                                     \
549     if (in) {                                                    \
550       indent(-1);                                                \
551     }                                                            \
552     fprintf(ll_dfile, ">%s" str "\n", __FUNCTION__, p1, p2, p3); \
553   }
554 #define DBGXTRACEOUT4(xsw, in, str, p1, p2, p3, p4)                  \
555   if (xsw) {                                                         \
556     if (in) {                                                        \
557       indent(-1);                                                    \
558     }                                                                \
559     fprintf(ll_dfile, ">%s" str "\n", __FUNCTION__, p1, p2, p3, p4); \
560   }
561 #define DBGXDUMPLLTYPE(xsw, in, str, llt) \
562   if (xsw) {                              \
563     if (in) {                             \
564       indent(0);                          \
565     }                                     \
566     fprintf(ll_dfile, str);               \
567     if (llt)                              \
568       dump_type_for_debug(llt);           \
569     fprintf(ll_dfile, "\n");              \
570   }
571 #define DBGXTRACE(xsw, in, str)  \
572   if (xsw) {                     \
573     if (in) {                    \
574       indent(0);                 \
575     }                            \
576     fprintf(ll_dfile, str "\n"); \
577   }
578 #define DBGXTRACE1(xsw, in, str, p1) \
579   if (xsw) {                         \
580     if (in) {                        \
581       indent(0);                     \
582     }                                \
583     fprintf(ll_dfile, str "\n", p1); \
584   }
585 #define DBGXTRACE2(xsw, in, str, p1, p2) \
586   if (xsw) {                             \
587     if (in) {                            \
588       indent(0);                         \
589     }                                    \
590     fprintf(ll_dfile, str "\n", p1, p2); \
591   }
592 #define DBGXTRACE3(xsw, in, str, p1, p2, p3) \
593   if (xsw) {                                 \
594     if (in) {                                \
595       indent(0);                             \
596     }                                        \
597     fprintf(ll_dfile, str "\n", p1, p2, p3); \
598   }
599 #define DBGXTRACE4(xsw, in, str, p1, p2, p3, p4) \
600   if (xsw) {                                     \
601     if (in) {                                    \
602       indent(0);                                 \
603     }                                            \
604     fprintf(ll_dfile, str "\n", p1, p2, p3, p4); \
605   }
606 #define DBGXTRACE5(xsw, in, str, p1, p2, p3, p4, p5) \
607   if (xsw) {                                         \
608     if (in) {                                        \
609       indent(0);                                     \
610     }                                                \
611     fprintf(ll_dfile, str "\n", p1, p2, p3, p4, p5); \
612   }
613 #else
614 #define DBGXTRACEIN(xsw, in, str) ;
615 #define DBGXTRACEIN1(xsw, in, str, p1) ;
616 #define DBGXTRACEIN2(xsw, in, str, p1, p2) ;
617 #define DBGXTRACEIN3(xsw, in, str, p1, p2, p3) ;
618 #define DBGXTRACEIN4(xsw, in, str, p1, p2, p3, p4) ;
619 #define DBGXTRACEIN7(xsw, in, str, p1, p2, p3, p4, p5, p6, p7) ;
620 #define DBGXTRACEOUT(xsw, in, str) ;
621 #define DBGXTRACEOUT1(xsw, in, str, p1) ;
622 #define DBGXTRACEOUT2(xsw, in, str, p1, p2) ;
623 #define DBGXTRACEOUT3(xsw, in, str, p1, p2, p3) ;
624 #define DBGXTRACEOUT4(xsw, in, str, p1, p2, p3, p4) ;
625 #define DBGXDUMPLLTYPE(xsw, in, str, llt) ;
626 #define DBGXTRACE(xsw, in, str) ;
627 #define DBGXTRACE1(xsw, in, str, p1) ;
628 #define DBGXTRACE2(xsw, in, str, p1, p2) ;
629 #define DBGXTRACE3(xsw, in, str, p1, p2, p3) ;
630 #define DBGXTRACE4(xsw, in, str, p1, p2, p3, p4) ;
631 #define DBGXTRACE5(xsw, in, str, p1, p2, p3, p4, p5) ;
632 
633 #endif
634 
635 #if defined(TARGET_LLVM_X8632) || defined(TARGET_LLVM_POWER)
636 #define PASS_STRUCT_BY_REF
637 #define IS_SMALLT_STRUCT(dtype) (XBIT(121, 0x400000) && DTY(dtype) == TY_CMPLX)
638 #elif defined(TARGET_LLVM_X8664)
639 #define PASS_STRUCT_BY_REF
640 #define IS_SMALLT_STRUCT(dtype) (0)
641 #elif defined(TARGET_LLVM_ARM)
642 #define PASS_STRUCT_BY_VALUE
643 #define IS_SMALLT_STRUCT(dtype) (0)
644 #else
645 #define PASS_STRUCT_BY_REF
646 #define IS_SMALLT_STRUCT(dtype) (XBIT(121, 0x400000) && DTY(dtype) == TY_CMPLX)
647 #endif
648 
649 #define ZSIZEOF(dtype) size_of(dtype)
650 
651 extern SPTR_DREC *sptr_dinit_array;
652 
653 /* Routines in llutil.h using the ll_structure.h types. */
654 struct LL_Module;
655 
656 /* ABI lowering for LLVM.
657  *
658  * LLVM divides the lowering of function arguments and return values between
659  * the front end and the back end. The front end simplifies certain language
660  * constructs when it generates LLVM IR prototypes for functions. The back end
661  * selects argument registers based on the LLVM IR function prototype and
662  * calling convention.
663  *
664  * - The LLVM code generator will flatten any aggregate function arguments, so
665  *   C structs that need to be passed in registers must be coerced to
666  *   register-sized arguments by the front end.
667  *
668  * - A hidden struct return argument must be represented explicitly in LLVM IR
669  *   as a pointer function argument with the 'sret' attribute.
670  *
671  * - Large struct arguments that are passed by reference should be converted
672  *   to pointer arguments by the front end.
673  *
674  * - Large struct arguments that must be copied to the stack may be converted
675  *   to 'byval' pointer arguments.
676  *
677  * - Some calling conventions require small integer arguments to be sign or
678  *   zero-extended. Since LLVM IR doesn't have signed/unsigned integer types,
679  *   the extension must be requested by the front end by setting the sext/zext
680  *   argument attributes.
681  *
682  * - The 'inreg' attribute can be used to request that an argument is passed
683  *   in a register (for x86-32). Other target architectures may use this bit
684  *   for other purposes.
685  */
686 
687 /**
688    These are the possible ways of lowering a single function argument (or return
689    value) to LLVM IR.
690  */
691 enum LL_ABI_ArgKind {
692   /* Unknown argument type. This type can't be lowered to LLVM IR. */
693           LL_ARG_UNKNOWN,
694 
695   /* Argument or return value is passed directly without any translation.
696    * The LLVM IR type that is normally used to represent to argument is also
697    * used in the function prototype. */
698           LL_ARG_DIRECT,
699 
700   /* Unsigned integers only: Argument or return value is passed directly,
701    * with a zext attribute indicating that it should be zero-extended to the
702    * full register. */
703           LL_ARG_ZEROEXT,
704 
705   /* Signed integers only: Argument or return value is passed directly, with
706    * a sext attribute indicating that it should be sign-extended to the full
707    * register. */
708           LL_ARG_SIGNEXT,
709 
710   /* Argument or return value is coerced to one or more register-sized
711    * arguments of varying types. The coercion is equivalent to storing the
712    * original argument to memory followed by loading the LLVM IR arguments
713    * from the same memory.
714    *
715    * The coercion_type pointer in LL_ABI_ArgInfo describes the sequence of
716    * arguments to produce. */
717           LL_ARG_COERCE,
718 
719   /* Argument should be passed indirectly as a pointer.  The argument is
720    * saved to stack memory and a pointer to that memory is passed instead.
721    *
722    * If this is used for a return value, it means that the caller passes an
723    * 'sret' argument pointing to temporary stack space. */
724           LL_ARG_INDIRECT,
725 
726   /* Argument is passed by value on the stack.
727    *
728    * This is represented in LLVM IR as a pointer argument with the 'byval'
729    * attribute set. This looks like an indirect argument, but the LLVM code
730    * generator will pass the argument by value by copying it to the stack.
731    *
732    * This option does not apply to return values. */
733           LL_ARG_BYVAL
734 };
735 
736 /**
737    \brief Information about how a single argument should be lowered to LLVM IR.
738  */
739 typedef struct LL_ABI_ArgInfo_ {
740   enum LL_ABI_ArgKind kind;
741 
742   /* Set the 'inreg' attribute. */
743   unsigned inreg;
744 
745   /* Fortran pass by value. */
746   bool ftn_pass_by_val;
747 
748   /* LLVM type of this argument. When kind is LL_ARG_COERCE, points to the
749    * coercion type. */
750   const struct LL_Type_ *type;
751 
752   /* Symbol table entry representing this function argument, if available. */
753   SPTR sptr;
754 } LL_ABI_ArgInfo;
755 
756 /**
757    \brief Information about LLVM lowering the return value and all arguments of
758   a function.
759  */
760 typedef struct LL_ABI_Info_ {
761   /** The LL_Module containing any referenced types. */
762   struct LL_Module *module;
763 
764   /** Number of formal function arguments. */
765   unsigned nargs;
766 
767   /** This is a varargs function. */
768   unsigned is_varargs : 1;
769 
770   /** This is an old-style declaration without a prototype: int f(). */
771   unsigned missing_prototype : 1;
772 
773   /** This function should be called as a varargs function, even thouh we
774       don't know if it is a varargs function. Typically only used with
775       missing_prototype. */
776   unsigned call_as_varargs : 1;
777 
778   /** This represents a fortran call to a ISO c function. */
779   unsigned is_iso_c : 1;
780 
781   /** This represents a call within a Fortran program. */
782   unsigned is_fortran : 1;
783 
784   /** Callee is a pure function */
785   unsigned is_pure : 1;
786 
787   /** Workaround for X86 ABI, does not sign-extend shorts */
788   unsigned extend_abi_return : 1;
789 
790   /** This is a fast_math function */
791   unsigned fast_math : 1;
792 
793   /** Calling convention to use. See enum LL_CallConv. */
794   unsigned call_conv;
795 
796   /** Number of integer registers used for arguments so for, including any
797       hidden struct return pointers. */
798   unsigned used_iregs;
799 
800   /** Number of floating point / vector registers used. */
801   unsigned used_fregs;
802 
803   /** Lowering information for the return value [0] and arguments [1..nargs].
804       This array contains nargs+1 elements.
805       A void function will have an LL_ARG_UNKNOWN entry in arg[0]. */
806   LL_ABI_ArgInfo arg[1];
807 } LL_ABI_Info;
808 
809 /* Allocate an empty ABI instance, call ll_abi_free when done. */
810 LL_ABI_Info *ll_abi_alloc(struct LL_Module *, unsigned nargs);
811 
812 /* Free an allocated ABI instance, returns NULL to avoid dangling pointers. */
813 LL_ABI_Info *ll_abi_free(LL_ABI_Info *);
814 
815 #ifdef DT_INT /* Use DT_INT to detect whether DTYPE is defined. */
816 /* Get ABI lowering info for a function dtype (TY_FUNC / TY_PFUNC).
817  *
818  * Note that this does not provide lowering information for the ellipsis part
819  * of a varargs function.
820  */
821 LL_ABI_Info *ll_abi_for_func_dtype(struct LL_Module *, DTYPE dtype);
822 
823 #endif
824 
825 /* Get the LLVM return type of a function represented by abi. */
826 const struct LL_Type_ *ll_abi_return_type(LL_ABI_Info *abi);
827 
828 /* Get the LLVM function type corresponding to an LL_ABI_Info instance. */
829 const struct LL_Type_ *ll_abi_function_type(LL_ABI_Info *abi);
830 
831 /* Does this function use an sret argument to return a struct? */
832 #define LL_ABI_HAS_SRET(abi) ((abi)->arg[0].kind == LL_ARG_INDIRECT)
833 
834 /* Create an LL_Type corresponding to an ABI argument.
835  */
836 LL_Type *make_lltype_from_abi_arg(LL_ABI_ArgInfo *arg);
837 
838 /* Target-specific low-level interface for ABI lowering.
839  *
840  * These functions will be called in this order:
841  *
842  * 1. abi is allocated and nargs, is_varargs, and missing_prototype is
843  * initialized.
844  * 2. ll_abi_compute_call_conv().
845  * 3. ll_abi_classify_return_dtype().
846  * 4. for i in 1..nargs: ll_abi_classify_arg_dtype(arg[i]).
847  * 5. for extra arguments passed: ll_abi_classify_arg_dtype().
848  */
849 
850 /* Determine the calling convention to use for calling func_sptr.
851  *
852  * This target-dependent function will be called after the nargs, is_varargs,
853  * and missing_prototype fields in abi have been set, but before calling any of
854  * the ll_abi_classify_*() functions. The function should determine which
855  * calling convention to use and make any other necessary changes to abi such
856  * as setting call_as_varargs.
857  *
858  * - func_sptr is the symbol table entry for the called function, or 0 if
859  * unknown.
860  * - jsra_flags is the flags operand on the JSRA instruction for an indirect
861  *   call, or 0 if unknown.
862  *
863  * This function may be called with (0, 0) arguments when translating function
864  * pointer types, for example.
865  */
866 void ll_abi_compute_call_conv(LL_ABI_Info *abi, int func_sptr, int jsra_flags);
867 
868 #ifdef DT_INT /* Use DT_INT to detect whether DTYPE is defined. */
869 /* Classify a function return type for the current target ABI.
870  *
871  * On entry, these fields should be initialized to 0:
872  *
873  *  abi->used_iregs
874  *  abi->used_fregs
875  *  abi->arg[0]
876  *
877  * The used_iregs and used_fregs members are updated to reflect the number of
878  * registers used on *function entry*. Usually, that means that used_iregs
879  * will be set when abi->arg[0].kind == LL_ARG_INDIRECT, and 0 otherwise.
880  *
881  * The return value classification will be stored in abi->arg[0].
882  */
883 void ll_abi_classify_return_dtype(LL_ABI_Info *abi, DTYPE return_dtype);
884 
885 /* Classify a function argument dtype for the current target ABI.
886  *
887  * The counters abi->used_iregs and abi->used_fregs should reflect the number
888  * of registers used by arguments before the current one, including hidden
889  * arguments used to return large structs. They will be incremented to reflect
890  * the registers used by this argument.
891  *
892  * The arg pointer does not necessarily have to point to an argument in the
893  * abi->arg[] array. It should point to a zero-initialized struct.
894  */
895 void ll_abi_classify_arg_dtype(LL_ABI_Info *abi, LL_ABI_ArgInfo *arg,
896                                DTYPE arg_dtype);
897 
898 /* The ll_abi_classify_* functions above don't always fill out the entire
899  * LL_ABI_ArgInfo struct. Call this function to ensure that arg->type is always
900  * present, even when it is trivial. */
901 void ll_abi_complete_arg_info(LL_ABI_Info *abi, LL_ABI_ArgInfo *arg,
902                               DTYPE dtype);
903 
904 /* Classify a dtype for va_arg().
905  *
906  * This is needed for x86-64 and aarch64 which use a separate register save
907  * area in varargs functions.
908  *
909  * Return the number of general-purpose and and floating point registers needed
910  * to pass dtype in num_gp, num_fp. Return (0,0) for an argument that must be
911  * passed in memory.
912  *
913  * The returned value is the 'map' argument for __builtin_va_genargs. It is one
914  * of GP_XM(0), XM_GP(1), or XM_XM(2). See rte/pgc/hammer/src/va_arg.c.
915  */
916 unsigned ll_abi_classify_va_arg_dtype(LL_Module* module, DTYPE dtype,
917                                       unsigned *num_gp, unsigned *num_fp);
918 
919 /* Function for visiting struct members.
920  *
921  * Recursion / iteration keeps going as long as struct_visitor returns 0.
922  *
923  * The member_sptr is the ST_MEMBER entry, or 0 when visiting copmponents that
924  * are not struct members.
925  */
926 typedef int (*dtype_visitor)(void *context, DTYPE dtype, unsigned address,
927                              int member_sptr);
928 
929 /* Given a dtype possibly containing nested structs and unions, recursively
930  * visit all non-struct members in address order.
931  *
932  * Stop as soon as a visit call returns non-zero, return the value returned
933  * from the last visit call.
934  */
935 int visit_flattened_dtype(dtype_visitor visitor, void *context, DTYPE dtype,
936                           unsigned address, unsigned member_sptr);
937 
938 FILE *llvm_file(void);
939 LL_Type *ll_convert_dtype(LL_Module *module, DTYPE dtype);
940 LL_Type *ll_convert_dtype_with_addrspace(LL_Module *module, DTYPE dtype, int addrspace);
941 #endif /* ifdef DT_INT */
942 
943 /* llopt.c */
944 void optimize_block(INSTR_LIST *last_block_instr);
945 void maybe_undo_recip_div(INSTR_LIST *isns);
946 void widenAddressArith(void);
947 bool funcHasNoDepChk(void);
948 void redundantLdLdElim(void);
949 bool block_branches_to(int bih, int target);
950 
951 /* llsched.c */
952 int enhanced_conflict(int nme1, int nme2);
953 void sched_instructions(INSTR_LIST *);
954 
955 
956 /**
957    \brief set the module as gpu/cpu module
958  */
959 void llvm_set_acc_module(void);
960 void llvm_set_cpu_module(void);
961 LL_Module* get_current_module(void);
962 LL_Module* llvm_get_current_module(void);
963 
964 typedef struct FTN_LLVM_ST {
965   union {
966     UINT all;
967     struct {
968       unsigned host_reg : 1;
969       unsigned has_init : 1;
970       unsigned gpu_init : 1;
971     } bits;
972   } flags;
973 } FTN_LLVM_ST;
974 
975 extern FTN_LLVM_ST ftn_llvm_st;
976 
977 #define FTN_HOST_REG() ftn_llvm_st.flags.bits.host_reg
978 #define FTN_HAS_INIT() ftn_llvm_st.flags.bits.has_init
979 #define FTN_GPU_INIT() ftn_llvm_st.flags.bits.gpu_init
980 
981 void reset_equiv_var(void);
982 void reset_master_sptr(void);
983 void process_cmblk_sptr(int sptr);
984 void write_ftn_typedefs(void);
985 void write_local_overlap(void);
986 void print_entry_subroutine(LL_Module *module);
987 LL_Type *make_generic_dummy_lltype(void);
988 bool llis_dummied_arg(SPTR sptr);
989 bool currsub_is_sret(void);
990 void write_gblvar_defs(FILE *target_file, LL_Module *module);
991 
992 /**
993    \brief ...
994  */
995 bool is_function(int sptr);
996 
997 /**
998    \brief Should this function be represented as a varags LLVM function type?
999  */
1000 bool ll_abi_use_llvm_varargs(LL_ABI_Info *abi);
1001 
1002 /**
1003    \brief ...
1004  */
1005 bool llis_dummied_arg(SPTR sptr);
1006 
1007 /**
1008    \brief ...
1009  */
1010 bool small_aggr_return(DTYPE dtype);
1011 
1012 /**
1013    \brief ...
1014  */
1015 char *ll_get_cstring_buf(int sptr, int skip_quotes);
1016 
1017 /**
1018    \brief ...
1019  */
1020 char *llvm_fc_type(DTYPE dtype);
1021 
1022 /**
1023    \brief ...
1024  */
1025 char *process_dtype_struct(DTYPE dtype);
1026 
1027 /**
1028    \brief ...
1029  */
1030 char *process_ftn_dtype_struct(DTYPE dtype, char *tname, bool printed);
1031 
1032 /**
1033    \brief ...
1034  */
1035 const char *get_ot_name(unsigned ot);
1036 
1037 /**
1038    \brief ...
1039  */
1040 const char *llutil_strdup(const char *str);
1041 
1042 /**
1043    \brief ...
1044  */
1045 DTYPE dtype_from_return_type(ILI_OP ret_opc);
1046 
1047 /**
1048    \brief ...
1049  */
1050 DTYPE get_dtype_from_arg_opc(ILI_OP opc);
1051 
1052 /**
1053    \brief ...
1054  */
1055 DTYPE get_dtype_from_tytype(TY_KIND ty);
1056 
1057 /**
1058    \brief ...
1059  */
1060 DTYPE get_param_equiv_dtype(DTYPE dtype);
1061 
1062 /**
1063    \brief ...
1064  */
1065 FILE *llvm_file(void);
1066 
1067 /**
1068    \brief ...
1069  */
1070 DTYPE generic_dummy_dtype(void);
1071 
1072 /**
1073    \brief ...
1074  */
1075 int get_dim_size(ADSC *ad, int dim);
1076 
1077 /**
1078    \brief ...
1079  */
1080 DTYPE get_int_dtype_from_size(int size);
1081 
1082 /**
1083    \brief ...
1084  */
1085 DTYPE get_return_dtype(DTYPE dtype, unsigned *flags, unsigned new_flag);
1086 
1087 /**
1088    \brief ...
1089  */
1090 int is_struct_kind(DTYPE dtype, bool check_return,
1091                    bool return_vector_as_struct);
1092 
1093 /**
1094    \brief ...
1095  */
1096 int visit_flattened_dtype(dtype_visitor visitor, void *context, DTYPE dtype,
1097                           unsigned address, unsigned member_sptr);
1098 
1099 /**
1100    \brief ...
1101  */
1102 LL_ABI_Info *ll_abi_alloc(LL_Module *module, unsigned nargs);
1103 
1104 /**
1105    \brief Get ABI lowering info for a function given its symbol table entry.
1106 
1107    Since IPA can change the dtype of functions between calls to schedule(), use
1108    the passed dtype instead of DTYPEG(func_sptr).
1109  */
1110 LL_ABI_Info *ll_abi_for_func_sptr(LL_Module *module, SPTR func_sptr,
1111                                   DTYPE dtype);
1112 
1113 /**
1114    \brief ...
1115  */
1116 LL_ABI_Info *ll_abi_free(LL_ABI_Info *abi);
1117 
1118 /**
1119    \brief Get ABI lowering info for a specific call site.
1120 
1121    - ilix is the IL_JSR or IL_JSRA instruction representing the call.
1122    - ret_ili is the IL_DFRx instruction extracting the return value for the
1123      call, or 0 if the return value is unused.
1124 
1125    This may not provide lowering information for all function arguments if the
1126    callee is varargs or if a prototype is not available.
1127  */
1128 LL_ABI_Info *ll_abi_from_call_site(LL_Module *module, int ilix,
1129                                    DTYPE ret_dtype);
1130 
1131 /**
1132    \brief ...
1133  */
1134 LL_ABI_Info *process_ll_abi_func_ftn(SPTR func_sptr, bool use_sptrs);
1135 
1136 /**
1137    \brief ...
1138  */
1139 LL_ABI_Info *process_ll_abi_func_ftn_mod(LL_Module *mod, SPTR func_sptr,
1140                                          bool update);
1141 
1142 
1143 /**
1144    \brief ...
1145  */
1146 LL_InstrListFlags ldst_instr_flags_from_dtype(DTYPE dtype);
1147 
1148 /**
1149    \brief ...
1150  */
1151 LL_InstrListFlags ldst_instr_flags_from_dtype_nme(DTYPE dtype, int nme);
1152 
1153 /**
1154    \brief ...
1155  */
1156 LL_Type *get_ftn_cbind_lltype(SPTR sptr);
1157 
1158 /**
1159    \brief ...
1160  */
1161 LL_Type *get_ftn_cmblk_lltype(SPTR sptr);
1162 
1163 /**
1164    \brief ...
1165  */
1166 LL_Type *get_ftn_dummy_lltype(int sptr);
1167 
1168 /**
1169    \brief ...
1170  */
1171 LL_Type *get_ftn_extern_lltype(SPTR sptr);
1172 
1173 /**
1174    \brief ...
1175  */
1176 LL_Type *get_ftn_func_lltype(SPTR sptr);
1177 
1178 /**
1179    \brief ...
1180  */
1181 LL_Type *get_ftn_hollerith_type(int sptr);
1182 
1183 /**
1184    \brief ...
1185  */
1186 LL_Type *get_ftn_static_lltype(SPTR sptr);
1187 
1188 /**
1189    \brief ...
1190  */
1191 LL_Type *get_ftn_typedesc_lltype(SPTR sptr);
1192 
1193 /**
1194    \brief ...
1195  */
1196 LL_Type *get_struct_def_type(char *def_name, LL_Module *module);
1197 
1198 /**
1199    \brief ...
1200  */
1201 LL_Type *ll_abi_function_type(LL_ABI_Info *abi);
1202 
1203 /**
1204    \brief ...
1205  */
1206 LL_Type *ll_abi_return_type(LL_ABI_Info *abi);
1207 
1208 /**
1209    \brief ...
1210  */
1211 LL_Type *ll_convert_array_dtype(LL_Module *module, DTYPE dtype, int addrspace);
1212 
1213 /**
1214    \brief ...
1215  */
1216 LL_Type *ll_convert_dtype(LL_Module *module, DTYPE dtype);
1217 
1218 /**
1219    \brief ...
1220  */
1221 LL_Type *make_array_lltype(int size, LL_Type *pts_to);
1222 
1223 /**
1224    \brief ...
1225  */
1226 LL_Type *make_generic_dummy_lltype(void);
1227 
1228 /**
1229    \brief ...
1230  */
1231 LL_Type *make_int_lltype(unsigned bits);
1232 
1233 /**
1234    \brief ...
1235  */
1236 LL_Type *make_lltype_from_abi_arg(LL_ABI_ArgInfo *arg);
1237 
1238 /**
1239    \brief ...
1240  */
1241 LL_Type *make_lltype_from_arg(int arg);
1242 
1243 /**
1244    \brief ...
1245  */
1246 LL_Type *make_lltype_from_arg_noproto(int arg);
1247 
1248 /**
1249    \brief ...
1250  */
1251 LL_Type *make_lltype_from_dtype(DTYPE dtype);
1252 
1253 /**
1254    \brief ...
1255  */
1256 LL_Type *
1257 make_lltype_from_dtype_with_addrspace(DTYPE dtype, int addrspace);
1258 
1259 /**
1260    \brief ...
1261  */
1262 LL_Type *make_lltype_from_iface(SPTR sptr);
1263 
1264 /**
1265    \brief ...
1266  */
1267 LL_Type *make_lltype_from_sptr(SPTR sptr);
1268 
1269 /**
1270    \brief ...
1271  */
1272 LL_Type *make_lltype_sz4v3_from_dtype(DTYPE dtype);
1273 
1274 /**
1275    \brief ...
1276  */
1277 LL_Type *make_lltype_sz4v3_from_sptr(SPTR sptr);
1278 
1279 /**
1280    \brief ...
1281  */
1282 LL_Type *make_ptr_lltype(LL_Type *pts_to);
1283 
1284 /**
1285    \brief ...
1286  */
1287 LL_Type *make_vector_lltype(int size, LL_Type *pts_to);
1288 
1289 /**
1290    \brief ...
1291  */
1292 LL_Type *make_void_lltype(void);
1293 
1294 /**
1295    \brief ...
1296  */
1297 void layout_struct_body(LL_Module *module, LL_Type *struct_type,
1298 			int member_sptr, ISZ_T size_bytes);
1299 
1300 /**
1301    \brief ...
1302  */
1303 LL_Type *process_acc_decl_common(LL_Module *module, int first, BIGINT size,
1304                                  int is_constant, char *d_name,
1305                                  int externcommon);
1306 
1307 /**
1308    \brief it defines a global variable, used by lili2llvm_decl_globalvar.
1309  */
1310 LL_Type *
1311 process_acc_decl_gblvar(LL_Module *module, LL_Type* lltype, SPTR sptr, char* name);
1312 
1313 /**
1314    \brief ...
1315  */
1316 LL_Type *process_acc_decl_statics(LL_Module *module, int first, char *d_name,
1317                                   int init, int addrspace);
1318 
1319 /**
1320    \brief ...
1321  */
1322 LL_Type *process_acc_string(LL_Module *module, char *name, int sptr,
1323                             bool initialize);
1324 
1325 /**
1326    \brief ...
1327  */
1328 OPERAND *gen_copy_list_op(OPERAND *operands);
1329 
1330 /**
1331    \brief ...
1332  */
1333 OPERAND *gen_copy_op(OPERAND *op);
1334 
1335 /**
1336    \brief ...
1337  */
1338 OPERAND *make_constsptr_op(SPTR sptr);
1339 
1340 /**
1341    \brief ...
1342  */
1343 OPERAND *make_constval32_op(int idx);
1344 
1345 /**
1346    \brief ...
1347  */
1348 OPERAND *make_constval_opL(LL_Type *ll_type, INT conval0, INT conval1, INT conval2, INT conval3);
1349 
1350 /**
1351    \brief ...
1352  */
1353 OPERAND *make_constval_op(LL_Type *ll_type, INT conval0, INT conval1);
1354 
1355 /**
1356    \brief ...
1357  */
1358 OPERAND *make_def_op(char *str);
1359 
1360 /**
1361    \brief ...
1362  */
1363 OPERAND *make_label_op(SPTR sptr);
1364 
1365 /**
1366    \brief ...
1367  */
1368 OPERAND *make_mdref_op(LL_MDRef mdref);
1369 
1370 /**
1371    \brief Create metadata operand that wraps an LLVM value
1372 
1373    For example, <tt>metadata !{i32 %x}</tt>.
1374    Used by \c llvm.dbg.declare intrinsic.
1375  */
1376 OPERAND *make_metadata_wrapper_op(SPTR sptr, LL_Type *llTy);
1377 
1378 /**
1379    \brief ...
1380  */
1381 OPERAND *make_null_op(LL_Type *llt);
1382 
1383 /**
1384    \brief ...
1385  */
1386 OPERAND *make_operand(void);
1387 
1388 /**
1389    \brief ...
1390  */
1391 OPERAND *make_target_op(SPTR sptr);
1392 
1393 /**
1394    \brief ...
1395  */
1396 OPERAND *make_tmp_op(LL_Type *llt, TMPS *tmps);
1397 
1398 /**
1399    \brief ...
1400  */
1401 OPERAND *make_undef_op(LL_Type *llt);
1402 
1403 /**
1404    \brief ...
1405  */
1406 OPERAND *make_var_op(SPTR sptr);
1407 
1408 /**
1409    \brief ...
1410  */
1411 OPERAND *process_symlinked_sptr(int sptr, int total_init_sz, int is_union, int max_field_sz);
1412 
1413 /**
1414    \brief ...
1415  */
1416 TMPS *make_tmps(void);
1417 
1418 /**
1419    \brief ...
1420  */
1421 void indent(int change);
1422 
1423 /**
1424    \brief ...
1425  */
1426 void init_metadata_index(TMPS *t);
1427 
1428 /**
1429    \brief ...
1430  */
1431 void init_output_file(void);
1432 
1433 /**
1434    \brief ...
1435  */
1436 void ll_abi_complete_arg_info(LL_ABI_Info *abi, LL_ABI_ArgInfo *arg, DTYPE dtype);
1437 
1438 /**
1439    \brief Add a function prototype declaration to the LLVM module
1440    \param sptr  The symbol of the function
1441    \param nargs Number of arguments to the function
1442    \param args  An array of DTYPEs for the arguments
1443  */
1444 void ll_add_func_proto(int sptr, unsigned flags, int nargs, DTYPE *args);
1445 
1446 /**
1447    \brief ...
1448  */
1449 void ll_override_type_string(LL_Type *llt, const char *str);
1450 
1451 /**
1452    \brief ...
1453  */
1454 void llutil_def_reset(void);
1455 
1456 /**
1457    \brief ...
1458  */
1459 void llutil_dfile_init(void);
1460 
1461 /**
1462    \brief ...
1463  */
1464 void llutil_gblvar_def_reset(void);
1465 
1466 /**
1467    \brief ...
1468  */
1469 void llutil_struct_def_reset(void);
1470 
1471 /**
1472    \brief ...
1473  */
1474 void print_line(char *ln);
1475 
1476 /**
1477    \brief ...
1478  */
1479 void print_line_tobuf(char *ln, char *buf);
1480 
1481 /**
1482    \brief ...
1483  */
1484 void print_llsize(LL_Type *llt);
1485 
1486 /**
1487    \brief ...
1488  */
1489 void print_llsize_tobuf(LL_Type *llt, char *buf);
1490 
1491 /**
1492    \brief ...
1493  */
1494 void print_metadata_name(TMPS *t);
1495 
1496 /**
1497    \brief ...
1498  */
1499 void print_nl_tobuf(char *buf);
1500 
1501 /**
1502    \brief ...
1503  */
1504 void print_nl(void);
1505 
1506 /**
1507    \brief ...
1508  */
1509 void print_space(int num);
1510 
1511 /**
1512    \brief ...
1513  */
1514 void print_space_tobuf(int num, char *buf);
1515 
1516 /**
1517    \brief ...
1518  */
1519 void print_token(const char *tk);
1520 
1521 /**
1522    \brief ...
1523  */
1524 void print_token_tobuf(char *tk, char *buf);
1525 
1526 /**
1527    \brief ...
1528  */
1529 void process_acc_decl_const_param(LL_Module *module, char *name, int sptr);
1530 
1531 /**
1532    \brief ...
1533  */
1534 void process_acc_put_dinit(int cmem, int x, bool loop);
1535 
1536 /**
1537    \brief ...
1538  */
1539 void set_metadata_string(TMPS *t, char *string);
1540 
1541 /**
1542    \brief ...
1543  */
1544 void write_constant_value(int sptr, LL_Type *type, INT conval0, INT conval1,
1545                           bool uns);
1546 
1547 /**
1548    \brief ...
1549  */
1550 void write_ftn_typedefs(void);
1551 
1552 /**
1553    \brief ...
1554  */
1555 void write_gblvar_defs(FILE *target_file, LL_Module *module);
1556 
1557 /**
1558    \brief ...
1559  */
1560 void write_operand(OPERAND *p, const char *punc_string, int flags);
1561 
1562 /**
1563    \brief ...
1564  */
1565 void write_operands(OPERAND *operand, int flags);
1566 
1567 /**
1568    \brief ...
1569  */
1570 void write_struct_defs(void);
1571 
1572 /**
1573    \brief ...
1574  */
1575 void write_type(LL_Type *ll_type);
1576 
1577 /// \brief Compute the LL_InstrListFlag set from an ATOMIC_RMW_OP
1578 /// \param aop   an ATOMIC_RMW_OP value
1579 LL_InstrListFlags ll_instr_flags_from_aop(ATOMIC_RMW_OP aop);
1580 
1581 bool llis_integral_kind(DTYPE dtype);
1582 bool llis_pointer_kind(DTYPE dtype);
1583 bool llis_array_kind(DTYPE dtype);
1584 bool llis_vector_kind(DTYPE dtype);
1585 bool llis_struct_kind(DTYPE dtype);
1586 bool llis_function_kind(DTYPE dtype);
1587 
1588 #ifdef OMP_OFFLOAD_LLVM
1589 /**
1590    \brief Create a file to write the device code if it has not already been created,
1591  */
1592 void init_gpu_output_file(void);
1593 
1594 /**
1595    \brief Assign the cpu file to LLVMFIL
1596  */
1597 void use_cpu_output_file(void);
1598 
1599 /**
1600    \brief Assign the gpu file to LLVMFIL
1601  */
1602 void use_gpu_output_file(void);
1603 #endif
1604 #endif
1605