Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,30 @@ import SIL
let booleanLiteralFolding = FunctionPass(name: "boolean-literal-folding") {
(function: Function, context: FunctionPassContext) in

var changed = false
for block in function.blocks {
if let condBr = block.terminator as? CondBranchInst {
fold(condBranch: condBr, context)
changed = fold(condBranch: condBr, context) || changed
}
}
if changed {
_ = context.removeDeadBlocks(in: function)
}
}

private func fold(condBranch: CondBranchInst, _ context: FunctionPassContext) {
private func fold(condBranch: CondBranchInst, _ context: FunctionPassContext) -> Bool {
guard let structExtract = condBranch.condition as? StructExtractInst,
let initApply = structExtract.struct as? ApplyInst,
initApply.hasSemanticsAttribute("bool.literal_init"),
initApply.arguments.count == 2,
let literal = initApply.arguments[0] as? IntegerLiteralInst,
let literalValue = literal.value else
{
return
return false
}

let builder = Builder(before: condBranch, context)
builder.createBranch(to: literalValue == 0 ? condBranch.falseBlock : condBranch.trueBlock)
context.erase(instruction: condBranch)
return true
}
9 changes: 8 additions & 1 deletion test/SILOptimizer/boolean-literal-folding.sil
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ sil public_external [_semantics "bool.literal_init"] @wrong_bool_literal_init :
// CHECK: struct_extract
// CHECK-NEXT: br bb1
// CHECK: bb1:
// CHECK-NEXT: debug_step
// CHECK-NEXT: br bb2
// CHECK-NOT: bb3
// CHECK: } // end sil function 'replace_true'
sil [ossa] @replace_true : $@convention(thin) () -> () {
bb0:
Expand All @@ -25,6 +28,7 @@ bb0:
%4 = struct_extract %3 : $Bool, #Bool._value
cond_br %4, bb1, bb2
bb1:
debug_step
br bb3
bb2:
br bb3
Expand All @@ -35,8 +39,10 @@ bb3:

// CHECK-LABEL: sil [ossa] @replace_false :
// CHECK: struct_extract
// CHECK-NEXT: br bb2
// CHECK-NEXT: br bb1
// CHECK: bb1:
// CHECK-NEXT: br bb2
// CHECK-NOT: bb3
// CHECK: } // end sil function 'replace_false'
sil [ossa] @replace_false : $@convention(thin) () -> () {
bb0:
Expand All @@ -47,6 +53,7 @@ bb0:
%4 = struct_extract %3 : $Bool, #Bool._value
cond_br %4, bb1, bb2
bb1:
debug_step
br bb3
bb2:
br bb3
Expand Down
6 changes: 3 additions & 3 deletions test/SILOptimizer/discard_checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ struct Basics: ~Copyable {
repeat {
switch c {
case .red:
fatalError("bah!") // expected-error {{must consume 'self' before exiting method that discards self}}
fatalError("bah!")
case .blue:
throw E.someError // expected-error {{must consume 'self' before exiting method that discards self}}
throw E.someError
case .green:
self = Basics()
default: print("hi")
}
} while true
discard self // expected-note 2{{discarded self here}}
discard self
}

consuming func test2_fixed(_ c: Color) throws {
Expand Down