LLVM 24.0.0git
DXILRemoveUnusedResources.cpp
Go to the documentation of this file.
1//===- DXILResourceAccess.cpp - Resource access via load/store ------------===//
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#include "DirectX.h"
11#include "llvm/ADT/SetVector.h"
13#include "llvm/IR/BasicBlock.h"
15#include "llvm/IR/Dominators.h"
16#include "llvm/IR/Instruction.h"
19#include "llvm/IR/Intrinsics.h"
20#include "llvm/IR/IntrinsicsDirectX.h"
21#include "llvm/IR/LLVMContext.h"
24
25#define DEBUG_TYPE "dxil-remove-unused-resources"
26
27using namespace llvm;
28
30 return ID == Intrinsic::dx_resource_handlefrombinding ||
31 ID == Intrinsic::dx_resource_handlefromimplicitbinding ||
32 ID == Intrinsic::dx_resource_handlefromheap;
33}
34
35// Removes all calls to resource handle creation intrinsics that
36// either are not used, or their only use is in a store instruction, which
37// stores the initialized handle into a global variable that does not have
38// external linkage and that is not used anywhere else in the module.
40 if (!F.getContext()
41 .getLibraryOptions()
42 .get<DirectXMCOptions>()
43 .remove_unused_resources)
44 return false;
45
48 for (BasicBlock &BB : make_early_inc_range(F)) {
49 for (Instruction &I : BB) {
50 if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
51 if (!isResourceHandleCreation(II->getIntrinsicID()))
52 continue;
53 if (II->user_empty()) {
54 // Initialized handle is not used anywhere.
55 DeadInstr.push_back(II);
56 continue;
57 }
58 if (!II->hasOneUser())
59 continue;
60
61 // Initialized handle is only used in one store instruction, the store
62 // is into global variable, and that global variable is not used
63 // anywhere else and does not have external linkage.
64 auto *SI = dyn_cast<StoreInst>(*II->user_begin());
65 if (!SI)
66 continue;
67 assert(SI->getValueOperand() == II &&
68 "expected value operand to be the resource handle");
69
70 GlobalVariable *GV = dyn_cast<GlobalVariable>(SI->getPointerOperand());
71 if (!GV || GV->hasExternalLinkage())
72 continue;
73
74 if (GV->hasOneUser()) {
75 assert(*GV->user_begin() == SI &&
76 "expected single user to be the store instruction");
77 DeadInstr.push_back(SI);
78 DeadInstr.push_back(II);
79 DeadGlobals.insert(GV);
80 }
81 }
82 }
83 }
84
85 if (DeadInstr.empty())
86 return false;
87
88 for (auto *Instr : DeadInstr) {
89 if (auto *II = dyn_cast<IntrinsicInst>(Instr)) {
90 assert(isResourceHandleCreation(II->getIntrinsicID()));
91 // A heap resource does not have an associated global variable to remove.
92 if (II->getIntrinsicID() == Intrinsic::dx_resource_handlefromheap)
93 continue;
94 const unsigned ResourceNameOpIndex = 4;
96 II->getArgOperand(ResourceNameOpIndex));
97 if (ResourceName)
98 DeadGlobals.insert(ResourceName);
99 }
100 Instr->eraseFromParent();
101 }
102
103 for (auto *GV : DeadGlobals)
104 if (GV->use_empty())
105 GV->eraseFromParent();
106
107 return true;
108}
109
115
116namespace {
117class DXILRemoveUnusedResourcesLegacy : public FunctionPass {
118public:
119 bool runOnFunction(Function &F) override { return removeUnusedResources(F); }
120 StringRef getPassName() const override {
121 return "DXIL Remove Unused Resources";
122 }
123 DXILRemoveUnusedResourcesLegacy() : FunctionPass(ID) {}
124
125 static char ID; // Pass identification.
126 void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
127 AU.setPreservesAll();
128 }
129};
130char DXILRemoveUnusedResourcesLegacy::ID = 0;
131} // end anonymous namespace
132
133INITIALIZE_PASS_BEGIN(DXILRemoveUnusedResourcesLegacy, DEBUG_TYPE,
134 "DXIL Remove Unused Resources", false, false)
136INITIALIZE_PASS_END(DXILRemoveUnusedResourcesLegacy, DEBUG_TYPE,
137 "DXIL Remove Unused Resources", false, false)
138
140 return new DXILRemoveUnusedResourcesLegacy();
141}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool removeUnusedResources(Function &F)
static bool isResourceHandleCreation(Intrinsic::ID ID)
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file implements a set that has insertion order iteration characteristics.
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool hasExternalLinkage() const
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:346
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
user_iterator user_begin()
Definition Value.h:404
LLVM_ABI bool hasOneUser() const
Return true if there is exactly one user of this value.
Definition Value.cpp:158
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
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
FunctionPass * createDXILRemoveUnusedResourcesLegacyPass()
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.