Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/SPIRVLowerBool.cpp

raw
1 //===- SPIRVLowerBool.cpp - Lower instructions with bool operands ---------===//
2 //
3 // The LLVM/SPIRV Translator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 // Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal with the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimers.
19 // Redistributions in binary form must reproduce the above copyright notice,
20 // this list of conditions and the following disclaimers in the documentation
21 // and/or other materials provided with the distribution.
22 // Neither the names of Advanced Micro Devices, Inc., nor the names of its
23 // contributors may be used to endorse or promote products derived from this
24 // Software without specific prior written permission.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
31 // THE SOFTWARE.
32 //
33 //===----------------------------------------------------------------------===//
34 //
35 // This file implements lowering instructions with bool operands.
36 //
37 //===----------------------------------------------------------------------===//
38 #define DEBUG_TYPE "spvbool"
39
40 #include "SPIRVLowerBool.h"
41 #include "SPIRVInternal.h"
42 #include "libSPIRV/SPIRVDebug.h"
43
44 #include "llvm/IR/IRBuilder.h"
45
46 using namespace llvm;
47 using namespace SPIRV;
48
49 namespace SPIRV {
50
51 void SPIRVLowerBoolBase::replace(Instruction *I, Instruction *NewI) {
52 NewI->takeName(I);
53 NewI->setDebugLoc(I->getDebugLoc());
54 I->replaceAllUsesWith(NewI);
55 I->dropAllReferences();
56 I->eraseFromParent();
57 }
58
59 bool SPIRVLowerBoolBase::isBoolType(Type *Ty) {
60 if (Ty->isIntegerTy(1))
61 return true;
62 if (auto VT = dyn_cast<VectorType>(Ty))
63 return isBoolType(VT->getElementType());
64 return false;
65 }
66
67 void SPIRVLowerBoolBase::visitTruncInst(TruncInst &I) {
68 if (isBoolType(I.getType())) {
69 auto Op = I.getOperand(0);
70 auto And = BinaryOperator::CreateAnd(
71 Op, getScalarOrVectorConstantInt(Op->getType(), 1, false), "", &I);
72 And->setDebugLoc(I.getDebugLoc());
73 auto Zero = getScalarOrVectorConstantInt(Op->getType(), 0, false);
74 auto Cmp = new ICmpInst(&I, CmpInst::ICMP_NE, And, Zero);
75 replace(&I, Cmp);
76 }
77 }
78
79 void SPIRVLowerBoolBase::handleExtInstructions(Instruction &I) {
80 auto Op = I.getOperand(0);
81 if (isBoolType(Op->getType())) {
82 auto Opcode = I.getOpcode();
83 auto Ty = I.getType();
84 auto Zero = getScalarOrVectorConstantInt(Ty, 0, false);
85 auto One = getScalarOrVectorConstantInt(
86 Ty, (Opcode == Instruction::SExt) ? ~0 : 1, false);
87 assert(Zero && One && "Couldn't create constant int");
88 auto Sel = SelectInst::Create(Op, One, Zero, "", &I);
89 replace(&I, Sel);
90 }
91 }
92
93 void SPIRVLowerBoolBase::handleCastInstructions(Instruction &I) {
94 auto Op = I.getOperand(0);
95 auto *OpTy = Op->getType();
96 if (isBoolType(OpTy)) {
97 Type *Ty = Type::getInt32Ty(*Context);
98 if (auto VT = dyn_cast<FixedVectorType>(OpTy))
99 Ty = llvm::FixedVectorType::get(Ty, VT->getNumElements());
100 auto Zero = getScalarOrVectorConstantInt(Ty, 0, false);
101 auto One = getScalarOrVectorConstantInt(Ty, 1, false);
102 assert(Zero && One && "Couldn't create constant int");
103 auto Sel = SelectInst::Create(Op, One, Zero, "", &I);
104 Sel->setDebugLoc(I.getDebugLoc());
105 I.setOperand(0, Sel);
106 }
107 }
108
109 void SPIRVLowerBoolBase::visitZExtInst(ZExtInst &I) {
110 handleExtInstructions(I);
111 }
112
113 void SPIRVLowerBoolBase::visitSExtInst(SExtInst &I) {
114 handleExtInstructions(I);
115 }
116
117 void SPIRVLowerBoolBase::visitUIToFPInst(UIToFPInst &I) {
118 handleCastInstructions(I);
119 }
120
121 void SPIRVLowerBoolBase::visitSIToFPInst(SIToFPInst &I) {
122 handleCastInstructions(I);
123 }
124
125 bool SPIRVLowerBoolBase::runLowerBool(Module &M) {
126 Context = &M.getContext();
127 visit(M);
128
129 verifyRegularizationPass(M, "SPIRVLowerBool");
130 return true;
131 }
132
133 llvm::PreservedAnalyses
134 SPIRVLowerBoolPass::run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) {
135 return runLowerBool(M) ? llvm::PreservedAnalyses::none()
136 : llvm::PreservedAnalyses::all();
137 }
138
139 SPIRVLowerBoolLegacy::SPIRVLowerBoolLegacy() : ModulePass(ID) {
140 initializeSPIRVLowerBoolLegacyPass(*PassRegistry::getPassRegistry());
141 }
142
143 bool SPIRVLowerBoolLegacy::runOnModule(Module &M) { return runLowerBool(M); }
144
145 char SPIRVLowerBoolLegacy::ID = 0;
146 } // namespace SPIRV
147
148 INITIALIZE_PASS(SPIRVLowerBoolLegacy, "spvbool",
149 "Lower instructions with bool operands", false, false)
150
151 ModulePass *llvm::createSPIRVLowerBoolLegacy() {
152 return new SPIRVLowerBoolLegacy();
153 }