Skip to content

Commit

Permalink
Emit Inc/Dec during IR generation
Browse files Browse the repository at this point in the history
Summary: Emit Inc/Dec nodes during IR generation.

Reviewed By: neildhar

Differential Revision: D34879601

fbshipit-source-id: 060f2365386611e60ad6686f989fb2d29cab07ec
  • Loading branch information
jpporto authored and facebook-github-bot committed Mar 22, 2022
1 parent b74eb2d commit fdf3d86
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 180 deletions.
20 changes: 6 additions & 14 deletions lib/IRGen/ESTreeIRGen-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1444,30 +1444,22 @@ Value *ESTreeIRGen::genUpdateExpr(ESTree::UpdateExpressionNode *updateExpr) {
LLVM_DEBUG(dbgs() << "IRGen update expression.\n");
bool isPrefix = updateExpr->_prefix;

// The operands ++ and -- are equivalent to adding or subtracting the
// literal 1.
// See section 12.4.4.1.
BinaryOperatorInst::OpKind opKind;
UnaryOperatorInst::OpKind opKind;
if (updateExpr->_operator->str() == "++") {
opKind = BinaryOperatorInst::OpKind::AddKind;
opKind = UnaryOperatorInst::OpKind::IncKind;
} else if (updateExpr->_operator->str() == "--") {
opKind = BinaryOperatorInst::OpKind::SubtractKind;
opKind = UnaryOperatorInst::OpKind::DecKind;
} else {
llvm_unreachable("Invalid update operator");
}

LReference lref = createLRef(updateExpr->_argument, false);

// Load the original value.
Value *original = lref.emitLoad();
Value *original = Builder.createAsNumberInst(lref.emitLoad());

// Convert the original value to number. Even on suffix operators we return
// the converted value.
original = Builder.createAsNumberInst(original);

// Create the +1 or -1.
Value *result = Builder.createBinaryOperatorInst(
original, Builder.getLiteralNumber(1), opKind);
// Create the inc or dec.
Value *result = Builder.createUnaryOperatorInst(original, opKind);

// Store the result.
lref.emitStore(result);
Expand Down
29 changes: 14 additions & 15 deletions test/BCGen/HBC/debuggercheckbreak.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,26 @@
//CHECK-NEXT: AsyncBreakCheck
//CHECK-NEXT: Ret r0

//CHECK-LABEL:Function<test1>(1 params, 17 registers, 0 symbols):
//CHECK-LABEL:Function<test1>(1 params, 16 registers, 0 symbols):
//CHECK-NEXT:Offset in debug table: {{.*}}
//CHECK-NEXT: GetGlobalObject r0
//CHECK-NEXT: LoadConstUInt8 r5, 3
//CHECK-NEXT: LoadConstUInt8 r4, 1
//CHECK-NEXT: LoadConstUInt8 r4, 3
//CHECK-NEXT: LoadConstUInt8 r1, 5
//CHECK-NEXT: LoadConstUInt8 r3, 10
//CHECK-NEXT: LoadConstZero r6
//CHECK-NEXT: LoadConstZero r5
//CHECK-NEXT: AsyncBreakCheck
//CHECK-NEXT:L3:
//CHECK-NEXT: TryGetById r7, r0, 1, "Math"
//CHECK-NEXT: GetByIdShort r2, r7, 2, "random"
//CHECK-NEXT: Call1 r7, r2, r7
//CHECK-NEXT: Mov r2, r6
//CHECK-NEXT: TryGetById r6, r0, 1, "Math"
//CHECK-NEXT: GetByIdShort r2, r6, 2, "random"
//CHECK-NEXT: Call1 r6, r2, r6
//CHECK-NEXT: Mov r2, r5
//CHECK-NEXT: AsyncBreakCheck
//CHECK-NEXT: JStrictEqual L1, r7, r5
//CHECK-NEXT: TryGetById r8, r0, 1, "Math"
//CHECK-NEXT: GetByIdShort r7, r8, 2, "random"
//CHECK-NEXT: Call1 r7, r7, r8
//CHECK-NEXT: JStrictEqual L2, r7, r1
//CHECK-NEXT: AddN r6, r2, r4
//CHECK-NEXT: JStrictEqual L1, r6, r4
//CHECK-NEXT: TryGetById r7, r0, 1, "Math"
//CHECK-NEXT: GetByIdShort r6, r7, 2, "random"
//CHECK-NEXT: Call1 r6, r6, r7
//CHECK-NEXT: JStrictEqual L2, r6, r1
//CHECK-NEXT: Inc r5, r2
//CHECK-NEXT: Jmp L3
//CHECK-NEXT:L2:
//CHECK-NEXT: AsyncBreakCheck
Expand All @@ -48,7 +47,7 @@
//CHECK-NEXT: Mov r2, r1
//CHECK-NEXT: JNotGreaterN L4, r2, r3
//CHECK-NEXT:L5:
//CHECK-NEXT: SubN r1, r1, r4
//CHECK-NEXT: Dec r1, r1
//CHECK-NEXT: Mov r2, r1
//CHECK-NEXT: AsyncBreakCheck
//CHECK-NEXT: JGreaterN L5, r2, r3
Expand Down
47 changes: 23 additions & 24 deletions test/BCGen/HBC/es6/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,47 @@ function *loop(x) {
// CHECK-NEXT: CreateGenerator r2, r0, 2
// CHECK-NEXT: Ret r2

// CHECK-LABEL: Function<?anon_0_loop>(2 params, 15 registers, 2 symbols):
// CHECK-LABEL: Function<?anon_0_loop>(2 params, 14 registers, 2 symbols):
// CHECK-NEXT: Offset in debug table: source 0x{{.*}}, lexical 0x0000
// CHECK-NEXT: StartGenerator
// CHECK-NEXT: CreateEnvironment r0
// CHECK-NEXT: LoadParam r1, 1
// CHECK-NEXT: LoadConstUndefined r2
// CHECK-NEXT: LoadConstZero r3
// CHECK-NEXT: LoadConstUInt8 r4, 1
// CHECK-NEXT: LoadConstString r5, "DONE LOOPING"
// CHECK-NEXT: GetGlobalObject r6
// CHECK-NEXT: ResumeGenerator r8, r7
// CHECK-NEXT: Mov r9, r7
// CHECK-NEXT: JmpTrue L1, r9
// CHECK-NEXT: LoadConstString r4, "DONE LOOPING"
// CHECK-NEXT: GetGlobalObject r5
// CHECK-NEXT: ResumeGenerator r7, r6
// CHECK-NEXT: Mov r8, r6
// CHECK-NEXT: JmpTrue L1, r8
// CHECK-NEXT: StoreNPToEnvironment r0, 0, r2
// CHECK-NEXT: StoreToEnvironment r0, 1, r1
// CHECK-NEXT: StoreNPToEnvironment r0, 0, r3
// CHECK-NEXT: TryGetById r7, r6, 1, "y"
// CHECK-NEXT: JmpFalse L2, r7
// CHECK-NEXT: TryGetById r6, r5, 1, "y"
// CHECK-NEXT: JmpFalse L2, r6
// CHECK-NEXT: L5:
// CHECK-NEXT: LoadFromEnvironment r7, r0, 1
// CHECK-NEXT: LoadFromEnvironment r9, r0, 0
// CHECK-NEXT: ToNumber r10, r9
// CHECK-NEXT: AddN r11, r10, r4
// CHECK-NEXT: StoreToEnvironment r0, 0, r11
// CHECK-NEXT: GetByVal r12, r7, r10
// CHECK-NEXT: LoadFromEnvironment r6, r0, 1
// CHECK-NEXT: LoadFromEnvironment r8, r0, 0
// CHECK-NEXT: ToNumber r9, r8
// CHECK-NEXT: Inc r10, r9
// CHECK-NEXT: StoreToEnvironment r0, 0, r10
// CHECK-NEXT: GetByVal r11, r6, r9
// CHECK-NEXT: SaveGenerator L3
// CHECK-NEXT: Ret r12
// CHECK-NEXT: Ret r11
// CHECK-NEXT: L3:
// CHECK-NEXT: ResumeGenerator r7, r13
// CHECK-NEXT: Mov r9, r13
// CHECK-NEXT: JmpTrue L4, r9
// CHECK-NEXT: TryGetById r9, r6, 1, "y"
// CHECK-NEXT: JmpTrue L5, r9
// CHECK-NEXT: ResumeGenerator r6, r12
// CHECK-NEXT: Mov r8, r12
// CHECK-NEXT: JmpTrue L4, r8
// CHECK-NEXT: TryGetById r8, r5, 1, "y"
// CHECK-NEXT: JmpTrue L5, r8
// CHECK-NEXT: L2:
// CHECK-NEXT: CompleteGenerator
// CHECK-NEXT: Ret r5
// CHECK-NEXT: Ret r4
// CHECK-NEXT: L4:
// CHECK-NEXT: CompleteGenerator
// CHECK-NEXT: Ret r7
// CHECK-NEXT: Ret r6
// CHECK-NEXT: L1:
// CHECK-NEXT: CompleteGenerator
// CHECK-NEXT: Ret r8
// CHECK-NEXT: Ret r7

function *args() {
yield arguments[0];
Expand Down
5 changes: 2 additions & 3 deletions test/BCGen/HBC/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ function foo (a) {

//CHECK-LABEL:Function<foo>(2 params, {{[0-9]+}} registers, 0 symbols):
//CHECK-NEXT:Offset in debug table: {{.*}}
//CHECK-NEXT: LoadConstUInt8 r2, 1
//CHECK-NEXT: LoadParam r0, 1
//CHECK-NEXT: ToNumber r0, r0
//CHECK-NEXT: SubN r1, r0, r2
//CHECK-NEXT: Dec r1, r0
//CHECK-NEXT: LoadConstZero r0
//CHECK-NEXT: LoadConstZero r3
//CHECK-NEXT: JmpFalse L1, r1
//CHECK-NEXT:L2:
//CHECK-NEXT: Add r0, r0, r1
//CHECK-NEXT: SubN r1, r1, r2
//CHECK-NEXT: Dec r1, r1
//CHECK-NEXT: Mov r3, r0
//CHECK-NEXT: JmpTrue L2, r1
//CHECK-NEXT:L1:
Expand Down
6 changes: 2 additions & 4 deletions test/BCGen/HBC/store_to_env.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ function foo() {
// CHECK: Function<bar>(1 params, 13 registers, 0 symbols):
// CHECK-NEXT: Offset in debug table: {{.*}}
// CHECK-NEXT: GetEnvironment r1, 0
// CHECK-NEXT: LoadFromEnvironment r2, r1, 0
// CHECK-NEXT: LoadConstUInt8 r0, 1
// CHECK-NEXT: Add r0, r2, r0
// CHECK-NEXT: LoadFromEnvironment r0, r1, 0
// CHECK-NEXT: Inc r0, r0
// CHECK-NEXT: StoreNPToEnvironment r1, 0, r0
// CHECK-NEXT: LoadConstFalse r0
// CHECK-NEXT: StoreNPToEnvironment r1, 1, r0
Expand All @@ -70,4 +69,3 @@ function foo() {
// CHECK-NEXT: Call2 r2, r3, r0, r2
// CHECK-NEXT: StoreNPToEnvironment r1, 5, r0
// CHECK-NEXT: Ret r0

6 changes: 1 addition & 5 deletions test/IRGen/do_while.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function do_while_continue_test(cond) {
//CHECK-NEXT: %BB6:
//CHECK-NEXT: %12 = LoadFrameInst [i]
//CHECK-NEXT: %13 = AsNumberInst %12
//CHECK-NEXT: %14 = BinaryOperatorInst '+', %13 : number, 1 : number
//CHECK-NEXT: %14 = UnaryOperatorInst '++', %13 : number
//CHECK-NEXT: %15 = StoreFrameInst %14, [i]
//CHECK-NEXT: %16 = BranchInst %BB5
//CHECK-NEXT: %BB7:
Expand Down Expand Up @@ -143,7 +143,3 @@ function for_while_do_mixed_test(cond) {
}
}
}




2 changes: 1 addition & 1 deletion test/IRGen/es6/for-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function forof_update(seq) {
//CHECK-NEXT: %23 = LoadFrameInst [ar]
//CHECK-NEXT: %24 = LoadFrameInst [i]
//CHECK-NEXT: %25 = AsNumberInst %24
//CHECK-NEXT: %26 = BinaryOperatorInst '+', %25 : number, 1 : number
//CHECK-NEXT: %26 = UnaryOperatorInst '++', %25 : number
//CHECK-NEXT: %27 = StoreFrameInst %26, [i]
//CHECK-NEXT: %28 = StorePropertyInst %13, %23, %25 : number
//CHECK-NEXT: %29 = BranchInst %BB6
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/es6/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function *loop(x) {
//CHECK-NEXT: %10 = LoadFrameInst [x]
//CHECK-NEXT: %11 = LoadFrameInst [i]
//CHECK-NEXT: %12 = AsNumberInst %11
//CHECK-NEXT: %13 = BinaryOperatorInst '+', %12 : number, 1 : number
//CHECK-NEXT: %13 = UnaryOperatorInst '++', %12 : number
//CHECK-NEXT: %14 = StoreFrameInst %13, [i]
//CHECK-NEXT: %15 = LoadPropertyInst %10, %12 : number
//CHECK-NEXT: %16 = AllocStackInst $?anon_1_isReturn
Expand Down
3 changes: 1 addition & 2 deletions test/IRGen/for_in_prop_access.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function modify_prop(obj) {
//CHECK-NEXT: %18 = LoadFrameInst [x]
//CHECK-NEXT: %19 = LoadPropertyInst %17, %18
//CHECK-NEXT: %20 = AsNumberInst %19
//CHECK-NEXT: %21 = BinaryOperatorInst '+', %20 : number, 1 : number
//CHECK-NEXT: %21 = UnaryOperatorInst '++', %20 : number
//CHECK-NEXT: %22 = StorePropertyInst %21, %17, %18
//CHECK-NEXT: %23 = LoadFrameInst [ret]
//CHECK-NEXT: %24 = LoadFrameInst [obj]
Expand Down Expand Up @@ -271,4 +271,3 @@ function expression_prop(obj) {
}
return ret;
}

5 changes: 2 additions & 3 deletions test/IRGen/for_loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ function naked_for_loop() {
//CHECK-NEXT: %BB3:
//CHECK-NEXT: %8 = LoadFrameInst [i]
//CHECK-NEXT: %9 = AsNumberInst %8
//CHECK-NEXT: %10 = BinaryOperatorInst '+', %9 : number, 1 : number
//CHECK-NEXT: %10 = UnaryOperatorInst '++', %9 : number
//CHECK-NEXT: %11 = StoreFrameInst %10, [i]
//CHECK-NEXT: %12 = BranchInst %BB6
//CHECK-NEXT: %BB7:
Expand All @@ -260,7 +260,7 @@ function naked_for_loop() {
//CHECK-NEXT: %BB8:
//CHECK-NEXT: %18 = LoadFrameInst [i]
//CHECK-NEXT: %19 = AsNumberInst %18
//CHECK-NEXT: %20 = BinaryOperatorInst '-', %19 : number, 1 : number
//CHECK-NEXT: %20 = UnaryOperatorInst '--', %19 : number
//CHECK-NEXT: %21 = StoreFrameInst %20, [i]
//CHECK-NEXT: %22 = BranchInst %BB11
//CHECK-NEXT: %BB12:
Expand All @@ -279,4 +279,3 @@ function test_init_update_exprs(param1) {
for (4 ; false ; --i) { }
for (param1 ; false ; 2) { }
}

22 changes: 10 additions & 12 deletions test/IRGen/try_catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//CHECK-NEXT: %3 = StoreFrameInst %2, [?anon_0_e]
//CHECK-NEXT: %4 = LoadFrameInst [?anon_0_e]
//CHECK-NEXT: %5 = AsNumberInst %4
//CHECK-NEXT: %6 = BinaryOperatorInst '+', %5 : number, 1 : number
//CHECK-NEXT: %6 = UnaryOperatorInst '++', %5 : number
//CHECK-NEXT: %7 = StoreFrameInst %6, [?anon_0_e]
//CHECK-NEXT: %8 = LoadFrameInst [i]
//CHECK-NEXT: %9 = BinaryOperatorInst '-', %8, 3 : number
Expand Down Expand Up @@ -50,13 +50,13 @@
//CHECK-NEXT: %BB6:
//CHECK-NEXT: %28 = LoadFrameInst [i]
//CHECK-NEXT: %29 = AsNumberInst %28
//CHECK-NEXT: %30 = BinaryOperatorInst '+', %29 : number, 1 : number
//CHECK-NEXT: %30 = UnaryOperatorInst '++', %29 : number
//CHECK-NEXT: %31 = StoreFrameInst %30, [i]
//CHECK-NEXT: %32 = BranchInst %BB8
//CHECK-NEXT: %BB9:
//CHECK-NEXT: %33 = LoadFrameInst [i]
//CHECK-NEXT: %34 = AsNumberInst %33
//CHECK-NEXT: %35 = BinaryOperatorInst '-', %34 : number, 1 : number
//CHECK-NEXT: %35 = UnaryOperatorInst '--', %34 : number
//CHECK-NEXT: %36 = StoreFrameInst %35, [i]
//CHECK-NEXT: %37 = BranchInst %BB10
//CHECK-NEXT: %BB10:
Expand Down Expand Up @@ -112,7 +112,7 @@ function simple_try_catch_test() {
//CHECK-NEXT:%BB5:
//CHECK-NEXT: %20 = LoadFrameInst [i]
//CHECK-NEXT: %21 = AsNumberInst %20
//CHECK-NEXT: %22 = BinaryOperatorInst '+', %21 : number, 1 : number
//CHECK-NEXT: %22 = UnaryOperatorInst '++', %21 : number
//CHECK-NEXT: %23 = StoreFrameInst %22, [i]
//CHECK-NEXT: %24 = BranchInst %BB8
//CHECK-NEXT:%BB8:
Expand Down Expand Up @@ -160,7 +160,7 @@ function simple_try_catch_finally_test() {
//CHECK-NEXT: %BB2:
//CHECK-NEXT: %12 = LoadFrameInst [i]
//CHECK-NEXT: %13 = AsNumberInst %12
//CHECK-NEXT: %14 = BinaryOperatorInst '+', %13 : number, 1 : number
//CHECK-NEXT: %14 = UnaryOperatorInst '++', %13 : number
//CHECK-NEXT: %15 = StoreFrameInst %14, [i]
//CHECK-NEXT: %16 = BranchInst %BB4
//CHECK-NEXT: %BB4:
Expand Down Expand Up @@ -214,7 +214,7 @@ function simple_try_finally_test() {
//CHECK-NEXT:%BB5:
//CHECK-NEXT: %20 = LoadFrameInst [i]
//CHECK-NEXT: %21 = AsNumberInst %20
//CHECK-NEXT: %22 = BinaryOperatorInst '+', %21 : number, 1 : number
//CHECK-NEXT: %22 = UnaryOperatorInst '++', %21 : number
//CHECK-NEXT: %23 = StoreFrameInst %22, [i]
//CHECK-NEXT: %24 = BranchInst %BB9
//CHECK-NEXT:%BB9:
Expand Down Expand Up @@ -307,7 +307,7 @@ function try_catch_finally_with_return_test() {
//CHECK-NEXT:%BB5:
//CHECK-NEXT: %20 = LoadFrameInst [i]
//CHECK-NEXT: %21 = AsNumberInst %20
//CHECK-NEXT: %22 = BinaryOperatorInst '+', %21 : number, 1 : number
//CHECK-NEXT: %22 = UnaryOperatorInst '++', %21 : number
//CHECK-NEXT: %23 = StoreFrameInst %22, [i]
//CHECK-NEXT: %24 = TryStartInst %BB8, %BB9
//CHECK-NEXT:%BB8:
Expand Down Expand Up @@ -481,7 +481,7 @@ function nested_try_test() {
//CHECK-NEXT:%BB5:
//CHECK-NEXT: %20 = LoadFrameInst [i]
//CHECK-NEXT: %21 = AsNumberInst %20
//CHECK-NEXT: %22 = BinaryOperatorInst '+', %21 : number, 1 : number
//CHECK-NEXT: %22 = UnaryOperatorInst '++', %21 : number
//CHECK-NEXT: %23 = StoreFrameInst %22, [i]
//CHECK-NEXT: %24 = BranchInst %BB10
//CHECK-NEXT:%BB10:
Expand Down Expand Up @@ -651,7 +651,7 @@ function nested_catch_test() {
//CHECK-NEXT:%BB7:
//CHECK-NEXT: %14 = LoadFrameInst [i]
//CHECK-NEXT: %15 = AsNumberInst %14
//CHECK-NEXT: %16 = BinaryOperatorInst '+', %15 : number, 1 : number
//CHECK-NEXT: %16 = UnaryOperatorInst '++', %15 : number
//CHECK-NEXT: %17 = StoreFrameInst %16, [i]
//CHECK-NEXT: %18 = BranchInst %BB6
//CHECK-NEXT:%BB3:
Expand All @@ -678,7 +678,7 @@ function nested_catch_test() {
//CHECK-NEXT:%BB10:
//CHECK-NEXT: %35 = LoadFrameInst [i]
//CHECK-NEXT: %36 = AsNumberInst %35
//CHECK-NEXT: %37 = BinaryOperatorInst '+', %36 : number, 1 : number
//CHECK-NEXT: %37 = UnaryOperatorInst '++', %36 : number
//CHECK-NEXT: %38 = StoreFrameInst %37, [i]
//CHECK-NEXT: %39 = BranchInst %BB15
//CHECK-NEXT:%BB15:
Expand Down Expand Up @@ -744,5 +744,3 @@ function finally_with_break_continue_test() {
}
i += 4;
}


Loading

0 comments on commit fdf3d86

Please sign in to comment.