xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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 
19 using namespace llvm;
20 
21 /// Moves I before IP. Returns new insert point.
22 static 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(&*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.
38 BasicBlock::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>(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;
54 }
55 
56 // Create a constant for Str so that we can pass it to the run-time lib.
57 GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,
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;
71 }
72 
73 Comdat *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(F.getName());
82   if (T.isOSBinFormatELF() || (T.isOSBinFormatCOFF() && !F.isWeakForLinker()))
83     C->setSelectionKind(Comdat::NoDeduplicate);
84   F.setComdat(C);
85   return C;
86 }
87 
88