[vmkit-commits] [vmkit] r55469 - in /vmkit/branches/isolate/lib/JnJVM: LLVMRuntime/runtime-default.ll LLVMRuntime/runtime-isolate.ll VMCore/JavaJIT.cpp VMCore/JavaJIT.h VMCore/JavaRuntimeJIT.cpp VMCore/JnjvmModule.cpp VMCore/JnjvmModule.h VMCore/LowerConstantCalls.cpp

Nicolas Geoffray nicolas.geoffray at lip6.fr
Thu Aug 28 04:15:54 PDT 2008


Author: geoffray
Date: Thu Aug 28 06:15:51 2008
New Revision: 55469

URL: http://llvm.org/viewvc/llvm-project?rev=55469&view=rev
Log:
Make the runtime lookup functions readnone to exploit compilation
optimizations.


Modified:
    vmkit/branches/isolate/lib/JnJVM/LLVMRuntime/runtime-default.ll
    vmkit/branches/isolate/lib/JnJVM/LLVMRuntime/runtime-isolate.ll
    vmkit/branches/isolate/lib/JnJVM/VMCore/JavaJIT.cpp
    vmkit/branches/isolate/lib/JnJVM/VMCore/JavaJIT.h
    vmkit/branches/isolate/lib/JnJVM/VMCore/JavaRuntimeJIT.cpp
    vmkit/branches/isolate/lib/JnJVM/VMCore/JnjvmModule.cpp
    vmkit/branches/isolate/lib/JnJVM/VMCore/JnjvmModule.h
    vmkit/branches/isolate/lib/JnJVM/VMCore/LowerConstantCalls.cpp

Modified: vmkit/branches/isolate/lib/JnJVM/LLVMRuntime/runtime-default.ll
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/isolate/lib/JnJVM/LLVMRuntime/runtime-default.ll?rev=55469&r1=55468&r2=55469&view=diff

==============================================================================
--- vmkit/branches/isolate/lib/JnJVM/LLVMRuntime/runtime-default.ll (original)
+++ vmkit/branches/isolate/lib/JnJVM/LLVMRuntime/runtime-default.ll Thu Aug 28 06:15:51 2008
@@ -94,21 +94,27 @@
 ;;; implementation.
 declare void @forceInitialisationCheck(%JavaClass*)
 
+;;; getConstantPoolAt - Get the value in the constant pool of this class.
+;;; This function is removed by Jnjvm after the GVn pass, therefore it does
+;;; not have an actual implementation.
+declare i8* @getConstantPoolAt(i8* (%JavaClass*, i32, ...)*, i8**, %JavaClass*,
+                               i32, ...) readnone
+
 ;;; vtableLookup - Look up the offset in a virtual table of a specific
 ;;; function. This function takes a class and an index to lookup in the
 ;;; constant pool and returns and stores it in the constant pool cache.
-declare i32 @vtableLookup(%JavaClass*, i32, %JavaObject*) readnone 
+declare i8* @vtableLookup(%JavaClass*, i32, ...)
 
 ;;; newLookup - Look up a specific class. The function takes a class and an
 ;;; index to lookup in the constant pool and returns and stores it in the
 ;;; constant pool cache.
-declare %JavaClass* @classLookup(%JavaClass*, i32) readnone 
+declare i8* @classLookup(%JavaClass*, i32, ...)
 
-;;; fieldLookup - Look up a specific field.
-declare i32 @virtualFieldLookup(%JavaClass*, i32) readnone 
+;;; virtualFieldLookup - Look up a specific virtual field.
+declare i8* @virtualFieldLookup(%JavaClass*, i32, ...)
 
-;;; fieldLookup - Look up a specific field.
-declare i8* @staticFieldLookup(%JavaClass*, i32) readnone 
+;;; staticFieldLookup - Look up a specific static field.
+declare i8* @staticFieldLookup(%JavaClass*, i32, ...)
 
 ;;; JavaObjectAquire - This function is called when starting a synchronized
 ;;; block or method.

