124 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines the common initialization infrastructure for the10// Instrumentation library.11//12//===----------------------------------------------------------------------===//13 14#include "llvm/Transforms/Utils/Instrumentation.h"15#include "llvm/IR/DiagnosticInfo.h"16#include "llvm/IR/DiagnosticPrinter.h"17#include "llvm/IR/IntrinsicInst.h"18#include "llvm/IR/Module.h"19#include "llvm/TargetParser/Triple.h"20 21using namespace llvm;22 23static cl::opt<bool> ClIgnoreRedundantInstrumentation(24 "ignore-redundant-instrumentation",25 cl::desc("Ignore redundant instrumentation"), cl::Hidden, cl::init(false));26 27/// Check if module has flag attached, if not add the flag.28bool llvm::checkIfAlreadyInstrumented(Module &M, StringRef Flag) {29 if (!M.getModuleFlag(Flag)) {30 M.addModuleFlag(Module::ModFlagBehavior::Override, Flag, 1);31 return false;32 }33 if (ClIgnoreRedundantInstrumentation)34 return true;35 std::string diagInfo =36 "Redundant instrumentation detected, with module flag: " +37 std::string(Flag);38 M.getContext().diagnose(39 DiagnosticInfoInstrumentation(diagInfo, DiagnosticSeverity::DS_Warning));40 return true;41}42 43/// Moves I before IP. Returns new insert point.44static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I,45 BasicBlock::iterator IP) {46 // If I is IP, move the insert point down.47 if (I == IP) {48 ++IP;49 } else {50 // Otherwise, move I before IP and return IP.51 I->moveBefore(IP);52 }53 return IP;54}55 56/// Instrumentation passes often insert conditional checks into entry blocks.57/// Call this function before splitting the entry block to move instructions58/// that must remain in the entry block up before the split point. Static59/// allocas and llvm.localescape calls, for example, must remain in the entry60/// block.61BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB,62 BasicBlock::iterator IP) {63 assert(&BB.getParent()->getEntryBlock() == &BB);64 for (auto I = IP, E = BB.end(); I != E; ++I) {65 bool KeepInEntry = false;66 if (auto *AI = dyn_cast<AllocaInst>(I)) {67 if (AI->isStaticAlloca())68 KeepInEntry = true;69 } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {70 if (II->getIntrinsicID() == llvm::Intrinsic::localescape)71 KeepInEntry = true;72 }73 if (KeepInEntry)74 IP = moveBeforeInsertPoint(I, IP);75 }76 return IP;77}78 79// Create a constant for Str so that we can pass it to the run-time lib.80GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,81 bool AllowMerging,82 Twine NamePrefix) {83 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);84 // We use private linkage for module-local strings. If they can be merged85 // with another one, we set the unnamed_addr attribute.86 GlobalVariable *GV =87 new GlobalVariable(M, StrConst->getType(), true,88 GlobalValue::PrivateLinkage, StrConst, NamePrefix);89 if (AllowMerging)90 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);91 GV->setAlignment(Align(1)); // Strings may not be merged w/o setting92 // alignment explicitly.93 return GV;94}95 96Comdat *llvm::getOrCreateFunctionComdat(Function &F, Triple &T) {97 if (auto Comdat = F.getComdat())98 return Comdat;99 assert(F.hasName());100 Module *M = F.getParent();101 102 // Make a new comdat for the function. Use the "no duplicates" selection kind103 // if the object file format supports it. For COFF we restrict it to non-weak104 // symbols.105 Comdat *C = M->getOrInsertComdat(F.getName());106 if (T.isOSBinFormatELF() || (T.isOSBinFormatCOFF() && !F.isWeakForLinker()))107 C->setSelectionKind(Comdat::NoDeduplicate);108 F.setComdat(C);109 return C;110}111 112void llvm::setGlobalVariableLargeSection(const Triple &TargetTriple,113 GlobalVariable &GV) {114 // Limit to x86-64 ELF.115 if (TargetTriple.getArch() != Triple::x86_64 ||116 TargetTriple.getObjectFormat() != Triple::ELF)117 return;118 // Limit to medium/large code models.119 std::optional<CodeModel::Model> CM = GV.getParent()->getCodeModel();120 if (!CM || (*CM != CodeModel::Medium && *CM != CodeModel::Large))121 return;122 GV.setCodeModel(CodeModel::Large);123}124