llvm.org GIT mirror llvm / 8a56643
[simplifycfg][loop-simplify] Preserve loop metadata in 2 transformations. insertUniqueBackedgeBlock in lib/Transforms/Utils/LoopSimplify.cpp now propagates existing llvm.loop metadata to newly the added backedge. llvm::TryToSimplifyUncondBranchFromEmptyBlock in lib/Transforms/Utils/Local.cpp now propagates existing llvm.loop metadata to the branch instructions in the predecessor blocks of the empty block that is removed. Differential Revision: https://reviews.llvm.org/D26495 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287341 91177308-0d34-0410-b5e6-96231b3b80d8 Florian Hahn 2 years ago
4 changed file(s) with 115 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
888888 PN->eraseFromParent();
889889 }
890890 }
891
892 // If the unconditional branch we replaced contains llvm.loop metadata, we
893 // add the metadata to the branch instructions in the predecessors.
894 unsigned LoopMDKind = BB->getContext().getMDKindID("llvm.loop");
895 Instruction *TI = BB->getTerminator();
896 if (TI)
897 if (MDNode *LoopMD = TI->getMetadata(LoopMDKind))
898 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
899 BasicBlock *Pred = *PI;
900 Pred->getTerminator()->setMetadata(LoopMDKind, LoopMD);
901 }
891902
892903 // Everything that jumped to BB now goes to Succ.
893904 BB->replaceAllUsesWith(Succ);
469469 }
470470
471471 // Now that all of the PHI nodes have been inserted and adjusted, modify the
472 // backedge blocks to just to the BEBlock instead of the header.
472 // backedge blocks to jump to the BEBlock instead of the header.
473 // If one of the backedges has llvm.loop metadata attached, we remove
474 // it from the backedge and add it to BEBlock.
475 unsigned LoopMDKind = BEBlock->getContext().getMDKindID("llvm.loop");
476 MDNode *LoopMD = nullptr;
473477 for (unsigned i = 0, e = BackedgeBlocks.size(); i != e; ++i) {
474478 TerminatorInst *TI = BackedgeBlocks[i]->getTerminator();
479 if (!LoopMD)
480 LoopMD = TI->getMetadata(LoopMDKind);
481 TI->setMetadata(LoopMDKind, nullptr);
475482 for (unsigned Op = 0, e = TI->getNumSuccessors(); Op != e; ++Op)
476483 if (TI->getSuccessor(Op) == Header)
477484 TI->setSuccessor(Op, BEBlock);
478485 }
486 BEBlock->getTerminator()->setMetadata(LoopMDKind, LoopMD);
479487
480488 //===--- Update all analyses which we must preserve now -----------------===//
481489
0 ; RUN: opt -loop-simplify -S < %s | FileCheck %s
1
2 define void @test1(i32 %n) {
3 entry:
4 br label %while.cond
5
6 while.cond: ; preds = %if.then, %if.else, %entry
7 %count.0 = phi i32 [ 0, %entry ], [ %add, %if.then ], [ %add2, %if.else ]
8 %cmp = icmp ugt i32 %count.0, %n
9 br i1 %cmp, label %while.end, label %while.body
10
11 while.body: ; preds = %while.cond
12 %rem = and i32 %count.0, 1
13 %cmp1 = icmp eq i32 %rem, 0
14 br i1 %cmp1, label %if.then, label %if.else
15
16 if.then: ; preds = %while.body
17 %add = add i32 %count.0, 1
18 br label %while.cond, !llvm.loop !0
19
20 if.else: ; preds = %while.body
21 %add2 = add i32 %count.0, 2
22 br label %while.cond, !llvm.loop !0
23
24 while.end: ; preds = %while.cond
25 ret void
26 }
27
28 ; CHECK: if.then
29 ; CHECK-NOT: br {{.*}}!llvm.loop{{.*}}
30
31 ; CHECK: while.cond.backedge:
32 ; CHECK: br label %while.cond, !llvm.loop !0
33
34 ; CHECK: if.else
35 ; CHECK-NOT: br {{.*}}!llvm.loop{{.*}}
36
37
38 !0 = distinct !{!0, !1}
39 !1 = !{!"llvm.loop.distribute.enable", i1 true}
40 ; CHECK: !0 = distinct !{!0, !1}
41 ; CHECK: !1 = !{!"llvm.loop.distribute.enable", i1 true}
0 ; RUN: opt -simplifycfg -S < %s | FileCheck %s
1
2 define void @test1(i32 %n) #0 {
3 entry:
4 %n.addr = alloca i32, align 4
5 %count = alloca i32, align 4
6 store i32 %n, i32* %n.addr, align 4
7 %0 = bitcast i32* %count to i8*
8 store i32 0, i32* %count, align 4
9 br label %while.cond
10
11 while.cond: ; preds = %if.end, %entry
12 %1 = load i32, i32* %count, align 4
13 %2 = load i32, i32* %n.addr, align 4
14 %cmp = icmp ule i32 %1, %2
15 br i1 %cmp, label %while.body, label %while.end
16
17 while.body: ; preds = %while.cond
18 %3 = load i32, i32* %count, align 4
19 %rem = urem i32 %3, 2
20 %cmp1 = icmp eq i32 %rem, 0
21 br i1 %cmp1, label %if.then, label %if.else
22
23 if.then: ; preds = %while.body
24 %4 = load i32, i32* %count, align 4
25 %add = add i32 %4, 1
26 store i32 %add, i32* %count, align 4
27 br label %if.end
28
29 ; CHECK: if.then:
30 ; CHECK: br label %while.cond, !llvm.loop !0
31
32 if.else: ; preds = %while.body
33 %5 = load i32, i32* %count, align 4
34 %add2 = add i32 %5, 2
35 store i32 %add2, i32* %count, align 4
36 br label %if.end
37
38 ; CHECK: if.else:
39 ; CHECK: br label %while.cond, !llvm.loop !0
40
41 if.end: ; preds = %if.else, %if.then
42 br label %while.cond, !llvm.loop !0
43
44 while.end: ; preds = %while.cond
45 %6 = bitcast i32* %count to i8*
46 ret void
47 }
48
49 !0 = distinct !{!0, !1}
50 !1 = !{!"llvm.loop.distribute.enable", i1 true}
51 ; CHECK: !0 = distinct !{!0, !1}
52 ; CHECK: !1 = !{!"llvm.loop.distribute.enable", i1 true}