2004-06-20 09:49:54 +02:00
|
|
|
//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
|
2005-04-22 00:36:52 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-04-22 00:36:52 +02:00
|
|
|
//
|
2004-06-20 09:49:54 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2006-11-15 19:00:10 +01:00
|
|
|
// This file implements the IntrinsicLowering class.
|
2004-06-20 09:49:54 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-06-29 14:38:19 +02:00
|
|
|
#include "llvm/CodeGen/IntrinsicLowering.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2009-07-11 15:10:19 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-23 08:03:38 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-06-20 09:49:54 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-12-15 19:34:45 +01:00
|
|
|
/// This function is used when we want to lower an intrinsic call to a call of
|
|
|
|
/// an external function. This handles hard cases such as when there was already
|
|
|
|
/// a prototype for the external function, but that prototype doesn't match the
|
|
|
|
/// arguments we expect to pass in.
|
2004-06-20 09:49:54 +02:00
|
|
|
template <class ArgIt>
|
|
|
|
static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
|
2007-01-07 09:12:01 +01:00
|
|
|
ArgIt ArgBegin, ArgIt ArgEnd,
|
2011-07-18 06:54:35 +02:00
|
|
|
Type *RetTy) {
|
2009-06-26 22:33:47 +02:00
|
|
|
// If we haven't already looked up this function, check to see if the
|
|
|
|
// program already contains a function with this name.
|
2015-12-14 18:24:23 +01:00
|
|
|
Module *M = CI->getModule();
|
2009-06-26 22:33:47 +02:00
|
|
|
// Get or insert the definition now.
|
2011-07-12 16:06:48 +02:00
|
|
|
std::vector<Type *> ParamTys;
|
2009-06-26 22:33:47 +02:00
|
|
|
for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
|
|
|
|
ParamTys.push_back((*I)->getType());
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 03:28:03 +01:00
|
|
|
FunctionCallee FCache =
|
|
|
|
M->getOrInsertFunction(NewFn, FunctionType::get(RetTy, ParamTys, false));
|
2004-06-20 09:49:54 +02:00
|
|
|
|
2015-10-09 21:13:58 +02:00
|
|
|
IRBuilder<> Builder(CI->getParent(), CI->getIterator());
|
2007-08-01 05:43:44 +02:00
|
|
|
SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
|
2011-07-15 10:37:34 +02:00
|
|
|
CallInst *NewCI = Builder.CreateCall(FCache, Args);
|
2009-05-12 22:27:44 +02:00
|
|
|
NewCI->setName(CI->getName());
|
2007-01-07 09:12:01 +01:00
|
|
|
if (!CI->use_empty())
|
|
|
|
CI->replaceAllUsesWith(NewCI);
|
2004-06-20 09:49:54 +02:00
|
|
|
return NewCI;
|
|
|
|
}
|
|
|
|
|
2017-12-15 19:34:45 +01:00
|
|
|
/// Emit the code to lower bswap of V before the specified instruction IP.
|
2009-07-15 01:09:55 +02:00
|
|
|
static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
|
2017-11-30 12:06:22 +01:00
|
|
|
assert(V->getType()->isIntOrIntVectorTy() && "Can't bswap a non-integer type!");
|
2006-01-16 08:57:00 +01:00
|
|
|
|
2017-11-30 12:06:22 +01:00
|
|
|
unsigned BitSize = V->getType()->getScalarSizeInBits();
|
2015-10-09 21:13:58 +02:00
|
|
|
|
|
|
|
IRBuilder<> Builder(IP);
|
2009-05-12 22:27:44 +02:00
|
|
|
|
2006-01-16 08:57:00 +01:00
|
|
|
switch(BitSize) {
|
2009-07-14 18:55:14 +02:00
|
|
|
default: llvm_unreachable("Unhandled type size of value to byteswap!");
|
2006-01-16 08:57:00 +01:00
|
|
|
case 16: {
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.2");
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.1");
|
|
|
|
V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
|
2006-01-16 08:57:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 32: {
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.4");
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.3");
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.2");
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.1");
|
2009-07-15 01:09:55 +02:00
|
|
|
Tmp3 = Builder.CreateAnd(Tmp3,
|
2017-11-30 12:06:22 +01:00
|
|
|
ConstantInt::get(V->getType(), 0xFF0000),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.and3");
|
2009-07-15 01:09:55 +02:00
|
|
|
Tmp2 = Builder.CreateAnd(Tmp2,
|
2017-11-30 12:06:22 +01:00
|
|
|
ConstantInt::get(V->getType(), 0xFF00),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.and2");
|
|
|
|
Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
|
|
|
|
Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
|
|
|
|
V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
|
2006-01-16 08:57:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 64: {
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.8");
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.7");
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.6");
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.5");
|
2009-07-25 01:12:02 +02:00
|
|
|
Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.4");
|
2018-07-30 21:41:25 +02:00
|
|
|
Value* Tmp3 = Builder.CreateLShr(V,
|
2009-07-25 01:12:02 +02:00
|
|
|
ConstantInt::get(V->getType(), 24),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.3");
|
2018-07-30 21:41:25 +02:00
|
|
|
Value* Tmp2 = Builder.CreateLShr(V,
|
2009-07-25 01:12:02 +02:00
|
|
|
ConstantInt::get(V->getType(), 40),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.2");
|
2018-07-30 21:41:25 +02:00
|
|
|
Value* Tmp1 = Builder.CreateLShr(V,
|
2009-07-25 01:12:02 +02:00
|
|
|
ConstantInt::get(V->getType(), 56),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.1");
|
|
|
|
Tmp7 = Builder.CreateAnd(Tmp7,
|
2017-11-30 12:06:22 +01:00
|
|
|
ConstantInt::get(V->getType(),
|
2009-05-12 22:27:44 +02:00
|
|
|
0xFF000000000000ULL),
|
|
|
|
"bswap.and7");
|
|
|
|
Tmp6 = Builder.CreateAnd(Tmp6,
|
2017-11-30 12:06:22 +01:00
|
|
|
ConstantInt::get(V->getType(),
|
2009-05-12 22:27:44 +02:00
|
|
|
0xFF0000000000ULL),
|
|
|
|
"bswap.and6");
|
|
|
|
Tmp5 = Builder.CreateAnd(Tmp5,
|
2017-11-30 12:06:22 +01:00
|
|
|
ConstantInt::get(V->getType(),
|
2009-08-13 23:58:54 +02:00
|
|
|
0xFF00000000ULL),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.and5");
|
|
|
|
Tmp4 = Builder.CreateAnd(Tmp4,
|
2017-11-30 12:06:22 +01:00
|
|
|
ConstantInt::get(V->getType(),
|
2009-08-13 23:58:54 +02:00
|
|
|
0xFF000000ULL),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.and4");
|
|
|
|
Tmp3 = Builder.CreateAnd(Tmp3,
|
2017-11-30 12:06:22 +01:00
|
|
|
ConstantInt::get(V->getType(),
|
2009-08-13 23:58:54 +02:00
|
|
|
0xFF0000ULL),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.and3");
|
|
|
|
Tmp2 = Builder.CreateAnd(Tmp2,
|
2017-11-30 12:06:22 +01:00
|
|
|
ConstantInt::get(V->getType(),
|
2009-08-13 23:58:54 +02:00
|
|
|
0xFF00ULL),
|
2009-05-12 22:27:44 +02:00
|
|
|
"bswap.and2");
|
|
|
|
Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
|
|
|
|
Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
|
|
|
|
Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
|
|
|
|
Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
|
|
|
|
Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
|
|
|
|
Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
|
|
|
|
V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
|
2006-01-16 08:57:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2017-12-15 19:34:45 +01:00
|
|
|
/// Emit the code to lower ctpop of V before the specified instruction IP.
|
2009-07-15 01:09:55 +02:00
|
|
|
static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
|
2010-02-15 17:12:20 +01:00
|
|
|
assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
|
2005-05-11 21:42:05 +02:00
|
|
|
|
|
|
|
static const uint64_t MaskValues[6] = {
|
|
|
|
0x5555555555555555ULL, 0x3333333333333333ULL,
|
|
|
|
0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
|
|
|
|
0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
|
|
|
|
};
|
|
|
|
|
2015-10-09 21:13:58 +02:00
|
|
|
IRBuilder<> Builder(IP);
|
2009-05-12 22:27:44 +02:00
|
|
|
|
2005-05-11 22:24:12 +02:00
|
|
|
unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
|
2007-06-02 06:10:33 +02:00
|
|
|
unsigned WordSize = (BitSize + 63) / 64;
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *Count = ConstantInt::get(V->getType(), 0);
|
2006-11-08 07:47:33 +01:00
|
|
|
|
2007-06-02 06:10:33 +02:00
|
|
|
for (unsigned n = 0; n < WordSize; ++n) {
|
|
|
|
Value *PartValue = V;
|
2018-07-30 21:41:25 +02:00
|
|
|
for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
|
2007-06-02 06:10:33 +02:00
|
|
|
i <<= 1, ++ct) {
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
|
2009-05-12 22:27:44 +02:00
|
|
|
Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
|
|
|
|
Value *VShift = Builder.CreateLShr(PartValue,
|
2009-07-25 01:12:02 +02:00
|
|
|
ConstantInt::get(V->getType(), i),
|
2009-05-12 22:27:44 +02:00
|
|
|
"ctpop.sh");
|
|
|
|
Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
|
|
|
|
PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
|
2007-06-02 06:10:33 +02:00
|
|
|
}
|
2009-05-12 22:27:44 +02:00
|
|
|
Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
|
2007-06-02 06:10:33 +02:00
|
|
|
if (BitSize > 64) {
|
2009-07-25 01:12:02 +02:00
|
|
|
V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
|
2009-05-12 22:27:44 +02:00
|
|
|
"ctpop.part.sh");
|
2007-06-02 06:10:33 +02:00
|
|
|
BitSize -= 64;
|
|
|
|
}
|
2005-05-11 21:42:05 +02:00
|
|
|
}
|
|
|
|
|
2007-08-06 18:36:18 +02:00
|
|
|
return Count;
|
2005-05-11 21:42:05 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 19:34:45 +01:00
|
|
|
/// Emit the code to lower ctlz of V before the specified instruction IP.
|
2009-07-15 01:09:55 +02:00
|
|
|
static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
|
2005-05-11 22:24:12 +02:00
|
|
|
|
2015-10-09 21:13:58 +02:00
|
|
|
IRBuilder<> Builder(IP);
|
2009-05-12 22:27:44 +02:00
|
|
|
|
2005-05-11 22:24:12 +02:00
|
|
|
unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
|
2007-06-02 06:10:33 +02:00
|
|
|
for (unsigned i = 1; i < BitSize; i <<= 1) {
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *ShVal = ConstantInt::get(V->getType(), i);
|
2009-05-12 22:27:44 +02:00
|
|
|
ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
|
|
|
|
V = Builder.CreateOr(V, ShVal, "ctlz.step");
|
2005-05-11 22:24:12 +02:00
|
|
|
}
|
|
|
|
|
2009-05-12 22:27:44 +02:00
|
|
|
V = Builder.CreateNot(V);
|
2009-07-15 01:09:55 +02:00
|
|
|
return LowerCTPOP(Context, V, IP);
|
2005-05-11 22:24:12 +02:00
|
|
|
}
|
|
|
|
|
2009-06-26 22:33:47 +02:00
|
|
|
static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
|
|
|
|
const char *Dname,
|
2008-09-22 22:51:30 +02:00
|
|
|
const char *LDname) {
|
2010-06-25 11:03:52 +02:00
|
|
|
switch (CI->getArgOperand(0)->getType()->getTypeID()) {
|
2009-07-14 18:55:14 +02:00
|
|
|
default: llvm_unreachable("Invalid type in intrinsic");
|
2008-09-22 22:51:30 +02:00
|
|
|
case Type::FloatTyID:
|
2020-04-13 08:34:57 +02:00
|
|
|
ReplaceCallWith(Fname, CI, CI->arg_begin(), CI->arg_end(),
|
|
|
|
Type::getFloatTy(CI->getContext()));
|
2008-09-22 22:51:30 +02:00
|
|
|
break;
|
|
|
|
case Type::DoubleTyID:
|
2020-04-13 08:34:57 +02:00
|
|
|
ReplaceCallWith(Dname, CI, CI->arg_begin(), CI->arg_end(),
|
|
|
|
Type::getDoubleTy(CI->getContext()));
|
2008-09-22 22:51:30 +02:00
|
|
|
break;
|
|
|
|
case Type::X86_FP80TyID:
|
|
|
|
case Type::FP128TyID:
|
|
|
|
case Type::PPC_FP128TyID:
|
2020-04-13 08:34:57 +02:00
|
|
|
ReplaceCallWith(LDname, CI, CI->arg_begin(), CI->arg_end(),
|
|
|
|
CI->getArgOperand(0)->getType());
|
2008-09-22 22:51:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-04-05 01:48:25 +02:00
|
|
|
|
2006-11-15 19:00:10 +01:00
|
|
|
void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
|
2015-10-09 21:13:58 +02:00
|
|
|
IRBuilder<> Builder(CI);
|
2009-07-22 02:24:57 +02:00
|
|
|
LLVMContext &Context = CI->getContext();
|
2009-05-12 22:27:44 +02:00
|
|
|
|
2010-04-15 03:51:59 +02:00
|
|
|
const Function *Callee = CI->getCalledFunction();
|
2004-06-20 09:49:54 +02:00
|
|
|
assert(Callee && "Cannot lower an indirect call!");
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2004-06-20 09:49:54 +02:00
|
|
|
switch (Callee->getIntrinsicID()) {
|
|
|
|
case Intrinsic::not_intrinsic:
|
2010-04-08 00:58:41 +02:00
|
|
|
report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
|
2009-07-11 15:10:19 +02:00
|
|
|
Callee->getName() + "'!");
|
2004-06-20 09:49:54 +02:00
|
|
|
default:
|
2010-04-08 00:58:41 +02:00
|
|
|
report_fatal_error("Code generator does not support intrinsic function '"+
|
2009-07-11 15:10:19 +02:00
|
|
|
Callee->getName()+"'!");
|
2004-06-20 09:49:54 +02:00
|
|
|
|
2011-07-06 20:22:43 +02:00
|
|
|
case Intrinsic::expect: {
|
|
|
|
// Just replace __builtin_expect(exp, c) with EXP.
|
|
|
|
Value *V = CI->getArgOperand(0);
|
|
|
|
CI->replaceAllUsesWith(V);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-04-01 09:35:23 +02:00
|
|
|
case Intrinsic::ctpop:
|
2010-06-25 11:03:52 +02:00
|
|
|
CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getArgOperand(0), CI));
|
For PR411:
This patch is an incremental step towards supporting a flat symbol table.
It de-overloads the intrinsic functions by providing type-specific intrinsics
and arranging for automatically upgrading from the old overloaded name to
the new non-overloaded name. Specifically:
llvm.isunordered -> llvm.isunordered.f32, llvm.isunordered.f64
llvm.sqrt -> llvm.sqrt.f32, llvm.sqrt.f64
llvm.ctpop -> llvm.ctpop.i8, llvm.ctpop.i16, llvm.ctpop.i32, llvm.ctpop.i64
llvm.ctlz -> llvm.ctlz.i8, llvm.ctlz.i16, llvm.ctlz.i32, llvm.ctlz.i64
llvm.cttz -> llvm.cttz.i8, llvm.cttz.i16, llvm.cttz.i32, llvm.cttz.i64
New code should not use the overloaded intrinsic names. Warnings will be
emitted if they are used.
llvm-svn: 25366
2006-01-16 22:12:35 +01:00
|
|
|
break;
|
|
|
|
|
2007-04-01 09:35:23 +02:00
|
|
|
case Intrinsic::bswap:
|
2010-06-25 11:03:52 +02:00
|
|
|
CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getArgOperand(0), CI));
|
2006-01-16 08:57:00 +01:00
|
|
|
break;
|
2018-07-30 21:41:25 +02:00
|
|
|
|
2007-04-01 09:35:23 +02:00
|
|
|
case Intrinsic::ctlz:
|
2010-06-25 11:03:52 +02:00
|
|
|
CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getArgOperand(0), CI));
|
2005-05-03 19:19:30 +02:00
|
|
|
break;
|
2006-01-16 08:57:00 +01:00
|
|
|
|
2007-04-01 09:35:23 +02:00
|
|
|
case Intrinsic::cttz: {
|
2005-05-11 22:02:14 +02:00
|
|
|
// cttz(x) -> ctpop(~X & (X-1))
|
2010-06-25 11:03:52 +02:00
|
|
|
Value *Src = CI->getArgOperand(0);
|
2009-05-12 22:27:44 +02:00
|
|
|
Value *NotSrc = Builder.CreateNot(Src);
|
|
|
|
NotSrc->setName(Src->getName() + ".not");
|
2009-07-25 01:12:02 +02:00
|
|
|
Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
|
2009-05-12 22:27:44 +02:00
|
|
|
SrcM1 = Builder.CreateSub(Src, SrcM1);
|
2009-07-22 02:24:57 +02:00
|
|
|
Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
|
2005-05-03 19:19:30 +02:00
|
|
|
CI->replaceAllUsesWith(Src);
|
|
|
|
break;
|
|
|
|
}
|
2004-06-20 09:49:54 +02:00
|
|
|
|
2006-01-13 03:22:08 +01:00
|
|
|
case Intrinsic::stacksave:
|
2013-02-08 22:48:29 +01:00
|
|
|
case Intrinsic::stackrestore: {
|
2006-01-13 03:22:08 +01:00
|
|
|
if (!Warned)
|
2013-02-08 22:48:29 +01:00
|
|
|
errs() << "WARNING: this target does not support the llvm.stack"
|
|
|
|
<< (Callee->getIntrinsicID() == Intrinsic::stacksave ?
|
|
|
|
"save" : "restore") << " intrinsic.\n";
|
2006-01-13 03:22:08 +01:00
|
|
|
Warned = true;
|
2013-02-08 22:48:29 +01:00
|
|
|
if (Callee->getIntrinsicID() == Intrinsic::stacksave)
|
|
|
|
CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
|
2006-01-13 03:22:08 +01:00
|
|
|
break;
|
2013-02-08 22:48:29 +01:00
|
|
|
}
|
2018-07-30 21:41:25 +02:00
|
|
|
|
2015-12-01 12:40:55 +01:00
|
|
|
case Intrinsic::get_dynamic_area_offset:
|
|
|
|
errs() << "WARNING: this target does not support the custom llvm.get."
|
|
|
|
"dynamic.area.offset. It is being lowered to a constant 0\n";
|
|
|
|
// Just lower it to a constant 0 because for most targets
|
|
|
|
// @llvm.get.dynamic.area.offset is lowered to zero.
|
|
|
|
CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 0));
|
|
|
|
break;
|
2004-06-20 09:49:54 +02:00
|
|
|
case Intrinsic::returnaddress:
|
|
|
|
case Intrinsic::frameaddress:
|
2013-02-08 22:48:29 +01:00
|
|
|
errs() << "WARNING: this target does not support the llvm."
|
|
|
|
<< (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
|
|
|
|
"return" : "frame") << "address intrinsic.\n";
|
2016-10-13 00:13:19 +02:00
|
|
|
CI->replaceAllUsesWith(
|
|
|
|
ConstantPointerNull::get(cast<PointerType>(CI->getType())));
|
|
|
|
break;
|
|
|
|
case Intrinsic::addressofreturnaddress:
|
|
|
|
errs() << "WARNING: this target does not support the "
|
|
|
|
"llvm.addressofreturnaddress intrinsic.\n";
|
|
|
|
CI->replaceAllUsesWith(
|
|
|
|
ConstantPointerNull::get(cast<PointerType>(CI->getType())));
|
2004-06-20 09:49:54 +02:00
|
|
|
break;
|
|
|
|
|
2005-02-28 20:27:23 +01:00
|
|
|
case Intrinsic::prefetch:
|
|
|
|
break; // Simply strip out prefetches on unsupported architectures
|
|
|
|
|
2005-03-28 22:05:49 +02:00
|
|
|
case Intrinsic::pcmarker:
|
|
|
|
break; // Simply strip out pcmarker on unsupported architectures
|
2013-02-08 22:48:29 +01:00
|
|
|
case Intrinsic::readcyclecounter: {
|
|
|
|
errs() << "WARNING: this target does not support the llvm.readcyclecoun"
|
|
|
|
<< "ter intrinsic. It is being lowered to a constant 0\n";
|
2009-08-13 23:58:54 +02:00
|
|
|
CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
|
2005-11-11 17:47:30 +01:00
|
|
|
break;
|
2013-02-08 22:48:29 +01:00
|
|
|
}
|
2005-03-28 22:05:49 +02:00
|
|
|
|
2006-03-23 19:06:46 +01:00
|
|
|
case Intrinsic::dbg_declare:
|
[DebugInfo] Add DILabel metadata and intrinsic llvm.dbg.label.
In order to set breakpoints on labels and list source code around
labels, we need collect debug information for labels, i.e., label
name, the function label belong, line number in the file, and the
address label located. In order to keep these information in LLVM
IR and to allow backend to generate debug information correctly.
We create a new kind of metadata for labels, DILabel. The format
of DILabel is
!DILabel(scope: !1, name: "foo", file: !2, line: 3)
We hope to keep debug information as much as possible even the
code is optimized. So, we create a new kind of intrinsic for label
metadata to avoid the metadata is eliminated with basic block.
The intrinsic will keep existing if we keep it from optimized out.
The format of the intrinsic is
llvm.dbg.label(metadata !1)
It has only one argument, that is the DILabel metadata. The
intrinsic will follow the label immediately. Backend could get the
label metadata through the intrinsic's parameter.
We also create DIBuilder API for labels to be used by Frontend.
Frontend could use createLabel() to allocate DILabel objects, and use
insertLabel() to insert llvm.dbg.label intrinsic in LLVM IR.
Differential Revision: https://reviews.llvm.org/D45024
Patch by Hsiangkai Wang.
llvm-svn: 331841
2018-05-09 04:40:45 +02:00
|
|
|
case Intrinsic::dbg_label:
|
2007-07-06 16:46:23 +02:00
|
|
|
break; // Simply strip out debugging intrinsics
|
|
|
|
|
2009-10-14 18:11:37 +02:00
|
|
|
case Intrinsic::eh_typeid_for:
|
2007-07-06 16:46:23 +02:00
|
|
|
// Return something different to eh_selector.
|
2009-07-25 01:12:02 +02:00
|
|
|
CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
|
2007-07-06 16:46:23 +02:00
|
|
|
break;
|
2004-06-20 09:49:54 +02:00
|
|
|
|
2013-05-21 16:37:16 +02:00
|
|
|
case Intrinsic::annotation:
|
|
|
|
case Intrinsic::ptr_annotation:
|
|
|
|
// Just drop the annotation, but forward the value
|
|
|
|
CI->replaceAllUsesWith(CI->getOperand(0));
|
|
|
|
break;
|
|
|
|
|
2014-07-25 23:13:35 +02:00
|
|
|
case Intrinsic::assume:
|
2021-01-16 09:14:18 +01:00
|
|
|
case Intrinsic::experimental_noalias_scope_decl:
|
2007-06-16 00:26:58 +02:00
|
|
|
case Intrinsic::var_annotation:
|
2014-07-25 23:13:35 +02:00
|
|
|
break; // Strip out these intrinsics
|
2018-07-30 21:41:25 +02:00
|
|
|
|
2008-11-21 17:42:48 +01:00
|
|
|
case Intrinsic::memcpy: {
|
2014-02-18 16:33:12 +01:00
|
|
|
Type *IntPtr = DL.getIntPtrType(Context);
|
2010-06-25 11:03:52 +02:00
|
|
|
Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
|
2009-05-12 22:27:44 +02:00
|
|
|
/* isSigned */ false);
|
2007-02-06 20:06:38 +01:00
|
|
|
Value *Ops[3];
|
2010-06-25 11:03:52 +02:00
|
|
|
Ops[0] = CI->getArgOperand(0);
|
|
|
|
Ops[1] = CI->getArgOperand(1);
|
2007-02-06 20:06:38 +01:00
|
|
|
Ops[2] = Size;
|
2010-06-25 11:03:52 +02:00
|
|
|
ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
|
2004-06-20 09:49:54 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-11-21 17:42:48 +01:00
|
|
|
case Intrinsic::memmove: {
|
2014-02-18 16:33:12 +01:00
|
|
|
Type *IntPtr = DL.getIntPtrType(Context);
|
2010-06-25 11:03:52 +02:00
|
|
|
Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
|
2009-05-12 22:27:44 +02:00
|
|
|
/* isSigned */ false);
|
2007-02-06 20:06:38 +01:00
|
|
|
Value *Ops[3];
|
2010-06-25 11:03:52 +02:00
|
|
|
Ops[0] = CI->getArgOperand(0);
|
|
|
|
Ops[1] = CI->getArgOperand(1);
|
2007-02-06 20:06:38 +01:00
|
|
|
Ops[2] = Size;
|
2010-06-25 11:03:52 +02:00
|
|
|
ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
|
2007-02-06 20:06:38 +01:00
|
|
|
break;
|
2006-11-27 02:05:10 +01:00
|
|
|
}
|
2008-11-21 17:42:48 +01:00
|
|
|
case Intrinsic::memset: {
|
2013-11-10 05:46:57 +01:00
|
|
|
Value *Op0 = CI->getArgOperand(0);
|
2014-02-18 16:33:12 +01:00
|
|
|
Type *IntPtr = DL.getIntPtrType(Op0->getType());
|
2010-06-25 11:03:52 +02:00
|
|
|
Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
|
2009-05-12 22:27:44 +02:00
|
|
|
/* isSigned */ false);
|
2007-02-06 20:06:38 +01:00
|
|
|
Value *Ops[3];
|
2013-11-10 05:46:57 +01:00
|
|
|
Ops[0] = Op0;
|
2007-02-06 20:06:38 +01:00
|
|
|
// Extend the amount to i32.
|
2010-07-22 12:37:47 +02:00
|
|
|
Ops[1] = Builder.CreateIntCast(CI->getArgOperand(1),
|
|
|
|
Type::getInt32Ty(Context),
|
2009-05-12 22:27:44 +02:00
|
|
|
/* isSigned */ false);
|
2007-02-06 20:06:38 +01:00
|
|
|
Ops[2] = Size;
|
2010-06-25 11:03:52 +02:00
|
|
|
ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
|
2004-06-20 09:49:54 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-10-02 19:43:59 +02:00
|
|
|
case Intrinsic::sqrt: {
|
2009-06-26 22:33:47 +02:00
|
|
|
ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
|
2007-09-28 20:06:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-09-04 02:47:13 +02:00
|
|
|
case Intrinsic::log: {
|
2009-06-26 22:33:47 +02:00
|
|
|
ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
|
2008-09-04 02:47:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Intrinsic::log2: {
|
2009-06-26 22:33:47 +02:00
|
|
|
ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
|
2008-09-04 02:47:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Intrinsic::log10: {
|
2009-06-26 22:33:47 +02:00
|
|
|
ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
|
2008-09-04 02:47:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Intrinsic::exp: {
|
2009-06-26 22:33:47 +02:00
|
|
|
ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
|
2008-09-04 02:47:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Intrinsic::exp2: {
|
2009-06-26 22:33:47 +02:00
|
|
|
ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
|
2008-09-04 02:47:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Intrinsic::pow: {
|
2009-06-26 22:33:47 +02:00
|
|
|
ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
|
2008-09-04 02:47:13 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-08-08 17:00:12 +02:00
|
|
|
case Intrinsic::sin: {
|
|
|
|
ReplaceFPIntrinsicWithCall(CI, "sinf", "sin", "sinl");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Intrinsic::cos: {
|
|
|
|
ReplaceFPIntrinsicWithCall(CI, "cosf", "cos", "cosl");
|
|
|
|
break;
|
|
|
|
}
|
2014-08-30 20:33:35 +02:00
|
|
|
case Intrinsic::floor: {
|
|
|
|
ReplaceFPIntrinsicWithCall(CI, "floorf", "floor", "floorl");
|
|
|
|
break;
|
|
|
|
}
|
2014-08-08 17:00:12 +02:00
|
|
|
case Intrinsic::ceil: {
|
|
|
|
ReplaceFPIntrinsicWithCall(CI, "ceilf", "ceil", "ceill");
|
|
|
|
break;
|
|
|
|
}
|
2014-08-30 20:33:35 +02:00
|
|
|
case Intrinsic::trunc: {
|
|
|
|
ReplaceFPIntrinsicWithCall(CI, "truncf", "trunc", "truncl");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Intrinsic::round: {
|
|
|
|
ReplaceFPIntrinsicWithCall(CI, "roundf", "round", "roundl");
|
|
|
|
break;
|
|
|
|
}
|
2020-05-26 14:24:05 +02:00
|
|
|
case Intrinsic::roundeven: {
|
|
|
|
ReplaceFPIntrinsicWithCall(CI, "roundevenf", "roundeven", "roundevenl");
|
|
|
|
break;
|
|
|
|
}
|
2014-08-30 20:33:35 +02:00
|
|
|
case Intrinsic::copysign: {
|
|
|
|
ReplaceFPIntrinsicWithCall(CI, "copysignf", "copysign", "copysignl");
|
|
|
|
break;
|
|
|
|
}
|
2007-11-16 00:25:33 +01:00
|
|
|
case Intrinsic::flt_rounds:
|
|
|
|
// Lower to "round to the nearest"
|
2010-01-05 14:12:22 +01:00
|
|
|
if (!CI->getType()->isVoidTy())
|
2009-07-25 01:12:02 +02:00
|
|
|
CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
|
2007-11-16 00:25:33 +01:00
|
|
|
break;
|
2009-11-10 10:08:09 +01:00
|
|
|
case Intrinsic::invariant_start:
|
|
|
|
case Intrinsic::lifetime_start:
|
|
|
|
// Discard region information.
|
|
|
|
CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
|
|
|
|
break;
|
|
|
|
case Intrinsic::invariant_end:
|
|
|
|
case Intrinsic::lifetime_end:
|
|
|
|
// Discard region information.
|
|
|
|
break;
|
2004-06-20 09:49:54 +02:00
|
|
|
}
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2004-06-20 09:49:54 +02:00
|
|
|
assert(CI->use_empty() &&
|
|
|
|
"Lowering should have eliminated any uses of the intrinsic call!");
|
2005-05-11 21:42:05 +02:00
|
|
|
CI->eraseFromParent();
|
2004-06-20 09:49:54 +02:00
|
|
|
}
|
2011-01-08 02:24:27 +01:00
|
|
|
|
|
|
|
bool IntrinsicLowering::LowerToByteSwap(CallInst *CI) {
|
|
|
|
// Verify this is a simple bswap.
|
|
|
|
if (CI->getNumArgOperands() != 1 ||
|
|
|
|
CI->getType() != CI->getArgOperand(0)->getType() ||
|
|
|
|
!CI->getType()->isIntegerTy())
|
|
|
|
return false;
|
|
|
|
|
2011-07-12 16:06:48 +02:00
|
|
|
IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
|
2011-01-08 02:24:27 +01:00
|
|
|
if (!Ty)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Okay, we can do this xform, do so now.
|
2015-12-14 18:24:23 +01:00
|
|
|
Module *M = CI->getModule();
|
2019-02-01 21:43:25 +01:00
|
|
|
Function *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Ty);
|
2011-01-08 02:24:27 +01:00
|
|
|
|
|
|
|
Value *Op = CI->getArgOperand(0);
|
|
|
|
Op = CallInst::Create(Int, Op, CI->getName(), CI);
|
|
|
|
|
|
|
|
CI->replaceAllUsesWith(Op);
|
|
|
|
CI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|