llvm.org GIT mirror llvm / a7dea54
Merging r142168: ------------------------------------------------------------------------ r142168 | chandlerc | 2011-10-16 18:11:57 -0700 (Sun, 16 Oct 2011) | 8 lines Add a routine to swap branch instruction operands, and update any profile metadata at the same time. Use it to preserve metadata attached to a branch when re-writing it in InstCombine. Add metadata to the canonicalize_branch InstCombine test, and check that it is tranformed correctly. Reviewed by Nick Lewycky! ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_30@142545 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 9 years ago
4 changed file(s) with 59 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
23662366 *(&Op<-1>() - idx) = (Value*)NewSucc;
23672367 }
23682368
2369 /// \brief Swap the successors of this branch instruction.
2370 ///
2371 /// Swaps the successors of the branch instruction. This also swaps any
2372 /// branch weight metadata associated with the instruction so that it
2373 /// continues to map correctly to each operand.
2374 void swapSuccessors();
2375
23692376 // Methods for support type inquiry through isa, cast, and dyn_cast:
23702377 static inline bool classof(const BranchInst *) { return true; }
23712378 static inline bool classof(const Instruction *I) {
11891189 !isa(X)) {
11901190 // Swap Destinations and condition...
11911191 BI.setCondition(X);
1192 BI.setSuccessor(0, FalseDest);
1193 BI.setSuccessor(1, TrueDest);
1192 BI.swapSuccessors();
11941193 return &BI;
11951194 }
11961195
12051204 Cond->setPredicate(FCmpInst::getInversePredicate(FPred));
12061205
12071206 // Swap Destinations and condition.
1208 BI.setSuccessor(0, FalseDest);
1209 BI.setSuccessor(1, TrueDest);
1207 BI.swapSuccessors();
12101208 Worklist.Add(Cond);
12111209 return &BI;
12121210 }
12221220 ICmpInst *Cond = cast(BI.getCondition());
12231221 Cond->setPredicate(ICmpInst::getInversePredicate(IPred));
12241222 // Swap Destinations and condition.
1225 BI.setSuccessor(0, FalseDest);
1226 BI.setSuccessor(1, TrueDest);
1223 BI.swapSuccessors();
12271224 Worklist.Add(Cond);
12281225 return &BI;
12291226 }
782782 Op<-2>() = BI.Op<-2>();
783783 }
784784 SubclassOptionalData = BI.SubclassOptionalData;
785 }
786
787 void BranchInst::swapSuccessors() {
788 assert(isConditional() &&
789 "Cannot swap successors of an unconditional branch");
790 Op<-1>().swap(Op<-2>());
791
792 // Update profile metadata if present and it matches our structural
793 // expectations.
794 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
795 if (!ProfileData || ProfileData->getNumOperands() != 3)
796 return;
797
798 // The first operand is the name. Fetch them backwards and build a new one.
799 Value *Ops[] = {
800 ProfileData->getOperand(0),
801 ProfileData->getOperand(2),
802 ProfileData->getOperand(1)
803 };
804 setMetadata(LLVMContext::MD_prof,
805 MDNode::get(ProfileData->getContext(), Ops));
785806 }
786807
787808 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
0 ; RUN: opt < %s -instcombine -S | FileCheck %s
1
2 ; Test an already canonical branch to make sure we don't flip those.
3 define i32 @test0(i32 %X, i32 %Y) {
4 %C = icmp eq i32 %X, %Y
5 br i1 %C, label %T, label %F, !prof !0
6
7 ; CHECK: @test0
8 ; CHECK: %C = icmp eq i32 %X, %Y
9 ; CHECK: br i1 %C, label %T, label %F
10
11 T:
12 ret i32 12
13 F:
14 ret i32 123
15 }
116
217 define i32 @test1(i32 %X, i32 %Y) {
318 %C = icmp ne i32 %X, %Y
4 br i1 %C, label %T, label %F
19 br i1 %C, label %T, label %F, !prof !1
520
621 ; CHECK: @test1
722 ; CHECK: %C = icmp eq i32 %X, %Y
1530
1631 define i32 @test2(i32 %X, i32 %Y) {
1732 %C = icmp ule i32 %X, %Y
18 br i1 %C, label %T, label %F
33 br i1 %C, label %T, label %F, !prof !2
1934
2035 ; CHECK: @test2
2136 ; CHECK: %C = icmp ugt i32 %X, %Y
2944
3045 define i32 @test3(i32 %X, i32 %Y) {
3146 %C = icmp uge i32 %X, %Y
32 br i1 %C, label %T, label %F
47 br i1 %C, label %T, label %F, !prof !3
3348
3449 ; CHECK: @test3
3550 ; CHECK: %C = icmp ult i32 %X, %Y
4156 ret i32 123
4257 }
4358
59 !0 = metadata !{metadata !"branch_weights", i32 1, i32 2}
60 !1 = metadata !{metadata !"branch_weights", i32 3, i32 4}
61 !2 = metadata !{metadata !"branch_weights", i32 5, i32 6}
62 !3 = metadata !{metadata !"branch_weights", i32 7, i32 8}
63 ; Base case shouldn't change.
64 ; CHECK: !0 = {{.*}} i32 1, i32 2}
65 ; Ensure that the branch metadata is reversed to match the reversals above.
66 ; CHECK: !1 = {{.*}} i32 4, i32 3}
67 ; CHECK: !2 = {{.*}} i32 6, i32 5}
68 ; CHECK: !3 = {{.*}} i32 8, i32 7}