[llvm-commits] CVS: llvm/lib/Reoptimizer/TraceCache/TraceCache.cpp
Anand Shukla
ashukla at cs.uiuc.edu
Sat May 31 21:34:36 PDT 2003
Changes in directory llvm/lib/Reoptimizer/TraceCache:
TraceCache.cpp updated: 1.11 -> 1.12
---
Log message:
Simple additions to the runtime API
---
Diffs of the changes:
Index: llvm/lib/Reoptimizer/TraceCache/TraceCache.cpp
diff -u llvm/lib/Reoptimizer/TraceCache/TraceCache.cpp:1.11 llvm/lib/Reoptimizer/TraceCache/TraceCache.cpp:1.12
--- llvm/lib/Reoptimizer/TraceCache/TraceCache.cpp:1.11 Thu Apr 10 17:49:24 2003
+++ llvm/lib/Reoptimizer/TraceCache/TraceCache.cpp Sat May 31 21:33:08 2003
@@ -19,6 +19,7 @@
#include "llvm/Reoptimizer/MemoryManager.h"
#include "llvm/Reoptimizer/TraceCache.h"
+#include "llvm/Reoptimizer/TraceCache2.h"
#include "llvm/Reoptimizer/VirtualMem.h"
#include "llvm/Reoptimizer/InstrUtils.h"
#include "llvm/Reoptimizer/GetTraceTime.h"
@@ -51,6 +52,16 @@
vm = new VirtualMem();
}
+TraceCache::TraceCache(VirtualMem *vmem){
+ mm = new MemoryManager();
+
+ isLimitSet = true; //making limit as the size of memory
+ currSize = 0;
+ limit = mm->getMemSize();
+
+ vm = vmem;
+}
+
void TraceCache::setLimit(int n) {
assert(isLimitSet && "can't chage limit, if not set initially");
assert(currSize < n && "trying to set to a smaller limit than current size");
@@ -87,7 +98,7 @@
removeTrace(addr);
}
- uint64_t traceStartAddr = mm->getMemory(trace.size());
+ uint64_t traceStartAddr = mm->getMemory(sz);
if(traceStartAddr == 0) return false; //could not allocate space!
@@ -143,19 +154,20 @@
}
bool TraceCache::hasTraceInstructions(uint64_t addr){
- return (traceInstructions.find(addr) != traceInstructions.end());
+ return (traceInstructions.find(addr) != traceInstructions.end() &&
+ traceInstructions[addr].size()!=0);
}
unsigned int TraceCache::getAddr(uint64_t addr){
if(hasTraceInstructions(addr))
return traceInstructions[addr][0];
+ //else if(hasTraceInstructions(addr-4))
+ //return traceInstructions[addr-4][1];
+#ifdef GET_TRACE_TIME
else if(hasTraceInstructions(addr-4))
return traceInstructions[addr-4][1];
-#ifdef GET_TRACE_TIME
else if(hasTraceInstructions(addr-8))
- return traceInstructions[addr-8][2];
- else if(hasTraceInstructions(addr-12))
- return traceInstructions[addr-12][3];
+ return traceInstructions[addr-8][2];
#endif
return 0;
}
@@ -172,15 +184,23 @@
std::vector<unsigned int> &trace,
int traceUniqId,
std::map<int, uint64_t> &callMap,
- std::map<int, uint64_t> &branchMap){
+ std::map<int, uint64_t> &branchMap, TraceCache2 *tr2){
//remove trace if its already there
- if(hasTraceAddr(instAddr))
- removeTrace(instAddr);
+ //if(hasTraceAddr(instAddr))
+ //removeTrace(instAddr);
int sz = trace.size();
-
+
+ if(sz%8){
+ sz = sz - (sz%8);
+ sz += 8;
+ }
+
while(isLimitSet && currSize+sz>limit){
+
+ assert(0 && "out of space");
+
if(currSize == 0)
return false;
@@ -191,7 +211,7 @@
removeTrace(addr);
}
- uint64_t traceStartAddr = mm->getMemory(trace.size());
+ uint64_t traceStartAddr = mm->getMemory(sz);
if(traceStartAddr == 0) return false; //could not allocate space!
@@ -208,24 +228,30 @@
//add instAddr to queue
allocationOrder.push_back(instAddr);
- //copy the first few instructions of the trace in the original code
- //and write a jump instruction in its place
- traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr));
- traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr+4));
-
+ bool ifPushed = false;
+ if(!hasTraceInstructions(instAddr)){
+ //traceInstructions[instAddr].clear();
+ //copy the first few instructions of the trace in the original code
+ //and write a jump instruction in its place
+ traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr, tr2));
+ //traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr+4));
+ ifPushed = true;
+ }
#ifdef GET_TRACE_TIME
- //two more: to accomodate call instruction and get time!
- traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr+8));
- traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr+12));
-
+ //two more: to accomodate call instruction and get time!
+ if(ifPushed){
+ traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr+4, tr2));
+ traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr+8, tr2));
+ ifPushed = false;
+ }
//write call instruction!
vm->writeCallLLVMTime(instAddr);
-
+
//Now write branch instruction with target as
//traceStartAddr at the address instAddr
vm->writeBranchInstruction(instAddr+8, traceStartAddr);
#endif
-
+
#ifndef GET_TRACE_TIME
vm->writeBranchInstruction(instAddr, traceStartAddr);
#endif
@@ -246,6 +272,25 @@
return true;
}
+void TraceCache::patchTrace(uint64_t n){
+ //put back the original instruction at n
+ assert(reverseMap.find(n) != reverseMap.end());
+
+ n = reverseMap[n];
+
+ assert(traceInstructions.find(n) != traceInstructions.end() &&
+ "Starting instruction not found!");
+
+ //std::cerr<<" Removed branch at :"<<(void *)n<<"\n";
+
+ int removeSize = traceInstructions[n].size();
+
+ for(int i=0; i < removeSize; i++)
+ vm->writeInstToVM(n+4*i, (traceInstructions[n])[i]);
+
+ doFlush(n-8, n+4*removeSize+16);
+}
+
void TraceCache::removeTrace(uint64_t n){
//if no trace, do nothing
if(traces.find(n) == traces.end())
@@ -309,7 +354,7 @@
removeTrace(addr);
}
- uint64_t traceStartAddr = mm->getMemory(trace.size());
+ uint64_t traceStartAddr = mm->getMemory(sz);
if(traceStartAddr == 0) return false; //could not allocate space!
@@ -368,7 +413,7 @@
}
-#undef GET_TRACE_TIME
+//#undef GET_TRACE_TIME
bool TraceCache::addTrace(uint64_t instAddr, int sz,
int traceUniqId, uint64_t &addr){
@@ -434,3 +479,115 @@
return true;
}
+
+//Do the following:
+//1. Insert a jump at location instrAddr with target as new trace
+bool TraceCache::addTrace(uint64_t instAddr,
+ std::vector<unsigned int> &trace,
+ int traceUniqId,
+ std::map<int, uint64_t> &callMap,
+ std::map<int, uint64_t> &myBranchMap,
+ std::map<int, int> &branchMap,
+ uint64_t firstLevelTraceStartAddr){
+
+ //remove trace if its already there
+ //if(hasTraceAddr(instAddr))// && uniqueID(instAddrtraceUniqId))
+ //removeTrace(instAddr);
+
+ int sz = trace.size();
+ if(sz%8){
+ sz = sz - (sz % 8);
+ sz = sz + 8;
+ }
+
+ while(isLimitSet && currSize+sz>limit){
+
+ assert(0 && "Can't allocate more mem!");
+
+ if(currSize == 0)
+ return false;
+
+ //erase first addr from queue
+ assert(allocationOrder.size()>0 && "No entries in trace!");
+ uint64_t addr = allocationOrder.front();
+ allocationOrder.pop_front();
+ removeTrace(addr);
+ }
+
+ uint64_t traceStartAddr = mm->getMemory(sz);
+
+ if(traceStartAddr == 0) return false; //could not allocate space!
+
+ //copy trace to the memory
+ vm->copyToVM(trace, traceStartAddr);
+
+ traces[instAddr] = traceStartAddr;
+
+ traceId[traceUniqId] = traceStartAddr;
+ reverseTraceId[instAddr] = traceUniqId;
+ traceSize[instAddr] = sz;
+ reverseMap[traceStartAddr] = instAddr;
+ currSize += sz;
+
+ //add instAddr to queue
+ allocationOrder.push_back(instAddr);
+
+ //copy the first few instructions of the trace in the original code
+ //and write a jump instruction in its place
+ traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr));
+ traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr+4));
+
+#ifdef GET_TRACE_TIME
+ //two more: to accomodate call instruction and get time!
+ traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr+8));
+ traceInstructions[instAddr].push_back(vm->readInstrFrmVm(instAddr+12));
+
+ //write call instruction!
+ vm->writeCallLLVMTime(instAddr);
+
+ //Now write branch instruction with target as
+ //traceStartAddr at the address instAddr
+ vm->writeBranchInstruction(instAddr+8, traceStartAddr);
+#endif
+
+#ifndef GET_TRACE_TIME
+ vm->writeBranchInstruction(instAddr, traceStartAddr);
+#endif
+
+ //now set correctly all branches
+ vm->setBranches(branchMap, traceStartAddr);
+
+ //now set correctly all branches
+ vm->setBranches(myBranchMap, traceStartAddr);
+
+ //set correctly all calls
+ vm->setCalls(callMap, traceStartAddr);
+
+ doFlush(traceStartAddr, traceStartAddr+4*sz+4);
+ doFlush(instAddr, instAddr+4);
+
+#ifdef GET_TRACE_TIME
+ doFlush(instAddr+8, instAddr+12);
+#endif
+
+ //now write branch in the level 1 tracecache
+ vm->writeBranchInstruction(firstLevelTraceStartAddr+4, traceStartAddr);
+ doFlush(firstLevelTraceStartAddr+4, firstLevelTraceStartAddr+20);
+
+ return true;
+}
+
+uint64_t TraceCache::getAddrLessThan(uint64_t brAddr){
+ std::map<uint64_t, uint64_t> myMap;
+
+ for(std::map<uint64_t, uint64_t>::iterator MI = traces.begin(),
+ ME = traces.end(); MI != ME; ++MI)
+ myMap[MI->second] = MI->first;
+
+ for(std::map<uint64_t, uint64_t>::reverse_iterator RMI = myMap.rbegin(),
+ RME = myMap.rend(); RMI != RME; ++RMI)
+ if(RMI->first < brAddr)
+ return RMI->first;
+
+ assert(0);
+}
More information about the llvm-commits
mailing list