1//===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the common initialization infrastructure for the
10// Instrumentation library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/Instrumentation.h"
15#include "llvm/IR/IntrinsicInst.h"
16#include "llvm/IR/Module.h"
17#include "llvm/TargetParser/Triple.h"
18
19using namespace llvm;
20
21/// Moves I before IP. Returns new insert point.
22static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP) {
23 // If I is IP, move the insert point down.
24 if (I == IP) {
25 ++IP;
26 } else {
27 // Otherwise, move I before IP and return IP.
28 I->moveBefore(MovePos: &*IP);
29 }
30 return IP;
31}
32
33/// Instrumentation passes often insert conditional checks into entry blocks.
34/// Call this function before splitting the entry block to move instructions
35/// that must remain in the entry block up before the split point. Static
36/// allocas and llvm.localescape calls, for example, must remain in the entry
37/// block.
38BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB,
39 BasicBlock::iterator IP) {
40 assert(&BB.getParent()->getEntryBlock() == &BB);
41 for (auto I = IP, E = BB.end(); I != E; ++I) {
42 bool KeepInEntry = false;
43 if (auto *AI = dyn_cast<AllocaInst>(Val&: I)) {
44 if (AI->isStaticAlloca())
45 KeepInEntry = true;
46 } else if (auto *II = dyn_cast<IntrinsicInst>(Val&: I)) {
47 if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
48 KeepInEntry = true;
49 }
50 if (KeepInEntry)
51 IP = moveBeforeInsertPoint(I, IP);
52 }
53 return IP;
54}
55
56// Create a constant for Str so that we can pass it to the run-time lib.
57GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,
58 bool AllowMerging,
59 const char *NamePrefix) {
60 Constant *StrConst = ConstantDataArray::getString(Context&: M.getContext(), Initializer: Str);
61 // We use private linkage for module-local strings. If they can be merged
62 // with another one, we set the unnamed_addr attribute.
63 GlobalVariable *GV =
64 new GlobalVariable(M, StrConst->getType(), true,
65 GlobalValue::PrivateLinkage, StrConst, NamePrefix);
66 if (AllowMerging)
67 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
68 GV->setAlignment(Align(1)); // Strings may not be merged w/o setting
69 // alignment explicitly.
70 return GV;
71}
72
73Comdat *llvm::getOrCreateFunctionComdat(Function &F, Triple &T) {
74 if (auto Comdat = F.getComdat()) return Comdat;
75 assert(F.hasName());
76 Module *M = F.getParent();
77
78 // Make a new comdat for the function. Use the "no duplicates" selection kind
79 // if the object file format supports it. For COFF we restrict it to non-weak
80 // symbols.
81 Comdat *C = M->getOrInsertComdat(Name: F.getName());
82 if (T.isOSBinFormatELF() || (T.isOSBinFormatCOFF() && !F.isWeakForLinker()))
83 C->setSelectionKind(Comdat::NoDeduplicate);
84 F.setComdat(C);
85 return C;
86}
87
88void llvm::setGlobalVariableLargeSection(const Triple &TargetTriple,
89 GlobalVariable &GV) {
90 // Limit to x86-64 ELF.
91 if (TargetTriple.getArch() != Triple::x86_64 ||
92 TargetTriple.getObjectFormat() != Triple::ELF)
93 return;
94 // Limit to medium/large code models.
95 std::optional<CodeModel::Model> CM = GV.getParent()->getCodeModel();
96 if (!CM || (*CM != CodeModel::Medium && *CM != CodeModel::Large))
97 return;
98 GV.setCodeModel(CodeModel::Large);
99}
100