Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
bit.newnewcc
Compiler2023-bit.newnewcc
Commits
4fea4434
Commit
4fea4434
authored
1 year ago
by
OMG-link
Browse files
Options
Download
Patches
Plain Diff
feat: 条件判断语句简化
替代模式匹配,修复前端产生冗余语句的问题
parent
f4bb1a96
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/cn/edu/bit/newnewcc/pass/ir/BranchInstructionSimplifyPass.java
+55
-0
...u/bit/newnewcc/pass/ir/BranchInstructionSimplifyPass.java
with
55 additions
and
0 deletions
+55
-0
src/main/java/cn/edu/bit/newnewcc/pass/ir/BranchInstructionSimplifyPass.java
0 → 100644
+
55
−
0
View file @
4fea4434
package
cn.edu.bit.newnewcc.pass.ir
;
import
cn.edu.bit.newnewcc.ir.Module
;
import
cn.edu.bit.newnewcc.ir.type.IntegerType
;
import
cn.edu.bit.newnewcc.ir.value.BasicBlock
;
import
cn.edu.bit.newnewcc.ir.value.Function
;
import
cn.edu.bit.newnewcc.ir.value.constant.ConstInt
;
import
cn.edu.bit.newnewcc.ir.value.instruction.BranchInst
;
import
cn.edu.bit.newnewcc.ir.value.instruction.IntegerCompareInst
;
import
cn.edu.bit.newnewcc.ir.value.instruction.ZeroExtensionInst
;
/**
* 消除前端产生的冗余条件跳转语句 <br>
*/
/*
* 前端产生的语句:
* %1 = cmp v1, v2
* %2 = zext i1 %1 to i32
* %3 = icmp ne %2, 0
* br %3, %true_block, %false_block
*
* 简化后的语句:
* %1 = cmp v1, v2
* br %1, %true_block, %false_block
*
*/
public
class
BranchInstructionSimplifyPass
{
public
static
boolean
runOnBasicBlock
(
BasicBlock
basicBlock
)
{
if
(!(
basicBlock
.
getTerminateInstruction
()
instanceof
BranchInst
branchInst
))
return
false
;
if
(!(
branchInst
.
getCondition
()
instanceof
IntegerCompareInst
extraCmp
))
return
false
;
if
(!(
extraCmp
.
getCondition
()
==
IntegerCompareInst
.
Condition
.
NE
))
return
false
;
if
(!(
extraCmp
.
getOperand2
()
==
ConstInt
.
getInstance
(
0
)))
return
false
;
if
(!(
extraCmp
.
getOperand1
()
instanceof
ZeroExtensionInst
zeroExtensionInst
))
return
false
;
if
(!(
zeroExtensionInst
.
getSourceType
()
==
IntegerType
.
getI1
()))
return
false
;
branchInst
.
setCondition
(
zeroExtensionInst
.
getSourceOperand
());
return
true
;
}
public
static
boolean
runOnFunction
(
Function
function
)
{
boolean
changed
=
false
;
for
(
BasicBlock
basicBlock
:
function
.
getBasicBlocks
())
{
changed
|=
runOnBasicBlock
(
basicBlock
);
}
return
changed
;
}
public
static
boolean
runOnModule
(
Module
module
)
{
boolean
changed
=
false
;
for
(
Function
function
:
module
.
getFunctions
())
{
changed
|=
runOnFunction
(
function
);
}
return
changed
;
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets