Skip to content
SimplifyLibCalls.cpp 76.5 KiB
Newer Older
      if (CI->use_empty()) return CI;
      return B.CreateIntCast(Res, CI->getType(), true);
    // printf("%s\n", str) --> puts(str)
Gabor Greif's avatar
Gabor Greif committed
    if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
Gabor Greif's avatar
Gabor Greif committed
        CI->getArgOperand(1)->getType()->isPointerTy() &&
Gabor Greif's avatar
Gabor Greif committed
      EmitPutS(CI->getArgOperand(1), B, TD);
      return CI;
    }
    return 0;
  }
};

//===---------------------------------------===//
// 'sprintf' Optimizations

struct SPrintFOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    // Require two fixed pointer arguments and an integer result.
    const FunctionType *FT = Callee->getFunctionType();
    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
        !FT->getParamType(1)->isPointerTy() ||
        !FT->getReturnType()->isIntegerTy())
      return 0;

    // Check for a fixed format string.
    std::string FormatStr;
Gabor Greif's avatar
Gabor Greif committed
    if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
      return 0;
    // If we just have a format string (nothing else crazy) transform it.
Gabor Greif's avatar
Gabor Greif committed
    if (CI->getNumArgOperands() == 2) {
      // Make sure there's no % in the constant array.  We could try to handle
      // %% -> % in the future if we cared.
      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->getArgOperand(0), CI->getArgOperand(1),   // Copy the
                 ConstantInt::get(TD->getIntPtrType(*Context), // nul byte.
                 FormatStr.size() + 1), 1, false, B, TD);
      return ConstantInt::get(CI->getType(), FormatStr.size());
    // The remaining optimizations require the format string to be "%s" or "%c"
    // and have an extra operand.
Gabor Greif's avatar
Gabor Greif committed
    if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
        CI->getNumArgOperands() < 3)
    // Decode the second character of the format string.
    if (FormatStr[1] == 'c') {
Chris Lattner's avatar
Chris Lattner committed
      // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
Gabor Greif's avatar
Gabor Greif committed
      if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
      Value *V = B.CreateTrunc(CI->getArgOperand(2),
                               Type::getInt8Ty(*Context), "char");
Gabor Greif's avatar
Gabor Greif committed
      Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
Chris Lattner's avatar
Chris Lattner committed
      B.CreateStore(V, Ptr);
      Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
      B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
      return ConstantInt::get(CI->getType(), 1);
      // These optimizations require TargetData.
      if (!TD) return 0;

      // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
Gabor Greif's avatar
Gabor Greif committed
      if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
Gabor Greif's avatar
Gabor Greif committed
      Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
      Value *IncLen = B.CreateAdd(Len,
      EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(2),
                 IncLen, 1, false, B, TD);
      // The sprintf result is the unincremented number of bytes in the string.
      return B.CreateIntCast(Len, CI->getType(), false);
    }
    return 0;
  }
};

//===---------------------------------------===//
// 'fwrite' Optimizations

struct FWriteOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    // Require a pointer, an integer, an integer, a pointer, returning integer.
    const FunctionType *FT = Callee->getFunctionType();
    if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
        !FT->getParamType(1)->isIntegerTy() ||
        !FT->getParamType(2)->isIntegerTy() ||
        !FT->getParamType(3)->isPointerTy() ||
        !FT->getReturnType()->isIntegerTy())
    // Get the element size and count.
Gabor Greif's avatar
Gabor Greif committed
    ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
    ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
    if (!SizeC || !CountC) return 0;
    uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
    // If this is writing zero records, remove the call (it's a noop).
    if (Bytes == 0)
      return ConstantInt::get(CI->getType(), 0);
    // If this is writing one byte, turn it into fputc.
    if (Bytes == 1) {  // fwrite(S,1,1,F) -> fputc(S[0],F)
Gabor Greif's avatar
Gabor Greif committed
      Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
      EmitFPutC(Char, CI->getArgOperand(3), B, TD);
      return ConstantInt::get(CI->getType(), 1);
    }

    return 0;
  }
};

//===---------------------------------------===//
// 'fputs' Optimizations

struct 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 || !FT->getParamType(0)->isPointerTy() ||
        !FT->getParamType(1)->isPointerTy() ||
    // fputs(s,F) --> fwrite(s,1,strlen(s),F)
Gabor Greif's avatar
Gabor Greif committed
    uint64_t Len = GetStringLength(CI->getArgOperand(0));
