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