diff options
author | Chris Lattner <sabre@nondot.org> | 2010-07-20 21:07:09 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2010-07-20 21:07:09 +0000 |
commit | f2f387018903c5f0c8ec0c655392454be83f43ed (patch) | |
tree | 50bafd9ad6d2995d1362181680b87fe018ca6027 /clang/lib/CodeGen/CGExprCXX.cpp | |
parent | bb610d90535bddf87dc4d9b90c695e9c0add69d3 (diff) | |
download | bcm5719-llvm-f2f387018903c5f0c8ec0c655392454be83f43ed.tar.gz bcm5719-llvm-f2f387018903c5f0c8ec0c655392454be83f43ed.zip |
Follow the implementation approach suggested by PR6687,
which generates more efficient and more obviously conformant
code. We now test for overflow of the multiply then force
the result to -1 if so. On X86, this generates nice code
like this:
__Z4testl: ## @_Z4testl
## BB#0: ## %entry
subl $12, %esp
movl $4, %eax
mull 16(%esp)
testl %edx, %edx
movl $-1, %ecx
cmovel %eax, %ecx
movl %ecx, (%esp)
call __Znam
addl $12, %esp
ret
llvm-svn: 108927
Diffstat (limited to 'clang/lib/CodeGen/CGExprCXX.cpp')
-rw-r--r-- | clang/lib/CodeGen/CGExprCXX.cpp | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp index cc34b3d75c6..d3e7919224a 100644 --- a/clang/lib/CodeGen/CGExprCXX.cpp +++ b/clang/lib/CodeGen/CGExprCXX.cpp @@ -440,7 +440,7 @@ static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context, // new double[n] // where n is 2^30 on a 32-bit machine or 2^62 on a 64-bit machine. Because // of this, we need to detect the overflow and ensure that an exception is - // called by invoking std::__throw_length_error. + // called by forcing the size to -1 on overflow. llvm::Value *UMulF = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, &SizeTy, 1); llvm::Value *MulRes = CGF.Builder.CreateCall2(UMulF, NumElements, @@ -448,15 +448,26 @@ static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context, TypeSize.getQuantity())); // Branch on the overflow bit to the overflow block, which is lazily created. llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(MulRes, 1); + // Get the normal result of the multiplication. + llvm::Value *V = CGF.Builder.CreateExtractValue(MulRes, 0); llvm::BasicBlock *NormalBB = CGF.createBasicBlock("no_overflow"); + llvm::BasicBlock *OverflowBB = CGF.createBasicBlock("overflow"); - CGF.Builder.CreateCondBr(DidOverflow, CGF.getThrowLengthErrorBB(), NormalBB); + CGF.Builder.CreateCondBr(DidOverflow, OverflowBB, NormalBB); + + llvm::BasicBlock *PrevBB = CGF.Builder.GetInsertBlock(); + + // We just need the overflow block to build a PHI node. + CGF.EmitBlock(OverflowBB); CGF.EmitBlock(NormalBB); - // Get the normal result of the multiplication. - llvm::Value *V = CGF.Builder.CreateExtractValue(MulRes, 0); - + llvm::PHINode *PN = CGF.Builder.CreatePHI(V->getType()); + + PN->addIncoming(V, PrevBB); + PN->addIncoming(llvm::Constant::getAllOnesValue(V->getType()), OverflowBB); + V = PN; + // And add the cookie padding if necessary. if (!CookiePadding.isZero()) V = CGF.Builder.CreateAdd(V, |