编译手动编辑的 IR 文件时 llc 中的模块错误

Broken module error in llc when compiling manually edited IR file

我尝试在代码中插入第35行和第36行,但是当我尝试使用llc生成.o文件时出现错误

 35   %12 = ptrtoint i32* %1 to i64 
 36   call void @__Storemy(i32 10, i64 %12) 
 37   store i32 %10, i32* %1 
 38   br label %18 

错误信息:

Instruction does not dominate all uses! 
  %12 = ptrtoint i32* %1 to i64 
  call void @__StoreTo(i32 15, i64 %12) 
Broken module found, compilation aborted! 
0  libLLVM-3.4.so.1 0x00007f6d31fe25d2 llvm::sys::PrintStackTrace(_IO_FILE*) + 34 
1  libLLVM-3.4.so.1 0x00007f6d31fe23c4 
2  libc.so.6        0x00007f6d30a62d40 
3  libc.so.6        0x00007f6d30a62cc9 gsignal + 57 
4  libc.so.6        0x00007f6d30a660d8 abort + 328 
5  libLLVM-3.4.so.1 0x00007f6d319d2a41 
6  libLLVM-3.4.so.1 0x00007f6d319dbb03 
7  libLLVM-3.4.so.1 0x00007f6d319b2f77 llvm::FPPassManager::runOnFunction(llvm::Function&) + 471 
8  libLLVM-3.4.so.1 0x00007f6d319b2ffb llvm::FPPassManager::runOnModule(llvm::Module&) + 43 
9  libLLVM-3.4.so.1 0x00007f6d319b54b5 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 693 
10 llc              0x000000000040c0d4 
11 llc              0x000000000040b150 main + 368 
12 libc.so.6        0x00007f6d30a4dec5 __libc_start_main + 245 
13 llc              0x000000000040b1a9 
Stack dump: 
0.  Program arguments: llc -filetype=obj test.bc -o test.o 
1.  Running pass 'Function Pass Manager' on module 'test.bc'. 
2.  Running pass 'Module Verifier' on function '@dblfun' 

这是此函数的完整 IR:

define i32 @dbl(i32* %a, i32 %x) #0 {
  call void @__myFuncCall(i32 1, i32 1)
  %1 = alloca i32, align 4
  %2 = alloca i32*, align 8
  %3 = alloca i32, align 4
  store i32* %a, i32** %2, align 8
  %4 = ptrtoint i32* %3 to i64
  call void @__myStore(i32 2, i64 %4)
  store i32 %x, i32* %3, align 4
  %5 = load i32* %3, align 4
  %6 = ptrtoint i32* %3 to i64
  call void @__myLoad(i32 3, i64 %6, i32 %5)
  call void @__myLoad(i32 4, i64 0, i32 5)
  %7 = icmp sgt i32 %5, 5
  call void @__myApply(i32 5, i32 14, i1 %7)
  br i1 %7, label %_then, label %_else

; <label>:8                                       ; preds = %_then
  %9 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([3 x i8]* @.str, i32 0, i32 0))
  call void @__Clear(i32 8)
  %10 = load i32* %3, align 4
  %11 = ptrtoint i32* %3 to i64
  call void @__myLoad(i32 9, i64 %11, i32 %10)
  %12 = ptrtoint i32* %1 to i64
  call void @__StoreTo(i32 10, i64 %12)
  store i32 %10, i32* %1
  br label %18

_else:                                            ; preds = %0
  call void @__test(i32 7, i32 2, i32 0)
  br label %13

_then:                                            ; preds = %0
  call void @__test(i32 6, i32 1, i32 1)
  br label %8

; <label>:13                                      ; preds = %_else
  %14 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([3 x i8]* @.str1, i32 0, i32 0))
  call void @__Clear(i32 11)
  %15 = load i32* %3, align 4
  %16 = ptrtoint i32* %3 to i64
  call void @__myLoad(i32 12, i64 0, i32 2)
  call void @__myLoad(i32 13, i64 %16, i32 %15)
  %17 = mul nsw i32 2, %15
  call void @__myApply(i32 14, i32 2, i32 %17)
  call void @__myStore(i32 15, i64 %12)
  store i32 %17, i32* %1
  br label %18

; <label>:18                                      ; preds = %13, %8
  %19 = load i32* %1
  %20 = ptrtoint i32* %1 to i64
  call void @__myLoad(i32 16, i64 %20, i32 %19)
  call void @__myReturn(i32 17)
  ret i32 %19
}

我找不到这段代码有什么问题,谁能给我一些建议?

您的函数的控制流图如下所示:

      entry
      /   \
     /     \
  _then   _else
    |       | 
    8       13
     \     /
      \   /
       18

%12 在块 8 中定义。紧接着就用了一次就好了,然后在块13中又用了一次,从图中可以看出它不被8支配(实际上它甚至无法从[=12=到达) ]).

在这种情况下,您应该能够将 ptrtoint 指令向上移动到入口块——这样它就可以在所有其他块中访问。