From b7a48d833a3fbd3b881ff16807734046b0d01ce8 Mon Sep 17 00:00:00 2001 From: Davide Italiano Date: Wed, 23 Aug 2017 09:14:37 +0000 Subject: [PATCH] [InstCombine] Fold branches with irrelevant conditions to a constant. InstCombine folds instructions with irrelevant conditions to undef. This, as Nuno confirmed is a bug. (see https://bugs.llvm.org/show_bug.cgi?id=33409#c1 ) Given the original motivation for the change is that of removing an USE, we now fold to false instead (which reaches the same goal without undesired side effects). Fixes PR33409. Differential Revision: https://reviews.llvm.org/D36975 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@311540 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/InstCombine/InstructionCombining.cpp | 7 +++---- test/Transforms/InstCombine/branch.ll | 13 ++++++++++++- test/Transforms/InstCombine/pr33765.ll | 2 +- test/Transforms/InstCombine/select-cmp-br.ll | 2 +- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index 6690765c09b..1881f78255b 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2260,10 +2260,9 @@ Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { // If the condition is irrelevant, remove the use so that other // transforms on the condition become more effective. - if (BI.isConditional() && - BI.getSuccessor(0) == BI.getSuccessor(1) && - !isa(BI.getCondition())) { - BI.setCondition(UndefValue::get(BI.getCondition()->getType())); + if (BI.isConditional() && !isa(BI.getCondition()) && + BI.getSuccessor(0) == BI.getSuccessor(1)) { + BI.setCondition(ConstantInt::getFalse(BI.getCondition()->getType())); return &BI; } diff --git a/test/Transforms/InstCombine/branch.ll b/test/Transforms/InstCombine/branch.ll index 5be93cc2023..2168c9fd9a1 100644 --- a/test/Transforms/InstCombine/branch.ll +++ b/test/Transforms/InstCombine/branch.ll @@ -1,10 +1,12 @@ +; Check that we fold the condition of branches of the +; form: br dest1, dest2, where dest1 == dest2. ; RUN: opt -instcombine -S < %s | FileCheck %s define i32 @test(i32 %x) { ; CHECK-LABEL: @test entry: ; CHECK-NOT: icmp -; CHECK: br i1 undef, +; CHECK: br i1 false %cmp = icmp ult i32 %x, 7 br i1 %cmp, label %merge, label %merge merge: @@ -13,4 +15,13 @@ merge: ret i32 %x } +@global = global i8 0 +define i32 @pat(i32 %x) { +; CHECK-NOT: icmp false +; CHECK: br i1 false + %y = icmp eq i32 27, ptrtoint(i8* @global to i32) + br i1 %y, label %patatino, label %patatino +patatino: + ret i32 %x +} diff --git a/test/Transforms/InstCombine/pr33765.ll b/test/Transforms/InstCombine/pr33765.ll index 99ed0d13b5c..5aa99bbf7cb 100644 --- a/test/Transforms/InstCombine/pr33765.ll +++ b/test/Transforms/InstCombine/pr33765.ll @@ -6,7 +6,7 @@ define void @patatino(i8 %beth) { ; CHECK-LABEL: @patatino( ; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[BETH:%.*]] to i32 -; CHECK-NEXT: br i1 undef, label [[IF_THEN9:%.*]], label [[IF_THEN9]] +; CHECK-NEXT: br i1 false, label [[IF_THEN9:%.*]], label [[IF_THEN9]] ; CHECK: if.then9: ; CHECK-NEXT: [[MUL:%.*]] = mul nuw nsw i32 [[CONV]], [[CONV]] ; CHECK-NEXT: [[TINKY:%.*]] = load i16, i16* @glob, align 2 diff --git a/test/Transforms/InstCombine/select-cmp-br.ll b/test/Transforms/InstCombine/select-cmp-br.ll index 59384ab7b1f..06f32828c96 100644 --- a/test/Transforms/InstCombine/select-cmp-br.ll +++ b/test/Transforms/InstCombine/select-cmp-br.ll @@ -248,7 +248,7 @@ bb5: ; preds = %entry define i32 @test6(i32 %arg, i1 %arg1) { ; CHECK-LABEL: @test6( ; CHECK-NEXT: entry: -; CHECK-NEXT: br i1 undef, label [[BB:%.*]], label [[BB]] +; CHECK-NEXT: br i1 false, label [[BB:%.*]], label [[BB]] ; CHECK: bb: ; CHECK-NEXT: [[TMP:%.*]] = select i1 [[ARG1:%.*]], i32 [[ARG:%.*]], i32 0 ; CHECK-NEXT: ret i32 [[TMP]] -- 2.50.1