Skip to content

Commit 585f021

Browse files
committed
Go CFG: merge parameter 'arg' node into 'param-init'
Each parameter previously created two additional nodes: an 'arg:i' node (ReadArgumentInstruction) holding the incoming argument value, and a 'param-init:i' node (InitParameterInstruction) writing it to the parameter's SSA variable. The 'arg' node existed only to be the RHS of the param-init write; the ParameterNode and inter-procedural flow are already anchored on param-init. Make InitParameterInstruction its own RHS and drop the 'arg:i' node, halving the per-parameter node count. DeadStoreOfLocal now excludes InitParameterInstruction (instead of ReadArgumentInstruction). The data-flow changes are pure relabelings (arg -> param-init) with no flow loss.
1 parent 06bc989 commit 585f021

6 files changed

Lines changed: 106 additions & 205 deletions

File tree

go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,12 @@ module GoCfg {
561561
tag = "result-read:" + i.toString()
562562
)
563563
or
564-
// Parameter init + argument nodes (on the function body)
564+
// Parameter init nodes (on the function body)
565565
exists(int i, Go::FuncDef fd |
566566
n = fd.getBody() and
567567
exists(fd.getBody()) and
568568
exists(fd.getParameter(i)) and
569-
(tag = "param-init:" + i.toString() or tag = "arg:" + i.toString())
569+
tag = "param-init:" + i.toString()
570570
)
571571
or
572572
// Result variable init (on the function body)
@@ -1729,8 +1729,8 @@ module GoCfg {
17291729

17301730
/**
17311731
* Function definition prologue and epilogue:
1732-
* - Prologue: Before(body) → arg:-1 → param-init:-1 → arg:0 → param-init:0 → ...
1733-
* when a receiver exists; otherwise it starts at arg:0. Then
1732+
* - Prologue: Before(body) → param-init:-1 → param-init:0 → ... when a
1733+
* receiver exists; otherwise it starts at param-init:0. Then
17341734
* result-zero-init:0 → result-init:0 → ... → first statement
17351735
* - Epilogue: return → result-read:0 → result-read:1 → ... → result-read:last
17361736
*
@@ -1773,13 +1773,13 @@ module GoCfg {
17731773
// Before(body) → first prologue node, or first body statement if no prologue
17741774
n1.isBefore(fd.getBody()) and
17751775
(
1776-
// Has receiver: start with arg:-1
1777-
exists(fd.getParameter(-1)) and n2.isAdditional(fd.getBody(), "arg:-1")
1776+
// Has receiver: start with param-init:-1
1777+
exists(fd.getParameter(-1)) and n2.isAdditional(fd.getBody(), "param-init:-1")
17781778
or
1779-
// Has ordinary parameters: start with arg:0
1779+
// Has ordinary parameters: start with param-init:0
17801780
not exists(fd.getParameter(-1)) and
17811781
exists(fd.getParameter(0)) and
1782-
n2.isAdditional(fd.getBody(), "arg:0")
1782+
n2.isAdditional(fd.getBody(), "param-init:0")
17831783
or
17841784
// No parameters, has result vars: start with result-zero-init:0
17851785
not exists(fd.getParameter(_)) and
@@ -1792,19 +1792,13 @@ module GoCfg {
17921792
funcDefBodyStart(fd, n2)
17931793
)
17941794
or
1795-
// arg:i → param-init:i (for each parameter)
1796-
exists(int i | exists(fd.getParameter(i)) |
1797-
n1.isAdditional(fd.getBody(), "arg:" + i.toString()) and
1798-
n2.isAdditional(fd.getBody(), "param-init:" + i.toString())
1799-
)
1800-
or
1801-
// param-init:i → next: arg:(i+1), or result-zero-init:0, or Before(body)
1795+
// param-init:i → next: param-init:(i+1), or result-zero-init:0, or Before(body)
18021796
exists(int i | exists(fd.getParameter(i)) |
18031797
n1.isAdditional(fd.getBody(), "param-init:" + i.toString()) and
18041798
(
18051799
// Next parameter exists
18061800
exists(fd.getParameter(i + 1)) and
1807-
n2.isAdditional(fd.getBody(), "arg:" + (i + 1).toString())
1801+
n2.isAdditional(fd.getBody(), "param-init:" + (i + 1).toString())
18081802
or
18091803
// No next parameter, has result vars: go to result-zero-init:0
18101804
not exists(fd.getParameter(i + 1)) and

go/ql/lib/semmle/go/controlflow/IR.qll

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ module IR {
199199
or
200200
this instanceof InitParameterInstruction and result = "parameter initialization"
201201
or
202-
this instanceof ReadArgumentInstruction and result = "argument"
203-
or
204202
this instanceof InitResultInstruction and result = "result initialization"
205203
or
206204
this instanceof GetNextEntryInstruction and result = "next key-value pair"
@@ -1033,23 +1031,7 @@ module IR {
10331031
parm = fd.getParameter(idx)
10341032
}
10351033

1036-
override Instruction getRhs() {
1037-
result.(ReadArgumentInstruction).isAdditional(fd.getBody(), "arg:" + idx.toString())
1038-
}
1039-
1040-
override ControlFlow::Root getRoot() { result = parm.getFunction() }
1041-
}
1042-
1043-
/** An instruction reading the value of a function argument. */
1044-
class ReadArgumentInstruction extends Instruction {
1045-
Parameter parm;
1046-
int idx;
1047-
FuncDef fd;
1048-
1049-
ReadArgumentInstruction() {
1050-
this.isAdditional(fd.getBody(), "arg:" + idx.toString()) and
1051-
parm = fd.getParameter(idx)
1052-
}
1034+
override Instruction getRhs() { result = this }
10531035

10541036
override Type getResultType() { result = parm.getType() }
10551037

go/ql/src/RedundantCode/DeadStoreOfLocal.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ predicate isSimple(IR::Instruction nd) {
2626
nd = IR::implicitInitInstruction(_)
2727
or
2828
// don't flag parameters
29-
nd instanceof IR::ReadArgumentInstruction
29+
nd instanceof IR::InitParameterInstruction
3030
}
3131

3232
from IR::Instruction def, SsaSourceVariable target, IR::Instruction rhs

0 commit comments

Comments
 (0)