Skip to content

Commit c4011e8

Browse files
remove a chance to restructure a block ending with OpBranchConditional
1 parent 1fe2818 commit c4011e8

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

llvm/lib/Target/SPIRV/SPIRVInstrInfo.cpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,19 @@ bool SPIRVInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
191191
auto MI = MBB.getLastNonDebugInstr();
192192
if (!MI.isValid())
193193
return false;
194-
if (MI->getOpcode() == SPIRV::OpBranch) {
195-
TBB = MI->getOperand(0).getMBB();
196-
return false;
197-
} else if (MI->getOpcode() == SPIRV::OpBranchConditional) {
198-
Cond.push_back(MI->getOperand(0));
199-
TBB = MI->getOperand(1).getMBB();
200-
FBB = MI->getOperand(2).getMBB();
201-
for (unsigned I = 3, E = MI->getNumOperands(); I < E; ++I)
202-
Cond.push_back(MI->getOperand(I));
203-
return false;
204-
} else {
194+
195+
// We do not allow to restructure blocks ending with OpBranchConditional,
196+
// because there is no way to encode `if (Cond) then Stmt` logic, only
197+
// full if-then-else is supported by OpBranchConditional.
198+
// If we supported splitting of blocks ending with OpBranchConditional
199+
// MachineBasicBlock.cpp would expect successfull implementation of calls
200+
// to insertBranch() setting FBB to null that is not feasible.
201+
if (MI->getOpcode() != SPIRV::OpBranch)
205202
return true;
206-
}
203+
204+
// Allow only 'unconditional branch' modifications of the basic block.
205+
TBB = MI->getOperand(0).getMBB();
206+
return false;
207207
}
208208

209209
// Remove the branching code at the end of the specific MBB.
@@ -217,8 +217,7 @@ unsigned SPIRVInstrInfo::removeBranch(MachineBasicBlock &MBB,
217217
if (I == MBB.end())
218218
return 0;
219219

220-
unsigned Opcode = I->getOpcode();
221-
if (Opcode == SPIRV::OpBranch || Opcode == SPIRV::OpBranchConditional) {
220+
if (I->getOpcode() == SPIRV::OpBranch) {
222221
I->eraseFromParent();
223222
return 1;
224223
}
@@ -246,16 +245,7 @@ unsigned SPIRVInstrInfo::insertBranch(MachineBasicBlock &MBB,
246245
int * /*BytesAdded*/) const {
247246
if (!TBB)
248247
return 0;
249-
if (Cond.empty()) {
250-
BuildMI(&MBB, DL, get(SPIRV::OpBranch)).addMBB(TBB);
251-
} else {
252-
auto MIB = BuildMI(&MBB, DL, get(SPIRV::OpBranchConditional))
253-
.add(Cond[0])
254-
.addMBB(TBB)
255-
.addMBB(FBB);
256-
for (unsigned i = 1; i < Cond.size(); ++i)
257-
MIB.add(Cond[i]);
258-
}
248+
BuildMI(&MBB, DL, get(SPIRV::OpBranch)).addMBB(TBB);
259249
return 1;
260250
}
261251

llvm/lib/Target/SPIRV/SPIRVInstrInfo.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,13 +622,13 @@ def OpLoopMerge: Op<246, (outs), (ins unknown:$merge, unknown:$continue, LoopCon
622622
def OpSelectionMerge: Op<247, (outs), (ins unknown:$merge, SelectionControl:$sc),
623623
"OpSelectionMerge $merge $sc">;
624624
def OpLabel: Op<248, (outs ID:$label), (ins), "$label = OpLabel">;
625-
let isBarrier = 1, isTerminator=1 in {
625+
let isBarrier = 1, isTerminator = 1, isBranch = 1 in {
626626
def OpBranch: Op<249, (outs), (ins unknown:$label), "OpBranch $label">;
627627
def OpBranchConditional: Op<250, (outs), (ins ID:$cond, unknown:$true, unknown:$false, variable_ops),
628628
"OpBranchConditional $cond $true $false">;
629629
def OpSwitch: Op<251, (outs), (ins ID:$sel, ID:$dflt, variable_ops), "OpSwitch $sel $dflt">;
630630
}
631-
let isReturn = 1, hasDelaySlot=0, isBarrier = 0, isTerminator=1, isNotDuplicable = 1 in {
631+
let isReturn = 1, hasDelaySlot = 0, isBarrier = 0, isTerminator = 1, isNotDuplicable = 1 in {
632632
def OpKill: SimpleOp<"OpKill", 252>;
633633
def OpReturn: SimpleOp<"OpReturn", 253>;
634634
def OpReturnValue: Op<254, (outs), (ins ID:$ret), "OpReturnValue $ret">;

0 commit comments

Comments
 (0)