blob: 4c9746b8c691e4fb9623c66a672118347312c7bf [file] [log] [blame]
Diego Novillof5041ce2014-03-03 20:06:11 +00001//===- AddDiscriminators.cpp - Insert DWARF path discriminators -----------===//
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//
10// This file adds DWARF discriminators to the IR. Path discriminators are
11// used to decide what CFG path was taken inside sub-graphs whose instructions
12// share the same line and column number information.
13//
14// The main user of this is the sample profiler. Instruction samples are
15// mapped to line number information. Since a single line may be spread
16// out over several basic blocks, discriminators add more precise location
17// for the samples.
18//
19// For example,
20//
21// 1 #define ASSERT(P)
22// 2 if (!(P))
23// 3 abort()
24// ...
25// 100 while (true) {
26// 101 ASSERT (sum < 0);
27// 102 ...
28// 130 }
29//
30// when converted to IR, this snippet looks something like:
31//
32// while.body: ; preds = %entry, %if.end
33// %0 = load i32* %sum, align 4, !dbg !15
34// %cmp = icmp slt i32 %0, 0, !dbg !15
35// br i1 %cmp, label %if.end, label %if.then, !dbg !15
36//
37// if.then: ; preds = %while.body
38// call void @abort(), !dbg !15
39// br label %if.end, !dbg !15
40//
41// Notice that all the instructions in blocks 'while.body' and 'if.then'
42// have exactly the same debug information. When this program is sampled
43// at runtime, the profiler will assume that all these instructions are
44// equally frequent. This, in turn, will consider the edge while.body->if.then
45// to be frequently taken (which is incorrect).
46//
47// By adding a discriminator value to the instructions in block 'if.then',
48// we can distinguish instructions at line 101 with discriminator 0 from
49// the instructions at line 101 with discriminator 1.
50//
51// For more details about DWARF discriminators, please visit
52// http://wiki.dwarfstd.org/index.php?title=Path_Discriminators
53//===----------------------------------------------------------------------===//
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"
Diego Novillof5041ce2014-03-03 20:06:11 +000058#include "llvm/IR/BasicBlock.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000059#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000060#include "llvm/IR/DebugInfo.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000061#include "llvm/IR/Instructions.h"
Pavel Labath978060c2015-11-16 10:40:38 +000062#include "llvm/IR/IntrinsicInst.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000063#include "llvm/IR/LLVMContext.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000064#include "llvm/Pass.h"
65#include "llvm/Support/CommandLine.h"
66#include "llvm/Support/Debug.h"
67#include "llvm/Support/raw_ostream.h"
Dehao Chen23e22782015-11-19 19:53:05 +000068#include "llvm/Transforms/Scalar.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000069
70using namespace llvm;
71
Chandler Carruth964daaa2014-04-22 02:55:47 +000072#define DEBUG_TYPE "add-discriminators"
73
Diego Novillof5041ce2014-03-03 20:06:11 +000074namespace {
Xinliang David Li1eaecef2016-06-15 21:51:30 +000075// The legacy pass of AddDiscriminators.
76struct AddDiscriminatorsLegacyPass : public FunctionPass {
Dehao Chen7ddf7862015-10-29 21:25:33 +000077 static char ID; // Pass identification, replacement for typeid
Xinliang David Li1eaecef2016-06-15 21:51:30 +000078 AddDiscriminatorsLegacyPass() : FunctionPass(ID) {
79 initializeAddDiscriminatorsLegacyPassPass(*PassRegistry::getPassRegistry());
Dehao Chen7ddf7862015-10-29 21:25:33 +000080 }
Diego Novillof5041ce2014-03-03 20:06:11 +000081
Dehao Chen7ddf7862015-10-29 21:25:33 +000082 bool runOnFunction(Function &F) override;
83};
Xinliang David Li1eaecef2016-06-15 21:51:30 +000084
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000085} // end anonymous namespace
Diego Novillof5041ce2014-03-03 20:06:11 +000086
Xinliang David Li1eaecef2016-06-15 21:51:30 +000087char AddDiscriminatorsLegacyPass::ID = 0;
88INITIALIZE_PASS_BEGIN(AddDiscriminatorsLegacyPass, "add-discriminators",
Diego Novillof5041ce2014-03-03 20:06:11 +000089 "Add DWARF path discriminators", false, false)
Xinliang David Li1eaecef2016-06-15 21:51:30 +000090INITIALIZE_PASS_END(AddDiscriminatorsLegacyPass, "add-discriminators",
Diego Novillof5041ce2014-03-03 20:06:11 +000091 "Add DWARF path discriminators", false, false)
92
93// Command line option to disable discriminator generation even in the
94// presence of debug information. This is only needed when debugging
95// debug info generation issues.
Dehao Chen7ddf7862015-10-29 21:25:33 +000096static cl::opt<bool> NoDiscriminators(
97 "no-discriminators", cl::init(false),
98 cl::desc("Disable generation of discriminator information."));
Diego Novillof5041ce2014-03-03 20:06:11 +000099
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000100// Create the legacy AddDiscriminatorsPass.
Diego Novillof5041ce2014-03-03 20:06:11 +0000101FunctionPass *llvm::createAddDiscriminatorsPass() {
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000102 return new AddDiscriminatorsLegacyPass();
Diego Novillof5041ce2014-03-03 20:06:11 +0000103}
104
Andrea Di Biagio8e269362017-04-11 19:07:30 +0000105static bool shouldHaveDiscriminator(const Instruction *I) {
106 return !isa<IntrinsicInst>(I) || isa<MemIntrinsic>(I);
107}
108
Diego Novillof5041ce2014-03-03 20:06:11 +0000109/// \brief Assign DWARF discriminators.
110///
111/// To assign discriminators, we examine the boundaries of every
112/// basic block and its successors. Suppose there is a basic block B1
113/// with successor B2. The last instruction I1 in B1 and the first
114/// instruction I2 in B2 are located at the same file and line number.
115/// This situation is illustrated in the following code snippet:
116///
117/// if (i < 10) x = i;
118///
119/// entry:
120/// br i1 %cmp, label %if.then, label %if.end, !dbg !10
121/// if.then:
122/// %1 = load i32* %i.addr, align 4, !dbg !10
123/// store i32 %1, i32* %x, align 4, !dbg !10
124/// br label %if.end, !dbg !10
125/// if.end:
126/// ret void, !dbg !12
127///
128/// Notice how the branch instruction in block 'entry' and all the
129/// instructions in block 'if.then' have the exact same debug location
130/// information (!dbg !10).
131///
132/// To distinguish instructions in block 'entry' from instructions in
133/// block 'if.then', we generate a new lexical block for all the
134/// instruction in block 'if.then' that share the same file and line
135/// location with the last instruction of block 'entry'.
136///
137/// This new lexical block will have the same location information as
138/// the previous one, but with a new DWARF discriminator value.
139///
140/// One of the main uses of this discriminator value is in runtime
141/// sample profilers. It allows the profiler to distinguish instructions
142/// at location !dbg !10 that execute on different basic blocks. This is
143/// important because while the predicate 'if (x < 10)' may have been
144/// executed millions of times, the assignment 'x = i' may have only
145/// executed a handful of times (meaning that the entry->if.then edge is
146/// seldom taken).
147///
148/// If we did not have discriminator information, the profiler would
149/// assign the same weight to both blocks 'entry' and 'if.then', which
150/// in turn will make it conclude that the entry->if.then edge is very
151/// hot.
152///
153/// To decide where to create new discriminator values, this function
154/// traverses the CFG and examines instruction at basic block boundaries.
155/// If the last instruction I1 of a block B1 is at the same file and line
156/// location as instruction I2 of successor B2, then it creates a new
157/// lexical block for I2 and all the instruction in B2 that share the same
158/// file and line location as I2. This new lexical block will have a
159/// different discriminator number than I1.
Xinliang David Li1e16d612016-06-15 22:20:56 +0000160static bool addDiscriminators(Function &F) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000161 // If the function has debug information, but the user has disabled
162 // discriminators, do nothing.
Diego Novillo0915c042014-04-17 22:33:50 +0000163 // Simlarly, if the function has no debug info, do nothing.
Dehao Chen6e0c8442016-10-07 15:21:31 +0000164 if (NoDiscriminators || !F.getSubprogram())
Diego Novillo0915c042014-04-17 22:33:50 +0000165 return false;
Diego Novillof5041ce2014-03-03 20:06:11 +0000166
167 bool Changed = false;
Diego Novillof5041ce2014-03-03 20:06:11 +0000168
Dehao Chen23e22782015-11-19 19:53:05 +0000169 typedef std::pair<StringRef, unsigned> Location;
Dehao Chene7130002016-10-26 15:48:45 +0000170 typedef DenseSet<const BasicBlock *> BBSet;
171 typedef DenseMap<Location, BBSet> LocationBBMap;
Dehao Chen939993f2016-02-29 18:59:48 +0000172 typedef DenseMap<Location, unsigned> LocationDiscriminatorMap;
Dehao Chen46f8fbb2016-04-14 18:37:18 +0000173 typedef DenseSet<Location> LocationSet;
Dehao Chen23e22782015-11-19 19:53:05 +0000174
175 LocationBBMap LBM;
Dehao Chen939993f2016-02-29 18:59:48 +0000176 LocationDiscriminatorMap LDM;
Dehao Chen23e22782015-11-19 19:53:05 +0000177
178 // Traverse all instructions in the function. If the source line location
179 // of the instruction appears in other basic block, assign a new
180 // discriminator for this instruction.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000181 for (BasicBlock &B : F) {
Dehao Chen23e22782015-11-19 19:53:05 +0000182 for (auto &I : B.getInstList()) {
Andrea Di Biagio8e269362017-04-11 19:07:30 +0000183 // Not all intrinsic calls should have a discriminator.
184 // We want to avoid a non-deterministic assignment of discriminators at
185 // different debug levels. We still allow discriminators on memory
186 // intrinsic calls because those can be early expanded by SROA into
187 // pairs of loads and stores, and the expanded load/store instructions
188 // should have a valid discriminator.
189 if (!shouldHaveDiscriminator(&I))
Duncan P. N. Exon Smithec819c02015-03-30 19:49:49 +0000190 continue;
Dehao Chen23e22782015-11-19 19:53:05 +0000191 const DILocation *DIL = I.getDebugLoc();
192 if (!DIL)
193 continue;
194 Location L = std::make_pair(DIL->getFilename(), DIL->getLine());
195 auto &BBMap = LBM[L];
Dehao Chene7130002016-10-26 15:48:45 +0000196 auto R = BBMap.insert(&B);
Dehao Chen23e22782015-11-19 19:53:05 +0000197 if (BBMap.size() == 1)
198 continue;
Adrian Prantl28d2d282016-10-24 18:23:51 +0000199 // If we could insert more than one block with the same line+file, a
Dehao Chen23e22782015-11-19 19:53:05 +0000200 // discriminator is needed to distinguish both instructions.
Dehao Chen2ca9be32016-11-08 16:32:32 +0000201 // Only the lowest 7 bits are used to represent a discriminator to fit
202 // it in 1 byte ULEB128 representation.
Dehao Chenfb02f712017-02-10 21:09:07 +0000203 unsigned Discriminator = R.second ? ++LDM[L] : LDM[L];
204 I.setDebugLoc(DIL->setBaseDiscriminator(Discriminator));
Dehao Chen23e22782015-11-19 19:53:05 +0000205 DEBUG(dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
Dehao Chen2ca9be32016-11-08 16:32:32 +0000206 << DIL->getColumn() << ":" << Discriminator << " " << I
207 << "\n");
Dehao Chen23e22782015-11-19 19:53:05 +0000208 Changed = true;
Diego Novillof5041ce2014-03-03 20:06:11 +0000209 }
210 }
Dehao Chen3656e302015-11-09 17:30:38 +0000211
212 // Traverse all instructions and assign new discriminators to call
213 // instructions with the same lineno that are in the same basic block.
214 // Sample base profile needs to distinguish different function calls within
215 // a same source line for correct profile annotation.
216 for (BasicBlock &B : F) {
Dehao Chen46f8fbb2016-04-14 18:37:18 +0000217 LocationSet CallLocations;
Dehao Chen3656e302015-11-09 17:30:38 +0000218 for (auto &I : B.getInstList()) {
219 CallInst *Current = dyn_cast<CallInst>(&I);
Andrea Di Biagio8e269362017-04-11 19:07:30 +0000220 // We bypass intrinsic calls for the following two reasons:
221 // 1) We want to avoid a non-deterministic assigment of
222 // discriminators.
223 // 2) We want to minimize the number of base discriminators used.
Dehao Chen17c6afc2016-08-05 17:56:49 +0000224 if (!Current || isa<IntrinsicInst>(&I))
Pavel Labath978060c2015-11-16 10:40:38 +0000225 continue;
226
227 DILocation *CurrentDIL = Current->getDebugLoc();
Dehao Chen34cc6762016-04-14 19:46:38 +0000228 if (!CurrentDIL)
229 continue;
Dehao Chen46f8fbb2016-04-14 18:37:18 +0000230 Location L =
231 std::make_pair(CurrentDIL->getFilename(), CurrentDIL->getLine());
232 if (!CallLocations.insert(L).second) {
Dehao Chenfb02f712017-02-10 21:09:07 +0000233 unsigned Discriminator = ++LDM[L];
234 Current->setDebugLoc(CurrentDIL->setBaseDiscriminator(Discriminator));
Dehao Chen46f8fbb2016-04-14 18:37:18 +0000235 Changed = true;
Dehao Chen3656e302015-11-09 17:30:38 +0000236 }
237 }
238 }
Diego Novillof5041ce2014-03-03 20:06:11 +0000239 return Changed;
240}
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000241
242bool AddDiscriminatorsLegacyPass::runOnFunction(Function &F) {
243 return addDiscriminators(F);
244}
245PreservedAnalyses AddDiscriminatorsPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000246 FunctionAnalysisManager &AM) {
Xinliang David Li1e16d612016-06-15 22:20:56 +0000247 if (!addDiscriminators(F))
248 return PreservedAnalyses::all();
249
250 // FIXME: should be all()
251 return PreservedAnalyses::none();
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000252}