[cfe-commits] r68221 - in /cfe/trunk: include/clang/Basic/LangOptions.h include/clang/Driver/Options.def lib/CodeGen/CGExprScalar.cpp tools/clang-cc/clang-cc.cpp
Douglas Gregor
dgregor at apple.com
Wed Apr 1 11:58:59 PDT 2009
This patch causes several tests in "make test" to fail:
/Users/dgregor/Projects/llvm/tools/clang/test/CodeGen//2008-08-04-void-
pointer-arithmetic.c failed ----
---- /Users/dgregor/Projects/llvm/tools/clang/test/CodeGen//2008-07-31-
promotion-of-compound-pointer-arithmetic.c failed ----
---- /Users/dgregor/Projects/llvm/tools/clang/test/CodeGen//compound.c
failed ----
---- /Users/dgregor/Projects/llvm/tools/clang/test/CodeGen//exprs.c
failed ----
---- /Users/dgregor/Projects/llvm/tools/clang/test/CodeGen//pointer-
arithmetic.c failed ----
---- /Users/dgregor/Projects/llvm/tools/clang/test/Coverage//codegen.c
failed ----
I'm reverting it until we have a patch that tests cleanly.
- Doug
On Apr 1, 2009, at 11:19 AM, Mike Stump wrote:
> Author: mrs
> Date: Wed Apr 1 13:19:33 2009
> New Revision: 68221
>
> URL: http://llvm.org/viewvc/llvm-project?rev=68221&view=rev
> Log:
> Add -ftrapv support, patch from David Chisnall; well all except the
> clang option code that is.
>
> Modified:
> cfe/trunk/include/clang/Basic/LangOptions.h
> cfe/trunk/include/clang/Driver/Options.def
> cfe/trunk/lib/CodeGen/CGExprScalar.cpp
> cfe/trunk/tools/clang-cc/clang-cc.cpp
>
> Modified: cfe/trunk/include/clang/Basic/LangOptions.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.h?rev=68221&r1=68220&r2=68221&view=diff
>
> =
> =
> =
> =
> =
> =
> =
> =
> ======================================================================
> --- cfe/trunk/include/clang/Basic/LangOptions.h (original)
> +++ cfe/trunk/include/clang/Basic/LangOptions.h Wed Apr 1 13:19:33
> 2009
> @@ -59,6 +59,9 @@
> unsigned MathErrno : 1; // Math functions must respect errno
> // (modulo the platform support).
>
> + unsigned OverflowChecking : 1; // Extension to call a handler
> function when
> + // integer arithmetic overflows.
> +
> unsigned HeinousExtensions : 1; // Extensions that we really don't
> like and
> // may be ripped out at any time.
> private:
> @@ -87,6 +90,8 @@
> EmitAllDecls = 0;
> MathErrno = 1;
>
> + OverflowChecking = 0;
> +
> InstantiationDepth = 99;
> }
>
>
> Modified: cfe/trunk/include/clang/Driver/Options.def
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Options.def?rev=68221&r1=68220&r2=68221&view=diff
>
> =
> =
> =
> =
> =
> =
> =
> =
> ======================================================================
> --- cfe/trunk/include/clang/Driver/Options.def (original)
> +++ cfe/trunk/include/clang/Driver/Options.def Wed Apr 1 13:19:33
> 2009
> @@ -448,6 +448,7 @@
> OPTION("-fterminated-vtables", fterminated_vtables, Flag, f_Group,
> INVALID, "", 0, 0, 0)
> OPTION("-ftime-report", ftime_report, Flag, clang_f_Group, INVALID,
> "", 0, 0, 0)
> OPTION("-ftraditional", ftraditional, Flag, f_Group, INVALID, "", 0,
> 0, 0)
> +OPTION("-ftrapv", ftrapv, Flag, clang_f_Group, INVALID, "", 0, 0, 0)
> OPTION("-funwind-tables", funwind_tables, Flag, f_Group, INVALID,
> "", 0, 0, 0)
> OPTION("-fverbose-asm", fverbose_asm, Flag, f_Group, INVALID, "", 0,
> 0, 0)
> OPTION("-fwritable-strings", fwritable_strings, Flag, clang_f_Group,
> INVALID, "", 0, 0, 0)
>
> Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=68221&r1=68220&r2=68221&view=diff
>
> =
> =
> =
> =
> =
> =
> =
> =
> ======================================================================
> --- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Wed Apr 1 13:19:33 2009
> @@ -22,6 +22,7 @@
> #include "llvm/Function.h"
> #include "llvm/GlobalVariable.h"
> #include "llvm/Intrinsics.h"
> +#include "llvm/Module.h"
> #include "llvm/Support/Compiler.h"
> #include "llvm/Support/CFG.h"
> #include "llvm/Target/TargetData.h"
> @@ -261,8 +262,13 @@
>
> // Binary Operators.
> Value *EmitMul(const BinOpInfo &Ops) {
> + if (CGF.getContext().getLangOptions().OverflowChecking)
> + return EmitOverflowCheckedBinOp(Ops);
> return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
> }
> + /// Create a binary op that checks for overflow.
> + /// Currently only supports +, - and *.
> + Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
> Value *EmitDiv(const BinOpInfo &Ops);
> Value *EmitRem(const BinOpInfo &Ops);
> Value *EmitAdd(const BinOpInfo &Ops);
> @@ -825,9 +831,112 @@
> return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
> }
>
> +Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo
> &Ops) {
> + unsigned IID;
> + unsigned OpID = 0;
> + if (Ops.Ty->isSignedIntegerType()) {
> + switch (Ops.E->getOpcode()) {
> + case BinaryOperator::Add:
> + OpID = 1;
> + IID = llvm::Intrinsic::sadd_with_overflow;
> + break;
> + case BinaryOperator::Sub:
> + OpID = 2;
> + IID = llvm::Intrinsic::ssub_with_overflow;
> + break;
> + case BinaryOperator::Mul:
> + OpID = 3;
> + IID = llvm::Intrinsic::smul_with_overflow;
> + break;
> + default:
> + assert(false && "Unsupported operation for overflow
> detection");
> + }
> + OpID <<= 1;
> + OpID |= 1;
> + }
> + else {
> + assert(Ops.Ty->isUnsignedIntegerType() &&
> + "Must be either a signed or unsigned integer op");
> + switch (Ops.E->getOpcode()) {
> + case BinaryOperator::Add:
> + OpID = 1;
> + IID = llvm::Intrinsic::uadd_with_overflow;
> + break;
> + case BinaryOperator::Sub:
> + OpID = 2;
> + IID = llvm::Intrinsic::usub_with_overflow;
> + break;
> + case BinaryOperator::Mul:
> + OpID = 3;
> + IID = llvm::Intrinsic::umul_with_overflow;
> + break;
> + default:
> + assert(false && "Unsupported operation for overflow
> detection");
> + }
> + OpID <<= 1;
> + }
> + const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
> +
> + llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1);
> +
> + Value *resultAndOverflow = Builder.CreateCall2(intrinsic,
> Ops.LHS, Ops.RHS);
> + Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
> + Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
> +
> + // Branch in case of overflow.
> + llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
> + llvm::BasicBlock *overflowBB =
> + CGF.createBasicBlock("overflow", CGF.CurFn);
> + llvm::BasicBlock *continueBB =
> + CGF.createBasicBlock("overflow.continue", CGF.CurFn);
> +
> + Builder.CreateCondBr(overflow, overflowBB, continueBB);
> +
> + // Handle overflow
> +
> + Builder.SetInsertPoint(overflowBB);
> +
> + // Handler is:
> + // long long *__overflow_handler)(long long a, long long b, char
> op,
> + // char width)
> + std::vector<const llvm::Type*> handerArgTypes;
> + handerArgTypes.push_back(llvm::Type::Int64Ty);
> + handerArgTypes.push_back(llvm::Type::Int64Ty);
> + handerArgTypes.push_back(llvm::Type::Int8Ty);
> + handerArgTypes.push_back(llvm::Type::Int8Ty);
> + llvm::FunctionType *handlerTy = llvm::FunctionType::get
> (llvm::Type::Int64Ty,
> + handerArgTypes, false);
> + llvm::Value *handlerFunction =
> + CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler",
> + llvm::PointerType::getUnqual(handlerTy));
> + handlerFunction = Builder.CreateLoad(handlerFunction);
> +
> + llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction,
> + Builder.CreateSExt(Ops.LHS, llvm::Type::Int64Ty),
> + Builder.CreateSExt(Ops.RHS, llvm::Type::Int64Ty),
> + llvm::ConstantInt::get(llvm::Type::Int8Ty, OpID),
> + llvm::ConstantInt::get(llvm::Type::Int8Ty,
> + cast<llvm::IntegerType>(opTy)->getBitWidth()));
> +
> + handlerResult = Builder.CreateTrunc(handlerResult, opTy);
> +
> + Builder.CreateBr(continueBB);
> +
> + // Set up the continuation
> + Builder.SetInsertPoint(continueBB);
> + // Get the correct result
> + llvm::PHINode *phi = Builder.CreatePHI(opTy);
> + phi->reserveOperandSpace(2);
> + phi->addIncoming(result, initialBB);
> + phi->addIncoming(handlerResult, overflowBB);
> +
> + return phi;
> +}
>
> Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
> if (!Ops.Ty->isPointerType())
> + if (CGF.getContext().getLangOptions().OverflowChecking)
> + return EmitOverflowCheckedBinOp(Ops);
> return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
>
> if (Ops.Ty->getAsPointerType()->isVariableArrayType()) {
> @@ -876,6 +985,8 @@
>
> Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
> if (!isa<llvm::PointerType>(Ops.LHS->getType()))
> + if (CGF.getContext().getLangOptions().OverflowChecking)
> + return EmitOverflowCheckedBinOp(Ops);
> return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
>
> if (Ops.E->getLHS()->getType()->getAsPointerType()-
> >isVariableArrayType()) {
>
> Modified: cfe/trunk/tools/clang-cc/clang-cc.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-cc/clang-cc.cpp?rev=68221&r1=68220&r2=68221&view=diff
>
> =
> =
> =
> =
> =
> =
> =
> =
> ======================================================================
> --- cfe/trunk/tools/clang-cc/clang-cc.cpp (original)
> +++ cfe/trunk/tools/clang-cc/clang-cc.cpp Wed Apr 1 13:19:33 2009
> @@ -675,6 +675,14 @@
> Options.setGCMode(LangOptions::HybridGC);
> }
>
> +static llvm::cl::opt<bool>
> +OverflowChecking("ftrapv",
> + llvm::cl::desc("Trap on integer overflow"),
> + llvm::cl::init(false));
> +
> +void InitializeOverflowChecking(LangOptions &Options) {
> + Options.OverflowChecking = OverflowChecking;
> +}
> //
> =
> =
> =
> ----------------------------------------------------------------------=
> ==//
> // Target Triple Processing.
> //
> =
> =
> =
> ----------------------------------------------------------------------=
> ==//
> @@ -1626,6 +1634,7 @@
> LangKind LK = GetLanguage(InFile);
> InitializeLangOptions(LangInfo, LK);
> InitializeGCMode(LangInfo);
> + InitializeOverflowChecking(LangInfo);
> InitializeLanguageStandard(LangInfo, LK, Target.get());
>
> // Process the -I options and set them in the HeaderInfo.
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
More information about the cfe-commits
mailing list