LLVM  8.0.1
Macros | Functions | Variables
AddDiscriminators.cpp File Reference
#include "llvm/Transforms/Utils/AddDiscriminators.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils.h"
#include <utility>
Include dependency graph for AddDiscriminators.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "add-discriminators"
 

Functions

 INITIALIZE_PASS_BEGIN (AddDiscriminatorsLegacyPass, "add-discriminators", "Add DWARF path discriminators", false, false) INITIALIZE_PASS_END(AddDiscriminatorsLegacyPass
 
static bool shouldHaveDiscriminator (const Instruction *I)
 
static bool addDiscriminators (Function &F)
 Assign DWARF discriminators. More...
 

Variables

static cl::opt< boolNoDiscriminators ("no-discriminators", cl::init(false), cl::desc("Disable generation of discriminator information."))
 
add discriminators
 
add Add DWARF path false
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "add-discriminators"

Definition at line 77 of file AddDiscriminators.cpp.

Function Documentation

◆ addDiscriminators()

static bool addDiscriminators ( Function F)
static

Assign DWARF discriminators.

To assign discriminators, we examine the boundaries of every basic block and its successors. Suppose there is a basic block B1 with successor B2. The last instruction I1 in B1 and the first instruction I2 in B2 are located at the same file and line number. This situation is illustrated in the following code snippet:

  if (i < 10) x = i;

entry:
  br i1 %cmp, label %if.then, label %if.end, !dbg !10
if.then:
  %1 = load i32* %i.addr, align 4, !dbg !10
  store i32 %1, i32* %x, align 4, !dbg !10
  br label %if.end, !dbg !10
if.end:
  ret void, !dbg !12

Notice how the branch instruction in block 'entry' and all the instructions in block 'if.then' have the exact same debug location information (!dbg !10).

To distinguish instructions in block 'entry' from instructions in block 'if.then', we generate a new lexical block for all the instruction in block 'if.then' that share the same file and line location with the last instruction of block 'entry'.

This new lexical block will have the same location information as the previous one, but with a new DWARF discriminator value.

One of the main uses of this discriminator value is in runtime sample profilers. It allows the profiler to distinguish instructions at location !dbg !10 that execute on different basic blocks. This is important because while the predicate 'if (x < 10)' may have been executed millions of times, the assignment 'x = i' may have only executed a handful of times (meaning that the entry->if.then edge is seldom taken).

If we did not have discriminator information, the profiler would assign the same weight to both blocks 'entry' and 'if.then', which in turn will make it conclude that the entry->if.then edge is very hot.

To decide where to create new discriminator values, this function traverses the CFG and examines instruction at basic block boundaries. If the last instruction I1 of a block B1 is at the same file and line location as instruction I2 of successor B2, then it creates a new lexical block for I2 and all the instruction in B2 that share the same file and line location as I2. This new lexical block will have a different discriminator number than I1.

Definition at line 168 of file AddDiscriminators.cpp.

References B, llvm::dbgs(), F(), llvm::Function::getSubprogram(), I, LLVM_DEBUG, NoDiscriminators, runOnFunction(), llvm::DILocation::setBaseDiscriminator(), and shouldHaveDiscriminator().

Referenced by llvm::AddDiscriminatorsPass::run().

◆ INITIALIZE_PASS_BEGIN()

INITIALIZE_PASS_BEGIN ( AddDiscriminatorsLegacyPass  ,
"add-discriminators ,
"Add DWARF path discriminators ,
false  ,
false   
)

◆ shouldHaveDiscriminator()

static bool shouldHaveDiscriminator ( const Instruction I)
static

Definition at line 113 of file AddDiscriminators.cpp.

References I.

Referenced by addDiscriminators().

Variable Documentation

◆ discriminators

add Add DWARF path discriminators

Definition at line 105 of file AddDiscriminators.cpp.

◆ false

add Add DWARF path false

Definition at line 105 of file AddDiscriminators.cpp.

◆ NoDiscriminators

cl::opt<bool> NoDiscriminators("no-discriminators", cl::init(false), cl::desc("Disable generation of discriminator information."))
static

Referenced by addDiscriminators().