Skip to content

Commit d3040c3

Browse files
committed
Use correct int size for libcalls.
1 parent ea56038 commit d3040c3

File tree

4 files changed

+67
-61
lines changed

4 files changed

+67
-61
lines changed

llvm/include/llvm/ADT/Triple.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ class Triple {
408408
/// Note that this tests for 32-bit pointer width, and nothing else.
409409
bool isArch32Bit() const;
410410

411+
/// Test whether the architecture is 24-bit
412+
///
413+
/// Note that this tests for 24-bit pointer width, and nothing else.
414+
bool isArch24Bit() const;
415+
411416
/// Test whether the architecture is 16-bit
412417
///
413418
/// Note that this tests for 16-bit pointer width, and nothing else.

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
153153
TLI.setShouldSignExtI32Param(ShouldSignExtI32Param);
154154

155155
// Let's assume by default that the size of int is 32 bits, unless the target
156-
// is a 16-bit architecture because then it most likely is 16 bits. If that
157-
// isn't true for a target those defaults should be overridden below.
158-
TLI.setIntSize(T.isArch16Bit() ? 16 : 32);
156+
// is a 16/24-bit architecture because then it most likely is 16/24 bits. If
157+
// that isn't true for a target those defaults should be overridden below.
158+
TLI.setIntSize(T.isArch16Bit() ? 16 : T.isArch24Bit() ? 24 : 32);
159159

160160
// There is really no runtime library on AMDGPU, apart from
161161
// __kmpc_alloc/free_shared.

llvm/lib/Support/Triple.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,10 @@ bool Triple::isArch32Bit() const {
13631363
return getArchPointerBitWidth(getArch()) == 32;
13641364
}
13651365

1366+
bool Triple::isArch24Bit() const {
1367+
return getArchPointerBitWidth(getArch()) == 24;
1368+
}
1369+
13661370
bool Triple::isArch16Bit() const {
13671371
return getArchPointerBitWidth(getArch()) == 16;
13681372
}

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/Transforms/Utils/BuildLibCalls.h"
1414
#include "llvm/ADT/SmallString.h"
1515
#include "llvm/ADT/Statistic.h"
16+
#include "llvm/Analysis/MemoryBuiltins.h"
1617
#include "llvm/Analysis/TargetLibraryInfo.h"
1718
#include "llvm/IR/Constants.h"
1819
#include "llvm/IR/DataLayout.h"
@@ -22,7 +23,6 @@
2223
#include "llvm/IR/LLVMContext.h"
2324
#include "llvm/IR/Module.h"
2425
#include "llvm/IR/Type.h"
25-
#include "llvm/Analysis/MemoryBuiltins.h"
2626

2727
using namespace llvm;
2828

@@ -150,7 +150,7 @@ static bool setOnlyWritesMemory(Function &F, unsigned ArgNo) {
150150
}
151151

152152
static bool setSignExtendedArg(Function &F, unsigned ArgNo) {
153-
if (F.hasParamAttribute(ArgNo, Attribute::SExt))
153+
if (F.hasParamAttribute(ArgNo, Attribute::SExt))
154154
return false;
155155
F.addParamAttr(ArgNo, Attribute::SExt);
156156
++NumSExtArg;
@@ -239,7 +239,7 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
239239

240240
bool Changed = false;
241241

242-
if(!isLibFreeFunction(&F, TheLibFunc) && !isReallocLikeFn(&F, &TLI))
242+
if (!isLibFreeFunction(&F, TheLibFunc) && !isReallocLikeFn(&F, &TLI))
243243
Changed |= setDoesNotFreeMemory(F);
244244

245245
if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT())
@@ -307,10 +307,10 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
307307
Changed |= setDoesNotCapture(F, 1);
308308
Changed |= setOnlyReadsMemory(F, 1);
309309
return Changed;
310-
case LibFunc_strcmp: // 0,1
311-
case LibFunc_strspn: // 0,1
312-
case LibFunc_strncmp: // 0,1
313-
case LibFunc_strcspn: // 0,1
310+
case LibFunc_strcmp: // 0,1
311+
case LibFunc_strspn: // 0,1
312+
case LibFunc_strncmp: // 0,1
313+
case LibFunc_strcspn: // 0,1
314314
Changed |= setDoesNotThrow(F);
315315
Changed |= setOnlyAccessesArgMemory(F);
316316
Changed |= setWillReturn(F);
@@ -1032,7 +1032,7 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
10321032
Changed |= setOnlyWritesMemory(F, 0);
10331033
Changed |= setDoesNotThrow(F);
10341034
return Changed;
1035-
// int __nvvm_reflect(const char *)
1035+
// int __nvvm_reflect(const char *)
10361036
case LibFunc_nvvm_reflect:
10371037
Changed |= setRetAndArgsNoUndef(F);
10381038
Changed |= setDoesNotAccessMemory(F);
@@ -1178,8 +1178,8 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
11781178
}
11791179
}
11801180

