Skip to content

Commit b41dc1c

Browse files
committed
fix for body with ctrl-flow
1 parent aff3990 commit b41dc1c

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,6 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
13261326
Continue = getJumpDestInCurrentScope("for.inc");
13271327
BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
13281328

1329-
llvm::BasicBlock *ForBody = nullptr;
13301329
if (S.getCond()) {
13311330
// If the for statement has a condition scope, emit the local variable
13321331
// declaration.
@@ -1351,7 +1350,7 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
13511350
ExitBlock = createBasicBlock("for.cond.cleanup");
13521351

13531352
// As long as the condition is true, iterate the loop.
1354-
ForBody = createBasicBlock("for.body");
1353+
llvm::BasicBlock *ForBody = createBasicBlock("for.body");
13551354

13561355
// C99 6.8.5p2/p4: The first substatement is executed if the expression
13571356
// compares unequal to 0. The condition must be a scalar type.
@@ -1397,6 +1396,8 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
13971396
EmitStmt(S.getBody());
13981397
}
13991398

1399+
auto *FinalBodyBB = Builder.GetInsertBlock();
1400+
14001401
// If there is an increment, emit it next.
14011402
if (S.getInc()) {
14021403
EmitBlock(Continue.getBlock());
@@ -1427,10 +1428,10 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
14271428
if (CGM.shouldEmitConvergenceTokens())
14281429
ConvergenceTokenStack.pop_back();
14291430

1430-
if (ForBody) {
1431+
if (FinalBodyBB) {
14311432
// Key Instructions: We want the for closing brace to be step-able on to
14321433
// match existing behaviour.
1433-
addInstToNewSourceAtom(ForBody->getTerminator(), nullptr);
1434+
addInstToNewSourceAtom(FinalBodyBB->getTerminator(), nullptr);
14341435
}
14351436
}
14361437

clang/test/DebugInfo/KeyInstructions/for.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,76 @@ void a(int A) {
2929
}
3030
}
3131

32+
void b(int A) {
33+
// CHECK: entry:
34+
// CHECK: store i32 0, ptr %i{{.*}}, !dbg [[bG1R1:!.*]]
35+
// CHECK: for.cond:
36+
// CHECK: %cmp = icmp slt i32 %0, %1, !dbg [[bG2R1:!.*]]
37+
// CHECK: br i1 %cmp, label %for.body, label %for.end, !dbg [[bG3R1:!.*]]
38+
39+
// CHECK: for.body:
40+
// CHECK-NEXT: %2 = load i32, ptr %A.addr
41+
// - If stmt atom:
42+
// CHECK-NEXT: %cmp1 = icmp sgt i32 %2, 1, !dbg [[bG4R2:!.*]]
43+
// CHECK-NEXT: br i1 %cmp1, label %if.then, label %if.end, !dbg [[bG4R1:!.*]]
44+
// CHECK: if.then:
45+
// CHECK-NEXT: br label %if.end
46+
47+
// - For closing brace.
48+
// CHECK: if.end:
49+
// CHECK-NEXT: br label %for.inc, !dbg [[bG6R1:!.*]]
50+
51+
// CHECK: for.inc:
52+
// CHECK: %inc = add{{.*}}, !dbg [[bG5R2:!.*]]
53+
// CHECK: store i32 %inc, ptr %i{{.*}}, !dbg [[bG5R1:!.*]]
54+
for (int i = 0; i < A; ++i) {
55+
if (A > 1)
56+
;
57+
}
58+
}
59+
60+
void c(int A) {
61+
// CHECK: entry:
62+
// CHECK: for.cond:
63+
// CHECK-NEXT: %0 = load i32, ptr %A.addr
64+
// - If stmt atom:
65+
// CHECK-NEXT: %cmp = icmp sgt i32 %0, 1, !dbg [[cG1R2:!.*]]
66+
// CHECK-NEXT: br i1 %cmp, label %if.then, label %if.end, !dbg [[cG1R1:!.*]]
67+
// CHECK: if.then:
68+
// CHECK-NEXT: br label %if.end
69+
70+
// - For closing brace.
71+
// CHECK: if.end:
72+
// CHECK-NEXT: br label %for.inc, !dbg [[cG3R1:!.*]]
73+
74+
// CHECK: for.inc:
75+
// CHECK-NEXT: %1 = load i32, ptr %A.addr
76+
// CHECK-NEXT: %inc = add{{.*}}, !dbg [[cG2R2:!.*]]
77+
// CHECK-NEXT: store i32 %inc, ptr %A.addr{{.*}}, !dbg [[cG2R1:!.*]]
78+
for (; /*no cond*/ ; ++A) {
79+
if (A > 1)
80+
;
81+
}
82+
}
83+
3284
// CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
3385
// CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
3486
// CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
3587
// CHECK: [[G5R1]] = !DILocation(line: 29,{{.*}} atomGroup: 5, atomRank: 1)
3688
// CHECK: [[G4R2]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 2)
3789
// CHECK: [[G4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
90+
91+
// CHECK: [[bG1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
92+
// CHECK: [[bG2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
93+
// CHECK: [[bG3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
94+
// CHECK: [[bG4R2]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 2)
95+
// CHECK: [[bG4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
96+
// CHECK: [[bG6R1]] = !DILocation(line: 57,{{.*}} atomGroup: 6, atomRank: 1)
97+
// CHECK: [[bG5R2]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 2)
98+
// CHECK: [[bG5R1]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 1)
99+
100+
// CHECK: [[cG1R2]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 2)
101+
// CHECK: [[cG1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
102+
// CHECK: [[cG3R1]] = !DILocation(line: 81,{{.*}} atomGroup: 3, atomRank: 1)
103+
// CHECK: [[cG2R2]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
104+
// CHECK: [[cG2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)

0 commit comments

Comments
 (0)