[llvm] r280600 - [Profile] preserve branch metadata lowering select in CGP

Xinliang David Li via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 3 14:26:37 PDT 2016


Author: davidxl
Date: Sat Sep  3 16:26:36 2016
New Revision: 280600

URL: http://llvm.org/viewvc/llvm-project?rev=280600&view=rev
Log:
[Profile] preserve branch metadata lowering select in CGP

CGP currently drops select's MD_prof profile data when
generating conditional branch which can lead to bad
code layout. The patch fixes the issue.

Differential Revision: http://reviews.llvm.org/D24169

Added:
    llvm/trunk/test/CodeGen/X86/select_meta.ll
Modified:
    llvm/trunk/include/llvm/IR/IRBuilder.h
    llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp
    llvm/trunk/test/CodeGen/X86/cmov-into-branch.ll

Modified: llvm/trunk/include/llvm/IR/IRBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/IRBuilder.h?rev=280600&r1=280599&r2=280600&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/IRBuilder.h (original)
+++ llvm/trunk/include/llvm/IR/IRBuilder.h Sat Sep  3 16:26:36 2016
@@ -703,6 +703,19 @@ public:
                                     BranchWeights, Unpredictable));
   }
 
+  /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
+  /// instruction. Copy branch meta data if available.
+  BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
+                           Instruction *MDSrc) {
+    BranchInst *Br = BranchInst::Create(True, False, Cond);
+    if (MDSrc) {
+      unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
+                        LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
+      Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
+    }
+    return Insert(Br);
+  }
+
   /// \brief Create a switch instruction with the specified value, default dest,
   /// and with a hint for the number of cases that will be added (for efficient
   /// allocation).

Modified: llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp?rev=280600&r1=280599&r2=280600&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp (original)
+++ llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp Sat Sep  3 16:26:36 2016
@@ -4676,15 +4676,20 @@ bool CodeGenPrepare::optimizeSelectInst(
   // of the condition, it means that side of the branch goes to the end block
   // directly and the path originates from the start block from the point of
   // view of the new PHI.
+  BasicBlock *TT, *FT;
   if (TrueBlock == nullptr) {
-    BranchInst::Create(EndBlock, FalseBlock, SI->getCondition(), SI);
+    TT = EndBlock;
+    FT = FalseBlock;
     TrueBlock = StartBlock;
   } else if (FalseBlock == nullptr) {
-    BranchInst::Create(TrueBlock, EndBlock, SI->getCondition(), SI);
+    TT = TrueBlock;
+    FT = EndBlock;
     FalseBlock = StartBlock;
   } else {
-    BranchInst::Create(TrueBlock, FalseBlock, SI->getCondition(), SI);
+    TT = TrueBlock;
+    FT = FalseBlock;
   }
+  IRBuilder<>(SI).CreateCondBr(SI->getCondition(), TT, FT, SI);
 
   // The select itself is replaced with a PHI Node.
   PHINode *PN = PHINode::Create(SI->getType(), 2, "", &EndBlock->front());

Modified: llvm/trunk/test/CodeGen/X86/cmov-into-branch.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/cmov-into-branch.ll?rev=280600&r1=280599&r2=280600&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/cmov-into-branch.ll (original)
+++ llvm/trunk/test/CodeGen/X86/cmov-into-branch.ll Sat Sep  3 16:26:36 2016
@@ -103,11 +103,11 @@ define i32 @weighted_select3(i32 %a, i32
 ; CHECK-LABEL: weighted_select3:
 ; CHECK:       # BB#0:
 ; CHECK-NEXT:    testl %edi, %edi
-; CHECK-NEXT:    jne [[LABEL_BB6:.*]]
-; CHECK:         movl %esi, %edi
-; CHECK-NEXT:  [[LABEL_BB6]]
-; CHECK-NEXT:    movl %edi, %eax
-; CHECK-NEXT:    retq
+; CHECK-NEXT:    je [[LABEL_BB6:.*]]
+; CHECK:         movl %edi, %eax
+; CHECK:         [[LABEL_BB6]]
+; CHECK-NEXT:    movl %esi, %edi
+; CHECK-NEXT:    jmp
 ;
   %cmp = icmp ne i32 %a, 0
   %sel = select i1 %cmp, i32 %a, i32 %b, !prof !2

Added: llvm/trunk/test/CodeGen/X86/select_meta.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/select_meta.ll?rev=280600&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/select_meta.ll (added)
+++ llvm/trunk/test/CodeGen/X86/select_meta.ll Sat Sep  3 16:26:36 2016
@@ -0,0 +1,16 @@
+; RUN: llc -mtriple=x86_64-unknown-unknown -print-after-all < %s 2>&1 | FileCheck %s
+
+; Function Attrs: norecurse nounwind readnone uwtable
+define i32 @foo(i32, i32, i32) {
+  %4 = and i32 %0, 3
+  %5 = icmp eq i32 %4, 1
+  %6 = select i1 %5, i32 %1, i32 %2, !prof  !1
+; CHECK: br {{.*}}label{{.*}}, label{{.*}}, !prof ![[WT:.*]]
+  ret i32 %6
+}
+
+!llvm.ident = !{!0}
+
+!0 = !{!"clang version 4.0.0 (trunk 279683)"}
+!1 = !{!"branch_weights", i32 1000, i32 1 }
+; CHECK ![[WT]] = !{!"branch_weights", i32 1000, i32 1 }




More information about the llvm-commits mailing list