Arjun
Arjun

Reputation: 133

How can I make a variable volatile or const using LLVM pass

Is there any LLVM built in method that allows making my variable volatile or const.

For example, if my program in C is,

#include<stdio.h>
int main() {
    int x=1,y=2,z=10;
    z=x+y;
    return 0;
}

I want to change this program using LLVM pass such that it behaves as below,

#include<stdio.h>
int main() {
    volatile int x=1,y=2,z=10;
    z=x+y;
    return 0;
}

Upvotes: 2

Views: 363

Answers (1)

Arjun
Arjun

Reputation: 133

Ok, I got it. It would be simple setVolatile(1).

Pass would look something like this,

for(Module::iterator F=M.begin(),e=M.end();F!=e;++F){
    for(Function::iterator B=F->begin(),e=F->end();B!=e;++B){
        for(BasicBlock::iterator i=B->begin(),e=B->end();i!=e;++i){
            if(isa<StoreInst>(i)){
                StoreInst *temps=dyn_cast<StoreInst>(i);
                temps->setVolatile(1);
            }
            if(isa<LoadInst>(i)){
                 LoadInst *temps=dyn_cast<LoadInst>(i);
                 temps->setVolatile(1);
            }       
        }
    }
}

Upvotes: 3

Related Questions