2015年11月24日 星期二

sizeof trap

see more detail in jesrv blog

http://blog.linux.org.tw/~jserv/archives/001876.html


  1. A class with an empty sequence of members and base class objects is an empty class. Complete objects and member subobjects of an empty class type shall have nonzero size.

#include <stdio.h>
int main()                                                                                        
{
    printf("%d, %d\n", sizeof('J'), sizeof(char));
    return 0;
}


gcc output is
$ gcc -o sizeof sizeof.c
$ ./sizeof 4, 1
but g++ output is

$ g++ -o sizeof sizeof.c 
$ ./sizeof 1, 1
because 'j' default is integer in c, is char in c++

good example for gdb reverse execution

https://www.youtube.com/watch?v=PorfLSr3DDI&t=9m5s


https://sourceware.org/gdb/onlinedocs/gdb/Process-Record-and-Replay.html
record

https://sourceware.org/gdb/onlinedocs/gdb/Reverse-Execution.html#Reverse-Execution
reverse-continue
reverse-stepi

2015年11月13日 星期五

被討厭的勇氣 自我啟發之父阿德勒的教導


  1. 重點在於並不是因為發生了這些事就一定有什麼樣的結果, 我們是藉著"賦予過去的經驗什麼意義"來決定自己的一生. 人生不是別人給的,是我們自己選擇的. 決定要怎麼生活的, 是我們自己
  2. 問題不是在"經歷什麼事", 而是"如何解釋它", "如何運用它"
  3. 無論之前你的人生發生過什麼事, 那對你將來要怎麼過日子一點影響也沒有. 決定你人生的, 是活在當下的自己
  4. 我們無法改變客觀的事實, 卻可以隨意更改主觀的解釋
  5. 憤怒不過是一種為了達到目的而採取的手段,只是個工具而已
  6. 老是想要尋求別人的認同,在意他人的評價,到最後你過的就是別人的人生
  7. 完全只在乎"別人是如何看我"的這種生活方式,其實正是以自我為中心,只關心"我"的生活型態  ==>  你都沒想到 因為你只想到你自己

Strength and Power of Generalized Algorithm in Compiler Technologies

cite: 
(need to login)
    - Post order Traversal
   - Reverse Post Order Traversal
    - DFS Search and BFS Search.
    - Topological Sorting Algorithms.
 Following mention the strength and Power of the above Algorithms in the Compiler Technologies.
     - Copy Propagation in SSA. The Copy propagation requires the traversal of SSA either in post order or reverse post order to get the USE and DEF. Post order traversal uses to traverse the use of SSA variables before Def in order to perform copy propagation. Reverse post order to traverse Def before use.
    - Loop Induction Variables requires to form a Strongly connected component between Load/Computation with plus/multiple in form of induction/Store. The traversal of SSA graph can be either in Post order and Reverse Post Order to form such Loop induction variable Analysis.
- Traversal of dominator tree can be either in Post Order and Reverse post order to rename the SSA variables to form the SSA graph based on Dominance frontier.
- Calculation of dominator and Post Dominator requires the Post Order and Reverse Post Order traversal.
- Traversal of dominator tree in Post Order and Reverse Post order helps in population of Live range representation in SSA graph.
 - Traversal of Loop body in Post order and Reverse post order traversal helps in creating the regions on the CFG like SESE/SEME, hyperblock and superblocks.
 - Live range population for each region formed above needs to be traversed in Post order and Reverse Post order.
 - Traversal of SSA graphs for tree SSA optimizations like Loop induction, Loop distribution, Vectorization, Copy propagation, Loop invariants, Identification of PRE candidates.
 - Traversal of Loop body in the DFS Search or BFS Search to form the Strongly connected Component. The SCC is formed with 2 Level DFS Search, and Tarjan Algorithm.
 - Topological ordering of the Data Dependency graph forms the basis of vectorization.
 - SLP based vectorization that forms the isomorphic operations requires traversal of Data Dependency graph in Depth Search or Breadth First Search manner.
 - Forming and identification of reducible and irreducible loop require the traversal on Depth First Search manner.
 - Basic Block Numbering of CFG require DFS Search Traversal.
 - Fixed point Data flow Analysis ( forward and Backward Data Flow) requires post order and Reverse post order traversal of CFG.
 - Formation and the scheduler algorithm like Trace Scheduling , Superblock Scheduling and the basic block scheduling require the DFS Search and the BFS Search on the DDG to perform the Scheduling Algorithm.
- Forming the Chain of recurrences and Scalar evolution of loops to represent the induction variable, require the traversal of SSA graphs.
 - Partitioning formed for Loop distribution, Data layout optimization require DFS or BFS Search.
 - Traversal of expression trees and Abstract Syntax tree require post order and Reverse Post order to form the code generation.
 - Loop fusion/Loop fission require to traverse the loop body in Post order and Reverse Post order.
 The above list is few and there are many application to the above algorithms in Compiler Technologies.

