Skip to content

Commit

Permalink
Use 0 instead of undefined as the replacement in FuncSigOpts
Browse files Browse the repository at this point in the history
Summary:
`0` is faster to create at runtime than `undefined`.
Therefore, change the unused arguments to pass `0` during FuncSigOpts.

Reviewed By: neildhar

Differential Revision: D42999599

fbshipit-source-id: 222d982c5438f094b7bcbfb8dc7a2d3a2637b238
  • Loading branch information
avp authored and facebook-github-bot committed May 24, 2023
1 parent 4f37e26 commit 58ac81b
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 26 deletions.
7 changes: 4 additions & 3 deletions lib/Optimizer/Scalar/FuncSigOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,16 @@ static bool performFSO(Function *F, std::vector<Function *> &worklist) {

llvh::DenseSet<Function *> toRedo;

// Replace all unused arguments with undef.
// Replace all unused arguments with 0 (which is cheap to create).
Literal *zero = builder.getLiteralPositiveZero();
for (auto &arg : unusedParams) {
Value *prevArg = arg.call->getArgument(arg.idx);
if (!llvh::isa<Literal>(prevArg))
toRedo.insert(arg.call->getParent()->getParent());

arg.call->setArgument(undef, arg.idx);
arg.call->setArgument(zero, arg.idx);
if (arg.param)
arg.param->setType(Type::createUndefined());
arg.param->setType(Type::createUint32());
NumArgsOpt++;
}

Expand Down
14 changes: 7 additions & 7 deletions test/Optimizer/callee.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function load_store_multiple_test() {
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %foo(): number
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %foo(): number, empty: any, undefined: undefined, 12: number
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %foo(): number, empty: any, 0: number, 12: number
// CHECK-NEXT: %2 = ReturnInst 12: number
// CHECK-NEXT:function_end

Expand All @@ -80,7 +80,7 @@ function load_store_multiple_test() {
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %"foo 1#"(): number
// CHECK-NEXT: %1 = LoadPropertyInst (:any) %0: object, "prototype": string
// CHECK-NEXT: %2 = CreateThisInst (:object) %1: any, %0: object
// CHECK-NEXT: %3 = ConstructInst (:number) %0: object, %"foo 1#"(): number, empty: any, undefined: undefined, 12: number
// CHECK-NEXT: %3 = ConstructInst (:number) %0: object, %"foo 1#"(): number, empty: any, 0: number, 12: number
// CHECK-NEXT: %4 = ReturnInst %2: object
// CHECK-NEXT:function_end

Expand All @@ -90,7 +90,7 @@ function load_store_multiple_test() {
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %ping(): number
// CHECK-NEXT: %1 = CreateFunctionInst (:object) %k(): number
// CHECK-NEXT: %2 = StoreFrameInst %1: object, [k]: object
// CHECK-NEXT: %3 = CallInst (:number) %0: object, %ping(): number, empty: any, undefined: undefined
// CHECK-NEXT: %3 = CallInst (:number) %0: object, %ping(): number, empty: any, 0: number
// CHECK-NEXT: %4 = ReturnInst 123: number
// CHECK-NEXT:function_end

Expand All @@ -100,7 +100,7 @@ function load_store_multiple_test() {
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %"foo 2#"(): number
// CHECK-NEXT: %1 = StoreFrameInst %0: object, [foo]: object
// CHECK-NEXT: %2 = CreateFunctionInst (:object) %bar(): number
// CHECK-NEXT: %3 = CallInst (:number) %2: object, %bar(): number, empty: any, undefined: undefined
// CHECK-NEXT: %3 = CallInst (:number) %2: object, %bar(): number, empty: any, 0: number
// CHECK-NEXT: %4 = ReturnInst %3: number
// CHECK-NEXT:function_end

Expand All @@ -120,7 +120,7 @@ function load_store_multiple_test() {
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = LoadFrameInst (:object) [k@load_store_test]: object
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %k(): number, empty: any, undefined: undefined, 123: number
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %k(): number, empty: any, 0: number, 123: number
// CHECK-NEXT: %2 = ReturnInst 123: number
// CHECK-NEXT:function_end

Expand All @@ -141,8 +141,8 @@ function load_store_multiple_test() {
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = LoadFrameInst (:object) [foo@load_store_multiple_test]: object
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %"foo 2#"(): number, empty: any, undefined: undefined, true: boolean, 7: number
// CHECK-NEXT: %2 = CallInst (:number) %0: object, %"foo 2#"(): number, empty: any, undefined: undefined, true: boolean, 8: number
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %"foo 2#"(): number, empty: any, 0: number, true: boolean, 7: number
// CHECK-NEXT: %2 = CallInst (:number) %0: object, %"foo 2#"(): number, empty: any, 0: number, true: boolean, 8: number
// CHECK-NEXT: %3 = BinaryAddInst (:number) %1: number, %2: number
// CHECK-NEXT: %4 = ReturnInst %3: number
// CHECK-NEXT:function_end
2 changes: 1 addition & 1 deletion test/Optimizer/constructor_callee.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function ctor_load_store_test() {
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %"use_this 1#"(): undefined
// CHECK-NEXT: %1 = StoreFrameInst %0: object, [use_this]: object
// CHECK-NEXT: %2 = CreateFunctionInst (:object) %construct_use_this(): object
// CHECK-NEXT: %3 = CallInst (:object) %2: object, %construct_use_this(): object, empty: any, undefined: undefined
// CHECK-NEXT: %3 = CallInst (:object) %2: object, %construct_use_this(): object, empty: any, 0: number
// CHECK-NEXT: %4 = ReturnInst %3: object
// CHECK-NEXT:function_end

Expand Down
12 changes: 6 additions & 6 deletions test/Optimizer/func_sig_opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function test_async() {
// CHECK-NEXT: %1 = CreateFunctionInst (:object) %foo(): number
// CHECK-NEXT: %2 = CreateFunctionInst (:object) %bar(): string|number|bigint
// CHECK-NEXT: %3 = StorePropertyStrictInst %2: object, %0: any, "p": string
// CHECK-NEXT: %4 = CallInst (:number) %1: object, %foo(): number, empty: any, undefined: undefined, 1: number, 2: number
// CHECK-NEXT: %4 = CallInst (:number) %1: object, %foo(): number, empty: any, 0: number, 1: number, 2: number
// CHECK-NEXT: %5 = CallInst (:string|number|bigint) %2: object, %bar(): string|number|bigint, empty: any, undefined: undefined, 1: number, 2: number
// CHECK-NEXT: %6 = BinaryAddInst (:string|number) %4: number, %5: string|number|bigint
// CHECK-NEXT: %7 = ReturnInst %6: string|number
Expand All @@ -99,8 +99,8 @@ function test_async() {
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %builder(): number
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %builder(): number, empty: any, undefined: undefined
// CHECK-NEXT: %2 = CallInst (:number) %0: object, %builder(): number, empty: any, undefined: undefined
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %builder(): number, empty: any, 0: number
// CHECK-NEXT: %2 = CallInst (:number) %0: object, %builder(): number, empty: any, 0: number
// CHECK-NEXT: %3 = BinaryAddInst (:number) %1: number, %2: number
// CHECK-NEXT: %4 = ReturnInst %3: number
// CHECK-NEXT:function_end
Expand Down Expand Up @@ -166,7 +166,7 @@ function test_async() {
// CHECK-NEXT: %2 = ReturnInst %1: number
// CHECK-NEXT:function_end

// CHECK:function foo2(a: any, b: number, c: number, d: undefined): string|number [allCallsitesKnownInStrictMode]
// CHECK:function foo2(a: any, b: number, c: number, d: number): string|number [allCallsitesKnownInStrictMode]
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = LoadParamInst (:any) %a: any
Expand All @@ -181,7 +181,7 @@ function test_async() {
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = LoadParamInst (:any) %e: any
// CHECK-NEXT: %1 = LoadFrameInst (:object) [foo2@test_unused_and_duplicate_params]: object
// CHECK-NEXT: %2 = CallInst (:string|number) %1: object, %foo2(): string|number, empty: any, undefined: undefined, %0: any, 2: number, 1: number, undefined: undefined, undefined: undefined, undefined: undefined
// CHECK-NEXT: %2 = CallInst (:string|number) %1: object, %foo2(): string|number, empty: any, 0: number, %0: any, 2: number, 1: number, 0: number, 0: number, 0: number
// CHECK-NEXT: %3 = ReturnInst undefined: undefined
// CHECK-NEXT:function_end

Expand All @@ -190,7 +190,7 @@ function test_async() {
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = LoadParamInst (:any) %e: any
// CHECK-NEXT: %1 = LoadFrameInst (:object) [foo2@test_unused_and_duplicate_params]: object
// CHECK-NEXT: %2 = CallInst (:string|number) %1: object, %foo2(): string|number, empty: any, undefined: undefined, %0: any, 2: number, 3: number, undefined: undefined, undefined: undefined
// CHECK-NEXT: %2 = CallInst (:string|number) %1: object, %foo2(): string|number, empty: any, 0: number, %0: any, 2: number, 3: number, 0: number, 0: number
// CHECK-NEXT: %3 = ReturnInst undefined: undefined
// CHECK-NEXT:function_end

Expand Down
2 changes: 1 addition & 1 deletion test/Optimizer/function-analysis-call-closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function main() {
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %f(): undefined
// CHECK-NEXT: %1 = CallInst (:undefined) %0: object, %f(): undefined, empty: any, undefined: undefined
// CHECK-NEXT: %1 = CallInst (:undefined) %0: object, %f(): undefined, empty: any, 0: number
// CHECK-NEXT: %2 = ReturnInst undefined: undefined
// CHECK-NEXT:function_end

Expand Down
4 changes: 2 additions & 2 deletions test/Optimizer/function-analysis-chained-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function main() {
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %foo(): object
// CHECK-NEXT: %1 = CreateFunctionInst (:object) %x(): number
// CHECK-NEXT: %2 = StoreFrameInst %1: object, [x]: object
// CHECK-NEXT: %3 = CallInst (:object) %0: object, %foo(): object, empty: any, undefined: undefined
// CHECK-NEXT: %3 = CallInst (:object) %0: object, %foo(): object, empty: any, 0: number
// CHECK-NEXT: %4 = ReturnInst undefined: undefined
// CHECK-NEXT:function_end

Expand All @@ -64,6 +64,6 @@ function main() {
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = LoadFrameInst (:object) [y@foo]: object
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %x(): number, empty: any, undefined: undefined
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %x(): number, empty: any, 0: number
// CHECK-NEXT: %2 = ReturnInst undefined: undefined
// CHECK-NEXT:function_end
2 changes: 1 addition & 1 deletion test/Optimizer/function-analysis-construction-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function main() {
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %f(): undefined
// CHECK-NEXT: %1 = LoadPropertyInst (:any) %0: object, "prototype": string
// CHECK-NEXT: %2 = CreateThisInst (:object) %1: any, %0: object
// CHECK-NEXT: %3 = ConstructInst (:undefined) %0: object, %f(): undefined, empty: any, undefined: undefined
// CHECK-NEXT: %3 = ConstructInst (:undefined) %0: object, %f(): undefined, empty: any, 0: number
// CHECK-NEXT: %4 = ReturnInst %2: object
// CHECK-NEXT:function_end

Expand Down
2 changes: 1 addition & 1 deletion test/Optimizer/function-analysis-construction.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function main() {
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %f(): undefined
// CHECK-NEXT: %1 = LoadPropertyInst (:any) %0: object, "prototype": string
// CHECK-NEXT: %2 = CreateThisInst (:object) %1: any, %0: object
// CHECK-NEXT: %3 = ConstructInst (:undefined) %0: object, %f(): undefined, empty: any, undefined: undefined
// CHECK-NEXT: %3 = ConstructInst (:undefined) %0: object, %f(): undefined, empty: any, 0: number
// CHECK-NEXT: %4 = ReturnInst %2: object
// CHECK-NEXT:function_end

Expand Down
2 changes: 1 addition & 1 deletion test/Optimizer/function-analysis-single-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function main(x) {
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %f(): undefined
// CHECK-NEXT: %1 = CallInst (:undefined) %0: object, %f(): undefined, empty: any, undefined: undefined
// CHECK-NEXT: %1 = CallInst (:undefined) %0: object, %f(): undefined, empty: any, 0: number
// CHECK-NEXT: %2 = ReturnInst undefined: undefined
// CHECK-NEXT:function_end

Expand Down
2 changes: 1 addition & 1 deletion test/Optimizer/inline-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %""(): number
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %""(): number, empty: any, undefined: undefined
// CHECK-NEXT: %1 = CallInst (:number) %0: object, %""(): number, empty: any, 0: number
// CHECK-NEXT: %2 = ReturnInst %1: number
// CHECK-NEXT:function_end

Expand Down
2 changes: 1 addition & 1 deletion test/Optimizer/inline-recursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = LoadFrameInst (:object) [f@main]: object
// CHECK-NEXT: %1 = CallInst (:any) %0: object, %f(): any, empty: any, undefined: undefined
// CHECK-NEXT: %1 = CallInst (:any) %0: object, %f(): any, empty: any, 0: number
// CHECK-NEXT: %2 = ReturnInst %1: any
// CHECK-NEXT:function_end
2 changes: 1 addition & 1 deletion test/Optimizer/inline-unreachable-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// CHECK-NEXT:frame = []
// CHECK-NEXT:%BB0:
// CHECK-NEXT: %0 = CreateFunctionInst (:object) %main(): undefined
// CHECK-NEXT: %1 = CallInst (:undefined) %0: object, %main(): undefined, empty: any, undefined: undefined
// CHECK-NEXT: %1 = CallInst (:undefined) %0: object, %main(): undefined, empty: any, 0: number
// CHECK-NEXT: %2 = ReturnInst undefined: undefined
// CHECK-NEXT:function_end

Expand Down

0 comments on commit 58ac81b

Please sign in to comment.