LLVM 22.0.0git
LVSort.cpp
Go to the documentation of this file.
1//===-- LVSort.cpp --------------------------------------------------------===//
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// Support for LVObject sorting.
10//
11//===----------------------------------------------------------------------===//
12
15#include <string>
16
17using namespace llvm;
18using namespace llvm::logicalview;
19
20#define DEBUG_TYPE "Sort"
21
22//===----------------------------------------------------------------------===//
23// Callback functions to sort objects.
24//===----------------------------------------------------------------------===//
25// Callback comparator based on ID.
27 const LVObject *RHS) {
28 return LHS->getID() < RHS->getID();
29}
30
31// Callback comparator based on kind.
33 const LVObject *RHS) {
34 return std::string(LHS->kind()) < std::string(RHS->kind());
35}
36
37// Callback comparator based on line.
39 const LVObject *RHS) {
40 return LHS->getLineNumber() < RHS->getLineNumber();
41}
42
43// Callback comparator based on name.
45 const LVObject *RHS) {
46 return LHS->getName() < RHS->getName();
47}
48
49// Callback comparator based on DIE offset.
51 const LVObject *RHS) {
52 return LHS->getOffset() < RHS->getOffset();
53}
54
55// Callback comparator for Range compare.
57 const LVObject *RHS) {
58 if (LHS->getLowerAddress() < RHS->getLowerAddress())
59 return true;
60
61 // If the lower address is the same, use the upper address value in
62 // order to put first the smallest interval.
63 if (LHS->getLowerAddress() == RHS->getLowerAddress())
64 return LHS->getUpperAddress() < RHS->getUpperAddress();
65
66 return false;
67}
68
69// Callback comparator based on multiple keys (First: Kind).
71 const LVObject *RHS) {
72 // Order in which the object attributes are used for comparison:
73 // kind, name, line number, offset.
74 std::tuple<std::string, StringRef, uint32_t, LVOffset> Left(
75 LHS->kind(), LHS->getName(), LHS->getLineNumber(), LHS->getOffset());
76 std::tuple<std::string, StringRef, uint32_t, LVOffset> Right(
77 RHS->kind(), RHS->getName(), RHS->getLineNumber(), RHS->getOffset());
78 return Left < Right;
79}
80
81// Callback comparator based on multiple keys (First: Line).
83 const LVObject *RHS) {
84 // Order in which the object attributes are used for comparison:
85 // line number, name, kind, offset.
86 std::tuple<uint32_t, StringRef, std::string, LVOffset> Left(
87 LHS->getLineNumber(), LHS->getName(), LHS->kind(), LHS->getOffset());
88 std::tuple<uint32_t, StringRef, std::string, LVOffset> Right(
89 RHS->getLineNumber(), RHS->getName(), RHS->kind(), RHS->getOffset());
90 return Left < Right;
91}
92
93// Callback comparator based on multiple keys (First: Name).
95 const LVObject *RHS) {
96 // Order in which the object attributes are used for comparison:
97 // name, line number, kind, offset.
98 std::tuple<StringRef, uint32_t, std::string, LVOffset> Left(
99 LHS->getName(), LHS->getLineNumber(), LHS->kind(), LHS->getOffset());
100 std::tuple<StringRef, uint32_t, std::string, LVOffset> Right(
101 RHS->getName(), RHS->getLineNumber(), RHS->kind(), RHS->getOffset());
102 return Left < Right;
103}
104
106 using LVSortInfo = std::map<LVSortMode, LVSortFunction>;
107 static LVSortInfo SortInfo = {
111 };
112
113 LVSortFunction SortFunction = nullptr;
114 LVSortInfo::iterator Iter = SortInfo.find(options().getSortMode());
115 if (Iter != SortInfo.end())
116 SortFunction = Iter->second;
117 return SortFunction;
118}
LLVM_ABI LVSortValue sortByName(const LVObject *LHS, const LVObject *RHS)
Definition LVSort.cpp:94
LLVM_ABI LVSortValue compareOffset(const LVObject *LHS, const LVObject *RHS)
Definition LVSort.cpp:50
LLVM_ABI LVSortValue sortByLine(const LVObject *LHS, const LVObject *RHS)
Definition LVSort.cpp:82
LLVM_ABI LVSortValue compareID(const LVObject *LHS, const LVObject *RHS)
Definition LVSort.cpp:26
LLVM_ABI LVSortValue sortByKind(const LVObject *LHS, const LVObject *RHS)
Definition LVSort.cpp:70
LLVM_ABI LVSortValue compareName(const LVObject *LHS, const LVObject *RHS)
Definition LVSort.cpp:44
LVSortValue(*)(const LVObject *LHS, const LVObject *RHS) LVSortFunction
Definition LVSort.h:35
LLVM_ABI LVSortValue compareRange(const LVObject *LHS, const LVObject *RHS)
Definition LVSort.cpp:56
LLVM_ABI LVSortValue compareLine(const LVObject *LHS, const LVObject *RHS)
Definition LVSort.cpp:38
LLVM_ABI LVSortFunction getSortFunction()
Definition LVSort.cpp:105
LLVM_ABI LVSortValue compareKind(const LVObject *LHS, const LVObject *RHS)
Definition LVSort.cpp:32
LVOptions & options()
Definition LVOptions.h:448
This is an optimization pass for GlobalISel generic memory operations.