/// specified pointer. This always returns an integer value of size intptr_t.
Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
Module *M = Caller->getParent();
- Constant *StrLen =M->getOrInsertFunction("strlen", TD->getIntPtrType(),
+ AttributeWithIndex AWI[2];
+ AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
+ AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
+ Attribute::NoUnwind);
+
+ Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
+ TD->getIntPtrType(),
PointerType::getUnqual(Type::Int8Ty),
NULL);
return B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
Value *Len, IRBuilder<> &B) {
Module *M = Caller->getParent();
- Value *MemChr = M->getOrInsertFunction("memchr",
+ AttributeWithIndex AWI;
+ AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
+
+ Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
PointerType::getUnqual(Type::Int8Ty),
PointerType::getUnqual(Type::Int8Ty),
Type::Int32Ty, TD->getIntPtrType(),
Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
Value *Len, IRBuilder<> &B) {
Module *M = Caller->getParent();
- Value *MemCmp = M->getOrInsertFunction("memcmp",
+ AttributeWithIndex AWI[3];
+ AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
+ AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
+ AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
+ Attribute::NoUnwind);
+
+ Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
Type::Int32Ty,
PointerType::getUnqual(Type::Int8Ty),
PointerType::getUnqual(Type::Int8Ty),
/// some pointer.
void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Module *M = Caller->getParent();
- Value *F = M->getOrInsertFunction("puts", Type::Int32Ty,
+ AttributeWithIndex AWI[2];
+ AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
+ AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
+
+ Value *F = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
+ Type::Int32Ty,
PointerType::getUnqual(Type::Int8Ty), NULL);
B.CreateCall(F, CastToCStr(Str, B), "puts");
}
/// an integer and File is a pointer to FILE.
void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
Module *M = Caller->getParent();
- Constant *F = M->getOrInsertFunction("fputc", Type::Int32Ty, Type::Int32Ty,
- File->getType(), NULL);
+ AttributeWithIndex AWI[2];
+ AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
+ AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
+ Constant *F;
+ if (isa<PointerType>(File->getType()))
+ F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2), Type::Int32Ty,
+ Type::Int32Ty, File->getType(), NULL);
+
+ else
+ F = M->getOrInsertFunction("fputc", Type::Int32Ty, Type::Int32Ty,
+ File->getType(), NULL);
Char = B.CreateIntCast(Char, Type::Int32Ty, "chari");
B.CreateCall2(F, Char, File, "fputc");
}
/// pointer and File is a pointer to FILE.
void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Module *M = Caller->getParent();
- Constant *F = M->getOrInsertFunction("fputs", Type::Int32Ty,
- PointerType::getUnqual(Type::Int8Ty),
- File->getType(), NULL);
+ AttributeWithIndex AWI[2];
+ AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
+ AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
+ Constant *F;
+ if (isa<PointerType>(File->getType()))
+ F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 2), Type::Int32Ty,
+ PointerType::getUnqual(Type::Int8Ty),
+ File->getType(), NULL);
+ else
+ F = M->getOrInsertFunction("fputs", Type::Int32Ty,
+ PointerType::getUnqual(Type::Int8Ty),
+ File->getType(), NULL);
B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
}
void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
IRBuilder<> &B) {
Module *M = Caller->getParent();
- Constant *F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
- PointerType::getUnqual(Type::Int8Ty),
- TD->getIntPtrType(), TD->getIntPtrType(),
- File->getType(), NULL);
+ AttributeWithIndex AWI[3];
+ AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
+ AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
+ AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
+ Constant *F;
+ if (isa<PointerType>(File->getType()))
+ F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
+ TD->getIntPtrType(),
+ PointerType::getUnqual(Type::Int8Ty),
+ TD->getIntPtrType(), TD->getIntPtrType(),
+ File->getType(), NULL);
+ else
+ F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
+ PointerType::getUnqual(Type::Int8Ty),
+ TD->getIntPtrType(), TD->getIntPtrType(),
+ File->getType(), NULL);
B.CreateCall4(F, CastToCStr(Ptr, B), Size,
ConstantInt::get(TD->getIntPtrType(), 1), File);
}
SPrintFOpt SPrintF; PrintFOpt PrintF;
FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
- bool Modified; // This is only used by doFinalization.
+ bool Modified; // This is only used by doInitialization.
public:
static char ID; // Pass identification
SimplifyLibCalls() : FunctionPass(&ID) {}
void setDoesNotThrow(Function &F);
void setDoesNotCapture(Function &F, unsigned n);
void setDoesNotAlias(Function &F, unsigned n);
- bool doFinalization(Module &M);
+ bool doInitialization(Module &M);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<TargetData>();
return Changed;
}
-// Utility methods for doFinalization.
+// Utility methods for doInitialization.
void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
if (!F.doesNotAccessMemory()) {
}
}
-/// doFinalization - Add attributes to well-known functions.
+/// doInitialization - Add attributes to well-known functions.
///
-bool SimplifyLibCalls::doFinalization(Module &M) {
+bool SimplifyLibCalls::doInitialization(Module &M) {
Modified = false;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Function &F = *I;
setDoesNotThrow(F);
setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2);
+ } else if ((NameLen == 6 && !strcmp(NameStr, "strdup")) ||
+ (NameLen == 7 && !strcmp(NameStr, "strndup"))) {
+ if (FTy->getNumParams() < 1 ||
+ !isa<PointerType>(FTy->getReturnType()) ||
+ !isa<PointerType>(FTy->getParamType(0)))
+ continue;
+ setDoesNotThrow(F);
+ setDoesNotAlias(F, 0);
+ setDoesNotCapture(F, 1);
}
break;
case 'm':