Move llvm/Support/IRBuilder.h -> llvm/IRBuilder.h
[oota-llvm.git] / examples / ExceptionDemo / ExceptionDemo.cpp
index 20516a783b9d8cbeba2b9fa802d6fca472be9945..6dbd6626de94021893e7161e7a00c8d36a513799 100644 (file)
@@ -52,6 +52,7 @@
 #include "llvm/DerivedTypes.h"
 #include "llvm/ExecutionEngine/ExecutionEngine.h"
 #include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/IRBuilder.h"
 #include "llvm/Module.h"
 #include "llvm/PassManager.h"
 #include "llvm/Intrinsics.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Transforms/Scalar.h"
-#include "llvm/Support/IRBuilder.h"
 #include "llvm/Support/Dwarf.h"
 #include "llvm/Support/TargetSelect.h"
 
-#ifdef OLD_EXC_SYSTEM
-// See use of UpgradeExceptionHandling(...) below                        
-#include "llvm/AutoUpgrade.h"
-#endif
-
 // FIXME: Although all systems tested with (Linux, OS X), do not need this 
 //        header file included. A user on ubuntu reported, undefined symbols 
 //        for stderr, and fprintf, and the addition of this include fixed the
@@ -188,9 +183,7 @@ static std::vector<std::string> ourTypeInfoNames;
 static std::map<int, std::string> ourTypeInfoNamesIndex;
 
 static llvm::StructType *ourTypeInfoType;
-#ifndef OLD_EXC_SYSTEM
 static llvm::StructType *ourCaughtResultType;
-#endif
 static llvm::StructType *ourExceptionType;
 static llvm::StructType *ourUnwindExceptionType;
 
