Skip to content

Commit 41b04d8

Browse files
committed
LLVMTypeAnalyzer: Drop variableTypeChangesInBranch()
variableTypeAfterBranch() is the preferred method now
1 parent 6cddc41 commit 41b04d8

File tree

5 files changed

+123
-177
lines changed

5 files changed

+123
-177
lines changed

src/engine/internal/llvm/llvmtypeanalyzer.cpp

Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -63,37 +63,6 @@ Compiler::StaticType LLVMTypeAnalyzer::variableType(LLVMVariablePtr *varPtr, LLV
6363
return previousType;
6464
}
6565

66-
bool LLVMTypeAnalyzer::variableTypeChangesInBranch(LLVMVariablePtr *varPtr, LLVMInstruction *start, Compiler::StaticType previousType) const
67-
{
68-
if (!varPtr || !start)
69-
return false;
70-
71-
assert(isLoopStart(start) || isIfStart(start) || isElse(start));
72-
73-
// Find loop/if statement end or else branch
74-
LLVMInstruction *ins = start->next;
75-
int level = 0;
76-
77-
while (ins && !((isLoopEnd(ins) || isIfEnd(ins) || isElse(ins)) && level == 0)) {
78-
if (isLoopStart(ins) || isIfStart(ins))
79-
level++;
80-
else if (isLoopEnd(ins) || isIfEnd(ins)) {
81-
assert(level > 0);
82-
level--;
83-
}
84-
85-
ins = ins->next;
86-
}
87-
88-
if (!ins) {
89-
assert(false);
90-
return true;
91-
}
92-
93-
// Process the branch from end
94-
return variableTypeChangesInBranchFromEnd(varPtr, ins, previousType);
95-
}
96-
9766
Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranch(LLVMVariablePtr *varPtr, LLVMInstruction *start, Compiler::StaticType previousType) const
9867
{
9968
if (!varPtr || !start)
@@ -129,66 +98,45 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(LLVMVariab
12998
{
13099
// Find the last write instruction
131100
LLVMInstruction *ins = end->previous;
101+
bool typeMightReset = false;
132102

133103
while (ins && !isLoopStart(ins) && !isIfStart(ins)) {
134104
if (isLoopEnd(ins) || isIfEnd(ins) || isElse(ins)) {
135105
// Process the nested loop or if statement
136106
Compiler::StaticType ret = variableTypeAfterBranchFromEnd(varPtr, ins, previousType);
137107

138-
if (!typesMatch(ret, previousType))
139-
return ret;
108+
if (typesMatch(ret, previousType))
109+
typeMightReset = true;
110+
else
111+
return Compiler::StaticType::Unknown;
140112

141113
ins = skipBranch(ins);
142114

143115
if (isElse(ins)) {
144116
// Process if branch (the else branch is already processed)
145117
ret = variableTypeAfterBranchFromEnd(varPtr, ins, previousType);
146118

147-
if (!typesMatch(ret, previousType))
148-
return ret;
119+
if (typesMatch(ret, previousType))
120+
typeMightReset = true;
121+
else
122+
return Compiler::StaticType::Unknown;
149123

150124
ins = skipBranch(ins);
151125
}
152126
} else if (ins->type == LLVMInstruction::Type::WriteVariable && ins->workVariable == varPtr->var) {
153127
// Variable write instruction
154-
return writeValueType(ins);
155-
}
156-
157-
ins = ins->previous;
158-
}
159-
160-
return previousType;
161-
}
162-
163-
bool LLVMTypeAnalyzer::variableTypeChangesInBranchFromEnd(LLVMVariablePtr *varPtr, LLVMInstruction *end, Compiler::StaticType previousType) const
164-
{
165-
// Find the last write instruction
166-
LLVMInstruction *ins = end->previous;
167-
168-
while (ins && !isLoopStart(ins) && !isIfStart(ins)) {
169-
if (isLoopEnd(ins) || isIfEnd(ins) || isElse(ins)) {
170-
// Process the nested loop or if statement
171-
if (variableTypeChangesInBranchFromEnd(varPtr, ins, previousType))
172-
return true;
128+
Compiler::StaticType writeType = writeValueType(ins);
173129

174-
ins = skipBranch(ins);
175-
176-
if (isElse(ins)) {
177-
// Process if branch (the else branch is already processed)
178-
if (variableTypeChangesInBranchFromEnd(varPtr, ins, previousType))
179-
return true;
180-
181-
ins = skipBranch(ins);
182-
}
183-
} else if (ins->type == LLVMInstruction::Type::WriteVariable && ins->workVariable == varPtr->var) {
184-
// Variable write instruction
185-
return !writeTypesMatch(ins, previousType);
130+
if (typesMatch(writeType, previousType))
131+
return writeType;
132+
else
133+
return typeMightReset ? Compiler::StaticType::Unknown : writeType;
186134
}
187135

188136
ins = ins->previous;
189137
}
190138

191-
return false;
139+
return previousType;
192140
}
193141

194142
LLVMInstruction *LLVMTypeAnalyzer::skipBranch(LLVMInstruction *pos) const

src/engine/internal/llvm/llvmtypeanalyzer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ class LLVMTypeAnalyzer
1313
{
1414
public:
1515
Compiler::StaticType variableType(LLVMVariablePtr *varPtr, LLVMInstruction *pos, Compiler::StaticType previousType) const;
16-
bool variableTypeChangesInBranch(LLVMVariablePtr *varPtr, LLVMInstruction *start, Compiler::StaticType previousType) const;
16+
Compiler::StaticType variableTypeAfterBranch(LLVMVariablePtr *varPtr, LLVMInstruction *start, Compiler::StaticType previousType) const;
1717

1818
private:
19-
Compiler::StaticType variableTypeAfterBranch(LLVMVariablePtr *varPtr, LLVMInstruction *start, Compiler::StaticType previousType) const;
2019
Compiler::StaticType variableTypeAfterBranchFromEnd(LLVMVariablePtr *varPtr, LLVMInstruction *end, Compiler::StaticType previousType) const;
21-
bool variableTypeChangesInBranchFromEnd(LLVMVariablePtr *varPtr, LLVMInstruction *end, Compiler::StaticType previousType) const;
2220
LLVMInstruction *skipBranch(LLVMInstruction *pos) const;
2321
bool isLoopStart(LLVMInstruction *ins) const;
2422
bool isLoopEnd(LLVMInstruction *ins) const;

test/llvm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ add_executable(
2020
llvmexecutablecode_test.cpp
2121
llvmcodebuilder_test.cpp
2222
llvminstructionlist_test.cpp
23-
type_analyzer/variabletypechangesinbranch_test.cpp
23+
type_analyzer/variabletypeafterbranch_test.cpp
2424
type_analyzer/variabletype_test.cpp
2525
)
2626

test/llvm/type_analyzer/variabletype_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ TEST(LLVMTypeAnalyzer_VariableType, SameTypeIfElseInLoopWithTypeChange)
17631763
list.addInstruction(funcCall);
17641764

17651765
// The type always changes to string
1766-
ASSERT_EQ(analyzer.variableType(&varPtr, funcCall.get(), Compiler::StaticType::Number), Compiler::StaticType::String);
1766+
ASSERT_EQ(analyzer.variableType(&varPtr, funcCall.get(), Compiler::StaticType::Number), Compiler::StaticType::Unknown);
17671767
}
17681768

17691769
// TODO: Handle cross-variable dependencies

0 commit comments

Comments
 (0)