diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2010-03-12 09:27:41 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2010-03-12 09:27:41 +0000 |
commit | 7b88a49f3e422f83443e22fac59e3e7739c6b3d6 (patch) | |
tree | b10bc8fcd84b48685c9195aecfd611504f978448 /llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp | |
parent | 661a30993312238863d21d87f3525a2171a7c179 (diff) | |
download | bcm5719-llvm-7b88a49f3e422f83443e22fac59e3e7739c6b3d6.tar.gz bcm5719-llvm-7b88a49f3e422f83443e22fac59e3e7739c6b3d6.zip |
Factor checked library call optimization into a common helper class and use it
to unify the almost identical code in CodeGenPrepare and InstCombineCalls.
llvm-svn: 98338
Diffstat (limited to 'llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp')
-rw-r--r-- | llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp | 115 |
1 files changed, 19 insertions, 96 deletions
diff --git a/llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp b/llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp index c26d74d0857..50c96304db0 100644 --- a/llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp +++ b/llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp @@ -540,9 +540,22 @@ static bool OptimizeCmpExpression(CmpInst *CI) { return MadeChange; } +namespace { +class CodeGenPrepareFortifiedLibCalls : public SimplifyFortifiedLibCalls { +protected: + void replaceCall(Value *With) { + CI->replaceAllUsesWith(With); + CI->eraseFromParent(); + } + bool isFoldable(unsigned SizeCIOp, unsigned, bool) const { + if (ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(SizeCIOp))) + return SizeCI->isAllOnesValue(); + return false; + } +}; +} // end anonymous namespace + bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) { - bool MadeChange = false; - // Lower all uses of llvm.objectsize.* IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI); if (II && II->getIntrinsicID() == Intrinsic::objectsize) { @@ -561,102 +574,12 @@ bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) { const TargetData *TD = TLI ? TLI->getTargetData() : 0; if (!TD) return false; - // Lower all default uses of _chk calls. This is code very similar - // to the code in InstCombineCalls, but here we are only lowering calls + // Lower all default uses of _chk calls. This is very similar + // to what InstCombineCalls does, but here we are only lowering calls // that have the default "don't know" as the objectsize. Anything else // should be left alone. - StringRef Name = CI->getCalledFunction()->getName(); - BasicBlock *BB = CI->getParent(); - IRBuilder<> B(CI->getParent()->getContext()); - - // Set the builder to the instruction after the call. - B.SetInsertPoint(BB, CI); - - if (Name == "__memcpy_chk") { - ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); - if (!SizeCI) - return 0; - if (SizeCI->isAllOnesValue()) { - EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), - 1, B, TD); - CI->replaceAllUsesWith(CI->getOperand(1)); - CI->eraseFromParent(); - return true; - } - return 0; - } - - // Should be similar to memcpy. - if (Name == "__mempcpy_chk") { - return 0; - } - - if (Name == "__memmove_chk") { - ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); - if (!SizeCI) - return 0; - if (SizeCI->isAllOnesValue()) { - EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), - 1, B, TD); - CI->replaceAllUsesWith(CI->getOperand(1)); - CI->eraseFromParent(); - return true; - } - return 0; - } - - if (Name == "__memset_chk") { - ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); - if (!SizeCI) - return 0; - if (SizeCI->isAllOnesValue()) { - Value *Val = B.CreateIntCast(CI->getOperand(2), B.getInt8Ty(), - false); - EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B, TD); - CI->replaceAllUsesWith(CI->getOperand(1)); - CI->eraseFromParent(); - return true; - } - return 0; - } - - if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") { - ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3)); - if (!SizeCI) - return 0; - if (SizeCI->isAllOnesValue()) { - Value *Ret = EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD, - Name.substr(2, 6)); - CI->replaceAllUsesWith(Ret); - CI->eraseFromParent(); - return true; - } - return 0; - } - - if (Name == "__strncpy_chk") { - ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); - if (!SizeCI) - return 0; - if (SizeCI->isAllOnesValue()) { - Value *Ret = EmitStrNCpy(CI->getOperand(1), CI->getOperand(2), - CI->getOperand(3), B, TD); - CI->replaceAllUsesWith(Ret); - CI->eraseFromParent(); - return true; - } - return 0; - } - - if (Name == "__strcat_chk") { - return 0; - } - - if (Name == "__strncat_chk") { - return 0; - } - - return MadeChange; + CodeGenPrepareFortifiedLibCalls Simplifier; + return Simplifier.fold(CI, TD); } //===----------------------------------------------------------------------===// // Memory Optimization |