Skip to content

Commit 15e0f4e

Browse files
committed
LLVMTypeAnalyzer: Move find branch end logic to branchEnd()
1 parent d432bfc commit 15e0f4e

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

src/engine/internal/llvm/llvmtypeanalyzer.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,10 @@ 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)
@@ -188,6 +171,30 @@ LLVMTypeAnalyzer::typeAfterBranchFromEnd(std::variant<Variable *, List *> varOrL
188171
return previousType;
189172
}
190173

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

src/engine/internal/llvm/llvmtypeanalyzer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class LLVMTypeAnalyzer
2424
Compiler::StaticType
2525
typeAfterBranchFromEnd(std::variant<Variable *, List *> varOrList, LLVMInstruction *end, Compiler::StaticType previousType, bool &write, InstructionSet &visitedInstructions) const;
2626

27+
LLVMInstruction *branchEnd(LLVMInstruction *start) const;
2728
LLVMInstruction *skipBranch(LLVMInstruction *pos) const;
2829

2930
bool isLoopStart(LLVMInstruction *ins) const;

0 commit comments

Comments
 (0)