Modified: vmkit/branches/isolate/lib/JnJVM/LLVMRuntime/runtime-isolate.ll
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/isolate/lib/JnJVM/LLVMRuntime/runtime-isolate.ll?rev=55469&r1=55468&r2=55469&view=diff

==============================================================================
--- vmkit/branches/isolate/lib/JnJVM/LLVMRuntime/runtime-isolate.ll (original)
+++ vmkit/branches/isolate/lib/JnJVM/LLVMRuntime/runtime-isolate.ll Thu Aug 28 06:15:51 2008
@@ -7,4 +7,4 @@
 declare %JavaObject* @getStaticInstance(%JavaClass*, i8*) readnone 
 
 ;;; runtimeUTF8ToStr - Convert the UTF8 in a string.
-declare %JavaObject* @stringLookup(%JavaClass*, i32)
+declare i8* @stringLookup(%JavaClass*, i32)

Modified: vmkit/branches/isolate/lib/JnJVM/VMCore/JavaJIT.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/isolate/lib/JnJVM/VMCore/JavaJIT.cpp?rev=55469&r1=55468&r2=55469&view=diff

==============================================================================
--- vmkit/branches/isolate/lib/JnJVM/VMCore/JavaJIT.cpp (original)
+++ vmkit/branches/isolate/lib/JnJVM/VMCore/JavaJIT.cpp Thu Aug 28 06:15:51 2008
@@ -82,7 +82,7 @@
   } else {
     
     Value* val = getConstantPoolAt(index, JnjvmModule::VirtualLookupFunction,
-                                   args[0], true);
+                                   Type::Int32Ty, args[0], true);
     indexes2.push_back(val);
   }
   
@@ -1082,7 +1082,7 @@
 #ifdef MULTIPLE_VM
     // Lookup the constant pool cache
     Value* val = getConstantPoolAt(index, JnjvmModule::StringLookupFunction,
-                                   0, true);
+                                   JnjvmModule::JavaObjectType, 0, true);
     push(node, AssessorDesc::dRef);  
 #else
     const UTF8* utf8 = ctpInfo->UTF8At(ctpInfo->ctpDef[index]);
