title: assert()s, optimisation and an assume() directive in D
body: <p>Say I have an assert() something like
      <code>assert( x &lt; limit );</code>
  I took a look at the behaviour of the optimiser in GDC in release and debug builds with the following snippet of code:</p>
  
  <pre><code>uint cxx1( uint x )
      {
      assert( x &lt; 10 );
      return x % 10;
      }
  
  uint cxx1a( uint x )
  in  { assert( x &lt; 10 ); }
  body
      {
      return x % 10;
      }
  
  uint cxx2( uint x )
      {
      if ( !( x &lt; 10 ))
         assert(0);
      return x % 10;
      }
  </code></pre>
  
  <p>Now when I build in debug mode, the asserts have the very pleasing effect of triggering huge optimisation. GDC gets rid of the horrid code to do the modulo operation entirely, because of its knowledge about the possible range of x due to the assert’s if-condition. But in release mode, the if-condition is discarded, so all of a sudden, the horrid code comes back, and there is no longer any optimisation in cxx1() nor even in cxx1a(). This is very ironic, that release mode generates far worse code than debug code. Of course, no-one wants executable code belonging to the if-tests to be present in release code as we must lose all that overhead.</p>
  
  <p>Now ideally, I would want to express the condition in the sense of communicating information to the compiler, regardless of release / debug builds, about conditions that may always be assumed to be true, and so such assumptions can guide optimisation in very powerful ways.</p>
  
  <p>I believe some C++ compilers have something called __assume() or some such, but memory fails me here. GCC has a __builtin_unreachable() special directive which might be useable to build an assume() feature. Basically if I could build my own assume() directive it would have the effect of asserting certain truths about known values or known ranges and exposing / publishing these to optimisation passes regardless of release / debug mode but without generating any actual code at all for the assume() condition in a release build, while in debug mode it would be exactly the same as assert().</p>
  
  <p>I tried an experiment which you see in cxx2 which triggers optimisation always, so good job there, but it generates what is morally debug code for the assume()'s if-condition even in release mode with a test and a conditional jump to an undefined instruction in order to halt the process.</p>
  
  <p>Does anyone have any ideas about whether this is solvable? Or do you think this is a useful D compiler fantasy wish-list item?</p>