LLVM 24.0.0git
MemCacheHint.cpp
Go to the documentation of this file.
1//===- MemCacheHint.cpp - Cache hint metadata helpers --------------------===//
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
10#include "llvm/ADT/STLExtras.h"
12#include "llvm/IR/Constants.h"
15#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/Metadata.h"
17#include <optional>
18#include <utility>
19
20using namespace llvm;
21
22MDNode *llvm::getMemCacheHint(const Instruction &I, unsigned OperandNo) {
23 MDNode *MD = I.getMetadata(LLVMContext::MD_mem_cache_hint);
24 if (!MD)
25 return nullptr;
26
27 for (unsigned Idx = 0; Idx + 1 < MD->getNumOperands(); Idx += 2)
28 if (mdconst::extract<ConstantInt>(MD->getOperand(Idx))->getZExtValue() ==
29 OperandNo)
30 return cast<MDNode>(MD->getOperand(Idx + 1));
31 return nullptr;
32}
33
34void llvm::setMemCacheHint(Instruction &I, unsigned OperandNo, MDNode *Hint) {
36 if (MDNode *MD = I.getMetadata(LLVMContext::MD_mem_cache_hint))
37 for (unsigned Idx = 0; Idx + 1 < MD->getNumOperands(); Idx += 2)
38 Entries.emplace_back(
39 mdconst::extract<ConstantInt>(MD->getOperand(Idx))->getZExtValue(),
40 cast<MDNode>(MD->getOperand(Idx + 1)));
41
42 auto It = llvm::lower_bound(Entries, OperandNo,
43 [](const auto &Entry, unsigned OperandNo) {
44 return Entry.first < OperandNo;
45 });
46 if (It != Entries.end() && It->first == OperandNo) {
47 if (Hint)
48 It->second = Hint;
49 else
50 Entries.erase(It);
51 } else if (Hint) {
52 Entries.insert(It, {OperandNo, Hint});
53 } else {
54 return;
55 }
56
58 for (const auto &[Index, Payload] : Entries) {
60 ConstantInt::get(Type::getInt32Ty(I.getContext()), Index)));
61 Operands.push_back(Payload);
62 }
63 I.setMetadata(LLVMContext::MD_mem_cache_hint,
64 Operands.empty() ? nullptr
65 : MDNode::get(I.getContext(), Operands));
66}
67
68// The supported accesses have one memory operand. Its position also
69// distinguishes reads (0) from writes (1).
70static std::optional<unsigned> getPointerOperandIndex(const Instruction &I) {
71 if (isa<LoadInst>(I))
73 if (isa<StoreInst>(I))
75 const auto *II = dyn_cast<IntrinsicInst>(&I);
76 if (!II)
77 return std::nullopt;
78 switch (II->getIntrinsicID()) {
79 case Intrinsic::masked_load:
80 case Intrinsic::masked_gather:
81 return 0;
82 case Intrinsic::masked_store:
83 case Intrinsic::masked_scatter:
84 return 1;
85 default:
86 return std::nullopt;
87 }
88}
89
91 ArrayRef<Value *> Sources) {
92 std::optional<unsigned> PointerIndex = getPointerOperandIndex(Dest);
93 if (!PointerIndex || Sources.empty())
94 return nullptr;
95
96 const auto *First = dyn_cast<Instruction>(Sources.front());
97 if (!First)
98 return nullptr;
99 MDNode *MD = First->getMetadata(LLVMContext::MD_mem_cache_hint);
100 if (!MD)
101 return nullptr;
102
103 for (Value *V : Sources) {
104 const auto *Source = dyn_cast<Instruction>(V);
105 if (!Source || getPointerOperandIndex(*Source) != PointerIndex ||
106 Source->getMetadata(LLVMContext::MD_mem_cache_hint) != MD)
107 return nullptr;
108 }
109 return MD;
110}
111
113 Dest.setMetadata(LLVMContext::MD_mem_cache_hint,
114 getCommonCacheHint(Dest, Sources));
115}
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define I(x, y, z)
Definition MD5.cpp:57
static MDNode * getCommonCacheHint(const Instruction &Dest, ArrayRef< Value * > Sources)
static std::optional< unsigned > getPointerOperandIndex(const Instruction &I)
This file contains the declarations for metadata subclasses.
uint64_t IntrinsicInst * II
SI Fold Operands
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
const T & front() const
Get the first element.
Definition ArrayRef.h:144
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:548
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
static unsigned getPointerOperandIndex()
Metadata node.
Definition Metadata.h:1081
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1437
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1443
reference emplace_back(ArgTypes &&... Args)
iterator erase(const_iterator CI)
iterator insert(iterator I, T &&Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static unsigned getPointerOperandIndex()
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:299
LLVM Value Representation.
Definition Value.h:75
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:679
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void setMemCacheHint(Instruction &I, unsigned OperandNo, MDNode *Hint)
Set the hint payload for operand OperandNo of I, preserving other operands' hints.
LLVM_ABI void propagateMemCacheHint(Instruction &Dest, ArrayRef< Value * > Sources)
Copy identical source cache-hint attachments to Dest, or clear its attachment if propagation fails.
LLVM_ABI MDNode * getMemCacheHint(const Instruction &I, unsigned OperandNo=0)
Return the hint payload for operand OperandNo of I, or nullptr if absent.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2068
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559