@@ -77,39 +77,28 @@ class SPIRVLowerBool : public ModulePass, public InstVisitor<SPIRVLowerBool> {
77
77
replace (&I, Cmp);
78
78
}
79
79
}
80
- virtual void visitZExtInst (ZExtInst &I) {
80
+ void handleCastInstructions (Instruction &I) {
81
81
auto Op = I.getOperand (0 );
82
82
if (isBoolType (Op->getType ())) {
83
- auto Ty = I.getType ();
83
+ auto Opcode = I.getOpcode ();
84
+ auto Ty = (Opcode == Instruction::ZExt || Opcode == Instruction::SExt)
85
+ ? I.getType ()
86
+ : Type::getInt32Ty (*Context);
84
87
auto Zero = getScalarOrVectorConstantInt (Ty, 0 , false );
85
- auto One = getScalarOrVectorConstantInt (Ty, 1 , false );
88
+ auto One = getScalarOrVectorConstantInt (
89
+ Ty, (Opcode == Instruction::SExt) ? ~0 : 1 , false );
86
90
assert (Zero && One && " Couldn't create constant int" );
87
91
auto Sel = SelectInst::Create (Op, One, Zero, " " , &I);
88
- replace (&I, Sel);
89
- }
90
- }
91
- virtual void visitSExtInst (SExtInst &I) {
92
- auto Op = I.getOperand (0 );
93
- if (isBoolType (Op->getType ())) {
94
- auto Ty = I.getType ();
95
- auto Zero = getScalarOrVectorConstantInt (Ty, 0 , false );
96
- auto One = getScalarOrVectorConstantInt (Ty, ~0 , false );
97
- assert (Zero && One && " Couldn't create constant int" );
98
- auto Sel = SelectInst::Create (Op, One, Zero, " " , &I);
99
- replace (&I, Sel);
100
- }
101
- }
102
- virtual void visitUIToFPInst (UIToFPInst &I) {
103
- auto Op = I.getOperand (0 );
104
- if (isBoolType (Op->getType ())) {
105
- auto Ty = Type::getInt32Ty (*Context);
106
- auto Zero = getScalarOrVectorConstantInt (Ty, 0 , false );
107
- auto One = getScalarOrVectorConstantInt (Ty, 1 , false );
108
- assert (Zero && One && " Couldn't create constant int" );
109
- auto Sel = SelectInst::Create (Op, One, Zero, " " , &I);
110
- I.setOperand (0 , Sel);
92
+ if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt)
93
+ replace (&I, Sel);
94
+ else if (Opcode == Instruction::UIToFP || Opcode == Instruction::SIToFP)
95
+ I.setOperand (0 , Sel);
111
96
}
112
97
}
98
+ virtual void visitZExtInst (ZExtInst &I) { handleCastInstructions (I); }
99
+ virtual void visitSExtInst (SExtInst &I) { handleCastInstructions (I); }
100
+ virtual void visitUIToFPInst (UIToFPInst &I) { handleCastInstructions (I); }
101
+ virtual void visitSIToFPInst (SIToFPInst &I) { handleCastInstructions (I); }
113
102
bool runOnModule (Module &M) override {
114
103
Context = &M.getContext ();
115
104
visit (M);
0 commit comments