Skip to content

Commit

Permalink
make all Emit*() functions consult the TargetLibraryInfo information …
Browse files Browse the repository at this point in the history
…before creating a call to a library function.

Update all clients to pass the TLI information around.
Previous draft reviewed by Eli.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160733 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
nunoplopes committed Jul 25, 2012
1 parent c338fe0 commit 51004df
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 81 deletions.
2 changes: 2 additions & 0 deletions include/llvm/Target/TargetLibraryInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ namespace llvm {
cxa_guard_acquire,
/// void __cxa_guard_release(guard_t *guard);
cxa_guard_release,
/// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
memcpy_chk,

NumLibFuncs
};
Expand Down
41 changes: 24 additions & 17 deletions include/llvm/Transforms/Utils/BuildLibCalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,46 @@ namespace llvm {
/// EmitStrLen - Emit a call to the strlen function to the builder, for the
/// specified pointer. Ptr is required to be some pointer type, and the
/// return value has 'intptr_t' type.
Value *EmitStrLen(Value *Ptr, IRBuilder<> &B, const TargetData *TD);
Value *EmitStrLen(Value *Ptr, IRBuilder<> &B, const TargetData *TD,
const TargetLibraryInfo *TLI);

/// EmitStrChr - Emit a call to the strchr function to the builder, for the
/// specified pointer and character. Ptr is required to be some pointer type,
/// and the return value has 'i8*' type.
Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, const TargetData *TD);
Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, const TargetData *TD,
const TargetLibraryInfo *TLI);

/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
Value *EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
const TargetData *TD);
const TargetData *TD, const TargetLibraryInfo *TLI);

/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
/// specified pointer arguments.
Value *EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
const TargetData *TD, StringRef Name = "strcpy");
const TargetData *TD, const TargetLibraryInfo *TLI,
StringRef Name = "strcpy");

/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
/// specified pointer arguments and length.
Value *EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
const TargetData *TD, StringRef Name = "strncpy");
const TargetData *TD, const TargetLibraryInfo *TLI,
StringRef Name = "strncpy");

/// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
/// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
/// are pointers.
Value *EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
IRBuilder<> &B, const TargetData *TD);
IRBuilder<> &B, const TargetData *TD,
const TargetLibraryInfo *TLI);

/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
const TargetData *TD);
const TargetData *TD, const TargetLibraryInfo *TLI);

/// EmitMemCmp - Emit a call to the memcmp function.
Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
const TargetData *TD);
const TargetData *TD, const TargetLibraryInfo *TLI);

/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name'
/// (e.g. 'floor'). This function is known to take a single of type matching
Expand All @@ -74,26 +79,28 @@ namespace llvm {

/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
/// is an integer.
Value *EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD);
Value *EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD,
const TargetLibraryInfo *TLI);

/// EmitPutS - Emit a call to the puts function. This assumes that Str is
/// some pointer.
void EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD);
Value *EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD,
const TargetLibraryInfo *TLI);

/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
/// an i32, and File is a pointer to FILE.
void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
const TargetData *TD);
Value *EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
const TargetData *TD, const TargetLibraryInfo *TLI);

/// EmitFPutS - Emit a call to the puts function. Str is required to be a
/// pointer and File is a pointer to FILE.
void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, const TargetData *TD,
const TargetLibraryInfo *TLI);
Value *EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, const TargetData *TD,
const TargetLibraryInfo *TLI);

/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
const TargetData *TD, const TargetLibraryInfo *TLI);
Value *EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
const TargetData *TD, const TargetLibraryInfo *TLI);

/// SimplifyFortifiedLibCalls - Helper class for folding checked library
/// calls (e.g. __strcpy_chk) into their unchecked counterparts.
Expand All @@ -105,7 +112,7 @@ namespace llvm {
bool isString) const = 0;
public:
virtual ~SimplifyFortifiedLibCalls();
bool fold(CallInst *CI, const TargetData *TD);
bool fold(CallInst *CI, const TargetData *TD, const TargetLibraryInfo *TLI);
};
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Target/TargetLibraryInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ const char* TargetLibraryInfo::StandardNames[LibFunc::NumLibFuncs] =
"__cxa_atexit",
"__cxa_guard_abort",
"__cxa_guard_acquire",
"__cxa_guard_release"
"__cxa_guard_release",
"__memcpy_chk"
};

/// initialize - Initialize the set of available library functions based on the
Expand Down
2 changes: 1 addition & 1 deletion lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ Instruction *InstCombiner::tryOptimizeCall(CallInst *CI, const TargetData *TD) {
if (CI->getCalledFunction() == 0) return 0;

InstCombineFortifiedLibCalls Simplifier(this);
Simplifier.fold(CI, TD);
Simplifier.fold(CI, TD, TLI);
return Simplifier.NewInstruction;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Transforms/Scalar/CodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) {
// that have the default "don't know" as the objectsize. Anything else
// should be left alone.
CodeGenPrepareFortifiedLibCalls Simplifier;
return Simplifier.fold(CI, TD);
return Simplifier.fold(CI, TD, TLInfo);
}

