[llvm-commits] CVS: llvm/lib/Reoptimizer/Trigger/Trigger.cpp

Anand Shukla ashukla at cs.uiuc.edu
Fri Feb 14 14:47:07 PST 2003


Changes in directory llvm/lib/Reoptimizer/Trigger:

Trigger.cpp updated: 1.14 -> 1.15

---
Log message:

Changes to runtime framework

---
Diffs of the changes:

Index: llvm/lib/Reoptimizer/Trigger/Trigger.cpp
diff -u llvm/lib/Reoptimizer/Trigger/Trigger.cpp:1.14 llvm/lib/Reoptimizer/Trigger/Trigger.cpp:1.15
--- llvm/lib/Reoptimizer/Trigger/Trigger.cpp:1.14	Tue Oct 15 17:14:28 2002
+++ llvm/lib/Reoptimizer/Trigger/Trigger.cpp	Fri Feb 14 14:46:29 2003
@@ -16,9 +16,7 @@
 #include "llvm/Module.h"
 #include "llvm/iTerminators.h"
 #include "llvm/Support/CFG.h"
-#include <map>
-
-#include <sys/time.h>
+#include "TriggerAuxillary.h"
 #include <iostream>
 #include <fstream>
 
@@ -31,96 +29,18 @@
 
 int reopt_threshold;
 
-#ifdef GET_TRACE_TIME
-
-static struct timeval llvm_trace_time;
-static struct timeval llvm_trigger_time;
-
-extern "C" void llvm_time_start(){
-  gettimeofday(&llvm_trace_time, (void *)0);
-}
-
-extern "C" void llvm_time_end(){
-  static std::ofstream f_out("llvm_last_run");
-
-  struct timeval time_end;
-  gettimeofday(&time_end, (void *)0);
-  
-  long time = (time_end.tv_sec - llvm_trace_time.tv_sec)*1000000 + 
-    (time_end.tv_usec - llvm_trace_time.tv_usec);
-  
-  f_out<<time<<"\n";
-}
-
-void llvm_trigger_time_start(){
-  gettimeofday(&llvm_trigger_time, (void *)0);
-}
-
-void llvm_trigger_time_end(){
-  static std::ofstream f_out("llvm_trigger_last_run");
-    
-  struct timeval time_end;
-  gettimeofday(&time_end, (void *)0);
-  
-  long time = (time_end.tv_sec - llvm_trigger_time.tv_sec)*1000000 + 
-    (time_end.tv_usec - llvm_trigger_time.tv_usec);
-  
-  f_out<<time<<"\n";
-}
-
+inline Module *initModules(vector<Function *> &funcList){
+  Module *M;
+#ifdef READER_TIME
+  cpc_count_usr_events(1);
 #endif
-
-
-void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M);
-
-void getBranchExitCode(vector<unsigned int> &instVec, 
-                       map<int, int> &branchMap, 
-                       map<int, uint64_t> &targetMap,
-                       map<int, uint64_t> &callMap);
-
-extern "C" void llvmInitializeCounter(int *cnt, int k){
-  for(int i=0; i<k; i++)
-    cnt[i]=0;
-}
-
-extern "C" void reoptimizerInitialize(int *t){
-  char *a = getenv("LLVM_THRESHOLD");
-  if(!a)
-    *t = 30; //default threshold!
-  else
-    *t = atoi(a);
-}
-
-void printTrace(vector<BasicBlock *> &vBB, int pn){
-  std::cerr<<"Path no:"<<pn<<" ---------\n";
-  for(vector<BasicBlock *>::iterator VVI = vBB.begin(), VVE = vBB.end(); 
-      VVI!=VVE; ++VVI)
-    std::cerr<<*VVI;
-  std::cerr<<"End of path----\n";
-}
-
-
-
-
-extern "C" void trigger(int mn, int pn, int *cnt){
-  static bool initialized = false; //so module is read just once
-  static Module *M;
-  static std::vector<Function *> funcList; //map of functions
-
-  //std::cerr<<"Meth: "<<mn<<"\t path"<<pn<<"\t count:"<<cnt[pn]<<std::endl;
-
-  //intialize counter!
-  cnt[pn]=0;
-#ifdef GET_TRACE_TIME
-  llvm_trigger_time_start();          
+  //BytecodeParser Parser;  
+  M = ParseBytecodeBuffer(LLVMBytecode, llvm_length);
+#ifdef READER_TIME
+  cpc_count_usr_events(0);
 #endif
-
-  if(!initialized){
-    //BytecodeParser Parser;
-    M = ParseBytecodeBuffer(LLVMBytecode, llvm_length);
     assert(M && "Module parsing failed!");
-    //std::cerr<<M;
-    initialized = true;
+
     //read in pointers to functions
     int mmn = -1;
     for(Module::iterator MI=M->begin(), ME=M->end(); MI!=ME; MI++){
@@ -133,26 +53,30 @@
       mmn++;
       funcList.push_back(F);
     }
-  }
+    return M;
+}
 
