Optimize Global variables in C++

I have a lot of lambdas in my code, each of them is trying to calculate some data.
lambda code like this:

[](){
    for(int i = 0; i < 2000; i++){
        data_2[i] = std::abs(data_1[i]);
        data_4[i] = data_3[i] - data_2[i];
    }
};

data_1, data_2 … are all global variables, I want to know which of them is only used in one lambda function so that I can put the declaration of this variable in lambda function.
after optimize

[](){
    double tmp = 0;
    for(int i = 0; i < 2000; i++){
        tmp = std::abs(data_1[i]);
        data_4[i] = data_3[i] - tmp;
    }
};

how can I do with this problem? I want to write two passes, the first pass is to analyse which variable can be put into lambda, another is to optimize the IR, put variable in lambda