1 //===-- NVPTXImageOptimizer.cpp - Image optimization pass -----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass implements IR-level optimizations of image access code,
12 // 1. Eliminate istypep intrinsics when image access qualifier is known
14 //===----------------------------------------------------------------------===//
17 #include "NVPTXUtilities.h"
18 #include "llvm/Analysis/ConstantFolding.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/IntrinsicsNVPTX.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Pass.h"
28 class NVPTXImageOptimizer
: public FunctionPass
{
31 SmallVector
<Instruction
*, 4> InstrToDelete
;
34 NVPTXImageOptimizer();
36 bool runOnFunction(Function
&F
) override
;
38 StringRef
getPassName() const override
{ return "NVPTX Image Optimizer"; }
41 bool replaceIsTypePSampler(Instruction
&I
);
42 bool replaceIsTypePSurface(Instruction
&I
);
43 bool replaceIsTypePTexture(Instruction
&I
);
44 Value
*cleanupValue(Value
*V
);
45 void replaceWith(Instruction
*From
, ConstantInt
*To
);
49 char NVPTXImageOptimizer::ID
= 0;
51 NVPTXImageOptimizer::NVPTXImageOptimizer()
54 bool NVPTXImageOptimizer::runOnFunction(Function
&F
) {
59 InstrToDelete
.clear();
61 // Look for call instructions in the function
62 for (BasicBlock
&BB
: F
) {
63 for (Instruction
&Instr
: BB
) {
64 if (CallInst
*CI
= dyn_cast
<CallInst
>(&Instr
)) {
65 Function
*CalledF
= CI
->getCalledFunction();
66 if (CalledF
&& CalledF
->isIntrinsic()) {
67 // This is an intrinsic function call, check if its an istypep
68 switch (CalledF
->getIntrinsicID()) {
70 case Intrinsic::nvvm_istypep_sampler
:
71 Changed
|= replaceIsTypePSampler(Instr
);
73 case Intrinsic::nvvm_istypep_surface
:
74 Changed
|= replaceIsTypePSurface(Instr
);
76 case Intrinsic::nvvm_istypep_texture
:
77 Changed
|= replaceIsTypePTexture(Instr
);
85 // Delete any istypep instances we replaced in the IR
86 for (Instruction
*I
: InstrToDelete
)
92 bool NVPTXImageOptimizer::replaceIsTypePSampler(Instruction
&I
) {
93 Value
*TexHandle
= cleanupValue(I
.getOperand(0));
94 if (isSampler(*TexHandle
)) {
95 // This is an OpenCL sampler, so it must be a samplerref
96 replaceWith(&I
, ConstantInt::getTrue(I
.getContext()));
98 } else if (isImage(*TexHandle
)) {
99 // This is an OpenCL image, so it cannot be a samplerref
100 replaceWith(&I
, ConstantInt::getFalse(I
.getContext()));
103 // The image type is unknown, so we cannot eliminate the intrinsic
108 bool NVPTXImageOptimizer::replaceIsTypePSurface(Instruction
&I
) {
109 Value
*TexHandle
= cleanupValue(I
.getOperand(0));
110 if (isImageReadWrite(*TexHandle
) ||
111 isImageWriteOnly(*TexHandle
)) {
112 // This is an OpenCL read-only/read-write image, so it must be a surfref
113 replaceWith(&I
, ConstantInt::getTrue(I
.getContext()));
115 } else if (isImageReadOnly(*TexHandle
) ||
116 isSampler(*TexHandle
)) {
117 // This is an OpenCL read-only/ imageor sampler, so it cannot be
119 replaceWith(&I
, ConstantInt::getFalse(I
.getContext()));
122 // The image type is unknown, so we cannot eliminate the intrinsic
127 bool NVPTXImageOptimizer::replaceIsTypePTexture(Instruction
&I
) {
128 Value
*TexHandle
= cleanupValue(I
.getOperand(0));
129 if (isImageReadOnly(*TexHandle
)) {
130 // This is an OpenCL read-only image, so it must be a texref
131 replaceWith(&I
, ConstantInt::getTrue(I
.getContext()));
133 } else if (isImageWriteOnly(*TexHandle
) ||
134 isImageReadWrite(*TexHandle
) ||
135 isSampler(*TexHandle
)) {
136 // This is an OpenCL read-write/write-only image or a sampler, so it
137 // cannot be a texref
138 replaceWith(&I
, ConstantInt::getFalse(I
.getContext()));
141 // The image type is unknown, so we cannot eliminate the intrinsic
146 void NVPTXImageOptimizer::replaceWith(Instruction
*From
, ConstantInt
*To
) {
147 // We implement "poor man's DCE" here to make sure any code that is no longer
148 // live is actually unreachable and can be trivially eliminated by the
149 // unreachable block elimination pass.
150 for (Use
&U
: From
->uses()) {
151 if (BranchInst
*BI
= dyn_cast
<BranchInst
>(U
)) {
152 if (BI
->isUnconditional()) continue;
156 Dest
= BI
->getSuccessor(1);
159 Dest
= BI
->getSuccessor(0);
160 BranchInst::Create(Dest
, BI
->getIterator());
161 InstrToDelete
.push_back(BI
);
164 From
->replaceAllUsesWith(To
);
165 InstrToDelete
.push_back(From
);
168 Value
*NVPTXImageOptimizer::cleanupValue(Value
*V
) {
169 if (ExtractValueInst
*EVI
= dyn_cast
<ExtractValueInst
>(V
)) {
170 return cleanupValue(EVI
->getAggregateOperand());
175 FunctionPass
*llvm::createNVPTXImageOptimizerPass() {
176 return new NVPTXImageOptimizer();