[InstCombine] Signed saturation patterns
[llvm-core.git] / lib / Transforms / Utils / SanitizerStats.cpp
bloba1313c77ed77ed606101e7b367d26eba34e6fdf8
1 //===- SanitizerStats.cpp - Sanitizer statistics gathering ----------------===//
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 // Implements code generation for sanitizer statistics gathering.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Transforms/Utils/SanitizerStats.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Transforms/Utils/ModuleUtils.h"
22 using namespace llvm;
24 SanitizerStatReport::SanitizerStatReport(Module *M) : M(M) {
25 StatTy = ArrayType::get(Type::getInt8PtrTy(M->getContext()), 2);
26 EmptyModuleStatsTy = makeModuleStatsTy();
28 ModuleStatsGV = new GlobalVariable(*M, EmptyModuleStatsTy, false,
29 GlobalValue::InternalLinkage, nullptr);
32 ArrayType *SanitizerStatReport::makeModuleStatsArrayTy() {
33 return ArrayType::get(StatTy, Inits.size());
36 StructType *SanitizerStatReport::makeModuleStatsTy() {
37 return StructType::get(M->getContext(), {Type::getInt8PtrTy(M->getContext()),
38 Type::getInt32Ty(M->getContext()),
39 makeModuleStatsArrayTy()});
42 void SanitizerStatReport::create(IRBuilder<> &B, SanitizerStatKind SK) {
43 Function *F = B.GetInsertBlock()->getParent();
44 Module *M = F->getParent();
45 PointerType *Int8PtrTy = B.getInt8PtrTy();
46 IntegerType *IntPtrTy = B.getIntPtrTy(M->getDataLayout());
47 ArrayType *StatTy = ArrayType::get(Int8PtrTy, 2);
49 Inits.push_back(ConstantArray::get(
50 StatTy,
51 {Constant::getNullValue(Int8PtrTy),
52 ConstantExpr::getIntToPtr(
53 ConstantInt::get(IntPtrTy, uint64_t(SK) << (IntPtrTy->getBitWidth() -
54 kSanitizerStatKindBits)),
55 Int8PtrTy)}));
57 FunctionType *StatReportTy =
58 FunctionType::get(B.getVoidTy(), Int8PtrTy, false);
59 FunctionCallee StatReport =
60 M->getOrInsertFunction("__sanitizer_stat_report", StatReportTy);
62 auto InitAddr = ConstantExpr::getGetElementPtr(
63 EmptyModuleStatsTy, ModuleStatsGV,
64 ArrayRef<Constant *>{
65 ConstantInt::get(IntPtrTy, 0), ConstantInt::get(B.getInt32Ty(), 2),
66 ConstantInt::get(IntPtrTy, Inits.size() - 1),
67 });
68 B.CreateCall(StatReport, ConstantExpr::getBitCast(InitAddr, Int8PtrTy));
71 void SanitizerStatReport::finish() {
72 if (Inits.empty()) {
73 ModuleStatsGV->eraseFromParent();
74 return;
77 PointerType *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
78 IntegerType *Int32Ty = Type::getInt32Ty(M->getContext());
79 Type *VoidTy = Type::getVoidTy(M->getContext());
81 // Create a new ModuleStatsGV to replace the old one. We can't just set the
82 // old one's initializer because its type is different.
83 auto NewModuleStatsGV = new GlobalVariable(
84 *M, makeModuleStatsTy(), false, GlobalValue::InternalLinkage,
85 ConstantStruct::getAnon(
86 {Constant::getNullValue(Int8PtrTy),
87 ConstantInt::get(Int32Ty, Inits.size()),
88 ConstantArray::get(makeModuleStatsArrayTy(), Inits)}));
89 ModuleStatsGV->replaceAllUsesWith(
90 ConstantExpr::getBitCast(NewModuleStatsGV, ModuleStatsGV->getType()));
91 ModuleStatsGV->eraseFromParent();
93 // Create a global constructor to register NewModuleStatsGV.
94 auto F = Function::Create(FunctionType::get(VoidTy, false),
95 GlobalValue::InternalLinkage, "", M);
96 auto BB = BasicBlock::Create(M->getContext(), "", F);
97 IRBuilder<> B(BB);
99 FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false);
100 FunctionCallee StatInit =
101 M->getOrInsertFunction("__sanitizer_stat_init", StatInitTy);
103 B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy));
104 B.CreateRetVoid();
106 appendToGlobalCtors(*M, F, 0);