Skip to content

Commit 0cd3ec6

Browse files
committed
utils: Fix segfault in flattencfg
v2: continue iterating through the rest of the bb use for loop v3: initialize FlattenCFG pass in ScalarOps add test v4: split off initializing flattencfg to a separate patch add comment Signed-off-by: Jan Vesely <[email protected]> llvm-svn: 215574
1 parent 5a956d4 commit 0cd3ec6

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

llvm/lib/Transforms/Utils/FlattenCFG.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,20 @@ bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,
238238
// Do branch inversion.
239239
BasicBlock *CurrBlock = LastCondBlock;
240240
bool EverChanged = false;
241-
while (1) {
241+
for (;CurrBlock != FirstCondBlock;
242+
CurrBlock = CurrBlock->getSinglePredecessor()) {
242243
BranchInst *BI = dyn_cast<BranchInst>(CurrBlock->getTerminator());
243244
CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
245+
if (!CI)
246+
continue;
247+
244248
CmpInst::Predicate Predicate = CI->getPredicate();
245249
// Canonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
246250
if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
247251
CI->setPredicate(ICmpInst::getInversePredicate(Predicate));
248252
BI->swapSuccessors();
249253
EverChanged = true;
250254
}
251-
if (CurrBlock == FirstCondBlock)
252-
break;
253-
CurrBlock = CurrBlock->getSinglePredecessor();
254255
}
255256
return EverChanged;
256257
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; RUN: opt -flattencfg -S < %s | FileCheck %s
2+
3+
4+
; This test checks whether the pass completes without a crash.
5+
; The code is not transformed in any way
6+
;
7+
; CHECK-LABEL: @test_not_crash
8+
define void @test_not_crash(i32 %in_a) #0 {
9+
entry:
10+
%cmp0 = icmp eq i32 %in_a, -1
11+
%cmp1 = icmp ne i32 %in_a, 0
12+
%cond0 = and i1 %cmp0, %cmp1
13+
br i1 %cond0, label %b0, label %b1
14+
15+
b0: ; preds = %entry
16+
%cmp2 = icmp eq i32 %in_a, 0
17+
%cmp3 = icmp ne i32 %in_a, 1
18+
%cond1 = or i1 %cmp2, %cmp3
19+
br i1 %cond1, label %exit, label %b1
20+
21+
b1: ; preds = %entry, %b0
22+
br label %exit
23+
24+
exit: ; preds = %entry, %b0, %b1
25+
ret void
26+
}

0 commit comments

Comments
 (0)