summaryrefslogtreecommitdiff
path: root/lib/Transforms/Instrumentation/ProfilePaths/InstLoops.cpp
blob: 21f3b25c0e933450c3a49418cf54c4f939c8ff56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//===-- InstLoops.cpp ---------------------------------------- ---*- C++ -*--=//
// Pass to instrument loops
//
// At every backedge, insert a counter for that backedge and a call function
//===----------------------------------------------------------------------===//

#include "llvm/Reoptimizer/InstLoops.h"
#include "llvm/Support/CFG.h"
#include "llvm/Constants.h"
#include "llvm/iMemory.h"
#include "llvm/GlobalVariable.h"
#include "llvm/DerivedTypes.h"
#include "llvm/iOther.h"
#include "llvm/iOperators.h"
#include "llvm/iTerminators.h"
#include "llvm/iPHINode.h"
#include "llvm/Module.h"
#include "llvm/Function.h"
#include "llvm/Pass.h"

//this is used to color vertices
//during DFS

enum Color{
  WHITE,
  GREY,
  BLACK
};

struct InstLoops : public FunctionPass {
  bool runOnFunction(Function &F);
};

static RegisterOpt<InstLoops> X("instloops", "Instrument backedges for profiling");

// createProfilePathsPass - Create a new pass to add path profiling
//
Pass *createInstLoopsPass() {
  return new InstLoops();
}


//helper function to get back edges: it is called by 
//the "getBackEdges" function below
void getBackEdgesVisit(BasicBlock *u,
                       std::map<BasicBlock *, Color > &color,
                       std::map<BasicBlock *, int > &d, 
                       int &time, Value *threshold) {
  
  color[u]=GREY;
  time++;
  d[u]=time;

  for(BasicBlock::succ_iterator vl = succ_begin(u), 
	ve = succ_end(u); vl != ve; ++vl){
    
    BasicBlock *BB = *vl;

    if(color[BB]!=GREY && color[BB]!=BLACK){
      getBackEdgesVisit(BB, color, d, time, threshold);
    }
    
    //now checking for d and f vals
    if(color[BB]==GREY){
      //so v is ancestor of u if time of u > time of v
      if(d[u] >= d[BB]){
        //insert a new basic block: modify terminator accordingly!
        BasicBlock *newBB = new BasicBlock("", u->getParent());
        BranchInst *ti = cast<BranchInst>(u->getTerminator());
        unsigned char index = 1;
        if(ti->getSuccessor(0) == BB){
          index = 0;
        }
        assert(ti->getNumSuccessors() > index && "Not enough successors!");
        ti->setSuccessor(index, newBB);

        //insert global variable of type int
        Constant *initializer = Constant::getNullValue(Type::IntTy);
        GlobalVariable *countVar = new GlobalVariable(Type::IntTy, false, true, 
                                                      initializer, 
                                                      "loopCounter", 
                                                      u->getParent()->getParent());
        
        //load the variable
        Instruction *ldInst = new LoadInst(countVar,"");
        
        //increment
        Instruction *addIn = 
          BinaryOperator::create(Instruction::Add, ldInst, 
                                 ConstantSInt::get(Type::IntTy,1), "");

        //store
        Instruction *stInst = new StoreInst(addIn, countVar);


        Instruction *etr = new LoadInst(threshold, "threshold");
        Instruction *cmpInst = new SetCondInst(Instruction::SetLE, etr, 
                                               addIn, "");
        
        BasicBlock *callTrigger = new BasicBlock("", u->getParent());
        //branch to calltrigger, or *vl
        Instruction *newBr = new BranchInst(callTrigger, BB, cmpInst);

        BasicBlock::InstListType &lt = newBB->getInstList();

        lt.push_back(ldInst);
        lt.push_back(addIn);
        lt.push_back(stInst);
        lt.push_back(etr);
        lt.push_back(cmpInst);
        lt.push_back(newBr);

        //Now add instructions to the triggerCall BB
        //now create a call function
        //call llvm_first_trigger(int *x);
        std::vector<const Type*> inCountArgs;
        inCountArgs.push_back(PointerType::get(Type::IntTy));
 
        const FunctionType *cFty = FunctionType::get(Type::VoidTy, inCountArgs, 
                                                     false);
        Function *inCountMth = 
          u->getParent()->getParent()->getOrInsertFunction("llvm_first_trigger", cFty);
        
        assert(inCountMth && "Initialize method could not be inserted!");

        std::vector<Value *> iniArgs;
        iniArgs.push_back(countVar);
        Instruction *call = new CallInst(inCountMth, iniArgs, "");
        callTrigger->getInstList().push_back(call);
        callTrigger->getInstList().push_back(new BranchInst(BB));
      
        //now iterate over *vl, and set its Phi nodes right
        for(BasicBlock::iterator BB2Inst = BB->begin(), BBend = BB->end(); 
            BB2Inst != BBend; ++BB2Inst){
        
          if(PHINode *phiInst=dyn_cast<PHINode>(&*BB2Inst)){
            int bbIndex = phiInst->getBasicBlockIndex(u);
            if(bbIndex>=0){
              phiInst->setIncomingBlock(bbIndex, newBB);
          
              Value *val = phiInst->getIncomingValue((unsigned int)bbIndex);
              phiInst->addIncoming(val, callTrigger);
            }
          }
        }
      }
    }
  }
  color[u]=BLACK;//done with visiting the node and its neighbors
}


//getting the backedges in a graph
//Its a variation of DFS to get the backedges in the graph
//We get back edges by associating a time
//and a color with each vertex.
//The time of a vertex is the time when it was first visited
//The color of a vertex is initially WHITE,
//Changes to GREY when it is first visited,
//and changes to BLACK when ALL its neighbors
//have been visited
//So we have a back edge when we meet a successor of
//a node with smaller time, and GREY color
void getBackEdges(Function &F, Value *threshold){
  std::map<BasicBlock *, Color > color;
  std::map<BasicBlock *, int> d;
  int time=0;
  getBackEdgesVisit(F.begin(), color, d, time, threshold);
}

//Per function pass for inserting counters and call function
bool InstLoops::runOnFunction(Function &F){
  
  static GlobalVariable *threshold = NULL;
  static bool insertedThreshold = false;
  
   if(!insertedThreshold){
    threshold = new GlobalVariable(Type::IntTy, false, false, 0,
                                                   "reopt_threshold");

    F.getParent()->getGlobalList().push_back(threshold);
    insertedThreshold = true;
  }

  if(F.getName() == "main"){
    //intialize threshold
    std::vector<const Type*> initialize_args;
    initialize_args.push_back(PointerType::get(Type::IntTy));
    
    const FunctionType *Fty = FunctionType::get(Type::VoidTy, initialize_args,
                                                false);
    Function *initialMeth = F.getParent()->getOrInsertFunction("reoptimizerInitialize", Fty);
    assert(initialMeth && "Initialize method could not be inserted!");
    
    std::vector<Value *> trargs;
    trargs.push_back(threshold);
  
    new CallInst(initialMeth, trargs, "", F.begin()->begin());
  }

  assert(threshold && "GlobalVariable threshold not defined!");

  if(F.isExternal()) {
    return false;
  }

  getBackEdges(F, threshold);
  
  return true;
}