Skip to contentSkip to conversation

Staging: sm750fb: Remove unnecessary local variables

PATCH In review Madhumitha Prabakaran 3 participants 6 messages Mar 12, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Staging: sm750fb: Remove unnecessary local variables

Remove unnecessary local variables in function get_mxclk_freq. Issue found by Coccinelle using ret.cocci.

---
 drivers/staging/sm750fb/ddk750_chip.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/staging/sm750fb/ddk750_chip.c +7 -10
index 90f5480304f4..d0462f21fe36 100644
@@ -33,19 +33,16 @@ void sm750_set_chip_type(unsigned short dev_id, u8 rev_id)
 
 static unsigned int get_mxclk_freq(void)
 {
-	unsigned int pll_reg;
-	unsigned int M, N, OD, POD;
-
 	if (sm750_get_chip_type() == SM750LE)
 		return MHz(130);
 
-	pll_reg = peek32(MXCLK_PLL_CTRL);
-	M = (pll_reg & PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT;
-	N = (pll_reg & PLL_CTRL_N_MASK) >> PLL_CTRL_N_SHIFT;
-	OD = (pll_reg & PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT;
-	POD = (pll_reg & PLL_CTRL_POD_MASK) >> PLL_CTRL_POD_SHIFT;
-
-	return DEFAULT_INPUT_CLOCK * M / N / (1 << OD) / (1 << POD);
+	return DEFAULT_INPUT_CLOCK * (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT /
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_N_MASK) >>
+	       PLL_CTRL_N_SHIFT / (1 << (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT) / (1 <<
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_POD_MASK) >>
+	       PLL_CTRL_POD_SHIFT);
 }
 
 /*
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 26 lines · Madhumitha Prabakaran

Remove unnecessary local variables in function get_mxclk_freq. Issue found by Coccinelle using ret.cocci.

---
 drivers/staging/sm750fb/ddk750_chip.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/staging/sm750fb/ddk750_chip.c +0 -7
index 90f5480304f4..d0462f21fe36 100644
@@ -33,19 +33,16 @@ void sm750_set_chip_type(unsigned short dev_id, u8 rev_id)
 
 static unsigned int get_mxclk_freq(void)
 {
-	unsigned int pll_reg;
-	unsigned int M, N, OD, POD;
-
 	if (sm750_get_chip_type() == SM750LE)
 		return MHz(130);
 
-	pll_reg = peek32(MXCLK_PLL_CTRL);
-	M = (pll_reg & PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT;
-	N = (pll_reg & PLL_CTRL_N_MASK) >> PLL_CTRL_N_SHIFT;
-	OD = (pll_reg & PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT;

drivers/staging/sm750fb/ddk750_chip.c void sm750_set_chip_type(unsigned short dev_id, u8 rev_id) · line 45

Quoted 10 lines
drivers/staging/sm750fb/ddk750_chip.c +7 -3
-	POD = (pll_reg & PLL_CTRL_POD_MASK) >> PLL_CTRL_POD_SHIFT;
-
-	return DEFAULT_INPUT_CLOCK * M / N / (1 << OD) / (1 << POD);
+	return DEFAULT_INPUT_CLOCK * (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT /
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_N_MASK) >>
+	       PLL_CTRL_N_SHIFT / (1 << (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT) / (1 <<
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_POD_MASK) >>
+	       PLL_CTRL_POD_SHIFT);

Oh wow, that's almost impossible to now read.

Sometimes you want intermediate variables for the programmer, not for the compiler. The end result is the same, but please tell me that you could figure out what that one single return line actually does :(

Remember, we write C code for people to understand first, and the CPU to understand second as we have to maintain it for the future.

thanks,

greg k-h

maintainer
#2 lore raw
Quoted 40 lines · Greg KH
Quoted 26 lines · Madhumitha Prabakaran

Remove unnecessary local variables in function get_mxclk_freq. Issue found by Coccinelle using ret.cocci.

---
 drivers/staging/sm750fb/ddk750_chip.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/staging/sm750fb/ddk750_chip.c +0 -7
index 90f5480304f4..d0462f21fe36 100644
@@ -33,19 +33,16 @@ void sm750_set_chip_type(unsigned short dev_id, u8 rev_id)
 
 static unsigned int get_mxclk_freq(void)
 {
-	unsigned int pll_reg;
-	unsigned int M, N, OD, POD;
-
 	if (sm750_get_chip_type() == SM750LE)
 		return MHz(130);
 
-	pll_reg = peek32(MXCLK_PLL_CTRL);
-	M = (pll_reg & PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT;
-	N = (pll_reg & PLL_CTRL_N_MASK) >> PLL_CTRL_N_SHIFT;
-	OD = (pll_reg & PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT;

drivers/staging/sm750fb/ddk750_chip.c void sm750_set_chip_type(unsigned short dev_id, u8 rev_id) · line 45

Quoted 10 lines
drivers/staging/sm750fb/ddk750_chip.c +7 -3
-	POD = (pll_reg & PLL_CTRL_POD_MASK) >> PLL_CTRL_POD_SHIFT;
-
-	return DEFAULT_INPUT_CLOCK * M / N / (1 << OD) / (1 << POD);
+	return DEFAULT_INPUT_CLOCK * (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT /
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_N_MASK) >>
+	       PLL_CTRL_N_SHIFT / (1 << (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT) / (1 <<
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_POD_MASK) >>
+	       PLL_CTRL_POD_SHIFT);

Oh wow, that's almost impossible to now read.

Quoted 6 lines

Sometimes you want intermediate variables for the programmer, not for the compiler. The end result is the same, but please tell me that you could figure out what that one single return line actually does :(

Remember, we write C code for people to understand first, and the CPU to understand second as we have to maintain it for the future.

Also, this change might be wrong, and at the least, will be slower as you now call peek() a lot more times. Is that a function that has no side-affects on the hardware? At the least it is doing a device access, so you only want to do that once if possible.

Remember, there is real hardware behind the driver, that's why the driver is written in the first place :)

thanks,

greg k-h

#3 lore raw
Quoted 53 lines · Greg KH
Quoted 40 lines · Greg KH
Quoted 26 lines · Madhumitha Prabakaran

Remove unnecessary local variables in function get_mxclk_freq. Issue found by Coccinelle using ret.cocci.

---
 drivers/staging/sm750fb/ddk750_chip.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/staging/sm750fb/ddk750_chip.c +0 -7
index 90f5480304f4..d0462f21fe36 100644
@@ -33,19 +33,16 @@ void sm750_set_chip_type(unsigned short dev_id, u8 rev_id)
 
 static unsigned int get_mxclk_freq(void)
 {
-	unsigned int pll_reg;
-	unsigned int M, N, OD, POD;
-
 	if (sm750_get_chip_type() == SM750LE)
 		return MHz(130);
 
-	pll_reg = peek32(MXCLK_PLL_CTRL);
-	M = (pll_reg & PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT;
-	N = (pll_reg & PLL_CTRL_N_MASK) >> PLL_CTRL_N_SHIFT;
-	OD = (pll_reg & PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT;

drivers/staging/sm750fb/ddk750_chip.c void sm750_set_chip_type(unsigned short dev_id, u8 rev_id) · line 45

Quoted 10 lines
drivers/staging/sm750fb/ddk750_chip.c +7 -3
-	POD = (pll_reg & PLL_CTRL_POD_MASK) >> PLL_CTRL_POD_SHIFT;
-
-	return DEFAULT_INPUT_CLOCK * M / N / (1 << OD) / (1 << POD);
+	return DEFAULT_INPUT_CLOCK * (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT /
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_N_MASK) >>
+	       PLL_CTRL_N_SHIFT / (1 << (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT) / (1 <<
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_POD_MASK) >>
+	       PLL_CTRL_POD_SHIFT);

Oh wow, that's almost impossible to now read.

Quoted 6 lines

Sometimes you want intermediate variables for the programmer, not for the compiler. The end result is the same, but please tell me that you could figure out what that one single return line actually does :(

Remember, we write C code for people to understand first, and the CPU to understand second as we have to maintain it for the future.

Also, this change might be wrong, and at the least, will be slower as you now call peek() a lot more times. Is that a function that has no side-affects on the hardware? At the least it is doing a device access, so you only want to do that once if possible.

Quoted 6 lines

Remember, there is real hardware behind the driver, that's why the driver is written in the first place :)

thanks,

greg k-h

Even I thought the same, when I was editing it but I was unsure about Coccinelle.

Thanks,
Madhumitha

#4 lore raw
In reply to Madhumitha Prabakaran
Quoted 26 lines · Madhumitha Prabakaran

Remove unnecessary local variables in function get_mxclk_freq. Issue found by Coccinelle using ret.cocci.

---
 drivers/staging/sm750fb/ddk750_chip.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/staging/sm750fb/ddk750_chip.c +0 -7
index 90f5480304f4..d0462f21fe36 100644
@@ -33,19 +33,16 @@ void sm750_set_chip_type(unsigned short dev_id, u8 rev_id)
 
 static unsigned int get_mxclk_freq(void)
 {
-	unsigned int pll_reg;
-	unsigned int M, N, OD, POD;
-
 	if (sm750_get_chip_type() == SM750LE)
 		return MHz(130);
 
-	pll_reg = peek32(MXCLK_PLL_CTRL);
-	M = (pll_reg & PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT;
-	N = (pll_reg & PLL_CTRL_N_MASK) >> PLL_CTRL_N_SHIFT;
-	OD = (pll_reg & PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT;

drivers/staging/sm750fb/ddk750_chip.c void sm750_set_chip_type(unsigned short dev_id, u8 rev_id) · line 45

Quoted 10 lines
drivers/staging/sm750fb/ddk750_chip.c +7 -3
-	POD = (pll_reg & PLL_CTRL_POD_MASK) >> PLL_CTRL_POD_SHIFT;
-
-	return DEFAULT_INPUT_CLOCK * M / N / (1 << OD) / (1 << POD);
+	return DEFAULT_INPUT_CLOCK * (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT /
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_N_MASK) >>
+	       PLL_CTRL_N_SHIFT / (1 << (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT) / (1 <<
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_POD_MASK) >>
+	       PLL_CTRL_POD_SHIFT);

I'm not sure what you are doing here. The proposed semantic patch doesn't do this. It only matches cases where there is an assignment to a variable and then a return of exactly that variable.

I have the impression that you are looking at the files that are handled by Coccinelle, then searching in those files for something that looks relevant, and then making the transformation by hand. You shouldn't do that. Coccinelle will find the proper places and make the transformation for you. You will have an appropriate patchon standard output. You can then apply it to your code, check that it compiles, check that the format is OK and checkpatch is happy, etc.

julia

Quoted 13 lines
drivers/staging/sm750fb/ddk750_chip.c +0 -1
 }
 
 /*
--

2.17.1

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20190312215737.29200-1-madhumithabiw%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

#5 lore raw
Quoted 53 lines · Julia Lawall
Quoted 26 lines · Madhumitha Prabakaran

Remove unnecessary local variables in function get_mxclk_freq. Issue found by Coccinelle using ret.cocci.

---
 drivers/staging/sm750fb/ddk750_chip.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/staging/sm750fb/ddk750_chip.c +0 -7
index 90f5480304f4..d0462f21fe36 100644
@@ -33,19 +33,16 @@ void sm750_set_chip_type(unsigned short dev_id, u8 rev_id)
 
 static unsigned int get_mxclk_freq(void)
 {
-	unsigned int pll_reg;
-	unsigned int M, N, OD, POD;
-
 	if (sm750_get_chip_type() == SM750LE)
 		return MHz(130);
 
-	pll_reg = peek32(MXCLK_PLL_CTRL);
-	M = (pll_reg & PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT;
-	N = (pll_reg & PLL_CTRL_N_MASK) >> PLL_CTRL_N_SHIFT;
-	OD = (pll_reg & PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT;

drivers/staging/sm750fb/ddk750_chip.c void sm750_set_chip_type(unsigned short dev_id, u8 rev_id) · line 45

Quoted 10 lines
drivers/staging/sm750fb/ddk750_chip.c +7 -3
-	POD = (pll_reg & PLL_CTRL_POD_MASK) >> PLL_CTRL_POD_SHIFT;
-
-	return DEFAULT_INPUT_CLOCK * M / N / (1 << OD) / (1 << POD);
+	return DEFAULT_INPUT_CLOCK * (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_M_MASK) >> PLL_CTRL_M_SHIFT /
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_N_MASK) >>
+	       PLL_CTRL_N_SHIFT / (1 << (peek32(MXCLK_PLL_CTRL) &
+	       PLL_CTRL_OD_MASK) >> PLL_CTRL_OD_SHIFT) / (1 <<
+	       (peek32(MXCLK_PLL_CTRL) & PLL_CTRL_POD_MASK) >>
+	       PLL_CTRL_POD_SHIFT);

I'm not sure what you are doing here. The proposed semantic patch doesn't do this. It only matches cases where there is an assignment to a variable and then a return of exactly that variable.

I have the impression that you are looking at the files that are handled by Coccinelle, then searching in those files for something that looks relevant, and then making the transformation by hand. You shouldn't do that. Coccinelle will find the proper places and make the transformation for you. You will have an appropriate patchon standard output. You can then apply it to your code, check that it compiles, check that the format is OK and checkpatch is happy, etc.

Quoted 17 lines

julia

Quoted 13 lines
drivers/staging/sm750fb/ddk750_chip.c +0 -1
 }
 
 /*
--

2.17.1

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20190312215737.29200-1-madhumithabiw%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Yeah, you are right. I have a misunderstanding about working of Coccinelle. I'm trying to understand it. I will drop this patch. Thanks,

Madhumitha