blob: ee0973002c47de5f8093cde61923292a623cd80f [file] [log] [blame]
Diego Novillof5041ce2014-03-03 20:06:11 +00001//===- AddDiscriminators.cpp - Insert DWARF path discriminators -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Diego Novillof5041ce2014-03-03 20:06:11 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file adds DWARF discriminators to the IR. Path discriminators are
10// used to decide what CFG path was taken inside sub-graphs whose instructions
11// share the same line and column number information.
12//
13// The main user of this is the sample profiler. Instruction samples are
14// mapped to line number information. Since a single line may be spread
15// out over several basic blocks, discriminators add more precise location
16// for the samples.
17//
18// For example,
19//
20// 1 #define ASSERT(P)
21// 2 if (!(P))
22// 3 abort()
23// ...
24// 100 while (true) {
25// 101 ASSERT (sum < 0);
26// 102 ...
27// 130 }
28//
29// when converted to IR, this snippet looks something like:
30//
31// while.body: ; preds = %entry, %if.end
32// %0 = load i32* %sum, align 4, !dbg !15
33// %cmp = icmp slt i32 %0, 0, !dbg !15
34// br i1 %cmp, label %if.end, label %if.then, !dbg !15
35//
36// if.then: ; preds = %while.body
37// call void @abort(), !dbg !15
38// br label %if.end, !dbg !15
39//
40// Notice that all the instructions in blocks 'while.body' and 'if.then'
41// have exactly the same debug information. When this program is sampled
42// at runtime, the profiler will assume that all these instructions are
43// equally frequent. This, in turn, will consider the edge while.body->if.then
44// to be frequently taken (which is incorrect).
45//
46// By adding a discriminator value to the instructions in block 'if.then',
47// we can distinguish instructions at line 101 with discriminator 0 from
48// the instructions at line 101 with discriminator 1.
49//
50// For more details about DWARF discriminators, please visit
51// http://wiki.dwarfstd.org/index.php?title=Path_Discriminators
Eugene Zelenko6cadde72017-10-17 21:27:42 +000052//
Diego Novillof5041ce2014-03-03 20:06:11 +000053//===----------------------------------------------------------------------===//
54
Xinliang David Li1eaecef2016-06-15 21:51:30 +000055#include "llvm/Transforms/Utils/AddDiscriminators.h"
Dehao Chen23e22782015-11-19 19:53:05 +000056#include "llvm/ADT/DenseMap.h"
Dehao Chen46f8fbb2016-04-14 18:37:18 +000057#include "llvm/ADT/DenseSet.h"
Eugene Zelenko6cadde72017-10-17 21:27:42 +000058#include "llvm/ADT/StringRef.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000059#include "llvm/IR/BasicBlock.h"
Eugene Zelenko6cadde72017-10-17 21:27:42 +000060#include "llvm/IR/DebugInfoMetadata.h"
61#include "llvm/IR/Function.h"
62#include "llvm/IR/Instruction.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000063#include "llvm/IR/Instructions.h"
Pavel Labath978060c2015-11-16 10:40:38 +000064#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko6cadde72017-10-17 21:27:42 +000065#include "llvm/IR/PassManager.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000066#include "llvm/Pass.h"
Eugene Zelenko6cadde72017-10-17 21:27:42 +000067#include "llvm/Support/Casting.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000068#include "llvm/Support/CommandLine.h"
69#include "llvm/Support/Debug.h"
70#include "llvm/Support/raw_ostream.h"
David Blaikiea373d182018-03-28 17:44:36 +000071#include "llvm/Transforms/Utils.h"
Eugene Zelenko6cadde72017-10-17 21:27:42 +000072#include <utility>
Diego Novillof5041ce2014-03-03 20:06:11 +000073
74using namespace llvm;
75
Chandler Carruth964daaa2014-04-22 02:55:47 +000076#define DEBUG_TYPE "add-discriminators"
77
Eugene Zelenko6cadde72017-10-17 21:27:42 +000078// Command line option to disable discriminator generation even in the
79// presence of debug information. This is only needed when debugging
80// debug info generation issues.
81static cl::opt<bool> NoDiscriminators(
82 "no-discriminators", cl::init(false),
83 cl::desc("Disable generation of discriminator information."));
84
Diego Novillof5041ce2014-03-03 20:06:11 +000085namespace {
Eugene Zelenko6cadde72017-10-17 21:27:42 +000086
Xinliang David Li1eaecef2016-06-15 21:51:30 +000087// The legacy pass of AddDiscriminators.
88struct AddDiscriminatorsLegacyPass : public FunctionPass {
Dehao Chen7ddf7862015-10-29 21:25:33 +000089 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko6cadde72017-10-17 21:27:42 +000090
Xinliang David Li1eaecef2016-06-15 21:51:30 +000091 AddDiscriminatorsLegacyPass() : FunctionPass(ID) {
92 initializeAddDiscriminatorsLegacyPassPass(*PassRegistry::getPassRegistry());
Dehao Chen7ddf7862015-10-29 21:25:33 +000093 }
Diego Novillof5041ce2014-03-03 20:06:11 +000094
Dehao Chen7ddf7862015-10-29 21:25:33 +000095 bool runOnFunction(Function &F) override;
96};
Xinliang David Li1eaecef2016-06-15 21:51:30 +000097
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000098} // end anonymous namespace
Diego Novillof5041ce2014-03-03 20:06:11 +000099
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000100char AddDiscriminatorsLegacyPass::ID = 0;
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000101
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000102INITIALIZE_PASS_BEGIN(AddDiscriminatorsLegacyPass, "add-discriminators",
Diego Novillof5041ce2014-03-03 20:06:11 +0000103 "Add DWARF path discriminators", false, false)
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000104INITIALIZE_PASS_END(AddDiscriminatorsLegacyPass, "add-discriminators",
Diego Novillof5041ce2014-03-03 20:06:11 +0000105 "Add DWARF path discriminators", false, false)
106
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000107// Create the legacy AddDiscriminatorsPass.
Diego Novillof5041ce2014-03-03 20:06:11 +0000108FunctionPass *llvm::createAddDiscriminatorsPass() {
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000109 return new AddDiscriminatorsLegacyPass();
Diego Novillof5041ce2014-03-03 20:06:11 +0000110}
111
Andrea Di Biagio8e269362017-04-11 19:07:30 +0000112static bool shouldHaveDiscriminator(const Instruction *I) {
113 return !isa<IntrinsicInst>(I) || isa<MemIntrinsic>(I);
114}
115
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000116/// Assign DWARF discriminators.
Diego Novillof5041ce2014-03-03 20:06:11 +0000117///
118/// To assign discriminators, we examine the boundaries of every
119/// basic block and its successors. Suppose there is a basic block B1
120/// with successor B2. The last instruction I1 in B1 and the first
121/// instruction I2 in B2 are located at the same file and line number.
122/// This situation is illustrated in the following code snippet:
123///
124/// if (i < 10) x = i;
125///
126/// entry:
127/// br i1 %cmp, label %if.then, label %if.end, !dbg !10
128/// if.then:
129/// %1 = load i32* %i.addr, align 4, !dbg !10
130/// store i32 %1, i32* %x, align 4, !dbg !10
131/// br label %if.end, !dbg !10
132/// if.end:
133/// ret void, !dbg !12
134///
135/// Notice how the branch instruction in block 'entry' and all the
136/// instructions in block 'if.then' have the exact same debug location
137/// information (!dbg !10).
138///
139/// To distinguish instructions in block 'entry' from instructions in
140/// block 'if.then', we generate a new lexical block for all the
141/// instruction in block 'if.then' that share the same file and line
142/// location with the last instruction of block 'entry'.
143///
144/// This new lexical block will have the same location information as
145/// the previous one, but with a new DWARF discriminator value.
146///
147/// One of the main uses of this discriminator value is in runtime
148/// sample profilers. It allows the profiler to distinguish instructions
149/// at location !dbg !10 that execute on different basic blocks. This is
150/// important because while the predicate 'if (x < 10)' may have been
151/// executed millions of times, the assignment 'x = i' may have only
152/// executed a handful of times (meaning that the entry->if.then edge is
153/// seldom taken).
154///
155/// If we did not have discriminator information, the profiler would
156/// assign the same weight to both blocks 'entry' and 'if.then', which
157/// in turn will make it conclude that the entry->if.then edge is very
158/// hot.
159///
160/// To decide where to create new discriminator values, this function
161/// traverses the CFG and examines instruction at basic block boundaries.
162/// If the last instruction I1 of a block B1 is at the same file and line
163/// location as instruction I2 of successor B2, then it creates a new
164/// lexical block for I2 and all the instruction in B2 that share the same
165/// file and line location as I2. This new lexical block will have a
166/// different discriminator number than I1.
Xinliang David Li1e16d612016-06-15 22:20:56 +0000167static bool addDiscriminators(Function &F) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000168 // If the function has debug information, but the user has disabled
169 // discriminators, do nothing.
Diego Novillo0915c042014-04-17 22:33:50 +0000170 // Simlarly, if the function has no debug info, do nothing.
Dehao Chen6e0c8442016-10-07 15:21:31 +0000171 if (NoDiscriminators || !F.getSubprogram())
Diego Novillo0915c042014-04-17 22:33:50 +0000172 return false;
Diego Novillof5041ce2014-03-03 20:06:11 +0000173
174 bool Changed = false;
Diego Novillof5041ce2014-03-03 20:06:11 +0000175
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000176 using Location = std::pair<StringRef, unsigned>;
177 using BBSet = DenseSet<const BasicBlock *>;
178 using LocationBBMap = DenseMap<Location, BBSet>;
179 using LocationDiscriminatorMap = DenseMap<Location, unsigned>;
180 using LocationSet = DenseSet<Location>;
Dehao Chen23e22782015-11-19 19:53:05 +0000181
182 LocationBBMap LBM;
Dehao Chen939993f2016-02-29 18:59:48 +0000183 LocationDiscriminatorMap LDM;
Dehao Chen23e22782015-11-19 19:53:05 +0000184
185 // Traverse all instructions in the function. If the source line location
186 // of the instruction appears in other basic block, assign a new
187 // discriminator for this instruction.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000188 for (BasicBlock &B : F) {
Dehao Chen23e22782015-11-19 19:53:05 +0000189 for (auto &I : B.getInstList()) {
Andrea Di Biagio8e269362017-04-11 19:07:30 +0000190 // Not all intrinsic calls should have a discriminator.
191 // We want to avoid a non-deterministic assignment of discriminators at
192 // different debug levels. We still allow discriminators on memory
193 // intrinsic calls because those can be early expanded by SROA into
194 // pairs of loads and stores, and the expanded load/store instructions
195 // should have a valid discriminator.
196 if (!shouldHaveDiscriminator(&I))
Duncan P. N. Exon Smithec819c02015-03-30 19:49:49 +0000197 continue;
Dehao Chen23e22782015-11-19 19:53:05 +0000198 const DILocation *DIL = I.getDebugLoc();
199 if (!DIL)
200 continue;
201 Location L = std::make_pair(DIL->getFilename(), DIL->getLine());
202 auto &BBMap = LBM[L];
Dehao Chene7130002016-10-26 15:48:45 +0000203 auto R = BBMap.insert(&B);
Dehao Chen23e22782015-11-19 19:53:05 +0000204 if (BBMap.size() == 1)
205 continue;
Adrian Prantl28d2d282016-10-24 18:23:51 +0000206 // If we could insert more than one block with the same line+file, a
Dehao Chen23e22782015-11-19 19:53:05 +0000207 // discriminator is needed to distinguish both instructions.
Dehao Chen2ca9be32016-11-08 16:32:32 +0000208 // Only the lowest 7 bits are used to represent a discriminator to fit
209 // it in 1 byte ULEB128 representation.
Dehao Chenfb02f712017-02-10 21:09:07 +0000210 unsigned Discriminator = R.second ? ++LDM[L] : LDM[L];
Mircea Trofinec026302019-01-24 00:10:25 +0000211 auto NewDIL = DIL->cloneWithBaseDiscriminator(Discriminator);
Mircea Trofinb53eeb62018-12-21 22:48:50 +0000212 if (!NewDIL) {
213 LLVM_DEBUG(dbgs() << "Could not encode discriminator: "
214 << DIL->getFilename() << ":" << DIL->getLine() << ":"
215 << DIL->getColumn() << ":" << Discriminator << " "
216 << I << "\n");
217 } else {
218 I.setDebugLoc(NewDIL.getValue());
219 LLVM_DEBUG(dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
220 << DIL->getColumn() << ":" << Discriminator << " " << I
221 << "\n");
222 }
Dehao Chen23e22782015-11-19 19:53:05 +0000223 Changed = true;
Diego Novillof5041ce2014-03-03 20:06:11 +0000224 }
225 }
Dehao Chen3656e302015-11-09 17:30:38 +0000226
227 // Traverse all instructions and assign new discriminators to call
228 // instructions with the same lineno that are in the same basic block.
229 // Sample base profile needs to distinguish different function calls within
230 // a same source line for correct profile annotation.
231 for (BasicBlock &B : F) {
Dehao Chen46f8fbb2016-04-14 18:37:18 +0000232 LocationSet CallLocations;
Dehao Chen3656e302015-11-09 17:30:38 +0000233 for (auto &I : B.getInstList()) {
Andrea Di Biagio8e269362017-04-11 19:07:30 +0000234 // We bypass intrinsic calls for the following two reasons:
235 // 1) We want to avoid a non-deterministic assigment of
236 // discriminators.
237 // 2) We want to minimize the number of base discriminators used.
David Callahand129d3e2019-01-15 21:26:51 +0000238 if (!isa<InvokeInst>(I) && (!isa<CallInst>(I) || isa<IntrinsicInst>(I)))
Pavel Labath978060c2015-11-16 10:40:38 +0000239 continue;
240
David Callahand129d3e2019-01-15 21:26:51 +0000241 DILocation *CurrentDIL = I.getDebugLoc();
Dehao Chen34cc6762016-04-14 19:46:38 +0000242 if (!CurrentDIL)
243 continue;
Dehao Chen46f8fbb2016-04-14 18:37:18 +0000244 Location L =
245 std::make_pair(CurrentDIL->getFilename(), CurrentDIL->getLine());
246 if (!CallLocations.insert(L).second) {
Dehao Chenfb02f712017-02-10 21:09:07 +0000247 unsigned Discriminator = ++LDM[L];
Mircea Trofinec026302019-01-24 00:10:25 +0000248 auto NewDIL = CurrentDIL->cloneWithBaseDiscriminator(Discriminator);
Mircea Trofinb53eeb62018-12-21 22:48:50 +0000249 if (!NewDIL) {
250 LLVM_DEBUG(dbgs()
251 << "Could not encode discriminator: "
252 << CurrentDIL->getFilename() << ":"
253 << CurrentDIL->getLine() << ":" << CurrentDIL->getColumn()
254 << ":" << Discriminator << " " << I << "\n");
255 } else {
David Callahand129d3e2019-01-15 21:26:51 +0000256 I.setDebugLoc(NewDIL.getValue());
Mircea Trofinb53eeb62018-12-21 22:48:50 +0000257 Changed = true;
258 }
Dehao Chen3656e302015-11-09 17:30:38 +0000259 }
260 }
261 }
Diego Novillof5041ce2014-03-03 20:06:11 +0000262 return Changed;
263}
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000264
265bool AddDiscriminatorsLegacyPass::runOnFunction(Function &F) {
266 return addDiscriminators(F);
267}
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000268
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000269PreservedAnalyses AddDiscriminatorsPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000270 FunctionAnalysisManager &AM) {
Xinliang David Li1e16d612016-06-15 22:20:56 +0000271 if (!addDiscriminators(F))
272 return PreservedAnalyses::all();
273
274 // FIXME: should be all()
275 return PreservedAnalyses::none();
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000276}