move a massive amount of code out into its own helper function

to reduce nesting.  This needs to be turned into a table.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126366 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2011-02-24 07:12:12 +00:00
parent 091b1e3c74
commit e265ad8678

View File

@@ -1402,6 +1402,7 @@ namespace {
void setDoesNotAlias(Function &F, unsigned n); void setDoesNotAlias(Function &F, unsigned n);
bool doInitialization(Module &M); bool doInitialization(Module &M);
void inferPrototypeAttributes(Function &F);
virtual void getAnalysisUsage(AnalysisUsage &AU) const { virtual void getAnalysisUsage(AnalysisUsage &AU) const {
} }
}; };
@@ -1597,27 +1598,16 @@ void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
} }
} }
/// doInitialization - Add attributes to well-known functions.
///
bool SimplifyLibCalls::doInitialization(Module &M) {
Modified = false;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Function &F = *I;
if (!F.isDeclaration())
continue;
if (!F.hasName())
continue;
void SimplifyLibCalls::inferPrototypeAttributes(Function &F) {
const FunctionType *FTy = F.getFunctionType(); const FunctionType *FTy = F.getFunctionType();
StringRef Name = F.getName(); StringRef Name = F.getName();
switch (Name[0]) { switch (Name[0]) {
case 's': case 's':
if (Name == "strlen") { if (Name == "strlen") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setOnlyReadsMemory(F); setOnlyReadsMemory(F);
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
@@ -1626,7 +1616,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isIntegerTy()) !FTy->getParamType(1)->isIntegerTy())
continue; return;
setOnlyReadsMemory(F); setOnlyReadsMemory(F);
setDoesNotThrow(F); setDoesNotThrow(F);
} else if (Name == "strcpy" || } else if (Name == "strcpy" ||
@@ -1643,14 +1633,14 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
Name == "strtoull") { Name == "strtoull") {
if (FTy->getNumParams() < 2 || if (FTy->getNumParams() < 2 ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "strxfrm") { } else if (Name == "strxfrm") {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
@@ -1664,40 +1654,36 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() < 2 || if (FTy->getNumParams() < 2 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setOnlyReadsMemory(F); setOnlyReadsMemory(F);
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "strstr" || } else if (Name == "strstr" ||
Name == "strpbrk") { Name == "strpbrk") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
setOnlyReadsMemory(F); setOnlyReadsMemory(F);
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "strtok" || } else if (Name == "strtok" ||
Name == "strtok_r") { Name == "strtok_r") {
if (FTy->getNumParams() < 2 || if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "scanf" || } else if (Name == "scanf" ||
Name == "setbuf" || Name == "setbuf" ||
Name == "setvbuf") { Name == "setvbuf") {
if (FTy->getNumParams() < 1 || if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "strdup" || } else if (Name == "strdup" ||
Name == "strndup") { Name == "strndup") {
if (FTy->getNumParams() < 1 || if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
!FTy->getReturnType()->isPointerTy() ||
!FTy->getParamType(0)->isPointerTy()) !FTy->getParamType(0)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
@@ -1708,7 +1694,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() < 2 || if (FTy->getNumParams() < 2 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
@@ -1716,7 +1702,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(2)->isPointerTy()) !FTy->getParamType(2)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 3); setDoesNotCapture(F, 3);
@@ -1724,14 +1710,14 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(1)->isPointerTy() || !FTy->getParamType(1)->isPointerTy() ||
!FTy->getParamType(2)->isPointerTy()) !FTy->getParamType(2)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
setDoesNotCapture(F, 3); setDoesNotCapture(F, 3);
} else if (Name == "system") { } else if (Name == "system") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 ||
!FTy->getParamType(0)->isPointerTy()) !FTy->getParamType(0)->isPointerTy())
continue; return;
// May throw; "system" is a valid pthread cancellation point. // May throw; "system" is a valid pthread cancellation point.
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} }
@@ -1740,14 +1726,14 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (Name == "malloc") { if (Name == "malloc") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 ||
!FTy->getReturnType()->isPointerTy()) !FTy->getReturnType()->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
} else if (Name == "memcmp") { } else if (Name == "memcmp") {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setOnlyReadsMemory(F); setOnlyReadsMemory(F);
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
@@ -1755,7 +1741,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
} else if (Name == "memchr" || } else if (Name == "memchr" ||
Name == "memrchr") { Name == "memrchr") {
if (FTy->getNumParams() != 3) if (FTy->getNumParams() != 3)
continue; return;
setOnlyReadsMemory(F); setOnlyReadsMemory(F);
setDoesNotThrow(F); setDoesNotThrow(F);
} else if (Name == "modf" || } else if (Name == "modf" ||
@@ -1766,18 +1752,18 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
Name == "memmove") { Name == "memmove") {
if (FTy->getNumParams() < 2 || if (FTy->getNumParams() < 2 ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "memalign") { } else if (Name == "memalign") {
if (!FTy->getReturnType()->isPointerTy()) if (!FTy->getReturnType()->isPointerTy())
continue; return;
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
} else if (Name == "mkdir" || } else if (Name == "mkdir" ||
Name == "mktime") { Name == "mktime") {
if (FTy->getNumParams() == 0 || if (FTy->getNumParams() == 0 ||
!FTy->getParamType(0)->isPointerTy()) !FTy->getParamType(0)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} }
@@ -1787,14 +1773,14 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getReturnType()->isPointerTy()) !FTy->getReturnType()->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "read") { } else if (Name == "read") {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
// May throw; "read" is a valid pthread cancellation point. // May throw; "read" is a valid pthread cancellation point.
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "rmdir" || } else if (Name == "rmdir" ||
@@ -1803,7 +1789,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
Name == "realpath") { Name == "realpath") {
if (FTy->getNumParams() < 1 || if (FTy->getNumParams() < 1 ||
!FTy->getParamType(0)->isPointerTy()) !FTy->getParamType(0)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "rename" || } else if (Name == "rename" ||
@@ -1811,7 +1797,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() < 2 || if (FTy->getNumParams() < 2 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
@@ -1819,9 +1805,8 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
break; break;
case 'w': case 'w':
if (Name == "write") { if (Name == "write") {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
// May throw; "write" is a valid pthread cancellation point. // May throw; "write" is a valid pthread cancellation point.
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} }
@@ -1831,7 +1816,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
@@ -1839,15 +1824,14 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setOnlyReadsMemory(F); setOnlyReadsMemory(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "bzero") { } else if (Name == "bzero") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} }
@@ -1856,7 +1840,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (Name == "calloc") { if (Name == "calloc") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 ||
!FTy->getReturnType()->isPointerTy()) !FTy->getReturnType()->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
} else if (Name == "chmod" || } else if (Name == "chmod" ||
@@ -1864,9 +1848,8 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
Name == "ctermid" || Name == "ctermid" ||
Name == "clearerr" || Name == "clearerr" ||
Name == "closedir") { Name == "closedir") {
if (FTy->getNumParams() == 0 || if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} }
@@ -1876,16 +1859,14 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
Name == "atol" || Name == "atol" ||
Name == "atof" || Name == "atof" ||
Name == "atoll") { Name == "atoll") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setOnlyReadsMemory(F); setOnlyReadsMemory(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "access") { } else if (Name == "access") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} }
@@ -1896,7 +1877,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
!FTy->getReturnType()->isPointerTy() || !FTy->getReturnType()->isPointerTy() ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
@@ -1905,7 +1886,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 ||
!FTy->getReturnType()->isPointerTy() || !FTy->getReturnType()->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
@@ -1923,15 +1904,13 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
Name == "flockfile" || Name == "flockfile" ||
Name == "funlockfile" || Name == "funlockfile" ||
Name == "ftrylockfile") { Name == "ftrylockfile") {
if (FTy->getNumParams() == 0 || if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "ferror") { } else if (Name == "ferror") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setOnlyReadsMemory(F); setOnlyReadsMemory(F);
@@ -1941,16 +1920,15 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
Name == "frexpf" || Name == "frexpf" ||
Name == "frexpl" || Name == "frexpl" ||
Name == "fstatvfs") { Name == "fstatvfs") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "fgets") { } else if (Name == "fgets") {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(2)->isPointerTy()) !FTy->getParamType(2)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 3); setDoesNotCapture(F, 3);
} else if (Name == "fread" || } else if (Name == "fread" ||
@@ -1958,7 +1936,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 4 || if (FTy->getNumParams() != 4 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(3)->isPointerTy()) !FTy->getParamType(3)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 4); setDoesNotCapture(F, 4);
@@ -1969,7 +1947,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() < 2 || if (FTy->getNumParams() < 2 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
@@ -1979,15 +1957,13 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (Name == "getc" || if (Name == "getc" ||
Name == "getlogin_r" || Name == "getlogin_r" ||
Name == "getc_unlocked") { Name == "getc_unlocked") {
if (FTy->getNumParams() == 0 || if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "getenv") { } else if (Name == "getenv") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setOnlyReadsMemory(F); setOnlyReadsMemory(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
@@ -1995,32 +1971,28 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
Name == "getchar") { Name == "getchar") {
setDoesNotThrow(F); setDoesNotThrow(F);
} else if (Name == "getitimer") { } else if (Name == "getitimer") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "getpwnam") { } else if (Name == "getpwnam") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} }
break; break;
case 'u': case 'u':
if (Name == "ungetc") { if (Name == "ungetc") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "uname" || } else if (Name == "uname" ||
Name == "unlink" || Name == "unlink" ||
Name == "unsetenv") { Name == "unsetenv") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "utime" || } else if (Name == "utime" ||
@@ -2028,7 +2000,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
@@ -2036,24 +2008,21 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
break; break;
case 'p': case 'p':
if (Name == "putc") { if (Name == "putc") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "puts" || } else if (Name == "puts" ||
Name == "printf" || Name == "printf" ||
Name == "perror") { Name == "perror") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "pread" || } else if (Name == "pread" ||
Name == "pwrite") { Name == "pwrite") {
if (FTy->getNumParams() != 4 || if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
// May throw; these are valid pthread cancellation points. // May throw; these are valid pthread cancellation points.
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "putchar") { } else if (Name == "putchar") {
@@ -2063,24 +2032,22 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
!FTy->getReturnType()->isPointerTy() || !FTy->getReturnType()->isPointerTy() ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "pclose") { } else if (Name == "pclose") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} }
break; break;
case 'v': case 'v':
if (Name == "vscanf") { if (Name == "vscanf") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "vsscanf" || } else if (Name == "vsscanf" ||
@@ -2088,19 +2055,18 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(1)->isPointerTy() || !FTy->getParamType(1)->isPointerTy() ||
!FTy->getParamType(2)->isPointerTy()) !FTy->getParamType(2)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "valloc") { } else if (Name == "valloc") {
if (!FTy->getReturnType()->isPointerTy()) if (!FTy->getReturnType()->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
} else if (Name == "vprintf") { } else if (Name == "vprintf") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "vfprintf" || } else if (Name == "vfprintf" ||
@@ -2108,7 +2074,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
@@ -2116,7 +2082,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 4 || if (FTy->getNumParams() != 4 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(2)->isPointerTy()) !FTy->getParamType(2)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 3); setDoesNotCapture(F, 3);
@@ -2124,16 +2090,15 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
break; break;
case 'o': case 'o':
if (Name == "open") { if (Name == "open") {
if (FTy->getNumParams() < 2 || if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
// May throw; "open" is a valid pthread cancellation point. // May throw; "open" is a valid pthread cancellation point.
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "opendir") { } else if (Name == "opendir") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 ||
!FTy->getReturnType()->isPointerTy() || !FTy->getReturnType()->isPointerTy() ||
!FTy->getParamType(0)->isPointerTy()) !FTy->getParamType(0)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
@@ -2142,13 +2107,12 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
case 't': case 't':
if (Name == "tmpfile") { if (Name == "tmpfile") {
if (!FTy->getReturnType()->isPointerTy()) if (!FTy->getReturnType()->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
} else if (Name == "times") { } else if (Name == "times") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} }
@@ -2172,23 +2136,21 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "lchown") { } else if (Name == "lchown") {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} }
break; break;
case 'q': case 'q':
if (Name == "qsort") { if (Name == "qsort") {
if (FTy->getNumParams() != 4 || if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
!FTy->getParamType(3)->isPointerTy()) return;
continue;
// May throw; places call through function pointer. // May throw; places call through function pointer.
setDoesNotCapture(F, 4); setDoesNotCapture(F, 4);
} }
@@ -2199,26 +2161,24 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() < 1 || if (FTy->getNumParams() < 1 ||
!FTy->getReturnType()->isPointerTy() || !FTy->getReturnType()->isPointerTy() ||
!FTy->getParamType(0)->isPointerTy()) !FTy->getParamType(0)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "__strtok_r") { } else if (Name == "__strtok_r") {
if (FTy->getNumParams() != 3 || if (FTy->getNumParams() != 3 ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "_IO_getc") { } else if (Name == "_IO_getc") {
if (FTy->getNumParams() != 1 || if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "_IO_putc") { } else if (Name == "_IO_putc") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} }
@@ -2227,7 +2187,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (Name == "\1__isoc99_scanf") { if (Name == "\1__isoc99_scanf") {
if (FTy->getNumParams() < 1 || if (FTy->getNumParams() < 1 ||
!FTy->getParamType(0)->isPointerTy()) !FTy->getParamType(0)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "\1stat64" || } else if (Name == "\1stat64" ||
@@ -2237,7 +2197,7 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
if (FTy->getNumParams() < 1 || if (FTy->getNumParams() < 1 ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
@@ -2246,40 +2206,47 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
!FTy->getReturnType()->isPointerTy() || !FTy->getReturnType()->isPointerTy() ||
!FTy->getParamType(0)->isPointerTy() || !FTy->getParamType(0)->isPointerTy() ||
!FTy->getParamType(1)->isPointerTy()) !FTy->getParamType(1)->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "\1fseeko64" || } else if (Name == "\1fseeko64" ||
Name == "\1ftello64") { Name == "\1ftello64") {
if (FTy->getNumParams() == 0 || if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} else if (Name == "\1tmpfile64") { } else if (Name == "\1tmpfile64") {
if (!FTy->getReturnType()->isPointerTy()) if (!FTy->getReturnType()->isPointerTy())
continue; return;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotAlias(F, 0); setDoesNotAlias(F, 0);
} else if (Name == "\1fstat64" || } else if (Name == "\1fstat64" ||
Name == "\1fstatvfs64") { Name == "\1fstatvfs64") {
if (FTy->getNumParams() != 2 || if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
!FTy->getParamType(1)->isPointerTy()) return;
continue;
setDoesNotThrow(F); setDoesNotThrow(F);
setDoesNotCapture(F, 2); setDoesNotCapture(F, 2);
} else if (Name == "\1open64") { } else if (Name == "\1open64") {
if (FTy->getNumParams() < 2 || if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
!FTy->getParamType(0)->isPointerTy()) return;
continue;
// May throw; "open" is a valid pthread cancellation point. // May throw; "open" is a valid pthread cancellation point.
setDoesNotCapture(F, 1); setDoesNotCapture(F, 1);
} }
break; break;
} }
} }
/// doInitialization - Add attributes to well-known functions.
///
bool SimplifyLibCalls::doInitialization(Module &M) {
Modified = false;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Function &F = *I;
if (F.isDeclaration() && F.hasName())
inferPrototypeAttributes(F);
}
return Modified; return Modified;
} }