xref: /llvm-project/polly/lib/Support/ScopLocation.cpp (revision 0257a9218ba24fb9152faf267353b77c1fd17859)
1 //=== ScopLocation.cpp - Debug location for ScopDetection ----- -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Helper function for extracting region debug information.
10 //
11 //===----------------------------------------------------------------------===//
12 //
13 #include "polly/Support/ScopLocation.h"
14 #include "llvm/Analysis/RegionInfo.h"
15 #include "llvm/IR/DebugInfoMetadata.h"
16 
17 using namespace llvm;
18 
19 namespace polly {
20 
21 void getDebugLocation(const Region *R, unsigned &LineBegin, unsigned &LineEnd,
22                       std::string &FileName) {
23   LineBegin = -1;
24   LineEnd = 0;
25 
26   for (const BasicBlock *BB : R->blocks())
27     for (const Instruction &Inst : *BB) {
28       DebugLoc DL = Inst.getDebugLoc();
29       if (!DL)
30         continue;
31 
32       auto *Scope = cast<DIScope>(DL.getScope());
33 
34       if (FileName.empty())
35         FileName = Scope->getFilename().str();
36 
37       unsigned NewLine = DL.getLine();
38 
39       LineBegin = std::min(LineBegin, NewLine);
40       LineEnd = std::max(LineEnd, NewLine);
41     }
42 }
43 } // namespace polly
44