-  assert(M && "Module unparsed!");
 
-  assert(funcList[mn] && "Function not parsed!");
-   
- 
-  std::vector<BasicBlock *> vBB;
+bool isThrashing(TraceCache *tr, uint64_t startAddr){
+  //Keep count of thrashing, so as to avoid it!
+  static std::map<uint64_t, int> thrashing;
+
+  if(tr->hasTraceAddr(startAddr) && thrashing[startAddr]<4){
+    thrashing[startAddr] += 1;
+    tr->removeTrace(startAddr);
+  }
+  else if(thrashing[startAddr] > 3){
+    return true;
+  }
+  return false;
+}
 
-  getBBtrace(vBB, pn, funcList[mn]);
-  
-  //return;//just measure time to retrace!
 
-  if(vBB[0] == &funcList[mn]->front() || 
+bool isWellFormedLoop(vector<BasicBlock *> &vBB, Function *f){
+  if(vBB[0] == &f->front() || 
      isa<ReturnInst>(vBB[vBB.size()-1]->getTerminator())){
-    //std::cerr<<"Found a path starting with root!\n";
-#ifdef GET_TRACE_TIME
-    llvm_trigger_time_end();          
-#endif
-    return;
+    //Found a path starting with root
+    return false;
   }
 
   //check if loop is not formed
@@ -161,45 +85,112 @@
   if(Bcheck->isConditional()){
     if((vBB[0] != Bcheck->getSuccessor(0)) && 
        (vBB[0] != Bcheck->getSuccessor(1))){
-      std::cerr<<"llvm:: Not a loop!\n";
-#ifdef GET_TRACE_TIME
-      llvm_trigger_time_end();          
-#endif
-      return;
+      //std::cerr<<"llvm:: Not a loop!\n";
+      return false;
     }
   }
   else{
     if(vBB[0] != Bcheck->getSuccessor(0)){
-      std::cerr<<"llvm:: Not a loop!\n";
-#ifdef GET_TRACE_TIME
-      llvm_trigger_time_end();          
-#endif
-      return;
+      //std::cerr<<"llvm:: Not a loop!\n";
+      return false;
     }
   }
-  
+  return true;
+}
+
+extern "C" void trigger(int mn, int pn, int *cnt, int *reg){
+  static bool initialized = false; //so module is read just once
+  static std::vector<Function *> funcList; //map of functions
+  static Module *M;
+
   static VirtualMem *vm = new VirtualMem();
   static TraceCache *tr = new TraceCache();
 
-  std::pair<uint64_t, uint64_t> firtBBLimits = getBasicBlockInfo(*(vBB.begin()));
+#ifdef GET_TRIGGER_TIME
+  llvm_trigger_time_start();          
+#endif
+
+  if(!initialized){
+    M = initModules(funcList);
+    initialized = true;
+  }
+
+  assert(M && "Module unparsed!");
+
+  assert(funcList[mn] && "Function not parsed!");
+  
+
+  ///Do analysis over module
+  vector<Instruction *> instToRemv;
+  
+  findAllRegs(funcList[mn], instToRemv);
+
+  std::map<uint64_t, char> addrToRm;
+  //addrToRm[0x000000] = 1;
+ 
+  for(std::vector<Instruction *>::iterator IRI = instToRemv.begin(), IRE = 
+        instToRemv.end(); IRI != IRE; ++IRI){
+    std::vector<uint64_t> vaddr = getLLVMInstrInfo(*IRI);
+    for(std::vector<uint64_t>::iterator VRR = vaddr.begin(), VRRE = vaddr.end();
+        VRR != VRRE; ++VRR){
+      if(!isBranchInstr(vm->readInstrFrmVm(*VRR, tr)))
+        addrToRm[*VRR] = 3;
+    }
+  }
+  
+  std::vector<BasicBlock *> vBB; //trace of BBs
+  //  std::vector<Instruction *> instToErase;
+
+
+#ifdef BBTRACE_TIME
+  cpc_count_usr_events(1);
+#endif
+  getBBtrace(vBB, pn, funcList[mn]);//, instToErase);
+#ifdef BBTRACE_TIME
+  cpc_count_usr_events(0);
+#endif  
+
+
+  if(!isWellFormedLoop(vBB, funcList[mn])){
+    cnt[pn] = -99999999;
+#ifdef GET_TRIGGER_TIME
+    llvm_trigger_time_end();          
+#endif
+    return;
+  }
+   
+#ifdef MAP_TIME
+  cpc_count_usr_events(1);
+#endif
+  std::pair<uint64_t, uint64_t> firtBBLimits = 
+    getBasicBlockInfo(*(vBB.begin()));
+#ifdef MAP_TIME
+  cpc_count_usr_events(0);
+#endif
   uint64_t startAddr = firtBBLimits.first;
 
   assert(startAddr && "startAddr should have been defined!");
 
   //return if SAME trace already there!
   if(tr->hasTraceAddr(startAddr) && tr->hasTraceId(pn)){
-    std::cerr<<"Trace exists!!!\n";
-#ifdef GET_TRACE_TIME
+    //std::cerr<<"Trace exists!!!\n";
+#ifdef GET_TRIGGER_TIME
     llvm_trigger_time_end();          
 #endif
+    cnt[pn] = -9999999;
     return;
   }
 
-  if(tr->hasTraceAddr(startAddr)){
-    std::cerr<<"llvm:: Removing old trace!\n";
-    tr->removeTrace(startAddr);
+  
+  if(isThrashing(tr, startAddr)){
+#ifdef GET_TRIGGER_TIME
+    llvm_trigger_time_end();          
+#endif
+    cnt[pn] = -9999999;
+    return;
   }
-
+  
+    
   //the trace of instructions
   vector<unsigned int> trace;
   
@@ -216,259 +207,214 @@
   
   //Now iterate ove trace of BBs, and insert them into the cache
 
+
+  int branchIndex = 0;
+  map<int, vector<unsigned int> *> exitStubs;
+  vector<unsigned int> *stubVec = new vector<unsigned int>();
   for(vector<BasicBlock *>::iterator VBI = vBB.begin(), VBE = vBB.end(); 
       VBI!=VBE; ++VBI){
 
     //Take the first instr of the basic block, and insert
     //all instructions uptil the end of basic block in the trace
+#ifdef MAP_TIME
+    cpc_count_usr_events(1);
+#endif
     std::pair<uint64_t, uint64_t> bbInst = getBasicBlockInfo(*VBI);
+#ifdef MAP_TIME
+    cpc_count_usr_events(0);
+#endif
 
-    //set various flags
-    bool isCondBranch = false;
-    bool hasNextBB = false;
-    bool hasTrueIndex = false;
-
-    //see if terminator is a conditional branch
-    TerminatorInst *TI = (*VBI)->getTerminator();
-    if(isa<BranchInst>(TI)){
-      BranchInst *BI = cast<BranchInst>(TI);
-      if(BI->isConditional()){
-        isCondBranch = true;
-        
-        //see if next BB is in vBB
-        if(VBI != vBB.end()-1 && BI->getSuccessor(0) == *(VBI+1) ){
-          hasNextBB = true;
-          hasTrueIndex = true;
-        }
-        else if(VBI != vBB.end()-1 && BI->getSuccessor(1) == *(VBI+1) )
-          hasNextBB = true;
-      }
-      else{//is unconditional branch
-        assert(BI->isUnconditional() && "Ought to be unconditional branch!");
-        if(VBI != vBB.end()-1 && BI->getSuccessor(0) == *(VBI+1) ){
-          hasNextBB = true;
-        }
-      }
-    }
-
-    if(VBI != vBB.end()-1){//so its NOT last BB
-      assert(hasNextBB && "Next BB not in sequence!");
-    }
-
-    //Now, the last inst corresponding to the BB MAY or MAY NOT be included
-    //in the returned addresses. So leave last two instructions
-    //Now if unconditional branch, then read until last 2, 
-    //if unconditional branch
-    //then read until last 4
-    int skipIndex = (isCondBranch ? 4:2);
-
-    std::cerr<<"llvm:: From->to\t"<<(void *)bbInst.first<<"->"<<(void *)(bbInst.second-4*skipIndex)<<"\n";
-
-    for(uint64_t addr = bbInst.first, endAddr = bbInst.second;
-        addr<=endAddr-4*skipIndex; addr+=4, instIndex++){
-      unsigned inst = vm->readInstrFrmVm(addr);
-
-      trace.push_back(inst);
-      //handle call instructions later!
-      if(isCallInstr(inst)){
-        callMap[instIndex] = addr;
-        //std::cerr<<(void *)inst;
-        //std::cerr<<"\t\tcall is index:"<<instIndex<<"\t address: "<<addr<<"\n";
-      }
+    uint64_t bbStart = bbInst.first;
+    uint64_t bbEnd = bbInst.second;
 
-      //std::cerr<< "address: "<<(void *)addr<<" inst: "<<(void *)inst<<"\n";
-      assert(!isBranchInstr(inst) &&  
-             "No branch instruction should occur in middle of BasicBlock!");
-    }
-    //std::cerr<<"---------------\n";
+    //std::cerr<<"Start: "<<(void *)bbStart<<"\t End: "<<(void *)bbEnd<<"\n";
     
-    if(isCondBranch){//skipped last 4!
-      //if 4th last is a branch, then no probs!!!
-      unsigned int instrCheck = vm->readInstrFrmVm(bbInst.second-12);
-      unsigned int br1, ds1, br2, ds2;
-      uint64_t addrbr1, addrbr2;
-      if(isBranchInstr(instrCheck)){
-        br1 = instrCheck;
-        addrbr1 = bbInst.second-12;
-        ds1 = vm->readInstrFrmVm(bbInst.second-8);
-        br2 = vm->readInstrFrmVm(bbInst.second-4);
-        addrbr2 = bbInst.second-4;
-        ds2 = vm->readInstrFrmVm(bbInst.second);
-      }
-      else{
-        trace.push_back(instrCheck);
-        
-        if(isCallInstr(instrCheck)){
-          callMap[instIndex] = bbInst.second - 12;
-        }
-        
-        instIndex++;
-        
-        br1 = vm->readInstrFrmVm(bbInst.second-8);
-        addrbr1 = bbInst.second-8;
-        ds1 = vm->readInstrFrmVm(bbInst.second-4);
-        br2 = vm->readInstrFrmVm(bbInst.second);
-        addrbr2 = bbInst.second;
-        ds2 = vm->readInstrFrmVm(bbInst.second+4);
+    for(uint64_t addr = bbStart; addr <= bbEnd; addr+=4){
+      unsigned instr = vm->readInstrFrmVm(addr, tr);
+
+      if(addrToRm.find(addr) != addrToRm.end()){
+        if(!isCallInstr(instr))
+          stubVec->push_back(instr);
+        continue;
       }
       
-      assert(isBranchInstr(br1) && isBranchInstr(br2) && "Not a branch!");
-      
-      if(hasNextBB){
-        //eliminate one of the branches!
-        std::vector<BasicBlock *>::iterator VBNext = VBI+1;
-        std::pair<uint64_t, uint64_t> bbDest = getBasicBlockInfo(*VBNext);
-        //get branch br1 and br2 targets to check if any matches bbDest
-        uint64_t tar1, tar2;
-        
-        tar1 = getBranchTarget(br1, addrbr1);
-        tar2 = getBranchTarget(br2, addrbr2);
-        
-        assert( (tar1 == bbDest.first || tar2 == bbDest.first) && 
-                "No destination matched for consecutive BBs");
-        
-        if(tar1 == bbDest.first){
-          //check that second branch is ba, or bn
-          assert( (isBranchAlways(br2) || isBranchNever(br2) ) &&
-                  "Branch 2 should have been branch always, or branch never!");
-          
-          //so invert branch condition, make it second branch, 
-          //and eliminate first branch!
-          
-          //take the target of br2 and inverted condition of br1
-          unsigned int invertedBranch = vm->getInvertedBranch(br1);
-          
-          //only handling the case when ds2 == nop!
-
-          //insert  br2, ds1 in the trace (in that order)
-          //store branches and PCs for later correction in branchMap
-          
-          assert(ds2 == 0x01000000 && 
-                 "Can't handle the case when the unconditional branch doesn't have nop in delay slot!");
-
-            //trace.push_back(ds1);
-            //instIndex++;
-          
-          trace.push_back(invertedBranch);
-          //branchMap[instIndex] = addrbr2;
-          targetMap[instIndex] = getBranchTarget(br2, addrbr2);
+      assert(instr);
 
-          trace.push_back(ds1);
-          instIndex += 2;
+      bool gotoNextBB = false;
+
+      if(isBranchInstr(instr) && !isBranchNever(instr)){
+        if(isBranchAlways(instr)){
+          //check that the target of branch is next in trace!
+          if(VBI != VBE-1){//is not last BB
+            TerminatorInst *TI = (*VBI)->getTerminator();
+            BranchInst *BI = cast<BranchInst>(TI);
+            assert(BI && 
+                   getBasicBlockInfo(BI->getSuccessor(0)).first == 
+                   getBranchTarget(instr, addr) && "Incorrect trace!");
+            //insert delay slot
+            if(addrToRm.find(addr+4) == addrToRm.end())
+              trace.push_back(vm->readInstrFrmVm(addr+4, tr));
+            else{
+              stubVec->push_back(vm->readInstrFrmVm(addr+4, tr));
+              trace.push_back(NOP);
+            }
+            
+            instIndex++;
+            addr += 4;
+          }
+          else{
+          //else must be last BB, so just ignore branch and insert a BA to top
+            TerminatorInst *TI = (*VBI)->getTerminator();
+            BranchInst *BI = cast<BranchInst>(TI);
+            assert(BI && 
+                   getBasicBlockInfo(BI->getSuccessor(0)).first == startAddr 
+                   && "Incorrect trace");
+
+            //insert a BA going to the top
+            trace.push_back(getBranchInst(0x10800000, (uint64_t)(&trace[0]),
+                                          (uint64_t)(&trace[instIndex])));
+            //insert delay slot
+            if(addrToRm.find(addr+4) == addrToRm.end())
+              trace.push_back(vm->readInstrFrmVm(addr+4, tr));
+            else{
+              stubVec->push_back(vm->readInstrFrmVm(addr+4, tr));
+              trace.push_back(NOP);
+            }
+
+            exitStubs[branchIndex] = stubVec;
+            branchIndex++;
+            stubVec = new vector<unsigned int>();
+
+            instIndex += 2;
+            addr += 4;
+          }
+          gotoNextBB = true;
         }
-        else{
-          //erase second branch
-          //insert into trace br1, ds1, ds2
-          
-          trace.push_back(br1);
-          //branchMap[instIndex] = addrbr1;
-          targetMap[instIndex] = getBranchTarget(br1, addrbr1);
-
-          trace.push_back(ds1);
-          trace.push_back(ds2);
-          
-          instIndex += 3;
+        else{ //is a conditional branch!
+          if(VBI != VBE-1){//is not last BB
+            //check if it points to next BB insequence
+            //TerminatorInst *TI = (*(VBI+1))->getTerminator();
+            //BranchInst *BI = cast<BranchInst>(TI);
+            //assert(BI && "No branch???");
+            uint64_t nextBBaddr = getBasicBlockInfo(*(VBI+1)).first;
+            uint64_t tar = getBranchTarget(instr, addr);
+            if(tar == nextBBaddr){
+              //taken path
+              //reverse the branch condition, and point it to addr+8
+              unsigned int revBr = vm->getInvertedBranch(instr);
+              targetMap[instIndex] = addr+8;
+
+              trace.push_back(revBr);
+              if(addrToRm.find(addr+4) == addrToRm.end())
+                trace.push_back(vm->readInstrFrmVm(addr+4, tr));
+              else{
+                stubVec->push_back(vm->readInstrFrmVm(addr+4, tr));
+                trace.push_back(NOP);
+              }
+
+              exitStubs[branchIndex] = stubVec;
+              branchIndex++;
+              stubVec = new vector<unsigned int>();
+
+              instIndex += 2;
+              
+              addr += 8;
+              gotoNextBB = true;
+            }
+            else{
+              //not taken path
+              //leave the instruction as is, and goto next instruction
+              targetMap[instIndex] = getBranchTarget(instr, addr);
+              
+              trace.push_back(instr);
+              if(addrToRm.find(addr+4) == addrToRm.end())
+                trace.push_back(vm->readInstrFrmVm(addr+4, tr));
+              else{
+                stubVec->push_back(vm->readInstrFrmVm(addr+4, tr));
+                trace.push_back(NOP);
+              }
+              
+              exitStubs[branchIndex] = stubVec;
+              branchIndex++;
+              stubVec = new vector<unsigned int>();
+
+              instIndex += 2;
+
+              addr += 8;
+            }
+          }
+          else{ // last BB in seq!
+            TerminatorInst *TI = (*VBI)->getTerminator();
+            BranchInst *BI = cast<BranchInst>(TI);
+            assert(BI && 
+                   getBasicBlockInfo(BI->getSuccessor(1)).first == startAddr 
+                   && "Incorrect trace");
+            
+            //insert a BA going to the top
+            trace.push_back(getBranchInst(0x10800000, (uint64_t)(&trace[0]),
+                                          (uint64_t)(&trace[instIndex])));
+            
+            
+            
+            //exitStubs[branchIndex] = stubVec;
+            //branchIndex++;
+            stubVec = new vector<unsigned int>();
+
+            if(addrToRm.find(addr+4) == addrToRm.end())
+              trace.push_back(vm->readInstrFrmVm(addr+4, tr));
+            else{
+              stubVec->push_back(vm->readInstrFrmVm(addr+4, tr));
+              trace.push_back(NOP);
+            }
+
+            vector<unsigned int> *tVec = exitStubs[branchIndex-1];
+            assert(tVec);
+            
+            for(vector<unsigned int>::iterator TVI = stubVec->begin(), 
+                  TVE = stubVec->end(); TVI != TVE; ++TVI)
+              tVec->push_back(*TVI);
+            
+            instIndex += 2;
+            
+            addr += 4;
+            gotoNextBB = true;
+          }
         }
       }
-      else{//So it must be last BB in sequence!
-        
-        //check that it points to first BB
-        std::pair<uint64_t, uint64_t> bbDest = getBasicBlockInfo(*(vBB.begin()));
-
-        //get branch br1 and br2 targets to check if any matches bbDest
-        uint64_t tar1, tar2;
-
-        tar1 = getBranchTarget(br1, addrbr1);
-        tar2 = getBranchTarget(br2, addrbr2);
-        
-        assert(tar2 == bbDest.first && "Last BB must point to first BB");
-        //remove br1: it ALWAYS goes to target!
-
-        //br1
-        // trace.push_back(br1);
-        //targetMap[instIndex] = getBranchTarget(br1, addrbr1);
-        //instIndex++;
-        //br1 --end
-
-        trace.push_back(ds1);
-        instIndex++;
-
-        //point new branch to the tracehead!
-        trace.push_back(br2);     
-        unsigned int newBr = getBranchInst(br2, (uint64_t)(&trace[0]), (uint64_t)(&trace[instIndex]));
-        trace[instIndex] = newBr;
-       
-        trace.push_back(ds2); 
-        instIndex += 2;
-      }
-    }
-    //is unconditional branch
-    else{
-      unsigned int instrCheck = vm->readInstrFrmVm(bbInst.second-4);
-      unsigned int br1, ds1;
-      uint64_t addrbr1;
-      if(isBranchInstr(instrCheck)){
-        br1 = instrCheck;
-        addrbr1 = bbInst.second-4;
-        ds1 = vm->readInstrFrmVm(bbInst.second);
-      }
-      else{
-        trace.push_back(instrCheck);
-        
-        if(isCallInstr(instrCheck)){
-          callMap[instIndex] = bbInst.second - 4;
-        }
+      
+      else if(isCallInstr(instr)){
+        callMap[instIndex] = getCallTarget(instr, addr);
 
+        trace.push_back(instr);
         instIndex++;
-        
-        br1 = vm->readInstrFrmVm(bbInst.second);
-        addrbr1 = bbInst.second;
-        ds1 = vm->readInstrFrmVm(bbInst.second+4);
       }
       
-      //std::cerr<<"Branch: "<<(void *)br1<<"\n";
-      assert( (isBranchAlways(br1) || isBranchNever(br1) ) &&
-              "Unconditional branch: should be branch always, or branch never!");
-      
-      //now do fancy stuff!
-      //if branch points to the next BB, then erase it
-      if(hasNextBB){
-        std::pair<uint64_t, uint64_t> bbDest = getBasicBlockInfo(*(VBI+1));
-        
-        //get branch br1 target to check if it matches bbDest
-        uint64_t tar1 = getBranchTarget(br1, addrbr1);
-
-        assert(tar1 == bbDest.first && "The trace is not continuous!");
-        
-        //copy ds1, and tht's all
-        trace.push_back(ds1);
-        instIndex++;
-      }
       else{
-        //Must be last BB. check for coninuity!
-        std::pair<uint64_t, uint64_t> bbDest = 
-          getBasicBlockInfo(*(vBB.begin()));
-
-        uint64_t tar1 = getBranchTarget(br1, addrbr1);
-
-        assert(tar1 == bbDest.first && "Last BB must point to first BB");
-        trace.push_back(br1);
-        //branchMap[instIndex] = addrbr1;
-        targetMap[instIndex] = getBranchTarget(br1, addrbr1);
-        trace.push_back(ds1);
-        
-        instIndex += 2;
+        trace.push_back(instr);
+        instIndex++;
       }
+
+      if(gotoNextBB)
+        break;
     }
   }
   
   //insert exit code!
-  getBranchExitCode(trace, branchMap, targetMap, callMap);
+  getBranchExitCode(trace, branchMap, targetMap, callMap, exitStubs);
   //Now add trace to cache, and we are all set!
+  vector<unsigned int> *vstubs = exitStubs[branchIndex-1];
+  /*
+  if(vstubs){
+    if(!tr->addTrace(startAddr, trace, pn, callMap, branchMap, vstubs))
+      std::cerr<<" Trace could not be added!!!\n";
+  }
+  else*/
+  
   if(!tr->addTrace(startAddr, trace, pn, callMap, branchMap))
-    std::cerr<<"^^^^^^^^^^************** Trace could not be added!!!\n";
+    std::cerr<<" Trace could not be added!!!\n";
+  
 
-#ifdef GET_TRACE_TIME
+#ifdef GET_TRIGGER_TIME
   llvm_trigger_time_end();          
 #endif
 }
@@ -477,13 +423,18 @@
 void getBranchExitCode(vector<unsigned int> &instVec, 
                        map<int, int> &branchMap, 
                        map<int, uint64_t> &targetMap, 
-                       map<int, uint64_t> &callMap){
+                       map<int, uint64_t> &callMap,
+                       map<int, vector<unsigned int> *> exitStubs){
 
   //for each branch, insert code!
   int instIndex = instVec.size();
 
+  int branchIndex = 0;
   for(map<int, uint64_t>::iterator MI = targetMap.begin(), ME = targetMap.end();
-      MI != ME; ++MI){
+      MI != ME; ++MI, branchIndex++){
+
+    assert(exitStubs[branchIndex]);
+
     unsigned int instr = instVec[MI->first];
     uint64_t pc = MI->second;
     
@@ -495,13 +446,20 @@
                                           (uint64_t)&instVec[instIndex]); 
     instVec[instIndex] = call_inst;
 
-    callMap[instIndex] = (uint64_t)&instVec[instIndex];
+    callMap[instIndex] = (uint64_t)&llvm_time_end;//CHANGED! instVec[instIndex];
     //end call_inst!
 #endif
 
+    //insert instructions in exit stub
+    vector<unsigned int> *stubVec = exitStubs[branchIndex];
+    for(vector<unsigned int>::iterator VI = stubVec->begin(), 
+          VE = stubVec->end(); VI != VE; ++VI)
+      instVec.push_back(*VI);
+
+
     //save %io, ad1
     unsigned int stx1 = 0xf077a000|0x7f7; //stx %i0, %fp+offset-8
-      unsigned int stx2 = 0xf277a000|0x7ef; //stx %i1, offset-16
+    unsigned int stx2 = 0xf277a000|0x7ef; //stx %i1, offset-16
 
     //sethi
     uint64_t target = targetMap[MI->first];
@@ -555,7 +513,7 @@
     branchMap.erase(MI->first);
     branchMap[MI->first] = instIndex; 
 
-    instIndex += 11;
+    instIndex += (11 + stubVec->size());
 
 #ifdef GET_TRACE_TIME
     instIndex++;





More information about the llvm-commits mailing list