]> granicus.if.org Git - clang/commitdiff
Add support for detecting undefined shift behavior. WIP.
authorMike Stump <mrs@apple.com>
Mon, 14 Dec 2009 21:58:14 +0000 (21:58 +0000)
committerMike Stump <mrs@apple.com>
Mon, 14 Dec 2009 21:58:14 +0000 (21:58 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@91341 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprScalar.cpp
lib/CodeGen/CodeGenFunction.cpp
lib/CodeGen/CodeGenFunction.h

index afb7a798ef7bcd7416c08ae8e0da772e21804528..a25e369766548a8cddc4d88cd016a86c2da2c285 100644 (file)
@@ -1526,6 +1526,16 @@ Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
   if (Ops.LHS->getType() != RHS->getType())
     RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
 
+  if (CGF.CatchUndefined 
+      && isa<llvm::IntegerType>(Ops.LHS->getType())) {
+    unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth();
+    llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
+    CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS,
+                                 llvm::ConstantInt::get(RHS->getType(), Width)),
+                             Cont, CGF.getAbortBB());
+    CGF.EmitBlock(Cont);
+  }
+
   return Builder.CreateShl(Ops.LHS, RHS, "shl");
 }
 
@@ -1536,6 +1546,16 @@ Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
   if (Ops.LHS->getType() != RHS->getType())
     RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
 
+  if (CGF.CatchUndefined 
+      && isa<llvm::IntegerType>(Ops.LHS->getType())) {
+    unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth();
+    llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
+    CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS,
+                                 llvm::ConstantInt::get(RHS->getType(), Width)),
+                             Cont, CGF.getAbortBB());
+    CGF.EmitBlock(Cont);
+  }
+
   if (Ops.Ty->isUnsignedIntegerType())
     return Builder.CreateLShr(Ops.LHS, RHS, "shr");
   return Builder.CreateAShr(Ops.LHS, RHS, "shr");
index db604f63b18ab52f13f7206b3754ef4fd3552a6d..c3bae0768c31ab6de49bafcc7f142e0239ea5e70 100644 (file)
@@ -31,8 +31,8 @@ CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
     DebugInfo(0), IndirectBranch(0),
     SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0),
     CXXThisDecl(0), CXXVTTDecl(0),
-    ConditionalBranchLevel(0), TerminateHandler(0),
-    UniqueAggrDestructorCount(0), AbortBB(0) {
+    ConditionalBranchLevel(0), TerminateHandler(0), AbortBB(0),
+    UniqueAggrDestructorCount(0) {
   LLVMIntTy = ConvertType(getContext().IntTy);
   LLVMPointerWidth = Target.getPointerWidth(0);
   Exceptions = getContext().getLangOptions().Exceptions;
index 8820e6ec8b793e0352d049006ab6af378c985839..a44e53b4e3d2341e90500cab9371253b427b1c70 100644 (file)
@@ -425,6 +425,7 @@ private:
   unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
 
   llvm::BasicBlock *TerminateHandler;
+  llvm::BasicBlock *AbortBB;
 
   int UniqueAggrDestructorCount;
 public:
@@ -1194,6 +1195,10 @@ public:
   /// try to simplify the codegen of the conditional based on the branch.
   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();
 private:
 
   void EmitReturnOfRValue(RValue RV, QualType Ty);
@@ -1267,11 +1272,6 @@ private:
                                     ArgType));
     }
   }
-
-  llvm::BasicBlock *AbortBB;
-  /// getAbortBB - Create a basic block that will call abort.  We'll generate
-  /// a branch around the created basic block as necessary.
-  llvm::BasicBlock* getAbortBB();
 };