blob: 56b1736b4156f4e4e40ea04acfca5f10d74895f7 [file] [log] [blame]
Chris Lattner3b66ecb2003-12-28 08:19:41 +00001//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3b66ecb2003-12-28 08:19:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3b66ecb2003-12-28 08:19:41 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerb71fd782006-11-15 18:00:10 +000010// This file implements the IntrinsicLowering class.
Chris Lattner3b66ecb2003-12-28 08:19:41 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnercf899082004-02-14 02:47:17 +000014#include "llvm/Constants.h"
Chris Lattner5fe51cc2004-02-12 17:01:09 +000015#include "llvm/DerivedTypes.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000016#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000018#include "llvm/Type.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
20#include "llvm/Support/Streams.h"
Reid Spencer6addf2c2007-01-29 17:42:06 +000021#include "llvm/Target/TargetData.h"
Chris Lattner990b8492007-02-13 06:01:22 +000022#include "llvm/ADT/SmallVector.h"
Owen Anderson718cb662007-09-07 04:06:50 +000023#include "llvm/ADT/STLExtras.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000024using namespace llvm;
25
Chris Lattner0979ca72004-05-09 04:29:57 +000026template <class ArgIt>
Chris Lattnerb76efb72007-01-07 08:12:01 +000027static void EnsureFunctionExists(Module &M, const char *Name,
28 ArgIt ArgBegin, ArgIt ArgEnd,
29 const Type *RetTy) {
30 // Insert a correctly-typed definition now.
Chris Lattner0979ca72004-05-09 04:29:57 +000031 std::vector<const Type *> ParamTys;
32 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
33 ParamTys.push_back(I->getType());
Chris Lattnerb76efb72007-01-07 08:12:01 +000034 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
Chris Lattner0979ca72004-05-09 04:29:57 +000035}
36
Chris Lattner556b4a62009-02-07 22:37:06 +000037static void EnsureFPIntrinsicsExist(Module &M, Function *Fn,
38 const char *FName,
39 const char *DName, const char *LDName) {
Dale Johannesenc4342ea2008-09-22 19:51:58 +000040 // Insert definitions for all the floating point types.
Chris Lattner556b4a62009-02-07 22:37:06 +000041 switch((int)Fn->arg_begin()->getType()->getTypeID()) {
Dale Johannesenc4342ea2008-09-22 19:51:58 +000042 case Type::FloatTyID:
Chris Lattner556b4a62009-02-07 22:37:06 +000043 EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(),
Dale Johannesenc4342ea2008-09-22 19:51:58 +000044 Type::FloatTy);
Chris Lattner556b4a62009-02-07 22:37:06 +000045 break;
Dale Johannesenc4342ea2008-09-22 19:51:58 +000046 case Type::DoubleTyID:
Chris Lattner556b4a62009-02-07 22:37:06 +000047 EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(),
Dale Johannesenc4342ea2008-09-22 19:51:58 +000048 Type::DoubleTy);
Chris Lattner556b4a62009-02-07 22:37:06 +000049 break;
Dale Johannesenc4342ea2008-09-22 19:51:58 +000050 case Type::X86_FP80TyID:
51 case Type::FP128TyID:
52 case Type::PPC_FP128TyID:
Chris Lattner556b4a62009-02-07 22:37:06 +000053 EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(),
54 Fn->arg_begin()->getType());
55 break;
Dale Johannesenc4342ea2008-09-22 19:51:58 +000056 }
57}
58
Chris Lattner588e72d2004-02-15 22:16:39 +000059/// ReplaceCallWith - This function is used when we want to lower an intrinsic
60/// call to a call of an external function. This handles hard cases such as
61/// when there was already a prototype for the external function, and if that
62/// prototype doesn't match the arguments we expect to pass in.
63template <class ArgIt>
64static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
Chris Lattnerb76efb72007-01-07 08:12:01 +000065 ArgIt ArgBegin, ArgIt ArgEnd,
66 const Type *RetTy, Constant *&FCache) {
Chris Lattner588e72d2004-02-15 22:16:39 +000067 if (!FCache) {
68 // If we haven't already looked up this function, check to see if the
69 // program already contains a function with this name.
70 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattnerb76efb72007-01-07 08:12:01 +000071 // Get or insert the definition now.
72 std::vector<const Type *> ParamTys;
73 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
74 ParamTys.push_back((*I)->getType());
75 FCache = M->getOrInsertFunction(NewFn,
76 FunctionType::get(RetTy, ParamTys, false));
Chris Lattner588e72d2004-02-15 22:16:39 +000077 }
Chris Lattner588e72d2004-02-15 22:16:39 +000078
David Greene52eec542007-08-01 03:43:44 +000079 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Gabor Greif051a9502008-04-06 20:25:17 +000080 CallInst *NewCI = CallInst::Create(FCache, Args.begin(), Args.end(),
81 CI->getName(), CI);
Chris Lattnerb76efb72007-01-07 08:12:01 +000082 if (!CI->use_empty())
83 CI->replaceAllUsesWith(NewCI);
Chris Lattner02348ca2004-06-11 02:54:02 +000084 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000085}
86
Chris Lattnerb71fd782006-11-15 18:00:10 +000087void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattner0979ca72004-05-09 04:29:57 +000088 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000089 if (I->isDeclaration() && !I->use_empty())
Chris Lattner0979ca72004-05-09 04:29:57 +000090 switch (I->getIntrinsicID()) {
91 default: break;
92 case Intrinsic::setjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000093 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Reid Spencer47857812006-12-31 05:55:36 +000094 Type::Int32Ty);
Chris Lattner0979ca72004-05-09 04:29:57 +000095 break;
96 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000097 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
98 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000099 break;
100 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +0000101 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
102 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +0000103 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000104 case Intrinsic::memcpy:
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000105 M.getOrInsertFunction("memcpy", PointerType::getUnqual(Type::Int8Ty),
106 PointerType::getUnqual(Type::Int8Ty),
107 PointerType::getUnqual(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +0000108 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000109 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000110 case Intrinsic::memmove:
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000111 M.getOrInsertFunction("memmove", PointerType::getUnqual(Type::Int8Ty),
112 PointerType::getUnqual(Type::Int8Ty),
113 PointerType::getUnqual(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +0000114 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000115 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000116 case Intrinsic::memset:
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000117 M.getOrInsertFunction("memset", PointerType::getUnqual(Type::Int8Ty),
118 PointerType::getUnqual(Type::Int8Ty),
119 Type::Int32Ty,
Reid Spencer6addf2c2007-01-29 17:42:06 +0000120 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000121 break;
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000122 case Intrinsic::sqrt:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000123 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000124 break;
Dan Gohmanc4c96602007-10-15 22:07:31 +0000125 case Intrinsic::sin:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000126 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000127 break;
128 case Intrinsic::cos:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000129 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000130 break;
131 case Intrinsic::pow:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000132 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000133 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000134 case Intrinsic::log:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000135 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000136 break;
137 case Intrinsic::log2:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000138 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000139 break;
140 case Intrinsic::log10:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000141 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000142 break;
143 case Intrinsic::exp:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000144 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000145 break;
146 case Intrinsic::exp2:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000147 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000148 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000149 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000150}
Chris Lattner588e72d2004-02-15 22:16:39 +0000151
Nate Begemane5981812006-01-16 07:57:00 +0000152/// LowerBSWAP - Emit the code to lower bswap of V before the specified
153/// instruction IP.
154static Value *LowerBSWAP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000155 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
Nate Begemane5981812006-01-16 07:57:00 +0000156
Nate Begemane5981812006-01-16 07:57:00 +0000157 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
158
159 switch(BitSize) {
160 default: assert(0 && "Unhandled type size of value to byteswap!");
161 case 16: {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000162 Value *Tmp1 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000163 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000164 Value *Tmp2 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000165 ConstantInt::get(V->getType(),8),"bswap.1",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000166 V = BinaryOperator::CreateOr(Tmp1, Tmp2, "bswap.i16", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000167 break;
168 }
169 case 32: {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000170 Value *Tmp4 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000171 ConstantInt::get(V->getType(),24),"bswap.4", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000172 Value *Tmp3 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000173 ConstantInt::get(V->getType(),8),"bswap.3",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000174 Value *Tmp2 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000175 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000176 Value *Tmp1 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000177 ConstantInt::get(V->getType(),24),"bswap.1", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000178 Tmp3 = BinaryOperator::CreateAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000179 ConstantInt::get(Type::Int32Ty, 0xFF0000),
Nate Begemane5981812006-01-16 07:57:00 +0000180 "bswap.and3", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000181 Tmp2 = BinaryOperator::CreateAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000182 ConstantInt::get(Type::Int32Ty, 0xFF00),
Nate Begemane5981812006-01-16 07:57:00 +0000183 "bswap.and2", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000184 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or1", IP);
185 Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or2", IP);
186 V = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.i32", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000187 break;
188 }
189 case 64: {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000190 Value *Tmp8 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000191 ConstantInt::get(V->getType(),56),"bswap.8", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000192 Value *Tmp7 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000193 ConstantInt::get(V->getType(),40),"bswap.7", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000194 Value *Tmp6 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000195 ConstantInt::get(V->getType(),24),"bswap.6", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000196 Value *Tmp5 = BinaryOperator::CreateShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000197 ConstantInt::get(V->getType(),8),"bswap.5", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000198 Value* Tmp4 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000199 ConstantInt::get(V->getType(),8),"bswap.4", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000200 Value* Tmp3 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000201 ConstantInt::get(V->getType(),24),"bswap.3", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000202 Value* Tmp2 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000203 ConstantInt::get(V->getType(),40),"bswap.2", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000204 Value* Tmp1 = BinaryOperator::CreateLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000205 ConstantInt::get(V->getType(),56),"bswap.1", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000206 Tmp7 = BinaryOperator::CreateAnd(Tmp7,
Reid Spencer47857812006-12-31 05:55:36 +0000207 ConstantInt::get(Type::Int64Ty,
Reid Spencerb83eb642006-10-20 07:07:24 +0000208 0xFF000000000000ULL),
209 "bswap.and7", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000210 Tmp6 = BinaryOperator::CreateAnd(Tmp6,
Reid Spencer47857812006-12-31 05:55:36 +0000211 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000212 "bswap.and6", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000213 Tmp5 = BinaryOperator::CreateAnd(Tmp5,
Reid Spencer47857812006-12-31 05:55:36 +0000214 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000215 "bswap.and5", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000216 Tmp4 = BinaryOperator::CreateAnd(Tmp4,
Reid Spencer47857812006-12-31 05:55:36 +0000217 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000218 "bswap.and4", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000219 Tmp3 = BinaryOperator::CreateAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000220 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000221 "bswap.and3", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000222 Tmp2 = BinaryOperator::CreateAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000223 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000224 "bswap.and2", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000225 Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp7, "bswap.or1", IP);
226 Tmp6 = BinaryOperator::CreateOr(Tmp6, Tmp5, "bswap.or2", IP);
227 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or3", IP);
228 Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or4", IP);
229 Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp6, "bswap.or5", IP);
230 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.or6", IP);
231 V = BinaryOperator::CreateOr(Tmp8, Tmp4, "bswap.i64", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000232 break;
233 }
234 }
Nate Begemane5981812006-01-16 07:57:00 +0000235 return V;
236}
237
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000238/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000239/// instruction IP.
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000240static Value *LowerCTPOP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000241 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000242
243 static const uint64_t MaskValues[6] = {
244 0x5555555555555555ULL, 0x3333333333333333ULL,
245 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
246 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
247 };
248
Chris Lattner98cf45b2005-05-11 20:24:12 +0000249 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng02031c02007-06-02 04:10:33 +0000250 unsigned WordSize = (BitSize + 63) / 64;
251 Value *Count = ConstantInt::get(V->getType(), 0);
Reid Spencer3822ff52006-11-08 06:47:33 +0000252
Zhou Sheng02031c02007-06-02 04:10:33 +0000253 for (unsigned n = 0; n < WordSize; ++n) {
254 Value *PartValue = V;
255 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
256 i <<= 1, ++ct) {
257 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000258 Value *LHS = BinaryOperator::CreateAnd(
Zhou Sheng02031c02007-06-02 04:10:33 +0000259 PartValue, MaskCst, "cppop.and1", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000260 Value *VShift = BinaryOperator::CreateLShr(PartValue,
Zhou Sheng02031c02007-06-02 04:10:33 +0000261 ConstantInt::get(V->getType(), i), "ctpop.sh", IP);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000262 Value *RHS = BinaryOperator::CreateAnd(VShift, MaskCst, "cppop.and2", IP);
263 PartValue = BinaryOperator::CreateAdd(LHS, RHS, "ctpop.step", IP);
Zhou Sheng02031c02007-06-02 04:10:33 +0000264 }
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000265 Count = BinaryOperator::CreateAdd(PartValue, Count, "ctpop.part", IP);
Zhou Sheng02031c02007-06-02 04:10:33 +0000266 if (BitSize > 64) {
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000267 V = BinaryOperator::CreateLShr(V, ConstantInt::get(V->getType(), 64),
Zhou Sheng02031c02007-06-02 04:10:33 +0000268 "ctpop.part.sh", IP);
269 BitSize -= 64;
270 }
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000271 }
272
Chris Lattner914ce452007-08-06 16:36:18 +0000273 return Count;
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000274}
275
Chris Lattner98cf45b2005-05-11 20:24:12 +0000276/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000277/// instruction IP.
Chris Lattner98cf45b2005-05-11 20:24:12 +0000278static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner98cf45b2005-05-11 20:24:12 +0000279
280 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng02031c02007-06-02 04:10:33 +0000281 for (unsigned i = 1; i < BitSize; i <<= 1) {
Reid Spencer832254e2007-02-02 02:16:23 +0000282 Value *ShVal = ConstantInt::get(V->getType(), i);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000283 ShVal = BinaryOperator::CreateLShr(V, ShVal, "ctlz.sh", IP);
284 V = BinaryOperator::CreateOr(V, ShVal, "ctlz.step", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000285 }
286
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000287 V = BinaryOperator::CreateNot(V, "", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000288 return LowerCTPOP(V, IP);
289}
290
Reid Spencerf75b8742007-04-12 02:48:46 +0000291/// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes
292/// three integer arguments. The first argument is the Value from which the
293/// bits will be selected. It may be of any bit width. The second and third
294/// arguments specify a range of bits to select with the second argument
295/// specifying the low bit and the third argument specifying the high bit. Both
296/// must be type i32. The result is the corresponding selected bits from the
297/// Value in the same width as the Value (first argument). If the low bit index
298/// is higher than the high bit index then the inverse selection is done and
299/// the bits are returned in inverse order.
300/// @brief Lowering of llvm.part.select intrinsic.
301static Instruction *LowerPartSelect(CallInst *CI) {
Reid Spenceraddd11d2007-04-04 23:48:25 +0000302 // Make sure we're dealing with a part select intrinsic here
303 Function *F = CI->getCalledFunction();
304 const FunctionType *FT = F->getFunctionType();
305 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
306 FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
307 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
308 return CI;
309
310 // Get the intrinsic implementation function by converting all the . to _
311 // in the intrinsic's function name and then reconstructing the function
312 // declaration.
313 std::string Name(F->getName());
314 for (unsigned i = 4; i < Name.length(); ++i)
315 if (Name[i] == '.')
316 Name[i] = '_';
317 Module* M = F->getParent();
318 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Duncan Sands667d4b82009-03-07 15:45:40 +0000319 F->setLinkage(GlobalValue::WeakAnyLinkage);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000320
321 // If we haven't defined the impl function yet, do so now
322 if (F->isDeclaration()) {
323
324 // Get the arguments to the function
Reid Spencereeedcb62007-04-12 13:30:14 +0000325 Function::arg_iterator args = F->arg_begin();
326 Value* Val = args++; Val->setName("Val");
327 Value* Lo = args++; Lo->setName("Lo");
Gabor Greif051a9502008-04-06 20:25:17 +0000328 Value* Hi = args++; Hi->setName("High");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000329
Reid Spencereeedcb62007-04-12 13:30:14 +0000330 // We want to select a range of bits here such that [Hi, Lo] is shifted
331 // down to the low bits. However, it is quite possible that Hi is smaller
332 // than Lo in which case the bits have to be reversed.
Reid Spenceraddd11d2007-04-04 23:48:25 +0000333
334 // Create the blocks we will need for the two cases (forward, reverse)
Gabor Greif051a9502008-04-06 20:25:17 +0000335 BasicBlock* CurBB = BasicBlock::Create("entry", F);
336 BasicBlock *RevSize = BasicBlock::Create("revsize", CurBB->getParent());
337 BasicBlock *FwdSize = BasicBlock::Create("fwdsize", CurBB->getParent());
338 BasicBlock *Compute = BasicBlock::Create("compute", CurBB->getParent());
339 BasicBlock *Reverse = BasicBlock::Create("reverse", CurBB->getParent());
340 BasicBlock *RsltBlk = BasicBlock::Create("result", CurBB->getParent());
Reid Spenceraddd11d2007-04-04 23:48:25 +0000341
Reid Spencereeedcb62007-04-12 13:30:14 +0000342 // Cast Hi and Lo to the size of Val so the widths are all the same
343 if (Hi->getType() != Val->getType())
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000344 Hi = CastInst::CreateIntegerCast(Hi, Val->getType(), false,
Reid Spenceraddd11d2007-04-04 23:48:25 +0000345 "tmp", CurBB);
Reid Spencereeedcb62007-04-12 13:30:14 +0000346 if (Lo->getType() != Val->getType())
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000347 Lo = CastInst::CreateIntegerCast(Lo, Val->getType(), false,
Reid Spenceraddd11d2007-04-04 23:48:25 +0000348 "tmp", CurBB);
349
350 // Compute a few things that both cases will need, up front.
351 Constant* Zero = ConstantInt::get(Val->getType(), 0);
352 Constant* One = ConstantInt::get(Val->getType(), 1);
353 Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
354
Reid Spencereeedcb62007-04-12 13:30:14 +0000355 // Compare the Hi and Lo bit positions. This is used to determine
Reid Spenceraddd11d2007-04-04 23:48:25 +0000356 // which case we have (forward or reverse)
Reid Spencereeedcb62007-04-12 13:30:14 +0000357 ICmpInst *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, Hi, Lo, "less",CurBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000358 BranchInst::Create(RevSize, FwdSize, Cmp, CurBB);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000359
360 // First, copmute the number of bits in the forward case.
361 Instruction* FBitSize =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000362 BinaryOperator::CreateSub(Hi, Lo,"fbits", FwdSize);
Gabor Greif051a9502008-04-06 20:25:17 +0000363 BranchInst::Create(Compute, FwdSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000364
365 // Second, compute the number of bits in the reverse case.
366 Instruction* RBitSize =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000367 BinaryOperator::CreateSub(Lo, Hi, "rbits", RevSize);
Gabor Greif051a9502008-04-06 20:25:17 +0000368 BranchInst::Create(Compute, RevSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000369
370 // Now, compute the bit range. Start by getting the bitsize and the shift
Reid Spencereeedcb62007-04-12 13:30:14 +0000371 // amount (either Hi or Lo) from PHI nodes. Then we compute a mask for
Reid Spenceraddd11d2007-04-04 23:48:25 +0000372 // the number of bits we want in the range. We shift the bits down to the
373 // least significant bits, apply the mask to zero out unwanted high bits,
374 // and we have computed the "forward" result. It may still need to be
375 // reversed.
376
377 // Get the BitSize from one of the two subtractions
Gabor Greif051a9502008-04-06 20:25:17 +0000378 PHINode *BitSize = PHINode::Create(Val->getType(), "bits", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000379 BitSize->reserveOperandSpace(2);
380 BitSize->addIncoming(FBitSize, FwdSize);
381 BitSize->addIncoming(RBitSize, RevSize);
382
Reid Spencereeedcb62007-04-12 13:30:14 +0000383 // Get the ShiftAmount as the smaller of Hi/Lo
Gabor Greif051a9502008-04-06 20:25:17 +0000384 PHINode *ShiftAmt = PHINode::Create(Val->getType(), "shiftamt", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000385 ShiftAmt->reserveOperandSpace(2);
Reid Spencereeedcb62007-04-12 13:30:14 +0000386 ShiftAmt->addIncoming(Lo, FwdSize);
387 ShiftAmt->addIncoming(Hi, RevSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000388
389 // Increment the bit size
390 Instruction *BitSizePlusOne =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000391 BinaryOperator::CreateAdd(BitSize, One, "bits", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000392
393 // Create a Mask to zero out the high order bits.
394 Instruction* Mask =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000395 BinaryOperator::CreateShl(AllOnes, BitSizePlusOne, "mask", Compute);
396 Mask = BinaryOperator::CreateNot(Mask, "mask", Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000397
398 // Shift the bits down and apply the mask
399 Instruction* FRes =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000400 BinaryOperator::CreateLShr(Val, ShiftAmt, "fres", Compute);
401 FRes = BinaryOperator::CreateAnd(FRes, Mask, "fres", Compute);
Gabor Greif051a9502008-04-06 20:25:17 +0000402 BranchInst::Create(Reverse, RsltBlk, Cmp, Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000403
404 // In the Reverse block we have the mask already in FRes but we must reverse
405 // it by shifting FRes bits right and putting them in RRes by shifting them
406 // in from left.
407
408 // First set up our loop counters
Gabor Greif051a9502008-04-06 20:25:17 +0000409 PHINode *Count = PHINode::Create(Val->getType(), "count", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000410 Count->reserveOperandSpace(2);
411 Count->addIncoming(BitSizePlusOne, Compute);
412
413 // Next, get the value that we are shifting.
Gabor Greif051a9502008-04-06 20:25:17 +0000414 PHINode *BitsToShift = PHINode::Create(Val->getType(), "val", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000415 BitsToShift->reserveOperandSpace(2);
416 BitsToShift->addIncoming(FRes, Compute);
417
418 // Finally, get the result of the last computation
Gabor Greif051a9502008-04-06 20:25:17 +0000419 PHINode *RRes = PHINode::Create(Val->getType(), "rres", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000420 RRes->reserveOperandSpace(2);
421 RRes->addIncoming(Zero, Compute);
422
423 // Decrement the counter
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000424 Instruction *Decr = BinaryOperator::CreateSub(Count, One, "decr", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000425 Count->addIncoming(Decr, Reverse);
426
427 // Compute the Bit that we want to move
428 Instruction *Bit =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000429 BinaryOperator::CreateAnd(BitsToShift, One, "bit", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000430
431 // Compute the new value for next iteration.
432 Instruction *NewVal =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000433 BinaryOperator::CreateLShr(BitsToShift, One, "rshift", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000434 BitsToShift->addIncoming(NewVal, Reverse);
435
436 // Shift the bit into the low bits of the result.
437 Instruction *NewRes =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000438 BinaryOperator::CreateShl(RRes, One, "lshift", Reverse);
439 NewRes = BinaryOperator::CreateOr(NewRes, Bit, "addbit", Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000440 RRes->addIncoming(NewRes, Reverse);
441
442 // Terminate loop if we've moved all the bits.
443 ICmpInst *Cond =
444 new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "cond", Reverse);
Gabor Greif051a9502008-04-06 20:25:17 +0000445 BranchInst::Create(RsltBlk, Reverse, Cond, Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000446
447 // Finally, in the result block, select one of the two results with a PHI
448 // node and return the result;
449 CurBB = RsltBlk;
Gabor Greif051a9502008-04-06 20:25:17 +0000450 PHINode *BitSelect = PHINode::Create(Val->getType(), "part_select", CurBB);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000451 BitSelect->reserveOperandSpace(2);
452 BitSelect->addIncoming(FRes, Compute);
453 BitSelect->addIncoming(NewRes, Reverse);
Gabor Greif051a9502008-04-06 20:25:17 +0000454 ReturnInst::Create(BitSelect, CurBB);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000455 }
456
457 // Return a call to the implementation function
Reid Spencer5156f5b2007-05-12 11:07:40 +0000458 Value *Args[] = {
459 CI->getOperand(1),
460 CI->getOperand(2),
461 CI->getOperand(3)
462 };
Gabor Greif051a9502008-04-06 20:25:17 +0000463 return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000464}
465
Reid Spencerf75b8742007-04-12 02:48:46 +0000466/// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes
467/// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High)
468/// The first two arguments can be any bit width. The result is the same width
469/// as %Value. The operation replaces bits between %Low and %High with the value
470/// in %Replacement. If %Replacement is not the same width, it is truncated or
471/// zero extended as appropriate to fit the bits being replaced. If %Low is
472/// greater than %High then the inverse set of bits are replaced.
473/// @brief Lowering of llvm.bit.part.set intrinsic.
474static Instruction *LowerPartSet(CallInst *CI) {
475 // Make sure we're dealing with a part select intrinsic here
476 Function *F = CI->getCalledFunction();
477 const FunctionType *FT = F->getFunctionType();
478 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
479 FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() ||
480 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() ||
481 !FT->getParamType(3)->isInteger())
482 return CI;
483
484 // Get the intrinsic implementation function by converting all the . to _
485 // in the intrinsic's function name and then reconstructing the function
486 // declaration.
487 std::string Name(F->getName());
488 for (unsigned i = 4; i < Name.length(); ++i)
489 if (Name[i] == '.')
490 Name[i] = '_';
491 Module* M = F->getParent();
492 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Duncan Sands667d4b82009-03-07 15:45:40 +0000493 F->setLinkage(GlobalValue::WeakAnyLinkage);
Reid Spencerf75b8742007-04-12 02:48:46 +0000494
495 // If we haven't defined the impl function yet, do so now
496 if (F->isDeclaration()) {
Reid Spencerf75b8742007-04-12 02:48:46 +0000497 // Get the arguments for the function.
498 Function::arg_iterator args = F->arg_begin();
499 Value* Val = args++; Val->setName("Val");
500 Value* Rep = args++; Rep->setName("Rep");
501 Value* Lo = args++; Lo->setName("Lo");
502 Value* Hi = args++; Hi->setName("Hi");
503
504 // Get some types we need
505 const IntegerType* ValTy = cast<IntegerType>(Val->getType());
506 const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
507 uint32_t ValBits = ValTy->getBitWidth();
508 uint32_t RepBits = RepTy->getBitWidth();
509
510 // Constant Definitions
511 ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
512 ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
513 ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
Reid Spencer76c94b62007-05-15 02:26:52 +0000514 ConstantInt* One = ConstantInt::get(Type::Int32Ty, 1);
515 ConstantInt* ValOne = ConstantInt::get(ValTy, 1);
516 ConstantInt* Zero = ConstantInt::get(Type::Int32Ty, 0);
517 ConstantInt* ValZero = ConstantInt::get(ValTy, 0);
Reid Spencerf75b8742007-04-12 02:48:46 +0000518
Reid Spencer76c94b62007-05-15 02:26:52 +0000519 // Basic blocks we fill in below.
Gabor Greif051a9502008-04-06 20:25:17 +0000520 BasicBlock* entry = BasicBlock::Create("entry", F, 0);
521 BasicBlock* large = BasicBlock::Create("large", F, 0);
522 BasicBlock* small = BasicBlock::Create("small", F, 0);
523 BasicBlock* reverse = BasicBlock::Create("reverse", F, 0);
524 BasicBlock* result = BasicBlock::Create("result", F, 0);
Reid Spencerf75b8742007-04-12 02:48:46 +0000525
Reid Spencer76c94b62007-05-15 02:26:52 +0000526 // BASIC BLOCK: entry
Reid Spencereeedcb62007-04-12 13:30:14 +0000527 // First, get the number of bits that we're placing as an i32
528 ICmpInst* is_forward =
529 new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry);
Gabor Greif051a9502008-04-06 20:25:17 +0000530 SelectInst* Hi_pn = SelectInst::Create(is_forward, Hi, Lo, "", entry);
531 SelectInst* Lo_pn = SelectInst::Create(is_forward, Lo, Hi, "", entry);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000532 BinaryOperator* NumBits = BinaryOperator::CreateSub(Hi_pn, Lo_pn, "",entry);
533 NumBits = BinaryOperator::CreateAdd(NumBits, One, "", entry);
Reid Spencereeedcb62007-04-12 13:30:14 +0000534 // Now, convert Lo and Hi to ValTy bit width
Reid Spencerf75b8742007-04-12 02:48:46 +0000535 if (ValBits > 32) {
Reid Spencer76c94b62007-05-15 02:26:52 +0000536 Lo = new ZExtInst(Lo_pn, ValTy, "", entry);
Reid Spencerf75b8742007-04-12 02:48:46 +0000537 } else if (ValBits < 32) {
Reid Spencer76c94b62007-05-15 02:26:52 +0000538 Lo = new TruncInst(Lo_pn, ValTy, "", entry);
Zhou Shengc0425b62009-01-30 09:44:49 +0000539 } else {
Zhou Sheng904ebf92009-01-30 09:02:50 +0000540 Lo = Lo_pn;
Zhou Shengc0425b62009-01-30 09:44:49 +0000541 }
Reid Spencereeedcb62007-04-12 13:30:14 +0000542 // Determine if the replacement bits are larger than the number of bits we
543 // are replacing and deal with it.
Reid Spencerf75b8742007-04-12 02:48:46 +0000544 ICmpInst* is_large =
545 new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry);
Gabor Greif051a9502008-04-06 20:25:17 +0000546 BranchInst::Create(large, small, is_large, entry);
Reid Spencerf75b8742007-04-12 02:48:46 +0000547
Reid Spencer76c94b62007-05-15 02:26:52 +0000548 // BASIC BLOCK: large
Reid Spencer9a9203b2007-04-16 22:21:14 +0000549 Instruction* MaskBits =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000550 BinaryOperator::CreateSub(RepBitWidth, NumBits, "", large);
551 MaskBits = CastInst::CreateIntegerCast(MaskBits, RepMask->getType(),
Reid Spencer9a9203b2007-04-16 22:21:14 +0000552 false, "", large);
Reid Spencerf75b8742007-04-12 02:48:46 +0000553 BinaryOperator* Mask1 =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000554 BinaryOperator::CreateLShr(RepMask, MaskBits, "", large);
555 BinaryOperator* Rep2 = BinaryOperator::CreateAnd(Mask1, Rep, "", large);
Gabor Greif051a9502008-04-06 20:25:17 +0000556 BranchInst::Create(small, large);
Reid Spencerf75b8742007-04-12 02:48:46 +0000557
Reid Spencer76c94b62007-05-15 02:26:52 +0000558 // BASIC BLOCK: small
Gabor Greif051a9502008-04-06 20:25:17 +0000559 PHINode* Rep3 = PHINode::Create(RepTy, "", small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000560 Rep3->reserveOperandSpace(2);
Reid Spencereeedcb62007-04-12 13:30:14 +0000561 Rep3->addIncoming(Rep2, large);
Reid Spencerf75b8742007-04-12 02:48:46 +0000562 Rep3->addIncoming(Rep, entry);
Reid Spencer37958092007-04-12 12:46:33 +0000563 Value* Rep4 = Rep3;
564 if (ValBits > RepBits)
565 Rep4 = new ZExtInst(Rep3, ValTy, "", small);
566 else if (ValBits < RepBits)
567 Rep4 = new TruncInst(Rep3, ValTy, "", small);
Gabor Greif051a9502008-04-06 20:25:17 +0000568 BranchInst::Create(result, reverse, is_forward, small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000569
Reid Spencer76c94b62007-05-15 02:26:52 +0000570 // BASIC BLOCK: reverse (reverses the bits of the replacement)
571 // Set up our loop counter as a PHI so we can decrement on each iteration.
572 // We will loop for the number of bits in the replacement value.
Gabor Greif051a9502008-04-06 20:25:17 +0000573 PHINode *Count = PHINode::Create(Type::Int32Ty, "count", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000574 Count->reserveOperandSpace(2);
575 Count->addIncoming(NumBits, small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000576
Reid Spencer76c94b62007-05-15 02:26:52 +0000577 // Get the value that we are shifting bits out of as a PHI because
578 // we'll change this with each iteration.
Gabor Greif051a9502008-04-06 20:25:17 +0000579 PHINode *BitsToShift = PHINode::Create(Val->getType(), "val", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000580 BitsToShift->reserveOperandSpace(2);
581 BitsToShift->addIncoming(Rep4, small);
582
583 // Get the result of the last computation or zero on first iteration
Gabor Greif051a9502008-04-06 20:25:17 +0000584 PHINode *RRes = PHINode::Create(Val->getType(), "rres", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000585 RRes->reserveOperandSpace(2);
586 RRes->addIncoming(ValZero, small);
587
588 // Decrement the loop counter by one
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000589 Instruction *Decr = BinaryOperator::CreateSub(Count, One, "", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000590 Count->addIncoming(Decr, reverse);
591
592 // Get the bit that we want to move into the result
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000593 Value *Bit = BinaryOperator::CreateAnd(BitsToShift, ValOne, "", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000594
595 // Compute the new value of the bits to shift for the next iteration.
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000596 Value *NewVal = BinaryOperator::CreateLShr(BitsToShift, ValOne,"", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000597 BitsToShift->addIncoming(NewVal, reverse);
598
599 // Shift the bit we extracted into the low bit of the result.
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000600 Instruction *NewRes = BinaryOperator::CreateShl(RRes, ValOne, "", reverse);
601 NewRes = BinaryOperator::CreateOr(NewRes, Bit, "", reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000602 RRes->addIncoming(NewRes, reverse);
603
604 // Terminate loop if we've moved all the bits.
605 ICmpInst *Cond = new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "", reverse);
Gabor Greif051a9502008-04-06 20:25:17 +0000606 BranchInst::Create(result, reverse, Cond, reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000607
608 // BASIC BLOCK: result
Gabor Greif051a9502008-04-06 20:25:17 +0000609 PHINode *Rplcmnt = PHINode::Create(Val->getType(), "", result);
Reid Spencer76c94b62007-05-15 02:26:52 +0000610 Rplcmnt->reserveOperandSpace(2);
611 Rplcmnt->addIncoming(NewRes, reverse);
612 Rplcmnt->addIncoming(Rep4, small);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000613 Value* t0 = CastInst::CreateIntegerCast(NumBits,ValTy,false,"",result);
614 Value* t1 = BinaryOperator::CreateShl(ValMask, Lo, "", result);
615 Value* t2 = BinaryOperator::CreateNot(t1, "", result);
616 Value* t3 = BinaryOperator::CreateShl(t1, t0, "", result);
617 Value* t4 = BinaryOperator::CreateOr(t2, t3, "", result);
618 Value* t5 = BinaryOperator::CreateAnd(t4, Val, "", result);
619 Value* t6 = BinaryOperator::CreateShl(Rplcmnt, Lo, "", result);
620 Value* Rslt = BinaryOperator::CreateOr(t5, t6, "part_set", result);
Gabor Greif051a9502008-04-06 20:25:17 +0000621 ReturnInst::Create(Rslt, result);
Reid Spencerf75b8742007-04-12 02:48:46 +0000622 }
623
624 // Return a call to the implementation function
Reid Spencer5156f5b2007-05-12 11:07:40 +0000625 Value *Args[] = {
626 CI->getOperand(1),
627 CI->getOperand(2),
628 CI->getOperand(3),
629 CI->getOperand(4)
630 };
Gabor Greif051a9502008-04-06 20:25:17 +0000631 return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI);
Reid Spencerf75b8742007-04-12 02:48:46 +0000632}
633
Dale Johannesenf74185b2008-09-22 20:51:30 +0000634static void ReplaceFPIntrinsicWithCall(CallInst *CI, Constant *FCache,
635 Constant *DCache, Constant *LDCache,
636 const char *Fname, const char *Dname,
637 const char *LDname) {
638 switch (CI->getOperand(1)->getType()->getTypeID()) {
639 default: assert(0 && "Invalid type in intrinsic"); abort();
640 case Type::FloatTyID:
641 ReplaceCallWith(Fname, CI, CI->op_begin()+1, CI->op_end(),
642 Type::FloatTy, FCache);
643 break;
644 case Type::DoubleTyID:
645 ReplaceCallWith(Dname, CI, CI->op_begin()+1, CI->op_end(),
646 Type::DoubleTy, DCache);
647 break;
648 case Type::X86_FP80TyID:
649 case Type::FP128TyID:
650 case Type::PPC_FP128TyID:
651 ReplaceCallWith(LDname, CI, CI->op_begin()+1, CI->op_end(),
652 CI->getOperand(1)->getType(), LDCache);
653 break;
654 }
655}
Reid Spenceraddd11d2007-04-04 23:48:25 +0000656
Chris Lattnerb71fd782006-11-15 18:00:10 +0000657void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000658 Function *Callee = CI->getCalledFunction();
659 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000660
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000661 switch (Callee->getIntrinsicID()) {
662 case Intrinsic::not_intrinsic:
Bill Wendlinge8156192006-12-07 01:30:32 +0000663 cerr << "Cannot lower a call to a non-intrinsic function '"
664 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000665 abort();
666 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000667 cerr << "Error: Code generator does not support intrinsic function '"
668 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000669 abort();
670
Chris Lattner588e72d2004-02-15 22:16:39 +0000671 // The setjmp/longjmp intrinsics should only exist in the code if it was
672 // never optimized (ie, right out of the CFE), or if it has been hacked on
673 // by the lowerinvoke pass. In both cases, the right thing to do is to
674 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000675 case Intrinsic::setjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000676 static Constant *SetjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000677 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000678 Type::Int32Ty, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000679 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000680 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000681 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000682 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000683 case Intrinsic::sigsetjmp:
Chris Lattner9b700f72004-02-15 22:24:51 +0000684 if (CI->getType() != Type::VoidTy)
685 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
686 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000687
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000688 case Intrinsic::longjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000689 static Constant *LongjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000690 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000691 Type::VoidTy, LongjmpFCache);
Chris Lattner9b700f72004-02-15 22:24:51 +0000692 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000693 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000694
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000695 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000696 // Insert the call to abort
Chris Lattnerb76efb72007-01-07 08:12:01 +0000697 static Constant *AbortFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000698 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000699 Type::VoidTy, AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000700 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000701 }
Reid Spencere9391fd2007-04-01 07:35:23 +0000702 case Intrinsic::ctpop:
Reid Spencer0b118202006-01-16 21:12:35 +0000703 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
704 break;
705
Reid Spencere9391fd2007-04-01 07:35:23 +0000706 case Intrinsic::bswap:
Nate Begemane5981812006-01-16 07:57:00 +0000707 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
708 break;
709
Reid Spencere9391fd2007-04-01 07:35:23 +0000710 case Intrinsic::ctlz:
Chris Lattner98cf45b2005-05-11 20:24:12 +0000711 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000712 break;
Nate Begemane5981812006-01-16 07:57:00 +0000713
Reid Spencere9391fd2007-04-01 07:35:23 +0000714 case Intrinsic::cttz: {
Chris Lattnera8011722005-05-11 20:02:14 +0000715 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000716 Value *Src = CI->getOperand(1);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000717 Value *NotSrc = BinaryOperator::CreateNot(Src, Src->getName()+".not", CI);
Gabor Greif051a9502008-04-06 20:25:17 +0000718 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000719 SrcM1 = BinaryOperator::CreateSub(Src, SrcM1, "", CI);
720 Src = LowerCTPOP(BinaryOperator::CreateAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000721 CI->replaceAllUsesWith(Src);
722 break;
723 }
Chris Lattner77b13302004-01-05 05:36:30 +0000724
Chris Lattnerc6eb6d72007-04-10 03:20:39 +0000725 case Intrinsic::part_select:
Reid Spencerf75b8742007-04-12 02:48:46 +0000726 CI->replaceAllUsesWith(LowerPartSelect(CI));
727 break;
728
729 case Intrinsic::part_set:
730 CI->replaceAllUsesWith(LowerPartSet(CI));
Reid Spenceraddd11d2007-04-04 23:48:25 +0000731 break;
732
Chris Lattner0c067bc2006-01-13 02:22:08 +0000733 case Intrinsic::stacksave:
734 case Intrinsic::stackrestore: {
735 static bool Warned = false;
736 if (!Warned)
Bill Wendlinge8156192006-12-07 01:30:32 +0000737 cerr << "WARNING: this target does not support the llvm.stack"
738 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
739 "save" : "restore") << " intrinsic.\n";
Chris Lattner0c067bc2006-01-13 02:22:08 +0000740 Warned = true;
741 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
742 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
743 break;
744 }
745
Chris Lattnercf899082004-02-14 02:47:17 +0000746 case Intrinsic::returnaddress:
747 case Intrinsic::frameaddress:
Bill Wendlinge8156192006-12-07 01:30:32 +0000748 cerr << "WARNING: this target does not support the llvm."
749 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
750 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000751 CI->replaceAllUsesWith(ConstantPointerNull::get(
752 cast<PointerType>(CI->getType())));
753 break;
754
Chris Lattner0942b7c2005-02-28 19:27:23 +0000755 case Intrinsic::prefetch:
756 break; // Simply strip out prefetches on unsupported architectures
757
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000758 case Intrinsic::pcmarker:
759 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000760 case Intrinsic::readcyclecounter: {
Bill Wendlinge8156192006-12-07 01:30:32 +0000761 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
762 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencer47857812006-12-31 05:55:36 +0000763 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000764 break;
765 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000766
Chris Lattner77b13302004-01-05 05:36:30 +0000767 case Intrinsic::dbg_stoppoint:
768 case Intrinsic::dbg_region_start:
769 case Intrinsic::dbg_region_end:
770 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000771 case Intrinsic::dbg_declare:
Duncan Sandsf664e412007-07-06 14:46:23 +0000772 break; // Simply strip out debugging intrinsics
773
Jim Laskeyb180aa12007-02-21 22:53:45 +0000774 case Intrinsic::eh_exception:
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000775 case Intrinsic::eh_selector_i32:
776 case Intrinsic::eh_selector_i64:
Duncan Sandsf664e412007-07-06 14:46:23 +0000777 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
778 break;
779
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000780 case Intrinsic::eh_typeid_for_i32:
781 case Intrinsic::eh_typeid_for_i64:
Duncan Sandsf664e412007-07-06 14:46:23 +0000782 // Return something different to eh_selector.
783 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
784 break;
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000785
Tanya Lattner24e5aad2007-06-15 22:26:58 +0000786 case Intrinsic::var_annotation:
787 break; // Strip out annotate intrinsic
788
Chris Lattner824b9582008-11-21 16:42:48 +0000789 case Intrinsic::memcpy: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000790 static Constant *MemcpyFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000791 Value *Size = CI->getOperand(3);
792 const Type *IntPtr = TD.getIntPtrType();
793 if (Size->getType()->getPrimitiveSizeInBits() <
794 IntPtr->getPrimitiveSizeInBits())
795 Size = new ZExtInst(Size, IntPtr, "", CI);
796 else if (Size->getType()->getPrimitiveSizeInBits() >
797 IntPtr->getPrimitiveSizeInBits())
798 Size = new TruncInst(Size, IntPtr, "", CI);
799 Value *Ops[3];
800 Ops[0] = CI->getOperand(1);
801 Ops[1] = CI->getOperand(2);
802 Ops[2] = Size;
803 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
804 MemcpyFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000805 break;
806 }
Chris Lattner824b9582008-11-21 16:42:48 +0000807 case Intrinsic::memmove: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000808 static Constant *MemmoveFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000809 Value *Size = CI->getOperand(3);
810 const Type *IntPtr = TD.getIntPtrType();
811 if (Size->getType()->getPrimitiveSizeInBits() <
812 IntPtr->getPrimitiveSizeInBits())
813 Size = new ZExtInst(Size, IntPtr, "", CI);
814 else if (Size->getType()->getPrimitiveSizeInBits() >
815 IntPtr->getPrimitiveSizeInBits())
816 Size = new TruncInst(Size, IntPtr, "", CI);
817 Value *Ops[3];
818 Ops[0] = CI->getOperand(1);
819 Ops[1] = CI->getOperand(2);
820 Ops[2] = Size;
821 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
822 MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000823 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000824 }
Chris Lattner824b9582008-11-21 16:42:48 +0000825 case Intrinsic::memset: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000826 static Constant *MemsetFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000827 Value *Size = CI->getOperand(3);
Chris Lattner7d6f77d2007-02-06 06:07:51 +0000828 const Type *IntPtr = TD.getIntPtrType();
829 if (Size->getType()->getPrimitiveSizeInBits() <
830 IntPtr->getPrimitiveSizeInBits())
831 Size = new ZExtInst(Size, IntPtr, "", CI);
832 else if (Size->getType()->getPrimitiveSizeInBits() >
833 IntPtr->getPrimitiveSizeInBits())
834 Size = new TruncInst(Size, IntPtr, "", CI);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000835 Value *Ops[3];
836 Ops[0] = CI->getOperand(1);
837 // Extend the amount to i32.
838 Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI);
839 Ops[2] = Size;
840 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
841 MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000842 break;
843 }
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000844 case Intrinsic::sqrt: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000845 static Constant *sqrtFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000846 static Constant *sqrtDCache = 0;
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000847 static Constant *sqrtLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000848 ReplaceFPIntrinsicWithCall(CI, sqrtFCache, sqrtDCache, sqrtLDCache,
849 "sqrtf", "sqrt", "sqrtl");
Dale Johannesen4292d1c2007-09-28 18:06:58 +0000850 break;
851 }
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000852 case Intrinsic::log: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000853 static Constant *logFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000854 static Constant *logDCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000855 static Constant *logLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000856 ReplaceFPIntrinsicWithCall(CI, logFCache, logDCache, logLDCache,
857 "logf", "log", "logl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000858 break;
859 }
860 case Intrinsic::log2: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000861 static Constant *log2FCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000862 static Constant *log2DCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000863 static Constant *log2LDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000864 ReplaceFPIntrinsicWithCall(CI, log2FCache, log2DCache, log2LDCache,
865 "log2f", "log2", "log2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000866 break;
867 }
868 case Intrinsic::log10: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000869 static Constant *log10FCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000870 static Constant *log10DCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000871 static Constant *log10LDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000872 ReplaceFPIntrinsicWithCall(CI, log10FCache, log10DCache, log10LDCache,
873 "log10f", "log10", "log10l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000874 break;
875 }
876 case Intrinsic::exp: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000877 static Constant *expFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000878 static Constant *expDCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000879 static Constant *expLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000880 ReplaceFPIntrinsicWithCall(CI, expFCache, expDCache, expLDCache,
881 "expf", "exp", "expl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000882 break;
883 }
884 case Intrinsic::exp2: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000885 static Constant *exp2FCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000886 static Constant *exp2DCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000887 static Constant *exp2LDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000888 ReplaceFPIntrinsicWithCall(CI, exp2FCache, exp2DCache, exp2LDCache,
889 "exp2f", "exp2", "exp2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000890 break;
891 }
892 case Intrinsic::pow: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000893 static Constant *powFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000894 static Constant *powDCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000895 static Constant *powLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000896 ReplaceFPIntrinsicWithCall(CI, powFCache, powDCache, powLDCache,
897 "powf", "pow", "powl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000898 break;
899 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +0000900 case Intrinsic::flt_rounds:
901 // Lower to "round to the nearest"
902 if (CI->getType() != Type::VoidTy)
903 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
904 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000905 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000906
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000907 assert(CI->use_empty() &&
908 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000909 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000910}