How to get "phi" instruction in llvm without optimization

llvm, llvm-ir

Solution

I'm not sure what you mean by "do some optimization" but it seems to me that `mem2reg` is exactly what you need. Here is how it's described in the documentation:

This file promotes memory references to be register references. It promotes alloca instructions which only have loads and stores as uses. An alloca is transformed by using dominator frontiers to place phi nodes, then traversing the function in depth-first order to rewrite loads and stores as appropriate. This is just the standard SSA construction algorithm to construct “pruned” SSA form.

Clang itself does not produce optimized LLVM IR. It produces fairly straightforward IR wherein locals are kept in memory (using `alloca`s). The optimizations are done by `opt` on LLVM IR level, and one of the most important optimizations is indeed `mem2reg` which makes sure that locals are represented in LLVM's SSA values instead of memory.

Problem

When I use the command `clang -emit-llvm -S test.c -o test.ll`, there is no any "phi" instruction in the IR file. How can I get it? I know that I can use the pass "-mem2reg" or "-gvn" to get "phi" instruction. But they would do some optimization. I just want to get "phi" without any optimization.

Original source