1 /*
2 * Copyright (c) 2014-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 LLVM bridge representation
21 */
22
23 #include "gbldefs.h"
24 #include "error.h"
25 #include "ll_structure.h"
26 #include "lldebug.h"
27 #include "global.h"
28 #include "go.h"
29 #include "llassem.h"
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 /**
35 \brief Get the LLVM version being used
36 The -x 249 flag is set by the -Mllvm= driver option.
37 */
38 LL_IRVersion
get_llvm_version(void)39 get_llvm_version(void)
40 {
41 return flg.x[249] ? ((LL_IRVersion)flg.x[249]) : LL_Version_3_2;
42 }
43
44 static void *
ll_manage_mem(LLVMModuleRef module,void * space)45 ll_manage_mem(LLVMModuleRef module, void *space)
46 {
47 struct LL_ManagedMallocs_ *mem =
48 (struct LL_ManagedMallocs_ *)malloc(sizeof(struct LL_ManagedMallocs_));
49 mem->storage = space;
50 mem->next = module->first_malloc;
51 module->first_malloc = mem;
52 return space;
53 }
54
55 static const char *
ll_manage_strdup(LLVMModuleRef module,const char * str)56 ll_manage_strdup(LLVMModuleRef module, const char *str)
57 {
58 return (const char *)ll_manage_mem(module, strdup(str));
59 }
60
61 static void *
ll_manage_calloc(LLVMModuleRef module,size_t members,size_t member_size)62 ll_manage_calloc(LLVMModuleRef module, size_t members, size_t member_size)
63 {
64 void *space = calloc(members, member_size);
65 return ll_manage_mem(module, space);
66 }
67
68 static void *
ll_manage_malloc(LLVMModuleRef module,size_t malloc_size)69 ll_manage_malloc(LLVMModuleRef module, size_t malloc_size)
70 {
71 void *space = malloc(malloc_size);
72 return ll_manage_mem(module, space);
73 }
74
75 static void
ll_destroy_instruction(struct LL_Instruction_ * instruction)76 ll_destroy_instruction(struct LL_Instruction_ *instruction)
77 {
78 if (instruction->comment) {
79 free(instruction->comment);
80 }
81
82 if (instruction->operands) {
83 free(instruction->operands);
84 }
85
86 free(instruction);
87 }
88
89 static void
ll_destroy_basic_block(struct LL_BasicBlock_ * basic_block)90 ll_destroy_basic_block(struct LL_BasicBlock_ *basic_block)
91 {
92 struct LL_Instruction_ *current;
93 struct LL_Instruction_ *next;
94
95 current = basic_block->first;
96 while (current != NULL) {
97 next = current->next;
98 ll_destroy_instruction(current);
99 current = next;
100 }
101
102 free(basic_block->name);
103 free(basic_block);
104 }
105
106 static void
free_iterator(hash_key_t key,void * context)107 free_iterator(hash_key_t key, void *context)
108 {
109 (void)context;
110 free((void *)key);
111 }
112
113 void
ll_destroy_function(struct LL_Function_ * function)114 ll_destroy_function(struct LL_Function_ *function)
115 {
116 struct LL_BasicBlock_ *current;
117 struct LL_BasicBlock_ *next;
118
119 current = function->first;
120 while (current != NULL) {
121 next = current->next;
122 ll_destroy_basic_block(current);
123 current = next;
124 }
125
126 /* function->name is allocated by ll_manage_strdup(). */
127 if (function->arguments)
128 free(function->arguments);
129
130 if (function->used_local_names) {
131 /* Local names were allocated by strdupo in unique_name(). */
132 hashset_iterate(function->used_local_names, free_iterator, NULL);
133 hashset_free(function->used_local_names);
134 }
135
136 free(function);
137 }
138
139 void
ll_destroy_mem(struct LL_ManagedMallocs_ * current)140 ll_destroy_mem(struct LL_ManagedMallocs_ *current)
141 {
142 free(current->storage);
143 free(current);
144 }
145
146 /*
147 * Types are uniqued within a module to save memory and to allow type
148 * equivalence to be tested by pointer comparison.
149 *
150 * Named structs are identified by name. All other types are uniqued by their
151 * structure which is simple to do since cyclic types can only be created by
152 * using named structs.
153 *
154 * The functions types_equal and types_hash below don't expect to be called
155 * with a named struct, they will treat it like an anonynous struct.
156 */
157
158 /* How many elements in the sub_types array? */
159 static BIGUINT64
get_num_subtypes(const struct LL_Type_ * type)160 get_num_subtypes(const struct LL_Type_ *type)
161 {
162 switch (type->data_type) {
163 case LL_PTR:
164 case LL_ARRAY:
165 case LL_VECTOR:
166 return 1;
167 default:
168 return type->sub_elements;
169 }
170 }
171
172 static hash_value_t
types_hash(hash_key_t key)173 types_hash(hash_key_t key)
174 {
175 const struct LL_Type_ *t = (const struct LL_Type_ *)key;
176 hash_accu_t hacc = HASH_ACCU_INIT;
177 unsigned i, nsubtypes;
178
179 HASH_ACCU_ADD(hacc, t->data_type);
180 HASH_ACCU_ADD(hacc, t->flags);
181 HASH_ACCU_ADD(hacc, t->addrspace);
182 HASH_ACCU_ADD(hacc, (unsigned long)t->sub_elements);
183
184 /* Subtypes have already been uniqued, so just hash pointer values. */
185 nsubtypes = get_num_subtypes(t);
186 for (i = 0; i != nsubtypes; i++)
187 HASH_ACCU_ADD(hacc, (unsigned long)t->sub_types[i]);
188
189 HASH_ACCU_FINISH(hacc);
190 return HASH_ACCU_VALUE(hacc);
191 }
192
193 static int
types_equal(hash_key_t key_a,hash_key_t key_b)194 types_equal(hash_key_t key_a, hash_key_t key_b)
195 {
196 const struct LL_Type_ *a = (const struct LL_Type_ *)key_a;
197 const struct LL_Type_ *b = (const struct LL_Type_ *)key_b;
198 unsigned i, n;
199
200 if (a->data_type != b->data_type)
201 return false;
202 if (a->flags != b->flags)
203 return false;
204 if (a->addrspace != b->addrspace)
205 return false;
206 if (a->sub_elements != b->sub_elements)
207 return false;
208
209 n = get_num_subtypes(a);
210 for (i = 0; i != n; i++)
211 if (a->sub_types[i] != b->sub_types[i])
212 return false;
213
214 /* FIXME: We really shouldn't be adding named structs to the uniquing
215 * tables, but at the moment we may be creating multiple versions of a
216 * named struct for different address spaces.
217 *
218 * The correct way of handling this is to store the address space in
219 * pointer types only, just like LLVM does it.
220 */
221 if (a->data_type == LL_STRUCT && (a->flags & LL_TYPE_IS_NAMED_STRUCT))
222 return a->str == b->str;
223
224 return true;
225 }
226
227 static const hash_functions_t types_hash_functions = {types_hash, types_equal};
228
229 /*
230 * Create a unique name based on a printf-like template.
231 *
232 * 1. Pick a name based on format + ap that isn't already in 'names'.
233 * 2. Copy the name into malloc'ed memory.
234 * 3. Insert new pointer into names.
235 * 4. Return malloced pointer.
236 */
237 static const char *
unique_name(hashset_t names,char prefix,const char * format,va_list ap)238 unique_name(hashset_t names, char prefix, const char *format, va_list ap)
239 {
240 char buffer[256] = {prefix, 0};
241 size_t prefix_length;
242 unsigned count = 0;
243 int reseeded = 0;
244 const char *unique_name;
245
246 /* The return value from vsnprintf() is useless because Microsoft Visual
247 * Studio doesn't follow the standard. */
248 vsnprintf(buffer + 1, sizeof(buffer) - 1, format, ap);
249 buffer[sizeof(buffer) - 1] = '\0';
250 prefix_length = strlen(buffer);
251
252 /* Make room for a ".%u" suffix. */
253 if (prefix_length > sizeof(buffer) - 12)
254 prefix_length = sizeof(buffer) - 12;
255
256 /* Search for a not previously used name. */
257 while (hashset_lookup(names, buffer)) {
258 /* Try a pretty .1, .2, ... .9 suffix sequence at first, but then
259 * switch to a scheme that isn't quadratic time. */
260 if (++count == 10 && !reseeded) {
261 count = 10 * hashset_size(names);
262 reseeded = 1;
263 }
264 sprintf(buffer + prefix_length, ".%u", count);
265 }
266
267 unique_name = strdup(buffer);
268 hashset_insert(names, unique_name);
269
270 return unique_name;
271 }
272
273 /*
274 * Hash functions for interned constants.
275 *
276 * LLVM constants are identified by their uniqued type pointer and textual
277 * representation.
278 */
279
280 static hash_value_t
constants_hash(hash_key_t key)281 constants_hash(hash_key_t key)
282 {
283 const LL_Value *t = (const LL_Value *)key;
284 const unsigned char *p = (const unsigned char *)t->data;
285 hash_accu_t hacc = HASH_ACCU_INIT;
286
287 HASH_ACCU_ADD(hacc, (unsigned long)t->type_struct);
288 for (; *p; p++)
289 HASH_ACCU_ADD(hacc, *p);
290
291 HASH_ACCU_FINISH(hacc);
292 return HASH_ACCU_VALUE(hacc);
293 }
294
295 static int
constants_equal(hash_key_t key_a,hash_key_t key_b)296 constants_equal(hash_key_t key_a, hash_key_t key_b)
297 {
298 const LL_Value *a = (const LL_Value *)key_a;
299 const LL_Value *b = (const LL_Value *)key_b;
300
301 return a->type_struct == b->type_struct && strcmp(a->data, b->data) == 0;
302 }
303
304 static const hash_functions_t constants_hash_functions = {constants_hash,
305 constants_equal};
306
307 /*
308 * Metadata nodes.
309 *
310 * A metadata node is a tuple of MDRefs, structurally uniqued by a hashmap. The
311 * node header and element array is allocated in contiguous memory.
312 */
313
314 static hash_value_t
mdnode_hash(hash_key_t key)315 mdnode_hash(hash_key_t key)
316 {
317 const LL_MDNode *t = (const LL_MDNode *)key;
318 unsigned i;
319 hash_accu_t hacc = HASH_ACCU_INIT;
320
321 HASH_ACCU_ADD(hacc, t->num_elems);
322 for (i = 0; i < t->num_elems; i++) {
323 HASH_ACCU_ADD(hacc, LL_MDREF_kind(t->elem[i]));
324 HASH_ACCU_ADD(hacc, LL_MDREF_value(t->elem[i]));
325 }
326
327 HASH_ACCU_FINISH(hacc);
328 return HASH_ACCU_VALUE(hacc);
329 }
330
331 static int
mdnode_equal(hash_key_t key_a,hash_key_t key_b)332 mdnode_equal(hash_key_t key_a, hash_key_t key_b)
333 {
334 const LL_MDNode *a = (const LL_MDNode *)key_a;
335 const LL_MDNode *b = (const LL_MDNode *)key_b;
336 unsigned i;
337
338 if (a->num_elems != b->num_elems)
339 return false;
340
341 for (i = 0; i < a->num_elems; i++)
342 if (a->elem[i] != b->elem[i])
343 return false;
344
345 return true;
346 }
347
348 static const hash_functions_t mdnode_hash_functions = {mdnode_hash,
349 mdnode_equal};
350
351 void
llObjtodbgPush(LL_ObjToDbgList * odl,LL_MDRef md)352 llObjtodbgPush(LL_ObjToDbgList *odl, LL_MDRef md)
353 {
354 LL_ObjToDbgList *p = odl;
355 while (p->next)
356 p = p->next;
357 if (p->used == LL_ObjToDbgBucketSize) {
358 p->next = llObjtodbgCreate();
359 p = p->next;
360 }
361 p->refs[p->used++] = md;
362 }
363
364 void
llObjtodbgFree(LL_ObjToDbgList * ods)365 llObjtodbgFree(LL_ObjToDbgList *ods)
366 {
367 LL_ObjToDbgList *n;
368 for (; ods; ods = n) {
369 n = ods->next;
370 free(ods);
371 }
372 }
373
374 /**
375 \brief Reset the id map for named struct types.
376
377 This means that ll_get_struct_type() will not return any of the struct types
378 created so far, and previously used type ids can be reused.
379
380 Reset the `id -> type` mapping, but deliberately keep the \c used_type_names
381 set. We still want to guarantee globally unique type names in the module.
382 */
383 void
ll_reset_module_types(LLVMModuleRef module)384 ll_reset_module_types(LLVMModuleRef module)
385 {
386 hashmap_clear(module->user_structs_byid);
387 }
388
389 /**
390 \brief Compute the set of IR features in module when generating IR for the
391 specified LLVM version.
392 */
393 static void
compute_ir_feature_vector(LLVMModuleRef module,enum LL_IRVersion vers)394 compute_ir_feature_vector(LLVMModuleRef module, enum LL_IRVersion vers)
395 {
396 if (strncmp(module->target_triple, "nvptx", 5) == 0)
397 module->ir.is_nvvm = 1;
398 if (strncmp(module->target_triple, "spir", 4) == 0)
399 module->ir.is_spir = 1;
400
401 module->ir.version = vers;
402 InitializeDIFlags(&module->ir);
403 if (XBIT(120, 0x200)) {
404 module->ir.dwarf_version = LL_DWARF_Version_2;
405 } else if (XBIT(120, 0x4000)) {
406 module->ir.dwarf_version = LL_DWARF_Version_3;
407 } else if (true) { // FIXME - need a new bit
408 module->ir.dwarf_version = LL_DWARF_Version_4;
409 } else {
410 module->ir.dwarf_version = LL_DWARF_Version_5;
411 }
412
413 if (ll_feature_versioned_dw_tag(&module->ir)) {
414 /* LLVMDebugVersion 12 was used by LLVM versions 3.1 through 3.5, and we
415 * don't support LLVM versions older than 3.1, so the version as always 12.
416 */
417 module->ir.debug_info_version = 12;
418 } else {
419 /* LLVM 3.6 onwards encodes the debug info version in a module flag
420 * metadata node, and the numbering sequence started over.
421 *
422 * LLVM 3.6 used v2 which had the stringified header fields in metadata
423 * nodes. This format was abandoned in 3.7, and we don't support it.
424 */
425 if (vers >= LL_Version_3_7)
426 module->ir.debug_info_version = 3;
427 else
428 module->ir.debug_info_version = 1;
429 }
430 }
431
432 /**
433 \brief Convert the cached DWARF version to an unsigned
434
435 The DWARF version is either the default or specified from the command-line.
436 */
437 unsigned
ll_feature_dwarf_version(const LL_IRFeatures * feature)438 ll_feature_dwarf_version(const LL_IRFeatures *feature)
439 {
440 switch (feature->dwarf_version) {
441 case LL_DWARF_Version_2:
442 return 2;
443 case LL_DWARF_Version_3:
444 return 3;
445 case LL_DWARF_Version_4:
446 return 4;
447 }
448 }
449
450 struct triple_info {
451 const char *prefix;
452 const char *datalayout;
453 };
454
455 static const struct triple_info known_triples[] = {
456 /* These prefixes are tried in order against target_triple. */
457 {"nvptx64-", "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64"
458 "-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:"
459 "128-n16:32:64"},
460 {"spir64-", "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64"
461 "-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64"
462 "-v64:64:64-v96:128:128-v128:128:128-v192:256:256"
463 "-v256:256:256-v512:512:512-v1024:1024:1024"},
464 {"i386", "e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"},
465 {"x86_64-apple", "e-m:o-i64:64-f80:128-n8:16:32:64-S128"},
466 {"x86_64-", "e-p:64:64-i64:64-f80:128-n8:16:32:64-S128"},
467 {"armv7-", "e-p:32:32-i64:64-v128:64:128-n32-S64"},
468 {"aarch64-", "e-m:e-i64:64-i128:128-n32:64-S128"},
469 {"powerpc64le", "e-p:64:64-i64:64-n32:64"},
470 {"", ""}};
471
472 /* Compute the data layout for the requested target triple. */
473 static void
compute_datalayout(LLVMModuleRef module)474 compute_datalayout(LLVMModuleRef module)
475 {
476 const struct triple_info *triple = known_triples;
477
478 while (strncmp(module->target_triple, triple->prefix, strlen(triple->prefix)))
479 triple++;
480 module->datalayout_string = triple->datalayout;
481 }
482
483 void
ll_destroy_module(LLVMModuleRef module)484 ll_destroy_module(LLVMModuleRef module)
485 {
486 struct LL_Function_ *current;
487 struct LL_Function_ *next;
488 struct LL_ManagedMallocs_ *cur_m;
489 struct LL_ManagedMallocs_ *next_m;
490 unsigned i;
491
492 current = module->first;
493 while (current != NULL) {
494 next = current->next;
495 ll_destroy_function(current);
496 current = next;
497 }
498
499 cur_m = module->first_malloc;
500 while (cur_m != NULL) {
501 next_m = cur_m->next;
502 ll_destroy_mem(cur_m);
503 cur_m = next_m;
504 }
505 free(module->module_vars.values);
506 free(module->user_structs.values);
507 hashmap_free(module->user_structs_byid);
508 hashset_free(module->used_type_names);
509 hashset_free(module->used_global_names);
510 hashset_free(module->anon_types);
511
512 free(module->constants);
513 hashmap_free(module->constants_map);
514
515 for (i = 0; i < module->mdstrings_count; i++)
516 free((char *)module->mdstrings[i]);
517 free(module->mdstrings);
518 hashmap_free(module->mdstrings_map);
519
520 for (i = 0; i < module->mdnodes_count; i++)
521 free(module->mdnodes[i]);
522 free(module->mdnodes);
523 hashmap_free(module->mdnodes_map);
524
525 hashmap_free(module->global_debug_map);
526 hashmap_free(module->module_debug_map);
527 hashmap_free(module->common_debug_map);
528 hashmap_free(module->modvar_debug_map);
529
530 for (i = 0; i < MD_NUM_NAMES; i++)
531 free(module->named_mdnodes[i]);
532
533 lldbg_free(module->debug_info);
534 free(module);
535 }
536
537 LLVMModuleRef
ll_create_module(const char * module_name,const char * target_triple,enum LL_IRVersion llvm_ir_version)538 ll_create_module(const char *module_name, const char *target_triple,
539 enum LL_IRVersion llvm_ir_version)
540 {
541 LLVMModuleRef new_module = (LLVMModuleRef)calloc(1, sizeof(LL_Module));
542 new_module->first_malloc = NULL;
543 new_module->module_name = ll_manage_strdup(new_module, module_name);
544 new_module->target_triple = ll_manage_strdup(new_module, target_triple);
545 new_module->first = new_module->last = NULL;
546 new_module->module_vars.values = (LL_Value **)calloc(16, sizeof(LL_Value *));
547 new_module->module_vars.num_values = 16;
548 new_module->num_module_vars = 0;
549 new_module->user_structs.values = (LL_Value **)calloc(16, sizeof(LL_Value *));
550 new_module->user_structs.num_values = 16;
551 new_module->num_user_structs = 0;
552 new_module->written_user_structs = 0;
553 new_module->user_structs_byid = hashmap_alloc(hash_functions_direct);
554 new_module->used_type_names = hashset_alloc(hash_functions_strings);
555 new_module->used_global_names = hashset_alloc(hash_functions_strings);
556 new_module->anon_types = hashset_alloc(types_hash_functions);
557 new_module->num_refs = 0;
558 new_module->extern_func_refs = NULL;
559
560 new_module->constants = (LL_Value **)calloc(16, sizeof(LL_Value *));
561 new_module->constants_alloc = 16;
562 new_module->constants_map = hashmap_alloc(constants_hash_functions);
563
564 new_module->mdstrings = (const char **)calloc(16, sizeof(char *));
565 new_module->mdstrings_alloc = 16;
566 new_module->mdstrings_map = hashmap_alloc(hash_functions_strings);
567
568 new_module->mdnodes = (LL_MDNode **)calloc(16, sizeof(LL_MDNode *));
569 new_module->mdnodes_alloc = 16;
570 new_module->mdnodes_map = hashmap_alloc(mdnode_hash_functions);
571 new_module->mdnodes_fwdvars = hashmap_alloc(hash_functions_direct);
572
573 new_module->global_debug_map = hashmap_alloc(hash_functions_direct);
574
575 new_module->module_debug_map = hashmap_alloc(hash_functions_strings);
576 new_module->common_debug_map = hashmap_alloc(hash_functions_strings);
577 new_module->modvar_debug_map = hashmap_alloc(hash_functions_strings);
578
579 compute_ir_feature_vector(new_module, llvm_ir_version);
580 compute_datalayout(new_module);
581 return new_module;
582 }
583
584 struct LL_Function_ *
ll_create_function(LLVMModuleRef module,const char * name,LL_Type * return_type,int is_kernel,int launch_bounds,int launch_bounds_minctasm,const char * calling_convention,enum LL_LinkageType linkage)585 ll_create_function(LLVMModuleRef module, const char *name, LL_Type *return_type,
586 int is_kernel, int launch_bounds, int launch_bounds_minctasm,
587 const char *calling_convention, enum LL_LinkageType linkage)
588 {
589 LL_Function *new_function = (LL_Function *)calloc(1, sizeof(LL_Function));
590 new_function->name = ll_manage_strdup(module, name);
591 new_function->return_type = return_type;
592 new_function->first = NULL;
593 new_function->last = NULL;
594 new_function->next = NULL;
595 new_function->arguments = NULL;
596 new_function->num_args = 0;
597 new_function->num_locals = 0;
598 new_function->is_kernel = is_kernel;
599 new_function->launch_bounds = launch_bounds;
600 new_function->launch_bounds_minctasm = launch_bounds_minctasm;
601 new_function->calling_convention =
602 ll_manage_strdup(module, calling_convention);
603 new_function->linkage = linkage;
604
605 if (module->last == NULL) {
606 module->first = new_function;
607 } else {
608 module->last->next = new_function;
609 }
610
611 module->last = new_function;
612
613 new_function->local_vars.values =
614 (LL_Value **)ll_manage_calloc(module, 16, sizeof(LL_Value));
615 new_function->local_vars.num_values = 16;
616
617 return new_function;
618 }
619
620 /**
621 \brief Create an LL_Function
622
623 Must be given its name and full \c LL_FUNCTION type. Note: This does not add
624 the new function to the module's list of functions.
625 */
626 LL_Function *
ll_create_function_from_type(LL_Type * func_type,const char * name)627 ll_create_function_from_type(LL_Type *func_type, const char *name)
628 {
629 LLVMModuleRef module = func_type->module;
630 LL_Function *new_function = (LL_Function *)calloc(1, sizeof(LL_Function));
631
632 CHECK(func_type->data_type == LL_FUNCTION);
633 CHECK(func_type->sub_elements > 0);
634
635 new_function->name = ll_manage_strdup(module, name);
636 new_function->return_type = func_type->sub_types[0];
637 ll_set_function_num_arguments(new_function, func_type->sub_elements - 1);
638
639 return new_function;
640 }
641
642
643 LL_Function *
ll_create_device_function_from_type(LLVMModuleRef module,LL_Type * func_type,const char * name,int is_kernel,int launch_bounds,const char * calling_convention,enum LL_LinkageType linkage)644 ll_create_device_function_from_type(LLVMModuleRef module, LL_Type *func_type,
645 const char *name, int is_kernel,
646 int launch_bounds,
647 const char *calling_convention,
648 enum LL_LinkageType linkage)
649 {
650 LL_Function *new_function = (LL_Function *)calloc(1, sizeof(LL_Function));
651
652 CHECK(func_type->data_type == LL_FUNCTION);
653 CHECK(func_type->sub_elements > 0);
654
655 new_function->name = ll_manage_strdup(module, name);
656 new_function->return_type = func_type->sub_types[0];
657 ll_set_function_num_arguments(new_function, func_type->sub_elements - 1);
658
659 new_function->is_kernel = is_kernel;
660 new_function->launch_bounds = launch_bounds;
661 new_function->calling_convention =
662 ll_manage_strdup(module, calling_convention);
663 new_function->linkage = linkage;
664
665 if (module->last == NULL) {
666 module->first = new_function;
667 } else {
668 module->last->next = new_function;
669 }
670
671 module->last = new_function;
672
673 new_function->local_vars.values =
674 (LL_Value **)ll_manage_calloc(module, 16, sizeof(LL_Value));
675 new_function->local_vars.num_values = 16;
676
677 return new_function;
678 }
679
680 void
ll_create_sym(struct LL_Symbols_ * symbol_table,int index,LL_Value * new_value)681 ll_create_sym(struct LL_Symbols_ *symbol_table, int index, LL_Value *new_value)
682 {
683 int new_size;
684
685 if (index >= symbol_table->num_values) {
686 new_size = (3 * (index + 1)) / 2;
687 symbol_table->values = (LL_Value **)realloc(symbol_table->values,
688 new_size * sizeof(LL_Value *));
689 memset(&(symbol_table->values[symbol_table->num_values]), 0,
690 (new_size - symbol_table->num_values) * sizeof(LL_Value *));
691 symbol_table->num_values = new_size;
692 }
693 symbol_table->values[index] = new_value;
694 }
695
696 void
ll_set_function_num_arguments(struct LL_Function_ * function,int num_args)697 ll_set_function_num_arguments(struct LL_Function_ *function, int num_args)
698 {
699 function->arguments = (LL_Value **)calloc(num_args, sizeof(LL_Value *));
700 function->num_args = num_args;
701 }
702
703 void
ll_set_function_argument(struct LL_Function_ * function,int index,LL_Value * argument)704 ll_set_function_argument(struct LL_Function_ *function, int index,
705 LL_Value *argument)
706 {
707 function->arguments[index] = argument;
708 }
709
710 const char *
ll_get_str_type_for_basic_type(enum LL_BaseDataType type)711 ll_get_str_type_for_basic_type(enum LL_BaseDataType type)
712 {
713 switch (type) {
714 case LL_NOTYPE:
715 return "";
716 case LL_LABEL:
717 return "label";
718 case LL_METADATA:
719 return "metadata";
720 case LL_VOID:
721 return "void";
722 case LL_I1:
723 return "i1";
724 case LL_I8:
725 return "i8";
726 case LL_I16:
727 return "i16";
728 case LL_I24:
729 return "i24";
730 case LL_I32:
731 return "i32";
732 case LL_I40:
733 return "i40";
734 case LL_I48:
735 return "i48";
736 case LL_I56:
737 return "i56";
738 case LL_I64:
739 return "i64";
740 case LL_I128:
741 return "i128";
742 case LL_I256:
743 return "i256";
744 case LL_HALF:
745 return "half";
746 case LL_FLOAT:
747 return "float";
748 case LL_DOUBLE:
749 return "double";
750 case LL_FP128:
751 return "fp128";
752 case LL_X86_FP80:
753 return "x86_fp80";
754 case LL_PPC_FP128:
755 return "ppc_fp128";
756 case LL_X86_MMX:
757 return "x86_mmx";
758 default:
759 return "ERR";
760 }
761 return "ERR";
762 }
763
764 /**
765 \brief Get the size of a type in bytes without checks
766 \param type The type to be examined
767 \param noSize Set if not null and \p type isn't concrete [output]
768 \return size of an object of type \p type or 0
769 */
770 static ISZ_T
LLTypeGetBytesUnchecked(LL_Type * type,int * noSize)771 LLTypeGetBytesUnchecked(LL_Type *type, int *noSize)
772 {
773 if (noSize)
774 *noSize = 0;
775 switch (type->data_type) {
776 case LL_I1:
777 case LL_I8:
778 return 1;
779 case LL_I16:
780 return 2;
781 case LL_I24:
782 return 3;
783 case LL_I32:
784 return 4;
785 case LL_I40:
786 return 5;
787 case LL_I48:
788 return 6;
789 case LL_I56:
790 return 7;
791 case LL_I64:
792 return 8;
793 case LL_I128:
794 return 16;
795 case LL_I256:
796 return 32;
797 case LL_HALF:
798 return 2;
799 case LL_FLOAT:
800 return 4;
801 case LL_DOUBLE:
802 return 8;
803 case LL_X86_FP80:
804 case LL_FP128:
805 case LL_PPC_FP128:
806 return 16;
807 case LL_PTR:
808 /* FIXME: Use the data layout so we can cross compile. */
809 return sizeof(void *); // 8 * TARGET_PTRSIZE
810 case LL_ARRAY:
811 case LL_VECTOR:
812 return type->sub_elements * ll_type_bytes(type->sub_types[0]);
813 case LL_STRUCT: {
814 unsigned i;
815 ISZ_T sum;
816 if (type->sub_offsets) {
817 /* This should be the size in memory sans assumptions */
818 return type->sub_offsets[type->sub_elements];
819 }
820 sum = 0;
821 /* Note: We're assuming the struct has no implicit padding. */
822 for (i = 0; i < type->sub_elements; i++)
823 sum += ll_type_bytes(type->sub_types[i]);
824 return sum;
825 }
826 default:
827 if (noSize)
828 *noSize = 1;
829 break;
830 }
831 return 0;
832 }
833
834 /**
835 \brief Get the size of a type in bytes
836 \param type The type to be examined
837 \return size of an object of type \p type or 0
838 */
839 ISZ_T
ll_type_bytes(LL_Type * type)840 ll_type_bytes(LL_Type *type)
841 {
842 int notConcrete;
843 ISZ_T size = LLTypeGetBytesUnchecked(type, ¬Concrete);
844 if (notConcrete) {
845 interr("ll_type_bytes: Not a concrete type", type->data_type, ERR_Fatal);
846 }
847 return size;
848 }
849
850 /**
851 \brief Get the size of a type in bytes without checks
852 \param type The type to be examined
853 \return size of object of type \p type or 0
854 */
855 ISZ_T
ll_type_bytes_unchecked(LL_Type * type)856 ll_type_bytes_unchecked(LL_Type *type)
857 {
858 return LLTypeGetBytesUnchecked(type, NULL);
859 }
860
861 /**
862 \brief Get the number of bits in the given integer type
863 \param type The \ref LL_Type to inspect
864 \return 0 iff \p type is not an integer type
865 */
866 unsigned
ll_type_int_bits(LL_Type * type)867 ll_type_int_bits(LL_Type *type)
868 {
869 switch (type->data_type) {
870 case LL_I1:
871 return 1;
872 case LL_I8:
873 return 8;
874 case LL_I16:
875 return 16;
876 case LL_I24:
877 return 24;
878 case LL_I32:
879 return 32;
880 case LL_I40:
881 return 40;
882 case LL_I48:
883 return 48;
884 case LL_I56:
885 return 56;
886 case LL_I64:
887 return 64;
888 case LL_I128:
889 return 128;
890 case LL_I256:
891 return 256;
892 default:
893 break;
894 }
895 return 0;
896 }
897
898 int
ll_type_is_pointer_to_function(LL_Type * ty)899 ll_type_is_pointer_to_function(LL_Type *ty)
900 {
901 return (ty->data_type == LL_PTR) &&
902 (ty->sub_types[0]->data_type == LL_FUNCTION);
903 }
904
905 LL_Type *
ll_type_array_elety(LL_Type * ty)906 ll_type_array_elety(LL_Type *ty)
907 {
908 return (ty->data_type == LL_ARRAY) ? ty->sub_types[0] : (LL_Type *)0;
909 }
910
911 int
ll_type_is_fp(LL_Type * ty)912 ll_type_is_fp(LL_Type *ty)
913 {
914 switch (ty->data_type) {
915 case LL_HALF:
916 case LL_FLOAT:
917 case LL_DOUBLE:
918 case LL_FP128:
919 case LL_X86_FP80:
920 case LL_PPC_FP128:
921 return 1;
922 default:
923 break;
924 }
925 return 0;
926 }
927
928 int
ll_type_is_mem_seq(LL_Type * ty)929 ll_type_is_mem_seq(LL_Type *ty)
930 {
931 switch (ty->data_type) {
932 case LL_PTR:
933 case LL_ARRAY:
934 return 1;
935 default:
936 break;
937 }
938 return 0;
939 }
940
941 /**
942 \brief Create a "blank" value
943 */
944 static LL_Value *
ll_create_blank_value(LLVMModuleRef module,const char * data)945 ll_create_blank_value(LLVMModuleRef module, const char *data)
946 {
947 LL_Value *ret_value = (LL_Value *)ll_manage_malloc(module, sizeof(LL_Value));
948 ret_value->data = (data ? ll_manage_strdup(module, data) : NULL);
949 ret_value->linkage = LL_NO_LINKAGE;
950 ret_value->mvtype = LL_DEFAULT;
951 ret_value->type_struct = NULL;
952 ret_value->align_bytes = 0;
953 ret_value->flags = 0;
954 ret_value->storage = NULL;
955 ret_value->dbg_mdnode = ll_get_md_null();
956 ret_value->dbg_sptr = 0;
957 return ret_value;
958 }
959
960 LL_Value *
ll_create_pointer_value(LLVMModuleRef module,enum LL_BaseDataType type,const char * data,int addrspace)961 ll_create_pointer_value(LLVMModuleRef module, enum LL_BaseDataType type,
962 const char *data, int addrspace)
963 {
964 LL_Value *ret_value = ll_create_blank_value(module, data);
965 LL_Type *base_type;
966
967 base_type = ll_create_basic_type(module, type, addrspace);
968
969 ret_value->type_struct = ll_get_pointer_type(base_type);
970
971 return ret_value;
972 }
973
974 LL_Value *
ll_create_value_from_type(LLVMModuleRef module,LL_Type * type,const char * data)975 ll_create_value_from_type(LLVMModuleRef module, LL_Type *type, const char *data)
976 {
977 LL_Value *ret_value = ll_create_blank_value(module, data);
978 ret_value->type_struct = type;
979 return ret_value;
980 }
981
982 LL_Value *
ll_create_array_value_from_type(LLVMModuleRef module,LL_Type * type,const char * data,int addrspace)983 ll_create_array_value_from_type(LLVMModuleRef module, LL_Type *type,
984 const char *data, int addrspace)
985 {
986 LL_Value *ret_value = ll_create_blank_value(module, data);
987 ret_value->type_struct = ll_get_array_type(type, 0, addrspace);
988 return ret_value;
989 }
990
991 LL_Value *
ll_create_pointer_value_from_type(LLVMModuleRef module,LL_Type * type,const char * data,int addrspace)992 ll_create_pointer_value_from_type(LLVMModuleRef module, LL_Type *type,
993 const char *data, int addrspace)
994 {
995 LL_Value *ret_value = ll_create_blank_value(module, data);
996 ret_value->type_struct =
997 ll_get_pointer_type(ll_get_addrspace_type(type, addrspace));
998 return ret_value;
999 }
1000
1001 /**
1002 \brief get pointer's address space
1003 \param ptr A pointer type
1004
1005 Given a pointer type, return the address space that is being pointed into.
1006 */
1007 int
ll_get_pointer_addrspace(LL_Type * ptr)1008 ll_get_pointer_addrspace(LL_Type *ptr)
1009 {
1010 assert(ptr->data_type == LL_PTR, "ll_get_pointer_addrspace: Pointer required",
1011 ptr->data_type, ERR_Fatal);
1012 return ptr->sub_types[0]->addrspace;
1013 }
1014
1015 /*
1016 * Get a uniqued copy of type.
1017 *
1018 * If we already have a type identical to t, return that. Otherwise create a
1019 * copy of t and remember that.
1020 *
1021 * The copy will be shallow - the str and sub_types pointers will be copied
1022 * directly from type.
1023 */
1024 static struct LL_Type_ *
unique_type(LLVMModuleRef module,const struct LL_Type_ * type)1025 unique_type(LLVMModuleRef module, const struct LL_Type_ *type)
1026 {
1027 hash_key_t existing = hashset_lookup(module->anon_types, type);
1028 struct LL_Type_ *copy;
1029
1030 if (existing)
1031 return (struct LL_Type_ *)existing;
1032
1033 /* No such type exists. Save a copy. */
1034 copy = (struct LL_Type_ *)ll_manage_malloc(module, sizeof(struct LL_Type_));
1035 memcpy(copy, type, sizeof(*copy));
1036 copy->module = module;
1037 hashset_insert(module->anon_types, copy);
1038 return copy;
1039 }
1040
1041 LL_Type *
ll_create_basic_type(LLVMModuleRef module,enum LL_BaseDataType type,int addrspace)1042 ll_create_basic_type(LLVMModuleRef module, enum LL_BaseDataType type,
1043 int addrspace)
1044 {
1045 struct LL_Type_ new_type;
1046 struct LL_Type_ *ret_type;
1047
1048 assert(type <= LL_X86_MMX, "Basic LLVM base data type required", type,
1049 ERR_Fatal);
1050
1051 new_type.str = NULL;
1052 new_type.data_type = type;
1053 new_type.flags = 0;
1054 new_type.sub_types = NULL;
1055 new_type.sub_offsets = NULL;
1056 new_type.sub_elements = 0;
1057 new_type.sub_padding = 0;
1058 new_type.addrspace = addrspace;
1059
1060 ret_type = unique_type(module, &new_type);
1061
1062 /* LLVM basic types don't have address spaces (only pointer types do), so
1063 * don't include it in the representation string. */
1064 if (!ret_type->str) {
1065 ret_type->str = ll_get_str_type_for_basic_type(type);
1066 }
1067
1068 return ret_type;
1069 }
1070
1071 /**
1072 \brief Create an integer type with \p bits bits.
1073 \param module The LLVM module
1074 \param bits The number of bits used in the representation
1075 \param addrspace The address space where type will be in
1076 \return A uniqued integral \ref LL_Type
1077
1078 See \ref LL_BaseDataType for all the integer bitwidths supported.
1079 */
1080 LL_Type *
ll_create_int_type_with_addrspace(LLVMModuleRef module,unsigned bits,int addrspace)1081 ll_create_int_type_with_addrspace(LLVMModuleRef module, unsigned bits,
1082 int addrspace)
1083 {
1084 enum LL_BaseDataType bdt = LL_NOTYPE;
1085 switch (bits) {
1086 case 1:
1087 bdt = LL_I1;
1088 break;
1089 case 8:
1090 bdt = LL_I8;
1091 break;
1092 case 16:
1093 bdt = LL_I16;
1094 break;
1095 case 24:
1096 bdt = LL_I24;
1097 break;
1098 case 32:
1099 bdt = LL_I32;
1100 break;
1101 case 40:
1102 bdt = LL_I40;
1103 break;
1104 case 48:
1105 bdt = LL_I48;
1106 break;
1107 case 56:
1108 bdt = LL_I56;
1109 break;
1110 case 64:
1111 bdt = LL_I64;
1112 break;
1113 case 128:
1114 bdt = LL_I128;
1115 break;
1116 case 256:
1117 bdt = LL_I256;
1118 break;
1119 default:
1120 interr("Unsupport integer bitwidth", bits, ERR_Fatal);
1121 }
1122 return ll_create_basic_type(module, bdt, addrspace);
1123 }
1124
1125 /**
1126 \brief Create an integer type with \p bits bits.
1127 \param module The LLVM module
1128 \param bits The number of bits used in the representation
1129 \return A uniqued integral \ref LL_Type
1130
1131 See \ref LL_BaseDataType for all the integer bitwidths supported.
1132 */
1133 LL_Type *
ll_create_int_type(LLVMModuleRef module,unsigned bits)1134 ll_create_int_type(LLVMModuleRef module, unsigned bits)
1135 {
1136 return ll_create_int_type_with_addrspace(module, bits, LL_AddrSp_Default);
1137 }
1138
1139 /**
1140 \brief Get a version of type in different address space.
1141
1142 Note: In LLVM IR only pointer types have an address space.
1143 */
1144 LL_Type *
ll_get_addrspace_type(LL_Type * type,int addrspace)1145 ll_get_addrspace_type(LL_Type *type, int addrspace)
1146 {
1147 struct LL_Type_ new_type;
1148
1149 if (addrspace == type->addrspace)
1150 return type;
1151
1152 memcpy(&new_type, type, sizeof(new_type));
1153 new_type.addrspace = addrspace;
1154
1155 return unique_type(type->module, &new_type);
1156 }
1157
1158 /**
1159 \brief Create a pointer type pointing to the given pointee type.
1160 */
1161 LL_Type *
ll_get_pointer_type(LL_Type * type)1162 ll_get_pointer_type(LL_Type *type)
1163 {
1164 struct LL_Type_ new_type;
1165 struct LL_Type_ *ret_type;
1166 LLVMModuleRef module = type->module;
1167
1168 new_type.str = NULL;
1169 new_type.data_type = LL_PTR;
1170 new_type.flags = 0;
1171 new_type.sub_types = &type;
1172 new_type.sub_offsets = NULL;
1173 new_type.sub_elements = 1;
1174 new_type.sub_padding = NULL;
1175 new_type.addrspace = 0;//type->addrspace;
1176
1177 ret_type = unique_type(module, &new_type);
1178
1179 /* We need to assign a representation string and allocate a proper array
1180 * for sub_types if unique_type() actually allocated a new type. */
1181 if (!ret_type->str) {
1182 char suffix[32] = "*";
1183 char *new_str;
1184 int size;
1185
1186 if (type->addrspace) {
1187 snprintf(suffix, sizeof(suffix), " addrspace(%d)*", type->addrspace);
1188 }
1189 size = strlen(type->str) + strlen(suffix) + 1;
1190 new_str = (char *)ll_manage_malloc(module, size);
1191 sprintf(new_str, "%s%s", type->str, suffix);
1192
1193 ret_type->str = new_str;
1194 ret_type->sub_types =
1195 (LL_Type **)ll_manage_malloc(module, sizeof(LL_Type *));
1196 ret_type->sub_types[0] = type;
1197 }
1198
1199 return ret_type;
1200 }
1201
1202 LL_Type *
ll_get_array_type(LL_Type * type,BIGUINT64 num_elements,int addrspace)1203 ll_get_array_type(LL_Type *type, BIGUINT64 num_elements, int addrspace)
1204 {
1205 LLVMModuleRef module = type->module;
1206 struct LL_Type_ new_type;
1207 struct LL_Type_ *ret_type;
1208
1209 new_type.str = NULL;
1210 new_type.data_type = LL_ARRAY;
1211 new_type.flags = 0;
1212 new_type.sub_types = &type;
1213 new_type.sub_offsets = NULL;
1214 new_type.sub_elements = num_elements;
1215 new_type.sub_padding = NULL;
1216 new_type.addrspace = addrspace;
1217
1218 ret_type = unique_type(module, &new_type);
1219
1220 if (!ret_type->str) {
1221 const char *suffix = "]";
1222 char prefix[32];
1223 char *new_str;
1224
1225 sprintf(prefix, "[%" BIGIPFSZ "u x ", num_elements);
1226 new_str = (char *)ll_manage_malloc(
1227 module, strlen(prefix) + strlen(type->str) + strlen(suffix) + 1);
1228 sprintf(new_str, "%s%s%s", prefix, type->str, suffix);
1229
1230 ret_type->str = new_str;
1231 ret_type->sub_types =
1232 (LL_Type **)ll_manage_malloc(module, sizeof(LL_Type *));
1233 ret_type->sub_types[0] = type;
1234 }
1235
1236 return ret_type;
1237 }
1238
1239 /**
1240 \brief Get a vector type <tt> \<num x elem\> </tt>
1241 */
1242 LL_Type *
ll_get_vector_type(LL_Type * type,unsigned num_elements)1243 ll_get_vector_type(LL_Type *type, unsigned num_elements)
1244 {
1245 LLVMModuleRef module = type->module;
1246 struct LL_Type_ new_type;
1247 struct LL_Type_ *ret_type;
1248
1249 new_type.str = NULL;
1250 new_type.data_type = LL_VECTOR;
1251 new_type.flags = 0;
1252 new_type.sub_types = &type;
1253 new_type.sub_offsets = NULL;
1254 new_type.sub_elements = num_elements;
1255 new_type.sub_padding = NULL;
1256 new_type.addrspace = 0;
1257
1258 ret_type = unique_type(module, &new_type);
1259
1260 if (!ret_type->str) {
1261 char prefix[32];
1262 char *new_str;
1263
1264 sprintf(prefix, "<%u x ", num_elements);
1265
1266 new_str = (char *)ll_manage_malloc(module,
1267 strlen(prefix) + strlen(type->str) + 2);
1268 sprintf(new_str, "%s%s>", prefix, type->str);
1269
1270 ret_type->str = new_str;
1271 ret_type->sub_types =
1272 (LL_Type **)ll_manage_malloc(module, sizeof(LL_Type *));
1273 ret_type->sub_types[0] = type;
1274 }
1275
1276 return ret_type;
1277 }
1278
1279 /**
1280 * \brief Replace the entire body of a named struct type. This may be used to
1281 * change the number of elements in an existing named struct.
1282 *
1283 * It is not possible to change the body of an anonymous struct type.
1284 *
1285 * The struct type is identified by the LL_Value returned from
1286 * ll_create_named_struct_type().
1287 */
1288 void
ll_set_struct_body(LL_Type * ctype,LL_Type * const * elements,unsigned * const offsets,char * const pads,unsigned num_elements,int is_packed)1289 ll_set_struct_body(LL_Type *ctype, LL_Type *const *elements,
1290 unsigned *const offsets, char *const pads,
1291 unsigned num_elements, int is_packed)
1292 {
1293 struct LL_Type_ *type = (struct LL_Type_ *)ctype; /* cast away const */
1294 assert(type->data_type == LL_STRUCT &&
1295 (type->flags & LL_TYPE_IS_NAMED_STRUCT),
1296 "Can only set the body on a named struct type", 0, ERR_Fatal);
1297 if (type->sub_types)
1298 free(type->sub_types);
1299 type->sub_elements = num_elements;
1300 type->sub_types = NULL;
1301 type->sub_offsets = NULL;
1302 type->sub_padding = NULL;
1303 if (num_elements > 0) {
1304 type->sub_types = (LL_Type **)calloc(num_elements, sizeof(LL_Type *));
1305 if (elements)
1306 memcpy(type->sub_types, elements, num_elements * sizeof(LL_Type *));
1307 if (offsets) {
1308 type->sub_offsets =
1309 (unsigned *)calloc(num_elements + 1, sizeof(unsigned));
1310 memcpy(type->sub_offsets, offsets, (num_elements + 1) * sizeof(unsigned));
1311 }
1312 if (pads) {
1313 type->sub_padding = (char *)calloc(num_elements, 1);
1314 memcpy(type->sub_padding, pads, num_elements);
1315 }
1316 }
1317 if (is_packed)
1318 type->flags |= LL_TYPE_IS_PACKED_STRUCT;
1319 }
1320
1321 LL_Value *
ll_named_struct_type_exists(LLVMModuleRef module,int id,const char * format,...)1322 ll_named_struct_type_exists(LLVMModuleRef module, int id, const char *format,
1323 ...)
1324 {
1325 va_list ap;
1326 char buffer[256];
1327 LL_Value *struct_value;
1328
1329 buffer[0] = '%';
1330 buffer[1] = '\0';
1331 va_start(ap, format);
1332 vsnprintf(buffer + 1, sizeof(buffer) - 1, format, ap);
1333 va_end(ap);
1334 buffer[sizeof(buffer) - 1] = '\0';
1335 if (hashset_lookup(module->used_type_names, buffer)) {
1336 if (hashmap_lookup(module->user_structs_byid, INT2HKEY(id),
1337 (hash_data_t *)&struct_value)) {
1338 return struct_value;
1339 }
1340 }
1341 return NULL;
1342 }
1343
1344 /**
1345 \brief Create a new named struct type in the module.
1346
1347 Type can be uniquely named if parameter unique is set to true
1348
1349 If id is positive, the new struct type is associated with the id so it can be
1350 retrieved with ll_get_struct_type(). Usually, the TY_STRUCT dtype is used as
1351 an id.
1352
1353 It is an error to create multiple types with the same positive id, unless
1354 ll_reset_module_types() is called in between.
1355
1356 A zero or negative id is ignored.
1357
1358 The name of the new struct type will be based on the printf-style format and
1359 additional arguments. A '%' character will be prepended, don't include one in
1360 the format string.
1361
1362 The new struct name may not be exactly as requested, so don't depend on it.
1363 The type name may be modified to ensure its uniqueness.
1364 */
1365 LL_Type *
ll_create_named_struct_type(LLVMModuleRef module,int id,bool unique,const char * format,...)1366 ll_create_named_struct_type(LLVMModuleRef module, int id, bool unique,
1367 const char *format, ...)
1368 {
1369 va_list ap;
1370 struct LL_Type_ *new_type;
1371 LL_Value *struct_value;
1372
1373 if (!unique) {
1374 struct_value = ll_named_struct_type_exists(module, id, format, ap);
1375 if (struct_value)
1376 return struct_value->type_struct;
1377 ll_remove_struct_type(module, id);
1378 }
1379 va_start(ap, format);
1380 new_type = (struct LL_Type_ *)calloc(1, sizeof(struct LL_Type_));
1381 new_type->str = unique_name(module->used_type_names, '%', format, ap);
1382 va_end(ap);
1383
1384 new_type->module = module;
1385 new_type->data_type = LL_STRUCT;
1386 new_type->flags = LL_TYPE_IS_NAMED_STRUCT;
1387
1388 /* Maintain a list of types in the module so we can print them later. */
1389 struct_value = ll_create_value_from_type(module, new_type, new_type->str);
1390 ll_create_sym(&(module->user_structs), module->num_user_structs,
1391 struct_value);
1392 module->num_user_structs++;
1393
1394 if (id > 0) {
1395 hash_key_t old_id = hashmap_replace(module->user_structs_byid, INT2HKEY(id),
1396 (hash_data_t *)&struct_value);
1397 assert(!old_id, "Duplicate structs created for id.", id, ERR_Fatal);
1398 }
1399
1400 return new_type;
1401 }
1402
1403 /**
1404 \brief Remove struct type from hashmap
1405
1406 This is required whenever a struct has a same dtype but in different modules
1407 defined in same file
1408 */
1409 void
ll_remove_struct_type(LLVMModuleRef module,int struct_id)1410 ll_remove_struct_type(LLVMModuleRef module, int struct_id)
1411 {
1412 hashmap_erase(module->user_structs_byid, INT2HKEY(struct_id), NULL);
1413 }
1414
1415 /**
1416 \brief Get an existing named struct type by id.
1417
1418 The id must be positive.
1419
1420 If no named struct type has been created with that id, NULL is returned. If
1421 required is true, an internal compiler error is signaled instead of returning
1422 NULL.
1423 */
1424 LL_Type *
ll_get_struct_type(LLVMModuleRef module,int struct_id,int required)1425 ll_get_struct_type(LLVMModuleRef module, int struct_id, int required)
1426 {
1427 LL_Value *struct_value = NULL;
1428
1429 if (hashmap_lookup(module->user_structs_byid, INT2HKEY(struct_id),
1430 (hash_data_t *)&struct_value))
1431 return struct_value->type_struct;
1432
1433 assert(!required, "Can't find user defined struct.", struct_id, ERR_Fatal);
1434 return 0;
1435 }
1436
1437 /**
1438 \brief Get an anonymous struct type.
1439
1440 Anonymous structs are identified by their contents, they don't have a name.
1441 This will either return an existing anonymous struct type with the required
1442 elements, or create a new type with a copy of the elements array.
1443
1444 A packed struct has alignment 1 and no padding between members.
1445 */
1446 LL_Type *
ll_create_anon_struct_type(LLVMModuleRef module,LL_Type * elements[],unsigned num_elements,bool is_packed,int addrspace)1447 ll_create_anon_struct_type(LLVMModuleRef module, LL_Type *elements[],
1448 unsigned num_elements, bool is_packed, int addrspace)
1449 {
1450 struct LL_Type_ new_type;
1451 struct LL_Type_ *ret_type;
1452 unsigned offset;
1453 int i;
1454
1455 new_type.str = NULL;
1456 new_type.data_type = LL_STRUCT;
1457 new_type.flags = is_packed ? LL_TYPE_IS_PACKED_STRUCT : 0;
1458 new_type.sub_types = elements;
1459 new_type.sub_offsets = (unsigned *)calloc(num_elements + 1, sizeof(unsigned));
1460 new_type.sub_elements = num_elements;
1461 new_type.sub_padding = NULL;
1462 new_type.addrspace = 0; //addrspace;
1463
1464 for (i = 0, offset = 0; i < num_elements; ++i) {
1465 new_type.sub_offsets[i] = offset;
1466 offset += ll_type_bytes(new_type.sub_types[i]);
1467 }
1468 new_type.sub_offsets[num_elements] = offset;
1469
1470 ret_type = unique_type(module, &new_type);
1471
1472 if (!ret_type->str) {
1473 if (num_elements == 0) {
1474 ret_type->str = is_packed ? "<{}>" : "{}";
1475 ret_type->sub_types = NULL;
1476 } else {
1477 char *new_str;
1478 unsigned i, len, pos;
1479
1480 /* Build new_str = "{i32, i8}". First compute length to allocate. */
1481 len = 3;
1482 for (i = 0; i < num_elements; i++)
1483 len += strlen(elements[i]->str) + 2;
1484
1485 new_str = (char *)ll_manage_malloc(module, len);
1486 sprintf(new_str, is_packed ? "<{%s" : "{%s", elements[0]->str);
1487 pos = strlen(new_str);
1488 for (i = 1; i < num_elements; i++) {
1489 sprintf(new_str + pos, ", %s", elements[i]->str);
1490 pos += strlen(new_str + pos);
1491 }
1492 strcat(new_str + pos, is_packed ? "}>" : "}");
1493 ret_type->str = new_str;
1494 ret_type->sub_types = (LL_Type **)ll_manage_malloc(
1495 module, num_elements * sizeof(struct LL_Type_ *));
1496 memcpy(ret_type->sub_types, elements,
1497 num_elements * sizeof(struct LL_Type_ *));
1498 }
1499 }
1500
1501 return ret_type;
1502 }
1503
1504 /**
1505 \brief Get a function type.
1506
1507 The return type is passed as \c args[0], so the \c args[] array should have a
1508 total of <tt> num_args+1 </tt> elements.
1509 */
1510 LL_Type *
ll_create_function_type(LLVMModuleRef module,LL_Type * args[],unsigned num_args,int is_varargs)1511 ll_create_function_type(LLVMModuleRef module, LL_Type *args[],
1512 unsigned num_args, int is_varargs)
1513 {
1514 struct LL_Type_ new_type;
1515 struct LL_Type_ *ret_type;
1516
1517 new_type.str = NULL;
1518 new_type.data_type = LL_FUNCTION;
1519 new_type.flags = is_varargs ? LL_TYPE_IS_VARARGS_FUNC : 0;
1520 new_type.sub_types = args;
1521 new_type.sub_offsets = NULL;
1522 new_type.sub_elements = num_args + 1;
1523 new_type.sub_padding = NULL;
1524 new_type.addrspace = 0;
1525
1526 ret_type = unique_type(module, &new_type);
1527
1528 if (!ret_type->str) {
1529 char *new_str;
1530 unsigned i, len, pos;
1531
1532 /* Build new_str = "i32 (i8, i16)". First compute length to allocate. */
1533 len = 7;
1534 for (i = 0; i <= num_args; i++)
1535 len += strlen(args[i]->str) + 2;
1536
1537 new_str = (char *)ll_manage_malloc(module, len);
1538 /* Warning: MSVC's version of sprintf does not support %n by default. */
1539 sprintf(new_str, "%s (", args[0]->str);
1540 pos = strlen(new_str);
1541
1542 for (i = 1; i <= num_args; i++) {
1543 sprintf(new_str + pos, i > 1 ? ", %s" : "%s", args[i]->str);
1544 pos += strlen(new_str + pos);
1545 }
1546
1547 if (!is_varargs)
1548 strcat(new_str + pos, ")");
1549 else if (num_args == 0)
1550 strcat(new_str + pos, "...)");
1551 else
1552 strcat(new_str + pos, ", ...)");
1553
1554 ret_type->str = new_str;
1555 ret_type->sub_types = (LL_Type **)ll_manage_malloc(
1556 module, (1 + num_args) * sizeof(struct LL_Type_ *));
1557 memcpy(ret_type->sub_types, args,
1558 (1 + num_args) * sizeof(struct LL_Type_ *));
1559 }
1560 return ret_type;
1561 }
1562
1563 /** \brief Get the textual representation of a calling convention. */
1564 const char *
ll_get_calling_conv_str(enum LL_CallConv cc)1565 ll_get_calling_conv_str(enum LL_CallConv cc)
1566 {
1567 switch (cc) {
1568 case LL_CallConv_C:
1569 return "ccc";
1570 case LL_CallConv_Fast:
1571 return "fastcc";
1572 case LL_CallConv_Cold:
1573 return "coldcc";
1574 case LL_CallConv_X86_StdCall:
1575 return "x86_stdcallcc";
1576 case LL_CallConv_X86_FastCall:
1577 return "x86_fastcallcc";
1578 case LL_CallConv_X86_ThisCall:
1579 return "x86_thiscallcc";
1580 case LL_CallConv_X86_VectorCall:
1581 return "x86_vectorcallcc";
1582 case LL_CallConv_APCS:
1583 return "arm_apcscc";
1584 case LL_CallConv_AAPCS:
1585 return "arm_aapcscc";
1586 case LL_CallConv_AAPCS_VFP:
1587 return "arm_aapcs_vfpcc";
1588 case LL_CallConv_PTX_Kernel:
1589 return "ptx_kernel";
1590 case LL_CallConv_PTX_Device:
1591 return "ptx_device";
1592 case LL_CallConv_SPIR_FUNC:
1593 return "spir_func";
1594 case LL_CallConv_SPIR_KERNEL:
1595 return "spir_kernel";
1596 }
1597 interr("Unknown LLVM calling convention", cc, ERR_Fatal);
1598 return "unknown cc";
1599 }
1600
1601 /*
1602 * Interned constants.
1603 *
1604 * Constants are uniqued by their type_struct pointer (which is already
1605 * uniqued) and the text of their data.
1606 */
1607
1608 /* Find or create an LL_Value of the given type and textual representation.
1609 * Like ll_create_value_from_type() with uniquing.
1610 * Return an index into module->constants */
1611 static unsigned
intern_constant(LLVMModuleRef module,LL_Type * type,const char * data)1612 intern_constant(LLVMModuleRef module, LL_Type *type, const char *data)
1613 {
1614 LL_Value temp;
1615 hash_data_t oldval;
1616 LL_Value *newval;
1617 unsigned slot;
1618
1619 memset(&temp, 0, sizeof(temp));
1620 temp.data = data;
1621 temp.type_struct = type;
1622
1623 /* Was this constant seen before? */
1624 if (hashmap_lookup(module->constants_map, &temp, &oldval))
1625 return HKEY2INT(oldval);
1626
1627 /* First time we see this constant. */
1628 newval = ll_create_value_from_type(module, type, data);
1629 slot = module->constants_count;
1630 if (++module->constants_count > module->constants_alloc) {
1631 module->constants_alloc *= 2;
1632 module->constants = (LL_Value **)realloc(module->constants,
1633 module->constants_alloc *
1634 sizeof(module->constants[0]));
1635 }
1636 module->constants[slot] = newval;
1637 hashmap_insert(module->constants_map, newval, INT2HKEY(slot));
1638
1639 return slot;
1640 }
1641
1642 /* Get the slot number if an interned constant int. */
1643 static unsigned
intern_const_int(LLVMModuleRef module,unsigned bits,long long value)1644 intern_const_int(LLVMModuleRef module, unsigned bits, long long value)
1645 {
1646 LL_Type *type = ll_create_int_type(module, bits);
1647 char buf[32];
1648
1649 sprintf(buf, "%lld", value);
1650 return intern_constant(module, type, buf);
1651 }
1652
1653 /**
1654 \brief Get a shared LL_Value representing a constant integer of up to 64
1655 bits.
1656 */
1657 LL_Value *
ll_get_const_int(LLVMModuleRef module,unsigned bits,long long value)1658 ll_get_const_int(LLVMModuleRef module, unsigned bits, long long value)
1659 {
1660 unsigned slot = intern_const_int(module, bits, value);
1661 return module->constants[slot];
1662 }
1663
1664 /**
1665 \brief Get a pointer to an LLVM global given its name and type.
1666
1667 This will prepend \c \@ to the name and add one level of indirection to the
1668 type.
1669 */
1670 LL_Value *
ll_get_global_pointer(const char * name,LL_Type * type)1671 ll_get_global_pointer(const char *name, LL_Type *type)
1672 {
1673 char *llvmname = (char *)malloc(strlen(name) + 2);
1674 unsigned slot;
1675
1676 sprintf(llvmname, "@%s", name);
1677 type = ll_get_pointer_type(type);
1678 slot = intern_constant(type->module, type, llvmname);
1679 free(llvmname);
1680
1681 return type->module->constants[slot];
1682 }
1683
1684 /**
1685 \brief Get the constant function pointer value representing a function
1686
1687 Note that the type of this function pointer depends on the added function
1688 arguments.
1689 */
1690 LL_Value *
ll_get_function_pointer(LLVMModuleRef module,LL_Function * function)1691 ll_get_function_pointer(LLVMModuleRef module, LL_Function *function)
1692 {
1693 LL_Type *func_type;
1694 unsigned i;
1695 LL_Type **args =
1696 (LL_Type **)malloc((1 + function->num_args) * sizeof(LL_Type *));
1697 args[0] = function->return_type;
1698 for (i = 0; i < function->num_args; ++i)
1699 args[i + 1] = function->arguments[i]->type_struct;
1700
1701 /* FIXME: LL_Function needs a is_varargs flag. */
1702 func_type = ll_create_function_type(module, args, function->num_args, false);
1703 free(args);
1704
1705 return ll_get_global_pointer(function->name, func_type);
1706 }
1707
1708 /* Return the type corresponding to applying one gep index */
1709 static LL_Type *
apply_gep_index(LL_Type * type,unsigned idx)1710 apply_gep_index(LL_Type *type, unsigned idx)
1711 {
1712 switch (type->data_type) {
1713 case LL_PTR:
1714 case LL_VECTOR:
1715 case LL_ARRAY:
1716 return type->sub_types[0];
1717 case LL_STRUCT:
1718 /* When statics and common blocks create real struct types, we can:
1719 assert(idx < type->sub_elements,
1720 "apply_gep_index: GEP index outside struct", idx, 4);
1721 */
1722 if (idx < type->sub_elements)
1723 return type->sub_types[idx];
1724 else
1725 return type;
1726 default:
1727 interr("apply_gep_index: Invalid data type for GEP", type->data_type,
1728 ERR_Fatal);
1729 }
1730 return NULL;
1731 }
1732
1733 /**
1734 \brief Get a shared \ref LL_Value representing a constant getelementptr of
1735 the provided pointer value.
1736
1737 This creates a gep-expression that may be used to initialize globals and
1738 metadata. It does not create an LL_GEP instruction.
1739
1740 The num_idx argument is the number of index operands on the gep
1741 expression. It should be at least 1.
1742
1743 The gep indices following num_idx should be ints.
1744
1745 Note: element type is the exected element type, this does not get elements of
1746 any different type.
1747 */
1748 LL_Value *
ll_get_const_gep(LLVMModuleRef module,LL_Value * ptr,unsigned num_idx,...)1749 ll_get_const_gep(LLVMModuleRef module, LL_Value *ptr, unsigned num_idx, ...)
1750 {
1751 va_list ap;
1752 unsigned slot;
1753 char *pointee;
1754 /* Space for getelementptr(<ptr>, i32 <idx0>, i32 <idx1>, ...) */
1755 char *name = (char *)malloc(19 + strlen(ptr->type_struct->str) +
1756 2 * strlen(ptr->data) + 16 * num_idx);
1757 char *p = name;
1758 LL_Type *type = ptr->type_struct;
1759
1760 /*** getelementpointer can only be used on pointers ***/
1761 assert(num_idx >= 1, "ll_get_const_gep: Need at least one index.", num_idx,
1762 ERR_Fatal);
1763 assert(type->data_type == LL_PTR,
1764 "ll_get_const_gep: "
1765 "Expected a pointer type.",
1766 type->data_type, ERR_Fatal);
1767
1768 /*** Compose pointee type string ****/
1769 pointee = (char *)malloc(3 + strlen(ptr->type_struct->sub_types[0]->str));
1770 pointee[0] = '\0';
1771
1772 /* Not every version of LLVM requires pointee type for GEP */
1773 if (ll_feature_explicit_gep_load_type(&module->ir))
1774 sprintf(pointee, "%s, ", ptr->type_struct->sub_types[0]->str);
1775
1776 /*** Put everything together ***/
1777 sprintf(p, "getelementptr(%s%s %s", pointee, ptr->type_struct->str,
1778 ptr->data);
1779 p += strlen(p);
1780
1781 va_start(ap, num_idx);
1782 while (num_idx--) {
1783 int idx = va_arg(ap, int);
1784 sprintf(p, ", i32 %d", idx);
1785 p += strlen(p);
1786 type = apply_gep_index(type, idx);
1787 }
1788 va_end(ap);
1789 sprintf(p, ")");
1790
1791 /* getelementptr produces a pointer in the same address space as
1792 * the original pointer. */
1793 type =
1794 ll_get_addrspace_type(type, ll_get_pointer_addrspace(ptr->type_struct));
1795 type = ll_get_pointer_type(type);
1796
1797 slot = intern_constant(module, type, name);
1798
1799 free(name);
1800 free(pointee);
1801 return module->constants[slot];
1802 }
1803
1804 /**
1805 \brief Get a shared \ref LL_Value representing a constant bitcast of value to
1806 type.
1807
1808 This creates a bitcast expression that may be used to initialize globals and
1809 metadata. It does not create an \c LL_BITCAST instruction.
1810 */
1811 LL_Value *
ll_get_const_bitcast(LLVMModuleRef module,LL_Value * value,LL_Type * type)1812 ll_get_const_bitcast(LLVMModuleRef module, LL_Value *value, LL_Type *type)
1813 {
1814 char *name;
1815 unsigned slot;
1816
1817 if (value->type_struct == type)
1818 return value;
1819
1820 /* Space for bitcast(<value> to <type>). */
1821 name = (char *)malloc(15 + strlen(value->type_struct->str) +
1822 strlen(value->data) + strlen(type->str));
1823 sprintf(name, "bitcast(%s %s to %s)", value->type_struct->str, value->data,
1824 type->str);
1825
1826 slot = intern_constant(module, type, name);
1827
1828 free(name);
1829 return module->constants[slot];
1830 }
1831
1832 /**
1833 \brief Get a shared \ref LL_Value representing an addrspacecast of the
1834 pointer value to type.
1835
1836 When targeting LLVM version that don't have the addrspacecast instruction,
1837 this will use ptrtoint/inttoptr instead.
1838 */
1839 LL_Value *
ll_get_const_addrspacecast(LLVMModuleRef module,LL_Value * value,LL_Type * type)1840 ll_get_const_addrspacecast(LLVMModuleRef module, LL_Value *value, LL_Type *type)
1841 {
1842 char *name;
1843 unsigned slot;
1844 unsigned fromaddr = ll_get_pointer_addrspace(value->type_struct);
1845 unsigned destaddr = ll_get_pointer_addrspace(type);
1846
1847 assert(fromaddr != destaddr,
1848 "ll_get_const_addrspacecast: "
1849 "Address spaces must differ",
1850 0, ERR_Fatal);
1851
1852 /* Space for
1853 * addrspacecast(<value> to <type>) or
1854 * inttoptr(i64 ptrtoint(<value> to i64) to <type>)
1855 */
1856 name = (char *)malloc(48 + strlen(value->type_struct->str) +
1857 strlen(value->data) + strlen(type->str));
1858
1859 if (ll_feature_use_addrspacecast(&module->ir)) {
1860 sprintf(name, "addrspacecast(%s %s to %s)", value->type_struct->str,
1861 value->data, type->str);
1862 } else {
1863 const int ptrbits = 8 * ll_type_bytes(type);
1864 sprintf(name, "inttoptr(i%d ptrtoint(%s %s to i%d) to %s)", ptrbits,
1865 value->type_struct->str, value->data, ptrbits, type->str);
1866 }
1867
1868 slot = intern_constant(module, type, name);
1869
1870 free(name);
1871 return module->constants[slot];
1872 }
1873
1874 /*
1875 * Metadata representation.
1876 */
1877
1878 /** \brief Get an LL_MDRef representing an i1 boolean value. */
1879 LL_MDRef
ll_get_md_i1(int value)1880 ll_get_md_i1(int value)
1881 {
1882 LL_MDRef mdref = LL_MDREF_INITIALIZER(MDRef_SmallInt1, value);
1883 assert(value == 0 || value == 1, "ll_get_md_i1: Invalid i1 value", value,
1884 ERR_Fatal);
1885 return mdref;
1886 }
1887
1888 /** \brief Get an LL_MDRef representing an i32 integer value. */
1889 LL_MDRef
ll_get_md_i32(LLVMModuleRef module,int value)1890 ll_get_md_i32(LLVMModuleRef module, int value)
1891 {
1892 /* Will value fit in the 29 bits available for MDRef_SmallInt32? */
1893 if ((value >> 29) == 0) {
1894 return LL_MDREF_ctor(MDRef_SmallInt32, value);
1895 }
1896 /* Create an interned constant instead. */
1897 return LL_MDREF_ctor(MDRef_Constant, intern_const_int(module, 32, value));
1898 }
1899
1900 /** \brief Get an LL_MDRef representing an i64 integer value. */
1901 LL_MDRef
ll_get_md_i64(LLVMModuleRef module,long long value)1902 ll_get_md_i64(LLVMModuleRef module, long long value)
1903 {
1904 /* Will value fit in the 29 bits available for MDRef_SmallInt64? */
1905 if ((value >> 29) == 0) {
1906 LL_MDRef mdref = LL_MDREF_INITIALIZER(MDRef_SmallInt64, (unsigned)value);
1907 return mdref;
1908 }
1909 /* Create an interned constant instead. */
1910 return LL_MDREF_ctor(MDRef_Constant, intern_const_int(module, 64, value));
1911 }
1912
1913 #define NEEDS_ESC(C) ((C) < 32 || (C) >= 127 || (C) == '\\' || (C) == '"')
1914
1915 /**
1916 \brief Get an LL_MDRef representing a raw binary string.
1917
1918 The string can contain arbitrary bytes, including nuls; appropriate escaping
1919 will be added.
1920 */
1921 LL_MDRef
ll_get_md_rawstring(LLVMModuleRef module,const void * rawstr,size_t length)1922 ll_get_md_rawstring(LLVMModuleRef module, const void *rawstr, size_t length)
1923 {
1924 const unsigned char *ustr = (const unsigned char *)rawstr;
1925 unsigned num_escapes = 0;
1926 unsigned slot;
1927 size_t i;
1928 char *str, *p;
1929 hash_data_t oldval;
1930 LL_MDRef mdref = LL_MDREF_INITIALIZER(MDRef_String, 0);
1931
1932 /* Count the number of bytes that need escaping. */
1933 for (i = 0; i < length; i++)
1934 if (NEEDS_ESC(ustr[i]))
1935 ++num_escapes;
1936
1937 /* Make a copy with escaped bytes, a !" prefix and a "\0 suffix. */
1938 p = str = (char *)malloc(length + 3 * num_escapes + 4);
1939 *p++ = '!';
1940 *p++ = '"';
1941 for (i = 0; i < length; i++) {
1942 if (NEEDS_ESC(ustr[i])) {
1943 sprintf(p, "\\%02x", ustr[i]);
1944 p += 3;
1945 } else {
1946 *p++ = ustr[i];
1947 }
1948 }
1949 *p++ = '"';
1950 *p++ = 0;
1951
1952 /* Is this a known string? */
1953 if (hashmap_lookup(module->mdstrings_map, str, &oldval)) {
1954 mdref = LL_MDREF_ctor(mdref, HKEY2INT(oldval));
1955 free(str);
1956 return mdref;
1957 }
1958
1959 /* This string hasn't been seen before. Allocate a new slot. */
1960 slot = module->mdstrings_count;
1961 if (++module->mdstrings_count > module->mdstrings_alloc) {
1962 module->mdstrings_alloc *= 2;
1963 module->mdstrings = (const char **)realloc(
1964 module->mdstrings,
1965 module->mdstrings_alloc * sizeof(module->mdstrings[0]));
1966 }
1967 module->mdstrings[slot] = str;
1968 hashmap_insert(module->mdstrings_map, str, INT2HKEY(slot));
1969
1970 mdref = LL_MDREF_ctor(mdref, slot);
1971 assert(LL_MDREF_value(mdref) == slot, "Metadata string table overflow", 0,
1972 ERR_Fatal);
1973 return mdref;
1974 }
1975
1976 /**
1977 \brief Get an LL_MDRef representing a string.
1978
1979 The string can contain arbitrary bytes, except for nul; appropriate escaping
1980 will be added.
1981 */
1982 LL_MDRef
ll_get_md_string(LLVMModuleRef module,const char * str)1983 ll_get_md_string(LLVMModuleRef module, const char *str)
1984 {
1985 return ll_get_md_rawstring(module, str, strlen(str));
1986 }
1987
1988 /**
1989 \brief Get an LL_MDRef representing an arbitrary LLVM constant value
1990 */
1991 LL_MDRef
ll_get_md_value(LLVMModuleRef module,LL_Value * value)1992 ll_get_md_value(LLVMModuleRef module, LL_Value *value)
1993 {
1994 return LL_MDREF_ctor(
1995 MDRef_Constant, intern_constant(module, value->type_struct, value->data));
1996 }
1997
1998 /* Allocate a mdnode an initialize its content array. */
1999 static LL_MDNode *
alloc_mdnode(LLVMModuleRef module,enum LL_MDClass mdclass,const LL_MDRef * elems,unsigned nelems,int is_distinct)2000 alloc_mdnode(LLVMModuleRef module, enum LL_MDClass mdclass,
2001 const LL_MDRef *elems, unsigned nelems, int is_distinct)
2002 {
2003 LL_MDNode *node =
2004 (LL_MDNode *)malloc(sizeof(LL_MDNode) + nelems * sizeof(LL_MDRef));
2005
2006 node->num_elems = nelems;
2007 node->mdclass = mdclass;
2008 node->is_distinct = is_distinct;
2009 node->is_flexible = false;
2010 memcpy(node->elem, elems, nelems * sizeof(LL_MDRef));
2011
2012 /* Check for bitfield overflow. */
2013 assert(node->num_elems == nelems, "MDNode overflow", nelems, ERR_Fatal);
2014 assert(node->mdclass == mdclass, "invalid MDNode class", mdclass, ERR_Fatal);
2015 return node;
2016 }
2017
2018 #define MIN_FLEX_SIZE 4
2019
2020 /* Allocate a flexible mdnode and initialize its contents. */
2021 static LL_MDNode *
alloc_flexible_mdnode(LLVMModuleRef module,const LL_MDRef * elems,unsigned nelems)2022 alloc_flexible_mdnode(LLVMModuleRef module, const LL_MDRef *elems,
2023 unsigned nelems)
2024 {
2025 LL_MDNode *node;
2026 unsigned size;
2027
2028 /* Flexible nodes always have an allocated capacity that is a power of two
2029 * and at least 4. */
2030 if (nelems <= MIN_FLEX_SIZE) {
2031 size = MIN_FLEX_SIZE;
2032 } else {
2033 /* Size is the smallest power of two >= nelems. */
2034 size = nelems - 1;
2035 size |= size >> 1;
2036 size |= size >> 2;
2037 size |= size >> 4;
2038 size |= size >> 8;
2039 size |= size >> 16;
2040 size += 1;
2041 }
2042
2043 node = (LL_MDNode *)malloc(sizeof(LL_MDNode) + size * sizeof(LL_MDRef));
2044 node->num_elems = nelems;
2045 node->mdclass = LL_PlainMDNode;
2046 node->is_distinct = false;
2047 node->is_flexible = true;
2048 if (nelems)
2049 memcpy(node->elem, elems, nelems * sizeof(LL_MDRef));
2050
2051 assert(node->num_elems == nelems, "MDNode overflow", nelems, ERR_Fatal);
2052
2053 return node;
2054 }
2055
2056 /**
2057 \brief Append element to flexible mdnode
2058 */
2059 static void
mdnode_append(LLVMModuleRef module,LL_MDNode ** pnode,LL_MDRef elem)2060 mdnode_append(LLVMModuleRef module, LL_MDNode **pnode, LL_MDRef elem)
2061 {
2062 unsigned nelems = (*pnode)->num_elems;
2063
2064 assert((*pnode)->is_flexible, "Not a flexible metadata node", 0, ERR_Fatal);
2065
2066 /* The allocated capacity will be a power of two >= 4.
2067 * Check if the new element will fit. */
2068 if (nelems >= MIN_FLEX_SIZE && (nelems & (nelems - 1)) == 0) {
2069 /* Node is full. Reallocate to the next power of two. */
2070 *pnode = (LL_MDNode *)realloc(*pnode, sizeof(LL_MDNode) +
2071 2 * nelems * sizeof(LL_MDRef));
2072 }
2073 (*pnode)->elem[(*pnode)->num_elems++] = elem;
2074 }
2075
2076 /**
2077 \brief Reserve a slot for an LL_MDNode
2078 \param module the LL_Module
2079 \return the reserved slot plus 1
2080 */
2081 unsigned
ll_reserve_md_node(LLVMModuleRef module)2082 ll_reserve_md_node(LLVMModuleRef module)
2083 {
2084 const unsigned slot = module->mdnodes_count;
2085
2086 if (++module->mdnodes_count > module->mdnodes_alloc) {
2087 module->mdnodes_alloc *= 2;
2088 module->mdnodes = (LL_MDNode **)realloc(
2089 module->mdnodes, module->mdnodes_alloc * sizeof(module->mdnodes[0]));
2090 }
2091 module->mdnodes[slot] = NULL;
2092 return slot + 1;
2093 }
2094
2095 /**
2096 \brief Insert an allocated mdnode into the module's list
2097 \return the new node number (the position in the list)
2098
2099 The node number is one higher than the index into mdnodes, so node !10 lives
2100 in slot 9.
2101 */
2102 INLINE static unsigned
insert_mdnode(LLVMModuleRef module,LL_MDNode * node)2103 insert_mdnode(LLVMModuleRef module, LL_MDNode *node)
2104 {
2105 const unsigned mdNum = ll_reserve_md_node(module);
2106 module->mdnodes[mdNum - 1] = node;
2107 return mdNum;
2108 }
2109
2110 void
ll_set_md_node(LLVMModuleRef module,unsigned mdNum,LL_MDNode * node)2111 ll_set_md_node(LLVMModuleRef module, unsigned mdNum, LL_MDNode *node)
2112 {
2113 assert((mdNum > 0) && (mdNum <= module->mdnodes_alloc), "slot out of bounds",
2114 mdNum, ERR_Fatal);
2115 assert(!module->mdnodes[mdNum - 1], "slot already set", mdNum, ERR_Fatal);
2116 module->mdnodes[mdNum - 1] = node;
2117 }
2118
2119 /**
2120 \brief Get an LL_MDRef representing a numbered metadata node
2121 */
2122 LL_MDRef
ll_get_md_node(LLVMModuleRef module,enum LL_MDClass mdclass,const LL_MDRef * elems,unsigned nelems)2123 ll_get_md_node(LLVMModuleRef module, enum LL_MDClass mdclass,
2124 const LL_MDRef *elems, unsigned nelems)
2125 {
2126 LL_MDNode *node = alloc_mdnode(module, mdclass, elems, nelems, false);
2127 LL_MDRef mdref = LL_MDREF_INITIALIZER(MDRef_Node, 0);
2128 hash_data_t oldval;
2129 unsigned mdnum;
2130
2131 if (hashmap_lookup(module->mdnodes_map, node, &oldval)) {
2132 /* This is a duplicate, free the one we just allocated. */
2133 free(node);
2134 mdnum = HKEY2INT(oldval);
2135 } else {
2136 mdnum = insert_mdnode(module, node);
2137 hashmap_insert(module->mdnodes_map, node, INT2HKEY(mdnum));
2138 }
2139
2140 mdref = LL_MDREF_ctor(mdref, mdnum);
2141 return mdref;
2142 }
2143
2144 /**
2145 \brief Add \p sptr → \p mdnode to global debug map
2146 \param module The module containing the map
2147 \param sptr The key to be added
2148 \param mdnode The value to be added
2149
2150 If the key, \p sptr, is already in the map, the map is unaltered.
2151 */
2152 void
ll_add_global_debug(LLVMModuleRef module,int sptr,LL_MDRef mdnode)2153 ll_add_global_debug(LLVMModuleRef module, int sptr, LL_MDRef mdnode)
2154 {
2155 hash_data_t oldval;
2156 const hash_key_t key = INT2HKEY(sptr);
2157 const hash_data_t value = INT2HKEY(mdnode);
2158
2159 if (!hashmap_lookup(module->global_debug_map, key, &oldval))
2160 hashmap_insert(module->global_debug_map, key, value);
2161 }
2162
2163 LL_MDRef
ll_get_global_debug(LLVMModuleRef module,int sptr)2164 ll_get_global_debug(LLVMModuleRef module, int sptr)
2165 {
2166 hash_data_t oldval;
2167 const hash_key_t key = INT2HKEY(sptr);
2168
2169 if (hashmap_lookup(module->global_debug_map, key, &oldval))
2170 return HKEY2INT(oldval);
2171 return LL_MDREF_ctor(0, 0);
2172 }
2173
2174 /**
2175 \brief Create a distinct metadata node which will not be uniqued with other
2176 identical metadata nodes.
2177 */
2178 LL_MDRef
ll_create_distinct_md_node(LLVMModuleRef module,enum LL_MDClass mdclass,const LL_MDRef * elems,unsigned nelems)2179 ll_create_distinct_md_node(LLVMModuleRef module, enum LL_MDClass mdclass,
2180 const LL_MDRef *elems, unsigned nelems)
2181 {
2182 LL_MDNode *node = alloc_mdnode(module, mdclass, elems, nelems, true);
2183 LL_MDRef md = LL_MDREF_INITIALIZER(MDRef_Node, insert_mdnode(module, node));
2184 return md;
2185 }
2186
2187 /**
2188 \brief Create a distinct metadata node that can have elements appended
2189 */
2190 LL_MDRef
ll_create_flexible_md_node(LLVMModuleRef module)2191 ll_create_flexible_md_node(LLVMModuleRef module)
2192 {
2193 LL_MDNode *node = alloc_flexible_mdnode(module, NULL, 0);
2194 LL_MDRef md = LL_MDREF_INITIALIZER(MDRef_Node, insert_mdnode(module, node));
2195 return md;
2196 }
2197
2198 /**
2199 \brief Append an element to end of a flexible metadata node
2200
2201 The node must have been created by ll_create_flexible_md_node()
2202 */
2203 void
ll_extend_md_node(LLVMModuleRef module,LL_MDRef flexnode,LL_MDRef elem)2204 ll_extend_md_node(LLVMModuleRef module, LL_MDRef flexnode, LL_MDRef elem)
2205 {
2206 unsigned slot = LL_MDREF_value(flexnode) - 1;
2207 assert(LL_MDREF_kind(flexnode) == MDRef_Node && slot < module->mdnodes_count,
2208 "Bad flexnode reference", 0, ERR_Fatal);
2209 mdnode_append(module, &module->mdnodes[slot], elem);
2210 }
2211
2212 /**
2213 \brief Update one of the elements in a metadata node
2214
2215 The node being updated must be either distinct or flexible, it is not allowed
2216 to update nodes that may be shared.
2217 */
2218 void
ll_update_md_node(LLVMModuleRef module,LL_MDRef node_to_update,unsigned elem_index,LL_MDRef elem)2219 ll_update_md_node(LLVMModuleRef module, LL_MDRef node_to_update,
2220 unsigned elem_index, LL_MDRef elem)
2221 {
2222 unsigned slot = LL_MDREF_value(node_to_update) - 1;
2223 LL_MDNode *node;
2224 assert(LL_MDREF_kind(node_to_update) == MDRef_Node &&
2225 slot < module->mdnodes_count,
2226 "ll_update_md_node: Bad metadata node reference", 0, ERR_Fatal);
2227 node = module->mdnodes[slot];
2228 assert(node->is_distinct || node->is_flexible,
2229 "ll_update_md_node: Cannot update potentially shared node", 0,
2230 ERR_Fatal);
2231 assert(elem_index < node->num_elems,
2232 "ll_update_md_node: Element index out of range", elem_index,
2233 ERR_Fatal);
2234 node->elem[elem_index] = elem;
2235 }
2236
2237 /**
2238 \brief Add a named metadata node to the module
2239 */
2240 void
ll_set_named_md_node(LLVMModuleRef module,enum LL_MDName name,const LL_MDRef * elems,unsigned nelems)2241 ll_set_named_md_node(LLVMModuleRef module, enum LL_MDName name,
2242 const LL_MDRef *elems, unsigned nelems)
2243 {
2244 assert(name < MD_NUM_NAMES, "Invalid metadata name", name, ERR_Fatal);
2245 free(module->named_mdnodes[name]);
2246 module->named_mdnodes[name] = alloc_flexible_mdnode(module, elems, nelems);
2247 }
2248
2249 /**
2250 \brief Append an element to a named metadata node
2251
2252 Creates the named metadata node if needed.
2253 */
2254 void
ll_extend_named_md_node(LLVMModuleRef module,enum LL_MDName name,LL_MDRef elem)2255 ll_extend_named_md_node(LLVMModuleRef module, enum LL_MDName name,
2256 LL_MDRef elem)
2257 {
2258 assert(name < MD_NUM_NAMES, "Invalid metadata name", name, ERR_Fatal);
2259 if (module->named_mdnodes[name])
2260 mdnode_append(module, &module->named_mdnodes[name], elem);
2261 else
2262 module->named_mdnodes[name] = alloc_flexible_mdnode(module, &elem, 1);
2263 }
2264
2265 /**
2266 \brief Append a pointer to the \c \@llvm.used global
2267
2268 Pointer bitcasts and/or addrspacecasts will be added as needed.
2269 */
2270 void
ll_append_llvm_used(LLVMModuleRef module,LL_Value * ptr)2271 ll_append_llvm_used(LLVMModuleRef module, LL_Value *ptr)
2272 {
2273 unsigned addrspace = ll_get_pointer_addrspace(ptr->type_struct);
2274 LL_Type *i8ptr = ll_get_pointer_type(ll_create_int_type(module, 8));
2275
2276 if (addrspace) {
2277 /* Use an addrspacecast to get an addrspace 0 pointer. */
2278 ptr = ll_get_const_addrspacecast(module, ptr, i8ptr);
2279 } else if (ptr->type_struct != i8ptr) {
2280 /* Use a bitcast to convert pointer types withon addrspace 0. */
2281 ptr = ll_get_const_bitcast(module, ptr, i8ptr);
2282 }
2283
2284 /* Weed out duplicate entries, but don't bother with a full linear search.
2285 * Just catch consecutive duplicates. */
2286 if (module->num_llvm_used > 0 &&
2287 module->llvm_used.values[module->num_llvm_used - 1] == ptr)
2288 return;
2289
2290 ll_create_sym(&module->llvm_used, module->num_llvm_used++, ptr);
2291 }
2292
2293 /*
2294 * Global objects.
2295 *
2296 * The LL_Module maintains a linked list of LL_Objects with global scope. These
2297 * objects can be global variables, global constants, and aliases.
2298 */
2299
2300 static void
append_global(LLVMModuleRef module,LL_Object * object)2301 append_global(LLVMModuleRef module, LL_Object *object)
2302 {
2303 object->next = NULL;
2304 if (module->last_global)
2305 module->last_global->next = object;
2306 else
2307 module->first_global = object;
2308 module->last_global = object;
2309 }
2310
2311 static LL_Object *
create_global(LLVMModuleRef module,enum LL_ObjectKind kind,LL_Type * type,int addrspace,const char * format,va_list args)2312 create_global(LLVMModuleRef module, enum LL_ObjectKind kind, LL_Type *type,
2313 int addrspace, const char *format, va_list args)
2314 {
2315 LL_Object *object =
2316 (LL_Object *)ll_manage_calloc(module, 1, sizeof(LL_Object));
2317
2318 object->kind = kind;
2319 object->type = type;
2320 object->linkage = LL_EXTERNAL_LINKAGE;
2321
2322 object->address.type_struct =
2323 ll_get_pointer_type(ll_get_addrspace_type(type, addrspace));
2324 object->address.data =
2325 unique_name(module->used_global_names, '@', format, args);
2326
2327 append_global(module, object);
2328 return object;
2329 }
2330
2331 /**
2332 \brief Create a new global alias.
2333
2334 The name of the alias object is produced by the sprintf-link format string
2335 and arguments. If the generated name clashes with an existing global object
2336 in the module, a unique name will be generated.
2337
2338 The aliasee must be the address of another global object or a constant
2339 expression computing an address inside a global object.
2340 */
2341 LL_Object *
ll_create_global_alias(LL_Value * aliasee_ptr,const char * format,...)2342 ll_create_global_alias(LL_Value *aliasee_ptr, const char *format, ...)
2343 {
2344 int addrspace = ll_get_pointer_addrspace(aliasee_ptr->type_struct);
2345 LLVMModuleRef module = aliasee_ptr->type_struct->module;
2346 va_list ap;
2347 LL_Object *object;
2348
2349 va_start(ap, format);
2350 object =
2351 create_global(module, LLObj_Alias, aliasee_ptr->type_struct->sub_types[0],
2352 addrspace, format, ap);
2353 va_end(ap);
2354
2355 object->init_style = LLInit_ConstExpr;
2356 object->init_data.const_expr = aliasee_ptr;
2357
2358 return object;
2359 }
2360
2361 /**
2362 \brief Create a new unique name for a local value in function.
2363
2364 The name is based on the provided printf-style format string and arguments.
2365 The leading \c \% character should not be part for the format string.
2366
2367 Returns a pointer to a unique local name in function, including the leading
2368 \c \%. The memory for the returned name is managed by the function.
2369 */
2370 const char *
ll_create_local_name(LL_Function * function,const char * format,...)2371 ll_create_local_name(LL_Function *function, const char *format, ...)
2372 {
2373 va_list ap;
2374 const char *name;
2375
2376 if (!function->used_local_names)
2377 function->used_local_names = hashset_alloc(hash_functions_strings);
2378
2379 va_start(ap, format);
2380 name = unique_name(function->used_local_names, '%', format, ap);
2381 va_end(ap);
2382
2383 return name;
2384 }
2385
2386 /**
2387 \brief Create a new local object in function.
2388
2389 The object will be allocated by an LLVM alloca instruction in the entry
2390 block. It will not be initialized.
2391
2392 The name of the local object is produced by the sprintf-link format string
2393 and arguments. If the generated name clashes with an existing local value in
2394 the function, a unique name will be generated.
2395 */
2396 LL_Object *
ll_create_local_object(LL_Function * function,LL_Type * type,unsigned align_bytes,const char * format,...)2397 ll_create_local_object(LL_Function *function, LL_Type *type,
2398 unsigned align_bytes, const char *format, ...)
2399 {
2400 LL_Object *object =
2401 (LL_Object *)ll_manage_calloc(type->module, 1, sizeof(LL_Object));
2402 va_list ap;
2403
2404 if (!function->used_local_names)
2405 function->used_local_names = hashset_alloc(hash_functions_strings);
2406
2407 object->kind = LLObj_Local;
2408 object->type = type;
2409 object->linkage = LL_INTERNAL_LINKAGE;
2410 object->align_bytes = align_bytes;
2411 object->address.type_struct = ll_get_pointer_type(type);
2412 va_start(ap, format);
2413 object->address.data =
2414 unique_name(function->used_local_names, '%', format, ap);
2415 va_end(ap);
2416
2417 /* Append object to the linked list of locals in function. */
2418 if (function->last_local) {
2419 function->last_local->next = object;
2420 function->last_local = object;
2421 } else {
2422 function->first_local = function->last_local = object;
2423 }
2424
2425 return object;
2426 }
2427
2428 /* Global (covers all modules) */
2429 static hashmap_t _ll_proto_map;
2430
2431 /* Global (head of proto list):
2432 *
2433 * We maintain a list to ease iteration and to also produce
2434 * a consistent debugging output. A hash table might reshuffle the contents
2435 * and make debugging more difficult.
2436 */
2437 static LL_FnProto *_ll_proto_head;
2438
2439 /**
2440 \brief Find the function (interface) name to be used as a key
2441 \param func_sptr An sptr to a function symbol
2442
2443 Given a function \p sptr return the function name (or interface name) to be
2444 used as a key for the rest of the \c fnname arguments for the
2445 <tt>ll_proto_*</tt> API.
2446 */
2447 const char *
ll_proto_key(SPTR func_sptr)2448 ll_proto_key(SPTR func_sptr)
2449 {
2450 const char *ifacenm;
2451 const char *nm = NULL;
2452
2453 /* This is disabled for now, we plan on enabling this soon and cleaning up the
2454 * macros below.
2455 */
2456 #if defined(TARGET_LLVM) && !defined(MATTD)
2457 return get_llvm_name(func_sptr);
2458 #endif /* TARGET_LLVM && !MATTD */
2459
2460 #ifdef MATTD
2461 /* Fortran must check for interface names, C/C++ is straight forward) */
2462 ifacenm = get_llvm_ifacenm(func_sptr);
2463 if (find_ag(ifacenm))
2464 nm = ifacenm;
2465 else
2466 nm = get_llvm_name(func_sptr);
2467
2468 assert(nm, "ll_proto_key: No function name discovered", func_sptr, 4);
2469 return nm;
2470 #endif /* MATTD */
2471 return NULL;
2472 }
2473
2474 /**
2475 \brief Initialization
2476
2477 Should only ever be called once for the entire compilation
2478 */
2479 void
ll_proto_init(void)2480 ll_proto_init(void)
2481 {
2482 /* TODO: Remove language guard once cg_llvm_init and end are untangled. */
2483 if (_ll_proto_map)
2484 return;
2485 if (!(_ll_proto_map = hashmap_alloc(hash_functions_strings)))
2486 interr("ll_proto_init: Could not allocate hashmap", 0, ERR_Fatal);
2487 }
2488
2489 /* Set the name of the function. */
2490 static void
ll_proto_update_name(LL_FnProto * proto,const char * fnname)2491 ll_proto_update_name(LL_FnProto *proto, const char *fnname)
2492 {
2493 free(proto->fn_name);
2494 proto->fn_name = strdup(fnname);
2495 }
2496
2497 /**
2498 \brief Uniquely instantiate a new proto to the global map
2499 */
2500 LL_FnProto *
ll_proto_add(const char * fnname,struct LL_ABI_Info_ * abi)2501 ll_proto_add(const char *fnname, struct LL_ABI_Info_ *abi)
2502 {
2503 LL_FnProto *proto;
2504 const char *key;
2505
2506 if (hashmap_lookup(_ll_proto_map, fnname, (hash_data_t *)&proto))
2507 return proto;
2508
2509 proto = (LL_FnProto *)calloc(1, sizeof(LL_FnProto));
2510 if (!proto)
2511 interr("ll_proto_add: Could not allocate proto instance", 0, ERR_Fatal);
2512
2513 proto->abi = abi;
2514
2515 /* Keys are stored, we must remember the name string since
2516 * fortran will reset the name per module compilation.
2517 * Do not deallocate the keys unless the hashmap is deallocated.
2518 */
2519 fnname = (char *)strdup(fnname);
2520 key = (char *)strdup(fnname);
2521 proto->fn_name = (char *)fnname;
2522 hashmap_insert(_ll_proto_map, key, (hash_data_t)proto);
2523
2524 if (!_ll_proto_head) {
2525 _ll_proto_head = proto;
2526 } else {
2527 proto->next = _ll_proto_head;
2528 _ll_proto_head = proto;
2529 }
2530
2531 return proto;
2532 }
2533
2534 /**
2535 \brief Convienience routine to ll_proto_add
2536
2537 Also sets the proper name to use when defining/declaring the function.
2538 */
2539 LL_FnProto *
ll_proto_add_sptr(SPTR func_sptr,struct LL_ABI_Info_ * abi)2540 ll_proto_add_sptr(SPTR func_sptr, struct LL_ABI_Info_ *abi)
2541 {
2542 LL_FnProto *proto = ll_proto_add(ll_proto_key(func_sptr), abi);
2543 ll_proto_update_name(proto, get_llvm_name(func_sptr));
2544 return proto;
2545 }
2546
2547 void
ll_proto_set_abi(const char * fnname,struct LL_ABI_Info_ * abi)2548 ll_proto_set_abi(const char *fnname, struct LL_ABI_Info_ *abi)
2549 {
2550 LL_FnProto *proto = NULL;
2551 /* Fortran might not yet have added this to the hash */
2552 if (hashmap_lookup(_ll_proto_map, fnname, (hash_data_t *)&proto)) {
2553 proto->abi = abi;
2554 }
2555 }
2556
2557 struct LL_ABI_Info_ *
ll_proto_get_abi(const char * fnname)2558 ll_proto_get_abi(const char *fnname)
2559 {
2560 LL_FnProto *proto = NULL;
2561 hashmap_lookup(_ll_proto_map, fnname, (hash_data_t *)&proto);
2562 return proto ? proto->abi : NULL;
2563 }
2564
2565 /**
2566 \brief Set the flag "function has body"
2567 \param fnname a key (function name)
2568 \param has_defined true iff \p fnname is a definition
2569
2570 Sets the bit representing the information that the body of the function
2571 represented by \p fnname is defined.
2572 */
2573 void
ll_proto_set_defined_body(const char * fnname,bool has_defined)2574 ll_proto_set_defined_body(const char *fnname, bool has_defined)
2575 {
2576 LL_FnProto *proto = NULL;
2577
2578 if (!hashmap_lookup(_ll_proto_map, fnname, (hash_data_t *)&proto))
2579 interr("ll_proto_set_defined_body: Entry not found", 0, ERR_Fatal);
2580
2581 proto->has_defined_body = has_defined;
2582 }
2583
2584 bool
ll_proto_has_defined_body(const char * fnname)2585 ll_proto_has_defined_body(const char *fnname)
2586 {
2587 LL_FnProto *proto = NULL;
2588
2589 if (!hashmap_lookup(_ll_proto_map, fnname, (hash_data_t *)&proto))
2590 interr("ll_proto_has_defined_body: Entry not found", 0, ERR_Fatal);
2591
2592 return proto->has_defined_body;
2593 }
2594
2595 /**
2596 \brief Set the flag "function is weak"
2597 \param fnname a key (function name)
2598 \param is_weak true iff \p fnname is weak
2599
2600 Sets the bit representing the information that the function
2601 represented by \p fnname is weak.
2602 */
2603 void
ll_proto_set_weak(const char * fnname,bool is_weak)2604 ll_proto_set_weak(const char *fnname, bool is_weak)
2605 {
2606 LL_FnProto *proto = NULL;
2607
2608 if (!hashmap_lookup(_ll_proto_map, fnname, (hash_data_t *)&proto))
2609 interr("ll_proto_set_weak: Entry not found", 0, ERR_Fatal);
2610
2611 proto->is_weak = is_weak;
2612 }
2613
2614 bool
ll_proto_is_weak(const char * fnname)2615 ll_proto_is_weak(const char *fnname)
2616 {
2617 LL_FnProto *proto = NULL;
2618
2619 if (!hashmap_lookup(_ll_proto_map, fnname, (hash_data_t *)&proto))
2620 interr("ll_proto_is_weak: Entry not found", 0, ERR_Fatal);
2621
2622 return proto->is_weak;
2623 }
2624
2625 /**
2626 \brief Set compiler generated intrinsic string
2627 \param fnname The key to lookup the prototype
2628 \param intrinsic_decl_str The LLVM declare statement text
2629
2630 The prototype of \p fnname must already have been added to the map.
2631 */
2632 void
ll_proto_set_intrinsic(const char * fnname,const char * intrinsic_decl_str)2633 ll_proto_set_intrinsic(const char *fnname, const char *intrinsic_decl_str)
2634 {
2635 LL_FnProto *proto = NULL;
2636
2637 if (!hashmap_lookup(_ll_proto_map, fnname, (hash_data_t *)&proto))
2638 interr("ll_proto_set_intrinsic: Entry not found", 0, ERR_Fatal);
2639
2640 proto->intrinsic_decl_str = strdup(intrinsic_decl_str);
2641 }
2642
2643 void
ll_proto_iterate(LL_FnProto_Handler callback)2644 ll_proto_iterate(LL_FnProto_Handler callback)
2645 {
2646 LL_FnProto *proto;
2647 for (proto = _ll_proto_head; proto; proto = proto->next)
2648 callback(proto);
2649 }
2650
2651 static void
dump_proto(int counter,const LL_FnProto * proto)2652 dump_proto(int counter, const LL_FnProto *proto)
2653 {
2654 FILE *fil = gbl.dbgfil ? gbl.dbgfil : stdout;
2655 fprintf(fil,
2656 "%d) %s: abi(%p) has_defined_body(%d) is_weak(%d) "
2657 "intrinsic(%s)\n",
2658 counter, proto->fn_name, proto->abi, proto->has_defined_body,
2659 proto->is_weak, proto->intrinsic_decl_str);
2660 }
2661
2662 /**
2663 \brief Debugging: Dump the contents of the map
2664 */
2665 void
ll_proto_dump(void)2666 ll_proto_dump(void)
2667 {
2668 LL_FnProto *proto;
2669 int counter = 0;
2670 FILE *fil = gbl.dbgfil ? gbl.dbgfil : stdout;
2671
2672 fprintf(fil, "** Function Name - to - Prototype Map **\n");
2673 for (proto = _ll_proto_head; proto; proto = proto->next)
2674 dump_proto(++counter, proto);
2675 fprintf(fil, "****************************************\n");
2676 }
2677
2678 /**
2679 \brief Add \p module_name → \p mdnode to module debug map
2680 \param module The module containing the map
2681 \param module_name The key to be added
2682 \param mdnode The value to be added
2683
2684 If the key, \p module_name, is already in the map, the map is unaltered.
2685 */
2686 void
ll_add_module_debug(hashmap_t map,const char * module_name,LL_MDRef mdnode)2687 ll_add_module_debug(hashmap_t map, const char *module_name, LL_MDRef mdnode)
2688 {
2689 hash_data_t oldval;
2690 if (!hashmap_lookup(map, module_name, &oldval))
2691 hashmap_insert(map, module_name, INT2HKEY(mdnode));
2692 }
2693
2694 LL_MDRef
ll_get_module_debug(hashmap_t map,const char * module_name)2695 ll_get_module_debug(hashmap_t map, const char *module_name)
2696 {
2697 hash_data_t oldval;
2698 if (hashmap_lookup(map, module_name, &oldval))
2699 return HKEY2INT(oldval);
2700 return LL_MDREF_ctor(0, 0);
2701 }
2702
2703 #ifdef OMP_OFFLOAD_LLVM
2704 void
ll_set_device_function_arguments(LLVMModuleRef module,struct LL_Function_ * function,struct LL_ABI_Info_ * abi)2705 ll_set_device_function_arguments(LLVMModuleRef module,
2706 struct LL_Function_ *function,
2707 struct LL_ABI_Info_ *abi)
2708 {
2709 int i;
2710 for (i = 1; i <= abi->nargs; i++) {
2711 LL_ABI_ArgInfo *arg = &abi->arg[i];
2712 LL_Value *argument = (LL_Value *)malloc(sizeof(LL_Value));
2713 argument->type_struct = arg->type;
2714 ll_set_function_argument(function, (i - 1), argument);
2715 }
2716 }
2717 #endif
2718