@@ -885,7 +878,7 @@ void generateStringPrint(llvm::LLVMContext &context,
   
   llvm::Value *stringVar;
   llvm::Constant *stringConstant = 
-  llvm::ConstantArray::get(context, toPrint);
+  llvm::ConstantDataArray::getString(context, toPrint);
   
   if (useGlobal) {
     // Note: Does not work without allocation
@@ -927,7 +920,8 @@ void generateIntegerPrint(llvm::LLVMContext &context,
                           llvm::Value &toPrint,
                           std::string format, 
                           bool useGlobal = true) {
-  llvm::Constant *stringConstant = llvm::ConstantArray::get(context, format);
+  llvm::Constant *stringConstant =
+    llvm::ConstantDataArray::getString(context, format);
   llvm::Value *stringVar;
   
   if (useGlobal) {
@@ -969,9 +963,7 @@ void generateIntegerPrint(llvm::LLVMContext &context,
 /// @param unwindResumeBlock unwind resume block
 /// @param exceptionCaughtFlag reference exception caught/thrown status storage
 /// @param exceptionStorage reference to exception pointer storage
-#ifndef OLD_EXC_SYSTEM
 /// @param caughtResultStorage reference to landingpad result storage
-#endif
 /// @returns newly created block
 static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context, 
                                             llvm::Module &module, 
@@ -982,23 +974,17 @@ static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
                                             llvm::BasicBlock &terminatorBlock,
                                             llvm::BasicBlock &unwindResumeBlock,
                                             llvm::Value **exceptionCaughtFlag,
-                                            llvm::Value **exceptionStorage
-#ifndef OLD_EXC_SYSTEM
-                                            ,llvm::Value **caughtResultStorage
-#endif
-                                            ) {
+                                            llvm::Value **exceptionStorage,
+                                            llvm::Value **caughtResultStorage) {
   assert(exceptionCaughtFlag && 
          "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag "
          "is NULL");
   assert(exceptionStorage && 
          "ExceptionDemo::createFinallyBlock(...):exceptionStorage "
          "is NULL");
-
-#ifndef OLD_EXC_SYSTEM
   assert(caughtResultStorage && 
          "ExceptionDemo::createFinallyBlock(...):caughtResultStorage "
          "is NULL");
-#endif
   
   *exceptionCaughtFlag = createEntryBlockAlloca(toAddTo,
                                          "exceptionCaught",
@@ -1011,13 +997,11 @@ static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
                                              exceptionStorageType,
                                              llvm::ConstantPointerNull::get(
                                                exceptionStorageType));
-#ifndef OLD_EXC_SYSTEM
   *caughtResultStorage = createEntryBlockAlloca(toAddTo,
                                               "caughtResultStorage",
                                               ourCaughtResultType,
                                               llvm::ConstantAggregateZero::get(
                                                 ourCaughtResultType));
-#endif
   
   llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
                                                    blockName,
@@ -1171,9 +1155,7 @@ llvm::Function *createCatchWrappedInvokeFunction(llvm::Module &module,
   std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
   llvm::Value *exceptionCaughtFlag = NULL;
   llvm::Value *exceptionStorage = NULL;
-#ifndef OLD_EXC_SYSTEM
   llvm::Value *caughtResultStorage = NULL;
-#endif
   
   // Finally block which will branch to unwindResumeBlock if 
   // exception is not caught. Initializes/allocates stack locations.
@@ -1186,10 +1168,8 @@ llvm::Function *createCatchWrappedInvokeFunction(llvm::Module &module,
                                                       *endBlock,
                                                       *unwindResumeBlock,
                                                       &exceptionCaughtFlag,
-                                                      &exceptionStorage
-#ifndef OLD_EXC_SYSTEM
-                                                      ,&caughtResultStorage
-#endif
+                                                      &exceptionStorage,
+                                                      &caughtResultStorage
                                                       );
   
   for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
@@ -1250,15 +1230,7 @@ llvm::Function *createCatchWrappedInvokeFunction(llvm::Module &module,
   
   builder.SetInsertPoint(unwindResumeBlock);
   
-  
-#ifndef OLD_EXC_SYSTEM
   builder.CreateResume(builder.CreateLoad(caughtResultStorage));
-#else
-  llvm::Function *resumeOurException = module.getFunction("_Unwind_Resume");
-  builder.CreateCall(resumeOurException, 
-                     builder.CreateLoad(exceptionStorage));
-  builder.CreateUnreachable();
-#endif
   
   // Exception Block
   
@@ -1266,7 +1238,6 @@ llvm::Function *createCatchWrappedInvokeFunction(llvm::Module &module,
   
   llvm::Function *personality = module.getFunction("ourPersonality");
   
-#ifndef OLD_EXC_SYSTEM
   llvm::LandingPadInst *caughtResult = 
     builder.CreateLandingPad(ourCaughtResultType,
                              personality,
@@ -1286,46 +1257,10 @@ llvm::Function *createCatchWrappedInvokeFunction(llvm::Module &module,
 
   // FIXME: Redundant storage which, beyond utilizing value of 
   //        caughtResultStore for unwindException storage, may be alleviated 
-  //        alltogether with a block rearrangement
+  //        altogether with a block rearrangement
   builder.CreateStore(caughtResult, caughtResultStorage);
   builder.CreateStore(unwindException, exceptionStorage);
   builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
-#else
-  llvm::Function *ehException = module.getFunction("llvm.eh.exception");
-
-  // Retrieve thrown exception
-  llvm::Value *unwindException = builder.CreateCall(ehException);
-  
-  // Store exception and flag
-  builder.CreateStore(unwindException, exceptionStorage);
-  builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
-  llvm::Value *functPtr = builder.CreatePointerCast(personality, 
-                                                    builder.getInt8PtrTy());
-  
-  args.clear();
-  args.push_back(unwindException);
-  args.push_back(functPtr);
-  
-  // Note: Skipping index 0
-  for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
-    // Set up type infos to be caught
-    args.push_back(module.getGlobalVariable(
-                                  ourTypeInfoNames[exceptionTypesToCatch[i]]));
-  }
-  
-  args.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), 0));
-  
-  llvm::Function *ehSelector = module.getFunction("llvm.eh.selector");
-  
-  // Set up this exeption block as the landing pad which will handle
-  // given type infos. See case Intrinsic::eh_selector in 
-  // SelectionDAGBuilder::visitIntrinsicCall(...) and AddCatchInfo(...)
-  // implemented in FunctionLoweringInfo.cpp to see how the implementation
-  // handles this call. This landing pad (this exception block), will be 
-  // called either because it nees to cleanup (call finally) or a type 
-  // info was found which matched the thrown exception.
-  llvm::Value *retTypeInfoIndex = builder.CreateCall(ehSelector, args);
-#endif
   
   // Retrieve exception_class member from thrown exception 
   // (_Unwind_Exception instance). This member tells us whether or not
@@ -1404,12 +1339,6 @@ llvm::Function *createCatchWrappedInvokeFunction(llvm::Module &module,
                                 catchBlocks[nextTypeToCatch]);
   }
 
-#ifdef OLD_EXC_SYSTEM
-  // Must be run before verifier                                                
-  UpgradeExceptionHandling(&module);
-#endif
-
-  
   llvm::verifyFunction(*ret);
   fpm.run(*ret);
   
@@ -1709,8 +1638,6 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   ourTypeInfoType = llvm::StructType::get(context, 
                                           TypeArray(builder.getInt32Ty()));
 
-#ifndef OLD_EXC_SYSTEM
-
   llvm::Type *caughtResultFieldTypes[] = {
     builder.getInt8PtrTy(),
     builder.getInt32Ty()
@@ -1720,8 +1647,6 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   ourCaughtResultType = llvm::StructType::get(context,
                                             TypeArray(caughtResultFieldTypes));
 
-#endif
-
   // Create OurException type
   ourExceptionType = llvm::StructType::get(context, 
                                            TypeArray(ourTypeInfoType));
@@ -1965,14 +1890,6 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
                  true, 
                  false);
   
-  // llvm.eh.selector intrinsic
-  
-  getDeclaration(&module, llvm::Intrinsic::eh_selector);
-  
-  // llvm.eh.exception intrinsic
-  
-  getDeclaration(&module, llvm::Intrinsic::eh_exception);
-  
   // llvm.eh.typeid.for intrinsic
   
   getDeclaration(&module, llvm::Intrinsic::eh_typeid_for);
@@ -2005,7 +1922,8 @@ int main(int argc, char *argv[]) {
   }
   
   // If not set, exception handling will not be turned on
-  llvm::JITExceptionHandling = true;
+  llvm::TargetOptions Opts;
+  Opts.JITExceptionHandling = true;
   
   llvm::InitializeNativeTarget();
   llvm::LLVMContext &context = llvm::getGlobalContext();
@@ -2018,6 +1936,7 @@ int main(int argc, char *argv[]) {
   llvm::EngineBuilder factory(module);
   factory.setEngineKind(llvm::EngineKind::JIT);
   factory.setAllocateGVsWithCode(false);
+  factory.setTargetOptions(Opts);
   llvm::ExecutionEngine *executionEngine = factory.create();
   
   {