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 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Triple.h" |
| 17 | #include "llvm/IR/IRBuilder.h" |
| 18 | #include "llvm/IR/IntrinsicInst.h" |
| 19 | #include "llvm/IR/Module.h" |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 20 | #include "llvm/ProfileData/InstrProf.h" |
| 21 | #include "llvm/Transforms/Instrumentation.h" |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | #define DEBUG_TYPE "instrprof" |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | class InstrProfiling : public ModulePass { |
| 31 | public: |
| 32 | static char ID; |
| 33 | |
| 34 | InstrProfiling() : ModulePass(ID) {} |
| 35 | |
| 36 | InstrProfiling(const InstrProfOptions &Options) |
| 37 | : ModulePass(ID), Options(Options) {} |
| 38 | |
| 39 | const char *getPassName() const override { |
| 40 | return "Frontend instrumentation-based coverage lowering"; |
| 41 | } |
| 42 | |
| 43 | bool runOnModule(Module &M) override; |
| 44 | |
| 45 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 46 | AU.setPreservesCFG(); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | InstrProfOptions Options; |
| 51 | Module *M; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 52 | typedef struct PerFunctionProfileData { |
| 53 | uint32_t NumValueSites[IPVK_Last+1]; |
| 54 | GlobalVariable* RegionCounters; |
| 55 | GlobalVariable* DataVar; |
| 56 | PerFunctionProfileData() : RegionCounters(nullptr), DataVar(nullptr) { |
| 57 | memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last+1)); |
| 58 | } |
| 59 | } PerFunctionProfileData; |
| 60 | DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 61 | std::vector<Value *> UsedVars; |
| 62 | |
| 63 | bool isMachO() const { |
| 64 | return Triple(M->getTargetTriple()).isOSBinFormatMachO(); |
| 65 | } |
| 66 | |
| 67 | /// Get the section name for the counter variables. |
| 68 | StringRef getCountersSection() const { |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 69 | return getInstrProfCountersSectionName(isMachO()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /// Get the section name for the name variables. |
| 73 | StringRef getNameSection() const { |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 74 | return getInstrProfNameSectionName(isMachO()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /// Get the section name for the profile data variables. |
| 78 | StringRef getDataSection() const { |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 79 | return getInstrProfDataSectionName(isMachO()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 82 | /// Get the section name for the coverage mapping data. |
| 83 | StringRef getCoverageSection() const { |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 84 | return getInstrProfCoverageSectionName(isMachO()); |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 87 | /// Count the number of instrumented value sites for the function. |
| 88 | void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins); |
| 89 | |
| 90 | /// Replace instrprof_value_profile with a call to runtime library. |
| 91 | void lowerValueProfileInst(InstrProfValueProfileInst *Ins); |
| 92 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 93 | /// Replace instrprof_increment with an increment of the appropriate value. |
| 94 | void lowerIncrement(InstrProfIncrementInst *Inc); |
| 95 | |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 96 | /// Set up the section and uses for coverage data and its references. |
| 97 | void lowerCoverageData(GlobalVariable *CoverageData); |
| 98 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 99 | /// Get the region counters for an increment, creating them if necessary. |
| 100 | /// |
| 101 | /// If the counter array doesn't yet exist, the profile data variables |
| 102 | /// referring to them will also be created. |
| 103 | GlobalVariable *getOrCreateRegionCounters(InstrProfIncrementInst *Inc); |
| 104 | |
| 105 | /// Emit runtime registration functions for each profile data variable. |
| 106 | void emitRegistration(); |
| 107 | |
| 108 | /// Emit the necessary plumbing to pull in the runtime initialization. |
| 109 | void emitRuntimeHook(); |
| 110 | |
| 111 | /// Add uses of our data variables and runtime hook. |
| 112 | void emitUses(); |
| 113 | |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 114 | /// Create a static initializer for our data, on platforms that need it, |
| 115 | /// and for any profile output file that was specified. |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 116 | void emitInitialization(); |
| 117 | }; |
| 118 | |
| 119 | } // anonymous namespace |
| 120 | |
| 121 | char InstrProfiling::ID = 0; |
| 122 | INITIALIZE_PASS(InstrProfiling, "instrprof", |
| 123 | "Frontend instrumentation-based coverage lowering.", false, |
| 124 | false) |
| 125 | |
| 126 | ModulePass *llvm::createInstrProfilingPass(const InstrProfOptions &Options) { |
| 127 | return new InstrProfiling(Options); |
| 128 | } |
| 129 | |
| 130 | bool InstrProfiling::runOnModule(Module &M) { |
| 131 | bool MadeChange = false; |
| 132 | |
| 133 | this->M = &M; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 134 | ProfileDataMap.clear(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 135 | UsedVars.clear(); |
| 136 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 137 | // We did not know how many value sites there would be inside |
| 138 | // the instrumented function. This is counting the number of instrumented |
| 139 | // target value sites to enter it as field in the profile data variable. |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 140 | for (Function &F : M) |
| 141 | for (BasicBlock &BB : F) |
| 142 | for (auto I = BB.begin(), E = BB.end(); I != E;) |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 143 | if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I++)) |
| 144 | computeNumValueSiteCounts(Ind); |
| 145 | |
| 146 | for (Function &F : M) |
| 147 | for (BasicBlock &BB : F) |
| 148 | for (auto I = BB.begin(), E = BB.end(); I != E;) { |
| 149 | auto Instr = I++; |
| 150 | if (auto *Inc = dyn_cast<InstrProfIncrementInst>(Instr)) { |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 151 | lowerIncrement(Inc); |
| 152 | MadeChange = true; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 153 | } else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(Instr)) { |
| 154 | lowerValueProfileInst(Ind); |
| 155 | MadeChange = true; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 156 | } |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 159 | if (GlobalVariable *Coverage = |
| 160 | M.getNamedGlobal(getCoverageMappingVarName())) { |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 161 | lowerCoverageData(Coverage); |
| 162 | MadeChange = true; |
| 163 | } |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 164 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 165 | if (!MadeChange) |
| 166 | return false; |
| 167 | |
| 168 | emitRegistration(); |
| 169 | emitRuntimeHook(); |
| 170 | emitUses(); |
| 171 | emitInitialization(); |
| 172 | return true; |
| 173 | } |
| 174 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 175 | static Constant *getOrInsertValueProfilingCall(Module &M) { |
Xinliang David Li | c767323 | 2015-11-22 00:22:07 +0000 | [diff] [blame] | 176 | LLVMContext &Ctx = M.getContext(); |
| 177 | auto *ReturnTy = Type::getVoidTy(M.getContext()); |
| 178 | Type *ParamTypes[] = { |
| 179 | #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType |
| 180 | #include "llvm/ProfileData/InstrProfData.inc" |
| 181 | }; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 182 | auto *ValueProfilingCallTy = |
Xinliang David Li | c767323 | 2015-11-22 00:22:07 +0000 | [diff] [blame] | 183 | FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false); |
Xinliang David Li | 924e058 | 2015-11-22 05:42:31 +0000 | [diff] [blame] | 184 | return M.getOrInsertFunction(getInstrProfValueProfFuncName(), |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 185 | ValueProfilingCallTy); |
| 186 | } |
| 187 | |
| 188 | void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) { |
| 189 | |
| 190 | GlobalVariable *Name = Ind->getName(); |
| 191 | uint64_t ValueKind = Ind->getValueKind()->getZExtValue(); |
| 192 | uint64_t Index = Ind->getIndex()->getZExtValue(); |
| 193 | auto It = ProfileDataMap.find(Name); |
| 194 | if (It == ProfileDataMap.end()) { |
| 195 | PerFunctionProfileData PD; |
| 196 | PD.NumValueSites[ValueKind] = Index + 1; |
| 197 | ProfileDataMap[Name] = PD; |
| 198 | } else if (It->second.NumValueSites[ValueKind] <= Index) |
| 199 | It->second.NumValueSites[ValueKind] = Index + 1; |
| 200 | } |
| 201 | |
| 202 | void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) { |
| 203 | |
| 204 | GlobalVariable *Name = Ind->getName(); |
| 205 | auto It = ProfileDataMap.find(Name); |
| 206 | assert(It != ProfileDataMap.end() && It->second.DataVar && |
| 207 | "value profiling detected in function with no counter incerement"); |
| 208 | |
| 209 | GlobalVariable *DataVar = It->second.DataVar; |
| 210 | uint64_t ValueKind = Ind->getValueKind()->getZExtValue(); |
| 211 | uint64_t Index = Ind->getIndex()->getZExtValue(); |
| 212 | for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind) |
| 213 | Index += It->second.NumValueSites[Kind]; |
| 214 | |
| 215 | IRBuilder<> Builder(Ind); |
| 216 | Value* Args[3] = {Ind->getTargetValue(), |
| 217 | Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()), |
| 218 | Builder.getInt32(Index)}; |
| 219 | Ind->replaceAllUsesWith( |
| 220 | Builder.CreateCall(getOrInsertValueProfilingCall(*M), Args)); |
| 221 | Ind->eraseFromParent(); |
| 222 | } |
| 223 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 224 | void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) { |
| 225 | GlobalVariable *Counters = getOrCreateRegionCounters(Inc); |
| 226 | |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 227 | IRBuilder<> Builder(Inc); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 228 | uint64_t Index = Inc->getIndex()->getZExtValue(); |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 229 | Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index); |
| 230 | Value *Count = Builder.CreateLoad(Addr, "pgocount"); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 231 | Count = Builder.CreateAdd(Count, Builder.getInt64(1)); |
| 232 | Inc->replaceAllUsesWith(Builder.CreateStore(Count, Addr)); |
| 233 | Inc->eraseFromParent(); |
| 234 | } |
| 235 | |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 236 | void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageData) { |
| 237 | CoverageData->setSection(getCoverageSection()); |
| 238 | CoverageData->setAlignment(8); |
| 239 | |
| 240 | Constant *Init = CoverageData->getInitializer(); |
| 241 | // We're expecting { i32, i32, i32, i32, [n x { i8*, i32, i32 }], [m x i8] } |
| 242 | // for some C. If not, the frontend's given us something broken. |
| 243 | assert(Init->getNumOperands() == 6 && "bad number of fields in coverage map"); |
| 244 | assert(isa<ConstantArray>(Init->getAggregateElement(4)) && |
| 245 | "invalid function list in coverage map"); |
| 246 | ConstantArray *Records = cast<ConstantArray>(Init->getAggregateElement(4)); |
| 247 | for (unsigned I = 0, E = Records->getNumOperands(); I < E; ++I) { |
| 248 | Constant *Record = Records->getOperand(I); |
| 249 | Value *V = const_cast<Value *>(Record->getOperand(0))->stripPointerCasts(); |
| 250 | |
| 251 | assert(isa<GlobalVariable>(V) && "Missing reference to function name"); |
| 252 | GlobalVariable *Name = cast<GlobalVariable>(V); |
| 253 | |
| 254 | // If we have region counters for this name, we've already handled it. |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 255 | auto It = ProfileDataMap.find(Name); |
| 256 | if (It != ProfileDataMap.end()) |
| 257 | if (It->second.RegionCounters) |
| 258 | continue; |
Justin Bogner | d24e185 | 2015-02-11 02:52:44 +0000 | [diff] [blame] | 259 | |
| 260 | // Move the name variable to the right section. |
| 261 | Name->setSection(getNameSection()); |
| 262 | Name->setAlignment(1); |
| 263 | } |
| 264 | } |
| 265 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 266 | /// Get the name of a profiling variable for a particular function. |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 267 | static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix) { |
Xinliang David Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 268 | StringRef NamePrefix = getInstrProfNameVarPrefix(); |
| 269 | StringRef Name = Inc->getName()->getName().substr(NamePrefix.size()); |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 270 | return (Prefix + Name).str(); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 273 | static inline bool shouldRecordFunctionAddr(Function *F) { |
| 274 | // Check the linkage |
| 275 | if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() && |
| 276 | !F->hasAvailableExternallyLinkage()) |
| 277 | return true; |
| 278 | // Check uses of this function for other than direct calls or invokes to it. |
| 279 | return F->hasAddressTaken(); |
| 280 | } |
| 281 | |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 282 | static inline Comdat *getOrCreateProfileComdat(Module &M, |
| 283 | InstrProfIncrementInst *Inc) { |
| 284 | // COFF format requires a COMDAT section to have a key symbol with the same |
| 285 | // name. |
| 286 | StringRef ComdatPrefix = (Triple(M.getTargetTriple()).isOSBinFormatCOFF() |
| 287 | ? getInstrProfDataVarPrefix() |
| 288 | : getInstrProfComdatPrefix()); |
| 289 | return M.getOrInsertComdat(StringRef(getVarName(Inc, ComdatPrefix))); |
| 290 | } |
| 291 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 292 | GlobalVariable * |
| 293 | InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) { |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 294 | GlobalVariable *NamePtr = Inc->getName(); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 295 | auto It = ProfileDataMap.find(NamePtr); |
| 296 | PerFunctionProfileData PD; |
| 297 | if (It != ProfileDataMap.end()) { |
| 298 | if (It->second.RegionCounters) |
| 299 | return It->second.RegionCounters; |
| 300 | PD = It->second; |
| 301 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 302 | |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 303 | // Move the name variable to the right section. Place them in a COMDAT group |
| 304 | // if the associated function is a COMDAT. This will make sure that |
| 305 | // only one copy of counters of the COMDAT function will be emitted after |
| 306 | // linking. |
Diego Novillo | df4837b | 2015-05-27 19:34:01 +0000 | [diff] [blame] | 307 | Function *Fn = Inc->getParent()->getParent(); |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 308 | Comdat *ProfileVarsComdat = nullptr; |
| 309 | if (Fn->hasComdat()) |
Xinliang David Li | ab361ef | 2015-12-21 21:52:27 +0000 | [diff] [blame] | 310 | ProfileVarsComdat = getOrCreateProfileComdat(*M, Inc); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 311 | NamePtr->setSection(getNameSection()); |
| 312 | NamePtr->setAlignment(1); |
| 313 | NamePtr->setComdat(ProfileVarsComdat); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 314 | |
| 315 | uint64_t NumCounters = Inc->getNumCounters()->getZExtValue(); |
| 316 | LLVMContext &Ctx = M->getContext(); |
| 317 | ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters); |
| 318 | |
| 319 | // Create the counters variable. |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 320 | auto *CounterPtr = |
| 321 | new GlobalVariable(*M, CounterTy, false, NamePtr->getLinkage(), |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 322 | Constant::getNullValue(CounterTy), |
| 323 | getVarName(Inc, getInstrProfCountersVarPrefix())); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 324 | CounterPtr->setVisibility(NamePtr->getVisibility()); |
| 325 | CounterPtr->setSection(getCountersSection()); |
| 326 | CounterPtr->setAlignment(8); |
| 327 | CounterPtr->setComdat(ProfileVarsComdat); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 328 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 329 | // Create data variable. |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 330 | auto *Int8PtrTy = Type::getInt8PtrTy(Ctx); |
| 331 | auto *Int16Ty = Type::getInt16Ty(Ctx); |
| 332 | auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last+1); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 333 | Type *DataTypes[] = { |
| 334 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType, |
| 335 | #include "llvm/ProfileData/InstrProfData.inc" |
| 336 | }; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 337 | auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes)); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 338 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 339 | Constant *FunctionAddr = shouldRecordFunctionAddr(Fn) ? |
| 340 | ConstantExpr::getBitCast(Fn, Int8PtrTy) : |
| 341 | ConstantPointerNull::get(Int8PtrTy); |
| 342 | |
| 343 | Constant *Int16ArrayVals[IPVK_Last+1]; |
| 344 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 345 | Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]); |
| 346 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 347 | Constant *DataVals[] = { |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 348 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init, |
| 349 | #include "llvm/ProfileData/InstrProfData.inc" |
| 350 | }; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 351 | auto *Data = new GlobalVariable(*M, DataTy, false, NamePtr->getLinkage(), |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 352 | ConstantStruct::get(DataTy, DataVals), |
Xinliang David Li | 83bc422 | 2015-10-22 20:32:12 +0000 | [diff] [blame] | 353 | getVarName(Inc, getInstrProfDataVarPrefix())); |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 354 | Data->setVisibility(NamePtr->getVisibility()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 355 | Data->setSection(getDataSection()); |
Xinliang David Li | c7c1f85 | 2015-11-23 18:02:59 +0000 | [diff] [blame] | 356 | Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT); |
Wei Mi | 3cc9204 | 2015-09-23 22:40:45 +0000 | [diff] [blame] | 357 | Data->setComdat(ProfileVarsComdat); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 358 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 359 | PD.RegionCounters = CounterPtr; |
| 360 | PD.DataVar = Data; |
| 361 | ProfileDataMap[NamePtr] = PD; |
| 362 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 363 | // Mark the data variable as used so that it isn't stripped out. |
| 364 | UsedVars.push_back(Data); |
| 365 | |
Xinliang David Li | 192c748 | 2015-11-05 00:47:26 +0000 | [diff] [blame] | 366 | return CounterPtr; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void InstrProfiling::emitRegistration() { |
| 370 | // Don't do this for Darwin. compiler-rt uses linker magic. |
| 371 | if (Triple(M->getTargetTriple()).isOSDarwin()) |
| 372 | return; |
| 373 | |
Xinliang David Li | 3dd8817 | 2015-10-13 18:39:48 +0000 | [diff] [blame] | 374 | // Use linker script magic to get data/cnts/name start/end. |
Xinliang David Li | aa0592c | 2015-10-19 04:17:10 +0000 | [diff] [blame] | 375 | if (Triple(M->getTargetTriple()).isOSLinux() || |
| 376 | Triple(M->getTargetTriple()).isOSFreeBSD()) |
| 377 | return; |
Xinliang David Li | 3dd8817 | 2015-10-13 18:39:48 +0000 | [diff] [blame] | 378 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 379 | // Construct the function. |
| 380 | auto *VoidTy = Type::getVoidTy(M->getContext()); |
| 381 | auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext()); |
| 382 | auto *RegisterFTy = FunctionType::get(VoidTy, false); |
| 383 | auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 384 | getInstrProfRegFuncsName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 385 | RegisterF->setUnnamedAddr(true); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 386 | if (Options.NoRedZone) RegisterF->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 387 | |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 388 | auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 389 | auto *RuntimeRegisterF = |
| 390 | Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 391 | getInstrProfRegFuncName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 392 | |
| 393 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF)); |
| 394 | for (Value *Data : UsedVars) |
| 395 | IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy)); |
| 396 | IRB.CreateRetVoid(); |
| 397 | } |
| 398 | |
| 399 | void InstrProfiling::emitRuntimeHook() { |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 400 | |
Xinliang David Li | 7a88ad6 | 2015-10-29 04:08:31 +0000 | [diff] [blame] | 401 | // We expect the linker to be invoked with -u<hook_var> flag for linux, |
| 402 | // for which case there is no need to emit the user function. |
| 403 | if (Triple(M->getTargetTriple()).isOSLinux()) |
| 404 | return; |
| 405 | |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 406 | // If the module's provided its own runtime, we don't need to do anything. |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 407 | if (M->getGlobalVariable(getInstrProfRuntimeHookVarName())) return; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 408 | |
| 409 | // Declare an external variable that will pull in the runtime initialization. |
| 410 | auto *Int32Ty = Type::getInt32Ty(M->getContext()); |
| 411 | auto *Var = |
| 412 | new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage, |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 413 | nullptr, getInstrProfRuntimeHookVarName()); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 414 | |
| 415 | // Make a function that uses it. |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 416 | auto *User = Function::Create(FunctionType::get(Int32Ty, false), |
| 417 | GlobalValue::LinkOnceODRLinkage, |
| 418 | getInstrProfRuntimeHookVarUseFuncName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 419 | User->addFnAttr(Attribute::NoInline); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 420 | if (Options.NoRedZone) User->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 2e427d4 | 2015-02-25 22:52:20 +0000 | [diff] [blame] | 421 | User->setVisibility(GlobalValue::HiddenVisibility); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 422 | |
| 423 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", User)); |
| 424 | auto *Load = IRB.CreateLoad(Var); |
| 425 | IRB.CreateRet(Load); |
| 426 | |
| 427 | // Mark the user variable as used so that it isn't stripped out. |
| 428 | UsedVars.push_back(User); |
| 429 | } |
| 430 | |
| 431 | void InstrProfiling::emitUses() { |
| 432 | if (UsedVars.empty()) |
| 433 | return; |
| 434 | |
| 435 | GlobalVariable *LLVMUsed = M->getGlobalVariable("llvm.used"); |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 436 | std::vector<Constant *> MergedVars; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 437 | if (LLVMUsed) { |
| 438 | // Collect the existing members of llvm.used. |
| 439 | ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); |
| 440 | for (unsigned I = 0, E = Inits->getNumOperands(); I != E; ++I) |
| 441 | MergedVars.push_back(Inits->getOperand(I)); |
| 442 | LLVMUsed->eraseFromParent(); |
| 443 | } |
| 444 | |
| 445 | Type *i8PTy = Type::getInt8PtrTy(M->getContext()); |
| 446 | // Add uses for our data. |
| 447 | for (auto *Value : UsedVars) |
| 448 | MergedVars.push_back( |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 449 | ConstantExpr::getBitCast(cast<Constant>(Value), i8PTy)); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 450 | |
| 451 | // Recreate llvm.used. |
| 452 | ArrayType *ATy = ArrayType::get(i8PTy, MergedVars.size()); |
Diego Novillo | b3029d2 | 2015-06-04 11:45:32 +0000 | [diff] [blame] | 453 | LLVMUsed = |
| 454 | new GlobalVariable(*M, ATy, false, GlobalValue::AppendingLinkage, |
| 455 | ConstantArray::get(ATy, MergedVars), "llvm.used"); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 456 | LLVMUsed->setSection("llvm.metadata"); |
| 457 | } |
| 458 | |
| 459 | void InstrProfiling::emitInitialization() { |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 460 | std::string InstrProfileOutput = Options.InstrProfileOutput; |
| 461 | |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 462 | Constant *RegisterF = M->getFunction(getInstrProfRegFuncsName()); |
| 463 | if (!RegisterF && InstrProfileOutput.empty()) return; |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 464 | |
| 465 | // Create the initialization function. |
| 466 | auto *VoidTy = Type::getVoidTy(M->getContext()); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 467 | auto *F = Function::Create(FunctionType::get(VoidTy, false), |
| 468 | GlobalValue::InternalLinkage, |
| 469 | getInstrProfInitFuncName(), M); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 470 | F->setUnnamedAddr(true); |
| 471 | F->addFnAttr(Attribute::NoInline); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 472 | if (Options.NoRedZone) F->addFnAttr(Attribute::NoRedZone); |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 473 | |
| 474 | // Add the basic block and the necessary calls. |
| 475 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F)); |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 476 | if (RegisterF) |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 477 | IRB.CreateCall(RegisterF, {}); |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 478 | if (!InstrProfileOutput.empty()) { |
| 479 | auto *Int8PtrTy = Type::getInt8PtrTy(M->getContext()); |
| 480 | auto *SetNameTy = FunctionType::get(VoidTy, Int8PtrTy, false); |
Xinliang David Li | 8ee08b0 | 2015-10-23 04:22:58 +0000 | [diff] [blame] | 481 | auto *SetNameF = Function::Create(SetNameTy, GlobalValue::ExternalLinkage, |
| 482 | getInstrProfFileOverriderFuncName(), M); |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 483 | |
Diego Novillo | b0257c8 | 2015-06-29 20:03:46 +0000 | [diff] [blame] | 484 | // Create variable for profile name. |
Justin Bogner | ba1900c | 2015-04-30 23:49:23 +0000 | [diff] [blame] | 485 | Constant *ProfileNameConst = |
| 486 | ConstantDataArray::getString(M->getContext(), InstrProfileOutput, true); |
| 487 | GlobalVariable *ProfileName = |
| 488 | new GlobalVariable(*M, ProfileNameConst->getType(), true, |
| 489 | GlobalValue::PrivateLinkage, ProfileNameConst); |
| 490 | |
| 491 | IRB.CreateCall(SetNameF, IRB.CreatePointerCast(ProfileName, Int8PtrTy)); |
| 492 | } |
Justin Bogner | 61ba2e3 | 2014-12-08 18:02:35 +0000 | [diff] [blame] | 493 | IRB.CreateRetVoid(); |
| 494 | |
| 495 | appendToGlobalCtors(*M, F, 0); |
| 496 | } |