1181-
bool llvm::hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
1182-
LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn) {
1181+
bool llvm::hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn,
1182+
LibFunc FloatFn, LibFunc LongDoubleFn) {
11831183
switch (Ty->getTypeID()) {
11841184
case Type::HalfTyID:
11851185
return false;
@@ -1220,8 +1220,7 @@ Value *llvm::castToCStr(Value *V, IRBuilderBase &B) {
12201220
static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType,
12211221
ArrayRef<Type *> ParamTypes,
12221222
ArrayRef<Value *> Operands, IRBuilderBase &B,
1223-
const TargetLibraryInfo *TLI,
1224-
bool IsVaArgs = false) {
1223+
const TargetLibraryInfo *TLI, bool IsVaArgs = false) {
12251224
if (!TLI->has(TheLibFunc))
12261225
return nullptr;
12271226

@@ -1253,16 +1252,16 @@ Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B,
12531252
Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B,
12541253
const TargetLibraryInfo *TLI) {
12551254
Type *I8Ptr = B.getInt8PtrTy();
1256-
Type *I32Ty = B.getInt32Ty();
1257-
return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty},
1258-
{castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI);
1255+
Type *IntTy = B.getIntNTy(TLI->getIntSize());
1256+
return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, IntTy},
1257+
{castToCStr(Ptr, B), ConstantInt::get(IntTy, C)}, B, TLI);
12591258
}
12601259

