@@ -138,6 +138,10 @@ static cl::opt<bool>
138
138
EnableCodeSinking (" instcombine-code-sinking" , cl::desc(" Enable code sinking" ),
139
139
cl::init(true ));
140
140
141
+ static cl::opt<unsigned > MaxSinkNumUsers (
142
+ " instcombine-max-sink-users" , cl::init(32 ),
143
+ cl::desc(" Maximum number of undroppable users for instruction sinking" ));
144
+
141
145
static cl::opt<unsigned > LimitMaxIterations (
142
146
" instcombine-max-iterations" ,
143
147
cl::desc (" Limit the maximum number of instruction combining iterations" ),
@@ -3859,7 +3863,6 @@ static bool SoleWriteToDeadLocal(Instruction *I, TargetLibraryInfo &TLI) {
3859
3863
// / block.
3860
3864
static bool TryToSinkInstruction (Instruction *I, BasicBlock *DestBlock,
3861
3865
TargetLibraryInfo &TLI) {
3862
- assert (I->getUniqueUndroppableUser () && " Invariants didn't hold!" );
3863
3866
BasicBlock *SrcBlock = I->getParent ();
3864
3867
3865
3868
// Cannot move control-flow-involving, volatile loads, vaarg, etc.
@@ -4026,48 +4029,68 @@ bool InstCombinerImpl::run() {
4026
4029
[this ](Instruction *I) -> Optional<BasicBlock *> {
4027
4030
if (!EnableCodeSinking)
4028
4031
return None;
4029
- auto *UserInst = cast_or_null<Instruction>(I->getUniqueUndroppableUser ());
4030
- if (!UserInst)
4031
- return None;
4032
4032
4033
4033
BasicBlock *BB = I->getParent ();
4034
4034
BasicBlock *UserParent = nullptr ;
4035
+ unsigned NumUsers = 0 ;
4035
4036
4036
- // Special handling for Phi nodes - get the block the use occurs in.
4037
- if (PHINode *PN = dyn_cast<PHINode>(UserInst)) {
4038
- for (unsigned i = 0 ; i < PN->getNumIncomingValues (); i++) {
4039
- if (PN->getIncomingValue (i) == I) {
4040
- // Bail out if we have uses in different blocks. We don't do any
4041
- // sophisticated analysis (i.e finding NearestCommonDominator of these
4042
- // use blocks).
4043
- if (UserParent && UserParent != PN->getIncomingBlock (i))
4044
- return None;
4045
- UserParent = PN->getIncomingBlock (i);
4037
+ for (auto *U : I->users ()) {
4038
+ if (U->isDroppable ())
4039
+ continue ;
4040
+ if (NumUsers > MaxSinkNumUsers)
4041
+ return None;
4042
+
4043
+ Instruction *UserInst = cast<Instruction>(U);
4044
+ // Special handling for Phi nodes - get the block the use occurs in.
4045
+ if (PHINode *PN = dyn_cast<PHINode>(UserInst)) {
4046
+ for (unsigned i = 0 ; i < PN->getNumIncomingValues (); i++) {
4047
+ if (PN->getIncomingValue (i) == I) {
4048
+ // Bail out if we have uses in different blocks. We don't do any
4049
+ // sophisticated analysis (i.e finding NearestCommonDominator of
4050
+ // these use blocks).
4051
+ if (UserParent && UserParent != PN->getIncomingBlock (i))
4052
+ return None;
4053
+ UserParent = PN->getIncomingBlock (i);
4054
+ }
4046
4055
}
4056
+ assert (UserParent && " expected to find user block!" );
4057
+ } else {
4058
+ if (UserParent && UserParent != UserInst->getParent ())
4059
+ return None;
4060
+ UserParent = UserInst->getParent ();
4047
4061
}
4048
- assert (UserParent && " expected to find user block!" );
4049
- } else
4050
- UserParent = UserInst->getParent ();
4051
4062
4052
- // Try sinking to another block. If that block is unreachable, then do
4053
- // not bother. SimplifyCFG should handle it.
4054
- if (UserParent == BB || !DT.isReachableFromEntry (UserParent))
4055
- return None;
4063
+ // Make sure these checks are done only once, naturally we do the checks
4064
+ // the first time we get the userparent, this will save compile time.
4065
+ if (NumUsers == 0 ) {
4066
+ // Try sinking to another block. If that block is unreachable, then do
4067
+ // not bother. SimplifyCFG should handle it.
4068
+ if (UserParent == BB || !DT.isReachableFromEntry (UserParent))
4069
+ return None;
4070
+
4071
+ auto *Term = UserParent->getTerminator ();
4072
+ // See if the user is one of our successors that has only one
4073
+ // predecessor, so that we don't have to split the critical edge.
4074
+ // Another option where we can sink is a block that ends with a
4075
+ // terminator that does not pass control to other block (such as
4076
+ // return or unreachable or resume). In this case:
4077
+ // - I dominates the User (by SSA form);
4078
+ // - the User will be executed at most once.
4079
+ // So sinking I down to User is always profitable or neutral.
4080
+ if (UserParent->getUniquePredecessor () != BB && !succ_empty (Term))
4081
+ return None;
4082
+
4083
+ assert (DT.dominates (BB, UserParent) && " Dominance relation broken?" );
4084
+ }
4056
4085
4057
- auto *Term = UserParent->getTerminator ();
4058
- // See if the user is one of our successors that has only one
4059
- // predecessor, so that we don't have to split the critical edge.
4060
- // Another option where we can sink is a block that ends with a
4061
- // terminator that does not pass control to other block (such as
4062
- // return or unreachable or resume). In this case:
4063
- // - I dominates the User (by SSA form);
4064
- // - the User will be executed at most once.
4065
- // So sinking I down to User is always profitable or neutral.
4066
- if (UserParent->getUniquePredecessor () == BB || succ_empty (Term)) {
4067
- assert (DT.dominates (BB, UserParent) && " Dominance relation broken?" );
4068
- return UserParent;
4086
+ NumUsers++;
4069
4087
}
4070
- return None;
4088
+
4089
+ // No user or only has droppable users.
4090
+ if (!UserParent)
4091
+ return None;
4092
+
4093
+ return UserParent;
4071
4094
};
4072
4095
4073
4096
auto OptBB = getOptionalSinkBlockForInst (I);
0 commit comments