@@ -234,7 +234,6 @@ class SimplifyCFGOpt {
234
234
bool FoldValueComparisonIntoPredecessors (Instruction *TI,
235
235
IRBuilder<> &Builder);
236
236
237
- bool simplifyReturn (ReturnInst *RI, IRBuilder<> &Builder);
238
237
bool simplifyResume (ResumeInst *RI, IRBuilder<> &Builder);
239
238
bool simplifySingleResume (ResumeInst *RI);
240
239
bool simplifyCommonResume (ResumeInst *RI);
@@ -245,7 +244,6 @@ class SimplifyCFGOpt {
245
244
bool simplifyBranch (BranchInst *Branch, IRBuilder<> &Builder);
246
245
bool simplifyUncondBranch (BranchInst *BI, IRBuilder<> &Builder);
247
246
bool simplifyCondBranch (BranchInst *BI, IRBuilder<> &Builder);
248
- bool SimplifyCondBranchToTwoReturns (BranchInst *BI, IRBuilder<> &Builder);
249
247
250
248
bool tryToSimplifyUncondBranchWithICmpInIt (ICmpInst *ICI,
251
249
IRBuilder<> &Builder);
@@ -2903,103 +2901,6 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
2903
2901
return true ;
2904
2902
}
2905
2903
2906
- // / If we found a conditional branch that goes to two returning blocks,
2907
- // / try to merge them together into one return,
2908
- // / introducing a select if the return values disagree.
2909
- bool SimplifyCFGOpt::SimplifyCondBranchToTwoReturns (BranchInst *BI,
2910
- IRBuilder<> &Builder) {
2911
- auto *BB = BI->getParent ();
2912
- assert (BI->isConditional () && " Must be a conditional branch" );
2913
- BasicBlock *TrueSucc = BI->getSuccessor (0 );
2914
- BasicBlock *FalseSucc = BI->getSuccessor (1 );
2915
- if (TrueSucc == FalseSucc)
2916
- return false ;
2917
- ReturnInst *TrueRet = cast<ReturnInst>(TrueSucc->getTerminator ());
2918
- ReturnInst *FalseRet = cast<ReturnInst>(FalseSucc->getTerminator ());
2919
-
2920
- // Check to ensure both blocks are empty (just a return) or optionally empty
2921
- // with PHI nodes. If there are other instructions, merging would cause extra
2922
- // computation on one path or the other.
2923
- if (!TrueSucc->getFirstNonPHIOrDbg ()->isTerminator ())
2924
- return false ;
2925
- if (!FalseSucc->getFirstNonPHIOrDbg ()->isTerminator ())
2926
- return false ;
2927
-
2928
- Builder.SetInsertPoint (BI);
2929
- // Okay, we found a branch that is going to two return nodes. If
2930
- // there is no return value for this function, just change the
2931
- // branch into a return.
2932
- if (FalseRet->getNumOperands () == 0 ) {
2933
- TrueSucc->removePredecessor (BB);
2934
- FalseSucc->removePredecessor (BB);
2935
- Builder.CreateRetVoid ();
2936
- EraseTerminatorAndDCECond (BI);
2937
- if (DTU)
2938
- DTU->applyUpdates ({{DominatorTree::Delete, BB, TrueSucc},
2939
- {DominatorTree::Delete, BB, FalseSucc}});
2940
- return true ;
2941
- }
2942
-
2943
- // Otherwise, figure out what the true and false return values are
2944
- // so we can insert a new select instruction.
2945
- Value *TrueValue = TrueRet->getReturnValue ();
2946
- Value *FalseValue = FalseRet->getReturnValue ();
2947
-
2948
- // Unwrap any PHI nodes in the return blocks.
2949
- if (PHINode *TVPN = dyn_cast_or_null<PHINode>(TrueValue))
2950
- if (TVPN->getParent () == TrueSucc)
2951
- TrueValue = TVPN->getIncomingValueForBlock (BB);
2952
- if (PHINode *FVPN = dyn_cast_or_null<PHINode>(FalseValue))
2953
- if (FVPN->getParent () == FalseSucc)
2954
- FalseValue = FVPN->getIncomingValueForBlock (BB);
2955
-
2956
- // In order for this transformation to be safe, we must be able to
2957
- // unconditionally execute both operands to the return. This is
2958
- // normally the case, but we could have a potentially-trapping
2959
- // constant expression that prevents this transformation from being
2960
- // safe.
2961
- if (ConstantExpr *TCV = dyn_cast_or_null<ConstantExpr>(TrueValue))
2962
- if (TCV->canTrap ())
2963
- return false ;
2964
- if (ConstantExpr *FCV = dyn_cast_or_null<ConstantExpr>(FalseValue))
2965
- if (FCV->canTrap ())
2966
- return false ;
2967
-
2968
- // Okay, we collected all the mapped values and checked them for sanity, and
2969
- // defined to really do this transformation. First, update the CFG.
2970
- TrueSucc->removePredecessor (BB);
2971
- FalseSucc->removePredecessor (BB);
2972
-
2973
- // Insert select instructions where needed.
2974
- Value *BrCond = BI->getCondition ();
2975
- if (TrueValue) {
2976
- // Insert a select if the results differ.
2977
- if (TrueValue == FalseValue || isa<UndefValue>(FalseValue)) {
2978
- } else if (isa<UndefValue>(TrueValue)) {
2979
- TrueValue = FalseValue;
2980
- } else {
2981
- TrueValue =
2982
- Builder.CreateSelect (BrCond, TrueValue, FalseValue, " retval" , BI);
2983
- }
2984
- }
2985
-
2986
- Value *RI =
2987
- !TrueValue ? Builder.CreateRetVoid () : Builder.CreateRet (TrueValue);
2988
-
2989
- (void )RI;
2990
-
2991
- LLVM_DEBUG (dbgs () << " \n CHANGING BRANCH TO TWO RETURNS INTO SELECT:"
2992
- << " \n " << *BI << " \n NewRet = " << *RI << " \n TRUEBLOCK: "
2993
- << *TrueSucc << " \n FALSEBLOCK: " << *FalseSucc);
2994
-
2995
- EraseTerminatorAndDCECond (BI);
2996
- if (DTU)
2997
- DTU->applyUpdates ({{DominatorTree::Delete, BB, TrueSucc},
2998
- {DominatorTree::Delete, BB, FalseSucc}});
2999
-
3000
- return true ;
3001
- }
3002
-
3003
2904
static Value *createLogicalOp (IRBuilderBase &Builder,
3004
2905
Instruction::BinaryOps Opc, Value *LHS,
3005
2906
Value *RHS, const Twine &Name = " " ) {
@@ -4628,35 +4529,6 @@ bool SimplifyCFGOpt::simplifyCleanupReturn(CleanupReturnInst *RI) {
4628
4529
return false ;
4629
4530
}
4630
4531
4631
- bool SimplifyCFGOpt::simplifyReturn (ReturnInst *RI, IRBuilder<> &Builder) {
4632
- BasicBlock *BB = RI->getParent ();
4633
- if (!BB->getFirstNonPHIOrDbg ()->isTerminator ())
4634
- return false ;
4635
-
4636
- // Find predecessors that end with branches.
4637
- SmallVector<BranchInst *, 8 > CondBranchPreds;
4638
- for (BasicBlock *P : predecessors (BB)) {
4639
- Instruction *PTI = P->getTerminator ();
4640
- if (BranchInst *BI = dyn_cast<BranchInst>(PTI))
4641
- if (BI->isConditional ())
4642
- CondBranchPreds.push_back (BI);
4643
- }
4644
-
4645
- // Check out all of the conditional branches going to this return
4646
- // instruction. If any of them just select between returns, change the
4647
- // branch itself into a select/return pair.
4648
- while (!CondBranchPreds.empty ()) {
4649
- BranchInst *BI = CondBranchPreds.pop_back_val ();
4650
-
4651
- // Check to see if the non-BB successor is also a return block.
4652
- if (isa<ReturnInst>(BI->getSuccessor (0 )->getTerminator ()) &&
4653
- isa<ReturnInst>(BI->getSuccessor (1 )->getTerminator ()) &&
4654
- SimplifyCondBranchToTwoReturns (BI, Builder))
4655
- return true ;
4656
- }
4657
- return false ;
4658
- }
4659
-
4660
4532
// WARNING: keep in sync with InstCombinerImpl::visitUnreachableInst()!
4661
4533
bool SimplifyCFGOpt::simplifyUnreachable (UnreachableInst *UI) {
4662
4534
BasicBlock *BB = UI->getParent ();
@@ -6747,9 +6619,6 @@ bool SimplifyCFGOpt::simplifyOnceImpl(BasicBlock *BB) {
6747
6619
case Instruction::Br:
6748
6620
Changed |= simplifyBranch (cast<BranchInst>(Terminator), Builder);
6749
6621
break ;
6750
- case Instruction::Ret:
6751
- Changed |= simplifyReturn (cast<ReturnInst>(Terminator), Builder);
6752
- break ;
6753
6622
case Instruction::Resume:
6754
6623
Changed |= simplifyResume (cast<ResumeInst>(Terminator), Builder);
6755
6624
break ;
0 commit comments