Switch codegen for -fcatch-undefined-bahavior over to __builtin_trap
instead of abort to improve codesize and codegen.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@91374 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp
index b9334d0..975f749 100644
--- a/lib/CodeGen/CGExpr.cpp
+++ b/lib/CodeGen/CGExpr.cpp
@@ -17,6 +17,7 @@
 #include "CGObjCRuntime.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclObjC.h"
+#include "llvm/Intrinsics.h"
 #include "llvm/Target/TargetData.h"
 using namespace clang;
 using namespace CodeGen;
@@ -1012,34 +1013,27 @@
   }
 }
 
-static llvm::Constant *getAbortFn(CodeGenFunction &CGF) {
-  // void abort();
-
-  const llvm::FunctionType *FTy =
-    llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
-
-  return CGF.CGM.CreateRuntimeFunction(FTy, "abort");
-}
-
-llvm::BasicBlock*CodeGenFunction::getAbortBB() {
-  if (AbortBB)
-    return AbortBB;
+llvm::BasicBlock*CodeGenFunction::getTrapBB() {
+  if (TrapBB)
+    return TrapBB;
 
   llvm::BasicBlock *Cont = 0;
   if (HaveInsertPoint()) {
     Cont = createBasicBlock("cont");
     EmitBranch(Cont);
   }
-  AbortBB = createBasicBlock("abort");
-  EmitBlock(AbortBB);
-  llvm::CallInst *AbortCall = Builder.CreateCall(getAbortFn(*this));
-  AbortCall->setDoesNotReturn();
-  AbortCall->setDoesNotThrow();
+  TrapBB = createBasicBlock("trap");
+  EmitBlock(TrapBB);
+
+  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
+  llvm::CallInst *TrapCall = Builder.CreateCall(F);
+  TrapCall->setDoesNotReturn();
+  TrapCall->setDoesNotThrow();
   Builder.CreateUnreachable();
 
   if (Cont)
     EmitBlock(Cont);
-  return AbortBB;
+  return TrapBB;
 }
 
 LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
@@ -1080,7 +1074,7 @@
             llvm::BasicBlock *Cont = createBasicBlock("cont");
             Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
                                   llvm::ConstantInt::get(Idx->getType(), Size)),
-                                 Cont, getAbortBB());
+                                 Cont, getTrapBB());
             EmitBlock(Cont);
           }
         }
diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp
index a25e369..2f31c05 100644
--- a/lib/CodeGen/CGExprScalar.cpp
+++ b/lib/CodeGen/CGExprScalar.cpp
@@ -1532,7 +1532,7 @@
     llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
     CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS,
                                  llvm::ConstantInt::get(RHS->getType(), Width)),
-                             Cont, CGF.getAbortBB());
+                             Cont, CGF.getTrapBB());
     CGF.EmitBlock(Cont);
   }
 
@@ -1552,7 +1552,7 @@
     llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
     CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS,
                                  llvm::ConstantInt::get(RHS->getType(), Width)),
-                             Cont, CGF.getAbortBB());
+                             Cont, CGF.getTrapBB());
     CGF.EmitBlock(Cont);
   }
 
diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp
index c3bae07..28df9e4 100644
--- a/lib/CodeGen/CodeGenFunction.cpp
+++ b/lib/CodeGen/CodeGenFunction.cpp
@@ -31,7 +31,7 @@
     DebugInfo(0), IndirectBranch(0),
     SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0),
     CXXThisDecl(0), CXXVTTDecl(0),
-    ConditionalBranchLevel(0), TerminateHandler(0), AbortBB(0),
+    ConditionalBranchLevel(0), TerminateHandler(0), TrapBB(0),
     UniqueAggrDestructorCount(0) {
   LLVMIntTy = ConvertType(getContext().IntTy);
   LLVMPointerWidth = Target.getPointerWidth(0);
diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h
index a44e53b..12e636c8 100644
--- a/lib/CodeGen/CodeGenFunction.h
+++ b/lib/CodeGen/CodeGenFunction.h
@@ -425,7 +425,7 @@
   unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
 
   llvm::BasicBlock *TerminateHandler;
-  llvm::BasicBlock *AbortBB;
+  llvm::BasicBlock *TrapBB;
 
   int UniqueAggrDestructorCount;
 public:
@@ -1196,9 +1196,9 @@
   void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
                             llvm::BasicBlock *FalseBlock);
 
-  /// getAbortBB - Create a basic block that will call abort.  We'll generate
-  /// a branch around the created basic block as necessary.
-  llvm::BasicBlock* getAbortBB();
+  /// getTrapBB - Create a basic block that will call the trap intrinsic.  We'll
+  /// generate a branch around the created basic block as necessary.
+  llvm::BasicBlock* getTrapBB();
 private:
 
   void EmitReturnOfRValue(RValue RV, QualType Ty);