Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
2
2022fall-Compiler_CMinus
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李晓奇
2022fall-Compiler_CMinus
Commits
f0f0bb81
Commit
f0f0bb81
authored
Nov 29, 2022
by
李晓奇
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get 4.2 files
parent
86cd4103
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
34 deletions
+63
-34
README.md
README.md
+1
-0
src/optimization/GVN.cpp
src/optimization/GVN.cpp
+62
-34
No files found.
README.md
View file @
f0f0bb81
...
...
@@ -22,6 +22,7 @@
*
[
lab4.2
](
./Documentations/4.2-gvn/
)
+
DDL: 2022-12-12 23:59:59 (UTC+8)
-
[
report
](
./Reports/4.2-gvn/report.md
)
## FAQ: How to merge upstream remote branches
...
...
src/optimization/GVN.cpp
View file @
f0f0bb81
...
...
@@ -28,44 +28,69 @@ static auto get_const_fp_value = [](Value *v) { return dynamic_cast<ConstantFP *
Constant
*
ConstFolder
::
compute
(
Instruction
*
instr
,
Constant
*
value1
,
Constant
*
value2
)
{
auto
op
=
instr
->
get_instr_type
();
switch
(
op
)
{
case
Instruction
::
add
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
+
get_const_int_value
(
value2
),
module_
);
case
Instruction
::
sub
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
-
get_const_int_value
(
value2
),
module_
);
case
Instruction
::
mul
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
*
get_const_int_value
(
value2
),
module_
);
case
Instruction
::
sdiv
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
/
get_const_int_value
(
value2
),
module_
);
case
Instruction
::
fadd
:
return
ConstantFP
::
get
(
get_const_fp_value
(
value1
)
+
get_const_fp_value
(
value2
),
module_
);
case
Instruction
::
fsub
:
return
ConstantFP
::
get
(
get_const_fp_value
(
value1
)
-
get_const_fp_value
(
value2
),
module_
);
case
Instruction
::
fmul
:
return
ConstantFP
::
get
(
get_const_fp_value
(
value1
)
*
get_const_fp_value
(
value2
),
module_
);
case
Instruction
::
fdiv
:
return
ConstantFP
::
get
(
get_const_fp_value
(
value1
)
/
get_const_fp_value
(
value2
),
module_
);
case
Instruction
::
add
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
+
get_const_int_value
(
value2
),
module_
);
case
Instruction
::
sub
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
-
get_const_int_value
(
value2
),
module_
);
case
Instruction
::
mul
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
*
get_const_int_value
(
value2
),
module_
);
case
Instruction
::
sdiv
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
/
get_const_int_value
(
value2
),
module_
);
case
Instruction
::
fadd
:
return
ConstantFP
::
get
(
get_const_fp_value
(
value1
)
+
get_const_fp_value
(
value2
),
module_
);
case
Instruction
::
fsub
:
return
ConstantFP
::
get
(
get_const_fp_value
(
value1
)
-
get_const_fp_value
(
value2
),
module_
);
case
Instruction
::
fmul
:
return
ConstantFP
::
get
(
get_const_fp_value
(
value1
)
*
get_const_fp_value
(
value2
),
module_
);
case
Instruction
::
fdiv
:
return
ConstantFP
::
get
(
get_const_fp_value
(
value1
)
/
get_const_fp_value
(
value2
),
module_
);
case
Instruction
::
cmp
:
switch
(
dynamic_cast
<
CmpInst
*>
(
instr
)
->
get_cmp_op
())
{
case
CmpInst
::
EQ
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
==
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
NE
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
!=
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
GT
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
>
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
GE
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
>=
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
LT
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
<
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
LE
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
<=
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
EQ
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
==
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
NE
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
!=
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
GT
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
>
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
GE
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
>=
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
LT
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
<
get_const_int_value
(
value2
),
module_
);
case
CmpInst
::
LE
:
return
ConstantInt
::
get
(
get_const_int_value
(
value1
)
<=
get_const_int_value
(
value2
),
module_
);
}
case
Instruction
::
fcmp
:
switch
(
dynamic_cast
<
FCmpInst
*>
(
instr
)
->
get_cmp_op
())
{
case
FCmpInst
::
EQ
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
==
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
NE
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
!=
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
GT
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
>
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
GE
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
>=
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
LT
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
<
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
LE
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
<=
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
EQ
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
==
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
NE
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
!=
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
GT
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
>
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
GE
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
>=
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
LT
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
<
get_const_fp_value
(
value2
),
module_
);
case
FCmpInst
::
LE
:
return
ConstantInt
::
get
(
get_const_fp_value
(
value1
)
<=
get_const_fp_value
(
value2
),
module_
);
}
default:
return
nullptr
;
default:
return
nullptr
;
}
}
Constant
*
ConstFolder
::
compute
(
Instruction
*
instr
,
Constant
*
value1
)
{
auto
op
=
instr
->
get_instr_type
();
switch
(
op
)
{
case
Instruction
::
sitofp
:
return
ConstantFP
::
get
((
float
)
get_const_int_value
(
value1
),
module_
);
case
Instruction
::
fptosi
:
return
ConstantInt
::
get
((
int
)
get_const_fp_value
(
value1
),
module_
);
case
Instruction
::
zext
:
return
ConstantInt
::
get
((
int
)
get_const_int_value
(
value1
),
module_
);
default:
return
nullptr
;
case
Instruction
::
sitofp
:
return
ConstantFP
::
get
((
float
)
get_const_int_value
(
value1
),
module_
);
case
Instruction
::
fptosi
:
return
ConstantInt
::
get
((
int
)
get_const_fp_value
(
value1
),
module_
);
case
Instruction
::
zext
:
return
ConstantInt
::
get
((
int
)
get_const_int_value
(
value1
),
module_
);
default:
return
nullptr
;
}
}
...
...
@@ -275,9 +300,12 @@ bool GVNExpression::operator==(const Expression &lhs, const Expression &rhs) {
if
(
lhs
.
get_expr_type
()
!=
rhs
.
get_expr_type
())
return
false
;
switch
(
lhs
.
get_expr_type
())
{
case
Expression
::
e_constant
:
return
equiv_as
<
ConstantExpression
>
(
lhs
,
rhs
);
case
Expression
::
e_bin
:
return
equiv_as
<
BinaryExpression
>
(
lhs
,
rhs
);
case
Expression
::
e_phi
:
return
equiv_as
<
PhiExpression
>
(
lhs
,
rhs
);
case
Expression
::
e_constant
:
return
equiv_as
<
ConstantExpression
>
(
lhs
,
rhs
);
case
Expression
::
e_bin
:
return
equiv_as
<
BinaryExpression
>
(
lhs
,
rhs
);
case
Expression
::
e_phi
:
return
equiv_as
<
PhiExpression
>
(
lhs
,
rhs
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment