From 3aa5468391f6e530d551b12358697412a2d3296d Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Fri, 29 Apr 2016 22:23:16 +0000 Subject: [PATCH] Mark guards on true as "trivially dead" This moves some logic added to EarlyCSE in rL268120 into `llvm::isInstructionTriviallyDead`. Adds a test case for DCE to demonstrate that passes other than EarlyCSE can now pick up on the new information. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268126 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/EarlyCSE.cpp | 11 ++--------- lib/Transforms/Utils/Local.cpp | 8 ++++++-- test/Transforms/DCE/guards.ll | 11 +++++++++++ 3 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 test/Transforms/DCE/guards.ll diff --git a/lib/Transforms/Scalar/EarlyCSE.cpp b/lib/Transforms/Scalar/EarlyCSE.cpp index c453c843bfc..2e00d676f15 100644 --- a/lib/Transforms/Scalar/EarlyCSE.cpp +++ b/lib/Transforms/Scalar/EarlyCSE.cpp @@ -555,15 +555,8 @@ bool EarlyCSE::processNode(DomTreeNode *Node) { } if (match(Inst, m_Intrinsic())) { - Value *Cond = cast(Inst)->getArgOperand(0); - - if (match(Cond, m_One())) { - // Elide guards on true, since operationally they're no-ops. In the - // future we can consider more sophisticated tradeoffs here with - // consideration to potential for check widening, but for now we keep - // things simple. - Inst->eraseFromParent(); - } else if (auto *CondI = dyn_cast(Cond)) { + if (auto *CondI = + dyn_cast(cast(Inst)->getArgOperand(0))) { // The condition we're on guarding here is true for all dominated // locations. if (SimpleValue::canHandle(CondI)) diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index 82961a678be..5af3712a3ab 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -321,8 +321,12 @@ bool llvm::isInstructionTriviallyDead(Instruction *I, II->getIntrinsicID() == Intrinsic::lifetime_end) return isa(II->getArgOperand(1)); - // Assumptions are dead if their condition is trivially true. - if (II->getIntrinsicID() == Intrinsic::assume) { + // Assumptions are dead if their condition is trivially true. Guards on + // true are operationally no-ops. In the future we can consider more + // sophisticated tradeoffs for guards considering potential for check + // widening, but for now we keep things simple. + if (II->getIntrinsicID() == Intrinsic::assume || + II->getIntrinsicID() == Intrinsic::experimental_guard) { if (ConstantInt *Cond = dyn_cast(II->getArgOperand(0))) return !Cond->isZero(); diff --git a/test/Transforms/DCE/guards.ll b/test/Transforms/DCE/guards.ll new file mode 100644 index 00000000000..d39c44058a7 --- /dev/null +++ b/test/Transforms/DCE/guards.ll @@ -0,0 +1,11 @@ +; RUN: opt -dce -S < %s | FileCheck %s + +declare void @llvm.experimental.guard(i1,...) + +define void @f(i32 %val) { +; CHECK-LABEL: @f( +; CHECK-NEXT: ret void + %val2 = add i32 %val, 1 + call void(i1, ...) @llvm.experimental.guard(i1 true) [ "deopt"(i32 %val2) ] + ret void +} -- 2.11.0