Undefined, unspecified and implementation-defined behavior


Undefined behavior
The effect of attempting to modify a string literal is undefined.
 accessing an array beyond its bounds, dereferencing the null pointer
writing allegedly clever expressions like i++ + ++i.

Use of an uninitialized variable
Signed integer overflow:
Oversized Shift Amounts
Dereferences of Wild Pointers and Out of Bounds Array Accesses:
Dereferencing a NULL Pointer
Violating Type Rules: It is undefined behavior to cast an int* to a float* and dereference it
   ==> dereferencing a pointer that aliases another of an incompatible type is undefined behavior.

WHAT IS STRICT ALIASING?
Strict aliasing is an assumption, made by the C (or C++) compiler, that dereferencing pointers to objects of different types will never refer to the same memory location (i.e. alias eachother.) 


implementation-defined behavior
Certain aspects and operations of the abstract machine are described in this International Standard as implementation-defined (for example, sizeof(int)). These constitute the parameters of the abstract machine. Each implementation shall include documentation describing its characteristics and behavior in these respects.

The language says that we have data-types. The compiler vendors specify what sizes shall they use, and provide a documentation of what they did.

unspecified behavior

Certain other aspects and operations of the abstract machine are described in this International Standard as unspecified (for example, order of evaluation of arguments to a function). Where possible, this International Standard defines a set of allowable behaviors. These define the nondeterministic aspects of the abstract machine.
The language doesn't specify the evaluation,







how to avoid

For example, using the -fwrapv flag eliminates undefined behavior that results from signed integer overflow 
If writing code in a non-portable dialect of C isn't your thing, then the -ftrapv and -fcatch-undefined-behavior flags (along with the other tools mentioned before) can be useful weapons in your arsenal to track down these sorts of bugs

 -fcatch-undefined-behavior is deprecated

-ftrapv This option generates traps for signed overflow on addition, subtraction, multiplication operations. 
-fwrapv This option instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication wraps around using twos-complement representation. This flag enables some optimizations and disables others. This option is enabled by default for the Java front-end, as required by the Java language specification. 

2015年4月28日 星期二

avoid implicit type conversion in ternary operation

failed case on llvm 3.6

uint convert_uint_sat_rte(float x);

int main() {

  float x1;

  x1 = 9223373136366403584.000000f;
  uint r1 = convert_uint_sat_rte(x1);
  printf("%x\n", r1);
}
uint convert_uint_sat_rte(float x)
{
  return bigger(x) ? 0xffffffff: (small(x) ? 0 : rintf(x));
}


clang 3.6 + O2 get blow IR


define i32 @convert_uint_sat_rte(float %x) #0 {
entry:
  %call = call fastcc i32 @bigger(float %x)
  %tobool = icmp ne i32 %call, 0
  br i1 %tobool, label %cond.end6, label %cond.false
cond.false:                                       ; preds = %entry
  %call1 = call fastcc i32 @small(float %x)
  %tobool2 = icmp ne i32 %call1, 0
  br i1 %tobool2, label %cond.end6, label %cond.false4
cond.false4:                                      ; preds = %cond.false
  %call5 = call fastcc float @mce_rintf(float %x)
  br label %cond.end6
cond.end6:                                        ; preds = %cond.false4, %cond.false, %entry
  %cond7 = phi float [ 0x41F0000000000000, %entry ], [ %call5, %cond.false4 ], [ 0.000000e+00, %cond.false ]
  %conv = fptoui float %cond7 to i32
  ret i32 %conv

}

instructionCommbing will optimize phi instruction as below

cond.end6:                                        ; preds = %cond.false4, %cond.false, %entry
%cond7 = phi float [ fptoui(0x41F0000000000000), %entry ], [fptoui (call5), %cond.false4 ], [fptoui(0.000000e+00, %cond.false ]
  ret i32 %cond7

but 0x41F0000000000000 is bigger than float representation so result phi is


%cond7 = phi i32 [ undef, %entry ], [ %phitmp, %mce_rintf.exit ], [ 0, %cond.false ]


undef will generate failed code, 
3.5 is fine because without this patch

WHY:
in C99, float 's rank is &gt; any integer, so in this case



return bigger(x) ? 0xffffffff: (small(x) ? 0 : rintf(x));

0 and 0xfffffffff will promoted as float , but 0xfffffffff is too big (0x41F0000000000000 = 0xffffffff in double type representation)
rewrite as blow code will got correct result with O2 ( but it's strange O0 can get correct result)



return bigger(x) ? 0xffffffff: (small(x) ? 0 : (uint) rintf(x));

gcc 4.9 default open -fdelete-null-pointer-checks


#include
int foo(int *a, int size){

memset (a, 0, size);
if (a != NULL)
   return 1;
else
   return 0;
}



this function always return 1

gcc 4.9 default opens -fdelete-null-pointer-checks
but memset's first funtion parameters is "nonull attribute", so else section will be removed