Skip to content

Commit

Permalink
Merge pull request swiftlang#4819 from trentxintong/ACM
Browse files Browse the repository at this point in the history
ACM compilation time improvement
  • Loading branch information
trentxintong authored Sep 16, 2016
2 parents 0c68d0f + d00cec7 commit 38c5fbf
Showing 3 changed files with 46 additions and 10 deletions.
36 changes: 36 additions & 0 deletions lib/SILOptimizer/Transforms/ARCCodeMotion.cpp
Original file line number Diff line number Diff line change
@@ -316,6 +316,17 @@ class RetainCodeMotionContext : public CodeMotionContext {
return false;
}

/// Return the previous instruction if it happens to be a retain with the
/// given RC root, nullptr otherwise.
SILInstruction *getPrevReusableInst(SILInstruction *I, SILValue Root) {
if (&*I->getParent()->begin() == I)
return nullptr;
auto Prev = &*std::prev(SILBasicBlock::iterator(I));
if (isRetainInstruction(Prev) && getRCRoot(Prev) == Root)
return Prev;
return nullptr;
}

public:
/// Constructor.
RetainCodeMotionContext(llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
@@ -463,6 +474,13 @@ bool RetainCodeMotionContext::performCodeMotion() {
if (Iter == InsertPoints.end())
continue;
for (auto IP : Iter->second) {
// we are about to insert a new retain instruction before the insertion
// point. Check if the previous instruction is reuseable, reuse it, do
// not insert new instruction and delete old one.
if (auto I = getPrevReusableInst(IP, Iter->first)) {
RCInstructions.erase(I);
continue;
}
createIncrementBefore(Iter->first, IP);
Changed = true;
}
@@ -645,6 +663,17 @@ class ReleaseCodeMotionContext : public CodeMotionContext {
return false;
}

/// Return the successor instruction if it happens to be a release with the
/// given RC root, nullptr otherwise.
SILInstruction *getPrevReusableInst(SILInstruction *I, SILValue Root) {
if (&*I->getParent()->begin() == I)
return nullptr;
auto Prev = &*std::prev(SILBasicBlock::iterator(I));
if (isReleaseInstruction(Prev) && getRCRoot(Prev) == Root)
return Prev;
return nullptr;
}

public:
/// Constructor.
ReleaseCodeMotionContext(llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
@@ -829,6 +858,13 @@ bool ReleaseCodeMotionContext::performCodeMotion() {
if (Iter == InsertPoints.end())
continue;
for (auto IP : Iter->second) {
// we are about to insert a new release instruction before the insertion
// point. Check if the successor instruction is reuseable, reuse it, do
// not insert new instruction and delete old one.
if (auto I = getPrevReusableInst(IP, Iter->first)) {
RCInstructions.erase(I);
continue;
}
createDecrementBefore(Iter->first, IP);
Changed = true;
}
14 changes: 7 additions & 7 deletions test/SILOptimizer/earlycodemotion.sil
Original file line number Diff line number Diff line change
@@ -183,7 +183,7 @@ bb3(%6 : $Builtin.Int32):
// CHECK-NEXT: dealloc_stack
// CHECK-NEXT: switch_enum
// CHECK: bb1({{.*}}):
// CHECK-NEXT: retain_value
// CHECK: strong_retain
// CHECK: bb2:
// CHECK-NOT: unchecked_enum_data
// CHECK-NOT: retain_value
@@ -382,7 +382,7 @@ bb3:
// CHECK-NEXT: select_enum
// CHECK-NEXT: cond_br
// CHECK: bb1:
// CHECK-NEXT: retain_value
// CHECK: strong_retain
// CHECK: bb2:
// CHECK-NOT: unchecked_enum_data
// CHECK-NOT: retain_value
@@ -800,9 +800,9 @@ bb3:
//
// CHECK-LABEL: sil @enum_simplification_test6b : $@convention(thin) (Optional<Builtin.NativeObject>) -> () {
// CHECK: bb1:
// CHECK: retain_value
// CHECK: strong_retain
// CHECK: bb2:
// CHECK: retain_value
// CHECK: strong_retain
// CHECK: bb3:
// CHECK-NOT: retain_value
sil @enum_simplification_test6b : $@convention(thin) (Optional<Builtin.NativeObject>) -> () {
@@ -882,7 +882,7 @@ bb4:
// CHECK-LABEL: sil @enum_simplification_test9 : $@convention(thin) (Optional<Builtin.NativeObject>) -> () {
// CHECK: bb0([[INPUT:%[0-9]+]] : $Optional<Builtin.NativeObject>):
// CHECK: bb1:
// CHECK-NEXT: retain_value [[INPUT]]
// CHECK: strong_retain
// CHECK: bb2:
// CHECK-NOT: retain
// CHECK: bb3:
@@ -911,7 +911,7 @@ bb3:
// CHECK-LABEL: sil @enum_simplification_test9_enums : $@convention(thin) (Optional<Builtin.NativeObject>) -> () {
// CHECK: bb0([[INPUT:%[0-9]+]] : $Optional<Builtin.NativeObject>):
// CHECK: bb1:
// CHECK-NEXT: retain_value
// CHECK: strong_retain
// CHECK: bb2:
// CHECK-NOT: retain
// CHECK: bb3:
@@ -1508,7 +1508,7 @@ bb2(%9 : $fuzz):
//
// CHECK-LABEL: retain_blocked_by_copyaddr
// CHECK: bb0
// CHECK-NEXT: strong_retain
// CHECK-NEXT: retain_value
sil hidden @retain_blocked_by_copyaddr : $@convention(thin) <T> (@in T, B) -> @out T {
bb0(%0 : $*T, %1 : $*T, %2 : $B):
retain_value %2 : $B
6 changes: 3 additions & 3 deletions test/SILOptimizer/latecodemotion.sil
Original file line number Diff line number Diff line change
@@ -359,7 +359,7 @@ bb3(%6 : $Builtin.Int32):
// CHECK-NEXT: dealloc_stack
// CHECK-NEXT: switch_enum
// CHECK: bb1({{.*}}):
// CHECK-NEXT: retain_value
// CHECK: strong_retain
// CHECK: bb2:
// CHECK-NOT: unchecked_enum_data
// CHECK-NOT: retain_value
@@ -558,7 +558,7 @@ bb3:
// CHECK-NEXT: select_enum
// CHECK-NEXT: cond_br
// CHECK: bb1:
// CHECK-NEXT: retain_value
// CHECK: strong_retain
// CHECK-NOT: retain_value
// CHECK: bb2:
// CHECK-NOT: unchecked_enum_data
@@ -855,7 +855,7 @@ bb0(%0 : $FakeOptional<Builtin.NativeObject>):

// CHECK-LABEL: sil @enum_simplification_test2 : $@convention(thin) (FakeOptional<Builtin.NativeObject>) -> () {
// CHECK: bb0
// CHECK: retain_value
// CHECK: strong_retain
// CHECK: release_value
sil @enum_simplification_test2 : $@convention(thin) (FakeOptional<Builtin.NativeObject>) -> () {
bb0(%0 : $FakeOptional<Builtin.NativeObject>):

0 comments on commit 38c5fbf

Please sign in to comment.