@@ -112,7 +112,7 @@ bool RISCVVectorPeephole::tryToReduceVL(MachineInstr &MI) const {
112
112
//
113
113
// TODO: We can handle a bunch more instructions here, and probably
114
114
// recurse backwards through operands too.
115
- unsigned SrcIdx = 0 ;
115
+ SmallVector< unsigned , 2 > SrcIndices = { 0 } ;
116
116
switch (RISCV::getRVVMCOpcode (MI.getOpcode ())) {
117
117
default :
118
118
return false ;
@@ -122,10 +122,10 @@ bool RISCVVectorPeephole::tryToReduceVL(MachineInstr &MI) const {
122
122
case RISCV::VSE64_V:
123
123
break ;
124
124
case RISCV::VMV_V_V:
125
- SrcIdx = 2 ;
125
+ SrcIndices[ 0 ] = 2 ;
126
126
break ;
127
127
case RISCV::VMERGE_VVM:
128
- SrcIdx = 3 ; // TODO: We can also handle the false operand.
128
+ SrcIndices. assign ({ 2 , 3 });
129
129
break ;
130
130
case RISCV::VREDSUM_VS:
131
131
case RISCV::VREDMAXU_VS:
@@ -143,50 +143,56 @@ bool RISCVVectorPeephole::tryToReduceVL(MachineInstr &MI) const {
143
143
case RISCV::VFREDMIN_VS:
144
144
case RISCV::VFWREDUSUM_VS:
145
145
case RISCV::VFWREDOSUM_VS:
146
- SrcIdx = 2 ;
146
+ SrcIndices[ 0 ] = 2 ;
147
147
break ;
148
148
}
149
149
150
150
MachineOperand &VL = MI.getOperand (RISCVII::getVLOpNum (MI.getDesc ()));
151
151
if (VL.isImm () && VL.getImm () == RISCV::VLMaxSentinel)
152
152
return false ;
153
153
154
- Register SrcReg = MI.getOperand (SrcIdx).getReg ();
155
- // Note: one *use*, not one *user*.
156
- if (!MRI->hasOneUse (SrcReg))
157
- return false ;
158
-
159
- MachineInstr *Src = MRI->getVRegDef (SrcReg);
160
- if (!Src || Src->hasUnmodeledSideEffects () ||
161
- Src->getParent () != MI.getParent () || Src->getNumDefs () != 1 ||
162
- !RISCVII::hasVLOp (Src->getDesc ().TSFlags ) ||
163
- !RISCVII::hasSEWOp (Src->getDesc ().TSFlags ))
164
- return false ;
165
-
166
- // Src's dest needs to have the same EEW as MI's input.
167
- if (!hasSameEEW (MI, *Src))
168
- return false ;
169
-
170
- bool ElementsDependOnVL = RISCVII::elementsDependOnVL (
171
- TII->get (RISCV::getRVVMCOpcode (Src->getOpcode ())).TSFlags );
172
- if (ElementsDependOnVL || Src->mayRaiseFPException ())
173
- return false ;
154
+ bool Changed = false ;
155
+ for (unsigned SrcIdx : SrcIndices) {
156
+ Register SrcReg = MI.getOperand (SrcIdx).getReg ();
157
+ // Note: one *use*, not one *user*.
158
+ if (!MRI->hasOneUse (SrcReg))
159
+ continue ;
160
+
161
+ MachineInstr *Src = MRI->getVRegDef (SrcReg);
162
+ if (!Src || Src->hasUnmodeledSideEffects () ||
163
+ Src->getParent () != MI.getParent () || Src->getNumDefs () != 1 ||
164
+ !RISCVII::hasVLOp (Src->getDesc ().TSFlags ) ||
165
+ !RISCVII::hasSEWOp (Src->getDesc ().TSFlags ))
166
+ continue ;
167
+
168
+ // Src's dest needs to have the same EEW as MI's input.
169
+ if (!hasSameEEW (MI, *Src))
170
+ continue ;
171
+
172
+ bool ElementsDependOnVL = RISCVII::elementsDependOnVL (
173
+ TII->get (RISCV::getRVVMCOpcode (Src->getOpcode ())).TSFlags );
174
+ if (ElementsDependOnVL || Src->mayRaiseFPException ())
175
+ continue ;
176
+
177
+ MachineOperand &SrcVL =
178
+ Src->getOperand (RISCVII::getVLOpNum (Src->getDesc ()));
179
+ if (VL.isIdenticalTo (SrcVL) || !RISCV::isVLKnownLE (VL, SrcVL))
180
+ continue ;
174
181
175
- MachineOperand &SrcVL = Src->getOperand (RISCVII::getVLOpNum (Src->getDesc ()));
176
- if (VL.isIdenticalTo (SrcVL) || !RISCV::isVLKnownLE (VL, SrcVL))
177
- return false ;
182
+ if (!ensureDominates (VL, *Src))
183
+ continue ;
178
184
179
- if (!ensureDominates (VL, *Src))
180
- return false ;
185
+ if (VL.isImm ())
186
+ SrcVL.ChangeToImmediate (VL.getImm ());
187
+ else if (VL.isReg ())
188
+ SrcVL.ChangeToRegister (VL.getReg (), false );
181
189
182
- if (VL.isImm ())
183
- SrcVL.ChangeToImmediate (VL.getImm ());
184
- else if (VL.isReg ())
185
- SrcVL.ChangeToRegister (VL.getReg (), false );
190
+ Changed = true ;
191
+ }
186
192
187
193
// TODO: For instructions with a passthru, we could clear the passthru
188
194
// and tail policy since we've just proven the tail is not demanded.
189
- return true ;
195
+ return Changed ;
190
196
}
191
197
192
198
// / Check if an operand is an immediate or a materialized ADDI $x0, imm.
0 commit comments