@@ -1548,8 +1548,51 @@
 }
 
 Value* JavaJIT::getConstantPoolAt(uint32 index, Function* resolver,
+                                  const Type* returnType,
                                   Value* additionalArg, bool doThrow) {
-  const Type* returnType = resolver->getReturnType();
+
+// This makes unswitch loop very unhappy time-wise, but makes GVN happy
+// number-wise. IMO, it's better to have this than Unswitch.
+#if 1
+#ifdef MULTIPLE_VM
+  Value* CTP = ctpCache;
+  Value* Cl = new LoadInst(ctpCache, "", currentBlock);
+  Cl = new BitCastInst(v, JnjvmModule::JavaClassType, "", currentBlock);
+#else
+  JavaConstantPool* ctp = compilingClass->ctpInfo;
+  LLVMConstantPoolInfo* LCPI = module->getConstantPoolInfo(ctp);
+  Value* CTP = LCPI->getDelegatee(this);
+  LLVMClassInfo* LCI = (LLVMClassInfo*)module->getClassInfo(compilingClass);
+  Value* Cl = LCI->getVar(this);
+#endif
+
+  std::vector<Value*> Args;
+  Args.push_back(resolver);
+  Args.push_back(CTP);
+  Args.push_back(Cl);
+  Args.push_back(ConstantInt::get(Type::Int32Ty, index));
+  if (additionalArg) Args.push_back(additionalArg);
+
+  Value* res = 0;
+  if (doThrow) {
+    res = invoke(JnjvmModule::GetConstantPoolAtFunction, Args, "",
+                 currentBlock);
+  } else {
+    res = CallInst::Create(JnjvmModule::GetConstantPoolAtFunction, Args.begin(),
+                           Args.end(), "", currentBlock);
+  }
+  
+  const Type* realType = 
+    JnjvmModule::GetConstantPoolAtFunction->getReturnType();
+  if (returnType == Type::Int32Ty) {
+    return new PtrToIntInst(res, Type::Int32Ty, "", currentBlock);
+  } else if (returnType != realType) {
+    return new BitCastInst(res, returnType, "", currentBlock);
+  } 
+  
+  return res;
+#else
+
 #ifdef MULTIPLE_VM
   Value* CTP = ctpCache;
 #else
@@ -1568,14 +1611,9 @@
   Value* test = new ICmpInst(ICmpInst::ICMP_EQ, arg1, mvm::jit::constantPtrNull,
                              "", currentBlock);
  
-  if (returnType == Type::Int32Ty) {
-    arg1 = new PtrToIntInst(arg1, returnType, "", currentBlock);
-  } else if (returnType != arg1->getType()) {
-    arg1 = new BitCastInst(arg1, returnType, "", currentBlock);
-  }
   BasicBlock* trueCl = createBasicBlock("Ctp OK");
   BasicBlock* falseCl = createBasicBlock("Ctp Not OK");
-  PHINode* node = llvm::PHINode::Create(returnType, "", trueCl);
+  PHINode* node = llvm::PHINode::Create(mvm::jit::ptrType, "", trueCl);
   node->addIncoming(arg1, currentBlock);
   llvm::BranchInst::Create(falseCl, trueCl, test, currentBlock);
   
@@ -1607,13 +1645,18 @@
   llvm::BranchInst::Create(trueCl, currentBlock);
   currentBlock = trueCl;
 
-  return node;
+  if (returnType == Type::Int32Ty) {
+    return new PtrToIntInst(node, Type::Int32Ty, "", currentBlock);
+  } else {
+    return new BitCastInst(node, returnType, "", currentBlock);
+  } 
+#endif
 }
 
 Value* JavaJIT::getResolvedClass(uint16 index, bool clinit, bool doThrow) {
     
     Value* node = getConstantPoolAt(index, JnjvmModule::ClassLookupFunction,
-                                    0, doThrow);
+                                    JnjvmModule::JavaClassType, 0, doThrow);
     
     if (clinit)
       return invoke(JnjvmModule::InitialisationCheckFunction, node, "",
@@ -1731,7 +1774,14 @@
     
     Function* func = stat ? JnjvmModule::StaticFieldLookupFunction :
                             JnjvmModule::VirtualFieldLookupFunction;
-    Value* ptr = getConstantPoolAt(index, func, 0, true);
+    
+    const Type* returnType = 0;
+    if (stat)
+      returnType = mvm::jit::ptrType;
+    else
+      returnType = Type::Int32Ty;
+
+    Value* ptr = getConstantPoolAt(index, func, returnType, 0, true);
     if (!stat) {
       Value* tmp = new BitCastInst(object, Pty, "", currentBlock);
       std::vector<Value*> args;
@@ -1950,7 +2000,7 @@
                               JnjvmModule::EnveloppeType);
 #else
   llvmEnv = getConstantPoolAt(index, JnjvmModule::EnveloppeLookupFunction,
-                              0, false);
+                              JnjvmModule::EnveloppeType, 0, false);
 #endif
 
   std::vector<Value*> args1;

Modified: vmkit/branches/isolate/lib/JnJVM/VMCore/JavaJIT.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/isolate/lib/JnJVM/VMCore/JavaJIT.h?rev=55469&r1=55468&r2=55469&view=diff

==============================================================================
--- vmkit/branches/isolate/lib/JnJVM/VMCore/JavaJIT.h (original)
+++ vmkit/branches/isolate/lib/JnJVM/VMCore/JavaJIT.h Thu Aug 28 06:15:51 2008
@@ -59,6 +59,7 @@
 private:
 
   llvm::Value* getConstantPoolAt(uint32 index, llvm::Function* resolver,
+                                 const llvm::Type* returnType,
                                  llvm::Value* addArg, bool doThrow = true);
 
 public:

