From ea3364bf85ef72001b8db3c6fd2dc34f9311b111 Mon Sep 17 00:00:00 2001 From: Quentin Colombet Date: Thu, 18 Apr 2019 18:28:30 +0000 Subject: [BlockExtractor] Extend the file format to support the grouping of basic blocks Prior to this patch, each basic block listed in the extrack-blocks-file would be extracted to a different function. This patch adds the support for comma separated list of basic blocks to form group. When the region formed by a group is not extractable, e.g., not single entry, all the blocks of that group are left untouched. Let us see this new format in action (comments are not part of the file format): ;; funcName bbName[,bbName...] foo bb1 ;; Extract bb1 in its own function foo bb2,bb3 ;; Extract bb2,bb3 in their own function bar bb1,bb4 ;; Extract bb1,bb4 in their own function bar bb2 ;; Extract bb2 in its own function Assuming all regions are extractable, this will create one function and thus one call per region. Differential Revision: https://reviews.llvm.org/D60746 llvm-svn: 358701 --- .../BlockExtractor/extract-blocks-with-groups.ll | 122 +++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 llvm/test/Transforms/BlockExtractor/extract-blocks-with-groups.ll (limited to 'llvm/test/Transforms/BlockExtractor') diff --git a/llvm/test/Transforms/BlockExtractor/extract-blocks-with-groups.ll b/llvm/test/Transforms/BlockExtractor/extract-blocks-with-groups.ll new file mode 100644 index 00000000000..0c5b173c1bf --- /dev/null +++ b/llvm/test/Transforms/BlockExtractor/extract-blocks-with-groups.ll @@ -0,0 +1,122 @@ +; Extract the 'if', 'then', and 'else' blocks into the same function. +; RUN: echo 'foo if,then,else' > %t +; Make sure we can still extract a single basic block +; RUN: echo 'foo end' >> %t +; RUN: echo 'bar bb14,bb20' >> %t +; RUN: opt -S -extract-blocks -extract-blocks-file=%t %s | FileCheck %s + +; CHECK-LABEL: foo +; +; CHECK: if: +; The diamond will produce an i32 value, make sure we account for that. +; CHECK: [[RES_VAL_ADDR:%[^ ]*]] = alloca i32 +; CHECK-NEXT: br label %[[FOO_DIAMOND_LABEL:.*$]] +; +; The if-then-else blocks should be in just one function. +; CHECK: [[FOO_DIAMOND_LABEL]]: +; CHECK: call void [[FOO_DIAMOND:@[^(]*]](i32 %arg1, i32 %arg, i32* [[RES_VAL_ADDR]]) +; CHECK-NEXT: [[RES_VAL:%[^ ]*]] = load i32, i32* [[RES_VAL_ADDR]] +; Then it should directly jump to end. +; CHECK: br label %[[FOO_END_LABEL:.*$]] +; +; End should have been extracted into its own function. +; CHECK: [[FOO_END_LABEL]]: +; CHECK: [[CMP:%[^ ]*]] = call i1 [[FOO_END:@[^(]*]](i32 [[RES_VAL]], i32 %arg) +; CHECK-NEXT: br i1 [[CMP]], label %ret0, label %ret1 +define i32 @foo(i32 %arg, i32 %arg1) { +if: + %tmp5 = icmp sgt i32 %arg, 0 + %tmp8 = icmp sgt i32 %arg1, 0 + %or.cond = and i1 %tmp5, %tmp8 + br i1 %or.cond, label %then, label %else + +then: + %tmp12 = shl i32 %arg1, 2 + %tmp13 = add nsw i32 %tmp12, %arg + br label %end + +else: + %tmp22 = mul nsw i32 %arg, 3 + %tmp24 = sdiv i32 %arg1, 6 + %tmp25 = add nsw i32 %tmp24, %tmp22 + br label %end + +end: + %tmp.0 = phi i32 [ %tmp13, %then ], [ %tmp25, %else ] + %and0 = and i32 %tmp.0, %arg + %cmp1 = icmp slt i32 %and0, 0 + br i1 %cmp1, label %ret0, label %ret1 + +ret0: + ret i32 0 + +ret1: + ret i32 1 +} + +; CHECK-LABEL: bar +; +; Check that we extracted bb14 and bb20 in their own (shared) function. +; CHECK: bb +; CHECK: br i1 %or.cond, label %bb9, label %[[BAR_DIAMOND_LABEL:.*$]] +; +; CHECK: [[BAR_DIAMOND_LABEL]]: +; CHECK: [[CMP:%[^ ]*]] = call i1 [[BAR_DIAMOND:@[^(]*]](i32 %arg1, i32 %arg, i32* +; CHECK: br i1 [[CMP]], label %bb26, label %bb30 +define i32 @bar(i32 %arg, i32 %arg1) { +bb: + %tmp5 = icmp sgt i32 %arg, 0 + %tmp8 = icmp sgt i32 %arg1, 0 + %or.cond = and i1 %tmp5, %tmp8 + br i1 %or.cond, label %bb9, label %bb14 + +bb9: ; preds = %bb + %tmp12 = shl i32 %arg1, 2 + %tmp13 = add nsw i32 %tmp12, %arg + br label %bb30 + +bb14: ; preds = %bb + %0 = and i32 %arg1, %arg + %1 = icmp slt i32 %0, 0 + br i1 %1, label %bb20, label %bb26 + +bb20: ; preds = %bb14 + %tmp22 = mul nsw i32 %arg, 3 + %tmp24 = sdiv i32 %arg1, 6 + %tmp25 = add nsw i32 %tmp24, %tmp22 + br label %bb30 + +bb26: ; preds = %bb14 + %tmp29 = sub nsw i32 %arg, %arg1 + br label %bb30 + +bb30: ; preds = %bb26, %bb20, %bb9 + %tmp.0 = phi i32 [ %tmp13, %bb9 ], [ %tmp25, %bb20 ], [ %tmp29, %bb26 ] + ret i32 %tmp.0 +} + +; Check that we extracted the three asked basic blocks. +; CHECK: [[FOO_DIAMOND]] +; CHECK: then: +; Make sure the function doesn't end in the middle of the +; list of the blocks we are checking. +; CHECK-NOT: } +; CHECK: else: +; CHECK-NOT: } +; The name of the if block is weird because it had to be split +; since it was the name of the entry of the original function. +; CHECK: if.split: +; CHECK: } + +; CHECK: [[FOO_END]] +; CHECK-NOT: } +; CHECK: end: +; CHECK: } + +; Check that we extracted the two asked basic blocks. +; CHECK: [[BAR_DIAMOND]] +; CHECK-NOT: } +; CHECK: bb14: +; CHECK-NOT: } +; CHECK: bb20: +; CHECK: } -- cgit v1.2.3