Index: lib/Transforms/Scalar/LoopUnswitch.cpp =================================================================== --- lib/Transforms/Scalar/LoopUnswitch.cpp +++ lib/Transforms/Scalar/LoopUnswitch.cpp @@ -31,6 +31,7 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/GlobalsModRef.h" +#include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/AssumptionCache.h" #include "llvm/Analysis/CodeMetrics.h" #include "llvm/Analysis/InstructionSimplify.h" @@ -899,7 +900,6 @@ if (I.mayHaveSideEffects()) return false; - // FIXME: add check for constant foldable switch instructions. if (BranchInst *BI = dyn_cast(CurrentTerm)) { if (BI->isUnconditional()) { CurrentBB = BI->getSuccessor(0); @@ -911,7 +911,38 @@ // Found a trivial condition candidate: non-foldable conditional branch. break; } - } else { + } else if (SwitchInst *SI = dyn_cast(CurrentTerm)) { + // Either the Cond is a constant or we try to fold it into a constant. + Value *Cond = SI->getCondition(); + if (!isa(Cond)) { + Instruction *I = dyn_cast(Cond); + if (!I) + break; + const DataLayout &DL = I->getModule()->getDataLayout(); + if (Constant *Fold = ConstantFoldInstruction(I, DL)) + Cond = Fold; + else + break; + } + // At this point, the switched value is a constant. + assert(isa(Cond) && "Switched value must be a constant"); + bool FoundDest = false; + for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); + i != e; ++i) { + // The case value should have the same type as the switched value. + assert(i.getCaseValue()->getType() == Cond->getType() && + "Switched value has different types as the case value"); + if (Cond == i.getCaseValue()) { + CurrentBB = i.getCaseSuccessor(); + FoundDest = true; + break; + } + } + // The default case is taken. + if (!FoundDest) + CurrentBB = SI->getDefaultDest(); + } else { + // We do not understand this terminator instructions. break; } Index: test/Transforms/LoopUnswitch/trivial-unswitch.ll =================================================================== --- test/Transforms/LoopUnswitch/trivial-unswitch.ll +++ test/Transforms/LoopUnswitch/trivial-unswitch.ll @@ -44,4 +44,54 @@ ret i32 0 } -declare void @some_func() noreturn \ No newline at end of file + +; We should not trivially unswitch on the SwitchInst. Instead, we +; should be able to figure out that the switch can be folded into +; a unconditional branch to %continue. Then we unswitch on the br +; inst in %continue. +; +; CHECK: define i32 @test2( + +; This is an indication that the SwitchInst is unswitched upon, +; i.e. an ICmpInst is generated by the EmitPreheaderBranchOnCondition +; on the unswitched case value which leads to a trivial loop exit. +; +; CHECK-NOT: icmp +; CHECK: br i1 %cond1, label %..split_crit_edge, label %.loop_exit.split_crit_edge + +; CHECK: ..split_crit_edge: ; preds = %0 +; CHECK: br label %.split + +; CHECK: .split: ; preds = %..split_crit_edge +; CHECK: br label %loop_begin + +; CHECK: loop_begin: ; preds = %do_something, %.split +; CHECK: switch i32 + +; CHECK: continue: ; preds = %loop_begin +; CHECK: %var_val = load i32, i32* %var +; CHECK: br i1 true, label %do_something, label %loop_exit + +define i32 @test2(i32* %var, i1 %cond1) { + %sub = sub i32 6, 5 + br label %loop_begin + +loop_begin: + switch i32 %sub, label %continue [ + i32 0, label %loop_exit + i32 1, label %continue + ] + +continue: + %var_val = load i32, i32* %var + br i1 %cond1, label %do_something, label %loop_exit + +do_something: + call void @some_func() noreturn nounwind + br label %loop_begin + +loop_exit: + ret i32 0 +} + +declare void @some_func() noreturn