Index: lib/Transforms/Utils/Local.cpp =================================================================== --- lib/Transforms/Utils/Local.cpp +++ lib/Transforms/Utils/Local.cpp @@ -1310,7 +1310,7 @@ // Assumptions that are known to be false are equivalent to unreachable. // Also, if the condition is undefined, then we make the choice most // beneficial to the optimizer, and choose that to also be unreachable. - if (IntrinsicInst *II = dyn_cast(BBI)) + if (IntrinsicInst *II = dyn_cast(BBI)) { if (II->getIntrinsicID() == Intrinsic::assume) { bool MakeUnreachable = false; if (isa(II->getArgOperand(0))) @@ -1327,6 +1327,24 @@ } } + if (II->getIntrinsicID() == Intrinsic::experimental_guard) { + // An call to the guard intrinsic bails out of the current compilation + // unit if the predicate passed to it is false. If the predicate is a + // constant false, then we know the guard will bail out of the current + // compile unconditionally, so all code following it is dead. + // + // Note: unlike in llvm.assume, it is not "obviously profitable" for + // guards to treat `undef` as `false` since a guard on `undef` can + // still be useful for widening. + if (auto *CI = dyn_cast(II->getArgOperand(0))) + if (CI->isZero() && !isa(II->getNextNode())) { + changeToUnreachable(II->getNextNode(), /* UseLLVMTrap = */ false); + Changed = true; + break; + } + } + } + if (CallInst *CI = dyn_cast(BBI)) { if (CI->doesNotReturn()) { // If we found a call to a no-return function, insert an unreachable Index: test/Transforms/SimplifyCFG/guards.ll =================================================================== --- /dev/null +++ test/Transforms/SimplifyCFG/guards.ll @@ -0,0 +1,93 @@ +; RUN: opt -S -simplifycfg < %s | FileCheck %s + +declare void @llvm.experimental.guard(i1, ...) + +define i32 @f_0(i1 %c) { +; CHECK-LABEL: @f_0( +; CHECK-NEXT: entry: +; CHECK: true: +; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ] +; CHECK-NEXT: unreachable +entry: + br i1 %c, label %true, label %false + +true: + call void(i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ] + ret i32 10 + +false: + ret i32 20 +} + +define i32 @f_1(i1 %c) { +; Demonstrate that we do not yet simplify a guard on undef + +; CHECK-LABEL: @f_1( +; CHECK: ret i32 10 +; CHECK: ret i32 20 + +entry: + br i1 %c, label %true, label %false + +true: + call void(i1, ...) @llvm.experimental.guard(i1 undef) [ "deopt"() ] + ret i32 10 + +false: + ret i32 20 +} + +define i32 @f_2(i1 %c, i32* %buf) { +; CHECK-LABEL: @f_2( +entry: + br i1 %c, label %true, label %false + +true: + call void(i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ] + %val = load i32, i32* %buf + br label %false + +false: + %to.return = phi i32 [ %val, %true ], [ 50, %entry ] + ret i32 %to.return +; CHECK: true: +; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ] +; CHECK-NEXT: unreachable + +; CHECK: false: +; CHECK-NEXT: ret i32 50 +} + +define i32 @f_3(i1* %c, i32* %buf) { +; CHECK-LABEL: @f_3( +entry: + %c0 = load volatile i1, i1* %c + br i1 %c0, label %true, label %false + +true: + call void(i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ] + %val = load i32, i32* %buf + %c2 = load volatile i1, i1* %c + br i1 %c2, label %left, label %right + +false: + %c1 = load volatile i1, i1* %c + br i1 %c1, label %left, label %right + +left: + %val.left = phi i32 [ %val, %true ], [ 50, %false ] + ret i32 %val.left + +right: + %val.right = phi i32 [ %val, %true ], [ 100, %false ] + ret i32 %val.right + +; CHECK: true: +; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ] +; CHECK-NEXT: unreachable + +; CHECK: false: +; CHECK-NEXT: %c1 = load volatile i1, i1* %c +; CHECK-NEXT: [[VAL:%[^ ]]] = select i1 %c1, i32 50, i32 100 +; CHECK-NEXT: ret i32 [[VAL]] +}