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
baff6088
Commit
baff6088
authored
Feb 08, 2023
by
lxq
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use DFS order for block traverse, pass
parent
805d36ad
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
11 deletions
+56
-11
include/codegen/liverange.hpp
include/codegen/liverange.hpp
+5
-0
src/codegen/codegen.cpp
src/codegen/codegen.cpp
+1
-1
src/codegen/liverange.cpp
src/codegen/liverange.cpp
+50
-10
No files found.
include/codegen/liverange.hpp
View file @
baff6088
#ifndef LIVERANGE_HPP
#define LIVERANGE_HPP
#include "Function.h"
#include "Module.h"
#include "Value.h"
#include <iostream>
#include <map>
#include <set>
#include <vector>
using
std
::
map
;
using
std
::
pair
;
...
...
@@ -18,6 +20,7 @@ using std::vector;
#define UNINITIAL -1
#define __LRA_PRINT__
#define __USE_DFS_ORDER__
namespace
LRA
{
...
...
@@ -70,6 +73,7 @@ class LiveRangeAnalyzer {
Module
*
m
;
// Function *func;
int
ir_cnt
;
vector
<
BasicBlock
*>
BB_DFS_Order
;
map
<
int
,
LiveSet
>
IN
,
OUT
;
map
<
Value
*
,
int
>
instr_id
;
map
<
pair
<
Value
*
,
Value
*>
,
int
>
cpstmt_id
;
...
...
@@ -77,6 +81,7 @@ class LiveRangeAnalyzer {
LVITS
liveIntervals
;
map
<
Value
*
,
Interval
>
intervalmap
;
void
get_dfs_order
(
Function
*
);
void
make_id
(
Function
*
);
void
make_interval
(
Function
*
);
...
...
src/codegen/codegen.cpp
View file @
baff6088
...
...
@@ -405,7 +405,7 @@ CodeGen::bool2branch(Instruction *instr) {
instr_ir
=
"fcmp.ceq.s $fcc0"
;
break
;
case
FCmpInst
::
NE
:
instr_ir
=
"fcmp.c
un
.s $fcc0"
;
instr_ir
=
"fcmp.c
ne
.s $fcc0"
;
break
;
case
FCmpInst
::
GT
:
instr_ir
=
"fcmp.cle.s $fcc0"
;
...
...
src/codegen/liverange.cpp
View file @
baff6088
#include "liverange.hpp"
#include "BasicBlock.h"
#include "Function.h"
#include <deque>
using
std
::
cout
;
using
std
::
endl
;
using
namespace
LRA
;
...
...
@@ -15,6 +18,7 @@ LiveRangeAnalyzer::clear() {
cpstmt_id
.
clear
();
intervalmap
.
clear
();
liveIntervals
.
clear
();
BB_DFS_Order
.
clear
();
}
LiveSet
...
...
@@ -38,13 +42,18 @@ LiveRangeAnalyzer::make_id(Function *func) {
// instruction numbering
// this is also the structure of the IR logically:
// ignore phi, add copy-statement
for
(
auto
&
bb
:
func
->
get_basic_blocks
())
{
for
(
auto
&
instr
:
bb
.
get_instructions
())
{
#ifdef __USE_DFS_ORDER__
for
(
auto
bb
:
BB_DFS_Order
)
{
#else
for
(
auto
&
bb_
:
func
->
get_basic_blocks
())
{
auto
bb
=
&
bb_
;
#endif
for
(
auto
&
instr
:
bb
->
get_instructions
())
{
if
(
instr
.
is_phi
())
continue
;
if
(
instr
.
is_br
()
or
instr
.
is_ret
())
{
// insert copy-stmt in front of the jump ir
auto
it
=
phi_map
.
find
(
&
bb
);
auto
it
=
phi_map
.
find
(
bb
);
if
(
it
!=
phi_map
.
end
())
{
for
(
auto
pr
:
it
->
second
)
{
cpstmt_id
[
pr
]
=
++
ir_cnt
;
...
...
@@ -56,25 +65,51 @@ LiveRangeAnalyzer::make_id(Function *func) {
}
}
void
LiveRangeAnalyzer
::
get_dfs_order
(
Function
*
func
)
{
map
<
BasicBlock
*
,
bool
>
vis
;
std
::
deque
<
BasicBlock
*>
Q
;
Q
.
push_back
(
func
->
get_entry_block
());
while
(
not
Q
.
empty
())
{
auto
bb
=
Q
.
front
();
Q
.
pop_front
();
if
(
vis
[
bb
])
continue
;
vis
[
bb
]
=
true
;
BB_DFS_Order
.
push_back
(
bb
);
for
(
auto
succ
:
bb
->
get_succ_basic_blocks
())
Q
.
push_front
(
succ
);
}
}
void
LiveRangeAnalyzer
::
run
(
Function
*
func
)
{
clear
();
get_dfs_order
(
func
);
make_id
(
func
);
bool
cont
=
true
;
while
(
cont
)
{
cont
=
false
;
// reverse traverse BasicBlocks
#ifdef __USE_DFS_ORDER__
for
(
auto
rit_bb
=
BB_DFS_Order
.
rbegin
();
rit_bb
!=
BB_DFS_Order
.
rend
();
++
rit_bb
)
{
auto
bb
=
*
rit_bb
;
#else
for
(
auto
rit_bb
=
func
->
get_basic_blocks
().
rbegin
();
rit_bb
!=
func
->
get_basic_blocks
().
rend
();
++
rit_bb
)
{
auto
bb
=
&
(
*
rit_bb
);
#endif
LiveSet
bef_in
,
out
;
bool
last_ir
=
true
;
// reverse traverse instructions
for
(
auto
rit_ir
=
rit_
bb
->
get_instructions
().
rbegin
();
rit_ir
!=
rit_
bb
->
get_instructions
().
rend
();
for
(
auto
rit_ir
=
bb
->
get_instructions
().
rbegin
();
rit_ir
!=
bb
->
get_instructions
().
rend
();
++
rit_ir
)
{
auto
instr
=
&
(
*
rit_ir
);
if
(
instr
->
is_phi
())
{
...
...
@@ -133,7 +168,7 @@ LiveRangeAnalyzer::run(Function *func) {
make_interval
(
func
);
#ifdef __LRA_PRINT__
print
(
func
,
fals
e
,
true
);
print
(
func
,
tru
e
,
true
);
#endif
}
...
...
@@ -205,14 +240,19 @@ LiveRangeAnalyzer::print(Function *func,
cout
<<
"
\t
in-set: "
+
print_liveSet
(
IN
.
at
(
0
))
<<
"
\n
"
;
cout
<<
"
\t
out-set: "
+
print_liveSet
(
OUT
.
at
(
0
))
<<
"
\n
"
;
}
for
(
auto
&
bb
:
func
->
get_basic_blocks
())
{
for
(
auto
&
instr
:
bb
.
get_instructions
())
{
#ifdef __USE_DFS_ORDER__
for
(
auto
bb
:
BB_DFS_Order
)
{
#else
for
(
auto
&
bb_
:
func
->
get_basic_blocks
())
{
auto
bb
=
&
bb_
;
#endif
for
(
auto
&
instr
:
bb
->
get_instructions
())
{
if
(
instr
.
is_phi
())
// ignore phi
continue
;
// insert copy-stmt
if
((
instr
.
is_br
()
or
instr
.
is_ret
())
and
phi_map
.
find
(
&
bb
)
!=
phi_map
.
end
())
{
for
(
auto
pr
:
phi_map
.
find
(
&
bb
)
->
second
)
{
phi_map
.
find
(
bb
)
!=
phi_map
.
end
())
{
for
(
auto
pr
:
phi_map
.
find
(
bb
)
->
second
)
{
auto
[
lv
,
rv
]
=
pr
;
auto
idx
=
cpstmt_id
.
at
(
pr
);
cout
<<
cpstmt_id
.
at
(
pr
)
<<
". "
<<
lv
->
get_name
()
<<
" = "
...
...
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