LLVM 19.0.0git
Go to the documentation of this file.
1//===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
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
9// This file defines the common initialization infrastructure for the
10// Instrumentation library.
16#include "llvm/IR/Module.h"
19using namespace llvm;
21/// Moves I before IP. Returns new insert point.
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(&*IP);
29 }
30 return IP;
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.
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>(I)) {
44 if (AI->isStaticAlloca())
45 KeepInEntry = true;
46 } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
47 if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
48 KeepInEntry = true;
49 }
50 if (KeepInEntry)
51 IP = moveBeforeInsertPoint(I, IP);
52 }
53 return IP;
56// Create a constant for Str so that we can pass it to the run-time lib.
58 bool AllowMerging,
59 const char *NamePrefix) {
60 Constant *StrConst = ConstantDataArray::getString(M.getContext(), 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;
74 if (auto Comdat = F.getComdat()) return Comdat;
75 assert(F.hasName());
76 Module *M = F.getParent();
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(F.getName());
82 if (T.isOSBinFormatELF() || (T.isOSBinFormatCOFF() && !F.isWeakForLinker()))
83 C->setSelectionKind(Comdat::NoDeduplicate);
84 F.setComdat(C);
85 return C;
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;
static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP)
Moves I before IP. Returns new insert point.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Module.h This file contains the declarations for the Module class.
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
iterator end()
Definition: BasicBlock.h:443
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:206
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:165
@ NoDeduplicate
No deduplication is performed.
Definition: Comdat.h:39
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2913
This is an important base class in LLVM.
Definition: Constant.h:41
const BasicBlock & getEntryBlock() const
Definition: Function.h:790
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition: Globals.cpp:133
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:230
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:655
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:59
void setCodeModel(CodeModel::Model CM)
Change the code model for this global.
Definition: Globals.cpp:507
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
std::optional< CodeModel::Model > getCodeModel() const
Returns the code model (tiny, small, kernel, medium or large model)
Definition: Module.cpp:625
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ObjectFormatType getObjectFormat() const
Get the object format for this triple.
Definition: Triple.h:399
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:373
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
GlobalVariable * createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, const char *NamePrefix="")
Comdat * getOrCreateFunctionComdat(Function &F, Triple &T)
void setGlobalVariableLargeSection(const Triple &TargetTriple, GlobalVariable &GV)
BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39