xref: /llvm-project/polly/lib/Support/ScopLocation.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
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/BasicBlock.h"
16 #include "llvm/IR/DebugInfo.h"
17 #include "llvm/IR/DebugLoc.h"
18 
19 using namespace llvm;
20 
21 namespace polly {
22 
23 void getDebugLocation(const Region *R, unsigned &LineBegin, unsigned &LineEnd,
24                       std::string &FileName) {
25   LineBegin = -1;
26   LineEnd = 0;
27 
28   for (const BasicBlock *BB : R->blocks())
29     for (const Instruction &Inst : *BB) {
30       DebugLoc DL = Inst.getDebugLoc();
31       if (!DL)
32         continue;
33 
34       auto *Scope = cast<DIScope>(DL.getScope());
35 
36       if (FileName.empty())
37         FileName = Scope->getFilename();
38 
39       unsigned NewLine = DL.getLine();
40 
41       LineBegin = std::min(LineBegin, NewLine);
42       LineEnd = std::max(LineEnd, NewLine);
43     }
44 }
45 } // namespace polly
46