/// DupRetToEnableTailCallOpts - Look for opportunities to duplicate return
Expand Down
90 changes: 50 additions & 40 deletions lib/Transforms/Scalar/SimplifyLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ struct StrCatOpt : public LibCallOptimization {
void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
// 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.
Value *DstLen = EmitStrLen(Dst, B, TD);
Value *DstLen = EmitStrLen(Dst, B, TD, TLI);

// Now that we have the destination's length, we must index into the
// destination's pointer to get the actual memcpy destination (end of
Expand Down Expand Up @@ -254,7 +254,7 @@ struct StrChrOpt : public LibCallOptimization {

return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
ConstantInt::get(TD->getIntPtrType(*Context), Len),
B, TD);
B, TD, TLI);
}

// Otherwise, the character is a constant, see if the first argument is
Expand Down Expand Up @@ -299,7 +299,7 @@ struct StrRChrOpt : public LibCallOptimization {
if (!getConstantStringInfo(SrcStr, Str)) {
// strrchr(s, 0) -> strchr(s, 0)
if (TD && CharC->isZero())
return EmitStrChr(SrcStr, '\0', B, TD);
return EmitStrChr(SrcStr, '\0', B, TD, TLI);
return 0;
}

Expand Down Expand Up @@ -355,7 +355,7 @@ struct StrCmpOpt : public LibCallOptimization {

return EmitMemCmp(Str1P, Str2P,
ConstantInt::get(TD->getIntPtrType(*Context),
std::min(Len1, Len2)), B, TD);
std::min(Len1, Len2)), B, TD, TLI);
}

return 0;
Expand Down Expand Up @@ -391,7 +391,7 @@ struct StrNCmpOpt : public LibCallOptimization {
return ConstantInt::get(CI->getType(), 0);

if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD, TLI);

StringRef Str1, Str2;
bool HasStr1 = getConstantStringInfo(Str1P, Str1);
Expand Down Expand Up @@ -450,7 +450,7 @@ struct StrCpyOpt : public LibCallOptimization {
if (OptChkCall)
EmitMemCpyChk(Dst, Src,
ConstantInt::get(TD->getIntPtrType(*Context), Len),
CI->getArgOperand(2), B, TD);
CI->getArgOperand(2), B, TD, TLI);
else
B.CreateMemCpy(Dst, Src,
ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
Expand Down Expand Up @@ -480,8 +480,10 @@ struct StpCpyOpt: public LibCallOptimization {
if (!TD) return 0;

Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
if (Dst == Src) // stpcpy(x,x) -> x+strlen(x)
return B.CreateInBoundsGEP(Dst, EmitStrLen(Src, B, TD));
if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
Value *StrLen = EmitStrLen(Src, B, TD, TLI);
return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
}

// See if we can get the length of the input string.
uint64_t Len = GetStringLength(Src);
Expand All @@ -495,7 +497,7 @@ struct StpCpyOpt: public LibCallOptimization {
// 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.
if (OptChkCall)
EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, TD);
EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, TD, TLI);
else
B.CreateMemCpy(Dst, Src, LenV, 1);
return DstEnd;
Expand Down Expand Up @@ -609,7 +611,7 @@ struct StrPBrkOpt : public LibCallOptimization {

// strpbrk(s, "a") -> strchr(s, 'a')
if (TD && HasS2 && S2.size() == 1)
return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD);
return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD, TLI);

return 0;
}
Expand Down Expand Up @@ -698,7 +700,7 @@ struct StrCSpnOpt : public LibCallOptimization {

// strcspn(s, "") -> strlen(s)
if (TD && HasS2 && S2.empty())
return EmitStrLen(CI->getArgOperand(0), B, TD);
return EmitStrLen(CI->getArgOperand(0), B, TD, TLI);

return 0;
}
Expand All @@ -722,9 +724,13 @@ struct StrStrOpt : public LibCallOptimization {

// fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD);
Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD, TLI);
if (!StrLen)
return 0;
Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
StrLen, B, TD);
StrLen, B, TD, TLI);
if (!StrNCmp)
return 0;
for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
UI != UE; ) {
ICmpInst *Old = cast<ICmpInst>(*UI++);
Expand Down Expand Up @@ -760,9 +766,10 @@ struct StrStrOpt : public LibCallOptimization {
}

// fold strstr(x, "y") -> strchr(x, 'y').
if (HasStr2 && ToFindStr.size() == 1)
return B.CreateBitCast(EmitStrChr(CI->getArgOperand(0),
ToFindStr[0], B, TD), CI->getType());
if (HasStr2 && ToFindStr.size() == 1) {
Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TD, TLI);
return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0;
}
return 0;
}
};
Expand Down Expand Up @@ -1179,7 +1186,7 @@ struct PrintFOpt : public LibCallOptimization {

// printf("x") -> putchar('x'), even for '%'.
if (FormatStr.size() == 1) {
Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD);
Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI);
if (CI->use_empty()) return CI;
return B.CreateIntCast(Res, CI->getType(), true);
}
Expand All @@ -1191,16 +1198,17 @@ struct PrintFOpt : public LibCallOptimization {
// pass to be run after this pass, to merge duplicate strings.
FormatStr = FormatStr.drop_back();
Value *GV = B.CreateGlobalString(FormatStr, "str");
EmitPutS(GV, B, TD);
return CI->use_empty() ? (Value*)CI :
ConstantInt::get(CI->getType(), FormatStr.size()+1);
Value *NewCI = EmitPutS(GV, B, TD, TLI);
return (CI->use_empty() || !NewCI) ?
NewCI :
ConstantInt::get(CI->getType(), FormatStr.size()+1);
}

