do null checks for a few more Emit*() functions.

Thanks Eli for noticing.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160787 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Nuno Lopes
2012-07-26 17:10:46 +00:00
parent 20b2d21509
commit cd31fc7986

View File

@@ -157,14 +157,15 @@ struct StrCatOpt : public LibCallOptimization {
// These optimizations require TargetData. // These optimizations require TargetData.
if (!TD) return 0; if (!TD) return 0;
EmitStrLenMemCpy(Src, Dst, Len, B); return EmitStrLenMemCpy(Src, Dst, Len, B);
return Dst;
} }
void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) { Value *EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
// We need to find the end of the destination string. That's where the // We need to find the end of the destination string. That's where the
// memory is to be moved to. We just generate a call to strlen. // memory is to be moved to. We just generate a call to strlen.
Value *DstLen = EmitStrLen(Dst, B, TD, TLI); Value *DstLen = EmitStrLen(Dst, B, TD, TLI);
if (!DstLen)
return 0;
// Now that we have the destination's length, we must index into the // Now that we have the destination's length, we must index into the
// destination's pointer to get the actual memcpy destination (end of // destination's pointer to get the actual memcpy destination (end of
@@ -175,6 +176,7 @@ struct StrCatOpt : public LibCallOptimization {
// concatenation for us. Make a memcpy to copy the nul byte with align = 1. // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
B.CreateMemCpy(CpyDst, Src, B.CreateMemCpy(CpyDst, Src,
ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1); ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1);
return Dst;
} }
}; };
@@ -221,8 +223,7 @@ struct StrNCatOpt : public StrCatOpt {
// strncat(x, s, c) -> strcat(x, s) // strncat(x, s, c) -> strcat(x, s)
// s is constant so the strcat can be optimized further // s is constant so the strcat can be optimized further
EmitStrLenMemCpy(Src, Dst, SrcLen, B); return EmitStrLenMemCpy(Src, Dst, SrcLen, B);
return Dst;
} }
}; };
@@ -447,11 +448,10 @@ struct StrCpyOpt : public LibCallOptimization {
// We have enough information to now generate the memcpy call to do the // We have enough information to now generate the memcpy call to do the
// concatenation for us. Make a memcpy to copy the nul byte with align = 1. // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
if (OptChkCall) if (!OptChkCall ||
EmitMemCpyChk(Dst, Src, !EmitMemCpyChk(Dst, Src,
ConstantInt::get(TD->getIntPtrType(*Context), Len), ConstantInt::get(TD->getIntPtrType(*Context), Len),
CI->getArgOperand(2), B, TD, TLI); CI->getArgOperand(2), B, TD, TLI))
else
B.CreateMemCpy(Dst, Src, B.CreateMemCpy(Dst, Src,
ConstantInt::get(TD->getIntPtrType(*Context), Len), 1); ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
return Dst; return Dst;
@@ -496,9 +496,8 @@ struct StpCpyOpt: public LibCallOptimization {
// We have enough information to now generate the memcpy call to do the // We have enough information to now generate the memcpy call to do the
// copy for us. Make a memcpy to copy the nul byte with align = 1. // copy for us. Make a memcpy to copy the nul byte with align = 1.
if (OptChkCall) if (!OptChkCall || !EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B,
EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, TD, TLI); TD, TLI))
else
B.CreateMemCpy(Dst, Src, LenV, 1); B.CreateMemCpy(Dst, Src, LenV, 1);
return DstEnd; return DstEnd;
} }
@@ -1187,7 +1186,7 @@ struct PrintFOpt : public LibCallOptimization {
// printf("x") -> putchar('x'), even for '%'. // printf("x") -> putchar('x'), even for '%'.
if (FormatStr.size() == 1) { if (FormatStr.size() == 1) {
Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI); Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI);
if (CI->use_empty()) return CI; if (CI->use_empty() || !Res) return Res;
return B.CreateIntCast(Res, CI->getType(), true); return B.CreateIntCast(Res, CI->getType(), true);
} }
@@ -1210,7 +1209,7 @@ struct PrintFOpt : public LibCallOptimization {
CI->getArgOperand(1)->getType()->isIntegerTy()) { CI->getArgOperand(1)->getType()->isIntegerTy()) {
Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI); Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI);
if (CI->use_empty()) return CI; if (CI->use_empty() || !Res) return Res;
return B.CreateIntCast(Res, CI->getType(), true); return B.CreateIntCast(Res, CI->getType(), true);
} }
@@ -1504,8 +1503,7 @@ struct PutsOpt : public LibCallOptimization {
if (Str.empty() && CI->use_empty()) { if (Str.empty() && CI->use_empty()) {
// puts("") -> putchar('\n') // puts("") -> putchar('\n')
Value *Res = EmitPutChar(B.getInt32('\n'), B, TD, TLI); Value *Res = EmitPutChar(B.getInt32('\n'), B, TD, TLI);
if (!Res) return 0; if (CI->use_empty() || !Res) return Res;
if (CI->use_empty()) return CI;
return B.CreateIntCast(Res, CI->getType(), true); return B.CreateIntCast(Res, CI->getType(), true);
} }