Chris Lattner's avatar
Chris Lattner committed
    if (!Len) return 0;
Gabor Greif's avatar
Gabor Greif committed
    EmitFWrite(CI->getArgOperand(0),
               ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
Gabor Greif's avatar
Gabor Greif committed
               CI->getArgOperand(1), B, TD);
    return CI;  // Known to have no uses (see above).
  }
};

//===---------------------------------------===//
// 'fprintf' Optimizations

struct FPrintFOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    // Require two fixed paramters as pointers and integer result.
    const FunctionType *FT = Callee->getFunctionType();
    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
        !FT->getParamType(1)->isPointerTy() ||
        !FT->getReturnType()->isIntegerTy())
    // All the optimizations depend on the format string.
    std::string FormatStr;
Gabor Greif's avatar
Gabor Greif committed
    if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
      return 0;

    // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
Gabor Greif's avatar
Gabor Greif committed
    if (CI->getNumArgOperands() == 2) {
      for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
        if (FormatStr[i] == '%')  // Could handle %% -> % if we cared.
Chris Lattner's avatar
Chris Lattner committed
          return 0; // We found a format specifier.

      // These optimizations require TargetData.
      if (!TD) return 0;

Gabor Greif's avatar
Gabor Greif committed
      EmitFWrite(CI->getArgOperand(1),
                 ConstantInt::get(TD->getIntPtrType(*Context),
                                  FormatStr.size()),
Gabor Greif's avatar
Gabor Greif committed
                 CI->getArgOperand(0), B, TD);
      return ConstantInt::get(CI->getType(), FormatStr.size());
    // The remaining optimizations require the format string to be "%s" or "%c"
    // and have an extra operand.
Gabor Greif's avatar
Gabor Greif committed
    if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
        CI->getNumArgOperands() < 3)
    // Decode the second character of the format string.
    if (FormatStr[1] == 'c') {
Gabor Greif's avatar
Gabor Greif committed
      // fprintf(F, "%c", chr) --> fputc(chr, F)
      if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
      EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
      return ConstantInt::get(CI->getType(), 1);
Gabor Greif's avatar
Gabor Greif committed
      // fprintf(F, "%s", str) --> fputs(str, F)
      if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
Gabor Greif's avatar
Gabor Greif committed
      EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);

//===----------------------------------------------------------------------===//
// SimplifyLibCalls Pass Implementation
//===----------------------------------------------------------------------===//

namespace {
  /// This pass optimizes well known library functions from libc and libm.
  ///
  class SimplifyLibCalls : public FunctionPass {
    StringMap<LibCallOptimization*> Optimizations;
    // String and Memory LibCall Optimizations
    StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
    StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
    StrNCpyOpt StrNCpy; StrLenOpt StrLen;
    StrToOpt StrTo; StrStrOpt StrStr;
    MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
    PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
    FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
    ToAsciiOpt ToAscii;
    // Formatting and IO Optimizations
    SPrintFOpt SPrintF; PrintFOpt PrintF;
    FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
    bool Modified;  // This is only used by doInitialization.
  public:
    static char ID; // Pass identification
    SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {}
    void InitOptimizations();
    bool runOnFunction(Function &F);

    void setDoesNotAccessMemory(Function &F);
    void setOnlyReadsMemory(Function &F);
    void setDoesNotThrow(Function &F);
    void setDoesNotCapture(Function &F, unsigned n);
    void setDoesNotAlias(Function &F, unsigned n);
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
    }
  };
  char SimplifyLibCalls::ID = 0;
} // end anonymous namespace.

INITIALIZE_PASS(SimplifyLibCalls, "simplify-libcalls",
                "Simplify well-known library calls", false, false);

// Public interface to the Simplify LibCalls pass.
FunctionPass *llvm::createSimplifyLibCallsPass() {
  return new SimplifyLibCalls();
}

