| 106 | } |
| 107 | |
| 108 | bool LoopFusion::AreCompatible() { |
| 109 | // Check that the loops are in the same function. |
| 110 | if (loop_0_->GetHeaderBlock()->GetParent() != |
| 111 | loop_1_->GetHeaderBlock()->GetParent()) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | // Check that both loops have pre-header blocks. |
| 116 | if (!loop_0_->GetPreHeaderBlock() || !loop_1_->GetPreHeaderBlock()) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | // Check there are no breaks. |
| 121 | if (context_->cfg()->preds(loop_0_->GetMergeBlock()->id()).size() != 1 || |
| 122 | context_->cfg()->preds(loop_1_->GetMergeBlock()->id()).size() != 1) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // Check there are no continues. |
| 127 | if (context_->cfg()->preds(loop_0_->GetContinueBlock()->id()).size() != 1 || |
| 128 | context_->cfg()->preds(loop_1_->GetContinueBlock()->id()).size() != 1) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // |GetInductionVariables| returns all OpPhi in the header. Check that both |
| 133 | // loops have exactly one that is used in the continue and condition blocks. |
| 134 | std::vector<Instruction*> inductions_0{}, inductions_1{}; |
| 135 | loop_0_->GetInductionVariables(inductions_0); |
| 136 | RemoveIfNotUsedContinueOrConditionBlock(&inductions_0, loop_0_); |
| 137 | |
| 138 | if (inductions_0.size() != 1) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | induction_0_ = inductions_0.front(); |
| 143 | |
| 144 | loop_1_->GetInductionVariables(inductions_1); |
| 145 | RemoveIfNotUsedContinueOrConditionBlock(&inductions_1, loop_1_); |
| 146 | |
| 147 | if (inductions_1.size() != 1) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | induction_1_ = inductions_1.front(); |
| 152 | |
| 153 | if (!CheckInit()) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | if (!CheckCondition()) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | if (!CheckStep()) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | // Check adjacency, |loop_0_| should come just before |loop_1_|. |
no test coverage detected