Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 1 | //===-- InstrProfiling.cpp - Frontend instrumentation based profiling -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 10 | // This pass lowers instrprof_* intrinsics emitted by a frontend for profiling. |
| 11 | // It also builds the data structures and initialization code needed for |
| 12 | // updating execution counts and emitting the profile at runtime. |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/InstrProfiling.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Twine.h" |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Attributes.h" |
| 24 | #include "llvm/IR/BasicBlock.h" |
| 25 | #include "llvm/IR/Constant.h" |
| 26 | #include "llvm/IR/Constants.h" |
| 27 | #include "llvm/IR/DerivedTypes.h" |
| 28 | #include "llvm/IR/Function.h" |
| 29 | #include "llvm/IR/GlobalValue.h" |
| 30 | #include "llvm/IR/GlobalVariable.h" |
| 31 | #include "llvm/IR/Instruction.h" |
| 32 | #include "llvm/IR/Instructions.h" |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 33 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 34 | #include "llvm/IR/IRBuilder.h" |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Module.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Type.h" |
| 37 | #include "llvm/Pass.h" |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 38 | #include "llvm/ProfileData/InstrProf.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Casting.h" |
| 40 | #include "llvm/Support/CommandLine.h" |
| 41 | #include "llvm/Support/Error.h" |
| 42 | #include "llvm/Support/ErrorHandling.h" |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 44 | #include <algorithm> |
| 45 | #include <cassert> |
| 46 | #include <cstddef> |
| 47 | #include <cstdint> |
| 48 | #include <string> |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 49 | |
| 50 | using namespace llvm; |
| 51 | |
| 52 | #define DEBUG_TYPE "instrprof" |
| 53 | |
| 54 | namespace { |
| 55 | |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 56 | cl::opt<bool> DoNameCompression("enable-name-compression", |
| 57 | cl::desc("Enable name string compression"), |
| 58 | cl::init(true)); |
| 59 | |
Rong Xu | 20f5df1 | 2017-01-11 20:19:41 +0000 | [diff] [blame] | 60 | cl::opt<bool> DoHashBasedCounterSplit( |
| 61 | "hash-based-counter-split", |
| 62 | cl::desc("Rename counter variable of a comdat function based on cfg hash"), |
| 63 | cl::init(true)); |
| 64 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 65 | cl::opt<bool> ValueProfileStaticAlloc( |
| 66 | "vp-static-alloc", |
| 67 | cl::desc("Do static counter allocation for value profiler"), |
| 68 | cl::init(true)); |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 69 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 70 | cl::opt<double> NumCountersPerValueSite( |
| 71 | "vp-counters-per-site", |
| 72 | cl::desc("The average number of profile counters allocated " |
| 73 | "per value profiling site."), |
| 74 | // This is set to a very small value because in real programs, only |
| 75 | // a very small percentage of value sites have non-zero targets, e.g, 1/30. |
| 76 | // For those sites with non-zero profile, the average number of targets |
| 77 | // is usually smaller than 2. |
| 78 | cl::init(1.0)); |
| 79 | |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 80 | class InstrProfilingLegacyPass : public ModulePass { |
| 81 | InstrProfiling InstrProf; |
| 82 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 83 | public: |
| 84 | static char ID; |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 85 | |
| 86 | InstrProfilingLegacyPass() : ModulePass(ID) {} |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 87 | InstrProfilingLegacyPass(const InstrProfOptions &Options) |
| 88 | : ModulePass(ID), InstrProf(Options) {} |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 89 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 90 | StringRef getPassName() const override { |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 91 | return "Frontend instrumentation-based coverage lowering"; |
| 92 | } |
| 93 | |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 94 | bool runOnModule(Module &M) override { |
| 95 | return InstrProf.run(M, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()); |
| 96 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 97 | |
| 98 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 99 | AU.setPreservesCFG(); |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 100 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 101 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 104 | } // end anonymous namespace |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 105 | |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 106 | PreservedAnalyses InstrProfiling::run(Module &M, ModuleAnalysisManager &AM) { |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 107 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(M); |
| 108 | if (!run(M, TLI)) |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 109 | return PreservedAnalyses::all(); |
| 110 | |
| 111 | return PreservedAnalyses::none(); |
| 112 | } |
| 113 | |
| 114 | char InstrProfilingLegacyPass::ID = 0; |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 115 | INITIALIZE_PASS_BEGIN( |
| 116 | InstrProfilingLegacyPass, "instrprof", |
| 117 | "Frontend instrumentation-based coverage lowering.", false, false) |
| 118 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 119 | INITIALIZE_PASS_END( |
| 120 | InstrProfilingLegacyPass, "instrprof", |
| 121 | "Frontend instrumentation-based coverage lowering.", false, false) |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 122 | |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 123 | ModulePass * |
| 124 | llvm::createInstrProfilingLegacyPass(const InstrProfOptions &Options) { |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 125 | return new InstrProfilingLegacyPass(Options); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 128 | bool InstrProfiling::isMachO() const { |
| 129 | return Triple(M->getTargetTriple()).isOSBinFormatMachO(); |
| 130 | } |
| 131 | |
| 132 | /// Get the section name for the counter variables. |
| 133 | StringRef InstrProfiling::getCountersSection() const { |
| 134 | return getInstrProfCountersSectionName(isMachO()); |
| 135 | } |
| 136 | |
| 137 | /// Get the section name for the name variables. |
| 138 | StringRef InstrProfiling::getNameSection() const { |
| 139 | return getInstrProfNameSectionName(isMachO()); |
| 140 | } |
| 141 | |
| 142 | /// Get the section name for the profile data variables. |
| 143 | StringRef InstrProfiling::getDataSection() const { |
| 144 | return getInstrProfDataSectionName(isMachO()); |
| 145 | } |
| 146 | |
| 147 | /// Get the section name for the coverage mapping data. |
| 148 | StringRef InstrProfiling::getCoverageSection() const { |
| 149 | return getInstrProfCoverageSectionName(isMachO()); |
| 150 | } |
| 151 | |
Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 152 | static InstrProfIncrementInst *castToIncrementInst(Instruction *Instr) { |
| 153 | InstrProfIncrementInst *Inc = dyn_cast<InstrProfIncrementInstStep>(Instr); |
| 154 | if (Inc) |
| 155 | return Inc; |
| 156 | return dyn_cast<InstrProfIncrementInst>(Instr); |
| 157 | } |
| 158 | |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 159 | bool InstrProfiling::run(Module &M, const TargetLibraryInfo &TLI) { |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 160 | bool MadeChange = false; |
| 161 | |
| 162 | this->M = &M; |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 163 | this->TLI = &TLI; |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 164 | NamesVar = nullptr; |
| 165 | NamesSize = 0; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 166 | ProfileDataMap.clear(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 167 | UsedVars.clear(); |
| 168 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 169 | // We did not know how many value sites there would be inside |
| 170 | // the instrumented function. This is counting the number of instrumented |
| 171 | // target value sites to enter it as field in the profile data variable. |
Rong Xu | 294572f | 2016-01-19 18:29:54 +0000 | [diff] [blame] | 172 | for (Function &F : M) { |
| 173 | InstrProfIncrementInst *FirstProfIncInst = nullptr; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 174 | for (BasicBlock &BB : F) |
Rong Xu | 294572f | 2016-01-19 18:29:54 +0000 | [diff] [blame] | 175 | for (auto I = BB.begin(), E = BB.end(); I != E; I++) |
| 176 | if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I)) |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 177 | computeNumValueSiteCounts(Ind); |
Rong Xu | 294572f | 2016-01-19 18:29:54 +0000 | [diff] [blame] | 178 | else if (FirstProfIncInst == nullptr) |
| 179 | FirstProfIncInst = dyn_cast<InstrProfIncrementInst>(I); |
| 180 | |
| 181 | // Value profiling intrinsic lowering requires per-function profile data |
| 182 | // variable to be created first. |
| 183 | if (FirstProfIncInst != nullptr) |
| 184 | static_cast<void>(getOrCreateRegionCounters(FirstProfIncInst)); |
| 185 | } |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 186 | |
| 187 | for (Function &F : M) |
| 188 | for (BasicBlock &BB : F) |
| 189 | for (auto I = BB.begin(), E = BB.end(); I != E;) { |
| 190 | auto Instr = I++; |
Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 191 | InstrProfIncrementInst *Inc = castToIncrementInst(&*Instr); |
| 192 | if (Inc) { |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 193 | lowerIncrement(Inc); |
| 194 | MadeChange = true; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 195 | } else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(Instr)) { |
| 196 | lowerValueProfileInst(Ind); |
| 197 | MadeChange = true; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 198 | } |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 201 | if (GlobalVariable *CoverageNamesVar = |
Xinliang David Li | 440cd70 | 2016-01-20 00:24:36 +0000 | [diff] [blame] | 202 | M.getNamedGlobal(getCoverageUnusedNamesVarName())) { |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 203 | lowerCoverageData(CoverageNamesVar); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 204 | MadeChange = true; |
| 205 | } |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 206 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 207 | if (!MadeChange) |
| 208 | return false; |
| 209 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 210 | emitVNodes(); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 211 | emitNameData(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 212 | emitRegistration(); |
| 213 | emitRuntimeHook(); |
| 214 | emitUses(); |
| 215 | emitInitialization(); |
| 216 | return true; |
| 217 | } |
| 218 | |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 219 | static Constant *getOrInsertValueProfilingCall(Module &M, |
| 220 | const TargetLibraryInfo &TLI) { |
Xinliang David Li | c767323 | 2015-11-22 00:22:07 +0000 | [diff] [blame] | 221 | LLVMContext &Ctx = M.getContext(); |
| 222 | auto *ReturnTy = Type::getVoidTy(M.getContext()); |
| 223 | Type *ParamTypes[] = { |
| 224 | #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType |
| 225 | #include "llvm/ProfileData/InstrProfData.inc" |
| 226 | }; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 227 | auto *ValueProfilingCallTy = |
Xinliang David Li | c767323 | 2015-11-22 00:22:07 +0000 | [diff] [blame] | 228 | FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false); |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 229 | Constant *Res = M.getOrInsertFunction(getInstrProfValueProfFuncName(), |
| 230 | ValueProfilingCallTy); |
| 231 | if (Function *FunRes = dyn_cast<Function>(Res)) { |
| 232 | if (auto AK = TLI.getExtAttrForI32Param(false)) |
| 233 | FunRes->addAttribute(3, AK); |
| 234 | } |
| 235 | return Res; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) { |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 239 | GlobalVariable *Name = Ind->getName(); |
| 240 | uint64_t ValueKind = Ind->getValueKind()->getZExtValue(); |
| 241 | uint64_t Index = Ind->getIndex()->getZExtValue(); |
| 242 | auto It = ProfileDataMap.find(Name); |
| 243 | if (It == ProfileDataMap.end()) { |
| 244 | PerFunctionProfileData PD; |
| 245 | PD.NumValueSites[ValueKind] = Index + 1; |
| 246 | ProfileDataMap[Name] = PD; |
| 247 | } else if (It->second.NumValueSites[ValueKind] <= Index) |
| 248 | It->second.NumValueSites[ValueKind] = Index + 1; |
| 249 | } |
| 250 | |
| 251 | void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) { |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 252 | GlobalVariable *Name = Ind->getName(); |
| 253 | auto It = ProfileDataMap.find(Name); |
| 254 | assert(It != ProfileDataMap.end() && It->second.DataVar && |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 255 | "value profiling detected in function with no counter incerement"); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 256 | |
| 257 | GlobalVariable *DataVar = It->second.DataVar; |
| 258 | uint64_t ValueKind = Ind->getValueKind()->getZExtValue(); |
| 259 | uint64_t Index = Ind->getIndex()->getZExtValue(); |
| 260 | for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind) |
| 261 | Index += It->second.NumValueSites[Kind]; |
| 262 | |
| 263 | IRBuilder<> Builder(Ind); |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 264 | Value *Args[3] = {Ind->getTargetValue(), |
| 265 | Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()), |
| 266 | Builder.getInt32(Index)}; |
Marcin Koscielnicki | 1c2bd1e | 2016-11-21 11:57:19 +0000 | [diff] [blame] | 267 | CallInst *Call = Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI), |
| 268 | Args); |
| 269 | if (auto AK = TLI->getExtAttrForI32Param(false)) |
| 270 | Call->addAttribute(3, AK); |
| 271 | Ind->replaceAllUsesWith(Call); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 272 | Ind->eraseFromParent(); |
| 273 | } |
| 274 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 275 | void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) { |
| 276 | GlobalVariable *Counters = getOrCreateRegionCounters(Inc); |
| 277 | |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 278 | IRBuilder<> Builder(Inc); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 279 | uint64_t Index = Inc->getIndex()->getZExtValue(); |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 280 | Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index); |
| 281 | Value *Count = Builder.CreateLoad(Addr, "pgocount"); |
Xinliang David Li | a754c47 | 2016-09-20 19:07:22 +0000 | [diff] [blame] | 282 | Count = Builder.CreateAdd(Count, Inc->getStep()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 283 | Inc->replaceAllUsesWith(Builder.CreateStore(Count, Addr)); |
| 284 | Inc->eraseFromParent(); |
| 285 | } |
| 286 | |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 287 | void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) { |
Xinliang David Li | 8105607 | 2016-01-07 20:05:49 +0000 | [diff] [blame] | 288 | ConstantArray *Names = |
| 289 | cast<ConstantArray>(CoverageNamesVar->getInitializer()); |
| 290 | for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) { |
| 291 | Constant *NC = Names->getOperand(I); |
| 292 | Value *V = NC->stripPointerCasts(); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 293 | assert(isa<GlobalVariable>(V) && "Missing reference to function name"); |
| 294 | GlobalVariable *Name = cast<GlobalVariable>(V); |
| 295 | |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 296 | Name->setLinkage(GlobalValue::PrivateLinkage); |
| 297 | ReferencedNames.push_back(Name); |
Vedant Kumar | 55891fc | 2017-02-14 20:03:48 +0000 | [diff] [blame^] | 298 | NC->dropAllReferences(); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 299 | } |
Vedant Kumar | 55891fc | 2017-02-14 20:03:48 +0000 | [diff] [blame^] | 300 | CoverageNamesVar->eraseFromParent(); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 303 | /// Get the name of a profiling variable for a particular function. |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 304 | static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix) { |
Xinliang David Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 305 | StringRef NamePrefix = getInstrProfNameVarPrefix(); |
| 306 | StringRef Name = Inc->getName()->getName().substr(NamePrefix.size()); |
Rong Xu | 20f5df1 | 2017-01-11 20:19:41 +0000 | [diff] [blame] | 307 | Function *F = Inc->getParent()->getParent(); |
| 308 | Module *M = F->getParent(); |
| 309 | if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) || |
| 310 | !canRenameComdatFunc(*F)) |
| 311 | return (Prefix + Name).str(); |
| 312 | uint64_t FuncHash = Inc->getHash()->getZExtValue(); |
| 313 | SmallVector<char, 24> HashPostfix; |
| 314 | if (Name.endswith((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix))) |
| 315 | return (Prefix + Name).str(); |
| 316 | return (Prefix + Name + "." + Twine(FuncHash)).str(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 319 | static inline bool shouldRecordFunctionAddr(Function *F) { |
| 320 | // Check the linkage |
| 321 | if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() && |
| 322 | !F->hasAvailableExternallyLinkage()) |
| 323 | return true; |
Rong Xu | af5aeba | 2016-04-27 21:17:30 +0000 | [diff] [blame] | 324 | // Prohibit function address recording if the function is both internal and |
| 325 | // COMDAT. This avoids the profile data variable referencing internal symbols |
| 326 | // in COMDAT. |
| 327 | if (F->hasLocalLinkage() && F->hasComdat()) |
| 328 | return false; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 329 | // Check uses of this function for other than direct calls or invokes to it. |
Xinliang David Li | 7008ce3 | 2016-06-02 16:33:41 +0000 | [diff] [blame] | 330 | // Inline virtual functions have linkeOnceODR linkage. When a key method |
| 331 | // exists, the vtable will only be emitted in the TU where the key method |
| 332 | // is defined. In a TU where vtable is not available, the function won't |
Xinliang David Li | 6c44e9e | 2016-06-03 23:02:28 +0000 | [diff] [blame] | 333 | // be 'addresstaken'. If its address is not recorded here, the profile data |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 334 | // with missing address may be picked by the linker leading to missing |
Xinliang David Li | 6c44e9e | 2016-06-03 23:02:28 +0000 | [diff] [blame] | 335 | // indirect call target info. |
| 336 | return F->hasAddressTaken() || F->hasLinkOnceLinkage(); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Xinliang David Li | 985ff20 | 2016-02-27 23:11:30 +0000 | [diff] [blame] | 339 | static inline Comdat *getOrCreateProfileComdat(Module &M, Function &F, |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 340 | InstrProfIncrementInst *Inc) { |
Xinliang David Li | 985ff20 | 2016-02-27 23:11:30 +0000 | [diff] [blame] | 341 | if (!needsComdatForCounter(F, M)) |
| 342 | return nullptr; |
| 343 | |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 344 | // COFF format requires a COMDAT section to have a key symbol with the same |
Vedant Kumar | 2d5b5d3 | 2016-02-03 23:22:43 +0000 | [diff] [blame] | 345 | // name. The linker targeting COFF also requires that the COMDAT |
Xinliang David Li | 5fe0455 | 2015-12-22 00:11:15 +0000 | [diff] [blame] | 346 | // a section is associated to must precede the associating section. For this |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 347 | // reason, we must choose the counter var's name as the name of the comdat. |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 348 | StringRef ComdatPrefix = (Triple(M.getTargetTriple()).isOSBinFormatCOFF() |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 349 | ? getInstrProfCountersVarPrefix() |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 350 | : getInstrProfComdatPrefix()); |
| 351 | return M.getOrInsertComdat(StringRef(getVarName(Inc, ComdatPrefix))); |
| 352 | } |
| 353 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 354 | static bool needsRuntimeRegistrationOfSectionRange(const Module &M) { |
| 355 | // Don't do this for Darwin. compiler-rt uses linker magic. |
| 356 | if (Triple(M.getTargetTriple()).isOSDarwin()) |
| 357 | return false; |
| 358 | |
| 359 | // Use linker script magic to get data/cnts/name start/end. |
| 360 | if (Triple(M.getTargetTriple()).isOSLinux() || |
| 361 | Triple(M.getTargetTriple()).isOSFreeBSD() || |
| 362 | Triple(M.getTargetTriple()).isPS4CPU()) |
| 363 | return false; |
| 364 | |
| 365 | return true; |
| 366 | } |
| 367 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 368 | GlobalVariable * |
| 369 | InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) { |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 370 | GlobalVariable *NamePtr = Inc->getName(); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 371 | auto It = ProfileDataMap.find(NamePtr); |
| 372 | PerFunctionProfileData PD; |
| 373 | if (It != ProfileDataMap.end()) { |
| 374 | if (It->second.RegionCounters) |
| 375 | return It->second.RegionCounters; |
| 376 | PD = It->second; |
| 377 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 378 | |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 379 | // Move the name variable to the right section. Place them in a COMDAT group |
| 380 | // if the associated function is a COMDAT. This will make sure that |
| 381 | // only one copy of counters of the COMDAT function will be emitted after |
| 382 | // linking. |
Diego Novillo | df4837b | 2015-05-27 19:34:01 +0000 | [diff] [blame] | 383 | Function *Fn = Inc->getParent()->getParent(); |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 384 | Comdat *ProfileVarsComdat = nullptr; |
Xinliang David Li | 985ff20 | 2016-02-27 23:11:30 +0000 | [diff] [blame] | 385 | ProfileVarsComdat = getOrCreateProfileComdat(*M, *Fn, Inc); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 386 | |
| 387 | uint64_t NumCounters = Inc->getNumCounters()->getZExtValue(); |
| 388 | LLVMContext &Ctx = M->getContext(); |
| 389 | ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters); |
| 390 | |
| 391 | // Create the counters variable. |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 392 | auto *CounterPtr = |
| 393 | new GlobalVariable(*M, CounterTy, false, NamePtr->getLinkage(), |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 394 | Constant::getNullValue(CounterTy), |
| 395 | getVarName(Inc, getInstrProfCountersVarPrefix())); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 396 | CounterPtr->setVisibility(NamePtr->getVisibility()); |
| 397 | CounterPtr->setSection(getCountersSection()); |
| 398 | CounterPtr->setAlignment(8); |
| 399 | CounterPtr->setComdat(ProfileVarsComdat); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 400 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 401 | auto *Int8PtrTy = Type::getInt8PtrTy(Ctx); |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 402 | // Allocate statically the array of pointers to value profile nodes for |
| 403 | // the current function. |
| 404 | Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy); |
| 405 | if (ValueProfileStaticAlloc && !needsRuntimeRegistrationOfSectionRange(*M)) { |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 406 | uint64_t NS = 0; |
| 407 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 408 | NS += PD.NumValueSites[Kind]; |
| 409 | if (NS) { |
| 410 | ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS); |
| 411 | |
| 412 | auto *ValuesVar = |
| 413 | new GlobalVariable(*M, ValuesTy, false, NamePtr->getLinkage(), |
| 414 | Constant::getNullValue(ValuesTy), |
| 415 | getVarName(Inc, getInstrProfValuesVarPrefix())); |
| 416 | ValuesVar->setVisibility(NamePtr->getVisibility()); |
| 417 | ValuesVar->setSection(getInstrProfValuesSectionName(isMachO())); |
| 418 | ValuesVar->setAlignment(8); |
| 419 | ValuesVar->setComdat(ProfileVarsComdat); |
| 420 | ValuesPtrExpr = |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 421 | ConstantExpr::getBitCast(ValuesVar, Type::getInt8PtrTy(Ctx)); |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | // Create data variable. |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 426 | auto *Int16Ty = Type::getInt16Ty(Ctx); |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 427 | auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 428 | Type *DataTypes[] = { |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 429 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType, |
| 430 | #include "llvm/ProfileData/InstrProfData.inc" |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 431 | }; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 432 | auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes)); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 433 | |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 434 | Constant *FunctionAddr = shouldRecordFunctionAddr(Fn) |
| 435 | ? ConstantExpr::getBitCast(Fn, Int8PtrTy) |
| 436 | : ConstantPointerNull::get(Int8PtrTy); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 437 | |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 438 | Constant *Int16ArrayVals[IPVK_Last + 1]; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 439 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 440 | Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]); |
| 441 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 442 | Constant *DataVals[] = { |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 443 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init, |
| 444 | #include "llvm/ProfileData/InstrProfData.inc" |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 445 | }; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 446 | auto *Data = new GlobalVariable(*M, DataTy, false, NamePtr->getLinkage(), |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 447 | ConstantStruct::get(DataTy, DataVals), |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 448 | getVarName(Inc, getInstrProfDataVarPrefix())); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 449 | Data->setVisibility(NamePtr->getVisibility()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 450 | Data->setSection(getDataSection()); |
Xinliang David Li | c7c1f85 | 2015-11-23 18:02:59 +0000 | [diff] [blame] | 451 | Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT); |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 452 | Data->setComdat(ProfileVarsComdat); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 453 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 454 | PD.RegionCounters = CounterPtr; |
| 455 | PD.DataVar = Data; |
| 456 | ProfileDataMap[NamePtr] = PD; |
| 457 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 458 | // Mark the data variable as used so that it isn't stripped out. |
| 459 | UsedVars.push_back(Data); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 460 | // Now that the linkage set by the FE has been passed to the data and counter |
| 461 | // variables, reset Name variable's linkage and visibility to private so that |
| 462 | // it can be removed later by the compiler. |
| 463 | NamePtr->setLinkage(GlobalValue::PrivateLinkage); |
| 464 | // Collect the referenced names to be used by emitNameData. |
| 465 | ReferencedNames.push_back(NamePtr); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 466 | |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 467 | return CounterPtr; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 470 | void InstrProfiling::emitVNodes() { |
| 471 | if (!ValueProfileStaticAlloc) |
| 472 | return; |
Xinliang David Li | 8da773b | 2016-05-17 20:19:03 +0000 | [diff] [blame] | 473 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 474 | // For now only support this on platforms that do |
| 475 | // not require runtime registration to discover |
| 476 | // named section start/end. |
| 477 | if (needsRuntimeRegistrationOfSectionRange(*M)) |
| 478 | return; |
Xinliang David Li | 8da773b | 2016-05-17 20:19:03 +0000 | [diff] [blame] | 479 | |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 480 | size_t TotalNS = 0; |
| 481 | for (auto &PD : ProfileDataMap) { |
| 482 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 483 | TotalNS += PD.second.NumValueSites[Kind]; |
| 484 | } |
| 485 | |
| 486 | if (!TotalNS) |
| 487 | return; |
| 488 | |
| 489 | uint64_t NumCounters = TotalNS * NumCountersPerValueSite; |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 490 | // Heuristic for small programs with very few total value sites. |
| 491 | // The default value of vp-counters-per-site is chosen based on |
| 492 | // the observation that large apps usually have a low percentage |
| 493 | // of value sites that actually have any profile data, and thus |
| 494 | // the average number of counters per site is low. For small |
| 495 | // apps with very few sites, this may not be true. Bump up the |
| 496 | // number of counters in this case. |
Xinliang David Li | e452076 | 2016-05-23 19:29:26 +0000 | [diff] [blame] | 497 | #define INSTR_PROF_MIN_VAL_COUNTS 10 |
| 498 | if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS) |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 499 | NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2); |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 500 | |
| 501 | auto &Ctx = M->getContext(); |
| 502 | Type *VNodeTypes[] = { |
| 503 | #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType, |
| 504 | #include "llvm/ProfileData/InstrProfData.inc" |
| 505 | }; |
| 506 | auto *VNodeTy = StructType::get(Ctx, makeArrayRef(VNodeTypes)); |
| 507 | |
| 508 | ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters); |
| 509 | auto *VNodesVar = new GlobalVariable( |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 510 | *M, VNodesTy, false, GlobalValue::PrivateLinkage, |
Xinliang David Li | b628dd3 | 2016-05-21 22:55:34 +0000 | [diff] [blame] | 511 | Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName()); |
| 512 | VNodesVar->setSection(getInstrProfVNodesSectionName(isMachO())); |
| 513 | UsedVars.push_back(VNodesVar); |
Xinliang David Li | 8da773b | 2016-05-17 20:19:03 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 516 | void InstrProfiling::emitNameData() { |
| 517 | std::string UncompressedData; |
| 518 | |
| 519 | if (ReferencedNames.empty()) |
| 520 | return; |
| 521 | |
| 522 | std::string CompressedNameStr; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 523 | if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr, |
Vedant Kumar | 43cba73 | 2016-05-03 16:53:17 +0000 | [diff] [blame] | 524 | DoNameCompression)) { |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 525 | report_fatal_error(toString(std::move(E)), false); |
Vedant Kumar | 43cba73 | 2016-05-03 16:53:17 +0000 | [diff] [blame] | 526 | } |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 527 | |
| 528 | auto &Ctx = M->getContext(); |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 529 | auto *NamesVal = ConstantDataArray::getString( |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 530 | Ctx, StringRef(CompressedNameStr), false); |
Eugene Zelenko | 34c2327 | 2017-01-18 00:57:48 +0000 | [diff] [blame] | 531 | NamesVar = new GlobalVariable(*M, NamesVal->getType(), true, |
| 532 | GlobalValue::PrivateLinkage, NamesVal, |
| 533 | getInstrProfNamesVarName()); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 534 | NamesSize = CompressedNameStr.size(); |
| 535 | NamesVar->setSection(getNameSection()); |
| 536 | UsedVars.push_back(NamesVar); |
Vedant Kumar | 55891fc | 2017-02-14 20:03:48 +0000 | [diff] [blame^] | 537 | |
| 538 | for (auto *NamePtr : ReferencedNames) |
| 539 | NamePtr->eraseFromParent(); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 542 | void InstrProfiling::emitRegistration() { |
Xinliang David Li | 8da773b | 2016-05-17 20:19:03 +0000 | [diff] [blame] | 543 | if (!needsRuntimeRegistrationOfSectionRange(*M)) |
Xinliang David Li | aa0592c | 2015-10-19 04:17:10 +0000 | [diff] [blame] | 544 | return; |
Xinliang David Li | 3dd8817 | 2015-10-13 18:39:48 +0000 | [diff] [blame] | 545 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 546 | // Construct the function. |
| 547 | auto *VoidTy = Type::getVoidTy(M->getContext()); |
| 548 | auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext()); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 549 | auto *Int64Ty = Type::getInt64Ty(M->getContext()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 550 | auto *RegisterFTy = FunctionType::get(VoidTy, false); |
| 551 | auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 552 | getInstrProfRegFuncsName(), M); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 553 | RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 554 | if (Options.NoRedZone) |
| 555 | RegisterF->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 556 | |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 557 | auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 558 | auto *RuntimeRegisterF = |
| 559 | Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 560 | getInstrProfRegFuncName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 561 | |
| 562 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF)); |
| 563 | for (Value *Data : UsedVars) |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 564 | if (Data != NamesVar) |
| 565 | IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy)); |
| 566 | |
| 567 | if (NamesVar) { |
| 568 | Type *ParamTypes[] = {VoidPtrTy, Int64Ty}; |
| 569 | auto *NamesRegisterTy = |
| 570 | FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false); |
| 571 | auto *NamesRegisterF = |
| 572 | Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage, |
| 573 | getInstrProfNamesRegFuncName(), M); |
| 574 | IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy), |
| 575 | IRB.getInt64(NamesSize)}); |
| 576 | } |
| 577 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 578 | IRB.CreateRetVoid(); |
| 579 | } |
| 580 | |
| 581 | void InstrProfiling::emitRuntimeHook() { |
Xinliang David Li | 7a88ad6 | 2015-10-29 04:08:31 +0000 | [diff] [blame] | 582 | // We expect the linker to be invoked with -u<hook_var> flag for linux, |
| 583 | // for which case there is no need to emit the user function. |
| 584 | if (Triple(M->getTargetTriple()).isOSLinux()) |
| 585 | return; |
| 586 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 587 | // If the module's provided its own runtime, we don't need to do anything. |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 588 | if (M->getGlobalVariable(getInstrProfRuntimeHookVarName())) |
| 589 | return; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 590 | |
| 591 | // Declare an external variable that will pull in the runtime initialization. |
| 592 | auto *Int32Ty = Type::getInt32Ty(M->getContext()); |
| 593 | auto *Var = |
| 594 | new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 595 | nullptr, getInstrProfRuntimeHookVarName()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 596 | |
| 597 | // Make a function that uses it. |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 598 | auto *User = Function::Create(FunctionType::get(Int32Ty, false), |
| 599 | GlobalValue::LinkOnceODRLinkage, |
| 600 | getInstrProfRuntimeHookVarUseFuncName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 601 | User->addFnAttr(Attribute::NoInline); |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 602 | if (Options.NoRedZone) |
| 603 | User->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 2e427d4 | 2015-02-25 22:52:20 +0000 | [diff] [blame] | 604 | User->setVisibility(GlobalValue::HiddenVisibility); |
Xinliang David Li | a228608 | 2016-05-25 17:17:51 +0000 | [diff] [blame] | 605 | if (Triple(M->getTargetTriple()).supportsCOMDAT()) |
Xinliang David Li | f4edae6 | 2016-05-24 18:47:38 +0000 | [diff] [blame] | 606 | User->setComdat(M->getOrInsertComdat(User->getName())); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 607 | |
| 608 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", User)); |
| 609 | auto *Load = IRB.CreateLoad(Var); |
| 610 | IRB.CreateRet(Load); |
| 611 | |
| 612 | // Mark the user variable as used so that it isn't stripped out. |
| 613 | UsedVars.push_back(User); |
| 614 | } |
| 615 | |
| 616 | void InstrProfiling::emitUses() { |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 617 | if (!UsedVars.empty()) |
| 618 | appendToUsed(*M, UsedVars); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | void InstrProfiling::emitInitialization() { |
Vedant Kumar | cd32eba | 2016-07-21 17:50:07 +0000 | [diff] [blame] | 622 | StringRef InstrProfileOutput = Options.InstrProfileOutput; |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 623 | |
Xinliang David Li | 6f8c504 | 2016-07-21 23:19:10 +0000 | [diff] [blame] | 624 | if (!InstrProfileOutput.empty()) { |
| 625 | // Create variable for profile name. |
| 626 | Constant *ProfileNameConst = |
| 627 | ConstantDataArray::getString(M->getContext(), InstrProfileOutput, true); |
| 628 | GlobalVariable *ProfileNameVar = new GlobalVariable( |
| 629 | *M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage, |
| 630 | ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR)); |
| 631 | Triple TT(M->getTargetTriple()); |
| 632 | if (TT.supportsCOMDAT()) { |
| 633 | ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage); |
| 634 | ProfileNameVar->setComdat(M->getOrInsertComdat( |
| 635 | StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR)))); |
| 636 | } |
| 637 | } |
| 638 | |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 639 | Constant *RegisterF = M->getFunction(getInstrProfRegFuncsName()); |
Xinliang David Li | 6f8c504 | 2016-07-21 23:19:10 +0000 | [diff] [blame] | 640 | if (!RegisterF) |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 641 | return; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 642 | |
| 643 | // Create the initialization function. |
| 644 | auto *VoidTy = Type::getVoidTy(M->getContext()); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 645 | auto *F = Function::Create(FunctionType::get(VoidTy, false), |
| 646 | GlobalValue::InternalLinkage, |
| 647 | getInstrProfInitFuncName(), M); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 648 | F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 649 | F->addFnAttr(Attribute::NoInline); |
Xinliang David Li | 69a00f0 | 2016-06-21 02:39:08 +0000 | [diff] [blame] | 650 | if (Options.NoRedZone) |
| 651 | F->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 652 | |
| 653 | // Add the basic block and the necessary calls. |
| 654 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F)); |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 655 | if (RegisterF) |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 656 | IRB.CreateCall(RegisterF, {}); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 657 | IRB.CreateRetVoid(); |
| 658 | |
| 659 | appendToGlobalCtors(*M, F, 0); |
| 660 | } |