/// Optimizations - Populate the Optimizations map with all the optimizations
/// we know.
void SimplifyLibCalls::InitOptimizations() {
  // String and Memory LibCall Optimizations
  Optimizations["strcat"] = &StrCat;
  Optimizations["strncat"] = &StrNCat;
  Optimizations["strchr"] = &StrChr;
  Optimizations["strcmp"] = &StrCmp;
  Optimizations["strncmp"] = &StrNCmp;
  Optimizations["strcpy"] = &StrCpy;
  Optimizations["strncpy"] = &StrNCpy;
  Optimizations["strlen"] = &StrLen;
  Optimizations["strtol"] = &StrTo;
  Optimizations["strtod"] = &StrTo;
  Optimizations["strtof"] = &StrTo;
  Optimizations["strtoul"] = &StrTo;
  Optimizations["strtoll"] = &StrTo;
  Optimizations["strtold"] = &StrTo;
  Optimizations["strtoull"] = &StrTo;
  Optimizations["strstr"] = &StrStr;
  Optimizations["memcmp"] = &MemCmp;
  Optimizations["memcpy"] = &MemCpy;
  Optimizations["memmove"] = &MemMove;
  Optimizations["memset"] = &MemSet;
  // _chk variants of String and Memory LibCall Optimizations.
  Optimizations["__strcpy_chk"] = &StrCpyChk;

  // Math Library Optimizations
  Optimizations["powf"] = &Pow;
  Optimizations["pow"] = &Pow;
  Optimizations["powl"] = &Pow;
  Optimizations["llvm.pow.f32"] = &Pow;
  Optimizations["llvm.pow.f64"] = &Pow;
  Optimizations["llvm.pow.f80"] = &Pow;
  Optimizations["llvm.pow.f128"] = &Pow;
  Optimizations["llvm.pow.ppcf128"] = &Pow;
  Optimizations["exp2l"] = &Exp2;
  Optimizations["exp2"] = &Exp2;
  Optimizations["exp2f"] = &Exp2;
  Optimizations["llvm.exp2.ppcf128"] = &Exp2;
  Optimizations["llvm.exp2.f128"] = &Exp2;
  Optimizations["llvm.exp2.f80"] = &Exp2;
  Optimizations["llvm.exp2.f64"] = &Exp2;
  Optimizations["llvm.exp2.f32"] = &Exp2;
#ifdef HAVE_FLOORF
  Optimizations["floor"] = &UnaryDoubleFP;
#endif
#ifdef HAVE_CEILF
  Optimizations["ceil"] = &UnaryDoubleFP;
#endif
#ifdef HAVE_ROUNDF
  Optimizations["round"] = &UnaryDoubleFP;
#endif
#ifdef HAVE_RINTF
  Optimizations["rint"] = &UnaryDoubleFP;
#endif
#ifdef HAVE_NEARBYINTF
  Optimizations["nearbyint"] = &UnaryDoubleFP;
#endif
  // Integer Optimizations
  Optimizations["ffs"] = &FFS;
  Optimizations["ffsl"] = &FFS;
  Optimizations["ffsll"] = &FFS;
  Optimizations["abs"] = &Abs;
  Optimizations["labs"] = &Abs;
  Optimizations["llabs"] = &Abs;
  Optimizations["isdigit"] = &IsDigit;
  Optimizations["isascii"] = &IsAscii;
  Optimizations["toascii"] = &ToAscii;
  // Formatting and IO Optimizations
  Optimizations["sprintf"] = &SPrintF;
  Optimizations["printf"] = &PrintF;
  Optimizations["fwrite"] = &FWrite;
  Optimizations["fputs"] = &FPuts;
  Optimizations["fprintf"] = &FPrintF;
}


/// runOnFunction - Top level algorithm.
///
bool SimplifyLibCalls::runOnFunction(Function &F) {
  if (Optimizations.empty())
    InitOptimizations();
  const TargetData *TD = getAnalysisIfAvailable<TargetData>();
  IRBuilder<> Builder(F.getContext());

  bool Changed = false;
  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
      // Ignore non-calls.
      CallInst *CI = dyn_cast<CallInst>(I++);
      if (!CI) continue;
      // Ignore indirect calls and calls to non-external functions.
      Function *Callee = CI->getCalledFunction();
      if (Callee == 0 || !Callee->isDeclaration() ||
          !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
        continue;
Daniel Dunbar's avatar
Daniel Dunbar committed
      LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
      if (!LCO) continue;
      // Set the builder to the instruction after the call.
      Builder.SetInsertPoint(BB, I);
Daniel Dunbar's avatar
Daniel Dunbar committed
      Value *Result = LCO->OptimizeCall(CI, TD, Builder);
David Greene's avatar
David Greene committed
      DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
            dbgs() << "  into: " << *Result << "\n");
      // Something changed!
      Changed = true;
      ++NumSimplified;
      // Inspect the instruction after the call (which was potentially just
      // added) next.
      I = CI; ++I;
      if (CI != Result && !CI->use_empty()) {
        CI->replaceAllUsesWith(Result);
        if (!Result->hasName())
          Result->takeName(CI);
      }
      CI->eraseFromParent();
    }
  }
  return Changed;
}

