diff options
author | Chris Lattner <sabre@nondot.org> | 2010-06-26 22:18:28 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2010-06-26 22:18:28 +0000 |
commit | 640d3267228415328c0feb2bcc35757230ab4db7 (patch) | |
tree | f0c09c658ded6761213023fd91baf3dee72f62b6 /lib/CodeGen/CGExprScalar.cpp | |
parent | 8c11a65c18ae607b7073e1e451264492d2297726 (diff) |
fix inc/dec to honor -fwrapv and -ftrapv, implementing PR7426.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@106962 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CGExprScalar.cpp')
-rw-r--r-- | lib/CodeGen/CGExprScalar.cpp | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index 63d14f0e53..208e1a6c89 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -1179,11 +1179,27 @@ EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, } else if (isa<llvm::IntegerType>(InVal->getType())) { NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); - // Signed integer overflow is undefined behavior. - if (ValTy->isSignedIntegerType()) - NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); - else + if (!ValTy->isSignedIntegerType()) + // Unsigned integer inc is always two's complement. NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); + else { + switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { + case LangOptions::SOB_Undefined: + NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); + break; + case LangOptions::SOB_Defined: + NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); + break; + case LangOptions::SOB_Trapping: + BinOpInfo BinOp; + BinOp.LHS = InVal; + BinOp.RHS = NextVal; + BinOp.Ty = E->getType(); + BinOp.Opcode = BinaryOperator::Add; + BinOp.E = E; + return EmitOverflowCheckedBinOp(BinOp); + } + } } else { // Add the inc/dec to the real part. if (InVal->getType()->isFloatTy()) |