LLVM  8.0.1
X86DiscriminateMemOps.cpp
Go to the documentation of this file.
1 //===- X86DiscriminateMemOps.cpp - Unique IDs for Mem Ops -----------------===//
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 pass aids profile-driven cache prefetch insertion by ensuring all
11 /// instructions that have a memory operand are distinguishible from each other.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "X86.h"
16 #include "X86InstrBuilder.h"
17 #include "X86InstrInfo.h"
18 #include "X86MachineFunctionInfo.h"
19 #include "X86Subtarget.h"
24 #include "llvm/Support/Debug.h"
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "x86-discriminate-memops"
29 
31  DEBUG_TYPE, cl::init(false),
32  cl::desc("Generate unique debug info for each instruction with a memory "
33  "operand. Should be enabled for profile-drived cache prefetching, "
34  "both in the build of the binary being profiled, as well as in "
35  "the build of the binary consuming the profile."),
36  cl::Hidden);
37 
38 namespace {
39 
40 using Location = std::pair<StringRef, unsigned>;
41 
42 Location diToLocation(const DILocation *Loc) {
43  return std::make_pair(Loc->getFilename(), Loc->getLine());
44 }
45 
46 /// Ensure each instruction having a memory operand has a distinct <LineNumber,
47 /// Discriminator> pair.
48 void updateDebugInfo(MachineInstr *MI, const DILocation *Loc) {
49  DebugLoc DL(Loc);
50  MI->setDebugLoc(DL);
51 }
52 
53 class X86DiscriminateMemOps : public MachineFunctionPass {
54  bool runOnMachineFunction(MachineFunction &MF) override;
55  StringRef getPassName() const override {
56  return "X86 Discriminate Memory Operands";
57  }
58 
59 public:
60  static char ID;
61 
62  /// Default construct and initialize the pass.
63  X86DiscriminateMemOps();
64 };
65 
66 } // end anonymous namespace
67 
68 //===----------------------------------------------------------------------===//
69 // Implementation
70 //===----------------------------------------------------------------------===//
71 
73 
74 /// Default construct and initialize the pass.
75 X86DiscriminateMemOps::X86DiscriminateMemOps() : MachineFunctionPass(ID) {}
76 
77 bool X86DiscriminateMemOps::runOnMachineFunction(MachineFunction &MF) {
79  return false;
80 
82  if (!FDI || !FDI->getUnit()->getDebugInfoForProfiling())
83  return false;
84 
85  // Have a default DILocation, if we find instructions with memops that don't
86  // have any debug info.
87  const DILocation *ReferenceDI =
88  DILocation::get(FDI->getContext(), FDI->getLine(), 0, FDI);
89 
90  DenseMap<Location, unsigned> MemOpDiscriminators;
91  MemOpDiscriminators[diToLocation(ReferenceDI)] = 0;
92 
93  // Figure out the largest discriminator issued for each Location. When we
94  // issue new discriminators, we can thus avoid issuing discriminators
95  // belonging to instructions that don't have memops. This isn't a requirement
96  // for the goals of this pass, however, it avoids unnecessary ambiguity.
97  for (auto &MBB : MF) {
98  for (auto &MI : MBB) {
99  const auto &DI = MI.getDebugLoc();
100  if (!DI)
101  continue;
102  Location Loc = diToLocation(DI);
103  MemOpDiscriminators[Loc] =
104  std::max(MemOpDiscriminators[Loc], DI->getBaseDiscriminator());
105  }
106  }
107 
108  // Keep track of the discriminators seen at each Location. If an instruction's
109  // DebugInfo has a Location and discriminator we've already seen, replace its
110  // discriminator with a new one, to guarantee uniqueness.
112 
113  bool Changed = false;
114  for (auto &MBB : MF) {
115  for (auto &MI : MBB) {
116  if (X86II::getMemoryOperandNo(MI.getDesc().TSFlags) < 0)
117  continue;
118  const DILocation *DI = MI.getDebugLoc();
119  if (!DI) {
120  DI = ReferenceDI;
121  }
122  Location L = diToLocation(DI);
123  DenseSet<unsigned> &Set = Seen[L];
124  const std::pair<DenseSet<unsigned>::iterator, bool> TryInsert =
125  Set.insert(DI->getBaseDiscriminator());
126  if (!TryInsert.second) {
127  unsigned BF, DF, CI = 0;
128  DILocation::decodeDiscriminator(DI->getDiscriminator(), BF, DF, CI);
129  Optional<unsigned> EncodedDiscriminator = DILocation::encodeDiscriminator(
130  MemOpDiscriminators[L] + 1, DF, CI);
131 
132  if (!EncodedDiscriminator) {
133  // FIXME(mtrofin): The assumption is that this scenario is infrequent/OK
134  // not to support. If evidence points otherwise, we can explore synthesizeing
135  // unique DIs by adding fake line numbers, or by constructing 64 bit
136  // discriminators.
137  LLVM_DEBUG(dbgs() << "Unable to create a unique discriminator "
138  "for instruction with memory operand in: "
139  << DI->getFilename() << " Line: " << DI->getLine()
140  << " Column: " << DI->getColumn()
141  << ". This is likely due to a large macro expansion. \n");
142  continue;
143  }
144  // Since we were able to encode, bump the MemOpDiscriminators.
145  ++MemOpDiscriminators[L];
146  DI = DI->cloneWithDiscriminator(EncodedDiscriminator.getValue());
147  updateDebugInfo(&MI, DI);
148  Changed = true;
149  std::pair<DenseSet<unsigned>::iterator, bool> MustInsert =
150  Set.insert(DI->getBaseDiscriminator());
151  (void)MustInsert; // Silence warning in release build.
152  assert(MustInsert.second && "New discriminator shouldn't be present in set");
153  }
154 
155  // Bump the reference DI to avoid cramming discriminators on line 0.
156  // FIXME(mtrofin): pin ReferenceDI on blocks or first instruction with DI
157  // in a block. It's more consistent than just relying on the last memop
158  // instruction we happened to see.
159  ReferenceDI = DI;
160  }
161  }
162  return Changed;
163 }
164 
166  return new X86DiscriminateMemOps();
167 }
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A debug info location.
Definition: DebugLoc.h:34
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Subprogram description.
LLVMContext & getContext() const
Definition: Metadata.h:924
Debug location.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1508
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:188
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
static cl::opt< bool > EnableDiscriminateMemops(DEBUG_TYPE, cl::init(false), cl::desc("Generate unique debug info for each instruction with a memory " "operand. Should be enabled for profile-drived cache prefetching, " "both in the build of the binary being profiled, as well as in " "the build of the binary consuming the profile."), cl::Hidden)
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
void setDebugLoc(DebugLoc dl)
Replace current source information with new such.
#define DEBUG_TYPE
Representation of each machine instruction.
Definition: MachineInstr.h:64
unsigned getBaseDiscriminator() const
Returns the base discriminator stored in the discriminator.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const DILocation * cloneWithDiscriminator(unsigned Discriminator) const
Returns a new DILocation with updated Discriminator.
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
FunctionPass * createX86DiscriminateMemOpsPass()
This pass ensures instructions featuring a memory operand have distinctive <LineNumber, Discriminator> (with respect to eachother)
#define LLVM_DEBUG(X)
Definition: Debug.h:123
This file provides the interface for the sampled PGO loader pass.
int getMemoryOperandNo(uint64_t TSFlags)
getMemoryOperandNo - The function returns the MCInst operand # for the first field of the memory oper...
Definition: X86BaseInfo.h:699