Variables optimized out by GCC...

All design tool related questions: compiler, assembler, linker. Embedded programming questions: assembler, C code.

Moderator: nferre

clarkleach
Posts: 9
Joined: Fri Feb 07, 2014 7:16 pm

Variables optimized out by GCC...

Fri Oct 03, 2014 9:41 pm

I have the following code in my 'main' function:

Code: Select all

	uint8_t a, b, err = 0;
	io_init_control();
	for(a = 0; a < 255; a++){
		write_mcp23017(OLATA, a);
		b = read_mcp23017(GPIOB);
		if(b != a){
			++err;
		}
	}
And the following functions in another file:

Code: Select all

uint8_t read_mcp23017(uint8_t reg)
{
	uint8_t result = 0;

	if(reg < 23){
		i2c_slave_select(MCP23X17_BASE_ADDR);
		buf[0] = reg;
		i2c_write_no_stop(buf, 1);
		i2c_read(buf,1);
		if(i2c_status_reg == 0){
			img[reg] = buf[0];
		}
		result = buf[0];
	}
	return result;
}

void write_mcp23017(uint8_t reg, uint8_t data)
{
	if (reg < 23){
		i2c_slave_select(MCP23X17_BASE_ADDR);
		buf[0] = reg;
		buf[1] = data;
		i2c_write(buf, 2);
		if(i2c_status_reg == 0){
			img[reg] = data;
		}
	}
}
Is there an explanation why 'a' and 'b' are optimized out???  FWIW, in an earlier iteration of this mess, in read_mcp23017() I didn't use the intermediate 'result' and simply returned either buf[0] or (0).  This caused 'err' to be optimized out, but not 'a'.  My brain hurts.
Attachments
gcc_optimize_my_program_into_oblivion_please.png
gcc_optimize_my_program_into_oblivion_please.png (6.43 KiB) Viewed 2859 times
nutsnbolts
Posts: 31
Joined: Mon Aug 25, 2014 11:20 pm

Re: Variables optimized out by GCC...

Mon Oct 06, 2014 12:43 pm

.... or declare the variables as 'volatile' 

Regards,
Mike.

Return to “Development Tools”

Who is online

Users browsing this forum: No registered users and 1 guest