// Optimize specific format strings.
// printf("%c", chr) --> putchar(chr)
if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
CI->getArgOperand(1)->getType()->isIntegerTy()) {
Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD);
Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI);

if (CI->use_empty()) return CI;
return B.CreateIntCast(Res, CI->getType(), true);
Expand All @@ -1209,8 +1217,7 @@ struct PrintFOpt : public LibCallOptimization {
// printf("%s\n", str) --> puts(str)
if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
CI->getArgOperand(1)->getType()->isPointerTy()) {
EmitPutS(CI->getArgOperand(1), B, TD);
return CI;
return EmitPutS(CI->getArgOperand(1), B, TD, TLI);
}
return 0;
}
Expand Down Expand Up @@ -1297,7 +1304,9 @@ struct SPrintFOpt : public LibCallOptimization {
// sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;

Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD, TLI);
if (!Len)
return 0;
Value *IncLen = B.CreateAdd(Len,
ConstantInt::get(Len->getType(), 1),
"leninc");
Expand Down Expand Up @@ -1364,8 +1373,8 @@ struct FWriteOpt : public LibCallOptimization {
// This optimisation is only valid, if the return value is unused.
if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
EmitFPutC(Char, CI->getArgOperand(3), B, TD);
return ConstantInt::get(CI->getType(), 1);
Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, TD, TLI);
return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
}

return 0;
Expand All @@ -1390,10 +1399,10 @@ struct FPutsOpt : public LibCallOptimization {
// fputs(s,F) --> fwrite(s,1,strlen(s),F)
uint64_t Len = GetStringLength(CI->getArgOperand(0));
if (!Len) return 0;
EmitFWrite(CI->getArgOperand(0),
ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
CI->getArgOperand(1), B, TD, TLI);
return CI; // Known to have no uses (see above).
// Known to have no uses (see above).
return EmitFWrite(CI->getArgOperand(0),
ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
CI->getArgOperand(1), B, TD, TLI);
}
};

Expand All @@ -1417,11 +1426,11 @@ struct FPrintFOpt : public LibCallOptimization {
// These optimizations require TargetData.
if (!TD) return 0;

EmitFWrite(CI->getArgOperand(1),
ConstantInt::get(TD->getIntPtrType(*Context),
FormatStr.size()),
CI->getArgOperand(0), B, TD, TLI);
return ConstantInt::get(CI->getType(), FormatStr.size());
Value *NewCI = EmitFWrite(CI->getArgOperand(1),
ConstantInt::get(TD->getIntPtrType(*Context),
FormatStr.size()),
CI->getArgOperand(0), B, TD, TLI);
return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0;
}

// The remaining optimizations require the format string to be "%s" or "%c"
Expand All @@ -1434,16 +1443,16 @@ struct FPrintFOpt : public LibCallOptimization {
if (FormatStr[1] == 'c') {
// 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);
Value *NewCI = EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B,
TD, TLI);
return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
}

if (FormatStr[1] == 's') {
// fprintf(F, "%s", str) --> fputs(str, F)
if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
return 0;
EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD, TLI);
return CI;
return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD, TLI);
}
return 0;
}
Expand Down Expand Up @@ -1494,7 +1503,8 @@ struct PutsOpt : public LibCallOptimization {

if (Str.empty() && CI->use_empty()) {
// puts("") -> putchar('\n')
Value *Res = EmitPutChar(B.getInt32('\n'), B, TD);
Value *Res = EmitPutChar(B.getInt32('\n'), B, TD, TLI);
if (!Res) return 0;
if (CI->use_empty()) return CI;
return B.CreateIntCast(Res, CI->getType(), true);
}
Expand Down
Loading

0 comments on commit 51004df

Please sign in to comment.