Skip to content

Commit 9e23fed

Browse files
committedMar 17, 2016
propagate 'unpredictable' metadata on select instructions
This is similar to D18133 where we allowed profile weights on select instructions. This extends that change to also allow the 'unpredictable' attribute of branches to apply to selects. A test to check that 'unpredictable' metadata is preserved when cloning instructions was checked in at: http://reviews.llvm.org/rL263648 Differential Revision: http://reviews.llvm.org/D18220 llvm-svn: 263716
1 parent 071a099 commit 9e23fed

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed
 

‎llvm/include/llvm/IR/IRBuilder.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -1569,15 +1569,18 @@ class IRBuilder : public IRBuilderBase, public Inserter {
15691569
}
15701570

15711571
Value *CreateSelect(Value *C, Value *True, Value *False,
1572-
const Twine &Name = "", MDNode *ProfWeights = nullptr) {
1572+
const Twine &Name = "", Instruction *MDFrom = nullptr) {
15731573
if (Constant *CC = dyn_cast<Constant>(C))
15741574
if (Constant *TC = dyn_cast<Constant>(True))
15751575
if (Constant *FC = dyn_cast<Constant>(False))
15761576
return Insert(Folder.CreateSelect(CC, TC, FC), Name);
15771577

15781578
SelectInst *Sel = SelectInst::Create(C, True, False);
1579-
// TODO: "unpredictable" metadata can apply to a select too.
1580-
Sel->setMetadata(LLVMContext::MD_prof, ProfWeights);
1579+
if (MDFrom) {
1580+
MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof);
1581+
MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable);
1582+
Sel = addBranchMetadata(Sel, Prof, Unpred);
1583+
}
15811584
return Insert(Sel, Name);
15821585
}
15831586

‎llvm/lib/Transforms/Utils/SimplifyCFG.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1935,10 +1935,9 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
19351935
Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
19361936
Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
19371937

1938-
MDNode *MDN = InsertPt->getMetadata(LLVMContext::MD_prof);
1939-
Value *Select = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", MDN);
1940-
PN->replaceAllUsesWith(Select);
1941-
Select->takeName(PN);
1938+
Value *Sel = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", InsertPt);
1939+
PN->replaceAllUsesWith(Sel);
1940+
Sel->takeName(PN);
19421941
PN->eraseFromParent();
19431942
}
19441943

‎llvm/test/Transforms/SimplifyCFG/PhiEliminate2.ll

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
define i32 @FoldTwoEntryPHINode(i1 %C, i32 %V1, i32 %V2, i16 %V3) {
77
entry:
8-
br i1 %C, label %then, label %else, !prof !0
8+
br i1 %C, label %then, label %else, !prof !0, !unpredictable !1
99
then:
1010
%V4 = or i32 %V2, %V1
1111
br label %Cont
@@ -21,12 +21,14 @@ Cont:
2121
; CHECK-NEXT: entry:
2222
; CHECK-NEXT: %V5 = sext i16 %V3 to i32
2323
; CHECK-NEXT: %V4 = or i32 %V2, %V1
24-
; CHECK-NEXT: %V6 = select i1 %C, i32 %V4, i32 %V5, !prof !0
24+
; CHECK-NEXT: %V6 = select i1 %C, i32 %V4, i32 %V5, !prof !0, !unpredictable !1
2525
; CHECK-NEXT: %0 = call i32 @FoldTwoEntryPHINode(i1 false, i32 0, i32 0, i16 0)
2626
; CHECK-NEXT: ret i32 %V1
2727
}
2828

2929
!0 = !{!"branch_weights", i32 3, i32 5}
30+
!1 = !{}
3031

3132
; CHECK: !0 = !{!"branch_weights", i32 3, i32 5}
33+
; CHECK: !1 = !{}
3234

0 commit comments

Comments
 (0)
Please sign in to comment.