This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

About the gcc optimizer.


Hello,
	I have a few suggestions for the gcc optimizer, and warning unit.

I have attached 3 files: switch_test(1/2/3).c The 3rd file is a c++
file, because it uses bool.

I noticed that for the 1st file, gcc generates good code, using a tabel
lookup for where to jump and stuff.

For the 2nd file however, it uses a series o compares and jumps, which
probably can be avoided, and the whole approach can be made similar to
the approach used for the 1st file.

The 3rd file however, on compiling does not give me a warning even with
-Wall about unreachable code in the default: case. I'm using switch on
bool, and have 3 cases, namely true, false, and default. The default
case will never be entered, and the compiler should warn me about that.

Tested using gcc3.4

-- 
        -Dhruv Matani.
http://www.geocities.com/dhruvbird/

The price of freedom is responsibility, but it's a bargain, because
freedom is priceless. ~ Hugh Downs
#include <stdio.h>

int main()
{
  int x = 0;
  int y, z;
  scanf("%d", &x);

  switch (x)
    {
    case 1:
      y = x + x;
      z = x + y;
      break;

    case 2:
      y = x + 2 * x;
      z = x * y;
      break;

    case 3:
      y = x + x / 2;
      z = x / y;
      break;

    case 4:
      y = x - (2 - x);
      z = x * y;
      break;

    case 5:
      y = -x + 2;
      z = y * x;
      break;

    case 6:
      y = -x - 2;
      z = y / x;
      break;

    default:
      y = 0;
      z = y;
    }
  return z;
}
#include <stdio.h>

int main()
{
  int x = 0;
  int y, z;
  scanf("%d", &x);

  switch (x)
    {
    case 1:
      y = x + x;
      z = x + y;
      break;

    case 2:
      y = x + 2 * x;
      z = x * y;
      break;

    case 3:
      y = x + x / 2;
      z = x / y;
      break;

    case 4:
      y = x - (2 - x);
      z = x * y;
      break;

    case 99:
      y = -x + 2;
      z = y * x;
      break;

    case 6:
      y = -x - 2;
      z = y / x;
      break;

    default:
      y = 0;
      z = y;
    }
  return z;
}
#include <stdio.h>

int main()
{
  int x = 0;
  scanf("%d", &x);

  bool b = x;

  switch (b)
    {
    case true:
      x = 23;
      break;

    case false:
      x = 45;
      break;

    default:
      x = 69;
    }
  return x;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]