Skip to content

Commit 547c506

Browse files
committed
LLVMTypeAnalyzer: Move find branch end logic to branchEnd()
1 parent a76435b commit 547c506

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/engine/internal/llvm/llvmtypeanalyzer.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,31 +107,14 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranch(Variable *var, LL
107107
if (!var || !start)
108108
return previousType;
109109

110-
assert(isLoopStart(start) || isIfStart(start) || isElse(start));
111-
112-
// Find loop/if statement end or else branch
113-
LLVMInstruction *ins = start->next;
114-
int level = 0;
110+
LLVMInstruction *end = branchEnd(start);
115111

116-
while (ins && !((isLoopEnd(ins) || isIfEnd(ins) || isElse(ins)) && level == 0)) {
117-
if (isLoopStart(ins) || isIfStart(ins))
118-
level++;
119-
else if (isLoopEnd(ins) || isIfEnd(ins)) {
120-
assert(level > 0);
121-
level--;
122-
}
123-
124-
ins = ins->next;
125-
}
126-
127-
if (!ins) {
128-
assert(false);
112+
if (!end)
129113
return Compiler::StaticType::Unknown;
130-
}
131114

132115
// Process the branch from end
133116
bool write = false; // only used internally (the compiler doesn't need this)
134-
return variableTypeAfterBranchFromEnd(var, ins, previousType, write, visitedInstructions);
117+
return variableTypeAfterBranchFromEnd(var, end, previousType, write, visitedInstructions);
135118
}
136119

137120
Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(Variable *var, LLVMInstruction *end, Compiler::StaticType previousType, bool &write, InstructionSet &visitedInstructions) const
@@ -187,6 +170,30 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(Variable *
187170
return previousType;
188171
}
189172

173+
LLVMInstruction *LLVMTypeAnalyzer::branchEnd(LLVMInstruction *start) const
174+
{
175+
assert(start);
176+
assert(isLoopStart(start) || isIfStart(start) || isElse(start));
177+
178+
// Find loop/if statement end or else branch
179+
LLVMInstruction *ins = start->next;
180+
int level = 0;
181+
182+
while (ins && !((isLoopEnd(ins) || isIfEnd(ins) || isElse(ins)) && level == 0)) {
183+
if (isLoopStart(ins) || isIfStart(ins))
184+
level++;
185+
else if (isLoopEnd(ins) || isIfEnd(ins)) {
186+
assert(level > 0);
187+
level--;
188+
}
189+
190+
ins = ins->next;
191+
}
192+
193+
assert(ins);
194+
return ins;
195+
}
196+
190197
LLVMInstruction *LLVMTypeAnalyzer::skipBranch(LLVMInstruction *pos) const
191198
{
192199
int level = 0;

src/engine/internal/llvm/llvmtypeanalyzer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class LLVMTypeAnalyzer
2121
Compiler::StaticType variableTypeAfterBranch(Variable *var, LLVMInstruction *start, Compiler::StaticType previousType, InstructionSet &visitedInstructions) const;
2222
Compiler::StaticType variableTypeAfterBranchFromEnd(Variable *var, LLVMInstruction *end, Compiler::StaticType previousType, bool &write, InstructionSet &visitedInstructions) const;
2323

24+
LLVMInstruction *branchEnd(LLVMInstruction *start) const;
2425
LLVMInstruction *skipBranch(LLVMInstruction *pos) const;
2526

2627
bool isLoopStart(LLVMInstruction *ins) const;

0 commit comments

Comments
 (0)