blob: e0f58606d63fc6ff75dd6377f11af78d4a726c16 [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
Dehao Chen23e22782015-11-19 19:53:05 +000055#include "llvm/ADT/DenseMap.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000056#include "llvm/IR/BasicBlock.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000057#include "llvm/IR/Constants.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000058#include "llvm/IR/DIBuilder.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000059#include "llvm/IR/DebugInfo.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000060#include "llvm/IR/Instructions.h"
Pavel Labath978060c2015-11-16 10:40:38 +000061#include "llvm/IR/IntrinsicInst.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000062#include "llvm/IR/LLVMContext.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000063#include "llvm/IR/Module.h"
64#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 {
Dehao Chen7ddf7862015-10-29 21:25:33 +000075struct AddDiscriminators : public FunctionPass {
76 static char ID; // Pass identification, replacement for typeid
77 AddDiscriminators() : FunctionPass(ID) {
78 initializeAddDiscriminatorsPass(*PassRegistry::getPassRegistry());
79 }
Diego Novillof5041ce2014-03-03 20:06:11 +000080
Dehao Chen7ddf7862015-10-29 21:25:33 +000081 bool runOnFunction(Function &F) override;
82};
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000083} // end anonymous namespace
Diego Novillof5041ce2014-03-03 20:06:11 +000084
85char AddDiscriminators::ID = 0;
86INITIALIZE_PASS_BEGIN(AddDiscriminators, "add-discriminators",
87 "Add DWARF path discriminators", false, false)
88INITIALIZE_PASS_END(AddDiscriminators, "add-discriminators",
89 "Add DWARF path discriminators", false, false)
90
91// Command line option to disable discriminator generation even in the
92// presence of debug information. This is only needed when debugging
93// debug info generation issues.
Dehao Chen7ddf7862015-10-29 21:25:33 +000094static cl::opt<bool> NoDiscriminators(
95 "no-discriminators", cl::init(false),
96 cl::desc("Disable generation of discriminator information."));
Diego Novillof5041ce2014-03-03 20:06:11 +000097
98FunctionPass *llvm::createAddDiscriminatorsPass() {
99 return new AddDiscriminators();
100}
101
102static bool hasDebugInfo(const Function &F) {
Diego Novillo0354a9f62015-11-11 17:54:37 +0000103 DISubprogram *S = getDISubprogram(&F);
104 return S != nullptr;
Diego Novillof5041ce2014-03-03 20:06:11 +0000105}
106
107/// \brief Assign DWARF discriminators.
108///
109/// To assign discriminators, we examine the boundaries of every
110/// basic block and its successors. Suppose there is a basic block B1
111/// with successor B2. The last instruction I1 in B1 and the first
112/// instruction I2 in B2 are located at the same file and line number.
113/// This situation is illustrated in the following code snippet:
114///
115/// if (i < 10) x = i;
116///
117/// entry:
118/// br i1 %cmp, label %if.then, label %if.end, !dbg !10
119/// if.then:
120/// %1 = load i32* %i.addr, align 4, !dbg !10
121/// store i32 %1, i32* %x, align 4, !dbg !10
122/// br label %if.end, !dbg !10
123/// if.end:
124/// ret void, !dbg !12
125///
126/// Notice how the branch instruction in block 'entry' and all the
127/// instructions in block 'if.then' have the exact same debug location
128/// information (!dbg !10).
129///
130/// To distinguish instructions in block 'entry' from instructions in
131/// block 'if.then', we generate a new lexical block for all the
132/// instruction in block 'if.then' that share the same file and line
133/// location with the last instruction of block 'entry'.
134///
135/// This new lexical block will have the same location information as
136/// the previous one, but with a new DWARF discriminator value.
137///
138/// One of the main uses of this discriminator value is in runtime
139/// sample profilers. It allows the profiler to distinguish instructions
140/// at location !dbg !10 that execute on different basic blocks. This is
141/// important because while the predicate 'if (x < 10)' may have been
142/// executed millions of times, the assignment 'x = i' may have only
143/// executed a handful of times (meaning that the entry->if.then edge is
144/// seldom taken).
145///
146/// If we did not have discriminator information, the profiler would
147/// assign the same weight to both blocks 'entry' and 'if.then', which
148/// in turn will make it conclude that the entry->if.then edge is very
149/// hot.
150///
151/// To decide where to create new discriminator values, this function
152/// traverses the CFG and examines instruction at basic block boundaries.
153/// If the last instruction I1 of a block B1 is at the same file and line
154/// location as instruction I2 of successor B2, then it creates a new
155/// lexical block for I2 and all the instruction in B2 that share the same
156/// file and line location as I2. This new lexical block will have a
157/// different discriminator number than I1.
158bool AddDiscriminators::runOnFunction(Function &F) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000159 // If the function has debug information, but the user has disabled
160 // discriminators, do nothing.
Diego Novillo0915c042014-04-17 22:33:50 +0000161 // Simlarly, if the function has no debug info, do nothing.
162 // Finally, if this module is built with dwarf versions earlier than 4,
163 // do nothing (discriminator support is a DWARF 4 feature).
Dehao Chen7ddf7862015-10-29 21:25:33 +0000164 if (NoDiscriminators || !hasDebugInfo(F) ||
Diego Novillo0915c042014-04-17 22:33:50 +0000165 F.getParent()->getDwarfVersion() < 4)
166 return false;
Diego Novillof5041ce2014-03-03 20:06:11 +0000167
168 bool Changed = false;
169 Module *M = F.getParent();
170 LLVMContext &Ctx = M->getContext();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000171 DIBuilder Builder(*M, /*AllowUnresolved*/ false);
Diego Novillof5041ce2014-03-03 20:06:11 +0000172
Dehao Chen23e22782015-11-19 19:53:05 +0000173 typedef std::pair<StringRef, unsigned> Location;
174 typedef DenseMap<const BasicBlock *, Metadata *> BBScopeMap;
175 typedef DenseMap<Location, BBScopeMap> LocationBBMap;
Dehao Chen939993f2016-02-29 18:59:48 +0000176 typedef DenseMap<Location, unsigned> LocationDiscriminatorMap;
Dehao Chen23e22782015-11-19 19:53:05 +0000177
178 LocationBBMap LBM;
Dehao Chen939993f2016-02-29 18:59:48 +0000179 LocationDiscriminatorMap LDM;
Dehao Chen23e22782015-11-19 19:53:05 +0000180
181 // Traverse all instructions in the function. If the source line location
182 // of the instruction appears in other basic block, assign a new
183 // discriminator for this instruction.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000184 for (BasicBlock &B : F) {
Dehao Chen23e22782015-11-19 19:53:05 +0000185 for (auto &I : B.getInstList()) {
186 if (isa<DbgInfoIntrinsic>(&I))
Duncan P. N. Exon Smithec819c02015-03-30 19:49:49 +0000187 continue;
Dehao Chen23e22782015-11-19 19:53:05 +0000188 const DILocation *DIL = I.getDebugLoc();
189 if (!DIL)
190 continue;
191 Location L = std::make_pair(DIL->getFilename(), DIL->getLine());
192 auto &BBMap = LBM[L];
193 auto R = BBMap.insert(std::make_pair(&B, (Metadata *)nullptr));
194 if (BBMap.size() == 1)
195 continue;
196 bool InsertSuccess = R.second;
197 Metadata *&NewScope = R.first->second;
198 // If we could insert a different block in the same location, a
199 // discriminator is needed to distinguish both instructions.
200 if (InsertSuccess) {
201 auto *Scope = DIL->getScope();
202 auto *File =
203 Builder.createFile(DIL->getFilename(), Scope->getDirectory());
Dehao Chen939993f2016-02-29 18:59:48 +0000204 NewScope = Builder.createLexicalBlockFile(Scope, File, ++LDM[L]);
Diego Novillof5041ce2014-03-03 20:06:11 +0000205 }
Dehao Chen23e22782015-11-19 19:53:05 +0000206 I.setDebugLoc(DILocation::get(Ctx, DIL->getLine(), DIL->getColumn(),
207 NewScope, DIL->getInlinedAt()));
208 DEBUG(dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
Dehao Chen014fb552015-11-19 20:29:27 +0000209 << DIL->getColumn() << ":"
210 << dyn_cast<DILexicalBlockFile>(NewScope)->getDiscriminator()
Dehao Chen23e22782015-11-19 19:53:05 +0000211 << I << "\n");
212 Changed = true;
Diego Novillof5041ce2014-03-03 20:06:11 +0000213 }
214 }
Dehao Chen3656e302015-11-09 17:30:38 +0000215
216 // Traverse all instructions and assign new discriminators to call
217 // instructions with the same lineno that are in the same basic block.
218 // Sample base profile needs to distinguish different function calls within
219 // a same source line for correct profile annotation.
220 for (BasicBlock &B : F) {
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000221 const DILocation *FirstDIL = nullptr;
Dehao Chen3656e302015-11-09 17:30:38 +0000222 for (auto &I : B.getInstList()) {
223 CallInst *Current = dyn_cast<CallInst>(&I);
Pavel Labath978060c2015-11-16 10:40:38 +0000224 if (!Current || isa<DbgInfoIntrinsic>(&I))
225 continue;
226
227 DILocation *CurrentDIL = Current->getDebugLoc();
228 if (FirstDIL) {
229 if (CurrentDIL && CurrentDIL->getLine() == FirstDIL->getLine() &&
230 CurrentDIL->getFilename() == FirstDIL->getFilename()) {
231 auto *Scope = FirstDIL->getScope();
232 auto *File = Builder.createFile(FirstDIL->getFilename(),
233 Scope->getDirectory());
Dehao Chen939993f2016-02-29 18:59:48 +0000234 Location L =
235 std::make_pair(FirstDIL->getFilename(), FirstDIL->getLine());
236 auto *NewScope =
237 Builder.createLexicalBlockFile(Scope, File, ++LDM[L]);
Pavel Labath978060c2015-11-16 10:40:38 +0000238 Current->setDebugLoc(DILocation::get(
239 Ctx, CurrentDIL->getLine(), CurrentDIL->getColumn(), NewScope,
240 CurrentDIL->getInlinedAt()));
241 Changed = true;
Dehao Chen3656e302015-11-09 17:30:38 +0000242 } else {
243 FirstDIL = CurrentDIL;
244 }
Pavel Labath978060c2015-11-16 10:40:38 +0000245 } else {
246 FirstDIL = CurrentDIL;
Dehao Chen3656e302015-11-09 17:30:38 +0000247 }
248 }
249 }
Diego Novillof5041ce2014-03-03 20:06:11 +0000250 return Changed;
251}