Index: lib/Transforms/Scalar/LowerExpectIntrinsic.cpp =================================================================== --- lib/Transforms/Scalar/LowerExpectIntrinsic.cpp +++ lib/Transforms/Scalar/LowerExpectIntrinsic.cpp @@ -83,6 +83,138 @@ return true; } +/// Handler routine for PHINode instructions that define +/// Expect instrinsic's arg values. +/// +/// Handle PHI node which define the value of the the builtin_expect's +/// argument value: If the operand of the phi has a constant value and +/// it 'contradicts' with the expected value of phi def, then the +/// corresponding incoming edgeof the phi is unlikely to be taken. Using +/// that information, the branch probability info for the originating +/// branch can be inferred. +static void handlePhiDef(CallInst *Expect) { + Value &Arg = *Expect->getArgOperand(0); + ConstantInt *ExpectedValue = dyn_cast(Expect->getArgOperand(1)); + assert(ExpectedValue); + bool ValueInverted = false; + // + // Walk up in backward a list of instructions that + // have 'copy' semantics by 'stripping' the copies + // until a PHI node or an instruction of unknown kind + // is reached. Negation via xor is also handled. + // + // C = PHI(...); + // B = C; + // A = B; + // D = __builtin_expect(A, 0); + // + Value *V = &Arg; + while (!isa(V)) { + if (ZExtInst *ZExt = dyn_cast(V)) { + V = ZExt->getOperand(0); + continue; + } + + if (SExtInst *SExt = dyn_cast(V)) { + V = SExt->getOperand(0); + continue; + } + + BinaryOperator *BinOp = dyn_cast(V); + if (!BinOp || BinOp->getOpcode() != Instruction::Xor) + break; + + // Handle XOR: + Value *Opnd0 = BinOp->getOperand(0); + Value *Opnd1 = BinOp->getOperand(1); + ConstantInt *CInt = dyn_cast(Opnd1); + if (!CInt) + break; + + V = Opnd0; + if (!CInt->isZero()) { + if (CInt->isMinusOne()) + ValueInverted = !ValueInverted; + else + break; + } + } + + auto *PhiDef = dyn_cast(V); + if (!PhiDef) + return; + + bool IsPhiValueNonZero = ((!ValueInverted && !ExpectedValue->isZero()) || + (ValueInverted && ExpectedValue->isZero())); + + // Get the first dominating conditional branch of the operand + // i's incoming block. + auto GetDomConditional = [&](unsigned i) -> BranchInst * { + BasicBlock *BB = PhiDef->getIncomingBlock(i); + BranchInst *BR = dyn_cast(BB->getTerminator()); + if (BR && BR->isConditional()) + return BR; + BB = BB->getSinglePredecessor(); + if (!BB) + return nullptr; + BR = dyn_cast(BB->getTerminator()); + if (!BR || BR->isUnconditional()) + return nullptr; + return BR; + }; + + // Now walk through all Phi operands: + for (unsigned i = 0, e = PhiDef->getNumIncomingValues(); i != e; ++i) { + Value *PhiOpnd = PhiDef->getIncomingValue(i); + ConstantInt *CI = dyn_cast(PhiOpnd); + if (!CI) + continue; + bool IsPhiOpndNonZero = !CI->isZero(); + bool IsUnlikely = (IsPhiValueNonZero != IsPhiOpndNonZero); + // Not an interesting case: + // TODO: properly handlle non-i1 case. For instance, when the expected + // phi value is 10, and the phi operand value is 5, isUnlikely should + // be set to true. + if (!IsUnlikely) + continue; + + BranchInst *BR = GetDomConditional(i); + if (!BR) + continue; + + MDBuilder MDB(PhiDef->getContext()); + + // Figuring out which outgoing edge from BR that leads + // to the PhiDef's parent block. If the CFG is triangular + // shaped and the the operand's incoming block is BR's parent + // block itself, Phi's parent BB is the direct successor + // of BR's parent; Otherwise check the incoming block of the + // operand to see it if is one of BR's successors: + // + // Returns true of Phi's operand 'opnd' is coming from 'succ' th + // successor of BR: + auto IsOpndComingFromSuccessor = [&](int opnd, int succ) { + auto *OpndIncomingBB = PhiDef->getIncomingBlock(opnd); + auto *Succ = BR->getSuccessor(succ); + if (OpndIncomingBB == Succ) + return true; + if (OpndIncomingBB == BR->getParent() && + Succ == PhiDef->getParent()) + return true; + return false; + }; + + if (IsOpndComingFromSuccessor(i, 1)) + BR->setMetadata( + LLVMContext::MD_prof, + MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight)); + else if (IsOpndComingFromSuccessor(i, 0)) + BR->setMetadata( + LLVMContext::MD_prof, + MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight)); + } +} + // Handle both BranchInst and SelectInst. template static bool handleBrSelExpect(BrSelInst &BSI) { @@ -173,6 +305,10 @@ Function *Fn = CI->getCalledFunction(); if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) { + // Before erasing the phi, walk backward to find + // phi that define builin_expect's first arg, and + // infer branch probability: + handlePhiDef(CI); Value *Exp = CI->getArgOperand(0); CI->replaceAllUsesWith(Exp); CI->eraseFromParent(); Index: test/Transforms/LowerExpectIntrinsic/phi_merge.ll =================================================================== --- test/Transforms/LowerExpectIntrinsic/phi_merge.ll +++ test/Transforms/LowerExpectIntrinsic/phi_merge.ll @@ -0,0 +1,110 @@ +; RUN: opt -lower-expect -S -o - < %s | FileCheck %s +; RUN: opt -S -passes='function(lower-expect)' < %s | FileCheck %s + +; The C case +; if (__builtin_expect((x > goo() && y > hoo() && z > too()), 1)) +; For the above case, all 3 branches should be annotated. +; +; if (__builtin_expect((x > goo() && y > hoo() && z > too()), 0)) +; For the above case, we don't have enough information, so +; only the last branch is annotated. + +define void @foo(i32 %arg, i32 %arg1, i32 %arg2, i32 %arg3) #0 { +bb: + %tmp8 = call i32 (...) @goo() #1 + %tmp9 = icmp sgt i32 %tmp8, %arg +; CHECK: !prof [[WEIGHT:![0-9]+]] + br i1 %tmp9, label %bb10, label %bb18 + +bb10: ; preds = %bb + %tmp12 = call i32 (...) @hoo() + %tmp13 = icmp sgt i32 %arg1, %tmp12 +; CHECK: br i1 %tmp13, {{.*}}!prof [[WEIGHT]] + br i1 %tmp13, label %bb14, label %bb18 + +bb14: ; preds = %bb10 + %tmp16 = call i32 (...) @too() + %tmp17 = icmp sgt i32 %arg2, %tmp16 + br label %bb18 + +bb18: ; preds = %bb14, %bb10, %bb + %tmp19 = phi i1 [ false, %bb10 ], [ false, %bb ], [ %tmp17, %bb14 ] + %tmp20 = xor i1 %tmp19, true + %tmp21 = xor i1 %tmp20, true + %tmp22 = zext i1 %tmp21 to i32 + %tmp23 = sext i32 %tmp22 to i64 + %tmp24 = call i64 @llvm.expect.i64(i64 %tmp23, i64 1) + %tmp25 = icmp ne i64 %tmp24, 0 +; CHECK: br i1 %tmp25,{{.*}}!prof [[WEIGHT]] + br i1 %tmp25, label %bb26, label %bb28 + +bb26: ; preds = %bb18 + %tmp27 = call i32 (...) @goo() + br label %bb30 + +bb28: ; preds = %bb18 + %tmp29 = call i32 (...) @hoo() + br label %bb30 + +bb30: ; preds = %bb28, %bb26 + ret void +} + +define void @foo2(i32 %arg, i32 %arg1, i32 %arg2, i32 %arg3) #0 { +bb: + %tmp8 = call i32 (...) @goo() #1 + %tmp9 = icmp sgt i32 %tmp8, %arg +; CHECK-NOT: !prof + br i1 %tmp9, label %bb10, label %bb18 + +bb10: ; preds = %bb + %tmp12 = call i32 (...) @hoo() + %tmp13 = icmp sgt i32 %arg1, %tmp12 +; CHECK-NOT: !prof + br i1 %tmp13, label %bb14, label %bb18 + +bb14: ; preds = %bb10 + %tmp16 = call i32 (...) @too() + %tmp17 = icmp sgt i32 %arg2, %tmp16 + br label %bb18 + +bb18: ; preds = %bb14, %bb10, %bb + %tmp19 = phi i1 [ false, %bb10 ], [ false, %bb ], [ %tmp17, %bb14 ] + %tmp20 = xor i1 %tmp19, true + %tmp21 = xor i1 %tmp20, true + %tmp22 = zext i1 %tmp21 to i32 + %tmp23 = sext i32 %tmp22 to i64 + %tmp24 = call i64 @llvm.expect.i64(i64 %tmp23, i64 0) + %tmp25 = icmp ne i64 %tmp24, 0 +; CHECK: br i1 %tmp25,{{.*}}!prof [[WEIGHT2:![0-9]+]] + br i1 %tmp25, label %bb26, label %bb28 + +bb26: ; preds = %bb18 + %tmp27 = call i32 (...) @goo() + br label %bb30 + +bb28: ; preds = %bb18 + %tmp29 = call i32 (...) @hoo() + br label %bb30 + +bb30: ; preds = %bb28, %bb26 + ret void +} + +declare i32 @goo(...) + +declare i32 @hoo(...) + +declare i32 @too(...) + +; Function Attrs: nounwind readnone +declare i64 @llvm.expect.i64(i64, i64) #1 + +attributes #0 = { noinline nounwind uwtable } +attributes #1 = { nounwind readnone } + +!llvm.ident = !{!0} + +!0 = !{!"clang version 5.0.0 (trunk 302965)"} +; CHECK: [[WEIGHT]] = !{!"branch_weights", i32 2000, i32 1} +; CHECK: [[WEIGHT2]] = !{!"branch_weights", i32 1, i32 2000} Index: test/Transforms/LowerExpectIntrinsic/phi_or.ll =================================================================== --- test/Transforms/LowerExpectIntrinsic/phi_or.ll +++ test/Transforms/LowerExpectIntrinsic/phi_or.ll @@ -0,0 +1,103 @@ +; RUN: opt -lower-expect -S -o - < %s | FileCheck %s +; RUN: opt -S -passes='function(lower-expect)' < %s | FileCheck %s +; +; if (__builtin_expect((x > goo() || y > hoo()), 1)) { +; .. +; } +; For the above case, only the second branch should be +; annotated. +; if (__builtin_expect((x > goo() || y > hoo()), 0)) { +; .. +; } +; For the above case, two branches should be annotated. +; Function Attrs: noinline nounwind uwtable +define void @foo(i32 %arg, i32 %arg1, i32 %arg2, i32 %arg3) #0 { +bb: + %tmp8 = call i32 (...) @goo() + %tmp9 = icmp slt i32 %arg, %tmp8 +; CHECK-NOT: br i1 %tmp9{{.*}}!prof + br i1 %tmp9, label %bb14, label %bb10 + +bb10: ; preds = %bb + %tmp12 = call i32 (...) @hoo() + %tmp13 = icmp sgt i32 %arg1, %tmp12 + br label %bb14 + +bb14: ; preds = %bb10, %bb + %tmp15 = phi i1 [ true, %bb ], [ %tmp13, %bb10 ] + %tmp16 = zext i1 %tmp15 to i32 + %tmp17 = sext i32 %tmp16 to i64 + %expect = call i64 @llvm.expect.i64(i64 %tmp17, i64 1) + %tmp18 = icmp ne i64 %expect, 0 +; CHECK: br i1 %tmp18{{.*}}!prof [[WEIGHT:![0-9]+]] + br i1 %tmp18, label %bb19, label %bb21 + +bb19: ; preds = %bb14 + %tmp20 = call i32 (...) @goo() + br label %bb23 + +bb21: ; preds = %bb14 + %tmp22 = call i32 (...) @hoo() + br label %bb23 + +bb23: ; preds = %bb21, %bb19 + ret void +} + +define void @foo2(i32 %arg, i32 %arg1, i32 %arg2, i32 %arg3) #0 { +bb: + %tmp = alloca i32, align 4 + %tmp4 = alloca i32, align 4 + %tmp5 = alloca i32, align 4 + %tmp6 = alloca i32, align 4 + store i32 %arg, i32* %tmp, align 4 + store i32 %arg1, i32* %tmp4, align 4 + store i32 %arg2, i32* %tmp5, align 4 + store i32 %arg3, i32* %tmp6, align 4 + %tmp7 = load i32, i32* %tmp, align 4 + %tmp8 = call i32 (...) @goo() + %tmp9 = icmp slt i32 %tmp7, %tmp8 +; CHECK: br i1 %tmp9{{.*}}!prof [[WEIGHT2:![0-9]+]] + br i1 %tmp9, label %bb14, label %bb10 + +bb10: ; preds = %bb + %tmp11 = load i32, i32* %tmp5, align 4 + %tmp12 = call i32 (...) @hoo() + %tmp13 = icmp sgt i32 %tmp11, %tmp12 + br label %bb14 + +bb14: ; preds = %bb10, %bb + %tmp15 = phi i1 [ true, %bb ], [ %tmp13, %bb10 ] + %tmp16 = zext i1 %tmp15 to i32 + %tmp17 = sext i32 %tmp16 to i64 + %expect = call i64 @llvm.expect.i64(i64 %tmp17, i64 0) + %tmp18 = icmp ne i64 %expect, 0 +; CHECK: br i1 %tmp18{{.*}}!prof [[WEIGHT2]] + br i1 %tmp18, label %bb19, label %bb21 + +bb19: ; preds = %bb14 + %tmp20 = call i32 (...) @goo() + br label %bb23 + +bb21: ; preds = %bb14 + %tmp22 = call i32 (...) @hoo() + br label %bb23 + +bb23: ; preds = %bb21, %bb19 + ret void +} + +declare i32 @goo(...) #1 +declare i32 @hoo(...) #1 +declare i64 @llvm.expect.i64(i64, i64) #2 + +attributes #0 = { noinline nounwind uwtable } +attributes #1 = { noinline nounwind uwtable } +attributes #2 = { nounwind readnone } + +!llvm.ident = !{!0} + + +!0 = !{!"clang version 5.0.0 (trunk 302965)"} +; CHECK: [[WEIGHT]] = !{!"branch_weights", i32 2000, i32 1} +; CHECK: [[WEIGHT2]] = !{!"branch_weights", i32 1, i32 2000} Index: test/Transforms/LowerExpectIntrinsic/phi_tern.ll =================================================================== --- test/Transforms/LowerExpectIntrinsic/phi_tern.ll +++ test/Transforms/LowerExpectIntrinsic/phi_tern.ll @@ -0,0 +1,58 @@ +; RUN: opt -lower-expect -S -o - < %s | FileCheck %s +; RUN: opt -S -passes='function(lower-expect)' < %s | FileCheck %s + +; return __builtin_expect((a > b ? 1, goo(), 0); +; +; Function Attrs: noinline nounwind uwtable +define i32 @foo(i32 %arg, i32 %arg1) #0 { +bb: + %tmp5 = icmp sgt i32 %arg, %arg1 +; CHECK: br i1 %tmp5{{.*}}!prof [[WEIGHT:![0-9]+]] + br i1 %tmp5, label %bb9, label %bb7 + +bb7: ; preds = %bb + %tmp8 = call i32 (...) @goo() + br label %bb9 + +bb9: ; preds = %bb7, %bb9 + %tmp10 = phi i32 [ 1, %bb ], [ %tmp8, %bb7 ] + %tmp11 = sext i32 %tmp10 to i64 + %expect = call i64 @llvm.expect.i64(i64 %tmp11, i64 0) + %tmp12 = trunc i64 %expect to i32 + ret i32 %tmp12 +} + +define i32 @foo2(i32 %arg, i32 %arg1) #0 { +bb: + %tmp5 = icmp sgt i32 %arg, %arg1 +; CHECK: br i1 %tmp5{{.*}}!prof [[WEIGHT:![0-9]+]] + br i1 %tmp5, label %bb6, label %bb7 + +bb6: ; preds = %bb + br label %bb9 + +bb7: ; preds = %bb + %tmp8 = call i32 (...) @goo() + br label %bb9 + +bb9: ; preds = %bb7, %bb6 + %tmp10 = phi i32 [ 1, %bb6 ], [ %tmp8, %bb7 ] + %tmp11 = sext i32 %tmp10 to i64 + %expect = call i64 @llvm.expect.i64(i64 %tmp11, i64 0) + %tmp12 = trunc i64 %expect to i32 + ret i32 %tmp12 +} + +declare i32 @goo(...) #1 +declare i64 @llvm.expect.i64(i64, i64) #2 + +attributes #0 = { noinline nounwind uwtable } +attributes #1 = { noinline nounwind uwtable } +attributes #2 = { nounwind readnone } + + +!llvm.ident = !{!0} + +!0 = !{!"clang version 5.0.0 (trunk 302965)"} + +; CHECK: [[WEIGHT]] = !{!"branch_weights", i32 1, i32 2000}