blob: 34c2f4525eae8984b8ace99cb2e8647fb1f6ea06 [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
Diego Novillof5041ce2014-03-03 20:06:11 +000055#include "llvm/Transforms/Scalar.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"
61#include "llvm/IR/LLVMContext.h"
Diego Novillof5041ce2014-03-03 20:06:11 +000062#include "llvm/IR/Module.h"
63#include "llvm/Pass.h"
64#include "llvm/Support/CommandLine.h"
65#include "llvm/Support/Debug.h"
66#include "llvm/Support/raw_ostream.h"
67
68using namespace llvm;
69
Chandler Carruth964daaa2014-04-22 02:55:47 +000070#define DEBUG_TYPE "add-discriminators"
71
Diego Novillof5041ce2014-03-03 20:06:11 +000072namespace {
Dehao Chen7ddf7862015-10-29 21:25:33 +000073struct AddDiscriminators : public FunctionPass {
74 static char ID; // Pass identification, replacement for typeid
75 AddDiscriminators() : FunctionPass(ID) {
76 initializeAddDiscriminatorsPass(*PassRegistry::getPassRegistry());
77 }
Diego Novillof5041ce2014-03-03 20:06:11 +000078
Dehao Chen7ddf7862015-10-29 21:25:33 +000079 bool runOnFunction(Function &F) override;
80};
Diego Novillof5041ce2014-03-03 20:06:11 +000081}
82
83char AddDiscriminators::ID = 0;
84INITIALIZE_PASS_BEGIN(AddDiscriminators, "add-discriminators",
85 "Add DWARF path discriminators", false, false)
86INITIALIZE_PASS_END(AddDiscriminators, "add-discriminators",
87 "Add DWARF path discriminators", false, false)
88
89// Command line option to disable discriminator generation even in the
90// presence of debug information. This is only needed when debugging
91// debug info generation issues.
Dehao Chen7ddf7862015-10-29 21:25:33 +000092static cl::opt<bool> NoDiscriminators(
93 "no-discriminators", cl::init(false),
94 cl::desc("Disable generation of discriminator information."));
Diego Novillof5041ce2014-03-03 20:06:11 +000095
96FunctionPass *llvm::createAddDiscriminatorsPass() {
97 return new AddDiscriminators();
98}
99
100static bool hasDebugInfo(const Function &F) {
101 NamedMDNode *CUNodes = F.getParent()->getNamedMetadata("llvm.dbg.cu");
Craig Topperf40110f2014-04-25 05:29:35 +0000102 return CUNodes != nullptr;
Diego Novillof5041ce2014-03-03 20:06:11 +0000103}
104
105/// \brief Assign DWARF discriminators.
106///
107/// To assign discriminators, we examine the boundaries of every
108/// basic block and its successors. Suppose there is a basic block B1
109/// with successor B2. The last instruction I1 in B1 and the first
110/// instruction I2 in B2 are located at the same file and line number.
111/// This situation is illustrated in the following code snippet:
112///
113/// if (i < 10) x = i;
114///
115/// entry:
116/// br i1 %cmp, label %if.then, label %if.end, !dbg !10
117/// if.then:
118/// %1 = load i32* %i.addr, align 4, !dbg !10
119/// store i32 %1, i32* %x, align 4, !dbg !10
120/// br label %if.end, !dbg !10
121/// if.end:
122/// ret void, !dbg !12
123///
124/// Notice how the branch instruction in block 'entry' and all the
125/// instructions in block 'if.then' have the exact same debug location
126/// information (!dbg !10).
127///
128/// To distinguish instructions in block 'entry' from instructions in
129/// block 'if.then', we generate a new lexical block for all the
130/// instruction in block 'if.then' that share the same file and line
131/// location with the last instruction of block 'entry'.
132///
133/// This new lexical block will have the same location information as
134/// the previous one, but with a new DWARF discriminator value.
135///
136/// One of the main uses of this discriminator value is in runtime
137/// sample profilers. It allows the profiler to distinguish instructions
138/// at location !dbg !10 that execute on different basic blocks. This is
139/// important because while the predicate 'if (x < 10)' may have been
140/// executed millions of times, the assignment 'x = i' may have only
141/// executed a handful of times (meaning that the entry->if.then edge is
142/// seldom taken).
143///
144/// If we did not have discriminator information, the profiler would
145/// assign the same weight to both blocks 'entry' and 'if.then', which
146/// in turn will make it conclude that the entry->if.then edge is very
147/// hot.
148///
149/// To decide where to create new discriminator values, this function
150/// traverses the CFG and examines instruction at basic block boundaries.
151/// If the last instruction I1 of a block B1 is at the same file and line
152/// location as instruction I2 of successor B2, then it creates a new
153/// lexical block for I2 and all the instruction in B2 that share the same
154/// file and line location as I2. This new lexical block will have a
155/// different discriminator number than I1.
156bool AddDiscriminators::runOnFunction(Function &F) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000157 // If the function has debug information, but the user has disabled
158 // discriminators, do nothing.
Diego Novillo0915c042014-04-17 22:33:50 +0000159 // Simlarly, if the function has no debug info, do nothing.
160 // Finally, if this module is built with dwarf versions earlier than 4,
161 // do nothing (discriminator support is a DWARF 4 feature).
Dehao Chen7ddf7862015-10-29 21:25:33 +0000162 if (NoDiscriminators || !hasDebugInfo(F) ||
Diego Novillo0915c042014-04-17 22:33:50 +0000163 F.getParent()->getDwarfVersion() < 4)
164 return false;
Diego Novillof5041ce2014-03-03 20:06:11 +0000165
166 bool Changed = false;
167 Module *M = F.getParent();
168 LLVMContext &Ctx = M->getContext();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000169 DIBuilder Builder(*M, /*AllowUnresolved*/ false);
Diego Novillof5041ce2014-03-03 20:06:11 +0000170
171 // Traverse all the blocks looking for instructions in different
172 // blocks that are at the same file:line location.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000173 for (BasicBlock &B : F) {
174 TerminatorInst *Last = B.getTerminator();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000175 const DILocation *LastDIL = Last->getDebugLoc();
Duncan P. N. Exon Smithec819c02015-03-30 19:49:49 +0000176 if (!LastDIL)
177 continue;
Diego Novillof5041ce2014-03-03 20:06:11 +0000178
179 for (unsigned I = 0; I < Last->getNumSuccessors(); ++I) {
180 BasicBlock *Succ = Last->getSuccessor(I);
181 Instruction *First = Succ->getFirstNonPHIOrDbgOrLifetime();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000182 const DILocation *FirstDIL = First->getDebugLoc();
Duncan P. N. Exon Smithec819c02015-03-30 19:49:49 +0000183 if (!FirstDIL)
184 continue;
Diego Novillof5041ce2014-03-03 20:06:11 +0000185
186 // If the first instruction (First) of Succ is at the same file
187 // location as B's last instruction (Last), add a new
188 // discriminator for First's location and all the instructions
189 // in Succ that share the same location with First.
Duncan P. N. Exon Smith63ffa212015-04-11 01:00:47 +0000190 if (!FirstDIL->canDiscriminate(*LastDIL)) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000191 // Create a new lexical scope and compute a new discriminator
192 // number for it.
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000193 StringRef Filename = FirstDIL->getFilename();
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000194 auto *Scope = FirstDIL->getScope();
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000195 auto *File = Builder.createFile(Filename, Scope->getDirectory());
Duncan P. N. Exon Smith843237f2015-04-14 00:35:42 +0000196
197 // FIXME: Calculate the discriminator here, based on local information,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000198 // and delete DILocation::computeNewDiscriminator(). The current
Duncan P. N. Exon Smith843237f2015-04-14 00:35:42 +0000199 // solution gives different results depending on other modules in the
200 // same context. All we really need is to discriminate between
201 // FirstDIL and LastDIL -- a local map would suffice.
202 unsigned Discriminator = FirstDIL->computeNewDiscriminator();
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000203 auto *NewScope =
David Blaikie2f3f76f2014-08-21 22:45:21 +0000204 Builder.createLexicalBlockFile(Scope, File, Discriminator);
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000205 auto *NewDIL =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000206 DILocation::get(Ctx, FirstDIL->getLine(), FirstDIL->getColumn(),
Duncan P. N. Exon Smith4fd839b2015-04-14 00:34:30 +0000207 NewScope, FirstDIL->getInlinedAt());
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000208 DebugLoc newDebugLoc = NewDIL;
Diego Novillof5041ce2014-03-03 20:06:11 +0000209
210 // Attach this new debug location to First and every
211 // instruction following First that shares the same location.
212 for (BasicBlock::iterator I1(*First), E1 = Succ->end(); I1 != E1;
213 ++I1) {
Duncan P. N. Exon Smithec819c02015-03-30 19:49:49 +0000214 if (I1->getDebugLoc().get() != FirstDIL)
215 break;
Diego Novillof5041ce2014-03-03 20:06:11 +0000216 I1->setDebugLoc(newDebugLoc);
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000217 DEBUG(dbgs() << NewDIL->getFilename() << ":" << NewDIL->getLine()
218 << ":" << NewDIL->getColumn() << ":"
219 << NewDIL->getDiscriminator() << *I1 << "\n");
Diego Novillof5041ce2014-03-03 20:06:11 +0000220 }
221 DEBUG(dbgs() << "\n");
222 Changed = true;
223 }
224 }
225 }
226 return Changed;
227}