OpenASIP  2.0
LLVMUtilities.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2017 Tampere University.
3  *
4  * This file is part of TTA-Based Codesign Environment (TCE).
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file LLVMUtilities.cc
26  *
27  * Implementations of LLVM utilities.
28  *
29  * @author Henry Linjamäki 2017 (henry.linjamaki-no.spam-tut.fi)
30  * @note reting: red
31  */
32 
33 #include "LLVMUtilities.hh"
34 
35 #include "TCEString.hh"
36 #include "tce_config.h"
37 
38 #include "CompilerWarnings.hh"
39 IGNORE_COMPILER_WARNING("-Wunused-parameter")
40 
41 #include <llvm/CodeGen/MachineInstr.h>
42 #include <llvm/CodeGen/MachineOperand.h>
43 #include <llvm/IR/DebugLoc.h>
44 #include <llvm/IR/DebugInfo.h>
45 
47 
48 /**
49  * Extracts source location info from the instruction.
50  *
51  * If the instruction does not have debug info returns ("", 0);
52  */
53 std::tuple<std::string, size_t>
54 getSourceLocationInfo(const llvm::MachineInstr& mi) {
55  using namespace llvm;
56  DebugLoc dl = mi.getDebugLoc();
57  if (!dl) return std::make_tuple("", 0);
58 
59  bool hasDebugInfo = false;
60  hasDebugInfo = dl.getScope() != NULL;
61  if (!hasDebugInfo) return std::make_tuple("", 0);
62 
63  size_t sourceLineNumber = 0;
64  TCEString sourceFileName = "";
65 
66  // inspired from lib/codegen/MachineInstr.cpp
67  sourceLineNumber = dl.getLine();
68  sourceFileName = static_cast<TCEString>(
69  cast<DIScope>(dl.getScope())->getFilename().str());
70  return std::make_tuple(sourceFileName, sourceLineNumber);
71 }
72 
73 /**
74  * Returns source location as "<src-file>:<src-line>: " string if available.
75  *
76  * Otherwise return empty string.
77  */
78 std::string getSourceLocationString(const llvm::MachineInstr& mi) {
79  std::string srcFile;
80  unsigned srcLine;
81  std::tie(srcFile, srcLine) = getSourceLocationInfo(mi);
82  if (!srcFile.empty()) {
83  return srcFile + ":" + std::to_string(srcLine) + ": ";
84  }
85  return "";
86 }
87 
88 /**
89  * Decodes operands of INLINEASM instruction into more manageable struct.
90  */
92 getInlineAsmOperands(const llvm::MachineInstr& mi) {
93  using namespace llvm;
94  AsmOperandMap result;
95 
96  unsigned asmOpdPos = 0;
97  unsigned endPos = mi.getNumOperands();
98  unsigned i = InlineAsm::MIOp_FirstOperand;
99 
100  while (i < endPos) {
101  const MachineOperand& mo = mi.getOperand(i);
102  if (mo.isMetadata()) {
103  i++;
104  continue;
105  }
106  unsigned opdKind = InlineAsm::getKind(mo.getImm());
107  unsigned numAsmOpds = InlineAsm::getNumOperandRegisters(mo.getImm());
108  unsigned flagOpdBegin = i + 1;
109  unsigned flagOpdEnd = flagOpdBegin + numAsmOpds;
110  std::vector<const llvm::MachineOperand*> flagOps;
111  for (unsigned opdIdx = flagOpdBegin; opdIdx < flagOpdEnd; opdIdx++) {
112  flagOps.push_back(&mi.getOperand(opdIdx));
113  }
114  result.insert({asmOpdPos, std::make_tuple(opdKind, flagOps)});
115 
116  i += numAsmOpds + 1;
117  asmOpdPos += 1;
118  }
119 
120  return result;
121 }
122 
123 
124 
125 
getSourceLocationInfo
POP_COMPILER_DIAGS std::tuple< std::string, size_t > getSourceLocationInfo(const llvm::MachineInstr &mi)
Definition: LLVMUtilities.cc:54
llvm
Definition: InlineAsmParser.hh:49
LLVMUtilities.hh
getInlineAsmOperands
AsmOperandMap getInlineAsmOperands(const llvm::MachineInstr &mi)
Definition: LLVMUtilities.cc:92
TCEString.hh
getSourceLocationString
std::string getSourceLocationString(const llvm::MachineInstr &mi)
Definition: LLVMUtilities.cc:78
AsmOperandMap
std::map< AsmPosition, AsmOperands > AsmOperandMap
Definition: LLVMUtilities.hh:60
IGNORE_COMPILER_WARNING
#define IGNORE_COMPILER_WARNING(X)
Definition: CompilerWarnings.hh:51
TCEString
Definition: TCEString.hh:53
POP_COMPILER_DIAGS
#define POP_COMPILER_DIAGS
Definition: CompilerWarnings.hh:68
CompilerWarnings.hh