Index: lib/Transforms/Scalar/LoopUnswitch.cpp =================================================================== --- lib/Transforms/Scalar/LoopUnswitch.cpp +++ lib/Transforms/Scalar/LoopUnswitch.cpp @@ -195,10 +195,12 @@ /// Update the appropriate Phi nodes as we do so. void SplitExitEdges(Loop *L, const SmallVectorImpl &ExitBlocks); - bool UnswitchIfProfitable(Value *LoopCond, Constant *Val); + bool UnswitchIfProfitable(Value *LoopCond, Constant *Val, + TerminatorInst *TI = nullptr); void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val, - BasicBlock *ExitBlock); - void UnswitchNontrivialCondition(Value *LIC, Constant *OnVal, Loop *L); + BasicBlock *ExitBlock, TerminatorInst *TI); + void UnswitchNontrivialCondition(Value *LIC, Constant *OnVal, Loop *L, + TerminatorInst *TI); void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, Constant *Val, bool isEqual); @@ -206,7 +208,8 @@ void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val, BasicBlock *TrueDest, BasicBlock *FalseDest, - Instruction *InsertPt); + Instruction *InsertPt, + TerminatorInst *TI); void SimplifyCode(std::vector &Worklist, Loop *L); bool IsTrivialUnswitchCondition(Value *Cond, Constant **Val = nullptr, @@ -453,8 +456,8 @@ // unswitch on it if we desire. Value *LoopCond = FindLIVLoopCondition(BI->getCondition(), currentLoop, Changed); - if (LoopCond && UnswitchIfProfitable(LoopCond, - ConstantInt::getTrue(Context))) { + if (LoopCond && + UnswitchIfProfitable(LoopCond, ConstantInt::getTrue(Context), TI)) { ++NumBranches; return true; } @@ -643,7 +646,8 @@ /// UnswitchIfProfitable - We have found that we can unswitch currentLoop when /// LoopCond == Val to simplify the loop. If we decide that this is profitable, /// unswitch the loop, reprocess the pieces, then return true. -bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val) { +bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val, + TerminatorInst *TI) { Function *F = loopHeader->getParent(); Constant *CondVal = nullptr; BasicBlock *ExitBlock = nullptr; @@ -651,7 +655,7 @@ if (IsTrivialUnswitchCondition(LoopCond, &CondVal, &ExitBlock)) { // If the condition is trivial, always unswitch. There is no code growth // for this case. - UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, ExitBlock); + UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, ExitBlock, TI); return true; } @@ -661,7 +665,7 @@ if (OptimizeForSize || F->hasFnAttribute(Attribute::OptimizeForSize)) return false; - UnswitchNontrivialCondition(LoopCond, Val, currentLoop); + UnswitchNontrivialCondition(LoopCond, Val, currentLoop, TI); return true; } @@ -691,7 +695,8 @@ void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val, BasicBlock *TrueDest, BasicBlock *FalseDest, - Instruction *InsertPt) { + Instruction *InsertPt, + TerminatorInst *TI) { // Insert a conditional branch on LIC to the two preheaders. The original // code is the true version and the new code is the false version. Value *BranchVal = LIC; @@ -705,6 +710,13 @@ // Insert the new branch. BranchInst *BI = BranchInst::Create(TrueDest, FalseDest, BranchVal, InsertPt); + if (TI && TI->hasMetadata()) { + SmallVector, 4> MDs; + TI->getAllMetadata(MDs); + for (auto &MD : MDs) + BI->setMetadata(MD.first, MD.second); + } + // If either edge is critical, split it. This helps preserve LoopSimplify // form for enclosing loops. auto Options = CriticalEdgeSplittingOptions(DT, LI).setPreserveLCSSA(); @@ -717,13 +729,14 @@ /// where the path through the loop that doesn't execute its body has no /// side-effects), unswitch it. This doesn't involve any code duplication, just /// moving the conditional branch outside of the loop and updating loop info. -void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, - Constant *Val, - BasicBlock *ExitBlock) { +void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val, + BasicBlock *ExitBlock, + TerminatorInst *TI) { DEBUG(dbgs() << "loop-unswitch: Trivial-Unswitch loop %" - << loopHeader->getName() << " [" << L->getBlocks().size() - << " blocks] in Function " << L->getHeader()->getParent()->getName() - << " on cond: " << *Val << " == " << *Cond << "\n"); + << loopHeader->getName() << " [" << L->getBlocks().size() + << " blocks] in Function " + << L->getHeader()->getParent()->getName() << " on cond: " << *Val + << " == " << *Cond << "\n"); // First step, split the preheader, so that we know that there is a safe place // to insert the conditional branch. We will change loopPreheader to have a @@ -744,7 +757,7 @@ // Okay, now we have a position to branch from and a position to branch to, // insert the new conditional branch. EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH, - loopPreheader->getTerminator()); + loopPreheader->getTerminator(), TI); LPM->deleteSimpleAnalysisValue(loopPreheader->getTerminator(), L); loopPreheader->getTerminator()->eraseFromParent(); @@ -780,7 +793,7 @@ /// to unswitch when LIC equal Val. Split it into loop versions and test the /// condition outside of either loop. Return the loops created as Out1/Out2. void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val, - Loop *L) { + Loop *L, TerminatorInst *TI) { Function *F = loopHeader->getParent(); DEBUG(dbgs() << "loop-unswitch: Unswitching loop %" << loopHeader->getName() << " [" << L->getBlocks().size() @@ -897,7 +910,8 @@ "Preheader splitting did not work correctly!"); // Emit the new branch that selects between the two versions of this loop. - EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR); + EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR, + TI); LPM->deleteSimpleAnalysisValue(OldBR, L); OldBR->eraseFromParent(); Index: test/Transforms/LoopUnswitch/2015-06-17-Metadata.ll =================================================================== --- /dev/null +++ test/Transforms/LoopUnswitch/2015-06-17-Metadata.ll @@ -0,0 +1,57 @@ +;RUN: opt -loop-unswitch -simplifycfg -S < %s | FileCheck %s + +define i32 @foo(i32 %a, i32 %b) { +;CHECK-LABEL: foo +entry: + br label %for.body.lr.ph + +for.body.lr.ph: ; preds = %entry + %cmp.10 = icmp sgt i32 %b, 0 + br i1 %cmp.10, label %for.body, label %for.cond.cleanup + +for.body: ; preds = %for.inc, %for.body.lr.ph + %i.013 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.inc ] + %t2.012 = phi i32 [ 200, %for.body.lr.ph ], [ %t2.1, %for.inc ] + %t1.011 = phi i32 [ 100, %for.body.lr.ph ], [ %t1.1, %for.inc ] + %cmp1 = icmp eq i32 %a, 12345 + br i1 %cmp1, label %if.then, label %if.else, !prof !0 +; CHECK: %cmp1 = icmp eq i32 %a, 12345 +; CHECK-NEXT: br i1 %cmp1, label %if.then.us, label %if.else, !prof !0 +if.then: ; preds = %for.body +; CHECK: if.then.us: +; CHECK: %shr.us = ashr i32 %b, 1 +; CHECK: tail call i32 @bar(i32 %shr.us) +; CHECK: add nuw nsw i32 %i.{{.*}}, 1 +; CHECK: %exitcond.us = icmp eq i32 %inc.us, %b +; CHECK: br i1 %exitcond.us, label %for.cond.cleanup, label %if.then.us + %shr = ashr i32 %b, 1 + %call = tail call i32 @bar(i32 %shr) + %add = add nsw i32 %call, %t1.011 + br label %for.inc + +if.else: ; preds = %for.body + %call2 = tail call i32 @bar(i32 %b) + %mul = mul nsw i32 %call2, %t2.012 + br label %for.inc +; CHECK: if.else: +; CHECK: tail call i32 @bar(i32 %b) +; CHECK: %inc = add nuw nsw i32 %i.{{[0-9]+}}, 1 +; CHECK: %exitcond = icmp eq i32 %inc, %b +; CHECK: br i1 %exitcond, label %for.cond.cleanup, label %if.else +for.inc: ; preds = %if.then, %if.else + %t1.1 = phi i32 [ %add, %if.then ], [ %t1.011, %if.else ] + %t2.1 = phi i32 [ %t2.012, %if.then ], [ %mul, %if.else ] + %inc = add nuw nsw i32 %i.013, 1 + %exitcond = icmp eq i32 %inc, %b + br i1 %exitcond, label %for.cond.cleanup, label %for.body + +for.cond.cleanup: ; preds = %for.inc, %for.body.lr.ph + %t2.0.lcssa = phi i32 [ 200, %for.body.lr.ph ], [ %t2.1, %for.inc ] + %t1.0.lcssa = phi i32 [ 100, %for.body.lr.ph ], [ %t1.1, %for.inc ] + %add3 = add nsw i32 %t2.0.lcssa, %t1.0.lcssa + ret i32 %add3 +} +;CHECK: !0 = !{!"branch_weights", {{.*}}} +declare i32 @bar(i32) + +!0 = !{!"branch_weights", i32 64, i32 4}