[PATCH 06/13] expand linearize_conditional() into linearize_logical()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



linearize_logical() call linearize_conditional() but needs additional
tests there and generate code more complicated than needed.

Change this by expanding the call to linearize_conditional() and make
the obvious simplification concerning the shortcut expressions 0 & 1.
Also, removes the logical-specific parts in linearize_conditional(),
since there are now unneeded.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 linearize.c                 |  64 +++++++---
 validation/linear/logical.c | 238 +++++++++++++++++-------------------
 2 files changed, 158 insertions(+), 144 deletions(-)

diff --git a/linearize.c b/linearize.c
index 2b1b26582..3dfe45c0e 100644
--- a/linearize.c
+++ b/linearize.c
@@ -1615,8 +1615,7 @@ static pseudo_t linearize_short_conditional(struct entrypoint *ep, struct expres
 static pseudo_t linearize_conditional(struct entrypoint *ep, struct expression *expr,
 				      struct expression *cond,
 				      struct expression *expr_true,
-				      struct expression *expr_false,
-				      int logical)
+				      struct expression *expr_false)
 {
 	pseudo_t src1, src2;
 	pseudo_t phi1, phi2;
@@ -1632,19 +1631,11 @@ static pseudo_t linearize_conditional(struct entrypoint *ep, struct expression *
 
 	set_activeblock(ep, bb_true);
 	src1 = linearize_expression(ep, expr_true);
-	if (logical) {
-		src1 = add_convert_to_bool(ep, src1, expr_true->ctype);
-		src1 = cast_pseudo(ep, src1, &bool_ctype, expr->ctype);
-	}
 	phi1 = alloc_phi(ep->active, src1, expr->ctype);
 	add_goto(ep, merge); 
 
 	set_activeblock(ep, bb_false);
 	src2 = linearize_expression(ep, expr_false);
-	if (logical) {
-		src2 = add_convert_to_bool(ep, src2, expr_false->ctype);
-		src2 = cast_pseudo(ep, src2, &bool_ctype, expr->ctype);
-	}
 	phi2 = alloc_phi(ep->active, src2, expr->ctype);
 	set_activeblock(ep, merge);
 
@@ -1653,13 +1644,52 @@ static pseudo_t linearize_conditional(struct entrypoint *ep, struct expression *
 
 static pseudo_t linearize_logical(struct entrypoint *ep, struct expression *expr)
 {
-	struct expression *shortcut;
+	struct basic_block *merge;
+	pseudo_t phi1, phi2;
 
-	shortcut = alloc_const_expression(expr->pos, expr->op == SPECIAL_LOGICAL_OR);
-	shortcut->ctype = expr->ctype;
-	if (expr->op == SPECIAL_LOGICAL_OR)
-		return linearize_conditional(ep, expr, expr->left, shortcut, expr->right, 1);
-	return linearize_conditional(ep, expr, expr->left, expr->right, shortcut, 1);
+	if (!ep->active || !expr->left || !expr->right)
+		return VOID;
+
+	if (expr->op == SPECIAL_LOGICAL_OR) {
+		struct expression *expr_false = expr->right;
+		struct basic_block *bb_true = alloc_basic_block(ep, expr->pos);
+		struct basic_block *bb_false = alloc_basic_block(ep, expr_false->pos);
+		pseudo_t src1, src2;
+
+		merge = alloc_basic_block(ep, expr->pos);
+		linearize_cond_branch(ep, expr->left, bb_true, bb_false);
+
+		set_activeblock(ep, bb_true);
+		src1 = value_pseudo(1);
+		phi1 = alloc_phi(ep->active, src1, expr->ctype);
+		add_goto(ep, merge);
+
+		set_activeblock(ep, bb_false);
+		src2 = linearize_expression_to_bool(ep, expr_false);
+		src2 = cast_pseudo(ep, src2, &bool_ctype, expr->ctype);
+		phi2 = alloc_phi(ep->active, src2, expr->ctype);
+	} else {
+		struct expression *expr_true = expr->right;
+		struct basic_block *bb_true = alloc_basic_block(ep, expr_true->pos);
+		struct basic_block *bb_false = alloc_basic_block(ep, expr->pos);
+		pseudo_t src1, src2;
+
+		merge = alloc_basic_block(ep, expr->pos);
+		linearize_cond_branch(ep, expr->left, bb_true, bb_false);
+
+		set_activeblock(ep, bb_true);
+		src1 = linearize_expression_to_bool(ep, expr_true);
+		src1 = cast_pseudo(ep, src1, &bool_ctype, expr->ctype);
+		phi1 = alloc_phi(ep->active, src1, expr->ctype);
+		add_goto(ep, merge);
+
+		set_activeblock(ep, bb_false);
+		src2 = value_pseudo(0);
+		phi2 = alloc_phi(ep->active, src2, expr->ctype);
+	}
+
+	set_activeblock(ep, merge);
+	return add_join_conditional(ep, expr, phi1, phi2);
 }
 
 static pseudo_t linearize_compare(struct entrypoint *ep, struct expression *expr)
@@ -1838,7 +1868,7 @@ static pseudo_t linearize_expression(struct entrypoint *ep, struct expression *e
 			return linearize_short_conditional(ep, expr, expr->conditional, expr->cond_false);
 
 		return  linearize_conditional(ep, expr, expr->conditional,
-					      expr->cond_true, expr->cond_false, 0);
+					      expr->cond_true, expr->cond_false);
 
 	case EXPR_COMMA:
 		linearize_expression(ep, expr->left);
diff --git a/validation/linear/logical.c b/validation/linear/logical.c
index 972f87098..ed0dd70e9 100644
--- a/validation/linear/logical.c
+++ b/validation/linear/logical.c
@@ -31,29 +31,27 @@ os:
 	cbr         %r2, .L2, .L3
 
 .L2:
-	setne.1     %r3 <- $1, $0
-	zext.32     %r4 <- (1) %r3
-	phisrc.32   %phi1 <- %r4
+	phisrc.32   %phi1 <- $1
 	br          .L4
 
 .L3:
-	load.64     %r5 <- 0[b]
-	load.32     %r6 <- 0[%r5]
-	lsr.32      %r7 <- %r6, $1
-	trunc.2     %r8 <- (32) %r7
-	setne.1     %r9 <- %r8, $0
-	zext.32     %r10 <- (1) %r9
-	phisrc.32   %phi2 <- %r10
+	load.64     %r3 <- 0[b]
+	load.32     %r4 <- 0[%r3]
+	lsr.32      %r5 <- %r4, $1
+	trunc.2     %r6 <- (32) %r5
+	setne.1     %r7 <- %r6, $0
+	zext.32     %r8 <- (1) %r7
+	phisrc.32   %phi2 <- %r8
 	br          .L4
 
 .L4:
-	phi.32      %r11 <- %phi1, %phi2
-	phisrc.32   %phi3(return) <- %r11
+	phi.32      %r9 <- %phi1, %phi2
+	phisrc.32   %phi3(return) <- %r9
 	br          .L1
 
 .L1:
-	phi.32      %r12 <- %phi3(return)
-	ret.32      %r12
+	phi.32      %r10 <- %phi3(return)
+	ret.32      %r10
 
 
 ou:
@@ -61,34 +59,32 @@ ou:
 	<entry-point>
 	store.32    %arg1 -> 0[i]
 	store.64    %arg2 -> 0[b]
-	load.32     %r13 <- 0[i]
-	setne.1     %r14 <- %r13, $0
-	cbr         %r14, .L7, .L8
+	load.32     %r11 <- 0[i]
+	setne.1     %r12 <- %r11, $0
+	cbr         %r12, .L7, .L8
 
 .L7:
-	setne.1     %r15 <- $1, $0
-	zext.32     %r16 <- (1) %r15
-	phisrc.32   %phi5 <- %r16
+	phisrc.32   %phi5 <- $1
 	br          .L9
 
 .L8:
-	load.64     %r17 <- 0[b]
-	load.32     %r18 <- 0[%r17]
-	lsr.32      %r19 <- %r18, $3
-	trunc.3     %r20 <- (32) %r19
-	setne.1     %r21 <- %r20, $0
-	zext.32     %r22 <- (1) %r21
-	phisrc.32   %phi6 <- %r22
+	load.64     %r13 <- 0[b]
+	load.32     %r14 <- 0[%r13]
+	lsr.32      %r15 <- %r14, $3
+	trunc.3     %r16 <- (32) %r15
+	setne.1     %r17 <- %r16, $0
+	zext.32     %r18 <- (1) %r17
+	phisrc.32   %phi6 <- %r18
 	br          .L9
 
 .L9:
-	phi.32      %r23 <- %phi5, %phi6
-	phisrc.32   %phi7(return) <- %r23
+	phi.32      %r19 <- %phi5, %phi6
+	phisrc.32   %phi7(return) <- %r19
 	br          .L6
 
 .L6:
-	phi.32      %r24 <- %phi7(return)
-	ret.32      %r24
+	phi.32      %r20 <- %phi7(return)
+	ret.32      %r20
 
 
 ol:
@@ -96,32 +92,30 @@ ol:
 	<entry-point>
 	store.32    %arg1 -> 0[i]
 	store.64    %arg2 -> 0[b]
-	load.32     %r25 <- 0[i]
-	setne.1     %r26 <- %r25, $0
-	cbr         %r26, .L12, .L13
+	load.32     %r21 <- 0[i]
+	setne.1     %r22 <- %r21, $0
+	cbr         %r22, .L12, .L13
 
 .L12:
-	setne.1     %r27 <- $1, $0
-	zext.32     %r28 <- (1) %r27
-	phisrc.32   %phi9 <- %r28
+	phisrc.32   %phi9 <- $1
 	br          .L14
 
 .L13:
-	load.64     %r29 <- 0[b]
-	load.64     %r30 <- 8[%r29]
-	setne.1     %r31 <- %r30, $0
-	zext.32     %r32 <- (1) %r31
-	phisrc.32   %phi10 <- %r32
+	load.64     %r23 <- 0[b]
+	load.64     %r24 <- 8[%r23]
+	setne.1     %r25 <- %r24, $0
+	zext.32     %r26 <- (1) %r25
+	phisrc.32   %phi10 <- %r26
 	br          .L14
 
 .L14:
-	phi.32      %r33 <- %phi9, %phi10
-	phisrc.32   %phi11(return) <- %r33
+	phi.32      %r27 <- %phi9, %phi10
+	phisrc.32   %phi11(return) <- %r27
 	br          .L11
 
 .L11:
-	phi.32      %r34 <- %phi11(return)
-	ret.32      %r34
+	phi.32      %r28 <- %phi11(return)
+	ret.32      %r28
 
 
 od:
@@ -129,33 +123,31 @@ od:
 	<entry-point>
 	store.32    %arg1 -> 0[i]
 	store.64    %arg2 -> 0[b]
-	load.32     %r35 <- 0[i]
-	setne.1     %r36 <- %r35, $0
-	cbr         %r36, .L17, .L18
+	load.32     %r29 <- 0[i]
+	setne.1     %r30 <- %r29, $0
+	cbr         %r30, .L17, .L18
 
 .L17:
-	setne.1     %r37 <- $1, $0
-	zext.32     %r38 <- (1) %r37
-	phisrc.32   %phi13 <- %r38
+	phisrc.32   %phi13 <- $1
 	br          .L19
 
 .L18:
-	load.64     %r39 <- 0[b]
-	load.64     %r40 <- 16[%r39]
-	setfval.64  %r41 <- 0.000000e+00
-	fcmpune.1   %r42 <- %r40, %r41
-	zext.32     %r43 <- (1) %r42
-	phisrc.32   %phi14 <- %r43
+	load.64     %r31 <- 0[b]
+	load.64     %r32 <- 16[%r31]
+	setfval.64  %r33 <- 0.000000e+00
+	fcmpune.1   %r34 <- %r32, %r33
+	zext.32     %r35 <- (1) %r34
+	phisrc.32   %phi14 <- %r35
 	br          .L19
 
 .L19:
-	phi.32      %r44 <- %phi13, %phi14
-	phisrc.32   %phi15(return) <- %r44
+	phi.32      %r36 <- %phi13, %phi14
+	phisrc.32   %phi15(return) <- %r36
 	br          .L16
 
 .L16:
-	phi.32      %r45 <- %phi15(return)
-	ret.32      %r45
+	phi.32      %r37 <- %phi15(return)
+	ret.32      %r37
 
 
 as:
@@ -163,34 +155,32 @@ as:
 	<entry-point>
 	store.32    %arg1 -> 0[i]
 	store.64    %arg2 -> 0[b]
-	load.32     %r46 <- 0[i]
-	setne.1     %r47 <- %r46, $0
-	cbr         %r47, .L22, .L23
+	load.32     %r38 <- 0[i]
+	setne.1     %r39 <- %r38, $0
+	cbr         %r39, .L22, .L23
 
 .L22:
-	load.64     %r48 <- 0[b]
-	load.32     %r49 <- 0[%r48]
-	lsr.32      %r50 <- %r49, $1
-	trunc.2     %r51 <- (32) %r50
-	setne.1     %r52 <- %r51, $0
-	zext.32     %r53 <- (1) %r52
-	phisrc.32   %phi17 <- %r53
+	load.64     %r40 <- 0[b]
+	load.32     %r41 <- 0[%r40]
+	lsr.32      %r42 <- %r41, $1
+	trunc.2     %r43 <- (32) %r42
+	setne.1     %r44 <- %r43, $0
+	zext.32     %r45 <- (1) %r44
+	phisrc.32   %phi17 <- %r45
 	br          .L24
 
 .L23:
-	setne.1     %r54 <- $0, $0
-	zext.32     %r55 <- (1) %r54
-	phisrc.32   %phi18 <- %r55
+	phisrc.32   %phi18 <- $0
 	br          .L24
 
 .L24:
-	phi.32      %r56 <- %phi17, %phi18
-	phisrc.32   %phi19(return) <- %r56
+	phi.32      %r46 <- %phi17, %phi18
+	phisrc.32   %phi19(return) <- %r46
 	br          .L21
 
 .L21:
-	phi.32      %r57 <- %phi19(return)
-	ret.32      %r57
+	phi.32      %r47 <- %phi19(return)
+	ret.32      %r47
 
 
 au:
@@ -198,34 +188,32 @@ au:
 	<entry-point>
 	store.32    %arg1 -> 0[i]
 	store.64    %arg2 -> 0[b]
-	load.32     %r58 <- 0[i]
-	setne.1     %r59 <- %r58, $0
-	cbr         %r59, .L27, .L28
+	load.32     %r48 <- 0[i]
+	setne.1     %r49 <- %r48, $0
+	cbr         %r49, .L27, .L28
 
 .L27:
-	load.64     %r60 <- 0[b]
-	load.32     %r61 <- 0[%r60]
-	lsr.32      %r62 <- %r61, $3
-	trunc.3     %r63 <- (32) %r62
-	setne.1     %r64 <- %r63, $0
-	zext.32     %r65 <- (1) %r64
-	phisrc.32   %phi21 <- %r65
+	load.64     %r50 <- 0[b]
+	load.32     %r51 <- 0[%r50]
+	lsr.32      %r52 <- %r51, $3
+	trunc.3     %r53 <- (32) %r52
+	setne.1     %r54 <- %r53, $0
+	zext.32     %r55 <- (1) %r54
+	phisrc.32   %phi21 <- %r55
 	br          .L29
 
 .L28:
-	setne.1     %r66 <- $0, $0
-	zext.32     %r67 <- (1) %r66
-	phisrc.32   %phi22 <- %r67
+	phisrc.32   %phi22 <- $0
 	br          .L29
 
 .L29:
-	phi.32      %r68 <- %phi21, %phi22
-	phisrc.32   %phi23(return) <- %r68
+	phi.32      %r56 <- %phi21, %phi22
+	phisrc.32   %phi23(return) <- %r56
 	br          .L26
 
 .L26:
-	phi.32      %r69 <- %phi23(return)
-	ret.32      %r69
+	phi.32      %r57 <- %phi23(return)
+	ret.32      %r57
 
 
 al:
@@ -233,32 +221,30 @@ al:
 	<entry-point>
 	store.32    %arg1 -> 0[i]
 	store.64    %arg2 -> 0[b]
-	load.32     %r70 <- 0[i]
-	setne.1     %r71 <- %r70, $0
-	cbr         %r71, .L32, .L33
+	load.32     %r58 <- 0[i]
+	setne.1     %r59 <- %r58, $0
+	cbr         %r59, .L32, .L33
 
 .L32:
-	load.64     %r72 <- 0[b]
-	load.64     %r73 <- 8[%r72]
-	setne.1     %r74 <- %r73, $0
-	zext.32     %r75 <- (1) %r74
-	phisrc.32   %phi25 <- %r75
+	load.64     %r60 <- 0[b]
+	load.64     %r61 <- 8[%r60]
+	setne.1     %r62 <- %r61, $0
+	zext.32     %r63 <- (1) %r62
+	phisrc.32   %phi25 <- %r63
 	br          .L34
 
 .L33:
-	setne.1     %r76 <- $0, $0
-	zext.32     %r77 <- (1) %r76
-	phisrc.32   %phi26 <- %r77
+	phisrc.32   %phi26 <- $0
 	br          .L34
 
 .L34:
-	phi.32      %r78 <- %phi25, %phi26
-	phisrc.32   %phi27(return) <- %r78
+	phi.32      %r64 <- %phi25, %phi26
+	phisrc.32   %phi27(return) <- %r64
 	br          .L31
 
 .L31:
-	phi.32      %r79 <- %phi27(return)
-	ret.32      %r79
+	phi.32      %r65 <- %phi27(return)
+	ret.32      %r65
 
 
 ad:
@@ -266,33 +252,31 @@ ad:
 	<entry-point>
 	store.32    %arg1 -> 0[i]
 	store.64    %arg2 -> 0[b]
-	load.32     %r80 <- 0[i]
-	setne.1     %r81 <- %r80, $0
-	cbr         %r81, .L37, .L38
+	load.32     %r66 <- 0[i]
+	setne.1     %r67 <- %r66, $0
+	cbr         %r67, .L37, .L38
 
 .L37:
-	load.64     %r82 <- 0[b]
-	load.64     %r83 <- 16[%r82]
-	setfval.64  %r84 <- 0.000000e+00
-	fcmpune.1   %r85 <- %r83, %r84
-	zext.32     %r86 <- (1) %r85
-	phisrc.32   %phi29 <- %r86
+	load.64     %r68 <- 0[b]
+	load.64     %r69 <- 16[%r68]
+	setfval.64  %r70 <- 0.000000e+00
+	fcmpune.1   %r71 <- %r69, %r70
+	zext.32     %r72 <- (1) %r71
+	phisrc.32   %phi29 <- %r72
 	br          .L39
 
 .L38:
-	setne.1     %r87 <- $0, $0
-	zext.32     %r88 <- (1) %r87
-	phisrc.32   %phi30 <- %r88
+	phisrc.32   %phi30 <- $0
 	br          .L39
 
 .L39:
-	phi.32      %r89 <- %phi29, %phi30
-	phisrc.32   %phi31(return) <- %r89
+	phi.32      %r73 <- %phi29, %phi30
+	phisrc.32   %phi31(return) <- %r73
 	br          .L36
 
 .L36:
-	phi.32      %r90 <- %phi31(return)
-	ret.32      %r90
+	phi.32      %r74 <- %phi31(return)
+	ret.32      %r74
 
 
  * check-output-end
-- 
2.18.0

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux