GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 98.2% 437 / 6 / 451
Functions: 96.2% 50 / 0 / 52
Branches: 63.0% 481 / 20 / 784

src/irgenerator/IRGenerator.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "IRGenerator.h"
4
5 #include <SourceFile.h>
6 #include <driver/Driver.h>
7 #include <global/GlobalResourceManager.h>
8 #include <model/Function.h>
9 #include <symboltablebuilder/SymbolTableBuilder.h>
10 #include <typechecker/FunctionManager.h>
11
12 #include <llvm/IR/Module.h>
13 #include <llvm/IR/Verifier.h>
14
15 namespace spice::compiler {
16
17 const std::string PRODUCER_STRING = "spice version " + std::string(SPICE_VERSION) + " (https://github.com/spicelang/spice)";
18
19 2562 IRGenerator::IRGenerator(GlobalResourceManager &resourceManager, SourceFile *sourceFile)
20 2562 : CompilerPass(resourceManager, sourceFile), context(cliOptions.useLTO ? resourceManager.ltoContext : sourceFile->context),
21
1/2
✓ Branch 8 → 9 taken 2562 times.
✗ Branch 8 → 78 not taken.
2562 builder(sourceFile->builder), module(sourceFile->llvmModule.get()), conversionManager(sourceFile, this),
22
6/10
✓ Branch 4 → 5 taken 4 times.
✓ Branch 4 → 6 taken 2558 times.
✓ Branch 9 → 10 taken 2562 times.
✗ Branch 9 → 78 not taken.
✓ Branch 10 → 11 taken 2562 times.
✗ Branch 10 → 78 not taken.
✓ Branch 11 → 12 taken 2562 times.
✗ Branch 11 → 76 not taken.
✓ Branch 15 → 16 taken 2562 times.
✗ Branch 15 → 72 not taken.
5124 stdFunctionManager(sourceFile, resourceManager, module) {
23 // Attach information to the module
24
1/2
✓ Branch 19 → 20 taken 2562 times.
✗ Branch 19 → 51 not taken.
2562 module->setTargetTriple(cliOptions.targetTriple);
25
2/4
✓ Branch 23 → 24 taken 2562 times.
✗ Branch 23 → 54 not taken.
✓ Branch 24 → 25 taken 2562 times.
✗ Branch 24 → 52 not taken.
2562 module->setDataLayout(sourceFile->targetMachine->createDataLayout());
26
2/2
✓ Branch 26 → 27 taken 1 time.
✓ Branch 26 → 29 taken 2561 times.
2562 if (cliOptions.outputContainer == OutputContainer::SHARED_LIBRARY) {
27
1/2
✓ Branch 27 → 28 taken 1 time.
✗ Branch 27 → 64 not taken.
1 module->setPICLevel(llvm::PICLevel::SmallPIC);
28
1/2
✓ Branch 28 → 31 taken 1 time.
✗ Branch 28 → 64 not taken.
1 module->setPIELevel(llvm::PIELevel::Default);
29 } else {
30
1/2
✓ Branch 29 → 30 taken 2561 times.
✗ Branch 29 → 64 not taken.
2561 module->setPICLevel(llvm::PICLevel::BigPIC);
31
1/2
✓ Branch 30 → 31 taken 2561 times.
✗ Branch 30 → 64 not taken.
2561 module->setPIELevel(llvm::PIELevel::Large);
32 }
33
1/2
✓ Branch 31 → 32 taken 2562 times.
✗ Branch 31 → 64 not taken.
2562 module->setUwtable(llvm::UWTableKind::Default);
34
1/2
✓ Branch 32 → 33 taken 2562 times.
✗ Branch 32 → 64 not taken.
2562 module->setFramePointer(llvm::FramePointerKind::All);
35
36 // Add module identifier metadata
37
2/4
✓ Branch 33 → 34 taken 2562 times.
✗ Branch 33 → 55 not taken.
✓ Branch 34 → 35 taken 2562 times.
✗ Branch 34 → 55 not taken.
2562 llvm::NamedMDNode *identifierMetadata = module->getOrInsertNamedMetadata("llvm.ident");
38
3/6
✓ Branch 36 → 37 taken 2562 times.
✗ Branch 36 → 56 not taken.
✓ Branch 38 → 39 taken 2562 times.
✗ Branch 38 → 56 not taken.
✓ Branch 39 → 40 taken 2562 times.
✗ Branch 39 → 56 not taken.
2562 identifierMetadata->addOperand(llvm::MDNode::get(context, llvm::MDString::get(context, PRODUCER_STRING)));
39
40 // Initialize common LLVM types
41
4/8
✓ Branch 40 → 41 taken 2562 times.
✗ Branch 40 → 59 not taken.
✓ Branch 41 → 42 taken 2562 times.
✗ Branch 41 → 59 not taken.
✓ Branch 42 → 43 taken 2562 times.
✗ Branch 42 → 59 not taken.
✓ Branch 44 → 45 taken 2562 times.
✗ Branch 44 → 59 not taken.
2562 llvmTypes.lambdaFatPtrType = llvm::StructType::get(context, {builder.getPtrTy(), builder.getPtrTy(), builder.getInt64Ty()});
42
43 // Initialize debug info generator
44
2/2
✓ Branch 45 → 46 taken 59 times.
✓ Branch 45 → 50 taken 2503 times.
2562 if (cliOptions.instrumentation.generateDebugInfo)
45
2/4
✓ Branch 46 → 47 taken 59 times.
✗ Branch 46 → 63 not taken.
✓ Branch 47 → 48 taken 59 times.
✗ Branch 47 → 61 not taken.
59 diGenerator.initialize(sourceFile->fileName, sourceFile->fileDir);
46 2562 }
47
48 2562 std::any IRGenerator::visitEntry(const EntryNode *node) {
49 // Generate IR
50
1/2
✓ Branch 2 → 3 taken 2562 times.
✗ Branch 2 → 28 not taken.
2562 visitChildren(node);
51
52 // Generate test main if required
53
4/4
✓ Branch 4 → 5 taken 457 times.
✓ Branch 4 → 7 taken 2105 times.
✓ Branch 5 → 6 taken 4 times.
✓ Branch 5 → 7 taken 453 times.
2562 if (sourceFile->isMainFile && cliOptions.generateTestMain)
54 4 generateTestMain();
55
56 // Execute deferred VTable initializations
57
2/2
✓ Branch 21 → 9 taken 2277 times.
✓ Branch 21 → 22 taken 2562 times.
7401 for (DeferredLogic &deferredVTableInit : deferredVTableInitializations)
58
1/2
✓ Branch 11 → 12 taken 2277 times.
✗ Branch 11 → 29 not taken.
2277 deferredVTableInit.execute();
59
60 // Finalize debug info generator
61 2562 diGenerator.finalize();
62
63 // Verify module
64 2562 verifyModule(node->codeLoc);
65
66
1/2
✓ Branch 24 → 25 taken 2562 times.
✗ Branch 24 → 30 not taken.
5124 return nullptr;
67 }
68
69 141406 llvm::AllocaInst *IRGenerator::insertAlloca(llvm::Type *llvmType, const std::string &varName) {
70
2/2
✓ Branch 2 → 3 taken 100152 times.
✓ Branch 2 → 8 taken 41254 times.
141406 if (allocaInsertInst != nullptr) { // If there is already an alloca inst, insert right after that
71
2/4
✓ Branch 3 → 4 taken 100152 times.
✗ Branch 3 → 19 not taken.
✓ Branch 4 → 5 taken 100152 times.
✗ Branch 4 → 19 not taken.
100152 llvm::AllocaInst *allocaInst = builder.CreateAlloca(llvmType, nullptr, varName);
72 100152 allocaInst->dropLocation(); // Part of prologue
73 100152 allocaInst->moveAfter(allocaInsertInst);
74 100152 allocaInsertInst = allocaInst;
75 } else { // This is the first alloca inst in the current function -> insert at the entry block
76 // Save current basic block and move insert cursor to entry block of the current function
77 41254 llvm::BasicBlock *currentBlock = builder.GetInsertBlock();
78 41254 builder.SetInsertPoint(allocaInsertBlock, allocaInsertBlock->begin());
79
80 // Allocate the size of the given LLVM type
81
2/4
✓ Branch 11 → 12 taken 41254 times.
✗ Branch 11 → 20 not taken.
✓ Branch 12 → 13 taken 41254 times.
✗ Branch 12 → 20 not taken.
41254 allocaInsertInst = builder.CreateAlloca(llvmType, nullptr, varName);
82 41254 allocaInsertInst->dropLocation(); // Part of prologue
83
84 // Restore old basic block
85 41254 builder.SetInsertPoint(currentBlock);
86 }
87
88 // Insert lifetime start marker
89
2/2
✓ Branch 15 → 16 taken 1838 times.
✓ Branch 15 → 17 taken 139568 times.
141406 if (cliOptions.useLifetimeMarkers)
90 1838 builder.CreateLifetimeStart(allocaInsertInst);
91
92 141406 return allocaInsertInst;
93 }
94
95 118045 llvm::AllocaInst *IRGenerator::insertAlloca(const QualType &qualType, const std::string &varName) {
96 118045 llvm::Type *llvmType = qualType.toLLVMType(sourceFile);
97 118045 llvm::AllocaInst *alloca = insertAlloca(llvmType, varName);
98
99 // Insert type metadata
100
2/2
✓ Branch 4 → 5 taken 9 times.
✓ Branch 4 → 6 taken 118036 times.
118045 if (cliOptions.useTBAAMetadata)
101 9 mdGenerator.generateTypeMetadata(allocaInsertInst, qualType);
102
103 118045 return alloca;
104 }
105
106 280184 llvm::LoadInst *IRGenerator::insertLoad(llvm::Type *llvmType, llvm::Value *ptr, bool isVolatile,
107 const std::string &varName) const {
108
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 280184 times.
280184 assert(ptr->getType()->isPointerTy());
109
2/4
✓ Branch 6 → 7 taken 280184 times.
✗ Branch 6 → 11 not taken.
✓ Branch 7 → 8 taken 280184 times.
✗ Branch 7 → 11 not taken.
280184 return builder.CreateLoad(llvmType, ptr, isVolatile, varName);
110 }
111
112 240490 llvm::LoadInst *IRGenerator::insertLoad(const QualType &qualType, llvm::Value *ptr, bool isVolatile, const std::string &varName) {
113 240490 llvm::Type *llvmType = qualType.toLLVMType(sourceFile);
114 240490 llvm::LoadInst *load = insertLoad(llvmType, ptr, isVolatile, varName);
115
2/2
✓ Branch 4 → 5 taken 6 times.
✓ Branch 4 → 6 taken 240484 times.
240490 if (cliOptions.useTBAAMetadata)
116 6 mdGenerator.generateTBAAMetadata(load, qualType);
117 240490 return load;
118 }
119
120 146125 llvm::StoreInst *IRGenerator::insertStore(llvm::Value *val, llvm::Value *ptr, bool isVolatile) const {
121
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 146125 times.
146125 assert(ptr->getType()->isPointerTy());
122 146125 return builder.CreateStore(val, ptr, isVolatile);
123 }
124
125 43513 void IRGenerator::insertStore(llvm::Value *val, llvm::Value *ptr, const QualType &qualType, bool isVolatile) {
126 43513 llvm::StoreInst *store = insertStore(val, ptr, isVolatile);
127
2/2
✓ Branch 3 → 4 taken 9 times.
✓ Branch 3 → 5 taken 43504 times.
43513 if (cliOptions.useTBAAMetadata)
128 9 mdGenerator.generateTBAAMetadata(store, qualType);
129 43513 }
130
131 80132 llvm::Value *IRGenerator::insertInBoundsGEP(llvm::Type *type, llvm::Value *basePtr, llvm::ArrayRef<llvm::Value *> indices,
132 const std::string &varName) const {
133
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 80132 times.
80132 assert(basePtr->getType()->isPointerTy());
134
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 80132 times.
80132 assert(!indices.empty());
135
4/6
✓ Branch 4 → 5 taken 79283 times.
✓ Branch 4 → 7 taken 73005 times.
✓ Branch 6 → 7 taken 79283 times.
✗ Branch 6 → 8 not taken.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 80132 times.
232420 assert(std::ranges::all_of(indices, [](const llvm::Value *index) {
136 const llvm::Type *indexType = index->getType();
137 return indexType->isIntegerTy(32) || indexType->isIntegerTy(64);
138 }));
139
140 // Insert GEP
141
2/4
✓ Branch 12 → 13 taken 80132 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 80132 times.
✗ Branch 13 → 17 not taken.
80132 return builder.CreateInBoundsGEP(type, basePtr, indices, varName);
142 }
143
144 27786 llvm::Value *IRGenerator::insertStructGEP(llvm::Type *type, llvm::Value *basePtr, unsigned int index,
145 const std::string &varName) const {
146
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 27786 times.
27786 assert(basePtr->getType()->isPointerTy());
147
148 // If we use index 0 we can use the base pointer directly
149
2/2
✓ Branch 6 → 7 taken 9171 times.
✓ Branch 6 → 8 taken 18615 times.
27786 if (index == 0)
150 9171 return basePtr;
151
152 // Insert GEP
153
2/4
✓ Branch 8 → 9 taken 18615 times.
✗ Branch 8 → 13 not taken.
✓ Branch 9 → 10 taken 18615 times.
✗ Branch 9 → 13 not taken.
18615 return builder.CreateStructGEP(type, basePtr, index, varName);
154 }
155
156 158829 llvm::Value *IRGenerator::resolveValue(const ExprNode *node) {
157 // Visit the given AST node
158
2/4
✓ Branch 2 → 3 taken 158829 times.
✗ Branch 2 → 11 not taken.
✓ Branch 3 → 4 taken 158829 times.
✗ Branch 3 → 9 not taken.
158829 auto exprResult = any_cast<LLVMExprResult>(visit(node));
159
1/2
✓ Branch 5 → 6 taken 158829 times.
✗ Branch 5 → 12 not taken.
317658 return resolveValue(node, exprResult);
160 }
161
162 174360 llvm::Value *IRGenerator::resolveValue(const ExprNode *node, LLVMExprResult &exprResult) {
163 174360 return resolveValue(node->getEvaluatedSymbolType(manIdx), exprResult);
164 }
165
166 343628 llvm::Value *IRGenerator::resolveValue(const QualType &qualType, LLVMExprResult &exprResult) {
167 // Check if the value is already present
168
2/2
✓ Branch 2 → 3 taken 108825 times.
✓ Branch 2 → 4 taken 234803 times.
343628 if (exprResult.value != nullptr)
169 108825 return exprResult.value;
170
171 // Check if a constant is present
172
2/2
✓ Branch 4 → 5 taken 72710 times.
✓ Branch 4 → 7 taken 162093 times.
234803 if (exprResult.constant != nullptr) {
173 72710 materializeConstant(exprResult);
174 72710 return exprResult.value;
175 }
176
177
3/4
✓ Branch 7 → 8 taken 2225 times.
✓ Branch 7 → 10 taken 159868 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 2225 times.
162093 assert(exprResult.ptr != nullptr || exprResult.refPtr != nullptr);
178
179 // De-reference if reference type
180
4/4
✓ Branch 10 → 11 taken 152372 times.
✓ Branch 10 → 13 taken 9721 times.
✓ Branch 11 → 12 taken 11 times.
✓ Branch 11 → 13 taken 152361 times.
162093 const bool isVolatile = exprResult.entry && exprResult.entry->isVolatile;
181
4/4
✓ Branch 14 → 15 taken 2235 times.
✓ Branch 14 → 24 taken 159858 times.
✓ Branch 15 → 16 taken 2225 times.
✓ Branch 15 → 24 taken 10 times.
162093 if (exprResult.refPtr != nullptr && exprResult.ptr == nullptr)
182
2/4
✓ Branch 19 → 20 taken 2225 times.
✗ Branch 19 → 34 not taken.
✓ Branch 20 → 21 taken 2225 times.
✗ Branch 20 → 34 not taken.
2225 exprResult.ptr = insertLoad(builder.getPtrTy(), exprResult.refPtr, isVolatile);
183
184 // Load the value from the pointer
185
1/2
✓ Branch 24 → 25 taken 162093 times.
✗ Branch 24 → 46 not taken.
162093 const QualType referencedType = qualType.removeReferenceWrapper();
186
1/2
✓ Branch 28 → 29 taken 162093 times.
✗ Branch 28 → 40 not taken.
162093 exprResult.value = insertLoad(referencedType, exprResult.ptr, isVolatile);
187
188 162093 return exprResult.value;
189 }
190
191 17893 llvm::Value *IRGenerator::resolveAddress(const ASTNode *node) {
192 // Visit the given AST node
193
2/4
✓ Branch 2 → 3 taken 17893 times.
✗ Branch 2 → 11 not taken.
✓ Branch 3 → 4 taken 17893 times.
✗ Branch 3 → 9 not taken.
17893 auto exprResult = any_cast<LLVMExprResult>(visit(node));
194
1/2
✓ Branch 5 → 6 taken 17893 times.
✗ Branch 5 → 12 not taken.
35786 return resolveAddress(exprResult);
195 }
196
197 127456 llvm::Value *IRGenerator::resolveAddress(LLVMExprResult &exprResult) {
198 // Check if an address is already present
199
2/2
✓ Branch 2 → 3 taken 104180 times.
✓ Branch 2 → 4 taken 23276 times.
127456 if (exprResult.ptr != nullptr)
200 104180 return exprResult.ptr;
201
202 // Check if the reference address is already present
203
3/4
✓ Branch 4 → 5 taken 18014 times.
✓ Branch 4 → 7 taken 5262 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 18014 times.
23276 const bool isVolatile = exprResult.entry && exprResult.entry->isVolatile;
204
3/4
✓ Branch 8 → 9 taken 18981 times.
✓ Branch 8 → 18 taken 4295 times.
✓ Branch 9 → 10 taken 18981 times.
✗ Branch 9 → 18 not taken.
23276 if (exprResult.refPtr != nullptr && exprResult.ptr == nullptr) {
205
2/4
✓ Branch 13 → 14 taken 18981 times.
✗ Branch 13 → 35 not taken.
✓ Branch 14 → 15 taken 18981 times.
✗ Branch 14 → 35 not taken.
18981 exprResult.ptr = insertLoad(builder.getPtrTy(), exprResult.refPtr, isVolatile);
206 18981 return exprResult.ptr;
207 }
208
209 // If not, store the value or constant
210 4295 materializeConstant(exprResult);
211
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 4295 times.
4295 assert(exprResult.value != nullptr);
212
7/12
✓ Branch 21 → 22 taken 6 times.
✓ Branch 21 → 23 taken 4289 times.
✓ Branch 22 → 26 taken 6 times.
✗ Branch 22 → 43 not taken.
✓ Branch 25 → 26 taken 4289 times.
✗ Branch 25 → 43 not taken.
✓ Branch 27 → 28 taken 4295 times.
✗ Branch 27 → 41 not taken.
✓ Branch 29 → 30 taken 4289 times.
✓ Branch 29 → 32 taken 6 times.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 46 not taken.
8584 exprResult.ptr = insertAlloca(exprResult.value->getType(), exprResult.entry ? exprResult.entry->name : "");
213 4295 insertStore(exprResult.value, exprResult.ptr, isVolatile);
214
215 4295 return exprResult.ptr;
216 }
217
218 5699 llvm::Constant *IRGenerator::getDefaultValueForSymbolType(const QualType &symbolType) { // NOLINT(misc-no-recursion)
219 // Double
220
2/2
✓ Branch 3 → 4 taken 46 times.
✓ Branch 3 → 9 taken 5653 times.
5699 if (symbolType.is(TY_DOUBLE))
221
2/4
✓ Branch 4 → 5 taken 46 times.
✗ Branch 4 → 129 not taken.
✓ Branch 5 → 6 taken 46 times.
✗ Branch 5 → 127 not taken.
46 return llvm::ConstantFP::get(context, llvm::APFloat(0.0));
222
223 // Int
224
2/2
✓ Branch 10 → 11 taken 984 times.
✓ Branch 10 → 13 taken 4669 times.
5653 if (symbolType.is(TY_INT))
225 984 return builder.getInt32(0);
226
227 // Short
228
2/2
✓ Branch 14 → 15 taken 14 times.
✓ Branch 14 → 17 taken 4655 times.
4669 if (symbolType.is(TY_SHORT))
229 14 return builder.getInt16(0);
230
231 // Long
232
2/2
✓ Branch 18 → 19 taken 924 times.
✓ Branch 18 → 21 taken 3731 times.
4655 if (symbolType.is(TY_LONG))
233 924 return builder.getInt64(0);
234
235 // Byte or char
236
3/4
✓ Branch 21 → 22 taken 3731 times.
✗ Branch 21 → 130 not taken.
✓ Branch 22 → 23 taken 131 times.
✓ Branch 22 → 25 taken 3600 times.
3731 if (symbolType.isOneOf({TY_BYTE, TY_CHAR}))
237 131 return builder.getInt8(0);
238
239 // String
240
2/2
✓ Branch 26 → 27 taken 925 times.
✓ Branch 26 → 35 taken 2675 times.
3600 if (symbolType.is(TY_STRING)) {
241
3/6
✓ Branch 27 → 28 taken 925 times.
✗ Branch 27 → 132 not taken.
✓ Branch 28 → 29 taken 925 times.
✗ Branch 28 → 131 not taken.
✓ Branch 29 → 30 taken 925 times.
✗ Branch 29 → 131 not taken.
925 llvm::GlobalVariable *globalString = builder.CreateGlobalString("", "");
242
1/2
✓ Branch 30 → 31 taken 925 times.
✗ Branch 30 → 34 not taken.
925 if (cliOptions.comparableOutput)
243
2/4
✓ Branch 31 → 32 taken 925 times.
✗ Branch 31 → 133 not taken.
✓ Branch 32 → 33 taken 925 times.
✗ Branch 32 → 133 not taken.
925 globalString->setAlignment(llvm::Align(4));
244 925 return globalString;
245 }
246
247 // Bool
248
2/2
✓ Branch 36 → 37 taken 133 times.
✓ Branch 36 → 39 taken 2542 times.
2675 if (symbolType.is(TY_BOOL))
249 133 return builder.getFalse();
250
251 // Pointer or reference
252
3/4
✓ Branch 39 → 40 taken 2542 times.
✗ Branch 39 → 134 not taken.
✓ Branch 40 → 41 taken 2319 times.
✓ Branch 40 → 44 taken 223 times.
2542 if (symbolType.isOneOf({TY_PTR, TY_REF}))
253 2319 return llvm::Constant::getNullValue(builder.getPtrTy());
254
255 // Array
256
2/2
✓ Branch 45 → 46 taken 93 times.
✓ Branch 45 → 61 taken 130 times.
223 if (symbolType.isArray()) {
257 // Get array size
258
1/2
✓ Branch 46 → 47 taken 93 times.
✗ Branch 46 → 143 not taken.
93 const size_t arraySize = symbolType.getArraySize();
259
260 // Get default value for item
261
2/4
✓ Branch 47 → 48 taken 93 times.
✗ Branch 47 → 135 not taken.
✓ Branch 48 → 49 taken 93 times.
✗ Branch 48 → 135 not taken.
93 llvm::Constant *defaultItemValue = getDefaultValueForSymbolType(symbolType.getContained());
262
263 // Retrieve array and item type
264
2/4
✓ Branch 49 → 50 taken 93 times.
✗ Branch 49 → 136 not taken.
✓ Branch 50 → 51 taken 93 times.
✗ Branch 50 → 136 not taken.
93 llvm::Type *itemType = symbolType.getContained().toLLVMType(sourceFile);
265
1/2
✓ Branch 51 → 52 taken 93 times.
✗ Branch 51 → 143 not taken.
93 llvm::ArrayType *arrayType = llvm::ArrayType::get(itemType, arraySize);
266
267 // Create a constant array with n times the default value
268
1/2
✓ Branch 54 → 55 taken 93 times.
✗ Branch 54 → 137 not taken.
186 const std::vector itemConstants(arraySize, defaultItemValue);
269
1/2
✓ Branch 57 → 58 taken 93 times.
✗ Branch 57 → 140 not taken.
93 return llvm::ConstantArray::get(arrayType, itemConstants);
270 93 }
271
272 // Function or procedure
273
3/4
✓ Branch 61 → 62 taken 130 times.
✗ Branch 61 → 144 not taken.
✓ Branch 62 → 63 taken 72 times.
✓ Branch 62 → 70 taken 58 times.
130 if (symbolType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
274
2/4
✓ Branch 63 → 64 taken 72 times.
✗ Branch 63 → 145 not taken.
✓ Branch 64 → 65 taken 72 times.
✗ Branch 64 → 145 not taken.
72 llvm::Constant *ptrDefaultValue = getDefaultValueForSymbolType(QualType(TY_PTR));
275 72 llvm::Constant *sizeDefaultValue = builder.getInt64(0);
276
1/2
✓ Branch 67 → 68 taken 72 times.
✗ Branch 67 → 146 not taken.
72 return llvm::ConstantStruct::get(llvmTypes.lambdaFatPtrType, {ptrDefaultValue, ptrDefaultValue, sizeDefaultValue});
277 }
278
279 // Struct
280
2/2
✓ Branch 71 → 72 taken 57 times.
✓ Branch 71 → 110 taken 1 time.
58 if (symbolType.is(TY_STRUCT)) {
281 // Retrieve field count
282
1/2
✓ Branch 72 → 73 taken 57 times.
✗ Branch 72 → 153 not taken.
57 Scope *structScope = symbolType.getBodyScope();
283
1/2
✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 57 times.
57 assert(structScope != nullptr);
284
1/2
✓ Branch 75 → 76 taken 57 times.
✗ Branch 75 → 153 not taken.
57 const size_t fieldCount = structScope->getFieldCount();
285
286 // Get default values for all fields of the struct
287 57 std::vector<llvm::Constant *> fieldConstants;
288
1/2
✓ Branch 76 → 77 taken 57 times.
✗ Branch 76 → 151 not taken.
57 fieldConstants.reserve(fieldCount);
289
290 // Add default value for each struct field
291
2/2
✓ Branch 102 → 78 taken 117 times.
✓ Branch 102 → 103 taken 57 times.
174 for (size_t i = 0; i < fieldCount; i++) {
292 // Get entry of the field
293
1/2
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 80 taken 117 times.
117 const SymbolTableEntry *fieldEntry = structScope->lookupField(i);
294
3/6
✓ Branch 83 → 84 taken 117 times.
✗ Branch 83 → 87 not taken.
✓ Branch 84 → 85 taken 117 times.
✗ Branch 84 → 149 not taken.
✓ Branch 85 → 86 taken 117 times.
✗ Branch 85 → 87 not taken.
117 assert(fieldEntry != nullptr && fieldEntry->isField());
295
296 // Retrieve default field value
297 llvm::Constant *defaultFieldValue;
298
4/6
✓ Branch 88 → 89 taken 117 times.
✗ Branch 88 → 90 not taken.
✓ Branch 91 → 92 taken 117 times.
✗ Branch 91 → 97 not taken.
✓ Branch 92 → 93 taken 8 times.
✓ Branch 92 → 97 taken 109 times.
117 if (const auto fieldNode = dynamic_cast<FieldNode *>(fieldEntry->declNode); fieldNode && fieldNode->defaultValue)
299
3/6
✓ Branch 93 → 94 taken 8 times.
✗ Branch 93 → 149 not taken.
✓ Branch 94 → 95 taken 8 times.
✗ Branch 94 → 148 not taken.
✓ Branch 95 → 96 taken 8 times.
✗ Branch 95 → 148 not taken.
8 defaultFieldValue = getConst(fieldNode->defaultValue->getCompileTimeValue(manIdx), fieldEntry->getQualType(), fieldNode);
300 else
301
2/4
✓ Branch 97 → 98 taken 109 times.
✗ Branch 97 → 149 not taken.
✓ Branch 98 → 99 taken 109 times.
✗ Branch 98 → 149 not taken.
109 defaultFieldValue = getDefaultValueForSymbolType(fieldEntry->getQualType());
302
303
1/2
✓ Branch 100 → 101 taken 117 times.
✗ Branch 100 → 149 not taken.
117 fieldConstants.push_back(defaultFieldValue);
304 }
305
306
2/4
✓ Branch 103 → 104 taken 57 times.
✗ Branch 103 → 151 not taken.
✓ Branch 104 → 105 taken 57 times.
✗ Branch 104 → 151 not taken.
57 const auto structType = llvm::cast<llvm::StructType>(symbolType.toLLVMType(sourceFile));
307
1/2
✓ Branch 106 → 107 taken 57 times.
✗ Branch 106 → 150 not taken.
57 return llvm::ConstantStruct::get(structType, fieldConstants);
308 57 }
309
310 // Interface
311
1/2
✓ Branch 111 → 112 taken 1 time.
✗ Branch 111 → 118 not taken.
1 if (symbolType.is(TY_INTERFACE)) {
312 1 const auto structType = llvm::cast<llvm::StructType>(symbolType.toLLVMType(sourceFile));
313 1 return llvm::ConstantStruct::get(structType, llvm::Constant::getNullValue(builder.getPtrTy()));
314 }
315
316 throw CompilerError(INTERNAL_ERROR, "Cannot determine default value for symbol type"); // GCOV_EXCL_LINE
317 }
318
319 66318 llvm::Constant *IRGenerator::getConst(const CompileTimeValue &compileTimeValue, const QualType &type, const ASTNode *node) const {
320
2/2
✓ Branch 3 → 4 taken 1943 times.
✓ Branch 3 → 9 taken 64375 times.
66318 if (type.is(TY_DOUBLE))
321
2/4
✓ Branch 4 → 5 taken 1943 times.
✗ Branch 4 → 56 not taken.
✓ Branch 5 → 6 taken 1943 times.
✗ Branch 5 → 54 not taken.
1943 return llvm::ConstantFP::get(context, llvm::APFloat(compileTimeValue.doubleValue));
322
323
2/2
✓ Branch 10 → 11 taken 9759 times.
✓ Branch 10 → 13 taken 54616 times.
64375 if (type.is(TY_INT))
324 9759 return builder.getInt32(compileTimeValue.intValue);
325
326
2/2
✓ Branch 14 → 15 taken 1347 times.
✓ Branch 14 → 17 taken 53269 times.
54616 if (type.is(TY_SHORT))
327 1347 return builder.getInt16(compileTimeValue.shortValue);
328
329
2/2
✓ Branch 18 → 19 taken 30268 times.
✓ Branch 18 → 21 taken 23001 times.
53269 if (type.is(TY_LONG))
330 30268 return builder.getInt64(compileTimeValue.longValue);
331
332
3/4
✓ Branch 21 → 22 taken 23001 times.
✗ Branch 21 → 57 not taken.
✓ Branch 22 → 23 taken 6527 times.
✓ Branch 22 → 25 taken 16474 times.
23001 if (type.isOneOf({TY_BYTE, TY_CHAR}))
333 6527 return builder.getInt8(compileTimeValue.charValue);
334
335
2/2
✓ Branch 26 → 27 taken 11221 times.
✓ Branch 26 → 36 taken 5253 times.
16474 if (type.is(TY_STRING)) {
336 11221 const std::string &stringValue = resourceManager.compileTimeStringValues.at(compileTimeValue.stringValueOffset);
337
2/4
✓ Branch 30 → 31 taken 11221 times.
✗ Branch 30 → 60 not taken.
✓ Branch 31 → 32 taken 11221 times.
✗ Branch 31 → 58 not taken.
33663 return createGlobalStringConst(ANON_GLOBAL_STRING_NAME, stringValue, node->codeLoc);
338 }
339
340
2/2
✓ Branch 37 → 38 taken 5251 times.
✓ Branch 37 → 40 taken 2 times.
5253 if (type.is(TY_BOOL))
341 5251 return builder.getInt1(compileTimeValue.boolValue);
342
343
1/2
✓ Branch 41 → 42 taken 2 times.
✗ Branch 41 → 45 not taken.
2 if (type.is(TY_PTR))
344 2 return llvm::Constant::getNullValue(builder.getPtrTy());
345
346 throw CompilerError(UNHANDLED_BRANCH, "Constant fall-through"); // GCOV_EXCL_LINE
347 }
348
349 131918 llvm::BasicBlock *IRGenerator::createBlock(const std::string &blockName /*=""*/) const {
350
2/4
✓ Branch 2 → 3 taken 131918 times.
✗ Branch 2 → 7 not taken.
✓ Branch 3 → 4 taken 131918 times.
✗ Branch 3 → 7 not taken.
131918 return llvm::BasicBlock::Create(context, blockName);
351 }
352
353 131918 void IRGenerator::switchToBlock(llvm::BasicBlock *block, llvm::Function *parentFct /*=nullptr*/) {
354
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 131918 times.
131918 assert(block->getParent() == nullptr); // Ensure that the block was not added to a function already
355 // If no parent function were passed, use the current function
356
2/2
✓ Branch 5 → 6 taken 90488 times.
✓ Branch 5 → 8 taken 41430 times.
131918 if (!parentFct)
357 90488 parentFct = builder.GetInsertBlock()->getParent();
358 // Append block to current function
359 131918 parentFct->insert(parentFct->end(), block);
360 // Set insert point to the block
361 131918 builder.SetInsertPoint(block);
362 131918 blockAlreadyTerminated = false;
363 131918 }
364
365 2498 void IRGenerator::terminateBlock(const StmtLstNode *stmtLstNode) {
366 2498 generateScopeCleanup(stmtLstNode);
367 2498 blockAlreadyTerminated = true;
368 2498 }
369
370 47115 void IRGenerator::insertJump(llvm::BasicBlock *targetBlock) {
371
2/2
✓ Branch 2 → 3 taken 16095 times.
✓ Branch 2 → 4 taken 31020 times.
47115 if (blockAlreadyTerminated)
372 16095 return;
373 31020 builder.CreateBr(targetBlock);
374 31020 blockAlreadyTerminated = true;
375 }
376
377 39088 void IRGenerator::insertCondJump(llvm::Value *condition, llvm::BasicBlock *trueBlock, llvm::BasicBlock *falseBlock,
378 Likelihood likelihood /*=UNSPECIFIED*/) {
379
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 39088 times.
39088 if (blockAlreadyTerminated)
380 return;
381 39088 llvm::CondBrInst *jumpInst = builder.CreateCondBr(condition, trueBlock, falseBlock);
382 39088 blockAlreadyTerminated = true;
383
384
2/2
✓ Branch 5 → 6 taken 5571 times.
✓ Branch 5 → 7 taken 33517 times.
39088 if (likelihood != Likelihood::UNSPECIFIED)
385 5571 mdGenerator.generateBranchWeightsMetadata(jumpInst, likelihood);
386 }
387
388 41417 void IRGenerator::verifyFunction(const llvm::Function *fct, const CodeLoc &codeLoc) const {
389 // Skip the verifying step if the verifier was disabled manually or debug info is emitted
390
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 41417 times.
41417 if (cliOptions.disableVerifier)
391 return;
392
393 // Verify function
394 41417 std::string output;
395
1/2
✓ Branch 5 → 6 taken 41417 times.
✗ Branch 5 → 20 not taken.
41417 llvm::raw_string_ostream oss(output);
396 if (llvm::verifyFunction(*fct, &oss)) // LCOV_EXCL_LINE
397 throw CompilerError(codeLoc, INVALID_FUNCTION, output); // LCOV_EXCL_LINE
398 41417 }
399
400 2562 void IRGenerator::verifyModule(const CodeLoc &codeLoc) const {
401 // Skip the verifying step if the verifier was disabled manually or debug info is emitted
402
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2562 times.
2562 if (cliOptions.disableVerifier)
403 return;
404
405 // Verify module
406 2562 std::string output;
407
1/2
✓ Branch 5 → 6 taken 2562 times.
✗ Branch 5 → 20 not taken.
2562 llvm::raw_string_ostream oss(output);
408 if (llvm::verifyModule(*module, &oss)) // LCOV_EXCL_LINE
409 throw CompilerError(codeLoc, INVALID_MODULE, output); // LCOV_EXCL_LINE
410 2562 }
411
412 23226 LLVMExprResult IRGenerator::doAssignment(const ASTNode *lhsNode, const ExprNode *rhsNode, const ASTNode *node) {
413 // Get entry of left side
414
2/4
✓ Branch 2 → 3 taken 23226 times.
✗ Branch 2 → 18 not taken.
✓ Branch 3 → 4 taken 23226 times.
✗ Branch 3 → 16 not taken.
23226 auto exprResult = std::any_cast<LLVMExprResult>(visit(lhsNode));
415 23226 const SymbolTableEntry *entry = exprResult.entry;
416
7/10
✓ Branch 5 → 6 taken 21116 times.
✓ Branch 5 → 10 taken 2110 times.
✓ Branch 6 → 7 taken 21116 times.
✗ Branch 6 → 19 not taken.
✓ Branch 7 → 8 taken 21116 times.
✗ Branch 7 → 19 not taken.
✓ Branch 8 → 9 taken 1384 times.
✓ Branch 8 → 10 taken 19732 times.
✓ Branch 10 → 11 taken 21842 times.
✗ Branch 10 → 19 not taken.
23226 llvm::Value *lhsAddress = entry != nullptr && entry->getQualType().isRef() ? exprResult.refPtr : resolveAddress(exprResult);
417
1/2
✓ Branch 12 → 13 taken 23226 times.
✗ Branch 12 → 19 not taken.
46452 return doAssignment(lhsAddress, entry, rhsNode, node);
418 }
419
420 52041 LLVMExprResult IRGenerator::doAssignment(llvm::Value *lhsAddress, const SymbolTableEntry *lhsEntry, const ExprNode *rhsNode,
421 const ASTNode *node, bool isDecl) {
422 // Get symbol type of right side
423
1/2
✓ Branch 2 → 3 taken 52041 times.
✗ Branch 2 → 13 not taken.
52041 const QualType &rhsSType = rhsNode->getEvaluatedSymbolType(manIdx);
424
2/4
✓ Branch 3 → 4 taken 52041 times.
✗ Branch 3 → 12 not taken.
✓ Branch 4 → 5 taken 52041 times.
✗ Branch 4 → 10 not taken.
52041 auto rhs = std::any_cast<LLVMExprResult>(visit(rhsNode));
425
1/2
✓ Branch 6 → 7 taken 52041 times.
✗ Branch 6 → 13 not taken.
104082 return doAssignment(lhsAddress, lhsEntry, rhs, rhsSType, node, isDecl);
426 }
427
428 52638 LLVMExprResult IRGenerator::doAssignment(llvm::Value *lhsAddress, const SymbolTableEntry *lhsEntry, LLVMExprResult &rhs,
429 const QualType &rhsSType, const ASTNode *node, bool isDecl) {
430 // Deduce some information about the assignment
431
4/4
✓ Branch 2 → 3 taken 50528 times.
✓ Branch 2 → 7 taken 2110 times.
✓ Branch 5 → 6 taken 3595 times.
✓ Branch 5 → 7 taken 46933 times.
52638 const bool isRefAssign = lhsEntry != nullptr && lhsEntry->getQualType().isRef();
432 // A non-temporary struct value assigned by value needs a deep copy. This holds whether the destination is a direct
433 // lvalue or the value behind an already-bound reference (assign-through): the binding cases of a reference assignment
434 // (declaration/initial field ref/return value) all return early above before this is consumed, so the remaining
435 // reference assignments are assign-throughs that must copy into the referent instead of shallow-copying (which would
436 // alias the rhs' owned members and double-free).
437
6/8
✓ Branch 8 → 9 taken 52638 times.
✗ Branch 8 → 264 not taken.
✓ Branch 9 → 10 taken 52638 times.
✗ Branch 9 → 264 not taken.
✓ Branch 10 → 11 taken 8526 times.
✓ Branch 10 → 14 taken 44112 times.
✓ Branch 12 → 13 taken 1663 times.
✓ Branch 12 → 14 taken 6863 times.
52638 const bool needsCopy = rhsSType.removeReferenceWrapper().is(TY_STRUCT) && !rhs.isTemporary();
438
439
2/2
✓ Branch 15 → 16 taken 3595 times.
✓ Branch 15 → 57 taken 49043 times.
52638 if (isRefAssign) {
440
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 3595 times.
3595 assert(lhsEntry != nullptr);
441
2/2
✓ Branch 18 → 19 taken 2211 times.
✓ Branch 18 → 33 taken 1384 times.
3595 if (isDecl) { // Reference gets initially assigned
442 // Store lhs pointer to rhs
443
2/4
✓ Branch 22 → 23 taken 2211 times.
✗ Branch 22 → 265 not taken.
✓ Branch 23 → 24 taken 2211 times.
✗ Branch 23 → 265 not taken.
2211 llvm::Value *refAddress = insertAlloca(builder.getPtrTy());
444 2211 updateAddress(lhsEntry, refAddress);
445
446 // Generate debug info for variable declaration
447 2211 diGenerator.generateLocalVarDebugInfo(lhsEntry->name, refAddress);
448
449 // Get address of right side
450 2211 llvm::Value *rhsAddress = resolveAddress(rhs);
451
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 2211 times.
2211 assert(rhsAddress != nullptr);
452 2211 insertStore(rhsAddress, refAddress);
453
454 2211 return LLVMExprResult{.value = rhsAddress, .ptr = refAddress, .entry = lhsEntry};
455 }
456
457 // Reference to reference assignment (only for struct fields that are not initialized yet)
458 // These are only allowed inside a ctor body. In other cases, the value of the reference gets assigned, not the ref itself.
459
6/8
✓ Branch 33 → 34 taken 1013 times.
✓ Branch 33 → 40 taken 371 times.
✓ Branch 35 → 36 taken 1009 times.
✓ Branch 35 → 40 taken 4 times.
✓ Branch 36 → 37 taken 1009 times.
✗ Branch 36 → 40 not taken.
✓ Branch 38 → 39 taken 1009 times.
✗ Branch 38 → 40 not taken.
1384 const bool isInitialFieldRefAssign = isInCtorBody && rhsSType.isRef() && rhs.entry && lhsEntry->isField();
460 // Assigning the result variable
461 1384 const bool isReturnValAssign = lhsEntry->name == RETURN_VARIABLE_NAME;
462
4/4
✓ Branch 42 → 43 taken 375 times.
✓ Branch 42 → 44 taken 1009 times.
✓ Branch 43 → 44 taken 12 times.
✓ Branch 43 → 49 taken 363 times.
1384 if (isInitialFieldRefAssign || isReturnValAssign) {
463 // Get address of right side
464 1021 llvm::Value *referencedAddress = resolveAddress(rhs);
465
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 1021 times.
1021 assert(referencedAddress != nullptr);
466
467 // Store the rhs* to the lhs**
468 1021 insertStore(referencedAddress, lhsAddress);
469
470 1021 return LLVMExprResult{.value = referencedAddress, .ptr = lhsAddress, .entry = lhsEntry};
471 }
472
473 // Load referenced address
474
2/4
✓ Branch 52 → 53 taken 363 times.
✗ Branch 52 → 271 not taken.
✓ Branch 53 → 54 taken 363 times.
✗ Branch 53 → 271 not taken.
363 lhsAddress = insertLoad(builder.getPtrTy(), lhsAddress);
475 }
476
477
8/8
✓ Branch 57 → 58 taken 27201 times.
✓ Branch 57 → 63 taken 22205 times.
✓ Branch 59 → 60 taken 3266 times.
✓ Branch 59 → 63 taken 23935 times.
✓ Branch 61 → 62 taken 3255 times.
✓ Branch 61 → 63 taken 11 times.
✓ Branch 64 → 65 taken 3255 times.
✓ Branch 64 → 86 taken 46151 times.
49406 if (isDecl && rhsSType.is(TY_STRUCT) && rhs.isTemporary()) {
478
1/2
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 3255 times.
3255 assert(lhsEntry != nullptr);
479
1/2
✓ Branch 67 → 68 taken 3255 times.
✗ Branch 67 → 283 not taken.
3255 llvm::Value *rhsAddress = resolveAddress(rhs);
480 // Only adopt the temporary's storage directly (temp stealing) unless it is a phi merging two independently
481 // materialized pointers (e.g. a ternary whose branches each own their own storage). Lifetime markers require
482 // their operand to be a real alloca, so stealing such a phi here would emit an invalid llvm.lifetime.end
483 // under --sanitizer=address. Any other pointer (a plain alloca, or one derived from it via GEP/load, as in
484 // the foreach-loop item extraction below) denotes a single, stable piece of storage and is safe to adopt.
485 // The temporary's ownership fully transfers to lhsEntry either way (its underlying storage is never
486 // separately destructed), so falling back to a shallow copy into a dedicated alloca is exactly as correct,
487 // just gives up the pointer-adoption optimization for the phi case.
488
3/4
✓ Branch 68 → 69 taken 3255 times.
✗ Branch 68 → 283 not taken.
✓ Branch 69 → 70 taken 3249 times.
✓ Branch 69 → 73 taken 6 times.
3255 if (!llvm::isa<llvm::PHINode>(rhsAddress)) {
489 // Directly set the address to the lhs entry (temp stealing)
490
1/2
✓ Branch 70 → 71 taken 3249 times.
✗ Branch 70 → 283 not taken.
3249 updateAddress(lhsEntry, rhsAddress);
491 3249 rhs.entry = lhsEntry;
492 // Generate debug info for variable declaration
493
1/2
✓ Branch 71 → 72 taken 3249 times.
✗ Branch 71 → 283 not taken.
3249 diGenerator.generateLocalVarDebugInfo(lhsEntry->name, rhsAddress);
494 3249 return rhs;
495 }
496 // Size the copy off lhsEntry's own type, not rhsSType: some callers (e.g. the foreach-loop item
497 // extraction below) pass a struct rhsSType only to steer this branch, while rhs itself resolves (through
498 // refPtr indirection) to a differently-typed value; lhsEntry's type is always the authoritative one here.
499
1/2
✓ Branch 73 → 74 taken 6 times.
✗ Branch 73 → 283 not taken.
6 const QualType &lhsQualType = lhsEntry->getQualType();
500
1/2
✓ Branch 77 → 78 taken 6 times.
✗ Branch 77 → 277 not taken.
6 llvm::Value *lhsAddr = insertAlloca(lhsQualType);
501
1/2
✓ Branch 80 → 81 taken 6 times.
✗ Branch 80 → 283 not taken.
6 updateAddress(lhsEntry, lhsAddr);
502
1/2
✓ Branch 81 → 82 taken 6 times.
✗ Branch 81 → 283 not taken.
6 diGenerator.generateLocalVarDebugInfo(lhsEntry->name, lhsAddr);
503
2/4
✓ Branch 82 → 83 taken 6 times.
✗ Branch 82 → 283 not taken.
✓ Branch 83 → 84 taken 6 times.
✗ Branch 83 → 283 not taken.
6 generateShallowCopy(rhsAddress, lhsQualType.toLLVMType(sourceFile), lhsAddr, lhsEntry->isVolatile);
504 6 rhs.ptr = lhsAddr;
505 6 rhs.entry = lhsEntry;
506 6 return rhs;
507 }
508
509 // Allocate new memory if the lhs address does not exist
510
2/2
✓ Branch 86 → 87 taken 23723 times.
✓ Branch 86 → 98 taken 22428 times.
46151 if (!lhsAddress) {
511
1/2
✗ Branch 87 → 88 not taken.
✓ Branch 87 → 89 taken 23723 times.
23723 assert(lhsEntry != nullptr);
512
2/4
✓ Branch 92 → 93 taken 23723 times.
✗ Branch 92 → 284 not taken.
✓ Branch 93 → 94 taken 23723 times.
✗ Branch 93 → 284 not taken.
23723 lhsAddress = insertAlloca(lhsEntry->getQualType());
513 23723 updateAddress(lhsEntry, lhsAddress);
514 // Generate debug info for variable declaration
515 23723 diGenerator.generateLocalVarDebugInfo(lhsEntry->name, lhsAddress);
516 }
517
518 // Check if we try to assign an array by value to a pointer. Here we have to store the address of the first element to the lhs
519
10/10
✓ Branch 98 → 99 taken 44041 times.
✓ Branch 98 → 107 taken 2110 times.
✓ Branch 101 → 102 taken 10121 times.
✓ Branch 101 → 107 taken 33920 times.
✓ Branch 103 → 104 taken 7 times.
✓ Branch 103 → 107 taken 10114 times.
✓ Branch 105 → 106 taken 2 times.
✓ Branch 105 → 107 taken 5 times.
✓ Branch 108 → 109 taken 2 times.
✓ Branch 108 → 124 taken 46149 times.
46151 if (lhsEntry && lhsEntry->getQualType().isPtr() && rhsSType.isArray() && rhsSType.getArraySize() != ARRAY_SIZE_UNKNOWN) {
520 // Get address of right side
521
1/2
✓ Branch 109 → 110 taken 2 times.
✗ Branch 109 → 297 not taken.
2 llvm::Value *rhsAddress = resolveAddress(rhs);
522
1/2
✗ Branch 110 → 111 not taken.
✓ Branch 110 → 112 taken 2 times.
2 assert(rhsAddress != nullptr);
523
1/2
✓ Branch 112 → 113 taken 2 times.
✗ Branch 112 → 297 not taken.
2 llvm::Type *elementTy = rhsSType.toLLVMType(sourceFile);
524
2/4
✓ Branch 113 → 114 taken 2 times.
✗ Branch 113 → 297 not taken.
✓ Branch 114 → 115 taken 2 times.
✗ Branch 114 → 297 not taken.
2 llvm::Value *indices[2] = {builder.getInt64(0), builder.getInt32(0)};
525
1/2
✓ Branch 119 → 120 taken 2 times.
✗ Branch 119 → 290 not taken.
2 llvm::Value *firstItemAddress = insertInBoundsGEP(elementTy, rhsAddress, indices);
526
1/2
✓ Branch 122 → 123 taken 2 times.
✗ Branch 122 → 297 not taken.
2 insertStore(firstItemAddress, lhsAddress);
527 2 return LLVMExprResult{.value = rhsAddress, .ptr = lhsAddress, .entry = lhsEntry};
528 }
529
530 // Handle operator overloads
531
6/6
✓ Branch 124 → 125 taken 22205 times.
✓ Branch 124 → 128 taken 23944 times.
✓ Branch 126 → 127 taken 506 times.
✓ Branch 126 → 128 taken 21699 times.
✓ Branch 129 → 130 taken 506 times.
✓ Branch 129 → 146 taken 45643 times.
46149 if (!isDecl && conversionManager.callsOverloadedOpFct(node, DEFAULT_OP_IDX)) {
532 506 ResolverFct lhsV = [&] { return static_cast<llvm::Value *>(nullptr); };
533
1/2
✓ Branch 131 → 132 taken 506 times.
✗ Branch 131 → 298 not taken.
506 ResolverFct rhsV = [&] { return resolveValue(rhsSType, rhs); };
534 1012 ResolverFct lhsP = [&] { return lhsAddress; };
535 1012 ResolverFct rhsP = [&] { return resolveAddress(rhs); };
536 506 return conversionManager.callOperatorOverloadFct<2>(node, {lhsV, lhsP, rhsV, rhsP}, DEFAULT_OP_IDX);
537 506 }
538
539 // Check if we need to copy the rhs to the lhs. This happens for structs
540
2/2
✓ Branch 146 → 147 taken 649 times.
✓ Branch 146 → 232 taken 44994 times.
45643 if (needsCopy) {
541 // Get address of right side
542
1/2
✓ Branch 147 → 148 taken 649 times.
✗ Branch 147 → 374 not taken.
649 llvm::Value *rhsAddress = resolveAddress(rhs);
543
1/2
✗ Branch 148 → 149 not taken.
✓ Branch 148 → 150 taken 649 times.
649 assert(rhsAddress != nullptr);
544
545 // If the lhs already holds an initialized, non-trivially-destructible struct, its old value must be
546 // destructed before the copy overwrites it, otherwise its owning members (heap pointers, strings, ...)
547 // would leak. The typechecker only sets a dtor in exactly those cases. To stay correct for a self-
548 // assignment like 'a = a', the destruct + copy are skipped entirely when both sides share the address
549 // (the assignment is a no-op in that case, and destructing first would corrupt the value to copy from).
550
1/2
✓ Branch 150 → 151 taken 649 times.
✗ Branch 150 → 152 not taken.
649 const auto *assignNode = dynamic_cast<const AssignExprNode *>(node);
551
3/4
✓ Branch 153 → 154 taken 638 times.
✓ Branch 153 → 156 taken 11 times.
✓ Branch 154 → 155 taken 638 times.
✗ Branch 154 → 374 not taken.
649 const Function *lhsDtor = assignNode ? assignNode->lhsDtorFct.at(manIdx) : nullptr;
552 649 llvm::BasicBlock *bCopyEnd = nullptr;
553
2/2
✓ Branch 157 → 158 taken 19 times.
✓ Branch 157 → 178 taken 630 times.
649 if (lhsDtor != nullptr) {
554
2/4
✓ Branch 160 → 161 taken 19 times.
✗ Branch 160 → 318 not taken.
✓ Branch 161 → 162 taken 19 times.
✗ Branch 161 → 316 not taken.
38 llvm::BasicBlock *bCopy = createBlock("assign.copy");
555
2/4
✓ Branch 166 → 167 taken 19 times.
✗ Branch 166 → 324 not taken.
✓ Branch 167 → 168 taken 19 times.
✗ Branch 167 → 322 not taken.
19 bCopyEnd = createBlock("assign.copy.end");
556
3/6
✓ Branch 170 → 171 taken 19 times.
✗ Branch 170 → 328 not taken.
✓ Branch 171 → 172 taken 19 times.
✗ Branch 171 → 328 not taken.
✓ Branch 172 → 173 taken 19 times.
✗ Branch 172 → 328 not taken.
19 insertCondJump(builder.CreateICmpEQ(lhsAddress, rhsAddress), bCopyEnd, bCopy);
557
1/2
✓ Branch 173 → 174 taken 19 times.
✗ Branch 173 → 374 not taken.
19 switchToBlock(bCopy);
558
1/2
✓ Branch 175 → 176 taken 19 times.
✗ Branch 175 → 329 not taken.
19 generateCtorOrDtorCall(lhsAddress, lhsDtor, {});
559 }
560
561
2/4
✓ Branch 178 → 179 taken 649 times.
✗ Branch 178 → 332 not taken.
✓ Branch 179 → 180 taken 649 times.
✗ Branch 179 → 332 not taken.
649 const QualType rhsSTypeNonRef = rhsSType.removeReferenceWrapper().toNonConst();
562
3/4
✓ Branch 180 → 181 taken 649 times.
✗ Branch 180 → 374 not taken.
✓ Branch 181 → 182 taken 491 times.
✓ Branch 181 → 198 taken 158 times.
649 if (rhsSTypeNonRef.isTriviallyCopyable(node)) {
563 // Create shallow copy
564
1/2
✓ Branch 182 → 183 taken 491 times.
✗ Branch 182 → 340 not taken.
491 llvm::Type *rhsType = rhsSTypeNonRef.toLLVMType(sourceFile);
565
3/10
✓ Branch 183 → 184 taken 491 times.
✗ Branch 183 → 185 not taken.
✓ Branch 184 → 188 taken 491 times.
✗ Branch 184 → 333 not taken.
✗ Branch 187 → 188 not taken.
✗ Branch 187 → 333 not taken.
✗ Branch 188 → 189 not taken.
✓ Branch 188 → 191 taken 491 times.
✗ Branch 333 → 334 not taken.
✗ Branch 333 → 336 not taken.
491 const std::string copyName = lhsEntry ? lhsEntry->name : "";
566
3/6
✓ Branch 191 → 192 taken 491 times.
✗ Branch 191 → 194 not taken.
✗ Branch 192 → 193 not taken.
✓ Branch 192 → 194 taken 491 times.
✓ Branch 195 → 196 taken 491 times.
✗ Branch 195 → 338 not taken.
491 generateShallowCopy(rhsAddress, rhsType, lhsAddress, lhsEntry && lhsEntry->isVolatile);
567 491 } else {
568 // Check if we have a copy ctor
569
1/2
✓ Branch 198 → 199 taken 158 times.
✗ Branch 198 → 373 not taken.
158 Scope *structScope = rhsSTypeNonRef.getBodyScope();
570
2/4
✓ Branch 199 → 200 taken 158 times.
✗ Branch 199 → 345 not taken.
✓ Branch 204 → 205 taken 158 times.
✗ Branch 204 → 341 not taken.
474 const ArgList args = {{rhsSTypeNonRef.toConstRef(node), rhs.isTemporary()}};
571
2/4
✓ Branch 208 → 209 taken 158 times.
✗ Branch 208 → 349 not taken.
✓ Branch 209 → 210 taken 158 times.
✗ Branch 209 → 347 not taken.
158 const Function *copyCtor = FunctionManager::lookup(structScope, CTOR_FUNCTION_NAME, rhsSTypeNonRef, args, true);
572
1/2
✓ Branch 212 → 213 taken 158 times.
✗ Branch 212 → 221 not taken.
158 if (copyCtor != nullptr) {
573 // Call copy ctor
574
2/4
✓ Branch 215 → 216 taken 158 times.
✗ Branch 215 → 355 not taken.
✓ Branch 216 → 217 taken 158 times.
✗ Branch 216 → 353 not taken.
316 generateCtorOrDtorCall(lhsAddress, copyCtor, {rhsAddress});
575 } else {
576 const std::string structName = rhsSTypeNonRef.getName();
577 const std::string msg = "Cannot copy struct '" + structName + "', as it is not trivially copyable and has no copy ctor";
578 throw SemanticError(node, COPY_CTOR_REQUIRED, msg);
579 }
580 158 }
581
582 // Close the self-assignment guard
583
2/2
✓ Branch 228 → 229 taken 19 times.
✓ Branch 228 → 231 taken 630 times.
649 if (bCopyEnd != nullptr) {
584
1/2
✓ Branch 229 → 230 taken 19 times.
✗ Branch 229 → 374 not taken.
19 insertJump(bCopyEnd);
585
1/2
✓ Branch 230 → 231 taken 19 times.
✗ Branch 230 → 374 not taken.
19 switchToBlock(bCopyEnd);
586 }
587 649 return LLVMExprResult{.ptr = lhsAddress, .entry = lhsEntry};
588 }
589
590 // Optimization: If we have the address of both sides, we can do a memcpy instead of loading and storing the value
591 44994 llvm::Value *rhsValue = nullptr;
592
8/8
✓ Branch 233 → 234 taken 2025 times.
✓ Branch 233 → 237 taken 42969 times.
✓ Branch 234 → 235 taken 1938 times.
✓ Branch 234 → 237 taken 87 times.
✓ Branch 235 → 236 taken 1931 times.
✓ Branch 235 → 237 taken 7 times.
✓ Branch 238 → 239 taken 1931 times.
✓ Branch 238 → 260 taken 43063 times.
44994 if (rhsSType.is(TY_STRUCT) && rhs.value == nullptr && rhs.constant == nullptr) {
593 // Create shallow copy
594
2/4
✓ Branch 239 → 240 taken 1931 times.
✗ Branch 239 → 375 not taken.
✓ Branch 240 → 241 taken 1931 times.
✗ Branch 240 → 375 not taken.
1931 const QualType rhsSTypeNonRef = rhsSType.removeReferenceWrapper().toNonConst();
595
1/2
✓ Branch 241 → 242 taken 1931 times.
✗ Branch 241 → 383 not taken.
1931 llvm::Type *rhsType = rhsSTypeNonRef.toLLVMType(sourceFile);
596
1/2
✓ Branch 242 → 243 taken 1931 times.
✗ Branch 242 → 383 not taken.
1931 llvm::Value *rhsAddress = resolveAddress(rhs);
597
1/2
✗ Branch 243 → 244 not taken.
✓ Branch 243 → 245 taken 1931 times.
1931 assert(rhsAddress != nullptr);
598
6/10
✓ Branch 245 → 246 taken 1920 times.
✓ Branch 245 → 247 taken 11 times.
✓ Branch 246 → 250 taken 1920 times.
✗ Branch 246 → 376 not taken.
✓ Branch 249 → 250 taken 11 times.
✗ Branch 249 → 376 not taken.
✓ Branch 250 → 251 taken 11 times.
✓ Branch 250 → 253 taken 1920 times.
✗ Branch 376 → 377 not taken.
✗ Branch 376 → 379 not taken.
1942 const std::string copyName = lhsEntry ? lhsEntry->name : "";
599
4/6
✓ Branch 253 → 254 taken 1920 times.
✓ Branch 253 → 256 taken 11 times.
✗ Branch 254 → 255 not taken.
✓ Branch 254 → 256 taken 1920 times.
✓ Branch 257 → 258 taken 1931 times.
✗ Branch 257 → 381 not taken.
1931 generateShallowCopy(rhsAddress, rhsType, lhsAddress, lhsEntry && lhsEntry->isVolatile);
600 1931 } else {
601 // We can load the value from the right side and store it to the left side
602 // Retrieve value of the right side
603 43063 rhsValue = resolveValue(rhsSType, rhs);
604 // Store the value to the address
605 43063 insertStore(rhsValue, lhsAddress, rhsSType);
606 }
607
608 44994 return LLVMExprResult{.value = rhsValue, .ptr = lhsAddress, .entry = lhsEntry};
609
5/14
✓ Branch 134 → 135 taken 506 times.
✗ Branch 134 → 301 not taken.
✓ Branch 135 → 136 taken 506 times.
✗ Branch 135 → 301 not taken.
✓ Branch 136 → 137 taken 506 times.
✗ Branch 136 → 301 not taken.
✓ Branch 137 → 138 taken 506 times.
✗ Branch 137 → 301 not taken.
✓ Branch 138 → 139 taken 506 times.
✗ Branch 138 → 299 not taken.
✗ Branch 301 → 302 not taken.
✗ Branch 301 → 305 not taken.
✗ Branch 303 → 304 not taken.
✗ Branch 303 → 305 not taken.
506 }
610
611 2726 void IRGenerator::generateShallowCopy(llvm::Value *oldAddress, llvm::Type *varType, llvm::Value *targetAddress,
612 bool isVolatile) const {
613 // Retrieve size to copy
614
1/2
✓ Branch 3 → 4 taken 2726 times.
✗ Branch 3 → 19 not taken.
2726 const llvm::TypeSize typeSize = module->getDataLayout().getTypeAllocSize(varType);
615
616 // Create values for memcpy intrinsic
617
2/4
✓ Branch 4 → 5 taken 2726 times.
✗ Branch 4 → 19 not taken.
✓ Branch 5 → 6 taken 2726 times.
✗ Branch 5 → 19 not taken.
2726 llvm::Value *structSize = builder.getInt64(typeSize);
618
1/2
✓ Branch 6 → 7 taken 2726 times.
✗ Branch 6 → 19 not taken.
2726 llvm::Value *copyVolatile = builder.getInt1(isVolatile);
619
620 // Call memcpy intrinsic to execute the shallow copy
621
1/2
✓ Branch 7 → 8 taken 2726 times.
✗ Branch 7 → 19 not taken.
2726 llvm::Function *memcpyFct = stdFunctionManager.getMemcpyIntrinsic();
622
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 2726 times.
2726 assert(targetAddress != nullptr);
623
3/6
✓ Branch 10 → 11 taken 2726 times.
✗ Branch 10 → 18 not taken.
✓ Branch 12 → 13 taken 2726 times.
✗ Branch 12 → 15 not taken.
✓ Branch 13 → 14 taken 2726 times.
✗ Branch 13 → 15 not taken.
2726 builder.CreateCall(memcpyFct, {targetAddress, oldAddress, structSize, copyVolatile});
624 2726 }
625
626 105837 void IRGenerator::autoDeReferencePtr(llvm::Value *&ptr, QualType &symbolType) {
627
6/6
✓ Branch 12 → 13 taken 110434 times.
✓ Branch 12 → 15 taken 73800 times.
✓ Branch 14 → 15 taken 4597 times.
✓ Branch 14 → 16 taken 105837 times.
✓ Branch 17 → 3 taken 78397 times.
✓ Branch 17 → 18 taken 105837 times.
184234 while (symbolType.isPtr() || symbolType.isRef()) {
628
1/2
✓ Branch 6 → 7 taken 78397 times.
✗ Branch 6 → 19 not taken.
78397 ptr = insertLoad(symbolType, ptr);
629
1/2
✓ Branch 9 → 10 taken 78397 times.
✗ Branch 9 → 25 not taken.
78397 symbolType = symbolType.getContained();
630 }
631 105837 }
632
633 314 llvm::GlobalVariable *IRGenerator::createGlobalConst(const std::string &baseName, llvm::Constant *constant) const {
634 // Get unused name
635
1/2
✓ Branch 2 → 3 taken 314 times.
✗ Branch 2 → 19 not taken.
314 const std::string globalName = getUnusedGlobalName(baseName);
636 // Create global
637
1/2
✓ Branch 5 → 6 taken 314 times.
✗ Branch 5 → 15 not taken.
314 module->getOrInsertGlobal(globalName, constant->getType());
638
1/2
✓ Branch 7 → 8 taken 314 times.
✗ Branch 7 → 16 not taken.
314 llvm::GlobalVariable *global = module->getNamedGlobal(globalName);
639 // Set initializer to the given constant
640
1/2
✓ Branch 8 → 9 taken 314 times.
✗ Branch 8 → 17 not taken.
314 global->setInitializer(constant);
641 314 global->setConstant(true);
642
1/2
✓ Branch 10 → 11 taken 314 times.
✗ Branch 10 → 17 not taken.
314 global->setLinkage(llvm::GlobalValue::PrivateLinkage);
643 314 global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
644 314 return global;
645 314 }
646
647 12538 llvm::GlobalVariable *IRGenerator::createGlobalStringConst(const std::string &baseName, const std::string &value) const {
648 // Get unused name
649
1/2
✓ Branch 2 → 3 taken 12538 times.
✗ Branch 2 → 21 not taken.
12538 const std::string globalName = getUnusedGlobalName(baseName);
650 // Create global
651
2/4
✓ Branch 3 → 4 taken 12538 times.
✗ Branch 3 → 16 not taken.
✓ Branch 5 → 6 taken 12538 times.
✗ Branch 5 → 15 not taken.
12538 builder.CreateGlobalString(value, globalName, 0, module);
652
1/2
✓ Branch 7 → 8 taken 12538 times.
✗ Branch 7 → 17 not taken.
12538 llvm::GlobalVariable *global = module->getNamedGlobal(globalName);
653 // If the output should be comparable, fix alignment to 4 bytes
654
1/2
✓ Branch 8 → 9 taken 12538 times.
✗ Branch 8 → 12 not taken.
12538 if (cliOptions.comparableOutput)
655
2/4
✓ Branch 9 → 10 taken 12538 times.
✗ Branch 9 → 18 not taken.
✓ Branch 10 → 11 taken 12538 times.
✗ Branch 10 → 18 not taken.
12538 global->setAlignment(llvm::Align(4));
656 12538 return global;
657 12538 }
658
659 12538 llvm::GlobalVariable *IRGenerator::createGlobalStringConst(const std::string &baseName, const std::string &value,
660 const CodeLoc &codeLoc) const {
661 12538 llvm::GlobalVariable *global = createGlobalStringConst(baseName, value);
662 // Create debug info
663
2/2
✓ Branch 3 → 4 taken 135 times.
✓ Branch 3 → 10 taken 12403 times.
12538 if (cliOptions.instrumentation.generateDebugInfo)
664
3/6
✓ Branch 5 → 6 taken 135 times.
✗ Branch 5 → 14 not taken.
✓ Branch 6 → 7 taken 135 times.
✗ Branch 6 → 14 not taken.
✓ Branch 7 → 8 taken 135 times.
✗ Branch 7 → 12 not taken.
135 diGenerator.generateGlobalStringDebugInfo(global, global->getName().str(), value.length(), codeLoc);
665 12538 return global;
666 }
667
668 20865 std::string IRGenerator::getUnusedGlobalName(const std::string &baseName) const {
669 // Find an unused global name
670 20865 std::string globalName;
671 20865 unsigned int suffixNumber = 0;
672 do {
673
1/2
✓ Branch 5 → 6 taken 826307 times.
✗ Branch 5 → 15 not taken.
826307 globalName = baseName + std::to_string(suffixNumber);
674 826307 suffixNumber++;
675
3/4
✓ Branch 10 → 11 taken 826307 times.
✗ Branch 10 → 19 not taken.
✓ Branch 11 → 12 taken 805442 times.
✓ Branch 11 → 13 taken 20865 times.
826307 } while (module->getNamedGlobal(globalName) != nullptr);
676 20865 return globalName;
677 }
678
679 77005 void IRGenerator::materializeConstant(LLVMExprResult &exprResult) {
680 // Skip results, that do not contain a constant or already have a value
681
3/4
✓ Branch 2 → 3 taken 74142 times.
✓ Branch 2 → 4 taken 2863 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 74142 times.
77005 if (exprResult.value != nullptr || exprResult.constant == nullptr)
682 2863 return;
683
684 // Default case: the value to the constant
685 74142 exprResult.value = exprResult.constant;
686 }
687
688 49477 bool IRGenerator::isSymbolDSOLocal(bool isPublic) const {
689 // If we are compiling a shared library and export the global symbol, we need to drop dso_local
690 // because it may be interposed by other shared objects by the dynamic linker.
691
3/4
✓ Branch 2 → 3 taken 44179 times.
✓ Branch 2 → 4 taken 5298 times.
✓ Branch 3 → 4 taken 44179 times.
✗ Branch 3 → 5 not taken.
49477 return !(isPublic && cliOptions.outputContainer == OutputContainer::SHARED_LIBRARY);
692 }
693
694 43584 llvm::GlobalValue::LinkageTypes IRGenerator::getSymbolLinkageType(bool isPublic) const {
695
2/2
✓ Branch 2 → 3 taken 38385 times.
✓ Branch 2 → 4 taken 5199 times.
43584 return isPublic ? llvm::GlobalValue::ExternalLinkage : llvm::GlobalValue::InternalLinkage;
696 }
697
698 6831 llvm::GlobalValue::LinkageTypes IRGenerator::getVTableLinkageType(bool isPublic) const {
699 // VTables, type infos and type info names are ODR entities that may legitimately be emitted in more than one
700 // translation unit (e.g. an interface that is defined in one file and used from several importing files). Giving
701 // them weak ODR linkage lets the linker coalesce the duplicates. On ELF this pairs with the comdat group below; on
702 // MachO, which has no comdat support, the weak/coalesced linkage is what prevents a duplicate-symbol error.
703
2/2
✓ Branch 2 → 3 taken 6732 times.
✓ Branch 2 → 4 taken 99 times.
6831 return isPublic ? llvm::GlobalValue::WeakODRLinkage : llvm::GlobalValue::PrivateLinkage;
704 }
705
706 6831 void IRGenerator::attachComdatToSymbol(llvm::GlobalVariable *global, const std::string &comdatName, bool isPublic) const {
707 // MachO does not support comdat annotations
708
6/6
✓ Branch 2 → 3 taken 6732 times.
✓ Branch 2 → 6 taken 99 times.
✓ Branch 4 → 5 taken 6717 times.
✓ Branch 4 → 6 taken 15 times.
✓ Branch 7 → 8 taken 6717 times.
✓ Branch 7 → 12 taken 114 times.
6831 if (isPublic && cliOptions.targetTriple.getObjectFormat() != llvm::Triple::MachO)
709
2/4
✓ Branch 9 → 10 taken 6717 times.
✗ Branch 9 → 13 not taken.
✓ Branch 10 → 11 taken 6717 times.
✗ Branch 10 → 13 not taken.
6717 global->setComdat(module->getOrInsertComdat(comdatName));
710 6831 }
711
712 /**
713 * Attach the function attributes that all functions we emit have in common.
714 *
715 * Spice does not know exceptions and we never emit landing pads, so none of our functions can unwind. We still request
716 * an unwind table, so that debuggers and profilers are able to produce correct stack traces.
717 *
718 * The size levels are not communicated to LLVM by the pass pipeline alone - since Os and Oz both select the O2
719 * pipeline, 'optsize' and 'minsize' on the individual function are what actually distinguishes them. Without 'minsize',
720 * Oz is indistinguishable from Os.
721 *
722 * @param fct Function to attach the attributes to
723 * @param isAlwaysInline Whether the function was declared as inline
724 */
725 41430 void IRGenerator::addCommonFctAttrs(llvm::Function *fct, bool isAlwaysInline) const {
726 41430 fct->addFnAttr(llvm::Attribute::NoUnwind);
727 41430 fct->addFnAttr(llvm::Attribute::getWithUWTableKind(context, llvm::UWTableKind::Default));
728
729 // Explicitly inlined functions must not be marked as 'optnone', because that is incompatible with 'alwaysinline'.
730 // This matches the behavior of other frontends: an inline request is honored, even at O0.
731
2/2
✓ Branch 5 → 6 taken 9136 times.
✓ Branch 5 → 7 taken 32294 times.
41430 if (isAlwaysInline) {
732 9136 fct->addFnAttr(llvm::Attribute::AlwaysInline);
733
2/2
✓ Branch 7 → 8 taken 32243 times.
✓ Branch 7 → 10 taken 51 times.
32294 } else if (cliOptions.optLevel == OptLevel::O0) {
734 32243 fct->addFnAttr(llvm::Attribute::OptimizeNone);
735 32243 fct->addFnAttr(llvm::Attribute::NoInline); // 'optnone' requires 'noinline'
736 }
737
738
2/2
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 14 taken 41428 times.
41430 if (cliOptions.optLevel >= OptLevel::Os) {
739 2 fct->addFnAttr(llvm::Attribute::OptimizeForSize);
740
2/2
✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 14 taken 1 time.
2 if (cliOptions.optLevel == OptLevel::Oz)
741 1 fct->addFnAttr(llvm::Attribute::MinSize);
742 }
743 41430 }
744
745 270677 llvm::Value *IRGenerator::getAddress(const SymbolTableEntry *entry) {
746
1/2
✓ Branch 2 → 3 taken 270677 times.
✗ Branch 2 → 18 not taken.
270677 const auto it = addressMap.find(entry);
747
5/6
✓ Branch 5 → 6 taken 270075 times.
✓ Branch 5 → 9 taken 602 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 270075 times.
✓ Branch 11 → 12 taken 602 times.
✓ Branch 11 → 13 taken 270075 times.
270677 if (it == addressMap.end() || it->second.empty())
748 602 return nullptr;
749 270075 return it->second.top();
750 }
751
752 200832 void IRGenerator::updateAddress(const SymbolTableEntry *entry, llvm::Value *address) {
753
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 200832 times.
200832 assert(address != nullptr);
754
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 200832 times.
200832 assert(address->getType()->isPointerTy());
755 200832 auto &stack = addressMap[entry];
756
2/2
✓ Branch 10 → 11 taken 161825 times.
✓ Branch 10 → 12 taken 39007 times.
200832 if (stack.empty())
757 161825 stack.push(address);
758 else
759 39007 stack.top() = address;
760 200832 }
761
762 63 void IRGenerator::pushAddress(const SymbolTableEntry *entry, llvm::Value *address) {
763
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 63 times.
63 assert(address != nullptr);
764 63 addressMap[entry].push(address);
765 63 }
766
767 63 void IRGenerator::popAddress(const SymbolTableEntry *entry) {
768
1/2
✓ Branch 2 → 3 taken 63 times.
✗ Branch 2 → 14 not taken.
63 auto it = addressMap.find(entry);
769
2/4
✓ Branch 5 → 6 taken 63 times.
✗ Branch 5 → 10 not taken.
✓ Branch 8 → 9 taken 63 times.
✗ Branch 8 → 10 not taken.
63 assert(it != addressMap.end() && !it->second.empty());
770 63 it->second.pop();
771 63 }
772
773 7356 llvm::Function *IRGenerator::getLLVMFunction(const Function *spiceFunc) {
774
1/2
✓ Branch 2 → 3 taken 7356 times.
✗ Branch 2 → 12 not taken.
7356 const auto it = llvmFunctions.find(spiceFunc);
775
2/2
✓ Branch 5 → 6 taken 2969 times.
✓ Branch 5 → 8 taken 4387 times.
14712 return it != llvmFunctions.end() ? it->second : nullptr;
776 }
777
778 39935 void IRGenerator::setLLVMFunction(const Function *spiceFunc, llvm::Function *llvmFunction) {
779
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 39935 times.
39935 assert(llvmFunction != nullptr);
780 39935 llvmFunctions[spiceFunc] = llvmFunction;
781 39935 }
782
783 4879 std::string IRGenerator::getIRString(llvm::Module *llvmModule, const CliOptions &cliOptions) {
784
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 4879 times.
4879 assert(llvmModule != nullptr); // Make sure the module hasn't been moved away
785
3/4
✓ Branch 4 → 5 taken 4879 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 4781 times.
✓ Branch 5 → 7 taken 98 times.
4879 const bool eliminateTarget = cliOptions.comparableOutput && cliOptions.isNativeTarget;
786
787 // Backup target triple and data layout
788
1/2
✓ Branch 9 → 10 taken 4879 times.
✗ Branch 9 → 45 not taken.
4879 const llvm::Triple targetTriple = llvmModule->getTargetTriple();
789
1/2
✓ Branch 11 → 12 taken 4879 times.
✗ Branch 11 → 43 not taken.
4879 const std::string targetDataLayout = llvmModule->getDataLayoutStr();
790 // Remove target triple and data layout
791
2/2
✓ Branch 12 → 13 taken 4781 times.
✓ Branch 12 → 19 taken 98 times.
4879 if (eliminateTarget) {
792 4781 llvmModule->setTargetTriple(llvm::Triple());
793
2/4
✓ Branch 16 → 17 taken 4781 times.
✗ Branch 16 → 34 not taken.
✓ Branch 17 → 18 taken 4781 times.
✗ Branch 17 → 34 not taken.
4781 llvmModule->setDataLayout("");
794 }
795
796 // Get IR string
797 4879 std::string output;
798
1/2
✓ Branch 20 → 21 taken 4879 times.
✗ Branch 20 → 39 not taken.
4879 llvm::raw_string_ostream oss(output);
799
1/2
✓ Branch 21 → 22 taken 4879 times.
✗ Branch 21 → 37 not taken.
4879 llvmModule->print(oss, nullptr);
800
801 // Restore target triple and data layout
802
2/2
✓ Branch 22 → 23 taken 4781 times.
✓ Branch 22 → 29 taken 98 times.
4879 if (eliminateTarget) {
803
1/2
✓ Branch 23 → 24 taken 4781 times.
✗ Branch 23 → 35 not taken.
4781 llvmModule->setTargetTriple(targetTriple);
804
1/2
✓ Branch 27 → 28 taken 4781 times.
✗ Branch 27 → 36 not taken.
4781 llvmModule->setDataLayout(targetDataLayout);
805 }
806
807 4879 return output;
808 4879 }
809
810 /**
811 * Returns the operator function list for the current manifestation and the given node
812 *
813 * @param node Node to retrieve the op fct pointer list from
814 * @return Op fct pointer list
815 */
816 88275 const std::vector<const Function *> &IRGenerator::getOpFctPointers(const ASTNode *node) const {
817
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 88275 times.
88275 assert(node->getOpFctPointers()->size() > manIdx);
818 88275 return node->getOpFctPointers()->at(manIdx);
819 }
820
821 } // namespace spice::compiler
822