2

I am working on creating LLVM front-end module pass. So, Basically I need to duplicate all load instructions and store in a different register. At -O0 for clang, opt and llc tool, this duplicated load instruction is removed. I looked at the final assembly using objdump, I could see that duplicate load instruction was removed. I want a solution that somehow does not delete duplicate load instruction.

Actual C program is,

int main(){
    int* p = (int *)(0x600000);//Some address
    int x=0x01, y=0x01;
    int z;
    z=x+y;
    *p=z;
}

The corresponding IR is,

define i32 @main() #0 {
entry:
  %p = alloca i32*, align 8
  %x = alloca i32, align 4
  %y = alloca i32, align 4
  %z = alloca i32, align 4
  store i32* inttoptr (i64 6291456 to i32*), i32** %p, align 8
  store i32 1, i32* %x, align 4
  store i32 1, i32* %y, align 4
  %0 = load i32, i32* %x, align 4
  %1 = load i32, i32* %y, align 4
  %add = add nsw i32 %0, %1
  store i32 %add, i32* %z, align 4
  %2 = load i32, i32* %z, align 4
  %3 = load i32*, i32** %p, align 8
  store i32 %2, i32* %3, align 4
  ret i32 0
}

But when my pass is enabled, this IR will change and I duplicate only load instructions with load address being same memory even for duplicated load.

The changed IR would be,

define i32 @main() #0 {
entry:
  %p = alloca i32*, align 8
  %x = alloca i32, align 4
  %y = alloca i32, align 4
  %z = alloca i32, align 4
  store i32* inttoptr (i64 6291456 to i32*), i32** %p, align 8
  store i32 1, i32* %x, align 4
  store i32 1, i32* %y, align 4
  %0 = load i32, i32* %x, align 4
  %1 = load i32, i32* %y, align 4
  %2 = load i32, i32* %x, align 4 //Added
  %3 = load i32, i32* %y, align 4 //Added
  %add = add nsw i32 %0, %1
  store i32 %add, i32* %z, align 4
  %4 = load i32, i32* %z, align 4
  %5 = load i32*, i32** %p, align 8
  %6 = load i32, i32* %z, align 4 //Added
  %7 = load i32*, i32** %p, align 8        //Added
  store i32 %4, i32* %5, align 4
  ret i32 0
}

I am able to see the changed IR at IR level but not at the final assembly level after llc. I think llc is removing all duplicated loads. How do I stop llc from removing?

Note: I tried making all variables volatile. For this it works, I am able to see duplicated loads after llc. But, this is not a proper solution. I cannot make all thousand variables volatile :(.

Arjun
  • 133
  • 6
  • 2
    Making all variables volatile sounds like an easy search-and-replace, and `volatile` is an accurate description of the semantics you want. – arnt Jan 31 '19 at 12:29
  • Yea, I already did that. But, do you think there is any way apart from making volatile. – Arjun Jan 31 '19 at 12:38
  • Perhaps, if you explain what you want and how it is different from having volatile variables. Programming languages (apart from perl) tend to have no more than one mechanism for each goal. – arnt Jan 31 '19 at 13:27
  • These instructions are deleted because they are unused, I think. Try "using" them in some way. – arrowd Jan 31 '19 at 15:47
  • I used. Even then it is deleted. Compiler is smart enough at even -O0 level to remove the duplicated load. – Arjun Feb 01 '19 at 14:43

0 Answers0