同一个基本块中的所有 LLVM PHI 指令是否总是具有相同的输入块集?

Do all LLVM PHI instructions in the same basic block always have the same set of incomming blocks?

我注意到在 LLVM 位码文件中,同一基本块中的所有 PHI 指令通常具有相同的一组传入块。

有谁知道所有 LLVM 位码文件是否总是如此?

或者是否有任何优化通过?

例如,这是我的 C 代码:

// test.c

int main(){
  int **p, *q;
  int *a, *b, c, d;

  p = &a;

  if (p) {
    if (c) {
      q = &c;
    }
  }
  else{
    p = &b;
    q = &d;
  }
  if (d) {
    *p = q;
  }
}

clangopt编译后:

clang -Xclang -disable-O0-optnone -c -emit-llvm test.c
opt -mem2reg test.bc -o test.opt.bc

这是输出 test.opt.bc,其中块 12 中的所有 PHI 指令都具有相同的块 11 和 12:

; Function Attrs: noinline nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32*, align 8
  %2 = alloca i32*, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = icmp ne i32** %1, null
  br i1 %5, label %6, label %11

6:                                                ; preds = %0
  %7 = load i32, i32* %3, align 4
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %6
  br label %10

10:                                               ; preds = %9, %6
  br label %12

11:                                               ; preds = %0
  br label %12

12:                                               ; preds = %11, %10
  %.01 = phi i32** [ %1, %10 ], [ %2, %11 ]
  %.1 = phi i32* [ %3, %10 ], [ %4, %11 ]
  %13 = load i32, i32* %4, align 4
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %12
  store i32* %.1, i32** %.01, align 8
  br label %16

16:                                               ; preds = %15, %12
  ret i32 0
}

答案是

它直接来自language referencephi指令描述:

After this, the ‘phi’ instruction takes a list of pairs as arguments, with one pair for each predecessor basic block of the current block.

由于所涉及的所有 phi 指令都属于同一个基本块,因此这些成对列表应该引用同一组前导指令。