@@ -62,7 +62,7 @@ Compiler::StaticType LLVMTypeAnalyzer::variableType(Variable *var, LLVMInstructi
6262 } else if (isElse (ins)) {
6363 // Skip if branch if coming from else
6464 firstElseBranch = ins;
65- ins = skipBranch (ins);
65+ ins = branchStart (ins);
6666 continue ;
6767 } else if (isVariableWrite (ins, var) && !isWriteNoOp (ins)) {
6868 if (level <= 0 ) { // ignore nested branches (they're handled by the branch analyzer)
@@ -135,7 +135,7 @@ LLVMTypeAnalyzer::typeAfterBranchFromEnd(std::variant<Variable *, List *> varOrL
135135 } else
136136 return Compiler::StaticType::Unknown;
137137
138- ins = skipBranch (ins);
138+ ins = branchStart (ins);
139139
140140 if (isElse (ins)) {
141141 // Process if branch (the else branch is already processed)
@@ -151,7 +151,7 @@ LLVMTypeAnalyzer::typeAfterBranchFromEnd(std::variant<Variable *, List *> varOrL
151151 } else
152152 return Compiler::StaticType::Unknown;
153153
154- ins = skipBranch (ins);
154+ ins = branchStart (ins);
155155 }
156156 } else if (std::holds_alternative<Variable *>(varOrList), isVariableWrite (ins, std::get<Variable *>(varOrList)) && !isWriteNoOp (ins)) {
157157 // Variable write instruction
@@ -195,29 +195,33 @@ LLVMInstruction *LLVMTypeAnalyzer::branchEnd(LLVMInstruction *start) const
195195 return ins;
196196}
197197
198- LLVMInstruction *LLVMTypeAnalyzer::skipBranch (LLVMInstruction *pos ) const
198+ LLVMInstruction *LLVMTypeAnalyzer::branchStart (LLVMInstruction *end ) const
199199{
200+ assert (end);
201+ assert (isLoopEnd (end) || isIfEnd (end) || isElse (end));
202+
203+ // Find loop/if statement/else branch start
204+ LLVMInstruction *ins = end->previous ;
200205 int level = 0 ;
201- pos = pos->previous ;
202206
203- while (pos && !((isLoopStart (pos ) || isIfStart (pos )) && level == 0 )) {
204- if (isLoopStart (pos ) || isIfStart (pos )) {
207+ while (ins && !((isLoopStart (ins ) || isIfStart (ins )) && level == 0 )) {
208+ if (isLoopStart (ins ) || isIfStart (ins )) {
205209 assert (level > 0 );
206210 level--;
207211 }
208212
209- if (isLoopEnd (pos ) || isIfEnd (pos ) || isElse (pos )) {
210- if (isElse (pos ) && level == 0 )
213+ if (isLoopEnd (ins ) || isIfEnd (ins ) || isElse (ins )) {
214+ if (isElse (ins ) && level == 0 )
211215 break ;
212216
213217 level++;
214218 }
215219
216- pos = pos ->previous ;
220+ ins = ins ->previous ;
217221 };
218222
219- assert (pos );
220- return pos ;
223+ assert (ins );
224+ return ins ;
221225}
222226
223227bool LLVMTypeAnalyzer::isLoopStart (LLVMInstruction *ins) const
0 commit comments