virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
=0;
- Value *OptimizeCall(CallInst *CI, const TargetData &TD, IRBuilder<> &B) {
+ Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Caller = CI->getParent()->getParent();
- this->TD = &TD;
+ this->TD = TD;
if (CI->getCalledFunction())
Context = &CI->getCalledFunction()->getContext();
return CallOptimizer(CI->getCalledFunction(), CI, B);
// Handle the simple, do-nothing case: strcat(x, "") -> x
if (Len == 0)
return Dst;
-
+
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
EmitStrLenMemCpy(Src, Dst, Len, B);
return Dst;
}
// strncat(x, c, 0) -> x
if (SrcLen == 0 || Len == 0) return Dst;
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
// We don't optimize this case
if (Len < SrcLen) return 0;
// of the input string and turn this into memchr.
ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
if (CharC == 0) {
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
uint64_t Len = GetStringLength(SrcStr);
if (Len == 0 || FT->getParamType(1) != Type::getInt32Ty(*Context)) // memchr needs i32.
return 0;
uint64_t Len1 = GetStringLength(Str1P);
uint64_t Len2 = GetStringLength(Str2P);
if (Len1 && Len2) {
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
return EmitMemCmp(Str1P, Str2P,
ConstantInt::get(TD->getIntPtrType(*Context),
std::min(Len1, Len2)), B);
if (Dst == Src) // strcpy(x,x) -> x
return Src;
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
// See if we can get the length of the input string.
uint64_t Len = GetStringLength(Src);
if (Len == 0) return 0;
if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
// Let strncpy handle the zero padding
if (Len > SrcLen+1) return 0;
struct VISIBILITY_HIDDEN MemCpyOpt : public LibCallOptimization {
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
!isa<PointerType>(FT->getParamType(0)) ||
struct VISIBILITY_HIDDEN MemMoveOpt : public LibCallOptimization {
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
!isa<PointerType>(FT->getParamType(0)) ||
struct VISIBILITY_HIDDEN MemSetOpt : public LibCallOptimization {
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
!isa<PointerType>(FT->getParamType(0)) ||
for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
if (FormatStr[i] == '%')
return 0; // we found a format specifier, bail out.
-
+
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
// sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
ConstantInt::get(TD->getIntPtrType(*Context), FormatStr.size()+1),1,B);
}
if (FormatStr[1] == 's') {
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
// sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
struct VISIBILITY_HIDDEN FPutsOpt : public LibCallOptimization {
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
// Require two pointers. Also, we can't optimize if return value is used.
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
return 0; // We found a format specifier.
-
+
+ // These optimizations require TargetData.
+ if (!TD) return 0;
+
EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(*Context),
FormatStr.size()),
CI->getOperand(1), B);
bool doInitialization(Module &M);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequired<TargetData>();
}
};
char SimplifyLibCalls::ID = 0;
if (Optimizations.empty())
InitOptimizations();
- const TargetData &TD = getAnalysis<TargetData>();
+ const TargetData *TD = getAnalysisIfAvailable<TargetData>();
IRBuilder<> Builder(F.getContext());