Modified: vmkit/branches/isolate/lib/JnJVM/VMCore/JavaRuntimeJIT.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/isolate/lib/JnJVM/VMCore/JavaRuntimeJIT.cpp?rev=55469&r1=55468&r2=55469&view=diff

==============================================================================
--- vmkit/branches/isolate/lib/JnJVM/VMCore/JavaRuntimeJIT.cpp (original)
+++ vmkit/branches/isolate/lib/JnJVM/VMCore/JavaRuntimeJIT.cpp Thu Aug 28 06:15:51 2008
@@ -84,10 +84,10 @@
   return rcache->methPtr;
 }
 
-extern "C" uint32 virtualFieldLookup(UserClass* caller, uint32 index) {
+extern "C" void* virtualFieldLookup(UserClass* caller, uint32 index) {
   JavaConstantPool* ctpInfo = caller->getConstantPool();
   if (ctpInfo->ctpRes[index]) {
-    return (uint32)ctpInfo->ctpRes[index];
+    return ctpInfo->ctpRes[index];
   }
   
   UserCommonClass* cl = 0;
@@ -101,7 +101,7 @@
   
   ctpInfo->ctpRes[index] = (void*)field->ptrOffset;
   
-  return field->ptrOffset;
+  return (void*)field->ptrOffset;
 }
 
 extern "C" void* staticFieldLookup(UserClass* caller, uint32 index) {
@@ -129,17 +129,17 @@
 }
 
 #ifdef MULTIPLE_VM
-extern "C" JavaString* stringLookup(UserClass* cl, uint32 index) {
+extern "C" void* stringLookup(UserClass* cl, uint32 index) {
   JavaConstantPool* ctpInfo = cl->getConstantPool();
   const UTF8* utf8 = ctpInfo->UTF8At(ctpInfo->ctpDef[index]);
   JavaString* str = JavaThread::get()->isolate->UTF8ToStr(utf8);
   ctpInfo->ctpRes[index] = str;
-  return str;
+  return (void*)str;
 }
 #endif
 
 #ifndef WITHOUT_VTABLE
-extern "C" uint32 vtableLookup(UserClass* caller, uint32 index, JavaObject* obj) {
+extern "C" void* vtableLookup(UserClass* caller, uint32 index, ...) {
   UserCommonClass* cl = 0;
   const UTF8* utf8 = 0;
   Signdef* sign = 0;
@@ -147,8 +147,12 @@
   caller->getConstantPool()->resolveMethod(index, cl, utf8, sign);
   JavaMethod* dmeth = cl->lookupMethodDontThrow(utf8, sign->keyName, false,
                                                 true);
-  assert(obj->classOf->isReady() && "Class not ready in a virtual lookup.");
   if (!dmeth) {
+    va_list ap;
+    va_start(ap, index);
+    JavaObject* obj = va_arg(ap, JavaObject*);
+    va_end(ap);
+    assert(obj->classOf->isReady() && "Class not ready in a virtual lookup.");
     // Arg, it should have been an invoke interface.... Perform the lookup
     // on the object class and do not update offset.
     dmeth = obj->classOf->lookupMethod(utf8, sign->keyName, false, true);
@@ -158,14 +162,14 @@
   
   assert(dmeth->classDef->isReady() && "Class not ready in a virtual lookup.");
 
-  return dmeth->offset;
+  return (void*)dmeth->offset;
 }
 #endif
 
-extern "C" UserClass* classLookup(UserClass* caller, uint32 index) { 
+extern "C" void* classLookup(UserClass* caller, uint32 index) { 
   JavaConstantPool* ctpInfo = caller->getConstantPool();
   UserClass* cl = (UserClass*)ctpInfo->loadClass(index);
-  return cl;
+  return (void*)cl;
 }
 
 

Modified: vmkit/branches/isolate/lib/JnJVM/VMCore/JnjvmModule.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/isolate/lib/JnJVM/VMCore/JnjvmModule.cpp?rev=55469&r1=55468&r2=55469&view=diff

==============================================================================
--- vmkit/branches/isolate/lib/JnJVM/VMCore/JnjvmModule.cpp (original)
+++ vmkit/branches/isolate/lib/JnJVM/VMCore/JnjvmModule.cpp Thu Aug 28 06:15:51 2008
@@ -103,6 +103,7 @@
 llvm::Function* JnjvmModule::AquireObjectFunction = 0;
 llvm::Function* JnjvmModule::ReleaseObjectFunction = 0;
 llvm::Function* JnjvmModule::MultiCallNewFunction = 0;
+llvm::Function* JnjvmModule::GetConstantPoolAtFunction = 0;
 
 #ifdef MULTIPLE_VM
 llvm::Function* JnjvmModule::StringLookupFunction = 0;
@@ -954,7 +955,8 @@
   InitialisationCheckFunction = module->getFunction("initialisationCheck");
   ForceInitialisationCheckFunction = 
     module->getFunction("forceInitialisationCheck");
-
+  
+  GetConstantPoolAtFunction = module->getFunction("getConstantPoolAt");
   ArrayLengthFunction = module->getFunction("arrayLength");
   GetVTFunction = module->getFunction("getVT");
   GetClassFunction = module->getFunction("getClass");

Modified: vmkit/branches/isolate/lib/JnJVM/VMCore/JnjvmModule.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/isolate/lib/JnJVM/VMCore/JnjvmModule.h?rev=55469&r1=55468&r2=55469&view=diff

==============================================================================
--- vmkit/branches/isolate/lib/JnJVM/VMCore/JnjvmModule.h (original)
+++ vmkit/branches/isolate/lib/JnJVM/VMCore/JnjvmModule.h Thu Aug 28 06:15:51 2008
@@ -334,6 +334,7 @@
   static llvm::Function* GetDisplayFunction;
   static llvm::Function* AquireObjectFunction;
   static llvm::Function* ReleaseObjectFunction;
+  static llvm::Function* GetConstantPoolAtFunction;
 #ifdef SERVICE_VM
   static llvm::Function* AquireObjectInSharedDomainFunction;
   static llvm::Function* ReleaseObjectInSharedDomainFunction;

Modified: vmkit/branches/isolate/lib/JnJVM/VMCore/LowerConstantCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/isolate/lib/JnJVM/VMCore/LowerConstantCalls.cpp?rev=55469&r1=55468&r2=55469&view=diff

==============================================================================
--- vmkit/branches/isolate/lib/JnJVM/VMCore/LowerConstantCalls.cpp (original)
+++ vmkit/branches/isolate/lib/JnJVM/VMCore/LowerConstantCalls.cpp Thu Aug 28 06:15:51 2008
@@ -11,6 +11,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Function.h"
 #include "llvm/Instructions.h"
+#include "llvm/Support/CallSite.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
 
@@ -18,6 +19,7 @@
 
 #include "JnjvmModule.h"
 
+#include <iostream>
 using namespace llvm;
 using namespace jnjvm;
 
@@ -34,7 +36,6 @@
   char LowerConstantCalls::ID = 0;
   static RegisterPass<LowerConstantCalls> X("LowerConstantCalls",
                                             "Lower Constant calls");
-
 bool LowerConstantCalls::runOnFunction(Function& F) {
   bool Changed = false;
   for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; BI++) { 
@@ -42,11 +43,13 @@
     for (BasicBlock::iterator II = Cur->begin(), IE = Cur->end(); II != IE;) {
       Instruction *I = II;
       II++;
-      if (CallInst *CI = dyn_cast<CallInst>(I)) {
-        Value* V = CI->getOperand(0);
+      CallSite Call = CallSite::get(I);
+      Instruction* CI = Call.getInstruction();
+      if (CI) {
+        Value* V = Call.getCalledValue();
         if (V == jnjvm::JnjvmModule::ArrayLengthFunction) {
           Changed = true;
-          Value* val = CI->getOperand(1); // get the array
+          Value* val = Call.getArgument(0); // get the array
           Value* array = new BitCastInst(val, jnjvm::JnjvmModule::JavaArrayType,
                                          "", CI);
           std::vector<Value*> args; //size=  2
@@ -59,7 +62,7 @@
           CI->eraseFromParent();
         } else if (V == jnjvm::JnjvmModule::GetVTFunction) {
           Changed = true;
-          Value* val = CI->getOperand(1); // get the object
+          Value* val = Call.getArgument(0); // get the object
           std::vector<Value*> indexes; //[3];
           indexes.push_back(mvm::jit::constantZero);
           indexes.push_back(mvm::jit::constantZero);
@@ -70,7 +73,7 @@
           CI->eraseFromParent();
         } else if (V == jnjvm::JnjvmModule::GetClassFunction) {
           Changed = true;
-          Value* val = CI->getOperand(1); // get the object
+          Value* val = Call.getArgument(0); // get the object
           std::vector<Value*> args2;
           args2.push_back(mvm::jit::constantZero);
           args2.push_back(jnjvm::JnjvmModule::JavaObjectClassOffsetConstant);
@@ -82,7 +85,7 @@
           CI->eraseFromParent();
         } else if (V == jnjvm::JnjvmModule::GetVTFromClassFunction) {
           Changed = true;
-          Value* val = CI->getOperand(1); 
+          Value* val = Call.getArgument(0); 
           std::vector<Value*> indexes; 
           indexes.push_back(mvm::jit::constantZero);
           indexes.push_back(jnjvm::JnjvmModule::OffsetVTInClassConstant);
@@ -93,7 +96,7 @@
           CI->eraseFromParent();
         } else if (V == jnjvm::JnjvmModule::GetObjectSizeFromClassFunction) {
           Changed = true;
-          Value* val = CI->getOperand(1); 
+          Value* val = Call.getArgument(0); 
           std::vector<Value*> indexes; 
           indexes.push_back(mvm::jit::constantZero);
           indexes.push_back(JnjvmModule::OffsetObjectSizeInClassConstant);
@@ -104,7 +107,7 @@
           CI->eraseFromParent();
         } else if (V == jnjvm::JnjvmModule::GetDepthFunction) {
           Changed = true;
-          Value* val = CI->getOperand(1); 
+          Value* val = Call.getArgument(0); 
           std::vector<Value*> indexes; 
           indexes.push_back(mvm::jit::constantZero);
           indexes.push_back(JnjvmModule::OffsetDepthInClassConstant);
@@ -115,7 +118,7 @@
           CI->eraseFromParent();
         } else if (V == jnjvm::JnjvmModule::GetDisplayFunction) {
           Changed = true;
-          Value* val = CI->getOperand(1); 
+          Value* val = Call.getArgument(0); 
           std::vector<Value*> indexes; 
           indexes.push_back(mvm::jit::constantZero);
           indexes.push_back(JnjvmModule::OffsetDisplayInClassConstant);
@@ -126,21 +129,21 @@
           CI->eraseFromParent();
         } else if (V == jnjvm::JnjvmModule::GetClassInDisplayFunction) {
           Changed = true;
-          Value* val = CI->getOperand(1); 
-          Value* depth = CI->getOperand(2); 
+          Value* val = Call.getArgument(0); 
+          Value* depth = Call.getArgument(1); 
           Value* ClassPtr = GetElementPtrInst::Create(val, depth, "", CI);
           Value* Class = new LoadInst(ClassPtr, "", CI);
           CI->replaceAllUsesWith(Class);
           CI->eraseFromParent();
         } else if (V == jnjvm::JnjvmModule::InstanceOfFunction) {
-          ConstantExpr* CE = dyn_cast<ConstantExpr>(CI->getOperand(2));
+          ConstantExpr* CE = dyn_cast<ConstantExpr>(Call.getArgument(1));
           if (CE) {
             ConstantInt* C = (ConstantInt*)CE->getOperand(0);
             CommonClass* cl = (CommonClass*)C->getZExtValue();
             Changed = true;
             BasicBlock* NBB = II->getParent()->splitBasicBlock(II);
             I->getParent()->getTerminator()->eraseFromParent();
-            Value* obj = CI->getOperand(1);
+            Value* obj = Call.getArgument(0);
             Instruction* cmp = new ICmpInst(ICmpInst::ICMP_EQ, obj,
                                             JnjvmModule::JavaObjectNullConstant,
                                             "", CI);
@@ -225,10 +228,77 @@
             break;
           }
         }
+        
+        else if (V == jnjvm::JnjvmModule::GetConstantPoolAtFunction) {
+          Function* resolver = dyn_cast<Function>(Call.getArgument(0));
+          assert(resolver && "Wrong use of GetConstantPoolAt");
+          const Type* returnType = resolver->getReturnType();
+          Value* CTP = Call.getArgument(1);
+          Value* Index = Call.getArgument(3);
+          Changed = true;
+          BasicBlock* NBB = 0;
+          if (CI->getParent()->getTerminator() != CI) {
+            NBB = II->getParent()->splitBasicBlock(II);
+            CI->getParent()->getTerminator()->eraseFromParent();
+          } else {
+            InvokeInst* Invoke = dyn_cast<InvokeInst>(CI);
+            assert(Invoke && "Last instruction is not an invoke");
+            NBB = Invoke->getNormalDest();
+          }
+          
+          std::vector<Value*> indexes; //[3];
+          indexes.push_back(Index);
+          Value* arg1 = GetElementPtrInst::Create(CTP, indexes.begin(),
+                                                  indexes.end(),  "", CI);
+          arg1 = new LoadInst(arg1, "", false, CI);
+          Value* test = new ICmpInst(ICmpInst::ICMP_EQ, arg1,
+                                     mvm::jit::constantPtrNull, "", CI);
+ 
+          BasicBlock* trueCl = BasicBlock::Create("Ctp OK", &F);
+          BasicBlock* falseCl = BasicBlock::Create("Ctp Not OK", &F);
+          PHINode* node = llvm::PHINode::Create(returnType, "", trueCl);
+          node->addIncoming(arg1, CI->getParent());
+          BranchInst::Create(falseCl, trueCl, test, CI);
+  
+          std::vector<Value*> Args;
+          unsigned ArgSize = Call.arg_size(), i = 1;
+          while (++i < ArgSize) {
+            Args.push_back(Call.getArgument(i));
+          }
+          
+          Value* res = 0;
+          if (InvokeInst* Invoke = dyn_cast<InvokeInst>(CI)) {
+            BasicBlock* UI = Invoke->getUnwindDest();
+            res = InvokeInst::Create(resolver, trueCl, UI, Args.begin(),
+                                     Args.end(), "", falseCl);
+
+            // For some reason, an LLVM pass may add PHI nodes to the
+            // exception destination.
+            BasicBlock::iterator Temp = UI->getInstList().begin();
+            while (PHINode* PHI = dyn_cast<PHINode>(Temp)) {
+              Value* Val = PHI->getIncomingValueForBlock(CI->getParent());
+              PHI->removeIncomingValue(CI->getParent(), false);
+              PHI->addIncoming(Val, falseCl);
+              Temp++;
+            }
+          } else {
+            res = CallInst::Create(resolver, Args.begin(), Args.end(), "",
+                                   falseCl);
+            BranchInst::Create(trueCl, falseCl);
+          }
+          
+          node->addIncoming(res, falseCl);
+
+
+          CI->replaceAllUsesWith(node);
+          CI->eraseFromParent();
+          BranchInst::Create(NBB, trueCl);
+          break;
+        }
 #ifdef MULTIPLE_GC
         else if (V == jnjvm::JnjvmModule::GetCollectorFunction) {
           Changed = true;
-          Value* val = CI->getOperand(1); 
+          Value* val = Call.getArgument(0); 
           std::vector<Value*> indexes; 
           indexes.push_back(mvm::jit::constantOne);
           val = new BitCastInst(val, mvm::jit::ptrPtrType, "", CI);





More information about the vmkit-commits mailing list