LLVM 20.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 kind.
27 const LVObject *RHS) {
28 return std::string(LHS->kind()) < std::string(RHS->kind());
29}
30
31// Callback comparator based on line.
33 const LVObject *RHS) {
34 return LHS->getLineNumber() < RHS->getLineNumber();
35}
36
37// Callback comparator based on name.
39 const LVObject *RHS) {
40 return LHS->getName() < RHS->getName();
41}
42
43// Callback comparator based on DIE offset.
45 const LVObject *RHS) {
46 return LHS->getOffset() < RHS->getOffset();
47}
48
49// Callback comparator for Range compare.
51 const LVObject *RHS) {
52 if (LHS->getLowerAddress() < RHS->getLowerAddress())
53 return true;
54
55 // If the lower address is the same, use the upper address value in
56 // order to put first the smallest interval.
57 if (LHS->getLowerAddress() == RHS->getLowerAddress())
58 return LHS->getUpperAddress() < RHS->getUpperAddress();
59
60 return false;
61}
62
63// Callback comparator based on multiple keys (First: Kind).
65 const LVObject *RHS) {
66 // Order in which the object attributes are used for comparison:
67 // kind, name, line number, offset.
68 std::tuple<std::string, StringRef, uint32_t, LVOffset> Left(
69 LHS->kind(), LHS->getName(), LHS->getLineNumber(), LHS->getOffset());
70 std::tuple<std::string, StringRef, uint32_t, LVOffset> Right(
71 RHS->kind(), RHS->getName(), RHS->getLineNumber(), RHS->getOffset());
72 return Left < Right;
73}
74
75// Callback comparator based on multiple keys (First: Line).
77 const LVObject *RHS) {
78 // Order in which the object attributes are used for comparison:
79 // line number, name, kind, offset.
80 std::tuple<uint32_t, StringRef, std::string, LVOffset> Left(
81 LHS->getLineNumber(), LHS->getName(), LHS->kind(), LHS->getOffset());
82 std::tuple<uint32_t, StringRef, std::string, LVOffset> Right(
83 RHS->getLineNumber(), RHS->getName(), RHS->kind(), RHS->getOffset());
84 return Left < Right;
85}
86
87// Callback comparator based on multiple keys (First: Name).
89 const LVObject *RHS) {
90 // Order in which the object attributes are used for comparison:
91 // name, line number, kind, offset.
92 std::tuple<StringRef, uint32_t, std::string, LVOffset> Left(
93 LHS->getName(), LHS->getLineNumber(), LHS->kind(), LHS->getOffset());
94 std::tuple<StringRef, uint32_t, std::string, LVOffset> Right(
95 RHS->getName(), RHS->getLineNumber(), RHS->kind(), RHS->getOffset());
96 return Left < Right;
97}
98
100 using LVSortInfo = std::map<LVSortMode, LVSortFunction>;
101 static LVSortInfo SortInfo = {
102 {LVSortMode::None, nullptr}, {LVSortMode::Kind, sortByKind},
103 {LVSortMode::Line, sortByLine}, {LVSortMode::Name, sortByName},
104 {LVSortMode::Offset, compareOffset},
105 };
106
107 LVSortFunction SortFunction = nullptr;
108 LVSortInfo::iterator Iter = SortInfo.find(options().getSortMode());
109 if (Iter != SortInfo.end())
110 SortFunction = Iter->second;
111 return SortFunction;
112}
Value * RHS
Value * LHS
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
LVSortValue sortByName(const LVObject *LHS, const LVObject *RHS)
Definition: LVSort.cpp:88
LVSortValue compareOffset(const LVObject *LHS, const LVObject *RHS)
Definition: LVSort.cpp:44
LVSortValue sortByLine(const LVObject *LHS, const LVObject *RHS)
Definition: LVSort.cpp:76
LVSortValue sortByKind(const LVObject *LHS, const LVObject *RHS)
Definition: LVSort.cpp:64
LVSortValue compareName(const LVObject *LHS, const LVObject *RHS)
Definition: LVSort.cpp:38
LVSortValue compareRange(const LVObject *LHS, const LVObject *RHS)
Definition: LVSort.cpp:50
LVSortValue compareLine(const LVObject *LHS, const LVObject *RHS)
Definition: LVSort.cpp:32
LVSortFunction getSortFunction()
Definition: LVSort.cpp:99
LVSortValue compareKind(const LVObject *LHS, const LVObject *RHS)
Definition: LVSort.cpp:26
LVOptions & options()
Definition: LVOptions.h:445
LVSortValue(*)(const LVObject *LHS, const LVObject *RHS) LVSortFunction
Definition: LVSort.h:33
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18