Notes on the LLVM Demo ====================== llvm.org/docs/LangRef.html To try the C program: > clang -o demo demo.c > ./demo 10 Do not forget the command line argument! Translate demo.c into LLVM IR: > clang -c -emit-llvm -o demo.bc demo.c Interpret the code: > lli demo.bc 10 Translate to textual form: > llvm-dis -o demo.ll demo.bc > less demo.ll Note: * Allocation of global variables, string constants. * Note three address code. * Note types. * Note SSA form, but no phi-functions as all always updating the *global* variables. * Note control flow graph ("preds = ...") explicit. Optimize using O1: > opt -O1 -o demo-O1.bc demo.bc > llvm-dis -o demo-O1.ll demo-O1.bc > less demo-O1.ll Note: * Keeping variables in registers to a much larger extent. * The phi-functions and how explicitly identifies relationship between control flow and argument to choose. * How the final value of i is determined *if* the loop is executed at least once: %9 contains the result of m > i + 1 %smax is picked to be m if true, i + 1 (= 1) otherwise This result is then written to i, rather than %14 which is the register containing i. ------------------------------------------------------------------------------ From the manual: The ~icmp~ instruction takes three operands. The first operand is the condition code indicating the kind of comparison to perform. It is not a value, just a keyword. The possible condition codes are: eq: equal ne: not equal ugt: unsigned greater than uge: unsigned greater or equal ult: unsigned less than ule: unsigned less or equal sgt: signed greater than sge: signed greater or equal slt: signed less than sle: signed less or equal ---------- nuw and nsw stand for ~No Unsigned Wrap~ and ~No Signed Wrap~, respectively. If the nuw and/or nsw keywords are present, the result value of the add is a poison value if unsigned and/or signed overflow, respectively, occurs. ---------- Poison values are similar to undef values, however they also represent the fact that an instruction or constant expression that cannot evoke side effects has nevertheless detected a condition that results in undefined behavior. There is currently no way of representing a poison value in the IR; they only exist when produced by operations such as add with the nsw flag. Poison values have the same behavior as undef values, with the additional effect that any instruction that has a dependence on a poison value has undefined behavior.