// Utility methods for doInitialization.

void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
  if (!F.doesNotAccessMemory()) {
    F.setDoesNotAccessMemory();
    ++NumAnnotated;
    Modified = true;
  }
}
void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
  if (!F.onlyReadsMemory()) {
    F.setOnlyReadsMemory();
    ++NumAnnotated;
    Modified = true;
  }
}
void SimplifyLibCalls::setDoesNotThrow(Function &F) {
  if (!F.doesNotThrow()) {
    F.setDoesNotThrow();
    ++NumAnnotated;
    Modified = true;
  }
}
void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
  if (!F.doesNotCapture(n)) {
    F.setDoesNotCapture(n);
    ++NumAnnotated;
    Modified = true;
  }
}
void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
  if (!F.doesNotAlias(n)) {
    F.setDoesNotAlias(n);
    ++NumAnnotated;
    Modified = true;
  }
}

/// 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;

      continue;

    const FunctionType *FTy = F.getFunctionType();

    StringRef Name = F.getName();
    switch (Name[0]) {
        if (Name == "strlen") {
          if (FTy->getNumParams() != 1 ||
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "strchr" ||
                   Name == "strrchr") {
          if (FTy->getNumParams() != 2 ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isIntegerTy())
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
        } else if (Name == "strcpy" ||
                   Name == "stpcpy" ||
                   Name == "strcat" ||
                   Name == "strtol" ||
                   Name == "strtod" ||
                   Name == "strtof" ||
                   Name == "strtoul" ||
                   Name == "strtoll" ||
                   Name == "strtold" ||
                   Name == "strncat" ||
                   Name == "strncpy" ||
                   Name == "strtoull") {
          if (FTy->getNumParams() < 2 ||
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "strxfrm") {
          if (FTy->getNumParams() != 3 ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "strcmp" ||
                   Name == "strspn" ||
                   Name == "strncmp" ||
                   Name == "strcspn" ||
                   Name == "strcoll" ||
                   Name == "strcasecmp" ||
                   Name == "strncasecmp") {
          if (FTy->getNumParams() < 2 ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "strstr" ||
                   Name == "strpbrk") {
          if (FTy->getNumParams() != 2 ||
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "strtok" ||
                   Name == "strtok_r") {
          if (FTy->getNumParams() < 2 ||
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "scanf" ||
                   Name == "setbuf" ||
                   Name == "setvbuf") {
          if (FTy->getNumParams() < 1 ||
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "strdup" ||
                   Name == "strndup") {
              !FTy->getReturnType()->isPointerTy() ||
              !FTy->getParamType(0)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
          setDoesNotCapture(F, 1);
        } else if (Name == "stat" ||
                   Name == "sscanf" ||
                   Name == "sprintf" ||
                   Name == "statvfs") {
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "snprintf") {
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(2)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 3);
        } else if (Name == "setitimer") {
              !FTy->getParamType(1)->isPointerTy() ||
              !FTy->getParamType(2)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
          setDoesNotCapture(F, 3);
        } else if (Name == "system") {
            continue;
          // May throw; "system" is a valid pthread cancellation point.
          setDoesNotCapture(F, 1);
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
        } else if (Name == "memcmp") {
          if (FTy->getNumParams() != 3 ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "memchr" ||
                   Name == "memrchr") {
          if (FTy->getNumParams() != 3)
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
        } else if (Name == "modf" ||
                   Name == "modff" ||
                   Name == "modfl" ||
                   Name == "memcpy" ||
                   Name == "memccpy" ||
                   Name == "memmove") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "memalign") {
          if (!FTy->getReturnType()->isPointerTy())
        } else if (Name == "mkdir" ||
                   Name == "mktime") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        if (Name == "realloc") {
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getReturnType()->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
          setDoesNotCapture(F, 1);
        } else if (Name == "read") {
          if (FTy->getNumParams() != 3 ||
          // May throw; "read" is a valid pthread cancellation point.
        } else if (Name == "rmdir" ||
                   Name == "rewind" ||
                   Name == "remove" ||
                   Name == "realpath") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "rename" ||
                   Name == "readlink") {
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        }
        break;
      case 'w':
          if (FTy->getNumParams() != 3 ||
          // May throw; "write" is a valid pthread cancellation point.
          if (FTy->getNumParams() != 3 ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "bcmp") {
          if (FTy->getNumParams() != 3 ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setOnlyReadsMemory(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "bzero") {
          if (FTy->getNumParams() != 2 ||
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        }
        break;
      case 'c':
        if (Name == "calloc") {
          if (FTy->getNumParams() != 2 ||
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
        } else if (Name == "chmod" ||
                   Name == "chown" ||
                   Name == "ctermid" ||
                   Name == "clearerr" ||
                   Name == "closedir") {
          if (FTy->getNumParams() == 0 ||
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        }
        break;
      case 'a':
        if (Name == "atoi" ||
            Name == "atol" ||
            Name == "atof" ||
            Name == "atoll") {
          if (FTy->getNumParams() != 1 ||
            continue;
          setDoesNotThrow(F);
          setOnlyReadsMemory(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "access") {
          if (FTy->getNumParams() != 2 ||
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        }
        break;
      case 'f':
              !FTy->getReturnType()->isPointerTy() ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "fdopen") {
              !FTy->getReturnType()->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
          setDoesNotCapture(F, 2);
        } else if (Name == "feof" ||
                   Name == "free" ||
                   Name == "fseek" ||
                   Name == "ftell" ||
                   Name == "fgetc" ||
                   Name == "fseeko" ||
                   Name == "ftello" ||
                   Name == "fileno" ||
                   Name == "fflush" ||
                   Name == "fclose" ||
                   Name == "fsetpos" ||
                   Name == "flockfile" ||
                   Name == "funlockfile" ||
                   Name == "ftrylockfile") {
          if (FTy->getNumParams() == 0 ||
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "ferror") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setOnlyReadsMemory(F);
        } else if (Name == "fputc" ||
                   Name == "fstat" ||
                   Name == "frexp" ||
                   Name == "frexpf" ||
                   Name == "frexpl" ||
                   Name == "fstatvfs") {
          if (FTy->getNumParams() != 2 ||
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "fgets") {
          if (FTy->getNumParams() != 3 ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(2)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 3);
        } else if (Name == "fread" ||
                   Name == "fwrite") {
          if (FTy->getNumParams() != 4 ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(3)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 4);
        } else if (Name == "fputs" ||
                   Name == "fscanf" ||
                   Name == "fprintf" ||
                   Name == "fgetpos") {
          if (FTy->getNumParams() < 2 ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        }
        break;
      case 'g':
        if (Name == "getc" ||
            Name == "getlogin_r" ||
            Name == "getc_unlocked") {
          if (FTy->getNumParams() == 0 ||
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "getenv") {
            continue;
          setDoesNotThrow(F);
          setOnlyReadsMemory(F);
        } else if (Name == "gets" ||
                   Name == "getchar") {
        } else if (Name == "getitimer") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "getpwnam") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        if (Name == "ungetc") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "uname" ||
                   Name == "unlink" ||
                   Name == "unsetenv") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "utime" ||
                   Name == "utimes") {
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "puts" ||
                   Name == "printf" ||
                   Name == "perror") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "pread" ||
                   Name == "pwrite") {
            continue;
          // May throw; these are valid pthread cancellation points.
          setDoesNotCapture(F, 2);
        } else if (Name == "putchar") {
        } else if (Name == "popen") {
              !FTy->getReturnType()->isPointerTy() ||
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "pclose") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        if (Name == "vscanf") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "vsscanf" ||
                   Name == "vfscanf") {
              !FTy->getParamType(1)->isPointerTy() ||
              !FTy->getParamType(2)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "valloc") {
          if (!FTy->getReturnType()->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
        } else if (Name == "vprintf") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "vfprintf" ||
                   Name == "vsprintf") {
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(1)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "vsnprintf") {
              !FTy->getParamType(0)->isPointerTy() ||
              !FTy->getParamType(2)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 3);
            continue;
          // May throw; "open" is a valid pthread cancellation point.
          setDoesNotCapture(F, 1);
        } else if (Name == "opendir") {
              !FTy->getReturnType()->isPointerTy() ||
              !FTy->getParamType(0)->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
        if (Name == "tmpfile") {
          if (!FTy->getReturnType()->isPointerTy())
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
        } else if (Name == "times") {
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        if (Name == "htonl" ||
            Name == "htons") {