| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "MetadataGenerator.h" | ||
| 4 | |||
| 5 | #include <global/TypeRegistry.h> | ||
| 6 | #include <irgenerator/IRGenerator.h> | ||
| 7 | #include <util/CustomHashFunctions.h> | ||
| 8 | |||
| 9 | #include <llvm/IR/Module.h> | ||
| 10 | |||
| 11 | namespace spice::compiler { | ||
| 12 | |||
| 13 | 971 | MetadataGenerator::MetadataGenerator(IRGenerator *irGenerator) | |
| 14 |
2/4✓ Branch 3 → 4 taken 971 times.
✗ Branch 3 → 8 not taken.
✓ Branch 4 → 5 taken 971 times.
✗ Branch 4 → 8 not taken.
|
971 | : irGenerator(irGenerator), mdBuilder(irGenerator->context), tbaaRoot(mdBuilder.createTBAARoot("Simple Spice TBAA")), |
| 15 |
2/4✓ Branch 5 → 6 taken 971 times.
✗ Branch 5 → 9 not taken.
✓ Branch 6 → 7 taken 971 times.
✗ Branch 6 → 9 not taken.
|
971 | omnipotentByte(mdBuilder.createTBAAScalarTypeNode("omnipotent byte", tbaaRoot)) {} |
| 16 | |||
| 17 | 1204 | void MetadataGenerator::generateBranchWeightsMetadata(llvm::BranchInst *jumpInst, Likelihood likeliness) { | |
| 18 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1204 times.
|
1204 | assert(likeliness != Likelihood::UNSPECIFIED); |
| 19 | 1204 | const bool likely = likeliness == Likelihood::LIKELY; | |
| 20 |
1/2✓ Branch 4 → 5 taken 1204 times.
✗ Branch 4 → 7 not taken.
|
1204 | llvm::MDNode *profMetadata = likely ? mdBuilder.createLikelyBranchWeights() : mdBuilder.createUnlikelyBranchWeights(); |
| 21 | 1204 | jumpInst->setMetadata(llvm::LLVMContext::MD_prof, profMetadata); | |
| 22 | 1204 | } | |
| 23 | |||
| 24 | 9 | void MetadataGenerator::generateTypeMetadata(llvm::Instruction *inst, const QualType &type) { | |
| 25 | 9 | const uint64_t typeHash = TypeRegistry::getTypeHash(*type.getType()); | |
| 26 | |||
| 27 |
2/4✓ Branch 4 → 5 taken 9 times.
✗ Branch 4 → 16 not taken.
✓ Branch 6 → 7 taken 9 times.
✗ Branch 6 → 14 not taken.
|
9 | llvm::MDString *typeName = mdBuilder.createString(type.getName(true, true)); |
| 28 | 9 | llvm::ConstantAsMetadata *typeId = mdBuilder.createConstant(irGenerator->builder.getInt64(typeHash)); | |
| 29 |
1/2✓ Branch 11 → 12 taken 9 times.
✗ Branch 11 → 18 not taken.
|
9 | llvm::MDNode *typeMetadata = llvm::MDNode::get(irGenerator->context, {typeId, typeName}); |
| 30 | |||
| 31 | 9 | inst->setMetadata(llvm::LLVMContext::MD_type, typeMetadata); | |
| 32 | 9 | } | |
| 33 | |||
| 34 | 17 | void MetadataGenerator::generateTBAAMetadata(llvm::Instruction *inst, const QualType &type) { | |
| 35 |
1/2✓ Branch 2 → 3 taken 17 times.
✗ Branch 2 → 17 not taken.
|
17 | const std::string typeName = type.getName(true, true); |
| 36 |
1/2✓ Branch 4 → 5 taken 17 times.
✗ Branch 4 → 12 not taken.
|
17 | llvm::MDNode *tbaaTypeNode = mdBuilder.createTBAAScalarTypeNode(typeName, omnipotentByte); |
| 37 | // Seems so new, that the verifier does not accept it | ||
| 38 | //const llvm::TypeSize typeSize = irGenerator->module->getDataLayout().getTypeAllocSize(llvmType); | ||
| 39 | //const bool isImmutable = type.isConst(); | ||
| 40 | //llvm::MDNode *tbaaAccessTag = mdBuilder.createTBAAAccessTag(tbaaTypeNode, tbaaTypeNode, 0, typeSize, isImmutable); | ||
| 41 |
2/4✓ Branch 5 → 6 taken 17 times.
✗ Branch 5 → 15 not taken.
✓ Branch 6 → 7 taken 17 times.
✗ Branch 6 → 15 not taken.
|
17 | llvm::ConstantAsMetadata *offset = llvm::ConstantAsMetadata::get(irGenerator->builder.getInt64(0)); |
| 42 |
1/2✓ Branch 8 → 9 taken 17 times.
✗ Branch 8 → 13 not taken.
|
17 | llvm::MDNode *tbaaAccessTag = llvm::MDNode::get(irGenerator->context, {tbaaTypeNode, tbaaTypeNode, offset}); |
| 43 | |||
| 44 |
1/2✓ Branch 9 → 10 taken 17 times.
✗ Branch 9 → 15 not taken.
|
17 | inst->setMetadata(llvm::LLVMContext::MD_tbaa, tbaaAccessTag); |
| 45 | 17 | } | |
| 46 | |||
| 47 | } // namespace spice::compiler | ||
| 48 |