Index: lib/Transforms/Scalar/LowerExpectIntrinsic.cpp =================================================================== --- lib/Transforms/Scalar/LowerExpectIntrinsic.cpp +++ lib/Transforms/Scalar/LowerExpectIntrinsic.cpp @@ -83,9 +83,7 @@ return true; } -static bool handleBranchExpect(BranchInst &BI) { - if (BI.isUnconditional()) - return false; +template static bool handleBrSelExpect(BrSelInst &BI) { // Handle non-optimized IR code like: // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1) @@ -138,6 +136,13 @@ return true; } +static bool handleBranchExpect(BranchInst &BI) { + if (BI.isUnconditional()) + return false; + + return handleBrSelExpect(BI); +} + static bool lowerExpectIntrinsic(Function &F) { bool Changed = false; @@ -151,6 +156,12 @@ ExpectIntrinsicsHandled++; } + for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) { + if (SelectInst *SI = dyn_cast(BI++)) + if (handleBrSelExpect(*SI)) + ExpectIntrinsicsHandled++; + } + // Remove llvm.expect intrinsics. for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) { CallInst *CI = dyn_cast(BI++); Index: test/Transforms/LowerExpectIntrinsic/basic.ll =================================================================== --- test/Transforms/LowerExpectIntrinsic/basic.ll +++ test/Transforms/LowerExpectIntrinsic/basic.ll @@ -273,6 +273,19 @@ ret i32 %0 } +; CHECK-LABEL: @test10( +define i32 @test10(i32, i32, i32) #0 { + %4 = and i32 %0, 3 + %5 = icmp eq i32 %4, 1 + %6 = zext i1 %5 to i64 + %7 = call i64 @llvm.expect.i64(i64 %6, i64 0) + %8 = icmp ne i64 %7, 0 + %9 = select i1 %8, i32 %1, i32 %2 +; CHECK: select{{.*}}, !prof !1 + ret i32 %9 +} + + declare i1 @llvm.expect.i1(i1, i1) nounwind readnone ; CHECK: !0 = !{!"branch_weights", i32 2000, i32 1}