[source]

compiler/llvmGen/LlvmCodeGen/CodeGen.hs

Note [Literals and branch conditions]

[note link]

It is important that whenever we generate branch conditions for literals like ‘1’, they are properly narrowed to an LLVM expression of type ‘i1’ (for bools.) Otherwise, nobody is happy. So when we convert a CmmExpr to an LLVM expression for a branch conditional, exprToVarOpt must be certain to return a properly narrowed type. genLit is responsible for this, in the case of literal integers.

Often, we won’t see direct statements like:

if(1) {
} else {

}

at this point in the pipeline, because the Glorious Code Generator will do trivial branch elimination in the sinking pass (among others,) which will eliminate the expression entirely.

However, it’s certainly possible and reasonable for this to occur in hand-written C– code. Consider something like:

#if !defined(SOME_CONDITIONAL)
#define CHECK_THING(x) 1
#else
#define CHECK_THING(x) some_operation((x))
#endif
f() {
if (CHECK_THING(xyz)) {
} else {

}

}

In such an instance, CHECK_THING might result in an expression in one case, and a literal in the other, depending on what in particular was #define’d. So we must be sure to properly narrow the literal in this case to i1 as it won’t be eliminated beforehand.

For a real example of this, see ./rts/StgStdThunks.cmm