12611260
Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
12621261
const DataLayout &DL, const TargetLibraryInfo *TLI) {
12631262
LLVMContext &Context = B.GetInsertBlock()->getContext();
12641263
return emitLibCall(
1265-
LibFunc_strncmp, B.getInt32Ty(),
1264+
LibFunc_strncmp, B.getIntNTy(TLI->getIntSize()),
12661265
{B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
12671266
{castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
12681267
}
@@ -1331,17 +1330,17 @@ Value *llvm::emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
13311330
Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
13321331
const DataLayout &DL, const TargetLibraryInfo *TLI) {
13331332
LLVMContext &Context = B.GetInsertBlock()->getContext();
1334-
return emitLibCall(
1335-
LibFunc_memchr, B.getInt8PtrTy(),
1336-
{B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)},
1337-
{castToCStr(Ptr, B), Val, Len}, B, TLI);
1333+
return emitLibCall(LibFunc_memchr, B.getInt8PtrTy(),
1334+
{B.getInt8PtrTy(), B.getIntNTy(TLI->getIntSize()),
1335+
DL.getIntPtrType(Context)},
1336+
{castToCStr(Ptr, B), Val, Len}, B, TLI);
13381337
}
13391338

13401339
Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
13411340
const DataLayout &DL, const TargetLibraryInfo *TLI) {
13421341
LLVMContext &Context = B.GetInsertBlock()->getContext();
13431342
return emitLibCall(
1344-
LibFunc_memcmp, B.getInt32Ty(),
1343+
LibFunc_memcmp, B.getIntNTy(TLI->getIntSize()),
13451344
{B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
13461345
{castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
13471346
}
@@ -1350,25 +1349,25 @@ Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
13501349
const DataLayout &DL, const TargetLibraryInfo *TLI) {
13511350
LLVMContext &Context = B.GetInsertBlock()->getContext();
13521351
return emitLibCall(
1353-
LibFunc_bcmp, B.getInt32Ty(),
1352+
LibFunc_bcmp, B.getIntNTy(TLI->getIntSize()),
13541353
{B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
13551354
{castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
13561355
}
13571356

13581357
Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
13591358
IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1360-
return emitLibCall(
1361-
LibFunc_memccpy, B.getInt8PtrTy(),
1362-
{B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt32Ty(), Len->getType()},
1363-
{Ptr1, Ptr2, Val, Len}, B, TLI);
1359+
return emitLibCall(LibFunc_memccpy, B.getInt8PtrTy(),
1360+
{B.getInt8PtrTy(), B.getInt8PtrTy(),
1361+
B.getIntNTy(TLI->getIntSize()), Len->getType()},
1362+
{Ptr1, Ptr2, Val, Len}, B, TLI);
13641363
}
13651364

13661365
Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
13671366
ArrayRef<Value *> VariadicArgs, IRBuilderBase &B,
13681367
const TargetLibraryInfo *TLI) {
13691368
SmallVector<Value *, 8> Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)};
13701369
llvm::append_range(Args, VariadicArgs);
1371-
return emitLibCall(LibFunc_snprintf, B.getInt32Ty(),
1370+
return emitLibCall(LibFunc_snprintf, B.getIntNTy(TLI->getIntSize()),
13721371
{B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy()},
13731372
Args, B, TLI, /*IsVaArgs=*/true);
13741373
}
@@ -1378,7 +1377,7 @@ Value *llvm::emitSPrintf(Value *Dest, Value *Fmt,
13781377
const TargetLibraryInfo *TLI) {
13791378
SmallVector<Value *, 8> Args{castToCStr(Dest, B), castToCStr(Fmt, B)};
13801379
llvm::append_range(Args, VariadicArgs);
1381-
return emitLibCall(LibFunc_sprintf, B.getInt32Ty(),
1380+
return emitLibCall(LibFunc_sprintf, B.getIntNTy(TLI->getIntSize()),
13821381
{B.getInt8PtrTy(), B.getInt8PtrTy()}, Args, B, TLI,
13831382
/*IsVaArgs=*/true);
13841383
}
@@ -1414,14 +1413,14 @@ Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
14141413
Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
14151414
IRBuilderBase &B, const TargetLibraryInfo *TLI) {
14161415
return emitLibCall(
1417-
LibFunc_vsnprintf, B.getInt32Ty(),
1416+
LibFunc_vsnprintf, B.getIntNTy(TLI->getIntSize()),
14181417
{B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy(), VAList->getType()},
14191418
{castToCStr(Dest, B), Size, castToCStr(Fmt, B), VAList}, B, TLI);
14201419
}
14211420

14221421
Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList,
14231422
IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1424-
return emitLibCall(LibFunc_vsprintf, B.getInt32Ty(),
1423+
return emitLibCall(LibFunc_vsprintf, B.getIntNTy(TLI->getIntSize()),
14251424
{B.getInt8PtrTy(), B.getInt8PtrTy(), VAList->getType()},
14261425
{castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI);
14271426
}
@@ -1430,7 +1429,7 @@ Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList,
14301429
static void appendTypeSuffix(Value *Op, StringRef &Name,
14311430
SmallString<20> &NameBuffer) {
14321431
if (!Op->getType()->isDoubleTy()) {
1433-
NameBuffer += Name;
1432+
NameBuffer += Name;
14341433

14351434
if (Op->getType()->isFloatTy())
14361435
NameBuffer += 'f';
@@ -1476,24 +1475,24 @@ Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
14761475
LibFunc LongDoubleFn, IRBuilderBase &B,
14771476
const AttributeList &Attrs) {
14781477
// Get the name of the function according to TLI.
1479-
StringRef Name = getFloatFnName(TLI, Op->getType(),
1480-
DoubleFn, FloatFn, LongDoubleFn);
1478+
StringRef Name =
1479+
getFloatFnName(TLI, Op->getType(), DoubleFn, FloatFn, LongDoubleFn);
14811480

14821481
return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
14831482
}
14841483

1485-
static Value *emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2,
1486-
StringRef Name, IRBuilderBase &B,
1487-
const AttributeList &Attrs,
1488-
const TargetLibraryInfo *TLI = nullptr) {
1484+
static Value *
1485+
emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2, StringRef Name,
1486+
IRBuilderBase &B, const AttributeList &Attrs,
1487+
const TargetLibraryInfo *TLI = nullptr) {
14891488
assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
14901489

14911490
Module *M = B.GetInsertBlock()->getModule();
1492-
FunctionCallee Callee = M->getOrInsertFunction(Name, Op1->getType(),
1493-
Op1->getType(), Op2->getType());
1491+
FunctionCallee Callee = M->getOrInsertFunction(
1492+
Name, Op1->getType(), Op1->getType(), Op2->getType());
14941493
if (TLI != nullptr)
14951494
inferLibFuncAttributes(M, Name, *TLI);
1496-
CallInst *CI = B.CreateCall(Callee, { Op1, Op2 }, Name);
1495+
CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
14971496

14981497
// The incoming attribute set may have come from a speculatable intrinsic, but
14991498
// is being replaced with a library call which is not allowed to be
@@ -1524,8 +1523,8 @@ Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2,
15241523
LibFunc LongDoubleFn, IRBuilderBase &B,
15251524
const AttributeList &Attrs) {
15261525
// Get the name of the function according to TLI.
1527-
StringRef Name = getFloatFnName(TLI, Op1->getType(),
1528-
DoubleFn, FloatFn, LongDoubleFn);
1526+
StringRef Name =
1527+
getFloatFnName(TLI, Op1->getType(), DoubleFn, FloatFn, LongDoubleFn);
15291528

15301529
return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs, TLI);
15311530
}
@@ -1537,15 +1536,12 @@ Value *llvm::emitPutChar(Value *Char, IRBuilderBase &B,
15371536

15381537
Module *M = B.GetInsertBlock()->getModule();
15391538
StringRef PutCharName = TLI->getName(LibFunc_putchar);
1540-
FunctionCallee PutChar =
1541-
M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
1539+
Type *IntTy = B.getIntNTy(TLI->getIntSize());
1540+
FunctionCallee PutChar = M->getOrInsertFunction(PutCharName, IntTy, IntTy);
15421541
inferLibFuncAttributes(M, PutCharName, *TLI);
1543-
CallInst *CI = B.CreateCall(PutChar,
1544-
B.CreateIntCast(Char,
1545-
B.getInt32Ty(),
1546-
/*isSigned*/true,
1547-
"chari"),
1548-
PutCharName);
1542+
CallInst *CI = B.CreateCall(
1543+
PutChar, B.CreateIntCast(Char, IntTy, /*isSigned=*/true, "chari"),
1544+
PutCharName);
15491545

15501546
if (const Function *F =
15511547
dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts()))
@@ -1560,8 +1556,8 @@ Value *llvm::emitPutS(Value *Str, IRBuilderBase &B,
15601556

15611557
Module *M = B.GetInsertBlock()->getModule();
15621558
StringRef PutsName = TLI->getName(LibFunc_puts);
1563-
FunctionCallee PutS =
1564-
M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
1559+
FunctionCallee PutS = M->getOrInsertFunction(
1560+
PutsName, B.getIntNTy(TLI->getIntSize()), B.getInt8PtrTy());
15651561
inferLibFuncAttributes(M, PutsName, *TLI);
15661562
CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
15671563
if (const Function *F =
@@ -1577,12 +1573,12 @@ Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilderBase &B,
15771573

15781574
Module *M = B.GetInsertBlock()->getModule();
15791575
StringRef FPutcName = TLI->getName(LibFunc_fputc);
1580-
FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(),
1581-
B.getInt32Ty(), File->getType());
1576+
Type *IntTy = B.getIntNTy(TLI->getIntSize());
1577+
FunctionCallee F =
1578+
M->getOrInsertFunction(FPutcName, IntTy, IntTy, File->getType());
15821579
if (File->getType()->isPointerTy())
15831580
inferLibFuncAttributes(M, FPutcName, *TLI);
1584-
Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
1585-
"chari");
1581+
Char = B.CreateIntCast(Char, IntTy, /*isSigned*/ true, "chari");
15861582
CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
15871583

15881584
if (const Function *Fn =
@@ -1598,8 +1594,9 @@ Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilderBase &B,
15981594

15991595
Module *M = B.GetInsertBlock()->getModule();
16001596
StringRef FPutsName = TLI->getName(LibFunc_fputs);
1601-
FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
1602-
B.getInt8PtrTy(), File->getType());
1597+
FunctionCallee F =
1598+
M->getOrInsertFunction(FPutsName, B.getIntNTy(TLI->getIntSize()),
1599+
B.getInt8PtrTy(), File->getType());
16031600
if (File->getType()->isPointerTy())
16041601
inferLibFuncAttributes(M, FPutsName, *TLI);
16051602
CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);

0 commit comments

Comments
 (0)