[PowerPC] Do not emit record-form rotates when record-form andi/andis suffices
[llvm-core.git] / lib / IR / DebugLoc.cpp
blob36f3e179a2c0c02db365860a7ff7522749365c14
1 //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
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 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/DebugLoc.h"
11 #include "LLVMContextImpl.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/IR/DebugInfo.h"
14 using namespace llvm;
16 //===----------------------------------------------------------------------===//
17 // DebugLoc Implementation
18 //===----------------------------------------------------------------------===//
19 DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
20 DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
22 DILocation *DebugLoc::get() const {
23 return cast_or_null<DILocation>(Loc.get());
26 unsigned DebugLoc::getLine() const {
27 assert(get() && "Expected valid DebugLoc");
28 return get()->getLine();
31 unsigned DebugLoc::getCol() const {
32 assert(get() && "Expected valid DebugLoc");
33 return get()->getColumn();
36 MDNode *DebugLoc::getScope() const {
37 assert(get() && "Expected valid DebugLoc");
38 return get()->getScope();
41 DILocation *DebugLoc::getInlinedAt() const {
42 assert(get() && "Expected valid DebugLoc");
43 return get()->getInlinedAt();
46 MDNode *DebugLoc::getInlinedAtScope() const {
47 return cast<DILocation>(Loc)->getInlinedAtScope();
50 DebugLoc DebugLoc::getFnDebugLoc() const {
51 // FIXME: Add a method on \a DILocation that does this work.
52 const MDNode *Scope = getInlinedAtScope();
53 if (auto *SP = getDISubprogram(Scope))
54 return DebugLoc::get(SP->getScopeLine(), 0, SP);
56 return DebugLoc();
59 DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
60 const MDNode *InlinedAt) {
61 // If no scope is available, this is an unknown location.
62 if (!Scope)
63 return DebugLoc();
65 return DILocation::get(Scope->getContext(), Line, Col,
66 const_cast<MDNode *>(Scope),
67 const_cast<MDNode *>(InlinedAt));
70 DebugLoc DebugLoc::appendInlinedAt(DebugLoc DL, DILocation *InlinedAt,
71 LLVMContext &Ctx,
72 DenseMap<const MDNode *, MDNode *> &Cache,
73 bool ReplaceLast) {
74 SmallVector<DILocation *, 3> InlinedAtLocations;
75 DILocation *Last = InlinedAt;
76 DILocation *CurInlinedAt = DL;
78 // Gather all the inlined-at nodes.
79 while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
80 // Skip any we've already built nodes for.
81 if (auto *Found = Cache[IA]) {
82 Last = cast<DILocation>(Found);
83 break;
86 if (ReplaceLast && !IA->getInlinedAt())
87 break;
88 InlinedAtLocations.push_back(IA);
89 CurInlinedAt = IA;
92 // Starting from the top, rebuild the nodes to point to the new inlined-at
93 // location (then rebuilding the rest of the chain behind it) and update the
94 // map of already-constructed inlined-at nodes.
95 for (const DILocation *MD : reverse(InlinedAtLocations))
96 Cache[MD] = Last = DILocation::getDistinct(
97 Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
99 return Last;
102 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
103 LLVM_DUMP_METHOD void DebugLoc::dump() const { print(dbgs()); }
104 #endif
106 void DebugLoc::print(raw_ostream &OS) const {
107 if (!Loc)
108 return;
110 // Print source line info.
111 auto *Scope = cast<DIScope>(getScope());
112 OS << Scope->getFilename();
113 OS << ':' << getLine();
114 if (getCol() != 0)
115 OS << ':' << getCol();
117 if (DebugLoc InlinedAtDL = getInlinedAt()) {
118 OS << " @[ ";
119 InlinedAtDL.print(OS);
120 OS << " ]";